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.
30 #include "elfcpp_file.h"
37 class General_options;
43 template<typename Stringpool_char>
44 class Stringpool_template;
46 // Data to pass from read_symbols() to add_symbols().
48 struct Read_symbols_data
51 File_view* section_headers;
53 File_view* section_names;
54 // Size of section name data in bytes.
55 off_t section_names_size;
58 // Size of symbol data in bytes.
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).
69 // Version definition data (from SHT_GNU_verdef section).
72 unsigned int verdef_info;
73 // Needed version data (from SHT_GNU_verneed section).
76 unsigned int verneed_info;
79 // Information used to print error messages.
81 struct Symbol_location_info
83 std::string source_file;
84 std::string enclosing_symbol_name;
88 // Data about a single relocation section. This is read in
89 // read_relocs and processed in scan_relocs.
93 // Index of reloc section.
94 unsigned int reloc_shndx;
95 // Index of section that relocs apply to.
96 unsigned int data_shndx;
97 // Contents of reloc section.
99 // Reloc section type.
100 unsigned int sh_type;
101 // Number of reloc entries.
105 // Relocations in an object file. This is read in read_relocs and
106 // processed in scan_relocs.
108 struct Read_relocs_data
110 typedef std::vector<Section_relocs> Relocs_list;
113 // The local symbols.
114 File_view* local_symbols;
117 // Object is an abstract base class which represents either a 32-bit
118 // or a 64-bit input object. This can be a regular object file
119 // (ET_REL) or a shared object (ET_DYN).
124 // NAME is the name of the object as we would report it to the user
125 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
126 // used to read the file. OFFSET is the offset within the input
127 // file--0 for a .o or .so file, something else for a .a file.
128 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
130 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
131 is_dynamic_(is_dynamic), target_(NULL)
137 // Return the name of the object as we would report it to the tuser.
140 { return this->name_; }
142 // Get the offset into the file.
145 { return this->offset_; }
147 // Return whether this is a dynamic object.
150 { return this->is_dynamic_; }
152 // Return the target structure associated with this object.
155 { return this->target_; }
157 // Lock the underlying file.
160 { this->input_file_->file().lock(); }
162 // Unlock the underlying file.
165 { this->input_file_->file().unlock(); }
167 // Return whether the underlying file is locked.
170 { return this->input_file_->file().is_locked(); }
172 // Return the sized target structure associated with this object.
173 // This is like the target method but it returns a pointer of
174 // appropriate checked type.
175 template<int size, bool big_endian>
176 Sized_target<size, big_endian>*
177 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
179 // Get the number of sections.
182 { return this->shnum_; }
184 // Return a view of the contents of a section. Set *PLEN to the
185 // size. CACHE is a hint as in File_read::get_view.
187 section_contents(unsigned int shndx, off_t* plen, bool cache);
189 // Return the name of a section given a section index. This is only
190 // used for error messages.
192 section_name(unsigned int shndx)
193 { return this->do_section_name(shndx); }
195 // Return the section flags given a section index.
197 section_flags(unsigned int shndx)
198 { return this->do_section_flags(shndx); }
200 // Return the section link field given a section index.
202 section_link(unsigned int shndx)
203 { return this->do_section_link(shndx); }
205 // Read the symbol information.
207 read_symbols(Read_symbols_data* sd)
208 { return this->do_read_symbols(sd); }
210 // Pass sections which should be included in the link to the Layout
211 // object, and record where the sections go in the output file.
213 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
214 { this->do_layout(symtab, layout, sd); }
216 // Add symbol information to the global symbol table.
218 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
219 { this->do_add_symbols(symtab, sd); }
221 // Functions and types for the elfcpp::Elf_file interface. This
222 // permit us to use Object as the File template parameter for
225 // The View class is returned by view. It must support a single
226 // method, data(). This is trivial, because get_view does what we
231 View(const unsigned char* p)
240 const unsigned char* p_;
245 view(off_t file_offset, off_t data_size)
246 { return View(this->get_view(file_offset, data_size, true)); }
250 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
252 // A location in the file.
258 Location(off_t fo, off_t ds)
259 : file_offset(fo), data_size(ds)
263 // Get a View given a Location.
264 View view(Location loc)
265 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
268 // Read the symbols--implemented by child class.
270 do_read_symbols(Read_symbols_data*) = 0;
272 // Lay out sections--implemented by child class.
274 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
276 // Add symbol information to the global symbol table--implemented by
279 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
281 // Return the location of the contents of a section. Implemented by
284 do_section_contents(unsigned int shndx) = 0;
286 // Get the name of a section--implemented by child class.
288 do_section_name(unsigned int shndx) = 0;
290 // Get section flags--implemented by child class.
292 do_section_flags(unsigned int shndx) = 0;
294 // Get section link field--implemented by child class.
296 do_section_link(unsigned int shndx) = 0;
301 { return this->input_file_; }
303 // Get a view into the underlying file.
305 get_view(off_t start, off_t size, bool cache)
307 return this->input_file_->file().get_view(start + this->offset_, size,
311 // Get a lasting view into the underlying file.
313 get_lasting_view(off_t start, off_t size, bool cache)
315 return this->input_file_->file().get_lasting_view(start + this->offset_,
319 // Read data from the underlying file.
321 read(off_t start, off_t size, void* p)
322 { this->input_file_->file().read(start + this->offset_, size, p); }
326 set_target(int machine, int size, bool big_endian, int osabi,
329 // Set the number of sections.
332 { this->shnum_ = shnum; }
334 // Functions used by both Sized_relobj and Sized_dynobj.
336 // Read the section data into a Read_symbols_data object.
337 template<int size, bool big_endian>
339 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
342 // If NAME is the name of a special .gnu.warning section, arrange
343 // for the warning to be issued. SHNDX is the section index.
344 // Return whether it is a warning section.
346 handle_gnu_warning_section(const char* name, unsigned int shndx,
350 // This class may not be copied.
351 Object(const Object&);
352 Object& operator=(const Object&);
354 // Name of object as printed to user.
356 // For reading the file.
357 Input_file* input_file_;
358 // Offset within the file--0 for an object file, non-0 for an
361 // Number of input sections.
363 // Whether this is a dynamic object.
365 // Target functions--may be NULL if the target is not known.
369 // Implement sized_target inline for efficiency. This approach breaks
370 // static type checking, but is made safe using asserts.
372 template<int size, bool big_endian>
373 inline Sized_target<size, big_endian>*
374 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
376 gold_assert(this->target_->get_size() == size);
377 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
378 return static_cast<Sized_target<size, big_endian>*>(this->target_);
381 // A regular object (ET_REL). This is an abstract base class itself.
382 // The implementation is the template class Sized_relobj.
384 class Relobj : public Object
387 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
388 : Object(name, input_file, false, offset)
393 read_relocs(Read_relocs_data* rd)
394 { return this->do_read_relocs(rd); }
396 // Scan the relocs and adjust the symbol table.
398 scan_relocs(const General_options& options, Symbol_table* symtab,
399 Layout* layout, Read_relocs_data* rd)
400 { return this->do_scan_relocs(options, symtab, layout, rd); }
402 // Initial local symbol processing: set the offset where local
403 // symbol information will be stored; add local symbol names to
404 // *POOL; return the new local symbol index.
406 finalize_local_symbols(unsigned int index, off_t off,
407 Stringpool_template<char>* pool)
408 { return this->do_finalize_local_symbols(index, off, pool); }
410 // Relocate the input sections and write out the local symbols.
412 relocate(const General_options& options, const Symbol_table* symtab,
413 const Layout* layout, Output_file* of)
414 { return this->do_relocate(options, symtab, layout, of); }
416 // Return whether an input section is being included in the link.
418 is_section_included(unsigned int shndx) const
420 gold_assert(shndx < this->map_to_output_.size());
421 return this->map_to_output_[shndx].output_section != NULL;
424 // Given a section index, return the corresponding Output_section
425 // (which will be NULL if the section is not included in the link)
426 // and set *POFF to the offset within that section.
427 inline Output_section*
428 output_section(unsigned int shndx, off_t* poff) const;
430 // Set the offset of an input section within its output section.
432 set_section_offset(unsigned int shndx, off_t off)
434 gold_assert(shndx < this->map_to_output_.size());
435 this->map_to_output_[shndx].offset = off;
439 // What we need to know to map an input section to an output
440 // section. We keep an array of these, one for each input section,
441 // indexed by the input section number.
444 // The output section. This is NULL if the input section is to be
446 Output_section* output_section;
447 // The offset within the output section. This is -1 if the
448 // section requires special handling.
452 // Read the relocs--implemented by child class.
454 do_read_relocs(Read_relocs_data*) = 0;
456 // Scan the relocs--implemented by child class.
458 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
459 Read_relocs_data*) = 0;
461 // Finalize local symbols--implemented by child class.
463 do_finalize_local_symbols(unsigned int, off_t,
464 Stringpool_template<char>*) = 0;
466 // Relocate the input sections and write out the local
467 // symbols--implemented by child class.
469 do_relocate(const General_options& options, const Symbol_table* symtab,
470 const Layout*, Output_file* of) = 0;
472 // Return the vector mapping input sections to output sections.
473 std::vector<Map_to_output>&
475 { return this->map_to_output_; }
477 const std::vector<Map_to_output>&
478 map_to_output() const
479 { return this->map_to_output_; }
482 // Mapping from input sections to output section.
483 std::vector<Map_to_output> map_to_output_;
486 // Implement Object::output_section inline for efficiency.
487 inline Output_section*
488 Relobj::output_section(unsigned int shndx, off_t* poff) const
490 gold_assert(shndx < this->map_to_output_.size());
491 const Map_to_output& mo(this->map_to_output_[shndx]);
493 return mo.output_section;
496 // This POD class is holds the value of a symbol. This is used for
497 // local symbols, and for all symbols during relocation processing.
498 // In order to process relocs we need to be able to handle SHF_MERGE
499 // sections correctly.
505 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
508 : output_symtab_index_(0), input_shndx_(0), is_section_symbol_(false),
509 needs_output_address_(false), value_(0)
512 // Get the value of this symbol. OBJECT is the object in which this
513 // symbol is defined, and ADDEND is an addend to add to the value.
514 template<bool big_endian>
516 value(const Sized_relobj<size, big_endian>* object, Value addend) const
518 if (!this->needs_output_address_)
519 return this->value_ + addend;
520 return object->local_value(this->input_shndx_, this->value_,
521 this->is_section_symbol_, addend);
524 // Set the value of this symbol in the output symbol table.
526 set_output_value(Value value)
528 this->value_ = value;
529 this->needs_output_address_ = false;
532 // If this symbol is mapped to an output section which requires
533 // special handling to determine the output value, we store the
534 // value of the symbol in the input file. This is used for
535 // SHF_MERGE sections.
537 set_input_value(Value value)
539 this->value_ = value;
540 this->needs_output_address_ = true;
543 // Return whether this symbol should go into the output symbol
546 needs_output_symtab_entry() const
548 gold_assert(this->output_symtab_index_ != 0);
549 return this->output_symtab_index_ != -1U;
552 // Return the index in the output symbol table.
554 output_symtab_index() const
556 gold_assert(this->output_symtab_index_ != 0);
557 return this->output_symtab_index_;
560 // Set the index in the output symbol table.
562 set_output_symtab_index(unsigned int i)
564 gold_assert(this->output_symtab_index_ == 0);
565 this->output_symtab_index_ = i;
568 // Record that this symbol should not go into the output symbol
571 set_no_output_symtab_entry()
573 gold_assert(this->output_symtab_index_ == 0);
574 this->output_symtab_index_ = -1U;
577 // Set the index of the input section in the input file.
579 set_input_shndx(unsigned int i)
580 { this->input_shndx_ = i; }
582 // Record that this is a section symbol.
584 set_is_section_symbol()
585 { this->is_section_symbol_ = true; }
588 // The index of this local symbol in the output symbol table. This
589 // will be -1 if the symbol should not go into the symbol table.
590 unsigned int output_symtab_index_;
591 // The section index in the input file in which this symbol is
593 unsigned int input_shndx_ : 30;
594 // Whether this is a STT_SECTION symbol.
595 bool is_section_symbol_ : 1;
596 // Whether getting the value of this symbol requires calling an
597 // Output_section method. For example, this will be true of a
598 // symbol in a SHF_MERGE section.
599 bool needs_output_address_ : 1;
600 // The value of the symbol. If !needs_output_address_, this is the
601 // value in the output file. If needs_output_address_, this is the
602 // value in the input file.
606 // A regular object file. This is size and endian specific.
608 template<int size, bool big_endian>
609 class Sized_relobj : public Relobj
612 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
613 typedef std::vector<Symbol_value<size> > Local_values;
615 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
616 const typename elfcpp::Ehdr<size, big_endian>&);
620 // Set up the object file based on the ELF header.
622 setup(const typename elfcpp::Ehdr<size, big_endian>&);
624 // Return the index of local symbol SYM in the ordinary symbol
625 // table. A value of -1U means that the symbol is not being output.
627 symtab_index(unsigned int sym) const
629 gold_assert(sym < this->local_values_.size());
630 return this->local_values_[sym].output_symtab_index();
633 // Return the appropriate Sized_target structure.
634 Sized_target<size, big_endian>*
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 the local symbol symndx.
644 local_symbol_value(unsigned int symndx) const;
646 // Return the value of a local symbol defined in input section
647 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
648 // indicates whether the symbol is a section symbol. This handles
649 // SHF_MERGE sections.
651 local_value(unsigned int shndx, Address value, bool is_section_symbol,
652 Address addend) const;
654 // Return whether the local symbol SYMNDX has a GOT offset.
656 local_has_got_offset(unsigned int symndx) const
658 return (this->local_got_offsets_.find(symndx)
659 != this->local_got_offsets_.end());
662 // Return the GOT offset of the local symbol SYMNDX.
664 local_got_offset(unsigned int symndx) const
666 Local_got_offsets::const_iterator p =
667 this->local_got_offsets_.find(symndx);
668 gold_assert(p != this->local_got_offsets_.end());
672 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
674 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
676 std::pair<Local_got_offsets::iterator, bool> ins =
677 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
678 gold_assert(ins.second);
681 // Return the name of the symbol that spans the given offset in the
682 // specified section in this object. This is used only for error
683 // messages and is not particularly efficient.
685 get_symbol_location_info(unsigned int shndx, off_t offset,
686 Symbol_location_info* info);
690 do_read_symbols(Read_symbols_data*);
692 // Lay out the input sections.
694 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
696 // Add the symbols to the symbol table.
698 do_add_symbols(Symbol_table*, Read_symbols_data*);
702 do_read_relocs(Read_relocs_data*);
704 // Scan the relocs and adjust the symbol table.
706 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
709 // Finalize the local symbols.
711 do_finalize_local_symbols(unsigned int, off_t,
712 Stringpool_template<char>*);
714 // Relocate the input sections and write out the local symbols.
716 do_relocate(const General_options& options, const Symbol_table* symtab,
717 const Layout*, Output_file* of);
719 // Get the name of a section.
721 do_section_name(unsigned int shndx)
722 { return this->elf_file_.section_name(shndx); }
724 // Return the location of the contents of a section.
726 do_section_contents(unsigned int shndx)
727 { return this->elf_file_.section_contents(shndx); }
729 // Return section flags.
731 do_section_flags(unsigned int shndx)
732 { return this->elf_file_.section_flags(shndx); }
734 // Return the section link field.
736 do_section_link(unsigned int shndx)
737 { return this->elf_file_.section_link(shndx); }
741 typedef Sized_relobj<size, big_endian> This;
742 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
743 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
744 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
745 typedef elfcpp::Shdr<size, big_endian> Shdr;
747 // Find the SHT_SYMTAB section, given the section headers.
749 find_symtab(const unsigned char* pshdrs);
751 // Whether to include a section group in the link.
753 include_section_group(Layout*, unsigned int,
754 const elfcpp::Shdr<size, big_endian>&,
757 // Whether to include a linkonce section in the link.
759 include_linkonce_section(Layout*, const char*,
760 const elfcpp::Shdr<size, big_endian>&);
762 // Views and sizes when relocating.
766 typename elfcpp::Elf_types<size>::Elf_Addr address;
771 typedef std::vector<View_size> Views;
773 // Write section data to the output file. Record the views and
774 // sizes in VIEWS for use when relocating.
776 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
778 // Relocate the sections in the output file.
780 relocate_sections(const General_options& options, const Symbol_table*,
781 const Layout*, const unsigned char* pshdrs, Views*);
783 // Write out the local symbols.
785 write_local_symbols(Output_file*,
786 const Stringpool_template<char>*);
788 // The GOT offsets of local symbols.
789 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
791 // General access to the ELF file.
792 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
793 // Index of SHT_SYMTAB section.
794 unsigned int symtab_shndx_;
795 // The number of local symbols.
796 unsigned int local_symbol_count_;
797 // The number of local symbols which go into the output file.
798 unsigned int output_local_symbol_count_;
799 // The entries in the symbol table for the external symbols.
801 // File offset for local symbols.
802 off_t local_symbol_offset_;
803 // Values of local symbols.
804 Local_values local_values_;
805 // GOT offsets for local symbols, indexed by symbol number.
806 Local_got_offsets local_got_offsets_;
809 // A class to manage the list of all objects.
815 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_()
818 // The type of the list of input relocateable objects.
819 typedef std::vector<Relobj*> Relobj_list;
820 typedef Relobj_list::const_iterator Relobj_iterator;
822 // The type of the list of input dynamic objects.
823 typedef std::vector<Dynobj*> Dynobj_list;
824 typedef Dynobj_list::const_iterator Dynobj_iterator;
826 // Add an object to the list. Return true if all is well, or false
827 // if this object should be ignored.
831 // Get the target we should use for the output file.
834 { return this->target_; }
836 // Iterate over all regular objects.
840 { return this->relobj_list_.begin(); }
844 { return this->relobj_list_.end(); }
846 // Iterate over all dynamic objects.
850 { return this->dynobj_list_.begin(); }
854 { return this->dynobj_list_.end(); }
856 // Return whether we have seen any dynamic objects.
859 { return !this->dynobj_list_.empty(); }
861 // Return the number of input objects.
863 number_of_input_objects() const
864 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
867 Input_objects(const Input_objects&);
868 Input_objects& operator=(const Input_objects&);
870 // The list of ordinary objects included in the link.
871 Relobj_list relobj_list_;
872 // The list of dynamic objects included in the link.
873 Dynobj_list dynobj_list_;
876 // SONAMEs that we have seen.
877 Unordered_set<std::string> sonames_;
880 // Some of the information we pass to the relocation routines. We
881 // group this together to avoid passing a dozen different arguments.
883 template<int size, bool big_endian>
886 // Command line options.
887 const General_options* options;
889 const Symbol_table* symtab;
891 const Layout* layout;
892 // Object being relocated.
893 Sized_relobj<size, big_endian>* object;
894 // Number of local symbols.
895 unsigned int local_symbol_count;
896 // Values of local symbols.
897 const typename Sized_relobj<size, big_endian>::Local_values* local_values;
899 const Symbol* const * symbols;
900 // Section index of relocation section.
901 unsigned int reloc_shndx;
902 // Section index of section being relocated.
903 unsigned int data_shndx;
905 // Return a string showing the location of a relocation. This is
906 // only used for error messages.
908 location(size_t relnum, off_t reloffset) const;
911 // Return an Object appropriate for the input file. P is BYTES long,
912 // and holds the ELF header.
915 make_elf_object(const std::string& name, Input_file*,
916 off_t offset, const unsigned char* p,
919 } // end namespace gold
921 #endif // !defined(GOLD_OBJECT_H)