* readelf.c (dump_section_as_strings): Use %6tx instead of %6zx.
[binutils.git] / gold / object.h
blobcc1d5b2f8a70218695f6fc568ea41b5ac96fe836
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #ifndef GOLD_OBJECT_H
24 #define GOLD_OBJECT_H
26 #include <string>
27 #include <vector>
29 #include "elfcpp.h"
30 #include "elfcpp_file.h"
31 #include "fileread.h"
32 #include "target.h"
34 namespace gold
37 class General_options;
38 class Layout;
39 class Output_section;
40 class Output_file;
41 class Dynobj;
43 template<typename Stringpool_char>
44 class Stringpool_template;
46 // Data to pass from read_symbols() to add_symbols().
48 struct Read_symbols_data
50 // Section headers.
51 File_view* section_headers;
52 // Section names.
53 File_view* section_names;
54 // Size of section name data in bytes.
55 off_t section_names_size;
56 // Symbol data.
57 File_view* symbols;
58 // Size of symbol data in bytes.
59 off_t symbols_size;
60 // Symbol names.
61 File_view* symbol_names;
62 // Size of symbol name data in bytes.
63 off_t symbol_names_size;
65 // Version information. This is only used on dynamic objects.
66 // Version symbol data (from SHT_GNU_versym section).
67 File_view* versym;
68 off_t versym_size;
69 // Version definition data (from SHT_GNU_verdef section).
70 File_view* verdef;
71 off_t verdef_size;
72 unsigned int verdef_info;
73 // Needed version data (from SHT_GNU_verneed section).
74 File_view* verneed;
75 off_t verneed_size;
76 unsigned int verneed_info;
79 // Data about a single relocation section. This is read in
80 // read_relocs and processed in scan_relocs.
82 struct Section_relocs
84 // Index of reloc section.
85 unsigned int reloc_shndx;
86 // Index of section that relocs apply to.
87 unsigned int data_shndx;
88 // Contents of reloc section.
89 File_view* contents;
90 // Reloc section type.
91 unsigned int sh_type;
92 // Number of reloc entries.
93 size_t reloc_count;
96 // Relocations in an object file. This is read in read_relocs and
97 // processed in scan_relocs.
99 struct Read_relocs_data
101 typedef std::vector<Section_relocs> Relocs_list;
102 // The relocations.
103 Relocs_list relocs;
104 // The local symbols.
105 File_view* local_symbols;
108 // Object is an abstract base class which represents either a 32-bit
109 // or a 64-bit input object. This can be a regular object file
110 // (ET_REL) or a shared object (ET_DYN).
112 class Object
114 public:
115 // NAME is the name of the object as we would report it to the user
116 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
117 // used to read the file. OFFSET is the offset within the input
118 // file--0 for a .o or .so file, something else for a .a file.
119 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
120 off_t offset = 0)
121 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
122 is_dynamic_(is_dynamic), target_(NULL)
125 virtual ~Object()
128 // Return the name of the object as we would report it to the tuser.
129 const std::string&
130 name() const
131 { return this->name_; }
133 // Return whether this is a dynamic object.
134 bool
135 is_dynamic() const
136 { return this->is_dynamic_; }
138 // Return the target structure associated with this object.
139 Target*
140 target() const
141 { return this->target_; }
143 // Lock the underlying file.
144 void
145 lock()
146 { this->input_file_->file().lock(); }
148 // Unlock the underlying file.
149 void
150 unlock()
151 { this->input_file_->file().unlock(); }
153 // Return whether the underlying file is locked.
154 bool
155 is_locked() const
156 { return this->input_file_->file().is_locked(); }
158 // Return the sized target structure associated with this object.
159 // This is like the target method but it returns a pointer of
160 // appropriate checked type.
161 template<int size, bool big_endian>
162 Sized_target<size, big_endian>*
163 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
165 // Get the number of sections.
166 unsigned int
167 shnum() const
168 { return this->shnum_; }
170 // Return a view of the contents of a section. Set *PLEN to the
171 // size. CACHE is a hint as in File_read::get_view.
172 const unsigned char*
173 section_contents(unsigned int shndx, off_t* plen, bool cache);
175 // Return the name of a section given a section index. This is only
176 // used for error messages.
177 std::string
178 section_name(unsigned int shndx)
179 { return this->do_section_name(shndx); }
181 // Return the section flags given a section index.
182 uint64_t
183 section_flags(unsigned int shndx)
184 { return this->do_section_flags(shndx); }
186 // Read the symbol information.
187 void
188 read_symbols(Read_symbols_data* sd)
189 { return this->do_read_symbols(sd); }
191 // Pass sections which should be included in the link to the Layout
192 // object, and record where the sections go in the output file.
193 void
194 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
195 { this->do_layout(symtab, layout, sd); }
197 // Add symbol information to the global symbol table.
198 void
199 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
200 { this->do_add_symbols(symtab, sd); }
202 // Functions and types for the elfcpp::Elf_file interface. This
203 // permit us to use Object as the File template parameter for
204 // elfcpp::Elf_file.
206 // The View class is returned by view. It must support a single
207 // method, data(). This is trivial, because get_view does what we
208 // need.
209 class View
211 public:
212 View(const unsigned char* p)
213 : p_(p)
216 const unsigned char*
217 data() const
218 { return this->p_; }
220 private:
221 const unsigned char* p_;
224 // Return a View.
225 View
226 view(off_t file_offset, off_t data_size)
227 { return View(this->get_view(file_offset, data_size, true)); }
229 // Report an error.
230 void
231 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
233 // A location in the file.
234 struct Location
236 off_t file_offset;
237 off_t data_size;
239 Location(off_t fo, off_t ds)
240 : file_offset(fo), data_size(ds)
244 // Get a View given a Location.
245 View view(Location loc)
246 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
248 protected:
249 // Read the symbols--implemented by child class.
250 virtual void
251 do_read_symbols(Read_symbols_data*) = 0;
253 // Lay out sections--implemented by child class.
254 virtual void
255 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
257 // Add symbol information to the global symbol table--implemented by
258 // child class.
259 virtual void
260 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
262 // Return the location of the contents of a section. Implemented by
263 // child class.
264 virtual Location
265 do_section_contents(unsigned int shndx) = 0;
267 // Get the name of a section--implemented by child class.
268 virtual std::string
269 do_section_name(unsigned int shndx) = 0;
271 // Get section flags--implemented by child class.
272 virtual uint64_t
273 do_section_flags(unsigned int shndx) = 0;
275 // Get the file.
276 Input_file*
277 input_file() const
278 { return this->input_file_; }
280 // Get the offset into the file.
281 off_t
282 offset() const
283 { return this->offset_; }
285 // Get a view into the underlying file.
286 const unsigned char*
287 get_view(off_t start, off_t size, bool cache)
289 return this->input_file_->file().get_view(start + this->offset_, size,
290 cache);
293 // Get a lasting view into the underlying file.
294 File_view*
295 get_lasting_view(off_t start, off_t size, bool cache)
297 return this->input_file_->file().get_lasting_view(start + this->offset_,
298 size, cache);
301 // Read data from the underlying file.
302 void
303 read(off_t start, off_t size, void* p)
304 { this->input_file_->file().read(start + this->offset_, size, p); }
306 // Set the target.
307 void
308 set_target(int machine, int size, bool big_endian, int osabi,
309 int abiversion);
311 // Set the number of sections.
312 void
313 set_shnum(int shnum)
314 { this->shnum_ = shnum; }
316 // Functions used by both Sized_relobj and Sized_dynobj.
318 // Read the section data into a Read_symbols_data object.
319 template<int size, bool big_endian>
320 void
321 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
322 Read_symbols_data*);
324 // If NAME is the name of a special .gnu.warning section, arrange
325 // for the warning to be issued. SHNDX is the section index.
326 // Return whether it is a warning section.
327 bool
328 handle_gnu_warning_section(const char* name, unsigned int shndx,
329 Symbol_table*);
331 private:
332 // This class may not be copied.
333 Object(const Object&);
334 Object& operator=(const Object&);
336 // Name of object as printed to user.
337 std::string name_;
338 // For reading the file.
339 Input_file* input_file_;
340 // Offset within the file--0 for an object file, non-0 for an
341 // archive.
342 off_t offset_;
343 // Number of input sections.
344 unsigned int shnum_;
345 // Whether this is a dynamic object.
346 bool is_dynamic_;
347 // Target functions--may be NULL if the target is not known.
348 Target* target_;
351 // Implement sized_target inline for efficiency. This approach breaks
352 // static type checking, but is made safe using asserts.
354 template<int size, bool big_endian>
355 inline Sized_target<size, big_endian>*
356 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
358 gold_assert(this->target_->get_size() == size);
359 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
360 return static_cast<Sized_target<size, big_endian>*>(this->target_);
363 // A regular object (ET_REL). This is an abstract base class itself.
364 // The implementation is the template class Sized_relobj.
366 class Relobj : public Object
368 public:
369 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
370 : Object(name, input_file, false, offset)
373 // Read the relocs.
374 void
375 read_relocs(Read_relocs_data* rd)
376 { return this->do_read_relocs(rd); }
378 // Scan the relocs and adjust the symbol table.
379 void
380 scan_relocs(const General_options& options, Symbol_table* symtab,
381 Layout* layout, Read_relocs_data* rd)
382 { return this->do_scan_relocs(options, symtab, layout, rd); }
384 // Initial local symbol processing: set the offset where local
385 // symbol information will be stored; add local symbol names to
386 // *POOL; return the new local symbol index.
387 unsigned int
388 finalize_local_symbols(unsigned int index, off_t off,
389 Stringpool_template<char>* pool)
390 { return this->do_finalize_local_symbols(index, off, pool); }
392 // Relocate the input sections and write out the local symbols.
393 void
394 relocate(const General_options& options, const Symbol_table* symtab,
395 const Layout* layout, Output_file* of)
396 { return this->do_relocate(options, symtab, layout, of); }
398 // Return whether an input section is being included in the link.
399 bool
400 is_section_included(unsigned int shndx) const
402 gold_assert(shndx < this->map_to_output_.size());
403 return this->map_to_output_[shndx].output_section != NULL;
406 // Given a section index, return the corresponding Output_section
407 // (which will be NULL if the section is not included in the link)
408 // and set *POFF to the offset within that section.
409 inline Output_section*
410 output_section(unsigned int shndx, off_t* poff) const;
412 // Set the offset of an input section within its output section.
413 void
414 set_section_offset(unsigned int shndx, off_t off)
416 gold_assert(shndx < this->map_to_output_.size());
417 this->map_to_output_[shndx].offset = off;
420 protected:
421 // What we need to know to map an input section to an output
422 // section. We keep an array of these, one for each input section,
423 // indexed by the input section number.
424 struct Map_to_output
426 // The output section. This is NULL if the input section is to be
427 // discarded.
428 Output_section* output_section;
429 // The offset within the output section. This is -1 if the
430 // section requires special handling.
431 off_t offset;
434 // Read the relocs--implemented by child class.
435 virtual void
436 do_read_relocs(Read_relocs_data*) = 0;
438 // Scan the relocs--implemented by child class.
439 virtual void
440 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
441 Read_relocs_data*) = 0;
443 // Finalize local symbols--implemented by child class.
444 virtual unsigned int
445 do_finalize_local_symbols(unsigned int, off_t,
446 Stringpool_template<char>*) = 0;
448 // Relocate the input sections and write out the local
449 // symbols--implemented by child class.
450 virtual void
451 do_relocate(const General_options& options, const Symbol_table* symtab,
452 const Layout*, Output_file* of) = 0;
454 // Return the vector mapping input sections to output sections.
455 std::vector<Map_to_output>&
456 map_to_output()
457 { return this->map_to_output_; }
459 const std::vector<Map_to_output>&
460 map_to_output() const
461 { return this->map_to_output_; }
463 private:
464 // Mapping from input sections to output section.
465 std::vector<Map_to_output> map_to_output_;
468 // Implement Object::output_section inline for efficiency.
469 inline Output_section*
470 Relobj::output_section(unsigned int shndx, off_t* poff) const
472 gold_assert(shndx < this->map_to_output_.size());
473 const Map_to_output& mo(this->map_to_output_[shndx]);
474 *poff = mo.offset;
475 return mo.output_section;
478 // This POD class is holds the value of a symbol. This is used for
479 // local symbols, and for all symbols during relocation processing.
480 // In order to process relocs we need to be able to handle SHF_MERGE
481 // sections correctly.
483 template<int size>
484 class Symbol_value
486 public:
487 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
489 Symbol_value()
490 : output_symtab_index_(0), input_shndx_(0), needs_output_address_(false),
491 value_(0)
494 // Get the value of this symbol. OBJECT is the object in which this
495 // symbol is defined, and ADDEND is an addend to add to the value.
496 template<bool big_endian>
497 Value
498 value(const Sized_relobj<size, big_endian>* object, Value addend) const
500 if (!this->needs_output_address_)
501 return this->value_ + addend;
502 return object->local_value(this->input_shndx_, this->value_, addend);
505 // Set the value of this symbol in the output symbol table.
506 void
507 set_output_value(Value value)
509 this->value_ = value;
510 this->needs_output_address_ = false;
513 // If this symbol is mapped to an output section which requires
514 // special handling to determine the output value, we store the
515 // value of the symbol in the input file. This is used for
516 // SHF_MERGE sections.
517 void
518 set_input_value(Value value)
520 this->value_ = value;
521 this->needs_output_address_ = true;
524 // Return whether this symbol should go into the output symbol
525 // table.
526 bool
527 needs_output_symtab_entry() const
529 gold_assert(this->output_symtab_index_ != 0);
530 return this->output_symtab_index_ != -1U;
533 // Return the index in the output symbol table.
534 unsigned int
535 output_symtab_index() const
537 gold_assert(this->output_symtab_index_ != 0);
538 return this->output_symtab_index_;
541 // Set the index in the output symbol table.
542 void
543 set_output_symtab_index(unsigned int i)
545 gold_assert(this->output_symtab_index_ == 0);
546 this->output_symtab_index_ = i;
549 // Record that this symbol should not go into the output symbol
550 // table.
551 void
552 set_no_output_symtab_entry()
554 gold_assert(this->output_symtab_index_ == 0);
555 this->output_symtab_index_ = -1U;
558 // Set the index of the input section in the input file.
559 void
560 set_input_shndx(unsigned int i)
561 { this->input_shndx_ = i; }
563 private:
564 // The index of this local symbol in the output symbol table. This
565 // will be -1 if the symbol should not go into the symbol table.
566 unsigned int output_symtab_index_;
567 // The section index in the input file in which this symbol is
568 // defined.
569 unsigned int input_shndx_ : 31;
570 // Whether getting the value of this symbol requires calling an
571 // Output_section method. For example, this will be true of a
572 // STT_SECTION symbol in a SHF_MERGE section.
573 bool needs_output_address_ : 1;
574 // The value of the symbol. If !needs_output_address_, this is the
575 // value in the output file. If needs_output_address_, this is the
576 // value in the input file.
577 Value value_;
580 // A regular object file. This is size and endian specific.
582 template<int size, bool big_endian>
583 class Sized_relobj : public Relobj
585 public:
586 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
587 typedef std::vector<Symbol_value<size> > Local_values;
589 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
590 const typename elfcpp::Ehdr<size, big_endian>&);
592 ~Sized_relobj();
594 // Set up the object file based on the ELF header.
595 void
596 setup(const typename elfcpp::Ehdr<size, big_endian>&);
598 // Return the index of local symbol SYM in the ordinary symbol
599 // table. A value of -1U means that the symbol is not being output.
600 unsigned int
601 symtab_index(unsigned int sym) const
603 gold_assert(sym < this->local_values_.size());
604 return this->local_values_[sym].output_symtab_index();
607 // Read the symbols.
608 void
609 do_read_symbols(Read_symbols_data*);
611 // Lay out the input sections.
612 void
613 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
615 // Add the symbols to the symbol table.
616 void
617 do_add_symbols(Symbol_table*, Read_symbols_data*);
619 // Read the relocs.
620 void
621 do_read_relocs(Read_relocs_data*);
623 // Scan the relocs and adjust the symbol table.
624 void
625 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
626 Read_relocs_data*);
628 // Finalize the local symbols.
629 unsigned int
630 do_finalize_local_symbols(unsigned int, off_t,
631 Stringpool_template<char>*);
633 // Relocate the input sections and write out the local symbols.
634 void
635 do_relocate(const General_options& options, const Symbol_table* symtab,
636 const Layout*, Output_file* of);
638 // Get the name of a section.
639 std::string
640 do_section_name(unsigned int shndx)
641 { return this->elf_file_.section_name(shndx); }
643 // Return the location of the contents of a section.
644 Object::Location
645 do_section_contents(unsigned int shndx)
646 { return this->elf_file_.section_contents(shndx); }
648 // Return section flags.
649 uint64_t
650 do_section_flags(unsigned int shndx)
651 { return this->elf_file_.section_flags(shndx); }
653 // Return the appropriate Sized_target structure.
654 Sized_target<size, big_endian>*
655 sized_target()
657 return this->Object::sized_target
658 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
659 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
662 // Return the value of a local symbol define in input section SHNDX,
663 // with value VALUE, adding addend ADDEND. This handles SHF_MERGE
664 // sections.
665 Address
666 local_value(unsigned int shndx, Address value, Address addend) const;
668 private:
669 // For convenience.
670 typedef Sized_relobj<size, big_endian> This;
671 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
672 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
673 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
674 typedef elfcpp::Shdr<size, big_endian> Shdr;
676 // Find the SHT_SYMTAB section, given the section headers.
677 void
678 find_symtab(const unsigned char* pshdrs);
680 // Whether to include a section group in the link.
681 bool
682 include_section_group(Layout*, unsigned int,
683 const elfcpp::Shdr<size, big_endian>&,
684 std::vector<bool>*);
686 // Whether to include a linkonce section in the link.
687 bool
688 include_linkonce_section(Layout*, const char*,
689 const elfcpp::Shdr<size, big_endian>&);
691 // Views and sizes when relocating.
692 struct View_size
694 unsigned char* view;
695 typename elfcpp::Elf_types<size>::Elf_Addr address;
696 off_t offset;
697 off_t view_size;
700 typedef std::vector<View_size> Views;
702 // Write section data to the output file. Record the views and
703 // sizes in VIEWS for use when relocating.
704 void
705 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
707 // Relocate the sections in the output file.
708 void
709 relocate_sections(const General_options& options, const Symbol_table*,
710 const Layout*, const unsigned char* pshdrs, Views*);
712 // Write out the local symbols.
713 void
714 write_local_symbols(Output_file*,
715 const Stringpool_template<char>*);
717 // General access to the ELF file.
718 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
719 // Index of SHT_SYMTAB section.
720 unsigned int symtab_shndx_;
721 // The number of local symbols.
722 unsigned int local_symbol_count_;
723 // The number of local symbols which go into the output file.
724 unsigned int output_local_symbol_count_;
725 // The entries in the symbol table for the external symbols.
726 Symbol** symbols_;
727 // File offset for local symbols.
728 off_t local_symbol_offset_;
729 // Values of local symbols.
730 Local_values local_values_;
733 // A class to manage the list of all objects.
735 class Input_objects
737 public:
738 Input_objects()
739 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_()
742 // The type of the list of input relocateable objects.
743 typedef std::vector<Relobj*> Relobj_list;
744 typedef Relobj_list::const_iterator Relobj_iterator;
746 // The type of the list of input dynamic objects.
747 typedef std::vector<Dynobj*> Dynobj_list;
748 typedef Dynobj_list::const_iterator Dynobj_iterator;
750 // Add an object to the list. Return true if all is well, or false
751 // if this object should be ignored.
752 bool
753 add_object(Object*);
755 // Get the target we should use for the output file.
756 Target*
757 target() const
758 { return this->target_; }
760 // Iterate over all regular objects.
762 Relobj_iterator
763 relobj_begin() const
764 { return this->relobj_list_.begin(); }
766 Relobj_iterator
767 relobj_end() const
768 { return this->relobj_list_.end(); }
770 // Iterate over all dynamic objects.
772 Dynobj_iterator
773 dynobj_begin() const
774 { return this->dynobj_list_.begin(); }
776 Dynobj_iterator
777 dynobj_end() const
778 { return this->dynobj_list_.end(); }
780 // Return whether we have seen any dynamic objects.
781 bool
782 any_dynamic() const
783 { return !this->dynobj_list_.empty(); }
785 private:
786 Input_objects(const Input_objects&);
787 Input_objects& operator=(const Input_objects&);
789 // The list of ordinary objects included in the link.
790 Relobj_list relobj_list_;
791 // The list of dynamic objects included in the link.
792 Dynobj_list dynobj_list_;
793 // The target.
794 Target* target_;
795 // SONAMEs that we have seen.
796 Unordered_set<std::string> sonames_;
799 // Some of the information we pass to the relocation routines. We
800 // group this together to avoid passing a dozen different arguments.
802 template<int size, bool big_endian>
803 struct Relocate_info
805 // Command line options.
806 const General_options* options;
807 // Symbol table.
808 const Symbol_table* symtab;
809 // Layout.
810 const Layout* layout;
811 // Object being relocated.
812 Sized_relobj<size, big_endian>* object;
813 // Number of local symbols.
814 unsigned int local_symbol_count;
815 // Values of local symbols.
816 const typename Sized_relobj<size, big_endian>::Local_values* local_values;
817 // Global symbols.
818 const Symbol* const * symbols;
819 // Section index of relocation section.
820 unsigned int reloc_shndx;
821 // Section index of section being relocated.
822 unsigned int data_shndx;
824 // Return a string showing the location of a relocation. This is
825 // only used for error messages.
826 std::string
827 location(size_t relnum, off_t reloffset) const;
830 // Return an Object appropriate for the input file. P is BYTES long,
831 // and holds the ELF header.
833 extern Object*
834 make_elf_object(const std::string& name, Input_file*,
835 off_t offset, const unsigned char* p,
836 off_t bytes);
838 } // end namespace gold
840 #endif // !defined(GOLD_OBJECT_H)