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