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 offset following the local symbols.
357 finalize_local_symbols(off_t off
, Stringpool
* pool
)
358 { return this->do_finalize_local_symbols(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(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 Sized_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
447 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
451 // Set up the object file based on the ELF header.
453 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
457 do_read_symbols(Read_symbols_data
*);
459 // Lay out the input sections.
461 do_layout(const General_options
&, Symbol_table
*, Layout
*,
464 // Add the symbols to the symbol table.
466 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
470 do_read_relocs(Read_relocs_data
*);
472 // Scan the relocs and adjust the symbol table.
474 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
477 // Finalize the local symbols.
479 do_finalize_local_symbols(off_t
, Stringpool
*);
481 // Relocate the input sections and write out the local symbols.
483 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
484 const Layout
*, Output_file
* of
);
486 // Get the name of a section.
488 do_section_name(unsigned int shndx
)
489 { return this->elf_file_
.section_name(shndx
); }
491 // Return the location of the contents of a section.
493 do_section_contents(unsigned int shndx
)
494 { return this->elf_file_
.section_contents(shndx
); }
496 // Return the appropriate Sized_target structure.
497 Sized_target
<size
, big_endian
>*
500 return this->Object::sized_target
501 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
502 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
507 typedef Sized_relobj
<size
, big_endian
> This
;
508 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
509 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
510 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
511 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
513 // Find the SHT_SYMTAB section, given the section headers.
515 find_symtab(const unsigned char* pshdrs
);
517 // Whether to include a section group in the link.
519 include_section_group(Layout
*, unsigned int,
520 const elfcpp::Shdr
<size
, big_endian
>&,
523 // Whether to include a linkonce section in the link.
525 include_linkonce_section(Layout
*, const char*,
526 const elfcpp::Shdr
<size
, big_endian
>&);
528 // Views and sizes when relocating.
532 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
537 typedef std::vector
<View_size
> Views
;
539 // Write section data to the output file. Record the views and
540 // sizes in VIEWS for use when relocating.
542 write_sections(const unsigned char* pshdrs
, Output_file
*, Views
*);
544 // Relocate the sections in the output file.
546 relocate_sections(const General_options
& options
, const Symbol_table
*,
547 const Layout
*, const unsigned char* pshdrs
, Views
*);
549 // Write out the local symbols.
551 write_local_symbols(Output_file
*, const Stringpool
*);
553 // General access to the ELF file.
554 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
555 // Index of SHT_SYMTAB section.
556 unsigned int symtab_shndx_
;
557 // The number of local symbols.
558 unsigned int local_symbol_count_
;
559 // The number of local symbols which go into the output file.
560 unsigned int output_local_symbol_count_
;
561 // The entries in the symbol table for the external symbols.
563 // File offset for local symbols.
564 off_t local_symbol_offset_
;
565 // Values of local symbols.
566 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*values_
;
569 // A class to manage the list of all objects.
575 : relobj_list_(), target_(NULL
)
578 // The type of the list of input relocateable objects.
579 typedef std::vector
<Relobj
*> Relobj_list
;
580 typedef Relobj_list::const_iterator Relobj_iterator
;
582 // The type of the list of input dynamic objects.
583 typedef std::vector
<Dynobj
*> Dynobj_list
;
584 typedef Dynobj_list::const_iterator Dynobj_iterator
;
586 // Add an object to the list.
590 // Get the target we should use for the output file.
593 { return this->target_
; }
595 // Iterate over all regular objects.
599 { return this->relobj_list_
.begin(); }
603 { return this->relobj_list_
.end(); }
605 // Iterate over all dynamic objects.
609 { return this->dynobj_list_
.begin(); }
613 { return this->dynobj_list_
.end(); }
615 // Return whether we have seen any dynamic objects.
618 { return !this->dynobj_list_
.empty(); }
621 Input_objects(const Input_objects
&);
622 Input_objects
& operator=(const Input_objects
&);
624 Relobj_list relobj_list_
;
625 Dynobj_list dynobj_list_
;
629 // Some of the information we pass to the relocation routines. We
630 // group this together to avoid passing a dozen different arguments.
632 template<int size
, bool big_endian
>
635 // Command line options.
636 const General_options
* options
;
638 const Symbol_table
* symtab
;
640 const Layout
* layout
;
641 // Object being relocated.
642 Sized_relobj
<size
, big_endian
>* object
;
643 // Number of local symbols.
644 unsigned int local_symbol_count
;
645 // Values of local symbols.
646 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*values
;
649 // Section index of relocation section.
650 unsigned int reloc_shndx
;
651 // Section index of section being relocated.
652 unsigned int data_shndx
;
654 // Return a string showing the location of a relocation. This is
655 // only used for error messages.
657 location(size_t relnum
, off_t reloffset
) const;
660 // Return an Object appropriate for the input file. P is BYTES long,
661 // and holds the ELF header.
664 make_elf_object(const std::string
& name
, Input_file
*,
665 off_t offset
, const unsigned char* p
,
668 } // end namespace gold
670 #endif // !defined(GOLD_OBJECT_H)