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