Generate a complete exception frame header. Discard duplicate
[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
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  private:
160   // For convenience.
161   typedef Sized_dynobj<size, big_endian> This;
162   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
163   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
164   static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
165   typedef elfcpp::Shdr<size, big_endian> Shdr;
166   typedef elfcpp::Dyn<size, big_endian> Dyn;
167
168   // Find the dynamic symbol table and the version sections, given the
169   // section headers.
170   void
171   find_dynsym_sections(const unsigned char* pshdrs,
172                        unsigned int* pdynshm_shndx,
173                        unsigned int* pversym_shndx,
174                        unsigned int* pverdef_shndx,
175                        unsigned int* pverneed_shndx,
176                        unsigned int* pdynamic_shndx);
177
178   // Read the dynamic symbol section SHNDX.
179   void
180   read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
181                       elfcpp::SHT type, unsigned int link,
182                       File_view** view, off_t* view_size,
183                       unsigned int* view_info);
184
185   // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
186   // The STRTAB parameters may have the relevant string table.
187   void
188   set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
189              unsigned int strtab_shndx, const unsigned char* strtabu,
190              off_t strtab_size);
191
192   // Mapping from version number to version name.
193   typedef std::vector<const char*> Version_map;
194
195   // Create the version map.
196   void
197   make_version_map(Read_symbols_data* sd, Version_map*) const;
198
199   // Add version definitions to the version map.
200   void
201   make_verdef_map(Read_symbols_data* sd, Version_map*) const;
202
203   // Add version references to the version map.
204   void
205   make_verneed_map(Read_symbols_data* sd, Version_map*) const;
206
207   // Add an entry to the version map.
208   void
209   set_version_map(Version_map*, unsigned int ndx, const char* name) const;
210
211   // General access to the ELF file.
212   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
213 };
214
215 // A base class for Verdef and Verneed_version which just handles the
216 // version index which will be stored in the SHT_GNU_versym section.
217
218 class Version_base
219 {
220  public:
221   Version_base()
222     : index_(-1U)
223   { }
224
225   virtual
226   ~Version_base()
227   { }
228
229   // Return the version index.
230   unsigned int
231   index() const
232   {
233     gold_assert(this->index_ != -1U);
234     return this->index_;
235   }
236
237   // Set the version index.
238   void
239   set_index(unsigned int index)
240   {
241     gold_assert(this->index_ == -1U);
242     this->index_ = index;
243   }
244
245   // Clear the weak flag in a version definition.
246   virtual void
247   clear_weak() = 0;
248
249  private:
250   Version_base(const Version_base&);
251   Version_base& operator=(const Version_base&);
252
253   // The index of the version definition or reference.
254   unsigned int index_;
255 };
256
257 // This class handles a version being defined in the file we are
258 // generating.
259
260 class Verdef : public Version_base
261 {
262  public:
263   Verdef(const char* name, bool is_base, bool is_weak, bool is_symbol_created)
264     : name_(name), deps_(), is_base_(is_base), is_weak_(is_weak),
265       is_symbol_created_(is_symbol_created)
266   { }
267
268   // Return the version name.
269   const char*
270   name() const
271   { return this->name_; }
272
273   // Return the number of dependencies.
274   unsigned int
275   count_dependencies() const
276   { return this->deps_.size(); }
277
278   // Add a dependency to this version.  The NAME should be
279   // canonicalized in the dynamic Stringpool.
280   void
281   add_dependency(const char* name)
282   { this->deps_.push_back(name); }
283
284   // Return whether this definition is weak.
285   bool
286   is_weak() const
287   { return this->is_weak_; }
288
289   // Clear the weak flag.
290   void
291   clear_weak()
292   { this->is_weak_ = false; }
293
294   // Return whether a version symbol has been created for this
295   // definition.
296   bool
297   is_symbol_created() const
298   { return this->is_symbol_created_; }
299
300   // Write contents to buffer.
301   template<int size, bool big_endian>
302   unsigned char*
303   write(const Stringpool*, bool is_last, unsigned char*
304         ACCEPT_SIZE_ENDIAN) const;
305
306  private:
307   Verdef(const Verdef&);
308   Verdef& operator=(const Verdef&);
309
310   // The type of the list of version dependencies.  Each dependency
311   // should be canonicalized in the dynamic Stringpool.
312   typedef std::vector<const char*> Deps;
313
314   // The name of this version.  This should be canonicalized in the
315   // dynamic Stringpool.
316   const char* name_;
317   // A list of other versions which this version depends upon.
318   Deps deps_;
319   // Whether this is the base version.
320   bool is_base_;
321   // Whether this version is weak.
322   bool is_weak_;
323   // Whether a version symbol has been created.
324   bool is_symbol_created_;
325 };
326
327 // A referened version.  This will be associated with a filename by
328 // Verneed.
329
330 class Verneed_version : public Version_base
331 {
332  public:
333   Verneed_version(const char* version)
334     : version_(version)
335   { }
336
337   // Return the version name.
338   const char*
339   version() const
340   { return this->version_; }
341
342   // Clear the weak flag.  This is invalid for a reference.
343   void
344   clear_weak()
345   { gold_unreachable(); }
346
347  private:
348   Verneed_version(const Verneed_version&);
349   Verneed_version& operator=(const Verneed_version&);
350
351   const char* version_;
352 };
353
354 // Version references in a single dynamic object.
355
356 class Verneed
357 {
358  public:
359   Verneed(const char* filename)
360     : filename_(filename), need_versions_()
361   { }
362
363   ~Verneed();
364
365   // Return the file name.
366   const char*
367   filename() const
368   { return this->filename_; }
369
370   // Return the number of versions.
371   unsigned int
372   count_versions() const
373   { return this->need_versions_.size(); }
374
375   // Add a version name.  The name should be canonicalized in the
376   // dynamic Stringpool.  If the name is already present, this does
377   // nothing.
378   Verneed_version*
379   add_name(const char* name);
380
381   // Set the version indexes, starting at INDEX.  Return the updated
382   // INDEX.
383   unsigned int
384   finalize(unsigned int index);
385
386   // Write contents to buffer.
387   template<int size, bool big_endian>
388   unsigned char*
389   write(const Stringpool*, bool is_last, unsigned char*
390         ACCEPT_SIZE_ENDIAN) const;
391
392  private:
393   Verneed(const Verneed&);
394   Verneed& operator=(const Verneed&);
395
396   // The type of the list of version names.  Each name should be
397   // canonicalized in the dynamic Stringpool.
398   typedef std::vector<Verneed_version*> Need_versions;
399
400   // The filename of the dynamic object.  This should be
401   // canonicalized in the dynamic Stringpool.
402   const char* filename_;
403   // The list of version names.
404   Need_versions need_versions_;
405 };
406
407 // This class handles version definitions and references which go into
408 // the output file.
409
410 class Versions
411 {
412  public:
413   Versions()
414     : defs_(), needs_(), version_table_(), is_finalized_(false)
415   { }
416
417   ~Versions();
418
419   // SYM is going into the dynamic symbol table and has a version.
420   // Record the appropriate version information.
421   void
422   record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
423
424   // Set the version indexes.  DYNSYM_INDEX is the index we should use
425   // for the next dynamic symbol.  We add new dynamic symbols to SYMS
426   // and return an updated DYNSYM_INDEX.
427   unsigned int
428   finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
429            std::vector<Symbol*>* syms);
430
431   // Return whether there are any version definitions.
432   bool
433   any_defs() const
434   { return !this->defs_.empty(); }
435
436   // Return whether there are any version references.
437   bool
438   any_needs() const
439   { return !this->needs_.empty(); }
440
441   // Build an allocated buffer holding the contents of the symbol
442   // version section (.gnu.version).
443   template<int size, bool big_endian>
444   void
445   symbol_section_contents(const Symbol_table*, const Stringpool*,
446                           unsigned int local_symcount,
447                           const std::vector<Symbol*>& syms,
448                           unsigned char**, unsigned int*
449                           ACCEPT_SIZE_ENDIAN) const;
450
451   // Build an allocated buffer holding the contents of the version
452   // definition section (.gnu.version_d).
453   template<int size, bool big_endian>
454   void
455   def_section_contents(const Stringpool*, unsigned char**,
456                        unsigned int* psize, unsigned int* pentries
457                        ACCEPT_SIZE_ENDIAN) const;
458
459   // Build an allocated buffer holding the contents of the version
460   // reference section (.gnu.version_r).
461   template<int size, bool big_endian>
462   void
463   need_section_contents(const Stringpool*, unsigned char**,
464                         unsigned int* psize, unsigned int* pentries
465                         ACCEPT_SIZE_ENDIAN) const;
466
467  private:
468   // The type of the list of version definitions.
469   typedef std::vector<Verdef*> Defs;
470
471   // The type of the list of version references.
472   typedef std::vector<Verneed*> Needs;
473
474   // Handle a symbol SYM defined with version VERSION.
475   void
476   add_def(const Symbol* sym, const char* version, Stringpool::Key);
477
478   // Add a reference to version NAME in file FILENAME.
479   void
480   add_need(Stringpool*, const char* filename, const char* name,
481            Stringpool::Key);
482
483   // Get the dynamic object to use for SYM.
484   Dynobj*
485   get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
486
487   // Return the version index to use for SYM.
488   unsigned int
489   version_index(const Symbol_table*, const Stringpool*,
490                 const Symbol* sym) const;
491
492   // We keep a hash table mapping canonicalized name/version pairs to
493   // a version base.
494   typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
495
496   struct Version_table_hash
497   {
498     size_t
499     operator()(const Key& k) const
500     { return k.first + k.second; }
501   };
502
503   struct Version_table_eq
504   {
505     bool
506     operator()(const Key& k1, const Key& k2) const
507     { return k1.first == k2.first && k1.second == k2.second; }
508   };
509
510   typedef Unordered_map<Key, Version_base*, Version_table_hash,
511                         Version_table_eq> Version_table;
512
513   // The version definitions.
514   Defs defs_;
515   // The version references.
516   Needs needs_;
517   // The mapping from a canonicalized version/filename pair to a
518   // version index.  The filename may be NULL.
519   Version_table version_table_;
520   // Whether the version indexes have been set.
521   bool is_finalized_;
522 };
523
524 } // End namespace gold.
525
526 #endif // !defined(GOLD_DYNOBJ_H)