* archive.cc (Library_base::should_include_member): Move
[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, 2010, 2011 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 #include "archive.h"
34
35 namespace gold
36 {
37
38 class General_options;
39 class Task;
40 class Cref;
41 class Layout;
42 class Output_data;
43 class Output_section;
44 class Output_file;
45 class Output_symtab_xindex;
46 class Pluginobj;
47 class Dynobj;
48 class Object_merge_map;
49 class Relocatable_relocs;
50 class Symbols_data;
51
52 template<typename Stringpool_char>
53 class Stringpool_template;
54
55 // Data to pass from read_symbols() to add_symbols().
56
57 struct Read_symbols_data
58 {
59 Read_symbols_data()
60 : section_headers(NULL), section_names(NULL), symbols(NULL),
61 symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL)
62 { }
63
64 ~Read_symbols_data();
65
66 // Section headers.
67 File_view* section_headers;
68 // Section names.
69 File_view* section_names;
70 // Size of section name data in bytes.
71 section_size_type section_names_size;
72 // Symbol data.
73 File_view* symbols;
74 // Size of symbol data in bytes.
75 section_size_type symbols_size;
76 // Offset of external symbols within symbol data. This structure
77 // sometimes contains only external symbols, in which case this will
78 // be zero. Sometimes it contains all symbols.
79 section_offset_type external_symbols_offset;
80 // Symbol names.
81 File_view* symbol_names;
82 // Size of symbol name data in bytes.
83 section_size_type symbol_names_size;
84
85 // Version information. This is only used on dynamic objects.
86 // Version symbol data (from SHT_GNU_versym section).
87 File_view* versym;
88 section_size_type versym_size;
89 // Version definition data (from SHT_GNU_verdef section).
90 File_view* verdef;
91 section_size_type verdef_size;
92 unsigned int verdef_info;
93 // Needed version data (from SHT_GNU_verneed section).
94 File_view* verneed;
95 section_size_type verneed_size;
96 unsigned int verneed_info;
97 };
98
99 // Information used to print error messages.
100
101 struct Symbol_location_info
102 {
103 std::string source_file;
104 std::string enclosing_symbol_name;
105 int line_number;
106 };
107
108 // Data about a single relocation section. This is read in
109 // read_relocs and processed in scan_relocs.
110
111 struct Section_relocs
112 {
113 Section_relocs()
114 : contents(NULL)
115 { }
116
117 ~Section_relocs()
118 { delete this->contents; }
119
120 // Index of reloc section.
121 unsigned int reloc_shndx;
122 // Index of section that relocs apply to.
123 unsigned int data_shndx;
124 // Contents of reloc section.
125 File_view* contents;
126 // Reloc section type.
127 unsigned int sh_type;
128 // Number of reloc entries.
129 size_t reloc_count;
130 // Output section.
131 Output_section* output_section;
132 // Whether this section has special handling for offsets.
133 bool needs_special_offset_handling;
134 // Whether the data section is allocated (has the SHF_ALLOC flag set).
135 bool is_data_section_allocated;
136 };
137
138 // Relocations in an object file. This is read in read_relocs and
139 // processed in scan_relocs.
140
141 struct Read_relocs_data
142 {
143 Read_relocs_data()
144 : local_symbols(NULL)
145 { }
146
147 ~Read_relocs_data()
148 { delete this->local_symbols; }
149
150 typedef std::vector<Section_relocs> Relocs_list;
151 // The relocations.
152 Relocs_list relocs;
153 // The local symbols.
154 File_view* local_symbols;
155 };
156
157 // The Xindex class manages section indexes for objects with more than
158 // 0xff00 sections.
159
160 class Xindex
161 {
162 public:
163 Xindex(int large_shndx_offset)
164 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
165 { }
166
167 // Initialize the symtab_xindex_ array, given the object and the
168 // section index of the symbol table to use.
169 template<int size, bool big_endian>
170 void
171 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
172
173 // Read in the symtab_xindex_ array, given its section index.
174 // PSHDRS may optionally point to the section headers.
175 template<int size, bool big_endian>
176 void
177 read_symtab_xindex(Object*, unsigned int xindex_shndx,
178 const unsigned char* pshdrs);
179
180 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
181 // real section index.
182 unsigned int
183 sym_xindex_to_shndx(Object* object, unsigned int symndx);
184
185 private:
186 // The type of the array giving the real section index for symbols
187 // whose st_shndx field holds SHN_XINDEX.
188 typedef std::vector<unsigned int> Symtab_xindex;
189
190 // Adjust a section index if necessary. This should only be called
191 // for ordinary section indexes.
192 unsigned int
193 adjust_shndx(unsigned int shndx)
194 {
195 if (shndx >= elfcpp::SHN_LORESERVE)
196 shndx += this->large_shndx_offset_;
197 return shndx;
198 }
199
200 // Adjust to apply to large section indexes.
201 int large_shndx_offset_;
202 // The data from the SHT_SYMTAB_SHNDX section.
203 Symtab_xindex symtab_xindex_;
204 };
205
206 // Object is an abstract base class which represents either a 32-bit
207 // or a 64-bit input object. This can be a regular object file
208 // (ET_REL) or a shared object (ET_DYN).
209
210 class Object
211 {
212 public:
213 typedef std::vector<Symbol*> Symbols;
214
215 // NAME is the name of the object as we would report it to the user
216 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
217 // used to read the file. OFFSET is the offset within the input
218 // file--0 for a .o or .so file, something else for a .a file.
219 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
220 off_t offset = 0)
221 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
222 is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
223 has_no_split_stack_(false), no_export_(false), xindex_(NULL)
224 { input_file->file().add_object(); }
225
226 virtual ~Object()
227 { this->input_file_->file().remove_object(); }
228
229 // Return the name of the object as we would report it to the tuser.
230 const std::string&
231 name() const
232 { return this->name_; }
233
234 // Get the offset into the file.
235 off_t
236 offset() const
237 { return this->offset_; }
238
239 // Return whether this is a dynamic object.
240 bool
241 is_dynamic() const
242 { return this->is_dynamic_; }
243
244 // Return whether this object is needed--true if it is a dynamic
245 // object which defines some symbol referenced by a regular object.
246 // We keep the flag here rather than in Dynobj for convenience when
247 // setting it.
248 bool
249 is_needed() const
250 { return this->is_needed_; }
251
252 // Record that this object is needed.
253 void
254 set_is_needed()
255 { this->is_needed_ = true; }
256
257 // Return whether this object was compiled with -fsplit-stack.
258 bool
259 uses_split_stack() const
260 { return this->uses_split_stack_; }
261
262 // Return whether this object contains any functions compiled with
263 // the no_split_stack attribute.
264 bool
265 has_no_split_stack() const
266 { return this->has_no_split_stack_; }
267
268 // Returns NULL for Objects that are not plugin objects. This method
269 // is overridden in the Pluginobj class.
270 Pluginobj*
271 pluginobj()
272 { return this->do_pluginobj(); }
273
274 // Get the file. We pass on const-ness.
275 Input_file*
276 input_file()
277 { return this->input_file_; }
278
279 const Input_file*
280 input_file() const
281 { return this->input_file_; }
282
283 // Lock the underlying file.
284 void
285 lock(const Task* t)
286 { this->input_file()->file().lock(t); }
287
288 // Unlock the underlying file.
289 void
290 unlock(const Task* t)
291 { this->input_file()->file().unlock(t); }
292
293 // Return whether the underlying file is locked.
294 bool
295 is_locked() const
296 { return this->input_file()->file().is_locked(); }
297
298 // Return the token, so that the task can be queued.
299 Task_token*
300 token()
301 { return this->input_file()->file().token(); }
302
303 // Release the underlying file.
304 void
305 release()
306 { this->input_file_->file().release(); }
307
308 // Return whether we should just read symbols from this file.
309 bool
310 just_symbols() const
311 { return this->input_file()->just_symbols(); }
312
313 // Get the number of sections.
314 unsigned int
315 shnum() const
316 { return this->shnum_; }
317
318 // Return a view of the contents of a section. Set *PLEN to the
319 // size. CACHE is a hint as in File_read::get_view.
320 const unsigned char*
321 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
322
323 // Adjust a symbol's section index as needed. SYMNDX is the index
324 // of the symbol and SHNDX is the symbol's section from
325 // get_st_shndx. This returns the section index. It sets
326 // *IS_ORDINARY to indicate whether this is a normal section index,
327 // rather than a special code between SHN_LORESERVE and
328 // SHN_HIRESERVE.
329 unsigned int
330 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
331 {
332 if (shndx < elfcpp::SHN_LORESERVE)
333 *is_ordinary = true;
334 else if (shndx == elfcpp::SHN_XINDEX)
335 {
336 if (this->xindex_ == NULL)
337 this->xindex_ = this->do_initialize_xindex();
338 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
339 *is_ordinary = true;
340 }
341 else
342 *is_ordinary = false;
343 return shndx;
344 }
345
346 // Return the size of a section given a section index.
347 uint64_t
348 section_size(unsigned int shndx)
349 { return this->do_section_size(shndx); }
350
351 // Return the name of a section given a section index.
352 std::string
353 section_name(unsigned int shndx)
354 { return this->do_section_name(shndx); }
355
356 // Return the section flags given a section index.
357 uint64_t
358 section_flags(unsigned int shndx)
359 { return this->do_section_flags(shndx); }
360
361 // Return the section entsize given a section index.
362 uint64_t
363 section_entsize(unsigned int shndx)
364 { return this->do_section_entsize(shndx); }
365
366 // Return the section address given a section index.
367 uint64_t
368 section_address(unsigned int shndx)
369 { return this->do_section_address(shndx); }
370
371 // Return the section type given a section index.
372 unsigned int
373 section_type(unsigned int shndx)
374 { return this->do_section_type(shndx); }
375
376 // Return the section link field given a section index.
377 unsigned int
378 section_link(unsigned int shndx)
379 { return this->do_section_link(shndx); }
380
381 // Return the section info field given a section index.
382 unsigned int
383 section_info(unsigned int shndx)
384 { return this->do_section_info(shndx); }
385
386 // Return the required section alignment given a section index.
387 uint64_t
388 section_addralign(unsigned int shndx)
389 { return this->do_section_addralign(shndx); }
390
391 // Return the output section given a section index.
392 Output_section*
393 output_section(unsigned int shndx) const
394 { return this->do_output_section(shndx); }
395
396 // Given a section index, return the offset in the Output_section.
397 // The return value will be -1U if the section is specially mapped,
398 // such as a merge section.
399 uint64_t
400 output_section_offset(unsigned int shndx) const
401 { return this->do_output_section_offset(shndx); }
402
403 // Read the symbol information.
404 void
405 read_symbols(Read_symbols_data* sd)
406 { return this->do_read_symbols(sd); }
407
408 // Pass sections which should be included in the link to the Layout
409 // object, and record where the sections go in the output file.
410 void
411 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
412 { this->do_layout(symtab, layout, sd); }
413
414 // Add symbol information to the global symbol table.
415 void
416 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
417 { this->do_add_symbols(symtab, sd, layout); }
418
419 // Add symbol information to the global symbol table.
420 Archive::Should_include
421 should_include_member(Symbol_table* symtab, Layout* layout,
422 Read_symbols_data* sd, std::string* why)
423 { return this->do_should_include_member(symtab, layout, sd, why); }
424
425 // Iterate over global symbols, calling a visitor class V for each.
426 void
427 for_all_global_symbols(Read_symbols_data* sd,
428 Library_base::Symbol_visitor_base* v)
429 { return this->do_for_all_global_symbols(sd, v); }
430
431 // Functions and types for the elfcpp::Elf_file interface. This
432 // permit us to use Object as the File template parameter for
433 // elfcpp::Elf_file.
434
435 // The View class is returned by view. It must support a single
436 // method, data(). This is trivial, because get_view does what we
437 // need.
438 class View
439 {
440 public:
441 View(const unsigned char* p)
442 : p_(p)
443 { }
444
445 const unsigned char*
446 data() const
447 { return this->p_; }
448
449 private:
450 const unsigned char* p_;
451 };
452
453 // Return a View.
454 View
455 view(off_t file_offset, section_size_type data_size)
456 { return View(this->get_view(file_offset, data_size, true, true)); }
457
458 // Report an error.
459 void
460 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
461
462 // A location in the file.
463 struct Location
464 {
465 off_t file_offset;
466 off_t data_size;
467
468 Location(off_t fo, section_size_type ds)
469 : file_offset(fo), data_size(ds)
470 { }
471 };
472
473 // Get a View given a Location.
474 View view(Location loc)
475 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
476
477 // Get a view into the underlying file.
478 const unsigned char*
479 get_view(off_t start, section_size_type size, bool aligned, bool cache)
480 {
481 return this->input_file()->file().get_view(this->offset_, start, size,
482 aligned, cache);
483 }
484
485 // Get a lasting view into the underlying file.
486 File_view*
487 get_lasting_view(off_t start, section_size_type size, bool aligned,
488 bool cache)
489 {
490 return this->input_file()->file().get_lasting_view(this->offset_, start,
491 size, aligned, cache);
492 }
493
494 // Read data from the underlying file.
495 void
496 read(off_t start, section_size_type size, void* p)
497 { this->input_file()->file().read(start + this->offset_, size, p); }
498
499 // Read multiple data from the underlying file.
500 void
501 read_multiple(const File_read::Read_multiple& rm)
502 { this->input_file()->file().read_multiple(this->offset_, rm); }
503
504 // Stop caching views in the underlying file.
505 void
506 clear_view_cache_marks()
507 { this->input_file()->file().clear_view_cache_marks(); }
508
509 // Get the number of global symbols defined by this object, and the
510 // number of the symbols whose final definition came from this
511 // object.
512 void
513 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
514 size_t* used) const
515 { this->do_get_global_symbol_counts(symtab, defined, used); }
516
517 // Get the symbols defined in this object.
518 const Symbols*
519 get_global_symbols() const
520 { return this->do_get_global_symbols(); }
521
522 // Return whether this object was found in a system directory.
523 bool
524 is_in_system_directory() const
525 { return this->input_file()->is_in_system_directory(); }
526
527 // Return whether we found this object by searching a directory.
528 bool
529 searched_for() const
530 { return this->input_file()->will_search_for(); }
531
532 bool
533 no_export() const
534 { return this->no_export_; }
535
536 void
537 set_no_export(bool value)
538 { this->no_export_ = value; }
539
540 // Return TRUE if the section is a compressed debug section, and set
541 // *UNCOMPRESSED_SIZE to the size of the uncompressed data.
542 bool
543 section_is_compressed(unsigned int shndx,
544 section_size_type* uncompressed_size) const
545 { return this->do_section_is_compressed(shndx, uncompressed_size); }
546
547 // Return the index of the first incremental relocation for symbol SYMNDX.
548 unsigned int
549 get_incremental_reloc_base(unsigned int symndx) const
550 { return this->do_get_incremental_reloc_base(symndx); }
551
552 // Return the number of incremental relocations for symbol SYMNDX.
553 unsigned int
554 get_incremental_reloc_count(unsigned int symndx) const
555 { return this->do_get_incremental_reloc_count(symndx); }
556
557 protected:
558 // Returns NULL for Objects that are not plugin objects. This method
559 // is overridden in the Pluginobj class.
560 virtual Pluginobj*
561 do_pluginobj()
562 { return NULL; }
563
564 // Read the symbols--implemented by child class.
565 virtual void
566 do_read_symbols(Read_symbols_data*) = 0;
567
568 // Lay out sections--implemented by child class.
569 virtual void
570 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
571
572 // Add symbol information to the global symbol table--implemented by
573 // child class.
574 virtual void
575 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
576
577 virtual Archive::Should_include
578 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
579 std::string* why) = 0;
580
581 // Iterate over global symbols, calling a visitor class V for each.
582 virtual void
583 do_for_all_global_symbols(Read_symbols_data* sd,
584 Library_base::Symbol_visitor_base* v) = 0;
585
586 // Return the location of the contents of a section. Implemented by
587 // child class.
588 virtual Location
589 do_section_contents(unsigned int shndx) = 0;
590
591 // Get the size of a section--implemented by child class.
592 virtual uint64_t
593 do_section_size(unsigned int shndx) = 0;
594
595 // Get the name of a section--implemented by child class.
596 virtual std::string
597 do_section_name(unsigned int shndx) = 0;
598
599 // Get section flags--implemented by child class.
600 virtual uint64_t
601 do_section_flags(unsigned int shndx) = 0;
602
603 // Get section entsize--implemented by child class.
604 virtual uint64_t
605 do_section_entsize(unsigned int shndx) = 0;
606
607 // Get section address--implemented by child class.
608 virtual uint64_t
609 do_section_address(unsigned int shndx) = 0;
610
611 // Get section type--implemented by child class.
612 virtual unsigned int
613 do_section_type(unsigned int shndx) = 0;
614
615 // Get section link field--implemented by child class.
616 virtual unsigned int
617 do_section_link(unsigned int shndx) = 0;
618
619 // Get section info field--implemented by child class.
620 virtual unsigned int
621 do_section_info(unsigned int shndx) = 0;
622
623 // Get section alignment--implemented by child class.
624 virtual uint64_t
625 do_section_addralign(unsigned int shndx) = 0;
626
627 // Return the output section given a section index--implemented
628 // by child class.
629 virtual Output_section*
630 do_output_section(unsigned int) const
631 { gold_unreachable(); }
632
633 // Get the offset of a section--implemented by child class.
634 virtual uint64_t
635 do_output_section_offset(unsigned int) const
636 { gold_unreachable(); }
637
638 // Return the Xindex structure to use.
639 virtual Xindex*
640 do_initialize_xindex() = 0;
641
642 // Implement get_global_symbol_counts--implemented by child class.
643 virtual void
644 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
645
646 virtual const Symbols*
647 do_get_global_symbols() const = 0;
648
649 // Set the number of sections.
650 void
651 set_shnum(int shnum)
652 { this->shnum_ = shnum; }
653
654 // Functions used by both Sized_relobj and Sized_dynobj.
655
656 // Read the section data into a Read_symbols_data object.
657 template<int size, bool big_endian>
658 void
659 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
660 Read_symbols_data*);
661
662 // Let the child class initialize the xindex object directly.
663 void
664 set_xindex(Xindex* xindex)
665 {
666 gold_assert(this->xindex_ == NULL);
667 this->xindex_ = xindex;
668 }
669
670 // If NAME is the name of a special .gnu.warning section, arrange
671 // for the warning to be issued. SHNDX is the section index.
672 // Return whether it is a warning section.
673 bool
674 handle_gnu_warning_section(const char* name, unsigned int shndx,
675 Symbol_table*);
676
677 // If NAME is the name of the special section which indicates that
678 // this object was compiled with -fsplit-stack, mark it accordingly,
679 // and return true. Otherwise return false.
680 bool
681 handle_split_stack_section(const char* name);
682
683 // Return TRUE if the section is a compressed debug section, and set
684 // *UNCOMPRESSED_SIZE to the size of the uncompressed data.
685 virtual bool
686 do_section_is_compressed(unsigned int, section_size_type*) const
687 { return false; }
688
689 // Return the index of the first incremental relocation for symbol SYMNDX--
690 // implemented by child class.
691 virtual unsigned int
692 do_get_incremental_reloc_base(unsigned int) const
693 { gold_unreachable(); }
694
695 // Return the number of incremental relocations for symbol SYMNDX--
696 // implemented by child class.
697 virtual unsigned int
698 do_get_incremental_reloc_count(unsigned int) const
699 { gold_unreachable(); }
700
701 private:
702 // This class may not be copied.
703 Object(const Object&);
704 Object& operator=(const Object&);
705
706 // Name of object as printed to user.
707 std::string name_;
708 // For reading the file.
709 Input_file* input_file_;
710 // Offset within the file--0 for an object file, non-0 for an
711 // archive.
712 off_t offset_;
713 // Number of input sections.
714 unsigned int shnum_;
715 // Whether this is a dynamic object.
716 bool is_dynamic_ : 1;
717 // Whether this object is needed. This is only set for dynamic
718 // objects, and means that the object defined a symbol which was
719 // used by a reference from a regular object.
720 bool is_needed_ : 1;
721 // Whether this object was compiled with -fsplit-stack.
722 bool uses_split_stack_ : 1;
723 // Whether this object contains any functions compiled with the
724 // no_split_stack attribute.
725 bool has_no_split_stack_ : 1;
726 // True if exclude this object from automatic symbol export.
727 // This is used only for archive objects.
728 bool no_export_ : 1;
729 // Many sections for objects with more than SHN_LORESERVE sections.
730 Xindex* xindex_;
731 };
732
733 // A regular object (ET_REL). This is an abstract base class itself.
734 // The implementation is the template class Sized_relobj.
735
736 class Relobj : public Object
737 {
738 public:
739 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
740 : Object(name, input_file, false, offset),
741 output_sections_(),
742 map_to_relocatable_relocs_(NULL),
743 object_merge_map_(NULL),
744 relocs_must_follow_section_writes_(false),
745 sd_(NULL),
746 reloc_counts_(NULL),
747 reloc_bases_(NULL)
748 { }
749
750 // During garbage collection, the Read_symbols_data pass for
751 // each object is stored as layout needs to be done after
752 // reloc processing.
753 Symbols_data*
754 get_symbols_data()
755 { return this->sd_; }
756
757 // Decides which section names have to be included in the worklist
758 // as roots.
759 bool
760 is_section_name_included(const char* name);
761
762 void
763 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
764 unsigned int section_header_size);
765
766 void
767 set_symbols_data(Symbols_data* sd)
768 { this->sd_ = sd; }
769
770 // During garbage collection, the Read_relocs pass for all objects
771 // is done before scanning the relocs. In that case, this->rd_ is
772 // used to store the information from Read_relocs for each object.
773 // This data is also used to compute the list of relevant sections.
774 Read_relocs_data*
775 get_relocs_data()
776 { return this->rd_; }
777
778 void
779 set_relocs_data(Read_relocs_data* rd)
780 { this->rd_ = rd; }
781
782 virtual bool
783 is_output_section_offset_invalid(unsigned int shndx) const = 0;
784
785 // Read the relocs.
786 void
787 read_relocs(Read_relocs_data* rd)
788 { return this->do_read_relocs(rd); }
789
790 // Process the relocs, during garbage collection only.
791 void
792 gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
793 { return this->do_gc_process_relocs(symtab, layout, rd); }
794
795 // Scan the relocs and adjust the symbol table.
796 void
797 scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
798 { return this->do_scan_relocs(symtab, layout, rd); }
799
800 // The number of local symbols in the input symbol table.
801 virtual unsigned int
802 local_symbol_count() const
803 { return this->do_local_symbol_count(); }
804
805 // Initial local symbol processing: count the number of local symbols
806 // in the output symbol table and dynamic symbol table; add local symbol
807 // names to *POOL and *DYNPOOL.
808 void
809 count_local_symbols(Stringpool_template<char>* pool,
810 Stringpool_template<char>* dynpool)
811 { return this->do_count_local_symbols(pool, dynpool); }
812
813 // Set the values of the local symbols, set the output symbol table
814 // indexes for the local variables, and set the offset where local
815 // symbol information will be stored. Returns the new local symbol index.
816 unsigned int
817 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
818 { return this->do_finalize_local_symbols(index, off, symtab); }
819
820 // Set the output dynamic symbol table indexes for the local variables.
821 unsigned int
822 set_local_dynsym_indexes(unsigned int index)
823 { return this->do_set_local_dynsym_indexes(index); }
824
825 // Set the offset where local dynamic symbol information will be stored.
826 unsigned int
827 set_local_dynsym_offset(off_t off)
828 { return this->do_set_local_dynsym_offset(off); }
829
830 // Relocate the input sections and write out the local symbols.
831 void
832 relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of)
833 { return this->do_relocate(symtab, layout, of); }
834
835 // Return whether an input section is being included in the link.
836 bool
837 is_section_included(unsigned int shndx) const
838 {
839 gold_assert(shndx < this->output_sections_.size());
840 return this->output_sections_[shndx] != NULL;
841 }
842
843 // The the output section of the input section with index SHNDX.
844 // This is only used currently to remove a section from the link in
845 // relaxation.
846 void
847 set_output_section(unsigned int shndx, Output_section* os)
848 {
849 gold_assert(shndx < this->output_sections_.size());
850 this->output_sections_[shndx] = os;
851 }
852
853 // Set the offset of an input section within its output section.
854 void
855 set_section_offset(unsigned int shndx, uint64_t off)
856 { this->do_set_section_offset(shndx, off); }
857
858 // Return true if we need to wait for output sections to be written
859 // before we can apply relocations. This is true if the object has
860 // any relocations for sections which require special handling, such
861 // as the exception frame section.
862 bool
863 relocs_must_follow_section_writes() const
864 { return this->relocs_must_follow_section_writes_; }
865
866 // Return the object merge map.
867 Object_merge_map*
868 merge_map() const
869 { return this->object_merge_map_; }
870
871 // Set the object merge map.
872 void
873 set_merge_map(Object_merge_map* object_merge_map)
874 {
875 gold_assert(this->object_merge_map_ == NULL);
876 this->object_merge_map_ = object_merge_map;
877 }
878
879 // Record the relocatable reloc info for an input reloc section.
880 void
881 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
882 {
883 gold_assert(reloc_shndx < this->shnum());
884 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
885 }
886
887 // Get the relocatable reloc info for an input reloc section.
888 Relocatable_relocs*
889 relocatable_relocs(unsigned int reloc_shndx)
890 {
891 gold_assert(reloc_shndx < this->shnum());
892 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
893 }
894
895 // Layout sections whose layout was deferred while waiting for
896 // input files from a plugin.
897 void
898 layout_deferred_sections(Layout* layout)
899 { this->do_layout_deferred_sections(layout); }
900
901 // Return the index of the first incremental relocation for symbol SYMNDX.
902 virtual unsigned int
903 do_get_incremental_reloc_base(unsigned int symndx) const
904 { return this->reloc_bases_[symndx]; }
905
906 // Return the number of incremental relocations for symbol SYMNDX.
907 virtual unsigned int
908 do_get_incremental_reloc_count(unsigned int symndx) const
909 { return this->reloc_counts_[symndx]; }
910
911 protected:
912 // The output section to be used for each input section, indexed by
913 // the input section number. The output section is NULL if the
914 // input section is to be discarded.
915 typedef std::vector<Output_section*> Output_sections;
916
917 // Read the relocs--implemented by child class.
918 virtual void
919 do_read_relocs(Read_relocs_data*) = 0;
920
921 // Process the relocs--implemented by child class.
922 virtual void
923 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
924
925 // Scan the relocs--implemented by child class.
926 virtual void
927 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
928
929 // Return the number of local symbols--implemented by child class.
930 virtual unsigned int
931 do_local_symbol_count() const = 0;
932
933 // Count local symbols--implemented by child class.
934 virtual void
935 do_count_local_symbols(Stringpool_template<char>*,
936 Stringpool_template<char>*) = 0;
937
938 // Finalize the local symbols. Set the output symbol table indexes
939 // for the local variables, and set the offset where local symbol
940 // information will be stored.
941 virtual unsigned int
942 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
943
944 // Set the output dynamic symbol table indexes for the local variables.
945 virtual unsigned int
946 do_set_local_dynsym_indexes(unsigned int) = 0;
947
948 // Set the offset where local dynamic symbol information will be stored.
949 virtual unsigned int
950 do_set_local_dynsym_offset(off_t) = 0;
951
952 // Relocate the input sections and write out the local
953 // symbols--implemented by child class.
954 virtual void
955 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
956
957 // Set the offset of a section--implemented by child class.
958 virtual void
959 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
960
961 // Layout sections whose layout was deferred while waiting for
962 // input files from a plugin--implemented by child class.
963 virtual void
964 do_layout_deferred_sections(Layout*) = 0;
965
966 // Given a section index, return the corresponding Output_section.
967 // The return value will be NULL if the section is not included in
968 // the link.
969 Output_section*
970 do_output_section(unsigned int shndx) const
971 {
972 gold_assert(shndx < this->output_sections_.size());
973 return this->output_sections_[shndx];
974 }
975
976 // Return the vector mapping input sections to output sections.
977 Output_sections&
978 output_sections()
979 { return this->output_sections_; }
980
981 const Output_sections&
982 output_sections() const
983 { return this->output_sections_; }
984
985 // Set the size of the relocatable relocs array.
986 void
987 size_relocatable_relocs()
988 {
989 this->map_to_relocatable_relocs_ =
990 new std::vector<Relocatable_relocs*>(this->shnum());
991 }
992
993 // Record that we must wait for the output sections to be written
994 // before applying relocations.
995 void
996 set_relocs_must_follow_section_writes()
997 { this->relocs_must_follow_section_writes_ = true; }
998
999 // Allocate the array for counting incremental relocations.
1000 void
1001 allocate_incremental_reloc_counts()
1002 {
1003 unsigned int nsyms = this->do_get_global_symbols()->size();
1004 this->reloc_counts_ = new unsigned int[nsyms];
1005 gold_assert(this->reloc_counts_ != NULL);
1006 memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int));
1007 }
1008
1009 // Record a relocation in this object referencing global symbol SYMNDX.
1010 // Used for tracking incremental link information.
1011 void
1012 count_incremental_reloc(unsigned int symndx)
1013 {
1014 unsigned int nsyms = this->do_get_global_symbols()->size();
1015 gold_assert(symndx < nsyms);
1016 gold_assert(this->reloc_counts_ != NULL);
1017 ++this->reloc_counts_[symndx];
1018 }
1019
1020 // Finalize the incremental relocation information.
1021 void
1022 finalize_incremental_relocs(Layout* layout);
1023
1024 // Return the index of the next relocation to be written for global symbol
1025 // SYMNDX. Only valid after finalize_incremental_relocs() has been called.
1026 unsigned int
1027 next_incremental_reloc_index(unsigned int symndx)
1028 {
1029 unsigned int nsyms = this->do_get_global_symbols()->size();
1030
1031 gold_assert(this->reloc_counts_ != NULL);
1032 gold_assert(this->reloc_bases_ != NULL);
1033 gold_assert(symndx < nsyms);
1034
1035 unsigned int counter = this->reloc_counts_[symndx]++;
1036 return this->reloc_bases_[symndx] + counter;
1037 }
1038
1039 private:
1040 // Mapping from input sections to output section.
1041 Output_sections output_sections_;
1042 // Mapping from input section index to the information recorded for
1043 // the relocations. This is only used for a relocatable link.
1044 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
1045 // Mappings for merge sections. This is managed by the code in the
1046 // Merge_map class.
1047 Object_merge_map* object_merge_map_;
1048 // Whether we need to wait for output sections to be written before
1049 // we can apply relocations.
1050 bool relocs_must_follow_section_writes_;
1051 // Used to store the relocs data computed by the Read_relocs pass.
1052 // Used during garbage collection of unused sections.
1053 Read_relocs_data* rd_;
1054 // Used to store the symbols data computed by the Read_symbols pass.
1055 // Again used during garbage collection when laying out referenced
1056 // sections.
1057 gold::Symbols_data* sd_;
1058 // Per-symbol counts of relocations, for incremental links.
1059 unsigned int* reloc_counts_;
1060 // Per-symbol base indexes of relocations, for incremental links.
1061 unsigned int* reloc_bases_;
1062 };
1063
1064 // This class is used to handle relocations against a section symbol
1065 // in an SHF_MERGE section. For such a symbol, we need to know the
1066 // addend of the relocation before we can determine the final value.
1067 // The addend gives us the location in the input section, and we can
1068 // determine how it is mapped to the output section. For a
1069 // non-section symbol, we apply the addend to the final value of the
1070 // symbol; that is done in finalize_local_symbols, and does not use
1071 // this class.
1072
1073 template<int size>
1074 class Merged_symbol_value
1075 {
1076 public:
1077 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1078
1079 // We use a hash table to map offsets in the input section to output
1080 // addresses.
1081 typedef Unordered_map<section_offset_type, Value> Output_addresses;
1082
1083 Merged_symbol_value(Value input_value, Value output_start_address)
1084 : input_value_(input_value), output_start_address_(output_start_address),
1085 output_addresses_()
1086 { }
1087
1088 // Initialize the hash table.
1089 void
1090 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
1091
1092 // Release the hash table to save space.
1093 void
1094 free_input_to_output_map()
1095 { this->output_addresses_.clear(); }
1096
1097 // Get the output value corresponding to an addend. The object and
1098 // input section index are passed in because the caller will have
1099 // them; otherwise we could store them here.
1100 Value
1101 value(const Relobj* object, unsigned int input_shndx, Value addend) const
1102 {
1103 // This is a relocation against a section symbol. ADDEND is the
1104 // offset in the section. The result should be the start of some
1105 // merge area. If the object file wants something else, it should
1106 // use a regular symbol rather than a section symbol.
1107 // Unfortunately, PR 6658 shows a case in which the object file
1108 // refers to the section symbol, but uses a negative ADDEND to
1109 // compensate for a PC relative reloc. We can't handle the
1110 // general case. However, we can handle the special case of a
1111 // negative addend, by assuming that it refers to the start of the
1112 // section. Of course, that means that we have to guess when
1113 // ADDEND is negative. It is normal to see a 32-bit value here
1114 // even when the template parameter size is 64, as 64-bit object
1115 // file formats have 32-bit relocations. We know this is a merge
1116 // section, so we know it has to fit into memory. So we assume
1117 // that we won't see a value larger than a large 32-bit unsigned
1118 // value. This will break objects with very very large merge
1119 // sections; they probably break in other ways anyhow.
1120 Value input_offset = this->input_value_;
1121 if (addend < 0xffffff00)
1122 {
1123 input_offset += addend;
1124 addend = 0;
1125 }
1126 typename Output_addresses::const_iterator p =
1127 this->output_addresses_.find(input_offset);
1128 if (p != this->output_addresses_.end())
1129 return p->second + addend;
1130
1131 return (this->value_from_output_section(object, input_shndx, input_offset)
1132 + addend);
1133 }
1134
1135 private:
1136 // Get the output value for an input offset if we couldn't find it
1137 // in the hash table.
1138 Value
1139 value_from_output_section(const Relobj*, unsigned int input_shndx,
1140 Value input_offset) const;
1141
1142 // The value of the section symbol in the input file. This is
1143 // normally zero, but could in principle be something else.
1144 Value input_value_;
1145 // The start address of this merged section in the output file.
1146 Value output_start_address_;
1147 // A hash table which maps offsets in the input section to output
1148 // addresses. This only maps specific offsets, not all offsets.
1149 Output_addresses output_addresses_;
1150 };
1151
1152 // This POD class is holds the value of a symbol. This is used for
1153 // local symbols, and for all symbols during relocation processing.
1154 // For special sections, such as SHF_MERGE sections, this calls a
1155 // function to get the final symbol value.
1156
1157 template<int size>
1158 class Symbol_value
1159 {
1160 public:
1161 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1162
1163 Symbol_value()
1164 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
1165 is_ordinary_shndx_(false), is_section_symbol_(false),
1166 is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
1167 { this->u_.value = 0; }
1168
1169 ~Symbol_value()
1170 {
1171 if (!this->has_output_value_)
1172 delete this->u_.merged_symbol_value;
1173 }
1174
1175 // Get the value of this symbol. OBJECT is the object in which this
1176 // symbol is defined, and ADDEND is an addend to add to the value.
1177 template<bool big_endian>
1178 Value
1179 value(const Sized_relobj<size, big_endian>* object, Value addend) const
1180 {
1181 if (this->has_output_value_)
1182 return this->u_.value + addend;
1183 else
1184 {
1185 gold_assert(this->is_ordinary_shndx_);
1186 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1187 addend);
1188 }
1189 }
1190
1191 // Set the value of this symbol in the output symbol table.
1192 void
1193 set_output_value(Value value)
1194 { this->u_.value = value; }
1195
1196 // For a section symbol in a merged section, we need more
1197 // information.
1198 void
1199 set_merged_symbol_value(Merged_symbol_value<size>* msv)
1200 {
1201 gold_assert(this->is_section_symbol_);
1202 this->has_output_value_ = false;
1203 this->u_.merged_symbol_value = msv;
1204 }
1205
1206 // Initialize the input to output map for a section symbol in a
1207 // merged section. We also initialize the value of a non-section
1208 // symbol in a merged section.
1209 void
1210 initialize_input_to_output_map(const Relobj* object)
1211 {
1212 if (!this->has_output_value_)
1213 {
1214 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
1215 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1216 msv->initialize_input_to_output_map(object, this->input_shndx_);
1217 }
1218 }
1219
1220 // Free the input to output map for a section symbol in a merged
1221 // section.
1222 void
1223 free_input_to_output_map()
1224 {
1225 if (!this->has_output_value_)
1226 this->u_.merged_symbol_value->free_input_to_output_map();
1227 }
1228
1229 // Set the value of the symbol from the input file. This is only
1230 // called by count_local_symbols, to communicate the value to
1231 // finalize_local_symbols.
1232 void
1233 set_input_value(Value value)
1234 { this->u_.value = value; }
1235
1236 // Return the input value. This is only called by
1237 // finalize_local_symbols and (in special cases) relocate_section.
1238 Value
1239 input_value() const
1240 { return this->u_.value; }
1241
1242 // Return whether we have set the index in the output symbol table
1243 // yet.
1244 bool
1245 is_output_symtab_index_set() const
1246 {
1247 return (this->output_symtab_index_ != 0
1248 && this->output_symtab_index_ != -2U);
1249 }
1250
1251 // Return whether this symbol may be discarded from the normal
1252 // symbol table.
1253 bool
1254 may_be_discarded_from_output_symtab() const
1255 {
1256 gold_assert(!this->is_output_symtab_index_set());
1257 return this->output_symtab_index_ != -2U;
1258 }
1259
1260 // Return whether this symbol has an entry in the output symbol
1261 // table.
1262 bool
1263 has_output_symtab_entry() const
1264 {
1265 gold_assert(this->is_output_symtab_index_set());
1266 return this->output_symtab_index_ != -1U;
1267 }
1268
1269 // Return the index in the output symbol table.
1270 unsigned int
1271 output_symtab_index() const
1272 {
1273 gold_assert(this->is_output_symtab_index_set()
1274 && this->output_symtab_index_ != -1U);
1275 return this->output_symtab_index_;
1276 }
1277
1278 // Set the index in the output symbol table.
1279 void
1280 set_output_symtab_index(unsigned int i)
1281 {
1282 gold_assert(!this->is_output_symtab_index_set());
1283 gold_assert(i != 0 && i != -1U && i != -2U);
1284 this->output_symtab_index_ = i;
1285 }
1286
1287 // Record that this symbol should not go into the output symbol
1288 // table.
1289 void
1290 set_no_output_symtab_entry()
1291 {
1292 gold_assert(this->output_symtab_index_ == 0);
1293 this->output_symtab_index_ = -1U;
1294 }
1295
1296 // Record that this symbol must go into the output symbol table,
1297 // because it there is a relocation that uses it.
1298 void
1299 set_must_have_output_symtab_entry()
1300 {
1301 gold_assert(!this->is_output_symtab_index_set());
1302 this->output_symtab_index_ = -2U;
1303 }
1304
1305 // Set the index in the output dynamic symbol table.
1306 void
1307 set_needs_output_dynsym_entry()
1308 {
1309 gold_assert(!this->is_section_symbol());
1310 this->output_dynsym_index_ = 0;
1311 }
1312
1313 // Return whether this symbol should go into the dynamic symbol
1314 // table.
1315 bool
1316 needs_output_dynsym_entry() const
1317 {
1318 return this->output_dynsym_index_ != -1U;
1319 }
1320
1321 // Return whether this symbol has an entry in the dynamic symbol
1322 // table.
1323 bool
1324 has_output_dynsym_entry() const
1325 {
1326 gold_assert(this->output_dynsym_index_ != 0);
1327 return this->output_dynsym_index_ != -1U;
1328 }
1329
1330 // Record that this symbol should go into the dynamic symbol table.
1331 void
1332 set_output_dynsym_index(unsigned int i)
1333 {
1334 gold_assert(this->output_dynsym_index_ == 0);
1335 gold_assert(i != 0 && i != -1U);
1336 this->output_dynsym_index_ = i;
1337 }
1338
1339 // Return the index in the output dynamic symbol table.
1340 unsigned int
1341 output_dynsym_index() const
1342 {
1343 gold_assert(this->output_dynsym_index_ != 0
1344 && this->output_dynsym_index_ != -1U);
1345 return this->output_dynsym_index_;
1346 }
1347
1348 // Set the index of the input section in the input file.
1349 void
1350 set_input_shndx(unsigned int i, bool is_ordinary)
1351 {
1352 this->input_shndx_ = i;
1353 // input_shndx_ field is a bitfield, so make sure that the value
1354 // fits.
1355 gold_assert(this->input_shndx_ == i);
1356 this->is_ordinary_shndx_ = is_ordinary;
1357 }
1358
1359 // Return the index of the input section in the input file.
1360 unsigned int
1361 input_shndx(bool* is_ordinary) const
1362 {
1363 *is_ordinary = this->is_ordinary_shndx_;
1364 return this->input_shndx_;
1365 }
1366
1367 // Whether this is a section symbol.
1368 bool
1369 is_section_symbol() const
1370 { return this->is_section_symbol_; }
1371
1372 // Record that this is a section symbol.
1373 void
1374 set_is_section_symbol()
1375 {
1376 gold_assert(!this->needs_output_dynsym_entry());
1377 this->is_section_symbol_ = true;
1378 }
1379
1380 // Record that this is a TLS symbol.
1381 void
1382 set_is_tls_symbol()
1383 { this->is_tls_symbol_ = true; }
1384
1385 // Return true if this is a TLS symbol.
1386 bool
1387 is_tls_symbol() const
1388 { return this->is_tls_symbol_; }
1389
1390 // Record that this is an IFUNC symbol.
1391 void
1392 set_is_ifunc_symbol()
1393 { this->is_ifunc_symbol_ = true; }
1394
1395 // Return true if this is an IFUNC symbol.
1396 bool
1397 is_ifunc_symbol() const
1398 { return this->is_ifunc_symbol_; }
1399
1400 // Return true if this has output value.
1401 bool
1402 has_output_value() const
1403 { return this->has_output_value_; }
1404
1405 private:
1406 // The index of this local symbol in the output symbol table. This
1407 // will be 0 if no value has been assigned yet, and the symbol may
1408 // be omitted. This will be -1U if the symbol should not go into
1409 // the symbol table. This will be -2U if the symbol must go into
1410 // the symbol table, but no index has been assigned yet.
1411 unsigned int output_symtab_index_;
1412 // The index of this local symbol in the dynamic symbol table. This
1413 // will be -1U if the symbol should not go into the symbol table.
1414 unsigned int output_dynsym_index_;
1415 // The section index in the input file in which this symbol is
1416 // defined.
1417 unsigned int input_shndx_ : 27;
1418 // Whether the section index is an ordinary index, not a special
1419 // value.
1420 bool is_ordinary_shndx_ : 1;
1421 // Whether this is a STT_SECTION symbol.
1422 bool is_section_symbol_ : 1;
1423 // Whether this is a STT_TLS symbol.
1424 bool is_tls_symbol_ : 1;
1425 // Whether this is a STT_GNU_IFUNC symbol.
1426 bool is_ifunc_symbol_ : 1;
1427 // Whether this symbol has a value for the output file. This is
1428 // normally set to true during Layout::finalize, by
1429 // finalize_local_symbols. It will be false for a section symbol in
1430 // a merge section, as for such symbols we can not determine the
1431 // value to use in a relocation until we see the addend.
1432 bool has_output_value_ : 1;
1433 union
1434 {
1435 // This is used if has_output_value_ is true. Between
1436 // count_local_symbols and finalize_local_symbols, this is the
1437 // value in the input file. After finalize_local_symbols, it is
1438 // the value in the output file.
1439 Value value;
1440 // This is used if has_output_value_ is false. It points to the
1441 // information we need to get the value for a merge section.
1442 Merged_symbol_value<size>* merged_symbol_value;
1443 } u_;
1444 };
1445
1446 // A GOT offset list. A symbol may have more than one GOT offset
1447 // (e.g., when mixing modules compiled with two different TLS models),
1448 // but will usually have at most one. GOT_TYPE identifies the type of
1449 // GOT entry; its values are specific to each target.
1450
1451 class Got_offset_list
1452 {
1453 public:
1454 Got_offset_list()
1455 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1456 { }
1457
1458 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1459 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1460 { }
1461
1462 ~Got_offset_list()
1463 {
1464 if (this->got_next_ != NULL)
1465 {
1466 delete this->got_next_;
1467 this->got_next_ = NULL;
1468 }
1469 }
1470
1471 // Initialize the fields to their default values.
1472 void
1473 init()
1474 {
1475 this->got_type_ = -1U;
1476 this->got_offset_ = 0;
1477 this->got_next_ = NULL;
1478 }
1479
1480 // Set the offset for the GOT entry of type GOT_TYPE.
1481 void
1482 set_offset(unsigned int got_type, unsigned int got_offset)
1483 {
1484 if (this->got_type_ == -1U)
1485 {
1486 this->got_type_ = got_type;
1487 this->got_offset_ = got_offset;
1488 }
1489 else
1490 {
1491 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1492 {
1493 if (g->got_type_ == got_type)
1494 {
1495 g->got_offset_ = got_offset;
1496 return;
1497 }
1498 }
1499 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1500 g->got_next_ = this->got_next_;
1501 this->got_next_ = g;
1502 }
1503 }
1504
1505 // Return the offset for a GOT entry of type GOT_TYPE.
1506 unsigned int
1507 get_offset(unsigned int got_type) const
1508 {
1509 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1510 {
1511 if (g->got_type_ == got_type)
1512 return g->got_offset_;
1513 }
1514 return -1U;
1515 }
1516
1517 // Return a pointer to the list, or NULL if the list is empty.
1518 const Got_offset_list*
1519 get_list() const
1520 {
1521 if (this->got_type_ == -1U)
1522 return NULL;
1523 return this;
1524 }
1525
1526 // Loop over all GOT offset entries, applying the function F to each.
1527 template<typename F>
1528 void
1529 for_all_got_offsets(F f) const
1530 {
1531 if (this->got_type_ == -1U)
1532 return;
1533 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1534 f(g->got_type_, g->got_offset_);
1535 }
1536
1537 private:
1538 unsigned int got_type_;
1539 unsigned int got_offset_;
1540 Got_offset_list* got_next_;
1541 };
1542
1543 // This type is used to modify relocations for -fsplit-stack. It is
1544 // indexed by relocation index, and means that the relocation at that
1545 // index should use the symbol from the vector, rather than the one
1546 // indicated by the relocation.
1547
1548 class Reloc_symbol_changes
1549 {
1550 public:
1551 Reloc_symbol_changes(size_t count)
1552 : vec_(count, NULL)
1553 { }
1554
1555 void
1556 set(size_t i, Symbol* sym)
1557 { this->vec_[i] = sym; }
1558
1559 const Symbol*
1560 operator[](size_t i) const
1561 { return this->vec_[i]; }
1562
1563 private:
1564 std::vector<Symbol*> vec_;
1565 };
1566
1567 // Type for mapping section index to uncompressed size.
1568
1569 typedef std::map<unsigned int, section_size_type> Compressed_section_map;
1570
1571 // A regular object file. This is size and endian specific.
1572
1573 template<int size, bool big_endian>
1574 class Sized_relobj : public Relobj
1575 {
1576 public:
1577 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1578 typedef std::vector<Symbol*> Symbols;
1579 typedef std::vector<Symbol_value<size> > Local_values;
1580
1581 static const Address invalid_address = static_cast<Address>(0) - 1;
1582
1583 enum Compute_final_local_value_status
1584 {
1585 // No error.
1586 CFLV_OK,
1587 // An error occurred.
1588 CFLV_ERROR,
1589 // The local symbol has no output section.
1590 CFLV_DISCARDED
1591 };
1592
1593 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1594 const typename elfcpp::Ehdr<size, big_endian>&);
1595
1596 ~Sized_relobj();
1597
1598 // Checks if the offset of input section SHNDX within its output
1599 // section is invalid.
1600 bool
1601 is_output_section_offset_invalid(unsigned int shndx) const
1602 { return this->get_output_section_offset(shndx) == invalid_address; }
1603
1604 // Set up the object file based on TARGET.
1605 void
1606 setup()
1607 { this->do_setup(); }
1608
1609 // Return the number of symbols. This is only valid after
1610 // Object::add_symbols has been called.
1611 unsigned int
1612 symbol_count() const
1613 { return this->local_symbol_count_ + this->symbols_.size(); }
1614
1615 // If SYM is the index of a global symbol in the object file's
1616 // symbol table, return the Symbol object. Otherwise, return NULL.
1617 Symbol*
1618 global_symbol(unsigned int sym) const
1619 {
1620 if (sym >= this->local_symbol_count_)
1621 {
1622 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1623 return this->symbols_[sym - this->local_symbol_count_];
1624 }
1625 return NULL;
1626 }
1627
1628 // Return the section index of symbol SYM. Set *VALUE to its value
1629 // in the object file. Set *IS_ORDINARY if this is an ordinary
1630 // section index, not a special code between SHN_LORESERVE and
1631 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1632 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1633 // it will not return the final value of the symbol in the link.
1634 unsigned int
1635 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1636
1637 // Return a pointer to the Symbol_value structure which holds the
1638 // value of a local symbol.
1639 const Symbol_value<size>*
1640 local_symbol(unsigned int sym) const
1641 {
1642 gold_assert(sym < this->local_values_.size());
1643 return &this->local_values_[sym];
1644 }
1645
1646 // Return the index of local symbol SYM in the ordinary symbol
1647 // table. A value of -1U means that the symbol is not being output.
1648 unsigned int
1649 symtab_index(unsigned int sym) const
1650 {
1651 gold_assert(sym < this->local_values_.size());
1652 return this->local_values_[sym].output_symtab_index();
1653 }
1654
1655 // Return the index of local symbol SYM in the dynamic symbol
1656 // table. A value of -1U means that the symbol is not being output.
1657 unsigned int
1658 dynsym_index(unsigned int sym) const
1659 {
1660 gold_assert(sym < this->local_values_.size());
1661 return this->local_values_[sym].output_dynsym_index();
1662 }
1663
1664 // Return the input section index of local symbol SYM.
1665 unsigned int
1666 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1667 {
1668 gold_assert(sym < this->local_values_.size());
1669 return this->local_values_[sym].input_shndx(is_ordinary);
1670 }
1671
1672 // Record that local symbol SYM must be in the output symbol table.
1673 void
1674 set_must_have_output_symtab_entry(unsigned int sym)
1675 {
1676 gold_assert(sym < this->local_values_.size());
1677 this->local_values_[sym].set_must_have_output_symtab_entry();
1678 }
1679
1680 // Record that local symbol SYM needs a dynamic symbol entry.
1681 void
1682 set_needs_output_dynsym_entry(unsigned int sym)
1683 {
1684 gold_assert(sym < this->local_values_.size());
1685 this->local_values_[sym].set_needs_output_dynsym_entry();
1686 }
1687
1688 // Return whether the local symbol SYMNDX has a GOT offset.
1689 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1690 bool
1691 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1692 {
1693 Local_got_offsets::const_iterator p =
1694 this->local_got_offsets_.find(symndx);
1695 return (p != this->local_got_offsets_.end()
1696 && p->second->get_offset(got_type) != -1U);
1697 }
1698
1699 // Return the GOT offset of the local symbol SYMNDX.
1700 unsigned int
1701 local_got_offset(unsigned int symndx, unsigned int got_type) const
1702 {
1703 Local_got_offsets::const_iterator p =
1704 this->local_got_offsets_.find(symndx);
1705 gold_assert(p != this->local_got_offsets_.end());
1706 unsigned int off = p->second->get_offset(got_type);
1707 gold_assert(off != -1U);
1708 return off;
1709 }
1710
1711 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1712 void
1713 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1714 unsigned int got_offset)
1715 {
1716 Local_got_offsets::const_iterator p =
1717 this->local_got_offsets_.find(symndx);
1718 if (p != this->local_got_offsets_.end())
1719 p->second->set_offset(got_type, got_offset);
1720 else
1721 {
1722 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1723 std::pair<Local_got_offsets::iterator, bool> ins =
1724 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1725 gold_assert(ins.second);
1726 }
1727 }
1728
1729 // Return the GOT offset list for the local symbol SYMNDX.
1730 const Got_offset_list*
1731 local_got_offset_list(unsigned int symndx) const
1732 {
1733 Local_got_offsets::const_iterator p =
1734 this->local_got_offsets_.find(symndx);
1735 if (p == this->local_got_offsets_.end())
1736 return NULL;
1737 return p->second;
1738 }
1739
1740 // Return whether the local symbol SYMNDX has a PLT offset.
1741 bool
1742 local_has_plt_offset(unsigned int symndx) const;
1743
1744 // Return the PLT offset for a local symbol. It is an error to call
1745 // this if it doesn't have one.
1746 unsigned int
1747 local_plt_offset(unsigned int symndx) const;
1748
1749 // Set the PLT offset of the local symbol SYMNDX.
1750 void
1751 set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
1752
1753 // Get the offset of input section SHNDX within its output section.
1754 // This is -1 if the input section requires a special mapping, such
1755 // as a merge section. The output section can be found in the
1756 // output_sections_ field of the parent class Relobj.
1757 Address
1758 get_output_section_offset(unsigned int shndx) const
1759 {
1760 gold_assert(shndx < this->section_offsets_.size());
1761 return this->section_offsets_[shndx];
1762 }
1763
1764 // Return the name of the symbol that spans the given offset in the
1765 // specified section in this object. This is used only for error
1766 // messages and is not particularly efficient.
1767 bool
1768 get_symbol_location_info(unsigned int shndx, off_t offset,
1769 Symbol_location_info* info);
1770
1771 // Look for a kept section corresponding to the given discarded section,
1772 // and return its output address. This is used only for relocations in
1773 // debugging sections.
1774 Address
1775 map_to_kept_section(unsigned int shndx, bool* found) const;
1776
1777 // Compute final local symbol value. R_SYM is the local symbol index.
1778 // LV_IN points to a local symbol value containing the input value.
1779 // LV_OUT points to a local symbol value storing the final output value,
1780 // which must not be a merged symbol value since before calling this
1781 // method to avoid memory leak. SYMTAB points to a symbol table.
1782 //
1783 // The method returns a status code at return. If the return status is
1784 // CFLV_OK, *LV_OUT contains the final value. If the return status is
1785 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
1786 // *LV_OUT is not modified.
1787 Compute_final_local_value_status
1788 compute_final_local_value(unsigned int r_sym,
1789 const Symbol_value<size>* lv_in,
1790 Symbol_value<size>* lv_out,
1791 const Symbol_table* symtab);
1792
1793 protected:
1794 // Set up.
1795 virtual void
1796 do_setup();
1797
1798 // Read the symbols.
1799 void
1800 do_read_symbols(Read_symbols_data*);
1801
1802 // Return the number of local symbols.
1803 unsigned int
1804 do_local_symbol_count() const
1805 { return this->local_symbol_count_; }
1806
1807 // Lay out the input sections.
1808 void
1809 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1810
1811 // Layout sections whose layout was deferred while waiting for
1812 // input files from a plugin.
1813 void
1814 do_layout_deferred_sections(Layout*);
1815
1816 // Add the symbols to the symbol table.
1817 void
1818 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1819
1820 Archive::Should_include
1821 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1822 std::string* why);
1823
1824 // Iterate over global symbols, calling a visitor class V for each.
1825 void
1826 do_for_all_global_symbols(Read_symbols_data* sd,
1827 Library_base::Symbol_visitor_base* v);
1828
1829 // Read the relocs.
1830 void
1831 do_read_relocs(Read_relocs_data*);
1832
1833 // Process the relocs to find list of referenced sections. Used only
1834 // during garbage collection.
1835 void
1836 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1837
1838 // Scan the relocs and adjust the symbol table.
1839 void
1840 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1841
1842 // Count the local symbols.
1843 void
1844 do_count_local_symbols(Stringpool_template<char>*,
1845 Stringpool_template<char>*);
1846
1847 // Finalize the local symbols.
1848 unsigned int
1849 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1850
1851 // Set the offset where local dynamic symbol information will be stored.
1852 unsigned int
1853 do_set_local_dynsym_indexes(unsigned int);
1854
1855 // Set the offset where local dynamic symbol information will be stored.
1856 unsigned int
1857 do_set_local_dynsym_offset(off_t);
1858
1859 // Relocate the input sections and write out the local symbols.
1860 void
1861 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
1862
1863 // Get the size of a section.
1864 uint64_t
1865 do_section_size(unsigned int shndx)
1866 { return this->elf_file_.section_size(shndx); }
1867
1868 // Get the name of a section.
1869 std::string
1870 do_section_name(unsigned int shndx)
1871 { return this->elf_file_.section_name(shndx); }
1872
1873 // Return the location of the contents of a section.
1874 Object::Location
1875 do_section_contents(unsigned int shndx)
1876 { return this->elf_file_.section_contents(shndx); }
1877
1878 // Return section flags.
1879 uint64_t
1880 do_section_flags(unsigned int shndx);
1881
1882 // Return section entsize.
1883 uint64_t
1884 do_section_entsize(unsigned int shndx);
1885
1886 // Return section address.
1887 uint64_t
1888 do_section_address(unsigned int shndx)
1889 { return this->elf_file_.section_addr(shndx); }
1890
1891 // Return section type.
1892 unsigned int
1893 do_section_type(unsigned int shndx)
1894 { return this->elf_file_.section_type(shndx); }
1895
1896 // Return the section link field.
1897 unsigned int
1898 do_section_link(unsigned int shndx)
1899 { return this->elf_file_.section_link(shndx); }
1900
1901 // Return the section info field.
1902 unsigned int
1903 do_section_info(unsigned int shndx)
1904 { return this->elf_file_.section_info(shndx); }
1905
1906 // Return the section alignment.
1907 uint64_t
1908 do_section_addralign(unsigned int shndx)
1909 { return this->elf_file_.section_addralign(shndx); }
1910
1911 // Return the Xindex structure to use.
1912 Xindex*
1913 do_initialize_xindex();
1914
1915 // Get symbol counts.
1916 void
1917 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1918
1919 // Get the global symbols.
1920 const Symbols*
1921 do_get_global_symbols() const
1922 { return &this->symbols_; }
1923
1924 // Get the offset of a section.
1925 uint64_t
1926 do_output_section_offset(unsigned int shndx) const
1927 {
1928 Address off = this->get_output_section_offset(shndx);
1929 if (off == invalid_address)
1930 return -1ULL;
1931 return off;
1932 }
1933
1934 // Set the offset of a section.
1935 void
1936 do_set_section_offset(unsigned int shndx, uint64_t off)
1937 {
1938 gold_assert(shndx < this->section_offsets_.size());
1939 this->section_offsets_[shndx] =
1940 (off == static_cast<uint64_t>(-1)
1941 ? invalid_address
1942 : convert_types<Address, uint64_t>(off));
1943 }
1944
1945 // Adjust a section index if necessary.
1946 unsigned int
1947 adjust_shndx(unsigned int shndx)
1948 {
1949 if (shndx >= elfcpp::SHN_LORESERVE)
1950 shndx += this->elf_file_.large_shndx_offset();
1951 return shndx;
1952 }
1953
1954 // Initialize input to output maps for section symbols in merged
1955 // sections.
1956 void
1957 initialize_input_to_output_maps();
1958
1959 // Free the input to output maps for section symbols in merged
1960 // sections.
1961 void
1962 free_input_to_output_maps();
1963
1964 // Return symbol table section index.
1965 unsigned int
1966 symtab_shndx() const
1967 { return this->symtab_shndx_; }
1968
1969 // Allow a child class to access the ELF file.
1970 elfcpp::Elf_file<size, big_endian, Object>*
1971 elf_file()
1972 { return &this->elf_file_; }
1973
1974 // Allow a child class to access the local values.
1975 Local_values*
1976 local_values()
1977 { return &this->local_values_; }
1978
1979 // Views and sizes when relocating.
1980 struct View_size
1981 {
1982 unsigned char* view;
1983 typename elfcpp::Elf_types<size>::Elf_Addr address;
1984 off_t offset;
1985 section_size_type view_size;
1986 bool is_input_output_view;
1987 bool is_postprocessing_view;
1988 };
1989
1990 typedef std::vector<View_size> Views;
1991
1992 // This may be overriden by a child class.
1993 virtual void
1994 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1995 const unsigned char* pshdrs, Output_file* of,
1996 Views* pviews);
1997
1998 // Allow a child to set output local symbol count.
1999 void
2000 set_output_local_symbol_count(unsigned int value)
2001 { this->output_local_symbol_count_ = value; }
2002
2003 // Return TRUE if the section is a compressed debug section, and set
2004 // *UNCOMPRESSED_SIZE to the size of the uncompressed data.
2005 bool
2006 do_section_is_compressed(unsigned int shndx,
2007 section_size_type* uncompressed_size) const
2008 {
2009 if (this->compressed_sections_ == NULL)
2010 return false;
2011 Compressed_section_map::const_iterator p =
2012 this->compressed_sections_->find(shndx);
2013 if (p != this->compressed_sections_->end())
2014 {
2015 if (uncompressed_size != NULL)
2016 *uncompressed_size = p->second;
2017 return true;
2018 }
2019 return false;
2020 }
2021
2022 private:
2023 // For convenience.
2024 typedef Sized_relobj<size, big_endian> This;
2025 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
2026 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2027 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2028 typedef elfcpp::Shdr<size, big_endian> Shdr;
2029
2030 // To keep track of discarded comdat sections, we need to map a member
2031 // section index to the object and section index of the corresponding
2032 // kept section.
2033 struct Kept_comdat_section
2034 {
2035 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx)
2036 : object(a_object), shndx(a_shndx)
2037 { }
2038 Relobj* object;
2039 unsigned int shndx;
2040 };
2041 typedef std::map<unsigned int, Kept_comdat_section>
2042 Kept_comdat_section_table;
2043
2044 // Find the SHT_SYMTAB section, given the section headers.
2045 void
2046 find_symtab(const unsigned char* pshdrs);
2047
2048 // Return whether SHDR has the right flags for a GNU style exception
2049 // frame section.
2050 bool
2051 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
2052
2053 // Return whether there is a section named .eh_frame which might be
2054 // a GNU style exception frame section.
2055 bool
2056 find_eh_frame(const unsigned char* pshdrs, const char* names,
2057 section_size_type names_size) const;
2058
2059 // Whether to include a section group in the link.
2060 bool
2061 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
2062 const unsigned char*, const char*, section_size_type,
2063 std::vector<bool>*);
2064
2065 // Whether to include a linkonce section in the link.
2066 bool
2067 include_linkonce_section(Layout*, unsigned int, const char*,
2068 const elfcpp::Shdr<size, big_endian>&);
2069
2070 // Layout an input section.
2071 void
2072 layout_section(Layout* layout, unsigned int shndx, const char* name,
2073 typename This::Shdr& shdr, unsigned int reloc_shndx,
2074 unsigned int reloc_type);
2075
2076 // Write section data to the output file. Record the views and
2077 // sizes in VIEWS for use when relocating.
2078 void
2079 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
2080
2081 // Relocate the sections in the output file.
2082 void
2083 relocate_sections(const Symbol_table* symtab, const Layout* layout,
2084 const unsigned char* pshdrs, Output_file* of,
2085 Views* pviews)
2086 { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
2087
2088 // Scan the input relocations for --emit-relocs.
2089 void
2090 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
2091 const Read_relocs_data::Relocs_list::iterator&);
2092
2093 // Scan the input relocations for --emit-relocs, templatized on the
2094 // type of the relocation section.
2095 template<int sh_type>
2096 void
2097 emit_relocs_scan_reltype(Symbol_table*, Layout*,
2098 const unsigned char* plocal_syms,
2099 const Read_relocs_data::Relocs_list::iterator&,
2100 Relocatable_relocs*);
2101
2102 // Emit the relocs for --emit-relocs.
2103 void
2104 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
2105 unsigned int sh_type, const unsigned char* prelocs,
2106 size_t reloc_count, Output_section*, Address output_offset,
2107 unsigned char* view, Address address,
2108 section_size_type view_size,
2109 unsigned char* reloc_view, section_size_type reloc_view_size);
2110
2111 // Emit the relocs for --emit-relocs, templatized on the type of the
2112 // relocation section.
2113 template<int sh_type>
2114 void
2115 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
2116 const unsigned char* prelocs, size_t reloc_count,
2117 Output_section*, Address output_offset,
2118 unsigned char* view, Address address,
2119 section_size_type view_size,
2120 unsigned char* reloc_view,
2121 section_size_type reloc_view_size);
2122
2123 // Scan the input relocations for --incremental.
2124 void
2125 incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&);
2126
2127 // Scan the input relocations for --incremental, templatized on the
2128 // type of the relocation section.
2129 template<int sh_type>
2130 void
2131 incremental_relocs_scan_reltype(
2132 const Read_relocs_data::Relocs_list::iterator&);
2133
2134 void
2135 incremental_relocs_write(const Relocate_info<size, big_endian>*,
2136 unsigned int sh_type,
2137 const unsigned char* prelocs,
2138 size_t reloc_count,
2139 Output_section*,
2140 Address output_offset,
2141 Output_file*);
2142
2143 template<int sh_type>
2144 void
2145 incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*,
2146 const unsigned char* prelocs,
2147 size_t reloc_count,
2148 Output_section*,
2149 Address output_offset,
2150 Output_file*);
2151
2152 // A type shared by split_stack_adjust_reltype and find_functions.
2153 typedef std::map<section_offset_type, section_size_type> Function_offsets;
2154
2155 // Check for -fsplit-stack routines calling non-split-stack routines.
2156 void
2157 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
2158 unsigned int sh_type, unsigned int shndx,
2159 const unsigned char* prelocs, size_t reloc_count,
2160 unsigned char* view, section_size_type view_size,
2161 Reloc_symbol_changes** reloc_map);
2162
2163 template<int sh_type>
2164 void
2165 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
2166 unsigned int shndx, const unsigned char* prelocs,
2167 size_t reloc_count, unsigned char* view,
2168 section_size_type view_size,
2169 Reloc_symbol_changes** reloc_map);
2170
2171 // Find all functions in a section.
2172 void
2173 find_functions(const unsigned char* pshdrs, unsigned int shndx,
2174 Function_offsets*);
2175
2176 // Write out the local symbols.
2177 void
2178 write_local_symbols(Output_file*,
2179 const Stringpool_template<char>*,
2180 const Stringpool_template<char>*,
2181 Output_symtab_xindex*,
2182 Output_symtab_xindex*);
2183
2184 // Record a mapping from discarded section SHNDX to the corresponding
2185 // kept section.
2186 void
2187 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object,
2188 unsigned int kept_shndx)
2189 {
2190 Kept_comdat_section kept(kept_object, kept_shndx);
2191 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
2192 }
2193
2194 // Find the kept section corresponding to the discarded section
2195 // SHNDX. Return true if found.
2196 bool
2197 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object,
2198 unsigned int* kept_shndx) const
2199 {
2200 typename Kept_comdat_section_table::const_iterator p =
2201 this->kept_comdat_sections_.find(shndx);
2202 if (p == this->kept_comdat_sections_.end())
2203 return false;
2204 *kept_object = p->second.object;
2205 *kept_shndx = p->second.shndx;
2206 return true;
2207 }
2208
2209 // Compute final local symbol value. R_SYM is the local symbol index.
2210 // LV_IN points to a local symbol value containing the input value.
2211 // LV_OUT points to a local symbol value storing the final output value,
2212 // which must not be a merged symbol value since before calling this
2213 // method to avoid memory leak. RELOCATABLE indicates whether we are
2214 // linking a relocatable output. OUT_SECTIONS is an array of output
2215 // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB
2216 // points to a symbol table.
2217 //
2218 // The method returns a status code at return. If the return status is
2219 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2220 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2221 // *LV_OUT is not modified.
2222 inline Compute_final_local_value_status
2223 compute_final_local_value_internal(unsigned int r_sym,
2224 const Symbol_value<size>* lv_in,
2225 Symbol_value<size>* lv_out,
2226 bool relocatable,
2227 const Output_sections& out_sections,
2228 const std::vector<Address>& out_offsets,
2229 const Symbol_table* symtab);
2230
2231 // The GOT offsets of local symbols. This map also stores GOT offsets
2232 // for tp-relative offsets for TLS symbols.
2233 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
2234
2235 // The PLT offsets of local symbols.
2236 typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
2237
2238 // Saved information for sections whose layout was deferred.
2239 struct Deferred_layout
2240 {
2241 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2242 Deferred_layout(unsigned int shndx, const char* name,
2243 const unsigned char* pshdr,
2244 unsigned int reloc_shndx, unsigned int reloc_type)
2245 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx),
2246 reloc_type_(reloc_type)
2247 {
2248 memcpy(this->shdr_data_, pshdr, shdr_size);
2249 }
2250 unsigned int shndx_;
2251 std::string name_;
2252 unsigned int reloc_shndx_;
2253 unsigned int reloc_type_;
2254 unsigned char shdr_data_[shdr_size];
2255 };
2256
2257 // General access to the ELF file.
2258 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
2259 // Index of SHT_SYMTAB section.
2260 unsigned int symtab_shndx_;
2261 // The number of local symbols.
2262 unsigned int local_symbol_count_;
2263 // The number of local symbols which go into the output file.
2264 unsigned int output_local_symbol_count_;
2265 // The number of local symbols which go into the output file's dynamic
2266 // symbol table.
2267 unsigned int output_local_dynsym_count_;
2268 // The entries in the symbol table for the external symbols.
2269 Symbols symbols_;
2270 // Number of symbols defined in object file itself.
2271 size_t defined_count_;
2272 // File offset for local symbols.
2273 off_t local_symbol_offset_;
2274 // File offset for local dynamic symbols.
2275 off_t local_dynsym_offset_;
2276 // Values of local symbols.
2277 Local_values local_values_;
2278 // GOT offsets for local non-TLS symbols, and tp-relative offsets
2279 // for TLS symbols, indexed by symbol number.
2280 Local_got_offsets local_got_offsets_;
2281 // PLT offsets for local symbols.
2282 Local_plt_offsets local_plt_offsets_;
2283 // For each input section, the offset of the input section in its
2284 // output section. This is INVALID_ADDRESS if the input section requires a
2285 // special mapping.
2286 std::vector<Address> section_offsets_;
2287 // Table mapping discarded comdat sections to corresponding kept sections.
2288 Kept_comdat_section_table kept_comdat_sections_;
2289 // Whether this object has a GNU style .eh_frame section.
2290 bool has_eh_frame_;
2291 // If this object has a GNU style .eh_frame section that is discarded in
2292 // output, record the index here. Otherwise it is -1U.
2293 unsigned int discarded_eh_frame_shndx_;
2294 // The list of sections whose layout was deferred.
2295 std::vector<Deferred_layout> deferred_layout_;
2296 // The list of relocation sections whose layout was deferred.
2297 std::vector<Deferred_layout> deferred_layout_relocs_;
2298 // For compressed debug sections, map section index to uncompressed size.
2299 Compressed_section_map* compressed_sections_;
2300 };
2301
2302 // A class to manage the list of all objects.
2303
2304 class Input_objects
2305 {
2306 public:
2307 Input_objects()
2308 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
2309 { }
2310
2311 // The type of the list of input relocateable objects.
2312 typedef std::vector<Relobj*> Relobj_list;
2313 typedef Relobj_list::const_iterator Relobj_iterator;
2314
2315 // The type of the list of input dynamic objects.
2316 typedef std::vector<Dynobj*> Dynobj_list;
2317 typedef Dynobj_list::const_iterator Dynobj_iterator;
2318
2319 // Add an object to the list. Return true if all is well, or false
2320 // if this object should be ignored.
2321 bool
2322 add_object(Object*);
2323
2324 // Start processing an archive.
2325 void
2326 archive_start(Archive*);
2327
2328 // Stop processing an archive.
2329 void
2330 archive_stop(Archive*);
2331
2332 // For each dynamic object, check whether we've seen all of its
2333 // explicit dependencies.
2334 void
2335 check_dynamic_dependencies() const;
2336
2337 // Return whether an object was found in the system library
2338 // directory.
2339 bool
2340 found_in_system_library_directory(const Object*) const;
2341
2342 // Print symbol counts.
2343 void
2344 print_symbol_counts(const Symbol_table*) const;
2345
2346 // Print a cross reference table.
2347 void
2348 print_cref(const Symbol_table*, FILE*) const;
2349
2350 // Iterate over all regular objects.
2351
2352 Relobj_iterator
2353 relobj_begin() const
2354 { return this->relobj_list_.begin(); }
2355
2356 Relobj_iterator
2357 relobj_end() const
2358 { return this->relobj_list_.end(); }
2359
2360 // Iterate over all dynamic objects.
2361
2362 Dynobj_iterator
2363 dynobj_begin() const
2364 { return this->dynobj_list_.begin(); }
2365
2366 Dynobj_iterator
2367 dynobj_end() const
2368 { return this->dynobj_list_.end(); }
2369
2370 // Return whether we have seen any dynamic objects.
2371 bool
2372 any_dynamic() const
2373 { return !this->dynobj_list_.empty(); }
2374
2375 // Return the number of non dynamic objects.
2376 int
2377 number_of_relobjs() const
2378 { return this->relobj_list_.size(); }
2379
2380 // Return the number of input objects.
2381 int
2382 number_of_input_objects() const
2383 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
2384
2385 private:
2386 Input_objects(const Input_objects&);
2387 Input_objects& operator=(const Input_objects&);
2388
2389 // The list of ordinary objects included in the link.
2390 Relobj_list relobj_list_;
2391 // The list of dynamic objects included in the link.
2392 Dynobj_list dynobj_list_;
2393 // SONAMEs that we have seen.
2394 Unordered_set<std::string> sonames_;
2395 // Manage cross-references if requested.
2396 Cref* cref_;
2397 };
2398
2399 // Some of the information we pass to the relocation routines. We
2400 // group this together to avoid passing a dozen different arguments.
2401
2402 template<int size, bool big_endian>
2403 struct Relocate_info
2404 {
2405 // Symbol table.
2406 const Symbol_table* symtab;
2407 // Layout.
2408 const Layout* layout;
2409 // Object being relocated.
2410 Sized_relobj<size, big_endian>* object;
2411 // Section index of relocation section.
2412 unsigned int reloc_shndx;
2413 // Section header of relocation section.
2414 const unsigned char* reloc_shdr;
2415 // Section index of section being relocated.
2416 unsigned int data_shndx;
2417 // Section header of data section.
2418 const unsigned char* data_shdr;
2419
2420 // Return a string showing the location of a relocation. This is
2421 // only used for error messages.
2422 std::string
2423 location(size_t relnum, off_t reloffset) const;
2424 };
2425
2426 // This is used to represent a section in an object and is used as the
2427 // key type for various section maps.
2428 typedef std::pair<Object*, unsigned int> Section_id;
2429
2430 // This is similar to Section_id but is used when the section
2431 // pointers are const.
2432 typedef std::pair<const Object*, unsigned int> Const_section_id;
2433
2434 // The hash value is based on the address of an object in memory during
2435 // linking. It is okay to use this for looking up sections but never use
2436 // this in an unordered container that we want to traverse in a repeatable
2437 // manner.
2438
2439 struct Section_id_hash
2440 {
2441 size_t operator()(const Section_id& loc) const
2442 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
2443 };
2444
2445 struct Const_section_id_hash
2446 {
2447 size_t operator()(const Const_section_id& loc) const
2448 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
2449 };
2450
2451 // Return whether INPUT_FILE contains an ELF object start at file
2452 // offset OFFSET. This sets *START to point to a view of the start of
2453 // the file. It sets *READ_SIZE to the number of bytes in the view.
2454
2455 extern bool
2456 is_elf_object(Input_file* input_file, off_t offset,
2457 const unsigned char** start, int* read_size);
2458
2459 // Return an Object appropriate for the input file. P is BYTES long,
2460 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
2461 // this sees an object the linker is not configured to support, it
2462 // sets *PUNCONFIGURED to true and returns NULL without giving an
2463 // error message.
2464
2465 extern Object*
2466 make_elf_object(const std::string& name, Input_file*,
2467 off_t offset, const unsigned char* p,
2468 section_offset_type bytes, bool* punconfigured);
2469
2470 } // end namespace gold
2471
2472 #endif // !defined(GOLD_OBJECT_H)
This page took 0.111524 seconds and 5 git commands to generate.