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
;
45 // Data about a single relocation section. This is read in
46 // read_relocs and processed in scan_relocs.
50 // Index of reloc section.
51 unsigned int reloc_shndx
;
52 // Index of section that relocs apply to.
53 unsigned int data_shndx
;
54 // Contents of reloc section.
56 // Reloc section type.
58 // Number of reloc entries.
62 // Relocations in an object file. This is read in read_relocs and
63 // processed in scan_relocs.
65 struct Read_relocs_data
67 typedef std::vector
<Section_relocs
> Relocs_list
;
71 File_view
* local_symbols
;
74 // Object is an abstract base class which represents either a 32-bit
75 // or a 64-bit input object. This can be a regular object file
76 // (ET_REL) or a shared object (ET_DYN).
81 // NAME is the name of the object as we would report it to the user
82 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
83 // used to read the file. OFFSET is the offset within the input
84 // file--0 for a .o or .so file, something else for a .a file.
85 Object(const std::string
& name
, Input_file
* input_file
, bool is_dynamic
,
87 : name_(name
), input_file_(input_file
), offset_(offset
),
88 is_dynamic_(is_dynamic
), target_(NULL
)
94 // Return the name of the object as we would report it to the tuser.
97 { return this->name_
; }
99 // Return whether this is a dynamic object.
102 { return this->is_dynamic_
; }
104 // Return the target structure associated with this object.
107 { return this->target_
; }
109 // Lock the underlying file.
112 { this->input_file_
->file().lock(); }
114 // Unlock the underlying file.
117 { this->input_file_
->file().unlock(); }
119 // Return whether the underlying file is locked.
122 { return this->input_file_
->file().is_locked(); }
124 // Return the sized target structure associated with this object.
125 // This is like the target method but it returns a pointer of
126 // appropriate checked type.
127 template<int size
, bool big_endian
>
128 Sized_target
<size
, big_endian
>*
129 sized_target(ACCEPT_SIZE_ENDIAN_ONLY
);
131 // Read the symbol information.
133 read_symbols(Read_symbols_data
* sd
)
134 { return this->do_read_symbols(sd
); }
136 // Pass sections which should be included in the link to the Layout
137 // object, and record where the sections go in the output file.
139 layout(const General_options
& options
, Symbol_table
* symtab
,
140 Layout
* layout
, Read_symbols_data
* sd
)
141 { this->do_layout(options
, symtab
, layout
, sd
); }
143 // Add symbol information to the global symbol table.
145 add_symbols(Symbol_table
* symtab
, Read_symbols_data
* sd
)
146 { this->do_add_symbols(symtab
, sd
); }
148 // Return a view of the contents of a section. Set *PLEN to the
151 section_contents(unsigned int shndx
, off_t
* plen
);
153 // Return the name of a section given a section index. This is only
154 // used for error messages.
156 section_name(unsigned int shnum
)
157 { return this->do_section_name(shnum
); }
159 // Functions and types for the elfcpp::Elf_file interface. This
160 // permit us to use Object as the File template parameter for
163 // The View class is returned by view. It must support a single
164 // method, data(). This is trivial, because get_view does what we
169 View(const unsigned char* p
)
178 const unsigned char* p_
;
183 view(off_t file_offset
, off_t data_size
)
184 { return View(this->get_view(file_offset
, data_size
)); }
188 error(const char* format
, ...) ATTRIBUTE_PRINTF_2
;
190 // A location in the file.
196 Location(off_t fo
, off_t ds
)
197 : file_offset(fo
), data_size(ds
)
201 // Get a View given a Location.
202 View
view(Location loc
)
203 { return View(this->get_view(loc
.file_offset
, loc
.data_size
)); }
206 // Read the symbols--implemented by child class.
208 do_read_symbols(Read_symbols_data
*) = 0;
210 // Lay out sections--implemented by child class.
212 do_layout(const General_options
&, Symbol_table
*, Layout
*,
213 Read_symbols_data
*) = 0;
215 // Add symbol information to the global symbol table--implemented by
218 do_add_symbols(Symbol_table
*, Read_symbols_data
*) = 0;
220 // Return the location of the contents of a section. Implemented by
223 do_section_contents(unsigned int shnum
) = 0;
225 // Get the name of a section--implemented by child class.
227 do_section_name(unsigned int shnum
) = 0;
232 { return this->input_file_
; }
234 // Get the offset into the file.
237 { return this->offset_
; }
239 // Get a view into the underlying file.
241 get_view(off_t start
, off_t size
)
242 { return this->input_file_
->file().get_view(start
+ this->offset_
, size
); }
244 // Get a lasting view into the underlying file.
246 get_lasting_view(off_t start
, off_t size
)
248 return this->input_file_
->file().get_lasting_view(start
+ this->offset_
,
252 // Read data from the underlying file.
254 read(off_t start
, off_t size
, void* p
)
255 { this->input_file_
->file().read(start
+ this->offset_
, size
, p
); }
259 set_target(Target
* target
)
260 { this->target_
= target
; }
263 // This class may not be copied.
264 Object(const Object
&);
265 Object
& operator=(const Object
&);
267 // Name of object as printed to user.
269 // For reading the file.
270 Input_file
* input_file_
;
271 // Offset within the file--0 for an object file, non-0 for an
274 // Whether this is a dynamic object.
276 // Target functions--may be NULL if the target is not known.
280 // Implement sized_target inline for efficiency. This approach breaks
281 // static type checking, but is made safe using asserts.
283 template<int size
, bool big_endian
>
284 inline Sized_target
<size
, big_endian
>*
285 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY
)
287 assert(this->target_
->get_size() == size
);
288 assert(this->target_
->is_big_endian() ? big_endian
: !big_endian
);
289 return static_cast<Sized_target
<size
, big_endian
>*>(this->target_
);
292 // A regular object (ET_REL). This is an abstract base class itself.
293 // The implementations is the template class Sized_relobj.
295 class Relobj
: public Object
298 Relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
= 0)
299 : Object(name
, input_file
, false, offset
)
304 read_relocs(Read_relocs_data
* rd
)
305 { return this->do_read_relocs(rd
); }
307 // Scan the relocs and adjust the symbol table.
309 scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
310 Layout
* layout
, Read_relocs_data
* rd
)
311 { return this->do_scan_relocs(options
, symtab
, layout
, rd
); }
313 // Initial local symbol processing: set the offset where local
314 // symbol information will be stored; add local symbol names to
315 // *POOL; return the offset following the local symbols.
317 finalize_local_symbols(off_t off
, Stringpool
* pool
)
318 { return this->do_finalize_local_symbols(off
, pool
); }
320 // Relocate the input sections and write out the local symbols.
322 relocate(const General_options
& options
, const Symbol_table
* symtab
,
323 const Layout
* layout
, Output_file
* of
)
324 { return this->do_relocate(options
, symtab
, layout
, of
); }
326 // Return whether an input section is being included in the link.
328 is_section_included(unsigned int shnum
) const
330 assert(shnum
< this->map_to_output_
.size());
331 return this->map_to_output_
[shnum
].output_section
!= NULL
;
334 // Given a section index, return the corresponding Output_section
335 // (which will be NULL if the section is not included in the link)
336 // and set *POFF to the offset within that section.
337 inline Output_section
*
338 output_section(unsigned int shnum
, off_t
* poff
);
340 // Set the offset of an input section within its output section.
342 set_section_offset(unsigned int shndx
, off_t off
)
344 assert(shndx
< this->map_to_output_
.size());
345 this->map_to_output_
[shndx
].offset
= off
;
349 // What we need to know to map an input section to an output
350 // section. We keep an array of these, one for each input section,
351 // indexed by the input section number.
354 // The output section. This is NULL if the input section is to be
356 Output_section
* output_section
;
357 // The offset within the output section.
361 // Read the relocs--implemented by child class.
363 do_read_relocs(Read_relocs_data
*) = 0;
365 // Scan the relocs--implemented by child class.
367 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
368 Read_relocs_data
*) = 0;
370 // Finalize local symbols--implemented by child class.
372 do_finalize_local_symbols(off_t
, Stringpool
*) = 0;
374 // Relocate the input sections and write out the local
375 // symbols--implemented by child class.
377 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
378 const Layout
*, Output_file
* of
) = 0;
380 // Get the number of sections.
383 { return this->shnum_
; }
385 // Set the number of sections.
388 { this->shnum_
= shnum
; }
390 // Return the vector mapping input sections to output sections.
391 std::vector
<Map_to_output
>&
393 { return this->map_to_output_
; }
396 // Number of input sections.
398 // Mapping from input sections to output section.
399 std::vector
<Map_to_output
> map_to_output_
;
402 // Implement Object::output_section inline for efficiency.
403 inline Output_section
*
404 Relobj::output_section(unsigned int shnum
, off_t
* poff
)
406 assert(shnum
< this->map_to_output_
.size());
407 const Map_to_output
& mo(this->map_to_output_
[shnum
]);
409 return mo
.output_section
;
412 // A regular object file. This is size and endian specific.
414 template<int size
, bool big_endian
>
415 class Sized_relobj
: public Relobj
418 Sized_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
419 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
423 // Set up the object file based on the ELF header.
425 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
429 do_read_symbols(Read_symbols_data
*);
431 // Add the symbols to the symbol table.
433 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
437 do_read_relocs(Read_relocs_data
*);
439 // Scan the relocs and adjust the symbol table.
441 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
444 // Lay out the input sections.
446 do_layout(const General_options
&, Symbol_table
*, Layout
*,
449 // Finalize the local symbols.
451 do_finalize_local_symbols(off_t
, Stringpool
*);
453 // Relocate the input sections and write out the local symbols.
455 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
456 const Layout
*, Output_file
* of
);
458 // Get the name of a section.
460 do_section_name(unsigned int shndx
)
461 { return this->elf_file_
.section_name(shndx
); }
463 // Return the location of the contents of a section.
465 do_section_contents(unsigned int shndx
)
466 { return this->elf_file_
.section_contents(shndx
); }
468 // Return the appropriate Sized_target structure.
469 Sized_target
<size
, big_endian
>*
472 return this->Object::sized_target
473 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
474 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
479 typedef Sized_relobj
<size
, big_endian
> This
;
480 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
481 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
482 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
483 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
485 // Whether to include a section group in the link.
487 include_section_group(Layout
*, unsigned int,
488 const elfcpp::Shdr
<size
, big_endian
>&,
491 // Whether to include a linkonce section in the link.
493 include_linkonce_section(Layout
*, const char*,
494 const elfcpp::Shdr
<size
, big_endian
>&);
496 // Views and sizes when relocating.
500 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
505 typedef std::vector
<View_size
> Views
;
507 // Write section data to the output file. Record the views and
508 // sizes in VIEWS for use when relocating.
510 write_sections(const unsigned char* pshdrs
, Output_file
*, Views
*);
512 // Relocate the sections in the output file.
514 relocate_sections(const General_options
& options
, const Symbol_table
*,
515 const Layout
*, const unsigned char* pshdrs
, Views
*);
517 // Write out the local symbols.
519 write_local_symbols(Output_file
*, const Stringpool
*);
521 // General access to the ELF file.
522 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
523 // If non-NULL, a view of the section header data.
524 File_view
* section_headers_
;
525 // Index of SHT_SYMTAB section.
526 unsigned int symtab_shndx_
;
527 // The number of local symbols.
528 unsigned int local_symbol_count_
;
529 // The number of local symbols which go into the output file.
530 unsigned int output_local_symbol_count_
;
531 // The entries in the symbol table for the external symbols.
533 // File offset for local symbols.
534 off_t local_symbol_offset_
;
535 // Values of local symbols.
536 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*values_
;
539 // A class to manage the list of all objects.
545 : relobj_list_(), target_(NULL
)
548 // The type of the list of input relocateable objects.
549 typedef std::vector
<Relobj
*> Relobj_list
;
550 typedef Relobj_list::const_iterator Relobj_iterator
;
552 // The type of the list of input dynamic objects.
553 typedef std::vector
<Dynobj
*> Dynobj_list
;
554 typedef Dynobj_list::const_iterator Dynobj_iterator
;
556 // Add an object to the list.
560 // Get the target we should use for the output file.
563 { return this->target_
; }
565 // Iterate over all regular objects.
569 { return this->relobj_list_
.begin(); }
573 { return this->relobj_list_
.end(); }
575 // Iterate over all dynamic objects.
579 { return this->dynobj_list_
.begin(); }
583 { return this->dynobj_list_
.end(); }
585 // Return whether we have seen any dynamic objects.
588 { return !this->dynobj_list_
.empty(); }
591 Input_objects(const Input_objects
&);
592 Input_objects
& operator=(const Input_objects
&);
594 Relobj_list relobj_list_
;
595 Dynobj_list dynobj_list_
;
599 // Some of the information we pass to the relocation routines. We
600 // group this together to avoid passing a dozen different arguments.
602 template<int size
, bool big_endian
>
605 // Command line options.
606 const General_options
* options
;
608 const Symbol_table
* symtab
;
610 const Layout
* layout
;
611 // Object being relocated.
612 Sized_relobj
<size
, big_endian
>* object
;
613 // Number of local symbols.
614 unsigned int local_symbol_count
;
615 // Values of local symbols.
616 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*values
;
619 // Section index of relocation section.
620 unsigned int reloc_shndx
;
621 // Section index of section being relocated.
622 unsigned int data_shndx
;
624 // Return a string showing the location of a relocation. This is
625 // only used for error messages.
627 location(size_t relnum
, off_t reloffset
) const;
630 // Return an Object appropriate for the input file. P is BYTES long,
631 // and holds the ELF header.
634 make_elf_object(const std::string
& name
, Input_file
*,
635 off_t offset
, const unsigned char* p
,
638 } // end namespace gold
640 #endif // !defined(GOLD_OBJECT_H)