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