* configure.ac: If targ_extra_obj is set, link it in.
[deliverable/binutils-gdb.git] / gold / dynobj.cc
CommitLineData
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 <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
dbe717ef
ILT
23#include "gold.h"
24
25#include <vector>
26#include <cstring>
27
a3ad94ed 28#include "elfcpp.h"
7e1edb90 29#include "parameters.h"
14144f39 30#include "script.h"
dbe717ef
ILT
31#include "symtab.h"
32#include "dynobj.h"
33
34namespace 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
42Dynobj::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
68template<int size, bool big_endian>
69Sized_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
82template<int size, bool big_endian>
83void
84Sized_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
98template<int size, bool big_endian>
99void
100Sized_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
179template<int size, bool big_endian>
180void
181Sized_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
219template<int size, bool big_endian>
220void
e2827e5f
ILT
221Sized_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
307template<int size, bool big_endian>
308void
309Sized_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
402template<int size, bool big_endian>
403Xindex*
404Sized_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
416template<int size, bool big_endian>
417void
7e1edb90 418Sized_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
460template<int size, bool big_endian>
461void
462Sized_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
476template<int size, bool big_endian>
477void
14b31740 478Sized_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
555template<int size, bool big_endian>
556void
557Sized_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
634template<int size, bool big_endian>
635void
636Sized_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
653template<int size, bool big_endian>
654void
655Sized_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
716unsigned int
717Dynobj::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
755uint32_t
756Dynobj::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
782void
9025d29d 783Dynobj::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
841template<bool big_endian>
842void
843Dynobj::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
876uint32_t
877Dynobj::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
895void
9025d29d 896Dynobj::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
1008template<int size, bool big_endian>
1009void
1010Dynobj::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
1149template<int size, bool big_endian>
1150unsigned char*
7d1a9ebb 1151Verdef::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
1191Verneed::~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
1201Verneed_version*
1202Verneed::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
1211unsigned int
1212Verneed::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
1227template<int size, bool big_endian>
1228unsigned char*
1229Verneed::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
1268Versions::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
1309Versions::~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
1324Dynobj*
1325Versions::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
1341void
35cdfc9a 1342Versions::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
1366void
35cdfc9a
ILT
1367Versions::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())
14b31740 1391 {
75f2446e 1392 gold_error(_("symbol %s has undefined version %s"),
a2b1aa12 1393 sym->demangled_name().c_str(), version);
75f2446e 1394 return;
14b31740
ILT
1395 }
1396
14b31740
ILT
1397 // When creating a regular executable, automatically define
1398 // a new version.
09124467
ILT
1399 Verdef* vd = new Verdef(version, std::vector<std::string>(),
1400 false, false, false);
14b31740
ILT
1401 this->defs_.push_back(vd);
1402 ins.first->second = vd;
1403 }
1404}
1405
1406// Add a reference to version NAME in file FILENAME.
1407
1408void
1409Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1410 Stringpool::Key name_key)
1411{
1412 Stringpool::Key filename_key;
cfd73a4e 1413 filename = dynpool->add(filename, true, &filename_key);
14b31740
ILT
1414
1415 Key k(name_key, filename_key);
1416 Version_base* const vbnull = NULL;
1417 std::pair<Version_table::iterator, bool> ins =
1418 this->version_table_.insert(std::make_pair(k, vbnull));
1419
1420 if (!ins.second)
1421 {
1422 // We already have an entry for this filename/version.
1423 return;
1424 }
1425
1426 // See whether we already have this filename. We don't expect many
1427 // version references, so we just do a linear search. This could be
1428 // replaced by a hash table.
1429 Verneed* vn = NULL;
1430 for (Needs::iterator p = this->needs_.begin();
1431 p != this->needs_.end();
1432 ++p)
1433 {
1434 if ((*p)->filename() == filename)
1435 {
1436 vn = *p;
1437 break;
1438 }
1439 }
1440
1441 if (vn == NULL)
1442 {
1443 // We have a new filename.
1444 vn = new Verneed(filename);
1445 this->needs_.push_back(vn);
1446 }
1447
1448 ins.first->second = vn->add_name(name);
1449}
1450
1451// Set the version indexes. Create a new dynamic version symbol for
1452// each new version definition.
1453
1454unsigned int
9b07f471
ILT
1455Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
1456 std::vector<Symbol*>* syms)
14b31740
ILT
1457{
1458 gold_assert(!this->is_finalized_);
1459
1460 unsigned int vi = 1;
1461
1462 for (Defs::iterator p = this->defs_.begin();
1463 p != this->defs_.end();
1464 ++p)
1465 {
1466 (*p)->set_index(vi);
1467 ++vi;
1468
1469 // Create a version symbol if necessary.
1470 if (!(*p)->is_symbol_created())
1471 {
9b07f471 1472 Symbol* vsym = symtab->define_as_constant((*p)->name(),
008db82e
ILT
1473 (*p)->name(), 0, 0,
1474 elfcpp::STT_OBJECT,
1475 elfcpp::STB_GLOBAL,
1476 elfcpp::STV_DEFAULT, 0,
caa9d5d9 1477 false, false);
14b31740 1478 vsym->set_needs_dynsym_entry();
92f0e169 1479 vsym->set_dynsym_index(dynsym_index);
14b31740
ILT
1480 ++dynsym_index;
1481 syms->push_back(vsym);
1482 // The name is already in the dynamic pool.
1483 }
1484 }
1485
1486 // Index 1 is used for global symbols.
1487 if (vi == 1)
1488 {
1489 gold_assert(this->defs_.empty());
1490 vi = 2;
1491 }
1492
1493 for (Needs::iterator p = this->needs_.begin();
1494 p != this->needs_.end();
1495 ++p)
1496 vi = (*p)->finalize(vi);
1497
1498 this->is_finalized_ = true;
1499
1500 return dynsym_index;
1501}
1502
1503// Return the version index to use for a symbol. This does two hash
1504// table lookups: one in DYNPOOL and one in this->version_table_.
1505// Another approach alternative would be store a pointer in SYM, which
1506// would increase the size of the symbol table. Or perhaps we could
1507// use a hash table from dynamic symbol pointer values to Version_base
1508// pointers.
1509
1510unsigned int
46fe1623
ILT
1511Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1512 const Symbol* sym) const
14b31740
ILT
1513{
1514 Stringpool::Key version_key;
1515 const char* version = dynpool->find(sym->version(), &version_key);
1516 gold_assert(version != NULL);
1517
91da9340 1518 Key k;
46fe1623 1519 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
31365f57 1520 {
8851ecca 1521 if (!parameters->options().shared())
31365f57
ILT
1522 return elfcpp::VER_NDX_GLOBAL;
1523 k = Key(version_key, 0);
1524 }
14b31740
ILT
1525 else
1526 {
46fe1623 1527 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
14b31740
ILT
1528
1529 Stringpool::Key filename_key;
1530 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1531 gold_assert(filename != NULL);
1532
91da9340 1533 k = Key(version_key, filename_key);
14b31740
ILT
1534 }
1535
91da9340 1536 Version_table::const_iterator p = this->version_table_.find(k);
14b31740
ILT
1537 gold_assert(p != this->version_table_.end());
1538
1539 return p->second->index();
1540}
1541
1542// Return an allocated buffer holding the contents of the symbol
1543// version section.
1544
1545template<int size, bool big_endian>
1546void
46fe1623
ILT
1547Versions::symbol_section_contents(const Symbol_table* symtab,
1548 const Stringpool* dynpool,
14b31740
ILT
1549 unsigned int local_symcount,
1550 const std::vector<Symbol*>& syms,
1551 unsigned char** pp,
7d1a9ebb 1552 unsigned int* psize) const
14b31740
ILT
1553{
1554 gold_assert(this->is_finalized_);
1555
1556 unsigned int sz = (local_symcount + syms.size()) * 2;
1557 unsigned char* pbuf = new unsigned char[sz];
1558
1559 for (unsigned int i = 0; i < local_symcount; ++i)
1560 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1561 elfcpp::VER_NDX_LOCAL);
1562
1563 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1564 p != syms.end();
1565 ++p)
1566 {
1567 unsigned int version_index;
1568 const char* version = (*p)->version();
1569 if (version == NULL)
1570 version_index = elfcpp::VER_NDX_GLOBAL;
09124467 1571 else
46fe1623 1572 version_index = this->version_index(symtab, dynpool, *p);
09124467
ILT
1573 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1574 // the hidden bit.
1575 if ((*p)->version() != NULL && !(*p)->is_default())
1576 version_index |= elfcpp::VERSYM_HIDDEN;
14b31740 1577 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
09124467 1578 version_index);
14b31740
ILT
1579 }
1580
1581 *pp = pbuf;
1582 *psize = sz;
1583}
1584
1585// Return an allocated buffer holding the contents of the version
1586// definition section.
1587
1588template<int size, bool big_endian>
1589void
1590Versions::def_section_contents(const Stringpool* dynpool,
1591 unsigned char** pp, unsigned int* psize,
7d1a9ebb 1592 unsigned int* pentries) const
14b31740
ILT
1593{
1594 gold_assert(this->is_finalized_);
1595 gold_assert(!this->defs_.empty());
1596
1597 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1598 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1599
1600 unsigned int sz = 0;
1601 for (Defs::const_iterator p = this->defs_.begin();
1602 p != this->defs_.end();
1603 ++p)
1604 {
1605 sz += verdef_size + verdaux_size;
1606 sz += (*p)->count_dependencies() * verdaux_size;
1607 }
1608
1609 unsigned char* pbuf = new unsigned char[sz];
1610
1611 unsigned char* pb = pbuf;
1612 Defs::const_iterator p;
1613 unsigned int i;
1614 for (p = this->defs_.begin(), i = 0;
1615 p != this->defs_.end();
1616 ++p, ++i)
7d1a9ebb
ILT
1617 pb = (*p)->write<size, big_endian>(dynpool,
1618 i + 1 >= this->defs_.size(),
1619 pb);
14b31740
ILT
1620
1621 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1622
1623 *pp = pbuf;
1624 *psize = sz;
1625 *pentries = this->defs_.size();
1626}
1627
1628// Return an allocated buffer holding the contents of the version
1629// reference section.
1630
1631template<int size, bool big_endian>
1632void
1633Versions::need_section_contents(const Stringpool* dynpool,
1634 unsigned char** pp, unsigned int *psize,
7d1a9ebb 1635 unsigned int *pentries) const
14b31740
ILT
1636{
1637 gold_assert(this->is_finalized_);
1638 gold_assert(!this->needs_.empty());
1639
1640 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1641 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1642
1643 unsigned int sz = 0;
1644 for (Needs::const_iterator p = this->needs_.begin();
1645 p != this->needs_.end();
1646 ++p)
1647 {
1648 sz += verneed_size;
1649 sz += (*p)->count_versions() * vernaux_size;
1650 }
1651
1652 unsigned char* pbuf = new unsigned char[sz];
1653
1654 unsigned char* pb = pbuf;
1655 Needs::const_iterator p;
1656 unsigned int i;
1657 for (p = this->needs_.begin(), i = 0;
1658 p != this->needs_.end();
1659 ++p, ++i)
7d1a9ebb
ILT
1660 pb = (*p)->write<size, big_endian>(dynpool,
1661 i + 1 >= this->needs_.size(),
1662 pb);
14b31740
ILT
1663
1664 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1665
1666 *pp = pbuf;
1667 *psize = sz;
1668 *pentries = this->needs_.size();
1669}
1670
dbe717ef
ILT
1671// Instantiate the templates we need. We could use the configure
1672// script to restrict this to only the ones for implemented targets.
1673
193a53d9 1674#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
1675template
1676class Sized_dynobj<32, false>;
193a53d9 1677#endif
dbe717ef 1678
193a53d9 1679#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
1680template
1681class Sized_dynobj<32, true>;
193a53d9 1682#endif
dbe717ef 1683
193a53d9 1684#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
1685template
1686class Sized_dynobj<64, false>;
193a53d9 1687#endif
dbe717ef 1688
193a53d9 1689#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
1690template
1691class Sized_dynobj<64, true>;
193a53d9 1692#endif
dbe717ef 1693
193a53d9 1694#ifdef HAVE_TARGET_32_LITTLE
14b31740
ILT
1695template
1696void
91da9340 1697Versions::symbol_section_contents<32, false>(
46fe1623 1698 const Symbol_table*,
91da9340
ILT
1699 const Stringpool*,
1700 unsigned int,
1701 const std::vector<Symbol*>&,
1702 unsigned char**,
7d1a9ebb 1703 unsigned int*) const;
193a53d9 1704#endif
14b31740 1705
193a53d9 1706#ifdef HAVE_TARGET_32_BIG
14b31740
ILT
1707template
1708void
91da9340 1709Versions::symbol_section_contents<32, true>(
46fe1623 1710 const Symbol_table*,
91da9340
ILT
1711 const Stringpool*,
1712 unsigned int,
1713 const std::vector<Symbol*>&,
1714 unsigned char**,
7d1a9ebb 1715 unsigned int*) const;
193a53d9 1716#endif
14b31740 1717
193a53d9 1718#ifdef HAVE_TARGET_64_LITTLE
14b31740
ILT
1719template
1720void
91da9340 1721Versions::symbol_section_contents<64, false>(
46fe1623 1722 const Symbol_table*,
91da9340
ILT
1723 const Stringpool*,
1724 unsigned int,
1725 const std::vector<Symbol*>&,
1726 unsigned char**,
7d1a9ebb 1727 unsigned int*) const;
193a53d9 1728#endif
14b31740 1729
193a53d9 1730#ifdef HAVE_TARGET_64_BIG
14b31740
ILT
1731template
1732void
91da9340 1733Versions::symbol_section_contents<64, true>(
46fe1623 1734 const Symbol_table*,
91da9340
ILT
1735 const Stringpool*,
1736 unsigned int,
1737 const std::vector<Symbol*>&,
1738 unsigned char**,
7d1a9ebb 1739 unsigned int*) const;
193a53d9 1740#endif
14b31740 1741
193a53d9 1742#ifdef HAVE_TARGET_32_LITTLE
14b31740
ILT
1743template
1744void
91da9340
ILT
1745Versions::def_section_contents<32, false>(
1746 const Stringpool*,
1747 unsigned char**,
1748 unsigned int*,
7d1a9ebb 1749 unsigned int*) const;
193a53d9 1750#endif
14b31740 1751
193a53d9 1752#ifdef HAVE_TARGET_32_BIG
14b31740
ILT
1753template
1754void
91da9340
ILT
1755Versions::def_section_contents<32, true>(
1756 const Stringpool*,
1757 unsigned char**,
1758 unsigned int*,
7d1a9ebb 1759 unsigned int*) const;
193a53d9 1760#endif
14b31740 1761
193a53d9 1762#ifdef HAVE_TARGET_64_LITTLE
14b31740
ILT
1763template
1764void
91da9340
ILT
1765Versions::def_section_contents<64, false>(
1766 const Stringpool*,
1767 unsigned char**,
1768 unsigned int*,
7d1a9ebb 1769 unsigned int*) const;
193a53d9 1770#endif
14b31740 1771
193a53d9 1772#ifdef HAVE_TARGET_64_BIG
14b31740
ILT
1773template
1774void
91da9340
ILT
1775Versions::def_section_contents<64, true>(
1776 const Stringpool*,
1777 unsigned char**,
1778 unsigned int*,
7d1a9ebb 1779 unsigned int*) const;
193a53d9 1780#endif
14b31740 1781
193a53d9 1782#ifdef HAVE_TARGET_32_LITTLE
14b31740
ILT
1783template
1784void
91da9340
ILT
1785Versions::need_section_contents<32, false>(
1786 const Stringpool*,
1787 unsigned char**,
1788 unsigned int*,
7d1a9ebb 1789 unsigned int*) const;
193a53d9 1790#endif
14b31740 1791
193a53d9 1792#ifdef HAVE_TARGET_32_BIG
14b31740
ILT
1793template
1794void
91da9340
ILT
1795Versions::need_section_contents<32, true>(
1796 const Stringpool*,
1797 unsigned char**,
1798 unsigned int*,
7d1a9ebb 1799 unsigned int*) const;
193a53d9 1800#endif
14b31740 1801
193a53d9 1802#ifdef HAVE_TARGET_64_LITTLE
14b31740
ILT
1803template
1804void
91da9340
ILT
1805Versions::need_section_contents<64, false>(
1806 const Stringpool*,
1807 unsigned char**,
1808 unsigned int*,
7d1a9ebb 1809 unsigned int*) const;
193a53d9 1810#endif
14b31740 1811
193a53d9 1812#ifdef HAVE_TARGET_64_BIG
14b31740
ILT
1813template
1814void
91da9340
ILT
1815Versions::need_section_contents<64, true>(
1816 const Stringpool*,
1817 unsigned char**,
1818 unsigned int*,
7d1a9ebb 1819 unsigned int*) const;
193a53d9 1820#endif
14b31740 1821
dbe717ef 1822} // End namespace gold.
This page took 0.183296 seconds and 4 git commands to generate.