1 // object.h -- support for an object file for linking in gold -*- C++ -*-
11 #include "elfcpp_file.h"
18 class General_options;
25 // Data to pass from read_symbols() to add_symbols().
27 struct Read_symbols_data
30 File_view* section_headers;
32 File_view* section_names;
33 // Size of section name data in bytes.
34 off_t section_names_size;
37 // Size of symbol data in bytes.
40 File_view* symbol_names;
41 // Size of symbol name data in bytes.
42 off_t symbol_names_size;
44 // Version information. This is only used on dynamic objects.
45 // Version symbol data (from SHT_GNU_versym section).
48 // Version definition data (from SHT_GNU_verdef section).
51 unsigned int verdef_info;
52 // Needed version data (from SHT_GNU_verneed section).
55 unsigned int verneed_info;
58 // Data about a single relocation section. This is read in
59 // read_relocs and processed in scan_relocs.
63 // Index of reloc section.
64 unsigned int reloc_shndx;
65 // Index of section that relocs apply to.
66 unsigned int data_shndx;
67 // Contents of reloc section.
69 // Reloc section type.
71 // Number of reloc entries.
75 // Relocations in an object file. This is read in read_relocs and
76 // processed in scan_relocs.
78 struct Read_relocs_data
80 typedef std::vector<Section_relocs> Relocs_list;
84 File_view* local_symbols;
87 // Object is an abstract base class which represents either a 32-bit
88 // or a 64-bit input object. This can be a regular object file
89 // (ET_REL) or a shared object (ET_DYN).
94 // NAME is the name of the object as we would report it to the user
95 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
96 // used to read the file. OFFSET is the offset within the input
97 // file--0 for a .o or .so file, something else for a .a file.
98 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
100 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
101 is_dynamic_(is_dynamic), target_(NULL)
107 // Return the name of the object as we would report it to the tuser.
110 { return this->name_; }
112 // Return whether this is a dynamic object.
115 { return this->is_dynamic_; }
117 // Return the target structure associated with this object.
120 { return this->target_; }
122 // Lock the underlying file.
125 { this->input_file_->file().lock(); }
127 // Unlock the underlying file.
130 { this->input_file_->file().unlock(); }
132 // Return whether the underlying file is locked.
135 { return this->input_file_->file().is_locked(); }
137 // Return the sized target structure associated with this object.
138 // This is like the target method but it returns a pointer of
139 // appropriate checked type.
140 template<int size, bool big_endian>
141 Sized_target<size, big_endian>*
142 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
144 // Read the symbol information.
146 read_symbols(Read_symbols_data* sd)
147 { return this->do_read_symbols(sd); }
149 // Pass sections which should be included in the link to the Layout
150 // object, and record where the sections go in the output file.
152 layout(const General_options& options, Symbol_table* symtab,
153 Layout* layout, Read_symbols_data* sd)
154 { this->do_layout(options, symtab, layout, sd); }
156 // Add symbol information to the global symbol table.
158 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
159 { this->do_add_symbols(symtab, sd); }
161 // Return a view of the contents of a section. Set *PLEN to the
164 section_contents(unsigned int shndx, off_t* plen);
166 // Return the name of a section given a section index. This is only
167 // used for error messages.
169 section_name(unsigned int shnum)
170 { return this->do_section_name(shnum); }
172 // Functions and types for the elfcpp::Elf_file interface. This
173 // permit us to use Object as the File template parameter for
176 // The View class is returned by view. It must support a single
177 // method, data(). This is trivial, because get_view does what we
182 View(const unsigned char* p)
191 const unsigned char* p_;
196 view(off_t file_offset, off_t data_size)
197 { return View(this->get_view(file_offset, data_size)); }
201 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
203 // A location in the file.
209 Location(off_t fo, off_t ds)
210 : file_offset(fo), data_size(ds)
214 // Get a View given a Location.
215 View view(Location loc)
216 { return View(this->get_view(loc.file_offset, loc.data_size)); }
219 // Read the symbols--implemented by child class.
221 do_read_symbols(Read_symbols_data*) = 0;
223 // Lay out sections--implemented by child class.
225 do_layout(const General_options&, Symbol_table*, Layout*,
226 Read_symbols_data*) = 0;
228 // Add symbol information to the global symbol table--implemented by
231 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
233 // Return the location of the contents of a section. Implemented by
236 do_section_contents(unsigned int shnum) = 0;
238 // Get the name of a section--implemented by child class.
240 do_section_name(unsigned int shnum) = 0;
245 { return this->input_file_; }
247 // Get the offset into the file.
250 { return this->offset_; }
252 // Get a view into the underlying file.
254 get_view(off_t start, off_t size)
255 { return this->input_file_->file().get_view(start + this->offset_, size); }
257 // Get a lasting view into the underlying file.
259 get_lasting_view(off_t start, off_t size)
261 return this->input_file_->file().get_lasting_view(start + this->offset_,
265 // Read data from the underlying file.
267 read(off_t start, off_t size, void* p)
268 { this->input_file_->file().read(start + this->offset_, size, p); }
272 set_target(int machine, int size, bool big_endian, int osabi,
275 // Get the number of sections.
278 { return this->shnum_; }
280 // Set the number of sections.
283 { this->shnum_ = shnum; }
285 // Functions used by both Sized_relobj and Sized_dynobj.
287 // Read the section data into a Read_symbols_data object.
288 template<int size, bool big_endian>
290 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
293 // If NAME is the name of a special .gnu.warning section, arrange
294 // for the warning to be issued. SHNDX is the section index.
295 // Return whether it is a warning section.
297 handle_gnu_warning_section(const char* name, unsigned int shndx,
301 // This class may not be copied.
302 Object(const Object&);
303 Object& operator=(const Object&);
305 // Name of object as printed to user.
307 // For reading the file.
308 Input_file* input_file_;
309 // Offset within the file--0 for an object file, non-0 for an
312 // Number of input sections.
314 // Whether this is a dynamic object.
316 // Target functions--may be NULL if the target is not known.
320 // Implement sized_target inline for efficiency. This approach breaks
321 // static type checking, but is made safe using asserts.
323 template<int size, bool big_endian>
324 inline Sized_target<size, big_endian>*
325 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
327 assert(this->target_->get_size() == size);
328 assert(this->target_->is_big_endian() ? big_endian : !big_endian);
329 return static_cast<Sized_target<size, big_endian>*>(this->target_);
332 // A regular object (ET_REL). This is an abstract base class itself.
333 // The implementations is the template class Sized_relobj.
335 class Relobj : public Object
338 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
339 : Object(name, input_file, false, offset)
344 read_relocs(Read_relocs_data* rd)
345 { return this->do_read_relocs(rd); }
347 // Scan the relocs and adjust the symbol table.
349 scan_relocs(const General_options& options, Symbol_table* symtab,
350 Layout* layout, Read_relocs_data* rd)
351 { return this->do_scan_relocs(options, symtab, layout, rd); }
353 // Initial local symbol processing: set the offset where local
354 // symbol information will be stored; add local symbol names to
355 // *POOL; return the new local symbol index.
357 finalize_local_symbols(unsigned int index, off_t off, Stringpool* pool)
358 { return this->do_finalize_local_symbols(index, off, pool); }
360 // Relocate the input sections and write out the local symbols.
362 relocate(const General_options& options, const Symbol_table* symtab,
363 const Layout* layout, Output_file* of)
364 { return this->do_relocate(options, symtab, layout, of); }
366 // Return whether an input section is being included in the link.
368 is_section_included(unsigned int shnum) const
370 assert(shnum < this->map_to_output_.size());
371 return this->map_to_output_[shnum].output_section != NULL;
374 // Given a section index, return the corresponding Output_section
375 // (which will be NULL if the section is not included in the link)
376 // and set *POFF to the offset within that section.
377 inline Output_section*
378 output_section(unsigned int shnum, off_t* poff);
380 // Set the offset of an input section within its output section.
382 set_section_offset(unsigned int shndx, off_t off)
384 assert(shndx < this->map_to_output_.size());
385 this->map_to_output_[shndx].offset = off;
389 // What we need to know to map an input section to an output
390 // section. We keep an array of these, one for each input section,
391 // indexed by the input section number.
394 // The output section. This is NULL if the input section is to be
396 Output_section* output_section;
397 // The offset within the output section.
401 // Read the relocs--implemented by child class.
403 do_read_relocs(Read_relocs_data*) = 0;
405 // Scan the relocs--implemented by child class.
407 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
408 Read_relocs_data*) = 0;
410 // Finalize local symbols--implemented by child class.
412 do_finalize_local_symbols(unsigned int, off_t, Stringpool*) = 0;
414 // Relocate the input sections and write out the local
415 // symbols--implemented by child class.
417 do_relocate(const General_options& options, const Symbol_table* symtab,
418 const Layout*, Output_file* of) = 0;
420 // Return the vector mapping input sections to output sections.
421 std::vector<Map_to_output>&
423 { return this->map_to_output_; }
426 // Mapping from input sections to output section.
427 std::vector<Map_to_output> map_to_output_;
430 // Implement Object::output_section inline for efficiency.
431 inline Output_section*
432 Relobj::output_section(unsigned int shnum, off_t* poff)
434 assert(shnum < this->map_to_output_.size());
435 const Map_to_output& mo(this->map_to_output_[shnum]);
437 return mo.output_section;
440 // A regular object file. This is size and endian specific.
442 template<int size, bool big_endian>
443 class Sized_relobj : public Relobj
446 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
447 typedef std::vector<Address> Local_values;
449 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
450 const typename elfcpp::Ehdr<size, big_endian>&);
454 // Set up the object file based on the ELF header.
456 setup(const typename elfcpp::Ehdr<size, big_endian>&);
458 // Return the index of local symbol SYM in the ordinary symbol
459 // table. A value of -1U means that the symbol is not being output.
461 symtab_index(unsigned int sym) const
463 assert(sym < this->local_indexes_.size());
464 assert(this->local_indexes_[sym] != 0);
465 return this->local_indexes_[sym];
470 do_read_symbols(Read_symbols_data*);
472 // Lay out the input sections.
474 do_layout(const General_options&, Symbol_table*, Layout*,
477 // Add the symbols to the symbol table.
479 do_add_symbols(Symbol_table*, Read_symbols_data*);
483 do_read_relocs(Read_relocs_data*);
485 // Scan the relocs and adjust the symbol table.
487 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
490 // Finalize the local symbols.
492 do_finalize_local_symbols(unsigned int, off_t, Stringpool*);
494 // Relocate the input sections and write out the local symbols.
496 do_relocate(const General_options& options, const Symbol_table* symtab,
497 const Layout*, Output_file* of);
499 // Get the name of a section.
501 do_section_name(unsigned int shndx)
502 { return this->elf_file_.section_name(shndx); }
504 // Return the location of the contents of a section.
506 do_section_contents(unsigned int shndx)
507 { return this->elf_file_.section_contents(shndx); }
509 // Return the appropriate Sized_target structure.
510 Sized_target<size, big_endian>*
513 return this->Object::sized_target
514 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
515 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
520 typedef Sized_relobj<size, big_endian> This;
521 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
522 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
523 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
524 typedef elfcpp::Shdr<size, big_endian> Shdr;
526 // Find the SHT_SYMTAB section, given the section headers.
528 find_symtab(const unsigned char* pshdrs);
530 // Whether to include a section group in the link.
532 include_section_group(Layout*, unsigned int,
533 const elfcpp::Shdr<size, big_endian>&,
536 // Whether to include a linkonce section in the link.
538 include_linkonce_section(Layout*, const char*,
539 const elfcpp::Shdr<size, big_endian>&);
541 // Views and sizes when relocating.
545 typename elfcpp::Elf_types<size>::Elf_Addr address;
550 typedef std::vector<View_size> Views;
552 // Write section data to the output file. Record the views and
553 // sizes in VIEWS for use when relocating.
555 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
557 // Relocate the sections in the output file.
559 relocate_sections(const General_options& options, const Symbol_table*,
560 const Layout*, const unsigned char* pshdrs, Views*);
562 // Write out the local symbols.
564 write_local_symbols(Output_file*, const Stringpool*);
566 // General access to the ELF file.
567 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
568 // Index of SHT_SYMTAB section.
569 unsigned int symtab_shndx_;
570 // The number of local symbols.
571 unsigned int local_symbol_count_;
572 // The number of local symbols which go into the output file.
573 unsigned int output_local_symbol_count_;
574 // The entries in the symbol table for the external symbols.
576 // File offset for local symbols.
577 off_t local_symbol_offset_;
578 // Values of local symbols.
579 Local_values local_values_;
580 // Indexes of local symbols in the output file; -1U if not present.
581 std::vector<unsigned int> local_indexes_;
584 // A class to manage the list of all objects.
590 : relobj_list_(), target_(NULL)
593 // The type of the list of input relocateable objects.
594 typedef std::vector<Relobj*> Relobj_list;
595 typedef Relobj_list::const_iterator Relobj_iterator;
597 // The type of the list of input dynamic objects.
598 typedef std::vector<Dynobj*> Dynobj_list;
599 typedef Dynobj_list::const_iterator Dynobj_iterator;
601 // Add an object to the list.
605 // Get the target we should use for the output file.
608 { return this->target_; }
610 // Iterate over all regular objects.
614 { return this->relobj_list_.begin(); }
618 { return this->relobj_list_.end(); }
620 // Iterate over all dynamic objects.
624 { return this->dynobj_list_.begin(); }
628 { return this->dynobj_list_.end(); }
630 // Return whether we have seen any dynamic objects.
633 { return !this->dynobj_list_.empty(); }
636 Input_objects(const Input_objects&);
637 Input_objects& operator=(const Input_objects&);
639 Relobj_list relobj_list_;
640 Dynobj_list dynobj_list_;
644 // Some of the information we pass to the relocation routines. We
645 // group this together to avoid passing a dozen different arguments.
647 template<int size, bool big_endian>
650 // Command line options.
651 const General_options* options;
653 const Symbol_table* symtab;
655 const Layout* layout;
656 // Object being relocated.
657 Sized_relobj<size, big_endian>* object;
658 // Number of local symbols.
659 unsigned int local_symbol_count;
660 // Values of local symbols.
661 const typename Sized_relobj<size, big_endian>::Local_values* local_values;
663 const Symbol* const * symbols;
664 // Section index of relocation section.
665 unsigned int reloc_shndx;
666 // Section index of section being relocated.
667 unsigned int data_shndx;
669 // Return a string showing the location of a relocation. This is
670 // only used for error messages.
672 location(size_t relnum, off_t reloffset) const;
675 // Return an Object appropriate for the input file. P is BYTES long,
676 // and holds the ELF header.
679 make_elf_object(const std::string& name, Input_file*,
680 off_t offset, const unsigned char* p,
683 } // end namespace gold
685 #endif // !defined(GOLD_OBJECT_H)