* cref.cc: New file.
[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) const
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 Value input_offset = this->input_value_ + addend;
826 typename Output_addresses::const_iterator p =
827 this->output_addresses_.find(input_offset);
828 if (p != this->output_addresses_.end())
829 return p->second;
830
831 return this->value_from_output_section(object, input_shndx, input_offset);
832 }
833
834 private:
835 // Get the output value for an input offset if we couldn't find it
836 // in the hash table.
837 Value
838 value_from_output_section(const Relobj*, unsigned int input_shndx,
839 Value input_offset) const;
840
841 // The value of the section symbol in the input file. This is
842 // normally zero, but could in principle be something else.
843 Value input_value_;
844 // The start address of this merged section in the output file.
845 Value output_start_address_;
846 // A hash table which maps offsets in the input section to output
847 // addresses. This only maps specific offsets, not all offsets.
848 Output_addresses output_addresses_;
849 };
850
851 // This POD class is holds the value of a symbol. This is used for
852 // local symbols, and for all symbols during relocation processing.
853 // For special sections, such as SHF_MERGE sections, this calls a
854 // function to get the final symbol value.
855
856 template<int size>
857 class Symbol_value
858 {
859 public:
860 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
861
862 Symbol_value()
863 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
864 is_ordinary_shndx_(false), is_section_symbol_(false),
865 is_tls_symbol_(false), has_output_value_(true)
866 { this->u_.value = 0; }
867
868 // Get the value of this symbol. OBJECT is the object in which this
869 // symbol is defined, and ADDEND is an addend to add to the value.
870 template<bool big_endian>
871 Value
872 value(const Sized_relobj<size, big_endian>* object, Value addend) const
873 {
874 if (this->has_output_value_)
875 return this->u_.value + addend;
876 else
877 {
878 gold_assert(this->is_ordinary_shndx_);
879 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
880 addend);
881 }
882 }
883
884 // Set the value of this symbol in the output symbol table.
885 void
886 set_output_value(Value value)
887 { this->u_.value = value; }
888
889 // For a section symbol in a merged section, we need more
890 // information.
891 void
892 set_merged_symbol_value(Merged_symbol_value<size>* msv)
893 {
894 gold_assert(this->is_section_symbol_);
895 this->has_output_value_ = false;
896 this->u_.merged_symbol_value = msv;
897 }
898
899 // Initialize the input to output map for a section symbol in a
900 // merged section. We also initialize the value of a non-section
901 // symbol in a merged section.
902 void
903 initialize_input_to_output_map(const Relobj* object)
904 {
905 if (!this->has_output_value_)
906 {
907 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
908 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
909 msv->initialize_input_to_output_map(object, this->input_shndx_);
910 }
911 }
912
913 // Free the input to output map for a section symbol in a merged
914 // section.
915 void
916 free_input_to_output_map()
917 {
918 if (!this->has_output_value_)
919 this->u_.merged_symbol_value->free_input_to_output_map();
920 }
921
922 // Set the value of the symbol from the input file. This is only
923 // called by count_local_symbols, to communicate the value to
924 // finalize_local_symbols.
925 void
926 set_input_value(Value value)
927 { this->u_.value = value; }
928
929 // Return the input value. This is only called by
930 // finalize_local_symbols and (in special cases) relocate_section.
931 Value
932 input_value() const
933 { return this->u_.value; }
934
935 // Return whether this symbol should go into the output symbol
936 // table.
937 bool
938 needs_output_symtab_entry() const
939 { return this->output_symtab_index_ != -1U; }
940
941 // Return the index in the output symbol table.
942 unsigned int
943 output_symtab_index() const
944 {
945 gold_assert(this->output_symtab_index_ != 0);
946 return this->output_symtab_index_;
947 }
948
949 // Set the index in the output symbol table.
950 void
951 set_output_symtab_index(unsigned int i)
952 {
953 gold_assert(this->output_symtab_index_ == 0);
954 this->output_symtab_index_ = i;
955 }
956
957 // Record that this symbol should not go into the output symbol
958 // table.
959 void
960 set_no_output_symtab_entry()
961 {
962 gold_assert(this->output_symtab_index_ == 0);
963 this->output_symtab_index_ = -1U;
964 }
965
966 // Set the index in the output dynamic symbol table.
967 void
968 set_needs_output_dynsym_entry()
969 {
970 gold_assert(!this->is_section_symbol());
971 this->output_dynsym_index_ = 0;
972 }
973
974 // Return whether this symbol should go into the output symbol
975 // table.
976 bool
977 needs_output_dynsym_entry() const
978 {
979 return this->output_dynsym_index_ != -1U;
980 }
981
982 // Record that this symbol should go into the dynamic symbol table.
983 void
984 set_output_dynsym_index(unsigned int i)
985 {
986 gold_assert(this->output_dynsym_index_ == 0);
987 this->output_dynsym_index_ = i;
988 }
989
990 // Return the index in the output dynamic symbol table.
991 unsigned int
992 output_dynsym_index() const
993 {
994 gold_assert(this->output_dynsym_index_ != 0
995 && this->output_dynsym_index_ != -1U);
996 return this->output_dynsym_index_;
997 }
998
999 // Set the index of the input section in the input file.
1000 void
1001 set_input_shndx(unsigned int i, bool is_ordinary)
1002 {
1003 this->input_shndx_ = i;
1004 // input_shndx_ field is a bitfield, so make sure that the value
1005 // fits.
1006 gold_assert(this->input_shndx_ == i);
1007 this->is_ordinary_shndx_ = is_ordinary;
1008 }
1009
1010 // Return the index of the input section in the input file.
1011 unsigned int
1012 input_shndx(bool* is_ordinary) const
1013 {
1014 *is_ordinary = this->is_ordinary_shndx_;
1015 return this->input_shndx_;
1016 }
1017
1018 // Whether this is a section symbol.
1019 bool
1020 is_section_symbol() const
1021 { return this->is_section_symbol_; }
1022
1023 // Record that this is a section symbol.
1024 void
1025 set_is_section_symbol()
1026 {
1027 gold_assert(!this->needs_output_dynsym_entry());
1028 this->is_section_symbol_ = true;
1029 }
1030
1031 // Record that this is a TLS symbol.
1032 void
1033 set_is_tls_symbol()
1034 { this->is_tls_symbol_ = true; }
1035
1036 // Return TRUE if this is a TLS symbol.
1037 bool
1038 is_tls_symbol() const
1039 { return this->is_tls_symbol_; }
1040
1041 private:
1042 // The index of this local symbol in the output symbol table. This
1043 // will be -1 if the symbol should not go into the symbol table.
1044 unsigned int output_symtab_index_;
1045 // The index of this local symbol in the dynamic symbol table. This
1046 // will be -1 if the symbol should not go into the symbol table.
1047 unsigned int output_dynsym_index_;
1048 // The section index in the input file in which this symbol is
1049 // defined.
1050 unsigned int input_shndx_ : 28;
1051 // Whether the section index is an ordinary index, not a special
1052 // value.
1053 bool is_ordinary_shndx_ : 1;
1054 // Whether this is a STT_SECTION symbol.
1055 bool is_section_symbol_ : 1;
1056 // Whether this is a STT_TLS symbol.
1057 bool is_tls_symbol_ : 1;
1058 // Whether this symbol has a value for the output file. This is
1059 // normally set to true during Layout::finalize, by
1060 // finalize_local_symbols. It will be false for a section symbol in
1061 // a merge section, as for such symbols we can not determine the
1062 // value to use in a relocation until we see the addend.
1063 bool has_output_value_ : 1;
1064 union
1065 {
1066 // This is used if has_output_value_ is true. Between
1067 // count_local_symbols and finalize_local_symbols, this is the
1068 // value in the input file. After finalize_local_symbols, it is
1069 // the value in the output file.
1070 Value value;
1071 // This is used if has_output_value_ is false. It points to the
1072 // information we need to get the value for a merge section.
1073 Merged_symbol_value<size>* merged_symbol_value;
1074 } u_;
1075 };
1076
1077 // A GOT offset list. A symbol may have more than one GOT offset
1078 // (e.g., when mixing modules compiled with two different TLS models),
1079 // but will usually have at most one. GOT_TYPE identifies the type of
1080 // GOT entry; its values are specific to each target.
1081
1082 class Got_offset_list
1083 {
1084 public:
1085 Got_offset_list()
1086 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1087 { }
1088
1089 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1090 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1091 { }
1092
1093 ~Got_offset_list()
1094 {
1095 if (this->got_next_ != NULL)
1096 {
1097 delete this->got_next_;
1098 this->got_next_ = NULL;
1099 }
1100 }
1101
1102 // Initialize the fields to their default values.
1103 void
1104 init()
1105 {
1106 this->got_type_ = -1U;
1107 this->got_offset_ = 0;
1108 this->got_next_ = NULL;
1109 }
1110
1111 // Set the offset for the GOT entry of type GOT_TYPE.
1112 void
1113 set_offset(unsigned int got_type, unsigned int got_offset)
1114 {
1115 if (this->got_type_ == -1U)
1116 {
1117 this->got_type_ = got_type;
1118 this->got_offset_ = got_offset;
1119 }
1120 else
1121 {
1122 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1123 {
1124 if (g->got_type_ == got_type)
1125 {
1126 g->got_offset_ = got_offset;
1127 return;
1128 }
1129 }
1130 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1131 g->got_next_ = this->got_next_;
1132 this->got_next_ = g;
1133 }
1134 }
1135
1136 // Return the offset for a GOT entry of type GOT_TYPE.
1137 unsigned int
1138 get_offset(unsigned int got_type) const
1139 {
1140 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1141 {
1142 if (g->got_type_ == got_type)
1143 return g->got_offset_;
1144 }
1145 return -1U;
1146 }
1147
1148 private:
1149 unsigned int got_type_;
1150 unsigned int got_offset_;
1151 Got_offset_list* got_next_;
1152 };
1153
1154 // A regular object file. This is size and endian specific.
1155
1156 template<int size, bool big_endian>
1157 class Sized_relobj : public Relobj
1158 {
1159 public:
1160 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1161 typedef std::vector<Symbol*> Symbols;
1162 typedef std::vector<Symbol_value<size> > Local_values;
1163
1164 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1165 const typename elfcpp::Ehdr<size, big_endian>&);
1166
1167 ~Sized_relobj();
1168
1169 // Set up the object file based on the ELF header.
1170 void
1171 setup(const typename elfcpp::Ehdr<size, big_endian>&);
1172
1173 // Return the number of symbols. This is only valid after
1174 // Object::add_symbols has been called.
1175 unsigned int
1176 symbol_count() const
1177 { return this->local_symbol_count_ + this->symbols_.size(); }
1178
1179 // If SYM is the index of a global symbol in the object file's
1180 // symbol table, return the Symbol object. Otherwise, return NULL.
1181 Symbol*
1182 global_symbol(unsigned int sym) const
1183 {
1184 if (sym >= this->local_symbol_count_)
1185 {
1186 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1187 return this->symbols_[sym - this->local_symbol_count_];
1188 }
1189 return NULL;
1190 }
1191
1192 // Return the section index of symbol SYM. Set *VALUE to its value
1193 // in the object file. Set *IS_ORDINARY if this is an ordinary
1194 // section index, not a special code between SHN_LORESERVE and
1195 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1196 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1197 // it will not return the final value of the symbol in the link.
1198 unsigned int
1199 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1200
1201 // Return a pointer to the Symbol_value structure which holds the
1202 // value of a local symbol.
1203 const Symbol_value<size>*
1204 local_symbol(unsigned int sym) const
1205 {
1206 gold_assert(sym < this->local_values_.size());
1207 return &this->local_values_[sym];
1208 }
1209
1210 // Return the index of local symbol SYM in the ordinary symbol
1211 // table. A value of -1U means that the symbol is not being output.
1212 unsigned int
1213 symtab_index(unsigned int sym) const
1214 {
1215 gold_assert(sym < this->local_values_.size());
1216 return this->local_values_[sym].output_symtab_index();
1217 }
1218
1219 // Return the index of local symbol SYM in the dynamic symbol
1220 // table. A value of -1U means that the symbol is not being output.
1221 unsigned int
1222 dynsym_index(unsigned int sym) const
1223 {
1224 gold_assert(sym < this->local_values_.size());
1225 return this->local_values_[sym].output_dynsym_index();
1226 }
1227
1228 // Return the input section index of local symbol SYM.
1229 unsigned int
1230 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1231 {
1232 gold_assert(sym < this->local_values_.size());
1233 return this->local_values_[sym].input_shndx(is_ordinary);
1234 }
1235
1236 // Return the appropriate Sized_target structure.
1237 Sized_target<size, big_endian>*
1238 sized_target()
1239 { return this->Object::sized_target<size, big_endian>(); }
1240
1241 // Record that local symbol SYM needs a dynamic symbol entry.
1242 void
1243 set_needs_output_dynsym_entry(unsigned int sym)
1244 {
1245 gold_assert(sym < this->local_values_.size());
1246 this->local_values_[sym].set_needs_output_dynsym_entry();
1247 }
1248
1249 // Return whether the local symbol SYMNDX has a GOT offset.
1250 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1251 bool
1252 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1253 {
1254 Local_got_offsets::const_iterator p =
1255 this->local_got_offsets_.find(symndx);
1256 return (p != this->local_got_offsets_.end()
1257 && p->second->get_offset(got_type) != -1U);
1258 }
1259
1260 // Return the GOT offset of the local symbol SYMNDX.
1261 unsigned int
1262 local_got_offset(unsigned int symndx, unsigned int got_type) const
1263 {
1264 Local_got_offsets::const_iterator p =
1265 this->local_got_offsets_.find(symndx);
1266 gold_assert(p != this->local_got_offsets_.end());
1267 unsigned int off = p->second->get_offset(got_type);
1268 gold_assert(off != -1U);
1269 return off;
1270 }
1271
1272 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1273 void
1274 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1275 unsigned int got_offset)
1276 {
1277 Local_got_offsets::const_iterator p =
1278 this->local_got_offsets_.find(symndx);
1279 if (p != this->local_got_offsets_.end())
1280 p->second->set_offset(got_type, got_offset);
1281 else
1282 {
1283 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1284 std::pair<Local_got_offsets::iterator, bool> ins =
1285 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1286 gold_assert(ins.second);
1287 }
1288 }
1289
1290 // Get the offset of input section SHNDX within its output section.
1291 // This is -1 if the input section requires a special mapping, such
1292 // as a merge section. The output section can be found in the
1293 // output_sections_ field of the parent class Relobj.
1294 Address
1295 get_output_section_offset(unsigned int shndx) const
1296 {
1297 gold_assert(shndx < this->section_offsets_.size());
1298 return this->section_offsets_[shndx];
1299 }
1300
1301 // Return the name of the symbol that spans the given offset in the
1302 // specified section in this object. This is used only for error
1303 // messages and is not particularly efficient.
1304 bool
1305 get_symbol_location_info(unsigned int shndx, off_t offset,
1306 Symbol_location_info* info);
1307
1308 // Look for a kept section corresponding to the given discarded section,
1309 // and return its output address. This is used only for relocations in
1310 // debugging sections.
1311 Address
1312 map_to_kept_section(unsigned int shndx, bool* found) const;
1313
1314 protected:
1315 // Read the symbols.
1316 void
1317 do_read_symbols(Read_symbols_data*);
1318
1319 // Return the number of local symbols.
1320 unsigned int
1321 do_local_symbol_count() const
1322 { return this->local_symbol_count_; }
1323
1324 // Lay out the input sections.
1325 void
1326 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1327
1328 // Add the symbols to the symbol table.
1329 void
1330 do_add_symbols(Symbol_table*, Read_symbols_data*);
1331
1332 // Read the relocs.
1333 void
1334 do_read_relocs(Read_relocs_data*);
1335
1336 // Scan the relocs and adjust the symbol table.
1337 void
1338 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1339 Read_relocs_data*);
1340
1341 // Count the local symbols.
1342 void
1343 do_count_local_symbols(Stringpool_template<char>*,
1344 Stringpool_template<char>*);
1345
1346 // Finalize the local symbols.
1347 unsigned int
1348 do_finalize_local_symbols(unsigned int, off_t);
1349
1350 // Set the offset where local dynamic symbol information will be stored.
1351 unsigned int
1352 do_set_local_dynsym_indexes(unsigned int);
1353
1354 // Set the offset where local dynamic symbol information will be stored.
1355 unsigned int
1356 do_set_local_dynsym_offset(off_t);
1357
1358 // Relocate the input sections and write out the local symbols.
1359 void
1360 do_relocate(const General_options& options, const Symbol_table* symtab,
1361 const Layout*, Output_file* of);
1362
1363 // Get the size of a section.
1364 uint64_t
1365 do_section_size(unsigned int shndx)
1366 { return this->elf_file_.section_size(shndx); }
1367
1368 // Get the name of a section.
1369 std::string
1370 do_section_name(unsigned int shndx)
1371 { return this->elf_file_.section_name(shndx); }
1372
1373 // Return the location of the contents of a section.
1374 Object::Location
1375 do_section_contents(unsigned int shndx)
1376 { return this->elf_file_.section_contents(shndx); }
1377
1378 // Return section flags.
1379 uint64_t
1380 do_section_flags(unsigned int shndx)
1381 { return this->elf_file_.section_flags(shndx); }
1382
1383 // Return section address.
1384 uint64_t
1385 do_section_address(unsigned int shndx)
1386 { return this->elf_file_.section_addr(shndx); }
1387
1388 // Return section type.
1389 unsigned int
1390 do_section_type(unsigned int shndx)
1391 { return this->elf_file_.section_type(shndx); }
1392
1393 // Return the section link field.
1394 unsigned int
1395 do_section_link(unsigned int shndx)
1396 { return this->elf_file_.section_link(shndx); }
1397
1398 // Return the section info field.
1399 unsigned int
1400 do_section_info(unsigned int shndx)
1401 { return this->elf_file_.section_info(shndx); }
1402
1403 // Return the section alignment.
1404 uint64_t
1405 do_section_addralign(unsigned int shndx)
1406 { return this->elf_file_.section_addralign(shndx); }
1407
1408 // Return the Xindex structure to use.
1409 Xindex*
1410 do_initialize_xindex();
1411
1412 // Get symbol counts.
1413 void
1414 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1415
1416 // Get the offset of a section.
1417 uint64_t
1418 do_output_section_offset(unsigned int shndx) const
1419 { return this->get_output_section_offset(shndx); }
1420
1421 // Set the offset of a section.
1422 void
1423 do_set_section_offset(unsigned int shndx, uint64_t off)
1424 {
1425 gold_assert(shndx < this->section_offsets_.size());
1426 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1427 }
1428
1429 private:
1430 // For convenience.
1431 typedef Sized_relobj<size, big_endian> This;
1432 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1433 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1434 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1435 typedef elfcpp::Shdr<size, big_endian> Shdr;
1436
1437 // To keep track of discarded comdat sections, we need to map a member
1438 // section index to the object and section index of the corresponding
1439 // kept section.
1440 struct Kept_comdat_section
1441 {
1442 Kept_comdat_section(Sized_relobj<size, big_endian>* object,
1443 unsigned int shndx)
1444 : object_(object), shndx_(shndx)
1445 { }
1446 Sized_relobj<size, big_endian>* object_;
1447 unsigned int shndx_;
1448 };
1449 typedef std::map<unsigned int, Kept_comdat_section*>
1450 Kept_comdat_section_table;
1451
1452 // Information needed to keep track of kept comdat groups. This is
1453 // simply a map from the section name to its section index. This may
1454 // not be a one-to-one mapping, but we ignore that possibility since
1455 // this is used only to attempt to handle stray relocations from
1456 // non-comdat debug sections that refer to comdat loadable sections.
1457 typedef Unordered_map<std::string, unsigned int> Comdat_group;
1458
1459 // A map from group section index to the table of group members.
1460 typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
1461
1462 // Find a comdat group table given its group section SHNDX.
1463 Comdat_group*
1464 find_comdat_group(unsigned int shndx) const
1465 {
1466 Comdat_group_table::const_iterator p =
1467 this->comdat_groups_.find(shndx);
1468 if (p != this->comdat_groups_.end())
1469 return p->second;
1470 return NULL;
1471 }
1472
1473 // Record a new comdat group whose group section index is SHNDX.
1474 void
1475 add_comdat_group(unsigned int shndx, Comdat_group* group)
1476 { this->comdat_groups_[shndx] = group; }
1477
1478 // Adjust a section index if necessary.
1479 unsigned int
1480 adjust_shndx(unsigned int shndx)
1481 {
1482 if (shndx >= elfcpp::SHN_LORESERVE)
1483 shndx += this->elf_file_.large_shndx_offset();
1484 return shndx;
1485 }
1486
1487 // Find the SHT_SYMTAB section, given the section headers.
1488 void
1489 find_symtab(const unsigned char* pshdrs);
1490
1491 // Return whether SHDR has the right flags for a GNU style exception
1492 // frame section.
1493 bool
1494 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1495
1496 // Return whether there is a section named .eh_frame which might be
1497 // a GNU style exception frame section.
1498 bool
1499 find_eh_frame(const unsigned char* pshdrs, const char* names,
1500 section_size_type names_size) const;
1501
1502 // Whether to include a section group in the link.
1503 bool
1504 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1505 const unsigned char*, const char *, section_size_type,
1506 std::vector<bool>*);
1507
1508 // Whether to include a linkonce section in the link.
1509 bool
1510 include_linkonce_section(Layout*, unsigned int, const char*,
1511 const elfcpp::Shdr<size, big_endian>&);
1512
1513 // Views and sizes when relocating.
1514 struct View_size
1515 {
1516 unsigned char* view;
1517 typename elfcpp::Elf_types<size>::Elf_Addr address;
1518 off_t offset;
1519 section_size_type view_size;
1520 bool is_input_output_view;
1521 bool is_postprocessing_view;
1522 };
1523
1524 typedef std::vector<View_size> Views;
1525
1526 // Write section data to the output file. Record the views and
1527 // sizes in VIEWS for use when relocating.
1528 void
1529 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1530
1531 // Relocate the sections in the output file.
1532 void
1533 relocate_sections(const General_options& options, const Symbol_table*,
1534 const Layout*, const unsigned char* pshdrs, Views*);
1535
1536 // Scan the input relocations for --emit-relocs.
1537 void
1538 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1539 const unsigned char* plocal_syms,
1540 const Read_relocs_data::Relocs_list::iterator&);
1541
1542 // Scan the input relocations for --emit-relocs, templatized on the
1543 // type of the relocation section.
1544 template<int sh_type>
1545 void
1546 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1547 const unsigned char* plocal_syms,
1548 const Read_relocs_data::Relocs_list::iterator&,
1549 Relocatable_relocs*);
1550
1551 // Emit the relocs for --emit-relocs.
1552 void
1553 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1554 unsigned int sh_type, const unsigned char* prelocs,
1555 size_t reloc_count, Output_section*, Address output_offset,
1556 unsigned char* view, Address address,
1557 section_size_type view_size,
1558 unsigned char* reloc_view, section_size_type reloc_view_size);
1559
1560 // Emit the relocs for --emit-relocs, templatized on the type of the
1561 // relocation section.
1562 template<int sh_type>
1563 void
1564 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1565 const unsigned char* prelocs, size_t reloc_count,
1566 Output_section*, Address output_offset,
1567 unsigned char* view, Address address,
1568 section_size_type view_size,
1569 unsigned char* reloc_view,
1570 section_size_type reloc_view_size);
1571
1572 // Initialize input to output maps for section symbols in merged
1573 // sections.
1574 void
1575 initialize_input_to_output_maps();
1576
1577 // Free the input to output maps for section symbols in merged
1578 // sections.
1579 void
1580 free_input_to_output_maps();
1581
1582 // Write out the local symbols.
1583 void
1584 write_local_symbols(Output_file*,
1585 const Stringpool_template<char>*,
1586 const Stringpool_template<char>*,
1587 Output_symtab_xindex*,
1588 Output_symtab_xindex*);
1589
1590 // Clear the local symbol information.
1591 void
1592 clear_local_symbols()
1593 {
1594 this->local_values_.clear();
1595 this->local_got_offsets_.clear();
1596 }
1597
1598 // Record a mapping from discarded section SHNDX to the corresponding
1599 // kept section.
1600 void
1601 set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
1602 {
1603 this->kept_comdat_sections_[shndx] = kept;
1604 }
1605
1606 // Find the kept section corresponding to the discarded section SHNDX.
1607 Kept_comdat_section*
1608 get_kept_comdat_section(unsigned int shndx) const
1609 {
1610 typename Kept_comdat_section_table::const_iterator p =
1611 this->kept_comdat_sections_.find(shndx);
1612 if (p == this->kept_comdat_sections_.end())
1613 return NULL;
1614 return p->second;
1615 }
1616
1617 // The GOT offsets of local symbols. This map also stores GOT offsets
1618 // for tp-relative offsets for TLS symbols.
1619 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1620
1621 // The TLS GOT offsets of local symbols. The map stores the offsets
1622 // for either a single GOT entry that holds the module index of a TLS
1623 // symbol, or a pair of GOT entries containing the module index and
1624 // dtv-relative offset.
1625 struct Tls_got_entry
1626 {
1627 Tls_got_entry(int got_offset, bool have_pair)
1628 : got_offset_(got_offset),
1629 have_pair_(have_pair)
1630 { }
1631 int got_offset_;
1632 bool have_pair_;
1633 };
1634 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1635
1636 // General access to the ELF file.
1637 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1638 // Index of SHT_SYMTAB section.
1639 unsigned int symtab_shndx_;
1640 // The number of local symbols.
1641 unsigned int local_symbol_count_;
1642 // The number of local symbols which go into the output file.
1643 unsigned int output_local_symbol_count_;
1644 // The number of local symbols which go into the output file's dynamic
1645 // symbol table.
1646 unsigned int output_local_dynsym_count_;
1647 // The entries in the symbol table for the external symbols.
1648 Symbols symbols_;
1649 // Number of symbols defined in object file itself.
1650 size_t defined_count_;
1651 // File offset for local symbols.
1652 off_t local_symbol_offset_;
1653 // File offset for local dynamic symbols.
1654 off_t local_dynsym_offset_;
1655 // Values of local symbols.
1656 Local_values local_values_;
1657 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1658 // for TLS symbols, indexed by symbol number.
1659 Local_got_offsets local_got_offsets_;
1660 // For each input section, the offset of the input section in its
1661 // output section. This is -1U if the input section requires a
1662 // special mapping.
1663 std::vector<Address> section_offsets_;
1664 // Table mapping discarded comdat sections to corresponding kept sections.
1665 Kept_comdat_section_table kept_comdat_sections_;
1666 // Table of kept comdat groups.
1667 Comdat_group_table comdat_groups_;
1668 // Whether this object has a GNU style .eh_frame section.
1669 bool has_eh_frame_;
1670 };
1671
1672 // A class to manage the list of all objects.
1673
1674 class Input_objects
1675 {
1676 public:
1677 Input_objects()
1678 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_(),
1679 cref_(NULL)
1680 { }
1681
1682 // The type of the list of input relocateable objects.
1683 typedef std::vector<Relobj*> Relobj_list;
1684 typedef Relobj_list::const_iterator Relobj_iterator;
1685
1686 // The type of the list of input dynamic objects.
1687 typedef std::vector<Dynobj*> Dynobj_list;
1688 typedef Dynobj_list::const_iterator Dynobj_iterator;
1689
1690 // Add an object to the list. Return true if all is well, or false
1691 // if this object should be ignored.
1692 bool
1693 add_object(Object*);
1694
1695 // Start processing an archive.
1696 void
1697 archive_start(Archive*);
1698
1699 // Stop processing an archive.
1700 void
1701 archive_stop(Archive*);
1702
1703 // For each dynamic object, check whether we've seen all of its
1704 // explicit dependencies.
1705 void
1706 check_dynamic_dependencies() const;
1707
1708 // Return whether an object was found in the system library
1709 // directory.
1710 bool
1711 found_in_system_library_directory(const Object*) const;
1712
1713 // Print symbol counts.
1714 void
1715 print_symbol_counts(const Symbol_table*) const;
1716
1717 // Iterate over all regular objects.
1718
1719 Relobj_iterator
1720 relobj_begin() const
1721 { return this->relobj_list_.begin(); }
1722
1723 Relobj_iterator
1724 relobj_end() const
1725 { return this->relobj_list_.end(); }
1726
1727 // Iterate over all dynamic objects.
1728
1729 Dynobj_iterator
1730 dynobj_begin() const
1731 { return this->dynobj_list_.begin(); }
1732
1733 Dynobj_iterator
1734 dynobj_end() const
1735 { return this->dynobj_list_.end(); }
1736
1737 // Return whether we have seen any dynamic objects.
1738 bool
1739 any_dynamic() const
1740 { return !this->dynobj_list_.empty(); }
1741
1742 // Return the number of input objects.
1743 int
1744 number_of_input_objects() const
1745 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1746
1747 private:
1748 Input_objects(const Input_objects&);
1749 Input_objects& operator=(const Input_objects&);
1750
1751 // The list of ordinary objects included in the link.
1752 Relobj_list relobj_list_;
1753 // The list of dynamic objects included in the link.
1754 Dynobj_list dynobj_list_;
1755 // SONAMEs that we have seen.
1756 Unordered_set<std::string> sonames_;
1757 // The directory in which we find the libc.so.
1758 std::string system_library_directory_;
1759 // Manage cross-references if requested.
1760 Cref* cref_;
1761 };
1762
1763 // Some of the information we pass to the relocation routines. We
1764 // group this together to avoid passing a dozen different arguments.
1765
1766 template<int size, bool big_endian>
1767 struct Relocate_info
1768 {
1769 // Command line options.
1770 const General_options* options;
1771 // Symbol table.
1772 const Symbol_table* symtab;
1773 // Layout.
1774 const Layout* layout;
1775 // Object being relocated.
1776 Sized_relobj<size, big_endian>* object;
1777 // Section index of relocation section.
1778 unsigned int reloc_shndx;
1779 // Section index of section being relocated.
1780 unsigned int data_shndx;
1781
1782 // Return a string showing the location of a relocation. This is
1783 // only used for error messages.
1784 std::string
1785 location(size_t relnum, off_t reloffset) const;
1786 };
1787
1788 // Return an Object appropriate for the input file. P is BYTES long,
1789 // and holds the ELF header.
1790
1791 extern Object*
1792 make_elf_object(const std::string& name, Input_file*,
1793 off_t offset, const unsigned char* p,
1794 section_offset_type bytes);
1795
1796 } // end namespace gold
1797
1798 #endif // !defined(GOLD_OBJECT_H)
This page took 0.066606 seconds and 5 git commands to generate.