OSDN Git Service

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