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