Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / gold / object.h
blobc2359beadf00fc01571d5f5bac6d7f10c6971a07
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_OBJECT_H
24 #define GOLD_OBJECT_H
26 #include <string>
27 #include <vector>
29 #include "elfcpp.h"
30 #include "elfcpp_file.h"
31 #include "fileread.h"
32 #include "target.h"
34 namespace gold
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;
51 template<typename Stringpool_char>
52 class Stringpool_template;
54 // Data to pass from read_symbols() to add_symbols().
56 struct Read_symbols_data
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;
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;
91 // Information used to print error messages.
93 struct Symbol_location_info
95 std::string source_file;
96 std::string enclosing_symbol_name;
97 int line_number;
100 // Data about a single relocation section. This is read in
101 // read_relocs and processed in scan_relocs.
103 struct Section_relocs
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;
123 // Relocations in an object file. This is read in read_relocs and
124 // processed in scan_relocs.
126 struct Read_relocs_data
128 typedef std::vector<Section_relocs> Relocs_list;
129 // The relocations.
130 Relocs_list relocs;
131 // The local symbols.
132 File_view* local_symbols;
135 // The Xindex class manages section indexes for objects with more than
136 // 0xff00 sections.
138 class Xindex
140 public:
141 Xindex(int large_shndx_offset)
142 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
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);
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);
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);
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;
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)
173 if (shndx >= elfcpp::SHN_LORESERVE)
174 shndx += this->large_shndx_offset_;
175 return shndx;
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_;
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).
188 class Object
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& oname, Input_file* oinput_file, bool ois_dynamic,
196 off_t ooffset = 0)
197 : name_(oname), input_file_(oinput_file), offset_(ooffset), shnum_(-1U),
198 is_dynamic_(ois_dynamic), is_needed_(false), uses_split_stack_(false),
199 has_no_split_stack_(false), no_export_(false), xindex_(NULL)
200 { oinput_file->file().add_object(); }
202 virtual ~Object()
203 { this->input_file_->file().remove_object(); }
205 // Return the name of the object as we would report it to the tuser.
206 const std::string&
207 name() const
208 { return this->name_; }
210 // Get the offset into the file.
211 off_t
212 offset() const
213 { return this->offset_; }
215 // Return whether this is a dynamic object.
216 bool
217 is_dynamic() const
218 { return this->is_dynamic_; }
220 // Return whether this object is needed--true if it is a dynamic
221 // object which defines some symbol referenced by a regular object.
222 // We keep the flag here rather than in Dynobj for convenience when
223 // setting it.
224 bool
225 is_needed() const
226 { return this->is_needed_; }
228 // Record that this object is needed.
229 void
230 set_is_needed()
231 { this->is_needed_ = true; }
233 // Return whether this object was compiled with -fsplit-stack.
234 bool
235 uses_split_stack() const
236 { return this->uses_split_stack_; }
238 // Return whether this object contains any functions compiled with
239 // the no_split_stack attribute.
240 bool
241 has_no_split_stack() const
242 { return this->has_no_split_stack_; }
244 // Returns NULL for Objects that are not plugin objects. This method
245 // is overridden in the Pluginobj class.
246 Pluginobj*
247 pluginobj()
248 { return this->do_pluginobj(); }
250 // Get the file. We pass on const-ness.
251 Input_file*
252 input_file()
253 { return this->input_file_; }
255 const Input_file*
256 input_file() const
257 { return this->input_file_; }
259 // Lock the underlying file.
260 void
261 lock(const Task* t)
262 { this->input_file()->file().lock(t); }
264 // Unlock the underlying file.
265 void
266 unlock(const Task* t)
267 { this->input_file()->file().unlock(t); }
269 // Return whether the underlying file is locked.
270 bool
271 is_locked() const
272 { return this->input_file()->file().is_locked(); }
274 // Return the token, so that the task can be queued.
275 Task_token*
276 token()
277 { return this->input_file()->file().token(); }
279 // Release the underlying file.
280 void
281 release()
282 { this->input_file_->file().release(); }
284 // Return whether we should just read symbols from this file.
285 bool
286 just_symbols() const
287 { return this->input_file()->just_symbols(); }
289 // Get the number of sections.
290 unsigned int
291 shnum() const
292 { return this->shnum_; }
294 // Return a view of the contents of a section. Set *PLEN to the
295 // size. CACHE is a hint as in File_read::get_view.
296 const unsigned char*
297 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
299 // Adjust a symbol's section index as needed. SYMNDX is the index
300 // of the symbol and SHNDX is the symbol's section from
301 // get_st_shndx. This returns the section index. It sets
302 // *IS_ORDINARY to indicate whether this is a normal section index,
303 // rather than a special code between SHN_LORESERVE and
304 // SHN_HIRESERVE.
305 unsigned int
306 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
308 if (shndx < elfcpp::SHN_LORESERVE)
309 *is_ordinary = true;
310 else if (shndx == elfcpp::SHN_XINDEX)
312 if (this->xindex_ == NULL)
313 this->xindex_ = this->do_initialize_xindex();
314 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
315 *is_ordinary = true;
317 else
318 *is_ordinary = false;
319 return shndx;
322 // Return the size of a section given a section index.
323 uint64_t
324 section_size(unsigned int shndx)
325 { return this->do_section_size(shndx); }
327 // Return the name of a section given a section index.
328 std::string
329 section_name(unsigned int shndx)
330 { return this->do_section_name(shndx); }
332 // Return the section flags given a section index.
333 uint64_t
334 section_flags(unsigned int shndx)
335 { return this->do_section_flags(shndx); }
337 // Return the section entsize given a section index.
338 uint64_t
339 section_entsize(unsigned int shndx)
340 { return this->do_section_entsize(shndx); }
342 // Return the section address given a section index.
343 uint64_t
344 section_address(unsigned int shndx)
345 { return this->do_section_address(shndx); }
347 // Return the section type given a section index.
348 unsigned int
349 section_type(unsigned int shndx)
350 { return this->do_section_type(shndx); }
352 // Return the section link field given a section index.
353 unsigned int
354 section_link(unsigned int shndx)
355 { return this->do_section_link(shndx); }
357 // Return the section info field given a section index.
358 unsigned int
359 section_info(unsigned int shndx)
360 { return this->do_section_info(shndx); }
362 // Return the required section alignment given a section index.
363 uint64_t
364 section_addralign(unsigned int shndx)
365 { return this->do_section_addralign(shndx); }
367 // Read the symbol information.
368 void
369 read_symbols(Read_symbols_data* sd)
370 { return this->do_read_symbols(sd); }
372 // Pass sections which should be included in the link to the Layout
373 // object, and record where the sections go in the output file.
374 void
375 layout(Symbol_table* symtab, Layout* olayout, Read_symbols_data* sd)
376 { this->do_layout(symtab, olayout, sd); }
378 // Add symbol information to the global symbol table.
379 void
380 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *olayout)
381 { this->do_add_symbols(symtab, sd, olayout); }
383 // Functions and types for the elfcpp::Elf_file interface. This
384 // permit us to use Object as the File template parameter for
385 // elfcpp::Elf_file.
387 // The View class is returned by view. It must support a single
388 // method, data(). This is trivial, because get_view does what we
389 // need.
390 class View
392 public:
393 View(const unsigned char* p)
394 : p_(p)
397 const unsigned char*
398 data() const
399 { return this->p_; }
401 private:
402 const unsigned char* p_;
405 // Return a View.
406 View
407 view(off_t file_offset, section_size_type data_size)
408 { return View(this->get_view(file_offset, data_size, true, true)); }
410 // Report an error.
411 void
412 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
414 // A location in the file.
415 struct Location
417 off_t file_offset;
418 off_t data_size;
420 Location(off_t fo, section_size_type ds)
421 : file_offset(fo), data_size(ds)
425 // Get a View given a Location.
426 View view(Location loc)
427 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
429 // Get a view into the underlying file.
430 const unsigned char*
431 get_view(off_t start, section_size_type size, bool aligned, bool cache)
433 return this->input_file()->file().get_view(this->offset_, start, size,
434 aligned, cache);
437 // Get a lasting view into the underlying file.
438 File_view*
439 get_lasting_view(off_t start, section_size_type size, bool aligned,
440 bool cache)
442 return this->input_file()->file().get_lasting_view(this->offset_, start,
443 size, aligned, cache);
446 // Read data from the underlying file.
447 void
448 read(off_t start, section_size_type size, void* p)
449 { this->input_file()->file().read(start + this->offset_, size, p); }
451 // Read multiple data from the underlying file.
452 void
453 read_multiple(const File_read::Read_multiple& rm)
454 { this->input_file()->file().read_multiple(this->offset_, rm); }
456 // Stop caching views in the underlying file.
457 void
458 clear_view_cache_marks()
459 { this->input_file()->file().clear_view_cache_marks(); }
461 // Get the number of global symbols defined by this object, and the
462 // number of the symbols whose final definition came from this
463 // object.
464 void
465 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
466 size_t* used) const
467 { this->do_get_global_symbol_counts(symtab, defined, used); }
469 // Return whether this object was found in a system directory.
470 bool
471 is_in_system_directory() const
472 { return this->input_file()->is_in_system_directory(); }
474 // Return whether we found this object by searching a directory.
475 bool
476 searched_for() const
477 { return this->input_file()->will_search_for(); }
479 bool
480 no_export() const
481 { return this->no_export_; }
483 void
484 set_no_export(bool value)
485 { this->no_export_ = value; }
487 protected:
488 // Returns NULL for Objects that are not plugin objects. This method
489 // is overridden in the Pluginobj class.
490 virtual Pluginobj*
491 do_pluginobj()
492 { return NULL; }
494 // Read the symbols--implemented by child class.
495 virtual void
496 do_read_symbols(Read_symbols_data*) = 0;
498 // Lay out sections--implemented by child class.
499 virtual void
500 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
502 // Add symbol information to the global symbol table--implemented by
503 // child class.
504 virtual void
505 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
507 // Return the location of the contents of a section. Implemented by
508 // child class.
509 virtual Location
510 do_section_contents(unsigned int shndx) = 0;
512 // Get the size of a section--implemented by child class.
513 virtual uint64_t
514 do_section_size(unsigned int shndx) = 0;
516 // Get the name of a section--implemented by child class.
517 virtual std::string
518 do_section_name(unsigned int shndx) = 0;
520 // Get section flags--implemented by child class.
521 virtual uint64_t
522 do_section_flags(unsigned int shndx) = 0;
524 // Get section entsize--implemented by child class.
525 virtual uint64_t
526 do_section_entsize(unsigned int shndx) = 0;
528 // Get section address--implemented by child class.
529 virtual uint64_t
530 do_section_address(unsigned int shndx) = 0;
532 // Get section type--implemented by child class.
533 virtual unsigned int
534 do_section_type(unsigned int shndx) = 0;
536 // Get section link field--implemented by child class.
537 virtual unsigned int
538 do_section_link(unsigned int shndx) = 0;
540 // Get section info field--implemented by child class.
541 virtual unsigned int
542 do_section_info(unsigned int shndx) = 0;
544 // Get section alignment--implemented by child class.
545 virtual uint64_t
546 do_section_addralign(unsigned int shndx) = 0;
548 // Return the Xindex structure to use.
549 virtual Xindex*
550 do_initialize_xindex() = 0;
552 // Implement get_global_symbol_counts--implemented by child class.
553 virtual void
554 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
556 // Set the number of sections.
557 void
558 set_shnum(int sec_shnum)
559 { this->shnum_ = sec_shnum; }
561 // Functions used by both Sized_relobj and Sized_dynobj.
563 // Read the section data into a Read_symbols_data object.
564 template<int size, bool big_endian>
565 void
566 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
567 Read_symbols_data*);
569 // Let the child class initialize the xindex object directly.
570 void
571 set_xindex(Xindex* xindex)
573 gold_assert(this->xindex_ == NULL);
574 this->xindex_ = xindex;
577 // If NAME is the name of a special .gnu.warning section, arrange
578 // for the warning to be issued. SHNDX is the section index.
579 // Return whether it is a warning section.
580 bool
581 handle_gnu_warning_section(const char* name, unsigned int shndx,
582 Symbol_table*);
584 // If NAME is the name of the special section which indicates that
585 // this object was compiled with -fstack-split, mark it accordingly,
586 // and return true. Otherwise return false.
587 bool
588 handle_split_stack_section(const char* name);
590 private:
591 // This class may not be copied.
592 Object(const Object&);
593 Object& operator=(const Object&);
595 // Name of object as printed to user.
596 std::string name_;
597 // For reading the file.
598 Input_file* input_file_;
599 // Offset within the file--0 for an object file, non-0 for an
600 // archive.
601 off_t offset_;
602 // Number of input sections.
603 unsigned int shnum_;
604 // Whether this is a dynamic object.
605 bool is_dynamic_ : 1;
606 // Whether this object is needed. This is only set for dynamic
607 // objects, and means that the object defined a symbol which was
608 // used by a reference from a regular object.
609 bool is_needed_ : 1;
610 // Whether this object was compiled with -fsplit-stack.
611 bool uses_split_stack_ : 1;
612 // Whether this object contains any functions compiled with the
613 // no_split_stack attribute.
614 bool has_no_split_stack_ : 1;
615 // True if exclude this object from automatic symbol export.
616 // This is used only for archive objects.
617 bool no_export_ : 1;
618 // Many sections for objects with more than SHN_LORESERVE sections.
619 Xindex* xindex_;
622 // A regular object (ET_REL). This is an abstract base class itself.
623 // The implementation is the template class Sized_relobj.
625 class Relobj : public Object
627 public:
628 Relobj(const std::string& rname, Input_file* rinput_file, off_t roffset = 0)
629 : Object(rname, rinput_file, false, roffset),
630 output_sections_(),
631 map_to_relocatable_relocs_(NULL),
632 object_merge_map_(NULL),
633 relocs_must_follow_section_writes_(false),
634 sd_(NULL)
637 // During garbage collection, the Read_symbols_data pass for
638 // each object is stored as layout needs to be done after
639 // reloc processing.
640 Symbols_data*
641 get_symbols_data()
642 { return this->sd_; }
644 // Decides which section names have to be included in the worklist
645 // as roots.
646 bool
647 is_section_name_included(const char *name);
649 void
650 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
651 unsigned int section_header_size);
653 void
654 set_symbols_data(Symbols_data* sd)
655 { this->sd_ = sd; }
657 // During garbage collection, the Read_relocs pass for all objects
658 // is done before scanning the relocs. In that case, this->rd_ is
659 // used to store the information from Read_relocs for each object.
660 // This data is also used to compute the list of relevant sections.
661 Read_relocs_data*
662 get_relocs_data()
663 { return this->rd_; }
665 void
666 set_relocs_data(Read_relocs_data* rd)
667 { this->rd_ = rd; }
669 virtual bool
670 is_output_section_offset_invalid(unsigned int shndx) const = 0;
672 // Read the relocs.
673 void
674 read_relocs(Read_relocs_data* rd)
675 { return this->do_read_relocs(rd); }
677 // Process the relocs, during garbage collection only.
678 void
679 gc_process_relocs(Symbol_table* symtab, Layout* olayout, Read_relocs_data* rd)
680 { return this->do_gc_process_relocs(symtab, olayout, rd); }
682 // Scan the relocs and adjust the symbol table.
683 void
684 scan_relocs(Symbol_table* symtab, Layout* olayout, Read_relocs_data* rd)
685 { return this->do_scan_relocs(symtab, olayout, rd); }
687 // The number of local symbols in the input symbol table.
688 virtual unsigned int
689 local_symbol_count() const
690 { return this->do_local_symbol_count(); }
692 // Initial local symbol processing: count the number of local symbols
693 // in the output symbol table and dynamic symbol table; add local symbol
694 // names to *POOL and *DYNPOOL.
695 void
696 count_local_symbols(Stringpool_template<char>* pool,
697 Stringpool_template<char>* dynpool)
698 { return this->do_count_local_symbols(pool, dynpool); }
700 // Set the values of the local symbols, set the output symbol table
701 // indexes for the local variables, and set the offset where local
702 // symbol information will be stored. Returns the new local symbol index.
703 unsigned int
704 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
705 { return this->do_finalize_local_symbols(index, off, symtab); }
707 // Set the output dynamic symbol table indexes for the local variables.
708 unsigned int
709 set_local_dynsym_indexes(unsigned int index)
710 { return this->do_set_local_dynsym_indexes(index); }
712 // Set the offset where local dynamic symbol information will be stored.
713 unsigned int
714 set_local_dynsym_offset(off_t off)
715 { return this->do_set_local_dynsym_offset(off); }
717 // Relocate the input sections and write out the local symbols.
718 void
719 relocate(const Symbol_table* symtab, const Layout* olayout, Output_file* of)
720 { return this->do_relocate(symtab, olayout, of); }
722 // Return whether an input section is being included in the link.
723 bool
724 is_section_included(unsigned int shndx) const
726 gold_assert(shndx < this->output_sections_.size());
727 return this->output_sections_[shndx] != NULL;
730 // Given a section index, return the corresponding Output_section.
731 // The return value will be NULL if the section is not included in
732 // the link.
733 Output_section*
734 output_section(unsigned int shndx) const
736 gold_assert(shndx < this->output_sections_.size());
737 return this->output_sections_[shndx];
740 // Given a section index, return the offset in the Output_section.
741 // The return value will be -1U if the section is specially mapped,
742 // such as a merge section.
743 uint64_t
744 output_section_offset(unsigned int shndx) const
745 { return this->do_output_section_offset(shndx); }
747 // Set the offset of an input section within its output section.
748 void
749 set_section_offset(unsigned int shndx, uint64_t off)
750 { this->do_set_section_offset(shndx, off); }
752 // Return true if we need to wait for output sections to be written
753 // before we can apply relocations. This is true if the object has
754 // any relocations for sections which require special handling, such
755 // as the exception frame section.
756 bool
757 relocs_must_follow_section_writes() const
758 { return this->relocs_must_follow_section_writes_; }
760 // Return the object merge map.
761 Object_merge_map*
762 merge_map() const
763 { return this->object_merge_map_; }
765 // Set the object merge map.
766 void
767 set_merge_map(Object_merge_map* object_merge_map)
769 gold_assert(this->object_merge_map_ == NULL);
770 this->object_merge_map_ = object_merge_map;
773 // Record the relocatable reloc info for an input reloc section.
774 void
775 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
777 gold_assert(reloc_shndx < this->shnum());
778 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
781 // Get the relocatable reloc info for an input reloc section.
782 Relocatable_relocs*
783 relocatable_relocs(unsigned int reloc_shndx)
785 gold_assert(reloc_shndx < this->shnum());
786 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
789 // Layout sections whose layout was deferred while waiting for
790 // input files from a plugin.
791 void
792 layout_deferred_sections(Layout* olayout)
793 { this->do_layout_deferred_sections(olayout); }
795 protected:
796 // The output section to be used for each input section, indexed by
797 // the input section number. The output section is NULL if the
798 // input section is to be discarded.
799 typedef std::vector<Output_section*> Output_sections;
801 // Read the relocs--implemented by child class.
802 virtual void
803 do_read_relocs(Read_relocs_data*) = 0;
805 // Process the relocs--implemented by child class.
806 virtual void
807 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
809 // Scan the relocs--implemented by child class.
810 virtual void
811 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
813 // Return the number of local symbols--implemented by child class.
814 virtual unsigned int
815 do_local_symbol_count() const = 0;
817 // Count local symbols--implemented by child class.
818 virtual void
819 do_count_local_symbols(Stringpool_template<char>*,
820 Stringpool_template<char>*) = 0;
822 // Finalize the local symbols. Set the output symbol table indexes
823 // for the local variables, and set the offset where local symbol
824 // information will be stored.
825 virtual unsigned int
826 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
828 // Set the output dynamic symbol table indexes for the local variables.
829 virtual unsigned int
830 do_set_local_dynsym_indexes(unsigned int) = 0;
832 // Set the offset where local dynamic symbol information will be stored.
833 virtual unsigned int
834 do_set_local_dynsym_offset(off_t) = 0;
836 // Relocate the input sections and write out the local
837 // symbols--implemented by child class.
838 virtual void
839 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
841 // Get the offset of a section--implemented by child class.
842 virtual uint64_t
843 do_output_section_offset(unsigned int shndx) const = 0;
845 // Set the offset of a section--implemented by child class.
846 virtual void
847 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
849 // Layout sections whose layout was deferred while waiting for
850 // input files from a plugin--implemented by child class.
851 virtual void
852 do_layout_deferred_sections(Layout*) = 0;
854 // Return the vector mapping input sections to output sections.
855 Output_sections&
856 output_sections()
857 { return this->output_sections_; }
859 const Output_sections&
860 output_sections() const
861 { return this->output_sections_; }
863 // Set the size of the relocatable relocs array.
864 void
865 size_relocatable_relocs()
867 this->map_to_relocatable_relocs_ =
868 new std::vector<Relocatable_relocs*>(this->shnum());
871 // Record that we must wait for the output sections to be written
872 // before applying relocations.
873 void
874 set_relocs_must_follow_section_writes()
875 { this->relocs_must_follow_section_writes_ = true; }
877 private:
878 // Mapping from input sections to output section.
879 Output_sections output_sections_;
880 // Mapping from input section index to the information recorded for
881 // the relocations. This is only used for a relocatable link.
882 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
883 // Mappings for merge sections. This is managed by the code in the
884 // Merge_map class.
885 Object_merge_map* object_merge_map_;
886 // Whether we need to wait for output sections to be written before
887 // we can apply relocations.
888 bool relocs_must_follow_section_writes_;
889 // Used to store the relocs data computed by the Read_relocs pass.
890 // Used during garbage collection of unused sections.
891 Read_relocs_data* rd_;
892 // Used to store the symbols data computed by the Read_symbols pass.
893 // Again used during garbage collection when laying out referenced
894 // sections.
895 gold::Symbols_data *sd_;
898 // This class is used to handle relocations against a section symbol
899 // in an SHF_MERGE section. For such a symbol, we need to know the
900 // addend of the relocation before we can determine the final value.
901 // The addend gives us the location in the input section, and we can
902 // determine how it is mapped to the output section. For a
903 // non-section symbol, we apply the addend to the final value of the
904 // symbol; that is done in finalize_local_symbols, and does not use
905 // this class.
907 template<int size>
908 class Merged_symbol_value
910 public:
911 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
913 // We use a hash table to map offsets in the input section to output
914 // addresses.
915 typedef Unordered_map<section_offset_type, Value> Output_addresses;
917 Merged_symbol_value(Value input_value, Value output_start_address)
918 : input_value_(input_value), output_start_address_(output_start_address),
919 output_addresses_()
922 // Initialize the hash table.
923 void
924 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
926 // Release the hash table to save space.
927 void
928 free_input_to_output_map()
929 { this->output_addresses_.clear(); }
931 // Get the output value corresponding to an addend. The object and
932 // input section index are passed in because the caller will have
933 // them; otherwise we could store them here.
934 Value
935 value(const Relobj* object, unsigned int input_shndx, Value addend) const
937 // This is a relocation against a section symbol. ADDEND is the
938 // offset in the section. The result should be the start of some
939 // merge area. If the object file wants something else, it should
940 // use a regular symbol rather than a section symbol.
941 // Unfortunately, PR 6658 shows a case in which the object file
942 // refers to the section symbol, but uses a negative ADDEND to
943 // compensate for a PC relative reloc. We can't handle the
944 // general case. However, we can handle the special case of a
945 // negative addend, by assuming that it refers to the start of the
946 // section. Of course, that means that we have to guess when
947 // ADDEND is negative. It is normal to see a 32-bit value here
948 // even when the template parameter size is 64, as 64-bit object
949 // file formats have 32-bit relocations. We know this is a merge
950 // section, so we know it has to fit into memory. So we assume
951 // that we won't see a value larger than a large 32-bit unsigned
952 // value. This will break objects with very very large merge
953 // sections; they probably break in other ways anyhow.
954 Value input_offset = this->input_value_;
955 if (addend < 0xffffff00)
957 input_offset += addend;
958 addend = 0;
960 typename Output_addresses::const_iterator p =
961 this->output_addresses_.find(input_offset);
962 if (p != this->output_addresses_.end())
963 return p->second + addend;
965 return (this->value_from_output_section(object, input_shndx, input_offset)
966 + addend);
969 private:
970 // Get the output value for an input offset if we couldn't find it
971 // in the hash table.
972 Value
973 value_from_output_section(const Relobj*, unsigned int input_shndx,
974 Value input_offset) const;
976 // The value of the section symbol in the input file. This is
977 // normally zero, but could in principle be something else.
978 Value input_value_;
979 // The start address of this merged section in the output file.
980 Value output_start_address_;
981 // A hash table which maps offsets in the input section to output
982 // addresses. This only maps specific offsets, not all offsets.
983 Output_addresses output_addresses_;
986 // This POD class is holds the value of a symbol. This is used for
987 // local symbols, and for all symbols during relocation processing.
988 // For special sections, such as SHF_MERGE sections, this calls a
989 // function to get the final symbol value.
991 template<int size>
992 class Symbol_value
994 public:
995 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
997 Symbol_value()
998 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
999 is_ordinary_shndx_(false), is_section_symbol_(false),
1000 is_tls_symbol_(false), has_output_value_(true)
1001 { this->u_.value = 0; }
1003 // Get the value of this symbol. OBJECT is the object in which this
1004 // symbol is defined, and ADDEND is an addend to add to the value.
1005 template<bool big_endian>
1006 Value
1007 value(const Sized_relobj<size, big_endian>* object, Value addend) const
1009 if (this->has_output_value_)
1010 return this->u_.value + addend;
1011 else
1013 gold_assert(this->is_ordinary_shndx_);
1014 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1015 addend);
1019 // Set the value of this symbol in the output symbol table.
1020 void
1021 set_output_value(Value val)
1022 { this->u_.value = val; }
1024 // For a section symbol in a merged section, we need more
1025 // information.
1026 void
1027 set_merged_symbol_value(Merged_symbol_value<size>* msv)
1029 gold_assert(this->is_section_symbol_);
1030 this->has_output_value_ = false;
1031 this->u_.merged_symbol_value = msv;
1034 // Initialize the input to output map for a section symbol in a
1035 // merged section. We also initialize the value of a non-section
1036 // symbol in a merged section.
1037 void
1038 initialize_input_to_output_map(const Relobj* object)
1040 if (!this->has_output_value_)
1042 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
1043 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1044 msv->initialize_input_to_output_map(object, this->input_shndx_);
1048 // Free the input to output map for a section symbol in a merged
1049 // section.
1050 void
1051 free_input_to_output_map()
1053 if (!this->has_output_value_)
1054 this->u_.merged_symbol_value->free_input_to_output_map();
1057 // Set the value of the symbol from the input file. This is only
1058 // called by count_local_symbols, to communicate the value to
1059 // finalize_local_symbols.
1060 void
1061 set_input_value(Value val)
1062 { this->u_.value = val; }
1064 // Return the input value. This is only called by
1065 // finalize_local_symbols and (in special cases) relocate_section.
1066 Value
1067 input_value() const
1068 { return this->u_.value; }
1070 // Return whether this symbol should go into the output symbol
1071 // table.
1072 bool
1073 needs_output_symtab_entry() const
1074 { return this->output_symtab_index_ != -1U; }
1076 // Return the index in the output symbol table.
1077 unsigned int
1078 output_symtab_index() const
1080 gold_assert(this->output_symtab_index_ != 0);
1081 return this->output_symtab_index_;
1084 // Set the index in the output symbol table.
1085 void
1086 set_output_symtab_index(unsigned int i)
1088 gold_assert(this->output_symtab_index_ == 0);
1089 this->output_symtab_index_ = i;
1092 // Record that this symbol should not go into the output symbol
1093 // table.
1094 void
1095 set_no_output_symtab_entry()
1097 gold_assert(this->output_symtab_index_ == 0);
1098 this->output_symtab_index_ = -1U;
1101 // Set the index in the output dynamic symbol table.
1102 void
1103 set_needs_output_dynsym_entry()
1105 gold_assert(!this->is_section_symbol());
1106 this->output_dynsym_index_ = 0;
1109 // Return whether this symbol should go into the output symbol
1110 // table.
1111 bool
1112 needs_output_dynsym_entry() const
1114 return this->output_dynsym_index_ != -1U;
1117 // Record that this symbol should go into the dynamic symbol table.
1118 void
1119 set_output_dynsym_index(unsigned int i)
1121 gold_assert(this->output_dynsym_index_ == 0);
1122 this->output_dynsym_index_ = i;
1125 // Return the index in the output dynamic symbol table.
1126 unsigned int
1127 output_dynsym_index() const
1129 gold_assert(this->output_dynsym_index_ != 0
1130 && this->output_dynsym_index_ != -1U);
1131 return this->output_dynsym_index_;
1134 // Set the index of the input section in the input file.
1135 void
1136 set_input_shndx(unsigned int i, bool is_ordinary)
1138 this->input_shndx_ = i;
1139 // input_shndx_ field is a bitfield, so make sure that the value
1140 // fits.
1141 gold_assert(this->input_shndx_ == i);
1142 this->is_ordinary_shndx_ = is_ordinary;
1145 // Return the index of the input section in the input file.
1146 unsigned int
1147 input_shndx(bool* is_ordinary) const
1149 *is_ordinary = this->is_ordinary_shndx_;
1150 return this->input_shndx_;
1153 // Whether this is a section symbol.
1154 bool
1155 is_section_symbol() const
1156 { return this->is_section_symbol_; }
1158 // Record that this is a section symbol.
1159 void
1160 set_is_section_symbol()
1162 gold_assert(!this->needs_output_dynsym_entry());
1163 this->is_section_symbol_ = true;
1166 // Record that this is a TLS symbol.
1167 void
1168 set_is_tls_symbol()
1169 { this->is_tls_symbol_ = true; }
1171 // Return TRUE if this is a TLS symbol.
1172 bool
1173 is_tls_symbol() const
1174 { return this->is_tls_symbol_; }
1176 private:
1177 // The index of this local symbol in the output symbol table. This
1178 // will be -1 if the symbol should not go into the symbol table.
1179 unsigned int output_symtab_index_;
1180 // The index of this local symbol in the dynamic symbol table. This
1181 // will be -1 if the symbol should not go into the symbol table.
1182 unsigned int output_dynsym_index_;
1183 // The section index in the input file in which this symbol is
1184 // defined.
1185 unsigned int input_shndx_ : 28;
1186 // Whether the section index is an ordinary index, not a special
1187 // value.
1188 bool is_ordinary_shndx_ : 1;
1189 // Whether this is a STT_SECTION symbol.
1190 bool is_section_symbol_ : 1;
1191 // Whether this is a STT_TLS symbol.
1192 bool is_tls_symbol_ : 1;
1193 // Whether this symbol has a value for the output file. This is
1194 // normally set to true during Layout::finalize, by
1195 // finalize_local_symbols. It will be false for a section symbol in
1196 // a merge section, as for such symbols we can not determine the
1197 // value to use in a relocation until we see the addend.
1198 bool has_output_value_ : 1;
1199 union
1201 // This is used if has_output_value_ is true. Between
1202 // count_local_symbols and finalize_local_symbols, this is the
1203 // value in the input file. After finalize_local_symbols, it is
1204 // the value in the output file.
1205 Value value;
1206 // This is used if has_output_value_ is false. It points to the
1207 // information we need to get the value for a merge section.
1208 Merged_symbol_value<size>* merged_symbol_value;
1209 } u_;
1212 // A GOT offset list. A symbol may have more than one GOT offset
1213 // (e.g., when mixing modules compiled with two different TLS models),
1214 // but will usually have at most one. GOT_TYPE identifies the type of
1215 // GOT entry; its values are specific to each target.
1217 class Got_offset_list
1219 public:
1220 Got_offset_list()
1221 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1224 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1225 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1228 ~Got_offset_list()
1230 if (this->got_next_ != NULL)
1232 delete this->got_next_;
1233 this->got_next_ = NULL;
1237 // Initialize the fields to their default values.
1238 void
1239 init()
1241 this->got_type_ = -1U;
1242 this->got_offset_ = 0;
1243 this->got_next_ = NULL;
1246 // Set the offset for the GOT entry of type GOT_TYPE.
1247 void
1248 set_offset(unsigned int got_type, unsigned int got_offset)
1250 if (this->got_type_ == -1U)
1252 this->got_type_ = got_type;
1253 this->got_offset_ = got_offset;
1255 else
1257 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1259 if (g->got_type_ == got_type)
1261 g->got_offset_ = got_offset;
1262 return;
1265 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1266 g->got_next_ = this->got_next_;
1267 this->got_next_ = g;
1271 // Return the offset for a GOT entry of type GOT_TYPE.
1272 unsigned int
1273 get_offset(unsigned int got_type) const
1275 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1277 if (g->got_type_ == got_type)
1278 return g->got_offset_;
1280 return -1U;
1283 private:
1284 unsigned int got_type_;
1285 unsigned int got_offset_;
1286 Got_offset_list* got_next_;
1289 // This type is used to modify relocations for -fsplit-stack. It is
1290 // indexed by relocation index, and means that the relocation at that
1291 // index should use the symbol from the vector, rather than the one
1292 // indicated by the relocation.
1294 class Reloc_symbol_changes
1296 public:
1297 Reloc_symbol_changes(size_t count)
1298 : vec_(count, NULL)
1301 void
1302 set(size_t i, Symbol* sym)
1303 { this->vec_[i] = sym; }
1305 const Symbol*
1306 operator[](size_t i) const
1307 { return this->vec_[i]; }
1309 private:
1310 std::vector<Symbol*> vec_;
1313 // A regular object file. This is size and endian specific.
1315 template<int size, bool big_endian>
1316 class Sized_relobj : public Relobj
1318 public:
1319 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1320 typedef std::vector<Symbol*> Symbols;
1321 typedef std::vector<Symbol_value<size> > Local_values;
1323 static const Address invalid_address = static_cast<Address>(0) - 1;
1325 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1326 const typename elfcpp::Ehdr<size, big_endian>&);
1328 ~Sized_relobj();
1330 // Checks if the offset of input section SHNDX within its output
1331 // section is invalid.
1332 bool
1333 is_output_section_offset_invalid(unsigned int shndx) const
1334 { return this->get_output_section_offset(shndx) == invalid_address; }
1336 // Set up the object file based on TARGET.
1337 void
1338 setup()
1339 { this->do_setup(); }
1341 // Return the number of symbols. This is only valid after
1342 // Object::add_symbols has been called.
1343 unsigned int
1344 symbol_count() const
1345 { return this->local_symbol_count_ + this->symbols_.size(); }
1347 // If SYM is the index of a global symbol in the object file's
1348 // symbol table, return the Symbol object. Otherwise, return NULL.
1349 Symbol*
1350 global_symbol(unsigned int sym) const
1352 if (sym >= this->local_symbol_count_)
1354 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1355 return this->symbols_[sym - this->local_symbol_count_];
1357 return NULL;
1360 // Return the section index of symbol SYM. Set *VALUE to its value
1361 // in the object file. Set *IS_ORDINARY if this is an ordinary
1362 // section index, not a special code between SHN_LORESERVE and
1363 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1364 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1365 // it will not return the final value of the symbol in the link.
1366 unsigned int
1367 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1369 // Return a pointer to the Symbol_value structure which holds the
1370 // value of a local symbol.
1371 const Symbol_value<size>*
1372 local_symbol(unsigned int sym) const
1374 gold_assert(sym < this->local_values_.size());
1375 return &this->local_values_[sym];
1378 // Return the index of local symbol SYM in the ordinary symbol
1379 // table. A value of -1U means that the symbol is not being output.
1380 unsigned int
1381 symtab_index(unsigned int sym) const
1383 gold_assert(sym < this->local_values_.size());
1384 return this->local_values_[sym].output_symtab_index();
1387 // Return the index of local symbol SYM in the dynamic symbol
1388 // table. A value of -1U means that the symbol is not being output.
1389 unsigned int
1390 dynsym_index(unsigned int sym) const
1392 gold_assert(sym < this->local_values_.size());
1393 return this->local_values_[sym].output_dynsym_index();
1396 // Return the input section index of local symbol SYM.
1397 unsigned int
1398 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1400 gold_assert(sym < this->local_values_.size());
1401 return this->local_values_[sym].input_shndx(is_ordinary);
1404 // Record that local symbol SYM needs a dynamic symbol entry.
1405 void
1406 set_needs_output_dynsym_entry(unsigned int sym)
1408 gold_assert(sym < this->local_values_.size());
1409 this->local_values_[sym].set_needs_output_dynsym_entry();
1412 // Return whether the local symbol SYMNDX has a GOT offset.
1413 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1414 bool
1415 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1417 Local_got_offsets::const_iterator p =
1418 this->local_got_offsets_.find(symndx);
1419 return (p != this->local_got_offsets_.end()
1420 && p->second->get_offset(got_type) != -1U);
1423 // Return the GOT offset of the local symbol SYMNDX.
1424 unsigned int
1425 local_got_offset(unsigned int symndx, unsigned int got_type) const
1427 Local_got_offsets::const_iterator p =
1428 this->local_got_offsets_.find(symndx);
1429 gold_assert(p != this->local_got_offsets_.end());
1430 unsigned int off = p->second->get_offset(got_type);
1431 gold_assert(off != -1U);
1432 return off;
1435 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1436 void
1437 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1438 unsigned int got_offset)
1440 Local_got_offsets::const_iterator p =
1441 this->local_got_offsets_.find(symndx);
1442 if (p != this->local_got_offsets_.end())
1443 p->second->set_offset(got_type, got_offset);
1444 else
1446 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1447 std::pair<Local_got_offsets::iterator, bool> ins =
1448 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1449 gold_assert(ins.second);
1453 // Get the offset of input section SHNDX within its output section.
1454 // This is -1 if the input section requires a special mapping, such
1455 // as a merge section. The output section can be found in the
1456 // output_sections_ field of the parent class Relobj.
1457 Address
1458 get_output_section_offset(unsigned int shndx) const
1460 gold_assert(shndx < this->section_offsets_.size());
1461 return this->section_offsets_[shndx];
1464 // Return the name of the symbol that spans the given offset in the
1465 // specified section in this object. This is used only for error
1466 // messages and is not particularly efficient.
1467 bool
1468 get_symbol_location_info(unsigned int shndx, off_t offset,
1469 Symbol_location_info* info);
1471 // Look for a kept section corresponding to the given discarded section,
1472 // and return its output address. This is used only for relocations in
1473 // debugging sections.
1474 Address
1475 map_to_kept_section(unsigned int shndx, bool* found) const;
1477 // Make section offset invalid. This is needed for relaxation.
1478 void
1479 invalidate_section_offset(unsigned int shndx)
1480 { this->do_invalidate_section_offset(shndx); }
1482 protected:
1483 // Set up.
1484 virtual void
1485 do_setup();
1487 // Read the symbols.
1488 void
1489 do_read_symbols(Read_symbols_data*);
1491 // Return the number of local symbols.
1492 unsigned int
1493 do_local_symbol_count() const
1494 { return this->local_symbol_count_; }
1496 // Lay out the input sections.
1497 void
1498 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1500 // Layout sections whose layout was deferred while waiting for
1501 // input files from a plugin.
1502 void
1503 do_layout_deferred_sections(Layout*);
1505 // Add the symbols to the symbol table.
1506 void
1507 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1509 // Read the relocs.
1510 void
1511 do_read_relocs(Read_relocs_data*);
1513 // Process the relocs to find list of referenced sections. Used only
1514 // during garbage collection.
1515 void
1516 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1518 // Scan the relocs and adjust the symbol table.
1519 void
1520 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1522 // Count the local symbols.
1523 void
1524 do_count_local_symbols(Stringpool_template<char>*,
1525 Stringpool_template<char>*);
1527 // Finalize the local symbols.
1528 unsigned int
1529 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1531 // Set the offset where local dynamic symbol information will be stored.
1532 unsigned int
1533 do_set_local_dynsym_indexes(unsigned int);
1535 // Set the offset where local dynamic symbol information will be stored.
1536 unsigned int
1537 do_set_local_dynsym_offset(off_t);
1539 // Relocate the input sections and write out the local symbols.
1540 void
1541 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
1543 // Get the size of a section.
1544 uint64_t
1545 do_section_size(unsigned int shndx)
1546 { return this->elf_file_.section_size(shndx); }
1548 // Get the name of a section.
1549 std::string
1550 do_section_name(unsigned int shndx)
1551 { return this->elf_file_.section_name(shndx); }
1553 // Return the location of the contents of a section.
1554 Object::Location
1555 do_section_contents(unsigned int shndx)
1556 { return this->elf_file_.section_contents(shndx); }
1558 // Return section flags.
1559 uint64_t
1560 do_section_flags(unsigned int shndx);
1562 // Return section entsize.
1563 uint64_t
1564 do_section_entsize(unsigned int shndx);
1566 // Return section address.
1567 uint64_t
1568 do_section_address(unsigned int shndx)
1569 { return this->elf_file_.section_addr(shndx); }
1571 // Return section type.
1572 unsigned int
1573 do_section_type(unsigned int shndx)
1574 { return this->elf_file_.section_type(shndx); }
1576 // Return the section link field.
1577 unsigned int
1578 do_section_link(unsigned int shndx)
1579 { return this->elf_file_.section_link(shndx); }
1581 // Return the section info field.
1582 unsigned int
1583 do_section_info(unsigned int shndx)
1584 { return this->elf_file_.section_info(shndx); }
1586 // Return the section alignment.
1587 uint64_t
1588 do_section_addralign(unsigned int shndx)
1589 { return this->elf_file_.section_addralign(shndx); }
1591 // Return the Xindex structure to use.
1592 Xindex*
1593 do_initialize_xindex();
1595 // Get symbol counts.
1596 void
1597 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1599 // Get the offset of a section.
1600 uint64_t
1601 do_output_section_offset(unsigned int shndx) const
1603 Address off = this->get_output_section_offset(shndx);
1604 if (off == invalid_address)
1605 return -1ULL;
1606 return off;
1609 // Set the offset of a section.
1610 void
1611 do_set_section_offset(unsigned int shndx, uint64_t off)
1613 gold_assert(shndx < this->section_offsets_.size());
1614 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1617 // Set the offset of a section to invalid_address.
1618 virtual void
1619 do_invalidate_section_offset(unsigned int shndx)
1621 gold_assert(shndx < this->section_offsets_.size());
1622 this->section_offsets_[shndx] = invalid_address;
1625 // Adjust a section index if necessary.
1626 unsigned int
1627 adjust_shndx(unsigned int shndx)
1629 if (shndx >= elfcpp::SHN_LORESERVE)
1630 shndx += this->elf_file_.large_shndx_offset();
1631 return shndx;
1634 // Initialize input to output maps for section symbols in merged
1635 // sections.
1636 void
1637 initialize_input_to_output_maps();
1639 // Free the input to output maps for section symbols in merged
1640 // sections.
1641 void
1642 free_input_to_output_maps();
1644 // Return symbol table section index.
1645 unsigned int
1646 symtab_shndx() const
1647 { return this->symtab_shndx_; }
1649 // Allow a child class to access the ELF file.
1650 elfcpp::Elf_file<size, big_endian, Object>*
1651 elf_file()
1652 { return &this->elf_file_; }
1654 // Allow a child class to access the local values.
1655 Local_values*
1656 local_values()
1657 { return &this->local_values_; }
1659 // Views and sizes when relocating.
1660 struct View_size
1662 unsigned char* view;
1663 typename elfcpp::Elf_types<size>::Elf_Addr address;
1664 off_t offset;
1665 section_size_type view_size;
1666 bool is_input_output_view;
1667 bool is_postprocessing_view;
1670 typedef std::vector<View_size> Views;
1672 // This may be overriden by a child class.
1673 virtual void
1674 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1675 const unsigned char* pshdrs, Views* pviews);
1677 private:
1678 // For convenience.
1679 typedef Sized_relobj<size, big_endian> This;
1680 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1681 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1682 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1683 typedef elfcpp::Shdr<size, big_endian> Shdr;
1685 // To keep track of discarded comdat sections, we need to map a member
1686 // section index to the object and section index of the corresponding
1687 // kept section.
1688 struct Kept_comdat_section
1690 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx)
1691 : object(a_object), shndx(a_shndx)
1693 Relobj* object;
1694 unsigned int shndx;
1696 typedef std::map<unsigned int, Kept_comdat_section>
1697 Kept_comdat_section_table;
1699 // Find the SHT_SYMTAB section, given the section headers.
1700 void
1701 find_symtab(const unsigned char* pshdrs);
1703 // Return whether SHDR has the right flags for a GNU style exception
1704 // frame section.
1705 bool
1706 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1708 // Return whether there is a section named .eh_frame which might be
1709 // a GNU style exception frame section.
1710 bool
1711 find_eh_frame(const unsigned char* pshdrs, const char* names,
1712 section_size_type names_size) const;
1714 // Whether to include a section group in the link.
1715 bool
1716 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1717 const unsigned char*, const char *, section_size_type,
1718 std::vector<bool>*);
1720 // Whether to include a linkonce section in the link.
1721 bool
1722 include_linkonce_section(Layout*, unsigned int, const char*,
1723 const elfcpp::Shdr<size, big_endian>&);
1725 // Layout an input section.
1726 void
1727 layout_section(Layout* layout, unsigned int shndx, const char* name,
1728 typename This::Shdr& shdr, unsigned int reloc_shndx,
1729 unsigned int reloc_type);
1731 // Write section data to the output file. Record the views and
1732 // sizes in VIEWS for use when relocating.
1733 void
1734 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1736 // Relocate the sections in the output file.
1737 void
1738 relocate_sections(const Symbol_table* symtab, const Layout* olayout,
1739 const unsigned char* pshdrs, Views* pviews)
1740 { this->do_relocate_sections(symtab, olayout, pshdrs, pviews); }
1742 // Scan the input relocations for --emit-relocs.
1743 void
1744 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
1745 const Read_relocs_data::Relocs_list::iterator&);
1747 // Scan the input relocations for --emit-relocs, templatized on the
1748 // type of the relocation section.
1749 template<int sh_type>
1750 void
1751 emit_relocs_scan_reltype(Symbol_table*, Layout*,
1752 const unsigned char* plocal_syms,
1753 const Read_relocs_data::Relocs_list::iterator&,
1754 Relocatable_relocs*);
1756 // Emit the relocs for --emit-relocs.
1757 void
1758 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1759 unsigned int sh_type, const unsigned char* prelocs,
1760 size_t reloc_count, Output_section*, Address output_offset,
1761 unsigned char* view, Address address,
1762 section_size_type view_size,
1763 unsigned char* reloc_view, section_size_type reloc_view_size);
1765 // Emit the relocs for --emit-relocs, templatized on the type of the
1766 // relocation section.
1767 template<int sh_type>
1768 void
1769 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1770 const unsigned char* prelocs, size_t reloc_count,
1771 Output_section*, Address output_offset,
1772 unsigned char* view, Address address,
1773 section_size_type view_size,
1774 unsigned char* reloc_view,
1775 section_size_type reloc_view_size);
1777 // A type shared by split_stack_adjust_reltype and find_functions.
1778 typedef std::map<section_offset_type, section_size_type> Function_offsets;
1780 // Check for -fsplit-stack routines calling non-split-stack routines.
1781 void
1782 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
1783 unsigned int sh_type, unsigned int shndx,
1784 const unsigned char* prelocs, size_t reloc_count,
1785 unsigned char* view, section_size_type view_size,
1786 Reloc_symbol_changes** reloc_map);
1788 template<int sh_type>
1789 void
1790 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
1791 unsigned int shndx, const unsigned char* prelocs,
1792 size_t reloc_count, unsigned char* view,
1793 section_size_type view_size,
1794 Reloc_symbol_changes** reloc_map);
1796 // Find all functions in a section.
1797 void
1798 find_functions(const unsigned char* pshdrs, unsigned int shndx,
1799 Function_offsets*);
1801 // Write out the local symbols.
1802 void
1803 write_local_symbols(Output_file*,
1804 const Stringpool_template<char>*,
1805 const Stringpool_template<char>*,
1806 Output_symtab_xindex*,
1807 Output_symtab_xindex*);
1809 // Clear the local symbol information.
1810 void
1811 clear_local_symbols()
1813 this->local_values_.clear();
1814 this->local_got_offsets_.clear();
1817 // Record a mapping from discarded section SHNDX to the corresponding
1818 // kept section.
1819 void
1820 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object,
1821 unsigned int kept_shndx)
1823 Kept_comdat_section kept(kept_object, kept_shndx);
1824 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
1827 // Find the kept section corresponding to the discarded section
1828 // SHNDX. Return true if found.
1829 bool
1830 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object,
1831 unsigned int* kept_shndx) const
1833 typename Kept_comdat_section_table::const_iterator p =
1834 this->kept_comdat_sections_.find(shndx);
1835 if (p == this->kept_comdat_sections_.end())
1836 return false;
1837 *kept_object = p->second.object;
1838 *kept_shndx = p->second.shndx;
1839 return true;
1842 // The GOT offsets of local symbols. This map also stores GOT offsets
1843 // for tp-relative offsets for TLS symbols.
1844 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1846 // The TLS GOT offsets of local symbols. The map stores the offsets
1847 // for either a single GOT entry that holds the module index of a TLS
1848 // symbol, or a pair of GOT entries containing the module index and
1849 // dtv-relative offset.
1850 struct Tls_got_entry
1852 Tls_got_entry(int got_offset, bool have_pair)
1853 : got_offset_(got_offset),
1854 have_pair_(have_pair)
1856 int got_offset_;
1857 bool have_pair_;
1859 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1861 // Saved information for sections whose layout was deferred.
1862 struct Deferred_layout
1864 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1865 Deferred_layout(unsigned int shndx, const char* name,
1866 const unsigned char* pshdr,
1867 unsigned int reloc_shndx, unsigned int reloc_type)
1868 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx),
1869 reloc_type_(reloc_type)
1871 memcpy(this->shdr_data_, pshdr, shdr_size);
1873 unsigned int shndx_;
1874 std::string name_;
1875 unsigned int reloc_shndx_;
1876 unsigned int reloc_type_;
1877 unsigned char shdr_data_[shdr_size];
1880 // General access to the ELF file.
1881 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1882 // Index of SHT_SYMTAB section.
1883 unsigned int symtab_shndx_;
1884 // The number of local symbols.
1885 unsigned int local_symbol_count_;
1886 // The number of local symbols which go into the output file.
1887 unsigned int output_local_symbol_count_;
1888 // The number of local symbols which go into the output file's dynamic
1889 // symbol table.
1890 unsigned int output_local_dynsym_count_;
1891 // The entries in the symbol table for the external symbols.
1892 Symbols symbols_;
1893 // Number of symbols defined in object file itself.
1894 size_t defined_count_;
1895 // File offset for local symbols.
1896 off_t local_symbol_offset_;
1897 // File offset for local dynamic symbols.
1898 off_t local_dynsym_offset_;
1899 // Values of local symbols.
1900 Local_values local_values_;
1901 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1902 // for TLS symbols, indexed by symbol number.
1903 Local_got_offsets local_got_offsets_;
1904 // For each input section, the offset of the input section in its
1905 // output section. This is INVALID_ADDRESS if the input section requires a
1906 // special mapping.
1907 std::vector<Address> section_offsets_;
1908 // Table mapping discarded comdat sections to corresponding kept sections.
1909 Kept_comdat_section_table kept_comdat_sections_;
1910 // Whether this object has a GNU style .eh_frame section.
1911 bool has_eh_frame_;
1912 // If this object has a GNU style .eh_frame section that is discarded in
1913 // output, record the index here. Otherwise it is -1U.
1914 unsigned int discarded_eh_frame_shndx_;
1915 // The list of sections whose layout was deferred.
1916 std::vector<Deferred_layout> deferred_layout_;
1919 // A class to manage the list of all objects.
1921 class Input_objects
1923 public:
1924 Input_objects()
1925 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
1928 // The type of the list of input relocateable objects.
1929 typedef std::vector<Relobj*> Relobj_list;
1930 typedef Relobj_list::const_iterator Relobj_iterator;
1932 // The type of the list of input dynamic objects.
1933 typedef std::vector<Dynobj*> Dynobj_list;
1934 typedef Dynobj_list::const_iterator Dynobj_iterator;
1936 // Add an object to the list. Return true if all is well, or false
1937 // if this object should be ignored.
1938 bool
1939 add_object(Object*);
1941 // Start processing an archive.
1942 void
1943 archive_start(Archive*);
1945 // Stop processing an archive.
1946 void
1947 archive_stop(Archive*);
1949 // For each dynamic object, check whether we've seen all of its
1950 // explicit dependencies.
1951 void
1952 check_dynamic_dependencies() const;
1954 // Return whether an object was found in the system library
1955 // directory.
1956 bool
1957 found_in_system_library_directory(const Object*) const;
1959 // Print symbol counts.
1960 void
1961 print_symbol_counts(const Symbol_table*) const;
1963 // Iterate over all regular objects.
1965 Relobj_iterator
1966 relobj_begin() const
1967 { return this->relobj_list_.begin(); }
1969 Relobj_iterator
1970 relobj_end() const
1971 { return this->relobj_list_.end(); }
1973 // Iterate over all dynamic objects.
1975 Dynobj_iterator
1976 dynobj_begin() const
1977 { return this->dynobj_list_.begin(); }
1979 Dynobj_iterator
1980 dynobj_end() const
1981 { return this->dynobj_list_.end(); }
1983 // Return whether we have seen any dynamic objects.
1984 bool
1985 any_dynamic() const
1986 { return !this->dynobj_list_.empty(); }
1988 // Return the number of input objects.
1990 number_of_input_objects() const
1991 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1993 private:
1994 Input_objects(const Input_objects&);
1995 Input_objects& operator=(const Input_objects&);
1997 // The list of ordinary objects included in the link.
1998 Relobj_list relobj_list_;
1999 // The list of dynamic objects included in the link.
2000 Dynobj_list dynobj_list_;
2001 // SONAMEs that we have seen.
2002 Unordered_set<std::string> sonames_;
2003 // Manage cross-references if requested.
2004 Cref* cref_;
2007 // Some of the information we pass to the relocation routines. We
2008 // group this together to avoid passing a dozen different arguments.
2010 template<int size, bool big_endian>
2011 struct Relocate_info
2013 // Symbol table.
2014 const Symbol_table* symtab;
2015 // Layout.
2016 const Layout* layout;
2017 // Object being relocated.
2018 Sized_relobj<size, big_endian>* object;
2019 // Section index of relocation section.
2020 unsigned int reloc_shndx;
2021 // Section header of relocation section.
2022 const unsigned char* reloc_shdr;
2023 // Section index of section being relocated.
2024 unsigned int data_shndx;
2025 // Section header of data section.
2026 const unsigned char* data_shdr;
2028 // Return a string showing the location of a relocation. This is
2029 // only used for error messages.
2030 std::string
2031 location(size_t relnum, off_t reloffset) const;
2034 // Return whether INPUT_FILE contains an ELF object start at file
2035 // offset OFFSET. This sets *START to point to a view of the start of
2036 // the file. It sets *READ_SIZE to the number of bytes in the view.
2038 extern bool
2039 is_elf_object(Input_file* input_file, off_t offset,
2040 const unsigned char** start, int *read_size);
2042 // Return an Object appropriate for the input file. P is BYTES long,
2043 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
2044 // this sees an object the linker is not configured to support, it
2045 // sets *PUNCONFIGURED to true and returns NULL without giving an
2046 // error message.
2048 extern Object*
2049 make_elf_object(const std::string& name, Input_file*,
2050 off_t offset, const unsigned char* p,
2051 section_offset_type bytes, bool* punconfigured);
2053 } // end namespace gold
2055 #endif // !defined(GOLD_OBJECT_H)