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