OSDN Git Service

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