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