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