Speed up relocations against local symbols in merged sections.
[external/binutils.git] / gold / object.h
1 // object.h -- support for an object file for linking in gold  -*- C++ -*-
2
3 // Copyright 2006, 2007 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 Layout;
40 class Output_section;
41 class Output_file;
42 class Dynobj;
43 class Object_merge_map;
44
45 template<typename Stringpool_char>
46 class Stringpool_template;
47
48 // Data to pass from read_symbols() to add_symbols().
49
50 struct Read_symbols_data
51 {
52   // Section headers.
53   File_view* section_headers;
54   // Section names.
55   File_view* section_names;
56   // Size of section name data in bytes.
57   section_size_type section_names_size;
58   // Symbol data.
59   File_view* symbols;
60   // Size of symbol data in bytes.
61   section_size_type symbols_size;
62   // Offset of external symbols within symbol data.  This structure
63   // sometimes contains only external symbols, in which case this will
64   // be zero.  Sometimes it contains all symbols.
65   section_offset_type external_symbols_offset;
66   // Symbol names.
67   File_view* symbol_names;
68   // Size of symbol name data in bytes.
69   section_size_type symbol_names_size;
70
71   // Version information.  This is only used on dynamic objects.
72   // Version symbol data (from SHT_GNU_versym section).
73   File_view* versym;
74   section_size_type versym_size;
75   // Version definition data (from SHT_GNU_verdef section).
76   File_view* verdef;
77   section_size_type verdef_size;
78   unsigned int verdef_info;
79   // Needed version data  (from SHT_GNU_verneed section).
80   File_view* verneed;
81   section_size_type verneed_size;
82   unsigned int verneed_info;
83 };
84
85 // Information used to print error messages.
86
87 struct Symbol_location_info
88 {
89   std::string source_file;
90   std::string enclosing_symbol_name;
91   int line_number;
92 };
93
94 // Data about a single relocation section.  This is read in
95 // read_relocs and processed in scan_relocs.
96
97 struct Section_relocs
98 {
99   // Index of reloc section.
100   unsigned int reloc_shndx;
101   // Index of section that relocs apply to.
102   unsigned int data_shndx;
103   // Contents of reloc section.
104   File_view* contents;
105   // Reloc section type.
106   unsigned int sh_type;
107   // Number of reloc entries.
108   size_t reloc_count;
109   // Output section.
110   Output_section* output_section;
111   // Whether this section has special handling for offsets.
112   bool needs_special_offset_handling;
113 };
114
115 // Relocations in an object file.  This is read in read_relocs and
116 // processed in scan_relocs.
117
118 struct Read_relocs_data
119 {
120   typedef std::vector<Section_relocs> Relocs_list;
121   // The relocations.
122   Relocs_list relocs;
123   // The local symbols.
124   File_view* local_symbols;
125 };
126
127 // Object is an abstract base class which represents either a 32-bit
128 // or a 64-bit input object.  This can be a regular object file
129 // (ET_REL) or a shared object (ET_DYN).
130
131 class Object
132 {
133  public:
134   // NAME is the name of the object as we would report it to the user
135   // (e.g., libfoo.a(bar.o) if this is in an archive.  INPUT_FILE is
136   // used to read the file.  OFFSET is the offset within the input
137   // file--0 for a .o or .so file, something else for a .a file.
138   Object(const std::string& name, Input_file* input_file, bool is_dynamic,
139          off_t offset = 0)
140     : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
141       is_dynamic_(is_dynamic), target_(NULL)
142   { }
143
144   virtual ~Object()
145   { }
146
147   // Return the name of the object as we would report it to the tuser.
148   const std::string&
149   name() const
150   { return this->name_; }
151
152   // Get the offset into the file.
153   off_t
154   offset() const
155   { return this->offset_; }
156
157   // Return whether this is a dynamic object.
158   bool
159   is_dynamic() const
160   { return this->is_dynamic_; }
161
162   // Return the target structure associated with this object.
163   Target*
164   target() const
165   { return this->target_; }
166
167   // Lock the underlying file.
168   void
169   lock(const Task* t)
170   { this->input_file()->file().lock(t); }
171
172   // Unlock the underlying file.
173   void
174   unlock(const Task* t)
175   { this->input_file()->file().unlock(t); }
176
177   // Return whether the underlying file is locked.
178   bool
179   is_locked() const
180   { return this->input_file()->file().is_locked(); }
181
182   // Return the token, so that the task can be queued.
183   Task_token*
184   token()
185   { return this->input_file()->file().token(); }
186
187   // Release the underlying file.
188   void
189   release()
190   { this->input_file_->file().release(); }
191
192   // Return the sized target structure associated with this object.
193   // This is like the target method but it returns a pointer of
194   // appropriate checked type.
195   template<int size, bool big_endian>
196   Sized_target<size, big_endian>*
197   sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const;
198
199   // Get the number of sections.
200   unsigned int
201   shnum() const
202   { return this->shnum_; }
203
204   // Return a view of the contents of a section.  Set *PLEN to the
205   // size.  CACHE is a hint as in File_read::get_view.
206   const unsigned char*
207   section_contents(unsigned int shndx, section_size_type* plen, bool cache);
208
209   // Return the name of a section given a section index.  This is only
210   // used for error messages.
211   std::string
212   section_name(unsigned int shndx)
213   { return this->do_section_name(shndx); }
214
215   // Return the section flags given a section index.
216   uint64_t
217   section_flags(unsigned int shndx)
218   { return this->do_section_flags(shndx); }
219
220   // Return the section type given a section index.
221   unsigned int
222   section_type(unsigned int shndx)
223   { return this->do_section_type(shndx); }
224
225   // Return the section link field given a section index.
226   unsigned int
227   section_link(unsigned int shndx)
228   { return this->do_section_link(shndx); }
229
230   // Return the section info field given a section index.
231   unsigned int
232   section_info(unsigned int shndx)
233   { return this->do_section_info(shndx); }
234
235   // Read the symbol information.
236   void
237   read_symbols(Read_symbols_data* sd)
238   { return this->do_read_symbols(sd); }
239
240   // Pass sections which should be included in the link to the Layout
241   // object, and record where the sections go in the output file.
242   void
243   layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
244   { this->do_layout(symtab, layout, sd); }
245
246   // Add symbol information to the global symbol table.
247   void
248   add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
249   { this->do_add_symbols(symtab, sd); }
250
251   // Functions and types for the elfcpp::Elf_file interface.  This
252   // permit us to use Object as the File template parameter for
253   // elfcpp::Elf_file.
254
255   // The View class is returned by view.  It must support a single
256   // method, data().  This is trivial, because get_view does what we
257   // need.
258   class View
259   {
260    public:
261     View(const unsigned char* p)
262       : p_(p)
263     { }
264
265     const unsigned char*
266     data() const
267     { return this->p_; }
268
269    private:
270     const unsigned char* p_;
271   };
272
273   // Return a View.
274   View
275   view(off_t file_offset, section_size_type data_size)
276   { return View(this->get_view(file_offset, data_size, true)); }
277
278   // Report an error.
279   void
280   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
281
282   // A location in the file.
283   struct Location
284   {
285     off_t file_offset;
286     off_t data_size;
287
288     Location(off_t fo, section_size_type ds)
289       : file_offset(fo), data_size(ds)
290     { }
291   };
292
293   // Get a View given a Location.
294   View view(Location loc)
295   { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
296
297  protected:
298   // Read the symbols--implemented by child class.
299   virtual void
300   do_read_symbols(Read_symbols_data*) = 0;
301
302   // Lay out sections--implemented by child class.
303   virtual void
304   do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
305
306   // Add symbol information to the global symbol table--implemented by
307   // child class.
308   virtual void
309   do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
310
311   // Return the location of the contents of a section.  Implemented by
312   // child class.
313   virtual Location
314   do_section_contents(unsigned int shndx) = 0;
315
316   // Get the name of a section--implemented by child class.
317   virtual std::string
318   do_section_name(unsigned int shndx) = 0;
319
320   // Get section flags--implemented by child class.
321   virtual uint64_t
322   do_section_flags(unsigned int shndx) = 0;
323
324   // Get section type--implemented by child class.
325   virtual unsigned int
326   do_section_type(unsigned int shndx) = 0;
327
328   // Get section link field--implemented by child class.
329   virtual unsigned int
330   do_section_link(unsigned int shndx) = 0;
331
332   // Get section info field--implemented by child class.
333   virtual unsigned int
334   do_section_info(unsigned int shndx) = 0;
335
336   // Get the file.  We pass on const-ness.
337   Input_file*
338   input_file()
339   { return this->input_file_; }
340
341   const Input_file*
342   input_file() const
343   { return this->input_file_; }
344
345   // Get a view into the underlying file.
346   const unsigned char*
347   get_view(off_t start, section_size_type size, bool cache)
348   {
349     return this->input_file()->file().get_view(start + this->offset_, size,
350                                                cache);
351   }
352
353   // Get a lasting view into the underlying file.
354   File_view*
355   get_lasting_view(off_t start, section_size_type size, bool cache)
356   {
357     return this->input_file()->file().get_lasting_view(start + this->offset_,
358                                                        size, cache);
359   }
360
361   // Read data from the underlying file.
362   void
363   read(off_t start, section_size_type size, void* p) const
364   { this->input_file()->file().read(start + this->offset_, size, p); }
365
366   // Set the target.
367   void
368   set_target(int machine, int size, bool big_endian, int osabi,
369              int abiversion);
370
371   // Set the number of sections.
372   void
373   set_shnum(int shnum)
374   { this->shnum_ = shnum; }
375
376   // Functions used by both Sized_relobj and Sized_dynobj.
377
378   // Read the section data into a Read_symbols_data object.
379   template<int size, bool big_endian>
380   void
381   read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
382                     Read_symbols_data*);
383
384   // If NAME is the name of a special .gnu.warning section, arrange
385   // for the warning to be issued.  SHNDX is the section index.
386   // Return whether it is a warning section.
387   bool
388   handle_gnu_warning_section(const char* name, unsigned int shndx,
389                              Symbol_table*);
390
391  private:
392   // This class may not be copied.
393   Object(const Object&);
394   Object& operator=(const Object&);
395
396   // Name of object as printed to user.
397   std::string name_;
398   // For reading the file.
399   Input_file* input_file_;
400   // Offset within the file--0 for an object file, non-0 for an
401   // archive.
402   off_t offset_;
403   // Number of input sections.
404   unsigned int shnum_;
405   // Whether this is a dynamic object.
406   bool is_dynamic_;
407   // Target functions--may be NULL if the target is not known.
408   Target* target_;
409 };
410
411 // Implement sized_target inline for efficiency.  This approach breaks
412 // static type checking, but is made safe using asserts.
413
414 template<int size, bool big_endian>
415 inline Sized_target<size, big_endian>*
416 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const
417 {
418   gold_assert(this->target_->get_size() == size);
419   gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
420   return static_cast<Sized_target<size, big_endian>*>(this->target_);
421 }
422
423 // A regular object (ET_REL).  This is an abstract base class itself.
424 // The implementation is the template class Sized_relobj.
425
426 class Relobj : public Object
427 {
428  public:
429   Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
430     : Object(name, input_file, false, offset),
431       map_to_output_(),
432       object_merge_map_(NULL),
433       relocs_must_follow_section_writes_(false)
434   { }
435
436   // Read the relocs.
437   void
438   read_relocs(Read_relocs_data* rd)
439   { return this->do_read_relocs(rd); }
440
441   // Scan the relocs and adjust the symbol table.
442   void
443   scan_relocs(const General_options& options, Symbol_table* symtab,
444               Layout* layout, Read_relocs_data* rd)
445   { return this->do_scan_relocs(options, symtab, layout, rd); }
446
447   // The number of local symbols in the input symbol table.
448   virtual unsigned int
449   local_symbol_count() const
450   { return this->do_local_symbol_count(); }
451
452   // Initial local symbol processing: count the number of local symbols
453   // in the output symbol table and dynamic symbol table; add local symbol
454   // names to *POOL and *DYNPOOL.
455   void
456   count_local_symbols(Stringpool_template<char>* pool,
457                       Stringpool_template<char>* dynpool)
458   { return this->do_count_local_symbols(pool, dynpool); }
459
460   // Set the values of the local symbols, set the output symbol table
461   // indexes for the local variables, and set the offset where local
462   // symbol information will be stored. Returns the new local symbol index.
463   unsigned int
464   finalize_local_symbols(unsigned int index, off_t off)
465   { return this->do_finalize_local_symbols(index, off); }
466
467   // Set the output dynamic symbol table indexes for the local variables.
468   unsigned int
469   set_local_dynsym_indexes(unsigned int index)
470   { return this->do_set_local_dynsym_indexes(index); }
471
472   // Set the offset where local dynamic symbol information will be stored.
473   unsigned int
474   set_local_dynsym_offset(off_t off)
475   { return this->do_set_local_dynsym_offset(off); }
476
477   // Relocate the input sections and write out the local symbols.
478   void
479   relocate(const General_options& options, const Symbol_table* symtab,
480            const Layout* layout, Output_file* of)
481   { return this->do_relocate(options, symtab, layout, of); }
482
483   // Return whether an input section is being included in the link.
484   bool
485   is_section_included(unsigned int shndx) const
486   {
487     gold_assert(shndx < this->map_to_output_.size());
488     return this->map_to_output_[shndx].output_section != NULL;
489   }
490
491   // Return whether an input section requires special
492   // handling--whether it is not simply mapped from the input file to
493   // the output file.
494   bool
495   is_section_specially_mapped(unsigned int shndx) const
496   {
497     gold_assert(shndx < this->map_to_output_.size());
498     return (this->map_to_output_[shndx].output_section != NULL
499             && this->map_to_output_[shndx].offset == -1);
500   }
501
502   // Given a section index, return the corresponding Output_section
503   // (which will be NULL if the section is not included in the link)
504   // and set *POFF to the offset within that section.  *POFF will be
505   // set to -1 if the section requires special handling.
506   inline Output_section*
507   output_section(unsigned int shndx, section_offset_type* poff) const;
508
509   // Set the offset of an input section within its output section.
510   void
511   set_section_offset(unsigned int shndx, section_offset_type off)
512   {
513     gold_assert(shndx < this->map_to_output_.size());
514     this->map_to_output_[shndx].offset = off;
515   }
516
517   // Return true if we need to wait for output sections to be written
518   // before we can apply relocations.  This is true if the object has
519   // any relocations for sections which require special handling, such
520   // as the exception frame section.
521   bool
522   relocs_must_follow_section_writes() const
523   { return this->relocs_must_follow_section_writes_; }
524
525   // Return the object merge map.
526   Object_merge_map*
527   merge_map() const
528   { return this->object_merge_map_; }
529
530   // Set the object merge map.
531   void
532   set_merge_map(Object_merge_map* object_merge_map)
533   {
534     gold_assert(this->object_merge_map_ == NULL);
535     this->object_merge_map_ = object_merge_map;
536   }
537
538  protected:
539   // What we need to know to map an input section to an output
540   // section.  We keep an array of these, one for each input section,
541   // indexed by the input section number.
542   struct Map_to_output
543   {
544     // The output section.  This is NULL if the input section is to be
545     // discarded.
546     Output_section* output_section;
547     // The offset within the output section.  This is -1 if the
548     // section requires special handling.
549     section_offset_type offset;
550   };
551
552   // Read the relocs--implemented by child class.
553   virtual void
554   do_read_relocs(Read_relocs_data*) = 0;
555
556   // Scan the relocs--implemented by child class.
557   virtual void
558   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
559                  Read_relocs_data*) = 0;
560
561   // Return the number of local symbols--implemented by child class.
562   virtual unsigned int
563   do_local_symbol_count() const = 0;
564
565   // Count local symbols--implemented by child class.
566   virtual void
567   do_count_local_symbols(Stringpool_template<char>*,
568                             Stringpool_template<char>*) = 0;
569
570   // Finalize the local symbols.  Set the output symbol table indexes for the local variables, and set the
571   // offset where local symbol information will be stored.
572   virtual unsigned int
573   do_finalize_local_symbols(unsigned int, off_t) = 0;
574
575   // Set the output dynamic symbol table indexes for the local variables.
576   virtual unsigned int
577   do_set_local_dynsym_indexes(unsigned int) = 0;
578
579   // Set the offset where local dynamic symbol information will be stored.
580   virtual unsigned int
581   do_set_local_dynsym_offset(off_t) = 0;
582
583   // Relocate the input sections and write out the local
584   // symbols--implemented by child class.
585   virtual void
586   do_relocate(const General_options& options, const Symbol_table* symtab,
587               const Layout*, Output_file* of) = 0;
588
589   // Return the vector mapping input sections to output sections.
590   std::vector<Map_to_output>&
591   map_to_output()
592   { return this->map_to_output_; }
593
594   const std::vector<Map_to_output>&
595   map_to_output() const
596   { return this->map_to_output_; }
597
598   // Record that we must wait for the output sections to be written
599   // before applying relocations.
600   void
601   set_relocs_must_follow_section_writes()
602   { this->relocs_must_follow_section_writes_ = true; }
603
604  private:
605   // Mapping from input sections to output section.
606   std::vector<Map_to_output> map_to_output_;
607   // Mappings for merge sections.  This is managed by the code in the
608   // Merge_map class.
609   Object_merge_map* object_merge_map_;
610   // Whether we need to wait for output sections to be written before
611   // we can apply relocations.
612   bool relocs_must_follow_section_writes_;
613 };
614
615 // Implement Object::output_section inline for efficiency.
616 inline Output_section*
617 Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
618 {
619   gold_assert(shndx < this->map_to_output_.size());
620   const Map_to_output& mo(this->map_to_output_[shndx]);
621   *poff = mo.offset;
622   return mo.output_section;
623 }
624
625 // This class is used to handle relocations against a section symbol
626 // in an SHF_MERGE section.  For such a symbol, we need to know the
627 // addend of the relocation before we can determine the final value.
628 // The addend gives us the location in the input section, and we can
629 // determine how it is mapped to the output section.  For a
630 // non-section symbol, we apply the addend to the final value of the
631 // symbol; that is done in finalize_local_symbols, and does not use
632 // this class.
633
634 template<int size>
635 class Merged_symbol_value
636 {
637  public:
638   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
639
640   // We use a hash table to map offsets in the input section to output
641   // addresses.
642   typedef Unordered_map<section_offset_type, Value> Output_addresses;
643
644   Merged_symbol_value(Value input_value, Value output_start_address)
645     : input_value_(input_value), output_start_address_(output_start_address),
646       output_addresses_()
647   { }
648
649   // Initialize the hash table.
650   void
651   initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
652
653   // Release the hash table to save space.
654   void
655   free_input_to_output_map()
656   { this->output_addresses_.clear(); }
657
658   // Get the output value corresponding to an addend.  The object and
659   // input section index are passed in because the caller will have
660   // them; otherwise we could store them here.
661   Value
662   value(const Relobj* object, unsigned int input_shndx, Value addend) const
663   {
664     Value input_offset = this->input_value_ + addend;
665     typename Output_addresses::const_iterator p =
666       this->output_addresses_.find(input_offset);
667     if (p != this->output_addresses_.end())
668       return p->second;
669
670     return this->value_from_output_section(object, input_shndx, input_offset);
671   }
672
673  private:
674   // Get the output value for an input offset if we couldn't find it
675   // in the hash table.
676   Value
677   value_from_output_section(const Relobj*, unsigned int input_shndx,
678                             Value input_offset) const;
679
680   // The value of the section symbol in the input file.  This is
681   // normally zero, but could in principle be something else.
682   Value input_value_;
683   // The start address of this merged section in the output file.
684   Value output_start_address_;
685   // A hash table which maps offsets in the input section to output
686   // addresses.  This only maps specific offsets, not all offsets.
687   Output_addresses output_addresses_;
688 };
689
690 // This POD class is holds the value of a symbol.  This is used for
691 // local symbols, and for all symbols during relocation processing.
692 // For special sections, such as SHF_MERGE sections, this calls a
693 // function to get the final symbol value.
694
695 template<int size>
696 class Symbol_value
697 {
698  public:
699   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
700
701   Symbol_value()
702     : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
703       is_section_symbol_(false), is_tls_symbol_(false),
704       has_output_value_(true)
705   { this->u_.value = 0; }
706
707   // Get the value of this symbol.  OBJECT is the object in which this
708   // symbol is defined, and ADDEND is an addend to add to the value.
709   template<bool big_endian>
710   Value
711   value(const Sized_relobj<size, big_endian>* object, Value addend) const
712   {
713     if (this->has_output_value_)
714       return this->u_.value + addend;
715     else
716       return this->u_.merged_symbol_value->value(object, this->input_shndx_,
717                                                  addend);
718   }
719
720   // Set the value of this symbol in the output symbol table.
721   void
722   set_output_value(Value value)
723   { this->u_.value = value; }
724
725   // For a section symbol in a merged section, we need more
726   // information.
727   void
728   set_merged_symbol_value(Merged_symbol_value<size>* msv)
729   {
730     gold_assert(this->is_section_symbol_);
731     this->has_output_value_ = false;
732     this->u_.merged_symbol_value = msv;
733   }
734
735   // Initialize the input to output map for a section symbol in a
736   // merged section.  We also initialize the value of a non-section
737   // symbol in a merged section.
738   void
739   initialize_input_to_output_map(const Relobj* object)
740   {
741     if (!this->has_output_value_)
742       {
743         gold_assert(this->is_section_symbol_);
744         Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
745         msv->initialize_input_to_output_map(object, this->input_shndx_);
746       }
747   }
748
749   // Free the input to output map for a section symbol in a merged
750   // section.
751   void
752   free_input_to_output_map()
753   {
754     if (!this->has_output_value_)
755       this->u_.merged_symbol_value->free_input_to_output_map();
756   }
757
758   // Set the value of the symbol from the input file.  This is only
759   // called by count_local_symbols, to communicate the value to
760   // finalize_local_symbols.
761   void
762   set_input_value(Value value)
763   { this->u_.value = value; }
764
765   // Return the input value.  This is only called by
766   // finalize_local_symbols.
767   Value
768   input_value() const
769   { return this->u_.value; }
770
771   // Return whether this symbol should go into the output symbol
772   // table.
773   bool
774   needs_output_symtab_entry() const
775   { return this->output_symtab_index_ != -1U; }
776
777   // Return the index in the output symbol table.
778   unsigned int
779   output_symtab_index() const
780   {
781     gold_assert(this->output_symtab_index_ != 0);
782     return this->output_symtab_index_;
783   }
784
785   // Set the index in the output symbol table.
786   void
787   set_output_symtab_index(unsigned int i)
788   {
789     gold_assert(this->output_symtab_index_ == 0);
790     this->output_symtab_index_ = i;
791   }
792
793   // Record that this symbol should not go into the output symbol
794   // table.
795   void
796   set_no_output_symtab_entry()
797   {
798     gold_assert(this->output_symtab_index_ == 0);
799     this->output_symtab_index_ = -1U;
800   }
801
802   // Set the index in the output dynamic symbol table.
803   void
804   set_needs_output_dynsym_entry()
805   {
806     this->output_dynsym_index_ = 0;
807   }
808
809   // Return whether this symbol should go into the output symbol
810   // table.
811   bool
812   needs_output_dynsym_entry() const
813   {
814     return this->output_dynsym_index_ != -1U;
815   }
816
817   // Record that this symbol should go into the dynamic symbol table.
818   void
819   set_output_dynsym_index(unsigned int i)
820   {
821     gold_assert(this->output_dynsym_index_ == 0);
822     this->output_dynsym_index_ = i;
823   }
824
825   // Return the index in the output dynamic symbol table.
826   unsigned int
827   output_dynsym_index() const
828   {
829     gold_assert(this->output_dynsym_index_ != 0);
830     return this->output_dynsym_index_;
831   }
832
833   // Set the index of the input section in the input file.
834   void
835   set_input_shndx(unsigned int i)
836   {
837     this->input_shndx_ = i;
838     // input_shndx_ field is a bitfield, so make sure that the value
839     // fits.
840     gold_assert(this->input_shndx_ == i);
841   }
842
843   // Return the index of the input section in the input file.
844   unsigned int
845   input_shndx() const
846   { return this->input_shndx_; }
847
848   // Whether this is a section symbol.
849   bool
850   is_section_symbol() const
851   { return this->is_section_symbol_; }
852
853   // Record that this is a section symbol.
854   void
855   set_is_section_symbol()
856   { this->is_section_symbol_ = true; }
857
858   // Record that this is a TLS symbol.
859   void
860   set_is_tls_symbol()
861   { this->is_tls_symbol_ = true; }
862
863   // Return TRUE if this is a TLS symbol.
864   bool
865   is_tls_symbol() const
866   { return this->is_tls_symbol_; }
867
868  private:
869   // The index of this local symbol in the output symbol table.  This
870   // will be -1 if the symbol should not go into the symbol table.
871   unsigned int output_symtab_index_;
872   // The index of this local symbol in the dynamic symbol table.  This
873   // will be -1 if the symbol should not go into the symbol table.
874   unsigned int output_dynsym_index_;
875   // The section index in the input file in which this symbol is
876   // defined.
877   unsigned int input_shndx_ : 29;
878   // Whether this is a STT_SECTION symbol.
879   bool is_section_symbol_ : 1;
880   // Whether this is a STT_TLS symbol.
881   bool is_tls_symbol_ : 1;
882   // Whether this symbol has a value for the output file.  This is
883   // normally set to true during Layout::finalize, by
884   // finalize_local_symbols.  It will be false for a section symbol in
885   // a merge section, as for such symbols we can not determine the
886   // value to use in a relocation until we see the addend.
887   bool has_output_value_ : 1;
888   union
889   {
890     // This is used if has_output_value_ is true.  Between
891     // count_local_symbols and finalize_local_symbols, this is the
892     // value in the input file.  After finalize_local_symbols, it is
893     // the value in the output file.
894     Value value;
895     // This is used if has_output_value_ is false.  It points to the
896     // information we need to get the value for a merge section.
897     Merged_symbol_value<size>* merged_symbol_value;
898   } u_;
899 };
900
901 // A regular object file.  This is size and endian specific.
902
903 template<int size, bool big_endian>
904 class Sized_relobj : public Relobj
905 {
906  public:
907   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
908   typedef std::vector<Symbol*> Symbols;
909   typedef std::vector<Symbol_value<size> > Local_values;
910
911   Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
912                const typename elfcpp::Ehdr<size, big_endian>&);
913
914   ~Sized_relobj();
915
916   // Set up the object file based on the ELF header.
917   void
918   setup(const typename elfcpp::Ehdr<size, big_endian>&);
919
920   // If SYM is the index of a global symbol in the object file's
921   // symbol table, return the Symbol object.  Otherwise, return NULL.
922   Symbol*
923   global_symbol(unsigned int sym) const
924   {
925     if (sym >= this->local_symbol_count_)
926       {
927         gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
928         return this->symbols_[sym - this->local_symbol_count_];
929       }
930     return NULL;
931   }
932
933   // Return the section index of symbol SYM.  Set *VALUE to its value
934   // in the object file.  Note that for a symbol which is not defined
935   // in this object file, this will set *VALUE to 0 and return
936   // SHN_UNDEF; it will not return the final value of the symbol in
937   // the link.
938   unsigned int
939   symbol_section_and_value(unsigned int sym, Address* value);
940
941   // Return a pointer to the Symbol_value structure which holds the
942   // value of a local symbol.
943   const Symbol_value<size>*
944   local_symbol(unsigned int sym) const
945   {
946     gold_assert(sym < this->local_values_.size());
947     return &this->local_values_[sym];
948   }
949
950   // Return the index of local symbol SYM in the ordinary symbol
951   // table.  A value of -1U means that the symbol is not being output.
952   unsigned int
953   symtab_index(unsigned int sym) const
954   {
955     gold_assert(sym < this->local_values_.size());
956     return this->local_values_[sym].output_symtab_index();
957   }
958
959   // Return the index of local symbol SYM in the dynamic symbol
960   // table.  A value of -1U means that the symbol is not being output.
961   unsigned int
962   dynsym_index(unsigned int sym) const
963   {
964     gold_assert(sym < this->local_values_.size());
965     return this->local_values_[sym].output_dynsym_index();
966   }
967
968   // Return the appropriate Sized_target structure.
969   Sized_target<size, big_endian>*
970   sized_target()
971   {
972     return this->Object::sized_target
973       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
974           SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
975   }
976
977   // Return the value of the local symbol symndx.
978   Address
979   local_symbol_value(unsigned int symndx) const;
980
981   void
982   set_needs_output_dynsym_entry(unsigned int sym)
983   {
984     gold_assert(sym < this->local_values_.size());
985     this->local_values_[sym].set_needs_output_dynsym_entry();
986   }
987
988   // Return whether the local symbol SYMNDX has a GOT offset.
989   // For TLS symbols, the GOT entry will hold its tp-relative offset.
990   bool
991   local_has_got_offset(unsigned int symndx) const
992   {
993     return (this->local_got_offsets_.find(symndx)
994             != this->local_got_offsets_.end());
995   }
996
997   // Return the GOT offset of the local symbol SYMNDX.
998   unsigned int
999   local_got_offset(unsigned int symndx) const
1000   {
1001     Local_got_offsets::const_iterator p =
1002         this->local_got_offsets_.find(symndx);
1003     gold_assert(p != this->local_got_offsets_.end());
1004     return p->second;
1005   }
1006
1007   // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1008   void
1009   set_local_got_offset(unsigned int symndx, unsigned int got_offset)
1010   {
1011     std::pair<Local_got_offsets::iterator, bool> ins =
1012         this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
1013     gold_assert(ins.second);
1014   }
1015
1016   // Return whether the local TLS symbol SYMNDX has a GOT offset.
1017   // The GOT entry at this offset will contain a module index. If
1018   // NEED_PAIR is true, a second entry immediately following the first
1019   // will contain the dtv-relative offset.
1020   bool
1021   local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
1022   {
1023     typename Local_tls_got_offsets::const_iterator p =
1024         this->local_tls_got_offsets_.find(symndx);
1025     if (p == this->local_tls_got_offsets_.end()
1026         || (need_pair && !p->second.have_pair_))
1027       return false;
1028     return true;
1029   }
1030
1031   // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1032   // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1033   // otherwise we need the offset of the GOT entry for the module index.
1034   unsigned int
1035   local_tls_got_offset(unsigned int symndx, bool need_pair) const
1036   {
1037     typename Local_tls_got_offsets::const_iterator p =
1038         this->local_tls_got_offsets_.find(symndx);
1039     gold_assert(p != this->local_tls_got_offsets_.end());
1040     gold_assert(!need_pair || p->second.have_pair_);
1041     return p->second.got_offset_;
1042   }
1043
1044   // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1045   // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1046   // otherwise, we have just a single entry for the module index.
1047   void
1048   set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
1049                            bool have_pair)
1050   {
1051     typename Local_tls_got_offsets::iterator p =
1052         this->local_tls_got_offsets_.find(symndx);
1053     if (p != this->local_tls_got_offsets_.end())
1054       {
1055         // An entry already existed for this symbol. This can happen
1056         // if we see a relocation asking for the module index before
1057         // a relocation asking for the pair. In that case, the original
1058         // GOT entry will remain, but won't get used by any further
1059         // relocations.
1060         p->second.got_offset_ = got_offset;
1061         gold_assert(have_pair);
1062         p->second.have_pair_ = true;
1063       }
1064     else
1065       {
1066         std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
1067             this->local_tls_got_offsets_.insert(
1068               std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
1069         gold_assert(ins.second);
1070       }
1071   }
1072
1073   // Return the name of the symbol that spans the given offset in the
1074   // specified section in this object.  This is used only for error
1075   // messages and is not particularly efficient.
1076   bool
1077   get_symbol_location_info(unsigned int shndx, off_t offset,
1078                            Symbol_location_info* info);
1079
1080  protected:
1081   // Read the symbols.
1082   void
1083   do_read_symbols(Read_symbols_data*);
1084
1085   // Return the number of local symbols.
1086   unsigned int
1087   do_local_symbol_count() const
1088   { return this->local_symbol_count_; }
1089
1090   // Lay out the input sections.
1091   void
1092   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1093
1094   // Add the symbols to the symbol table.
1095   void
1096   do_add_symbols(Symbol_table*, Read_symbols_data*);
1097
1098   // Read the relocs.
1099   void
1100   do_read_relocs(Read_relocs_data*);
1101
1102   // Scan the relocs and adjust the symbol table.
1103   void
1104   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1105                  Read_relocs_data*);
1106
1107   // Count the local symbols.
1108   void
1109   do_count_local_symbols(Stringpool_template<char>*,
1110                             Stringpool_template<char>*);
1111
1112   // Finalize the local symbols.
1113   unsigned int
1114   do_finalize_local_symbols(unsigned int, off_t);
1115
1116   // Set the offset where local dynamic symbol information will be stored.
1117   unsigned int
1118   do_set_local_dynsym_indexes(unsigned int);
1119
1120   // Set the offset where local dynamic symbol information will be stored.
1121   unsigned int
1122   do_set_local_dynsym_offset(off_t);
1123
1124   // Relocate the input sections and write out the local symbols.
1125   void
1126   do_relocate(const General_options& options, const Symbol_table* symtab,
1127               const Layout*, Output_file* of);
1128
1129   // Get the name of a section.
1130   std::string
1131   do_section_name(unsigned int shndx)
1132   { return this->elf_file_.section_name(shndx); }
1133
1134   // Return the location of the contents of a section.
1135   Object::Location
1136   do_section_contents(unsigned int shndx)
1137   { return this->elf_file_.section_contents(shndx); }
1138
1139   // Return section flags.
1140   uint64_t
1141   do_section_flags(unsigned int shndx)
1142   { return this->elf_file_.section_flags(shndx); }
1143
1144   // Return section type.
1145   unsigned int
1146   do_section_type(unsigned int shndx)
1147   { return this->elf_file_.section_type(shndx); }
1148
1149   // Return the section link field.
1150   unsigned int
1151   do_section_link(unsigned int shndx)
1152   { return this->elf_file_.section_link(shndx); }
1153
1154   // Return the section info field.
1155   unsigned int
1156   do_section_info(unsigned int shndx)
1157   { return this->elf_file_.section_info(shndx); }
1158
1159  private:
1160   // For convenience.
1161   typedef Sized_relobj<size, big_endian> This;
1162   static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1163   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1164   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1165   typedef elfcpp::Shdr<size, big_endian> Shdr;
1166
1167   // Find the SHT_SYMTAB section, given the section headers.
1168   void
1169   find_symtab(const unsigned char* pshdrs);
1170
1171   // Return whether SHDR has the right flags for a GNU style exception
1172   // frame section.
1173   bool
1174   check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1175
1176   // Return whether there is a section named .eh_frame which might be
1177   // a GNU style exception frame section.
1178   bool
1179   find_eh_frame(const unsigned char* pshdrs, const char* names,
1180                 section_size_type names_size) const;
1181
1182   // Whether to include a section group in the link.
1183   bool
1184   include_section_group(Layout*, unsigned int,
1185                         const elfcpp::Shdr<size, big_endian>&,
1186                         std::vector<bool>*);
1187
1188   // Whether to include a linkonce section in the link.
1189   bool
1190   include_linkonce_section(Layout*, const char*,
1191                            const elfcpp::Shdr<size, big_endian>&);
1192
1193   // Views and sizes when relocating.
1194   struct View_size
1195   {
1196     unsigned char* view;
1197     typename elfcpp::Elf_types<size>::Elf_Addr address;
1198     off_t offset;
1199     section_size_type view_size;
1200     bool is_input_output_view;
1201     bool is_postprocessing_view;
1202   };
1203
1204   typedef std::vector<View_size> Views;
1205
1206   // Write section data to the output file.  Record the views and
1207   // sizes in VIEWS for use when relocating.
1208   void
1209   write_sections(const unsigned char* pshdrs, Output_file*, Views*) const;
1210
1211   // Relocate the sections in the output file.
1212   void
1213   relocate_sections(const General_options& options, const Symbol_table*,
1214                     const Layout*, const unsigned char* pshdrs, Views*);
1215
1216   // Initialize input to output maps for section symbols in merged
1217   // sections.
1218   void
1219   initialize_input_to_output_maps();
1220
1221   // Free the input to output maps for section symbols in merged
1222   // sections.
1223   void
1224   free_input_to_output_maps();
1225
1226   // Write out the local symbols.
1227   void
1228   write_local_symbols(Output_file*,
1229                       const Stringpool_template<char>*,
1230                       const Stringpool_template<char>*);
1231
1232   // The GOT offsets of local symbols. This map also stores GOT offsets
1233   // for tp-relative offsets for TLS symbols.
1234   typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1235
1236   // The TLS GOT offsets of local symbols. The map stores the offsets
1237   // for either a single GOT entry that holds the module index of a TLS
1238   // symbol, or a pair of GOT entries containing the module index and
1239   // dtv-relative offset.
1240   struct Tls_got_entry
1241   {
1242     Tls_got_entry(int got_offset, bool have_pair)
1243       : got_offset_(got_offset),
1244         have_pair_(have_pair)
1245     { }
1246     int got_offset_;
1247     bool have_pair_;
1248   };
1249   typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1250
1251   // General access to the ELF file.
1252   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1253   // Index of SHT_SYMTAB section.
1254   unsigned int symtab_shndx_;
1255   // The number of local symbols.
1256   unsigned int local_symbol_count_;
1257   // The number of local symbols which go into the output file.
1258   unsigned int output_local_symbol_count_;
1259   // The number of local symbols which go into the output file's dynamic
1260   // symbol table.
1261   unsigned int output_local_dynsym_count_;
1262   // The entries in the symbol table for the external symbols.
1263   Symbols symbols_;
1264   // File offset for local symbols.
1265   off_t local_symbol_offset_;
1266   // File offset for local dynamic symbols.
1267   off_t local_dynsym_offset_;
1268   // Values of local symbols.
1269   Local_values local_values_;
1270   // GOT offsets for local non-TLS symbols, and tp-relative offsets
1271   // for TLS symbols, indexed by symbol number.
1272   Local_got_offsets local_got_offsets_;
1273   // GOT offsets for local TLS symbols, indexed by symbol number
1274   // and GOT entry type.
1275   Local_tls_got_offsets local_tls_got_offsets_;
1276   // Whether this object has a GNU style .eh_frame section.
1277   bool has_eh_frame_;
1278 };
1279
1280 // A class to manage the list of all objects.
1281
1282 class Input_objects
1283 {
1284  public:
1285   Input_objects()
1286     : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1287       system_library_directory_()
1288   { }
1289
1290   // The type of the list of input relocateable objects.
1291   typedef std::vector<Relobj*> Relobj_list;
1292   typedef Relobj_list::const_iterator Relobj_iterator;
1293
1294   // The type of the list of input dynamic objects.
1295   typedef std::vector<Dynobj*> Dynobj_list;
1296   typedef Dynobj_list::const_iterator Dynobj_iterator;
1297
1298   // Add an object to the list.  Return true if all is well, or false
1299   // if this object should be ignored.
1300   bool
1301   add_object(Object*);
1302
1303   // Get the target we should use for the output file.
1304   Target*
1305   target() const
1306   { return this->target_; }
1307
1308   // For each dynamic object, check whether we've seen all of its
1309   // explicit dependencies.
1310   void
1311   check_dynamic_dependencies() const;
1312
1313   // Return whether an object was found in the system library
1314   // directory.
1315   bool
1316   found_in_system_library_directory(const Object*) const;
1317
1318   // Iterate over all regular objects.
1319
1320   Relobj_iterator
1321   relobj_begin() const
1322   { return this->relobj_list_.begin(); }
1323
1324   Relobj_iterator
1325   relobj_end() const
1326   { return this->relobj_list_.end(); }
1327
1328   // Iterate over all dynamic objects.
1329
1330   Dynobj_iterator
1331   dynobj_begin() const
1332   { return this->dynobj_list_.begin(); }
1333
1334   Dynobj_iterator
1335   dynobj_end() const
1336   { return this->dynobj_list_.end(); }
1337
1338   // Return whether we have seen any dynamic objects.
1339   bool
1340   any_dynamic() const
1341   { return !this->dynobj_list_.empty(); }
1342
1343   // Return the number of input objects.
1344   int
1345   number_of_input_objects() const
1346   { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1347
1348  private:
1349   Input_objects(const Input_objects&);
1350   Input_objects& operator=(const Input_objects&);
1351
1352   // The list of ordinary objects included in the link.
1353   Relobj_list relobj_list_;
1354   // The list of dynamic objects included in the link.
1355   Dynobj_list dynobj_list_;
1356   // The target.
1357   Target* target_;
1358   // SONAMEs that we have seen.
1359   Unordered_set<std::string> sonames_;
1360   // The directory in which we find the libc.so.
1361   std::string system_library_directory_;
1362 };
1363
1364 // Some of the information we pass to the relocation routines.  We
1365 // group this together to avoid passing a dozen different arguments.
1366
1367 template<int size, bool big_endian>
1368 struct Relocate_info
1369 {
1370   // Command line options.
1371   const General_options* options;
1372   // Symbol table.
1373   const Symbol_table* symtab;
1374   // Layout.
1375   const Layout* layout;
1376   // Object being relocated.
1377   Sized_relobj<size, big_endian>* object;
1378   // Section index of relocation section.
1379   unsigned int reloc_shndx;
1380   // Section index of section being relocated.
1381   unsigned int data_shndx;
1382
1383   // Return a string showing the location of a relocation.  This is
1384   // only used for error messages.
1385   std::string
1386   location(size_t relnum, off_t reloffset) const;
1387 };
1388
1389 // Return an Object appropriate for the input file.  P is BYTES long,
1390 // and holds the ELF header.
1391
1392 extern Object*
1393 make_elf_object(const std::string& name, Input_file*,
1394                 off_t offset, const unsigned char* p,
1395                 section_offset_type bytes);
1396
1397 } // end namespace gold
1398
1399 #endif // !defined(GOLD_OBJECT_H)