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