OSDN Git Service

From Craig Silverstein: Use relocations in reporting error message
[pf3gnuchains/pf3gnuchains4x.git] / gold / dynobj.h
1 // dynobj.h -- dynamic object support for 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_DYNOBJ_H
24 #define GOLD_DYNOBJ_H
25
26 #include <vector>
27
28 #include "stringpool.h"
29 #include "object.h"
30
31 namespace gold
32 {
33
34 class General_options;
35
36 // A dynamic object (ET_DYN).  This is an abstract base class itself.
37 // The implementations is the template class Sized_dynobj.
38
39 class Dynobj : public Object
40 {
41  public:
42   Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
43
44   // Return the name to use in a DT_NEEDED entry for this object.
45   const char*
46   soname() const;
47
48   // Compute the ELF hash code for a string.
49   static uint32_t
50   elf_hash(const char*);
51
52   // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
53   // DYNSYMS is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the
54   // number of local dynamic symbols, which is the index of the first
55   // dynamic gobal symbol.
56   static void
57   create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
58                         unsigned int local_dynsym_count,
59                         unsigned char** pphash,
60                         unsigned int* phashlen);
61
62   // Create a GNU hash table, setting *PPHASH and *PHASHLEN.  DYNSYMS
63   // is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the number
64   // of local dynamic symbols, which is the index of the first dynamic
65   // gobal symbol.
66   static void
67   create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
68                         unsigned int local_dynsym_count,
69                         unsigned char** pphash, unsigned int* phashlen);
70
71  protected:
72   // Set the DT_SONAME string.
73   void
74   set_soname_string(const char* s)
75   { this->soname_.assign(s); }
76
77  private:
78   // Compute the GNU hash code for a string.
79   static uint32_t
80   gnu_hash(const char*);
81
82   // Compute the number of hash buckets to use.
83   static unsigned int
84   compute_bucket_count(const std::vector<uint32_t>& hashcodes,
85                        bool for_gnu_hash_table);
86
87   // Sized version of create_elf_hash_table.
88   template<bool big_endian>
89   static void
90   sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
91                               const std::vector<uint32_t>& chain,
92                               unsigned char* phash,
93                               unsigned int hashlen);
94
95   // Sized version of create_gnu_hash_table.
96   template<int size, bool big_endian>
97   static void
98   sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
99                               const std::vector<uint32_t>& dynsym_hashvals,
100                               unsigned int unhashed_dynsym_count,
101                               unsigned char** pphash,
102                               unsigned int* phashlen);
103
104   // The DT_SONAME name, if any.
105   std::string soname_;
106 };
107
108 // A dynamic object, size and endian specific version.
109
110 template<int size, bool big_endian>
111 class Sized_dynobj : public Dynobj
112 {
113  public:
114   Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
115                const typename elfcpp::Ehdr<size, big_endian>&);
116
117   // Set up the object file based on the ELF header.
118   void
119   setup(const typename elfcpp::Ehdr<size, big_endian>&);
120
121   // Read the symbols.
122   void
123   do_read_symbols(Read_symbols_data*);
124
125   // Lay out the input sections.
126   void
127   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
128
129   // Add the symbols to the symbol table.
130   void
131   do_add_symbols(Symbol_table*, Read_symbols_data*);
132
133   // Get the name of a section.
134   std::string
135   do_section_name(unsigned int shndx)
136   { return this->elf_file_.section_name(shndx); }
137
138   // Return a view of the contents of a section.  Set *PLEN to the
139   // size.
140   Object::Location
141   do_section_contents(unsigned int shndx)
142   { return this->elf_file_.section_contents(shndx); }
143
144   // Return section flags.
145   uint64_t
146   do_section_flags(unsigned int shndx)
147   { return this->elf_file_.section_flags(shndx); }
148
149   // Return section type.
150   unsigned int
151   do_section_type(unsigned int shndx)
152   { return this->elf_file_.section_type(shndx); }
153
154   // Return the section link field.
155   unsigned int
156   do_section_link(unsigned int shndx)
157   { return this->elf_file_.section_link(shndx); }
158
159   // Return the section link field.
160   unsigned int
161   do_section_info(unsigned int shndx)
162   { return this->elf_file_.section_info(shndx); }
163
164  private:
165   // For convenience.
166   typedef Sized_dynobj<size, big_endian> This;
167   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
168   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
169   static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
170   typedef elfcpp::Shdr<size, big_endian> Shdr;
171   typedef elfcpp::Dyn<size, big_endian> Dyn;
172
173   // Find the dynamic symbol table and the version sections, given the
174   // section headers.
175   void
176   find_dynsym_sections(const unsigned char* pshdrs,
177                        unsigned int* pdynshm_shndx,
178                        unsigned int* pversym_shndx,
179                        unsigned int* pverdef_shndx,
180                        unsigned int* pverneed_shndx,
181                        unsigned int* pdynamic_shndx);
182
183   // Read the dynamic symbol section SHNDX.
184   void
185   read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
186                       elfcpp::SHT type, unsigned int link,
187                       File_view** view, off_t* view_size,
188                       unsigned int* view_info);
189
190   // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
191   // The STRTAB parameters may have the relevant string table.
192   void
193   set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
194              unsigned int strtab_shndx, const unsigned char* strtabu,
195              off_t strtab_size);
196
197   // Mapping from version number to version name.
198   typedef std::vector<const char*> Version_map;
199
200   // Create the version map.
201   void
202   make_version_map(Read_symbols_data* sd, Version_map*) const;
203
204   // Add version definitions to the version map.
205   void
206   make_verdef_map(Read_symbols_data* sd, Version_map*) const;
207
208   // Add version references to the version map.
209   void
210   make_verneed_map(Read_symbols_data* sd, Version_map*) const;
211
212   // Add an entry to the version map.
213   void
214   set_version_map(Version_map*, unsigned int ndx, const char* name) const;
215
216   // General access to the ELF file.
217   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
218 };
219
220 // A base class for Verdef and Verneed_version which just handles the
221 // version index which will be stored in the SHT_GNU_versym section.
222
223 class Version_base
224 {
225  public:
226   Version_base()
227     : index_(-1U)
228   { }
229
230   virtual
231   ~Version_base()
232   { }
233
234   // Return the version index.
235   unsigned int
236   index() const
237   {
238     gold_assert(this->index_ != -1U);
239     return this->index_;
240   }
241
242   // Set the version index.
243   void
244   set_index(unsigned int index)
245   {
246     gold_assert(this->index_ == -1U);
247     this->index_ = index;
248   }
249
250   // Clear the weak flag in a version definition.
251   virtual void
252   clear_weak() = 0;
253
254  private:
255   Version_base(const Version_base&);
256   Version_base& operator=(const Version_base&);
257
258   // The index of the version definition or reference.
259   unsigned int index_;
260 };
261
262 // This class handles a version being defined in the file we are
263 // generating.
264
265 class Verdef : public Version_base
266 {
267  public:
268   Verdef(const char* name, bool is_base, bool is_weak, bool is_symbol_created)
269     : name_(name), deps_(), is_base_(is_base), is_weak_(is_weak),
270       is_symbol_created_(is_symbol_created)
271   { }
272
273   // Return the version name.
274   const char*
275   name() const
276   { return this->name_; }
277
278   // Return the number of dependencies.
279   unsigned int
280   count_dependencies() const
281   { return this->deps_.size(); }
282
283   // Add a dependency to this version.  The NAME should be
284   // canonicalized in the dynamic Stringpool.
285   void
286   add_dependency(const char* name)
287   { this->deps_.push_back(name); }
288
289   // Return whether this definition is weak.
290   bool
291   is_weak() const
292   { return this->is_weak_; }
293
294   // Clear the weak flag.
295   void
296   clear_weak()
297   { this->is_weak_ = false; }
298
299   // Return whether a version symbol has been created for this
300   // definition.
301   bool
302   is_symbol_created() const
303   { return this->is_symbol_created_; }
304
305   // Write contents to buffer.
306   template<int size, bool big_endian>
307   unsigned char*
308   write(const Stringpool*, bool is_last, unsigned char*
309         ACCEPT_SIZE_ENDIAN) const;
310
311  private:
312   Verdef(const Verdef&);
313   Verdef& operator=(const Verdef&);
314
315   // The type of the list of version dependencies.  Each dependency
316   // should be canonicalized in the dynamic Stringpool.
317   typedef std::vector<const char*> Deps;
318
319   // The name of this version.  This should be canonicalized in the
320   // dynamic Stringpool.
321   const char* name_;
322   // A list of other versions which this version depends upon.
323   Deps deps_;
324   // Whether this is the base version.
325   bool is_base_;
326   // Whether this version is weak.
327   bool is_weak_;
328   // Whether a version symbol has been created.
329   bool is_symbol_created_;
330 };
331
332 // A referened version.  This will be associated with a filename by
333 // Verneed.
334
335 class Verneed_version : public Version_base
336 {
337  public:
338   Verneed_version(const char* version)
339     : version_(version)
340   { }
341
342   // Return the version name.
343   const char*
344   version() const
345   { return this->version_; }
346
347   // Clear the weak flag.  This is invalid for a reference.
348   void
349   clear_weak()
350   { gold_unreachable(); }
351
352  private:
353   Verneed_version(const Verneed_version&);
354   Verneed_version& operator=(const Verneed_version&);
355
356   const char* version_;
357 };
358
359 // Version references in a single dynamic object.
360
361 class Verneed
362 {
363  public:
364   Verneed(const char* filename)
365     : filename_(filename), need_versions_()
366   { }
367
368   ~Verneed();
369
370   // Return the file name.
371   const char*
372   filename() const
373   { return this->filename_; }
374
375   // Return the number of versions.
376   unsigned int
377   count_versions() const
378   { return this->need_versions_.size(); }
379
380   // Add a version name.  The name should be canonicalized in the
381   // dynamic Stringpool.  If the name is already present, this does
382   // nothing.
383   Verneed_version*
384   add_name(const char* name);
385
386   // Set the version indexes, starting at INDEX.  Return the updated
387   // INDEX.
388   unsigned int
389   finalize(unsigned int index);
390
391   // Write contents to buffer.
392   template<int size, bool big_endian>
393   unsigned char*
394   write(const Stringpool*, bool is_last, unsigned char*
395         ACCEPT_SIZE_ENDIAN) const;
396
397  private:
398   Verneed(const Verneed&);
399   Verneed& operator=(const Verneed&);
400
401   // The type of the list of version names.  Each name should be
402   // canonicalized in the dynamic Stringpool.
403   typedef std::vector<Verneed_version*> Need_versions;
404
405   // The filename of the dynamic object.  This should be
406   // canonicalized in the dynamic Stringpool.
407   const char* filename_;
408   // The list of version names.
409   Need_versions need_versions_;
410 };
411
412 // This class handles version definitions and references which go into
413 // the output file.
414
415 class Versions
416 {
417  public:
418   Versions()
419     : defs_(), needs_(), version_table_(), is_finalized_(false)
420   { }
421
422   ~Versions();
423
424   // SYM is going into the dynamic symbol table and has a version.
425   // Record the appropriate version information.
426   void
427   record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
428
429   // Set the version indexes.  DYNSYM_INDEX is the index we should use
430   // for the next dynamic symbol.  We add new dynamic symbols to SYMS
431   // and return an updated DYNSYM_INDEX.
432   unsigned int
433   finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
434            std::vector<Symbol*>* syms);
435
436   // Return whether there are any version definitions.
437   bool
438   any_defs() const
439   { return !this->defs_.empty(); }
440
441   // Return whether there are any version references.
442   bool
443   any_needs() const
444   { return !this->needs_.empty(); }
445
446   // Build an allocated buffer holding the contents of the symbol
447   // version section (.gnu.version).
448   template<int size, bool big_endian>
449   void
450   symbol_section_contents(const Symbol_table*, const Stringpool*,
451                           unsigned int local_symcount,
452                           const std::vector<Symbol*>& syms,
453                           unsigned char**, unsigned int*
454                           ACCEPT_SIZE_ENDIAN) const;
455
456   // Build an allocated buffer holding the contents of the version
457   // definition section (.gnu.version_d).
458   template<int size, bool big_endian>
459   void
460   def_section_contents(const Stringpool*, unsigned char**,
461                        unsigned int* psize, unsigned int* pentries
462                        ACCEPT_SIZE_ENDIAN) const;
463
464   // Build an allocated buffer holding the contents of the version
465   // reference section (.gnu.version_r).
466   template<int size, bool big_endian>
467   void
468   need_section_contents(const Stringpool*, unsigned char**,
469                         unsigned int* psize, unsigned int* pentries
470                         ACCEPT_SIZE_ENDIAN) const;
471
472  private:
473   // The type of the list of version definitions.
474   typedef std::vector<Verdef*> Defs;
475
476   // The type of the list of version references.
477   typedef std::vector<Verneed*> Needs;
478
479   // Handle a symbol SYM defined with version VERSION.
480   void
481   add_def(const Symbol* sym, const char* version, Stringpool::Key);
482
483   // Add a reference to version NAME in file FILENAME.
484   void
485   add_need(Stringpool*, const char* filename, const char* name,
486            Stringpool::Key);
487
488   // Get the dynamic object to use for SYM.
489   Dynobj*
490   get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
491
492   // Return the version index to use for SYM.
493   unsigned int
494   version_index(const Symbol_table*, const Stringpool*,
495                 const Symbol* sym) const;
496
497   // We keep a hash table mapping canonicalized name/version pairs to
498   // a version base.
499   typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
500
501   struct Version_table_hash
502   {
503     size_t
504     operator()(const Key& k) const
505     { return k.first + k.second; }
506   };
507
508   struct Version_table_eq
509   {
510     bool
511     operator()(const Key& k1, const Key& k2) const
512     { return k1.first == k2.first && k1.second == k2.second; }
513   };
514
515   typedef Unordered_map<Key, Version_base*, Version_table_hash,
516                         Version_table_eq> Version_table;
517
518   // The version definitions.
519   Defs defs_;
520   // The version references.
521   Needs needs_;
522   // The mapping from a canonicalized version/filename pair to a
523   // version index.  The filename may be NULL.
524   Version_table version_table_;
525   // Whether the version indexes have been set.
526   bool is_finalized_;
527 };
528
529 } // End namespace gold.
530
531 #endif // !defined(GOLD_DYNOBJ_H)