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