OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [dynobj.cc] - Blame information for rev 53

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.