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