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