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