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