1 // dynobj.h -- dynamic object support for gold   -*- C++ -*-
2 
3 // Copyright (C) 2006-2014 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*
soname()49   soname() const
50   { return this->soname_.c_str(); }
51 
52   // Return the list of DT_NEEDED strings.
53   const Needed&
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
has_unknown_needed_entries()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
set_has_unknown_needed_entries(bool set)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*
do_dynobj()101   do_dynobj()
102   { return this; }
103 
104   // Set the DT_SONAME string.
105   void
set_soname_string(const char * s)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
add_needed(const char * s)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
do_section_size(unsigned int shndx)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
do_section_name(unsigned int shndx)206   do_section_name(unsigned int shndx) const
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   const unsigned char*
do_section_contents(unsigned int shndx,section_size_type * plen,bool cache)212   do_section_contents(unsigned int shndx, section_size_type* plen,
213 		      bool cache)
214   {
215     Location loc(this->elf_file_.section_contents(shndx));
216     *plen = convert_to_section_size_type(loc.data_size);
217     if (*plen == 0)
218       {
219 	static const unsigned char empty[1] = { '\0' };
220 	return empty;
221       }
222     return this->get_view(loc.file_offset, *plen, true, cache);
223   }
224 
225   // Return section flags.
226   uint64_t
do_section_flags(unsigned int shndx)227   do_section_flags(unsigned int shndx)
228   { return this->elf_file_.section_flags(shndx); }
229 
230   // Not used for dynobj.
231   uint64_t
do_section_entsize(unsigned int)232   do_section_entsize(unsigned int )
233   { gold_unreachable(); }
234 
235   // Return section address.
236   uint64_t
do_section_address(unsigned int shndx)237   do_section_address(unsigned int shndx)
238   { return this->elf_file_.section_addr(shndx); }
239 
240   // Return section type.
241   unsigned int
do_section_type(unsigned int shndx)242   do_section_type(unsigned int shndx)
243   { return this->elf_file_.section_type(shndx); }
244 
245   // Return the section link field.
246   unsigned int
do_section_link(unsigned int shndx)247   do_section_link(unsigned int shndx)
248   { return this->elf_file_.section_link(shndx); }
249 
250   // Return the section link field.
251   unsigned int
do_section_info(unsigned int shndx)252   do_section_info(unsigned int shndx)
253   { return this->elf_file_.section_info(shndx); }
254 
255   // Return the section alignment.
256   uint64_t
do_section_addralign(unsigned int shndx)257   do_section_addralign(unsigned int shndx)
258   { return this->elf_file_.section_addralign(shndx); }
259 
260   // Return the Xindex structure to use.
261   Xindex*
262   do_initialize_xindex();
263 
264   // Get symbol counts.
265   void
266   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
267 
268   // Get the global symbols.
269   const Symbols*
do_get_global_symbols()270   do_get_global_symbols() const
271   { return this->symbols_; }
272 
273  protected:
274   // Read the symbols.  This is common code for all target-specific
275   // overrides of do_read_symbols().
276   void
277   base_read_symbols(Read_symbols_data*);
278 
279  private:
280   // For convenience.
281   typedef Sized_dynobj<size, big_endian> This;
282   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
283   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
284   static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
285   typedef elfcpp::Shdr<size, big_endian> Shdr;
286   typedef elfcpp::Dyn<size, big_endian> Dyn;
287 
288   // Adjust a section index if necessary.
289   unsigned int
adjust_shndx(unsigned int shndx)290   adjust_shndx(unsigned int shndx)
291   {
292     if (shndx >= elfcpp::SHN_LORESERVE)
293       shndx += this->elf_file_.large_shndx_offset();
294     return shndx;
295   }
296 
297   // Find the dynamic symbol table and the version sections, given the
298   // section headers.
299   void
300   find_dynsym_sections(const unsigned char* pshdrs,
301 		       unsigned int* pversym_shndx,
302 		       unsigned int* pverdef_shndx,
303 		       unsigned int* pverneed_shndx,
304 		       unsigned int* pdynamic_shndx);
305 
306   // Read the dynamic symbol section SHNDX.
307   void
308   read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
309 		      elfcpp::SHT type, unsigned int link,
310 		      File_view** view, section_size_type* view_size,
311 		      unsigned int* view_info);
312 
313   // Read the dynamic tags.
314   void
315   read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
316 	       unsigned int strtab_shndx, const unsigned char* strtabu,
317 	       off_t strtab_size);
318 
319   // Mapping from version number to version name.
320   typedef std::vector<const char*> Version_map;
321 
322   // Create the version map.
323   void
324   make_version_map(Read_symbols_data* sd, Version_map*) const;
325 
326   // Add version definitions to the version map.
327   void
328   make_verdef_map(Read_symbols_data* sd, Version_map*) const;
329 
330   // Add version references to the version map.
331   void
332   make_verneed_map(Read_symbols_data* sd, Version_map*) const;
333 
334   // Add an entry to the version map.
335   void
336   set_version_map(Version_map*, unsigned int ndx, const char* name) const;
337 
338   // General access to the ELF file.
339   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
340   // The section index of the dynamic symbol table.
341   unsigned int dynsym_shndx_;
342   // The entries in the symbol table for the symbols.  We only keep
343   // this if we need it to print symbol information.
344   Symbols* symbols_;
345   // Number of defined symbols.
346   size_t defined_count_;
347 };
348 
349 // A base class for Verdef and Verneed_version which just handles the
350 // version index which will be stored in the SHT_GNU_versym section.
351 
352 class Version_base
353 {
354  public:
Version_base()355   Version_base()
356     : index_(-1U)
357   { }
358 
359   virtual
~Version_base()360   ~Version_base()
361   { }
362 
363   // Return the version index.
364   unsigned int
index()365   index() const
366   {
367     gold_assert(this->index_ != -1U);
368     return this->index_;
369   }
370 
371   // Set the version index.
372   void
set_index(unsigned int index)373   set_index(unsigned int index)
374   {
375     gold_assert(this->index_ == -1U);
376     this->index_ = index;
377   }
378 
379   // Clear the weak flag in a version definition.
380   virtual void
381   clear_weak() = 0;
382 
383  private:
384   Version_base(const Version_base&);
385   Version_base& operator=(const Version_base&);
386 
387   // The index of the version definition or reference.
388   unsigned int index_;
389 };
390 
391 // This class handles a version being defined in the file we are
392 // generating.
393 
394 class Verdef : public Version_base
395 {
396  public:
Verdef(const char * name,const std::vector<std::string> & deps,bool is_base,bool is_weak,bool is_info,bool is_symbol_created)397   Verdef(const char* name, const std::vector<std::string>& deps,
398          bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
399     : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
400       is_info_(is_info), is_symbol_created_(is_symbol_created)
401   { }
402 
403   // Return the version name.
404   const char*
name()405   name() const
406   { return this->name_; }
407 
408   // Return the number of dependencies.
409   unsigned int
count_dependencies()410   count_dependencies() const
411   { return this->deps_.size(); }
412 
413   // Add a dependency to this version.  The NAME should be
414   // canonicalized in the dynamic Stringpool.
415   void
add_dependency(const char * name)416   add_dependency(const char* name)
417   { this->deps_.push_back(name); }
418 
419   // Return whether this definition is weak.
420   bool
is_weak()421   is_weak() const
422   { return this->is_weak_; }
423 
424   // Clear the weak flag.
425   void
clear_weak()426   clear_weak()
427   { this->is_weak_ = false; }
428 
429   // Return whether this definition is informational.
430   bool
is_info()431   is_info() const
432   { return this->is_info_; }
433 
434   // Return whether a version symbol has been created for this
435   // definition.
436   bool
is_symbol_created()437   is_symbol_created() const
438   { return this->is_symbol_created_; }
439 
440   // Write contents to buffer.
441   template<int size, bool big_endian>
442   unsigned char*
443   write(const Stringpool*, bool is_last, unsigned char*) const;
444 
445  private:
446   Verdef(const Verdef&);
447   Verdef& operator=(const Verdef&);
448 
449   // The type of the list of version dependencies.  Each dependency
450   // should be canonicalized in the dynamic Stringpool.
451   typedef std::vector<std::string> Deps;
452 
453   // The name of this version.  This should be canonicalized in the
454   // dynamic Stringpool.
455   const char* name_;
456   // A list of other versions which this version depends upon.
457   Deps deps_;
458   // Whether this is the base version.
459   bool is_base_;
460   // Whether this version is weak.
461   bool is_weak_;
462   // Whether this version is informational.
463   bool is_info_;
464   // Whether a version symbol has been created.
465   bool is_symbol_created_;
466 };
467 
468 // A referened version.  This will be associated with a filename by
469 // Verneed.
470 
471 class Verneed_version : public Version_base
472 {
473  public:
Verneed_version(const char * version)474   Verneed_version(const char* version)
475     : version_(version)
476   { }
477 
478   // Return the version name.
479   const char*
version()480   version() const
481   { return this->version_; }
482 
483   // Clear the weak flag.  This is invalid for a reference.
484   void
clear_weak()485   clear_weak()
486   { gold_unreachable(); }
487 
488  private:
489   Verneed_version(const Verneed_version&);
490   Verneed_version& operator=(const Verneed_version&);
491 
492   const char* version_;
493 };
494 
495 // Version references in a single dynamic object.
496 
497 class Verneed
498 {
499  public:
Verneed(const char * filename)500   Verneed(const char* filename)
501     : filename_(filename), need_versions_()
502   { }
503 
504   ~Verneed();
505 
506   // Return the file name.
507   const char*
filename()508   filename() const
509   { return this->filename_; }
510 
511   // Return the number of versions.
512   unsigned int
count_versions()513   count_versions() const
514   { return this->need_versions_.size(); }
515 
516   // Add a version name.  The name should be canonicalized in the
517   // dynamic Stringpool.  If the name is already present, this does
518   // nothing.
519   Verneed_version*
520   add_name(const char* name);
521 
522   // Set the version indexes, starting at INDEX.  Return the updated
523   // INDEX.
524   unsigned int
525   finalize(unsigned int index);
526 
527   // Write contents to buffer.
528   template<int size, bool big_endian>
529   unsigned char*
530   write(const Stringpool*, bool is_last, unsigned char*) const;
531 
532  private:
533   Verneed(const Verneed&);
534   Verneed& operator=(const Verneed&);
535 
536   // The type of the list of version names.  Each name should be
537   // canonicalized in the dynamic Stringpool.
538   typedef std::vector<Verneed_version*> Need_versions;
539 
540   // The filename of the dynamic object.  This should be
541   // canonicalized in the dynamic Stringpool.
542   const char* filename_;
543   // The list of version names.
544   Need_versions need_versions_;
545 };
546 
547 // This class handles version definitions and references which go into
548 // the output file.
549 
550 class Versions
551 {
552  public:
553   Versions(const Version_script_info&, Stringpool*);
554 
555   ~Versions();
556 
557   // SYM is going into the dynamic symbol table and has a version.
558   // Record the appropriate version information.
559   void
560   record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
561 
562   // Set the version indexes.  DYNSYM_INDEX is the index we should use
563   // for the next dynamic symbol.  We add new dynamic symbols to SYMS
564   // and return an updated DYNSYM_INDEX.
565   unsigned int
566   finalize(Symbol_table* symtab, unsigned int dynsym_index,
567 	   std::vector<Symbol*>* syms);
568 
569   // Return whether there are any version definitions.
570   bool
any_defs()571   any_defs() const
572   { return !this->defs_.empty(); }
573 
574   // Return whether there are any version references.
575   bool
any_needs()576   any_needs() const
577   { return !this->needs_.empty(); }
578 
579   // Build an allocated buffer holding the contents of the symbol
580   // version section (.gnu.version).
581   template<int size, bool big_endian>
582   void
583   symbol_section_contents(const Symbol_table*, const Stringpool*,
584 			  unsigned int local_symcount,
585 			  const std::vector<Symbol*>& syms,
586 			  unsigned char**, unsigned int*) const;
587 
588   // Build an allocated buffer holding the contents of the version
589   // definition section (.gnu.version_d).
590   template<int size, bool big_endian>
591   void
592   def_section_contents(const Stringpool*, unsigned char**,
593 		       unsigned int* psize, unsigned int* pentries) const;
594 
595   // Build an allocated buffer holding the contents of the version
596   // reference section (.gnu.version_r).
597   template<int size, bool big_endian>
598   void
599   need_section_contents(const Stringpool*, unsigned char**,
600 			unsigned int* psize, unsigned int* pentries) const;
601 
602   const Version_script_info&
version_script()603   version_script() const
604   { return this->version_script_; }
605 
606  private:
607   Versions(const Versions&);
608   Versions& operator=(const Versions&);
609 
610   // The type of the list of version definitions.
611   typedef std::vector<Verdef*> Defs;
612 
613   // The type of the list of version references.
614   typedef std::vector<Verneed*> Needs;
615 
616   // Handle a symbol SYM defined with version VERSION.
617   void
618   add_def(Stringpool*, const Symbol* sym, const char* version,
619 	  Stringpool::Key);
620 
621   // Add a reference to version NAME in file FILENAME.
622   void
623   add_need(Stringpool*, const char* filename, const char* name,
624 	   Stringpool::Key);
625 
626   // Get the dynamic object to use for SYM.
627   Dynobj*
628   get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
629 
630   // Return the version index to use for SYM.
631   unsigned int
632   version_index(const Symbol_table*, const Stringpool*,
633 		const Symbol* sym) const;
634 
635   // Define the base version of a shared library.
636   void
637   define_base_version(Stringpool* dynpool);
638 
639   // We keep a hash table mapping canonicalized name/version pairs to
640   // a version base.
641   typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
642 
643   struct Version_table_hash
644   {
645     size_t
operatorVersion_table_hash646     operator()(const Key& k) const
647     { return k.first + k.second; }
648   };
649 
650   struct Version_table_eq
651   {
652     bool
operatorVersion_table_eq653     operator()(const Key& k1, const Key& k2) const
654     { return k1.first == k2.first && k1.second == k2.second; }
655   };
656 
657   typedef Unordered_map<Key, Version_base*, Version_table_hash,
658 			Version_table_eq> Version_table;
659 
660   // The version definitions.
661   Defs defs_;
662   // The version references.
663   Needs needs_;
664   // The mapping from a canonicalized version/filename pair to a
665   // version index.  The filename may be NULL.
666   Version_table version_table_;
667   // Whether the version indexes have been set.
668   bool is_finalized_;
669   // Contents of --version-script, if passed, or NULL.
670   const Version_script_info& version_script_;
671   // Whether we need to insert a base version.  This is only used for
672   // shared libraries and is cleared when the base version is defined.
673   bool needs_base_version_;
674 };
675 
676 } // End namespace gold.
677 
678 #endif // !defined(GOLD_DYNOBJ_H)
679