Merge branch 'vendor/WPA_SUPPLICANT'
[dragonfly.git] / contrib / binutils-2.22 / gold / dynobj.h
1 // dynobj.h -- dynamic object support for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011  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 Version_script_info;
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   // We keep a list of all the DT_NEEDED entries we find.
43   typedef std::vector<std::string> Needed;
44
45   Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
46
47   // Return the name to use in a DT_NEEDED entry for this object.
48   const char*
49   soname() const
50   { return this->soname_.c_str(); }
51
52   // Return the list of DT_NEEDED strings.
53   const Needed&
54   needed() const
55   { return this->needed_; }
56
57   // Return whether this dynamic object has any DT_NEEDED entries
58   // which were not seen during the link.
59   bool
60   has_unknown_needed_entries() const
61   {
62     gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
63     return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
64   }
65
66   // Set whether this dynamic object has any DT_NEEDED entries which
67   // were not seen during the link.
68   void
69   set_has_unknown_needed_entries(bool set)
70   {
71     gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
72     this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
73   }
74
75   // Compute the ELF hash code for a string.
76   static uint32_t
77   elf_hash(const char*);
78
79   // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
80   // DYNSYMS is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the
81   // number of local dynamic symbols, which is the index of the first
82   // dynamic gobal symbol.
83   static void
84   create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
85                         unsigned int local_dynsym_count,
86                         unsigned char** pphash,
87                         unsigned int* phashlen);
88
89   // Create a GNU hash table, setting *PPHASH and *PHASHLEN.  DYNSYMS
90   // is the global dynamic symbols.  LOCAL_DYNSYM_COUNT is the number
91   // of local dynamic symbols, which is the index of the first dynamic
92   // gobal symbol.
93   static void
94   create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
95                         unsigned int local_dynsym_count,
96                         unsigned char** pphash, unsigned int* phashlen);
97
98  protected:
99   // Return a pointer to this object.
100   virtual Dynobj*
101   do_dynobj()
102   { return this; }
103
104   // Set the DT_SONAME string.
105   void
106   set_soname_string(const char* s)
107   { this->soname_.assign(s); }
108
109   // Add an entry to the list of DT_NEEDED strings.
110   void
111   add_needed(const char* s)
112   { this->needed_.push_back(std::string(s)); }
113
114  private:
115   // Compute the GNU hash code for a string.
116   static uint32_t
117   gnu_hash(const char*);
118
119   // Compute the number of hash buckets to use.
120   static unsigned int
121   compute_bucket_count(const std::vector<uint32_t>& hashcodes,
122                        bool for_gnu_hash_table);
123
124   // Sized version of create_elf_hash_table.
125   template<bool big_endian>
126   static void
127   sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
128                               const std::vector<uint32_t>& chain,
129                               unsigned char* phash,
130                               unsigned int hashlen);
131
132   // Sized version of create_gnu_hash_table.
133   template<int size, bool big_endian>
134   static void
135   sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
136                               const std::vector<uint32_t>& dynsym_hashvals,
137                               unsigned int unhashed_dynsym_count,
138                               unsigned char** pphash,
139                               unsigned int* phashlen);
140
141   // Values for the has_unknown_needed_entries_ field.
142   enum Unknown_needed
143   {
144     UNKNOWN_NEEDED_UNSET,
145     UNKNOWN_NEEDED_TRUE,
146     UNKNOWN_NEEDED_FALSE
147   };
148
149   // The DT_SONAME name, if any.
150   std::string soname_;
151   // The list of DT_NEEDED entries.
152   Needed needed_;
153   // Whether this dynamic object has any DT_NEEDED entries not seen
154   // during the link.
155   Unknown_needed unknown_needed_;
156 };
157
158 // A dynamic object, size and endian specific version.
159
160 template<int size, bool big_endian>
161 class Sized_dynobj : public Dynobj
162 {
163  public:
164   typedef typename Sized_relobj_file<size, big_endian>::Symbols Symbols;
165
166   Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
167                const typename elfcpp::Ehdr<size, big_endian>&);
168
169   // Set up the object file based on TARGET.
170   void
171   setup();
172
173   // Read the symbols.
174   void
175   do_read_symbols(Read_symbols_data*);
176
177   // Lay out the input sections.
178   void
179   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
180
181   // Add the symbols to the symbol table.
182   void
183   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
184
185   Archive::Should_include
186   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
187                            std::string* why);
188
189   // Iterate over global symbols, calling a visitor class V for each.
190   void
191   do_for_all_global_symbols(Read_symbols_data* sd,
192                             Library_base::Symbol_visitor_base* v);
193
194   // Iterate over local symbols, calling a visitor class V for each GOT offset
195   // associated with a local symbol.
196   void
197   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
198
199   // Get the size of a section.
200   uint64_t
201   do_section_size(unsigned int shndx)
202   { return this->elf_file_.section_size(shndx); }
203
204   // Get the name of a section.
205   std::string
206   do_section_name(unsigned int shndx)
207   { return this->elf_file_.section_name(shndx); }
208
209   // Return a view of the contents of a section.  Set *PLEN to the
210   // size.
211   Object::Location
212   do_section_contents(unsigned int shndx)
213   { return this->elf_file_.section_contents(shndx); }
214
215   // Return section flags.
216   uint64_t
217   do_section_flags(unsigned int shndx)
218   { return this->elf_file_.section_flags(shndx); }
219
220   // Not used for dynobj.
221   uint64_t
222   do_section_entsize(unsigned int )
223   { gold_unreachable(); }
224
225   // Return section address.
226   uint64_t
227   do_section_address(unsigned int shndx)
228   { return this->elf_file_.section_addr(shndx); }
229
230   // Return section type.
231   unsigned int
232   do_section_type(unsigned int shndx)
233   { return this->elf_file_.section_type(shndx); }
234
235   // Return the section link field.
236   unsigned int
237   do_section_link(unsigned int shndx)
238   { return this->elf_file_.section_link(shndx); }
239
240   // Return the section link field.
241   unsigned int
242   do_section_info(unsigned int shndx)
243   { return this->elf_file_.section_info(shndx); }
244
245   // Return the section alignment.
246   uint64_t
247   do_section_addralign(unsigned int shndx)
248   { return this->elf_file_.section_addralign(shndx); }
249
250   // Return the Xindex structure to use.
251   Xindex*
252   do_initialize_xindex();
253
254   // Get symbol counts.
255   void
256   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
257
258   // Get the global symbols.
259   const Symbols*
260   do_get_global_symbols() const
261   { return this->symbols_; }
262
263  private:
264   // For convenience.
265   typedef Sized_dynobj<size, big_endian> This;
266   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
267   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
268   static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
269   typedef elfcpp::Shdr<size, big_endian> Shdr;
270   typedef elfcpp::Dyn<size, big_endian> Dyn;
271
272   // Adjust a section index if necessary.
273   unsigned int
274   adjust_shndx(unsigned int shndx)
275   {
276     if (shndx >= elfcpp::SHN_LORESERVE)
277       shndx += this->elf_file_.large_shndx_offset();
278     return shndx;
279   }
280
281   // Find the dynamic symbol table and the version sections, given the
282   // section headers.
283   void
284   find_dynsym_sections(const unsigned char* pshdrs,
285                        unsigned int* pversym_shndx,
286                        unsigned int* pverdef_shndx,
287                        unsigned int* pverneed_shndx,
288                        unsigned int* pdynamic_shndx);
289
290   // Read the dynamic symbol section SHNDX.
291   void
292   read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
293                       elfcpp::SHT type, unsigned int link,
294                       File_view** view, section_size_type* view_size,
295                       unsigned int* view_info);
296
297   // Read the dynamic tags.
298   void
299   read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
300                unsigned int strtab_shndx, const unsigned char* strtabu,
301                off_t strtab_size);
302
303   // Mapping from version number to version name.
304   typedef std::vector<const char*> Version_map;
305
306   // Create the version map.
307   void
308   make_version_map(Read_symbols_data* sd, Version_map*) const;
309
310   // Add version definitions to the version map.
311   void
312   make_verdef_map(Read_symbols_data* sd, Version_map*) const;
313
314   // Add version references to the version map.
315   void
316   make_verneed_map(Read_symbols_data* sd, Version_map*) const;
317
318   // Add an entry to the version map.
319   void
320   set_version_map(Version_map*, unsigned int ndx, const char* name) const;
321
322   // General access to the ELF file.
323   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
324   // The section index of the dynamic symbol table.
325   unsigned int dynsym_shndx_;
326   // The entries in the symbol table for the symbols.  We only keep
327   // this if we need it to print symbol information.
328   Symbols* symbols_;
329   // Number of defined symbols.
330   size_t defined_count_;
331 };
332
333 // A base class for Verdef and Verneed_version which just handles the
334 // version index which will be stored in the SHT_GNU_versym section.
335
336 class Version_base
337 {
338  public:
339   Version_base()
340     : index_(-1U)
341   { }
342
343   virtual
344   ~Version_base()
345   { }
346
347   // Return the version index.
348   unsigned int
349   index() const
350   {
351     gold_assert(this->index_ != -1U);
352     return this->index_;
353   }
354
355   // Set the version index.
356   void
357   set_index(unsigned int index)
358   {
359     gold_assert(this->index_ == -1U);
360     this->index_ = index;
361   }
362
363   // Clear the weak flag in a version definition.
364   virtual void
365   clear_weak() = 0;
366
367  private:
368   Version_base(const Version_base&);
369   Version_base& operator=(const Version_base&);
370
371   // The index of the version definition or reference.
372   unsigned int index_;
373 };
374
375 // This class handles a version being defined in the file we are
376 // generating.
377
378 class Verdef : public Version_base
379 {
380  public:
381   Verdef(const char* name, const std::vector<std::string>& deps,
382          bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
383     : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
384       is_info_(is_info), is_symbol_created_(is_symbol_created)
385   { }
386
387   // Return the version name.
388   const char*
389   name() const
390   { return this->name_; }
391
392   // Return the number of dependencies.
393   unsigned int
394   count_dependencies() const
395   { return this->deps_.size(); }
396
397   // Add a dependency to this version.  The NAME should be
398   // canonicalized in the dynamic Stringpool.
399   void
400   add_dependency(const char* name)
401   { this->deps_.push_back(name); }
402
403   // Return whether this definition is weak.
404   bool
405   is_weak() const
406   { return this->is_weak_; }
407
408   // Clear the weak flag.
409   void
410   clear_weak()
411   { this->is_weak_ = false; }
412
413   // Return whether this definition is informational.
414   bool
415   is_info() const
416   { return this->is_info_; }
417
418   // Return whether a version symbol has been created for this
419   // definition.
420   bool
421   is_symbol_created() const
422   { return this->is_symbol_created_; }
423
424   // Write contents to buffer.
425   template<int size, bool big_endian>
426   unsigned char*
427   write(const Stringpool*, bool is_last, unsigned char*) const;
428
429  private:
430   Verdef(const Verdef&);
431   Verdef& operator=(const Verdef&);
432
433   // The type of the list of version dependencies.  Each dependency
434   // should be canonicalized in the dynamic Stringpool.
435   typedef std::vector<std::string> Deps;
436
437   // The name of this version.  This should be canonicalized in the
438   // dynamic Stringpool.
439   const char* name_;
440   // A list of other versions which this version depends upon.
441   Deps deps_;
442   // Whether this is the base version.
443   bool is_base_;
444   // Whether this version is weak.
445   bool is_weak_;
446   // Whether this version is informational.
447   bool is_info_;
448   // Whether a version symbol has been created.
449   bool is_symbol_created_;
450 };
451
452 // A referened version.  This will be associated with a filename by
453 // Verneed.
454
455 class Verneed_version : public Version_base
456 {
457  public:
458   Verneed_version(const char* version)
459     : version_(version)
460   { }
461
462   // Return the version name.
463   const char*
464   version() const
465   { return this->version_; }
466
467   // Clear the weak flag.  This is invalid for a reference.
468   void
469   clear_weak()
470   { gold_unreachable(); }
471
472  private:
473   Verneed_version(const Verneed_version&);
474   Verneed_version& operator=(const Verneed_version&);
475
476   const char* version_;
477 };
478
479 // Version references in a single dynamic object.
480
481 class Verneed
482 {
483  public:
484   Verneed(const char* filename)
485     : filename_(filename), need_versions_()
486   { }
487
488   ~Verneed();
489
490   // Return the file name.
491   const char*
492   filename() const
493   { return this->filename_; }
494
495   // Return the number of versions.
496   unsigned int
497   count_versions() const
498   { return this->need_versions_.size(); }
499
500   // Add a version name.  The name should be canonicalized in the
501   // dynamic Stringpool.  If the name is already present, this does
502   // nothing.
503   Verneed_version*
504   add_name(const char* name);
505
506   // Set the version indexes, starting at INDEX.  Return the updated
507   // INDEX.
508   unsigned int
509   finalize(unsigned int index);
510
511   // Write contents to buffer.
512   template<int size, bool big_endian>
513   unsigned char*
514   write(const Stringpool*, bool is_last, unsigned char*) const;
515
516  private:
517   Verneed(const Verneed&);
518   Verneed& operator=(const Verneed&);
519
520   // The type of the list of version names.  Each name should be
521   // canonicalized in the dynamic Stringpool.
522   typedef std::vector<Verneed_version*> Need_versions;
523
524   // The filename of the dynamic object.  This should be
525   // canonicalized in the dynamic Stringpool.
526   const char* filename_;
527   // The list of version names.
528   Need_versions need_versions_;
529 };
530
531 // This class handles version definitions and references which go into
532 // the output file.
533
534 class Versions
535 {
536  public:
537   Versions(const Version_script_info&, Stringpool*);
538
539   ~Versions();
540
541   // SYM is going into the dynamic symbol table and has a version.
542   // Record the appropriate version information.
543   void
544   record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
545
546   // Set the version indexes.  DYNSYM_INDEX is the index we should use
547   // for the next dynamic symbol.  We add new dynamic symbols to SYMS
548   // and return an updated DYNSYM_INDEX.
549   unsigned int
550   finalize(Symbol_table* symtab, unsigned int dynsym_index,
551            std::vector<Symbol*>* syms);
552
553   // Return whether there are any version definitions.
554   bool
555   any_defs() const
556   { return !this->defs_.empty(); }
557
558   // Return whether there are any version references.
559   bool
560   any_needs() const
561   { return !this->needs_.empty(); }
562
563   // Build an allocated buffer holding the contents of the symbol
564   // version section (.gnu.version).
565   template<int size, bool big_endian>
566   void
567   symbol_section_contents(const Symbol_table*, const Stringpool*,
568                           unsigned int local_symcount,
569                           const std::vector<Symbol*>& syms,
570                           unsigned char**, unsigned int*) const;
571
572   // Build an allocated buffer holding the contents of the version
573   // definition section (.gnu.version_d).
574   template<int size, bool big_endian>
575   void
576   def_section_contents(const Stringpool*, unsigned char**,
577                        unsigned int* psize, unsigned int* pentries) const;
578
579   // Build an allocated buffer holding the contents of the version
580   // reference section (.gnu.version_r).
581   template<int size, bool big_endian>
582   void
583   need_section_contents(const Stringpool*, unsigned char**,
584                         unsigned int* psize, unsigned int* pentries) const;
585
586   const Version_script_info&
587   version_script() const
588   { return this->version_script_; }
589
590  private:
591   Versions(const Versions&);
592   Versions& operator=(const Versions&);
593
594   // The type of the list of version definitions.
595   typedef std::vector<Verdef*> Defs;
596
597   // The type of the list of version references.
598   typedef std::vector<Verneed*> Needs;
599
600   // Handle a symbol SYM defined with version VERSION.
601   void
602   add_def(Stringpool*, const Symbol* sym, const char* version,
603           Stringpool::Key);
604
605   // Add a reference to version NAME in file FILENAME.
606   void
607   add_need(Stringpool*, const char* filename, const char* name,
608            Stringpool::Key);
609
610   // Get the dynamic object to use for SYM.
611   Dynobj*
612   get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
613
614   // Return the version index to use for SYM.
615   unsigned int
616   version_index(const Symbol_table*, const Stringpool*,
617                 const Symbol* sym) const;
618
619   // Define the base version of a shared library.
620   void
621   define_base_version(Stringpool* dynpool);
622
623   // We keep a hash table mapping canonicalized name/version pairs to
624   // a version base.
625   typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
626
627   struct Version_table_hash
628   {
629     size_t
630     operator()(const Key& k) const
631     { return k.first + k.second; }
632   };
633
634   struct Version_table_eq
635   {
636     bool
637     operator()(const Key& k1, const Key& k2) const
638     { return k1.first == k2.first && k1.second == k2.second; }
639   };
640
641   typedef Unordered_map<Key, Version_base*, Version_table_hash,
642                         Version_table_eq> Version_table;
643
644   // The version definitions.
645   Defs defs_;
646   // The version references.
647   Needs needs_;
648   // The mapping from a canonicalized version/filename pair to a
649   // version index.  The filename may be NULL.
650   Version_table version_table_;
651   // Whether the version indexes have been set.
652   bool is_finalized_;
653   // Contents of --version-script, if passed, or NULL.
654   const Version_script_info& version_script_;
655   // Whether we need to insert a base version.  This is only used for
656   // shared libraries and is cleared when the base version is defined.
657   bool needs_base_version_;
658 };
659
660 } // End namespace gold.
661
662 #endif // !defined(GOLD_DYNOBJ_H)