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