PR 5990
[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, 2008 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 Cref;
40 class Archive;
41 class Layout;
42 class Output_section;
43 class Output_file;
44 class Output_symtab_xindex;
45 class Dynobj;
46 class Object_merge_map;
47 class Relocatable_relocs;
48
49 template<typename Stringpool_char>
50 class Stringpool_template;
51
52 // Data to pass from read_symbols() to add_symbols().
53
54 struct Read_symbols_data
55 {
56 // Section headers.
57 File_view* section_headers;
58 // Section names.
59 File_view* section_names;
60 // Size of section name data in bytes.
61 section_size_type section_names_size;
62 // Symbol data.
63 File_view* symbols;
64 // Size of symbol data in bytes.
65 section_size_type symbols_size;
66 // Offset of external symbols within symbol data. This structure
67 // sometimes contains only external symbols, in which case this will
68 // be zero. Sometimes it contains all symbols.
69 section_offset_type external_symbols_offset;
70 // Symbol names.
71 File_view* symbol_names;
72 // Size of symbol name data in bytes.
73 section_size_type symbol_names_size;
74
75 // Version information. This is only used on dynamic objects.
76 // Version symbol data (from SHT_GNU_versym section).
77 File_view* versym;
78 section_size_type versym_size;
79 // Version definition data (from SHT_GNU_verdef section).
80 File_view* verdef;
81 section_size_type verdef_size;
82 unsigned int verdef_info;
83 // Needed version data (from SHT_GNU_verneed section).
84 File_view* verneed;
85 section_size_type verneed_size;
86 unsigned int verneed_info;
87 };
88
89 // Information used to print error messages.
90
91 struct Symbol_location_info
92 {
93 std::string source_file;
94 std::string enclosing_symbol_name;
95 int line_number;
96 };
97
98 // Data about a single relocation section. This is read in
99 // read_relocs and processed in scan_relocs.
100
101 struct Section_relocs
102 {
103 // Index of reloc section.
104 unsigned int reloc_shndx;
105 // Index of section that relocs apply to.
106 unsigned int data_shndx;
107 // Contents of reloc section.
108 File_view* contents;
109 // Reloc section type.
110 unsigned int sh_type;
111 // Number of reloc entries.
112 size_t reloc_count;
113 // Output section.
114 Output_section* output_section;
115 // Whether this section has special handling for offsets.
116 bool needs_special_offset_handling;
117 // Whether the data section is allocated (has the SHF_ALLOC flag set).
118 bool is_data_section_allocated;
119 };
120
121 // Relocations in an object file. This is read in read_relocs and
122 // processed in scan_relocs.
123
124 struct Read_relocs_data
125 {
126 typedef std::vector<Section_relocs> Relocs_list;
127 // The relocations.
128 Relocs_list relocs;
129 // The local symbols.
130 File_view* local_symbols;
131 };
132
133 // The Xindex class manages section indexes for objects with more than
134 // 0xff00 sections.
135
136 class Xindex
137 {
138 public:
139 Xindex(int large_shndx_offset)
140 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
141 { }
142
143 // Initialize the symtab_xindex_ array, given the object and the
144 // section index of the symbol table to use.
145 template<int size, bool big_endian>
146 void
147 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
148
149 // Read in the symtab_xindex_ array, given its section index.
150 // PSHDRS may optionally point to the section headers.
151 template<int size, bool big_endian>
152 void
153 read_symtab_xindex(Object*, unsigned int xindex_shndx,
154 const unsigned char* pshdrs);
155
156 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
157 // real section index.
158 unsigned int
159 sym_xindex_to_shndx(Object* object, unsigned int symndx);
160
161 private:
162 // The type of the array giving the real section index for symbols
163 // whose st_shndx field holds SHN_XINDEX.
164 typedef std::vector<unsigned int> Symtab_xindex;
165
166 // Adjust a section index if necessary. This should only be called
167 // for ordinary section indexes.
168 unsigned int
169 adjust_shndx(unsigned int shndx)
170 {
171 if (shndx >= elfcpp::SHN_LORESERVE)
172 shndx += this->large_shndx_offset_;
173 return shndx;
174 }
175
176 // Adjust to apply to large section indexes.
177 int large_shndx_offset_;
178 // The data from the SHT_SYMTAB_SHNDX section.
179 Symtab_xindex symtab_xindex_;
180 };
181
182 // Object is an abstract base class which represents either a 32-bit
183 // or a 64-bit input object. This can be a regular object file
184 // (ET_REL) or a shared object (ET_DYN).
185
186 class Object
187 {
188 public:
189 // NAME is the name of the object as we would report it to the user
190 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
191 // used to read the file. OFFSET is the offset within the input
192 // file--0 for a .o or .so file, something else for a .a file.
193 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
194 off_t offset = 0)
195 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
196 is_dynamic_(is_dynamic), target_(NULL), xindex_(NULL)
197 { input_file->file().add_object(); }
198
199 virtual ~Object()
200 { this->input_file_->file().remove_object(); }
201
202 // Return the name of the object as we would report it to the tuser.
203 const std::string&
204 name() const
205 { return this->name_; }
206
207 // Get the offset into the file.
208 off_t
209 offset() const
210 { return this->offset_; }
211
212 // Return whether this is a dynamic object.
213 bool
214 is_dynamic() const
215 { return this->is_dynamic_; }
216
217 // Return the target structure associated with this object.
218 Target*
219 target() const
220 { return this->target_; }
221
222 // Lock the underlying file.
223 void
224 lock(const Task* t)
225 { this->input_file()->file().lock(t); }
226
227 // Unlock the underlying file.
228 void
229 unlock(const Task* t)
230 { this->input_file()->file().unlock(t); }
231
232 // Return whether the underlying file is locked.
233 bool
234 is_locked() const
235 { return this->input_file()->file().is_locked(); }
236
237 // Return the token, so that the task can be queued.
238 Task_token*
239 token()
240 { return this->input_file()->file().token(); }
241
242 // Release the underlying file.
243 void
244 release()
245 { this->input_file_->file().release(); }
246
247 // Return whether we should just read symbols from this file.
248 bool
249 just_symbols() const
250 { return this->input_file()->just_symbols(); }
251
252 // Return the sized target structure associated with this object.
253 // This is like the target method but it returns a pointer of
254 // appropriate checked type.
255 template<int size, bool big_endian>
256 Sized_target<size, big_endian>*
257 sized_target() const;
258
259 // Get the number of sections.
260 unsigned int
261 shnum() const
262 { return this->shnum_; }
263
264 // Return a view of the contents of a section. Set *PLEN to the
265 // size. CACHE is a hint as in File_read::get_view.
266 const unsigned char*
267 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
268
269 // Adjust a symbol's section index as needed. SYMNDX is the index
270 // of the symbol and SHNDX is the symbol's section from
271 // get_st_shndx. This returns the section index. It sets
272 // *IS_ORDINARY to indicate whether this is a normal section index,
273 // rather than a special code between SHN_LORESERVE and
274 // SHN_HIRESERVE.
275 unsigned int
276 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
277 {
278 if (shndx < elfcpp::SHN_LORESERVE)
279 *is_ordinary = true;
280 else if (shndx == elfcpp::SHN_XINDEX)
281 {
282 if (this->xindex_ == NULL)
283 this->xindex_ = this->do_initialize_xindex();
284 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
285 *is_ordinary = true;
286 }
287 else
288 *is_ordinary = false;
289 return shndx;
290 }
291
292 // Return the size of a section given a section index.
293 uint64_t
294 section_size(unsigned int shndx)
295 { return this->do_section_size(shndx); }
296
297 // Return the name of a section given a section index.
298 std::string
299 section_name(unsigned int shndx)
300 { return this->do_section_name(shndx); }
301
302 // Return the section flags given a section index.
303 uint64_t
304 section_flags(unsigned int shndx)
305 { return this->do_section_flags(shndx); }
306
307 // Return the section address given a section index.
308 uint64_t
309 section_address(unsigned int shndx)
310 { return this->do_section_address(shndx); }
311
312 // Return the section type given a section index.
313 unsigned int
314 section_type(unsigned int shndx)
315 { return this->do_section_type(shndx); }
316
317 // Return the section link field given a section index.
318 unsigned int
319 section_link(unsigned int shndx)
320 { return this->do_section_link(shndx); }
321
322 // Return the section info field given a section index.
323 unsigned int
324 section_info(unsigned int shndx)
325 { return this->do_section_info(shndx); }
326
327 // Return the required section alignment given a section index.
328 uint64_t
329 section_addralign(unsigned int shndx)
330 { return this->do_section_addralign(shndx); }
331
332 // Read the symbol information.
333 void
334 read_symbols(Read_symbols_data* sd)
335 { return this->do_read_symbols(sd); }
336
337 // Pass sections which should be included in the link to the Layout
338 // object, and record where the sections go in the output file.
339 void
340 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
341 { this->do_layout(symtab, layout, sd); }
342
343 // Add symbol information to the global symbol table.
344 void
345 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
346 { this->do_add_symbols(symtab, sd); }
347
348 // Functions and types for the elfcpp::Elf_file interface. This
349 // permit us to use Object as the File template parameter for
350 // elfcpp::Elf_file.
351
352 // The View class is returned by view. It must support a single
353 // method, data(). This is trivial, because get_view does what we
354 // need.
355 class View
356 {
357 public:
358 View(const unsigned char* p)
359 : p_(p)
360 { }
361
362 const unsigned char*
363 data() const
364 { return this->p_; }
365
366 private:
367 const unsigned char* p_;
368 };
369
370 // Return a View.
371 View
372 view(off_t file_offset, section_size_type data_size)
373 { return View(this->get_view(file_offset, data_size, true, true)); }
374
375 // Report an error.
376 void
377 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
378
379 // A location in the file.
380 struct Location
381 {
382 off_t file_offset;
383 off_t data_size;
384
385 Location(off_t fo, section_size_type ds)
386 : file_offset(fo), data_size(ds)
387 { }
388 };
389
390 // Get a View given a Location.
391 View view(Location loc)
392 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
393
394 // Get a view into the underlying file.
395 const unsigned char*
396 get_view(off_t start, section_size_type size, bool aligned, bool cache)
397 {
398 return this->input_file()->file().get_view(this->offset_, start, size,
399 aligned, cache);
400 }
401
402 // Get a lasting view into the underlying file.
403 File_view*
404 get_lasting_view(off_t start, section_size_type size, bool aligned,
405 bool cache)
406 {
407 return this->input_file()->file().get_lasting_view(this->offset_, start,
408 size, aligned, cache);
409 }
410
411 // Read data from the underlying file.
412 void
413 read(off_t start, section_size_type size, void* p)
414 { this->input_file()->file().read(start + this->offset_, size, p); }
415
416 // Read multiple data from the underlying file.
417 void
418 read_multiple(const File_read::Read_multiple& rm)
419 { this->input_file()->file().read_multiple(this->offset_, rm); }
420
421 // Stop caching views in the underlying file.
422 void
423 clear_view_cache_marks()
424 { this->input_file()->file().clear_view_cache_marks(); }
425
426 // Get the number of global symbols defined by this object, and the
427 // number of the symbols whose final definition came from this
428 // object.
429 void
430 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
431 size_t* used) const
432 { this->do_get_global_symbol_counts(symtab, defined, used); }
433
434 protected:
435 // Read the symbols--implemented by child class.
436 virtual void
437 do_read_symbols(Read_symbols_data*) = 0;
438
439 // Lay out sections--implemented by child class.
440 virtual void
441 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
442
443 // Add symbol information to the global symbol table--implemented by
444 // child class.
445 virtual void
446 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
447
448 // Return the location of the contents of a section. Implemented by
449 // child class.
450 virtual Location
451 do_section_contents(unsigned int shndx) = 0;
452
453 // Get the size of a section--implemented by child class.
454 virtual uint64_t
455 do_section_size(unsigned int shndx) = 0;
456
457 // Get the name of a section--implemented by child class.
458 virtual std::string
459 do_section_name(unsigned int shndx) = 0;
460
461 // Get section flags--implemented by child class.
462 virtual uint64_t
463 do_section_flags(unsigned int shndx) = 0;
464
465 // Get section address--implemented by child class.
466 virtual uint64_t
467 do_section_address(unsigned int shndx) = 0;
468
469 // Get section type--implemented by child class.
470 virtual unsigned int
471 do_section_type(unsigned int shndx) = 0;
472
473 // Get section link field--implemented by child class.
474 virtual unsigned int
475 do_section_link(unsigned int shndx) = 0;
476
477 // Get section info field--implemented by child class.
478 virtual unsigned int
479 do_section_info(unsigned int shndx) = 0;
480
481 // Get section alignment--implemented by child class.
482 virtual uint64_t
483 do_section_addralign(unsigned int shndx) = 0;
484
485 // Return the Xindex structure to use.
486 virtual Xindex*
487 do_initialize_xindex() = 0;
488
489 // Implement get_global_symbol_counts--implemented by child class.
490 virtual void
491 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
492
493 // Get the file. We pass on const-ness.
494 Input_file*
495 input_file()
496 { return this->input_file_; }
497
498 const Input_file*
499 input_file() const
500 { return this->input_file_; }
501
502 // Set the target.
503 void
504 set_target(int machine, int size, bool big_endian, int osabi,
505 int abiversion);
506
507 // Set the number of sections.
508 void
509 set_shnum(int shnum)
510 { this->shnum_ = shnum; }
511
512 // Functions used by both Sized_relobj and Sized_dynobj.
513
514 // Read the section data into a Read_symbols_data object.
515 template<int size, bool big_endian>
516 void
517 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
518 Read_symbols_data*);
519
520 // Let the child class initialize the xindex object directly.
521 void
522 set_xindex(Xindex* xindex)
523 {
524 gold_assert(this->xindex_ == NULL);
525 this->xindex_ = xindex;
526 }
527
528 // If NAME is the name of a special .gnu.warning section, arrange
529 // for the warning to be issued. SHNDX is the section index.
530 // Return whether it is a warning section.
531 bool
532 handle_gnu_warning_section(const char* name, unsigned int shndx,
533 Symbol_table*);
534
535 private:
536 // This class may not be copied.
537 Object(const Object&);
538 Object& operator=(const Object&);
539
540 // Name of object as printed to user.
541 std::string name_;
542 // For reading the file.
543 Input_file* input_file_;
544 // Offset within the file--0 for an object file, non-0 for an
545 // archive.
546 off_t offset_;
547 // Number of input sections.
548 unsigned int shnum_;
549 // Whether this is a dynamic object.
550 bool is_dynamic_;
551 // Target functions--may be NULL if the target is not known.
552 Target* target_;
553 // Many sections for objects with more than SHN_LORESERVE sections.
554 Xindex* xindex_;
555 };
556
557 // Implement sized_target inline for efficiency. This approach breaks
558 // static type checking, but is made safe using asserts.
559
560 template<int size, bool big_endian>
561 inline Sized_target<size, big_endian>*
562 Object::sized_target() const
563 {
564 gold_assert(this->target_->get_size() == size);
565 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
566 return static_cast<Sized_target<size, big_endian>*>(this->target_);
567 }
568
569 // A regular object (ET_REL). This is an abstract base class itself.
570 // The implementation is the template class Sized_relobj.
571
572 class Relobj : public Object
573 {
574 public:
575 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
576 : Object(name, input_file, false, offset),
577 output_sections_(),
578 map_to_relocatable_relocs_(NULL),
579 object_merge_map_(NULL),
580 relocs_must_follow_section_writes_(false)
581 { }
582
583 // Read the relocs.
584 void
585 read_relocs(Read_relocs_data* rd)
586 { return this->do_read_relocs(rd); }
587
588 // Scan the relocs and adjust the symbol table.
589 void
590 scan_relocs(const General_options& options, Symbol_table* symtab,
591 Layout* layout, Read_relocs_data* rd)
592 { return this->do_scan_relocs(options, symtab, layout, rd); }
593
594 // The number of local symbols in the input symbol table.
595 virtual unsigned int
596 local_symbol_count() const
597 { return this->do_local_symbol_count(); }
598
599 // Initial local symbol processing: count the number of local symbols
600 // in the output symbol table and dynamic symbol table; add local symbol
601 // names to *POOL and *DYNPOOL.
602 void
603 count_local_symbols(Stringpool_template<char>* pool,
604 Stringpool_template<char>* dynpool)
605 { return this->do_count_local_symbols(pool, dynpool); }
606
607 // Set the values of the local symbols, set the output symbol table
608 // indexes for the local variables, and set the offset where local
609 // symbol information will be stored. Returns the new local symbol index.
610 unsigned int
611 finalize_local_symbols(unsigned int index, off_t off)
612 { return this->do_finalize_local_symbols(index, off); }
613
614 // Set the output dynamic symbol table indexes for the local variables.
615 unsigned int
616 set_local_dynsym_indexes(unsigned int index)
617 { return this->do_set_local_dynsym_indexes(index); }
618
619 // Set the offset where local dynamic symbol information will be stored.
620 unsigned int
621 set_local_dynsym_offset(off_t off)
622 { return this->do_set_local_dynsym_offset(off); }
623
624 // Relocate the input sections and write out the local symbols.
625 void
626 relocate(const General_options& options, const Symbol_table* symtab,
627 const Layout* layout, Output_file* of)
628 { return this->do_relocate(options, symtab, layout, of); }
629
630 // Return whether an input section is being included in the link.
631 bool
632 is_section_included(unsigned int shndx) const
633 {
634 gold_assert(shndx < this->output_sections_.size());
635 return this->output_sections_[shndx] != NULL;
636 }
637
638 // Given a section index, return the corresponding Output_section.
639 // The return value will be NULL if the section is not included in
640 // the link.
641 Output_section*
642 output_section(unsigned int shndx) const
643 {
644 gold_assert(shndx < this->output_sections_.size());
645 return this->output_sections_[shndx];
646 }
647
648 // Given a section index, return the offset in the Output_section.
649 // The return value will be -1U if the section is specially mapped,
650 // such as a merge section.
651 uint64_t
652 output_section_offset(unsigned int shndx) const
653 { return this->do_output_section_offset(shndx); }
654
655 // Set the offset of an input section within its output section.
656 virtual void
657 set_section_offset(unsigned int shndx, uint64_t off)
658 { this->do_set_section_offset(shndx, off); }
659
660 // Return true if we need to wait for output sections to be written
661 // before we can apply relocations. This is true if the object has
662 // any relocations for sections which require special handling, such
663 // as the exception frame section.
664 bool
665 relocs_must_follow_section_writes() const
666 { return this->relocs_must_follow_section_writes_; }
667
668 // Return the object merge map.
669 Object_merge_map*
670 merge_map() const
671 { return this->object_merge_map_; }
672
673 // Set the object merge map.
674 void
675 set_merge_map(Object_merge_map* object_merge_map)
676 {
677 gold_assert(this->object_merge_map_ == NULL);
678 this->object_merge_map_ = object_merge_map;
679 }
680
681 // Record the relocatable reloc info for an input reloc section.
682 void
683 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
684 {
685 gold_assert(reloc_shndx < this->shnum());
686 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
687 }
688
689 // Get the relocatable reloc info for an input reloc section.
690 Relocatable_relocs*
691 relocatable_relocs(unsigned int reloc_shndx)
692 {
693 gold_assert(reloc_shndx < this->shnum());
694 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
695 }
696
697 protected:
698 // The output section to be used for each input section, indexed by
699 // the input section number. The output section is NULL if the
700 // input section is to be discarded.
701 typedef std::vector<Output_section*> Output_sections;
702
703 // Read the relocs--implemented by child class.
704 virtual void
705 do_read_relocs(Read_relocs_data*) = 0;
706
707 // Scan the relocs--implemented by child class.
708 virtual void
709 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
710 Read_relocs_data*) = 0;
711
712 // Return the number of local symbols--implemented by child class.
713 virtual unsigned int
714 do_local_symbol_count() const = 0;
715
716 // Count local symbols--implemented by child class.
717 virtual void
718 do_count_local_symbols(Stringpool_template<char>*,
719 Stringpool_template<char>*) = 0;
720
721 // Finalize the local symbols. Set the output symbol table indexes
722 // for the local variables, and set the offset where local symbol
723 // information will be stored.
724 virtual unsigned int
725 do_finalize_local_symbols(unsigned int, off_t) = 0;
726
727 // Set the output dynamic symbol table indexes for the local variables.
728 virtual unsigned int
729 do_set_local_dynsym_indexes(unsigned int) = 0;
730
731 // Set the offset where local dynamic symbol information will be stored.
732 virtual unsigned int
733 do_set_local_dynsym_offset(off_t) = 0;
734
735 // Relocate the input sections and write out the local
736 // symbols--implemented by child class.
737 virtual void
738 do_relocate(const General_options& options, const Symbol_table* symtab,
739 const Layout*, Output_file* of) = 0;
740
741 // Get the offset of a section--implemented by child class.
742 virtual uint64_t
743 do_output_section_offset(unsigned int shndx) const = 0;
744
745 // Set the offset of a section--implemented by child class.
746 virtual void
747 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
748
749 // Return the vector mapping input sections to output sections.
750 Output_sections&
751 output_sections()
752 { return this->output_sections_; }
753
754 const Output_sections&
755 output_sections() const
756 { return this->output_sections_; }
757
758 // Set the size of the relocatable relocs array.
759 void
760 size_relocatable_relocs()
761 {
762 this->map_to_relocatable_relocs_ =
763 new std::vector<Relocatable_relocs*>(this->shnum());
764 }
765
766 // Record that we must wait for the output sections to be written
767 // before applying relocations.
768 void
769 set_relocs_must_follow_section_writes()
770 { this->relocs_must_follow_section_writes_ = true; }
771
772 private:
773 // Mapping from input sections to output section.
774 Output_sections output_sections_;
775 // Mapping from input section index to the information recorded for
776 // the relocations. This is only used for a relocatable link.
777 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
778 // Mappings for merge sections. This is managed by the code in the
779 // Merge_map class.
780 Object_merge_map* object_merge_map_;
781 // Whether we need to wait for output sections to be written before
782 // we can apply relocations.
783 bool relocs_must_follow_section_writes_;
784 };
785
786 // This class is used to handle relocations against a section symbol
787 // in an SHF_MERGE section. For such a symbol, we need to know the
788 // addend of the relocation before we can determine the final value.
789 // The addend gives us the location in the input section, and we can
790 // determine how it is mapped to the output section. For a
791 // non-section symbol, we apply the addend to the final value of the
792 // symbol; that is done in finalize_local_symbols, and does not use
793 // this class.
794
795 template<int size>
796 class Merged_symbol_value
797 {
798 public:
799 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
800
801 // We use a hash table to map offsets in the input section to output
802 // addresses.
803 typedef Unordered_map<section_offset_type, Value> Output_addresses;
804
805 Merged_symbol_value(Value input_value, Value output_start_address)
806 : input_value_(input_value), output_start_address_(output_start_address),
807 output_addresses_()
808 { }
809
810 // Initialize the hash table.
811 void
812 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
813
814 // Release the hash table to save space.
815 void
816 free_input_to_output_map()
817 { this->output_addresses_.clear(); }
818
819 // Get the output value corresponding to an addend. The object and
820 // input section index are passed in because the caller will have
821 // them; otherwise we could store them here.
822 Value
823 value(const Relobj* object, unsigned int input_shndx, Value addend) const
824 {
825 // This is a relocation against a section symbol. ADDEND is the
826 // offset in the section. The result should be the start of some
827 // merge area. If the object file wants something else, it should
828 // use a regular symbol rather than a section symbol.
829 // Unfortunately, PR 6658 shows a case in which the object file
830 // refers to the section symbol, but uses a negative ADDEND to
831 // compensate for a PC relative reloc. We can't handle the
832 // general case. However, we can handle the special case of a
833 // negative addend, by assuming that it refers to the start of the
834 // section. Of course, that means that we have to guess when
835 // ADDEND is negative. It is normal to see a 32-bit value here
836 // even when the template parameter size is 64, as 64-bit object
837 // file formats have 32-bit relocations. We know this is a merge
838 // section, so we know it has to fit into memory. So we assume
839 // that we won't see a value larger than a large 32-bit unsigned
840 // value. This will break objects with very very large merge
841 // sections; they probably break in other ways anyhow.
842 Value input_offset = this->input_value_;
843 if (addend < 0xffffff00)
844 {
845 input_offset += addend;
846 addend = 0;
847 }
848 typename Output_addresses::const_iterator p =
849 this->output_addresses_.find(input_offset);
850 if (p != this->output_addresses_.end())
851 return p->second + addend;
852
853 return (this->value_from_output_section(object, input_shndx, input_offset)
854 + addend);
855 }
856
857 private:
858 // Get the output value for an input offset if we couldn't find it
859 // in the hash table.
860 Value
861 value_from_output_section(const Relobj*, unsigned int input_shndx,
862 Value input_offset) const;
863
864 // The value of the section symbol in the input file. This is
865 // normally zero, but could in principle be something else.
866 Value input_value_;
867 // The start address of this merged section in the output file.
868 Value output_start_address_;
869 // A hash table which maps offsets in the input section to output
870 // addresses. This only maps specific offsets, not all offsets.
871 Output_addresses output_addresses_;
872 };
873
874 // This POD class is holds the value of a symbol. This is used for
875 // local symbols, and for all symbols during relocation processing.
876 // For special sections, such as SHF_MERGE sections, this calls a
877 // function to get the final symbol value.
878
879 template<int size>
880 class Symbol_value
881 {
882 public:
883 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
884
885 Symbol_value()
886 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
887 is_ordinary_shndx_(false), is_section_symbol_(false),
888 is_tls_symbol_(false), has_output_value_(true)
889 { this->u_.value = 0; }
890
891 // Get the value of this symbol. OBJECT is the object in which this
892 // symbol is defined, and ADDEND is an addend to add to the value.
893 template<bool big_endian>
894 Value
895 value(const Sized_relobj<size, big_endian>* object, Value addend) const
896 {
897 if (this->has_output_value_)
898 return this->u_.value + addend;
899 else
900 {
901 gold_assert(this->is_ordinary_shndx_);
902 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
903 addend);
904 }
905 }
906
907 // Set the value of this symbol in the output symbol table.
908 void
909 set_output_value(Value value)
910 { this->u_.value = value; }
911
912 // For a section symbol in a merged section, we need more
913 // information.
914 void
915 set_merged_symbol_value(Merged_symbol_value<size>* msv)
916 {
917 gold_assert(this->is_section_symbol_);
918 this->has_output_value_ = false;
919 this->u_.merged_symbol_value = msv;
920 }
921
922 // Initialize the input to output map for a section symbol in a
923 // merged section. We also initialize the value of a non-section
924 // symbol in a merged section.
925 void
926 initialize_input_to_output_map(const Relobj* object)
927 {
928 if (!this->has_output_value_)
929 {
930 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
931 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
932 msv->initialize_input_to_output_map(object, this->input_shndx_);
933 }
934 }
935
936 // Free the input to output map for a section symbol in a merged
937 // section.
938 void
939 free_input_to_output_map()
940 {
941 if (!this->has_output_value_)
942 this->u_.merged_symbol_value->free_input_to_output_map();
943 }
944
945 // Set the value of the symbol from the input file. This is only
946 // called by count_local_symbols, to communicate the value to
947 // finalize_local_symbols.
948 void
949 set_input_value(Value value)
950 { this->u_.value = value; }
951
952 // Return the input value. This is only called by
953 // finalize_local_symbols and (in special cases) relocate_section.
954 Value
955 input_value() const
956 { return this->u_.value; }
957
958 // Return whether this symbol should go into the output symbol
959 // table.
960 bool
961 needs_output_symtab_entry() const
962 { return this->output_symtab_index_ != -1U; }
963
964 // Return the index in the output symbol table.
965 unsigned int
966 output_symtab_index() const
967 {
968 gold_assert(this->output_symtab_index_ != 0);
969 return this->output_symtab_index_;
970 }
971
972 // Set the index in the output symbol table.
973 void
974 set_output_symtab_index(unsigned int i)
975 {
976 gold_assert(this->output_symtab_index_ == 0);
977 this->output_symtab_index_ = i;
978 }
979
980 // Record that this symbol should not go into the output symbol
981 // table.
982 void
983 set_no_output_symtab_entry()
984 {
985 gold_assert(this->output_symtab_index_ == 0);
986 this->output_symtab_index_ = -1U;
987 }
988
989 // Set the index in the output dynamic symbol table.
990 void
991 set_needs_output_dynsym_entry()
992 {
993 gold_assert(!this->is_section_symbol());
994 this->output_dynsym_index_ = 0;
995 }
996
997 // Return whether this symbol should go into the output symbol
998 // table.
999 bool
1000 needs_output_dynsym_entry() const
1001 {
1002 return this->output_dynsym_index_ != -1U;
1003 }
1004
1005 // Record that this symbol should go into the dynamic symbol table.
1006 void
1007 set_output_dynsym_index(unsigned int i)
1008 {
1009 gold_assert(this->output_dynsym_index_ == 0);
1010 this->output_dynsym_index_ = i;
1011 }
1012
1013 // Return the index in the output dynamic symbol table.
1014 unsigned int
1015 output_dynsym_index() const
1016 {
1017 gold_assert(this->output_dynsym_index_ != 0
1018 && this->output_dynsym_index_ != -1U);
1019 return this->output_dynsym_index_;
1020 }
1021
1022 // Set the index of the input section in the input file.
1023 void
1024 set_input_shndx(unsigned int i, bool is_ordinary)
1025 {
1026 this->input_shndx_ = i;
1027 // input_shndx_ field is a bitfield, so make sure that the value
1028 // fits.
1029 gold_assert(this->input_shndx_ == i);
1030 this->is_ordinary_shndx_ = is_ordinary;
1031 }
1032
1033 // Return the index of the input section in the input file.
1034 unsigned int
1035 input_shndx(bool* is_ordinary) const
1036 {
1037 *is_ordinary = this->is_ordinary_shndx_;
1038 return this->input_shndx_;
1039 }
1040
1041 // Whether this is a section symbol.
1042 bool
1043 is_section_symbol() const
1044 { return this->is_section_symbol_; }
1045
1046 // Record that this is a section symbol.
1047 void
1048 set_is_section_symbol()
1049 {
1050 gold_assert(!this->needs_output_dynsym_entry());
1051 this->is_section_symbol_ = true;
1052 }
1053
1054 // Record that this is a TLS symbol.
1055 void
1056 set_is_tls_symbol()
1057 { this->is_tls_symbol_ = true; }
1058
1059 // Return TRUE if this is a TLS symbol.
1060 bool
1061 is_tls_symbol() const
1062 { return this->is_tls_symbol_; }
1063
1064 private:
1065 // The index of this local symbol in the output symbol table. This
1066 // will be -1 if the symbol should not go into the symbol table.
1067 unsigned int output_symtab_index_;
1068 // The index of this local symbol in the dynamic symbol table. This
1069 // will be -1 if the symbol should not go into the symbol table.
1070 unsigned int output_dynsym_index_;
1071 // The section index in the input file in which this symbol is
1072 // defined.
1073 unsigned int input_shndx_ : 28;
1074 // Whether the section index is an ordinary index, not a special
1075 // value.
1076 bool is_ordinary_shndx_ : 1;
1077 // Whether this is a STT_SECTION symbol.
1078 bool is_section_symbol_ : 1;
1079 // Whether this is a STT_TLS symbol.
1080 bool is_tls_symbol_ : 1;
1081 // Whether this symbol has a value for the output file. This is
1082 // normally set to true during Layout::finalize, by
1083 // finalize_local_symbols. It will be false for a section symbol in
1084 // a merge section, as for such symbols we can not determine the
1085 // value to use in a relocation until we see the addend.
1086 bool has_output_value_ : 1;
1087 union
1088 {
1089 // This is used if has_output_value_ is true. Between
1090 // count_local_symbols and finalize_local_symbols, this is the
1091 // value in the input file. After finalize_local_symbols, it is
1092 // the value in the output file.
1093 Value value;
1094 // This is used if has_output_value_ is false. It points to the
1095 // information we need to get the value for a merge section.
1096 Merged_symbol_value<size>* merged_symbol_value;
1097 } u_;
1098 };
1099
1100 // A GOT offset list. A symbol may have more than one GOT offset
1101 // (e.g., when mixing modules compiled with two different TLS models),
1102 // but will usually have at most one. GOT_TYPE identifies the type of
1103 // GOT entry; its values are specific to each target.
1104
1105 class Got_offset_list
1106 {
1107 public:
1108 Got_offset_list()
1109 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1110 { }
1111
1112 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1113 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1114 { }
1115
1116 ~Got_offset_list()
1117 {
1118 if (this->got_next_ != NULL)
1119 {
1120 delete this->got_next_;
1121 this->got_next_ = NULL;
1122 }
1123 }
1124
1125 // Initialize the fields to their default values.
1126 void
1127 init()
1128 {
1129 this->got_type_ = -1U;
1130 this->got_offset_ = 0;
1131 this->got_next_ = NULL;
1132 }
1133
1134 // Set the offset for the GOT entry of type GOT_TYPE.
1135 void
1136 set_offset(unsigned int got_type, unsigned int got_offset)
1137 {
1138 if (this->got_type_ == -1U)
1139 {
1140 this->got_type_ = got_type;
1141 this->got_offset_ = got_offset;
1142 }
1143 else
1144 {
1145 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1146 {
1147 if (g->got_type_ == got_type)
1148 {
1149 g->got_offset_ = got_offset;
1150 return;
1151 }
1152 }
1153 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1154 g->got_next_ = this->got_next_;
1155 this->got_next_ = g;
1156 }
1157 }
1158
1159 // Return the offset for a GOT entry of type GOT_TYPE.
1160 unsigned int
1161 get_offset(unsigned int got_type) const
1162 {
1163 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1164 {
1165 if (g->got_type_ == got_type)
1166 return g->got_offset_;
1167 }
1168 return -1U;
1169 }
1170
1171 private:
1172 unsigned int got_type_;
1173 unsigned int got_offset_;
1174 Got_offset_list* got_next_;
1175 };
1176
1177 // A regular object file. This is size and endian specific.
1178
1179 template<int size, bool big_endian>
1180 class Sized_relobj : public Relobj
1181 {
1182 public:
1183 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1184 typedef std::vector<Symbol*> Symbols;
1185 typedef std::vector<Symbol_value<size> > Local_values;
1186
1187 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1188 const typename elfcpp::Ehdr<size, big_endian>&);
1189
1190 ~Sized_relobj();
1191
1192 // Set up the object file based on the ELF header.
1193 void
1194 setup(const typename elfcpp::Ehdr<size, big_endian>&);
1195
1196 // Return the number of symbols. This is only valid after
1197 // Object::add_symbols has been called.
1198 unsigned int
1199 symbol_count() const
1200 { return this->local_symbol_count_ + this->symbols_.size(); }
1201
1202 // If SYM is the index of a global symbol in the object file's
1203 // symbol table, return the Symbol object. Otherwise, return NULL.
1204 Symbol*
1205 global_symbol(unsigned int sym) const
1206 {
1207 if (sym >= this->local_symbol_count_)
1208 {
1209 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1210 return this->symbols_[sym - this->local_symbol_count_];
1211 }
1212 return NULL;
1213 }
1214
1215 // Return the section index of symbol SYM. Set *VALUE to its value
1216 // in the object file. Set *IS_ORDINARY if this is an ordinary
1217 // section index, not a special code between SHN_LORESERVE and
1218 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1219 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1220 // it will not return the final value of the symbol in the link.
1221 unsigned int
1222 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1223
1224 // Return a pointer to the Symbol_value structure which holds the
1225 // value of a local symbol.
1226 const Symbol_value<size>*
1227 local_symbol(unsigned int sym) const
1228 {
1229 gold_assert(sym < this->local_values_.size());
1230 return &this->local_values_[sym];
1231 }
1232
1233 // Return the index of local symbol SYM in the ordinary symbol
1234 // table. A value of -1U means that the symbol is not being output.
1235 unsigned int
1236 symtab_index(unsigned int sym) const
1237 {
1238 gold_assert(sym < this->local_values_.size());
1239 return this->local_values_[sym].output_symtab_index();
1240 }
1241
1242 // Return the index of local symbol SYM in the dynamic symbol
1243 // table. A value of -1U means that the symbol is not being output.
1244 unsigned int
1245 dynsym_index(unsigned int sym) const
1246 {
1247 gold_assert(sym < this->local_values_.size());
1248 return this->local_values_[sym].output_dynsym_index();
1249 }
1250
1251 // Return the input section index of local symbol SYM.
1252 unsigned int
1253 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1254 {
1255 gold_assert(sym < this->local_values_.size());
1256 return this->local_values_[sym].input_shndx(is_ordinary);
1257 }
1258
1259 // Return the appropriate Sized_target structure.
1260 Sized_target<size, big_endian>*
1261 sized_target()
1262 { return this->Object::sized_target<size, big_endian>(); }
1263
1264 // Record that local symbol SYM needs a dynamic symbol entry.
1265 void
1266 set_needs_output_dynsym_entry(unsigned int sym)
1267 {
1268 gold_assert(sym < this->local_values_.size());
1269 this->local_values_[sym].set_needs_output_dynsym_entry();
1270 }
1271
1272 // Return whether the local symbol SYMNDX has a GOT offset.
1273 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1274 bool
1275 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1276 {
1277 Local_got_offsets::const_iterator p =
1278 this->local_got_offsets_.find(symndx);
1279 return (p != this->local_got_offsets_.end()
1280 && p->second->get_offset(got_type) != -1U);
1281 }
1282
1283 // Return the GOT offset of the local symbol SYMNDX.
1284 unsigned int
1285 local_got_offset(unsigned int symndx, unsigned int got_type) const
1286 {
1287 Local_got_offsets::const_iterator p =
1288 this->local_got_offsets_.find(symndx);
1289 gold_assert(p != this->local_got_offsets_.end());
1290 unsigned int off = p->second->get_offset(got_type);
1291 gold_assert(off != -1U);
1292 return off;
1293 }
1294
1295 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1296 void
1297 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1298 unsigned int got_offset)
1299 {
1300 Local_got_offsets::const_iterator p =
1301 this->local_got_offsets_.find(symndx);
1302 if (p != this->local_got_offsets_.end())
1303 p->second->set_offset(got_type, got_offset);
1304 else
1305 {
1306 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1307 std::pair<Local_got_offsets::iterator, bool> ins =
1308 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1309 gold_assert(ins.second);
1310 }
1311 }
1312
1313 // Get the offset of input section SHNDX within its output section.
1314 // This is -1 if the input section requires a special mapping, such
1315 // as a merge section. The output section can be found in the
1316 // output_sections_ field of the parent class Relobj.
1317 Address
1318 get_output_section_offset(unsigned int shndx) const
1319 {
1320 gold_assert(shndx < this->section_offsets_.size());
1321 return this->section_offsets_[shndx];
1322 }
1323
1324 // Return the name of the symbol that spans the given offset in the
1325 // specified section in this object. This is used only for error
1326 // messages and is not particularly efficient.
1327 bool
1328 get_symbol_location_info(unsigned int shndx, off_t offset,
1329 Symbol_location_info* info);
1330
1331 // Look for a kept section corresponding to the given discarded section,
1332 // and return its output address. This is used only for relocations in
1333 // debugging sections.
1334 Address
1335 map_to_kept_section(unsigned int shndx, bool* found) const;
1336
1337 protected:
1338 // Read the symbols.
1339 void
1340 do_read_symbols(Read_symbols_data*);
1341
1342 // Return the number of local symbols.
1343 unsigned int
1344 do_local_symbol_count() const
1345 { return this->local_symbol_count_; }
1346
1347 // Lay out the input sections.
1348 void
1349 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1350
1351 // Add the symbols to the symbol table.
1352 void
1353 do_add_symbols(Symbol_table*, Read_symbols_data*);
1354
1355 // Read the relocs.
1356 void
1357 do_read_relocs(Read_relocs_data*);
1358
1359 // Scan the relocs and adjust the symbol table.
1360 void
1361 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1362 Read_relocs_data*);
1363
1364 // Count the local symbols.
1365 void
1366 do_count_local_symbols(Stringpool_template<char>*,
1367 Stringpool_template<char>*);
1368
1369 // Finalize the local symbols.
1370 unsigned int
1371 do_finalize_local_symbols(unsigned int, off_t);
1372
1373 // Set the offset where local dynamic symbol information will be stored.
1374 unsigned int
1375 do_set_local_dynsym_indexes(unsigned int);
1376
1377 // Set the offset where local dynamic symbol information will be stored.
1378 unsigned int
1379 do_set_local_dynsym_offset(off_t);
1380
1381 // Relocate the input sections and write out the local symbols.
1382 void
1383 do_relocate(const General_options& options, const Symbol_table* symtab,
1384 const Layout*, Output_file* of);
1385
1386 // Get the size of a section.
1387 uint64_t
1388 do_section_size(unsigned int shndx)
1389 { return this->elf_file_.section_size(shndx); }
1390
1391 // Get the name of a section.
1392 std::string
1393 do_section_name(unsigned int shndx)
1394 { return this->elf_file_.section_name(shndx); }
1395
1396 // Return the location of the contents of a section.
1397 Object::Location
1398 do_section_contents(unsigned int shndx)
1399 { return this->elf_file_.section_contents(shndx); }
1400
1401 // Return section flags.
1402 uint64_t
1403 do_section_flags(unsigned int shndx)
1404 { return this->elf_file_.section_flags(shndx); }
1405
1406 // Return section address.
1407 uint64_t
1408 do_section_address(unsigned int shndx)
1409 { return this->elf_file_.section_addr(shndx); }
1410
1411 // Return section type.
1412 unsigned int
1413 do_section_type(unsigned int shndx)
1414 { return this->elf_file_.section_type(shndx); }
1415
1416 // Return the section link field.
1417 unsigned int
1418 do_section_link(unsigned int shndx)
1419 { return this->elf_file_.section_link(shndx); }
1420
1421 // Return the section info field.
1422 unsigned int
1423 do_section_info(unsigned int shndx)
1424 { return this->elf_file_.section_info(shndx); }
1425
1426 // Return the section alignment.
1427 uint64_t
1428 do_section_addralign(unsigned int shndx)
1429 { return this->elf_file_.section_addralign(shndx); }
1430
1431 // Return the Xindex structure to use.
1432 Xindex*
1433 do_initialize_xindex();
1434
1435 // Get symbol counts.
1436 void
1437 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1438
1439 // Get the offset of a section.
1440 uint64_t
1441 do_output_section_offset(unsigned int shndx) const
1442 { return this->get_output_section_offset(shndx); }
1443
1444 // Set the offset of a section.
1445 void
1446 do_set_section_offset(unsigned int shndx, uint64_t off)
1447 {
1448 gold_assert(shndx < this->section_offsets_.size());
1449 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1450 }
1451
1452 private:
1453 // For convenience.
1454 typedef Sized_relobj<size, big_endian> This;
1455 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1456 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1457 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1458 typedef elfcpp::Shdr<size, big_endian> Shdr;
1459
1460 // To keep track of discarded comdat sections, we need to map a member
1461 // section index to the object and section index of the corresponding
1462 // kept section.
1463 struct Kept_comdat_section
1464 {
1465 Kept_comdat_section(Sized_relobj<size, big_endian>* object,
1466 unsigned int shndx)
1467 : object_(object), shndx_(shndx)
1468 { }
1469 Sized_relobj<size, big_endian>* object_;
1470 unsigned int shndx_;
1471 };
1472 typedef std::map<unsigned int, Kept_comdat_section*>
1473 Kept_comdat_section_table;
1474
1475 // Information needed to keep track of kept comdat groups. This is
1476 // simply a map from the section name to its section index. This may
1477 // not be a one-to-one mapping, but we ignore that possibility since
1478 // this is used only to attempt to handle stray relocations from
1479 // non-comdat debug sections that refer to comdat loadable sections.
1480 typedef Unordered_map<std::string, unsigned int> Comdat_group;
1481
1482 // A map from group section index to the table of group members.
1483 typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
1484
1485 // Find a comdat group table given its group section SHNDX.
1486 Comdat_group*
1487 find_comdat_group(unsigned int shndx) const
1488 {
1489 Comdat_group_table::const_iterator p =
1490 this->comdat_groups_.find(shndx);
1491 if (p != this->comdat_groups_.end())
1492 return p->second;
1493 return NULL;
1494 }
1495
1496 // Record a new comdat group whose group section index is SHNDX.
1497 void
1498 add_comdat_group(unsigned int shndx, Comdat_group* group)
1499 { this->comdat_groups_[shndx] = group; }
1500
1501 // Adjust a section index if necessary.
1502 unsigned int
1503 adjust_shndx(unsigned int shndx)
1504 {
1505 if (shndx >= elfcpp::SHN_LORESERVE)
1506 shndx += this->elf_file_.large_shndx_offset();
1507 return shndx;
1508 }
1509
1510 // Find the SHT_SYMTAB section, given the section headers.
1511 void
1512 find_symtab(const unsigned char* pshdrs);
1513
1514 // Return whether SHDR has the right flags for a GNU style exception
1515 // frame section.
1516 bool
1517 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1518
1519 // Return whether there is a section named .eh_frame which might be
1520 // a GNU style exception frame section.
1521 bool
1522 find_eh_frame(const unsigned char* pshdrs, const char* names,
1523 section_size_type names_size) const;
1524
1525 // Whether to include a section group in the link.
1526 bool
1527 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1528 const unsigned char*, const char *, section_size_type,
1529 std::vector<bool>*);
1530
1531 // Whether to include a linkonce section in the link.
1532 bool
1533 include_linkonce_section(Layout*, unsigned int, const char*,
1534 const elfcpp::Shdr<size, big_endian>&);
1535
1536 // Views and sizes when relocating.
1537 struct View_size
1538 {
1539 unsigned char* view;
1540 typename elfcpp::Elf_types<size>::Elf_Addr address;
1541 off_t offset;
1542 section_size_type view_size;
1543 bool is_input_output_view;
1544 bool is_postprocessing_view;
1545 };
1546
1547 typedef std::vector<View_size> Views;
1548
1549 // Write section data to the output file. Record the views and
1550 // sizes in VIEWS for use when relocating.
1551 void
1552 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1553
1554 // Relocate the sections in the output file.
1555 void
1556 relocate_sections(const General_options& options, const Symbol_table*,
1557 const Layout*, const unsigned char* pshdrs, Views*);
1558
1559 // Scan the input relocations for --emit-relocs.
1560 void
1561 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1562 const unsigned char* plocal_syms,
1563 const Read_relocs_data::Relocs_list::iterator&);
1564
1565 // Scan the input relocations for --emit-relocs, templatized on the
1566 // type of the relocation section.
1567 template<int sh_type>
1568 void
1569 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1570 const unsigned char* plocal_syms,
1571 const Read_relocs_data::Relocs_list::iterator&,
1572 Relocatable_relocs*);
1573
1574 // Emit the relocs for --emit-relocs.
1575 void
1576 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1577 unsigned int sh_type, const unsigned char* prelocs,
1578 size_t reloc_count, Output_section*, Address output_offset,
1579 unsigned char* view, Address address,
1580 section_size_type view_size,
1581 unsigned char* reloc_view, section_size_type reloc_view_size);
1582
1583 // Emit the relocs for --emit-relocs, templatized on the type of the
1584 // relocation section.
1585 template<int sh_type>
1586 void
1587 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1588 const unsigned char* prelocs, size_t reloc_count,
1589 Output_section*, Address output_offset,
1590 unsigned char* view, Address address,
1591 section_size_type view_size,
1592 unsigned char* reloc_view,
1593 section_size_type reloc_view_size);
1594
1595 // Initialize input to output maps for section symbols in merged
1596 // sections.
1597 void
1598 initialize_input_to_output_maps();
1599
1600 // Free the input to output maps for section symbols in merged
1601 // sections.
1602 void
1603 free_input_to_output_maps();
1604
1605 // Write out the local symbols.
1606 void
1607 write_local_symbols(Output_file*,
1608 const Stringpool_template<char>*,
1609 const Stringpool_template<char>*,
1610 Output_symtab_xindex*,
1611 Output_symtab_xindex*);
1612
1613 // Clear the local symbol information.
1614 void
1615 clear_local_symbols()
1616 {
1617 this->local_values_.clear();
1618 this->local_got_offsets_.clear();
1619 }
1620
1621 // Record a mapping from discarded section SHNDX to the corresponding
1622 // kept section.
1623 void
1624 set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
1625 {
1626 this->kept_comdat_sections_[shndx] = kept;
1627 }
1628
1629 // Find the kept section corresponding to the discarded section SHNDX.
1630 Kept_comdat_section*
1631 get_kept_comdat_section(unsigned int shndx) const
1632 {
1633 typename Kept_comdat_section_table::const_iterator p =
1634 this->kept_comdat_sections_.find(shndx);
1635 if (p == this->kept_comdat_sections_.end())
1636 return NULL;
1637 return p->second;
1638 }
1639
1640 // The GOT offsets of local symbols. This map also stores GOT offsets
1641 // for tp-relative offsets for TLS symbols.
1642 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1643
1644 // The TLS GOT offsets of local symbols. The map stores the offsets
1645 // for either a single GOT entry that holds the module index of a TLS
1646 // symbol, or a pair of GOT entries containing the module index and
1647 // dtv-relative offset.
1648 struct Tls_got_entry
1649 {
1650 Tls_got_entry(int got_offset, bool have_pair)
1651 : got_offset_(got_offset),
1652 have_pair_(have_pair)
1653 { }
1654 int got_offset_;
1655 bool have_pair_;
1656 };
1657 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1658
1659 // General access to the ELF file.
1660 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1661 // Index of SHT_SYMTAB section.
1662 unsigned int symtab_shndx_;
1663 // The number of local symbols.
1664 unsigned int local_symbol_count_;
1665 // The number of local symbols which go into the output file.
1666 unsigned int output_local_symbol_count_;
1667 // The number of local symbols which go into the output file's dynamic
1668 // symbol table.
1669 unsigned int output_local_dynsym_count_;
1670 // The entries in the symbol table for the external symbols.
1671 Symbols symbols_;
1672 // Number of symbols defined in object file itself.
1673 size_t defined_count_;
1674 // File offset for local symbols.
1675 off_t local_symbol_offset_;
1676 // File offset for local dynamic symbols.
1677 off_t local_dynsym_offset_;
1678 // Values of local symbols.
1679 Local_values local_values_;
1680 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1681 // for TLS symbols, indexed by symbol number.
1682 Local_got_offsets local_got_offsets_;
1683 // For each input section, the offset of the input section in its
1684 // output section. This is -1U if the input section requires a
1685 // special mapping.
1686 std::vector<Address> section_offsets_;
1687 // Table mapping discarded comdat sections to corresponding kept sections.
1688 Kept_comdat_section_table kept_comdat_sections_;
1689 // Table of kept comdat groups.
1690 Comdat_group_table comdat_groups_;
1691 // Whether this object has a GNU style .eh_frame section.
1692 bool has_eh_frame_;
1693 };
1694
1695 // A class to manage the list of all objects.
1696
1697 class Input_objects
1698 {
1699 public:
1700 Input_objects()
1701 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_(),
1702 cref_(NULL)
1703 { }
1704
1705 // The type of the list of input relocateable objects.
1706 typedef std::vector<Relobj*> Relobj_list;
1707 typedef Relobj_list::const_iterator Relobj_iterator;
1708
1709 // The type of the list of input dynamic objects.
1710 typedef std::vector<Dynobj*> Dynobj_list;
1711 typedef Dynobj_list::const_iterator Dynobj_iterator;
1712
1713 // Add an object to the list. Return true if all is well, or false
1714 // if this object should be ignored.
1715 bool
1716 add_object(Object*);
1717
1718 // Start processing an archive.
1719 void
1720 archive_start(Archive*);
1721
1722 // Stop processing an archive.
1723 void
1724 archive_stop(Archive*);
1725
1726 // For each dynamic object, check whether we've seen all of its
1727 // explicit dependencies.
1728 void
1729 check_dynamic_dependencies() const;
1730
1731 // Return whether an object was found in the system library
1732 // directory.
1733 bool
1734 found_in_system_library_directory(const Object*) const;
1735
1736 // Print symbol counts.
1737 void
1738 print_symbol_counts(const Symbol_table*) const;
1739
1740 // Iterate over all regular objects.
1741
1742 Relobj_iterator
1743 relobj_begin() const
1744 { return this->relobj_list_.begin(); }
1745
1746 Relobj_iterator
1747 relobj_end() const
1748 { return this->relobj_list_.end(); }
1749
1750 // Iterate over all dynamic objects.
1751
1752 Dynobj_iterator
1753 dynobj_begin() const
1754 { return this->dynobj_list_.begin(); }
1755
1756 Dynobj_iterator
1757 dynobj_end() const
1758 { return this->dynobj_list_.end(); }
1759
1760 // Return whether we have seen any dynamic objects.
1761 bool
1762 any_dynamic() const
1763 { return !this->dynobj_list_.empty(); }
1764
1765 // Return the number of input objects.
1766 int
1767 number_of_input_objects() const
1768 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1769
1770 private:
1771 Input_objects(const Input_objects&);
1772 Input_objects& operator=(const Input_objects&);
1773
1774 // The list of ordinary objects included in the link.
1775 Relobj_list relobj_list_;
1776 // The list of dynamic objects included in the link.
1777 Dynobj_list dynobj_list_;
1778 // SONAMEs that we have seen.
1779 Unordered_set<std::string> sonames_;
1780 // The directory in which we find the libc.so.
1781 std::string system_library_directory_;
1782 // Manage cross-references if requested.
1783 Cref* cref_;
1784 };
1785
1786 // Some of the information we pass to the relocation routines. We
1787 // group this together to avoid passing a dozen different arguments.
1788
1789 template<int size, bool big_endian>
1790 struct Relocate_info
1791 {
1792 // Command line options.
1793 const General_options* options;
1794 // Symbol table.
1795 const Symbol_table* symtab;
1796 // Layout.
1797 const Layout* layout;
1798 // Object being relocated.
1799 Sized_relobj<size, big_endian>* object;
1800 // Section index of relocation section.
1801 unsigned int reloc_shndx;
1802 // Section index of section being relocated.
1803 unsigned int data_shndx;
1804
1805 // Return a string showing the location of a relocation. This is
1806 // only used for error messages.
1807 std::string
1808 location(size_t relnum, off_t reloffset) const;
1809 };
1810
1811 // Return an Object appropriate for the input file. P is BYTES long,
1812 // and holds the ELF header.
1813
1814 extern Object*
1815 make_elf_object(const std::string& name, Input_file*,
1816 off_t offset, const unsigned char* p,
1817 section_offset_type bytes);
1818
1819 } // end namespace gold
1820
1821 #endif // !defined(GOLD_OBJECT_H)
This page took 0.064333 seconds and 5 git commands to generate.