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