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