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