Commit | Line | Data |
---|---|---|
f6ce93d6 ILT |
1 | // dynobj.h -- dynamic object support for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
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 | ||
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 | ||
31 | namespace gold | |
32 | { | |
33 | ||
14b31740 | 34 | class General_options; |
09124467 | 35 | class Version_script_info; |
14b31740 | 36 | |
f6ce93d6 ILT |
37 | // A dynamic object (ET_DYN). This is an abstract base class itself. |
38 | // The implementations is the template class Sized_dynobj. | |
39 | ||
40 | class Dynobj : public Object | |
41 | { | |
42 | public: | |
e2827e5f ILT |
43 | // We keep a list of all the DT_NEEDED entries we find. |
44 | typedef std::vector<std::string> Needed; | |
45 | ||
a7a81c1d | 46 | Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0); |
a3ad94ed ILT |
47 | |
48 | // Return the name to use in a DT_NEEDED entry for this object. | |
49 | const char* | |
e2827e5f ILT |
50 | soname() const |
51 | { return this->soname_.c_str(); } | |
52 | ||
53 | // Return the list of DT_NEEDED strings. | |
54 | const Needed& | |
55 | needed() const | |
56 | { return this->needed_; } | |
57 | ||
58 | // Return whether this dynamic object has any DT_NEEDED entries | |
59 | // which were not seen during the link. | |
60 | bool | |
61 | has_unknown_needed_entries() const | |
62 | { | |
63 | gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET); | |
64 | return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE; | |
65 | } | |
66 | ||
67 | // Set whether this dynamic object has any DT_NEEDED entries which | |
68 | // were not seen during the link. | |
69 | void | |
70 | set_has_unknown_needed_entries(bool set) | |
71 | { | |
72 | gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET); | |
73 | this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE; | |
74 | } | |
a3ad94ed | 75 | |
14b31740 ILT |
76 | // Compute the ELF hash code for a string. |
77 | static uint32_t | |
78 | elf_hash(const char*); | |
79 | ||
a3ad94ed ILT |
80 | // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN. |
81 | // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the | |
82 | // number of local dynamic symbols, which is the index of the first | |
83 | // dynamic gobal symbol. | |
84 | static void | |
9025d29d | 85 | create_elf_hash_table(const std::vector<Symbol*>& dynsyms, |
a3ad94ed ILT |
86 | unsigned int local_dynsym_count, |
87 | unsigned char** pphash, | |
88 | unsigned int* phashlen); | |
89 | ||
90 | // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS | |
91 | // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number | |
92 | // of local dynamic symbols, which is the index of the first dynamic | |
93 | // gobal symbol. | |
94 | static void | |
9025d29d | 95 | create_gnu_hash_table(const std::vector<Symbol*>& dynsyms, |
a3ad94ed ILT |
96 | unsigned int local_dynsym_count, |
97 | unsigned char** pphash, unsigned int* phashlen); | |
98 | ||
99 | protected: | |
100 | // Set the DT_SONAME string. | |
101 | void | |
102 | set_soname_string(const char* s) | |
103 | { this->soname_.assign(s); } | |
104 | ||
e2827e5f ILT |
105 | // Add an entry to the list of DT_NEEDED strings. |
106 | void | |
107 | add_needed(const char* s) | |
108 | { this->needed_.push_back(std::string(s)); } | |
109 | ||
a3ad94ed | 110 | private: |
a3ad94ed ILT |
111 | // Compute the GNU hash code for a string. |
112 | static uint32_t | |
113 | gnu_hash(const char*); | |
114 | ||
115 | // Compute the number of hash buckets to use. | |
116 | static unsigned int | |
117 | compute_bucket_count(const std::vector<uint32_t>& hashcodes, | |
118 | bool for_gnu_hash_table); | |
119 | ||
120 | // Sized version of create_elf_hash_table. | |
121 | template<bool big_endian> | |
122 | static void | |
123 | sized_create_elf_hash_table(const std::vector<uint32_t>& bucket, | |
124 | const std::vector<uint32_t>& chain, | |
125 | unsigned char* phash, | |
126 | unsigned int hashlen); | |
127 | ||
128 | // Sized version of create_gnu_hash_table. | |
129 | template<int size, bool big_endian> | |
130 | static void | |
131 | sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms, | |
132 | const std::vector<uint32_t>& dynsym_hashvals, | |
133 | unsigned int unhashed_dynsym_count, | |
134 | unsigned char** pphash, | |
135 | unsigned int* phashlen); | |
136 | ||
e2827e5f ILT |
137 | // Values for the has_unknown_needed_entries_ field. |
138 | enum Unknown_needed | |
139 | { | |
140 | UNKNOWN_NEEDED_UNSET, | |
141 | UNKNOWN_NEEDED_TRUE, | |
142 | UNKNOWN_NEEDED_FALSE | |
143 | }; | |
144 | ||
a3ad94ed ILT |
145 | // The DT_SONAME name, if any. |
146 | std::string soname_; | |
e2827e5f ILT |
147 | // The list of DT_NEEDED entries. |
148 | Needed needed_; | |
149 | // Whether this dynamic object has any DT_NEEDED entries not seen | |
150 | // during the link. | |
151 | Unknown_needed unknown_needed_; | |
f6ce93d6 ILT |
152 | }; |
153 | ||
154 | // A dynamic object, size and endian specific version. | |
155 | ||
156 | template<int size, bool big_endian> | |
157 | class Sized_dynobj : public Dynobj | |
158 | { | |
159 | public: | |
160 | Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset, | |
161 | const typename elfcpp::Ehdr<size, big_endian>&); | |
162 | ||
dbe717ef ILT |
163 | // Set up the object file based on the ELF header. |
164 | void | |
165 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
166 | ||
f6ce93d6 ILT |
167 | // Read the symbols. |
168 | void | |
169 | do_read_symbols(Read_symbols_data*); | |
170 | ||
171 | // Lay out the input sections. | |
172 | void | |
7e1edb90 | 173 | do_layout(Symbol_table*, Layout*, Read_symbols_data*); |
f6ce93d6 ILT |
174 | |
175 | // Add the symbols to the symbol table. | |
176 | void | |
177 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
178 | ||
dbe717ef ILT |
179 | // Get the name of a section. |
180 | std::string | |
181 | do_section_name(unsigned int shndx) | |
182 | { return this->elf_file_.section_name(shndx); } | |
183 | ||
f6ce93d6 ILT |
184 | // Return a view of the contents of a section. Set *PLEN to the |
185 | // size. | |
dbe717ef ILT |
186 | Object::Location |
187 | do_section_contents(unsigned int shndx) | |
188 | { return this->elf_file_.section_contents(shndx); } | |
f6ce93d6 | 189 | |
a3ad94ed ILT |
190 | // Return section flags. |
191 | uint64_t | |
192 | do_section_flags(unsigned int shndx) | |
193 | { return this->elf_file_.section_flags(shndx); } | |
194 | ||
730cdc88 ILT |
195 | // Return section type. |
196 | unsigned int | |
197 | do_section_type(unsigned int shndx) | |
198 | { return this->elf_file_.section_type(shndx); } | |
199 | ||
f7e2ee48 ILT |
200 | // Return the section link field. |
201 | unsigned int | |
202 | do_section_link(unsigned int shndx) | |
203 | { return this->elf_file_.section_link(shndx); } | |
204 | ||
4c50553d ILT |
205 | // Return the section link field. |
206 | unsigned int | |
207 | do_section_info(unsigned int shndx) | |
208 | { return this->elf_file_.section_info(shndx); } | |
209 | ||
dbe717ef ILT |
210 | private: |
211 | // For convenience. | |
212 | typedef Sized_dynobj<size, big_endian> This; | |
213 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
214 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
215 | static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; | |
216 | typedef elfcpp::Shdr<size, big_endian> Shdr; | |
217 | typedef elfcpp::Dyn<size, big_endian> Dyn; | |
218 | ||
219 | // Find the dynamic symbol table and the version sections, given the | |
220 | // section headers. | |
221 | void | |
222 | find_dynsym_sections(const unsigned char* pshdrs, | |
223 | unsigned int* pdynshm_shndx, | |
224 | unsigned int* pversym_shndx, | |
225 | unsigned int* pverdef_shndx, | |
226 | unsigned int* pverneed_shndx, | |
227 | unsigned int* pdynamic_shndx); | |
228 | ||
229 | // Read the dynamic symbol section SHNDX. | |
230 | void | |
231 | read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx, | |
232 | elfcpp::SHT type, unsigned int link, | |
8383303e | 233 | File_view** view, section_size_type* view_size, |
dbe717ef ILT |
234 | unsigned int* view_info); |
235 | ||
e2827e5f | 236 | // Read the dynamic tags. |
dbe717ef | 237 | void |
e2827e5f ILT |
238 | read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx, |
239 | unsigned int strtab_shndx, const unsigned char* strtabu, | |
240 | off_t strtab_size); | |
dbe717ef ILT |
241 | |
242 | // Mapping from version number to version name. | |
243 | typedef std::vector<const char*> Version_map; | |
244 | ||
245 | // Create the version map. | |
246 | void | |
247 | make_version_map(Read_symbols_data* sd, Version_map*) const; | |
248 | ||
14b31740 ILT |
249 | // Add version definitions to the version map. |
250 | void | |
251 | make_verdef_map(Read_symbols_data* sd, Version_map*) const; | |
252 | ||
253 | // Add version references to the version map. | |
254 | void | |
255 | make_verneed_map(Read_symbols_data* sd, Version_map*) const; | |
256 | ||
dbe717ef ILT |
257 | // Add an entry to the version map. |
258 | void | |
259 | set_version_map(Version_map*, unsigned int ndx, const char* name) const; | |
260 | ||
261 | // General access to the ELF file. | |
262 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
f6ce93d6 ILT |
263 | }; |
264 | ||
14b31740 ILT |
265 | // A base class for Verdef and Verneed_version which just handles the |
266 | // version index which will be stored in the SHT_GNU_versym section. | |
267 | ||
268 | class Version_base | |
269 | { | |
270 | public: | |
271 | Version_base() | |
272 | : index_(-1U) | |
273 | { } | |
274 | ||
275 | virtual | |
276 | ~Version_base() | |
277 | { } | |
278 | ||
279 | // Return the version index. | |
280 | unsigned int | |
281 | index() const | |
282 | { | |
283 | gold_assert(this->index_ != -1U); | |
284 | return this->index_; | |
285 | } | |
286 | ||
287 | // Set the version index. | |
288 | void | |
289 | set_index(unsigned int index) | |
290 | { | |
291 | gold_assert(this->index_ == -1U); | |
292 | this->index_ = index; | |
293 | } | |
294 | ||
295 | // Clear the weak flag in a version definition. | |
296 | virtual void | |
297 | clear_weak() = 0; | |
298 | ||
299 | private: | |
300 | Version_base(const Version_base&); | |
301 | Version_base& operator=(const Version_base&); | |
302 | ||
303 | // The index of the version definition or reference. | |
304 | unsigned int index_; | |
305 | }; | |
306 | ||
307 | // This class handles a version being defined in the file we are | |
308 | // generating. | |
309 | ||
310 | class Verdef : public Version_base | |
311 | { | |
312 | public: | |
09124467 ILT |
313 | Verdef(const char* name, const std::vector<std::string>& deps, |
314 | bool is_base, bool is_weak, bool is_symbol_created) | |
315 | : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak), | |
14b31740 ILT |
316 | is_symbol_created_(is_symbol_created) |
317 | { } | |
318 | ||
319 | // Return the version name. | |
320 | const char* | |
321 | name() const | |
322 | { return this->name_; } | |
323 | ||
324 | // Return the number of dependencies. | |
325 | unsigned int | |
326 | count_dependencies() const | |
327 | { return this->deps_.size(); } | |
328 | ||
329 | // Add a dependency to this version. The NAME should be | |
330 | // canonicalized in the dynamic Stringpool. | |
331 | void | |
332 | add_dependency(const char* name) | |
333 | { this->deps_.push_back(name); } | |
334 | ||
335 | // Return whether this definition is weak. | |
336 | bool | |
337 | is_weak() const | |
338 | { return this->is_weak_; } | |
339 | ||
340 | // Clear the weak flag. | |
341 | void | |
342 | clear_weak() | |
343 | { this->is_weak_ = false; } | |
344 | ||
345 | // Return whether a version symbol has been created for this | |
346 | // definition. | |
347 | bool | |
348 | is_symbol_created() const | |
349 | { return this->is_symbol_created_; } | |
350 | ||
351 | // Write contents to buffer. | |
352 | template<int size, bool big_endian> | |
353 | unsigned char* | |
91da9340 ILT |
354 | write(const Stringpool*, bool is_last, unsigned char* |
355 | ACCEPT_SIZE_ENDIAN) const; | |
14b31740 ILT |
356 | |
357 | private: | |
358 | Verdef(const Verdef&); | |
359 | Verdef& operator=(const Verdef&); | |
360 | ||
361 | // The type of the list of version dependencies. Each dependency | |
362 | // should be canonicalized in the dynamic Stringpool. | |
09124467 | 363 | typedef std::vector<std::string> Deps; |
14b31740 ILT |
364 | |
365 | // The name of this version. This should be canonicalized in the | |
366 | // dynamic Stringpool. | |
367 | const char* name_; | |
368 | // A list of other versions which this version depends upon. | |
369 | Deps deps_; | |
370 | // Whether this is the base version. | |
371 | bool is_base_; | |
372 | // Whether this version is weak. | |
373 | bool is_weak_; | |
374 | // Whether a version symbol has been created. | |
375 | bool is_symbol_created_; | |
376 | }; | |
377 | ||
378 | // A referened version. This will be associated with a filename by | |
379 | // Verneed. | |
380 | ||
381 | class Verneed_version : public Version_base | |
382 | { | |
383 | public: | |
384 | Verneed_version(const char* version) | |
385 | : version_(version) | |
386 | { } | |
387 | ||
388 | // Return the version name. | |
389 | const char* | |
390 | version() const | |
391 | { return this->version_; } | |
392 | ||
393 | // Clear the weak flag. This is invalid for a reference. | |
394 | void | |
395 | clear_weak() | |
396 | { gold_unreachable(); } | |
397 | ||
398 | private: | |
399 | Verneed_version(const Verneed_version&); | |
400 | Verneed_version& operator=(const Verneed_version&); | |
401 | ||
402 | const char* version_; | |
403 | }; | |
404 | ||
405 | // Version references in a single dynamic object. | |
406 | ||
407 | class Verneed | |
408 | { | |
409 | public: | |
410 | Verneed(const char* filename) | |
411 | : filename_(filename), need_versions_() | |
412 | { } | |
413 | ||
414 | ~Verneed(); | |
415 | ||
416 | // Return the file name. | |
417 | const char* | |
418 | filename() const | |
419 | { return this->filename_; } | |
420 | ||
421 | // Return the number of versions. | |
422 | unsigned int | |
423 | count_versions() const | |
424 | { return this->need_versions_.size(); } | |
425 | ||
426 | // Add a version name. The name should be canonicalized in the | |
427 | // dynamic Stringpool. If the name is already present, this does | |
428 | // nothing. | |
429 | Verneed_version* | |
430 | add_name(const char* name); | |
431 | ||
432 | // Set the version indexes, starting at INDEX. Return the updated | |
433 | // INDEX. | |
434 | unsigned int | |
435 | finalize(unsigned int index); | |
436 | ||
437 | // Write contents to buffer. | |
438 | template<int size, bool big_endian> | |
439 | unsigned char* | |
91da9340 ILT |
440 | write(const Stringpool*, bool is_last, unsigned char* |
441 | ACCEPT_SIZE_ENDIAN) const; | |
14b31740 ILT |
442 | |
443 | private: | |
444 | Verneed(const Verneed&); | |
445 | Verneed& operator=(const Verneed&); | |
446 | ||
447 | // The type of the list of version names. Each name should be | |
448 | // canonicalized in the dynamic Stringpool. | |
449 | typedef std::vector<Verneed_version*> Need_versions; | |
450 | ||
451 | // The filename of the dynamic object. This should be | |
452 | // canonicalized in the dynamic Stringpool. | |
453 | const char* filename_; | |
454 | // The list of version names. | |
455 | Need_versions need_versions_; | |
456 | }; | |
457 | ||
458 | // This class handles version definitions and references which go into | |
459 | // the output file. | |
460 | ||
461 | class Versions | |
462 | { | |
463 | public: | |
09124467 | 464 | Versions(const General_options&, Stringpool*); |
14b31740 ILT |
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 | |
35cdfc9a | 471 | record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym); |
14b31740 ILT |
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 | |
9b07f471 | 477 | finalize(Symbol_table* symtab, unsigned int dynsym_index, |
14b31740 ILT |
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 | |
46fe1623 ILT |
494 | symbol_section_contents(const Symbol_table*, const Stringpool*, |
495 | unsigned int local_symcount, | |
14b31740 | 496 | const std::vector<Symbol*>& syms, |
91da9340 ILT |
497 | unsigned char**, unsigned int* |
498 | ACCEPT_SIZE_ENDIAN) const; | |
14b31740 ILT |
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**, | |
91da9340 ILT |
505 | unsigned int* psize, unsigned int* pentries |
506 | ACCEPT_SIZE_ENDIAN) const; | |
14b31740 ILT |
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**, | |
91da9340 ILT |
513 | unsigned int* psize, unsigned int* pentries |
514 | ACCEPT_SIZE_ENDIAN) const; | |
14b31740 | 515 | |
09124467 ILT |
516 | const Version_script_info& |
517 | version_script() const | |
518 | { return this->version_script_; } | |
519 | ||
14b31740 | 520 | private: |
09124467 ILT |
521 | Versions(const Versions&); |
522 | Versions& operator=(const Versions&); | |
523 | ||
14b31740 ILT |
524 | // The type of the list of version definitions. |
525 | typedef std::vector<Verdef*> Defs; | |
526 | ||
527 | // The type of the list of version references. | |
528 | typedef std::vector<Verneed*> Needs; | |
529 | ||
530 | // Handle a symbol SYM defined with version VERSION. | |
531 | void | |
35cdfc9a | 532 | add_def(const Symbol* sym, const char* version, Stringpool::Key); |
14b31740 ILT |
533 | |
534 | // Add a reference to version NAME in file FILENAME. | |
535 | void | |
536 | add_need(Stringpool*, const char* filename, const char* name, | |
537 | Stringpool::Key); | |
538 | ||
46fe1623 ILT |
539 | // Get the dynamic object to use for SYM. |
540 | Dynobj* | |
541 | get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const; | |
542 | ||
14b31740 ILT |
543 | // Return the version index to use for SYM. |
544 | unsigned int | |
46fe1623 ILT |
545 | version_index(const Symbol_table*, const Stringpool*, |
546 | const Symbol* sym) const; | |
14b31740 ILT |
547 | |
548 | // We keep a hash table mapping canonicalized name/version pairs to | |
549 | // a version base. | |
550 | typedef std::pair<Stringpool::Key, Stringpool::Key> Key; | |
551 | ||
552 | struct Version_table_hash | |
553 | { | |
554 | size_t | |
555 | operator()(const Key& k) const | |
556 | { return k.first + k.second; } | |
557 | }; | |
558 | ||
559 | struct Version_table_eq | |
560 | { | |
561 | bool | |
562 | operator()(const Key& k1, const Key& k2) const | |
563 | { return k1.first == k2.first && k1.second == k2.second; } | |
564 | }; | |
565 | ||
566 | typedef Unordered_map<Key, Version_base*, Version_table_hash, | |
567 | Version_table_eq> Version_table; | |
568 | ||
569 | // The version definitions. | |
570 | Defs defs_; | |
571 | // The version references. | |
572 | Needs needs_; | |
573 | // The mapping from a canonicalized version/filename pair to a | |
574 | // version index. The filename may be NULL. | |
575 | Version_table version_table_; | |
576 | // Whether the version indexes have been set. | |
577 | bool is_finalized_; | |
09124467 ILT |
578 | // Contents of --version-script, if passed, or NULL. |
579 | const Version_script_info& version_script_; | |
14b31740 ILT |
580 | }; |
581 | ||
f6ce93d6 ILT |
582 | } // End namespace gold. |
583 | ||
584 | #endif // !defined(GOLD_DYNOBJ_H) |