Add support for SHF_MERGE sections.
[deliverable/binutils-gdb.git] / gold / dynobj.h
1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
2
3 #ifndef GOLD_DYNOBJ_H
4 #define GOLD_DYNOBJ_H
5
6 #include <vector>
7
8 #include "stringpool.h"
9 #include "object.h"
10
11 namespace gold
12 {
13
14 class General_options;
15
16 // A dynamic object (ET_DYN). This is an abstract base class itself.
17 // The implementations is the template class Sized_dynobj.
18
19 class Dynobj : public Object
20 {
21 public:
22 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0)
23 : Object(name, input_file, true, offset), soname_()
24 { }
25
26 // Return the name to use in a DT_NEEDED entry for this object.
27 const char*
28 soname() const;
29
30 // Compute the ELF hash code for a string.
31 static uint32_t
32 elf_hash(const char*);
33
34 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
35 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
36 // number of local dynamic symbols, which is the index of the first
37 // dynamic gobal symbol.
38 static void
39 create_elf_hash_table(const Target*, const std::vector<Symbol*>& dynsyms,
40 unsigned int local_dynsym_count,
41 unsigned char** pphash,
42 unsigned int* phashlen);
43
44 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
45 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
46 // of local dynamic symbols, which is the index of the first dynamic
47 // gobal symbol.
48 static void
49 create_gnu_hash_table(const Target*, const std::vector<Symbol*>& dynsyms,
50 unsigned int local_dynsym_count,
51 unsigned char** pphash, unsigned int* phashlen);
52
53 protected:
54 // Set the DT_SONAME string.
55 void
56 set_soname_string(const char* s)
57 { this->soname_.assign(s); }
58
59 private:
60 // Compute the GNU hash code for a string.
61 static uint32_t
62 gnu_hash(const char*);
63
64 // Compute the number of hash buckets to use.
65 static unsigned int
66 compute_bucket_count(const std::vector<uint32_t>& hashcodes,
67 bool for_gnu_hash_table);
68
69 // Sized version of create_elf_hash_table.
70 template<bool big_endian>
71 static void
72 sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
73 const std::vector<uint32_t>& chain,
74 unsigned char* phash,
75 unsigned int hashlen);
76
77 // Sized version of create_gnu_hash_table.
78 template<int size, bool big_endian>
79 static void
80 sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
81 const std::vector<uint32_t>& dynsym_hashvals,
82 unsigned int unhashed_dynsym_count,
83 unsigned char** pphash,
84 unsigned int* phashlen);
85
86 // The DT_SONAME name, if any.
87 std::string soname_;
88 };
89
90 // A dynamic object, size and endian specific version.
91
92 template<int size, bool big_endian>
93 class Sized_dynobj : public Dynobj
94 {
95 public:
96 Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
97 const typename elfcpp::Ehdr<size, big_endian>&);
98
99 // Set up the object file based on the ELF header.
100 void
101 setup(const typename elfcpp::Ehdr<size, big_endian>&);
102
103 // Read the symbols.
104 void
105 do_read_symbols(Read_symbols_data*);
106
107 // Lay out the input sections.
108 void
109 do_layout(const General_options&, Symbol_table*, Layout*,
110 Read_symbols_data*);
111
112 // Add the symbols to the symbol table.
113 void
114 do_add_symbols(Symbol_table*, Read_symbols_data*);
115
116 // Get the name of a section.
117 std::string
118 do_section_name(unsigned int shndx)
119 { return this->elf_file_.section_name(shndx); }
120
121 // Return a view of the contents of a section. Set *PLEN to the
122 // size.
123 Object::Location
124 do_section_contents(unsigned int shndx)
125 { return this->elf_file_.section_contents(shndx); }
126
127 // Return section flags.
128 uint64_t
129 do_section_flags(unsigned int shndx)
130 { return this->elf_file_.section_flags(shndx); }
131
132 private:
133 // For convenience.
134 typedef Sized_dynobj<size, big_endian> This;
135 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
136 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
137 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
138 typedef elfcpp::Shdr<size, big_endian> Shdr;
139 typedef elfcpp::Dyn<size, big_endian> Dyn;
140
141 // Find the dynamic symbol table and the version sections, given the
142 // section headers.
143 void
144 find_dynsym_sections(const unsigned char* pshdrs,
145 unsigned int* pdynshm_shndx,
146 unsigned int* pversym_shndx,
147 unsigned int* pverdef_shndx,
148 unsigned int* pverneed_shndx,
149 unsigned int* pdynamic_shndx);
150
151 // Read the dynamic symbol section SHNDX.
152 void
153 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
154 elfcpp::SHT type, unsigned int link,
155 File_view** view, off_t* view_size,
156 unsigned int* view_info);
157
158 // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
159 // The STRTAB parameters may have the relevant string table.
160 void
161 set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
162 unsigned int strtab_shndx, const unsigned char* strtabu,
163 off_t strtab_size);
164
165 // Mapping from version number to version name.
166 typedef std::vector<const char*> Version_map;
167
168 // Create the version map.
169 void
170 make_version_map(Read_symbols_data* sd, Version_map*) const;
171
172 // Add version definitions to the version map.
173 void
174 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
175
176 // Add version references to the version map.
177 void
178 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
179
180 // Add an entry to the version map.
181 void
182 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
183
184 // General access to the ELF file.
185 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
186 };
187
188 // A base class for Verdef and Verneed_version which just handles the
189 // version index which will be stored in the SHT_GNU_versym section.
190
191 class Version_base
192 {
193 public:
194 Version_base()
195 : index_(-1U)
196 { }
197
198 virtual
199 ~Version_base()
200 { }
201
202 // Return the version index.
203 unsigned int
204 index() const
205 {
206 gold_assert(this->index_ != -1U);
207 return this->index_;
208 }
209
210 // Set the version index.
211 void
212 set_index(unsigned int index)
213 {
214 gold_assert(this->index_ == -1U);
215 this->index_ = index;
216 }
217
218 // Clear the weak flag in a version definition.
219 virtual void
220 clear_weak() = 0;
221
222 private:
223 Version_base(const Version_base&);
224 Version_base& operator=(const Version_base&);
225
226 // The index of the version definition or reference.
227 unsigned int index_;
228 };
229
230 // This class handles a version being defined in the file we are
231 // generating.
232
233 class Verdef : public Version_base
234 {
235 public:
236 Verdef(const char* name, bool is_base, bool is_weak, bool is_symbol_created)
237 : name_(name), deps_(), is_base_(is_base), is_weak_(is_weak),
238 is_symbol_created_(is_symbol_created)
239 { }
240
241 // Return the version name.
242 const char*
243 name() const
244 { return this->name_; }
245
246 // Return the number of dependencies.
247 unsigned int
248 count_dependencies() const
249 { return this->deps_.size(); }
250
251 // Add a dependency to this version. The NAME should be
252 // canonicalized in the dynamic Stringpool.
253 void
254 add_dependency(const char* name)
255 { this->deps_.push_back(name); }
256
257 // Return whether this definition is weak.
258 bool
259 is_weak() const
260 { return this->is_weak_; }
261
262 // Clear the weak flag.
263 void
264 clear_weak()
265 { this->is_weak_ = false; }
266
267 // Return whether a version symbol has been created for this
268 // definition.
269 bool
270 is_symbol_created() const
271 { return this->is_symbol_created_; }
272
273 // Write contents to buffer.
274 template<int size, bool big_endian>
275 unsigned char*
276 write(const Stringpool*, bool is_last, unsigned char*
277 ACCEPT_SIZE_ENDIAN) const;
278
279 private:
280 Verdef(const Verdef&);
281 Verdef& operator=(const Verdef&);
282
283 // The type of the list of version dependencies. Each dependency
284 // should be canonicalized in the dynamic Stringpool.
285 typedef std::vector<const char*> Deps;
286
287 // The name of this version. This should be canonicalized in the
288 // dynamic Stringpool.
289 const char* name_;
290 // A list of other versions which this version depends upon.
291 Deps deps_;
292 // Whether this is the base version.
293 bool is_base_;
294 // Whether this version is weak.
295 bool is_weak_;
296 // Whether a version symbol has been created.
297 bool is_symbol_created_;
298 };
299
300 // A referened version. This will be associated with a filename by
301 // Verneed.
302
303 class Verneed_version : public Version_base
304 {
305 public:
306 Verneed_version(const char* version)
307 : version_(version)
308 { }
309
310 // Return the version name.
311 const char*
312 version() const
313 { return this->version_; }
314
315 // Clear the weak flag. This is invalid for a reference.
316 void
317 clear_weak()
318 { gold_unreachable(); }
319
320 private:
321 Verneed_version(const Verneed_version&);
322 Verneed_version& operator=(const Verneed_version&);
323
324 const char* version_;
325 };
326
327 // Version references in a single dynamic object.
328
329 class Verneed
330 {
331 public:
332 Verneed(const char* filename)
333 : filename_(filename), need_versions_()
334 { }
335
336 ~Verneed();
337
338 // Return the file name.
339 const char*
340 filename() const
341 { return this->filename_; }
342
343 // Return the number of versions.
344 unsigned int
345 count_versions() const
346 { return this->need_versions_.size(); }
347
348 // Add a version name. The name should be canonicalized in the
349 // dynamic Stringpool. If the name is already present, this does
350 // nothing.
351 Verneed_version*
352 add_name(const char* name);
353
354 // Set the version indexes, starting at INDEX. Return the updated
355 // INDEX.
356 unsigned int
357 finalize(unsigned int index);
358
359 // Write contents to buffer.
360 template<int size, bool big_endian>
361 unsigned char*
362 write(const Stringpool*, bool is_last, unsigned char*
363 ACCEPT_SIZE_ENDIAN) const;
364
365 private:
366 Verneed(const Verneed&);
367 Verneed& operator=(const Verneed&);
368
369 // The type of the list of version names. Each name should be
370 // canonicalized in the dynamic Stringpool.
371 typedef std::vector<Verneed_version*> Need_versions;
372
373 // The filename of the dynamic object. This should be
374 // canonicalized in the dynamic Stringpool.
375 const char* filename_;
376 // The list of version names.
377 Need_versions need_versions_;
378 };
379
380 // This class handles version definitions and references which go into
381 // the output file.
382
383 class Versions
384 {
385 public:
386 Versions()
387 : defs_(), needs_(), version_table_(), is_finalized_(false)
388 { }
389
390 ~Versions();
391
392 // SYM is going into the dynamic symbol table and has a version.
393 // Record the appropriate version information.
394 void
395 record_version(const General_options*, Stringpool*, const Symbol* sym);
396
397 // Set the version indexes. DYNSYM_INDEX is the index we should use
398 // for the next dynamic symbol. We add new dynamic symbols to SYMS
399 // and return an updated DYNSYM_INDEX.
400 unsigned int
401 finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
402 std::vector<Symbol*>* syms);
403
404 // Return whether there are any version definitions.
405 bool
406 any_defs() const
407 { return !this->defs_.empty(); }
408
409 // Return whether there are any version references.
410 bool
411 any_needs() const
412 { return !this->needs_.empty(); }
413
414 // Build an allocated buffer holding the contents of the symbol
415 // version section (.gnu.version).
416 template<int size, bool big_endian>
417 void
418 symbol_section_contents(const Stringpool*, unsigned int local_symcount,
419 const std::vector<Symbol*>& syms,
420 unsigned char**, unsigned int*
421 ACCEPT_SIZE_ENDIAN) const;
422
423 // Build an allocated buffer holding the contents of the version
424 // definition section (.gnu.version_d).
425 template<int size, bool big_endian>
426 void
427 def_section_contents(const Stringpool*, unsigned char**,
428 unsigned int* psize, unsigned int* pentries
429 ACCEPT_SIZE_ENDIAN) const;
430
431 // Build an allocated buffer holding the contents of the version
432 // reference section (.gnu.version_r).
433 template<int size, bool big_endian>
434 void
435 need_section_contents(const Stringpool*, unsigned char**,
436 unsigned int* psize, unsigned int* pentries
437 ACCEPT_SIZE_ENDIAN) const;
438
439 private:
440 // The type of the list of version definitions.
441 typedef std::vector<Verdef*> Defs;
442
443 // The type of the list of version references.
444 typedef std::vector<Verneed*> Needs;
445
446 // Handle a symbol SYM defined with version VERSION.
447 void
448 add_def(const General_options*, const Symbol* sym, const char* version,
449 Stringpool::Key);
450
451 // Add a reference to version NAME in file FILENAME.
452 void
453 add_need(Stringpool*, const char* filename, const char* name,
454 Stringpool::Key);
455
456 // Return the version index to use for SYM.
457 unsigned int
458 version_index(const Stringpool*, const Symbol* sym) const;
459
460 // We keep a hash table mapping canonicalized name/version pairs to
461 // a version base.
462 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
463
464 struct Version_table_hash
465 {
466 size_t
467 operator()(const Key& k) const
468 { return k.first + k.second; }
469 };
470
471 struct Version_table_eq
472 {
473 bool
474 operator()(const Key& k1, const Key& k2) const
475 { return k1.first == k2.first && k1.second == k2.second; }
476 };
477
478 typedef Unordered_map<Key, Version_base*, Version_table_hash,
479 Version_table_eq> Version_table;
480
481 // The version definitions.
482 Defs defs_;
483 // The version references.
484 Needs needs_;
485 // The mapping from a canonicalized version/filename pair to a
486 // version index. The filename may be NULL.
487 Version_table version_table_;
488 // Whether the version indexes have been set.
489 bool is_finalized_;
490 };
491
492 } // End namespace gold.
493
494 #endif // !defined(GOLD_DYNOBJ_H)
This page took 0.045184 seconds and 5 git commands to generate.