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