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