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