]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/object.h
More dynamic object support, initial scripting support.
[thirdparty/binutils-gdb.git] / gold / object.h
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
2
3 #ifndef GOLD_OBJECT_H
4 #define GOLD_OBJECT_H
5
6 #include <cassert>
7 #include <string>
8 #include <vector>
9
10 #include "elfcpp.h"
11 #include "elfcpp_file.h"
12 #include "fileread.h"
13 #include "target.h"
14
15 namespace gold
16 {
17
18 class General_options;
19 class Stringpool;
20 class Layout;
21 class Output_section;
22 class Output_file;
23 class Dynobj;
24
25 // Data to pass from read_symbols() to add_symbols().
26
27 struct Read_symbols_data
28 {
29 // Section headers.
30 File_view* section_headers;
31 // Section names.
32 File_view* section_names;
33 // Size of section name data in bytes.
34 off_t section_names_size;
35 // Symbol data.
36 File_view* symbols;
37 // Size of symbol data in bytes.
38 off_t symbols_size;
39 // Symbol names.
40 File_view* symbol_names;
41 // Size of symbol name data in bytes.
42 off_t symbol_names_size;
43
44 // Version information. This is only used on dynamic objects.
45 // Version symbol data (from SHT_GNU_versym section).
46 File_view* versym;
47 off_t versym_size;
48 // Version definition data (from SHT_GNU_verdef section).
49 File_view* verdef;
50 off_t verdef_size;
51 unsigned int verdef_info;
52 // Needed version data (from SHT_GNU_verneed section).
53 File_view* verneed;
54 off_t verneed_size;
55 unsigned int verneed_info;
56 };
57
58 // Data about a single relocation section. This is read in
59 // read_relocs and processed in scan_relocs.
60
61 struct Section_relocs
62 {
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.
68 File_view* contents;
69 // Reloc section type.
70 unsigned int sh_type;
71 // Number of reloc entries.
72 size_t reloc_count;
73 };
74
75 // Relocations in an object file. This is read in read_relocs and
76 // processed in scan_relocs.
77
78 struct Read_relocs_data
79 {
80 typedef std::vector<Section_relocs> Relocs_list;
81 // The relocations.
82 Relocs_list relocs;
83 // The local symbols.
84 File_view* local_symbols;
85 };
86
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).
90
91 class Object
92 {
93 public:
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,
99 off_t offset = 0)
100 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
101 is_dynamic_(is_dynamic), target_(NULL)
102 { }
103
104 virtual ~Object()
105 { }
106
107 // Return the name of the object as we would report it to the tuser.
108 const std::string&
109 name() const
110 { return this->name_; }
111
112 // Return whether this is a dynamic object.
113 bool
114 is_dynamic() const
115 { return this->is_dynamic_; }
116
117 // Return the target structure associated with this object.
118 Target*
119 target() const
120 { return this->target_; }
121
122 // Lock the underlying file.
123 void
124 lock()
125 { this->input_file_->file().lock(); }
126
127 // Unlock the underlying file.
128 void
129 unlock()
130 { this->input_file_->file().unlock(); }
131
132 // Return whether the underlying file is locked.
133 bool
134 is_locked() const
135 { return this->input_file_->file().is_locked(); }
136
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);
143
144 // Read the symbol information.
145 void
146 read_symbols(Read_symbols_data* sd)
147 { return this->do_read_symbols(sd); }
148
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.
151 void
152 layout(const General_options& options, Symbol_table* symtab,
153 Layout* layout, Read_symbols_data* sd)
154 { this->do_layout(options, symtab, layout, sd); }
155
156 // Add symbol information to the global symbol table.
157 void
158 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
159 { this->do_add_symbols(symtab, sd); }
160
161 // Return a view of the contents of a section. Set *PLEN to the
162 // size.
163 const unsigned char*
164 section_contents(unsigned int shndx, off_t* plen);
165
166 // Return the name of a section given a section index. This is only
167 // used for error messages.
168 std::string
169 section_name(unsigned int shnum)
170 { return this->do_section_name(shnum); }
171
172 // Functions and types for the elfcpp::Elf_file interface. This
173 // permit us to use Object as the File template parameter for
174 // elfcpp::Elf_file.
175
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
178 // need.
179 class View
180 {
181 public:
182 View(const unsigned char* p)
183 : p_(p)
184 { }
185
186 const unsigned char*
187 data() const
188 { return this->p_; }
189
190 private:
191 const unsigned char* p_;
192 };
193
194 // Return a View.
195 View
196 view(off_t file_offset, off_t data_size)
197 { return View(this->get_view(file_offset, data_size)); }
198
199 // Report an error.
200 void
201 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
202
203 // A location in the file.
204 struct Location
205 {
206 off_t file_offset;
207 off_t data_size;
208
209 Location(off_t fo, off_t ds)
210 : file_offset(fo), data_size(ds)
211 { }
212 };
213
214 // Get a View given a Location.
215 View view(Location loc)
216 { return View(this->get_view(loc.file_offset, loc.data_size)); }
217
218 protected:
219 // Read the symbols--implemented by child class.
220 virtual void
221 do_read_symbols(Read_symbols_data*) = 0;
222
223 // Lay out sections--implemented by child class.
224 virtual void
225 do_layout(const General_options&, Symbol_table*, Layout*,
226 Read_symbols_data*) = 0;
227
228 // Add symbol information to the global symbol table--implemented by
229 // child class.
230 virtual void
231 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
232
233 // Return the location of the contents of a section. Implemented by
234 // child class.
235 virtual Location
236 do_section_contents(unsigned int shnum) = 0;
237
238 // Get the name of a section--implemented by child class.
239 virtual std::string
240 do_section_name(unsigned int shnum) = 0;
241
242 // Get the file.
243 Input_file*
244 input_file() const
245 { return this->input_file_; }
246
247 // Get the offset into the file.
248 off_t
249 offset() const
250 { return this->offset_; }
251
252 // Get a view into the underlying file.
253 const unsigned char*
254 get_view(off_t start, off_t size)
255 { return this->input_file_->file().get_view(start + this->offset_, size); }
256
257 // Get a lasting view into the underlying file.
258 File_view*
259 get_lasting_view(off_t start, off_t size)
260 {
261 return this->input_file_->file().get_lasting_view(start + this->offset_,
262 size);
263 }
264
265 // Read data from the underlying file.
266 void
267 read(off_t start, off_t size, void* p)
268 { this->input_file_->file().read(start + this->offset_, size, p); }
269
270 // Set the target.
271 void
272 set_target(int machine, int size, bool big_endian, int osabi,
273 int abiversion);
274
275 // Get the number of sections.
276 unsigned int
277 shnum() const
278 { return this->shnum_; }
279
280 // Set the number of sections.
281 void
282 set_shnum(int shnum)
283 { this->shnum_ = shnum; }
284
285 // Functions used by both Sized_relobj and Sized_dynobj.
286
287 // Read the section data into a Read_symbols_data object.
288 template<int size, bool big_endian>
289 void
290 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
291 Read_symbols_data*);
292
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.
296 bool
297 handle_gnu_warning_section(const char* name, unsigned int shndx,
298 Symbol_table*);
299
300 private:
301 // This class may not be copied.
302 Object(const Object&);
303 Object& operator=(const Object&);
304
305 // Name of object as printed to user.
306 std::string name_;
307 // For reading the file.
308 Input_file* input_file_;
309 // Offset within the file--0 for an object file, non-0 for an
310 // archive.
311 off_t offset_;
312 // Number of input sections.
313 unsigned int shnum_;
314 // Whether this is a dynamic object.
315 bool is_dynamic_;
316 // Target functions--may be NULL if the target is not known.
317 Target* target_;
318 };
319
320 // Implement sized_target inline for efficiency. This approach breaks
321 // static type checking, but is made safe using asserts.
322
323 template<int size, bool big_endian>
324 inline Sized_target<size, big_endian>*
325 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
326 {
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_);
330 }
331
332 // A regular object (ET_REL). This is an abstract base class itself.
333 // The implementations is the template class Sized_relobj.
334
335 class Relobj : public Object
336 {
337 public:
338 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
339 : Object(name, input_file, false, offset)
340 { }
341
342 // Read the relocs.
343 void
344 read_relocs(Read_relocs_data* rd)
345 { return this->do_read_relocs(rd); }
346
347 // Scan the relocs and adjust the symbol table.
348 void
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); }
352
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.
356 off_t
357 finalize_local_symbols(off_t off, Stringpool* pool)
358 { return this->do_finalize_local_symbols(off, pool); }
359
360 // Relocate the input sections and write out the local symbols.
361 void
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); }
365
366 // Return whether an input section is being included in the link.
367 bool
368 is_section_included(unsigned int shnum) const
369 {
370 assert(shnum < this->map_to_output_.size());
371 return this->map_to_output_[shnum].output_section != NULL;
372 }
373
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);
379
380 // Set the offset of an input section within its output section.
381 void
382 set_section_offset(unsigned int shndx, off_t off)
383 {
384 assert(shndx < this->map_to_output_.size());
385 this->map_to_output_[shndx].offset = off;
386 }
387
388 protected:
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.
392 struct Map_to_output
393 {
394 // The output section. This is NULL if the input section is to be
395 // discarded.
396 Output_section* output_section;
397 // The offset within the output section.
398 off_t offset;
399 };
400
401 // Read the relocs--implemented by child class.
402 virtual void
403 do_read_relocs(Read_relocs_data*) = 0;
404
405 // Scan the relocs--implemented by child class.
406 virtual void
407 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
408 Read_relocs_data*) = 0;
409
410 // Finalize local symbols--implemented by child class.
411 virtual off_t
412 do_finalize_local_symbols(off_t, Stringpool*) = 0;
413
414 // Relocate the input sections and write out the local
415 // symbols--implemented by child class.
416 virtual void
417 do_relocate(const General_options& options, const Symbol_table* symtab,
418 const Layout*, Output_file* of) = 0;
419
420 // Return the vector mapping input sections to output sections.
421 std::vector<Map_to_output>&
422 map_to_output()
423 { return this->map_to_output_; }
424
425 private:
426 // Mapping from input sections to output section.
427 std::vector<Map_to_output> map_to_output_;
428 };
429
430 // Implement Object::output_section inline for efficiency.
431 inline Output_section*
432 Relobj::output_section(unsigned int shnum, off_t* poff)
433 {
434 assert(shnum < this->map_to_output_.size());
435 const Map_to_output& mo(this->map_to_output_[shnum]);
436 *poff = mo.offset;
437 return mo.output_section;
438 }
439
440 // A regular object file. This is size and endian specific.
441
442 template<int size, bool big_endian>
443 class Sized_relobj : public Relobj
444 {
445 public:
446 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
447 const typename elfcpp::Ehdr<size, big_endian>&);
448
449 ~Sized_relobj();
450
451 // Set up the object file based on the ELF header.
452 void
453 setup(const typename elfcpp::Ehdr<size, big_endian>&);
454
455 // Read the symbols.
456 void
457 do_read_symbols(Read_symbols_data*);
458
459 // Lay out the input sections.
460 void
461 do_layout(const General_options&, Symbol_table*, Layout*,
462 Read_symbols_data*);
463
464 // Add the symbols to the symbol table.
465 void
466 do_add_symbols(Symbol_table*, Read_symbols_data*);
467
468 // Read the relocs.
469 void
470 do_read_relocs(Read_relocs_data*);
471
472 // Scan the relocs and adjust the symbol table.
473 void
474 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
475 Read_relocs_data*);
476
477 // Finalize the local symbols.
478 off_t
479 do_finalize_local_symbols(off_t, Stringpool*);
480
481 // Relocate the input sections and write out the local symbols.
482 void
483 do_relocate(const General_options& options, const Symbol_table* symtab,
484 const Layout*, Output_file* of);
485
486 // Get the name of a section.
487 std::string
488 do_section_name(unsigned int shndx)
489 { return this->elf_file_.section_name(shndx); }
490
491 // Return the location of the contents of a section.
492 Object::Location
493 do_section_contents(unsigned int shndx)
494 { return this->elf_file_.section_contents(shndx); }
495
496 // Return the appropriate Sized_target structure.
497 Sized_target<size, big_endian>*
498 sized_target()
499 {
500 return this->Object::sized_target
501 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
502 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
503 }
504
505 private:
506 // For convenience.
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;
512
513 // Find the SHT_SYMTAB section, given the section headers.
514 void
515 find_symtab(const unsigned char* pshdrs);
516
517 // Whether to include a section group in the link.
518 bool
519 include_section_group(Layout*, unsigned int,
520 const elfcpp::Shdr<size, big_endian>&,
521 std::vector<bool>*);
522
523 // Whether to include a linkonce section in the link.
524 bool
525 include_linkonce_section(Layout*, const char*,
526 const elfcpp::Shdr<size, big_endian>&);
527
528 // Views and sizes when relocating.
529 struct View_size
530 {
531 unsigned char* view;
532 typename elfcpp::Elf_types<size>::Elf_Addr address;
533 off_t offset;
534 off_t view_size;
535 };
536
537 typedef std::vector<View_size> Views;
538
539 // Write section data to the output file. Record the views and
540 // sizes in VIEWS for use when relocating.
541 void
542 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
543
544 // Relocate the sections in the output file.
545 void
546 relocate_sections(const General_options& options, const Symbol_table*,
547 const Layout*, const unsigned char* pshdrs, Views*);
548
549 // Write out the local symbols.
550 void
551 write_local_symbols(Output_file*, const Stringpool*);
552
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.
562 Symbol** 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_;
567 };
568
569 // A class to manage the list of all objects.
570
571 class Input_objects
572 {
573 public:
574 Input_objects()
575 : relobj_list_(), target_(NULL)
576 { }
577
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;
581
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;
585
586 // Add an object to the list.
587 void
588 add_object(Object*);
589
590 // Get the target we should use for the output file.
591 Target*
592 target() const
593 { return this->target_; }
594
595 // Iterate over all regular objects.
596
597 Relobj_iterator
598 relobj_begin() const
599 { return this->relobj_list_.begin(); }
600
601 Relobj_iterator
602 relobj_end() const
603 { return this->relobj_list_.end(); }
604
605 // Iterate over all dynamic objects.
606
607 Dynobj_iterator
608 dynobj_begin() const
609 { return this->dynobj_list_.begin(); }
610
611 Dynobj_iterator
612 dynobj_end() const
613 { return this->dynobj_list_.end(); }
614
615 // Return whether we have seen any dynamic objects.
616 bool
617 any_dynamic() const
618 { return !this->dynobj_list_.empty(); }
619
620 private:
621 Input_objects(const Input_objects&);
622 Input_objects& operator=(const Input_objects&);
623
624 Relobj_list relobj_list_;
625 Dynobj_list dynobj_list_;
626 Target* target_;
627 };
628
629 // Some of the information we pass to the relocation routines. We
630 // group this together to avoid passing a dozen different arguments.
631
632 template<int size, bool big_endian>
633 struct Relocate_info
634 {
635 // Command line options.
636 const General_options* options;
637 // Symbol table.
638 const Symbol_table* symtab;
639 // Layout.
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;
647 // Global symbols.
648 Symbol** symbols;
649 // Section index of relocation section.
650 unsigned int reloc_shndx;
651 // Section index of section being relocated.
652 unsigned int data_shndx;
653
654 // Return a string showing the location of a relocation. This is
655 // only used for error messages.
656 std::string
657 location(size_t relnum, off_t reloffset) const;
658 };
659
660 // Return an Object appropriate for the input file. P is BYTES long,
661 // and holds the ELF header.
662
663 extern Object*
664 make_elf_object(const std::string& name, Input_file*,
665 off_t offset, const unsigned char* p,
666 off_t bytes);
667
668 } // end namespace gold
669
670 #endif // !defined(GOLD_OBJECT_H)