9fdb9a372083000437ce33b2e19016d42741e5d5
[deliverable/binutils-gdb.git] / gold / object.h
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_OBJECT_H
24 #define GOLD_OBJECT_H
25
26 #include <string>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "elfcpp_file.h"
31 #include "fileread.h"
32 #include "target.h"
33
34 namespace gold
35 {
36
37 class General_options;
38 class Layout;
39 class Output_section;
40 class Output_file;
41 class Dynobj;
42
43 template<typename Stringpool_char>
44 class Stringpool_template;
45
46 // Data to pass from read_symbols() to add_symbols().
47
48 struct Read_symbols_data
49 {
50 // Section headers.
51 File_view* section_headers;
52 // Section names.
53 File_view* section_names;
54 // Size of section name data in bytes.
55 off_t section_names_size;
56 // Symbol data.
57 File_view* symbols;
58 // Size of symbol data in bytes.
59 off_t symbols_size;
60 // Symbol names.
61 File_view* symbol_names;
62 // Size of symbol name data in bytes.
63 off_t symbol_names_size;
64
65 // Version information. This is only used on dynamic objects.
66 // Version symbol data (from SHT_GNU_versym section).
67 File_view* versym;
68 off_t versym_size;
69 // Version definition data (from SHT_GNU_verdef section).
70 File_view* verdef;
71 off_t verdef_size;
72 unsigned int verdef_info;
73 // Needed version data (from SHT_GNU_verneed section).
74 File_view* verneed;
75 off_t verneed_size;
76 unsigned int verneed_info;
77 };
78
79 // Information used to print error messages.
80
81 struct Symbol_location_info
82 {
83 std::string source_file;
84 std::string enclosing_symbol_name;
85 int line_number;
86 };
87
88 // Data about a single relocation section. This is read in
89 // read_relocs and processed in scan_relocs.
90
91 struct Section_relocs
92 {
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.
98 File_view* contents;
99 // Reloc section type.
100 unsigned int sh_type;
101 // Number of reloc entries.
102 size_t reloc_count;
103 };
104
105 // Relocations in an object file. This is read in read_relocs and
106 // processed in scan_relocs.
107
108 struct Read_relocs_data
109 {
110 typedef std::vector<Section_relocs> Relocs_list;
111 // The relocations.
112 Relocs_list relocs;
113 // The local symbols.
114 File_view* local_symbols;
115 };
116
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).
120
121 class Object
122 {
123 public:
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,
129 off_t offset = 0)
130 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
131 is_dynamic_(is_dynamic), target_(NULL)
132 { }
133
134 virtual ~Object()
135 { }
136
137 // Return the name of the object as we would report it to the tuser.
138 const std::string&
139 name() const
140 { return this->name_; }
141
142 // Get the offset into the file.
143 off_t
144 offset() const
145 { return this->offset_; }
146
147 // Return whether this is a dynamic object.
148 bool
149 is_dynamic() const
150 { return this->is_dynamic_; }
151
152 // Return the target structure associated with this object.
153 Target*
154 target() const
155 { return this->target_; }
156
157 // Lock the underlying file.
158 void
159 lock()
160 { this->input_file_->file().lock(); }
161
162 // Unlock the underlying file.
163 void
164 unlock()
165 { this->input_file_->file().unlock(); }
166
167 // Return whether the underlying file is locked.
168 bool
169 is_locked() const
170 { return this->input_file_->file().is_locked(); }
171
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);
178
179 // Get the number of sections.
180 unsigned int
181 shnum() const
182 { return this->shnum_; }
183
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.
186 const unsigned char*
187 section_contents(unsigned int shndx, off_t* plen, bool cache);
188
189 // Return the name of a section given a section index. This is only
190 // used for error messages.
191 std::string
192 section_name(unsigned int shndx)
193 { return this->do_section_name(shndx); }
194
195 // Return the section flags given a section index.
196 uint64_t
197 section_flags(unsigned int shndx)
198 { return this->do_section_flags(shndx); }
199
200 // Return the section link field given a section index.
201 unsigned int
202 section_link(unsigned int shndx)
203 { return this->do_section_link(shndx); }
204
205 // Read the symbol information.
206 void
207 read_symbols(Read_symbols_data* sd)
208 { return this->do_read_symbols(sd); }
209
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.
212 void
213 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
214 { this->do_layout(symtab, layout, sd); }
215
216 // Add symbol information to the global symbol table.
217 void
218 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
219 { this->do_add_symbols(symtab, sd); }
220
221 // Functions and types for the elfcpp::Elf_file interface. This
222 // permit us to use Object as the File template parameter for
223 // elfcpp::Elf_file.
224
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
227 // need.
228 class View
229 {
230 public:
231 View(const unsigned char* p)
232 : p_(p)
233 { }
234
235 const unsigned char*
236 data() const
237 { return this->p_; }
238
239 private:
240 const unsigned char* p_;
241 };
242
243 // Return a View.
244 View
245 view(off_t file_offset, off_t data_size)
246 { return View(this->get_view(file_offset, data_size, true)); }
247
248 // Report an error.
249 void
250 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
251
252 // A location in the file.
253 struct Location
254 {
255 off_t file_offset;
256 off_t data_size;
257
258 Location(off_t fo, off_t ds)
259 : file_offset(fo), data_size(ds)
260 { }
261 };
262
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)); }
266
267 protected:
268 // Read the symbols--implemented by child class.
269 virtual void
270 do_read_symbols(Read_symbols_data*) = 0;
271
272 // Lay out sections--implemented by child class.
273 virtual void
274 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
275
276 // Add symbol information to the global symbol table--implemented by
277 // child class.
278 virtual void
279 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
280
281 // Return the location of the contents of a section. Implemented by
282 // child class.
283 virtual Location
284 do_section_contents(unsigned int shndx) = 0;
285
286 // Get the name of a section--implemented by child class.
287 virtual std::string
288 do_section_name(unsigned int shndx) = 0;
289
290 // Get section flags--implemented by child class.
291 virtual uint64_t
292 do_section_flags(unsigned int shndx) = 0;
293
294 // Get section link field--implemented by child class.
295 virtual unsigned int
296 do_section_link(unsigned int shndx) = 0;
297
298 // Get the file.
299 Input_file*
300 input_file() const
301 { return this->input_file_; }
302
303 // Get a view into the underlying file.
304 const unsigned char*
305 get_view(off_t start, off_t size, bool cache)
306 {
307 return this->input_file_->file().get_view(start + this->offset_, size,
308 cache);
309 }
310
311 // Get a lasting view into the underlying file.
312 File_view*
313 get_lasting_view(off_t start, off_t size, bool cache)
314 {
315 return this->input_file_->file().get_lasting_view(start + this->offset_,
316 size, cache);
317 }
318
319 // Read data from the underlying file.
320 void
321 read(off_t start, off_t size, void* p)
322 { this->input_file_->file().read(start + this->offset_, size, p); }
323
324 // Set the target.
325 void
326 set_target(int machine, int size, bool big_endian, int osabi,
327 int abiversion);
328
329 // Set the number of sections.
330 void
331 set_shnum(int shnum)
332 { this->shnum_ = shnum; }
333
334 // Functions used by both Sized_relobj and Sized_dynobj.
335
336 // Read the section data into a Read_symbols_data object.
337 template<int size, bool big_endian>
338 void
339 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
340 Read_symbols_data*);
341
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.
345 bool
346 handle_gnu_warning_section(const char* name, unsigned int shndx,
347 Symbol_table*);
348
349 private:
350 // This class may not be copied.
351 Object(const Object&);
352 Object& operator=(const Object&);
353
354 // Name of object as printed to user.
355 std::string name_;
356 // For reading the file.
357 Input_file* input_file_;
358 // Offset within the file--0 for an object file, non-0 for an
359 // archive.
360 off_t offset_;
361 // Number of input sections.
362 unsigned int shnum_;
363 // Whether this is a dynamic object.
364 bool is_dynamic_;
365 // Target functions--may be NULL if the target is not known.
366 Target* target_;
367 };
368
369 // Implement sized_target inline for efficiency. This approach breaks
370 // static type checking, but is made safe using asserts.
371
372 template<int size, bool big_endian>
373 inline Sized_target<size, big_endian>*
374 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
375 {
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_);
379 }
380
381 // A regular object (ET_REL). This is an abstract base class itself.
382 // The implementation is the template class Sized_relobj.
383
384 class Relobj : public Object
385 {
386 public:
387 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
388 : Object(name, input_file, false, offset)
389 { }
390
391 // Read the relocs.
392 void
393 read_relocs(Read_relocs_data* rd)
394 { return this->do_read_relocs(rd); }
395
396 // Scan the relocs and adjust the symbol table.
397 void
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); }
401
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.
405 unsigned int
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); }
409
410 // Relocate the input sections and write out the local symbols.
411 void
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); }
415
416 // Return whether an input section is being included in the link.
417 bool
418 is_section_included(unsigned int shndx) const
419 {
420 gold_assert(shndx < this->map_to_output_.size());
421 return this->map_to_output_[shndx].output_section != NULL;
422 }
423
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;
429
430 // Set the offset of an input section within its output section.
431 void
432 set_section_offset(unsigned int shndx, off_t off)
433 {
434 gold_assert(shndx < this->map_to_output_.size());
435 this->map_to_output_[shndx].offset = off;
436 }
437
438 protected:
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.
442 struct Map_to_output
443 {
444 // The output section. This is NULL if the input section is to be
445 // discarded.
446 Output_section* output_section;
447 // The offset within the output section. This is -1 if the
448 // section requires special handling.
449 off_t offset;
450 };
451
452 // Read the relocs--implemented by child class.
453 virtual void
454 do_read_relocs(Read_relocs_data*) = 0;
455
456 // Scan the relocs--implemented by child class.
457 virtual void
458 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
459 Read_relocs_data*) = 0;
460
461 // Finalize local symbols--implemented by child class.
462 virtual unsigned int
463 do_finalize_local_symbols(unsigned int, off_t,
464 Stringpool_template<char>*) = 0;
465
466 // Relocate the input sections and write out the local
467 // symbols--implemented by child class.
468 virtual void
469 do_relocate(const General_options& options, const Symbol_table* symtab,
470 const Layout*, Output_file* of) = 0;
471
472 // Return the vector mapping input sections to output sections.
473 std::vector<Map_to_output>&
474 map_to_output()
475 { return this->map_to_output_; }
476
477 const std::vector<Map_to_output>&
478 map_to_output() const
479 { return this->map_to_output_; }
480
481 private:
482 // Mapping from input sections to output section.
483 std::vector<Map_to_output> map_to_output_;
484 };
485
486 // Implement Object::output_section inline for efficiency.
487 inline Output_section*
488 Relobj::output_section(unsigned int shndx, off_t* poff) const
489 {
490 gold_assert(shndx < this->map_to_output_.size());
491 const Map_to_output& mo(this->map_to_output_[shndx]);
492 *poff = mo.offset;
493 return mo.output_section;
494 }
495
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.
500
501 template<int size>
502 class Symbol_value
503 {
504 public:
505 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
506
507 Symbol_value()
508 : output_symtab_index_(0), input_shndx_(0), is_section_symbol_(false),
509 needs_output_address_(false), value_(0)
510 { }
511
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>
515 Value
516 value(const Sized_relobj<size, big_endian>* object, Value addend) const
517 {
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);
522 }
523
524 // Set the value of this symbol in the output symbol table.
525 void
526 set_output_value(Value value)
527 {
528 this->value_ = value;
529 this->needs_output_address_ = false;
530 }
531
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.
536 void
537 set_input_value(Value value)
538 {
539 this->value_ = value;
540 this->needs_output_address_ = true;
541 }
542
543 // Return whether this symbol should go into the output symbol
544 // table.
545 bool
546 needs_output_symtab_entry() const
547 {
548 gold_assert(this->output_symtab_index_ != 0);
549 return this->output_symtab_index_ != -1U;
550 }
551
552 // Return the index in the output symbol table.
553 unsigned int
554 output_symtab_index() const
555 {
556 gold_assert(this->output_symtab_index_ != 0);
557 return this->output_symtab_index_;
558 }
559
560 // Set the index in the output symbol table.
561 void
562 set_output_symtab_index(unsigned int i)
563 {
564 gold_assert(this->output_symtab_index_ == 0);
565 this->output_symtab_index_ = i;
566 }
567
568 // Record that this symbol should not go into the output symbol
569 // table.
570 void
571 set_no_output_symtab_entry()
572 {
573 gold_assert(this->output_symtab_index_ == 0);
574 this->output_symtab_index_ = -1U;
575 }
576
577 // Set the index of the input section in the input file.
578 void
579 set_input_shndx(unsigned int i)
580 { this->input_shndx_ = i; }
581
582 // Record that this is a section symbol.
583 void
584 set_is_section_symbol()
585 { this->is_section_symbol_ = true; }
586
587 private:
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
592 // defined.
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.
603 Value value_;
604 };
605
606 // A regular object file. This is size and endian specific.
607
608 template<int size, bool big_endian>
609 class Sized_relobj : public Relobj
610 {
611 public:
612 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
613 typedef std::vector<Symbol_value<size> > Local_values;
614
615 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
616 const typename elfcpp::Ehdr<size, big_endian>&);
617
618 ~Sized_relobj();
619
620 // Set up the object file based on the ELF header.
621 void
622 setup(const typename elfcpp::Ehdr<size, big_endian>&);
623
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.
626 unsigned int
627 symtab_index(unsigned int sym) const
628 {
629 gold_assert(sym < this->local_values_.size());
630 return this->local_values_[sym].output_symtab_index();
631 }
632
633 // Return the appropriate Sized_target structure.
634 Sized_target<size, big_endian>*
635 sized_target()
636 {
637 return this->Object::sized_target
638 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
639 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
640 }
641
642 // Return the value of the local symbol symndx.
643 Address
644 local_symbol_value(unsigned int symndx) const;
645
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.
650 Address
651 local_value(unsigned int shndx, Address value, bool is_section_symbol,
652 Address addend) const;
653
654 // Return whether the local symbol SYMNDX has a GOT offset.
655 bool
656 local_has_got_offset(unsigned int symndx) const
657 {
658 return (this->local_got_offsets_.find(symndx)
659 != this->local_got_offsets_.end());
660 }
661
662 // Return the GOT offset of the local symbol SYMNDX.
663 unsigned int
664 local_got_offset(unsigned int symndx) const
665 {
666 Local_got_offsets::const_iterator p =
667 this->local_got_offsets_.find(symndx);
668 gold_assert(p != this->local_got_offsets_.end());
669 return p->second;
670 }
671
672 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
673 void
674 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
675 {
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);
679 }
680
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.
684 bool
685 get_symbol_location_info(unsigned int shndx, off_t offset,
686 Symbol_location_info* info);
687
688 // Read the symbols.
689 void
690 do_read_symbols(Read_symbols_data*);
691
692 // Lay out the input sections.
693 void
694 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
695
696 // Add the symbols to the symbol table.
697 void
698 do_add_symbols(Symbol_table*, Read_symbols_data*);
699
700 // Read the relocs.
701 void
702 do_read_relocs(Read_relocs_data*);
703
704 // Scan the relocs and adjust the symbol table.
705 void
706 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
707 Read_relocs_data*);
708
709 // Finalize the local symbols.
710 unsigned int
711 do_finalize_local_symbols(unsigned int, off_t,
712 Stringpool_template<char>*);
713
714 // Relocate the input sections and write out the local symbols.
715 void
716 do_relocate(const General_options& options, const Symbol_table* symtab,
717 const Layout*, Output_file* of);
718
719 // Get the name of a section.
720 std::string
721 do_section_name(unsigned int shndx)
722 { return this->elf_file_.section_name(shndx); }
723
724 // Return the location of the contents of a section.
725 Object::Location
726 do_section_contents(unsigned int shndx)
727 { return this->elf_file_.section_contents(shndx); }
728
729 // Return section flags.
730 uint64_t
731 do_section_flags(unsigned int shndx)
732 { return this->elf_file_.section_flags(shndx); }
733
734 // Return the section link field.
735 unsigned int
736 do_section_link(unsigned int shndx)
737 { return this->elf_file_.section_link(shndx); }
738
739 private:
740 // For convenience.
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;
746
747 // Find the SHT_SYMTAB section, given the section headers.
748 void
749 find_symtab(const unsigned char* pshdrs);
750
751 // Whether to include a section group in the link.
752 bool
753 include_section_group(Layout*, unsigned int,
754 const elfcpp::Shdr<size, big_endian>&,
755 std::vector<bool>*);
756
757 // Whether to include a linkonce section in the link.
758 bool
759 include_linkonce_section(Layout*, const char*,
760 const elfcpp::Shdr<size, big_endian>&);
761
762 // Views and sizes when relocating.
763 struct View_size
764 {
765 unsigned char* view;
766 typename elfcpp::Elf_types<size>::Elf_Addr address;
767 off_t offset;
768 off_t view_size;
769 };
770
771 typedef std::vector<View_size> Views;
772
773 // Write section data to the output file. Record the views and
774 // sizes in VIEWS for use when relocating.
775 void
776 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
777
778 // Relocate the sections in the output file.
779 void
780 relocate_sections(const General_options& options, const Symbol_table*,
781 const Layout*, const unsigned char* pshdrs, Views*);
782
783 // Write out the local symbols.
784 void
785 write_local_symbols(Output_file*,
786 const Stringpool_template<char>*);
787
788 // The GOT offsets of local symbols.
789 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
790
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.
800 Symbol** 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_;
807 };
808
809 // A class to manage the list of all objects.
810
811 class Input_objects
812 {
813 public:
814 Input_objects()
815 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_()
816 { }
817
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;
821
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;
825
826 // Add an object to the list. Return true if all is well, or false
827 // if this object should be ignored.
828 bool
829 add_object(Object*);
830
831 // Get the target we should use for the output file.
832 Target*
833 target() const
834 { return this->target_; }
835
836 // Iterate over all regular objects.
837
838 Relobj_iterator
839 relobj_begin() const
840 { return this->relobj_list_.begin(); }
841
842 Relobj_iterator
843 relobj_end() const
844 { return this->relobj_list_.end(); }
845
846 // Iterate over all dynamic objects.
847
848 Dynobj_iterator
849 dynobj_begin() const
850 { return this->dynobj_list_.begin(); }
851
852 Dynobj_iterator
853 dynobj_end() const
854 { return this->dynobj_list_.end(); }
855
856 // Return whether we have seen any dynamic objects.
857 bool
858 any_dynamic() const
859 { return !this->dynobj_list_.empty(); }
860
861 // Return the number of input objects.
862 int
863 number_of_input_objects() const
864 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
865
866 private:
867 Input_objects(const Input_objects&);
868 Input_objects& operator=(const Input_objects&);
869
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_;
874 // The target.
875 Target* target_;
876 // SONAMEs that we have seen.
877 Unordered_set<std::string> sonames_;
878 };
879
880 // Some of the information we pass to the relocation routines. We
881 // group this together to avoid passing a dozen different arguments.
882
883 template<int size, bool big_endian>
884 struct Relocate_info
885 {
886 // Command line options.
887 const General_options* options;
888 // Symbol table.
889 const Symbol_table* symtab;
890 // Layout.
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;
898 // Global symbols.
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;
904
905 // Return a string showing the location of a relocation. This is
906 // only used for error messages.
907 std::string
908 location(size_t relnum, off_t reloffset) const;
909 };
910
911 // Return an Object appropriate for the input file. P is BYTES long,
912 // and holds the ELF header.
913
914 extern Object*
915 make_elf_object(const std::string& name, Input_file*,
916 off_t offset, const unsigned char* p,
917 off_t bytes);
918
919 } // end namespace gold
920
921 #endif // !defined(GOLD_OBJECT_H)
This page took 0.050027 seconds and 4 git commands to generate.