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