Avoid some warnings which showed up in 64-bit mode.
[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 POD class is holds the value of a symbol.  This is used for
626 // local symbols, and for all symbols during relocation processing.
627 // For special sections, such as SHF_MERGE sections, this calls a
628 // function to get the final symbol value.
629
630 template<int size>
631 class Symbol_value
632 {
633  public:
634   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
635
636   Symbol_value()
637     : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
638       is_section_symbol_(false), is_tls_symbol_(false),
639       needs_output_address_(false), value_(0)
640   { }
641
642   // Get the value of this symbol.  OBJECT is the object in which this
643   // symbol is defined, and ADDEND is an addend to add to the value.
644   template<bool big_endian>
645   Value
646   value(const Sized_relobj<size, big_endian>* object, Value addend) const
647   {
648     if (!this->needs_output_address_)
649       return this->value_ + addend;
650     return object->local_value(this->input_shndx_, this->value_,
651                                this->is_section_symbol_, addend);
652   }
653
654   // Set the value of this symbol in the output symbol table.
655   void
656   set_output_value(Value value)
657   {
658     this->value_ = value;
659     this->needs_output_address_ = false;
660   }
661
662   // Set the value of the symbol from the input file.  This value
663   // will usually be replaced during finalization with the output
664   // value, but if the symbol is mapped to an output section which
665   // requires special handling to determine the output value, we
666   // leave the input value in place until later.  This is used for
667   // SHF_MERGE sections.
668   void
669   set_input_value(Value value)
670   {
671     this->value_ = value;
672     this->needs_output_address_ = true;
673   }
674
675   // Return the input value.
676   Value
677   input_value() const
678   {
679     gold_assert(this->needs_output_address_);
680     return this->value_;
681   }
682
683   // Return whether this symbol should go into the output symbol
684   // table.
685   bool
686   needs_output_symtab_entry() const
687   { return this->output_symtab_index_ != -1U; }
688
689   // Return the index in the output symbol table.
690   unsigned int
691   output_symtab_index() const
692   {
693     gold_assert(this->output_symtab_index_ != 0);
694     return this->output_symtab_index_;
695   }
696
697   // Set the index in the output symbol table.
698   void
699   set_output_symtab_index(unsigned int i)
700   {
701     gold_assert(this->output_symtab_index_ == 0);
702     this->output_symtab_index_ = i;
703   }
704
705   // Record that this symbol should not go into the output symbol
706   // table.
707   void
708   set_no_output_symtab_entry()
709   {
710     gold_assert(this->output_symtab_index_ == 0);
711     this->output_symtab_index_ = -1U;
712   }
713
714   // Set the index in the output dynamic symbol table.
715   void
716   set_needs_output_dynsym_entry()
717   {
718     this->output_dynsym_index_ = 0;
719   }
720
721   // Return whether this symbol should go into the output symbol
722   // table.
723   bool
724   needs_output_dynsym_entry() const
725   {
726     return this->output_dynsym_index_ != -1U;
727   }
728
729   // Record that this symbol should go into the dynamic symbol table.
730   void
731   set_output_dynsym_index(unsigned int i)
732   {
733     gold_assert(this->output_dynsym_index_ == 0);
734     this->output_dynsym_index_ = i;
735   }
736
737   // Return the index in the output dynamic symbol table.
738   unsigned int
739   output_dynsym_index() const
740   {
741     gold_assert(this->output_dynsym_index_ != 0);
742     return this->output_dynsym_index_;
743   }
744
745   // Set the index of the input section in the input file.
746   void
747   set_input_shndx(unsigned int i)
748   {
749     this->input_shndx_ = i;
750     // input_shndx_ field is a bitfield, so make sure that the value
751     // fits.
752     gold_assert(this->input_shndx_ == i);
753   }
754
755   // Return the index of the input section in the input file.
756   unsigned int
757   input_shndx() const
758   { return this->input_shndx_; }
759
760   // Record that this is a section symbol.
761   void
762   set_is_section_symbol()
763   { this->is_section_symbol_ = true; }
764
765   // Record that this is a TLS symbol.
766   void
767   set_is_tls_symbol()
768   { this->is_tls_symbol_ = true; }
769
770   // Return TRUE if this is a TLS symbol.
771   bool
772   is_tls_symbol() const
773   { return this->is_tls_symbol_; }
774
775  private:
776   // The index of this local symbol in the output symbol table.  This
777   // will be -1 if the symbol should not go into the symbol table.
778   unsigned int output_symtab_index_;
779   // The index of this local symbol in the dynamic symbol table.  This
780   // will be -1 if the symbol should not go into the symbol table.
781   unsigned int output_dynsym_index_;
782   // The section index in the input file in which this symbol is
783   // defined.
784   unsigned int input_shndx_ : 29;
785   // Whether this is a STT_SECTION symbol.
786   bool is_section_symbol_ : 1;
787   // Whether this is a STT_TLS symbol.
788   bool is_tls_symbol_ : 1;
789   // Whether getting the value of this symbol requires calling an
790   // Output_section method.  For example, this will be true of a
791   // symbol in a SHF_MERGE section.
792   bool needs_output_address_ : 1;
793   // The value of the symbol.  If !needs_output_address_, this is the
794   // value in the output file.  If needs_output_address_, this is the
795   // value in the input file.
796   Value value_;
797 };
798
799 // A regular object file.  This is size and endian specific.
800
801 template<int size, bool big_endian>
802 class Sized_relobj : public Relobj
803 {
804  public:
805   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
806   typedef std::vector<Symbol*> Symbols;
807   typedef std::vector<Symbol_value<size> > Local_values;
808
809   Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
810                const typename elfcpp::Ehdr<size, big_endian>&);
811
812   ~Sized_relobj();
813
814   // Set up the object file based on the ELF header.
815   void
816   setup(const typename elfcpp::Ehdr<size, big_endian>&);
817
818   // If SYM is the index of a global symbol in the object file's
819   // symbol table, return the Symbol object.  Otherwise, return NULL.
820   Symbol*
821   global_symbol(unsigned int sym) const
822   {
823     if (sym >= this->local_symbol_count_)
824       {
825         gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
826         return this->symbols_[sym - this->local_symbol_count_];
827       }
828     return NULL;
829   }
830
831   // Return the section index of symbol SYM.  Set *VALUE to its value
832   // in the object file.  Note that for a symbol which is not defined
833   // in this object file, this will set *VALUE to 0 and return
834   // SHN_UNDEF; it will not return the final value of the symbol in
835   // the link.
836   unsigned int
837   symbol_section_and_value(unsigned int sym, Address* value);
838
839   // Return a pointer to the Symbol_value structure which holds the
840   // value of a local symbol.
841   const Symbol_value<size>*
842   local_symbol(unsigned int sym) const
843   {
844     gold_assert(sym < this->local_values_.size());
845     return &this->local_values_[sym];
846   }
847
848   // Return the index of local symbol SYM in the ordinary symbol
849   // table.  A value of -1U means that the symbol is not being output.
850   unsigned int
851   symtab_index(unsigned int sym) const
852   {
853     gold_assert(sym < this->local_values_.size());
854     return this->local_values_[sym].output_symtab_index();
855   }
856
857   // Return the index of local symbol SYM in the dynamic symbol
858   // table.  A value of -1U means that the symbol is not being output.
859   unsigned int
860   dynsym_index(unsigned int sym) const
861   {
862     gold_assert(sym < this->local_values_.size());
863     return this->local_values_[sym].output_dynsym_index();
864   }
865
866   // Return the appropriate Sized_target structure.
867   Sized_target<size, big_endian>*
868   sized_target()
869   {
870     return this->Object::sized_target
871       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
872           SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
873   }
874
875   // Return the value of the local symbol symndx.
876   Address
877   local_symbol_value(unsigned int symndx) const;
878
879   // Return the value of a local symbol defined in input section
880   // SHNDX, with value VALUE, adding addend ADDEND.  IS_SECTION_SYMBOL
881   // indicates whether the symbol is a section symbol.  This handles
882   // SHF_MERGE sections.
883   Address
884   local_value(unsigned int shndx, Address value, bool is_section_symbol,
885               Address addend) const;
886
887   void
888   set_needs_output_dynsym_entry(unsigned int sym)
889   {
890     gold_assert(sym < this->local_values_.size());
891     this->local_values_[sym].set_needs_output_dynsym_entry();
892   }
893
894   // Return whether the local symbol SYMNDX has a GOT offset.
895   // For TLS symbols, the GOT entry will hold its tp-relative offset.
896   bool
897   local_has_got_offset(unsigned int symndx) const
898   {
899     return (this->local_got_offsets_.find(symndx)
900             != this->local_got_offsets_.end());
901   }
902
903   // Return the GOT offset of the local symbol SYMNDX.
904   unsigned int
905   local_got_offset(unsigned int symndx) const
906   {
907     Local_got_offsets::const_iterator p =
908         this->local_got_offsets_.find(symndx);
909     gold_assert(p != this->local_got_offsets_.end());
910     return p->second;
911   }
912
913   // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
914   void
915   set_local_got_offset(unsigned int symndx, unsigned int got_offset)
916   {
917     std::pair<Local_got_offsets::iterator, bool> ins =
918         this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
919     gold_assert(ins.second);
920   }
921
922   // Return whether the local TLS symbol SYMNDX has a GOT offset.
923   // The GOT entry at this offset will contain a module index. If
924   // NEED_PAIR is true, a second entry immediately following the first
925   // will contain the dtv-relative offset.
926   bool
927   local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
928   {
929     typename Local_tls_got_offsets::const_iterator p =
930         this->local_tls_got_offsets_.find(symndx);
931     if (p == this->local_tls_got_offsets_.end()
932         || (need_pair && !p->second.have_pair_))
933       return false;
934     return true;
935   }
936
937   // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
938   // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
939   // otherwise we need the offset of the GOT entry for the module index.
940   unsigned int
941   local_tls_got_offset(unsigned int symndx, bool need_pair) const
942   {
943     typename Local_tls_got_offsets::const_iterator p =
944         this->local_tls_got_offsets_.find(symndx);
945     gold_assert(p != this->local_tls_got_offsets_.end());
946     gold_assert(!need_pair || p->second.have_pair_);
947     return p->second.got_offset_;
948   }
949
950   // Set the offset of the GOT entry for the local TLS symbol SYMNDX
951   // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
952   // otherwise, we have just a single entry for the module index.
953   void
954   set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
955                            bool have_pair)
956   {
957     typename Local_tls_got_offsets::iterator p =
958         this->local_tls_got_offsets_.find(symndx);
959     if (p != this->local_tls_got_offsets_.end())
960       {
961         // An entry already existed for this symbol. This can happen
962         // if we see a relocation asking for the module index before
963         // a relocation asking for the pair. In that case, the original
964         // GOT entry will remain, but won't get used by any further
965         // relocations.
966         p->second.got_offset_ = got_offset;
967         gold_assert(have_pair);
968         p->second.have_pair_ = true;
969       }
970     else
971       {
972         std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
973             this->local_tls_got_offsets_.insert(
974               std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
975         gold_assert(ins.second);
976       }
977   }
978
979   // Return the name of the symbol that spans the given offset in the
980   // specified section in this object.  This is used only for error
981   // messages and is not particularly efficient.
982   bool
983   get_symbol_location_info(unsigned int shndx, off_t offset,
984                            Symbol_location_info* info);
985
986  protected:
987   // Read the symbols.
988   void
989   do_read_symbols(Read_symbols_data*);
990
991   // Return the number of local symbols.
992   unsigned int
993   do_local_symbol_count() const
994   { return this->local_symbol_count_; }
995
996   // Lay out the input sections.
997   void
998   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
999
1000   // Add the symbols to the symbol table.
1001   void
1002   do_add_symbols(Symbol_table*, Read_symbols_data*);
1003
1004   // Read the relocs.
1005   void
1006   do_read_relocs(Read_relocs_data*);
1007
1008   // Scan the relocs and adjust the symbol table.
1009   void
1010   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1011                  Read_relocs_data*);
1012
1013   // Count the local symbols.
1014   void
1015   do_count_local_symbols(Stringpool_template<char>*,
1016                             Stringpool_template<char>*);
1017
1018   // Finalize the local symbols.
1019   unsigned int
1020   do_finalize_local_symbols(unsigned int, off_t);
1021
1022   // Set the offset where local dynamic symbol information will be stored.
1023   unsigned int
1024   do_set_local_dynsym_indexes(unsigned int);
1025
1026   // Set the offset where local dynamic symbol information will be stored.
1027   unsigned int
1028   do_set_local_dynsym_offset(off_t);
1029
1030   // Relocate the input sections and write out the local symbols.
1031   void
1032   do_relocate(const General_options& options, const Symbol_table* symtab,
1033               const Layout*, Output_file* of);
1034
1035   // Get the name of a section.
1036   std::string
1037   do_section_name(unsigned int shndx)
1038   { return this->elf_file_.section_name(shndx); }
1039
1040   // Return the location of the contents of a section.
1041   Object::Location
1042   do_section_contents(unsigned int shndx)
1043   { return this->elf_file_.section_contents(shndx); }
1044
1045   // Return section flags.
1046   uint64_t
1047   do_section_flags(unsigned int shndx)
1048   { return this->elf_file_.section_flags(shndx); }
1049
1050   // Return section type.
1051   unsigned int
1052   do_section_type(unsigned int shndx)
1053   { return this->elf_file_.section_type(shndx); }
1054
1055   // Return the section link field.
1056   unsigned int
1057   do_section_link(unsigned int shndx)
1058   { return this->elf_file_.section_link(shndx); }
1059
1060   // Return the section info field.
1061   unsigned int
1062   do_section_info(unsigned int shndx)
1063   { return this->elf_file_.section_info(shndx); }
1064
1065  private:
1066   // For convenience.
1067   typedef Sized_relobj<size, big_endian> This;
1068   static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1069   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1070   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1071   typedef elfcpp::Shdr<size, big_endian> Shdr;
1072
1073   // Find the SHT_SYMTAB section, given the section headers.
1074   void
1075   find_symtab(const unsigned char* pshdrs);
1076
1077   // Return whether SHDR has the right flags for a GNU style exception
1078   // frame section.
1079   bool
1080   check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1081
1082   // Return whether there is a section named .eh_frame which might be
1083   // a GNU style exception frame section.
1084   bool
1085   find_eh_frame(const unsigned char* pshdrs, const char* names,
1086                 section_size_type names_size) const;
1087
1088   // Whether to include a section group in the link.
1089   bool
1090   include_section_group(Layout*, unsigned int,
1091                         const elfcpp::Shdr<size, big_endian>&,
1092                         std::vector<bool>*);
1093
1094   // Whether to include a linkonce section in the link.
1095   bool
1096   include_linkonce_section(Layout*, const char*,
1097                            const elfcpp::Shdr<size, big_endian>&);
1098
1099   // Views and sizes when relocating.
1100   struct View_size
1101   {
1102     unsigned char* view;
1103     typename elfcpp::Elf_types<size>::Elf_Addr address;
1104     off_t offset;
1105     section_size_type view_size;
1106     bool is_input_output_view;
1107     bool is_postprocessing_view;
1108   };
1109
1110   typedef std::vector<View_size> Views;
1111
1112   // Write section data to the output file.  Record the views and
1113   // sizes in VIEWS for use when relocating.
1114   void
1115   write_sections(const unsigned char* pshdrs, Output_file*, Views*) const;
1116
1117   // Relocate the sections in the output file.
1118   void
1119   relocate_sections(const General_options& options, const Symbol_table*,
1120                     const Layout*, const unsigned char* pshdrs, Views*);
1121
1122   // Write out the local symbols.
1123   void
1124   write_local_symbols(Output_file*,
1125                       const Stringpool_template<char>*,
1126                       const Stringpool_template<char>*);
1127
1128   // The GOT offsets of local symbols. This map also stores GOT offsets
1129   // for tp-relative offsets for TLS symbols.
1130   typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1131
1132   // The TLS GOT offsets of local symbols. The map stores the offsets
1133   // for either a single GOT entry that holds the module index of a TLS
1134   // symbol, or a pair of GOT entries containing the module index and
1135   // dtv-relative offset.
1136   struct Tls_got_entry
1137   {
1138     Tls_got_entry(int got_offset, bool have_pair)
1139       : got_offset_(got_offset),
1140         have_pair_(have_pair)
1141     { }
1142     int got_offset_;
1143     bool have_pair_;
1144   };
1145   typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1146
1147   // General access to the ELF file.
1148   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1149   // Index of SHT_SYMTAB section.
1150   unsigned int symtab_shndx_;
1151   // The number of local symbols.
1152   unsigned int local_symbol_count_;
1153   // The number of local symbols which go into the output file.
1154   unsigned int output_local_symbol_count_;
1155   // The number of local symbols which go into the output file's dynamic
1156   // symbol table.
1157   unsigned int output_local_dynsym_count_;
1158   // The entries in the symbol table for the external symbols.
1159   Symbols symbols_;
1160   // File offset for local symbols.
1161   off_t local_symbol_offset_;
1162   // File offset for local dynamic symbols.
1163   off_t local_dynsym_offset_;
1164   // Values of local symbols.
1165   Local_values local_values_;
1166   // GOT offsets for local non-TLS symbols, and tp-relative offsets
1167   // for TLS symbols, indexed by symbol number.
1168   Local_got_offsets local_got_offsets_;
1169   // GOT offsets for local TLS symbols, indexed by symbol number
1170   // and GOT entry type.
1171   Local_tls_got_offsets local_tls_got_offsets_;
1172   // Whether this object has a GNU style .eh_frame section.
1173   bool has_eh_frame_;
1174 };
1175
1176 // A class to manage the list of all objects.
1177
1178 class Input_objects
1179 {
1180  public:
1181   Input_objects()
1182     : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1183       system_library_directory_()
1184   { }
1185
1186   // The type of the list of input relocateable objects.
1187   typedef std::vector<Relobj*> Relobj_list;
1188   typedef Relobj_list::const_iterator Relobj_iterator;
1189
1190   // The type of the list of input dynamic objects.
1191   typedef std::vector<Dynobj*> Dynobj_list;
1192   typedef Dynobj_list::const_iterator Dynobj_iterator;
1193
1194   // Add an object to the list.  Return true if all is well, or false
1195   // if this object should be ignored.
1196   bool
1197   add_object(Object*);
1198
1199   // Get the target we should use for the output file.
1200   Target*
1201   target() const
1202   { return this->target_; }
1203
1204   // For each dynamic object, check whether we've seen all of its
1205   // explicit dependencies.
1206   void
1207   check_dynamic_dependencies() const;
1208
1209   // Return whether an object was found in the system library
1210   // directory.
1211   bool
1212   found_in_system_library_directory(const Object*) const;
1213
1214   // Iterate over all regular objects.
1215
1216   Relobj_iterator
1217   relobj_begin() const
1218   { return this->relobj_list_.begin(); }
1219
1220   Relobj_iterator
1221   relobj_end() const
1222   { return this->relobj_list_.end(); }
1223
1224   // Iterate over all dynamic objects.
1225
1226   Dynobj_iterator
1227   dynobj_begin() const
1228   { return this->dynobj_list_.begin(); }
1229
1230   Dynobj_iterator
1231   dynobj_end() const
1232   { return this->dynobj_list_.end(); }
1233
1234   // Return whether we have seen any dynamic objects.
1235   bool
1236   any_dynamic() const
1237   { return !this->dynobj_list_.empty(); }
1238
1239   // Return the number of input objects.
1240   int
1241   number_of_input_objects() const
1242   { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1243
1244  private:
1245   Input_objects(const Input_objects&);
1246   Input_objects& operator=(const Input_objects&);
1247
1248   // The list of ordinary objects included in the link.
1249   Relobj_list relobj_list_;
1250   // The list of dynamic objects included in the link.
1251   Dynobj_list dynobj_list_;
1252   // The target.
1253   Target* target_;
1254   // SONAMEs that we have seen.
1255   Unordered_set<std::string> sonames_;
1256   // The directory in which we find the libc.so.
1257   std::string system_library_directory_;
1258 };
1259
1260 // Some of the information we pass to the relocation routines.  We
1261 // group this together to avoid passing a dozen different arguments.
1262
1263 template<int size, bool big_endian>
1264 struct Relocate_info
1265 {
1266   // Command line options.
1267   const General_options* options;
1268   // Symbol table.
1269   const Symbol_table* symtab;
1270   // Layout.
1271   const Layout* layout;
1272   // Object being relocated.
1273   Sized_relobj<size, big_endian>* object;
1274   // Section index of relocation section.
1275   unsigned int reloc_shndx;
1276   // Section index of section being relocated.
1277   unsigned int data_shndx;
1278
1279   // Return a string showing the location of a relocation.  This is
1280   // only used for error messages.
1281   std::string
1282   location(size_t relnum, off_t reloffset) const;
1283 };
1284
1285 // Return an Object appropriate for the input file.  P is BYTES long,
1286 // and holds the ELF header.
1287
1288 extern Object*
1289 make_elf_object(const std::string& name, Input_file*,
1290                 off_t offset, const unsigned char* p,
1291                 section_offset_type bytes);
1292
1293 } // end namespace gold
1294
1295 #endif // !defined(GOLD_OBJECT_H)