Add Elf_file interface which can be used by both Sized_relobj and
[external/binutils.git] / gold / object.h
1 // object.h -- support for an object file for linking in gold  -*- C++ -*-
2
3 #ifndef GOLD_OBJECT_H
4 #define GOLD_OBJECT_H
5
6 #include <cassert>
7 #include <string>
8 #include <vector>
9
10 #include "elfcpp.h"
11 #include "elfcpp_file.h"
12 #include "fileread.h"
13 #include "target.h"
14
15 namespace gold
16 {
17
18 class General_options;
19 class Stringpool;
20 class Layout;
21 class Output_section;
22 class Output_file;
23 class Dynobj;
24
25 // Data to pass from read_symbols() to add_symbols().
26
27 struct Read_symbols_data
28 {
29   // Section headers.
30   File_view* section_headers;
31   // Section names.
32   File_view* section_names;
33   // Size of section name data in bytes.
34   off_t section_names_size;
35   // Symbol data.
36   File_view* symbols;
37   // Size of symbol data in bytes.
38   off_t symbols_size;
39   // Symbol names.
40   File_view* symbol_names;
41   // Size of symbol name data in bytes.
42   off_t symbol_names_size;
43 };
44
45 // Data about a single relocation section.  This is read in
46 // read_relocs and processed in scan_relocs.
47
48 struct Section_relocs
49 {
50   // Index of reloc section.
51   unsigned int reloc_shndx;
52   // Index of section that relocs apply to.
53   unsigned int data_shndx;
54   // Contents of reloc section.
55   File_view* contents;
56   // Reloc section type.
57   unsigned int sh_type;
58   // Number of reloc entries.
59   size_t reloc_count;
60 };
61
62 // Relocations in an object file.  This is read in read_relocs and
63 // processed in scan_relocs.
64
65 struct Read_relocs_data
66 {
67   typedef std::vector<Section_relocs> Relocs_list;
68   // The relocations.
69   Relocs_list relocs;
70   // The local symbols.
71   File_view* local_symbols;
72 };
73
74 // Object is an abstract base class which represents either a 32-bit
75 // or a 64-bit input object.  This can be a regular object file
76 // (ET_REL) or a shared object (ET_DYN).
77
78 class Object
79 {
80  public:
81   // NAME is the name of the object as we would report it to the user
82   // (e.g., libfoo.a(bar.o) if this is in an archive.  INPUT_FILE is
83   // used to read the file.  OFFSET is the offset within the input
84   // file--0 for a .o or .so file, something else for a .a file.
85   Object(const std::string& name, Input_file* input_file, bool is_dynamic,
86          off_t offset = 0)
87     : name_(name), input_file_(input_file), offset_(offset),
88       is_dynamic_(is_dynamic), target_(NULL)
89   { }
90
91   virtual ~Object()
92   { }
93
94   // Return the name of the object as we would report it to the tuser.
95   const std::string&
96   name() const
97   { return this->name_; }
98
99   // Return whether this is a dynamic object.
100   bool
101   is_dynamic() const
102   { return this->is_dynamic_; }
103
104   // Return the target structure associated with this object.
105   Target*
106   target() const
107   { return this->target_; }
108
109   // Lock the underlying file.
110   void
111   lock()
112   { this->input_file_->file().lock(); }
113
114   // Unlock the underlying file.
115   void
116   unlock()
117   { this->input_file_->file().unlock(); }
118
119   // Return whether the underlying file is locked.
120   bool
121   is_locked() const
122   { return this->input_file_->file().is_locked(); }
123
124   // Return the sized target structure associated with this object.
125   // This is like the target method but it returns a pointer of
126   // appropriate checked type.
127   template<int size, bool big_endian>
128   Sized_target<size, big_endian>*
129   sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
130
131   // Read the symbol information.
132   void
133   read_symbols(Read_symbols_data* sd)
134   { return this->do_read_symbols(sd); }
135
136   // Pass sections which should be included in the link to the Layout
137   // object, and record where the sections go in the output file.
138   void
139   layout(const General_options& options, Symbol_table* symtab,
140          Layout* layout, Read_symbols_data* sd)
141   { this->do_layout(options, symtab, layout, sd); }
142
143   // Add symbol information to the global symbol table.
144   void
145   add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
146   { this->do_add_symbols(symtab, sd); }
147
148   // Return a view of the contents of a section.  Set *PLEN to the
149   // size.
150   const unsigned char*
151   section_contents(unsigned int shndx, off_t* plen);
152
153   // Return the name of a section given a section index.  This is only
154   // used for error messages.
155   std::string
156   section_name(unsigned int shnum)
157   { return this->do_section_name(shnum); }
158
159   // Functions and types for the elfcpp::Elf_file interface.  This
160   // permit us to use Object as the File template parameter for
161   // elfcpp::Elf_file.
162
163   // The View class is returned by view.  It must support a single
164   // method, data().  This is trivial, because get_view does what we
165   // need.
166   class View
167   {
168    public:
169     View(const unsigned char* p)
170       : p_(p)
171     { }
172
173     const unsigned char*
174     data() const
175     { return this->p_; }
176
177    private:
178     const unsigned char* p_;
179   };
180
181   // Return a View.
182   View
183   view(off_t file_offset, off_t data_size)
184   { return View(this->get_view(file_offset, data_size)); }
185
186   // Report an error.
187   void
188   error(const char* format, ...) ATTRIBUTE_PRINTF_2;
189
190   // A location in the file.
191   struct Location
192   {
193     off_t file_offset;
194     off_t data_size;
195
196     Location(off_t fo, off_t ds)
197       : file_offset(fo), data_size(ds)
198     { }
199   };
200
201   // Get a View given a Location.
202   View view(Location loc)
203   { return View(this->get_view(loc.file_offset, loc.data_size)); }
204
205  protected:
206   // Read the symbols--implemented by child class.
207   virtual void
208   do_read_symbols(Read_symbols_data*) = 0;
209
210   // Lay out sections--implemented by child class.
211   virtual void
212   do_layout(const General_options&, Symbol_table*, Layout*,
213             Read_symbols_data*) = 0;
214
215   // Add symbol information to the global symbol table--implemented by
216   // child class.
217   virtual void
218   do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
219
220   // Return the location of the contents of a section.  Implemented by
221   // child class.
222   virtual Location
223   do_section_contents(unsigned int shnum) = 0;
224
225   // Get the name of a section--implemented by child class.
226   virtual std::string
227   do_section_name(unsigned int shnum) = 0;
228
229   // Get the file.
230   Input_file*
231   input_file() const
232   { return this->input_file_; }
233
234   // Get the offset into the file.
235   off_t
236   offset() const
237   { return this->offset_; }
238
239   // Get a view into the underlying file.
240   const unsigned char*
241   get_view(off_t start, off_t size)
242   { return this->input_file_->file().get_view(start + this->offset_, size); }
243
244   // Get a lasting view into the underlying file.
245   File_view*
246   get_lasting_view(off_t start, off_t size)
247   {
248     return this->input_file_->file().get_lasting_view(start + this->offset_,
249                                                       size);
250   }
251
252   // Read data from the underlying file.
253   void
254   read(off_t start, off_t size, void* p)
255   { this->input_file_->file().read(start + this->offset_, size, p); }
256
257   // Set the target.
258   void
259   set_target(Target* target)
260   { this->target_ = target; }
261
262  private:
263   // This class may not be copied.
264   Object(const Object&);
265   Object& operator=(const Object&);
266
267   // Name of object as printed to user.
268   std::string name_;
269   // For reading the file.
270   Input_file* input_file_;
271   // Offset within the file--0 for an object file, non-0 for an
272   // archive.
273   off_t offset_;
274   // Whether this is a dynamic object.
275   bool is_dynamic_;
276   // Target functions--may be NULL if the target is not known.
277   Target* target_;
278 };
279
280 // Implement sized_target inline for efficiency.  This approach breaks
281 // static type checking, but is made safe using asserts.
282
283 template<int size, bool big_endian>
284 inline Sized_target<size, big_endian>*
285 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
286 {
287   assert(this->target_->get_size() == size);
288   assert(this->target_->is_big_endian() ? big_endian : !big_endian);
289   return static_cast<Sized_target<size, big_endian>*>(this->target_);
290 }
291
292 // A regular object (ET_REL).  This is an abstract base class itself.
293 // The implementations is the template class Sized_relobj.
294
295 class Relobj : public Object
296 {
297  public:
298   Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
299     : Object(name, input_file, false, offset)
300   { }
301
302   // Read the relocs.
303   void
304   read_relocs(Read_relocs_data* rd)
305   { return this->do_read_relocs(rd); }
306
307   // Scan the relocs and adjust the symbol table.
308   void
309   scan_relocs(const General_options& options, Symbol_table* symtab,
310               Layout* layout, Read_relocs_data* rd)
311   { return this->do_scan_relocs(options, symtab, layout, rd); }
312
313   // Initial local symbol processing: set the offset where local
314   // symbol information will be stored; add local symbol names to
315   // *POOL; return the offset following the local symbols.
316   off_t
317   finalize_local_symbols(off_t off, Stringpool* pool)
318   { return this->do_finalize_local_symbols(off, pool); }
319
320   // Relocate the input sections and write out the local symbols.
321   void
322   relocate(const General_options& options, const Symbol_table* symtab,
323            const Layout* layout, Output_file* of)
324   { return this->do_relocate(options, symtab, layout, of); }
325
326   // Return whether an input section is being included in the link.
327   bool
328   is_section_included(unsigned int shnum) const
329   {
330     assert(shnum < this->map_to_output_.size());
331     return this->map_to_output_[shnum].output_section != NULL;
332   }
333
334   // Given a section index, return the corresponding Output_section
335   // (which will be NULL if the section is not included in the link)
336   // and set *POFF to the offset within that section.
337   inline Output_section*
338   output_section(unsigned int shnum, off_t* poff);
339
340   // Set the offset of an input section within its output section.
341   void
342   set_section_offset(unsigned int shndx, off_t off)
343   {
344     assert(shndx < this->map_to_output_.size());
345     this->map_to_output_[shndx].offset = off;
346   }
347
348  protected:
349   // What we need to know to map an input section to an output
350   // section.  We keep an array of these, one for each input section,
351   // indexed by the input section number.
352   struct Map_to_output
353   {
354     // The output section.  This is NULL if the input section is to be
355     // discarded.
356     Output_section* output_section;
357     // The offset within the output section.
358     off_t offset;
359   };
360
361   // Read the relocs--implemented by child class.
362   virtual void
363   do_read_relocs(Read_relocs_data*) = 0;
364
365   // Scan the relocs--implemented by child class.
366   virtual void
367   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
368                  Read_relocs_data*) = 0;
369
370   // Finalize local symbols--implemented by child class.
371   virtual off_t
372   do_finalize_local_symbols(off_t, Stringpool*) = 0;
373
374   // Relocate the input sections and write out the local
375   // symbols--implemented by child class.
376   virtual void
377   do_relocate(const General_options& options, const Symbol_table* symtab,
378               const Layout*, Output_file* of) = 0;
379
380   // Get the number of sections.
381   unsigned int
382   shnum() const
383   { return this->shnum_; }
384
385   // Set the number of sections.
386   void
387   set_shnum(int shnum)
388   { this->shnum_ = shnum; }
389
390   // Return the vector mapping input sections to output sections.
391   std::vector<Map_to_output>&
392   map_to_output()
393   { return this->map_to_output_; }
394
395  private:
396   // Number of input sections.
397   unsigned int shnum_;
398   // Mapping from input sections to output section.
399   std::vector<Map_to_output> map_to_output_;
400 };
401
402 // Implement Object::output_section inline for efficiency.
403 inline Output_section*
404 Relobj::output_section(unsigned int shnum, off_t* poff)
405 {
406   assert(shnum < this->map_to_output_.size());
407   const Map_to_output& mo(this->map_to_output_[shnum]);
408   *poff = mo.offset;
409   return mo.output_section;
410 }
411
412 // A regular object file.  This is size and endian specific.
413
414 template<int size, bool big_endian>
415 class Sized_relobj : public Relobj
416 {
417  public:
418   Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
419                const typename elfcpp::Ehdr<size, big_endian>&);
420
421   ~Sized_relobj();
422
423   // Set up the object file based on the ELF header.
424   void
425   setup(const typename elfcpp::Ehdr<size, big_endian>&);
426
427   // Read the symbols.
428   void
429   do_read_symbols(Read_symbols_data*);
430
431   // Add the symbols to the symbol table.
432   void
433   do_add_symbols(Symbol_table*, Read_symbols_data*);
434
435   // Read the relocs.
436   void
437   do_read_relocs(Read_relocs_data*);
438
439   // Scan the relocs and adjust the symbol table.
440   void
441   do_scan_relocs(const General_options&, Symbol_table*, Layout*,
442                  Read_relocs_data*);
443
444   // Lay out the input sections.
445   void
446   do_layout(const General_options&, Symbol_table*, Layout*,
447             Read_symbols_data*);
448
449   // Finalize the local symbols.
450   off_t
451   do_finalize_local_symbols(off_t, Stringpool*);
452
453   // Relocate the input sections and write out the local symbols.
454   void
455   do_relocate(const General_options& options, const Symbol_table* symtab,
456               const Layout*, Output_file* of);
457
458   // Get the name of a section.
459   std::string
460   do_section_name(unsigned int shndx)
461   { return this->elf_file_.section_name(shndx); }
462
463   // Return the location of the contents of a section.
464   Location
465   do_section_contents(unsigned int shndx)
466   { return this->elf_file_.section_contents(shndx); }
467
468   // Return the appropriate Sized_target structure.
469   Sized_target<size, big_endian>*
470   sized_target()
471   {
472     return this->Object::sized_target
473       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
474           SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
475   }
476
477  private:
478   // For convenience.
479   typedef Sized_relobj<size, big_endian> This;
480   static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
481   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
482   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
483   typedef elfcpp::Shdr<size, big_endian> Shdr;
484
485   // Whether to include a section group in the link.
486   bool
487   include_section_group(Layout*, unsigned int,
488                         const elfcpp::Shdr<size, big_endian>&,
489                         std::vector<bool>*);
490
491   // Whether to include a linkonce section in the link.
492   bool
493   include_linkonce_section(Layout*, const char*,
494                            const elfcpp::Shdr<size, big_endian>&);
495
496   // Views and sizes when relocating.
497   struct View_size
498   {
499     unsigned char* view;
500     typename elfcpp::Elf_types<size>::Elf_Addr address;
501     off_t offset;
502     off_t view_size;
503   };
504
505   typedef std::vector<View_size> Views;
506
507   // Write section data to the output file.  Record the views and
508   // sizes in VIEWS for use when relocating.
509   void
510   write_sections(const unsigned char* pshdrs, Output_file*, Views*);
511
512   // Relocate the sections in the output file.
513   void
514   relocate_sections(const General_options& options, const Symbol_table*,
515                     const Layout*, const unsigned char* pshdrs, Views*);
516
517   // Write out the local symbols.
518   void
519   write_local_symbols(Output_file*, const Stringpool*);
520
521   // General access to the ELF file.
522   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
523   // If non-NULL, a view of the section header data.
524   File_view* section_headers_;
525   // Index of SHT_SYMTAB section.
526   unsigned int symtab_shndx_;
527   // The number of local symbols.
528   unsigned int local_symbol_count_;
529   // The number of local symbols which go into the output file.
530   unsigned int output_local_symbol_count_;
531   // The entries in the symbol table for the external symbols.
532   Symbol** symbols_;
533   // File offset for local symbols.
534   off_t local_symbol_offset_;
535   // Values of local symbols.
536   typename elfcpp::Elf_types<size>::Elf_Addr *values_;
537 };
538
539 // A class to manage the list of all objects.
540
541 class Input_objects
542 {
543  public:
544   Input_objects()
545     : relobj_list_(), target_(NULL)
546   { }
547
548   // The type of the list of input relocateable objects.
549   typedef std::vector<Relobj*> Relobj_list;
550   typedef Relobj_list::const_iterator Relobj_iterator;
551
552   // The type of the list of input dynamic objects.
553   typedef std::vector<Dynobj*> Dynobj_list;
554   typedef Dynobj_list::const_iterator Dynobj_iterator;
555
556   // Add an object to the list.
557   void
558   add_object(Object*);
559
560   // Get the target we should use for the output file.
561   Target*
562   target() const
563   { return this->target_; }
564
565   // Iterate over all regular objects.
566
567   Relobj_iterator
568   relobj_begin() const
569   { return this->relobj_list_.begin(); }
570
571   Relobj_iterator
572   relobj_end() const
573   { return this->relobj_list_.end(); }
574
575   // Iterate over all dynamic objects.
576
577   Dynobj_iterator
578   dynobj_begin() const
579   { return this->dynobj_list_.begin(); }
580
581   Dynobj_iterator
582   dynobj_end() const
583   { return this->dynobj_list_.end(); }
584
585   // Return whether we have seen any dynamic objects.
586   bool
587   any_dynamic() const
588   { return !this->dynobj_list_.empty(); }
589
590  private:
591   Input_objects(const Input_objects&);
592   Input_objects& operator=(const Input_objects&);
593
594   Relobj_list relobj_list_;
595   Dynobj_list dynobj_list_;
596   Target* target_;
597 };
598
599 // Some of the information we pass to the relocation routines.  We
600 // group this together to avoid passing a dozen different arguments.
601
602 template<int size, bool big_endian>
603 struct Relocate_info
604 {
605   // Command line options.
606   const General_options* options;
607   // Symbol table.
608   const Symbol_table* symtab;
609   // Layout.
610   const Layout* layout;
611   // Object being relocated.
612   Sized_relobj<size, big_endian>* object;
613   // Number of local symbols.
614   unsigned int local_symbol_count;
615   // Values of local symbols.
616   typename elfcpp::Elf_types<size>::Elf_Addr *values;
617   // Global symbols.
618   Symbol** symbols;
619   // Section index of relocation section.
620   unsigned int reloc_shndx;
621   // Section index of section being relocated.
622   unsigned int data_shndx;
623
624   // Return a string showing the location of a relocation.  This is
625   // only used for error messages.
626   std::string
627   location(size_t relnum, off_t reloffset) const;
628 };
629
630 // Return an Object appropriate for the input file.  P is BYTES long,
631 // and holds the ELF header.
632
633 extern Object*
634 make_elf_object(const std::string& name, Input_file*,
635                 off_t offset, const unsigned char* p,
636                 off_t bytes);
637
638 } // end namespace gold
639
640 #endif // !defined(GOLD_OBJECT_H)