daily update
[binutils.git] / gold / object.h
blobfcb5d317ea1f2db0d78188a4f616093e6886f9f2
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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;
50 template<typename Stringpool_char>
51 class Stringpool_template;
53 // Data to pass from read_symbols() to add_symbols().
55 struct Read_symbols_data
57 // Section headers.
58 File_view* section_headers;
59 // Section names.
60 File_view* section_names;
61 // Size of section name data in bytes.
62 section_size_type section_names_size;
63 // Symbol data.
64 File_view* symbols;
65 // Size of symbol data in bytes.
66 section_size_type symbols_size;
67 // Offset of external symbols within symbol data. This structure
68 // sometimes contains only external symbols, in which case this will
69 // be zero. Sometimes it contains all symbols.
70 section_offset_type external_symbols_offset;
71 // Symbol names.
72 File_view* symbol_names;
73 // Size of symbol name data in bytes.
74 section_size_type symbol_names_size;
76 // Version information. This is only used on dynamic objects.
77 // Version symbol data (from SHT_GNU_versym section).
78 File_view* versym;
79 section_size_type versym_size;
80 // Version definition data (from SHT_GNU_verdef section).
81 File_view* verdef;
82 section_size_type verdef_size;
83 unsigned int verdef_info;
84 // Needed version data (from SHT_GNU_verneed section).
85 File_view* verneed;
86 section_size_type verneed_size;
87 unsigned int verneed_info;
90 // Information used to print error messages.
92 struct Symbol_location_info
94 std::string source_file;
95 std::string enclosing_symbol_name;
96 int line_number;
99 // Data about a single relocation section. This is read in
100 // read_relocs and processed in scan_relocs.
102 struct Section_relocs
104 // Index of reloc section.
105 unsigned int reloc_shndx;
106 // Index of section that relocs apply to.
107 unsigned int data_shndx;
108 // Contents of reloc section.
109 File_view* contents;
110 // Reloc section type.
111 unsigned int sh_type;
112 // Number of reloc entries.
113 size_t reloc_count;
114 // Output section.
115 Output_section* output_section;
116 // Whether this section has special handling for offsets.
117 bool needs_special_offset_handling;
118 // Whether the data section is allocated (has the SHF_ALLOC flag set).
119 bool is_data_section_allocated;
122 // Relocations in an object file. This is read in read_relocs and
123 // processed in scan_relocs.
125 struct Read_relocs_data
127 typedef std::vector<Section_relocs> Relocs_list;
128 // The relocations.
129 Relocs_list relocs;
130 // The local symbols.
131 File_view* local_symbols;
134 // The Xindex class manages section indexes for objects with more than
135 // 0xff00 sections.
137 class Xindex
139 public:
140 Xindex(int large_shndx_offset)
141 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
144 // Initialize the symtab_xindex_ array, given the object and the
145 // section index of the symbol table to use.
146 template<int size, bool big_endian>
147 void
148 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
150 // Read in the symtab_xindex_ array, given its section index.
151 // PSHDRS may optionally point to the section headers.
152 template<int size, bool big_endian>
153 void
154 read_symtab_xindex(Object*, unsigned int xindex_shndx,
155 const unsigned char* pshdrs);
157 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
158 // real section index.
159 unsigned int
160 sym_xindex_to_shndx(Object* object, unsigned int symndx);
162 private:
163 // The type of the array giving the real section index for symbols
164 // whose st_shndx field holds SHN_XINDEX.
165 typedef std::vector<unsigned int> Symtab_xindex;
167 // Adjust a section index if necessary. This should only be called
168 // for ordinary section indexes.
169 unsigned int
170 adjust_shndx(unsigned int shndx)
172 if (shndx >= elfcpp::SHN_LORESERVE)
173 shndx += this->large_shndx_offset_;
174 return shndx;
177 // Adjust to apply to large section indexes.
178 int large_shndx_offset_;
179 // The data from the SHT_SYMTAB_SHNDX section.
180 Symtab_xindex symtab_xindex_;
183 // Object is an abstract base class which represents either a 32-bit
184 // or a 64-bit input object. This can be a regular object file
185 // (ET_REL) or a shared object (ET_DYN).
187 class Object
189 public:
190 // NAME is the name of the object as we would report it to the user
191 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
192 // used to read the file. OFFSET is the offset within the input
193 // file--0 for a .o or .so file, something else for a .a file.
194 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
195 off_t offset = 0)
196 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
197 is_dynamic_(is_dynamic), target_(NULL), xindex_(NULL)
198 { input_file->file().add_object(); }
200 virtual ~Object()
201 { this->input_file_->file().remove_object(); }
203 // Return the name of the object as we would report it to the tuser.
204 const std::string&
205 name() const
206 { return this->name_; }
208 // Get the offset into the file.
209 off_t
210 offset() const
211 { return this->offset_; }
213 // Return whether this is a dynamic object.
214 bool
215 is_dynamic() const
216 { return this->is_dynamic_; }
218 // Returns NULL for Objects that are not plugin objects. This method
219 // is overridden in the Pluginobj class.
220 Pluginobj*
221 pluginobj()
222 { return this->do_pluginobj(); }
224 // Return the target structure associated with this object.
225 Target*
226 target() const
227 { return this->target_; }
229 // Lock the underlying file.
230 void
231 lock(const Task* t)
232 { this->input_file()->file().lock(t); }
234 // Unlock the underlying file.
235 void
236 unlock(const Task* t)
237 { this->input_file()->file().unlock(t); }
239 // Return whether the underlying file is locked.
240 bool
241 is_locked() const
242 { return this->input_file()->file().is_locked(); }
244 // Return the token, so that the task can be queued.
245 Task_token*
246 token()
247 { return this->input_file()->file().token(); }
249 // Release the underlying file.
250 void
251 release()
252 { this->input_file_->file().release(); }
254 // Return whether we should just read symbols from this file.
255 bool
256 just_symbols() const
257 { return this->input_file()->just_symbols(); }
259 // Return the sized target structure associated with this object.
260 // This is like the target method but it returns a pointer of
261 // appropriate checked type.
262 template<int size, bool big_endian>
263 Sized_target<size, big_endian>*
264 sized_target() const;
266 // Get the number of sections.
267 unsigned int
268 shnum() const
269 { return this->shnum_; }
271 // Return a view of the contents of a section. Set *PLEN to the
272 // size. CACHE is a hint as in File_read::get_view.
273 const unsigned char*
274 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
276 // Adjust a symbol's section index as needed. SYMNDX is the index
277 // of the symbol and SHNDX is the symbol's section from
278 // get_st_shndx. This returns the section index. It sets
279 // *IS_ORDINARY to indicate whether this is a normal section index,
280 // rather than a special code between SHN_LORESERVE and
281 // SHN_HIRESERVE.
282 unsigned int
283 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
285 if (shndx < elfcpp::SHN_LORESERVE)
286 *is_ordinary = true;
287 else if (shndx == elfcpp::SHN_XINDEX)
289 if (this->xindex_ == NULL)
290 this->xindex_ = this->do_initialize_xindex();
291 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
292 *is_ordinary = true;
294 else
295 *is_ordinary = false;
296 return shndx;
299 // Return the size of a section given a section index.
300 uint64_t
301 section_size(unsigned int shndx)
302 { return this->do_section_size(shndx); }
304 // Return the name of a section given a section index.
305 std::string
306 section_name(unsigned int shndx)
307 { return this->do_section_name(shndx); }
309 // Return the section flags given a section index.
310 uint64_t
311 section_flags(unsigned int shndx)
312 { return this->do_section_flags(shndx); }
314 // Return the section address given a section index.
315 uint64_t
316 section_address(unsigned int shndx)
317 { return this->do_section_address(shndx); }
319 // Return the section type given a section index.
320 unsigned int
321 section_type(unsigned int shndx)
322 { return this->do_section_type(shndx); }
324 // Return the section link field given a section index.
325 unsigned int
326 section_link(unsigned int shndx)
327 { return this->do_section_link(shndx); }
329 // Return the section info field given a section index.
330 unsigned int
331 section_info(unsigned int shndx)
332 { return this->do_section_info(shndx); }
334 // Return the required section alignment given a section index.
335 uint64_t
336 section_addralign(unsigned int shndx)
337 { return this->do_section_addralign(shndx); }
339 // Read the symbol information.
340 void
341 read_symbols(Read_symbols_data* sd)
342 { return this->do_read_symbols(sd); }
344 // Pass sections which should be included in the link to the Layout
345 // object, and record where the sections go in the output file.
346 void
347 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
348 { this->do_layout(symtab, layout, sd); }
350 // Add symbol information to the global symbol table.
351 void
352 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
353 { this->do_add_symbols(symtab, sd); }
355 // Functions and types for the elfcpp::Elf_file interface. This
356 // permit us to use Object as the File template parameter for
357 // elfcpp::Elf_file.
359 // The View class is returned by view. It must support a single
360 // method, data(). This is trivial, because get_view does what we
361 // need.
362 class View
364 public:
365 View(const unsigned char* p)
366 : p_(p)
369 const unsigned char*
370 data() const
371 { return this->p_; }
373 private:
374 const unsigned char* p_;
377 // Return a View.
378 View
379 view(off_t file_offset, section_size_type data_size)
380 { return View(this->get_view(file_offset, data_size, true, true)); }
382 // Report an error.
383 void
384 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
386 // A location in the file.
387 struct Location
389 off_t file_offset;
390 off_t data_size;
392 Location(off_t fo, section_size_type ds)
393 : file_offset(fo), data_size(ds)
397 // Get a View given a Location.
398 View view(Location loc)
399 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
401 // Get a view into the underlying file.
402 const unsigned char*
403 get_view(off_t start, section_size_type size, bool aligned, bool cache)
405 return this->input_file()->file().get_view(this->offset_, start, size,
406 aligned, cache);
409 // Get a lasting view into the underlying file.
410 File_view*
411 get_lasting_view(off_t start, section_size_type size, bool aligned,
412 bool cache)
414 return this->input_file()->file().get_lasting_view(this->offset_, start,
415 size, aligned, cache);
418 // Read data from the underlying file.
419 void
420 read(off_t start, section_size_type size, void* p)
421 { this->input_file()->file().read(start + this->offset_, size, p); }
423 // Read multiple data from the underlying file.
424 void
425 read_multiple(const File_read::Read_multiple& rm)
426 { this->input_file()->file().read_multiple(this->offset_, rm); }
428 // Stop caching views in the underlying file.
429 void
430 clear_view_cache_marks()
431 { this->input_file()->file().clear_view_cache_marks(); }
433 // Get the number of global symbols defined by this object, and the
434 // number of the symbols whose final definition came from this
435 // object.
436 void
437 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
438 size_t* used) const
439 { this->do_get_global_symbol_counts(symtab, defined, used); }
441 // Set the target.
442 void
443 set_target(Target* target)
444 { this->target_ = target; }
446 protected:
447 // Returns NULL for Objects that are not plugin objects. This method
448 // is overridden in the Pluginobj class.
449 virtual Pluginobj*
450 do_pluginobj()
451 { return NULL; }
453 // Read the symbols--implemented by child class.
454 virtual void
455 do_read_symbols(Read_symbols_data*) = 0;
457 // Lay out sections--implemented by child class.
458 virtual void
459 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
461 // Add symbol information to the global symbol table--implemented by
462 // child class.
463 virtual void
464 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
466 // Return the location of the contents of a section. Implemented by
467 // child class.
468 virtual Location
469 do_section_contents(unsigned int shndx) = 0;
471 // Get the size of a section--implemented by child class.
472 virtual uint64_t
473 do_section_size(unsigned int shndx) = 0;
475 // Get the name of a section--implemented by child class.
476 virtual std::string
477 do_section_name(unsigned int shndx) = 0;
479 // Get section flags--implemented by child class.
480 virtual uint64_t
481 do_section_flags(unsigned int shndx) = 0;
483 // Get section address--implemented by child class.
484 virtual uint64_t
485 do_section_address(unsigned int shndx) = 0;
487 // Get section type--implemented by child class.
488 virtual unsigned int
489 do_section_type(unsigned int shndx) = 0;
491 // Get section link field--implemented by child class.
492 virtual unsigned int
493 do_section_link(unsigned int shndx) = 0;
495 // Get section info field--implemented by child class.
496 virtual unsigned int
497 do_section_info(unsigned int shndx) = 0;
499 // Get section alignment--implemented by child class.
500 virtual uint64_t
501 do_section_addralign(unsigned int shndx) = 0;
503 // Return the Xindex structure to use.
504 virtual Xindex*
505 do_initialize_xindex() = 0;
507 // Implement get_global_symbol_counts--implemented by child class.
508 virtual void
509 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
511 // Get the file. We pass on const-ness.
512 Input_file*
513 input_file()
514 { return this->input_file_; }
516 const Input_file*
517 input_file() const
518 { return this->input_file_; }
520 // Set the target.
521 void
522 set_target(int machine, int size, bool big_endian, int osabi,
523 int abiversion);
525 // Set the number of sections.
526 void
527 set_shnum(int shnum)
528 { this->shnum_ = shnum; }
530 // Functions used by both Sized_relobj and Sized_dynobj.
532 // Read the section data into a Read_symbols_data object.
533 template<int size, bool big_endian>
534 void
535 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
536 Read_symbols_data*);
538 // Let the child class initialize the xindex object directly.
539 void
540 set_xindex(Xindex* xindex)
542 gold_assert(this->xindex_ == NULL);
543 this->xindex_ = xindex;
546 // If NAME is the name of a special .gnu.warning section, arrange
547 // for the warning to be issued. SHNDX is the section index.
548 // Return whether it is a warning section.
549 bool
550 handle_gnu_warning_section(const char* name, unsigned int shndx,
551 Symbol_table*);
553 private:
554 // This class may not be copied.
555 Object(const Object&);
556 Object& operator=(const Object&);
558 // Name of object as printed to user.
559 std::string name_;
560 // For reading the file.
561 Input_file* input_file_;
562 // Offset within the file--0 for an object file, non-0 for an
563 // archive.
564 off_t offset_;
565 // Number of input sections.
566 unsigned int shnum_;
567 // Whether this is a dynamic object.
568 bool is_dynamic_;
569 // Target functions--may be NULL if the target is not known.
570 Target* target_;
571 // Many sections for objects with more than SHN_LORESERVE sections.
572 Xindex* xindex_;
575 // Implement sized_target inline for efficiency. This approach breaks
576 // static type checking, but is made safe using asserts.
578 template<int size, bool big_endian>
579 inline Sized_target<size, big_endian>*
580 Object::sized_target() const
582 gold_assert(this->target_->get_size() == size);
583 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
584 return static_cast<Sized_target<size, big_endian>*>(this->target_);
587 // A regular object (ET_REL). This is an abstract base class itself.
588 // The implementation is the template class Sized_relobj.
590 class Relobj : public Object
592 public:
593 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
594 : Object(name, input_file, false, offset),
595 output_sections_(),
596 map_to_relocatable_relocs_(NULL),
597 object_merge_map_(NULL),
598 relocs_must_follow_section_writes_(false)
601 // Read the relocs.
602 void
603 read_relocs(Read_relocs_data* rd)
604 { return this->do_read_relocs(rd); }
606 // Scan the relocs and adjust the symbol table.
607 void
608 scan_relocs(const General_options& options, Symbol_table* symtab,
609 Layout* layout, Read_relocs_data* rd)
610 { return this->do_scan_relocs(options, symtab, layout, rd); }
612 // The number of local symbols in the input symbol table.
613 virtual unsigned int
614 local_symbol_count() const
615 { return this->do_local_symbol_count(); }
617 // Initial local symbol processing: count the number of local symbols
618 // in the output symbol table and dynamic symbol table; add local symbol
619 // names to *POOL and *DYNPOOL.
620 void
621 count_local_symbols(Stringpool_template<char>* pool,
622 Stringpool_template<char>* dynpool)
623 { return this->do_count_local_symbols(pool, dynpool); }
625 // Set the values of the local symbols, set the output symbol table
626 // indexes for the local variables, and set the offset where local
627 // symbol information will be stored. Returns the new local symbol index.
628 unsigned int
629 finalize_local_symbols(unsigned int index, off_t off)
630 { return this->do_finalize_local_symbols(index, off); }
632 // Set the output dynamic symbol table indexes for the local variables.
633 unsigned int
634 set_local_dynsym_indexes(unsigned int index)
635 { return this->do_set_local_dynsym_indexes(index); }
637 // Set the offset where local dynamic symbol information will be stored.
638 unsigned int
639 set_local_dynsym_offset(off_t off)
640 { return this->do_set_local_dynsym_offset(off); }
642 // Relocate the input sections and write out the local symbols.
643 void
644 relocate(const General_options& options, const Symbol_table* symtab,
645 const Layout* layout, Output_file* of)
646 { return this->do_relocate(options, symtab, layout, of); }
648 // Return whether an input section is being included in the link.
649 bool
650 is_section_included(unsigned int shndx) const
652 gold_assert(shndx < this->output_sections_.size());
653 return this->output_sections_[shndx] != NULL;
656 // Given a section index, return the corresponding Output_section.
657 // The return value will be NULL if the section is not included in
658 // the link.
659 Output_section*
660 output_section(unsigned int shndx) const
662 gold_assert(shndx < this->output_sections_.size());
663 return this->output_sections_[shndx];
666 // Given a section index, return the offset in the Output_section.
667 // The return value will be -1U if the section is specially mapped,
668 // such as a merge section.
669 uint64_t
670 output_section_offset(unsigned int shndx) const
671 { return this->do_output_section_offset(shndx); }
673 // Set the offset of an input section within its output section.
674 virtual void
675 set_section_offset(unsigned int shndx, uint64_t off)
676 { this->do_set_section_offset(shndx, off); }
678 // Return true if we need to wait for output sections to be written
679 // before we can apply relocations. This is true if the object has
680 // any relocations for sections which require special handling, such
681 // as the exception frame section.
682 bool
683 relocs_must_follow_section_writes() const
684 { return this->relocs_must_follow_section_writes_; }
686 // Return the object merge map.
687 Object_merge_map*
688 merge_map() const
689 { return this->object_merge_map_; }
691 // Set the object merge map.
692 void
693 set_merge_map(Object_merge_map* object_merge_map)
695 gold_assert(this->object_merge_map_ == NULL);
696 this->object_merge_map_ = object_merge_map;
699 // Record the relocatable reloc info for an input reloc section.
700 void
701 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
703 gold_assert(reloc_shndx < this->shnum());
704 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
707 // Get the relocatable reloc info for an input reloc section.
708 Relocatable_relocs*
709 relocatable_relocs(unsigned int reloc_shndx)
711 gold_assert(reloc_shndx < this->shnum());
712 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
715 protected:
716 // The output section to be used for each input section, indexed by
717 // the input section number. The output section is NULL if the
718 // input section is to be discarded.
719 typedef std::vector<Output_section*> Output_sections;
721 // Read the relocs--implemented by child class.
722 virtual void
723 do_read_relocs(Read_relocs_data*) = 0;
725 // Scan the relocs--implemented by child class.
726 virtual void
727 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
728 Read_relocs_data*) = 0;
730 // Return the number of local symbols--implemented by child class.
731 virtual unsigned int
732 do_local_symbol_count() const = 0;
734 // Count local symbols--implemented by child class.
735 virtual void
736 do_count_local_symbols(Stringpool_template<char>*,
737 Stringpool_template<char>*) = 0;
739 // Finalize the local symbols. Set the output symbol table indexes
740 // for the local variables, and set the offset where local symbol
741 // information will be stored.
742 virtual unsigned int
743 do_finalize_local_symbols(unsigned int, off_t) = 0;
745 // Set the output dynamic symbol table indexes for the local variables.
746 virtual unsigned int
747 do_set_local_dynsym_indexes(unsigned int) = 0;
749 // Set the offset where local dynamic symbol information will be stored.
750 virtual unsigned int
751 do_set_local_dynsym_offset(off_t) = 0;
753 // Relocate the input sections and write out the local
754 // symbols--implemented by child class.
755 virtual void
756 do_relocate(const General_options& options, const Symbol_table* symtab,
757 const Layout*, Output_file* of) = 0;
759 // Get the offset of a section--implemented by child class.
760 virtual uint64_t
761 do_output_section_offset(unsigned int shndx) const = 0;
763 // Set the offset of a section--implemented by child class.
764 virtual void
765 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
767 // Return the vector mapping input sections to output sections.
768 Output_sections&
769 output_sections()
770 { return this->output_sections_; }
772 const Output_sections&
773 output_sections() const
774 { return this->output_sections_; }
776 // Set the size of the relocatable relocs array.
777 void
778 size_relocatable_relocs()
780 this->map_to_relocatable_relocs_ =
781 new std::vector<Relocatable_relocs*>(this->shnum());
784 // Record that we must wait for the output sections to be written
785 // before applying relocations.
786 void
787 set_relocs_must_follow_section_writes()
788 { this->relocs_must_follow_section_writes_ = true; }
790 private:
791 // Mapping from input sections to output section.
792 Output_sections output_sections_;
793 // Mapping from input section index to the information recorded for
794 // the relocations. This is only used for a relocatable link.
795 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
796 // Mappings for merge sections. This is managed by the code in the
797 // Merge_map class.
798 Object_merge_map* object_merge_map_;
799 // Whether we need to wait for output sections to be written before
800 // we can apply relocations.
801 bool relocs_must_follow_section_writes_;
804 // This class is used to handle relocations against a section symbol
805 // in an SHF_MERGE section. For such a symbol, we need to know the
806 // addend of the relocation before we can determine the final value.
807 // The addend gives us the location in the input section, and we can
808 // determine how it is mapped to the output section. For a
809 // non-section symbol, we apply the addend to the final value of the
810 // symbol; that is done in finalize_local_symbols, and does not use
811 // this class.
813 template<int size>
814 class Merged_symbol_value
816 public:
817 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
819 // We use a hash table to map offsets in the input section to output
820 // addresses.
821 typedef Unordered_map<section_offset_type, Value> Output_addresses;
823 Merged_symbol_value(Value input_value, Value output_start_address)
824 : input_value_(input_value), output_start_address_(output_start_address),
825 output_addresses_()
828 // Initialize the hash table.
829 void
830 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
832 // Release the hash table to save space.
833 void
834 free_input_to_output_map()
835 { this->output_addresses_.clear(); }
837 // Get the output value corresponding to an addend. The object and
838 // input section index are passed in because the caller will have
839 // them; otherwise we could store them here.
840 Value
841 value(const Relobj* object, unsigned int input_shndx, Value addend) const
843 // This is a relocation against a section symbol. ADDEND is the
844 // offset in the section. The result should be the start of some
845 // merge area. If the object file wants something else, it should
846 // use a regular symbol rather than a section symbol.
847 // Unfortunately, PR 6658 shows a case in which the object file
848 // refers to the section symbol, but uses a negative ADDEND to
849 // compensate for a PC relative reloc. We can't handle the
850 // general case. However, we can handle the special case of a
851 // negative addend, by assuming that it refers to the start of the
852 // section. Of course, that means that we have to guess when
853 // ADDEND is negative. It is normal to see a 32-bit value here
854 // even when the template parameter size is 64, as 64-bit object
855 // file formats have 32-bit relocations. We know this is a merge
856 // section, so we know it has to fit into memory. So we assume
857 // that we won't see a value larger than a large 32-bit unsigned
858 // value. This will break objects with very very large merge
859 // sections; they probably break in other ways anyhow.
860 Value input_offset = this->input_value_;
861 if (addend < 0xffffff00)
863 input_offset += addend;
864 addend = 0;
866 typename Output_addresses::const_iterator p =
867 this->output_addresses_.find(input_offset);
868 if (p != this->output_addresses_.end())
869 return p->second + addend;
871 return (this->value_from_output_section(object, input_shndx, input_offset)
872 + addend);
875 private:
876 // Get the output value for an input offset if we couldn't find it
877 // in the hash table.
878 Value
879 value_from_output_section(const Relobj*, unsigned int input_shndx,
880 Value input_offset) const;
882 // The value of the section symbol in the input file. This is
883 // normally zero, but could in principle be something else.
884 Value input_value_;
885 // The start address of this merged section in the output file.
886 Value output_start_address_;
887 // A hash table which maps offsets in the input section to output
888 // addresses. This only maps specific offsets, not all offsets.
889 Output_addresses output_addresses_;
892 // This POD class is holds the value of a symbol. This is used for
893 // local symbols, and for all symbols during relocation processing.
894 // For special sections, such as SHF_MERGE sections, this calls a
895 // function to get the final symbol value.
897 template<int size>
898 class Symbol_value
900 public:
901 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
903 Symbol_value()
904 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
905 is_ordinary_shndx_(false), is_section_symbol_(false),
906 is_tls_symbol_(false), has_output_value_(true)
907 { this->u_.value = 0; }
909 // Get the value of this symbol. OBJECT is the object in which this
910 // symbol is defined, and ADDEND is an addend to add to the value.
911 template<bool big_endian>
912 Value
913 value(const Sized_relobj<size, big_endian>* object, Value addend) const
915 if (this->has_output_value_)
916 return this->u_.value + addend;
917 else
919 gold_assert(this->is_ordinary_shndx_);
920 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
921 addend);
925 // Set the value of this symbol in the output symbol table.
926 void
927 set_output_value(Value value)
928 { this->u_.value = value; }
930 // For a section symbol in a merged section, we need more
931 // information.
932 void
933 set_merged_symbol_value(Merged_symbol_value<size>* msv)
935 gold_assert(this->is_section_symbol_);
936 this->has_output_value_ = false;
937 this->u_.merged_symbol_value = msv;
940 // Initialize the input to output map for a section symbol in a
941 // merged section. We also initialize the value of a non-section
942 // symbol in a merged section.
943 void
944 initialize_input_to_output_map(const Relobj* object)
946 if (!this->has_output_value_)
948 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
949 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
950 msv->initialize_input_to_output_map(object, this->input_shndx_);
954 // Free the input to output map for a section symbol in a merged
955 // section.
956 void
957 free_input_to_output_map()
959 if (!this->has_output_value_)
960 this->u_.merged_symbol_value->free_input_to_output_map();
963 // Set the value of the symbol from the input file. This is only
964 // called by count_local_symbols, to communicate the value to
965 // finalize_local_symbols.
966 void
967 set_input_value(Value value)
968 { this->u_.value = value; }
970 // Return the input value. This is only called by
971 // finalize_local_symbols and (in special cases) relocate_section.
972 Value
973 input_value() const
974 { return this->u_.value; }
976 // Return whether this symbol should go into the output symbol
977 // table.
978 bool
979 needs_output_symtab_entry() const
980 { return this->output_symtab_index_ != -1U; }
982 // Return the index in the output symbol table.
983 unsigned int
984 output_symtab_index() const
986 gold_assert(this->output_symtab_index_ != 0);
987 return this->output_symtab_index_;
990 // Set the index in the output symbol table.
991 void
992 set_output_symtab_index(unsigned int i)
994 gold_assert(this->output_symtab_index_ == 0);
995 this->output_symtab_index_ = i;
998 // Record that this symbol should not go into the output symbol
999 // table.
1000 void
1001 set_no_output_symtab_entry()
1003 gold_assert(this->output_symtab_index_ == 0);
1004 this->output_symtab_index_ = -1U;
1007 // Set the index in the output dynamic symbol table.
1008 void
1009 set_needs_output_dynsym_entry()
1011 gold_assert(!this->is_section_symbol());
1012 this->output_dynsym_index_ = 0;
1015 // Return whether this symbol should go into the output symbol
1016 // table.
1017 bool
1018 needs_output_dynsym_entry() const
1020 return this->output_dynsym_index_ != -1U;
1023 // Record that this symbol should go into the dynamic symbol table.
1024 void
1025 set_output_dynsym_index(unsigned int i)
1027 gold_assert(this->output_dynsym_index_ == 0);
1028 this->output_dynsym_index_ = i;
1031 // Return the index in the output dynamic symbol table.
1032 unsigned int
1033 output_dynsym_index() const
1035 gold_assert(this->output_dynsym_index_ != 0
1036 && this->output_dynsym_index_ != -1U);
1037 return this->output_dynsym_index_;
1040 // Set the index of the input section in the input file.
1041 void
1042 set_input_shndx(unsigned int i, bool is_ordinary)
1044 this->input_shndx_ = i;
1045 // input_shndx_ field is a bitfield, so make sure that the value
1046 // fits.
1047 gold_assert(this->input_shndx_ == i);
1048 this->is_ordinary_shndx_ = is_ordinary;
1051 // Return the index of the input section in the input file.
1052 unsigned int
1053 input_shndx(bool* is_ordinary) const
1055 *is_ordinary = this->is_ordinary_shndx_;
1056 return this->input_shndx_;
1059 // Whether this is a section symbol.
1060 bool
1061 is_section_symbol() const
1062 { return this->is_section_symbol_; }
1064 // Record that this is a section symbol.
1065 void
1066 set_is_section_symbol()
1068 gold_assert(!this->needs_output_dynsym_entry());
1069 this->is_section_symbol_ = true;
1072 // Record that this is a TLS symbol.
1073 void
1074 set_is_tls_symbol()
1075 { this->is_tls_symbol_ = true; }
1077 // Return TRUE if this is a TLS symbol.
1078 bool
1079 is_tls_symbol() const
1080 { return this->is_tls_symbol_; }
1082 private:
1083 // The index of this local symbol in the output symbol table. This
1084 // will be -1 if the symbol should not go into the symbol table.
1085 unsigned int output_symtab_index_;
1086 // The index of this local symbol in the dynamic symbol table. This
1087 // will be -1 if the symbol should not go into the symbol table.
1088 unsigned int output_dynsym_index_;
1089 // The section index in the input file in which this symbol is
1090 // defined.
1091 unsigned int input_shndx_ : 28;
1092 // Whether the section index is an ordinary index, not a special
1093 // value.
1094 bool is_ordinary_shndx_ : 1;
1095 // Whether this is a STT_SECTION symbol.
1096 bool is_section_symbol_ : 1;
1097 // Whether this is a STT_TLS symbol.
1098 bool is_tls_symbol_ : 1;
1099 // Whether this symbol has a value for the output file. This is
1100 // normally set to true during Layout::finalize, by
1101 // finalize_local_symbols. It will be false for a section symbol in
1102 // a merge section, as for such symbols we can not determine the
1103 // value to use in a relocation until we see the addend.
1104 bool has_output_value_ : 1;
1105 union
1107 // This is used if has_output_value_ is true. Between
1108 // count_local_symbols and finalize_local_symbols, this is the
1109 // value in the input file. After finalize_local_symbols, it is
1110 // the value in the output file.
1111 Value value;
1112 // This is used if has_output_value_ is false. It points to the
1113 // information we need to get the value for a merge section.
1114 Merged_symbol_value<size>* merged_symbol_value;
1115 } u_;
1118 // A GOT offset list. A symbol may have more than one GOT offset
1119 // (e.g., when mixing modules compiled with two different TLS models),
1120 // but will usually have at most one. GOT_TYPE identifies the type of
1121 // GOT entry; its values are specific to each target.
1123 class Got_offset_list
1125 public:
1126 Got_offset_list()
1127 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1130 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1131 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1134 ~Got_offset_list()
1136 if (this->got_next_ != NULL)
1138 delete this->got_next_;
1139 this->got_next_ = NULL;
1143 // Initialize the fields to their default values.
1144 void
1145 init()
1147 this->got_type_ = -1U;
1148 this->got_offset_ = 0;
1149 this->got_next_ = NULL;
1152 // Set the offset for the GOT entry of type GOT_TYPE.
1153 void
1154 set_offset(unsigned int got_type, unsigned int got_offset)
1156 if (this->got_type_ == -1U)
1158 this->got_type_ = got_type;
1159 this->got_offset_ = got_offset;
1161 else
1163 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1165 if (g->got_type_ == got_type)
1167 g->got_offset_ = got_offset;
1168 return;
1171 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1172 g->got_next_ = this->got_next_;
1173 this->got_next_ = g;
1177 // Return the offset for a GOT entry of type GOT_TYPE.
1178 unsigned int
1179 get_offset(unsigned int got_type) const
1181 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1183 if (g->got_type_ == got_type)
1184 return g->got_offset_;
1186 return -1U;
1189 private:
1190 unsigned int got_type_;
1191 unsigned int got_offset_;
1192 Got_offset_list* got_next_;
1195 // A regular object file. This is size and endian specific.
1197 template<int size, bool big_endian>
1198 class Sized_relobj : public Relobj
1200 public:
1201 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1202 typedef std::vector<Symbol*> Symbols;
1203 typedef std::vector<Symbol_value<size> > Local_values;
1205 static const Address invalid_address = static_cast<Address>(0) - 1;
1207 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1208 const typename elfcpp::Ehdr<size, big_endian>&);
1210 ~Sized_relobj();
1212 // Set up the object file based on the ELF header.
1213 void
1214 setup(const typename elfcpp::Ehdr<size, big_endian>&);
1216 // Return the number of symbols. This is only valid after
1217 // Object::add_symbols has been called.
1218 unsigned int
1219 symbol_count() const
1220 { return this->local_symbol_count_ + this->symbols_.size(); }
1222 // If SYM is the index of a global symbol in the object file's
1223 // symbol table, return the Symbol object. Otherwise, return NULL.
1224 Symbol*
1225 global_symbol(unsigned int sym) const
1227 if (sym >= this->local_symbol_count_)
1229 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1230 return this->symbols_[sym - this->local_symbol_count_];
1232 return NULL;
1235 // Return the section index of symbol SYM. Set *VALUE to its value
1236 // in the object file. Set *IS_ORDINARY if this is an ordinary
1237 // section index, not a special code between SHN_LORESERVE and
1238 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1239 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1240 // it will not return the final value of the symbol in the link.
1241 unsigned int
1242 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1244 // Return a pointer to the Symbol_value structure which holds the
1245 // value of a local symbol.
1246 const Symbol_value<size>*
1247 local_symbol(unsigned int sym) const
1249 gold_assert(sym < this->local_values_.size());
1250 return &this->local_values_[sym];
1253 // Return the index of local symbol SYM in the ordinary symbol
1254 // table. A value of -1U means that the symbol is not being output.
1255 unsigned int
1256 symtab_index(unsigned int sym) const
1258 gold_assert(sym < this->local_values_.size());
1259 return this->local_values_[sym].output_symtab_index();
1262 // Return the index of local symbol SYM in the dynamic symbol
1263 // table. A value of -1U means that the symbol is not being output.
1264 unsigned int
1265 dynsym_index(unsigned int sym) const
1267 gold_assert(sym < this->local_values_.size());
1268 return this->local_values_[sym].output_dynsym_index();
1271 // Return the input section index of local symbol SYM.
1272 unsigned int
1273 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1275 gold_assert(sym < this->local_values_.size());
1276 return this->local_values_[sym].input_shndx(is_ordinary);
1279 // Return the appropriate Sized_target structure.
1280 Sized_target<size, big_endian>*
1281 sized_target()
1282 { return this->Object::sized_target<size, big_endian>(); }
1284 // Record that local symbol SYM needs a dynamic symbol entry.
1285 void
1286 set_needs_output_dynsym_entry(unsigned int sym)
1288 gold_assert(sym < this->local_values_.size());
1289 this->local_values_[sym].set_needs_output_dynsym_entry();
1292 // Return whether the local symbol SYMNDX has a GOT offset.
1293 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1294 bool
1295 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1297 Local_got_offsets::const_iterator p =
1298 this->local_got_offsets_.find(symndx);
1299 return (p != this->local_got_offsets_.end()
1300 && p->second->get_offset(got_type) != -1U);
1303 // Return the GOT offset of the local symbol SYMNDX.
1304 unsigned int
1305 local_got_offset(unsigned int symndx, unsigned int got_type) const
1307 Local_got_offsets::const_iterator p =
1308 this->local_got_offsets_.find(symndx);
1309 gold_assert(p != this->local_got_offsets_.end());
1310 unsigned int off = p->second->get_offset(got_type);
1311 gold_assert(off != -1U);
1312 return off;
1315 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1316 void
1317 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1318 unsigned int got_offset)
1320 Local_got_offsets::const_iterator p =
1321 this->local_got_offsets_.find(symndx);
1322 if (p != this->local_got_offsets_.end())
1323 p->second->set_offset(got_type, got_offset);
1324 else
1326 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1327 std::pair<Local_got_offsets::iterator, bool> ins =
1328 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1329 gold_assert(ins.second);
1333 // Get the offset of input section SHNDX within its output section.
1334 // This is -1 if the input section requires a special mapping, such
1335 // as a merge section. The output section can be found in the
1336 // output_sections_ field of the parent class Relobj.
1337 Address
1338 get_output_section_offset(unsigned int shndx) const
1340 gold_assert(shndx < this->section_offsets_.size());
1341 return this->section_offsets_[shndx];
1344 // Return the name of the symbol that spans the given offset in the
1345 // specified section in this object. This is used only for error
1346 // messages and is not particularly efficient.
1347 bool
1348 get_symbol_location_info(unsigned int shndx, off_t offset,
1349 Symbol_location_info* info);
1351 // Look for a kept section corresponding to the given discarded section,
1352 // and return its output address. This is used only for relocations in
1353 // debugging sections.
1354 Address
1355 map_to_kept_section(unsigned int shndx, bool* found) const;
1357 protected:
1358 // Read the symbols.
1359 void
1360 do_read_symbols(Read_symbols_data*);
1362 // Return the number of local symbols.
1363 unsigned int
1364 do_local_symbol_count() const
1365 { return this->local_symbol_count_; }
1367 // Lay out the input sections.
1368 void
1369 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1371 // Add the symbols to the symbol table.
1372 void
1373 do_add_symbols(Symbol_table*, Read_symbols_data*);
1375 // Read the relocs.
1376 void
1377 do_read_relocs(Read_relocs_data*);
1379 // Scan the relocs and adjust the symbol table.
1380 void
1381 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1382 Read_relocs_data*);
1384 // Count the local symbols.
1385 void
1386 do_count_local_symbols(Stringpool_template<char>*,
1387 Stringpool_template<char>*);
1389 // Finalize the local symbols.
1390 unsigned int
1391 do_finalize_local_symbols(unsigned int, off_t);
1393 // Set the offset where local dynamic symbol information will be stored.
1394 unsigned int
1395 do_set_local_dynsym_indexes(unsigned int);
1397 // Set the offset where local dynamic symbol information will be stored.
1398 unsigned int
1399 do_set_local_dynsym_offset(off_t);
1401 // Relocate the input sections and write out the local symbols.
1402 void
1403 do_relocate(const General_options& options, const Symbol_table* symtab,
1404 const Layout*, Output_file* of);
1406 // Get the size of a section.
1407 uint64_t
1408 do_section_size(unsigned int shndx)
1409 { return this->elf_file_.section_size(shndx); }
1411 // Get the name of a section.
1412 std::string
1413 do_section_name(unsigned int shndx)
1414 { return this->elf_file_.section_name(shndx); }
1416 // Return the location of the contents of a section.
1417 Object::Location
1418 do_section_contents(unsigned int shndx)
1419 { return this->elf_file_.section_contents(shndx); }
1421 // Return section flags.
1422 uint64_t
1423 do_section_flags(unsigned int shndx)
1424 { return this->elf_file_.section_flags(shndx); }
1426 // Return section address.
1427 uint64_t
1428 do_section_address(unsigned int shndx)
1429 { return this->elf_file_.section_addr(shndx); }
1431 // Return section type.
1432 unsigned int
1433 do_section_type(unsigned int shndx)
1434 { return this->elf_file_.section_type(shndx); }
1436 // Return the section link field.
1437 unsigned int
1438 do_section_link(unsigned int shndx)
1439 { return this->elf_file_.section_link(shndx); }
1441 // Return the section info field.
1442 unsigned int
1443 do_section_info(unsigned int shndx)
1444 { return this->elf_file_.section_info(shndx); }
1446 // Return the section alignment.
1447 uint64_t
1448 do_section_addralign(unsigned int shndx)
1449 { return this->elf_file_.section_addralign(shndx); }
1451 // Return the Xindex structure to use.
1452 Xindex*
1453 do_initialize_xindex();
1455 // Get symbol counts.
1456 void
1457 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1459 // Get the offset of a section.
1460 uint64_t
1461 do_output_section_offset(unsigned int shndx) const
1463 Address off = this->get_output_section_offset(shndx);
1464 if (off == invalid_address)
1465 return -1ULL;
1466 return off;
1469 // Set the offset of a section.
1470 void
1471 do_set_section_offset(unsigned int shndx, uint64_t off)
1473 gold_assert(shndx < this->section_offsets_.size());
1474 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1477 private:
1478 // For convenience.
1479 typedef Sized_relobj<size, big_endian> This;
1480 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1481 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1482 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1483 typedef elfcpp::Shdr<size, big_endian> Shdr;
1485 // To keep track of discarded comdat sections, we need to map a member
1486 // section index to the object and section index of the corresponding
1487 // kept section.
1488 struct Kept_comdat_section
1490 Kept_comdat_section(Sized_relobj<size, big_endian>* object,
1491 unsigned int shndx)
1492 : object_(object), shndx_(shndx)
1494 Sized_relobj<size, big_endian>* object_;
1495 unsigned int shndx_;
1497 typedef std::map<unsigned int, Kept_comdat_section*>
1498 Kept_comdat_section_table;
1500 // Information needed to keep track of kept comdat groups. This is
1501 // simply a map from the section name to its section index. This may
1502 // not be a one-to-one mapping, but we ignore that possibility since
1503 // this is used only to attempt to handle stray relocations from
1504 // non-comdat debug sections that refer to comdat loadable sections.
1505 typedef Unordered_map<std::string, unsigned int> Comdat_group;
1507 // A map from group section index to the table of group members.
1508 typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
1510 // Find a comdat group table given its group section SHNDX.
1511 Comdat_group*
1512 find_comdat_group(unsigned int shndx) const
1514 Comdat_group_table::const_iterator p =
1515 this->comdat_groups_.find(shndx);
1516 if (p != this->comdat_groups_.end())
1517 return p->second;
1518 return NULL;
1521 // Record a new comdat group whose group section index is SHNDX.
1522 void
1523 add_comdat_group(unsigned int shndx, Comdat_group* group)
1524 { this->comdat_groups_[shndx] = group; }
1526 // Adjust a section index if necessary.
1527 unsigned int
1528 adjust_shndx(unsigned int shndx)
1530 if (shndx >= elfcpp::SHN_LORESERVE)
1531 shndx += this->elf_file_.large_shndx_offset();
1532 return shndx;
1535 // Find the SHT_SYMTAB section, given the section headers.
1536 void
1537 find_symtab(const unsigned char* pshdrs);
1539 // Return whether SHDR has the right flags for a GNU style exception
1540 // frame section.
1541 bool
1542 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1544 // Return whether there is a section named .eh_frame which might be
1545 // a GNU style exception frame section.
1546 bool
1547 find_eh_frame(const unsigned char* pshdrs, const char* names,
1548 section_size_type names_size) const;
1550 // Whether to include a section group in the link.
1551 bool
1552 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1553 const unsigned char*, const char *, section_size_type,
1554 std::vector<bool>*);
1556 // Whether to include a linkonce section in the link.
1557 bool
1558 include_linkonce_section(Layout*, unsigned int, const char*,
1559 const elfcpp::Shdr<size, big_endian>&);
1561 // Views and sizes when relocating.
1562 struct View_size
1564 unsigned char* view;
1565 typename elfcpp::Elf_types<size>::Elf_Addr address;
1566 off_t offset;
1567 section_size_type view_size;
1568 bool is_input_output_view;
1569 bool is_postprocessing_view;
1572 typedef std::vector<View_size> Views;
1574 // Write section data to the output file. Record the views and
1575 // sizes in VIEWS for use when relocating.
1576 void
1577 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1579 // Relocate the sections in the output file.
1580 void
1581 relocate_sections(const General_options& options, const Symbol_table*,
1582 const Layout*, const unsigned char* pshdrs, Views*);
1584 // Scan the input relocations for --emit-relocs.
1585 void
1586 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1587 const unsigned char* plocal_syms,
1588 const Read_relocs_data::Relocs_list::iterator&);
1590 // Scan the input relocations for --emit-relocs, templatized on the
1591 // type of the relocation section.
1592 template<int sh_type>
1593 void
1594 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1595 const unsigned char* plocal_syms,
1596 const Read_relocs_data::Relocs_list::iterator&,
1597 Relocatable_relocs*);
1599 // Emit the relocs for --emit-relocs.
1600 void
1601 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1602 unsigned int sh_type, const unsigned char* prelocs,
1603 size_t reloc_count, Output_section*, Address output_offset,
1604 unsigned char* view, Address address,
1605 section_size_type view_size,
1606 unsigned char* reloc_view, section_size_type reloc_view_size);
1608 // Emit the relocs for --emit-relocs, templatized on the type of the
1609 // relocation section.
1610 template<int sh_type>
1611 void
1612 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1613 const unsigned char* prelocs, size_t reloc_count,
1614 Output_section*, Address output_offset,
1615 unsigned char* view, Address address,
1616 section_size_type view_size,
1617 unsigned char* reloc_view,
1618 section_size_type reloc_view_size);
1620 // Initialize input to output maps for section symbols in merged
1621 // sections.
1622 void
1623 initialize_input_to_output_maps();
1625 // Free the input to output maps for section symbols in merged
1626 // sections.
1627 void
1628 free_input_to_output_maps();
1630 // Write out the local symbols.
1631 void
1632 write_local_symbols(Output_file*,
1633 const Stringpool_template<char>*,
1634 const Stringpool_template<char>*,
1635 Output_symtab_xindex*,
1636 Output_symtab_xindex*);
1638 // Clear the local symbol information.
1639 void
1640 clear_local_symbols()
1642 this->local_values_.clear();
1643 this->local_got_offsets_.clear();
1646 // Record a mapping from discarded section SHNDX to the corresponding
1647 // kept section.
1648 void
1649 set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
1651 this->kept_comdat_sections_[shndx] = kept;
1654 // Find the kept section corresponding to the discarded section SHNDX.
1655 Kept_comdat_section*
1656 get_kept_comdat_section(unsigned int shndx) const
1658 typename Kept_comdat_section_table::const_iterator p =
1659 this->kept_comdat_sections_.find(shndx);
1660 if (p == this->kept_comdat_sections_.end())
1661 return NULL;
1662 return p->second;
1665 // The GOT offsets of local symbols. This map also stores GOT offsets
1666 // for tp-relative offsets for TLS symbols.
1667 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1669 // The TLS GOT offsets of local symbols. The map stores the offsets
1670 // for either a single GOT entry that holds the module index of a TLS
1671 // symbol, or a pair of GOT entries containing the module index and
1672 // dtv-relative offset.
1673 struct Tls_got_entry
1675 Tls_got_entry(int got_offset, bool have_pair)
1676 : got_offset_(got_offset),
1677 have_pair_(have_pair)
1679 int got_offset_;
1680 bool have_pair_;
1682 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1684 // General access to the ELF file.
1685 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1686 // Index of SHT_SYMTAB section.
1687 unsigned int symtab_shndx_;
1688 // The number of local symbols.
1689 unsigned int local_symbol_count_;
1690 // The number of local symbols which go into the output file.
1691 unsigned int output_local_symbol_count_;
1692 // The number of local symbols which go into the output file's dynamic
1693 // symbol table.
1694 unsigned int output_local_dynsym_count_;
1695 // The entries in the symbol table for the external symbols.
1696 Symbols symbols_;
1697 // Number of symbols defined in object file itself.
1698 size_t defined_count_;
1699 // File offset for local symbols.
1700 off_t local_symbol_offset_;
1701 // File offset for local dynamic symbols.
1702 off_t local_dynsym_offset_;
1703 // Values of local symbols.
1704 Local_values local_values_;
1705 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1706 // for TLS symbols, indexed by symbol number.
1707 Local_got_offsets local_got_offsets_;
1708 // For each input section, the offset of the input section in its
1709 // output section. This is INVALID_ADDRESS if the input section requires a
1710 // special mapping.
1711 std::vector<Address> section_offsets_;
1712 // Table mapping discarded comdat sections to corresponding kept sections.
1713 Kept_comdat_section_table kept_comdat_sections_;
1714 // Table of kept comdat groups.
1715 Comdat_group_table comdat_groups_;
1716 // Whether this object has a GNU style .eh_frame section.
1717 bool has_eh_frame_;
1720 // A class to manage the list of all objects.
1722 class Input_objects
1724 public:
1725 Input_objects()
1726 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_(),
1727 cref_(NULL)
1730 // The type of the list of input relocateable objects.
1731 typedef std::vector<Relobj*> Relobj_list;
1732 typedef Relobj_list::const_iterator Relobj_iterator;
1734 // The type of the list of input dynamic objects.
1735 typedef std::vector<Dynobj*> Dynobj_list;
1736 typedef Dynobj_list::const_iterator Dynobj_iterator;
1738 // Add an object to the list. Return true if all is well, or false
1739 // if this object should be ignored.
1740 bool
1741 add_object(Object*);
1743 // Start processing an archive.
1744 void
1745 archive_start(Archive*);
1747 // Stop processing an archive.
1748 void
1749 archive_stop(Archive*);
1751 // For each dynamic object, check whether we've seen all of its
1752 // explicit dependencies.
1753 void
1754 check_dynamic_dependencies() const;
1756 // Return whether an object was found in the system library
1757 // directory.
1758 bool
1759 found_in_system_library_directory(const Object*) const;
1761 // Print symbol counts.
1762 void
1763 print_symbol_counts(const Symbol_table*) const;
1765 // Iterate over all regular objects.
1767 Relobj_iterator
1768 relobj_begin() const
1769 { return this->relobj_list_.begin(); }
1771 Relobj_iterator
1772 relobj_end() const
1773 { return this->relobj_list_.end(); }
1775 // Iterate over all dynamic objects.
1777 Dynobj_iterator
1778 dynobj_begin() const
1779 { return this->dynobj_list_.begin(); }
1781 Dynobj_iterator
1782 dynobj_end() const
1783 { return this->dynobj_list_.end(); }
1785 // Return whether we have seen any dynamic objects.
1786 bool
1787 any_dynamic() const
1788 { return !this->dynobj_list_.empty(); }
1790 // Return the number of input objects.
1792 number_of_input_objects() const
1793 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1795 private:
1796 Input_objects(const Input_objects&);
1797 Input_objects& operator=(const Input_objects&);
1799 // The list of ordinary objects included in the link.
1800 Relobj_list relobj_list_;
1801 // The list of dynamic objects included in the link.
1802 Dynobj_list dynobj_list_;
1803 // SONAMEs that we have seen.
1804 Unordered_set<std::string> sonames_;
1805 // The directory in which we find the libc.so.
1806 std::string system_library_directory_;
1807 // Manage cross-references if requested.
1808 Cref* cref_;
1811 // Some of the information we pass to the relocation routines. We
1812 // group this together to avoid passing a dozen different arguments.
1814 template<int size, bool big_endian>
1815 struct Relocate_info
1817 // Command line options.
1818 const General_options* options;
1819 // Symbol table.
1820 const Symbol_table* symtab;
1821 // Layout.
1822 const Layout* layout;
1823 // Object being relocated.
1824 Sized_relobj<size, big_endian>* object;
1825 // Section index of relocation section.
1826 unsigned int reloc_shndx;
1827 // Section index of section being relocated.
1828 unsigned int data_shndx;
1830 // Return a string showing the location of a relocation. This is
1831 // only used for error messages.
1832 std::string
1833 location(size_t relnum, off_t reloffset) const;
1836 // Return an Object appropriate for the input file. P is BYTES long,
1837 // and holds the ELF header.
1839 extern Object*
1840 make_elf_object(const std::string& name, Input_file*,
1841 off_t offset, const unsigned char* p,
1842 section_offset_type bytes);
1844 } // end namespace gold
1846 #endif // !defined(GOLD_OBJECT_H)