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