* mapfile.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 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 map_to_output_(),
564 comdat_groups_(),
565 kept_comdat_sections_(),
566 map_to_relocatable_relocs_(NULL),
567 object_merge_map_(NULL),
568 relocs_must_follow_section_writes_(false)
569 { }
570
571 // Read the relocs.
572 void
573 read_relocs(Read_relocs_data* rd)
574 { return this->do_read_relocs(rd); }
575
576 // Scan the relocs and adjust the symbol table.
577 void
578 scan_relocs(const General_options& options, Symbol_table* symtab,
579 Layout* layout, Read_relocs_data* rd)
580 { return this->do_scan_relocs(options, symtab, layout, rd); }
581
582 // The number of local symbols in the input symbol table.
583 virtual unsigned int
584 local_symbol_count() const
585 { return this->do_local_symbol_count(); }
586
587 // Initial local symbol processing: count the number of local symbols
588 // in the output symbol table and dynamic symbol table; add local symbol
589 // names to *POOL and *DYNPOOL.
590 void
591 count_local_symbols(Stringpool_template<char>* pool,
592 Stringpool_template<char>* dynpool)
593 { return this->do_count_local_symbols(pool, dynpool); }
594
595 // Set the values of the local symbols, set the output symbol table
596 // indexes for the local variables, and set the offset where local
597 // symbol information will be stored. Returns the new local symbol index.
598 unsigned int
599 finalize_local_symbols(unsigned int index, off_t off)
600 { return this->do_finalize_local_symbols(index, off); }
601
602 // Set the output dynamic symbol table indexes for the local variables.
603 unsigned int
604 set_local_dynsym_indexes(unsigned int index)
605 { return this->do_set_local_dynsym_indexes(index); }
606
607 // Set the offset where local dynamic symbol information will be stored.
608 unsigned int
609 set_local_dynsym_offset(off_t off)
610 { return this->do_set_local_dynsym_offset(off); }
611
612 // Relocate the input sections and write out the local symbols.
613 void
614 relocate(const General_options& options, const Symbol_table* symtab,
615 const Layout* layout, Output_file* of)
616 { return this->do_relocate(options, symtab, layout, of); }
617
618 // Return whether an input section is being included in the link.
619 bool
620 is_section_included(unsigned int shndx) const
621 {
622 gold_assert(shndx < this->map_to_output_.size());
623 return this->map_to_output_[shndx].output_section != NULL;
624 }
625
626 // Return whether an input section requires special
627 // handling--whether it is not simply mapped from the input file to
628 // the output file.
629 bool
630 is_section_specially_mapped(unsigned int shndx) const
631 {
632 gold_assert(shndx < this->map_to_output_.size());
633 return (this->map_to_output_[shndx].output_section != NULL
634 && this->map_to_output_[shndx].offset == -1);
635 }
636
637 // Given a section index, return the corresponding Output_section
638 // (which will be NULL if the section is not included in the link)
639 // and set *POFF to the offset within that section. *POFF will be
640 // set to -1 if the section requires special handling.
641 inline Output_section*
642 output_section(unsigned int shndx, section_offset_type* poff) const;
643
644 // Set the offset of an input section within its output section.
645 void
646 set_section_offset(unsigned int shndx, section_offset_type off)
647 {
648 gold_assert(shndx < this->map_to_output_.size());
649 this->map_to_output_[shndx].offset = off;
650 }
651
652 // Return the output address of the input section SHNDX.
653 uint64_t
654 output_section_address(unsigned int shndx) const;
655
656 // Return true if we need to wait for output sections to be written
657 // before we can apply relocations. This is true if the object has
658 // any relocations for sections which require special handling, such
659 // as the exception frame section.
660 bool
661 relocs_must_follow_section_writes() const
662 { return this->relocs_must_follow_section_writes_; }
663
664 // Return the object merge map.
665 Object_merge_map*
666 merge_map() const
667 { return this->object_merge_map_; }
668
669 // Set the object merge map.
670 void
671 set_merge_map(Object_merge_map* object_merge_map)
672 {
673 gold_assert(this->object_merge_map_ == NULL);
674 this->object_merge_map_ = object_merge_map;
675 }
676
677 // Record the relocatable reloc info for an input reloc section.
678 void
679 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
680 {
681 gold_assert(reloc_shndx < this->shnum());
682 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
683 }
684
685 // Get the relocatable reloc info for an input reloc section.
686 Relocatable_relocs*
687 relocatable_relocs(unsigned int reloc_shndx)
688 {
689 gold_assert(reloc_shndx < this->shnum());
690 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
691 }
692
693 // Information needed to keep track of kept comdat groups. This is
694 // simply a map from the section name to its section index. This may
695 // not be a one-to-one mapping, but we ignore that possibility since
696 // this is used only to attempt to handle stray relocations from
697 // non-comdat debug sections that refer to comdat loadable sections.
698 typedef Unordered_map<std::string, unsigned int> Comdat_group;
699
700 // Find a comdat group table given its group section SHNDX.
701 Comdat_group*
702 find_comdat_group(unsigned int shndx) const
703 {
704 Comdat_group_table::const_iterator p =
705 this->comdat_groups_.find(shndx);
706 if (p != this->comdat_groups_.end())
707 return p->second;
708 return NULL;
709 }
710
711 protected:
712 // What we need to know to map an input section to an output
713 // section. We keep an array of these, one for each input section,
714 // indexed by the input section number.
715 struct Map_to_output
716 {
717 // The output section. This is NULL if the input section is to be
718 // discarded.
719 Output_section* output_section;
720 // The offset within the output section. This is -1 if the
721 // section requires special handling.
722 section_offset_type offset;
723 };
724
725 // A map from group section index to the table of group members.
726 typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
727
728 // To keep track of discarded comdat sections, we need to map a member
729 // section index to the object and section index of the corresponding
730 // kept section.
731 struct Kept_comdat_section
732 {
733 Kept_comdat_section(Relobj* object, unsigned int shndx)
734 : object_(object), shndx_(shndx)
735 { }
736 Relobj* object_;
737 unsigned int shndx_;
738 };
739 typedef std::map<unsigned int, Kept_comdat_section*>
740 Kept_comdat_section_table;
741
742 // Read the relocs--implemented by child class.
743 virtual void
744 do_read_relocs(Read_relocs_data*) = 0;
745
746 // Scan the relocs--implemented by child class.
747 virtual void
748 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
749 Read_relocs_data*) = 0;
750
751 // Return the number of local symbols--implemented by child class.
752 virtual unsigned int
753 do_local_symbol_count() const = 0;
754
755 // Count local symbols--implemented by child class.
756 virtual void
757 do_count_local_symbols(Stringpool_template<char>*,
758 Stringpool_template<char>*) = 0;
759
760 // Finalize the local symbols. Set the output symbol table indexes
761 // for the local variables, and set the offset where local symbol
762 // information will be stored.
763 virtual unsigned int
764 do_finalize_local_symbols(unsigned int, off_t) = 0;
765
766 // Set the output dynamic symbol table indexes for the local variables.
767 virtual unsigned int
768 do_set_local_dynsym_indexes(unsigned int) = 0;
769
770 // Set the offset where local dynamic symbol information will be stored.
771 virtual unsigned int
772 do_set_local_dynsym_offset(off_t) = 0;
773
774 // Relocate the input sections and write out the local
775 // symbols--implemented by child class.
776 virtual void
777 do_relocate(const General_options& options, const Symbol_table* symtab,
778 const Layout*, Output_file* of) = 0;
779
780 // Return the vector mapping input sections to output sections.
781 std::vector<Map_to_output>&
782 map_to_output()
783 { return this->map_to_output_; }
784
785 const std::vector<Map_to_output>&
786 map_to_output() const
787 { return this->map_to_output_; }
788
789 // Record a new comdat group whose group section index is SHNDX.
790 void
791 add_comdat_group(unsigned int shndx, Comdat_group* group)
792 { this->comdat_groups_[shndx] = group; }
793
794 // Record a mapping from discarded section SHNDX to the corresponding
795 // kept section.
796 void
797 set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
798 {
799 this->kept_comdat_sections_[shndx] = kept;
800 }
801
802 // Find the kept section corresponding to the discarded section SHNDX.
803 Kept_comdat_section*
804 get_kept_comdat_section(unsigned int shndx) const
805 {
806 Kept_comdat_section_table::const_iterator p =
807 this->kept_comdat_sections_.find(shndx);
808 if (p == this->kept_comdat_sections_.end())
809 return NULL;
810 return p->second;
811 }
812
813 // Set the size of the relocatable relocs array.
814 void
815 size_relocatable_relocs()
816 {
817 this->map_to_relocatable_relocs_ =
818 new std::vector<Relocatable_relocs*>(this->shnum());
819 }
820
821 // Record that we must wait for the output sections to be written
822 // before applying relocations.
823 void
824 set_relocs_must_follow_section_writes()
825 { this->relocs_must_follow_section_writes_ = true; }
826
827 private:
828 // Mapping from input sections to output section.
829 std::vector<Map_to_output> map_to_output_;
830 // Table of kept comdat groups.
831 Comdat_group_table comdat_groups_;
832 // Table mapping discarded comdat sections to corresponding kept sections.
833 Kept_comdat_section_table kept_comdat_sections_;
834 // Mapping from input section index to the information recorded for
835 // the relocations. This is only used for a relocatable link.
836 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
837 // Mappings for merge sections. This is managed by the code in the
838 // Merge_map class.
839 Object_merge_map* object_merge_map_;
840 // Whether we need to wait for output sections to be written before
841 // we can apply relocations.
842 bool relocs_must_follow_section_writes_;
843 };
844
845 // Implement Object::output_section inline for efficiency.
846 inline Output_section*
847 Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
848 {
849 gold_assert(shndx < this->map_to_output_.size());
850 const Map_to_output& mo(this->map_to_output_[shndx]);
851 *poff = mo.offset;
852 return mo.output_section;
853 }
854
855 // This class is used to handle relocations against a section symbol
856 // in an SHF_MERGE section. For such a symbol, we need to know the
857 // addend of the relocation before we can determine the final value.
858 // The addend gives us the location in the input section, and we can
859 // determine how it is mapped to the output section. For a
860 // non-section symbol, we apply the addend to the final value of the
861 // symbol; that is done in finalize_local_symbols, and does not use
862 // this class.
863
864 template<int size>
865 class Merged_symbol_value
866 {
867 public:
868 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
869
870 // We use a hash table to map offsets in the input section to output
871 // addresses.
872 typedef Unordered_map<section_offset_type, Value> Output_addresses;
873
874 Merged_symbol_value(Value input_value, Value output_start_address)
875 : input_value_(input_value), output_start_address_(output_start_address),
876 output_addresses_()
877 { }
878
879 // Initialize the hash table.
880 void
881 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
882
883 // Release the hash table to save space.
884 void
885 free_input_to_output_map()
886 { this->output_addresses_.clear(); }
887
888 // Get the output value corresponding to an addend. The object and
889 // input section index are passed in because the caller will have
890 // them; otherwise we could store them here.
891 Value
892 value(const Relobj* object, unsigned int input_shndx, Value addend) const
893 {
894 Value input_offset = this->input_value_ + addend;
895 typename Output_addresses::const_iterator p =
896 this->output_addresses_.find(input_offset);
897 if (p != this->output_addresses_.end())
898 return p->second;
899
900 return this->value_from_output_section(object, input_shndx, input_offset);
901 }
902
903 private:
904 // Get the output value for an input offset if we couldn't find it
905 // in the hash table.
906 Value
907 value_from_output_section(const Relobj*, unsigned int input_shndx,
908 Value input_offset) const;
909
910 // The value of the section symbol in the input file. This is
911 // normally zero, but could in principle be something else.
912 Value input_value_;
913 // The start address of this merged section in the output file.
914 Value output_start_address_;
915 // A hash table which maps offsets in the input section to output
916 // addresses. This only maps specific offsets, not all offsets.
917 Output_addresses output_addresses_;
918 };
919
920 // This POD class is holds the value of a symbol. This is used for
921 // local symbols, and for all symbols during relocation processing.
922 // For special sections, such as SHF_MERGE sections, this calls a
923 // function to get the final symbol value.
924
925 template<int size>
926 class Symbol_value
927 {
928 public:
929 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
930
931 Symbol_value()
932 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
933 is_ordinary_shndx_(false), is_section_symbol_(false),
934 is_tls_symbol_(false), has_output_value_(true)
935 { this->u_.value = 0; }
936
937 // Get the value of this symbol. OBJECT is the object in which this
938 // symbol is defined, and ADDEND is an addend to add to the value.
939 template<bool big_endian>
940 Value
941 value(const Sized_relobj<size, big_endian>* object, Value addend) const
942 {
943 if (this->has_output_value_)
944 return this->u_.value + addend;
945 else
946 {
947 gold_assert(this->is_ordinary_shndx_);
948 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
949 addend);
950 }
951 }
952
953 // Set the value of this symbol in the output symbol table.
954 void
955 set_output_value(Value value)
956 { this->u_.value = value; }
957
958 // For a section symbol in a merged section, we need more
959 // information.
960 void
961 set_merged_symbol_value(Merged_symbol_value<size>* msv)
962 {
963 gold_assert(this->is_section_symbol_);
964 this->has_output_value_ = false;
965 this->u_.merged_symbol_value = msv;
966 }
967
968 // Initialize the input to output map for a section symbol in a
969 // merged section. We also initialize the value of a non-section
970 // symbol in a merged section.
971 void
972 initialize_input_to_output_map(const Relobj* object)
973 {
974 if (!this->has_output_value_)
975 {
976 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
977 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
978 msv->initialize_input_to_output_map(object, this->input_shndx_);
979 }
980 }
981
982 // Free the input to output map for a section symbol in a merged
983 // section.
984 void
985 free_input_to_output_map()
986 {
987 if (!this->has_output_value_)
988 this->u_.merged_symbol_value->free_input_to_output_map();
989 }
990
991 // Set the value of the symbol from the input file. This is only
992 // called by count_local_symbols, to communicate the value to
993 // finalize_local_symbols.
994 void
995 set_input_value(Value value)
996 { this->u_.value = value; }
997
998 // Return the input value. This is only called by
999 // finalize_local_symbols and (in special cases) relocate_section.
1000 Value
1001 input_value() const
1002 { return this->u_.value; }
1003
1004 // Return whether this symbol should go into the output symbol
1005 // table.
1006 bool
1007 needs_output_symtab_entry() const
1008 { return this->output_symtab_index_ != -1U; }
1009
1010 // Return the index in the output symbol table.
1011 unsigned int
1012 output_symtab_index() const
1013 {
1014 gold_assert(this->output_symtab_index_ != 0);
1015 return this->output_symtab_index_;
1016 }
1017
1018 // Set the index in the output symbol table.
1019 void
1020 set_output_symtab_index(unsigned int i)
1021 {
1022 gold_assert(this->output_symtab_index_ == 0);
1023 this->output_symtab_index_ = i;
1024 }
1025
1026 // Record that this symbol should not go into the output symbol
1027 // table.
1028 void
1029 set_no_output_symtab_entry()
1030 {
1031 gold_assert(this->output_symtab_index_ == 0);
1032 this->output_symtab_index_ = -1U;
1033 }
1034
1035 // Set the index in the output dynamic symbol table.
1036 void
1037 set_needs_output_dynsym_entry()
1038 {
1039 gold_assert(!this->is_section_symbol());
1040 this->output_dynsym_index_ = 0;
1041 }
1042
1043 // Return whether this symbol should go into the output symbol
1044 // table.
1045 bool
1046 needs_output_dynsym_entry() const
1047 {
1048 return this->output_dynsym_index_ != -1U;
1049 }
1050
1051 // Record that this symbol should go into the dynamic symbol table.
1052 void
1053 set_output_dynsym_index(unsigned int i)
1054 {
1055 gold_assert(this->output_dynsym_index_ == 0);
1056 this->output_dynsym_index_ = i;
1057 }
1058
1059 // Return the index in the output dynamic symbol table.
1060 unsigned int
1061 output_dynsym_index() const
1062 {
1063 gold_assert(this->output_dynsym_index_ != 0
1064 && this->output_dynsym_index_ != -1U);
1065 return this->output_dynsym_index_;
1066 }
1067
1068 // Set the index of the input section in the input file.
1069 void
1070 set_input_shndx(unsigned int i, bool is_ordinary)
1071 {
1072 this->input_shndx_ = i;
1073 // input_shndx_ field is a bitfield, so make sure that the value
1074 // fits.
1075 gold_assert(this->input_shndx_ == i);
1076 this->is_ordinary_shndx_ = is_ordinary;
1077 }
1078
1079 // Return the index of the input section in the input file.
1080 unsigned int
1081 input_shndx(bool* is_ordinary) const
1082 {
1083 *is_ordinary = this->is_ordinary_shndx_;
1084 return this->input_shndx_;
1085 }
1086
1087 // Whether this is a section symbol.
1088 bool
1089 is_section_symbol() const
1090 { return this->is_section_symbol_; }
1091
1092 // Record that this is a section symbol.
1093 void
1094 set_is_section_symbol()
1095 {
1096 gold_assert(!this->needs_output_dynsym_entry());
1097 this->is_section_symbol_ = true;
1098 }
1099
1100 // Record that this is a TLS symbol.
1101 void
1102 set_is_tls_symbol()
1103 { this->is_tls_symbol_ = true; }
1104
1105 // Return TRUE if this is a TLS symbol.
1106 bool
1107 is_tls_symbol() const
1108 { return this->is_tls_symbol_; }
1109
1110 private:
1111 // The index of this local symbol in the output symbol table. This
1112 // will be -1 if the symbol should not go into the symbol table.
1113 unsigned int output_symtab_index_;
1114 // The index of this local symbol in the dynamic symbol table. This
1115 // will be -1 if the symbol should not go into the symbol table.
1116 unsigned int output_dynsym_index_;
1117 // The section index in the input file in which this symbol is
1118 // defined.
1119 unsigned int input_shndx_ : 28;
1120 // Whether the section index is an ordinary index, not a special
1121 // value.
1122 bool is_ordinary_shndx_ : 1;
1123 // Whether this is a STT_SECTION symbol.
1124 bool is_section_symbol_ : 1;
1125 // Whether this is a STT_TLS symbol.
1126 bool is_tls_symbol_ : 1;
1127 // Whether this symbol has a value for the output file. This is
1128 // normally set to true during Layout::finalize, by
1129 // finalize_local_symbols. It will be false for a section symbol in
1130 // a merge section, as for such symbols we can not determine the
1131 // value to use in a relocation until we see the addend.
1132 bool has_output_value_ : 1;
1133 union
1134 {
1135 // This is used if has_output_value_ is true. Between
1136 // count_local_symbols and finalize_local_symbols, this is the
1137 // value in the input file. After finalize_local_symbols, it is
1138 // the value in the output file.
1139 Value value;
1140 // This is used if has_output_value_ is false. It points to the
1141 // information we need to get the value for a merge section.
1142 Merged_symbol_value<size>* merged_symbol_value;
1143 } u_;
1144 };
1145
1146 // A GOT offset list. A symbol may have more than one GOT offset
1147 // (e.g., when mixing modules compiled with two different TLS models),
1148 // but will usually have at most one. GOT_TYPE identifies the type of
1149 // GOT entry; its values are specific to each target.
1150
1151 class Got_offset_list
1152 {
1153 public:
1154 Got_offset_list()
1155 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1156 { }
1157
1158 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1159 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1160 { }
1161
1162 ~Got_offset_list()
1163 {
1164 if (this->got_next_ != NULL)
1165 {
1166 delete this->got_next_;
1167 this->got_next_ = NULL;
1168 }
1169 }
1170
1171 // Initialize the fields to their default values.
1172 void
1173 init()
1174 {
1175 this->got_type_ = -1U;
1176 this->got_offset_ = 0;
1177 this->got_next_ = NULL;
1178 }
1179
1180 // Set the offset for the GOT entry of type GOT_TYPE.
1181 void
1182 set_offset(unsigned int got_type, unsigned int got_offset)
1183 {
1184 if (this->got_type_ == -1U)
1185 {
1186 this->got_type_ = got_type;
1187 this->got_offset_ = got_offset;
1188 }
1189 else
1190 {
1191 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1192 {
1193 if (g->got_type_ == got_type)
1194 {
1195 g->got_offset_ = got_offset;
1196 return;
1197 }
1198 }
1199 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1200 g->got_next_ = this->got_next_;
1201 this->got_next_ = g;
1202 }
1203 }
1204
1205 // Return the offset for a GOT entry of type GOT_TYPE.
1206 unsigned int
1207 get_offset(unsigned int got_type) const
1208 {
1209 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1210 {
1211 if (g->got_type_ == got_type)
1212 return g->got_offset_;
1213 }
1214 return -1U;
1215 }
1216
1217 private:
1218 unsigned int got_type_;
1219 unsigned int got_offset_;
1220 Got_offset_list* got_next_;
1221 };
1222
1223 // A regular object file. This is size and endian specific.
1224
1225 template<int size, bool big_endian>
1226 class Sized_relobj : public Relobj
1227 {
1228 public:
1229 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1230 typedef std::vector<Symbol*> Symbols;
1231 typedef std::vector<Symbol_value<size> > Local_values;
1232
1233 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1234 const typename elfcpp::Ehdr<size, big_endian>&);
1235
1236 ~Sized_relobj();
1237
1238 // Set up the object file based on the ELF header.
1239 void
1240 setup(const typename elfcpp::Ehdr<size, big_endian>&);
1241
1242 // Return the number of symbols. This is only valid after
1243 // Object::add_symbols has been called.
1244 unsigned int
1245 symbol_count() const
1246 { return this->local_symbol_count_ + this->symbols_.size(); }
1247
1248 // If SYM is the index of a global symbol in the object file's
1249 // symbol table, return the Symbol object. Otherwise, return NULL.
1250 Symbol*
1251 global_symbol(unsigned int sym) const
1252 {
1253 if (sym >= this->local_symbol_count_)
1254 {
1255 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1256 return this->symbols_[sym - this->local_symbol_count_];
1257 }
1258 return NULL;
1259 }
1260
1261 // Return the section index of symbol SYM. Set *VALUE to its value
1262 // in the object file. Set *IS_ORDINARY if this is an ordinary
1263 // section index, not a special code between SHN_LORESERVE and
1264 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1265 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1266 // it will not return the final value of the symbol in the link.
1267 unsigned int
1268 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1269
1270 // Return a pointer to the Symbol_value structure which holds the
1271 // value of a local symbol.
1272 const Symbol_value<size>*
1273 local_symbol(unsigned int sym) const
1274 {
1275 gold_assert(sym < this->local_values_.size());
1276 return &this->local_values_[sym];
1277 }
1278
1279 // Return the index of local symbol SYM in the ordinary symbol
1280 // table. A value of -1U means that the symbol is not being output.
1281 unsigned int
1282 symtab_index(unsigned int sym) const
1283 {
1284 gold_assert(sym < this->local_values_.size());
1285 return this->local_values_[sym].output_symtab_index();
1286 }
1287
1288 // Return the index of local symbol SYM in the dynamic symbol
1289 // table. A value of -1U means that the symbol is not being output.
1290 unsigned int
1291 dynsym_index(unsigned int sym) const
1292 {
1293 gold_assert(sym < this->local_values_.size());
1294 return this->local_values_[sym].output_dynsym_index();
1295 }
1296
1297 // Return the input section index of local symbol SYM.
1298 unsigned int
1299 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1300 {
1301 gold_assert(sym < this->local_values_.size());
1302 return this->local_values_[sym].input_shndx(is_ordinary);
1303 }
1304
1305 // Return the appropriate Sized_target structure.
1306 Sized_target<size, big_endian>*
1307 sized_target()
1308 { return this->Object::sized_target<size, big_endian>(); }
1309
1310 // Record that local symbol SYM needs a dynamic symbol entry.
1311 void
1312 set_needs_output_dynsym_entry(unsigned int sym)
1313 {
1314 gold_assert(sym < this->local_values_.size());
1315 this->local_values_[sym].set_needs_output_dynsym_entry();
1316 }
1317
1318 // Return whether the local symbol SYMNDX has a GOT offset.
1319 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1320 bool
1321 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1322 {
1323 Local_got_offsets::const_iterator p =
1324 this->local_got_offsets_.find(symndx);
1325 return (p != this->local_got_offsets_.end()
1326 && p->second->get_offset(got_type) != -1U);
1327 }
1328
1329 // Return the GOT offset of the local symbol SYMNDX.
1330 unsigned int
1331 local_got_offset(unsigned int symndx, unsigned int got_type) const
1332 {
1333 Local_got_offsets::const_iterator p =
1334 this->local_got_offsets_.find(symndx);
1335 gold_assert(p != this->local_got_offsets_.end());
1336 unsigned int off = p->second->get_offset(got_type);
1337 gold_assert(off != -1U);
1338 return off;
1339 }
1340
1341 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1342 void
1343 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1344 unsigned int got_offset)
1345 {
1346 Local_got_offsets::const_iterator p =
1347 this->local_got_offsets_.find(symndx);
1348 if (p != this->local_got_offsets_.end())
1349 p->second->set_offset(got_type, got_offset);
1350 else
1351 {
1352 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1353 std::pair<Local_got_offsets::iterator, bool> ins =
1354 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1355 gold_assert(ins.second);
1356 }
1357 }
1358
1359 // Return the name of the symbol that spans the given offset in the
1360 // specified section in this object. This is used only for error
1361 // messages and is not particularly efficient.
1362 bool
1363 get_symbol_location_info(unsigned int shndx, off_t offset,
1364 Symbol_location_info* info);
1365
1366 // Look for a kept section corresponding to the given discarded section,
1367 // and return its output address. This is used only for relocations in
1368 // debugging sections.
1369 Address
1370 map_to_kept_section(unsigned int shndx, bool* found) const;
1371
1372 protected:
1373 // Read the symbols.
1374 void
1375 do_read_symbols(Read_symbols_data*);
1376
1377 // Return the number of local symbols.
1378 unsigned int
1379 do_local_symbol_count() const
1380 { return this->local_symbol_count_; }
1381
1382 // Lay out the input sections.
1383 void
1384 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1385
1386 // Add the symbols to the symbol table.
1387 void
1388 do_add_symbols(Symbol_table*, Read_symbols_data*);
1389
1390 // Read the relocs.
1391 void
1392 do_read_relocs(Read_relocs_data*);
1393
1394 // Scan the relocs and adjust the symbol table.
1395 void
1396 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1397 Read_relocs_data*);
1398
1399 // Count the local symbols.
1400 void
1401 do_count_local_symbols(Stringpool_template<char>*,
1402 Stringpool_template<char>*);
1403
1404 // Finalize the local symbols.
1405 unsigned int
1406 do_finalize_local_symbols(unsigned int, off_t);
1407
1408 // Set the offset where local dynamic symbol information will be stored.
1409 unsigned int
1410 do_set_local_dynsym_indexes(unsigned int);
1411
1412 // Set the offset where local dynamic symbol information will be stored.
1413 unsigned int
1414 do_set_local_dynsym_offset(off_t);
1415
1416 // Relocate the input sections and write out the local symbols.
1417 void
1418 do_relocate(const General_options& options, const Symbol_table* symtab,
1419 const Layout*, Output_file* of);
1420
1421 // Get the size of a section.
1422 uint64_t
1423 do_section_size(unsigned int shndx)
1424 { return this->elf_file_.section_size(shndx); }
1425
1426 // Get the name of a section.
1427 std::string
1428 do_section_name(unsigned int shndx)
1429 { return this->elf_file_.section_name(shndx); }
1430
1431 // Return the location of the contents of a section.
1432 Object::Location
1433 do_section_contents(unsigned int shndx)
1434 { return this->elf_file_.section_contents(shndx); }
1435
1436 // Return section flags.
1437 uint64_t
1438 do_section_flags(unsigned int shndx)
1439 { return this->elf_file_.section_flags(shndx); }
1440
1441 // Return section address.
1442 uint64_t
1443 do_section_address(unsigned int shndx)
1444 { return this->elf_file_.section_addr(shndx); }
1445
1446 // Return section type.
1447 unsigned int
1448 do_section_type(unsigned int shndx)
1449 { return this->elf_file_.section_type(shndx); }
1450
1451 // Return the section link field.
1452 unsigned int
1453 do_section_link(unsigned int shndx)
1454 { return this->elf_file_.section_link(shndx); }
1455
1456 // Return the section info field.
1457 unsigned int
1458 do_section_info(unsigned int shndx)
1459 { return this->elf_file_.section_info(shndx); }
1460
1461 // Return the section alignment.
1462 uint64_t
1463 do_section_addralign(unsigned int shndx)
1464 { return this->elf_file_.section_addralign(shndx); }
1465
1466 // Return the Xindex structure to use.
1467 Xindex*
1468 do_initialize_xindex();
1469
1470 private:
1471 // For convenience.
1472 typedef Sized_relobj<size, big_endian> This;
1473 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1474 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1475 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1476 typedef elfcpp::Shdr<size, big_endian> Shdr;
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*, off_t 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*, off_t 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 // The GOT offsets of local symbols. This map also stores GOT offsets
1599 // for tp-relative offsets for TLS symbols.
1600 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1601
1602 // The TLS GOT offsets of local symbols. The map stores the offsets
1603 // for either a single GOT entry that holds the module index of a TLS
1604 // symbol, or a pair of GOT entries containing the module index and
1605 // dtv-relative offset.
1606 struct Tls_got_entry
1607 {
1608 Tls_got_entry(int got_offset, bool have_pair)
1609 : got_offset_(got_offset),
1610 have_pair_(have_pair)
1611 { }
1612 int got_offset_;
1613 bool have_pair_;
1614 };
1615 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1616
1617 // General access to the ELF file.
1618 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1619 // Index of SHT_SYMTAB section.
1620 unsigned int symtab_shndx_;
1621 // The number of local symbols.
1622 unsigned int local_symbol_count_;
1623 // The number of local symbols which go into the output file.
1624 unsigned int output_local_symbol_count_;
1625 // The number of local symbols which go into the output file's dynamic
1626 // symbol table.
1627 unsigned int output_local_dynsym_count_;
1628 // The entries in the symbol table for the external symbols.
1629 Symbols symbols_;
1630 // File offset for local symbols.
1631 off_t local_symbol_offset_;
1632 // File offset for local dynamic symbols.
1633 off_t local_dynsym_offset_;
1634 // Values of local symbols.
1635 Local_values local_values_;
1636 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1637 // for TLS symbols, indexed by symbol number.
1638 Local_got_offsets local_got_offsets_;
1639 // Whether this object has a GNU style .eh_frame section.
1640 bool has_eh_frame_;
1641 };
1642
1643 // A class to manage the list of all objects.
1644
1645 class Input_objects
1646 {
1647 public:
1648 Input_objects()
1649 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_()
1650 { }
1651
1652 // The type of the list of input relocateable objects.
1653 typedef std::vector<Relobj*> Relobj_list;
1654 typedef Relobj_list::const_iterator Relobj_iterator;
1655
1656 // The type of the list of input dynamic objects.
1657 typedef std::vector<Dynobj*> Dynobj_list;
1658 typedef Dynobj_list::const_iterator Dynobj_iterator;
1659
1660 // Add an object to the list. Return true if all is well, or false
1661 // if this object should be ignored.
1662 bool
1663 add_object(Object*);
1664
1665 // For each dynamic object, check whether we've seen all of its
1666 // explicit dependencies.
1667 void
1668 check_dynamic_dependencies() const;
1669
1670 // Return whether an object was found in the system library
1671 // directory.
1672 bool
1673 found_in_system_library_directory(const Object*) const;
1674
1675 // Iterate over all regular objects.
1676
1677 Relobj_iterator
1678 relobj_begin() const
1679 { return this->relobj_list_.begin(); }
1680
1681 Relobj_iterator
1682 relobj_end() const
1683 { return this->relobj_list_.end(); }
1684
1685 // Iterate over all dynamic objects.
1686
1687 Dynobj_iterator
1688 dynobj_begin() const
1689 { return this->dynobj_list_.begin(); }
1690
1691 Dynobj_iterator
1692 dynobj_end() const
1693 { return this->dynobj_list_.end(); }
1694
1695 // Return whether we have seen any dynamic objects.
1696 bool
1697 any_dynamic() const
1698 { return !this->dynobj_list_.empty(); }
1699
1700 // Return the number of input objects.
1701 int
1702 number_of_input_objects() const
1703 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1704
1705 private:
1706 Input_objects(const Input_objects&);
1707 Input_objects& operator=(const Input_objects&);
1708
1709 // The list of ordinary objects included in the link.
1710 Relobj_list relobj_list_;
1711 // The list of dynamic objects included in the link.
1712 Dynobj_list dynobj_list_;
1713 // SONAMEs that we have seen.
1714 Unordered_set<std::string> sonames_;
1715 // The directory in which we find the libc.so.
1716 std::string system_library_directory_;
1717 };
1718
1719 // Some of the information we pass to the relocation routines. We
1720 // group this together to avoid passing a dozen different arguments.
1721
1722 template<int size, bool big_endian>
1723 struct Relocate_info
1724 {
1725 // Command line options.
1726 const General_options* options;
1727 // Symbol table.
1728 const Symbol_table* symtab;
1729 // Layout.
1730 const Layout* layout;
1731 // Object being relocated.
1732 Sized_relobj<size, big_endian>* object;
1733 // Section index of relocation section.
1734 unsigned int reloc_shndx;
1735 // Section index of section being relocated.
1736 unsigned int data_shndx;
1737
1738 // Return a string showing the location of a relocation. This is
1739 // only used for error messages.
1740 std::string
1741 location(size_t relnum, off_t reloffset) const;
1742 };
1743
1744 // Return an Object appropriate for the input file. P is BYTES long,
1745 // and holds the ELF header.
1746
1747 extern Object*
1748 make_elf_object(const std::string& name, Input_file*,
1749 off_t offset, const unsigned char* p,
1750 section_offset_type bytes);
1751
1752 } // end namespace gold
1753
1754 #endif // !defined(GOLD_OBJECT_H)
This page took 0.06222 seconds and 5 git commands to generate.