Commit | Line | Data |
---|---|---|
dbe717ef ILT |
1 | // dynobj.cc -- dynamic object support for gold |
2 | ||
82704155 | 3 | // Copyright (C) 2006-2019 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 | ||
dbe717ef ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <vector> | |
26 | #include <cstring> | |
27 | ||
a3ad94ed | 28 | #include "elfcpp.h" |
7e1edb90 | 29 | #include "parameters.h" |
14144f39 | 30 | #include "script.h" |
dbe717ef ILT |
31 | #include "symtab.h" |
32 | #include "dynobj.h" | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
a3ad94ed ILT |
37 | // Class Dynobj. |
38 | ||
a7a81c1d ILT |
39 | // Sets up the default soname_ to use, in the (rare) cases we never |
40 | // see a DT_SONAME entry. | |
41 | ||
2ea97941 ILT |
42 | Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset) |
43 | : Object(name, input_file, true, offset), | |
e2827e5f ILT |
44 | needed_(), |
45 | unknown_needed_(UNKNOWN_NEEDED_UNSET) | |
a7a81c1d ILT |
46 | { |
47 | // This will be overridden by a DT_SONAME entry, hopefully. But if | |
48 | // we never see a DT_SONAME entry, our rule is to use the dynamic | |
49 | // object's filename. The only exception is when the dynamic object | |
50 | // is part of an archive (so the filename is the archive's | |
51 | // filename). In that case, we use just the dynobj's name-in-archive. | |
cdc29364 CC |
52 | if (input_file == NULL) |
53 | this->soname_ = name; | |
54 | else | |
a7a81c1d | 55 | { |
cdc29364 CC |
56 | this->soname_ = input_file->found_name(); |
57 | if (this->offset() != 0) | |
a7a81c1d | 58 | { |
cdc29364 CC |
59 | std::string::size_type open_paren = this->name().find('('); |
60 | std::string::size_type close_paren = this->name().find(')'); | |
61 | if (open_paren != std::string::npos | |
62 | && close_paren != std::string::npos) | |
63 | { | |
64 | // It's an archive, and name() is of the form 'foo.a(bar.so)'. | |
65 | open_paren += 1; | |
66 | this->soname_ = this->name().substr(open_paren, | |
67 | close_paren - open_paren); | |
68 | } | |
a7a81c1d ILT |
69 | } |
70 | } | |
71 | } | |
72 | ||
dbe717ef ILT |
73 | // Class Sized_dynobj. |
74 | ||
75 | template<int size, bool big_endian> | |
76 | Sized_dynobj<size, big_endian>::Sized_dynobj( | |
2ea97941 ILT |
77 | const std::string& name, |
78 | Input_file* input_file, | |
79 | off_t offset, | |
dbe717ef | 80 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
2ea97941 | 81 | : Dynobj(name, input_file, offset), |
d491d34e | 82 | elf_file_(this, ehdr), |
92de84a6 ILT |
83 | dynsym_shndx_(-1U), |
84 | symbols_(NULL), | |
85 | defined_count_(0) | |
dbe717ef ILT |
86 | { |
87 | } | |
88 | ||
89 | // Set up the object. | |
90 | ||
91 | template<int size, bool big_endian> | |
92 | void | |
029ba973 | 93 | Sized_dynobj<size, big_endian>::setup() |
dbe717ef | 94 | { |
2ea97941 ILT |
95 | const unsigned int shnum = this->elf_file_.shnum(); |
96 | this->set_shnum(shnum); | |
dbe717ef ILT |
97 | } |
98 | ||
99 | // Find the SHT_DYNSYM section and the various version sections, and | |
100 | // the dynamic section, given the section headers. | |
101 | ||
102 | template<int size, bool big_endian> | |
103 | void | |
104 | Sized_dynobj<size, big_endian>::find_dynsym_sections( | |
105 | const unsigned char* pshdrs, | |
dbe717ef ILT |
106 | unsigned int* pversym_shndx, |
107 | unsigned int* pverdef_shndx, | |
108 | unsigned int* pverneed_shndx, | |
109 | unsigned int* pdynamic_shndx) | |
110 | { | |
dbe717ef ILT |
111 | *pversym_shndx = -1U; |
112 | *pverdef_shndx = -1U; | |
113 | *pverneed_shndx = -1U; | |
114 | *pdynamic_shndx = -1U; | |
115 | ||
abecea76 | 116 | unsigned int symtab_shndx = 0; |
d491d34e ILT |
117 | unsigned int xindex_shndx = 0; |
118 | unsigned int xindex_link = 0; | |
2ea97941 | 119 | const unsigned int shnum = this->shnum(); |
dbe717ef | 120 | const unsigned char* p = pshdrs; |
2ea97941 | 121 | for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size) |
dbe717ef ILT |
122 | { |
123 | typename This::Shdr shdr(p); | |
124 | ||
125 | unsigned int* pi; | |
126 | switch (shdr.get_sh_type()) | |
127 | { | |
128 | case elfcpp::SHT_DYNSYM: | |
d491d34e ILT |
129 | this->dynsym_shndx_ = i; |
130 | if (xindex_shndx > 0 && xindex_link == i) | |
131 | { | |
132 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
133 | xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx, | |
134 | pshdrs); | |
135 | this->set_xindex(xindex); | |
136 | } | |
137 | pi = NULL; | |
dbe717ef | 138 | break; |
abecea76 ILT |
139 | case elfcpp::SHT_SYMTAB: |
140 | symtab_shndx = i; | |
2703e3eb | 141 | pi = NULL; |
abecea76 | 142 | break; |
dbe717ef ILT |
143 | case elfcpp::SHT_GNU_versym: |
144 | pi = pversym_shndx; | |
145 | break; | |
146 | case elfcpp::SHT_GNU_verdef: | |
147 | pi = pverdef_shndx; | |
148 | break; | |
149 | case elfcpp::SHT_GNU_verneed: | |
150 | pi = pverneed_shndx; | |
151 | break; | |
152 | case elfcpp::SHT_DYNAMIC: | |
153 | pi = pdynamic_shndx; | |
154 | break; | |
d491d34e ILT |
155 | case elfcpp::SHT_SYMTAB_SHNDX: |
156 | xindex_shndx = i; | |
157 | xindex_link = this->adjust_shndx(shdr.get_sh_link()); | |
158 | if (xindex_link == this->dynsym_shndx_) | |
159 | { | |
160 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
161 | xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx, | |
162 | pshdrs); | |
163 | this->set_xindex(xindex); | |
164 | } | |
165 | pi = NULL; | |
166 | break; | |
dbe717ef ILT |
167 | default: |
168 | pi = NULL; | |
169 | break; | |
170 | } | |
171 | ||
172 | if (pi == NULL) | |
173 | continue; | |
174 | ||
175 | if (*pi != -1U) | |
75f2446e ILT |
176 | this->error(_("unexpected duplicate type %u section: %u, %u"), |
177 | shdr.get_sh_type(), *pi, i); | |
dbe717ef ILT |
178 | |
179 | *pi = i; | |
180 | } | |
abecea76 ILT |
181 | |
182 | // If there is no dynamic symbol table, use the normal symbol table. | |
183 | // On some SVR4 systems, a shared library is stored in an archive. | |
184 | // The version stored in the archive only has a normal symbol table. | |
185 | // It has an SONAME entry which points to another copy in the file | |
186 | // system which has a dynamic symbol table as usual. This is way of | |
187 | // addressing the issues which glibc addresses using GROUP with | |
188 | // libc_nonshared.a. | |
189 | if (this->dynsym_shndx_ == -1U && symtab_shndx != 0) | |
190 | { | |
191 | this->dynsym_shndx_ = symtab_shndx; | |
192 | if (xindex_shndx > 0 && xindex_link == symtab_shndx) | |
193 | { | |
194 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
195 | xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx, | |
196 | pshdrs); | |
197 | this->set_xindex(xindex); | |
198 | } | |
199 | } | |
dbe717ef ILT |
200 | } |
201 | ||
202 | // Read the contents of section SHNDX. PSHDRS points to the section | |
203 | // headers. TYPE is the expected section type. LINK is the expected | |
204 | // section link. Store the data in *VIEW and *VIEW_SIZE. The | |
205 | // section's sh_info field is stored in *VIEW_INFO. | |
206 | ||
207 | template<int size, bool big_endian> | |
208 | void | |
209 | Sized_dynobj<size, big_endian>::read_dynsym_section( | |
210 | const unsigned char* pshdrs, | |
211 | unsigned int shndx, | |
212 | elfcpp::SHT type, | |
213 | unsigned int link, | |
2ea97941 | 214 | File_view** view, |
8383303e | 215 | section_size_type* view_size, |
dbe717ef ILT |
216 | unsigned int* view_info) |
217 | { | |
218 | if (shndx == -1U) | |
219 | { | |
2ea97941 | 220 | *view = NULL; |
dbe717ef ILT |
221 | *view_size = 0; |
222 | *view_info = 0; | |
223 | return; | |
224 | } | |
225 | ||
226 | typename This::Shdr shdr(pshdrs + shndx * This::shdr_size); | |
227 | ||
a3ad94ed | 228 | gold_assert(shdr.get_sh_type() == type); |
dbe717ef | 229 | |
d491d34e | 230 | if (this->adjust_shndx(shdr.get_sh_link()) != link) |
75f2446e | 231 | this->error(_("unexpected link in section %u header: %u != %u"), |
d491d34e | 232 | shndx, this->adjust_shndx(shdr.get_sh_link()), link); |
dbe717ef | 233 | |
2ea97941 ILT |
234 | *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(), |
235 | true, false); | |
8383303e | 236 | *view_size = convert_to_section_size_type(shdr.get_sh_size()); |
dbe717ef ILT |
237 | *view_info = shdr.get_sh_info(); |
238 | } | |
239 | ||
e2827e5f ILT |
240 | // Read the dynamic tags. Set the soname field if this shared object |
241 | // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to | |
242 | // the section headers. DYNAMIC_SHNDX is the section index of the | |
243 | // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the | |
244 | // section index and contents of a string table which may be the one | |
245 | // associated with the SHT_DYNAMIC section. | |
dbe717ef ILT |
246 | |
247 | template<int size, bool big_endian> | |
248 | void | |
e2827e5f ILT |
249 | Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs, |
250 | unsigned int dynamic_shndx, | |
251 | unsigned int strtab_shndx, | |
252 | const unsigned char* strtabu, | |
253 | off_t strtab_size) | |
dbe717ef ILT |
254 | { |
255 | typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size); | |
a3ad94ed | 256 | gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC); |
dbe717ef ILT |
257 | |
258 | const off_t dynamic_size = dynamicshdr.get_sh_size(); | |
259 | const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(), | |
39d0cb0e | 260 | dynamic_size, true, false); |
dbe717ef | 261 | |
d491d34e | 262 | const unsigned int link = this->adjust_shndx(dynamicshdr.get_sh_link()); |
dbe717ef ILT |
263 | if (link != strtab_shndx) |
264 | { | |
265 | if (link >= this->shnum()) | |
266 | { | |
75f2446e ILT |
267 | this->error(_("DYNAMIC section %u link out of range: %u"), |
268 | dynamic_shndx, link); | |
269 | return; | |
dbe717ef ILT |
270 | } |
271 | ||
272 | typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size); | |
273 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) | |
274 | { | |
75f2446e ILT |
275 | this->error(_("DYNAMIC section %u link %u is not a strtab"), |
276 | dynamic_shndx, link); | |
277 | return; | |
dbe717ef ILT |
278 | } |
279 | ||
280 | strtab_size = strtabshdr.get_sh_size(); | |
39d0cb0e ILT |
281 | strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false, |
282 | false); | |
dbe717ef ILT |
283 | } |
284 | ||
e2827e5f ILT |
285 | const char* const strtab = reinterpret_cast<const char*>(strtabu); |
286 | ||
dbe717ef ILT |
287 | for (const unsigned char* p = pdynamic; |
288 | p < pdynamic + dynamic_size; | |
289 | p += This::dyn_size) | |
290 | { | |
291 | typename This::Dyn dyn(p); | |
292 | ||
e2827e5f | 293 | switch (dyn.get_d_tag()) |
dbe717ef | 294 | { |
e2827e5f ILT |
295 | case elfcpp::DT_NULL: |
296 | // We should always see DT_NULL at the end of the dynamic | |
297 | // tags. | |
298 | return; | |
299 | ||
300 | case elfcpp::DT_SONAME: | |
301 | { | |
302 | off_t val = dyn.get_d_val(); | |
303 | if (val >= strtab_size) | |
75f2446e | 304 | this->error(_("DT_SONAME value out of range: %lld >= %lld"), |
e2827e5f ILT |
305 | static_cast<long long>(val), |
306 | static_cast<long long>(strtab_size)); | |
307 | else | |
308 | this->set_soname_string(strtab + val); | |
309 | } | |
310 | break; | |
dbe717ef | 311 | |
e2827e5f ILT |
312 | case elfcpp::DT_NEEDED: |
313 | { | |
314 | off_t val = dyn.get_d_val(); | |
315 | if (val >= strtab_size) | |
316 | this->error(_("DT_NEEDED value out of range: %lld >= %lld"), | |
317 | static_cast<long long>(val), | |
318 | static_cast<long long>(strtab_size)); | |
319 | else | |
320 | this->add_needed(strtab + val); | |
321 | } | |
322 | break; | |
dbe717ef | 323 | |
e2827e5f ILT |
324 | default: |
325 | break; | |
326 | } | |
dbe717ef ILT |
327 | } |
328 | ||
75f2446e | 329 | this->error(_("missing DT_NULL in dynamic segment")); |
dbe717ef ILT |
330 | } |
331 | ||
332 | // Read the symbols and sections from a dynamic object. We read the | |
333 | // dynamic symbols, not the normal symbols. | |
334 | ||
335 | template<int size, bool big_endian> | |
336 | void | |
337 | Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) | |
f35c4853 CC |
338 | { |
339 | this->base_read_symbols(sd); | |
340 | } | |
341 | ||
342 | // Read the symbols and sections from a dynamic object. We read the | |
343 | // dynamic symbols, not the normal symbols. This is common code for | |
344 | // all target-specific overrides of do_read_symbols(). | |
345 | ||
346 | template<int size, bool big_endian> | |
347 | void | |
348 | Sized_dynobj<size, big_endian>::base_read_symbols(Read_symbols_data* sd) | |
dbe717ef ILT |
349 | { |
350 | this->read_section_data(&this->elf_file_, sd); | |
351 | ||
352 | const unsigned char* const pshdrs = sd->section_headers->data(); | |
353 | ||
dbe717ef ILT |
354 | unsigned int versym_shndx; |
355 | unsigned int verdef_shndx; | |
356 | unsigned int verneed_shndx; | |
357 | unsigned int dynamic_shndx; | |
d491d34e ILT |
358 | this->find_dynsym_sections(pshdrs, &versym_shndx, &verdef_shndx, |
359 | &verneed_shndx, &dynamic_shndx); | |
dbe717ef ILT |
360 | |
361 | unsigned int strtab_shndx = -1U; | |
362 | ||
75f2446e ILT |
363 | sd->symbols = NULL; |
364 | sd->symbols_size = 0; | |
730cdc88 | 365 | sd->external_symbols_offset = 0; |
75f2446e ILT |
366 | sd->symbol_names = NULL; |
367 | sd->symbol_names_size = 0; | |
1276bc89 ILT |
368 | sd->versym = NULL; |
369 | sd->versym_size = 0; | |
370 | sd->verdef = NULL; | |
371 | sd->verdef_size = 0; | |
372 | sd->verdef_info = 0; | |
373 | sd->verneed = NULL; | |
374 | sd->verneed_size = 0; | |
375 | sd->verneed_info = 0; | |
75f2446e | 376 | |
0d5bbdb0 CC |
377 | const unsigned char* namesu = sd->section_names->data(); |
378 | const char* names = reinterpret_cast<const char*>(namesu); | |
379 | if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL) | |
380 | { | |
381 | Compressed_section_map* compressed_sections = | |
382 | build_compressed_section_map<size, big_endian>( | |
383 | pshdrs, this->shnum(), names, sd->section_names_size, this, true); | |
384 | if (compressed_sections != NULL) | |
385 | this->set_compressed_sections(compressed_sections); | |
386 | } | |
387 | ||
d491d34e | 388 | if (this->dynsym_shndx_ != -1U) |
dbe717ef ILT |
389 | { |
390 | // Get the dynamic symbols. | |
d491d34e ILT |
391 | typename This::Shdr dynsymshdr(pshdrs |
392 | + this->dynsym_shndx_ * This::shdr_size); | |
dbe717ef ILT |
393 | |
394 | sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(), | |
39d0cb0e ILT |
395 | dynsymshdr.get_sh_size(), true, |
396 | false); | |
8383303e ILT |
397 | sd->symbols_size = |
398 | convert_to_section_size_type(dynsymshdr.get_sh_size()); | |
dbe717ef ILT |
399 | |
400 | // Get the symbol names. | |
d491d34e | 401 | strtab_shndx = this->adjust_shndx(dynsymshdr.get_sh_link()); |
dbe717ef ILT |
402 | if (strtab_shndx >= this->shnum()) |
403 | { | |
75f2446e ILT |
404 | this->error(_("invalid dynamic symbol table name index: %u"), |
405 | strtab_shndx); | |
406 | return; | |
dbe717ef ILT |
407 | } |
408 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); | |
409 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) | |
410 | { | |
75f2446e ILT |
411 | this->error(_("dynamic symbol table name section " |
412 | "has wrong type: %u"), | |
413 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
414 | return; | |
dbe717ef ILT |
415 | } |
416 | ||
417 | sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
9eb9fa57 | 418 | strtabshdr.get_sh_size(), |
39d0cb0e | 419 | false, false); |
8383303e ILT |
420 | sd->symbol_names_size = |
421 | convert_to_section_size_type(strtabshdr.get_sh_size()); | |
dbe717ef ILT |
422 | |
423 | // Get the version information. | |
424 | ||
425 | unsigned int dummy; | |
426 | this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym, | |
d491d34e ILT |
427 | this->dynsym_shndx_, |
428 | &sd->versym, &sd->versym_size, &dummy); | |
dbe717ef ILT |
429 | |
430 | // We require that the version definition and need section link | |
431 | // to the same string table as the dynamic symbol table. This | |
432 | // is not a technical requirement, but it always happens in | |
433 | // practice. We could change this if necessary. | |
434 | ||
435 | this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef, | |
436 | strtab_shndx, &sd->verdef, &sd->verdef_size, | |
437 | &sd->verdef_info); | |
438 | ||
439 | this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed, | |
440 | strtab_shndx, &sd->verneed, &sd->verneed_size, | |
441 | &sd->verneed_info); | |
442 | } | |
443 | ||
444 | // Read the SHT_DYNAMIC section to find whether this shared object | |
e2827e5f ILT |
445 | // has a DT_SONAME tag and to record any DT_NEEDED tags. This |
446 | // doesn't really have anything to do with reading the symbols, but | |
447 | // this is a convenient place to do it. | |
dbe717ef | 448 | if (dynamic_shndx != -1U) |
e2827e5f ILT |
449 | this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx, |
450 | (sd->symbol_names == NULL | |
451 | ? NULL | |
452 | : sd->symbol_names->data()), | |
453 | sd->symbol_names_size); | |
dbe717ef ILT |
454 | } |
455 | ||
d491d34e ILT |
456 | // Return the Xindex structure to use for object with lots of |
457 | // sections. | |
458 | ||
459 | template<int size, bool big_endian> | |
460 | Xindex* | |
461 | Sized_dynobj<size, big_endian>::do_initialize_xindex() | |
462 | { | |
463 | gold_assert(this->dynsym_shndx_ != -1U); | |
464 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
465 | xindex->initialize_symtab_xindex<size, big_endian>(this, this->dynsym_shndx_); | |
466 | return xindex; | |
467 | } | |
468 | ||
dbe717ef ILT |
469 | // Lay out the input sections for a dynamic object. We don't want to |
470 | // include sections from a dynamic object, so all that we actually do | |
364c7fa5 | 471 | // here is check for .gnu.warning and .note.GNU-split-stack sections. |
dbe717ef ILT |
472 | |
473 | template<int size, bool big_endian> | |
474 | void | |
7e1edb90 | 475 | Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab, |
dbe717ef ILT |
476 | Layout*, |
477 | Read_symbols_data* sd) | |
478 | { | |
2ea97941 ILT |
479 | const unsigned int shnum = this->shnum(); |
480 | if (shnum == 0) | |
dbe717ef ILT |
481 | return; |
482 | ||
483 | // Get the section headers. | |
484 | const unsigned char* pshdrs = sd->section_headers->data(); | |
485 | ||
486 | // Get the section names. | |
487 | const unsigned char* pnamesu = sd->section_names->data(); | |
488 | const char* pnames = reinterpret_cast<const char*>(pnamesu); | |
489 | ||
490 | // Skip the first, dummy, section. | |
491 | pshdrs += This::shdr_size; | |
2ea97941 | 492 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
dbe717ef ILT |
493 | { |
494 | typename This::Shdr shdr(pshdrs); | |
495 | ||
496 | if (shdr.get_sh_name() >= sd->section_names_size) | |
497 | { | |
75f2446e ILT |
498 | this->error(_("bad section name offset for section %u: %lu"), |
499 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
500 | return; | |
dbe717ef ILT |
501 | } |
502 | ||
2ea97941 | 503 | const char* name = pnames + shdr.get_sh_name(); |
dbe717ef | 504 | |
2ea97941 ILT |
505 | this->handle_gnu_warning_section(name, i, symtab); |
506 | this->handle_split_stack_section(name); | |
dbe717ef ILT |
507 | } |
508 | ||
509 | delete sd->section_headers; | |
510 | sd->section_headers = NULL; | |
511 | delete sd->section_names; | |
512 | sd->section_names = NULL; | |
513 | } | |
514 | ||
515 | // Add an entry to the vector mapping version numbers to version | |
516 | // strings. | |
517 | ||
518 | template<int size, bool big_endian> | |
519 | void | |
520 | Sized_dynobj<size, big_endian>::set_version_map( | |
521 | Version_map* version_map, | |
522 | unsigned int ndx, | |
2ea97941 | 523 | const char* name) const |
dbe717ef | 524 | { |
14b31740 ILT |
525 | if (ndx >= version_map->size()) |
526 | version_map->resize(ndx + 1); | |
dbe717ef | 527 | if ((*version_map)[ndx] != NULL) |
75f2446e | 528 | this->error(_("duplicate definition for version %u"), ndx); |
2ea97941 | 529 | (*version_map)[ndx] = name; |
dbe717ef ILT |
530 | } |
531 | ||
14b31740 | 532 | // Add mappings for the version definitions to VERSION_MAP. |
dbe717ef ILT |
533 | |
534 | template<int size, bool big_endian> | |
535 | void | |
14b31740 | 536 | Sized_dynobj<size, big_endian>::make_verdef_map( |
dbe717ef ILT |
537 | Read_symbols_data* sd, |
538 | Version_map* version_map) const | |
539 | { | |
14b31740 | 540 | if (sd->verdef == NULL) |
dbe717ef ILT |
541 | return; |
542 | ||
14b31740 | 543 | const char* names = reinterpret_cast<const char*>(sd->symbol_names->data()); |
8383303e | 544 | section_size_type names_size = sd->symbol_names_size; |
dbe717ef | 545 | |
14b31740 | 546 | const unsigned char* pverdef = sd->verdef->data(); |
8383303e | 547 | section_size_type verdef_size = sd->verdef_size; |
14b31740 ILT |
548 | const unsigned int count = sd->verdef_info; |
549 | ||
550 | const unsigned char* p = pverdef; | |
551 | for (unsigned int i = 0; i < count; ++i) | |
dbe717ef | 552 | { |
14b31740 | 553 | elfcpp::Verdef<size, big_endian> verdef(p); |
dbe717ef | 554 | |
14b31740 | 555 | if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT) |
dbe717ef | 556 | { |
75f2446e ILT |
557 | this->error(_("unexpected verdef version %u"), |
558 | verdef.get_vd_version()); | |
559 | return; | |
14b31740 | 560 | } |
dbe717ef | 561 | |
9bb53bf8 | 562 | const section_size_type vd_ndx = verdef.get_vd_ndx(); |
dbe717ef | 563 | |
14b31740 ILT |
564 | // The GNU linker clears the VERSYM_HIDDEN bit. I'm not |
565 | // sure why. | |
dbe717ef | 566 | |
14b31740 ILT |
567 | // The first Verdaux holds the name of this version. Subsequent |
568 | // ones are versions that this one depends upon, which we don't | |
569 | // care about here. | |
9bb53bf8 | 570 | const section_size_type vd_cnt = verdef.get_vd_cnt(); |
14b31740 ILT |
571 | if (vd_cnt < 1) |
572 | { | |
9bb53bf8 ILT |
573 | this->error(_("verdef vd_cnt field too small: %u"), |
574 | static_cast<unsigned int>(vd_cnt)); | |
75f2446e | 575 | return; |
dbe717ef | 576 | } |
dbe717ef | 577 | |
9bb53bf8 | 578 | const section_size_type vd_aux = verdef.get_vd_aux(); |
14b31740 | 579 | if ((p - pverdef) + vd_aux >= verdef_size) |
dbe717ef | 580 | { |
9bb53bf8 ILT |
581 | this->error(_("verdef vd_aux field out of range: %u"), |
582 | static_cast<unsigned int>(vd_aux)); | |
75f2446e | 583 | return; |
14b31740 | 584 | } |
dbe717ef | 585 | |
14b31740 ILT |
586 | const unsigned char* pvda = p + vd_aux; |
587 | elfcpp::Verdaux<size, big_endian> verdaux(pvda); | |
dbe717ef | 588 | |
9bb53bf8 | 589 | const section_size_type vda_name = verdaux.get_vda_name(); |
14b31740 ILT |
590 | if (vda_name >= names_size) |
591 | { | |
9bb53bf8 ILT |
592 | this->error(_("verdaux vda_name field out of range: %u"), |
593 | static_cast<unsigned int>(vda_name)); | |
75f2446e | 594 | return; |
14b31740 | 595 | } |
dbe717ef | 596 | |
14b31740 | 597 | this->set_version_map(version_map, vd_ndx, names + vda_name); |
dbe717ef | 598 | |
9bb53bf8 | 599 | const section_size_type vd_next = verdef.get_vd_next(); |
14b31740 ILT |
600 | if ((p - pverdef) + vd_next >= verdef_size) |
601 | { | |
9bb53bf8 ILT |
602 | this->error(_("verdef vd_next field out of range: %u"), |
603 | static_cast<unsigned int>(vd_next)); | |
75f2446e | 604 | return; |
dbe717ef | 605 | } |
14b31740 ILT |
606 | |
607 | p += vd_next; | |
dbe717ef | 608 | } |
14b31740 | 609 | } |
dbe717ef | 610 | |
14b31740 | 611 | // Add mappings for the required versions to VERSION_MAP. |
dbe717ef | 612 | |
14b31740 ILT |
613 | template<int size, bool big_endian> |
614 | void | |
615 | Sized_dynobj<size, big_endian>::make_verneed_map( | |
616 | Read_symbols_data* sd, | |
617 | Version_map* version_map) const | |
618 | { | |
619 | if (sd->verneed == NULL) | |
620 | return; | |
dbe717ef ILT |
621 | |
622 | const char* names = reinterpret_cast<const char*>(sd->symbol_names->data()); | |
8383303e | 623 | section_size_type names_size = sd->symbol_names_size; |
dbe717ef | 624 | |
14b31740 | 625 | const unsigned char* pverneed = sd->verneed->data(); |
8383303e | 626 | const section_size_type verneed_size = sd->verneed_size; |
14b31740 ILT |
627 | const unsigned int count = sd->verneed_info; |
628 | ||
629 | const unsigned char* p = pverneed; | |
630 | for (unsigned int i = 0; i < count; ++i) | |
dbe717ef | 631 | { |
14b31740 | 632 | elfcpp::Verneed<size, big_endian> verneed(p); |
dbe717ef | 633 | |
14b31740 | 634 | if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT) |
dbe717ef | 635 | { |
75f2446e ILT |
636 | this->error(_("unexpected verneed version %u"), |
637 | verneed.get_vn_version()); | |
638 | return; | |
14b31740 | 639 | } |
dbe717ef | 640 | |
9bb53bf8 | 641 | const section_size_type vn_aux = verneed.get_vn_aux(); |
dbe717ef | 642 | |
14b31740 ILT |
643 | if ((p - pverneed) + vn_aux >= verneed_size) |
644 | { | |
9bb53bf8 ILT |
645 | this->error(_("verneed vn_aux field out of range: %u"), |
646 | static_cast<unsigned int>(vn_aux)); | |
75f2446e | 647 | return; |
14b31740 | 648 | } |
dbe717ef | 649 | |
14b31740 ILT |
650 | const unsigned int vn_cnt = verneed.get_vn_cnt(); |
651 | const unsigned char* pvna = p + vn_aux; | |
652 | for (unsigned int j = 0; j < vn_cnt; ++j) | |
653 | { | |
654 | elfcpp::Vernaux<size, big_endian> vernaux(pvna); | |
dbe717ef | 655 | |
14b31740 ILT |
656 | const unsigned int vna_name = vernaux.get_vna_name(); |
657 | if (vna_name >= names_size) | |
dbe717ef | 658 | { |
75f2446e | 659 | this->error(_("vernaux vna_name field out of range: %u"), |
9bb53bf8 | 660 | static_cast<unsigned int>(vna_name)); |
75f2446e | 661 | return; |
dbe717ef ILT |
662 | } |
663 | ||
14b31740 ILT |
664 | this->set_version_map(version_map, vernaux.get_vna_other(), |
665 | names + vna_name); | |
dbe717ef | 666 | |
9bb53bf8 | 667 | const section_size_type vna_next = vernaux.get_vna_next(); |
14b31740 | 668 | if ((pvna - pverneed) + vna_next >= verneed_size) |
dbe717ef | 669 | { |
75f2446e | 670 | this->error(_("verneed vna_next field out of range: %u"), |
9bb53bf8 | 671 | static_cast<unsigned int>(vna_next)); |
75f2446e | 672 | return; |
dbe717ef ILT |
673 | } |
674 | ||
14b31740 ILT |
675 | pvna += vna_next; |
676 | } | |
677 | ||
9bb53bf8 | 678 | const section_size_type vn_next = verneed.get_vn_next(); |
14b31740 ILT |
679 | if ((p - pverneed) + vn_next >= verneed_size) |
680 | { | |
9bb53bf8 ILT |
681 | this->error(_("verneed vn_next field out of range: %u"), |
682 | static_cast<unsigned int>(vn_next)); | |
75f2446e | 683 | return; |
dbe717ef | 684 | } |
14b31740 ILT |
685 | |
686 | p += vn_next; | |
dbe717ef | 687 | } |
14b31740 | 688 | } |
dbe717ef | 689 | |
14b31740 | 690 | // Create a vector mapping version numbers to version strings. |
dbe717ef | 691 | |
14b31740 ILT |
692 | template<int size, bool big_endian> |
693 | void | |
694 | Sized_dynobj<size, big_endian>::make_version_map( | |
695 | Read_symbols_data* sd, | |
696 | Version_map* version_map) const | |
697 | { | |
698 | if (sd->verdef == NULL && sd->verneed == NULL) | |
699 | return; | |
dbe717ef | 700 | |
14b31740 ILT |
701 | // A guess at the maximum version number we will see. If this is |
702 | // wrong we will be less efficient but still correct. | |
703 | version_map->reserve(sd->verdef_info + sd->verneed_info * 10); | |
dbe717ef | 704 | |
14b31740 ILT |
705 | this->make_verdef_map(sd, version_map); |
706 | this->make_verneed_map(sd, version_map); | |
dbe717ef ILT |
707 | } |
708 | ||
709 | // Add the dynamic symbols to the symbol table. | |
710 | ||
711 | template<int size, bool big_endian> | |
712 | void | |
713 | Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, | |
f488e4b0 CC |
714 | Read_symbols_data* sd, |
715 | Layout*) | |
dbe717ef ILT |
716 | { |
717 | if (sd->symbols == NULL) | |
718 | { | |
a3ad94ed ILT |
719 | gold_assert(sd->symbol_names == NULL); |
720 | gold_assert(sd->versym == NULL && sd->verdef == NULL | |
721 | && sd->verneed == NULL); | |
dbe717ef ILT |
722 | return; |
723 | } | |
724 | ||
2ea97941 ILT |
725 | const int sym_size = This::sym_size; |
726 | const size_t symcount = sd->symbols_size / sym_size; | |
730cdc88 | 727 | gold_assert(sd->external_symbols_offset == 0); |
2ea97941 | 728 | if (symcount * sym_size != sd->symbols_size) |
dbe717ef | 729 | { |
75f2446e ILT |
730 | this->error(_("size of dynamic symbols is not multiple of symbol size")); |
731 | return; | |
dbe717ef ILT |
732 | } |
733 | ||
734 | Version_map version_map; | |
735 | this->make_version_map(sd, &version_map); | |
736 | ||
cdc29364 CC |
737 | // If printing symbol counts or a cross reference table or |
738 | // preparing for an incremental link, we want to track symbols. | |
dde3f402 | 739 | if (parameters->options().user_set_print_symbol_counts() |
cdc29364 CC |
740 | || parameters->options().cref() |
741 | || parameters->incremental()) | |
92de84a6 ILT |
742 | { |
743 | this->symbols_ = new Symbols(); | |
744 | this->symbols_->resize(symcount); | |
745 | } | |
746 | ||
dbe717ef ILT |
747 | const char* sym_names = |
748 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
749 | symtab->add_from_dynobj(this, sd->symbols->data(), symcount, | |
750 | sym_names, sd->symbol_names_size, | |
751 | (sd->versym == NULL | |
752 | ? NULL | |
753 | : sd->versym->data()), | |
754 | sd->versym_size, | |
92de84a6 ILT |
755 | &version_map, |
756 | this->symbols_, | |
757 | &this->defined_count_); | |
dbe717ef ILT |
758 | |
759 | delete sd->symbols; | |
760 | sd->symbols = NULL; | |
761 | delete sd->symbol_names; | |
762 | sd->symbol_names = NULL; | |
763 | if (sd->versym != NULL) | |
764 | { | |
765 | delete sd->versym; | |
766 | sd->versym = NULL; | |
767 | } | |
768 | if (sd->verdef != NULL) | |
769 | { | |
770 | delete sd->verdef; | |
771 | sd->verdef = NULL; | |
772 | } | |
773 | if (sd->verneed != NULL) | |
774 | { | |
775 | delete sd->verneed; | |
776 | sd->verneed = NULL; | |
777 | } | |
cb295612 ILT |
778 | |
779 | // This is normally the last time we will read any data from this | |
780 | // file. | |
781 | this->clear_view_cache_marks(); | |
dbe717ef ILT |
782 | } |
783 | ||
b0193076 RÁE |
784 | template<int size, bool big_endian> |
785 | Archive::Should_include | |
88a4108b ILT |
786 | Sized_dynobj<size, big_endian>::do_should_include_member(Symbol_table*, |
787 | Layout*, | |
788 | Read_symbols_data*, | |
789 | std::string*) | |
b0193076 RÁE |
790 | { |
791 | return Archive::SHOULD_INCLUDE_YES; | |
792 | } | |
793 | ||
e0c52780 CC |
794 | // Iterate over global symbols, calling a visitor class V for each. |
795 | ||
796 | template<int size, bool big_endian> | |
797 | void | |
798 | Sized_dynobj<size, big_endian>::do_for_all_global_symbols( | |
799 | Read_symbols_data* sd, | |
800 | Library_base::Symbol_visitor_base* v) | |
801 | { | |
802 | const char* sym_names = | |
803 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
804 | const unsigned char* syms = | |
805 | sd->symbols->data() + sd->external_symbols_offset; | |
806 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
807 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) | |
808 | / sym_size); | |
809 | const unsigned char* p = syms; | |
810 | ||
811 | for (size_t i = 0; i < symcount; ++i, p += sym_size) | |
812 | { | |
813 | elfcpp::Sym<size, big_endian> sym(p); | |
814 | if (sym.get_st_shndx() != elfcpp::SHN_UNDEF | |
815 | && sym.get_st_bind() != elfcpp::STB_LOCAL) | |
816 | v->visit(sym_names + sym.get_st_name()); | |
817 | } | |
818 | } | |
819 | ||
cdc29364 CC |
820 | // Iterate over local symbols, calling a visitor class V for each GOT offset |
821 | // associated with a local symbol. | |
822 | ||
823 | template<int size, bool big_endian> | |
824 | void | |
825 | Sized_dynobj<size, big_endian>::do_for_all_local_got_entries( | |
826 | Got_offset_list::Visitor*) const | |
827 | { | |
828 | } | |
829 | ||
92de84a6 ILT |
830 | // Get symbol counts. |
831 | ||
832 | template<int size, bool big_endian> | |
833 | void | |
834 | Sized_dynobj<size, big_endian>::do_get_global_symbol_counts( | |
835 | const Symbol_table*, | |
836 | size_t* defined, | |
837 | size_t* used) const | |
838 | { | |
839 | *defined = this->defined_count_; | |
840 | size_t count = 0; | |
841 | for (typename Symbols::const_iterator p = this->symbols_->begin(); | |
842 | p != this->symbols_->end(); | |
843 | ++p) | |
844 | if (*p != NULL | |
845 | && (*p)->source() == Symbol::FROM_OBJECT | |
846 | && (*p)->object() == this | |
847 | && (*p)->is_defined() | |
fad072ac | 848 | && (*p)->has_dynsym_index()) |
92de84a6 ILT |
849 | ++count; |
850 | *used = count; | |
851 | } | |
852 | ||
a3ad94ed ILT |
853 | // Given a vector of hash codes, compute the number of hash buckets to |
854 | // use. | |
855 | ||
856 | unsigned int | |
857 | Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes, | |
858 | bool for_gnu_hash_table) | |
859 | { | |
860 | // FIXME: Implement optional hash table optimization. | |
861 | ||
862 | // Array used to determine the number of hash table buckets to use | |
863 | // based on the number of symbols there are. If there are fewer | |
864 | // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 | |
865 | // buckets, fewer than 37 we use 17 buckets, and so forth. We never | |
95317043 | 866 | // use more than 262147 buckets. This is straight from the old GNU |
a3ad94ed ILT |
867 | // linker. |
868 | static const unsigned int buckets[] = | |
869 | { | |
870 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, | |
95317043 | 871 | 16411, 32771, 65537, 131101, 262147 |
a3ad94ed ILT |
872 | }; |
873 | const int buckets_count = sizeof buckets / sizeof buckets[0]; | |
874 | ||
875 | unsigned int symcount = hashcodes.size(); | |
876 | unsigned int ret = 1; | |
a8627968 ILT |
877 | const double full_fraction |
878 | = 1.0 - parameters->options().hash_bucket_empty_fraction(); | |
a3ad94ed ILT |
879 | for (int i = 0; i < buckets_count; ++i) |
880 | { | |
a8627968 | 881 | if (symcount < buckets[i] * full_fraction) |
a3ad94ed ILT |
882 | break; |
883 | ret = buckets[i]; | |
884 | } | |
885 | ||
886 | if (for_gnu_hash_table && ret < 2) | |
887 | ret = 2; | |
888 | ||
889 | return ret; | |
890 | } | |
891 | ||
892 | // The standard ELF hash function. This hash function must not | |
893 | // change, as the dynamic linker uses it also. | |
894 | ||
895 | uint32_t | |
896 | Dynobj::elf_hash(const char* name) | |
897 | { | |
898 | const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name); | |
899 | uint32_t h = 0; | |
900 | unsigned char c; | |
901 | while ((c = *nameu++) != '\0') | |
902 | { | |
903 | h = (h << 4) + c; | |
904 | uint32_t g = h & 0xf0000000; | |
905 | if (g != 0) | |
906 | { | |
907 | h ^= g >> 24; | |
908 | // The ELF ABI says h &= ~g, but using xor is equivalent in | |
909 | // this case (since g was set from h) and may save one | |
910 | // instruction. | |
911 | h ^= g; | |
912 | } | |
913 | } | |
914 | return h; | |
915 | } | |
916 | ||
917 | // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN. | |
918 | // DYNSYMS is a vector with all the global dynamic symbols. | |
919 | // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic | |
920 | // symbol table. | |
921 | ||
922 | void | |
9025d29d | 923 | Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms, |
a3ad94ed ILT |
924 | unsigned int local_dynsym_count, |
925 | unsigned char** pphash, | |
926 | unsigned int* phashlen) | |
927 | { | |
928 | unsigned int dynsym_count = dynsyms.size(); | |
929 | ||
930 | // Get the hash values for all the symbols. | |
931 | std::vector<uint32_t> dynsym_hashvals(dynsym_count); | |
932 | for (unsigned int i = 0; i < dynsym_count; ++i) | |
933 | dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name()); | |
934 | ||
935 | const unsigned int bucketcount = | |
936 | Dynobj::compute_bucket_count(dynsym_hashvals, false); | |
937 | ||
938 | std::vector<uint32_t> bucket(bucketcount); | |
939 | std::vector<uint32_t> chain(local_dynsym_count + dynsym_count); | |
940 | ||
941 | for (unsigned int i = 0; i < dynsym_count; ++i) | |
942 | { | |
943 | unsigned int dynsym_index = dynsyms[i]->dynsym_index(); | |
944 | unsigned int bucketpos = dynsym_hashvals[i] % bucketcount; | |
945 | chain[dynsym_index] = bucket[bucketpos]; | |
946 | bucket[bucketpos] = dynsym_index; | |
947 | } | |
948 | ||
8d9743bd | 949 | int size = parameters->target().hash_entry_size(); |
a3ad94ed ILT |
950 | unsigned int hashlen = ((2 |
951 | + bucketcount | |
952 | + local_dynsym_count | |
953 | + dynsym_count) | |
8d9743bd | 954 | * size / 8); |
a3ad94ed ILT |
955 | unsigned char* phash = new unsigned char[hashlen]; |
956 | ||
8d9743bd MK |
957 | bool big_endian = parameters->target().is_big_endian(); |
958 | if (size == 32) | |
9025d29d | 959 | { |
8d9743bd MK |
960 | if (big_endian) |
961 | { | |
9025d29d | 962 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) |
8d9743bd MK |
963 | Dynobj::sized_create_elf_hash_table<32, true>(bucket, chain, phash, |
964 | hashlen); | |
9025d29d | 965 | #else |
8d9743bd | 966 | gold_unreachable(); |
9025d29d | 967 | #endif |
8d9743bd MK |
968 | } |
969 | else | |
970 | { | |
971 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
972 | Dynobj::sized_create_elf_hash_table<32, false>(bucket, chain, phash, | |
973 | hashlen); | |
974 | #else | |
975 | gold_unreachable(); | |
976 | #endif | |
977 | } | |
9025d29d | 978 | } |
8d9743bd | 979 | else if (size == 64) |
9025d29d | 980 | { |
8d9743bd MK |
981 | if (big_endian) |
982 | { | |
983 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
984 | Dynobj::sized_create_elf_hash_table<64, true>(bucket, chain, phash, | |
985 | hashlen); | |
986 | #else | |
987 | gold_unreachable(); | |
988 | #endif | |
989 | } | |
990 | else | |
991 | { | |
9025d29d | 992 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) |
8d9743bd MK |
993 | Dynobj::sized_create_elf_hash_table<64, false>(bucket, chain, phash, |
994 | hashlen); | |
9025d29d | 995 | #else |
8d9743bd | 996 | gold_unreachable(); |
9025d29d | 997 | #endif |
8d9743bd | 998 | } |
9025d29d | 999 | } |
8d9743bd MK |
1000 | else |
1001 | gold_unreachable(); | |
a3ad94ed ILT |
1002 | |
1003 | *pphash = phash; | |
1004 | *phashlen = hashlen; | |
1005 | } | |
1006 | ||
1007 | // Fill in an ELF hash table. | |
1008 | ||
8d9743bd | 1009 | template<int size, bool big_endian> |
a3ad94ed ILT |
1010 | void |
1011 | Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket, | |
1012 | const std::vector<uint32_t>& chain, | |
1013 | unsigned char* phash, | |
1014 | unsigned int hashlen) | |
1015 | { | |
1016 | unsigned char* p = phash; | |
1017 | ||
1018 | const unsigned int bucketcount = bucket.size(); | |
1019 | const unsigned int chaincount = chain.size(); | |
1020 | ||
8d9743bd MK |
1021 | elfcpp::Swap<size, big_endian>::writeval(p, bucketcount); |
1022 | p += size / 8; | |
1023 | elfcpp::Swap<size, big_endian>::writeval(p, chaincount); | |
1024 | p += size / 8; | |
a3ad94ed ILT |
1025 | |
1026 | for (unsigned int i = 0; i < bucketcount; ++i) | |
1027 | { | |
8d9743bd MK |
1028 | elfcpp::Swap<size, big_endian>::writeval(p, bucket[i]); |
1029 | p += size / 8; | |
a3ad94ed ILT |
1030 | } |
1031 | ||
1032 | for (unsigned int i = 0; i < chaincount; ++i) | |
1033 | { | |
8d9743bd MK |
1034 | elfcpp::Swap<size, big_endian>::writeval(p, chain[i]); |
1035 | p += size / 8; | |
a3ad94ed ILT |
1036 | } |
1037 | ||
1038 | gold_assert(static_cast<unsigned int>(p - phash) == hashlen); | |
1039 | } | |
1040 | ||
1041 | // The hash function used for the GNU hash table. This hash function | |
1042 | // must not change, as the dynamic linker uses it also. | |
1043 | ||
1044 | uint32_t | |
1045 | Dynobj::gnu_hash(const char* name) | |
1046 | { | |
1047 | const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name); | |
1048 | uint32_t h = 5381; | |
1049 | unsigned char c; | |
1050 | while ((c = *nameu++) != '\0') | |
1051 | h = (h << 5) + h + c; | |
1052 | return h; | |
1053 | } | |
1054 | ||
1055 | // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash | |
1056 | // tables are an extension to ELF which are recognized by the GNU | |
1057 | // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH. | |
1058 | // TARGET is the target. DYNSYMS is a vector with all the global | |
1059 | // symbols which will be going into the dynamic symbol table. | |
1060 | // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic | |
1061 | // symbol table. | |
1062 | ||
1063 | void | |
9025d29d | 1064 | Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms, |
a3ad94ed ILT |
1065 | unsigned int local_dynsym_count, |
1066 | unsigned char** pphash, | |
1067 | unsigned int* phashlen) | |
1068 | { | |
1069 | const unsigned int count = dynsyms.size(); | |
1070 | ||
1071 | // Sort the dynamic symbols into two vectors. Symbols which we do | |
1072 | // not want to put into the hash table we store into | |
1073 | // UNHASHED_DYNSYMS. Symbols which we do want to store we put into | |
1074 | // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS, | |
1075 | // and records the hash codes. | |
1076 | ||
1077 | std::vector<Symbol*> unhashed_dynsyms; | |
1078 | unhashed_dynsyms.reserve(count); | |
1079 | ||
1080 | std::vector<Symbol*> hashed_dynsyms; | |
1081 | hashed_dynsyms.reserve(count); | |
1082 | ||
1083 | std::vector<uint32_t> dynsym_hashvals; | |
1084 | dynsym_hashvals.reserve(count); | |
1085 | ||
1086 | for (unsigned int i = 0; i < count; ++i) | |
1087 | { | |
1088 | Symbol* sym = dynsyms[i]; | |
1089 | ||
176fe33f ILT |
1090 | if (!sym->needs_dynsym_value() |
1091 | && (sym->is_undefined() | |
1092 | || sym->is_from_dynobj() | |
1093 | || sym->is_forced_local())) | |
a3ad94ed ILT |
1094 | unhashed_dynsyms.push_back(sym); |
1095 | else | |
1096 | { | |
1097 | hashed_dynsyms.push_back(sym); | |
1098 | dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name())); | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | // Put the unhashed symbols at the start of the global portion of | |
1103 | // the dynamic symbol table. | |
1104 | const unsigned int unhashed_count = unhashed_dynsyms.size(); | |
1105 | unsigned int unhashed_dynsym_index = local_dynsym_count; | |
1106 | for (unsigned int i = 0; i < unhashed_count; ++i) | |
1107 | { | |
1108 | unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index); | |
1109 | ++unhashed_dynsym_index; | |
1110 | } | |
1111 | ||
1112 | // For the actual data generation we call out to a templatized | |
1113 | // function. | |
8851ecca ILT |
1114 | int size = parameters->target().get_size(); |
1115 | bool big_endian = parameters->target().is_big_endian(); | |
a3ad94ed ILT |
1116 | if (size == 32) |
1117 | { | |
1118 | if (big_endian) | |
9025d29d ILT |
1119 | { |
1120 | #ifdef HAVE_TARGET_32_BIG | |
1121 | Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms, | |
1122 | dynsym_hashvals, | |
1123 | unhashed_dynsym_index, | |
1124 | pphash, | |
1125 | phashlen); | |
1126 | #else | |
1127 | gold_unreachable(); | |
1128 | #endif | |
1129 | } | |
a3ad94ed | 1130 | else |
9025d29d ILT |
1131 | { |
1132 | #ifdef HAVE_TARGET_32_LITTLE | |
1133 | Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms, | |
1134 | dynsym_hashvals, | |
1135 | unhashed_dynsym_index, | |
1136 | pphash, | |
1137 | phashlen); | |
1138 | #else | |
1139 | gold_unreachable(); | |
1140 | #endif | |
1141 | } | |
a3ad94ed ILT |
1142 | } |
1143 | else if (size == 64) | |
1144 | { | |
1145 | if (big_endian) | |
9025d29d ILT |
1146 | { |
1147 | #ifdef HAVE_TARGET_64_BIG | |
1148 | Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms, | |
1149 | dynsym_hashvals, | |
1150 | unhashed_dynsym_index, | |
1151 | pphash, | |
1152 | phashlen); | |
1153 | #else | |
1154 | gold_unreachable(); | |
1155 | #endif | |
1156 | } | |
a3ad94ed | 1157 | else |
9025d29d ILT |
1158 | { |
1159 | #ifdef HAVE_TARGET_64_LITTLE | |
1160 | Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms, | |
1161 | dynsym_hashvals, | |
1162 | unhashed_dynsym_index, | |
1163 | pphash, | |
1164 | phashlen); | |
1165 | #else | |
1166 | gold_unreachable(); | |
1167 | #endif | |
1168 | } | |
a3ad94ed ILT |
1169 | } |
1170 | else | |
1171 | gold_unreachable(); | |
1172 | } | |
1173 | ||
1174 | // Create the actual data for a GNU hash table. This is just a copy | |
1175 | // of the code from the old GNU linker. | |
1176 | ||
1177 | template<int size, bool big_endian> | |
1178 | void | |
1179 | Dynobj::sized_create_gnu_hash_table( | |
1180 | const std::vector<Symbol*>& hashed_dynsyms, | |
1181 | const std::vector<uint32_t>& dynsym_hashvals, | |
1182 | unsigned int unhashed_dynsym_count, | |
1183 | unsigned char** pphash, | |
1184 | unsigned int* phashlen) | |
1185 | { | |
1186 | if (hashed_dynsyms.empty()) | |
1187 | { | |
1188 | // Special case for the empty hash table. | |
1189 | unsigned int hashlen = 5 * 4 + size / 8; | |
1190 | unsigned char* phash = new unsigned char[hashlen]; | |
1191 | // One empty bucket. | |
1192 | elfcpp::Swap<32, big_endian>::writeval(phash, 1); | |
1193 | // Symbol index above unhashed symbols. | |
1194 | elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count); | |
1195 | // One word for bitmask. | |
1196 | elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1); | |
1197 | // Only bloom filter. | |
1198 | elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0); | |
1199 | // No valid hashes. | |
1200 | elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0); | |
1201 | // No hashes in only bucket. | |
1202 | elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0); | |
1203 | ||
1204 | *phashlen = hashlen; | |
1205 | *pphash = phash; | |
1206 | ||
1207 | return; | |
1208 | } | |
1209 | ||
1210 | const unsigned int bucketcount = | |
1211 | Dynobj::compute_bucket_count(dynsym_hashvals, true); | |
1212 | ||
1213 | const unsigned int nsyms = hashed_dynsyms.size(); | |
1214 | ||
1215 | uint32_t maskbitslog2 = 1; | |
1216 | uint32_t x = nsyms >> 1; | |
1217 | while (x != 0) | |
1218 | { | |
1219 | ++maskbitslog2; | |
1220 | x >>= 1; | |
1221 | } | |
1222 | if (maskbitslog2 < 3) | |
1223 | maskbitslog2 = 5; | |
1224 | else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0) | |
1225 | maskbitslog2 += 3; | |
1226 | else | |
1227 | maskbitslog2 += 2; | |
1228 | ||
1229 | uint32_t shift1; | |
1230 | if (size == 32) | |
1231 | shift1 = 5; | |
1232 | else | |
1233 | { | |
1234 | if (maskbitslog2 == 5) | |
1235 | maskbitslog2 = 6; | |
1236 | shift1 = 6; | |
1237 | } | |
1238 | uint32_t mask = (1U << shift1) - 1U; | |
1239 | uint32_t shift2 = maskbitslog2; | |
1240 | uint32_t maskbits = 1U << maskbitslog2; | |
1241 | uint32_t maskwords = 1U << (maskbitslog2 - shift1); | |
1242 | ||
1243 | typedef typename elfcpp::Elf_types<size>::Elf_WXword Word; | |
1244 | std::vector<Word> bitmask(maskwords); | |
1245 | std::vector<uint32_t> counts(bucketcount); | |
1246 | std::vector<uint32_t> indx(bucketcount); | |
1247 | uint32_t symindx = unhashed_dynsym_count; | |
1248 | ||
1249 | // Count the number of times each hash bucket is used. | |
1250 | for (unsigned int i = 0; i < nsyms; ++i) | |
1251 | ++counts[dynsym_hashvals[i] % bucketcount]; | |
1252 | ||
1253 | unsigned int cnt = symindx; | |
1254 | for (unsigned int i = 0; i < bucketcount; ++i) | |
1255 | { | |
1256 | indx[i] = cnt; | |
1257 | cnt += counts[i]; | |
1258 | } | |
1259 | ||
1260 | unsigned int hashlen = (4 + bucketcount + nsyms) * 4; | |
1261 | hashlen += maskbits / 8; | |
1262 | unsigned char* phash = new unsigned char[hashlen]; | |
1263 | ||
1264 | elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount); | |
1265 | elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx); | |
1266 | elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords); | |
1267 | elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2); | |
1268 | ||
1269 | unsigned char* p = phash + 16 + maskbits / 8; | |
1270 | for (unsigned int i = 0; i < bucketcount; ++i) | |
1271 | { | |
1272 | if (counts[i] == 0) | |
1273 | elfcpp::Swap<32, big_endian>::writeval(p, 0); | |
1274 | else | |
1275 | elfcpp::Swap<32, big_endian>::writeval(p, indx[i]); | |
1276 | p += 4; | |
1277 | } | |
1278 | ||
1279 | for (unsigned int i = 0; i < nsyms; ++i) | |
1280 | { | |
1281 | Symbol* sym = hashed_dynsyms[i]; | |
1282 | uint32_t hashval = dynsym_hashvals[i]; | |
1283 | ||
1284 | unsigned int bucket = hashval % bucketcount; | |
1285 | unsigned int val = ((hashval >> shift1) | |
1286 | & ((maskbits >> shift1) - 1)); | |
1287 | bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask); | |
1288 | bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask); | |
1289 | val = hashval & ~ 1U; | |
1290 | if (counts[bucket] == 1) | |
1291 | { | |
1292 | // Last element terminates the chain. | |
1293 | val |= 1; | |
1294 | } | |
1295 | elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4, | |
1296 | val); | |
1297 | --counts[bucket]; | |
1298 | ||
1299 | sym->set_dynsym_index(indx[bucket]); | |
1300 | ++indx[bucket]; | |
1301 | } | |
1302 | ||
1303 | p = phash + 16; | |
1304 | for (unsigned int i = 0; i < maskwords; ++i) | |
1305 | { | |
1306 | elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]); | |
1307 | p += size / 8; | |
1308 | } | |
1309 | ||
1310 | *phashlen = hashlen; | |
1311 | *pphash = phash; | |
1312 | } | |
1313 | ||
14b31740 ILT |
1314 | // Verdef methods. |
1315 | ||
1316 | // Write this definition to a buffer for the output section. | |
1317 | ||
1318 | template<int size, bool big_endian> | |
1319 | unsigned char* | |
7d1a9ebb | 1320 | Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const |
14b31740 ILT |
1321 | { |
1322 | const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size; | |
1323 | const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size; | |
1324 | ||
1325 | elfcpp::Verdef_write<size, big_endian> vd(pb); | |
1326 | vd.set_vd_version(elfcpp::VER_DEF_CURRENT); | |
1327 | vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0) | |
44ec90b9 RO |
1328 | | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0) |
1329 | | (this->is_info_ ? elfcpp::VER_FLG_INFO : 0)); | |
14b31740 ILT |
1330 | vd.set_vd_ndx(this->index()); |
1331 | vd.set_vd_cnt(1 + this->deps_.size()); | |
1332 | vd.set_vd_hash(Dynobj::elf_hash(this->name())); | |
1333 | vd.set_vd_aux(verdef_size); | |
1334 | vd.set_vd_next(is_last | |
1335 | ? 0 | |
1336 | : verdef_size + (1 + this->deps_.size()) * verdaux_size); | |
1337 | pb += verdef_size; | |
1338 | ||
1339 | elfcpp::Verdaux_write<size, big_endian> vda(pb); | |
1340 | vda.set_vda_name(dynpool->get_offset(this->name())); | |
1341 | vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size); | |
1342 | pb += verdaux_size; | |
1343 | ||
1344 | Deps::const_iterator p; | |
1345 | unsigned int i; | |
1346 | for (p = this->deps_.begin(), i = 0; | |
1347 | p != this->deps_.end(); | |
1348 | ++p, ++i) | |
1349 | { | |
2ea97941 ILT |
1350 | elfcpp::Verdaux_write<size, big_endian> vda(pb); |
1351 | vda.set_vda_name(dynpool->get_offset(*p)); | |
1352 | vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size); | |
14b31740 ILT |
1353 | pb += verdaux_size; |
1354 | } | |
1355 | ||
1356 | return pb; | |
1357 | } | |
1358 | ||
1359 | // Verneed methods. | |
1360 | ||
1361 | Verneed::~Verneed() | |
1362 | { | |
1363 | for (Need_versions::iterator p = this->need_versions_.begin(); | |
1364 | p != this->need_versions_.end(); | |
1365 | ++p) | |
1366 | delete *p; | |
1367 | } | |
1368 | ||
1369 | // Add a new version to this file reference. | |
1370 | ||
1371 | Verneed_version* | |
1372 | Verneed::add_name(const char* name) | |
1373 | { | |
1374 | Verneed_version* vv = new Verneed_version(name); | |
1375 | this->need_versions_.push_back(vv); | |
1376 | return vv; | |
1377 | } | |
1378 | ||
1379 | // Set the version indexes starting at INDEX. | |
1380 | ||
1381 | unsigned int | |
1382 | Verneed::finalize(unsigned int index) | |
1383 | { | |
1384 | for (Need_versions::iterator p = this->need_versions_.begin(); | |
1385 | p != this->need_versions_.end(); | |
1386 | ++p) | |
1387 | { | |
1388 | (*p)->set_index(index); | |
1389 | ++index; | |
1390 | } | |
1391 | return index; | |
1392 | } | |
1393 | ||
1394 | // Write this list of referenced versions to a buffer for the output | |
1395 | // section. | |
1396 | ||
1397 | template<int size, bool big_endian> | |
1398 | unsigned char* | |
1399 | Verneed::write(const Stringpool* dynpool, bool is_last, | |
7d1a9ebb | 1400 | unsigned char* pb) const |
14b31740 ILT |
1401 | { |
1402 | const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size; | |
1403 | const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size; | |
1404 | ||
1405 | elfcpp::Verneed_write<size, big_endian> vn(pb); | |
1406 | vn.set_vn_version(elfcpp::VER_NEED_CURRENT); | |
1407 | vn.set_vn_cnt(this->need_versions_.size()); | |
1408 | vn.set_vn_file(dynpool->get_offset(this->filename())); | |
1409 | vn.set_vn_aux(verneed_size); | |
1410 | vn.set_vn_next(is_last | |
1411 | ? 0 | |
1412 | : verneed_size + this->need_versions_.size() * vernaux_size); | |
1413 | pb += verneed_size; | |
1414 | ||
1415 | Need_versions::const_iterator p; | |
1416 | unsigned int i; | |
1417 | for (p = this->need_versions_.begin(), i = 0; | |
1418 | p != this->need_versions_.end(); | |
1419 | ++p, ++i) | |
1420 | { | |
1421 | elfcpp::Vernaux_write<size, big_endian> vna(pb); | |
1422 | vna.set_vna_hash(Dynobj::elf_hash((*p)->version())); | |
1423 | // FIXME: We need to sometimes set VER_FLG_WEAK here. | |
1424 | vna.set_vna_flags(0); | |
1425 | vna.set_vna_other((*p)->index()); | |
1426 | vna.set_vna_name(dynpool->get_offset((*p)->version())); | |
1427 | vna.set_vna_next(i + 1 >= this->need_versions_.size() | |
1428 | ? 0 | |
1429 | : vernaux_size); | |
1430 | pb += vernaux_size; | |
1431 | } | |
1432 | ||
1433 | return pb; | |
1434 | } | |
1435 | ||
1436 | // Versions methods. | |
1437 | ||
2ea97941 | 1438 | Versions::Versions(const Version_script_info& version_script, |
a5dc0706 | 1439 | Stringpool* dynpool) |
09124467 | 1440 | : defs_(), needs_(), version_table_(), |
2ea97941 | 1441 | is_finalized_(false), version_script_(version_script), |
ebb13322 | 1442 | needs_base_version_(true) |
09124467 | 1443 | { |
09124467 ILT |
1444 | if (!this->version_script_.empty()) |
1445 | { | |
1446 | // Parse the version script, and insert each declared version into | |
1447 | // defs_ and version_table_. | |
1448 | std::vector<std::string> versions = this->version_script_.get_versions(); | |
c5617f2f DK |
1449 | |
1450 | if (this->needs_base_version_ && !versions.empty()) | |
1451 | this->define_base_version(dynpool); | |
1452 | ||
09124467 ILT |
1453 | for (size_t k = 0; k < versions.size(); ++k) |
1454 | { | |
1455 | Stringpool::Key version_key; | |
1456 | const char* version = dynpool->add(versions[k].c_str(), | |
1457 | true, &version_key); | |
1458 | Verdef* const vd = new Verdef( | |
1459 | version, | |
a5dc0706 | 1460 | this->version_script_.get_dependencies(version), |
44ec90b9 | 1461 | false, false, false, false); |
09124467 ILT |
1462 | this->defs_.push_back(vd); |
1463 | Key key(version_key, 0); | |
1464 | this->version_table_.insert(std::make_pair(key, vd)); | |
1465 | } | |
1466 | } | |
1467 | } | |
1468 | ||
14b31740 ILT |
1469 | Versions::~Versions() |
1470 | { | |
1471 | for (Defs::iterator p = this->defs_.begin(); | |
1472 | p != this->defs_.end(); | |
1473 | ++p) | |
1474 | delete *p; | |
1475 | ||
1476 | for (Needs::iterator p = this->needs_.begin(); | |
1477 | p != this->needs_.end(); | |
1478 | ++p) | |
1479 | delete *p; | |
1480 | } | |
1481 | ||
c5617f2f DK |
1482 | // Define the base version of a shared library. The base version definition |
1483 | // must be the first entry in defs_. We insert it lazily so that defs_ is | |
1484 | // empty if no symbol versioning is used. Then layout can just drop the | |
1485 | // version sections. | |
1486 | ||
1487 | void | |
1488 | Versions::define_base_version(Stringpool* dynpool) | |
1489 | { | |
1490 | // If we do any versioning at all, we always need a base version, so | |
1491 | // define that first. Nothing explicitly declares itself as part of base, | |
1492 | // so it doesn't need to be in version_table_. | |
1493 | gold_assert(this->defs_.empty()); | |
1494 | const char* name = parameters->options().soname(); | |
1495 | if (name == NULL) | |
1496 | name = parameters->options().output_file_name(); | |
1497 | name = dynpool->add(name, false, NULL); | |
1498 | Verdef* vdbase = new Verdef(name, std::vector<std::string>(), | |
44ec90b9 | 1499 | true, false, false, true); |
c5617f2f DK |
1500 | this->defs_.push_back(vdbase); |
1501 | this->needs_base_version_ = false; | |
1502 | } | |
1503 | ||
46fe1623 ILT |
1504 | // Return the dynamic object which a symbol refers to. |
1505 | ||
1506 | Dynobj* | |
1507 | Versions::get_dynobj_for_sym(const Symbol_table* symtab, | |
1508 | const Symbol* sym) const | |
1509 | { | |
1510 | if (sym->is_copied_from_dynobj()) | |
1511 | return symtab->get_copy_source(sym); | |
1512 | else | |
1513 | { | |
1514 | Object* object = sym->object(); | |
1515 | gold_assert(object->is_dynamic()); | |
1516 | return static_cast<Dynobj*>(object); | |
1517 | } | |
1518 | } | |
1519 | ||
14b31740 ILT |
1520 | // Record version information for a symbol going into the dynamic |
1521 | // symbol table. | |
1522 | ||
1523 | void | |
35cdfc9a | 1524 | Versions::record_version(const Symbol_table* symtab, |
14b31740 ILT |
1525 | Stringpool* dynpool, const Symbol* sym) |
1526 | { | |
1527 | gold_assert(!this->is_finalized_); | |
1528 | gold_assert(sym->version() != NULL); | |
8851ecca | 1529 | |
b45e00b3 CC |
1530 | // A symbol defined as "sym@" is bound to an unspecified base version. |
1531 | if (sym->version()[0] == '\0') | |
1532 | return; | |
1533 | ||
14b31740 | 1534 | Stringpool::Key version_key; |
cfd73a4e | 1535 | const char* version = dynpool->add(sym->version(), false, &version_key); |
14b31740 | 1536 | |
46fe1623 | 1537 | if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj()) |
92f0e169 | 1538 | { |
ebb13322 | 1539 | this->add_def(dynpool, sym, version, version_key); |
92f0e169 | 1540 | } |
14b31740 ILT |
1541 | else |
1542 | { | |
1543 | // This is a version reference. | |
46fe1623 | 1544 | Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym); |
14b31740 ILT |
1545 | this->add_need(dynpool, dynobj->soname(), version, version_key); |
1546 | } | |
1547 | } | |
1548 | ||
1549 | // We've found a symbol SYM defined in version VERSION. | |
1550 | ||
1551 | void | |
21131061 | 1552 | Versions::add_def(Stringpool* dynpool, const Symbol* sym, const char* version, |
35cdfc9a | 1553 | Stringpool::Key version_key) |
14b31740 ILT |
1554 | { |
1555 | Key k(version_key, 0); | |
1556 | Version_base* const vbnull = NULL; | |
1557 | std::pair<Version_table::iterator, bool> ins = | |
1558 | this->version_table_.insert(std::make_pair(k, vbnull)); | |
55a93433 | 1559 | |
14b31740 ILT |
1560 | if (!ins.second) |
1561 | { | |
1562 | // We already have an entry for this version. | |
1563 | Version_base* vb = ins.first->second; | |
1564 | ||
1565 | // We have now seen a symbol in this version, so it is not | |
1566 | // weak. | |
99f8faca | 1567 | gold_assert(vb != NULL); |
14b31740 | 1568 | vb->clear_weak(); |
14b31740 ILT |
1569 | } |
1570 | else | |
1571 | { | |
1572 | // If we are creating a shared object, it is an error to | |
1573 | // find a definition of a symbol with a version which is not | |
1574 | // in the version script. | |
8851ecca | 1575 | if (parameters->options().shared()) |
ebb13322 CC |
1576 | gold_error(_("symbol %s has undefined version %s"), |
1577 | sym->demangled_name().c_str(), version); | |
1578 | ||
14b31740 ILT |
1579 | // When creating a regular executable, automatically define |
1580 | // a new version. | |
ebb13322 CC |
1581 | if (this->needs_base_version_) |
1582 | this->define_base_version(dynpool); | |
09124467 | 1583 | Verdef* vd = new Verdef(version, std::vector<std::string>(), |
44ec90b9 | 1584 | false, false, false, false); |
14b31740 ILT |
1585 | this->defs_.push_back(vd); |
1586 | ins.first->second = vd; | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | // Add a reference to version NAME in file FILENAME. | |
1591 | ||
1592 | void | |
1593 | Versions::add_need(Stringpool* dynpool, const char* filename, const char* name, | |
1594 | Stringpool::Key name_key) | |
1595 | { | |
1596 | Stringpool::Key filename_key; | |
cfd73a4e | 1597 | filename = dynpool->add(filename, true, &filename_key); |
14b31740 ILT |
1598 | |
1599 | Key k(name_key, filename_key); | |
1600 | Version_base* const vbnull = NULL; | |
1601 | std::pair<Version_table::iterator, bool> ins = | |
1602 | this->version_table_.insert(std::make_pair(k, vbnull)); | |
1603 | ||
1604 | if (!ins.second) | |
1605 | { | |
1606 | // We already have an entry for this filename/version. | |
1607 | return; | |
1608 | } | |
1609 | ||
1610 | // See whether we already have this filename. We don't expect many | |
1611 | // version references, so we just do a linear search. This could be | |
1612 | // replaced by a hash table. | |
1613 | Verneed* vn = NULL; | |
1614 | for (Needs::iterator p = this->needs_.begin(); | |
1615 | p != this->needs_.end(); | |
1616 | ++p) | |
1617 | { | |
1618 | if ((*p)->filename() == filename) | |
1619 | { | |
1620 | vn = *p; | |
1621 | break; | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | if (vn == NULL) | |
1626 | { | |
c5617f2f | 1627 | // Create base version definition lazily for shared library. |
ebb13322 | 1628 | if (parameters->options().shared() && this->needs_base_version_) |
c5617f2f DK |
1629 | this->define_base_version(dynpool); |
1630 | ||
14b31740 ILT |
1631 | // We have a new filename. |
1632 | vn = new Verneed(filename); | |
1633 | this->needs_.push_back(vn); | |
1634 | } | |
1635 | ||
1636 | ins.first->second = vn->add_name(name); | |
1637 | } | |
1638 | ||
1639 | // Set the version indexes. Create a new dynamic version symbol for | |
1640 | // each new version definition. | |
1641 | ||
1642 | unsigned int | |
9b07f471 ILT |
1643 | Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index, |
1644 | std::vector<Symbol*>* syms) | |
14b31740 ILT |
1645 | { |
1646 | gold_assert(!this->is_finalized_); | |
1647 | ||
1648 | unsigned int vi = 1; | |
1649 | ||
1650 | for (Defs::iterator p = this->defs_.begin(); | |
1651 | p != this->defs_.end(); | |
1652 | ++p) | |
1653 | { | |
1654 | (*p)->set_index(vi); | |
1655 | ++vi; | |
1656 | ||
1657 | // Create a version symbol if necessary. | |
1658 | if (!(*p)->is_symbol_created()) | |
1659 | { | |
9b07f471 | 1660 | Symbol* vsym = symtab->define_as_constant((*p)->name(), |
99fff23b ILT |
1661 | (*p)->name(), |
1662 | Symbol_table::PREDEFINED, | |
1663 | 0, 0, | |
008db82e ILT |
1664 | elfcpp::STT_OBJECT, |
1665 | elfcpp::STB_GLOBAL, | |
1666 | elfcpp::STV_DEFAULT, 0, | |
caa9d5d9 | 1667 | false, false); |
14b31740 | 1668 | vsym->set_needs_dynsym_entry(); |
92f0e169 | 1669 | vsym->set_dynsym_index(dynsym_index); |
98e090bd | 1670 | vsym->set_is_default(); |
14b31740 ILT |
1671 | ++dynsym_index; |
1672 | syms->push_back(vsym); | |
1673 | // The name is already in the dynamic pool. | |
1674 | } | |
1675 | } | |
1676 | ||
1677 | // Index 1 is used for global symbols. | |
1678 | if (vi == 1) | |
1679 | { | |
1680 | gold_assert(this->defs_.empty()); | |
1681 | vi = 2; | |
1682 | } | |
1683 | ||
1684 | for (Needs::iterator p = this->needs_.begin(); | |
1685 | p != this->needs_.end(); | |
1686 | ++p) | |
1687 | vi = (*p)->finalize(vi); | |
1688 | ||
1689 | this->is_finalized_ = true; | |
1690 | ||
1691 | return dynsym_index; | |
1692 | } | |
1693 | ||
1694 | // Return the version index to use for a symbol. This does two hash | |
1695 | // table lookups: one in DYNPOOL and one in this->version_table_. | |
1696 | // Another approach alternative would be store a pointer in SYM, which | |
1697 | // would increase the size of the symbol table. Or perhaps we could | |
1698 | // use a hash table from dynamic symbol pointer values to Version_base | |
1699 | // pointers. | |
1700 | ||
1701 | unsigned int | |
46fe1623 ILT |
1702 | Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool, |
1703 | const Symbol* sym) const | |
14b31740 ILT |
1704 | { |
1705 | Stringpool::Key version_key; | |
1706 | const char* version = dynpool->find(sym->version(), &version_key); | |
1707 | gold_assert(version != NULL); | |
1708 | ||
91da9340 | 1709 | Key k; |
46fe1623 | 1710 | if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj()) |
31365f57 | 1711 | { |
31365f57 ILT |
1712 | k = Key(version_key, 0); |
1713 | } | |
14b31740 ILT |
1714 | else |
1715 | { | |
46fe1623 | 1716 | Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym); |
14b31740 ILT |
1717 | |
1718 | Stringpool::Key filename_key; | |
1719 | const char* filename = dynpool->find(dynobj->soname(), &filename_key); | |
1720 | gold_assert(filename != NULL); | |
1721 | ||
91da9340 | 1722 | k = Key(version_key, filename_key); |
14b31740 ILT |
1723 | } |
1724 | ||
91da9340 | 1725 | Version_table::const_iterator p = this->version_table_.find(k); |
14b31740 ILT |
1726 | gold_assert(p != this->version_table_.end()); |
1727 | ||
1728 | return p->second->index(); | |
1729 | } | |
1730 | ||
1731 | // Return an allocated buffer holding the contents of the symbol | |
1732 | // version section. | |
1733 | ||
1734 | template<int size, bool big_endian> | |
1735 | void | |
46fe1623 ILT |
1736 | Versions::symbol_section_contents(const Symbol_table* symtab, |
1737 | const Stringpool* dynpool, | |
14b31740 ILT |
1738 | unsigned int local_symcount, |
1739 | const std::vector<Symbol*>& syms, | |
1740 | unsigned char** pp, | |
7d1a9ebb | 1741 | unsigned int* psize) const |
14b31740 ILT |
1742 | { |
1743 | gold_assert(this->is_finalized_); | |
1744 | ||
1745 | unsigned int sz = (local_symcount + syms.size()) * 2; | |
1746 | unsigned char* pbuf = new unsigned char[sz]; | |
1747 | ||
1748 | for (unsigned int i = 0; i < local_symcount; ++i) | |
1749 | elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2, | |
1750 | elfcpp::VER_NDX_LOCAL); | |
1751 | ||
1752 | for (std::vector<Symbol*>::const_iterator p = syms.begin(); | |
1753 | p != syms.end(); | |
1754 | ++p) | |
1755 | { | |
2ea97941 | 1756 | unsigned int version_index; |
14b31740 | 1757 | const char* version = (*p)->version(); |
b45e00b3 | 1758 | if (version == NULL) |
98e090bd ILT |
1759 | { |
1760 | if ((*p)->is_defined() && !(*p)->is_from_dynobj()) | |
1761 | version_index = elfcpp::VER_NDX_GLOBAL; | |
1762 | else | |
1763 | version_index = elfcpp::VER_NDX_LOCAL; | |
1764 | } | |
b45e00b3 CC |
1765 | else if (version[0] == '\0') |
1766 | version_index = elfcpp::VER_NDX_GLOBAL; | |
1767 | else | |
1768 | version_index = this->version_index(symtab, dynpool, *p); | |
09124467 ILT |
1769 | // If the symbol was defined as foo@V1 instead of foo@@V1, add |
1770 | // the hidden bit. | |
890d1555 CC |
1771 | if ((*p)->version() != NULL |
1772 | && (*p)->is_defined() | |
1773 | && !(*p)->is_default() | |
1774 | && !(*p)->from_dyn()) | |
2ea97941 | 1775 | version_index |= elfcpp::VERSYM_HIDDEN; |
14b31740 | 1776 | elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2, |
2ea97941 | 1777 | version_index); |
14b31740 ILT |
1778 | } |
1779 | ||
1780 | *pp = pbuf; | |
1781 | *psize = sz; | |
1782 | } | |
1783 | ||
1784 | // Return an allocated buffer holding the contents of the version | |
1785 | // definition section. | |
1786 | ||
1787 | template<int size, bool big_endian> | |
1788 | void | |
1789 | Versions::def_section_contents(const Stringpool* dynpool, | |
1790 | unsigned char** pp, unsigned int* psize, | |
7d1a9ebb | 1791 | unsigned int* pentries) const |
14b31740 ILT |
1792 | { |
1793 | gold_assert(this->is_finalized_); | |
1794 | gold_assert(!this->defs_.empty()); | |
1795 | ||
1796 | const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size; | |
1797 | const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size; | |
1798 | ||
1799 | unsigned int sz = 0; | |
1800 | for (Defs::const_iterator p = this->defs_.begin(); | |
1801 | p != this->defs_.end(); | |
1802 | ++p) | |
1803 | { | |
1804 | sz += verdef_size + verdaux_size; | |
1805 | sz += (*p)->count_dependencies() * verdaux_size; | |
1806 | } | |
1807 | ||
1808 | unsigned char* pbuf = new unsigned char[sz]; | |
1809 | ||
1810 | unsigned char* pb = pbuf; | |
1811 | Defs::const_iterator p; | |
1812 | unsigned int i; | |
1813 | for (p = this->defs_.begin(), i = 0; | |
1814 | p != this->defs_.end(); | |
1815 | ++p, ++i) | |
7d1a9ebb ILT |
1816 | pb = (*p)->write<size, big_endian>(dynpool, |
1817 | i + 1 >= this->defs_.size(), | |
1818 | pb); | |
14b31740 ILT |
1819 | |
1820 | gold_assert(static_cast<unsigned int>(pb - pbuf) == sz); | |
1821 | ||
1822 | *pp = pbuf; | |
1823 | *psize = sz; | |
1824 | *pentries = this->defs_.size(); | |
1825 | } | |
1826 | ||
1827 | // Return an allocated buffer holding the contents of the version | |
1828 | // reference section. | |
1829 | ||
1830 | template<int size, bool big_endian> | |
1831 | void | |
1832 | Versions::need_section_contents(const Stringpool* dynpool, | |
ca09d69a NC |
1833 | unsigned char** pp, unsigned int* psize, |
1834 | unsigned int* pentries) const | |
14b31740 ILT |
1835 | { |
1836 | gold_assert(this->is_finalized_); | |
1837 | gold_assert(!this->needs_.empty()); | |
1838 | ||
1839 | const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size; | |
1840 | const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size; | |
1841 | ||
1842 | unsigned int sz = 0; | |
1843 | for (Needs::const_iterator p = this->needs_.begin(); | |
1844 | p != this->needs_.end(); | |
1845 | ++p) | |
1846 | { | |
1847 | sz += verneed_size; | |
1848 | sz += (*p)->count_versions() * vernaux_size; | |
1849 | } | |
1850 | ||
1851 | unsigned char* pbuf = new unsigned char[sz]; | |
1852 | ||
1853 | unsigned char* pb = pbuf; | |
1854 | Needs::const_iterator p; | |
1855 | unsigned int i; | |
1856 | for (p = this->needs_.begin(), i = 0; | |
1857 | p != this->needs_.end(); | |
1858 | ++p, ++i) | |
7d1a9ebb ILT |
1859 | pb = (*p)->write<size, big_endian>(dynpool, |
1860 | i + 1 >= this->needs_.size(), | |
1861 | pb); | |
14b31740 ILT |
1862 | |
1863 | gold_assert(static_cast<unsigned int>(pb - pbuf) == sz); | |
1864 | ||
1865 | *pp = pbuf; | |
1866 | *psize = sz; | |
1867 | *pentries = this->needs_.size(); | |
1868 | } | |
1869 | ||
dbe717ef ILT |
1870 | // Instantiate the templates we need. We could use the configure |
1871 | // script to restrict this to only the ones for implemented targets. | |
1872 | ||
193a53d9 | 1873 | #ifdef HAVE_TARGET_32_LITTLE |
dbe717ef ILT |
1874 | template |
1875 | class Sized_dynobj<32, false>; | |
193a53d9 | 1876 | #endif |
dbe717ef | 1877 | |
193a53d9 | 1878 | #ifdef HAVE_TARGET_32_BIG |
dbe717ef ILT |
1879 | template |
1880 | class Sized_dynobj<32, true>; | |
193a53d9 | 1881 | #endif |
dbe717ef | 1882 | |
193a53d9 | 1883 | #ifdef HAVE_TARGET_64_LITTLE |
dbe717ef ILT |
1884 | template |
1885 | class Sized_dynobj<64, false>; | |
193a53d9 | 1886 | #endif |
dbe717ef | 1887 | |
193a53d9 | 1888 | #ifdef HAVE_TARGET_64_BIG |
dbe717ef ILT |
1889 | template |
1890 | class Sized_dynobj<64, true>; | |
193a53d9 | 1891 | #endif |
dbe717ef | 1892 | |
193a53d9 | 1893 | #ifdef HAVE_TARGET_32_LITTLE |
14b31740 ILT |
1894 | template |
1895 | void | |
91da9340 | 1896 | Versions::symbol_section_contents<32, false>( |
46fe1623 | 1897 | const Symbol_table*, |
91da9340 ILT |
1898 | const Stringpool*, |
1899 | unsigned int, | |
1900 | const std::vector<Symbol*>&, | |
1901 | unsigned char**, | |
7d1a9ebb | 1902 | unsigned int*) const; |
193a53d9 | 1903 | #endif |
14b31740 | 1904 | |
193a53d9 | 1905 | #ifdef HAVE_TARGET_32_BIG |
14b31740 ILT |
1906 | template |
1907 | void | |
91da9340 | 1908 | Versions::symbol_section_contents<32, true>( |
46fe1623 | 1909 | const Symbol_table*, |
91da9340 ILT |
1910 | const Stringpool*, |
1911 | unsigned int, | |
1912 | const std::vector<Symbol*>&, | |
1913 | unsigned char**, | |
7d1a9ebb | 1914 | unsigned int*) const; |
193a53d9 | 1915 | #endif |
14b31740 | 1916 | |
193a53d9 | 1917 | #ifdef HAVE_TARGET_64_LITTLE |
14b31740 ILT |
1918 | template |
1919 | void | |
91da9340 | 1920 | Versions::symbol_section_contents<64, false>( |
46fe1623 | 1921 | const Symbol_table*, |
91da9340 ILT |
1922 | const Stringpool*, |
1923 | unsigned int, | |
1924 | const std::vector<Symbol*>&, | |
1925 | unsigned char**, | |
7d1a9ebb | 1926 | unsigned int*) const; |
193a53d9 | 1927 | #endif |
14b31740 | 1928 | |
193a53d9 | 1929 | #ifdef HAVE_TARGET_64_BIG |
14b31740 ILT |
1930 | template |
1931 | void | |
91da9340 | 1932 | Versions::symbol_section_contents<64, true>( |
46fe1623 | 1933 | const Symbol_table*, |
91da9340 ILT |
1934 | const Stringpool*, |
1935 | unsigned int, | |
1936 | const std::vector<Symbol*>&, | |
1937 | unsigned char**, | |
7d1a9ebb | 1938 | unsigned int*) const; |
193a53d9 | 1939 | #endif |
14b31740 | 1940 | |
193a53d9 | 1941 | #ifdef HAVE_TARGET_32_LITTLE |
14b31740 ILT |
1942 | template |
1943 | void | |
91da9340 ILT |
1944 | Versions::def_section_contents<32, false>( |
1945 | const Stringpool*, | |
1946 | unsigned char**, | |
1947 | unsigned int*, | |
7d1a9ebb | 1948 | unsigned int*) const; |
193a53d9 | 1949 | #endif |
14b31740 | 1950 | |
193a53d9 | 1951 | #ifdef HAVE_TARGET_32_BIG |
14b31740 ILT |
1952 | template |
1953 | void | |
91da9340 ILT |
1954 | Versions::def_section_contents<32, true>( |
1955 | const Stringpool*, | |
1956 | unsigned char**, | |
1957 | unsigned int*, | |
7d1a9ebb | 1958 | unsigned int*) const; |
193a53d9 | 1959 | #endif |
14b31740 | 1960 | |
193a53d9 | 1961 | #ifdef HAVE_TARGET_64_LITTLE |
14b31740 ILT |
1962 | template |
1963 | void | |
91da9340 ILT |
1964 | Versions::def_section_contents<64, false>( |
1965 | const Stringpool*, | |
1966 | unsigned char**, | |
1967 | unsigned int*, | |
7d1a9ebb | 1968 | unsigned int*) const; |
193a53d9 | 1969 | #endif |
14b31740 | 1970 | |
193a53d9 | 1971 | #ifdef HAVE_TARGET_64_BIG |
14b31740 ILT |
1972 | template |
1973 | void | |
91da9340 ILT |
1974 | Versions::def_section_contents<64, true>( |
1975 | const Stringpool*, | |
1976 | unsigned char**, | |
1977 | unsigned int*, | |
7d1a9ebb | 1978 | unsigned int*) const; |
193a53d9 | 1979 | #endif |
14b31740 | 1980 | |
193a53d9 | 1981 | #ifdef HAVE_TARGET_32_LITTLE |
14b31740 ILT |
1982 | template |
1983 | void | |
91da9340 ILT |
1984 | Versions::need_section_contents<32, false>( |
1985 | const Stringpool*, | |
1986 | unsigned char**, | |
1987 | unsigned int*, | |
7d1a9ebb | 1988 | unsigned int*) const; |
193a53d9 | 1989 | #endif |
14b31740 | 1990 | |
193a53d9 | 1991 | #ifdef HAVE_TARGET_32_BIG |
14b31740 ILT |
1992 | template |
1993 | void | |
91da9340 ILT |
1994 | Versions::need_section_contents<32, true>( |
1995 | const Stringpool*, | |
1996 | unsigned char**, | |
1997 | unsigned int*, | |
7d1a9ebb | 1998 | unsigned int*) const; |
193a53d9 | 1999 | #endif |
14b31740 | 2000 | |
193a53d9 | 2001 | #ifdef HAVE_TARGET_64_LITTLE |
14b31740 ILT |
2002 | template |
2003 | void | |
91da9340 ILT |
2004 | Versions::need_section_contents<64, false>( |
2005 | const Stringpool*, | |
2006 | unsigned char**, | |
2007 | unsigned int*, | |
7d1a9ebb | 2008 | unsigned int*) const; |
193a53d9 | 2009 | #endif |
14b31740 | 2010 | |
193a53d9 | 2011 | #ifdef HAVE_TARGET_64_BIG |
14b31740 ILT |
2012 | template |
2013 | void | |
91da9340 ILT |
2014 | Versions::need_section_contents<64, true>( |
2015 | const Stringpool*, | |
2016 | unsigned char**, | |
2017 | unsigned int*, | |
7d1a9ebb | 2018 | unsigned int*) const; |
193a53d9 | 2019 | #endif |
14b31740 | 2020 | |
dbe717ef | 2021 | } // End namespace gold. |