332739295f2e85c96c6ea6e391613a835495878d
[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 Layout;
39 class Output_section;
40 class Output_file;
41 class Dynobj;
42
43 template<typename Stringpool_char>
44 class Stringpool_template;
45
46 // Data to pass from read_symbols() to add_symbols().
47
48 struct Read_symbols_data
49 {
50   // Section headers.
51   File_view* section_headers;
52   // Section names.
53   File_view* section_names;
54   // Size of section name data in bytes.
55   off_t section_names_size;
56   // Symbol data.
57   File_view* symbols;
58   // Size of symbol data in bytes.
59   off_t symbols_size;
60   // Offset of external symbols within symbol data.  This structure
61   // sometimes contains only external symbols, in which case this will
62   // be zero.  Sometimes it contains all symbols.
63   off_t external_symbols_offset;
64   // Symbol names.
65   File_view* symbol_names;
66   // Size of symbol name data in bytes.
67   off_t symbol_names_size;
68
69   // Version information.  This is only used on dynamic objects.
70   // Version symbol data (from SHT_GNU_versym section).
71   File_view* versym;
72   off_t versym_size;
73   // Version definition data (from SHT_GNU_verdef section).
74   File_view* verdef;
75   off_t verdef_size;
76   unsigned int verdef_info;
77   // Needed version data  (from SHT_GNU_verneed section).
78   File_view* verneed;
79   off_t verneed_size;
80   unsigned int verneed_info;
81 };
82
83 // Information used to print error messages.
84
85 struct Symbol_location_info
86 {
87   std::string source_file;
88   std::string enclosing_symbol_name;
89   int line_number;
90 };
91
92 // Data about a single relocation section.  This is read in
93 // read_relocs and processed in scan_relocs.
94
95 struct Section_relocs
96 {
97   // Index of reloc section.
98   unsigned int reloc_shndx;
99   // Index of section that relocs apply to.
100   unsigned int data_shndx;
101   // Contents of reloc section.
102   File_view* contents;
103   // Reloc section type.
104   unsigned int sh_type;
105   // Number of reloc entries.
106   size_t reloc_count;
107   // Output section.
108   Output_section* output_section;
109   // Whether this section has special handling for offsets.
110   bool needs_special_offset_handling;
111 };
112
113 // Relocations in an object file.  This is read in read_relocs and
114 // processed in scan_relocs.
115
116 struct Read_relocs_data
117 {
118   typedef std::vector<Section_relocs> Relocs_list;
119   // The relocations.
120   Relocs_list relocs;
121   // The local symbols.
122   File_view* local_symbols;
123 };
124
125 // Object is an abstract base class which represents either a 32-bit
126 // or a 64-bit input object.  This can be a regular object file
127 // (ET_REL) or a shared object (ET_DYN).
128
129 class Object
130 {
131  public:
132   // NAME is the name of the object as we would report it to the user
133   // (e.g., libfoo.a(bar.o) if this is in an archive.  INPUT_FILE is
134   // used to read the file.  OFFSET is the offset within the input
135   // file--0 for a .o or .so file, something else for a .a file.
136   Object(const std::string& name, Input_file* input_file, bool is_dynamic,
137          off_t offset = 0)
138     : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
139       is_dynamic_(is_dynamic), target_(NULL)
140   { }
141
142   virtual ~Object()
143   { }
144
145   // Return the name of the object as we would report it to the tuser.
146   const std::string&
147   name() const
148   { return this->name_; }
149
150   // Get the offset into the file.
151   off_t
152   offset() const
153   { return this->offset_; }
154
155   // Return whether this is a dynamic object.
156   bool
157   is_dynamic() const
158   { return this->is_dynamic_; }
159
160   // Return the target structure associated with this object.
161   Target*
162   target() const
163   { return this->target_; }
164
165   // Lock the underlying file.
166   void
167   lock()
168   { this->input_file_->file().lock(); }
169
170   // Unlock the underlying file.
171   void
172   unlock()
173   { this->input_file_->file().unlock(); }
174
175   // Return whether the underlying file is locked.
176   bool
177   is_locked() const
178   { return this->input_file_->file().is_locked(); }
179
180   // Return the sized target structure associated with this object.
181   // This is like the target method but it returns a pointer of
182   // appropriate checked type.
183   template<int size, bool big_endian>
184   Sized_target<size, big_endian>*
185   sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
186
187   // Get the number of sections.
188   unsigned int
189   shnum() const
190   { return this->shnum_; }
191
192   // Return a view of the contents of a section.  Set *PLEN to the
193   // size.  CACHE is a hint as in File_read::get_view.
194   const unsigned char*
195   section_contents(unsigned int shndx, off_t* plen, bool cache);
196
197   // Return the name of a section given a section index.  This is only
198   // used for error messages.
199   std::string
200   section_name(unsigned int shndx)
201   { return this->do_section_name(shndx); }
202
203   // Return the section flags given a section index.
204   uint64_t
205   section_flags(unsigned int shndx)
206   { return this->do_section_flags(shndx); }
207
208   // Return the section type given a section index.
209   unsigned int
210   section_type(unsigned int shndx)
211   { return this->do_section_type(shndx); }
212
213   // Return the section link field given a section index.
214   unsigned int
215   section_link(unsigned int shndx)
216   { return this->do_section_link(shndx); }
217
218   // Return the section info field given a section index.
219   unsigned int
220   section_info(unsigned int shndx)
221   { return this->do_section_info(shndx); }
222
223   // Read the symbol information.
224   void
225   read_symbols(Read_symbols_data* sd)
226   { return this->do_read_symbols(sd); }
227
228   // Pass sections which should be included in the link to the Layout
229   // object, and record where the sections go in the output file.
230   void
231   layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
232   { this->do_layout(symtab, layout, sd); }
233
234   // Add symbol information to the global symbol table.
235   void
236   add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
237   { this->do_add_symbols(symtab, sd); }
238
239   // Functions and types for the elfcpp::Elf_file interface.  This
240   // permit us to use Object as the File template parameter for
241   // elfcpp::Elf_file.
242
243   // The View class is returned by view.  It must support a single
244   // method, data().  This is trivial, because get_view does what we
245   // need.
246   class View
247   {
248    public:
249     View(const unsigned char* p)
250       : p_(p)
251     { }
252
253     const unsigned char*
254     data() const
255     { return this->p_; }
256
257    private:
258     const unsigned char* p_;
259   };
260
261   // Return a View.
262   View
263   view(off_t file_offset, off_t data_size)
264   { return View(this->get_view(file_offset, data_size, true)); }
265
266   // Report an error.
267   void
268   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
269
270   // A location in the file.
271   struct Location
272   {
273     off_t file_offset;
274     off_t data_size;
275
276     Location(off_t fo, off_t ds)
277       : file_offset(fo), data_size(ds)
278     { }
279   };
280
281   // Get a View given a Location.
282   View view(Location loc)
283   { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
284
285  protected:
286   // Read the symbols--implemented by child class.
287   virtual void
288   do_read_symbols(Read_symbols_data*) = 0;
289
290   // Lay out sections--implemented by child class.
291   virtual void
292   do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
293
294   // Add symbol information to the global symbol table--implemented by
295   // child class.
296   virtual void
297   do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
298
299   // Return the location of the contents of a section.  Implemented by
300   // child class.
301   virtual Location
302   do_section_contents(unsigned int shndx) = 0;
303
304   // Get the name of a section--implemented by child class.
305   virtual std::string
306   do_section_name(unsigned int shndx) = 0;
307
308   // Get section flags--implemented by child class.
309   virtual uint64_t
310   do_section_flags(unsigned int shndx) = 0;
311
312   // Get section type--implemented by child class.
313   virtual unsigned int
314   do_section_type(unsigned int shndx) = 0;
315
316   // Get section link field--implemented by child class.
317   virtual unsigned int
318   do_section_link(unsigned int shndx) = 0;
319
320   // Get section info field--implemented by child class.
321   virtual unsigned int
322   do_section_info(unsigned int shndx) = 0;
323
324   // Get the file.
325   Input_file*
326   input_file() const
327   { return this->input_file_; }
328
329   // Get a view into the underlying file.
330   const unsigned char*
331   get_view(off_t start, off_t size, bool cache)
332   {
333     return this->input_file_->file().get_view(start + this->offset_, size,
334                                               cache);
335   }
336
337   // Get a lasting view into the underlying file.
338   File_view*
339   get_lasting_view(off_t start, off_t size, bool cache)
340   {
341     return this->input_file_->file().get_lasting_view(start + this->offset_,
342                                                       size, cache);
343   }
344
345   // Read data from the underlying file.
346   void
347   read(off_t start, off_t size, void* p)
348   { this->input_file_->file().read(start + this->offset_, size, p); }
349
350   // Set the target.
351   void
352   set_target(int machine, int size, bool big_endian, int osabi,
353              int abiversion);
354
355   // Set the number of sections.
356   void
357   set_shnum(int shnum)
358   { this->shnum_ = shnum; }
359
360   // Functions used by both Sized_relobj and Sized_dynobj.
361
362   // Read the section data into a Read_symbols_data object.
363   template<int size, bool big_endian>
364   void
365   read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
366                     Read_symbols_data*);
367
368   // If NAME is the name of a special .gnu.warning section, arrange
369   // for the warning to be issued.  SHNDX is the section index.
370   // Return whether it is a warning section.
371   bool
372   handle_gnu_warning_section(const char* name, unsigned int shndx,
373                              Symbol_table*);
374
375  private:
376   // This class may not be copied.
377   Object(const Object&);
378   Object& operator=(const Object&);
379
380   // Name of object as printed to user.
381   std::string name_;
382   // For reading the file.
383   Input_file* input_file_;
384   // Offset within the file--0 for an object file, non-0 for an
385   // archive.
386   off_t offset_;
387   // Number of input sections.
388   unsigned int shnum_;
389   // Whether this is a dynamic object.
390   bool is_dynamic_;
391   // Target functions--may be NULL if the target is not known.
392   Target* target_;
393 };
394
395 // Implement sized_target inline for efficiency.  This approach breaks
396 // static type checking, but is made safe using asserts.
397
398 template<int size, bool big_endian>
399 inline Sized_target<size, big_endian>*
400 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
401 {
402   gold_assert(this->target_->get_size() == size);
403   gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
404   return static_cast<Sized_target<size, big_endian>*>(this->target_);
405 }
406
407 // A regular object (ET_REL).  This is an abstract base class itself.
408 // The implementation is the template class Sized_relobj.
409
410 class Relobj : public Object
411 {
412  public:
413   Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
414     : Object(name, input_file, false, offset)
415   { }
416
417   // Read the relocs.
418   void
419   read_relocs(Read_relocs_data* rd)
420   { return this->do_read_relocs(rd); }
421
422   // Scan the relocs and adjust the symbol table.
423   void
424   scan_relocs(const General_options& options, Symbol_table* symtab,
425               Layout* layout, Read_relocs_data* rd)
426   { return this->do_scan_relocs(options, symtab, layout, rd); }
427
428   // Initial local symbol processing: set the offset where local
429   // symbol information will be stored; add local symbol names to
430   // *POOL; return the new local symbol index.
431   unsigned int
432   finalize_local_symbols(unsigned int index, off_t off,
433                          Stringpool_template<char>* pool)
434   { return this->do_finalize_local_symbols(index, off, pool); }
435
436   // Relocate the input sections and write out the local symbols.
437   void
438   relocate(const General_options& options, const Symbol_table* symtab,
439            const Layout* layout, Output_file* of)
440   { return this->do_relocate(options, symtab, layout, of); }
441
442   // Return whether an input section is being included in the link.
443   bool
444   is_section_included(unsigned int shndx) const
445   {
446     gold_assert(shndx < this->map_to_output_.size());
447     return this->map_to_output_[shndx].output_section != NULL;
448   }
449
450   // Return whether an input section requires special
451   // handling--whether it is not simply mapped from the input file to
452   // the output file.
453   bool
454   is_section_specially_mapped(unsigned int shndx) const
455   {
456     gold_assert(shndx < this->map_to_output_.size());
457     return (this->map_to_output_[shndx].output_section != NULL
458             && this->map_to_output_[shndx].offset == -1);
459   }
460
461   // Given a section index, return the corresponding Output_section
462   // (which will be NULL if the section is not included in the link)
463   // and set *POFF to the offset within that section.  *POFF will be
464   // set to -1 if the section requires special handling.
465   inline Output_section*
466   output_section(unsigned int shndx, off_t* poff) const;
467
468   // Set the offset of an input section within its output section.
469   void
470   set_section_offset(unsigned int shndx, off_t off)
471   {
472     gold_assert(shndx < this->map_to_output_.size());
473     this->map_to_output_[shndx].offset = off;
474   }
475
476   // Return true if we need to wait for output sections to be written
477   // before we can apply relocations.  This is true if the object has
478   // any relocations for sections which require special handling, such
479   // as the exception frame section.
480   bool
481   relocs_must_follow_section_writes()
482   { return this->relocs_must_follow_section_writes_; }
483
484  protected:
485   // What we need to know to map an input section to an output
486   // section.  We keep an array of these, one for each input section,
487   // indexed by the input section number.
488   struct Map_to_output
489   {
490     // The output section.  This is NULL if the input section is to be
491     // discarded.
492     Output_section* output_section;
493     // The offset within the output section.  This is -1 if the
494     // section requires special handling.
495     off_t offset;
496   };
497
498   // Read the relocs--implemented by child class.
499   virtual void
500   do_read_relocs(Read_relocs_data*) = 0;
501
502   // Scan the relocs--implemented by child class.
503   virtual void
504   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
505                  Read_relocs_data*) = 0;
506
507   // Finalize local symbols--implemented by child class.
508   virtual unsigned int
509   do_finalize_local_symbols(unsigned int, off_t,
510                             Stringpool_template<char>*) = 0;
511
512   // Relocate the input sections and write out the local
513   // symbols--implemented by child class.
514   virtual void
515   do_relocate(const General_options& options, const Symbol_table* symtab,
516               const Layout*, Output_file* of) = 0;
517
518   // Return the vector mapping input sections to output sections.
519   std::vector<Map_to_output>&
520   map_to_output()
521   { return this->map_to_output_; }
522
523   const std::vector<Map_to_output>&
524   map_to_output() const
525   { return this->map_to_output_; }
526
527   // Record that we must wait for the output sections to be written
528   // before applying relocations.
529   void
530   set_relocs_must_follow_section_writes()
531   { this->relocs_must_follow_section_writes_ = true; }
532
533  private:
534   // Mapping from input sections to output section.
535   std::vector<Map_to_output> map_to_output_;
536   // Whether we need to wait for output sections to be written before
537   // we can apply relocations.
538   bool relocs_must_follow_section_writes_;
539 };
540
541 // Implement Object::output_section inline for efficiency.
542 inline Output_section*
543 Relobj::output_section(unsigned int shndx, off_t* poff) const
544 {
545   gold_assert(shndx < this->map_to_output_.size());
546   const Map_to_output& mo(this->map_to_output_[shndx]);
547   *poff = mo.offset;
548   return mo.output_section;
549 }
550
551 // This POD class is holds the value of a symbol.  This is used for
552 // local symbols, and for all symbols during relocation processing.
553 // For special sections, such as SHF_MERGE sections, this calls a
554 // function to get the final symbol value.
555
556 template<int size>
557 class Symbol_value
558 {
559  public:
560   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
561
562   Symbol_value()
563     : output_symtab_index_(0), input_shndx_(0), is_section_symbol_(false),
564       needs_output_address_(false), value_(0)
565   { }
566
567   // Get the value of this symbol.  OBJECT is the object in which this
568   // symbol is defined, and ADDEND is an addend to add to the value.
569   template<bool big_endian>
570   Value
571   value(const Sized_relobj<size, big_endian>* object, Value addend) const
572   {
573     if (!this->needs_output_address_)
574       return this->value_ + addend;
575     return object->local_value(this->input_shndx_, this->value_,
576                                this->is_section_symbol_, addend);
577   }
578
579   // Set the value of this symbol in the output symbol table.
580   void
581   set_output_value(Value value)
582   {
583     this->value_ = value;
584     this->needs_output_address_ = false;
585   }
586
587   // If this symbol is mapped to an output section which requires
588   // special handling to determine the output value, we store the
589   // value of the symbol in the input file.  This is used for
590   // SHF_MERGE sections.
591   void
592   set_input_value(Value value)
593   {
594     this->value_ = value;
595     this->needs_output_address_ = true;
596   }
597
598   // Return whether this symbol should go into the output symbol
599   // table.
600   bool
601   needs_output_symtab_entry() const
602   {
603     gold_assert(this->output_symtab_index_ != 0);
604     return this->output_symtab_index_ != -1U;
605   }
606
607   // Return the index in the output symbol table.
608   unsigned int
609   output_symtab_index() const
610   {
611     gold_assert(this->output_symtab_index_ != 0);
612     return this->output_symtab_index_;
613   }
614
615   // Set the index in the output symbol table.
616   void
617   set_output_symtab_index(unsigned int i)
618   {
619     gold_assert(this->output_symtab_index_ == 0);
620     this->output_symtab_index_ = i;
621   }
622
623   // Record that this symbol should not go into the output symbol
624   // table.
625   void
626   set_no_output_symtab_entry()
627   {
628     gold_assert(this->output_symtab_index_ == 0);
629     this->output_symtab_index_ = -1U;
630   }
631
632   // Set the index of the input section in the input file.
633   void
634   set_input_shndx(unsigned int i)
635   {
636     this->input_shndx_ = i;
637     gold_assert(this->input_shndx_ == i);
638   }
639
640   // Record that this is a section symbol.
641   void
642   set_is_section_symbol()
643   { this->is_section_symbol_ = true; }
644
645  private:
646   // The index of this local symbol in the output symbol table.  This
647   // will be -1 if the symbol should not go into the symbol table.
648   unsigned int output_symtab_index_;
649   // The section index in the input file in which this symbol is
650   // defined.
651   unsigned int input_shndx_ : 30;
652   // Whether this is a STT_SECTION symbol.
653   bool is_section_symbol_ : 1;
654   // Whether getting the value of this symbol requires calling an
655   // Output_section method.  For example, this will be true of a
656   // symbol in a SHF_MERGE section.
657   bool needs_output_address_ : 1;
658   // The value of the symbol.  If !needs_output_address_, this is the
659   // value in the output file.  If needs_output_address_, this is the
660   // value in the input file.
661   Value value_;
662 };
663
664 // A regular object file.  This is size and endian specific.
665
666 template<int size, bool big_endian>
667 class Sized_relobj : public Relobj
668 {
669  public:
670   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
671   typedef std::vector<Symbol*> Symbols;
672   typedef std::vector<Symbol_value<size> > Local_values;
673
674   Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
675                const typename elfcpp::Ehdr<size, big_endian>&);
676
677   ~Sized_relobj();
678
679   // Set up the object file based on the ELF header.
680   void
681   setup(const typename elfcpp::Ehdr<size, big_endian>&);
682
683   // Return the number of local symbols.
684   unsigned int
685   local_symbol_count() const
686   { return this->local_symbol_count_; }
687
688   // If SYM is the index of a global symbol in the object file's
689   // symbol table, return the Symbol object.  Otherwise, return NULL.
690   Symbol*
691   global_symbol(unsigned int sym) const
692   {
693     if (sym >= this->local_symbol_count_)
694       {
695         gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
696         return this->symbols_[sym - this->local_symbol_count_];
697       }
698     return NULL;
699   }
700
701   // Return the section index of symbol SYM.  Set *VALUE to its value
702   // in the object file.  Note that for a symbol which is not defined
703   // in this object file, this will set *VALUE to 0 and return
704   // SHN_UNDEF; it will not return the final value of the symbol in
705   // the link.
706   unsigned int
707   symbol_section_and_value(unsigned int sym, Address* value);
708
709   // Return a pointer to the Symbol_value structure which holds the
710   // value of a local symbol.
711   const Symbol_value<size>*
712   local_symbol(unsigned int sym) const
713   {
714     gold_assert(sym < this->local_values_.size());
715     return &this->local_values_[sym];
716   }
717
718   // Return the index of local symbol SYM in the ordinary symbol
719   // table.  A value of -1U means that the symbol is not being output.
720   unsigned int
721   symtab_index(unsigned int sym) const
722   {
723     gold_assert(sym < this->local_values_.size());
724     return this->local_values_[sym].output_symtab_index();
725   }
726
727   // Return the appropriate Sized_target structure.
728   Sized_target<size, big_endian>*
729   sized_target()
730   {
731     return this->Object::sized_target
732       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
733           SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
734   }
735
736   // Return the value of the local symbol symndx.
737   Address
738   local_symbol_value(unsigned int symndx) const;
739
740   // Return the value of a local symbol defined in input section
741   // SHNDX, with value VALUE, adding addend ADDEND.  IS_SECTION_SYMBOL
742   // indicates whether the symbol is a section symbol.  This handles
743   // SHF_MERGE sections.
744   Address
745   local_value(unsigned int shndx, Address value, bool is_section_symbol,
746               Address addend) const;
747
748   // Return whether the local symbol SYMNDX has a GOT offset.
749   bool
750   local_has_got_offset(unsigned int symndx) const
751   {
752     return (this->local_got_offsets_.find(symndx)
753             != this->local_got_offsets_.end());
754   }
755
756   // Return the GOT offset of the local symbol SYMNDX.
757   unsigned int
758   local_got_offset(unsigned int symndx) const
759   {
760     Local_got_offsets::const_iterator p =
761         this->local_got_offsets_.find(symndx);
762     gold_assert(p != this->local_got_offsets_.end());
763     return p->second;
764   }
765
766   // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
767   void
768   set_local_got_offset(unsigned int symndx, unsigned int got_offset)
769   {
770     std::pair<Local_got_offsets::iterator, bool> ins =
771         this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
772     gold_assert(ins.second);
773   }
774
775   // Return the name of the symbol that spans the given offset in the
776   // specified section in this object.  This is used only for error
777   // messages and is not particularly efficient.
778   bool
779   get_symbol_location_info(unsigned int shndx, off_t offset,
780                            Symbol_location_info* info);
781
782   // Read the symbols.
783   void
784   do_read_symbols(Read_symbols_data*);
785
786   // Lay out the input sections.
787   void
788   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
789
790   // Add the symbols to the symbol table.
791   void
792   do_add_symbols(Symbol_table*, Read_symbols_data*);
793
794   // Read the relocs.
795   void
796   do_read_relocs(Read_relocs_data*);
797
798   // Scan the relocs and adjust the symbol table.
799   void
800   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
801                  Read_relocs_data*);
802
803   // Finalize the local symbols.
804   unsigned int
805   do_finalize_local_symbols(unsigned int, off_t,
806                             Stringpool_template<char>*);
807
808   // Relocate the input sections and write out the local symbols.
809   void
810   do_relocate(const General_options& options, const Symbol_table* symtab,
811               const Layout*, Output_file* of);
812
813   // Get the name of a section.
814   std::string
815   do_section_name(unsigned int shndx)
816   { return this->elf_file_.section_name(shndx); }
817
818   // Return the location of the contents of a section.
819   Object::Location
820   do_section_contents(unsigned int shndx)
821   { return this->elf_file_.section_contents(shndx); }
822
823   // Return section flags.
824   uint64_t
825   do_section_flags(unsigned int shndx)
826   { return this->elf_file_.section_flags(shndx); }
827
828   // Return section type.
829   unsigned int
830   do_section_type(unsigned int shndx)
831   { return this->elf_file_.section_type(shndx); }
832
833   // Return the section link field.
834   unsigned int
835   do_section_link(unsigned int shndx)
836   { return this->elf_file_.section_link(shndx); }
837
838   // Return the section info field.
839   unsigned int
840   do_section_info(unsigned int shndx)
841   { return this->elf_file_.section_info(shndx); }
842
843  private:
844   // For convenience.
845   typedef Sized_relobj<size, big_endian> This;
846   static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
847   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
848   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
849   typedef elfcpp::Shdr<size, big_endian> Shdr;
850
851   // Find the SHT_SYMTAB section, given the section headers.
852   void
853   find_symtab(const unsigned char* pshdrs);
854
855   // Return whether SHDR has the right flags for a GNU style exception
856   // frame section.
857   bool
858   check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
859
860   // Return whether there is a section named .eh_frame which might be
861   // a GNU style exception frame section.
862   bool
863   find_eh_frame(const unsigned char* pshdrs, const char* names,
864                 off_t names_size) const;
865
866   // Whether to include a section group in the link.
867   bool
868   include_section_group(Layout*, unsigned int,
869                         const elfcpp::Shdr<size, big_endian>&,
870                         std::vector<bool>*);
871
872   // Whether to include a linkonce section in the link.
873   bool
874   include_linkonce_section(Layout*, const char*,
875                            const elfcpp::Shdr<size, big_endian>&);
876
877   // Views and sizes when relocating.
878   struct View_size
879   {
880     unsigned char* view;
881     typename elfcpp::Elf_types<size>::Elf_Addr address;
882     off_t offset;
883     off_t view_size;
884     bool is_input_output_view;
885   };
886
887   typedef std::vector<View_size> Views;
888
889   // Write section data to the output file.  Record the views and
890   // sizes in VIEWS for use when relocating.
891   void
892   write_sections(const unsigned char* pshdrs, Output_file*, Views*);
893
894   // Relocate the sections in the output file.
895   void
896   relocate_sections(const General_options& options, const Symbol_table*,
897                     const Layout*, const unsigned char* pshdrs, Views*);
898
899   // Write out the local symbols.
900   void
901   write_local_symbols(Output_file*,
902                       const Stringpool_template<char>*);
903
904   // The GOT offsets of local symbols.
905   typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
906
907   // General access to the ELF file.
908   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
909   // Index of SHT_SYMTAB section.
910   unsigned int symtab_shndx_;
911   // The number of local symbols.
912   unsigned int local_symbol_count_;
913   // The number of local symbols which go into the output file.
914   unsigned int output_local_symbol_count_;
915   // The entries in the symbol table for the external symbols.
916   Symbols symbols_;
917   // File offset for local symbols.
918   off_t local_symbol_offset_;
919   // Values of local symbols.
920   Local_values local_values_;
921   // GOT offsets for local symbols, indexed by symbol number.
922   Local_got_offsets local_got_offsets_;
923   // Whether this object has a GNU style .eh_frame section.
924   bool has_eh_frame_;
925 };
926
927 // A class to manage the list of all objects.
928
929 class Input_objects
930 {
931  public:
932   Input_objects()
933     : relobj_list_(), dynobj_list_(), target_(NULL), sonames_()
934   { }
935
936   // The type of the list of input relocateable objects.
937   typedef std::vector<Relobj*> Relobj_list;
938   typedef Relobj_list::const_iterator Relobj_iterator;
939
940   // The type of the list of input dynamic objects.
941   typedef std::vector<Dynobj*> Dynobj_list;
942   typedef Dynobj_list::const_iterator Dynobj_iterator;
943
944   // Add an object to the list.  Return true if all is well, or false
945   // if this object should be ignored.
946   bool
947   add_object(Object*);
948
949   // Get the target we should use for the output file.
950   Target*
951   target() const
952   { return this->target_; }
953
954   // For each dynamic object, check whether we've seen all of its
955   // explicit dependencies.
956   void
957   check_dynamic_dependencies() const;
958
959   // Iterate over all regular objects.
960
961   Relobj_iterator
962   relobj_begin() const
963   { return this->relobj_list_.begin(); }
964
965   Relobj_iterator
966   relobj_end() const
967   { return this->relobj_list_.end(); }
968
969   // Iterate over all dynamic objects.
970
971   Dynobj_iterator
972   dynobj_begin() const
973   { return this->dynobj_list_.begin(); }
974
975   Dynobj_iterator
976   dynobj_end() const
977   { return this->dynobj_list_.end(); }
978
979   // Return whether we have seen any dynamic objects.
980   bool
981   any_dynamic() const
982   { return !this->dynobj_list_.empty(); }
983
984   // Return the number of input objects.
985   int
986   number_of_input_objects() const
987   { return this->relobj_list_.size() + this->dynobj_list_.size(); }
988
989  private:
990   Input_objects(const Input_objects&);
991   Input_objects& operator=(const Input_objects&);
992
993   // The list of ordinary objects included in the link.
994   Relobj_list relobj_list_;
995   // The list of dynamic objects included in the link.
996   Dynobj_list dynobj_list_;
997   // The target.
998   Target* target_;
999   // SONAMEs that we have seen.
1000   Unordered_set<std::string> sonames_;
1001 };
1002
1003 // Some of the information we pass to the relocation routines.  We
1004 // group this together to avoid passing a dozen different arguments.
1005
1006 template<int size, bool big_endian>
1007 struct Relocate_info
1008 {
1009   // Command line options.
1010   const General_options* options;
1011   // Symbol table.
1012   const Symbol_table* symtab;
1013   // Layout.
1014   const Layout* layout;
1015   // Object being relocated.
1016   Sized_relobj<size, big_endian>* object;
1017   // Section index of relocation section.
1018   unsigned int reloc_shndx;
1019   // Section index of section being relocated.
1020   unsigned int data_shndx;
1021
1022   // Return a string showing the location of a relocation.  This is
1023   // only used for error messages.
1024   std::string
1025   location(size_t relnum, off_t reloffset) const;
1026 };
1027
1028 // Return an Object appropriate for the input file.  P is BYTES long,
1029 // and holds the ELF header.
1030
1031 extern Object*
1032 make_elf_object(const std::string& name, Input_file*,
1033                 off_t offset, const unsigned char* p,
1034                 off_t bytes);
1035
1036 } // end namespace gold
1037
1038 #endif // !defined(GOLD_OBJECT_H)