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