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