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