2008-07-22 Simon Baldwin <simonb@google.com>
[deliverable/binutils-gdb.git] / gold / dynobj.h
CommitLineData
f6ce93d6
ILT
1// dynobj.h -- dynamic object support for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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
f6ce93d6
ILT
23#ifndef GOLD_DYNOBJ_H
24#define GOLD_DYNOBJ_H
25
dbe717ef
ILT
26#include <vector>
27
14b31740 28#include "stringpool.h"
f6ce93d6
ILT
29#include "object.h"
30
31namespace gold
32{
33
09124467 34class Version_script_info;
14b31740 35
f6ce93d6
ILT
36// A dynamic object (ET_DYN). This is an abstract base class itself.
37// The implementations is the template class Sized_dynobj.
38
39class Dynobj : public Object
40{
41 public:
e2827e5f
ILT
42 // We keep a list of all the DT_NEEDED entries we find.
43 typedef std::vector<std::string> Needed;
44
a7a81c1d 45 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
a3ad94ed
ILT
46
47 // Return the name to use in a DT_NEEDED entry for this object.
48 const char*
e2827e5f
ILT
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 }
a3ad94ed 74
14b31740
ILT
75 // Compute the ELF hash code for a string.
76 static uint32_t
77 elf_hash(const char*);
78
a3ad94ed
ILT
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
9025d29d 84 create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
a3ad94ed
ILT
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
9025d29d 94 create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
a3ad94ed
ILT
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
e2827e5f
ILT
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
a3ad94ed 109 private:
a3ad94ed
ILT
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
e2827e5f
ILT
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
a3ad94ed
ILT
144 // The DT_SONAME name, if any.
145 std::string soname_;
e2827e5f
ILT
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_;
f6ce93d6
ILT
151};
152
153// A dynamic object, size and endian specific version.
154
155template<int size, bool big_endian>
156class 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
dbe717ef
ILT
162 // Set up the object file based on the ELF header.
163 void
164 setup(const typename elfcpp::Ehdr<size, big_endian>&);
165
f6ce93d6
ILT
166 // Read the symbols.
167 void
168 do_read_symbols(Read_symbols_data*);
169
170 // Lay out the input sections.
171 void
7e1edb90 172 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
f6ce93d6
ILT
173
174 // Add the symbols to the symbol table.
175 void
176 do_add_symbols(Symbol_table*, Read_symbols_data*);
177
a445fddf
ILT
178 // Get the size of a section.
179 uint64_t
180 do_section_size(unsigned int shndx)
181 { return this->elf_file_.section_size(shndx); }
182
dbe717ef
ILT
183 // Get the name of a section.
184 std::string
185 do_section_name(unsigned int shndx)
186 { return this->elf_file_.section_name(shndx); }
187
f6ce93d6
ILT
188 // Return a view of the contents of a section. Set *PLEN to the
189 // size.
dbe717ef
ILT
190 Object::Location
191 do_section_contents(unsigned int shndx)
192 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 193
a3ad94ed
ILT
194 // Return section flags.
195 uint64_t
196 do_section_flags(unsigned int shndx)
197 { return this->elf_file_.section_flags(shndx); }
198
88dd47ac
ILT
199 // Return section address.
200 uint64_t
201 do_section_address(unsigned int shndx)
202 { return this->elf_file_.section_addr(shndx); }
203
730cdc88
ILT
204 // Return section type.
205 unsigned int
206 do_section_type(unsigned int shndx)
207 { return this->elf_file_.section_type(shndx); }
208
f7e2ee48
ILT
209 // Return the section link field.
210 unsigned int
211 do_section_link(unsigned int shndx)
212 { return this->elf_file_.section_link(shndx); }
213
4c50553d
ILT
214 // Return the section link field.
215 unsigned int
216 do_section_info(unsigned int shndx)
217 { return this->elf_file_.section_info(shndx); }
218
a445fddf
ILT
219 // Return the section alignment.
220 uint64_t
221 do_section_addralign(unsigned int shndx)
222 { return this->elf_file_.section_addralign(shndx); }
223
d491d34e
ILT
224 // Return the Xindex structure to use.
225 Xindex*
226 do_initialize_xindex();
227
dbe717ef
ILT
228 private:
229 // For convenience.
230 typedef Sized_dynobj<size, big_endian> This;
231 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
232 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
233 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
234 typedef elfcpp::Shdr<size, big_endian> Shdr;
235 typedef elfcpp::Dyn<size, big_endian> Dyn;
236
d491d34e
ILT
237 // Adjust a section index if necessary.
238 unsigned int
239 adjust_shndx(unsigned int shndx)
240 {
241 if (shndx >= elfcpp::SHN_LORESERVE)
242 shndx += this->elf_file_.large_shndx_offset();
243 return shndx;
244 }
245
dbe717ef
ILT
246 // Find the dynamic symbol table and the version sections, given the
247 // section headers.
248 void
249 find_dynsym_sections(const unsigned char* pshdrs,
dbe717ef
ILT
250 unsigned int* pversym_shndx,
251 unsigned int* pverdef_shndx,
252 unsigned int* pverneed_shndx,
253 unsigned int* pdynamic_shndx);
254
255 // Read the dynamic symbol section SHNDX.
256 void
257 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
258 elfcpp::SHT type, unsigned int link,
8383303e 259 File_view** view, section_size_type* view_size,
dbe717ef
ILT
260 unsigned int* view_info);
261
e2827e5f 262 // Read the dynamic tags.
dbe717ef 263 void
e2827e5f
ILT
264 read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
265 unsigned int strtab_shndx, const unsigned char* strtabu,
266 off_t strtab_size);
dbe717ef
ILT
267
268 // Mapping from version number to version name.
269 typedef std::vector<const char*> Version_map;
270
271 // Create the version map.
272 void
273 make_version_map(Read_symbols_data* sd, Version_map*) const;
274
14b31740
ILT
275 // Add version definitions to the version map.
276 void
277 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
278
279 // Add version references to the version map.
280 void
281 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
282
dbe717ef
ILT
283 // Add an entry to the version map.
284 void
285 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
286
287 // General access to the ELF file.
288 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
d491d34e
ILT
289 // The section index of the dynamic symbol table.
290 unsigned int dynsym_shndx_;
f6ce93d6
ILT
291};
292
14b31740
ILT
293// A base class for Verdef and Verneed_version which just handles the
294// version index which will be stored in the SHT_GNU_versym section.
295
296class Version_base
297{
298 public:
299 Version_base()
300 : index_(-1U)
301 { }
302
303 virtual
304 ~Version_base()
305 { }
306
307 // Return the version index.
308 unsigned int
309 index() const
310 {
311 gold_assert(this->index_ != -1U);
312 return this->index_;
313 }
314
315 // Set the version index.
316 void
317 set_index(unsigned int index)
318 {
319 gold_assert(this->index_ == -1U);
320 this->index_ = index;
321 }
322
323 // Clear the weak flag in a version definition.
324 virtual void
325 clear_weak() = 0;
326
327 private:
328 Version_base(const Version_base&);
329 Version_base& operator=(const Version_base&);
330
331 // The index of the version definition or reference.
332 unsigned int index_;
333};
334
335// This class handles a version being defined in the file we are
336// generating.
337
338class Verdef : public Version_base
339{
340 public:
09124467
ILT
341 Verdef(const char* name, const std::vector<std::string>& deps,
342 bool is_base, bool is_weak, bool is_symbol_created)
343 : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
14b31740
ILT
344 is_symbol_created_(is_symbol_created)
345 { }
346
347 // Return the version name.
348 const char*
349 name() const
350 { return this->name_; }
351
352 // Return the number of dependencies.
353 unsigned int
354 count_dependencies() const
355 { return this->deps_.size(); }
356
357 // Add a dependency to this version. The NAME should be
358 // canonicalized in the dynamic Stringpool.
359 void
360 add_dependency(const char* name)
361 { this->deps_.push_back(name); }
362
363 // Return whether this definition is weak.
364 bool
365 is_weak() const
366 { return this->is_weak_; }
367
368 // Clear the weak flag.
369 void
370 clear_weak()
371 { this->is_weak_ = false; }
372
373 // Return whether a version symbol has been created for this
374 // definition.
375 bool
376 is_symbol_created() const
377 { return this->is_symbol_created_; }
378
379 // Write contents to buffer.
380 template<int size, bool big_endian>
381 unsigned char*
7d1a9ebb 382 write(const Stringpool*, bool is_last, unsigned char*) const;
14b31740
ILT
383
384 private:
385 Verdef(const Verdef&);
386 Verdef& operator=(const Verdef&);
387
388 // The type of the list of version dependencies. Each dependency
389 // should be canonicalized in the dynamic Stringpool.
09124467 390 typedef std::vector<std::string> Deps;
14b31740
ILT
391
392 // The name of this version. This should be canonicalized in the
393 // dynamic Stringpool.
394 const char* name_;
395 // A list of other versions which this version depends upon.
396 Deps deps_;
397 // Whether this is the base version.
398 bool is_base_;
399 // Whether this version is weak.
400 bool is_weak_;
401 // Whether a version symbol has been created.
402 bool is_symbol_created_;
403};
404
405// A referened version. This will be associated with a filename by
406// Verneed.
407
408class Verneed_version : public Version_base
409{
410 public:
411 Verneed_version(const char* version)
412 : version_(version)
413 { }
414
415 // Return the version name.
416 const char*
417 version() const
418 { return this->version_; }
419
420 // Clear the weak flag. This is invalid for a reference.
421 void
422 clear_weak()
423 { gold_unreachable(); }
424
425 private:
426 Verneed_version(const Verneed_version&);
427 Verneed_version& operator=(const Verneed_version&);
428
429 const char* version_;
430};
431
432// Version references in a single dynamic object.
433
434class Verneed
435{
436 public:
437 Verneed(const char* filename)
438 : filename_(filename), need_versions_()
439 { }
440
441 ~Verneed();
442
443 // Return the file name.
444 const char*
445 filename() const
446 { return this->filename_; }
447
448 // Return the number of versions.
449 unsigned int
450 count_versions() const
451 { return this->need_versions_.size(); }
452
453 // Add a version name. The name should be canonicalized in the
454 // dynamic Stringpool. If the name is already present, this does
455 // nothing.
456 Verneed_version*
457 add_name(const char* name);
458
459 // Set the version indexes, starting at INDEX. Return the updated
460 // INDEX.
461 unsigned int
462 finalize(unsigned int index);
463
464 // Write contents to buffer.
465 template<int size, bool big_endian>
466 unsigned char*
7d1a9ebb 467 write(const Stringpool*, bool is_last, unsigned char*) const;
14b31740
ILT
468
469 private:
470 Verneed(const Verneed&);
471 Verneed& operator=(const Verneed&);
472
473 // The type of the list of version names. Each name should be
474 // canonicalized in the dynamic Stringpool.
475 typedef std::vector<Verneed_version*> Need_versions;
476
477 // The filename of the dynamic object. This should be
478 // canonicalized in the dynamic Stringpool.
479 const char* filename_;
480 // The list of version names.
481 Need_versions need_versions_;
482};
483
484// This class handles version definitions and references which go into
485// the output file.
486
487class Versions
488{
489 public:
a5dc0706 490 Versions(const Version_script_info&, Stringpool*);
14b31740
ILT
491
492 ~Versions();
493
494 // SYM is going into the dynamic symbol table and has a version.
495 // Record the appropriate version information.
496 void
35cdfc9a 497 record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
14b31740
ILT
498
499 // Set the version indexes. DYNSYM_INDEX is the index we should use
500 // for the next dynamic symbol. We add new dynamic symbols to SYMS
501 // and return an updated DYNSYM_INDEX.
502 unsigned int
9b07f471 503 finalize(Symbol_table* symtab, unsigned int dynsym_index,
14b31740
ILT
504 std::vector<Symbol*>* syms);
505
506 // Return whether there are any version definitions.
507 bool
508 any_defs() const
509 { return !this->defs_.empty(); }
510
511 // Return whether there are any version references.
512 bool
513 any_needs() const
514 { return !this->needs_.empty(); }
515
516 // Build an allocated buffer holding the contents of the symbol
517 // version section (.gnu.version).
518 template<int size, bool big_endian>
519 void
46fe1623
ILT
520 symbol_section_contents(const Symbol_table*, const Stringpool*,
521 unsigned int local_symcount,
14b31740 522 const std::vector<Symbol*>& syms,
7d1a9ebb 523 unsigned char**, unsigned int*) const;
14b31740
ILT
524
525 // Build an allocated buffer holding the contents of the version
526 // definition section (.gnu.version_d).
527 template<int size, bool big_endian>
528 void
529 def_section_contents(const Stringpool*, unsigned char**,
7d1a9ebb 530 unsigned int* psize, unsigned int* pentries) const;
14b31740
ILT
531
532 // Build an allocated buffer holding the contents of the version
533 // reference section (.gnu.version_r).
534 template<int size, bool big_endian>
535 void
536 need_section_contents(const Stringpool*, unsigned char**,
7d1a9ebb 537 unsigned int* psize, unsigned int* pentries) const;
14b31740 538
09124467
ILT
539 const Version_script_info&
540 version_script() const
541 { return this->version_script_; }
a5dc0706 542
14b31740 543 private:
09124467
ILT
544 Versions(const Versions&);
545 Versions& operator=(const Versions&);
546
14b31740
ILT
547 // The type of the list of version definitions.
548 typedef std::vector<Verdef*> Defs;
549
550 // The type of the list of version references.
551 typedef std::vector<Verneed*> Needs;
552
553 // Handle a symbol SYM defined with version VERSION.
554 void
35cdfc9a 555 add_def(const Symbol* sym, const char* version, Stringpool::Key);
14b31740
ILT
556
557 // Add a reference to version NAME in file FILENAME.
558 void
559 add_need(Stringpool*, const char* filename, const char* name,
560 Stringpool::Key);
561
46fe1623
ILT
562 // Get the dynamic object to use for SYM.
563 Dynobj*
564 get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
565
14b31740
ILT
566 // Return the version index to use for SYM.
567 unsigned int
46fe1623
ILT
568 version_index(const Symbol_table*, const Stringpool*,
569 const Symbol* sym) const;
14b31740
ILT
570
571 // We keep a hash table mapping canonicalized name/version pairs to
572 // a version base.
573 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
574
575 struct Version_table_hash
576 {
577 size_t
578 operator()(const Key& k) const
579 { return k.first + k.second; }
580 };
581
582 struct Version_table_eq
583 {
584 bool
585 operator()(const Key& k1, const Key& k2) const
586 { return k1.first == k2.first && k1.second == k2.second; }
587 };
588
589 typedef Unordered_map<Key, Version_base*, Version_table_hash,
590 Version_table_eq> Version_table;
591
592 // The version definitions.
593 Defs defs_;
594 // The version references.
595 Needs needs_;
596 // The mapping from a canonicalized version/filename pair to a
597 // version index. The filename may be NULL.
598 Version_table version_table_;
599 // Whether the version indexes have been set.
600 bool is_finalized_;
09124467
ILT
601 // Contents of --version-script, if passed, or NULL.
602 const Version_script_info& version_script_;
14b31740
ILT
603};
604
f6ce93d6
ILT
605} // End namespace gold.
606
607#endif // !defined(GOLD_DYNOBJ_H)
This page took 0.110061 seconds and 4 git commands to generate.