Only create a version definition for a shared library. Set the
[binutils.git] / gold / object.h
blob2027f8e97dff6c13c14df7a723858507cb751c43
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 #ifndef GOLD_OBJECT_H
4 #define GOLD_OBJECT_H
6 #include <string>
7 #include <vector>
9 #include "elfcpp.h"
10 #include "elfcpp_file.h"
11 #include "fileread.h"
12 #include "target.h"
14 namespace gold
17 class General_options;
18 class Layout;
19 class Output_section;
20 class Output_file;
21 class Dynobj;
23 template<typename Stringpool_char>
24 class Stringpool_template;
26 // Data to pass from read_symbols() to add_symbols().
28 struct Read_symbols_data
30 // Section headers.
31 File_view* section_headers;
32 // Section names.
33 File_view* section_names;
34 // Size of section name data in bytes.
35 off_t section_names_size;
36 // Symbol data.
37 File_view* symbols;
38 // Size of symbol data in bytes.
39 off_t symbols_size;
40 // Symbol names.
41 File_view* symbol_names;
42 // Size of symbol name data in bytes.
43 off_t symbol_names_size;
45 // Version information. This is only used on dynamic objects.
46 // Version symbol data (from SHT_GNU_versym section).
47 File_view* versym;
48 off_t versym_size;
49 // Version definition data (from SHT_GNU_verdef section).
50 File_view* verdef;
51 off_t verdef_size;
52 unsigned int verdef_info;
53 // Needed version data (from SHT_GNU_verneed section).
54 File_view* verneed;
55 off_t verneed_size;
56 unsigned int verneed_info;
59 // Data about a single relocation section. This is read in
60 // read_relocs and processed in scan_relocs.
62 struct Section_relocs
64 // Index of reloc section.
65 unsigned int reloc_shndx;
66 // Index of section that relocs apply to.
67 unsigned int data_shndx;
68 // Contents of reloc section.
69 File_view* contents;
70 // Reloc section type.
71 unsigned int sh_type;
72 // Number of reloc entries.
73 size_t reloc_count;
76 // Relocations in an object file. This is read in read_relocs and
77 // processed in scan_relocs.
79 struct Read_relocs_data
81 typedef std::vector<Section_relocs> Relocs_list;
82 // The relocations.
83 Relocs_list relocs;
84 // The local symbols.
85 File_view* local_symbols;
88 // Object is an abstract base class which represents either a 32-bit
89 // or a 64-bit input object. This can be a regular object file
90 // (ET_REL) or a shared object (ET_DYN).
92 class Object
94 public:
95 // NAME is the name of the object as we would report it to the user
96 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
97 // used to read the file. OFFSET is the offset within the input
98 // file--0 for a .o or .so file, something else for a .a file.
99 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
100 off_t offset = 0)
101 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
102 is_dynamic_(is_dynamic), target_(NULL)
105 virtual ~Object()
108 // Return the name of the object as we would report it to the tuser.
109 const std::string&
110 name() const
111 { return this->name_; }
113 // Return whether this is a dynamic object.
114 bool
115 is_dynamic() const
116 { return this->is_dynamic_; }
118 // Return the target structure associated with this object.
119 Target*
120 target() const
121 { return this->target_; }
123 // Lock the underlying file.
124 void
125 lock()
126 { this->input_file_->file().lock(); }
128 // Unlock the underlying file.
129 void
130 unlock()
131 { this->input_file_->file().unlock(); }
133 // Return whether the underlying file is locked.
134 bool
135 is_locked() const
136 { return this->input_file_->file().is_locked(); }
138 // Return the sized target structure associated with this object.
139 // This is like the target method but it returns a pointer of
140 // appropriate checked type.
141 template<int size, bool big_endian>
142 Sized_target<size, big_endian>*
143 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
145 // Get the number of sections.
146 unsigned int
147 shnum() const
148 { return this->shnum_; }
150 // Return a view of the contents of a section. Set *PLEN to the
151 // size.
152 const unsigned char*
153 section_contents(unsigned int shndx, off_t* plen);
155 // Return the name of a section given a section index. This is only
156 // used for error messages.
157 std::string
158 section_name(unsigned int shndx)
159 { return this->do_section_name(shndx); }
161 // Return the section flags given a section index.
162 uint64_t
163 section_flags(unsigned int shndx)
164 { return this->do_section_flags(shndx); }
166 // Read the symbol information.
167 void
168 read_symbols(Read_symbols_data* sd)
169 { return this->do_read_symbols(sd); }
171 // Pass sections which should be included in the link to the Layout
172 // object, and record where the sections go in the output file.
173 void
174 layout(const General_options& options, Symbol_table* symtab,
175 Layout* layout, Read_symbols_data* sd)
176 { this->do_layout(options, symtab, layout, sd); }
178 // Add symbol information to the global symbol table.
179 void
180 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
181 { this->do_add_symbols(symtab, sd); }
183 // Functions and types for the elfcpp::Elf_file interface. This
184 // permit us to use Object as the File template parameter for
185 // elfcpp::Elf_file.
187 // The View class is returned by view. It must support a single
188 // method, data(). This is trivial, because get_view does what we
189 // need.
190 class View
192 public:
193 View(const unsigned char* p)
194 : p_(p)
197 const unsigned char*
198 data() const
199 { return this->p_; }
201 private:
202 const unsigned char* p_;
205 // Return a View.
206 View
207 view(off_t file_offset, off_t data_size)
208 { return View(this->get_view(file_offset, data_size)); }
210 // Report an error.
211 void
212 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
214 // A location in the file.
215 struct Location
217 off_t file_offset;
218 off_t data_size;
220 Location(off_t fo, off_t ds)
221 : file_offset(fo), data_size(ds)
225 // Get a View given a Location.
226 View view(Location loc)
227 { return View(this->get_view(loc.file_offset, loc.data_size)); }
229 protected:
230 // Read the symbols--implemented by child class.
231 virtual void
232 do_read_symbols(Read_symbols_data*) = 0;
234 // Lay out sections--implemented by child class.
235 virtual void
236 do_layout(const General_options&, Symbol_table*, Layout*,
237 Read_symbols_data*) = 0;
239 // Add symbol information to the global symbol table--implemented by
240 // child class.
241 virtual void
242 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
244 // Return the location of the contents of a section. Implemented by
245 // child class.
246 virtual Location
247 do_section_contents(unsigned int shndx) = 0;
249 // Get the name of a section--implemented by child class.
250 virtual std::string
251 do_section_name(unsigned int shndx) = 0;
253 // Get section flags--implemented by child class.
254 virtual uint64_t
255 do_section_flags(unsigned int shndx) = 0;
257 // Get the file.
258 Input_file*
259 input_file() const
260 { return this->input_file_; }
262 // Get the offset into the file.
263 off_t
264 offset() const
265 { return this->offset_; }
267 // Get a view into the underlying file.
268 const unsigned char*
269 get_view(off_t start, off_t size)
270 { return this->input_file_->file().get_view(start + this->offset_, size); }
272 // Get a lasting view into the underlying file.
273 File_view*
274 get_lasting_view(off_t start, off_t size)
276 return this->input_file_->file().get_lasting_view(start + this->offset_,
277 size);
280 // Read data from the underlying file.
281 void
282 read(off_t start, off_t size, void* p)
283 { this->input_file_->file().read(start + this->offset_, size, p); }
285 // Set the target.
286 void
287 set_target(int machine, int size, bool big_endian, int osabi,
288 int abiversion);
290 // Set the number of sections.
291 void
292 set_shnum(int shnum)
293 { this->shnum_ = shnum; }
295 // Functions used by both Sized_relobj and Sized_dynobj.
297 // Read the section data into a Read_symbols_data object.
298 template<int size, bool big_endian>
299 void
300 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
301 Read_symbols_data*);
303 // If NAME is the name of a special .gnu.warning section, arrange
304 // for the warning to be issued. SHNDX is the section index.
305 // Return whether it is a warning section.
306 bool
307 handle_gnu_warning_section(const char* name, unsigned int shndx,
308 Symbol_table*);
310 private:
311 // This class may not be copied.
312 Object(const Object&);
313 Object& operator=(const Object&);
315 // Name of object as printed to user.
316 std::string name_;
317 // For reading the file.
318 Input_file* input_file_;
319 // Offset within the file--0 for an object file, non-0 for an
320 // archive.
321 off_t offset_;
322 // Number of input sections.
323 unsigned int shnum_;
324 // Whether this is a dynamic object.
325 bool is_dynamic_;
326 // Target functions--may be NULL if the target is not known.
327 Target* target_;
330 // Implement sized_target inline for efficiency. This approach breaks
331 // static type checking, but is made safe using asserts.
333 template<int size, bool big_endian>
334 inline Sized_target<size, big_endian>*
335 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
337 gold_assert(this->target_->get_size() == size);
338 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
339 return static_cast<Sized_target<size, big_endian>*>(this->target_);
342 // A regular object (ET_REL). This is an abstract base class itself.
343 // The implementation is the template class Sized_relobj.
345 class Relobj : public Object
347 public:
348 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
349 : Object(name, input_file, false, offset)
352 // Read the relocs.
353 void
354 read_relocs(Read_relocs_data* rd)
355 { return this->do_read_relocs(rd); }
357 // Scan the relocs and adjust the symbol table.
358 void
359 scan_relocs(const General_options& options, Symbol_table* symtab,
360 Layout* layout, Read_relocs_data* rd)
361 { return this->do_scan_relocs(options, symtab, layout, rd); }
363 // Initial local symbol processing: set the offset where local
364 // symbol information will be stored; add local symbol names to
365 // *POOL; return the new local symbol index.
366 unsigned int
367 finalize_local_symbols(unsigned int index, off_t off,
368 Stringpool_template<char>* pool)
369 { return this->do_finalize_local_symbols(index, off, pool); }
371 // Relocate the input sections and write out the local symbols.
372 void
373 relocate(const General_options& options, const Symbol_table* symtab,
374 const Layout* layout, Output_file* of)
375 { return this->do_relocate(options, symtab, layout, of); }
377 // Return whether an input section is being included in the link.
378 bool
379 is_section_included(unsigned int shndx) const
381 gold_assert(shndx < this->map_to_output_.size());
382 return this->map_to_output_[shndx].output_section != NULL;
385 // Given a section index, return the corresponding Output_section
386 // (which will be NULL if the section is not included in the link)
387 // and set *POFF to the offset within that section.
388 inline Output_section*
389 output_section(unsigned int shndx, off_t* poff) const;
391 // Set the offset of an input section within its output section.
392 void
393 set_section_offset(unsigned int shndx, off_t off)
395 gold_assert(shndx < this->map_to_output_.size());
396 this->map_to_output_[shndx].offset = off;
399 protected:
400 // What we need to know to map an input section to an output
401 // section. We keep an array of these, one for each input section,
402 // indexed by the input section number.
403 struct Map_to_output
405 // The output section. This is NULL if the input section is to be
406 // discarded.
407 Output_section* output_section;
408 // The offset within the output section. This is -1 if the
409 // section requires special handling.
410 off_t offset;
413 // Read the relocs--implemented by child class.
414 virtual void
415 do_read_relocs(Read_relocs_data*) = 0;
417 // Scan the relocs--implemented by child class.
418 virtual void
419 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
420 Read_relocs_data*) = 0;
422 // Finalize local symbols--implemented by child class.
423 virtual unsigned int
424 do_finalize_local_symbols(unsigned int, off_t,
425 Stringpool_template<char>*) = 0;
427 // Relocate the input sections and write out the local
428 // symbols--implemented by child class.
429 virtual void
430 do_relocate(const General_options& options, const Symbol_table* symtab,
431 const Layout*, Output_file* of) = 0;
433 // Return the vector mapping input sections to output sections.
434 std::vector<Map_to_output>&
435 map_to_output()
436 { return this->map_to_output_; }
438 const std::vector<Map_to_output>&
439 map_to_output() const
440 { return this->map_to_output_; }
442 private:
443 // Mapping from input sections to output section.
444 std::vector<Map_to_output> map_to_output_;
447 // Implement Object::output_section inline for efficiency.
448 inline Output_section*
449 Relobj::output_section(unsigned int shndx, off_t* poff) const
451 gold_assert(shndx < this->map_to_output_.size());
452 const Map_to_output& mo(this->map_to_output_[shndx]);
453 *poff = mo.offset;
454 return mo.output_section;
457 // This POD class is holds the value of a symbol. This is used for
458 // local symbols, and for all symbols during relocation processing.
459 // In order to process relocs we need to be able to handle SHF_MERGE
460 // sections correctly.
462 template<int size>
463 class Symbol_value
465 public:
466 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
468 Symbol_value()
469 : output_symtab_index_(0), input_shndx_(0), needs_output_address_(false),
470 value_(0)
473 // Get the value of this symbol. OBJECT is the object in which this
474 // symbol is defined, and ADDEND is an addend to add to the value.
475 template<bool big_endian>
476 Value
477 value(const Sized_relobj<size, big_endian>* object, Value addend) const
479 if (!this->needs_output_address_)
480 return this->value_ + addend;
481 return object->local_value(this->input_shndx_, this->value_, addend);
484 // Set the value of this symbol in the output symbol table.
485 void
486 set_output_value(Value value)
488 this->value_ = value;
489 this->needs_output_address_ = false;
492 // If this symbol is mapped to an output section which requires
493 // special handling to determine the output value, we store the
494 // value of the symbol in the input file. This is used for
495 // SHF_MERGE sections.
496 void
497 set_input_value(Value value)
499 this->value_ = value;
500 this->needs_output_address_ = true;
503 // Return whether this symbol should go into the output symbol
504 // table.
505 bool
506 needs_output_symtab_entry() const
508 gold_assert(this->output_symtab_index_ != 0);
509 return this->output_symtab_index_ != -1U;
512 // Return the index in the output symbol table.
513 unsigned int
514 output_symtab_index() const
516 gold_assert(this->output_symtab_index_ != 0);
517 return this->output_symtab_index_;
520 // Set the index in the output symbol table.
521 void
522 set_output_symtab_index(unsigned int i)
524 gold_assert(this->output_symtab_index_ == 0);
525 this->output_symtab_index_ = i;
528 // Record that this symbol should not go into the output symbol
529 // table.
530 void
531 set_no_output_symtab_entry()
533 gold_assert(this->output_symtab_index_ == 0);
534 this->output_symtab_index_ = -1U;
537 // Set the index of the input section in the input file.
538 void
539 set_input_shndx(unsigned int i)
540 { this->input_shndx_ = i; }
542 private:
543 // The index of this local symbol in the output symbol table. This
544 // will be -1 if the symbol should not go into the symbol table.
545 unsigned int output_symtab_index_;
546 // The section index in the input file in which this symbol is
547 // defined.
548 unsigned int input_shndx_ : 31;
549 // Whether getting the value of this symbol requires calling an
550 // Output_section method. For example, this will be true of a
551 // STT_SECTION symbol in a SHF_MERGE section.
552 bool needs_output_address_ : 1;
553 // The value of the symbol. If !needs_output_address_, this is the
554 // value in the output file. If needs_output_address_, this is the
555 // value in the input file.
556 Value value_;
559 // A regular object file. This is size and endian specific.
561 template<int size, bool big_endian>
562 class Sized_relobj : public Relobj
564 public:
565 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
566 typedef std::vector<Symbol_value<size> > Local_values;
568 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
569 const typename elfcpp::Ehdr<size, big_endian>&);
571 ~Sized_relobj();
573 // Set up the object file based on the ELF header.
574 void
575 setup(const typename elfcpp::Ehdr<size, big_endian>&);
577 // Return the index of local symbol SYM in the ordinary symbol
578 // table. A value of -1U means that the symbol is not being output.
579 unsigned int
580 symtab_index(unsigned int sym) const
582 gold_assert(sym < this->local_values_.size());
583 return this->local_values_[sym].output_symtab_index();
586 // Read the symbols.
587 void
588 do_read_symbols(Read_symbols_data*);
590 // Lay out the input sections.
591 void
592 do_layout(const General_options&, Symbol_table*, Layout*,
593 Read_symbols_data*);
595 // Add the symbols to the symbol table.
596 void
597 do_add_symbols(Symbol_table*, Read_symbols_data*);
599 // Read the relocs.
600 void
601 do_read_relocs(Read_relocs_data*);
603 // Scan the relocs and adjust the symbol table.
604 void
605 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
606 Read_relocs_data*);
608 // Finalize the local symbols.
609 unsigned int
610 do_finalize_local_symbols(unsigned int, off_t,
611 Stringpool_template<char>*);
613 // Relocate the input sections and write out the local symbols.
614 void
615 do_relocate(const General_options& options, const Symbol_table* symtab,
616 const Layout*, Output_file* of);
618 // Get the name of a section.
619 std::string
620 do_section_name(unsigned int shndx)
621 { return this->elf_file_.section_name(shndx); }
623 // Return the location of the contents of a section.
624 Object::Location
625 do_section_contents(unsigned int shndx)
626 { return this->elf_file_.section_contents(shndx); }
628 // Return section flags.
629 uint64_t
630 do_section_flags(unsigned int shndx)
631 { return this->elf_file_.section_flags(shndx); }
633 // Return the appropriate Sized_target structure.
634 Sized_target<size, big_endian>*
635 sized_target()
637 return this->Object::sized_target
638 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
639 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
642 // Return the value of a local symbol define in input section SHNDX,
643 // with value VALUE, adding addend ADDEND. This handles SHF_MERGE
644 // sections.
645 Address
646 local_value(unsigned int shndx, Address value, Address addend) const;
648 private:
649 // For convenience.
650 typedef Sized_relobj<size, big_endian> This;
651 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
652 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
653 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
654 typedef elfcpp::Shdr<size, big_endian> Shdr;
656 // Find the SHT_SYMTAB section, given the section headers.
657 void
658 find_symtab(const unsigned char* pshdrs);
660 // Whether to include a section group in the link.
661 bool
662 include_section_group(Layout*, unsigned int,
663 const elfcpp::Shdr<size, big_endian>&,
664 std::vector<bool>*);
666 // Whether to include a linkonce section in the link.
667 bool
668 include_linkonce_section(Layout*, const char*,
669 const elfcpp::Shdr<size, big_endian>&);
671 // Views and sizes when relocating.
672 struct View_size
674 unsigned char* view;
675 typename elfcpp::Elf_types<size>::Elf_Addr address;
676 off_t offset;
677 off_t view_size;
680 typedef std::vector<View_size> Views;
682 // Write section data to the output file. Record the views and
683 // sizes in VIEWS for use when relocating.
684 void
685 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
687 // Relocate the sections in the output file.
688 void
689 relocate_sections(const General_options& options, const Symbol_table*,
690 const Layout*, const unsigned char* pshdrs, Views*);
692 // Write out the local symbols.
693 void
694 write_local_symbols(Output_file*,
695 const Stringpool_template<char>*);
697 // General access to the ELF file.
698 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
699 // Index of SHT_SYMTAB section.
700 unsigned int symtab_shndx_;
701 // The number of local symbols.
702 unsigned int local_symbol_count_;
703 // The number of local symbols which go into the output file.
704 unsigned int output_local_symbol_count_;
705 // The entries in the symbol table for the external symbols.
706 Symbol** symbols_;
707 // File offset for local symbols.
708 off_t local_symbol_offset_;
709 // Values of local symbols.
710 Local_values local_values_;
713 // A class to manage the list of all objects.
715 class Input_objects
717 public:
718 Input_objects()
719 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_()
722 // The type of the list of input relocateable objects.
723 typedef std::vector<Relobj*> Relobj_list;
724 typedef Relobj_list::const_iterator Relobj_iterator;
726 // The type of the list of input dynamic objects.
727 typedef std::vector<Dynobj*> Dynobj_list;
728 typedef Dynobj_list::const_iterator Dynobj_iterator;
730 // Add an object to the list. Return true if all is well, or false
731 // if this object should be ignored.
732 bool
733 add_object(Object*);
735 // Get the target we should use for the output file.
736 Target*
737 target() const
738 { return this->target_; }
740 // Iterate over all regular objects.
742 Relobj_iterator
743 relobj_begin() const
744 { return this->relobj_list_.begin(); }
746 Relobj_iterator
747 relobj_end() const
748 { return this->relobj_list_.end(); }
750 // Iterate over all dynamic objects.
752 Dynobj_iterator
753 dynobj_begin() const
754 { return this->dynobj_list_.begin(); }
756 Dynobj_iterator
757 dynobj_end() const
758 { return this->dynobj_list_.end(); }
760 // Return whether we have seen any dynamic objects.
761 bool
762 any_dynamic() const
763 { return !this->dynobj_list_.empty(); }
765 private:
766 Input_objects(const Input_objects&);
767 Input_objects& operator=(const Input_objects&);
769 // The list of ordinary objects included in the link.
770 Relobj_list relobj_list_;
771 // The list of dynamic objects included in the link.
772 Dynobj_list dynobj_list_;
773 // The target.
774 Target* target_;
775 // SONAMEs that we have seen.
776 Unordered_set<std::string> sonames_;
779 // Some of the information we pass to the relocation routines. We
780 // group this together to avoid passing a dozen different arguments.
782 template<int size, bool big_endian>
783 struct Relocate_info
785 // Command line options.
786 const General_options* options;
787 // Symbol table.
788 const Symbol_table* symtab;
789 // Layout.
790 const Layout* layout;
791 // Object being relocated.
792 Sized_relobj<size, big_endian>* object;
793 // Number of local symbols.
794 unsigned int local_symbol_count;
795 // Values of local symbols.
796 const typename Sized_relobj<size, big_endian>::Local_values* local_values;
797 // Global symbols.
798 const Symbol* const * symbols;
799 // Section index of relocation section.
800 unsigned int reloc_shndx;
801 // Section index of section being relocated.
802 unsigned int data_shndx;
804 // Return a string showing the location of a relocation. This is
805 // only used for error messages.
806 std::string
807 location(size_t relnum, off_t reloffset) const;
810 // Return an Object appropriate for the input file. P is BYTES long,
811 // and holds the ELF header.
813 extern Object*
814 make_elf_object(const std::string& name, Input_file*,
815 off_t offset, const unsigned char* p,
816 off_t bytes);
818 } // end namespace gold
820 #endif // !defined(GOLD_OBJECT_H)