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