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