a98e75665d21bef518138fe4787c0c1b4fdefcc4
[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 class Object_merge_map;
43
44 template<typename Stringpool_char>
45 class Stringpool_template;
46
47 // Data to pass from read_symbols() to add_symbols().
48
49 struct Read_symbols_data
50 {
51 // Section headers.
52 File_view* section_headers;
53 // Section names.
54 File_view* section_names;
55 // Size of section name data in bytes.
56 off_t section_names_size;
57 // Symbol data.
58 File_view* symbols;
59 // Size of symbol data in bytes.
60 off_t symbols_size;
61 // Offset of external symbols within symbol data. This structure
62 // sometimes contains only external symbols, in which case this will
63 // be zero. Sometimes it contains all symbols.
64 off_t external_symbols_offset;
65 // Symbol names.
66 File_view* symbol_names;
67 // Size of symbol name data in bytes.
68 off_t symbol_names_size;
69
70 // Version information. This is only used on dynamic objects.
71 // Version symbol data (from SHT_GNU_versym section).
72 File_view* versym;
73 off_t versym_size;
74 // Version definition data (from SHT_GNU_verdef section).
75 File_view* verdef;
76 off_t verdef_size;
77 unsigned int verdef_info;
78 // Needed version data (from SHT_GNU_verneed section).
79 File_view* verneed;
80 off_t verneed_size;
81 unsigned int verneed_info;
82 };
83
84 // Information used to print error messages.
85
86 struct Symbol_location_info
87 {
88 std::string source_file;
89 std::string enclosing_symbol_name;
90 int line_number;
91 };
92
93 // Data about a single relocation section. This is read in
94 // read_relocs and processed in scan_relocs.
95
96 struct Section_relocs
97 {
98 // Index of reloc section.
99 unsigned int reloc_shndx;
100 // Index of section that relocs apply to.
101 unsigned int data_shndx;
102 // Contents of reloc section.
103 File_view* contents;
104 // Reloc section type.
105 unsigned int sh_type;
106 // Number of reloc entries.
107 size_t reloc_count;
108 // Output section.
109 Output_section* output_section;
110 // Whether this section has special handling for offsets.
111 bool needs_special_offset_handling;
112 };
113
114 // Relocations in an object file. This is read in read_relocs and
115 // processed in scan_relocs.
116
117 struct Read_relocs_data
118 {
119 typedef std::vector<Section_relocs> Relocs_list;
120 // The relocations.
121 Relocs_list relocs;
122 // The local symbols.
123 File_view* local_symbols;
124 };
125
126 // Object is an abstract base class which represents either a 32-bit
127 // or a 64-bit input object. This can be a regular object file
128 // (ET_REL) or a shared object (ET_DYN).
129
130 class Object
131 {
132 public:
133 // NAME is the name of the object as we would report it to the user
134 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
135 // used to read the file. OFFSET is the offset within the input
136 // file--0 for a .o or .so file, something else for a .a file.
137 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
138 off_t offset = 0)
139 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
140 is_dynamic_(is_dynamic), target_(NULL)
141 { }
142
143 virtual ~Object()
144 { }
145
146 // Return the name of the object as we would report it to the tuser.
147 const std::string&
148 name() const
149 { return this->name_; }
150
151 // Get the offset into the file.
152 off_t
153 offset() const
154 { return this->offset_; }
155
156 // Return whether this is a dynamic object.
157 bool
158 is_dynamic() const
159 { return this->is_dynamic_; }
160
161 // Return the target structure associated with this object.
162 Target*
163 target() const
164 { return this->target_; }
165
166 // Lock the underlying file.
167 void
168 lock()
169 { this->input_file_->file().lock(); }
170
171 // Unlock the underlying file.
172 void
173 unlock()
174 { this->input_file_->file().unlock(); }
175
176 // Return whether the underlying file is locked.
177 bool
178 is_locked() const
179 { return this->input_file_->file().is_locked(); }
180
181 // Return the sized target structure associated with this object.
182 // This is like the target method but it returns a pointer of
183 // appropriate checked type.
184 template<int size, bool big_endian>
185 Sized_target<size, big_endian>*
186 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
187
188 // Get the number of sections.
189 unsigned int
190 shnum() const
191 { return this->shnum_; }
192
193 // Return a view of the contents of a section. Set *PLEN to the
194 // size. CACHE is a hint as in File_read::get_view.
195 const unsigned char*
196 section_contents(unsigned int shndx, off_t* plen, bool cache);
197
198 // Return the name of a section given a section index. This is only
199 // used for error messages.
200 std::string
201 section_name(unsigned int shndx)
202 { return this->do_section_name(shndx); }
203
204 // Return the section flags given a section index.
205 uint64_t
206 section_flags(unsigned int shndx)
207 { return this->do_section_flags(shndx); }
208
209 // Return the section type given a section index.
210 unsigned int
211 section_type(unsigned int shndx)
212 { return this->do_section_type(shndx); }
213
214 // Return the section link field given a section index.
215 unsigned int
216 section_link(unsigned int shndx)
217 { return this->do_section_link(shndx); }
218
219 // Return the section info field given a section index.
220 unsigned int
221 section_info(unsigned int shndx)
222 { return this->do_section_info(shndx); }
223
224 // Read the symbol information.
225 void
226 read_symbols(Read_symbols_data* sd)
227 { return this->do_read_symbols(sd); }
228
229 // Pass sections which should be included in the link to the Layout
230 // object, and record where the sections go in the output file.
231 void
232 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
233 { this->do_layout(symtab, layout, sd); }
234
235 // Add symbol information to the global symbol table.
236 void
237 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
238 { this->do_add_symbols(symtab, sd); }
239
240 // Functions and types for the elfcpp::Elf_file interface. This
241 // permit us to use Object as the File template parameter for
242 // elfcpp::Elf_file.
243
244 // The View class is returned by view. It must support a single
245 // method, data(). This is trivial, because get_view does what we
246 // need.
247 class View
248 {
249 public:
250 View(const unsigned char* p)
251 : p_(p)
252 { }
253
254 const unsigned char*
255 data() const
256 { return this->p_; }
257
258 private:
259 const unsigned char* p_;
260 };
261
262 // Return a View.
263 View
264 view(off_t file_offset, off_t data_size)
265 { return View(this->get_view(file_offset, data_size, true)); }
266
267 // Report an error.
268 void
269 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
270
271 // A location in the file.
272 struct Location
273 {
274 off_t file_offset;
275 off_t data_size;
276
277 Location(off_t fo, off_t ds)
278 : file_offset(fo), data_size(ds)
279 { }
280 };
281
282 // Get a View given a Location.
283 View view(Location loc)
284 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
285
286 protected:
287 // Read the symbols--implemented by child class.
288 virtual void
289 do_read_symbols(Read_symbols_data*) = 0;
290
291 // Lay out sections--implemented by child class.
292 virtual void
293 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
294
295 // Add symbol information to the global symbol table--implemented by
296 // child class.
297 virtual void
298 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
299
300 // Return the location of the contents of a section. Implemented by
301 // child class.
302 virtual Location
303 do_section_contents(unsigned int shndx) = 0;
304
305 // Get the name of a section--implemented by child class.
306 virtual std::string
307 do_section_name(unsigned int shndx) = 0;
308
309 // Get section flags--implemented by child class.
310 virtual uint64_t
311 do_section_flags(unsigned int shndx) = 0;
312
313 // Get section type--implemented by child class.
314 virtual unsigned int
315 do_section_type(unsigned int shndx) = 0;
316
317 // Get section link field--implemented by child class.
318 virtual unsigned int
319 do_section_link(unsigned int shndx) = 0;
320
321 // Get section info field--implemented by child class.
322 virtual unsigned int
323 do_section_info(unsigned int shndx) = 0;
324
325 // Get the file.
326 Input_file*
327 input_file() const
328 { return this->input_file_; }
329
330 // Get a view into the underlying file.
331 const unsigned char*
332 get_view(off_t start, off_t size, bool cache)
333 {
334 return this->input_file_->file().get_view(start + this->offset_, size,
335 cache);
336 }
337
338 // Get a lasting view into the underlying file.
339 File_view*
340 get_lasting_view(off_t start, off_t size, bool cache)
341 {
342 return this->input_file_->file().get_lasting_view(start + this->offset_,
343 size, cache);
344 }
345
346 // Read data from the underlying file.
347 void
348 read(off_t start, off_t size, void* p)
349 { this->input_file_->file().read(start + this->offset_, size, p); }
350
351 // Set the target.
352 void
353 set_target(int machine, int size, bool big_endian, int osabi,
354 int abiversion);
355
356 // Set the number of sections.
357 void
358 set_shnum(int shnum)
359 { this->shnum_ = shnum; }
360
361 // Functions used by both Sized_relobj and Sized_dynobj.
362
363 // Read the section data into a Read_symbols_data object.
364 template<int size, bool big_endian>
365 void
366 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
367 Read_symbols_data*);
368
369 // If NAME is the name of a special .gnu.warning section, arrange
370 // for the warning to be issued. SHNDX is the section index.
371 // Return whether it is a warning section.
372 bool
373 handle_gnu_warning_section(const char* name, unsigned int shndx,
374 Symbol_table*);
375
376 private:
377 // This class may not be copied.
378 Object(const Object&);
379 Object& operator=(const Object&);
380
381 // Name of object as printed to user.
382 std::string name_;
383 // For reading the file.
384 Input_file* input_file_;
385 // Offset within the file--0 for an object file, non-0 for an
386 // archive.
387 off_t offset_;
388 // Number of input sections.
389 unsigned int shnum_;
390 // Whether this is a dynamic object.
391 bool is_dynamic_;
392 // Target functions--may be NULL if the target is not known.
393 Target* target_;
394 };
395
396 // Implement sized_target inline for efficiency. This approach breaks
397 // static type checking, but is made safe using asserts.
398
399 template<int size, bool big_endian>
400 inline Sized_target<size, big_endian>*
401 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
402 {
403 gold_assert(this->target_->get_size() == size);
404 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
405 return static_cast<Sized_target<size, big_endian>*>(this->target_);
406 }
407
408 // A regular object (ET_REL). This is an abstract base class itself.
409 // The implementation is the template class Sized_relobj.
410
411 class Relobj : public Object
412 {
413 public:
414 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
415 : Object(name, input_file, false, offset),
416 map_to_output_(),
417 object_merge_map_(NULL),
418 relocs_must_follow_section_writes_(false)
419 { }
420
421 // Read the relocs.
422 void
423 read_relocs(Read_relocs_data* rd)
424 { return this->do_read_relocs(rd); }
425
426 // Scan the relocs and adjust the symbol table.
427 void
428 scan_relocs(const General_options& options, Symbol_table* symtab,
429 Layout* layout, Read_relocs_data* rd)
430 { return this->do_scan_relocs(options, symtab, layout, rd); }
431
432 // The number of local symbols in the input symbol table.
433 virtual unsigned int
434 local_symbol_count() const
435 { return this->do_local_symbol_count(); }
436
437 // Initial local symbol processing: count the number of local symbols
438 // in the output symbol table and dynamic symbol table; add local symbol
439 // names to *POOL and *DYNPOOL.
440 void
441 count_local_symbols(Stringpool_template<char>* pool,
442 Stringpool_template<char>* dynpool)
443 { return this->do_count_local_symbols(pool, dynpool); }
444
445 // Set the values of the local symbols, set the output symbol table
446 // indexes for the local variables, and set the offset where local
447 // symbol information will be stored. Returns the new local symbol index.
448 unsigned int
449 finalize_local_symbols(unsigned int index, off_t off)
450 { return this->do_finalize_local_symbols(index, off); }
451
452 // Set the output dynamic symbol table indexes for the local variables.
453 unsigned int
454 set_local_dynsym_indexes(unsigned int index)
455 { return this->do_set_local_dynsym_indexes(index); }
456
457 // Set the offset where local dynamic symbol information will be stored.
458 unsigned int
459 set_local_dynsym_offset(off_t off)
460 { return this->do_set_local_dynsym_offset(off); }
461
462 // Relocate the input sections and write out the local symbols.
463 void
464 relocate(const General_options& options, const Symbol_table* symtab,
465 const Layout* layout, Output_file* of)
466 { return this->do_relocate(options, symtab, layout, of); }
467
468 // Return whether an input section is being included in the link.
469 bool
470 is_section_included(unsigned int shndx) const
471 {
472 gold_assert(shndx < this->map_to_output_.size());
473 return this->map_to_output_[shndx].output_section != NULL;
474 }
475
476 // Return whether an input section requires special
477 // handling--whether it is not simply mapped from the input file to
478 // the output file.
479 bool
480 is_section_specially_mapped(unsigned int shndx) const
481 {
482 gold_assert(shndx < this->map_to_output_.size());
483 return (this->map_to_output_[shndx].output_section != NULL
484 && this->map_to_output_[shndx].offset == -1);
485 }
486
487 // Given a section index, return the corresponding Output_section
488 // (which will be NULL if the section is not included in the link)
489 // and set *POFF to the offset within that section. *POFF will be
490 // set to -1 if the section requires special handling.
491 inline Output_section*
492 output_section(unsigned int shndx, off_t* poff) const;
493
494 // Set the offset of an input section within its output section.
495 void
496 set_section_offset(unsigned int shndx, off_t off)
497 {
498 gold_assert(shndx < this->map_to_output_.size());
499 this->map_to_output_[shndx].offset = off;
500 }
501
502 // Return true if we need to wait for output sections to be written
503 // before we can apply relocations. This is true if the object has
504 // any relocations for sections which require special handling, such
505 // as the exception frame section.
506 bool
507 relocs_must_follow_section_writes()
508 { return this->relocs_must_follow_section_writes_; }
509
510 // Return the object merge map.
511 Object_merge_map*
512 merge_map() const
513 { return this->object_merge_map_; }
514
515 // Set the object merge map.
516 void
517 set_merge_map(Object_merge_map* object_merge_map)
518 {
519 gold_assert(this->object_merge_map_ == NULL);
520 this->object_merge_map_ = object_merge_map;
521 }
522
523 protected:
524 // What we need to know to map an input section to an output
525 // section. We keep an array of these, one for each input section,
526 // indexed by the input section number.
527 struct Map_to_output
528 {
529 // The output section. This is NULL if the input section is to be
530 // discarded.
531 Output_section* output_section;
532 // The offset within the output section. This is -1 if the
533 // section requires special handling.
534 off_t offset;
535 };
536
537 // Read the relocs--implemented by child class.
538 virtual void
539 do_read_relocs(Read_relocs_data*) = 0;
540
541 // Scan the relocs--implemented by child class.
542 virtual void
543 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
544 Read_relocs_data*) = 0;
545
546 // Return the number of local symbols--implemented by child class.
547 virtual unsigned int
548 do_local_symbol_count() const = 0;
549
550 // Count local symbols--implemented by child class.
551 virtual void
552 do_count_local_symbols(Stringpool_template<char>*,
553 Stringpool_template<char>*) = 0;
554
555 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
556 // offset where local symbol information will be stored.
557 virtual unsigned int
558 do_finalize_local_symbols(unsigned int, off_t) = 0;
559
560 // Set the output dynamic symbol table indexes for the local variables.
561 virtual unsigned int
562 do_set_local_dynsym_indexes(unsigned int) = 0;
563
564 // Set the offset where local dynamic symbol information will be stored.
565 virtual unsigned int
566 do_set_local_dynsym_offset(off_t) = 0;
567
568 // Relocate the input sections and write out the local
569 // symbols--implemented by child class.
570 virtual void
571 do_relocate(const General_options& options, const Symbol_table* symtab,
572 const Layout*, Output_file* of) = 0;
573
574 // Return the vector mapping input sections to output sections.
575 std::vector<Map_to_output>&
576 map_to_output()
577 { return this->map_to_output_; }
578
579 const std::vector<Map_to_output>&
580 map_to_output() const
581 { return this->map_to_output_; }
582
583 // Record that we must wait for the output sections to be written
584 // before applying relocations.
585 void
586 set_relocs_must_follow_section_writes()
587 { this->relocs_must_follow_section_writes_ = true; }
588
589 private:
590 // Mapping from input sections to output section.
591 std::vector<Map_to_output> map_to_output_;
592 // Mappings for merge sections. This is managed by the code in the
593 // Merge_map class.
594 Object_merge_map* object_merge_map_;
595 // Whether we need to wait for output sections to be written before
596 // we can apply relocations.
597 bool relocs_must_follow_section_writes_;
598 };
599
600 // Implement Object::output_section inline for efficiency.
601 inline Output_section*
602 Relobj::output_section(unsigned int shndx, off_t* poff) const
603 {
604 gold_assert(shndx < this->map_to_output_.size());
605 const Map_to_output& mo(this->map_to_output_[shndx]);
606 *poff = mo.offset;
607 return mo.output_section;
608 }
609
610 // This POD class is holds the value of a symbol. This is used for
611 // local symbols, and for all symbols during relocation processing.
612 // For special sections, such as SHF_MERGE sections, this calls a
613 // function to get the final symbol value.
614
615 template<int size>
616 class Symbol_value
617 {
618 public:
619 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
620
621 Symbol_value()
622 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
623 is_section_symbol_(false), is_tls_symbol_(false),
624 needs_output_address_(false), value_(0)
625 { }
626
627 // Get the value of this symbol. OBJECT is the object in which this
628 // symbol is defined, and ADDEND is an addend to add to the value.
629 template<bool big_endian>
630 Value
631 value(const Sized_relobj<size, big_endian>* object, Value addend) const
632 {
633 if (!this->needs_output_address_)
634 return this->value_ + addend;
635 return object->local_value(this->input_shndx_, this->value_,
636 this->is_section_symbol_, addend);
637 }
638
639 // Set the value of this symbol in the output symbol table.
640 void
641 set_output_value(Value value)
642 {
643 this->value_ = value;
644 this->needs_output_address_ = false;
645 }
646
647 // Set the value of the symbol from the input file. This value
648 // will usually be replaced during finalization with the output
649 // value, but if the symbol is mapped to an output section which
650 // requires special handling to determine the output value, we
651 // leave the input value in place until later. This is used for
652 // SHF_MERGE sections.
653 void
654 set_input_value(Value value)
655 {
656 this->value_ = value;
657 this->needs_output_address_ = true;
658 }
659
660 // Return the input value.
661 Value
662 input_value() const
663 {
664 gold_assert(this->needs_output_address_);
665 return this->value_;
666 }
667
668 // Return whether this symbol should go into the output symbol
669 // table.
670 bool
671 needs_output_symtab_entry() const
672 { return this->output_symtab_index_ != -1U; }
673
674 // Return the index in the output symbol table.
675 unsigned int
676 output_symtab_index() const
677 {
678 gold_assert(this->output_symtab_index_ != 0);
679 return this->output_symtab_index_;
680 }
681
682 // Set the index in the output symbol table.
683 void
684 set_output_symtab_index(unsigned int i)
685 {
686 gold_assert(this->output_symtab_index_ == 0);
687 this->output_symtab_index_ = i;
688 }
689
690 // Record that this symbol should not go into the output symbol
691 // table.
692 void
693 set_no_output_symtab_entry()
694 {
695 gold_assert(this->output_symtab_index_ == 0);
696 this->output_symtab_index_ = -1U;
697 }
698
699 // Set the index in the output dynamic symbol table.
700 void
701 set_needs_output_dynsym_entry()
702 {
703 this->output_dynsym_index_ = 0;
704 }
705
706 // Return whether this symbol should go into the output symbol
707 // table.
708 bool
709 needs_output_dynsym_entry() const
710 {
711 return this->output_dynsym_index_ != -1U;
712 }
713
714 // Record that this symbol should go into the dynamic symbol table.
715 void
716 set_output_dynsym_index(unsigned int i)
717 {
718 gold_assert(this->output_dynsym_index_ == 0);
719 this->output_dynsym_index_ = i;
720 }
721
722 // Return the index in the output dynamic symbol table.
723 unsigned int
724 output_dynsym_index() const
725 {
726 gold_assert(this->output_dynsym_index_ != 0);
727 return this->output_dynsym_index_;
728 }
729
730 // Set the index of the input section in the input file.
731 void
732 set_input_shndx(unsigned int i)
733 {
734 this->input_shndx_ = i;
735 // input_shndx_ field is a bitfield, so make sure that the value
736 // fits.
737 gold_assert(this->input_shndx_ == i);
738 }
739
740 // Return the index of the input section in the input file.
741 unsigned int
742 input_shndx() const
743 { return this->input_shndx_; }
744
745 // Record that this is a section symbol.
746 void
747 set_is_section_symbol()
748 { this->is_section_symbol_ = true; }
749
750 // Record that this is a TLS symbol.
751 void
752 set_is_tls_symbol()
753 { this->is_tls_symbol_ = true; }
754
755 // Return TRUE if this is a TLS symbol.
756 bool
757 is_tls_symbol() const
758 { return this->is_tls_symbol_; }
759
760 private:
761 // The index of this local symbol in the output symbol table. This
762 // will be -1 if the symbol should not go into the symbol table.
763 unsigned int output_symtab_index_;
764 // The index of this local symbol in the dynamic symbol table. This
765 // will be -1 if the symbol should not go into the symbol table.
766 unsigned int output_dynsym_index_;
767 // The section index in the input file in which this symbol is
768 // defined.
769 unsigned int input_shndx_ : 29;
770 // Whether this is a STT_SECTION symbol.
771 bool is_section_symbol_ : 1;
772 // Whether this is a STT_TLS symbol.
773 bool is_tls_symbol_ : 1;
774 // Whether getting the value of this symbol requires calling an
775 // Output_section method. For example, this will be true of a
776 // symbol in a SHF_MERGE section.
777 bool needs_output_address_ : 1;
778 // The value of the symbol. If !needs_output_address_, this is the
779 // value in the output file. If needs_output_address_, this is the
780 // value in the input file.
781 Value value_;
782 };
783
784 // A regular object file. This is size and endian specific.
785
786 template<int size, bool big_endian>
787 class Sized_relobj : public Relobj
788 {
789 public:
790 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
791 typedef std::vector<Symbol*> Symbols;
792 typedef std::vector<Symbol_value<size> > Local_values;
793
794 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
795 const typename elfcpp::Ehdr<size, big_endian>&);
796
797 ~Sized_relobj();
798
799 // Set up the object file based on the ELF header.
800 void
801 setup(const typename elfcpp::Ehdr<size, big_endian>&);
802
803 // If SYM is the index of a global symbol in the object file's
804 // symbol table, return the Symbol object. Otherwise, return NULL.
805 Symbol*
806 global_symbol(unsigned int sym) const
807 {
808 if (sym >= this->local_symbol_count_)
809 {
810 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
811 return this->symbols_[sym - this->local_symbol_count_];
812 }
813 return NULL;
814 }
815
816 // Return the section index of symbol SYM. Set *VALUE to its value
817 // in the object file. Note that for a symbol which is not defined
818 // in this object file, this will set *VALUE to 0 and return
819 // SHN_UNDEF; it will not return the final value of the symbol in
820 // the link.
821 unsigned int
822 symbol_section_and_value(unsigned int sym, Address* value);
823
824 // Return a pointer to the Symbol_value structure which holds the
825 // value of a local symbol.
826 const Symbol_value<size>*
827 local_symbol(unsigned int sym) const
828 {
829 gold_assert(sym < this->local_values_.size());
830 return &this->local_values_[sym];
831 }
832
833 // Return the index of local symbol SYM in the ordinary symbol
834 // table. A value of -1U means that the symbol is not being output.
835 unsigned int
836 symtab_index(unsigned int sym) const
837 {
838 gold_assert(sym < this->local_values_.size());
839 return this->local_values_[sym].output_symtab_index();
840 }
841
842 // Return the index of local symbol SYM in the dynamic symbol
843 // table. A value of -1U means that the symbol is not being output.
844 unsigned int
845 dynsym_index(unsigned int sym) const
846 {
847 gold_assert(sym < this->local_values_.size());
848 return this->local_values_[sym].output_dynsym_index();
849 }
850
851 // Return the appropriate Sized_target structure.
852 Sized_target<size, big_endian>*
853 sized_target()
854 {
855 return this->Object::sized_target
856 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
857 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
858 }
859
860 // Return the value of the local symbol symndx.
861 Address
862 local_symbol_value(unsigned int symndx) const;
863
864 // Return the value of a local symbol defined in input section
865 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
866 // indicates whether the symbol is a section symbol. This handles
867 // SHF_MERGE sections.
868 Address
869 local_value(unsigned int shndx, Address value, bool is_section_symbol,
870 Address addend) const;
871
872 void
873 set_needs_output_dynsym_entry(unsigned int sym)
874 {
875 gold_assert(sym < this->local_values_.size());
876 this->local_values_[sym].set_needs_output_dynsym_entry();
877 }
878
879 // Return whether the local symbol SYMNDX has a GOT offset.
880 // For TLS symbols, the GOT entry will hold its tp-relative offset.
881 bool
882 local_has_got_offset(unsigned int symndx) const
883 {
884 return (this->local_got_offsets_.find(symndx)
885 != this->local_got_offsets_.end());
886 }
887
888 // Return the GOT offset of the local symbol SYMNDX.
889 unsigned int
890 local_got_offset(unsigned int symndx) const
891 {
892 Local_got_offsets::const_iterator p =
893 this->local_got_offsets_.find(symndx);
894 gold_assert(p != this->local_got_offsets_.end());
895 return p->second;
896 }
897
898 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
899 void
900 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
901 {
902 std::pair<Local_got_offsets::iterator, bool> ins =
903 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
904 gold_assert(ins.second);
905 }
906
907 // Return whether the local TLS symbol SYMNDX has a GOT offset.
908 // The GOT entry at this offset will contain a module index. If
909 // NEED_PAIR is true, a second entry immediately following the first
910 // will contain the dtv-relative offset.
911 bool
912 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
913 {
914 typename Local_tls_got_offsets::const_iterator p =
915 this->local_tls_got_offsets_.find(symndx);
916 if (p == this->local_tls_got_offsets_.end()
917 || (need_pair && !p->second.have_pair_))
918 return false;
919 return true;
920 }
921
922 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
923 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
924 // otherwise we need the offset of the GOT entry for the module index.
925 unsigned int
926 local_tls_got_offset(unsigned int symndx, bool need_pair) const
927 {
928 typename Local_tls_got_offsets::const_iterator p =
929 this->local_tls_got_offsets_.find(symndx);
930 gold_assert(p != this->local_tls_got_offsets_.end());
931 gold_assert(!need_pair || p->second.have_pair_);
932 return p->second.got_offset_;
933 }
934
935 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
936 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
937 // otherwise, we have just a single entry for the module index.
938 void
939 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
940 bool have_pair)
941 {
942 typename Local_tls_got_offsets::iterator p =
943 this->local_tls_got_offsets_.find(symndx);
944 if (p != this->local_tls_got_offsets_.end())
945 {
946 // An entry already existed for this symbol. This can happen
947 // if we see a relocation asking for the module index before
948 // a relocation asking for the pair. In that case, the original
949 // GOT entry will remain, but won't get used by any further
950 // relocations.
951 p->second.got_offset_ = got_offset;
952 gold_assert(have_pair);
953 p->second.have_pair_ = true;
954 }
955 else
956 {
957 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
958 this->local_tls_got_offsets_.insert(
959 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
960 gold_assert(ins.second);
961 }
962 }
963
964 // Return the name of the symbol that spans the given offset in the
965 // specified section in this object. This is used only for error
966 // messages and is not particularly efficient.
967 bool
968 get_symbol_location_info(unsigned int shndx, off_t offset,
969 Symbol_location_info* info);
970
971 protected:
972 // Read the symbols.
973 void
974 do_read_symbols(Read_symbols_data*);
975
976 // Return the number of local symbols.
977 unsigned int
978 do_local_symbol_count() const
979 { return this->local_symbol_count_; }
980
981 // Lay out the input sections.
982 void
983 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
984
985 // Add the symbols to the symbol table.
986 void
987 do_add_symbols(Symbol_table*, Read_symbols_data*);
988
989 // Read the relocs.
990 void
991 do_read_relocs(Read_relocs_data*);
992
993 // Scan the relocs and adjust the symbol table.
994 void
995 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
996 Read_relocs_data*);
997
998 // Count the local symbols.
999 void
1000 do_count_local_symbols(Stringpool_template<char>*,
1001 Stringpool_template<char>*);
1002
1003 // Finalize the local symbols.
1004 unsigned int
1005 do_finalize_local_symbols(unsigned int, off_t);
1006
1007 // Set the offset where local dynamic symbol information will be stored.
1008 unsigned int
1009 do_set_local_dynsym_indexes(unsigned int);
1010
1011 // Set the offset where local dynamic symbol information will be stored.
1012 unsigned int
1013 do_set_local_dynsym_offset(off_t);
1014
1015 // Relocate the input sections and write out the local symbols.
1016 void
1017 do_relocate(const General_options& options, const Symbol_table* symtab,
1018 const Layout*, Output_file* of);
1019
1020 // Get the name of a section.
1021 std::string
1022 do_section_name(unsigned int shndx)
1023 { return this->elf_file_.section_name(shndx); }
1024
1025 // Return the location of the contents of a section.
1026 Object::Location
1027 do_section_contents(unsigned int shndx)
1028 { return this->elf_file_.section_contents(shndx); }
1029
1030 // Return section flags.
1031 uint64_t
1032 do_section_flags(unsigned int shndx)
1033 { return this->elf_file_.section_flags(shndx); }
1034
1035 // Return section type.
1036 unsigned int
1037 do_section_type(unsigned int shndx)
1038 { return this->elf_file_.section_type(shndx); }
1039
1040 // Return the section link field.
1041 unsigned int
1042 do_section_link(unsigned int shndx)
1043 { return this->elf_file_.section_link(shndx); }
1044
1045 // Return the section info field.
1046 unsigned int
1047 do_section_info(unsigned int shndx)
1048 { return this->elf_file_.section_info(shndx); }
1049
1050 private:
1051 // For convenience.
1052 typedef Sized_relobj<size, big_endian> This;
1053 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1054 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1055 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1056 typedef elfcpp::Shdr<size, big_endian> Shdr;
1057
1058 // Find the SHT_SYMTAB section, given the section headers.
1059 void
1060 find_symtab(const unsigned char* pshdrs);
1061
1062 // Return whether SHDR has the right flags for a GNU style exception
1063 // frame section.
1064 bool
1065 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1066
1067 // Return whether there is a section named .eh_frame which might be
1068 // a GNU style exception frame section.
1069 bool
1070 find_eh_frame(const unsigned char* pshdrs, const char* names,
1071 off_t names_size) const;
1072
1073 // Whether to include a section group in the link.
1074 bool
1075 include_section_group(Layout*, unsigned int,
1076 const elfcpp::Shdr<size, big_endian>&,
1077 std::vector<bool>*);
1078
1079 // Whether to include a linkonce section in the link.
1080 bool
1081 include_linkonce_section(Layout*, const char*,
1082 const elfcpp::Shdr<size, big_endian>&);
1083
1084 // Views and sizes when relocating.
1085 struct View_size
1086 {
1087 unsigned char* view;
1088 typename elfcpp::Elf_types<size>::Elf_Addr address;
1089 off_t offset;
1090 off_t view_size;
1091 bool is_input_output_view;
1092 bool is_postprocessing_view;
1093 };
1094
1095 typedef std::vector<View_size> Views;
1096
1097 // Write section data to the output file. Record the views and
1098 // sizes in VIEWS for use when relocating.
1099 void
1100 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1101
1102 // Relocate the sections in the output file.
1103 void
1104 relocate_sections(const General_options& options, const Symbol_table*,
1105 const Layout*, const unsigned char* pshdrs, Views*);
1106
1107 // Write out the local symbols.
1108 void
1109 write_local_symbols(Output_file*,
1110 const Stringpool_template<char>*,
1111 const Stringpool_template<char>*);
1112
1113 // The GOT offsets of local symbols. This map also stores GOT offsets
1114 // for tp-relative offsets for TLS symbols.
1115 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1116
1117 // The TLS GOT offsets of local symbols. The map stores the offsets
1118 // for either a single GOT entry that holds the module index of a TLS
1119 // symbol, or a pair of GOT entries containing the module index and
1120 // dtv-relative offset.
1121 struct Tls_got_entry
1122 {
1123 Tls_got_entry(int got_offset, bool have_pair)
1124 : got_offset_(got_offset),
1125 have_pair_(have_pair)
1126 { }
1127 int got_offset_;
1128 bool have_pair_;
1129 };
1130 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1131
1132 // General access to the ELF file.
1133 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1134 // Index of SHT_SYMTAB section.
1135 unsigned int symtab_shndx_;
1136 // The number of local symbols.
1137 unsigned int local_symbol_count_;
1138 // The number of local symbols which go into the output file.
1139 unsigned int output_local_symbol_count_;
1140 // The number of local symbols which go into the output file's dynamic
1141 // symbol table.
1142 unsigned int output_local_dynsym_count_;
1143 // The entries in the symbol table for the external symbols.
1144 Symbols symbols_;
1145 // File offset for local symbols.
1146 off_t local_symbol_offset_;
1147 // File offset for local dynamic symbols.
1148 off_t local_dynsym_offset_;
1149 // Values of local symbols.
1150 Local_values local_values_;
1151 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1152 // for TLS symbols, indexed by symbol number.
1153 Local_got_offsets local_got_offsets_;
1154 // GOT offsets for local TLS symbols, indexed by symbol number
1155 // and GOT entry type.
1156 Local_tls_got_offsets local_tls_got_offsets_;
1157 // Whether this object has a GNU style .eh_frame section.
1158 bool has_eh_frame_;
1159 };
1160
1161 // A class to manage the list of all objects.
1162
1163 class Input_objects
1164 {
1165 public:
1166 Input_objects()
1167 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1168 system_library_directory_()
1169 { }
1170
1171 // The type of the list of input relocateable objects.
1172 typedef std::vector<Relobj*> Relobj_list;
1173 typedef Relobj_list::const_iterator Relobj_iterator;
1174
1175 // The type of the list of input dynamic objects.
1176 typedef std::vector<Dynobj*> Dynobj_list;
1177 typedef Dynobj_list::const_iterator Dynobj_iterator;
1178
1179 // Add an object to the list. Return true if all is well, or false
1180 // if this object should be ignored.
1181 bool
1182 add_object(Object*);
1183
1184 // Get the target we should use for the output file.
1185 Target*
1186 target() const
1187 { return this->target_; }
1188
1189 // For each dynamic object, check whether we've seen all of its
1190 // explicit dependencies.
1191 void
1192 check_dynamic_dependencies() const;
1193
1194 // Return whether an object was found in the system library
1195 // directory.
1196 bool
1197 found_in_system_library_directory(const Object*) const;
1198
1199 // Iterate over all regular objects.
1200
1201 Relobj_iterator
1202 relobj_begin() const
1203 { return this->relobj_list_.begin(); }
1204
1205 Relobj_iterator
1206 relobj_end() const
1207 { return this->relobj_list_.end(); }
1208
1209 // Iterate over all dynamic objects.
1210
1211 Dynobj_iterator
1212 dynobj_begin() const
1213 { return this->dynobj_list_.begin(); }
1214
1215 Dynobj_iterator
1216 dynobj_end() const
1217 { return this->dynobj_list_.end(); }
1218
1219 // Return whether we have seen any dynamic objects.
1220 bool
1221 any_dynamic() const
1222 { return !this->dynobj_list_.empty(); }
1223
1224 // Return the number of input objects.
1225 int
1226 number_of_input_objects() const
1227 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1228
1229 private:
1230 Input_objects(const Input_objects&);
1231 Input_objects& operator=(const Input_objects&);
1232
1233 // The list of ordinary objects included in the link.
1234 Relobj_list relobj_list_;
1235 // The list of dynamic objects included in the link.
1236 Dynobj_list dynobj_list_;
1237 // The target.
1238 Target* target_;
1239 // SONAMEs that we have seen.
1240 Unordered_set<std::string> sonames_;
1241 // The directory in which we find the libc.so.
1242 std::string system_library_directory_;
1243 };
1244
1245 // Some of the information we pass to the relocation routines. We
1246 // group this together to avoid passing a dozen different arguments.
1247
1248 template<int size, bool big_endian>
1249 struct Relocate_info
1250 {
1251 // Command line options.
1252 const General_options* options;
1253 // Symbol table.
1254 const Symbol_table* symtab;
1255 // Layout.
1256 const Layout* layout;
1257 // Object being relocated.
1258 Sized_relobj<size, big_endian>* object;
1259 // Section index of relocation section.
1260 unsigned int reloc_shndx;
1261 // Section index of section being relocated.
1262 unsigned int data_shndx;
1263
1264 // Return a string showing the location of a relocation. This is
1265 // only used for error messages.
1266 std::string
1267 location(size_t relnum, off_t reloffset) const;
1268 };
1269
1270 // Return an Object appropriate for the input file. P is BYTES long,
1271 // and holds the ELF header.
1272
1273 extern Object*
1274 make_elf_object(const std::string& name, Input_file*,
1275 off_t offset, const unsigned char* p,
1276 off_t bytes);
1277
1278 } // end namespace gold
1279
1280 #endif // !defined(GOLD_OBJECT_H)