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