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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [output.cc] - Blame information for rev 163

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

Line No. Rev Author Line
1 27 khays
// output.cc -- manage the output file for gold
2
 
3
// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 <cstdlib>
26
#include <cstring>
27
#include <cerrno>
28
#include <fcntl.h>
29
#include <unistd.h>
30
#include <sys/stat.h>
31
#include <algorithm>
32
 
33
#ifdef HAVE_SYS_MMAN_H
34
#include <sys/mman.h>
35
#endif
36
 
37
#include "libiberty.h"
38
 
39 159 khays
#include "dwarf.h"
40 27 khays
#include "parameters.h"
41
#include "object.h"
42
#include "symtab.h"
43
#include "reloc.h"
44
#include "merge.h"
45
#include "descriptors.h"
46 159 khays
#include "layout.h"
47 27 khays
#include "output.h"
48
 
49
// For systems without mmap support.
50
#ifndef HAVE_MMAP
51
# define mmap gold_mmap
52
# define munmap gold_munmap
53
# define mremap gold_mremap
54
# ifndef MAP_FAILED
55
#  define MAP_FAILED (reinterpret_cast<void*>(-1))
56
# endif
57
# ifndef PROT_READ
58
#  define PROT_READ 0
59
# endif
60
# ifndef PROT_WRITE
61
#  define PROT_WRITE 0
62
# endif
63
# ifndef MAP_PRIVATE
64
#  define MAP_PRIVATE 0
65
# endif
66
# ifndef MAP_ANONYMOUS
67
#  define MAP_ANONYMOUS 0
68
# endif
69
# ifndef MAP_SHARED
70
#  define MAP_SHARED 0
71
# endif
72
 
73
# ifndef ENOSYS
74
#  define ENOSYS EINVAL
75
# endif
76
 
77
static void *
78
gold_mmap(void *, size_t, int, int, int, off_t)
79
{
80
  errno = ENOSYS;
81
  return MAP_FAILED;
82
}
83
 
84
static int
85
gold_munmap(void *, size_t)
86
{
87
  errno = ENOSYS;
88
  return -1;
89
}
90
 
91
static void *
92
gold_mremap(void *, size_t, size_t, int)
93
{
94
  errno = ENOSYS;
95
  return MAP_FAILED;
96
}
97
 
98
#endif
99
 
100
#if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
101
# define mremap gold_mremap
102
extern "C" void *gold_mremap(void *, size_t, size_t, int);
103
#endif
104
 
105
// Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
106
#ifndef MAP_ANONYMOUS
107
# define MAP_ANONYMOUS  MAP_ANON
108
#endif
109
 
110
#ifndef MREMAP_MAYMOVE
111
# define MREMAP_MAYMOVE 1
112
#endif
113
 
114
#ifndef HAVE_POSIX_FALLOCATE
115
// A dummy, non general, version of posix_fallocate.  Here we just set
116
// the file size and hope that there is enough disk space.  FIXME: We
117
// could allocate disk space by walking block by block and writing a
118
// zero byte into each block.
119
static int
120
posix_fallocate(int o, off_t offset, off_t len)
121
{
122 163 khays
  if (ftruncate(o, offset + len) < 0)
123
    return errno;
124
  return 0;
125 27 khays
}
126
#endif // !defined(HAVE_POSIX_FALLOCATE)
127
 
128
// Mingw does not have S_ISLNK.
129
#ifndef S_ISLNK
130
# define S_ISLNK(mode) 0
131
#endif
132
 
133
namespace gold
134
{
135
 
136
// Output_data variables.
137
 
138
bool Output_data::allocated_sizes_are_fixed;
139
 
140
// Output_data methods.
141
 
142
Output_data::~Output_data()
143
{
144
}
145
 
146
// Return the default alignment for the target size.
147
 
148
uint64_t
149
Output_data::default_alignment()
150
{
151
  return Output_data::default_alignment_for_size(
152
      parameters->target().get_size());
153
}
154
 
155
// Return the default alignment for a size--32 or 64.
156
 
157
uint64_t
158
Output_data::default_alignment_for_size(int size)
159
{
160
  if (size == 32)
161
    return 4;
162
  else if (size == 64)
163
    return 8;
164
  else
165
    gold_unreachable();
166
}
167
 
168
// Output_section_header methods.  This currently assumes that the
169
// segment and section lists are complete at construction time.
170
 
171
Output_section_headers::Output_section_headers(
172
    const Layout* layout,
173
    const Layout::Segment_list* segment_list,
174
    const Layout::Section_list* section_list,
175
    const Layout::Section_list* unattached_section_list,
176
    const Stringpool* secnamepool,
177
    const Output_section* shstrtab_section)
178
  : layout_(layout),
179
    segment_list_(segment_list),
180
    section_list_(section_list),
181
    unattached_section_list_(unattached_section_list),
182
    secnamepool_(secnamepool),
183
    shstrtab_section_(shstrtab_section)
184
{
185
}
186
 
187
// Compute the current data size.
188
 
189
off_t
190
Output_section_headers::do_size() const
191
{
192
  // Count all the sections.  Start with 1 for the null section.
193
  off_t count = 1;
194
  if (!parameters->options().relocatable())
195
    {
196
      for (Layout::Segment_list::const_iterator p =
197
             this->segment_list_->begin();
198
           p != this->segment_list_->end();
199
           ++p)
200
        if ((*p)->type() == elfcpp::PT_LOAD)
201
          count += (*p)->output_section_count();
202
    }
203
  else
204
    {
205
      for (Layout::Section_list::const_iterator p =
206
             this->section_list_->begin();
207
           p != this->section_list_->end();
208
           ++p)
209
        if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
210
          ++count;
211
    }
212
  count += this->unattached_section_list_->size();
213
 
214
  const int size = parameters->target().get_size();
215
  int shdr_size;
216
  if (size == 32)
217
    shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
218
  else if (size == 64)
219
    shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
220
  else
221
    gold_unreachable();
222
 
223
  return count * shdr_size;
224
}
225
 
226
// Write out the section headers.
227
 
228
void
229
Output_section_headers::do_write(Output_file* of)
230
{
231
  switch (parameters->size_and_endianness())
232
    {
233
#ifdef HAVE_TARGET_32_LITTLE
234
    case Parameters::TARGET_32_LITTLE:
235
      this->do_sized_write<32, false>(of);
236
      break;
237
#endif
238
#ifdef HAVE_TARGET_32_BIG
239
    case Parameters::TARGET_32_BIG:
240
      this->do_sized_write<32, true>(of);
241
      break;
242
#endif
243
#ifdef HAVE_TARGET_64_LITTLE
244
    case Parameters::TARGET_64_LITTLE:
245
      this->do_sized_write<64, false>(of);
246
      break;
247
#endif
248
#ifdef HAVE_TARGET_64_BIG
249
    case Parameters::TARGET_64_BIG:
250
      this->do_sized_write<64, true>(of);
251
      break;
252
#endif
253
    default:
254
      gold_unreachable();
255
    }
256
}
257
 
258
template<int size, bool big_endian>
259
void
260
Output_section_headers::do_sized_write(Output_file* of)
261
{
262
  off_t all_shdrs_size = this->data_size();
263
  unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
264
 
265
  const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
266
  unsigned char* v = view;
267
 
268
  {
269
    typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
270
    oshdr.put_sh_name(0);
271
    oshdr.put_sh_type(elfcpp::SHT_NULL);
272
    oshdr.put_sh_flags(0);
273
    oshdr.put_sh_addr(0);
274
    oshdr.put_sh_offset(0);
275
 
276
    size_t section_count = (this->data_size()
277
                            / elfcpp::Elf_sizes<size>::shdr_size);
278
    if (section_count < elfcpp::SHN_LORESERVE)
279
      oshdr.put_sh_size(0);
280
    else
281
      oshdr.put_sh_size(section_count);
282
 
283
    unsigned int shstrndx = this->shstrtab_section_->out_shndx();
284
    if (shstrndx < elfcpp::SHN_LORESERVE)
285
      oshdr.put_sh_link(0);
286
    else
287
      oshdr.put_sh_link(shstrndx);
288
 
289
    size_t segment_count = this->segment_list_->size();
290
    oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
291
 
292
    oshdr.put_sh_addralign(0);
293
    oshdr.put_sh_entsize(0);
294
  }
295
 
296
  v += shdr_size;
297
 
298
  unsigned int shndx = 1;
299
  if (!parameters->options().relocatable())
300
    {
301
      for (Layout::Segment_list::const_iterator p =
302
             this->segment_list_->begin();
303
           p != this->segment_list_->end();
304
           ++p)
305
        v = (*p)->write_section_headers<size, big_endian>(this->layout_,
306
                                                          this->secnamepool_,
307
                                                          v,
308
                                                          &shndx);
309
    }
310
  else
311
    {
312
      for (Layout::Section_list::const_iterator p =
313
             this->section_list_->begin();
314
           p != this->section_list_->end();
315
           ++p)
316
        {
317
          // We do unallocated sections below, except that group
318
          // sections have to come first.
319
          if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
320
              && (*p)->type() != elfcpp::SHT_GROUP)
321
            continue;
322
          gold_assert(shndx == (*p)->out_shndx());
323
          elfcpp::Shdr_write<size, big_endian> oshdr(v);
324
          (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
325
          v += shdr_size;
326
          ++shndx;
327
        }
328
    }
329
 
330
  for (Layout::Section_list::const_iterator p =
331
         this->unattached_section_list_->begin();
332
       p != this->unattached_section_list_->end();
333
       ++p)
334
    {
335
      // For a relocatable link, we did unallocated group sections
336
      // above, since they have to come first.
337
      if ((*p)->type() == elfcpp::SHT_GROUP
338
          && parameters->options().relocatable())
339
        continue;
340
      gold_assert(shndx == (*p)->out_shndx());
341
      elfcpp::Shdr_write<size, big_endian> oshdr(v);
342
      (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
343
      v += shdr_size;
344
      ++shndx;
345
    }
346
 
347
  of->write_output_view(this->offset(), all_shdrs_size, view);
348
}
349
 
350
// Output_segment_header methods.
351
 
352
Output_segment_headers::Output_segment_headers(
353
    const Layout::Segment_list& segment_list)
354
  : segment_list_(segment_list)
355
{
356
  this->set_current_data_size_for_child(this->do_size());
357
}
358
 
359
void
360
Output_segment_headers::do_write(Output_file* of)
361
{
362
  switch (parameters->size_and_endianness())
363
    {
364
#ifdef HAVE_TARGET_32_LITTLE
365
    case Parameters::TARGET_32_LITTLE:
366
      this->do_sized_write<32, false>(of);
367
      break;
368
#endif
369
#ifdef HAVE_TARGET_32_BIG
370
    case Parameters::TARGET_32_BIG:
371
      this->do_sized_write<32, true>(of);
372
      break;
373
#endif
374
#ifdef HAVE_TARGET_64_LITTLE
375
    case Parameters::TARGET_64_LITTLE:
376
      this->do_sized_write<64, false>(of);
377
      break;
378
#endif
379
#ifdef HAVE_TARGET_64_BIG
380
    case Parameters::TARGET_64_BIG:
381
      this->do_sized_write<64, true>(of);
382
      break;
383
#endif
384
    default:
385
      gold_unreachable();
386
    }
387
}
388
 
389
template<int size, bool big_endian>
390
void
391
Output_segment_headers::do_sized_write(Output_file* of)
392
{
393
  const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
394
  off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
395
  gold_assert(all_phdrs_size == this->data_size());
396
  unsigned char* view = of->get_output_view(this->offset(),
397
                                            all_phdrs_size);
398
  unsigned char* v = view;
399
  for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
400
       p != this->segment_list_.end();
401
       ++p)
402
    {
403
      elfcpp::Phdr_write<size, big_endian> ophdr(v);
404
      (*p)->write_header(&ophdr);
405
      v += phdr_size;
406
    }
407
 
408
  gold_assert(v - view == all_phdrs_size);
409
 
410
  of->write_output_view(this->offset(), all_phdrs_size, view);
411
}
412
 
413
off_t
414
Output_segment_headers::do_size() const
415
{
416
  const int size = parameters->target().get_size();
417
  int phdr_size;
418
  if (size == 32)
419
    phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
420
  else if (size == 64)
421
    phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
422
  else
423
    gold_unreachable();
424
 
425
  return this->segment_list_.size() * phdr_size;
426
}
427
 
428
// Output_file_header methods.
429
 
430
Output_file_header::Output_file_header(const Target* target,
431
                                       const Symbol_table* symtab,
432
                                       const Output_segment_headers* osh)
433
  : target_(target),
434
    symtab_(symtab),
435
    segment_header_(osh),
436
    section_header_(NULL),
437
    shstrtab_(NULL)
438
{
439
  this->set_data_size(this->do_size());
440
}
441
 
442
// Set the section table information for a file header.
443
 
444
void
445
Output_file_header::set_section_info(const Output_section_headers* shdrs,
446
                                     const Output_section* shstrtab)
447
{
448
  this->section_header_ = shdrs;
449
  this->shstrtab_ = shstrtab;
450
}
451
 
452
// Write out the file header.
453
 
454
void
455
Output_file_header::do_write(Output_file* of)
456
{
457
  gold_assert(this->offset() == 0);
458
 
459
  switch (parameters->size_and_endianness())
460
    {
461
#ifdef HAVE_TARGET_32_LITTLE
462
    case Parameters::TARGET_32_LITTLE:
463
      this->do_sized_write<32, false>(of);
464
      break;
465
#endif
466
#ifdef HAVE_TARGET_32_BIG
467
    case Parameters::TARGET_32_BIG:
468
      this->do_sized_write<32, true>(of);
469
      break;
470
#endif
471
#ifdef HAVE_TARGET_64_LITTLE
472
    case Parameters::TARGET_64_LITTLE:
473
      this->do_sized_write<64, false>(of);
474
      break;
475
#endif
476
#ifdef HAVE_TARGET_64_BIG
477
    case Parameters::TARGET_64_BIG:
478
      this->do_sized_write<64, true>(of);
479
      break;
480
#endif
481
    default:
482
      gold_unreachable();
483
    }
484
}
485
 
486
// Write out the file header with appropriate size and endianness.
487
 
488
template<int size, bool big_endian>
489
void
490
Output_file_header::do_sized_write(Output_file* of)
491
{
492
  gold_assert(this->offset() == 0);
493
 
494
  int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
495
  unsigned char* view = of->get_output_view(0, ehdr_size);
496
  elfcpp::Ehdr_write<size, big_endian> oehdr(view);
497
 
498
  unsigned char e_ident[elfcpp::EI_NIDENT];
499
  memset(e_ident, 0, elfcpp::EI_NIDENT);
500
  e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
501
  e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
502
  e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
503
  e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
504
  if (size == 32)
505
    e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
506
  else if (size == 64)
507
    e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
508
  else
509
    gold_unreachable();
510
  e_ident[elfcpp::EI_DATA] = (big_endian
511
                              ? elfcpp::ELFDATA2MSB
512
                              : elfcpp::ELFDATA2LSB);
513
  e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
514
  oehdr.put_e_ident(e_ident);
515
 
516
  elfcpp::ET e_type;
517
  if (parameters->options().relocatable())
518
    e_type = elfcpp::ET_REL;
519
  else if (parameters->options().output_is_position_independent())
520
    e_type = elfcpp::ET_DYN;
521
  else
522
    e_type = elfcpp::ET_EXEC;
523
  oehdr.put_e_type(e_type);
524
 
525
  oehdr.put_e_machine(this->target_->machine_code());
526
  oehdr.put_e_version(elfcpp::EV_CURRENT);
527
 
528
  oehdr.put_e_entry(this->entry<size>());
529
 
530
  if (this->segment_header_ == NULL)
531
    oehdr.put_e_phoff(0);
532
  else
533
    oehdr.put_e_phoff(this->segment_header_->offset());
534
 
535
  oehdr.put_e_shoff(this->section_header_->offset());
536
  oehdr.put_e_flags(this->target_->processor_specific_flags());
537
  oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
538
 
539
  if (this->segment_header_ == NULL)
540
    {
541
      oehdr.put_e_phentsize(0);
542
      oehdr.put_e_phnum(0);
543
    }
544
  else
545
    {
546
      oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
547
      size_t phnum = (this->segment_header_->data_size()
548
                      / elfcpp::Elf_sizes<size>::phdr_size);
549
      if (phnum > elfcpp::PN_XNUM)
550
        phnum = elfcpp::PN_XNUM;
551
      oehdr.put_e_phnum(phnum);
552
    }
553
 
554
  oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
555
  size_t section_count = (this->section_header_->data_size()
556
                          / elfcpp::Elf_sizes<size>::shdr_size);
557
 
558
  if (section_count < elfcpp::SHN_LORESERVE)
559
    oehdr.put_e_shnum(this->section_header_->data_size()
560
                      / elfcpp::Elf_sizes<size>::shdr_size);
561
  else
562
    oehdr.put_e_shnum(0);
563
 
564
  unsigned int shstrndx = this->shstrtab_->out_shndx();
565
  if (shstrndx < elfcpp::SHN_LORESERVE)
566
    oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
567
  else
568
    oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
569
 
570
  // Let the target adjust the ELF header, e.g., to set EI_OSABI in
571
  // the e_ident field.
572
  parameters->target().adjust_elf_header(view, ehdr_size);
573
 
574
  of->write_output_view(0, ehdr_size, view);
575
}
576
 
577
// Return the value to use for the entry address.
578
 
579
template<int size>
580
typename elfcpp::Elf_types<size>::Elf_Addr
581
Output_file_header::entry()
582
{
583
  const bool should_issue_warning = (parameters->options().entry() != NULL
584
                                     && !parameters->options().relocatable()
585
                                     && !parameters->options().shared());
586
  const char* entry = parameters->entry();
587
  Symbol* sym = this->symtab_->lookup(entry);
588
 
589
  typename Sized_symbol<size>::Value_type v;
590
  if (sym != NULL)
591
    {
592
      Sized_symbol<size>* ssym;
593
      ssym = this->symtab_->get_sized_symbol<size>(sym);
594
      if (!ssym->is_defined() && should_issue_warning)
595
        gold_warning("entry symbol '%s' exists but is not defined", entry);
596
      v = ssym->value();
597
    }
598
  else
599
    {
600
      // We couldn't find the entry symbol.  See if we can parse it as
601
      // a number.  This supports, e.g., -e 0x1000.
602
      char* endptr;
603
      v = strtoull(entry, &endptr, 0);
604
      if (*endptr != '\0')
605
        {
606
          if (should_issue_warning)
607
            gold_warning("cannot find entry symbol '%s'", entry);
608
          v = 0;
609
        }
610
    }
611
 
612
  return v;
613
}
614
 
615
// Compute the current data size.
616
 
617
off_t
618
Output_file_header::do_size() const
619
{
620
  const int size = parameters->target().get_size();
621
  if (size == 32)
622
    return elfcpp::Elf_sizes<32>::ehdr_size;
623
  else if (size == 64)
624
    return elfcpp::Elf_sizes<64>::ehdr_size;
625
  else
626
    gold_unreachable();
627
}
628
 
629
// Output_data_const methods.
630
 
631
void
632
Output_data_const::do_write(Output_file* of)
633
{
634
  of->write(this->offset(), this->data_.data(), this->data_.size());
635
}
636
 
637
// Output_data_const_buffer methods.
638
 
639
void
640
Output_data_const_buffer::do_write(Output_file* of)
641
{
642
  of->write(this->offset(), this->p_, this->data_size());
643
}
644
 
645
// Output_section_data methods.
646
 
647
// Record the output section, and set the entry size and such.
648
 
649
void
650
Output_section_data::set_output_section(Output_section* os)
651
{
652
  gold_assert(this->output_section_ == NULL);
653
  this->output_section_ = os;
654
  this->do_adjust_output_section(os);
655
}
656
 
657
// Return the section index of the output section.
658
 
659
unsigned int
660
Output_section_data::do_out_shndx() const
661
{
662
  gold_assert(this->output_section_ != NULL);
663
  return this->output_section_->out_shndx();
664
}
665
 
666
// Set the alignment, which means we may need to update the alignment
667
// of the output section.
668
 
669
void
670
Output_section_data::set_addralign(uint64_t addralign)
671
{
672
  this->addralign_ = addralign;
673
  if (this->output_section_ != NULL
674
      && this->output_section_->addralign() < addralign)
675
    this->output_section_->set_addralign(addralign);
676
}
677
 
678
// Output_data_strtab methods.
679
 
680
// Set the final data size.
681
 
682
void
683
Output_data_strtab::set_final_data_size()
684
{
685
  this->strtab_->set_string_offsets();
686
  this->set_data_size(this->strtab_->get_strtab_size());
687
}
688
 
689
// Write out a string table.
690
 
691
void
692
Output_data_strtab::do_write(Output_file* of)
693
{
694
  this->strtab_->write(of, this->offset());
695
}
696
 
697
// Output_reloc methods.
698
 
699
// A reloc against a global symbol.
700
 
701
template<bool dynamic, int size, bool big_endian>
702
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
703
    Symbol* gsym,
704
    unsigned int type,
705
    Output_data* od,
706
    Address address,
707
    bool is_relative,
708
    bool is_symbolless)
709
  : address_(address), local_sym_index_(GSYM_CODE), type_(type),
710
    is_relative_(is_relative), is_symbolless_(is_symbolless),
711 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
712 27 khays
{
713
  // this->type_ is a bitfield; make sure TYPE fits.
714
  gold_assert(this->type_ == type);
715
  this->u1_.gsym = gsym;
716
  this->u2_.od = od;
717
  if (dynamic)
718
    this->set_needs_dynsym_index();
719
}
720
 
721
template<bool dynamic, int size, bool big_endian>
722
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
723
    Symbol* gsym,
724
    unsigned int type,
725
    Sized_relobj<size, big_endian>* relobj,
726
    unsigned int shndx,
727
    Address address,
728
    bool is_relative,
729
    bool is_symbolless)
730
  : address_(address), local_sym_index_(GSYM_CODE), type_(type),
731
    is_relative_(is_relative), is_symbolless_(is_symbolless),
732 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
733 27 khays
{
734
  gold_assert(shndx != INVALID_CODE);
735
  // this->type_ is a bitfield; make sure TYPE fits.
736
  gold_assert(this->type_ == type);
737
  this->u1_.gsym = gsym;
738
  this->u2_.relobj = relobj;
739
  if (dynamic)
740
    this->set_needs_dynsym_index();
741
}
742
 
743
// A reloc against a local symbol.
744
 
745
template<bool dynamic, int size, bool big_endian>
746
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
747
    Sized_relobj<size, big_endian>* relobj,
748
    unsigned int local_sym_index,
749
    unsigned int type,
750
    Output_data* od,
751
    Address address,
752
    bool is_relative,
753
    bool is_symbolless,
754 163 khays
    bool is_section_symbol,
755
    bool use_plt_offset)
756 27 khays
  : address_(address), local_sym_index_(local_sym_index), type_(type),
757
    is_relative_(is_relative), is_symbolless_(is_symbolless),
758 163 khays
    is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
759
    shndx_(INVALID_CODE)
760 27 khays
{
761
  gold_assert(local_sym_index != GSYM_CODE
762
              && local_sym_index != INVALID_CODE);
763
  // this->type_ is a bitfield; make sure TYPE fits.
764
  gold_assert(this->type_ == type);
765
  this->u1_.relobj = relobj;
766
  this->u2_.od = od;
767
  if (dynamic)
768
    this->set_needs_dynsym_index();
769
}
770
 
771
template<bool dynamic, int size, bool big_endian>
772
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
773
    Sized_relobj<size, big_endian>* relobj,
774
    unsigned int local_sym_index,
775
    unsigned int type,
776
    unsigned int shndx,
777
    Address address,
778
    bool is_relative,
779
    bool is_symbolless,
780 163 khays
    bool is_section_symbol,
781
    bool use_plt_offset)
782 27 khays
  : address_(address), local_sym_index_(local_sym_index), type_(type),
783
    is_relative_(is_relative), is_symbolless_(is_symbolless),
784 163 khays
    is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
785
    shndx_(shndx)
786 27 khays
{
787
  gold_assert(local_sym_index != GSYM_CODE
788
              && local_sym_index != INVALID_CODE);
789
  gold_assert(shndx != INVALID_CODE);
790
  // this->type_ is a bitfield; make sure TYPE fits.
791
  gold_assert(this->type_ == type);
792
  this->u1_.relobj = relobj;
793
  this->u2_.relobj = relobj;
794
  if (dynamic)
795
    this->set_needs_dynsym_index();
796
}
797
 
798
// A reloc against the STT_SECTION symbol of an output section.
799
 
800
template<bool dynamic, int size, bool big_endian>
801
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
802
    Output_section* os,
803
    unsigned int type,
804
    Output_data* od,
805
    Address address)
806
  : address_(address), local_sym_index_(SECTION_CODE), type_(type),
807
    is_relative_(false), is_symbolless_(false),
808 163 khays
    is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE)
809 27 khays
{
810
  // this->type_ is a bitfield; make sure TYPE fits.
811
  gold_assert(this->type_ == type);
812
  this->u1_.os = os;
813
  this->u2_.od = od;
814
  if (dynamic)
815
    this->set_needs_dynsym_index();
816
  else
817
    os->set_needs_symtab_index();
818
}
819
 
820
template<bool dynamic, int size, bool big_endian>
821
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
822
    Output_section* os,
823
    unsigned int type,
824
    Sized_relobj<size, big_endian>* relobj,
825
    unsigned int shndx,
826
    Address address)
827
  : address_(address), local_sym_index_(SECTION_CODE), type_(type),
828
    is_relative_(false), is_symbolless_(false),
829 163 khays
    is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx)
830 27 khays
{
831
  gold_assert(shndx != INVALID_CODE);
832
  // this->type_ is a bitfield; make sure TYPE fits.
833
  gold_assert(this->type_ == type);
834
  this->u1_.os = os;
835
  this->u2_.relobj = relobj;
836
  if (dynamic)
837
    this->set_needs_dynsym_index();
838
  else
839
    os->set_needs_symtab_index();
840
}
841
 
842
// An absolute relocation.
843
 
844
template<bool dynamic, int size, bool big_endian>
845
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
846
    unsigned int type,
847
    Output_data* od,
848
    Address address)
849
  : address_(address), local_sym_index_(0), type_(type),
850
    is_relative_(false), is_symbolless_(false),
851 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
852 27 khays
{
853
  // this->type_ is a bitfield; make sure TYPE fits.
854
  gold_assert(this->type_ == type);
855
  this->u1_.relobj = NULL;
856
  this->u2_.od = od;
857
}
858
 
859
template<bool dynamic, int size, bool big_endian>
860
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
861
    unsigned int type,
862
    Sized_relobj<size, big_endian>* relobj,
863
    unsigned int shndx,
864
    Address address)
865
  : address_(address), local_sym_index_(0), type_(type),
866
    is_relative_(false), is_symbolless_(false),
867 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
868 27 khays
{
869
  gold_assert(shndx != INVALID_CODE);
870
  // this->type_ is a bitfield; make sure TYPE fits.
871
  gold_assert(this->type_ == type);
872
  this->u1_.relobj = NULL;
873
  this->u2_.relobj = relobj;
874
}
875
 
876
// A target specific relocation.
877
 
878
template<bool dynamic, int size, bool big_endian>
879
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
880
    unsigned int type,
881
    void* arg,
882
    Output_data* od,
883
    Address address)
884
  : address_(address), local_sym_index_(TARGET_CODE), type_(type),
885
    is_relative_(false), is_symbolless_(false),
886 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
887 27 khays
{
888
  // this->type_ is a bitfield; make sure TYPE fits.
889
  gold_assert(this->type_ == type);
890
  this->u1_.arg = arg;
891
  this->u2_.od = od;
892
}
893
 
894
template<bool dynamic, int size, bool big_endian>
895
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
896
    unsigned int type,
897
    void* arg,
898
    Sized_relobj<size, big_endian>* relobj,
899
    unsigned int shndx,
900
    Address address)
901
  : address_(address), local_sym_index_(TARGET_CODE), type_(type),
902
    is_relative_(false), is_symbolless_(false),
903 163 khays
    is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
904 27 khays
{
905
  gold_assert(shndx != INVALID_CODE);
906
  // this->type_ is a bitfield; make sure TYPE fits.
907
  gold_assert(this->type_ == type);
908
  this->u1_.arg = arg;
909
  this->u2_.relobj = relobj;
910
}
911
 
912
// Record that we need a dynamic symbol index for this relocation.
913
 
914
template<bool dynamic, int size, bool big_endian>
915
void
916
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
917
set_needs_dynsym_index()
918
{
919
  if (this->is_symbolless_)
920
    return;
921
  switch (this->local_sym_index_)
922
    {
923
    case INVALID_CODE:
924
      gold_unreachable();
925
 
926
    case GSYM_CODE:
927
      this->u1_.gsym->set_needs_dynsym_entry();
928
      break;
929
 
930
    case SECTION_CODE:
931
      this->u1_.os->set_needs_dynsym_index();
932
      break;
933
 
934
    case TARGET_CODE:
935
      // The target must take care of this if necessary.
936
      break;
937
 
938
    case 0:
939
      break;
940
 
941
    default:
942
      {
943
        const unsigned int lsi = this->local_sym_index_;
944
        Sized_relobj_file<size, big_endian>* relobj =
945
            this->u1_.relobj->sized_relobj();
946
        gold_assert(relobj != NULL);
947
        if (!this->is_section_symbol_)
948
          relobj->set_needs_output_dynsym_entry(lsi);
949
        else
950
          relobj->output_section(lsi)->set_needs_dynsym_index();
951
      }
952
      break;
953
    }
954
}
955
 
956
// Get the symbol index of a relocation.
957
 
958
template<bool dynamic, int size, bool big_endian>
959
unsigned int
960
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
961
  const
962
{
963
  unsigned int index;
964
  if (this->is_symbolless_)
965
    return 0;
966
  switch (this->local_sym_index_)
967
    {
968
    case INVALID_CODE:
969
      gold_unreachable();
970
 
971
    case GSYM_CODE:
972
      if (this->u1_.gsym == NULL)
973
        index = 0;
974
      else if (dynamic)
975
        index = this->u1_.gsym->dynsym_index();
976
      else
977
        index = this->u1_.gsym->symtab_index();
978
      break;
979
 
980
    case SECTION_CODE:
981
      if (dynamic)
982
        index = this->u1_.os->dynsym_index();
983
      else
984
        index = this->u1_.os->symtab_index();
985
      break;
986
 
987
    case TARGET_CODE:
988
      index = parameters->target().reloc_symbol_index(this->u1_.arg,
989
                                                      this->type_);
990
      break;
991
 
992
    case 0:
993
      // Relocations without symbols use a symbol index of 0.
994
      index = 0;
995
      break;
996
 
997
    default:
998
      {
999
        const unsigned int lsi = this->local_sym_index_;
1000
        Sized_relobj_file<size, big_endian>* relobj =
1001
            this->u1_.relobj->sized_relobj();
1002
        gold_assert(relobj != NULL);
1003
        if (!this->is_section_symbol_)
1004
          {
1005
            if (dynamic)
1006
              index = relobj->dynsym_index(lsi);
1007
            else
1008
              index = relobj->symtab_index(lsi);
1009
          }
1010
        else
1011
          {
1012
            Output_section* os = relobj->output_section(lsi);
1013
            gold_assert(os != NULL);
1014
            if (dynamic)
1015
              index = os->dynsym_index();
1016
            else
1017
              index = os->symtab_index();
1018
          }
1019
      }
1020
      break;
1021
    }
1022
  gold_assert(index != -1U);
1023
  return index;
1024
}
1025
 
1026
// For a local section symbol, get the address of the offset ADDEND
1027
// within the input section.
1028
 
1029
template<bool dynamic, int size, bool big_endian>
1030
typename elfcpp::Elf_types<size>::Elf_Addr
1031
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1032
  local_section_offset(Addend addend) const
1033
{
1034
  gold_assert(this->local_sym_index_ != GSYM_CODE
1035
              && this->local_sym_index_ != SECTION_CODE
1036
              && this->local_sym_index_ != TARGET_CODE
1037
              && this->local_sym_index_ != INVALID_CODE
1038
              && this->local_sym_index_ != 0
1039
              && this->is_section_symbol_);
1040
  const unsigned int lsi = this->local_sym_index_;
1041
  Output_section* os = this->u1_.relobj->output_section(lsi);
1042
  gold_assert(os != NULL);
1043
  Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1044
  if (offset != invalid_address)
1045
    return offset + addend;
1046
  // This is a merge section.
1047
  Sized_relobj_file<size, big_endian>* relobj =
1048
      this->u1_.relobj->sized_relobj();
1049
  gold_assert(relobj != NULL);
1050
  offset = os->output_address(relobj, lsi, addend);
1051
  gold_assert(offset != invalid_address);
1052
  return offset;
1053
}
1054
 
1055
// Get the output address of a relocation.
1056
 
1057
template<bool dynamic, int size, bool big_endian>
1058
typename elfcpp::Elf_types<size>::Elf_Addr
1059
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
1060
{
1061
  Address address = this->address_;
1062
  if (this->shndx_ != INVALID_CODE)
1063
    {
1064
      Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1065
      gold_assert(os != NULL);
1066
      Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1067
      if (off != invalid_address)
1068
        address += os->address() + off;
1069
      else
1070
        {
1071
          Sized_relobj_file<size, big_endian>* relobj =
1072
              this->u2_.relobj->sized_relobj();
1073
          gold_assert(relobj != NULL);
1074
          address = os->output_address(relobj, this->shndx_, address);
1075
          gold_assert(address != invalid_address);
1076
        }
1077
    }
1078
  else if (this->u2_.od != NULL)
1079
    address += this->u2_.od->address();
1080
  return address;
1081
}
1082
 
1083
// Write out the offset and info fields of a Rel or Rela relocation
1084
// entry.
1085
 
1086
template<bool dynamic, int size, bool big_endian>
1087
template<typename Write_rel>
1088
void
1089
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1090
    Write_rel* wr) const
1091
{
1092
  wr->put_r_offset(this->get_address());
1093
  unsigned int sym_index = this->get_symbol_index();
1094
  wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1095
}
1096
 
1097
// Write out a Rel relocation.
1098
 
1099
template<bool dynamic, int size, bool big_endian>
1100
void
1101
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1102
    unsigned char* pov) const
1103
{
1104
  elfcpp::Rel_write<size, big_endian> orel(pov);
1105
  this->write_rel(&orel);
1106
}
1107
 
1108
// Get the value of the symbol referred to by a Rel relocation.
1109
 
1110
template<bool dynamic, int size, bool big_endian>
1111
typename elfcpp::Elf_types<size>::Elf_Addr
1112
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1113
    Addend addend) const
1114
{
1115
  if (this->local_sym_index_ == GSYM_CODE)
1116
    {
1117
      const Sized_symbol<size>* sym;
1118
      sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1119
      return sym->value() + addend;
1120
    }
1121
  gold_assert(this->local_sym_index_ != SECTION_CODE
1122
              && this->local_sym_index_ != TARGET_CODE
1123
              && this->local_sym_index_ != INVALID_CODE
1124
              && this->local_sym_index_ != 0
1125
              && !this->is_section_symbol_);
1126
  const unsigned int lsi = this->local_sym_index_;
1127
  Sized_relobj_file<size, big_endian>* relobj =
1128
      this->u1_.relobj->sized_relobj();
1129
  gold_assert(relobj != NULL);
1130 163 khays
  if (this->use_plt_offset_)
1131
    {
1132
      uint64_t plt_address =
1133
          parameters->target().plt_address_for_local(relobj, lsi);
1134
      return plt_address + relobj->local_plt_offset(lsi);
1135
    }
1136 27 khays
  const Symbol_value<size>* symval = relobj->local_symbol(lsi);
1137
  return symval->value(relobj, addend);
1138
}
1139
 
1140
// Reloc comparison.  This function sorts the dynamic relocs for the
1141
// benefit of the dynamic linker.  First we sort all relative relocs
1142
// to the front.  Among relative relocs, we sort by output address.
1143
// Among non-relative relocs, we sort by symbol index, then by output
1144
// address.
1145
 
1146
template<bool dynamic, int size, bool big_endian>
1147
int
1148
Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1149
  compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1150
    const
1151
{
1152
  if (this->is_relative_)
1153
    {
1154
      if (!r2.is_relative_)
1155
        return -1;
1156
      // Otherwise sort by reloc address below.
1157
    }
1158
  else if (r2.is_relative_)
1159
    return 1;
1160
  else
1161
    {
1162
      unsigned int sym1 = this->get_symbol_index();
1163
      unsigned int sym2 = r2.get_symbol_index();
1164
      if (sym1 < sym2)
1165
        return -1;
1166
      else if (sym1 > sym2)
1167
        return 1;
1168
      // Otherwise sort by reloc address.
1169
    }
1170
 
1171
  section_offset_type addr1 = this->get_address();
1172
  section_offset_type addr2 = r2.get_address();
1173
  if (addr1 < addr2)
1174
    return -1;
1175
  else if (addr1 > addr2)
1176
    return 1;
1177
 
1178
  // Final tie breaker, in order to generate the same output on any
1179
  // host: reloc type.
1180
  unsigned int type1 = this->type_;
1181
  unsigned int type2 = r2.type_;
1182
  if (type1 < type2)
1183
    return -1;
1184
  else if (type1 > type2)
1185
    return 1;
1186
 
1187
  // These relocs appear to be exactly the same.
1188
  return 0;
1189
}
1190
 
1191
// Write out a Rela relocation.
1192
 
1193
template<bool dynamic, int size, bool big_endian>
1194
void
1195
Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1196
    unsigned char* pov) const
1197
{
1198
  elfcpp::Rela_write<size, big_endian> orel(pov);
1199
  this->rel_.write_rel(&orel);
1200
  Addend addend = this->addend_;
1201
  if (this->rel_.is_target_specific())
1202
    addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1203
                                               this->rel_.type(), addend);
1204
  else if (this->rel_.is_symbolless())
1205
    addend = this->rel_.symbol_value(addend);
1206
  else if (this->rel_.is_local_section_symbol())
1207
    addend = this->rel_.local_section_offset(addend);
1208
  orel.put_r_addend(addend);
1209
}
1210
 
1211
// Output_data_reloc_base methods.
1212
 
1213
// Adjust the output section.
1214
 
1215
template<int sh_type, bool dynamic, int size, bool big_endian>
1216
void
1217
Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1218
    ::do_adjust_output_section(Output_section* os)
1219
{
1220
  if (sh_type == elfcpp::SHT_REL)
1221
    os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1222
  else if (sh_type == elfcpp::SHT_RELA)
1223
    os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1224
  else
1225
    gold_unreachable();
1226
 
1227
  // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1228
  // static link.  The backends will generate a dynamic reloc section
1229
  // to hold this.  In that case we don't want to link to the dynsym
1230
  // section, because there isn't one.
1231
  if (!dynamic)
1232
    os->set_should_link_to_symtab();
1233
  else if (parameters->doing_static_link())
1234
    ;
1235
  else
1236
    os->set_should_link_to_dynsym();
1237
}
1238
 
1239
// Write out relocation data.
1240
 
1241
template<int sh_type, bool dynamic, int size, bool big_endian>
1242
void
1243
Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1244
    Output_file* of)
1245
{
1246
  const off_t off = this->offset();
1247
  const off_t oview_size = this->data_size();
1248
  unsigned char* const oview = of->get_output_view(off, oview_size);
1249
 
1250
  if (this->sort_relocs())
1251
    {
1252
      gold_assert(dynamic);
1253
      std::sort(this->relocs_.begin(), this->relocs_.end(),
1254
                Sort_relocs_comparison());
1255
    }
1256
 
1257
  unsigned char* pov = oview;
1258
  for (typename Relocs::const_iterator p = this->relocs_.begin();
1259
       p != this->relocs_.end();
1260
       ++p)
1261
    {
1262
      p->write(pov);
1263
      pov += reloc_size;
1264
    }
1265
 
1266
  gold_assert(pov - oview == oview_size);
1267
 
1268
  of->write_output_view(off, oview_size, oview);
1269
 
1270
  // We no longer need the relocation entries.
1271
  this->relocs_.clear();
1272
}
1273
 
1274
// Class Output_relocatable_relocs.
1275
 
1276
template<int sh_type, int size, bool big_endian>
1277
void
1278
Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1279
{
1280
  this->set_data_size(this->rr_->output_reloc_count()
1281
                      * Reloc_types<sh_type, size, big_endian>::reloc_size);
1282
}
1283
 
1284
// class Output_data_group.
1285
 
1286
template<int size, bool big_endian>
1287
Output_data_group<size, big_endian>::Output_data_group(
1288
    Sized_relobj_file<size, big_endian>* relobj,
1289
    section_size_type entry_count,
1290
    elfcpp::Elf_Word flags,
1291
    std::vector<unsigned int>* input_shndxes)
1292
  : Output_section_data(entry_count * 4, 4, false),
1293
    relobj_(relobj),
1294
    flags_(flags)
1295
{
1296
  this->input_shndxes_.swap(*input_shndxes);
1297
}
1298
 
1299
// Write out the section group, which means translating the section
1300
// indexes to apply to the output file.
1301
 
1302
template<int size, bool big_endian>
1303
void
1304
Output_data_group<size, big_endian>::do_write(Output_file* of)
1305
{
1306
  const off_t off = this->offset();
1307
  const section_size_type oview_size =
1308
    convert_to_section_size_type(this->data_size());
1309
  unsigned char* const oview = of->get_output_view(off, oview_size);
1310
 
1311
  elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1312
  elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1313
  ++contents;
1314
 
1315
  for (std::vector<unsigned int>::const_iterator p =
1316
         this->input_shndxes_.begin();
1317
       p != this->input_shndxes_.end();
1318
       ++p, ++contents)
1319
    {
1320
      Output_section* os = this->relobj_->output_section(*p);
1321
 
1322
      unsigned int output_shndx;
1323
      if (os != NULL)
1324
        output_shndx = os->out_shndx();
1325
      else
1326
        {
1327
          this->relobj_->error(_("section group retained but "
1328
                                 "group element discarded"));
1329
          output_shndx = 0;
1330
        }
1331
 
1332
      elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1333
    }
1334
 
1335
  size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1336
  gold_assert(wrote == oview_size);
1337
 
1338
  of->write_output_view(off, oview_size, oview);
1339
 
1340
  // We no longer need this information.
1341
  this->input_shndxes_.clear();
1342
}
1343
 
1344
// Output_data_got::Got_entry methods.
1345
 
1346
// Write out the entry.
1347
 
1348
template<int size, bool big_endian>
1349
void
1350
Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1351
{
1352
  Valtype val = 0;
1353
 
1354
  switch (this->local_sym_index_)
1355
    {
1356
    case GSYM_CODE:
1357
      {
1358
        // If the symbol is resolved locally, we need to write out the
1359
        // link-time value, which will be relocated dynamically by a
1360
        // RELATIVE relocation.
1361
        Symbol* gsym = this->u_.gsym;
1362
        if (this->use_plt_offset_ && gsym->has_plt_offset())
1363 159 khays
          val = (parameters->target().plt_address_for_global(gsym)
1364 27 khays
                 + gsym->plt_offset());
1365
        else
1366
          {
1367
            Sized_symbol<size>* sgsym;
1368
            // This cast is a bit ugly.  We don't want to put a
1369
            // virtual method in Symbol, because we want Symbol to be
1370
            // as small as possible.
1371
            sgsym = static_cast<Sized_symbol<size>*>(gsym);
1372
            val = sgsym->value();
1373
          }
1374
      }
1375
      break;
1376
 
1377
    case CONSTANT_CODE:
1378
      val = this->u_.constant;
1379
      break;
1380
 
1381
    case RESERVED_CODE:
1382
      // If we're doing an incremental update, don't touch this GOT entry.
1383
      if (parameters->incremental_update())
1384
        return;
1385
      val = this->u_.constant;
1386
      break;
1387
 
1388
    default:
1389
      {
1390
        const Sized_relobj_file<size, big_endian>* object = this->u_.object;
1391
        const unsigned int lsi = this->local_sym_index_;
1392
        const Symbol_value<size>* symval = object->local_symbol(lsi);
1393
        if (!this->use_plt_offset_)
1394
          val = symval->value(this->u_.object, 0);
1395
        else
1396
          {
1397 159 khays
            uint64_t plt_address =
1398
              parameters->target().plt_address_for_local(object, lsi);
1399
            val = plt_address + object->local_plt_offset(lsi);
1400 27 khays
          }
1401
      }
1402
      break;
1403
    }
1404
 
1405
  elfcpp::Swap<size, big_endian>::writeval(pov, val);
1406
}
1407
 
1408
// Output_data_got methods.
1409
 
1410
// Add an entry for a global symbol to the GOT.  This returns true if
1411
// this is a new GOT entry, false if the symbol already had a GOT
1412
// entry.
1413
 
1414
template<int size, bool big_endian>
1415
bool
1416
Output_data_got<size, big_endian>::add_global(
1417
    Symbol* gsym,
1418
    unsigned int got_type)
1419
{
1420
  if (gsym->has_got_offset(got_type))
1421
    return false;
1422
 
1423
  unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1424
  gsym->set_got_offset(got_type, got_offset);
1425
  return true;
1426
}
1427
 
1428
// Like add_global, but use the PLT offset.
1429
 
1430
template<int size, bool big_endian>
1431
bool
1432
Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1433
                                                  unsigned int got_type)
1434
{
1435
  if (gsym->has_got_offset(got_type))
1436
    return false;
1437
 
1438
  unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1439
  gsym->set_got_offset(got_type, got_offset);
1440
  return true;
1441
}
1442
 
1443
// Add an entry for a global symbol to the GOT, and add a dynamic
1444
// relocation of type R_TYPE for the GOT entry.
1445
 
1446
template<int size, bool big_endian>
1447
void
1448
Output_data_got<size, big_endian>::add_global_with_rel(
1449
    Symbol* gsym,
1450
    unsigned int got_type,
1451
    Rel_dyn* rel_dyn,
1452
    unsigned int r_type)
1453
{
1454
  if (gsym->has_got_offset(got_type))
1455
    return;
1456
 
1457
  unsigned int got_offset = this->add_got_entry(Got_entry());
1458
  gsym->set_got_offset(got_type, got_offset);
1459
  rel_dyn->add_global(gsym, r_type, this, got_offset);
1460
}
1461
 
1462
template<int size, bool big_endian>
1463
void
1464
Output_data_got<size, big_endian>::add_global_with_rela(
1465
    Symbol* gsym,
1466
    unsigned int got_type,
1467
    Rela_dyn* rela_dyn,
1468
    unsigned int r_type)
1469
{
1470
  if (gsym->has_got_offset(got_type))
1471
    return;
1472
 
1473
  unsigned int got_offset = this->add_got_entry(Got_entry());
1474
  gsym->set_got_offset(got_type, got_offset);
1475
  rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1476
}
1477
 
1478
// Add a pair of entries for a global symbol to the GOT, and add
1479
// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1480
// If R_TYPE_2 == 0, add the second entry with no relocation.
1481
template<int size, bool big_endian>
1482
void
1483
Output_data_got<size, big_endian>::add_global_pair_with_rel(
1484
    Symbol* gsym,
1485
    unsigned int got_type,
1486
    Rel_dyn* rel_dyn,
1487
    unsigned int r_type_1,
1488
    unsigned int r_type_2)
1489
{
1490
  if (gsym->has_got_offset(got_type))
1491
    return;
1492
 
1493
  unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1494
  gsym->set_got_offset(got_type, got_offset);
1495
  rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1496
 
1497
  if (r_type_2 != 0)
1498
    rel_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8);
1499
}
1500
 
1501
template<int size, bool big_endian>
1502
void
1503
Output_data_got<size, big_endian>::add_global_pair_with_rela(
1504
    Symbol* gsym,
1505
    unsigned int got_type,
1506
    Rela_dyn* rela_dyn,
1507
    unsigned int r_type_1,
1508
    unsigned int r_type_2)
1509
{
1510
  if (gsym->has_got_offset(got_type))
1511
    return;
1512
 
1513
  unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1514
  gsym->set_got_offset(got_type, got_offset);
1515
  rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1516
 
1517
  if (r_type_2 != 0)
1518
    rela_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8, 0);
1519
}
1520
 
1521
// Add an entry for a local symbol to the GOT.  This returns true if
1522
// this is a new GOT entry, false if the symbol already has a GOT
1523
// entry.
1524
 
1525
template<int size, bool big_endian>
1526
bool
1527
Output_data_got<size, big_endian>::add_local(
1528
    Sized_relobj_file<size, big_endian>* object,
1529
    unsigned int symndx,
1530
    unsigned int got_type)
1531
{
1532
  if (object->local_has_got_offset(symndx, got_type))
1533
    return false;
1534
 
1535
  unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1536
                                                          false));
1537
  object->set_local_got_offset(symndx, got_type, got_offset);
1538
  return true;
1539
}
1540
 
1541
// Like add_local, but use the PLT offset.
1542
 
1543
template<int size, bool big_endian>
1544
bool
1545
Output_data_got<size, big_endian>::add_local_plt(
1546
    Sized_relobj_file<size, big_endian>* object,
1547
    unsigned int symndx,
1548
    unsigned int got_type)
1549
{
1550
  if (object->local_has_got_offset(symndx, got_type))
1551
    return false;
1552
 
1553
  unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1554
                                                          true));
1555
  object->set_local_got_offset(symndx, got_type, got_offset);
1556
  return true;
1557
}
1558
 
1559
// Add an entry for a local symbol to the GOT, and add a dynamic
1560
// relocation of type R_TYPE for the GOT entry.
1561
 
1562
template<int size, bool big_endian>
1563
void
1564
Output_data_got<size, big_endian>::add_local_with_rel(
1565
    Sized_relobj_file<size, big_endian>* object,
1566
    unsigned int symndx,
1567
    unsigned int got_type,
1568
    Rel_dyn* rel_dyn,
1569
    unsigned int r_type)
1570
{
1571
  if (object->local_has_got_offset(symndx, got_type))
1572
    return;
1573
 
1574
  unsigned int got_offset = this->add_got_entry(Got_entry());
1575
  object->set_local_got_offset(symndx, got_type, got_offset);
1576
  rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1577
}
1578
 
1579
template<int size, bool big_endian>
1580
void
1581
Output_data_got<size, big_endian>::add_local_with_rela(
1582
    Sized_relobj_file<size, big_endian>* object,
1583
    unsigned int symndx,
1584
    unsigned int got_type,
1585
    Rela_dyn* rela_dyn,
1586
    unsigned int r_type)
1587
{
1588
  if (object->local_has_got_offset(symndx, got_type))
1589
    return;
1590
 
1591
  unsigned int got_offset = this->add_got_entry(Got_entry());
1592
  object->set_local_got_offset(symndx, got_type, got_offset);
1593
  rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1594
}
1595
 
1596
// Add a pair of entries for a local symbol to the GOT, and add
1597
// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1598
// If R_TYPE_2 == 0, add the second entry with no relocation.
1599
template<int size, bool big_endian>
1600
void
1601
Output_data_got<size, big_endian>::add_local_pair_with_rel(
1602
    Sized_relobj_file<size, big_endian>* object,
1603
    unsigned int symndx,
1604
    unsigned int shndx,
1605
    unsigned int got_type,
1606
    Rel_dyn* rel_dyn,
1607
    unsigned int r_type_1,
1608
    unsigned int r_type_2)
1609
{
1610
  if (object->local_has_got_offset(symndx, got_type))
1611
    return;
1612
 
1613
  unsigned int got_offset =
1614
      this->add_got_entry_pair(Got_entry(),
1615
                               Got_entry(object, symndx, false));
1616
  object->set_local_got_offset(symndx, got_type, got_offset);
1617
  Output_section* os = object->output_section(shndx);
1618
  rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1619
 
1620
  if (r_type_2 != 0)
1621
    rel_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8);
1622
}
1623
 
1624
template<int size, bool big_endian>
1625
void
1626
Output_data_got<size, big_endian>::add_local_pair_with_rela(
1627
    Sized_relobj_file<size, big_endian>* object,
1628
    unsigned int symndx,
1629
    unsigned int shndx,
1630
    unsigned int got_type,
1631
    Rela_dyn* rela_dyn,
1632
    unsigned int r_type_1,
1633
    unsigned int r_type_2)
1634
{
1635
  if (object->local_has_got_offset(symndx, got_type))
1636
    return;
1637
 
1638
  unsigned int got_offset =
1639
      this->add_got_entry_pair(Got_entry(),
1640
                               Got_entry(object, symndx, false));
1641
  object->set_local_got_offset(symndx, got_type, got_offset);
1642
  Output_section* os = object->output_section(shndx);
1643
  rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1644
 
1645
  if (r_type_2 != 0)
1646
    rela_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8, 0);
1647
}
1648
 
1649
// Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1650
 
1651
template<int size, bool big_endian>
1652
void
1653
Output_data_got<size, big_endian>::reserve_local(
1654
    unsigned int i,
1655
    Sized_relobj<size, big_endian>* object,
1656
    unsigned int sym_index,
1657
    unsigned int got_type)
1658
{
1659
  this->reserve_slot(i);
1660
  object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
1661
}
1662
 
1663
// Reserve a slot in the GOT for a global symbol.
1664
 
1665
template<int size, bool big_endian>
1666
void
1667
Output_data_got<size, big_endian>::reserve_global(
1668
    unsigned int i,
1669
    Symbol* gsym,
1670
    unsigned int got_type)
1671
{
1672
  this->reserve_slot(i);
1673
  gsym->set_got_offset(got_type, this->got_offset(i));
1674
}
1675
 
1676
// Write out the GOT.
1677
 
1678
template<int size, bool big_endian>
1679
void
1680
Output_data_got<size, big_endian>::do_write(Output_file* of)
1681
{
1682
  const int add = size / 8;
1683
 
1684
  const off_t off = this->offset();
1685
  const off_t oview_size = this->data_size();
1686
  unsigned char* const oview = of->get_output_view(off, oview_size);
1687
 
1688
  unsigned char* pov = oview;
1689
  for (typename Got_entries::const_iterator p = this->entries_.begin();
1690
       p != this->entries_.end();
1691
       ++p)
1692
    {
1693
      p->write(pov);
1694
      pov += add;
1695
    }
1696
 
1697
  gold_assert(pov - oview == oview_size);
1698
 
1699
  of->write_output_view(off, oview_size, oview);
1700
 
1701
  // We no longer need the GOT entries.
1702
  this->entries_.clear();
1703
}
1704
 
1705
// Create a new GOT entry and return its offset.
1706
 
1707
template<int size, bool big_endian>
1708
unsigned int
1709
Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1710
{
1711
  if (!this->is_data_size_valid())
1712
    {
1713
      this->entries_.push_back(got_entry);
1714
      this->set_got_size();
1715
      return this->last_got_offset();
1716
    }
1717
  else
1718
    {
1719
      // For an incremental update, find an available slot.
1720
      off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1721
      if (got_offset == -1)
1722 148 khays
        gold_fallback(_("out of patch space (GOT);"
1723
                        " relink with --incremental-full"));
1724 27 khays
      unsigned int got_index = got_offset / (size / 8);
1725
      gold_assert(got_index < this->entries_.size());
1726
      this->entries_[got_index] = got_entry;
1727
      return static_cast<unsigned int>(got_offset);
1728
    }
1729
}
1730
 
1731
// Create a pair of new GOT entries and return the offset of the first.
1732
 
1733
template<int size, bool big_endian>
1734
unsigned int
1735
Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1736
                                                      Got_entry got_entry_2)
1737
{
1738
  if (!this->is_data_size_valid())
1739
    {
1740
      unsigned int got_offset;
1741
      this->entries_.push_back(got_entry_1);
1742
      got_offset = this->last_got_offset();
1743
      this->entries_.push_back(got_entry_2);
1744
      this->set_got_size();
1745
      return got_offset;
1746
    }
1747
  else
1748
    {
1749
      // For an incremental update, find an available pair of slots.
1750
      off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1751
      if (got_offset == -1)
1752 148 khays
        gold_fallback(_("out of patch space (GOT);"
1753
                        " relink with --incremental-full"));
1754 27 khays
      unsigned int got_index = got_offset / (size / 8);
1755
      gold_assert(got_index < this->entries_.size());
1756
      this->entries_[got_index] = got_entry_1;
1757
      this->entries_[got_index + 1] = got_entry_2;
1758
      return static_cast<unsigned int>(got_offset);
1759
    }
1760
}
1761
 
1762
// Output_data_dynamic::Dynamic_entry methods.
1763
 
1764
// Write out the entry.
1765
 
1766
template<int size, bool big_endian>
1767
void
1768
Output_data_dynamic::Dynamic_entry::write(
1769
    unsigned char* pov,
1770
    const Stringpool* pool) const
1771
{
1772
  typename elfcpp::Elf_types<size>::Elf_WXword val;
1773
  switch (this->offset_)
1774
    {
1775
    case DYNAMIC_NUMBER:
1776
      val = this->u_.val;
1777
      break;
1778
 
1779
    case DYNAMIC_SECTION_SIZE:
1780
      val = this->u_.od->data_size();
1781
      if (this->od2 != NULL)
1782
        val += this->od2->data_size();
1783
      break;
1784
 
1785
    case DYNAMIC_SYMBOL:
1786
      {
1787
        const Sized_symbol<size>* s =
1788
          static_cast<const Sized_symbol<size>*>(this->u_.sym);
1789
        val = s->value();
1790
      }
1791
      break;
1792
 
1793
    case DYNAMIC_STRING:
1794
      val = pool->get_offset(this->u_.str);
1795
      break;
1796
 
1797
    default:
1798
      val = this->u_.od->address() + this->offset_;
1799
      break;
1800
    }
1801
 
1802
  elfcpp::Dyn_write<size, big_endian> dw(pov);
1803
  dw.put_d_tag(this->tag_);
1804
  dw.put_d_val(val);
1805
}
1806
 
1807
// Output_data_dynamic methods.
1808
 
1809
// Adjust the output section to set the entry size.
1810
 
1811
void
1812
Output_data_dynamic::do_adjust_output_section(Output_section* os)
1813
{
1814
  if (parameters->target().get_size() == 32)
1815
    os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1816
  else if (parameters->target().get_size() == 64)
1817
    os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1818
  else
1819
    gold_unreachable();
1820
}
1821
 
1822
// Set the final data size.
1823
 
1824
void
1825
Output_data_dynamic::set_final_data_size()
1826
{
1827
  // Add the terminating entry if it hasn't been added.
1828
  // Because of relaxation, we can run this multiple times.
1829
  if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1830
    {
1831
      int extra = parameters->options().spare_dynamic_tags();
1832
      for (int i = 0; i < extra; ++i)
1833
        this->add_constant(elfcpp::DT_NULL, 0);
1834
      this->add_constant(elfcpp::DT_NULL, 0);
1835
    }
1836
 
1837
  int dyn_size;
1838
  if (parameters->target().get_size() == 32)
1839
    dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1840
  else if (parameters->target().get_size() == 64)
1841
    dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1842
  else
1843
    gold_unreachable();
1844
  this->set_data_size(this->entries_.size() * dyn_size);
1845
}
1846
 
1847
// Write out the dynamic entries.
1848
 
1849
void
1850
Output_data_dynamic::do_write(Output_file* of)
1851
{
1852
  switch (parameters->size_and_endianness())
1853
    {
1854
#ifdef HAVE_TARGET_32_LITTLE
1855
    case Parameters::TARGET_32_LITTLE:
1856
      this->sized_write<32, false>(of);
1857
      break;
1858
#endif
1859
#ifdef HAVE_TARGET_32_BIG
1860
    case Parameters::TARGET_32_BIG:
1861
      this->sized_write<32, true>(of);
1862
      break;
1863
#endif
1864
#ifdef HAVE_TARGET_64_LITTLE
1865
    case Parameters::TARGET_64_LITTLE:
1866
      this->sized_write<64, false>(of);
1867
      break;
1868
#endif
1869
#ifdef HAVE_TARGET_64_BIG
1870
    case Parameters::TARGET_64_BIG:
1871
      this->sized_write<64, true>(of);
1872
      break;
1873
#endif
1874
    default:
1875
      gold_unreachable();
1876
    }
1877
}
1878
 
1879
template<int size, bool big_endian>
1880
void
1881
Output_data_dynamic::sized_write(Output_file* of)
1882
{
1883
  const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1884
 
1885
  const off_t offset = this->offset();
1886
  const off_t oview_size = this->data_size();
1887
  unsigned char* const oview = of->get_output_view(offset, oview_size);
1888
 
1889
  unsigned char* pov = oview;
1890
  for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1891
       p != this->entries_.end();
1892
       ++p)
1893
    {
1894
      p->write<size, big_endian>(pov, this->pool_);
1895
      pov += dyn_size;
1896
    }
1897
 
1898
  gold_assert(pov - oview == oview_size);
1899
 
1900
  of->write_output_view(offset, oview_size, oview);
1901
 
1902
  // We no longer need the dynamic entries.
1903
  this->entries_.clear();
1904
}
1905
 
1906
// Class Output_symtab_xindex.
1907
 
1908
void
1909
Output_symtab_xindex::do_write(Output_file* of)
1910
{
1911
  const off_t offset = this->offset();
1912
  const off_t oview_size = this->data_size();
1913
  unsigned char* const oview = of->get_output_view(offset, oview_size);
1914
 
1915
  memset(oview, 0, oview_size);
1916
 
1917
  if (parameters->target().is_big_endian())
1918
    this->endian_do_write<true>(oview);
1919
  else
1920
    this->endian_do_write<false>(oview);
1921
 
1922
  of->write_output_view(offset, oview_size, oview);
1923
 
1924
  // We no longer need the data.
1925
  this->entries_.clear();
1926
}
1927
 
1928
template<bool big_endian>
1929
void
1930
Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1931
{
1932
  for (Xindex_entries::const_iterator p = this->entries_.begin();
1933
       p != this->entries_.end();
1934
       ++p)
1935
    {
1936
      unsigned int symndx = p->first;
1937
      gold_assert(symndx * 4 < this->data_size());
1938
      elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1939
    }
1940
}
1941
 
1942 159 khays
// Output_fill_debug_info methods.
1943
 
1944
// Return the minimum size needed for a dummy compilation unit header.
1945
 
1946
size_t
1947
Output_fill_debug_info::do_minimum_hole_size() const
1948
{
1949
  // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1950
  // address_size.
1951
  const size_t len = 4 + 2 + 4 + 1;
1952
  // For type units, add type_signature, type_offset.
1953
  if (this->is_debug_types_)
1954
    return len + 8 + 4;
1955
  return len;
1956
}
1957
 
1958
// Write a dummy compilation unit header to fill a hole in the
1959
// .debug_info or .debug_types section.
1960
 
1961
void
1962
Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
1963
{
1964 163 khays
  gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
1965
             static_cast<long>(off), static_cast<long>(len));
1966 159 khays
 
1967
  gold_assert(len >= this->do_minimum_hole_size());
1968
 
1969
  unsigned char* const oview = of->get_output_view(off, len);
1970
  unsigned char* pov = oview;
1971
 
1972
  // Write header fields: unit_length, version, debug_abbrev_offset,
1973
  // address_size.
1974
  if (this->is_big_endian())
1975
    {
1976 163 khays
      elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1977
      elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1978
      elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
1979 159 khays
    }
1980
  else
1981
    {
1982 163 khays
      elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1983
      elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1984
      elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
1985 159 khays
    }
1986
  pov += 4 + 2 + 4;
1987
  *pov++ = 4;
1988
 
1989
  // For type units, the additional header fields -- type_signature,
1990
  // type_offset -- can be filled with zeroes.
1991
 
1992
  // Fill the remainder of the free space with zeroes.  The first
1993
  // zero should tell the consumer there are no DIEs to read in this
1994
  // compilation unit.
1995
  if (pov < oview + len)
1996
    memset(pov, 0, oview + len - pov);
1997
 
1998
  of->write_output_view(off, len, oview);
1999
}
2000
 
2001
// Output_fill_debug_line methods.
2002
 
2003
// Return the minimum size needed for a dummy line number program header.
2004
 
2005
size_t
2006
Output_fill_debug_line::do_minimum_hole_size() const
2007
{
2008
  // Line number program header fields: unit_length, version, header_length,
2009
  // minimum_instruction_length, default_is_stmt, line_base, line_range,
2010
  // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2011
  const size_t len = 4 + 2 + 4 + this->header_length;
2012
  return len;
2013
}
2014
 
2015
// Write a dummy line number program header to fill a hole in the
2016
// .debug_line section.
2017
 
2018
void
2019
Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
2020
{
2021 163 khays
  gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
2022
             static_cast<long>(off), static_cast<long>(len));
2023 159 khays
 
2024
  gold_assert(len >= this->do_minimum_hole_size());
2025
 
2026
  unsigned char* const oview = of->get_output_view(off, len);
2027
  unsigned char* pov = oview;
2028
 
2029
  // Write header fields: unit_length, version, header_length,
2030
  // minimum_instruction_length, default_is_stmt, line_base, line_range,
2031
  // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2032
  // We set the header_length field to cover the entire hole, so the
2033
  // line number program is empty.
2034
  if (this->is_big_endian())
2035
    {
2036 163 khays
      elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2037
      elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2038
      elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
2039 159 khays
    }
2040
  else
2041
    {
2042 163 khays
      elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2043
      elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2044
      elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
2045 159 khays
    }
2046
  pov += 4 + 2 + 4;
2047
  *pov++ = 1;   // minimum_instruction_length
2048
  *pov++ = 0;    // default_is_stmt
2049
  *pov++ = 0;    // line_base
2050
  *pov++ = 5;   // line_range
2051
  *pov++ = 13;  // opcode_base
2052
  *pov++ = 0;    // standard_opcode_lengths[1]
2053
  *pov++ = 1;   // standard_opcode_lengths[2]
2054
  *pov++ = 1;   // standard_opcode_lengths[3]
2055
  *pov++ = 1;   // standard_opcode_lengths[4]
2056
  *pov++ = 1;   // standard_opcode_lengths[5]
2057
  *pov++ = 0;    // standard_opcode_lengths[6]
2058
  *pov++ = 0;    // standard_opcode_lengths[7]
2059
  *pov++ = 0;    // standard_opcode_lengths[8]
2060
  *pov++ = 1;   // standard_opcode_lengths[9]
2061
  *pov++ = 0;    // standard_opcode_lengths[10]
2062
  *pov++ = 0;    // standard_opcode_lengths[11]
2063
  *pov++ = 1;   // standard_opcode_lengths[12]
2064
  *pov++ = 0;    // include_directories (empty)
2065
  *pov++ = 0;    // filenames (empty)
2066
 
2067
  // Some consumers don't check the header_length field, and simply
2068
  // start reading the line number program immediately following the
2069
  // header.  For those consumers, we fill the remainder of the free
2070
  // space with DW_LNS_set_basic_block opcodes.  These are effectively
2071
  // no-ops: the resulting line table program will not create any rows.
2072
  if (pov < oview + len)
2073
    memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2074
 
2075
  of->write_output_view(off, len, oview);
2076
}
2077
 
2078 27 khays
// Output_section::Input_section methods.
2079
 
2080
// Return the current data size.  For an input section we store the size here.
2081
// For an Output_section_data, we have to ask it for the size.
2082
 
2083
off_t
2084
Output_section::Input_section::current_data_size() const
2085
{
2086
  if (this->is_input_section())
2087
    return this->u1_.data_size;
2088
  else
2089
    {
2090
      this->u2_.posd->pre_finalize_data_size();
2091
      return this->u2_.posd->current_data_size();
2092
    }
2093
}
2094
 
2095
// Return the data size.  For an input section we store the size here.
2096
// For an Output_section_data, we have to ask it for the size.
2097
 
2098
off_t
2099
Output_section::Input_section::data_size() const
2100
{
2101
  if (this->is_input_section())
2102
    return this->u1_.data_size;
2103
  else
2104
    return this->u2_.posd->data_size();
2105
}
2106
 
2107
// Return the object for an input section.
2108
 
2109
Relobj*
2110
Output_section::Input_section::relobj() const
2111
{
2112
  if (this->is_input_section())
2113
    return this->u2_.object;
2114
  else if (this->is_merge_section())
2115
    {
2116
      gold_assert(this->u2_.pomb->first_relobj() != NULL);
2117
      return this->u2_.pomb->first_relobj();
2118
    }
2119
  else if (this->is_relaxed_input_section())
2120
    return this->u2_.poris->relobj();
2121
  else
2122
    gold_unreachable();
2123
}
2124
 
2125
// Return the input section index for an input section.
2126
 
2127
unsigned int
2128
Output_section::Input_section::shndx() const
2129
{
2130
  if (this->is_input_section())
2131
    return this->shndx_;
2132
  else if (this->is_merge_section())
2133
    {
2134
      gold_assert(this->u2_.pomb->first_relobj() != NULL);
2135
      return this->u2_.pomb->first_shndx();
2136
    }
2137
  else if (this->is_relaxed_input_section())
2138
    return this->u2_.poris->shndx();
2139
  else
2140
    gold_unreachable();
2141
}
2142
 
2143
// Set the address and file offset.
2144
 
2145
void
2146
Output_section::Input_section::set_address_and_file_offset(
2147
    uint64_t address,
2148
    off_t file_offset,
2149
    off_t section_file_offset)
2150
{
2151
  if (this->is_input_section())
2152
    this->u2_.object->set_section_offset(this->shndx_,
2153
                                         file_offset - section_file_offset);
2154
  else
2155
    this->u2_.posd->set_address_and_file_offset(address, file_offset);
2156
}
2157
 
2158
// Reset the address and file offset.
2159
 
2160
void
2161
Output_section::Input_section::reset_address_and_file_offset()
2162
{
2163
  if (!this->is_input_section())
2164
    this->u2_.posd->reset_address_and_file_offset();
2165
}
2166
 
2167
// Finalize the data size.
2168
 
2169
void
2170
Output_section::Input_section::finalize_data_size()
2171
{
2172
  if (!this->is_input_section())
2173
    this->u2_.posd->finalize_data_size();
2174
}
2175
 
2176
// Try to turn an input offset into an output offset.  We want to
2177
// return the output offset relative to the start of this
2178
// Input_section in the output section.
2179
 
2180
inline bool
2181
Output_section::Input_section::output_offset(
2182
    const Relobj* object,
2183
    unsigned int shndx,
2184
    section_offset_type offset,
2185
    section_offset_type* poutput) const
2186
{
2187
  if (!this->is_input_section())
2188
    return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2189
  else
2190
    {
2191
      if (this->shndx_ != shndx || this->u2_.object != object)
2192
        return false;
2193
      *poutput = offset;
2194
      return true;
2195
    }
2196
}
2197
 
2198
// Return whether this is the merge section for the input section
2199
// SHNDX in OBJECT.
2200
 
2201
inline bool
2202
Output_section::Input_section::is_merge_section_for(const Relobj* object,
2203
                                                    unsigned int shndx) const
2204
{
2205
  if (this->is_input_section())
2206
    return false;
2207
  return this->u2_.posd->is_merge_section_for(object, shndx);
2208
}
2209
 
2210
// Write out the data.  We don't have to do anything for an input
2211
// section--they are handled via Object::relocate--but this is where
2212
// we write out the data for an Output_section_data.
2213
 
2214
void
2215
Output_section::Input_section::write(Output_file* of)
2216
{
2217
  if (!this->is_input_section())
2218
    this->u2_.posd->write(of);
2219
}
2220
 
2221
// Write the data to a buffer.  As for write(), we don't have to do
2222
// anything for an input section.
2223
 
2224
void
2225
Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2226
{
2227
  if (!this->is_input_section())
2228
    this->u2_.posd->write_to_buffer(buffer);
2229
}
2230
 
2231
// Print to a map file.
2232
 
2233
void
2234
Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2235
{
2236
  switch (this->shndx_)
2237
    {
2238
    case OUTPUT_SECTION_CODE:
2239
    case MERGE_DATA_SECTION_CODE:
2240
    case MERGE_STRING_SECTION_CODE:
2241
      this->u2_.posd->print_to_mapfile(mapfile);
2242
      break;
2243
 
2244
    case RELAXED_INPUT_SECTION_CODE:
2245
      {
2246
        Output_relaxed_input_section* relaxed_section =
2247
          this->relaxed_input_section();
2248
        mapfile->print_input_section(relaxed_section->relobj(),
2249
                                     relaxed_section->shndx());
2250
      }
2251
      break;
2252
    default:
2253
      mapfile->print_input_section(this->u2_.object, this->shndx_);
2254
      break;
2255
    }
2256
}
2257
 
2258
// Output_section methods.
2259
 
2260
// Construct an Output_section.  NAME will point into a Stringpool.
2261
 
2262
Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2263
                               elfcpp::Elf_Xword flags)
2264
  : name_(name),
2265
    addralign_(0),
2266
    entsize_(0),
2267
    load_address_(0),
2268
    link_section_(NULL),
2269
    link_(0),
2270
    info_section_(NULL),
2271
    info_symndx_(NULL),
2272
    info_(0),
2273
    type_(type),
2274
    flags_(flags),
2275
    order_(ORDER_INVALID),
2276
    out_shndx_(-1U),
2277
    symtab_index_(0),
2278
    dynsym_index_(0),
2279
    input_sections_(),
2280
    first_input_offset_(0),
2281
    fills_(),
2282
    postprocessing_buffer_(NULL),
2283
    needs_symtab_index_(false),
2284
    needs_dynsym_index_(false),
2285
    should_link_to_symtab_(false),
2286
    should_link_to_dynsym_(false),
2287
    after_input_sections_(false),
2288
    requires_postprocessing_(false),
2289
    found_in_sections_clause_(false),
2290
    has_load_address_(false),
2291
    info_uses_section_index_(false),
2292
    input_section_order_specified_(false),
2293
    may_sort_attached_input_sections_(false),
2294
    must_sort_attached_input_sections_(false),
2295
    attached_input_sections_are_sorted_(false),
2296
    is_relro_(false),
2297
    is_small_section_(false),
2298
    is_large_section_(false),
2299
    generate_code_fills_at_write_(false),
2300
    is_entsize_zero_(false),
2301
    section_offsets_need_adjustment_(false),
2302
    is_noload_(false),
2303
    always_keeps_input_sections_(false),
2304
    has_fixed_layout_(false),
2305 159 khays
    is_patch_space_allowed_(false),
2306 27 khays
    tls_offset_(0),
2307
    checkpoint_(NULL),
2308
    lookup_maps_(new Output_section_lookup_maps),
2309 159 khays
    free_list_(),
2310
    free_space_fill_(NULL),
2311
    patch_space_(0)
2312 27 khays
{
2313
  // An unallocated section has no address.  Forcing this means that
2314
  // we don't need special treatment for symbols defined in debug
2315
  // sections.
2316
  if ((flags & elfcpp::SHF_ALLOC) == 0)
2317
    this->set_address(0);
2318
}
2319
 
2320
Output_section::~Output_section()
2321
{
2322
  delete this->checkpoint_;
2323
}
2324
 
2325
// Set the entry size.
2326
 
2327
void
2328
Output_section::set_entsize(uint64_t v)
2329
{
2330
  if (this->is_entsize_zero_)
2331
    ;
2332
  else if (this->entsize_ == 0)
2333
    this->entsize_ = v;
2334
  else if (this->entsize_ != v)
2335
    {
2336
      this->entsize_ = 0;
2337
      this->is_entsize_zero_ = 1;
2338
    }
2339
}
2340
 
2341
// Add the input section SHNDX, with header SHDR, named SECNAME, in
2342
// OBJECT, to the Output_section.  RELOC_SHNDX is the index of a
2343
// relocation section which applies to this section, or 0 if none, or
2344
// -1U if more than one.  Return the offset of the input section
2345
// within the output section.  Return -1 if the input section will
2346
// receive special handling.  In the normal case we don't always keep
2347
// track of input sections for an Output_section.  Instead, each
2348
// Object keeps track of the Output_section for each of its input
2349
// sections.  However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2350
// track of input sections here; this is used when SECTIONS appears in
2351
// a linker script.
2352
 
2353
template<int size, bool big_endian>
2354
off_t
2355
Output_section::add_input_section(Layout* layout,
2356
                                  Sized_relobj_file<size, big_endian>* object,
2357
                                  unsigned int shndx,
2358
                                  const char* secname,
2359
                                  const elfcpp::Shdr<size, big_endian>& shdr,
2360
                                  unsigned int reloc_shndx,
2361
                                  bool have_sections_script)
2362
{
2363
  elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2364
  if ((addralign & (addralign - 1)) != 0)
2365
    {
2366
      object->error(_("invalid alignment %lu for section \"%s\""),
2367
                    static_cast<unsigned long>(addralign), secname);
2368
      addralign = 1;
2369
    }
2370
 
2371
  if (addralign > this->addralign_)
2372
    this->addralign_ = addralign;
2373
 
2374
  typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2375
  uint64_t entsize = shdr.get_sh_entsize();
2376
 
2377
  // .debug_str is a mergeable string section, but is not always so
2378
  // marked by compilers.  Mark manually here so we can optimize.
2379
  if (strcmp(secname, ".debug_str") == 0)
2380
    {
2381
      sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2382
      entsize = 1;
2383
    }
2384
 
2385
  this->update_flags_for_input_section(sh_flags);
2386
  this->set_entsize(entsize);
2387
 
2388
  // If this is a SHF_MERGE section, we pass all the input sections to
2389
  // a Output_data_merge.  We don't try to handle relocations for such
2390
  // a section.  We don't try to handle empty merge sections--they
2391
  // mess up the mappings, and are useless anyhow.
2392
  // FIXME: Need to handle merge sections during incremental update.
2393
  if ((sh_flags & elfcpp::SHF_MERGE) != 0
2394
      && reloc_shndx == 0
2395
      && shdr.get_sh_size() > 0
2396
      && !parameters->incremental())
2397
    {
2398
      // Keep information about merged input sections for rebuilding fast
2399
      // lookup maps if we have sections-script or we do relaxation.
2400
      bool keeps_input_sections = (this->always_keeps_input_sections_
2401
                                   || have_sections_script
2402
                                   || parameters->target().may_relax());
2403
 
2404
      if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2405
                                        addralign, keeps_input_sections))
2406
        {
2407
          // Tell the relocation routines that they need to call the
2408
          // output_offset method to determine the final address.
2409
          return -1;
2410
        }
2411
    }
2412
 
2413
  section_size_type input_section_size = shdr.get_sh_size();
2414
  section_size_type uncompressed_size;
2415
  if (object->section_is_compressed(shndx, &uncompressed_size))
2416
    input_section_size = uncompressed_size;
2417
 
2418
  off_t offset_in_section;
2419
  off_t aligned_offset_in_section;
2420
  if (this->has_fixed_layout())
2421
    {
2422
      // For incremental updates, find a chunk of unused space in the section.
2423
      offset_in_section = this->free_list_.allocate(input_section_size,
2424
                                                    addralign, 0);
2425
      if (offset_in_section == -1)
2426 159 khays
        gold_fallback(_("out of patch space in section %s; "
2427
                        "relink with --incremental-full"),
2428
                      this->name());
2429 27 khays
      aligned_offset_in_section = offset_in_section;
2430
    }
2431
  else
2432
    {
2433
      offset_in_section = this->current_data_size_for_child();
2434
      aligned_offset_in_section = align_address(offset_in_section,
2435
                                                addralign);
2436
      this->set_current_data_size_for_child(aligned_offset_in_section
2437
                                            + input_section_size);
2438
    }
2439
 
2440
  // Determine if we want to delay code-fill generation until the output
2441
  // section is written.  When the target is relaxing, we want to delay fill
2442
  // generating to avoid adjusting them during relaxation.  Also, if we are
2443
  // sorting input sections we must delay fill generation.
2444
  if (!this->generate_code_fills_at_write_
2445
      && !have_sections_script
2446
      && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2447
      && parameters->target().has_code_fill()
2448
      && (parameters->target().may_relax()
2449 159 khays
          || layout->is_section_ordering_specified()))
2450 27 khays
    {
2451
      gold_assert(this->fills_.empty());
2452
      this->generate_code_fills_at_write_ = true;
2453
    }
2454
 
2455
  if (aligned_offset_in_section > offset_in_section
2456
      && !this->generate_code_fills_at_write_
2457
      && !have_sections_script
2458
      && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2459
      && parameters->target().has_code_fill())
2460
    {
2461
      // We need to add some fill data.  Using fill_list_ when
2462
      // possible is an optimization, since we will often have fill
2463
      // sections without input sections.
2464
      off_t fill_len = aligned_offset_in_section - offset_in_section;
2465
      if (this->input_sections_.empty())
2466
        this->fills_.push_back(Fill(offset_in_section, fill_len));
2467
      else
2468
        {
2469
          std::string fill_data(parameters->target().code_fill(fill_len));
2470
          Output_data_const* odc = new Output_data_const(fill_data, 1);
2471
          this->input_sections_.push_back(Input_section(odc));
2472
        }
2473
    }
2474
 
2475
  // We need to keep track of this section if we are already keeping
2476
  // track of sections, or if we are relaxing.  Also, if this is a
2477
  // section which requires sorting, or which may require sorting in
2478
  // the future, we keep track of the sections.  If the
2479
  // --section-ordering-file option is used to specify the order of
2480
  // sections, we need to keep track of sections.
2481
  if (this->always_keeps_input_sections_
2482
      || have_sections_script
2483
      || !this->input_sections_.empty()
2484
      || this->may_sort_attached_input_sections()
2485
      || this->must_sort_attached_input_sections()
2486
      || parameters->options().user_set_Map()
2487
      || parameters->target().may_relax()
2488 159 khays
      || layout->is_section_ordering_specified())
2489 27 khays
    {
2490
      Input_section isecn(object, shndx, input_section_size, addralign);
2491 163 khays
      /* If section ordering is requested by specifying a ordering file,
2492
         using --section-ordering-file, match the section name with
2493
         a pattern.  */
2494
      if (parameters->options().section_ordering_file())
2495 27 khays
        {
2496
          unsigned int section_order_index =
2497
            layout->find_section_order_index(std::string(secname));
2498
          if (section_order_index != 0)
2499
            {
2500
              isecn.set_section_order_index(section_order_index);
2501
              this->set_input_section_order_specified();
2502
            }
2503
        }
2504
      if (this->has_fixed_layout())
2505
        {
2506
          // For incremental updates, finalize the address and offset now.
2507
          uint64_t addr = this->address();
2508
          isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2509
                                            aligned_offset_in_section,
2510
                                            this->offset());
2511
        }
2512
      this->input_sections_.push_back(isecn);
2513
    }
2514
 
2515
  return aligned_offset_in_section;
2516
}
2517
 
2518
// Add arbitrary data to an output section.
2519
 
2520
void
2521
Output_section::add_output_section_data(Output_section_data* posd)
2522
{
2523
  Input_section inp(posd);
2524
  this->add_output_section_data(&inp);
2525
 
2526
  if (posd->is_data_size_valid())
2527
    {
2528
      off_t offset_in_section;
2529
      if (this->has_fixed_layout())
2530
        {
2531
          // For incremental updates, find a chunk of unused space.
2532
          offset_in_section = this->free_list_.allocate(posd->data_size(),
2533
                                                        posd->addralign(), 0);
2534
          if (offset_in_section == -1)
2535 159 khays
            gold_fallback(_("out of patch space in section %s; "
2536
                            "relink with --incremental-full"),
2537
                          this->name());
2538 27 khays
          // Finalize the address and offset now.
2539
          uint64_t addr = this->address();
2540
          off_t offset = this->offset();
2541
          posd->set_address_and_file_offset(addr + offset_in_section,
2542
                                            offset + offset_in_section);
2543
        }
2544
      else
2545
        {
2546
          offset_in_section = this->current_data_size_for_child();
2547
          off_t aligned_offset_in_section = align_address(offset_in_section,
2548
                                                          posd->addralign());
2549
          this->set_current_data_size_for_child(aligned_offset_in_section
2550
                                                + posd->data_size());
2551
        }
2552
    }
2553
  else if (this->has_fixed_layout())
2554
    {
2555
      // For incremental updates, arrange for the data to have a fixed layout.
2556
      // This will mean that additions to the data must be allocated from
2557
      // free space within the containing output section.
2558
      uint64_t addr = this->address();
2559
      posd->set_address(addr);
2560
      posd->set_file_offset(0);
2561
      // FIXME: This should eventually be unreachable.
2562
      // gold_unreachable();
2563
    }
2564
}
2565
 
2566
// Add a relaxed input section.
2567
 
2568
void
2569
Output_section::add_relaxed_input_section(Layout* layout,
2570
                                          Output_relaxed_input_section* poris,
2571
                                          const std::string& name)
2572
{
2573
  Input_section inp(poris);
2574
 
2575
  // If the --section-ordering-file option is used to specify the order of
2576
  // sections, we need to keep track of sections.
2577 159 khays
  if (layout->is_section_ordering_specified())
2578 27 khays
    {
2579
      unsigned int section_order_index =
2580
        layout->find_section_order_index(name);
2581
      if (section_order_index != 0)
2582
        {
2583
          inp.set_section_order_index(section_order_index);
2584
          this->set_input_section_order_specified();
2585
        }
2586
    }
2587
 
2588
  this->add_output_section_data(&inp);
2589
  if (this->lookup_maps_->is_valid())
2590
    this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2591
                                                  poris->shndx(), poris);
2592
 
2593
  // For a relaxed section, we use the current data size.  Linker scripts
2594
  // get all the input sections, including relaxed one from an output
2595
  // section and add them back to them same output section to compute the
2596
  // output section size.  If we do not account for sizes of relaxed input
2597
  // sections,  an output section would be incorrectly sized.
2598
  off_t offset_in_section = this->current_data_size_for_child();
2599
  off_t aligned_offset_in_section = align_address(offset_in_section,
2600
                                                  poris->addralign());
2601
  this->set_current_data_size_for_child(aligned_offset_in_section
2602
                                        + poris->current_data_size());
2603
}
2604
 
2605
// Add arbitrary data to an output section by Input_section.
2606
 
2607
void
2608
Output_section::add_output_section_data(Input_section* inp)
2609
{
2610
  if (this->input_sections_.empty())
2611
    this->first_input_offset_ = this->current_data_size_for_child();
2612
 
2613
  this->input_sections_.push_back(*inp);
2614
 
2615
  uint64_t addralign = inp->addralign();
2616
  if (addralign > this->addralign_)
2617
    this->addralign_ = addralign;
2618
 
2619
  inp->set_output_section(this);
2620
}
2621
 
2622
// Add a merge section to an output section.
2623
 
2624
void
2625
Output_section::add_output_merge_section(Output_section_data* posd,
2626
                                         bool is_string, uint64_t entsize)
2627
{
2628
  Input_section inp(posd, is_string, entsize);
2629
  this->add_output_section_data(&inp);
2630
}
2631
 
2632
// Add an input section to a SHF_MERGE section.
2633
 
2634
bool
2635
Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2636
                                        uint64_t flags, uint64_t entsize,
2637
                                        uint64_t addralign,
2638
                                        bool keeps_input_sections)
2639
{
2640
  bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2641
 
2642
  // We only merge strings if the alignment is not more than the
2643
  // character size.  This could be handled, but it's unusual.
2644
  if (is_string && addralign > entsize)
2645
    return false;
2646
 
2647
  // We cannot restore merged input section states.
2648
  gold_assert(this->checkpoint_ == NULL);
2649
 
2650
  // Look up merge sections by required properties.
2651
  // Currently, we only invalidate the lookup maps in script processing
2652
  // and relaxation.  We should not have done either when we reach here.
2653
  // So we assume that the lookup maps are valid to simply code.
2654
  gold_assert(this->lookup_maps_->is_valid());
2655
  Merge_section_properties msp(is_string, entsize, addralign);
2656
  Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2657
  bool is_new = false;
2658
  if (pomb != NULL)
2659
    {
2660
      gold_assert(pomb->is_string() == is_string
2661
                  && pomb->entsize() == entsize
2662
                  && pomb->addralign() == addralign);
2663
    }
2664
  else
2665
    {
2666
      // Create a new Output_merge_data or Output_merge_string_data.
2667
      if (!is_string)
2668
        pomb = new Output_merge_data(entsize, addralign);
2669
      else
2670
        {
2671
          switch (entsize)
2672
            {
2673
            case 1:
2674
              pomb = new Output_merge_string<char>(addralign);
2675
              break;
2676
            case 2:
2677
              pomb = new Output_merge_string<uint16_t>(addralign);
2678
              break;
2679
            case 4:
2680
              pomb = new Output_merge_string<uint32_t>(addralign);
2681
              break;
2682
            default:
2683
              return false;
2684
            }
2685
        }
2686
      // If we need to do script processing or relaxation, we need to keep
2687
      // the original input sections to rebuild the fast lookup maps.
2688
      if (keeps_input_sections)
2689
        pomb->set_keeps_input_sections();
2690
      is_new = true;
2691
    }
2692
 
2693
  if (pomb->add_input_section(object, shndx))
2694
    {
2695
      // Add new merge section to this output section and link merge
2696
      // section properties to new merge section in map.
2697
      if (is_new)
2698
        {
2699
          this->add_output_merge_section(pomb, is_string, entsize);
2700
          this->lookup_maps_->add_merge_section(msp, pomb);
2701
        }
2702
 
2703
      // Add input section to new merge section and link input section to new
2704
      // merge section in map.
2705
      this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2706
      return true;
2707
    }
2708
  else
2709
    {
2710
      // If add_input_section failed, delete new merge section to avoid
2711
      // exporting empty merge sections in Output_section::get_input_section.
2712
      if (is_new)
2713
        delete pomb;
2714
      return false;
2715
    }
2716
}
2717
 
2718
// Build a relaxation map to speed up relaxation of existing input sections.
2719
// Look up to the first LIMIT elements in INPUT_SECTIONS.
2720
 
2721
void
2722
Output_section::build_relaxation_map(
2723
  const Input_section_list& input_sections,
2724
  size_t limit,
2725
  Relaxation_map* relaxation_map) const
2726
{
2727
  for (size_t i = 0; i < limit; ++i)
2728
    {
2729
      const Input_section& is(input_sections[i]);
2730
      if (is.is_input_section() || is.is_relaxed_input_section())
2731
        {
2732
          Section_id sid(is.relobj(), is.shndx());
2733
          (*relaxation_map)[sid] = i;
2734
        }
2735
    }
2736
}
2737
 
2738
// Convert regular input sections in INPUT_SECTIONS into relaxed input
2739
// sections in RELAXED_SECTIONS.  MAP is a prebuilt map from section id
2740
// indices of INPUT_SECTIONS.
2741
 
2742
void
2743
Output_section::convert_input_sections_in_list_to_relaxed_sections(
2744
  const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2745
  const Relaxation_map& map,
2746
  Input_section_list* input_sections)
2747
{
2748
  for (size_t i = 0; i < relaxed_sections.size(); ++i)
2749
    {
2750
      Output_relaxed_input_section* poris = relaxed_sections[i];
2751
      Section_id sid(poris->relobj(), poris->shndx());
2752
      Relaxation_map::const_iterator p = map.find(sid);
2753
      gold_assert(p != map.end());
2754
      gold_assert((*input_sections)[p->second].is_input_section());
2755
 
2756
      // Remember section order index of original input section
2757
      // if it is set.  Copy it to the relaxed input section.
2758
      unsigned int soi =
2759
        (*input_sections)[p->second].section_order_index();
2760
      (*input_sections)[p->second] = Input_section(poris);
2761
      (*input_sections)[p->second].set_section_order_index(soi);
2762
    }
2763
}
2764
 
2765
// Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2766
// is a vector of pointers to Output_relaxed_input_section or its derived
2767
// classes.  The relaxed sections must correspond to existing input sections.
2768
 
2769
void
2770
Output_section::convert_input_sections_to_relaxed_sections(
2771
  const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2772
{
2773
  gold_assert(parameters->target().may_relax());
2774
 
2775
  // We want to make sure that restore_states does not undo the effect of
2776
  // this.  If there is no checkpoint active, just search the current
2777
  // input section list and replace the sections there.  If there is
2778
  // a checkpoint, also replace the sections there.
2779
 
2780
  // By default, we look at the whole list.
2781
  size_t limit = this->input_sections_.size();
2782
 
2783
  if (this->checkpoint_ != NULL)
2784
    {
2785
      // Replace input sections with relaxed input section in the saved
2786
      // copy of the input section list.
2787
      if (this->checkpoint_->input_sections_saved())
2788
        {
2789
          Relaxation_map map;
2790
          this->build_relaxation_map(
2791
                    *(this->checkpoint_->input_sections()),
2792
                    this->checkpoint_->input_sections()->size(),
2793
                    &map);
2794
          this->convert_input_sections_in_list_to_relaxed_sections(
2795
                    relaxed_sections,
2796
                    map,
2797
                    this->checkpoint_->input_sections());
2798
        }
2799
      else
2800
        {
2801
          // We have not copied the input section list yet.  Instead, just
2802
          // look at the portion that would be saved.
2803
          limit = this->checkpoint_->input_sections_size();
2804
        }
2805
    }
2806
 
2807
  // Convert input sections in input_section_list.
2808
  Relaxation_map map;
2809
  this->build_relaxation_map(this->input_sections_, limit, &map);
2810
  this->convert_input_sections_in_list_to_relaxed_sections(
2811
            relaxed_sections,
2812
            map,
2813
            &this->input_sections_);
2814
 
2815
  // Update fast look-up map.
2816
  if (this->lookup_maps_->is_valid())
2817
    for (size_t i = 0; i < relaxed_sections.size(); ++i)
2818
      {
2819
        Output_relaxed_input_section* poris = relaxed_sections[i];
2820
        this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2821
                                                      poris->shndx(), poris);
2822
      }
2823
}
2824
 
2825
// Update the output section flags based on input section flags.
2826
 
2827
void
2828
Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2829
{
2830
  // If we created the section with SHF_ALLOC clear, we set the
2831
  // address.  If we are now setting the SHF_ALLOC flag, we need to
2832
  // undo that.
2833
  if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2834
      && (flags & elfcpp::SHF_ALLOC) != 0)
2835
    this->mark_address_invalid();
2836
 
2837
  this->flags_ |= (flags
2838
                   & (elfcpp::SHF_WRITE
2839
                      | elfcpp::SHF_ALLOC
2840
                      | elfcpp::SHF_EXECINSTR));
2841
 
2842
  if ((flags & elfcpp::SHF_MERGE) == 0)
2843
    this->flags_ &=~ elfcpp::SHF_MERGE;
2844
  else
2845
    {
2846
      if (this->current_data_size_for_child() == 0)
2847
        this->flags_ |= elfcpp::SHF_MERGE;
2848
    }
2849
 
2850
  if ((flags & elfcpp::SHF_STRINGS) == 0)
2851
    this->flags_ &=~ elfcpp::SHF_STRINGS;
2852
  else
2853
    {
2854
      if (this->current_data_size_for_child() == 0)
2855
        this->flags_ |= elfcpp::SHF_STRINGS;
2856
    }
2857
}
2858
 
2859
// Find the merge section into which an input section with index SHNDX in
2860
// OBJECT has been added.  Return NULL if none found.
2861
 
2862
Output_section_data*
2863
Output_section::find_merge_section(const Relobj* object,
2864
                                   unsigned int shndx) const
2865
{
2866
  if (!this->lookup_maps_->is_valid())
2867
    this->build_lookup_maps();
2868
  return this->lookup_maps_->find_merge_section(object, shndx);
2869
}
2870
 
2871
// Build the lookup maps for merge and relaxed sections.  This is needs
2872
// to be declared as a const methods so that it is callable with a const
2873
// Output_section pointer.  The method only updates states of the maps.
2874
 
2875
void
2876
Output_section::build_lookup_maps() const
2877
{
2878
  this->lookup_maps_->clear();
2879
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
2880
       p != this->input_sections_.end();
2881
       ++p)
2882
    {
2883
      if (p->is_merge_section())
2884
        {
2885
          Output_merge_base* pomb = p->output_merge_base();
2886
          Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2887
                                       pomb->addralign());
2888
          this->lookup_maps_->add_merge_section(msp, pomb);
2889
          for (Output_merge_base::Input_sections::const_iterator is =
2890
                 pomb->input_sections_begin();
2891
               is != pomb->input_sections_end();
2892
               ++is)
2893
            {
2894
              const Const_section_id& csid = *is;
2895
            this->lookup_maps_->add_merge_input_section(csid.first,
2896
                                                        csid.second, pomb);
2897
            }
2898
 
2899
        }
2900
      else if (p->is_relaxed_input_section())
2901
        {
2902
          Output_relaxed_input_section* poris = p->relaxed_input_section();
2903
          this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2904
                                                        poris->shndx(), poris);
2905
        }
2906
    }
2907
}
2908
 
2909
// Find an relaxed input section corresponding to an input section
2910
// in OBJECT with index SHNDX.
2911
 
2912
const Output_relaxed_input_section*
2913
Output_section::find_relaxed_input_section(const Relobj* object,
2914
                                           unsigned int shndx) const
2915
{
2916
  if (!this->lookup_maps_->is_valid())
2917
    this->build_lookup_maps();
2918
  return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2919
}
2920
 
2921
// Given an address OFFSET relative to the start of input section
2922
// SHNDX in OBJECT, return whether this address is being included in
2923
// the final link.  This should only be called if SHNDX in OBJECT has
2924
// a special mapping.
2925
 
2926
bool
2927
Output_section::is_input_address_mapped(const Relobj* object,
2928
                                        unsigned int shndx,
2929
                                        off_t offset) const
2930
{
2931
  // Look at the Output_section_data_maps first.
2932
  const Output_section_data* posd = this->find_merge_section(object, shndx);
2933
  if (posd == NULL)
2934
    posd = this->find_relaxed_input_section(object, shndx);
2935
 
2936
  if (posd != NULL)
2937
    {
2938
      section_offset_type output_offset;
2939
      bool found = posd->output_offset(object, shndx, offset, &output_offset);
2940
      gold_assert(found);
2941
      return output_offset != -1;
2942
    }
2943
 
2944
  // Fall back to the slow look-up.
2945
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
2946
       p != this->input_sections_.end();
2947
       ++p)
2948
    {
2949
      section_offset_type output_offset;
2950
      if (p->output_offset(object, shndx, offset, &output_offset))
2951
        return output_offset != -1;
2952
    }
2953
 
2954
  // By default we assume that the address is mapped.  This should
2955
  // only be called after we have passed all sections to Layout.  At
2956
  // that point we should know what we are discarding.
2957
  return true;
2958
}
2959
 
2960
// Given an address OFFSET relative to the start of input section
2961
// SHNDX in object OBJECT, return the output offset relative to the
2962
// start of the input section in the output section.  This should only
2963
// be called if SHNDX in OBJECT has a special mapping.
2964
 
2965
section_offset_type
2966
Output_section::output_offset(const Relobj* object, unsigned int shndx,
2967
                              section_offset_type offset) const
2968
{
2969
  // This can only be called meaningfully when we know the data size
2970
  // of this.
2971
  gold_assert(this->is_data_size_valid());
2972
 
2973
  // Look at the Output_section_data_maps first.
2974
  const Output_section_data* posd = this->find_merge_section(object, shndx);
2975
  if (posd == NULL)
2976
    posd = this->find_relaxed_input_section(object, shndx);
2977
  if (posd != NULL)
2978
    {
2979
      section_offset_type output_offset;
2980
      bool found = posd->output_offset(object, shndx, offset, &output_offset);
2981
      gold_assert(found);
2982
      return output_offset;
2983
    }
2984
 
2985
  // Fall back to the slow look-up.
2986
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
2987
       p != this->input_sections_.end();
2988
       ++p)
2989
    {
2990
      section_offset_type output_offset;
2991
      if (p->output_offset(object, shndx, offset, &output_offset))
2992
        return output_offset;
2993
    }
2994
  gold_unreachable();
2995
}
2996
 
2997
// Return the output virtual address of OFFSET relative to the start
2998
// of input section SHNDX in object OBJECT.
2999
 
3000
uint64_t
3001
Output_section::output_address(const Relobj* object, unsigned int shndx,
3002
                               off_t offset) const
3003
{
3004
  uint64_t addr = this->address() + this->first_input_offset_;
3005
 
3006
  // Look at the Output_section_data_maps first.
3007
  const Output_section_data* posd = this->find_merge_section(object, shndx);
3008
  if (posd == NULL)
3009
    posd = this->find_relaxed_input_section(object, shndx);
3010
  if (posd != NULL && posd->is_address_valid())
3011
    {
3012
      section_offset_type output_offset;
3013
      bool found = posd->output_offset(object, shndx, offset, &output_offset);
3014
      gold_assert(found);
3015
      return posd->address() + output_offset;
3016
    }
3017
 
3018
  // Fall back to the slow look-up.
3019
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
3020
       p != this->input_sections_.end();
3021
       ++p)
3022
    {
3023
      addr = align_address(addr, p->addralign());
3024
      section_offset_type output_offset;
3025
      if (p->output_offset(object, shndx, offset, &output_offset))
3026
        {
3027
          if (output_offset == -1)
3028
            return -1ULL;
3029
          return addr + output_offset;
3030
        }
3031
      addr += p->data_size();
3032
    }
3033
 
3034
  // If we get here, it means that we don't know the mapping for this
3035
  // input section.  This might happen in principle if
3036
  // add_input_section were called before add_output_section_data.
3037
  // But it should never actually happen.
3038
 
3039
  gold_unreachable();
3040
}
3041
 
3042
// Find the output address of the start of the merged section for
3043
// input section SHNDX in object OBJECT.
3044
 
3045
bool
3046
Output_section::find_starting_output_address(const Relobj* object,
3047
                                             unsigned int shndx,
3048
                                             uint64_t* paddr) const
3049
{
3050
  // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3051
  // Looking up the merge section map does not always work as we sometimes
3052
  // find a merge section without its address set.
3053
  uint64_t addr = this->address() + this->first_input_offset_;
3054
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
3055
       p != this->input_sections_.end();
3056
       ++p)
3057
    {
3058
      addr = align_address(addr, p->addralign());
3059
 
3060
      // It would be nice if we could use the existing output_offset
3061
      // method to get the output offset of input offset 0.
3062
      // Unfortunately we don't know for sure that input offset 0 is
3063
      // mapped at all.
3064
      if (p->is_merge_section_for(object, shndx))
3065
        {
3066
          *paddr = addr;
3067
          return true;
3068
        }
3069
 
3070
      addr += p->data_size();
3071
    }
3072
 
3073
  // We couldn't find a merge output section for this input section.
3074
  return false;
3075
}
3076
 
3077
// Update the data size of an Output_section.
3078
 
3079
void
3080
Output_section::update_data_size()
3081
{
3082
  if (this->input_sections_.empty())
3083
      return;
3084
 
3085
  if (this->must_sort_attached_input_sections()
3086
      || this->input_section_order_specified())
3087
    this->sort_attached_input_sections();
3088
 
3089
  off_t off = this->first_input_offset_;
3090
  for (Input_section_list::iterator p = this->input_sections_.begin();
3091
       p != this->input_sections_.end();
3092
       ++p)
3093
    {
3094
      off = align_address(off, p->addralign());
3095
      off += p->current_data_size();
3096
    }
3097
 
3098
  this->set_current_data_size_for_child(off);
3099
}
3100
 
3101
// Set the data size of an Output_section.  This is where we handle
3102
// setting the addresses of any Output_section_data objects.
3103
 
3104
void
3105
Output_section::set_final_data_size()
3106
{
3107 159 khays
  off_t data_size;
3108
 
3109 27 khays
  if (this->input_sections_.empty())
3110 159 khays
    data_size = this->current_data_size_for_child();
3111
  else
3112 27 khays
    {
3113 159 khays
      if (this->must_sort_attached_input_sections()
3114
          || this->input_section_order_specified())
3115
        this->sort_attached_input_sections();
3116
 
3117
      uint64_t address = this->address();
3118
      off_t startoff = this->offset();
3119
      off_t off = startoff + this->first_input_offset_;
3120
      for (Input_section_list::iterator p = this->input_sections_.begin();
3121
           p != this->input_sections_.end();
3122
           ++p)
3123
        {
3124
          off = align_address(off, p->addralign());
3125
          p->set_address_and_file_offset(address + (off - startoff), off,
3126
                                         startoff);
3127
          off += p->data_size();
3128
        }
3129
      data_size = off - startoff;
3130 27 khays
    }
3131
 
3132 159 khays
  // For full incremental links, we want to allocate some patch space
3133
  // in most sections for subsequent incremental updates.
3134
  if (this->is_patch_space_allowed_ && parameters->incremental_full())
3135 27 khays
    {
3136 159 khays
      double pct = parameters->options().incremental_patch();
3137
      size_t extra = static_cast<size_t>(data_size * pct);
3138
      if (this->free_space_fill_ != NULL
3139
          && this->free_space_fill_->minimum_hole_size() > extra)
3140
        extra = this->free_space_fill_->minimum_hole_size();
3141
      off_t new_size = align_address(data_size + extra, this->addralign());
3142
      this->patch_space_ = new_size - data_size;
3143
      gold_debug(DEBUG_INCREMENTAL,
3144
                 "set_final_data_size: %08lx + %08lx: section %s",
3145
                 static_cast<long>(data_size),
3146
                 static_cast<long>(this->patch_space_),
3147
                 this->name());
3148
      data_size = new_size;
3149 27 khays
    }
3150
 
3151 159 khays
  this->set_data_size(data_size);
3152 27 khays
}
3153
 
3154
// Reset the address and file offset.
3155
 
3156
void
3157
Output_section::do_reset_address_and_file_offset()
3158
{
3159
  // An unallocated section has no address.  Forcing this means that
3160
  // we don't need special treatment for symbols defined in debug
3161
  // sections.  We do the same in the constructor.  This does not
3162
  // apply to NOLOAD sections though.
3163
  if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
3164
     this->set_address(0);
3165
 
3166
  for (Input_section_list::iterator p = this->input_sections_.begin();
3167
       p != this->input_sections_.end();
3168
       ++p)
3169
    p->reset_address_and_file_offset();
3170 159 khays
 
3171
  // Remove any patch space that was added in set_final_data_size.
3172
  if (this->patch_space_ > 0)
3173
    {
3174
      this->set_current_data_size_for_child(this->current_data_size_for_child()
3175
                                            - this->patch_space_);
3176
      this->patch_space_ = 0;
3177
    }
3178 27 khays
}
3179 159 khays
 
3180 27 khays
// Return true if address and file offset have the values after reset.
3181
 
3182
bool
3183
Output_section::do_address_and_file_offset_have_reset_values() const
3184
{
3185
  if (this->is_offset_valid())
3186
    return false;
3187
 
3188
  // An unallocated section has address 0 after its construction or a reset.
3189
  if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3190
    return this->is_address_valid() && this->address() == 0;
3191
  else
3192
    return !this->is_address_valid();
3193
}
3194
 
3195
// Set the TLS offset.  Called only for SHT_TLS sections.
3196
 
3197
void
3198
Output_section::do_set_tls_offset(uint64_t tls_base)
3199
{
3200
  this->tls_offset_ = this->address() - tls_base;
3201
}
3202
 
3203
// In a few cases we need to sort the input sections attached to an
3204
// output section.  This is used to implement the type of constructor
3205
// priority ordering implemented by the GNU linker, in which the
3206
// priority becomes part of the section name and the sections are
3207
// sorted by name.  We only do this for an output section if we see an
3208 159 khays
// attached input section matching ".ctors.*", ".dtors.*",
3209 27 khays
// ".init_array.*" or ".fini_array.*".
3210
 
3211
class Output_section::Input_section_sort_entry
3212
{
3213
 public:
3214
  Input_section_sort_entry()
3215
    : input_section_(), index_(-1U), section_has_name_(false),
3216
      section_name_()
3217
  { }
3218
 
3219
  Input_section_sort_entry(const Input_section& input_section,
3220
                           unsigned int index,
3221
                           bool must_sort_attached_input_sections)
3222
    : input_section_(input_section), index_(index),
3223
      section_has_name_(input_section.is_input_section()
3224
                        || input_section.is_relaxed_input_section())
3225
  {
3226
    if (this->section_has_name_
3227
        && must_sort_attached_input_sections)
3228
      {
3229
        // This is only called single-threaded from Layout::finalize,
3230
        // so it is OK to lock.  Unfortunately we have no way to pass
3231
        // in a Task token.
3232
        const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3233
        Object* obj = (input_section.is_input_section()
3234
                       ? input_section.relobj()
3235
                       : input_section.relaxed_input_section()->relobj());
3236
        Task_lock_obj<Object> tl(dummy_task, obj);
3237
 
3238
        // This is a slow operation, which should be cached in
3239
        // Layout::layout if this becomes a speed problem.
3240
        this->section_name_ = obj->section_name(input_section.shndx());
3241
      }
3242
  }
3243
 
3244
  // Return the Input_section.
3245
  const Input_section&
3246
  input_section() const
3247
  {
3248
    gold_assert(this->index_ != -1U);
3249
    return this->input_section_;
3250
  }
3251
 
3252
  // The index of this entry in the original list.  This is used to
3253
  // make the sort stable.
3254
  unsigned int
3255
  index() const
3256
  {
3257
    gold_assert(this->index_ != -1U);
3258
    return this->index_;
3259
  }
3260
 
3261
  // Whether there is a section name.
3262
  bool
3263
  section_has_name() const
3264
  { return this->section_has_name_; }
3265
 
3266
  // The section name.
3267
  const std::string&
3268
  section_name() const
3269
  {
3270
    gold_assert(this->section_has_name_);
3271
    return this->section_name_;
3272
  }
3273
 
3274
  // Return true if the section name has a priority.  This is assumed
3275
  // to be true if it has a dot after the initial dot.
3276
  bool
3277
  has_priority() const
3278
  {
3279
    gold_assert(this->section_has_name_);
3280
    return this->section_name_.find('.', 1) != std::string::npos;
3281
  }
3282
 
3283 159 khays
  // Return the priority.  Believe it or not, gcc encodes the priority
3284
  // differently for .ctors/.dtors and .init_array/.fini_array
3285
  // sections.
3286
  unsigned int
3287
  get_priority() const
3288
  {
3289
    gold_assert(this->section_has_name_);
3290
    bool is_ctors;
3291
    if (is_prefix_of(".ctors.", this->section_name_.c_str())
3292
        || is_prefix_of(".dtors.", this->section_name_.c_str()))
3293
      is_ctors = true;
3294
    else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3295
             || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3296
      is_ctors = false;
3297
    else
3298
      return 0;
3299
    char* end;
3300
    unsigned long prio = strtoul((this->section_name_.c_str()
3301
                                  + (is_ctors ? 7 : 12)),
3302
                                 &end, 10);
3303
    if (*end != '\0')
3304
      return 0;
3305
    else if (is_ctors)
3306
      return 65535 - prio;
3307
    else
3308
      return prio;
3309
  }
3310
 
3311 27 khays
  // Return true if this an input file whose base name matches
3312
  // FILE_NAME.  The base name must have an extension of ".o", and
3313
  // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3314
  // This is to match crtbegin.o as well as crtbeginS.o without
3315
  // getting confused by other possibilities.  Overall matching the
3316
  // file name this way is a dreadful hack, but the GNU linker does it
3317
  // in order to better support gcc, and we need to be compatible.
3318
  bool
3319 159 khays
  match_file_name(const char* file_name) const
3320
  { return Layout::match_file_name(this->input_section_.relobj(), file_name); }
3321 27 khays
 
3322
  // Returns 1 if THIS should appear before S in section order, -1 if S
3323
  // appears before THIS and 0 if they are not comparable.
3324
  int
3325
  compare_section_ordering(const Input_section_sort_entry& s) const
3326
  {
3327
    unsigned int this_secn_index = this->input_section_.section_order_index();
3328
    unsigned int s_secn_index = s.input_section().section_order_index();
3329
    if (this_secn_index > 0 && s_secn_index > 0)
3330
      {
3331
        if (this_secn_index < s_secn_index)
3332
          return 1;
3333
        else if (this_secn_index > s_secn_index)
3334
          return -1;
3335
      }
3336
    return 0;
3337
  }
3338
 
3339
 private:
3340
  // The Input_section we are sorting.
3341
  Input_section input_section_;
3342
  // The index of this Input_section in the original list.
3343
  unsigned int index_;
3344
  // Whether this Input_section has a section name--it won't if this
3345
  // is some random Output_section_data.
3346
  bool section_has_name_;
3347
  // The section name if there is one.
3348
  std::string section_name_;
3349
};
3350
 
3351
// Return true if S1 should come before S2 in the output section.
3352
 
3353
bool
3354
Output_section::Input_section_sort_compare::operator()(
3355
    const Output_section::Input_section_sort_entry& s1,
3356
    const Output_section::Input_section_sort_entry& s2) const
3357
{
3358
  // crtbegin.o must come first.
3359
  bool s1_begin = s1.match_file_name("crtbegin");
3360
  bool s2_begin = s2.match_file_name("crtbegin");
3361
  if (s1_begin || s2_begin)
3362
    {
3363
      if (!s1_begin)
3364
        return false;
3365
      if (!s2_begin)
3366
        return true;
3367
      return s1.index() < s2.index();
3368
    }
3369
 
3370
  // crtend.o must come last.
3371
  bool s1_end = s1.match_file_name("crtend");
3372
  bool s2_end = s2.match_file_name("crtend");
3373
  if (s1_end || s2_end)
3374
    {
3375
      if (!s1_end)
3376
        return true;
3377
      if (!s2_end)
3378
        return false;
3379
      return s1.index() < s2.index();
3380
    }
3381
 
3382
  // We sort all the sections with no names to the end.
3383
  if (!s1.section_has_name() || !s2.section_has_name())
3384
    {
3385
      if (s1.section_has_name())
3386
        return true;
3387
      if (s2.section_has_name())
3388
        return false;
3389
      return s1.index() < s2.index();
3390
    }
3391
 
3392
  // A section with a priority follows a section without a priority.
3393
  bool s1_has_priority = s1.has_priority();
3394
  bool s2_has_priority = s2.has_priority();
3395
  if (s1_has_priority && !s2_has_priority)
3396
    return false;
3397
  if (!s1_has_priority && s2_has_priority)
3398
    return true;
3399
 
3400
  // Check if a section order exists for these sections through a section
3401
  // ordering file.  If sequence_num is 0, an order does not exist.
3402
  int sequence_num = s1.compare_section_ordering(s2);
3403
  if (sequence_num != 0)
3404
    return sequence_num == 1;
3405
 
3406
  // Otherwise we sort by name.
3407
  int compare = s1.section_name().compare(s2.section_name());
3408
  if (compare != 0)
3409
    return compare < 0;
3410
 
3411
  // Otherwise we keep the input order.
3412
  return s1.index() < s2.index();
3413
}
3414
 
3415
// Return true if S1 should come before S2 in an .init_array or .fini_array
3416
// output section.
3417
 
3418
bool
3419
Output_section::Input_section_sort_init_fini_compare::operator()(
3420
    const Output_section::Input_section_sort_entry& s1,
3421
    const Output_section::Input_section_sort_entry& s2) const
3422
{
3423
  // We sort all the sections with no names to the end.
3424
  if (!s1.section_has_name() || !s2.section_has_name())
3425
    {
3426
      if (s1.section_has_name())
3427
        return true;
3428
      if (s2.section_has_name())
3429
        return false;
3430
      return s1.index() < s2.index();
3431
    }
3432
 
3433
  // A section without a priority follows a section with a priority.
3434
  // This is the reverse of .ctors and .dtors sections.
3435
  bool s1_has_priority = s1.has_priority();
3436
  bool s2_has_priority = s2.has_priority();
3437
  if (s1_has_priority && !s2_has_priority)
3438
    return true;
3439
  if (!s1_has_priority && s2_has_priority)
3440
    return false;
3441
 
3442 159 khays
  // .ctors and .dtors sections without priority come after
3443
  // .init_array and .fini_array sections without priority.
3444
  if (!s1_has_priority
3445
      && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3446
      && s1.section_name() != s2.section_name())
3447
    return false;
3448
  if (!s2_has_priority
3449
      && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3450
      && s2.section_name() != s1.section_name())
3451
    return true;
3452
 
3453
  // Sort by priority if we can.
3454
  if (s1_has_priority)
3455
    {
3456
      unsigned int s1_prio = s1.get_priority();
3457
      unsigned int s2_prio = s2.get_priority();
3458
      if (s1_prio < s2_prio)
3459
        return true;
3460
      else if (s1_prio > s2_prio)
3461
        return false;
3462
    }
3463
 
3464 27 khays
  // Check if a section order exists for these sections through a section
3465
  // ordering file.  If sequence_num is 0, an order does not exist.
3466
  int sequence_num = s1.compare_section_ordering(s2);
3467
  if (sequence_num != 0)
3468
    return sequence_num == 1;
3469
 
3470
  // Otherwise we sort by name.
3471
  int compare = s1.section_name().compare(s2.section_name());
3472
  if (compare != 0)
3473
    return compare < 0;
3474
 
3475
  // Otherwise we keep the input order.
3476
  return s1.index() < s2.index();
3477
}
3478
 
3479
// Return true if S1 should come before S2.  Sections that do not match
3480
// any pattern in the section ordering file are placed ahead of the sections
3481
// that match some pattern.
3482
 
3483
bool
3484
Output_section::Input_section_sort_section_order_index_compare::operator()(
3485
    const Output_section::Input_section_sort_entry& s1,
3486
    const Output_section::Input_section_sort_entry& s2) const
3487
{
3488
  unsigned int s1_secn_index = s1.input_section().section_order_index();
3489
  unsigned int s2_secn_index = s2.input_section().section_order_index();
3490
 
3491
  // Keep input order if section ordering cannot determine order.
3492
  if (s1_secn_index == s2_secn_index)
3493
    return s1.index() < s2.index();
3494
 
3495
  return s1_secn_index < s2_secn_index;
3496
}
3497
 
3498 159 khays
// This updates the section order index of input sections according to the
3499
// the order specified in the mapping from Section id to order index.
3500
 
3501
void
3502
Output_section::update_section_layout(
3503 163 khays
  const Section_layout_order* order_map)
3504 159 khays
{
3505
  for (Input_section_list::iterator p = this->input_sections_.begin();
3506
       p != this->input_sections_.end();
3507
       ++p)
3508
    {
3509
      if (p->is_input_section()
3510
          || p->is_relaxed_input_section())
3511
        {
3512
          Object* obj = (p->is_input_section()
3513
                         ? p->relobj()
3514
                         : p->relaxed_input_section()->relobj());
3515
          unsigned int shndx = p->shndx();
3516
          Section_layout_order::const_iterator it
3517 163 khays
            = order_map->find(Section_id(obj, shndx));
3518
          if (it == order_map->end())
3519 159 khays
            continue;
3520
          unsigned int section_order_index = it->second;
3521
          if (section_order_index != 0)
3522
            {
3523
              p->set_section_order_index(section_order_index);
3524
              this->set_input_section_order_specified();
3525
            }
3526
        }
3527
    }
3528
}
3529
 
3530 27 khays
// Sort the input sections attached to an output section.
3531
 
3532
void
3533
Output_section::sort_attached_input_sections()
3534
{
3535
  if (this->attached_input_sections_are_sorted_)
3536
    return;
3537
 
3538
  if (this->checkpoint_ != NULL
3539
      && !this->checkpoint_->input_sections_saved())
3540
    this->checkpoint_->save_input_sections();
3541
 
3542
  // The only thing we know about an input section is the object and
3543
  // the section index.  We need the section name.  Recomputing this
3544
  // is slow but this is an unusual case.  If this becomes a speed
3545
  // problem we can cache the names as required in Layout::layout.
3546
 
3547
  // We start by building a larger vector holding a copy of each
3548
  // Input_section, plus its current index in the list and its name.
3549
  std::vector<Input_section_sort_entry> sort_list;
3550
 
3551
  unsigned int i = 0;
3552
  for (Input_section_list::iterator p = this->input_sections_.begin();
3553
       p != this->input_sections_.end();
3554
       ++p, ++i)
3555
      sort_list.push_back(Input_section_sort_entry(*p, i,
3556
                            this->must_sort_attached_input_sections()));
3557
 
3558
  // Sort the input sections.
3559
  if (this->must_sort_attached_input_sections())
3560
    {
3561
      if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3562
          || this->type() == elfcpp::SHT_INIT_ARRAY
3563
          || this->type() == elfcpp::SHT_FINI_ARRAY)
3564
        std::sort(sort_list.begin(), sort_list.end(),
3565
                  Input_section_sort_init_fini_compare());
3566
      else
3567
        std::sort(sort_list.begin(), sort_list.end(),
3568
                  Input_section_sort_compare());
3569
    }
3570
  else
3571
    {
3572 159 khays
      gold_assert(this->input_section_order_specified());
3573 27 khays
      std::sort(sort_list.begin(), sort_list.end(),
3574
                Input_section_sort_section_order_index_compare());
3575
    }
3576
 
3577
  // Copy the sorted input sections back to our list.
3578
  this->input_sections_.clear();
3579
  for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3580
       p != sort_list.end();
3581
       ++p)
3582
    this->input_sections_.push_back(p->input_section());
3583
  sort_list.clear();
3584
 
3585
  // Remember that we sorted the input sections, since we might get
3586
  // called again.
3587
  this->attached_input_sections_are_sorted_ = true;
3588
}
3589
 
3590
// Write the section header to *OSHDR.
3591
 
3592
template<int size, bool big_endian>
3593
void
3594
Output_section::write_header(const Layout* layout,
3595
                             const Stringpool* secnamepool,
3596
                             elfcpp::Shdr_write<size, big_endian>* oshdr) const
3597
{
3598
  oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3599
  oshdr->put_sh_type(this->type_);
3600
 
3601
  elfcpp::Elf_Xword flags = this->flags_;
3602
  if (this->info_section_ != NULL && this->info_uses_section_index_)
3603
    flags |= elfcpp::SHF_INFO_LINK;
3604
  oshdr->put_sh_flags(flags);
3605
 
3606
  oshdr->put_sh_addr(this->address());
3607
  oshdr->put_sh_offset(this->offset());
3608
  oshdr->put_sh_size(this->data_size());
3609
  if (this->link_section_ != NULL)
3610
    oshdr->put_sh_link(this->link_section_->out_shndx());
3611
  else if (this->should_link_to_symtab_)
3612 159 khays
    oshdr->put_sh_link(layout->symtab_section_shndx());
3613 27 khays
  else if (this->should_link_to_dynsym_)
3614
    oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3615
  else
3616
    oshdr->put_sh_link(this->link_);
3617
 
3618
  elfcpp::Elf_Word info;
3619
  if (this->info_section_ != NULL)
3620
    {
3621
      if (this->info_uses_section_index_)
3622
        info = this->info_section_->out_shndx();
3623
      else
3624
        info = this->info_section_->symtab_index();
3625
    }
3626
  else if (this->info_symndx_ != NULL)
3627
    info = this->info_symndx_->symtab_index();
3628
  else
3629
    info = this->info_;
3630
  oshdr->put_sh_info(info);
3631
 
3632
  oshdr->put_sh_addralign(this->addralign_);
3633
  oshdr->put_sh_entsize(this->entsize_);
3634
}
3635
 
3636
// Write out the data.  For input sections the data is written out by
3637
// Object::relocate, but we have to handle Output_section_data objects
3638
// here.
3639
 
3640
void
3641
Output_section::do_write(Output_file* of)
3642
{
3643
  gold_assert(!this->requires_postprocessing());
3644
 
3645
  // If the target performs relaxation, we delay filler generation until now.
3646
  gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3647
 
3648
  off_t output_section_file_offset = this->offset();
3649
  for (Fill_list::iterator p = this->fills_.begin();
3650
       p != this->fills_.end();
3651
       ++p)
3652
    {
3653
      std::string fill_data(parameters->target().code_fill(p->length()));
3654
      of->write(output_section_file_offset + p->section_offset(),
3655
                fill_data.data(), fill_data.size());
3656
    }
3657
 
3658
  off_t off = this->offset() + this->first_input_offset_;
3659
  for (Input_section_list::iterator p = this->input_sections_.begin();
3660
       p != this->input_sections_.end();
3661
       ++p)
3662
    {
3663
      off_t aligned_off = align_address(off, p->addralign());
3664
      if (this->generate_code_fills_at_write_ && (off != aligned_off))
3665
        {
3666
          size_t fill_len = aligned_off - off;
3667
          std::string fill_data(parameters->target().code_fill(fill_len));
3668
          of->write(off, fill_data.data(), fill_data.size());
3669
        }
3670
 
3671
      p->write(of);
3672
      off = aligned_off + p->data_size();
3673
    }
3674 159 khays
 
3675
  // For incremental links, fill in unused chunks in debug sections
3676
  // with dummy compilation unit headers.
3677
  if (this->free_space_fill_ != NULL)
3678
    {
3679
      for (Free_list::Const_iterator p = this->free_list_.begin();
3680
           p != this->free_list_.end();
3681
           ++p)
3682
        {
3683
          off_t off = p->start_;
3684
          size_t len = p->end_ - off;
3685
          this->free_space_fill_->write(of, this->offset() + off, len);
3686
        }
3687
      if (this->patch_space_ > 0)
3688
        {
3689
          off_t off = this->current_data_size_for_child() - this->patch_space_;
3690
          this->free_space_fill_->write(of, this->offset() + off,
3691
                                        this->patch_space_);
3692
        }
3693
    }
3694 27 khays
}
3695
 
3696
// If a section requires postprocessing, create the buffer to use.
3697
 
3698
void
3699
Output_section::create_postprocessing_buffer()
3700
{
3701
  gold_assert(this->requires_postprocessing());
3702
 
3703
  if (this->postprocessing_buffer_ != NULL)
3704
    return;
3705
 
3706
  if (!this->input_sections_.empty())
3707
    {
3708
      off_t off = this->first_input_offset_;
3709
      for (Input_section_list::iterator p = this->input_sections_.begin();
3710
           p != this->input_sections_.end();
3711
           ++p)
3712
        {
3713
          off = align_address(off, p->addralign());
3714
          p->finalize_data_size();
3715
          off += p->data_size();
3716
        }
3717
      this->set_current_data_size_for_child(off);
3718
    }
3719
 
3720
  off_t buffer_size = this->current_data_size_for_child();
3721
  this->postprocessing_buffer_ = new unsigned char[buffer_size];
3722
}
3723
 
3724
// Write all the data of an Output_section into the postprocessing
3725
// buffer.  This is used for sections which require postprocessing,
3726
// such as compression.  Input sections are handled by
3727
// Object::Relocate.
3728
 
3729
void
3730
Output_section::write_to_postprocessing_buffer()
3731
{
3732
  gold_assert(this->requires_postprocessing());
3733
 
3734
  // If the target performs relaxation, we delay filler generation until now.
3735
  gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3736
 
3737
  unsigned char* buffer = this->postprocessing_buffer();
3738
  for (Fill_list::iterator p = this->fills_.begin();
3739
       p != this->fills_.end();
3740
       ++p)
3741
    {
3742
      std::string fill_data(parameters->target().code_fill(p->length()));
3743
      memcpy(buffer + p->section_offset(), fill_data.data(),
3744
             fill_data.size());
3745
    }
3746
 
3747
  off_t off = this->first_input_offset_;
3748
  for (Input_section_list::iterator p = this->input_sections_.begin();
3749
       p != this->input_sections_.end();
3750
       ++p)
3751
    {
3752
      off_t aligned_off = align_address(off, p->addralign());
3753
      if (this->generate_code_fills_at_write_ && (off != aligned_off))
3754
        {
3755
          size_t fill_len = aligned_off - off;
3756
          std::string fill_data(parameters->target().code_fill(fill_len));
3757
          memcpy(buffer + off, fill_data.data(), fill_data.size());
3758
        }
3759
 
3760
      p->write_to_buffer(buffer + aligned_off);
3761
      off = aligned_off + p->data_size();
3762
    }
3763
}
3764
 
3765
// Get the input sections for linker script processing.  We leave
3766
// behind the Output_section_data entries.  Note that this may be
3767
// slightly incorrect for merge sections.  We will leave them behind,
3768
// but it is possible that the script says that they should follow
3769
// some other input sections, as in:
3770
//    .rodata { *(.rodata) *(.rodata.cst*) }
3771
// For that matter, we don't handle this correctly:
3772
//    .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3773
// With luck this will never matter.
3774
 
3775
uint64_t
3776
Output_section::get_input_sections(
3777
    uint64_t address,
3778
    const std::string& fill,
3779
    std::list<Input_section>* input_sections)
3780
{
3781
  if (this->checkpoint_ != NULL
3782
      && !this->checkpoint_->input_sections_saved())
3783
    this->checkpoint_->save_input_sections();
3784
 
3785
  // Invalidate fast look-up maps.
3786
  this->lookup_maps_->invalidate();
3787
 
3788
  uint64_t orig_address = address;
3789
 
3790
  address = align_address(address, this->addralign());
3791
 
3792
  Input_section_list remaining;
3793
  for (Input_section_list::iterator p = this->input_sections_.begin();
3794
       p != this->input_sections_.end();
3795
       ++p)
3796
    {
3797
      if (p->is_input_section()
3798
          || p->is_relaxed_input_section()
3799
          || p->is_merge_section())
3800
        input_sections->push_back(*p);
3801
      else
3802
        {
3803
          uint64_t aligned_address = align_address(address, p->addralign());
3804
          if (aligned_address != address && !fill.empty())
3805
            {
3806
              section_size_type length =
3807
                convert_to_section_size_type(aligned_address - address);
3808
              std::string this_fill;
3809
              this_fill.reserve(length);
3810
              while (this_fill.length() + fill.length() <= length)
3811
                this_fill += fill;
3812
              if (this_fill.length() < length)
3813
                this_fill.append(fill, 0, length - this_fill.length());
3814
 
3815
              Output_section_data* posd = new Output_data_const(this_fill, 0);
3816
              remaining.push_back(Input_section(posd));
3817
            }
3818
          address = aligned_address;
3819
 
3820
          remaining.push_back(*p);
3821
 
3822
          p->finalize_data_size();
3823
          address += p->data_size();
3824
        }
3825
    }
3826
 
3827
  this->input_sections_.swap(remaining);
3828
  this->first_input_offset_ = 0;
3829
 
3830
  uint64_t data_size = address - orig_address;
3831
  this->set_current_data_size_for_child(data_size);
3832
  return data_size;
3833
}
3834
 
3835
// Add a script input section.  SIS is an Output_section::Input_section,
3836
// which can be either a plain input section or a special input section like
3837
// a relaxed input section.  For a special input section, its size must be
3838
// finalized.
3839
 
3840
void
3841
Output_section::add_script_input_section(const Input_section& sis)
3842
{
3843
  uint64_t data_size = sis.data_size();
3844
  uint64_t addralign = sis.addralign();
3845
  if (addralign > this->addralign_)
3846
    this->addralign_ = addralign;
3847
 
3848
  off_t offset_in_section = this->current_data_size_for_child();
3849
  off_t aligned_offset_in_section = align_address(offset_in_section,
3850
                                                  addralign);
3851
 
3852
  this->set_current_data_size_for_child(aligned_offset_in_section
3853
                                        + data_size);
3854
 
3855
  this->input_sections_.push_back(sis);
3856
 
3857
  // Update fast lookup maps if necessary. 
3858
  if (this->lookup_maps_->is_valid())
3859
    {
3860
      if (sis.is_merge_section())
3861
        {
3862
          Output_merge_base* pomb = sis.output_merge_base();
3863
          Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3864
                                       pomb->addralign());
3865
          this->lookup_maps_->add_merge_section(msp, pomb);
3866
          for (Output_merge_base::Input_sections::const_iterator p =
3867
                 pomb->input_sections_begin();
3868
               p != pomb->input_sections_end();
3869
               ++p)
3870
            this->lookup_maps_->add_merge_input_section(p->first, p->second,
3871
                                                        pomb);
3872
        }
3873
      else if (sis.is_relaxed_input_section())
3874
        {
3875
          Output_relaxed_input_section* poris = sis.relaxed_input_section();
3876
          this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3877
                                                        poris->shndx(), poris);
3878
        }
3879
    }
3880
}
3881
 
3882
// Save states for relaxation.
3883
 
3884
void
3885
Output_section::save_states()
3886
{
3887
  gold_assert(this->checkpoint_ == NULL);
3888
  Checkpoint_output_section* checkpoint =
3889
    new Checkpoint_output_section(this->addralign_, this->flags_,
3890
                                  this->input_sections_,
3891
                                  this->first_input_offset_,
3892
                                  this->attached_input_sections_are_sorted_);
3893
  this->checkpoint_ = checkpoint;
3894
  gold_assert(this->fills_.empty());
3895
}
3896
 
3897
void
3898
Output_section::discard_states()
3899
{
3900
  gold_assert(this->checkpoint_ != NULL);
3901
  delete this->checkpoint_;
3902
  this->checkpoint_ = NULL;
3903
  gold_assert(this->fills_.empty());
3904
 
3905
  // Simply invalidate the fast lookup maps since we do not keep
3906
  // track of them.
3907
  this->lookup_maps_->invalidate();
3908
}
3909
 
3910
void
3911
Output_section::restore_states()
3912
{
3913
  gold_assert(this->checkpoint_ != NULL);
3914
  Checkpoint_output_section* checkpoint = this->checkpoint_;
3915
 
3916
  this->addralign_ = checkpoint->addralign();
3917
  this->flags_ = checkpoint->flags();
3918
  this->first_input_offset_ = checkpoint->first_input_offset();
3919
 
3920
  if (!checkpoint->input_sections_saved())
3921
    {
3922
      // If we have not copied the input sections, just resize it.
3923
      size_t old_size = checkpoint->input_sections_size();
3924
      gold_assert(this->input_sections_.size() >= old_size);
3925
      this->input_sections_.resize(old_size);
3926
    }
3927
  else
3928
    {
3929
      // We need to copy the whole list.  This is not efficient for
3930
      // extremely large output with hundreads of thousands of input
3931
      // objects.  We may need to re-think how we should pass sections
3932
      // to scripts.
3933
      this->input_sections_ = *checkpoint->input_sections();
3934
    }
3935
 
3936
  this->attached_input_sections_are_sorted_ =
3937
    checkpoint->attached_input_sections_are_sorted();
3938
 
3939
  // Simply invalidate the fast lookup maps since we do not keep
3940
  // track of them.
3941
  this->lookup_maps_->invalidate();
3942
}
3943
 
3944
// Update the section offsets of input sections in this.  This is required if
3945
// relaxation causes some input sections to change sizes.
3946
 
3947
void
3948
Output_section::adjust_section_offsets()
3949
{
3950
  if (!this->section_offsets_need_adjustment_)
3951
    return;
3952
 
3953
  off_t off = 0;
3954
  for (Input_section_list::iterator p = this->input_sections_.begin();
3955
       p != this->input_sections_.end();
3956
       ++p)
3957
    {
3958
      off = align_address(off, p->addralign());
3959
      if (p->is_input_section())
3960
        p->relobj()->set_section_offset(p->shndx(), off);
3961
      off += p->data_size();
3962
    }
3963
 
3964
  this->section_offsets_need_adjustment_ = false;
3965
}
3966
 
3967
// Print to the map file.
3968
 
3969
void
3970
Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3971
{
3972
  mapfile->print_output_section(this);
3973
 
3974
  for (Input_section_list::const_iterator p = this->input_sections_.begin();
3975
       p != this->input_sections_.end();
3976
       ++p)
3977
    p->print_to_mapfile(mapfile);
3978
}
3979
 
3980
// Print stats for merge sections to stderr.
3981
 
3982
void
3983
Output_section::print_merge_stats()
3984
{
3985
  Input_section_list::iterator p;
3986
  for (p = this->input_sections_.begin();
3987
       p != this->input_sections_.end();
3988
       ++p)
3989
    p->print_merge_stats(this->name_);
3990
}
3991
 
3992
// Set a fixed layout for the section.  Used for incremental update links.
3993
 
3994
void
3995
Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3996
                                 off_t sh_size, uint64_t sh_addralign)
3997
{
3998
  this->addralign_ = sh_addralign;
3999
  this->set_current_data_size(sh_size);
4000
  if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
4001
    this->set_address(sh_addr);
4002
  this->set_file_offset(sh_offset);
4003
  this->finalize_data_size();
4004
  this->free_list_.init(sh_size, false);
4005
  this->has_fixed_layout_ = true;
4006
}
4007
 
4008
// Reserve space within the fixed layout for the section.  Used for
4009
// incremental update links.
4010 148 khays
 
4011 27 khays
void
4012
Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
4013
{
4014
  this->free_list_.remove(sh_offset, sh_offset + sh_size);
4015
}
4016
 
4017 148 khays
// Allocate space from the free list for the section.  Used for
4018
// incremental update links.
4019
 
4020
off_t
4021
Output_section::allocate(off_t len, uint64_t addralign)
4022
{
4023
  return this->free_list_.allocate(len, addralign, 0);
4024
}
4025
 
4026 27 khays
// Output segment methods.
4027
 
4028
Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
4029
  : vaddr_(0),
4030
    paddr_(0),
4031
    memsz_(0),
4032
    max_align_(0),
4033
    min_p_align_(0),
4034
    offset_(0),
4035
    filesz_(0),
4036
    type_(type),
4037
    flags_(flags),
4038
    is_max_align_known_(false),
4039
    are_addresses_set_(false),
4040
    is_large_data_segment_(false)
4041
{
4042
  // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4043
  // the flags.
4044
  if (type == elfcpp::PT_TLS)
4045
    this->flags_ = elfcpp::PF_R;
4046
}
4047
 
4048
// Add an Output_section to a PT_LOAD Output_segment.
4049
 
4050
void
4051
Output_segment::add_output_section_to_load(Layout* layout,
4052
                                           Output_section* os,
4053
                                           elfcpp::Elf_Word seg_flags)
4054
{
4055
  gold_assert(this->type() == elfcpp::PT_LOAD);
4056
  gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4057
  gold_assert(!this->is_max_align_known_);
4058
  gold_assert(os->is_large_data_section() == this->is_large_data_segment());
4059
 
4060
  this->update_flags_for_output_section(seg_flags);
4061
 
4062
  // We don't want to change the ordering if we have a linker script
4063
  // with a SECTIONS clause.
4064
  Output_section_order order = os->order();
4065
  if (layout->script_options()->saw_sections_clause())
4066
    order = static_cast<Output_section_order>(0);
4067
  else
4068
    gold_assert(order != ORDER_INVALID);
4069
 
4070
  this->output_lists_[order].push_back(os);
4071
}
4072
 
4073
// Add an Output_section to a non-PT_LOAD Output_segment.
4074
 
4075
void
4076
Output_segment::add_output_section_to_nonload(Output_section* os,
4077
                                              elfcpp::Elf_Word seg_flags)
4078
{
4079
  gold_assert(this->type() != elfcpp::PT_LOAD);
4080
  gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4081
  gold_assert(!this->is_max_align_known_);
4082
 
4083
  this->update_flags_for_output_section(seg_flags);
4084
 
4085
  this->output_lists_[0].push_back(os);
4086
}
4087
 
4088
// Remove an Output_section from this segment.  It is an error if it
4089
// is not present.
4090
 
4091
void
4092
Output_segment::remove_output_section(Output_section* os)
4093
{
4094
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4095
    {
4096
      Output_data_list* pdl = &this->output_lists_[i];
4097
      for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
4098
        {
4099
          if (*p == os)
4100
            {
4101
              pdl->erase(p);
4102
              return;
4103
            }
4104
        }
4105
    }
4106
  gold_unreachable();
4107
}
4108
 
4109
// Add an Output_data (which need not be an Output_section) to the
4110
// start of a segment.
4111
 
4112
void
4113
Output_segment::add_initial_output_data(Output_data* od)
4114
{
4115
  gold_assert(!this->is_max_align_known_);
4116
  Output_data_list::iterator p = this->output_lists_[0].begin();
4117
  this->output_lists_[0].insert(p, od);
4118
}
4119
 
4120
// Return true if this segment has any sections which hold actual
4121
// data, rather than being a BSS section.
4122
 
4123
bool
4124
Output_segment::has_any_data_sections() const
4125
{
4126
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4127
    {
4128
      const Output_data_list* pdl = &this->output_lists_[i];
4129
      for (Output_data_list::const_iterator p = pdl->begin();
4130
           p != pdl->end();
4131
           ++p)
4132
        {
4133
          if (!(*p)->is_section())
4134
            return true;
4135
          if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4136
            return true;
4137
        }
4138
    }
4139
  return false;
4140
}
4141
 
4142
// Return whether the first data section (not counting TLS sections)
4143
// is a relro section.
4144
 
4145
bool
4146
Output_segment::is_first_section_relro() const
4147
{
4148
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4149
    {
4150
      if (i == static_cast<int>(ORDER_TLS_DATA)
4151
          || i == static_cast<int>(ORDER_TLS_BSS))
4152
        continue;
4153
      const Output_data_list* pdl = &this->output_lists_[i];
4154
      if (!pdl->empty())
4155
        {
4156
          Output_data* p = pdl->front();
4157
          return p->is_section() && p->output_section()->is_relro();
4158
        }
4159
    }
4160
  return false;
4161
}
4162
 
4163
// Return the maximum alignment of the Output_data in Output_segment.
4164
 
4165
uint64_t
4166
Output_segment::maximum_alignment()
4167
{
4168
  if (!this->is_max_align_known_)
4169
    {
4170
      for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4171
        {
4172
          const Output_data_list* pdl = &this->output_lists_[i];
4173
          uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4174
          if (addralign > this->max_align_)
4175
            this->max_align_ = addralign;
4176
        }
4177
      this->is_max_align_known_ = true;
4178
    }
4179
 
4180
  return this->max_align_;
4181
}
4182
 
4183
// Return the maximum alignment of a list of Output_data.
4184
 
4185
uint64_t
4186
Output_segment::maximum_alignment_list(const Output_data_list* pdl)
4187
{
4188
  uint64_t ret = 0;
4189
  for (Output_data_list::const_iterator p = pdl->begin();
4190
       p != pdl->end();
4191
       ++p)
4192
    {
4193
      uint64_t addralign = (*p)->addralign();
4194
      if (addralign > ret)
4195
        ret = addralign;
4196
    }
4197
  return ret;
4198
}
4199
 
4200
// Return whether this segment has any dynamic relocs.
4201
 
4202
bool
4203
Output_segment::has_dynamic_reloc() const
4204
{
4205
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4206
    if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4207
      return true;
4208
  return false;
4209
}
4210
 
4211
// Return whether this Output_data_list has any dynamic relocs.
4212
 
4213
bool
4214
Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4215
{
4216
  for (Output_data_list::const_iterator p = pdl->begin();
4217
       p != pdl->end();
4218
       ++p)
4219
    if ((*p)->has_dynamic_reloc())
4220
      return true;
4221
  return false;
4222
}
4223
 
4224
// Set the section addresses for an Output_segment.  If RESET is true,
4225
// reset the addresses first.  ADDR is the address and *POFF is the
4226
// file offset.  Set the section indexes starting with *PSHNDX.
4227
// INCREASE_RELRO is the size of the portion of the first non-relro
4228
// section that should be included in the PT_GNU_RELRO segment.
4229
// If this segment has relro sections, and has been aligned for
4230
// that purpose, set *HAS_RELRO to TRUE.  Return the address of
4231
// the immediately following segment.  Update *HAS_RELRO, *POFF,
4232
// and *PSHNDX.
4233
 
4234
uint64_t
4235
Output_segment::set_section_addresses(Layout* layout, bool reset,
4236
                                      uint64_t addr,
4237
                                      unsigned int* increase_relro,
4238
                                      bool* has_relro,
4239
                                      off_t* poff,
4240
                                      unsigned int* pshndx)
4241
{
4242
  gold_assert(this->type_ == elfcpp::PT_LOAD);
4243
 
4244
  uint64_t last_relro_pad = 0;
4245
  off_t orig_off = *poff;
4246
 
4247
  bool in_tls = false;
4248
 
4249
  // If we have relro sections, we need to pad forward now so that the
4250
  // relro sections plus INCREASE_RELRO end on a common page boundary.
4251
  if (parameters->options().relro()
4252
      && this->is_first_section_relro()
4253
      && (!this->are_addresses_set_ || reset))
4254
    {
4255
      uint64_t relro_size = 0;
4256
      off_t off = *poff;
4257
      uint64_t max_align = 0;
4258
      for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
4259
        {
4260
          Output_data_list* pdl = &this->output_lists_[i];
4261
          Output_data_list::iterator p;
4262
          for (p = pdl->begin(); p != pdl->end(); ++p)
4263
            {
4264
              if (!(*p)->is_section())
4265
                break;
4266
              uint64_t align = (*p)->addralign();
4267
              if (align > max_align)
4268
                max_align = align;
4269
              if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4270
                in_tls = true;
4271
              else if (in_tls)
4272
                {
4273
                  // Align the first non-TLS section to the alignment
4274
                  // of the TLS segment.
4275
                  align = max_align;
4276
                  in_tls = false;
4277
                }
4278
              relro_size = align_address(relro_size, align);
4279
              // Ignore the size of the .tbss section.
4280
              if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4281
                  && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4282
                continue;
4283
              if ((*p)->is_address_valid())
4284
                relro_size += (*p)->data_size();
4285
              else
4286
                {
4287
                  // FIXME: This could be faster.
4288
                  (*p)->set_address_and_file_offset(addr + relro_size,
4289
                                                    off + relro_size);
4290
                  relro_size += (*p)->data_size();
4291
                  (*p)->reset_address_and_file_offset();
4292
                }
4293
            }
4294
          if (p != pdl->end())
4295
            break;
4296
        }
4297
      relro_size += *increase_relro;
4298
      // Pad the total relro size to a multiple of the maximum
4299
      // section alignment seen.
4300
      uint64_t aligned_size = align_address(relro_size, max_align);
4301
      // Note the amount of padding added after the last relro section.
4302
      last_relro_pad = aligned_size - relro_size;
4303
      *has_relro = true;
4304
 
4305
      uint64_t page_align = parameters->target().common_pagesize();
4306
 
4307
      // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4308
      uint64_t desired_align = page_align - (aligned_size % page_align);
4309
      if (desired_align < *poff % page_align)
4310
        *poff += page_align - *poff % page_align;
4311
      *poff += desired_align - *poff % page_align;
4312
      addr += *poff - orig_off;
4313
      orig_off = *poff;
4314
    }
4315
 
4316
  if (!reset && this->are_addresses_set_)
4317
    {
4318
      gold_assert(this->paddr_ == addr);
4319
      addr = this->vaddr_;
4320
    }
4321
  else
4322
    {
4323
      this->vaddr_ = addr;
4324
      this->paddr_ = addr;
4325
      this->are_addresses_set_ = true;
4326
    }
4327
 
4328
  in_tls = false;
4329
 
4330
  this->offset_ = orig_off;
4331
 
4332
  off_t off = 0;
4333
  uint64_t ret;
4334
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4335
    {
4336
      if (i == static_cast<int>(ORDER_RELRO_LAST))
4337
        {
4338
          *poff += last_relro_pad;
4339
          addr += last_relro_pad;
4340
          if (this->output_lists_[i].empty())
4341
            {
4342
              // If there is nothing in the ORDER_RELRO_LAST list,
4343
              // the padding will occur at the end of the relro
4344
              // segment, and we need to add it to *INCREASE_RELRO.
4345
              *increase_relro += last_relro_pad;
4346
            }
4347
        }
4348
      addr = this->set_section_list_addresses(layout, reset,
4349
                                              &this->output_lists_[i],
4350
                                              addr, poff, pshndx, &in_tls);
4351
      if (i < static_cast<int>(ORDER_SMALL_BSS))
4352
        {
4353
          this->filesz_ = *poff - orig_off;
4354
          off = *poff;
4355
        }
4356
 
4357
      ret = addr;
4358
    }
4359
 
4360
  // If the last section was a TLS section, align upward to the
4361
  // alignment of the TLS segment, so that the overall size of the TLS
4362
  // segment is aligned.
4363
  if (in_tls)
4364
    {
4365
      uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4366
      *poff = align_address(*poff, segment_align);
4367
    }
4368
 
4369
  this->memsz_ = *poff - orig_off;
4370
 
4371
  // Ignore the file offset adjustments made by the BSS Output_data
4372
  // objects.
4373
  *poff = off;
4374
 
4375
  return ret;
4376
}
4377
 
4378
// Set the addresses and file offsets in a list of Output_data
4379
// structures.
4380
 
4381
uint64_t
4382
Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4383
                                           Output_data_list* pdl,
4384
                                           uint64_t addr, off_t* poff,
4385
                                           unsigned int* pshndx,
4386
                                           bool* in_tls)
4387
{
4388
  off_t startoff = *poff;
4389
  // For incremental updates, we may allocate non-fixed sections from
4390
  // free space in the file.  This keeps track of the high-water mark.
4391
  off_t maxoff = startoff;
4392
 
4393
  off_t off = startoff;
4394
  for (Output_data_list::iterator p = pdl->begin();
4395
       p != pdl->end();
4396
       ++p)
4397
    {
4398
      if (reset)
4399
        (*p)->reset_address_and_file_offset();
4400
 
4401
      // When doing an incremental update or when using a linker script,
4402
      // the section will most likely already have an address.
4403
      if (!(*p)->is_address_valid())
4404
        {
4405
          uint64_t align = (*p)->addralign();
4406
 
4407
          if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4408
            {
4409
              // Give the first TLS section the alignment of the
4410
              // entire TLS segment.  Otherwise the TLS segment as a
4411
              // whole may be misaligned.
4412
              if (!*in_tls)
4413
                {
4414
                  Output_segment* tls_segment = layout->tls_segment();
4415
                  gold_assert(tls_segment != NULL);
4416
                  uint64_t segment_align = tls_segment->maximum_alignment();
4417
                  gold_assert(segment_align >= align);
4418
                  align = segment_align;
4419
 
4420
                  *in_tls = true;
4421
                }
4422
            }
4423
          else
4424
            {
4425
              // If this is the first section after the TLS segment,
4426
              // align it to at least the alignment of the TLS
4427
              // segment, so that the size of the overall TLS segment
4428
              // is aligned.
4429
              if (*in_tls)
4430
                {
4431
                  uint64_t segment_align =
4432
                      layout->tls_segment()->maximum_alignment();
4433
                  if (segment_align > align)
4434
                    align = segment_align;
4435
 
4436
                  *in_tls = false;
4437
                }
4438
            }
4439
 
4440 148 khays
          if (!parameters->incremental_update())
4441 27 khays
            {
4442
              off = align_address(off, align);
4443
              (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4444
            }
4445
          else
4446
            {
4447
              // Incremental update: allocate file space from free list.
4448
              (*p)->pre_finalize_data_size();
4449
              off_t current_size = (*p)->current_data_size();
4450
              off = layout->allocate(current_size, align, startoff);
4451
              if (off == -1)
4452
                {
4453
                  gold_assert((*p)->output_section() != NULL);
4454 148 khays
                  gold_fallback(_("out of patch space for section %s; "
4455
                                  "relink with --incremental-full"),
4456
                                (*p)->output_section()->name());
4457 27 khays
                }
4458
              (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4459
              if ((*p)->data_size() > current_size)
4460
                {
4461
                  gold_assert((*p)->output_section() != NULL);
4462 148 khays
                  gold_fallback(_("%s: section changed size; "
4463
                                  "relink with --incremental-full"),
4464
                                (*p)->output_section()->name());
4465 27 khays
                }
4466
            }
4467
        }
4468
      else if (parameters->incremental_update())
4469
        {
4470
          // For incremental updates, use the fixed offset for the
4471
          // high-water mark computation.
4472
          off = (*p)->offset();
4473
        }
4474
      else
4475
        {
4476
          // The script may have inserted a skip forward, but it
4477
          // better not have moved backward.
4478
          if ((*p)->address() >= addr + (off - startoff))
4479
            off += (*p)->address() - (addr + (off - startoff));
4480
          else
4481
            {
4482
              if (!layout->script_options()->saw_sections_clause())
4483
                gold_unreachable();
4484
              else
4485
                {
4486
                  Output_section* os = (*p)->output_section();
4487
 
4488
                  // Cast to unsigned long long to avoid format warnings.
4489
                  unsigned long long previous_dot =
4490
                    static_cast<unsigned long long>(addr + (off - startoff));
4491
                  unsigned long long dot =
4492
                    static_cast<unsigned long long>((*p)->address());
4493
 
4494
                  if (os == NULL)
4495
                    gold_error(_("dot moves backward in linker script "
4496
                                 "from 0x%llx to 0x%llx"), previous_dot, dot);
4497
                  else
4498
                    gold_error(_("address of section '%s' moves backward "
4499
                                 "from 0x%llx to 0x%llx"),
4500
                               os->name(), previous_dot, dot);
4501
                }
4502
            }
4503
          (*p)->set_file_offset(off);
4504
          (*p)->finalize_data_size();
4505
        }
4506
 
4507 159 khays
      if (parameters->incremental_update())
4508
        gold_debug(DEBUG_INCREMENTAL,
4509
                   "set_section_list_addresses: %08lx %08lx %s",
4510
                   static_cast<long>(off),
4511
                   static_cast<long>((*p)->data_size()),
4512
                   ((*p)->output_section() != NULL
4513
                    ? (*p)->output_section()->name() : "(special)"));
4514 27 khays
 
4515 159 khays
      // We want to ignore the size of a SHF_TLS SHT_NOBITS
4516 27 khays
      // section.  Such a section does not affect the size of a
4517
      // PT_LOAD segment.
4518
      if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4519
          || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4520
        off += (*p)->data_size();
4521
 
4522
      if (off > maxoff)
4523
        maxoff = off;
4524
 
4525
      if ((*p)->is_section())
4526
        {
4527
          (*p)->set_out_shndx(*pshndx);
4528
          ++*pshndx;
4529
        }
4530
    }
4531
 
4532
  *poff = maxoff;
4533
  return addr + (maxoff - startoff);
4534
}
4535
 
4536
// For a non-PT_LOAD segment, set the offset from the sections, if
4537
// any.  Add INCREASE to the file size and the memory size.
4538
 
4539
void
4540
Output_segment::set_offset(unsigned int increase)
4541
{
4542
  gold_assert(this->type_ != elfcpp::PT_LOAD);
4543
 
4544
  gold_assert(!this->are_addresses_set_);
4545
 
4546
  // A non-load section only uses output_lists_[0].
4547
 
4548
  Output_data_list* pdl = &this->output_lists_[0];
4549
 
4550
  if (pdl->empty())
4551
    {
4552
      gold_assert(increase == 0);
4553
      this->vaddr_ = 0;
4554
      this->paddr_ = 0;
4555
      this->are_addresses_set_ = true;
4556
      this->memsz_ = 0;
4557
      this->min_p_align_ = 0;
4558
      this->offset_ = 0;
4559
      this->filesz_ = 0;
4560
      return;
4561
    }
4562
 
4563
  // Find the first and last section by address.
4564
  const Output_data* first = NULL;
4565
  const Output_data* last_data = NULL;
4566
  const Output_data* last_bss = NULL;
4567
  for (Output_data_list::const_iterator p = pdl->begin();
4568
       p != pdl->end();
4569
       ++p)
4570
    {
4571
      if (first == NULL
4572
          || (*p)->address() < first->address()
4573
          || ((*p)->address() == first->address()
4574
              && (*p)->data_size() < first->data_size()))
4575
        first = *p;
4576
      const Output_data** plast;
4577
      if ((*p)->is_section()
4578
          && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4579
        plast = &last_bss;
4580
      else
4581
        plast = &last_data;
4582
      if (*plast == NULL
4583
          || (*p)->address() > (*plast)->address()
4584
          || ((*p)->address() == (*plast)->address()
4585
              && (*p)->data_size() > (*plast)->data_size()))
4586
        *plast = *p;
4587
    }
4588
 
4589
  this->vaddr_ = first->address();
4590
  this->paddr_ = (first->has_load_address()
4591
                  ? first->load_address()
4592
                  : this->vaddr_);
4593
  this->are_addresses_set_ = true;
4594
  this->offset_ = first->offset();
4595
 
4596
  if (last_data == NULL)
4597
    this->filesz_ = 0;
4598
  else
4599
    this->filesz_ = (last_data->address()
4600
                     + last_data->data_size()
4601
                     - this->vaddr_);
4602
 
4603
  const Output_data* last = last_bss != NULL ? last_bss : last_data;
4604
  this->memsz_ = (last->address()
4605
                  + last->data_size()
4606
                  - this->vaddr_);
4607
 
4608
  this->filesz_ += increase;
4609
  this->memsz_ += increase;
4610
 
4611
  // If this is a RELRO segment, verify that the segment ends at a
4612
  // page boundary.
4613
  if (this->type_ == elfcpp::PT_GNU_RELRO)
4614
    {
4615
      uint64_t page_align = parameters->target().common_pagesize();
4616
      uint64_t segment_end = this->vaddr_ + this->memsz_;
4617
      if (parameters->incremental_update())
4618
        {
4619
          // The INCREASE_RELRO calculation is bypassed for an incremental
4620
          // update, so we need to adjust the segment size manually here.
4621
          segment_end = align_address(segment_end, page_align);
4622
          this->memsz_ = segment_end - this->vaddr_;
4623
        }
4624
      else
4625
        gold_assert(segment_end == align_address(segment_end, page_align));
4626
    }
4627
 
4628
  // If this is a TLS segment, align the memory size.  The code in
4629
  // set_section_list ensures that the section after the TLS segment
4630
  // is aligned to give us room.
4631
  if (this->type_ == elfcpp::PT_TLS)
4632
    {
4633
      uint64_t segment_align = this->maximum_alignment();
4634
      gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4635
      this->memsz_ = align_address(this->memsz_, segment_align);
4636
    }
4637
}
4638
 
4639
// Set the TLS offsets of the sections in the PT_TLS segment.
4640
 
4641
void
4642
Output_segment::set_tls_offsets()
4643
{
4644
  gold_assert(this->type_ == elfcpp::PT_TLS);
4645
 
4646
  for (Output_data_list::iterator p = this->output_lists_[0].begin();
4647
       p != this->output_lists_[0].end();
4648
       ++p)
4649
    (*p)->set_tls_offset(this->vaddr_);
4650
}
4651
 
4652
// Return the load address of the first section.
4653
 
4654
uint64_t
4655
Output_segment::first_section_load_address() const
4656
{
4657
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4658
    {
4659
      const Output_data_list* pdl = &this->output_lists_[i];
4660
      for (Output_data_list::const_iterator p = pdl->begin();
4661
           p != pdl->end();
4662
           ++p)
4663
        {
4664
          if ((*p)->is_section())
4665
            return ((*p)->has_load_address()
4666
                    ? (*p)->load_address()
4667
                    : (*p)->address());
4668
        }
4669
    }
4670
  gold_unreachable();
4671
}
4672
 
4673
// Return the number of Output_sections in an Output_segment.
4674
 
4675
unsigned int
4676
Output_segment::output_section_count() const
4677
{
4678
  unsigned int ret = 0;
4679
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4680
    ret += this->output_section_count_list(&this->output_lists_[i]);
4681
  return ret;
4682
}
4683
 
4684
// Return the number of Output_sections in an Output_data_list.
4685
 
4686
unsigned int
4687
Output_segment::output_section_count_list(const Output_data_list* pdl) const
4688
{
4689
  unsigned int count = 0;
4690
  for (Output_data_list::const_iterator p = pdl->begin();
4691
       p != pdl->end();
4692
       ++p)
4693
    {
4694
      if ((*p)->is_section())
4695
        ++count;
4696
    }
4697
  return count;
4698
}
4699
 
4700
// Return the section attached to the list segment with the lowest
4701
// load address.  This is used when handling a PHDRS clause in a
4702
// linker script.
4703
 
4704
Output_section*
4705
Output_segment::section_with_lowest_load_address() const
4706
{
4707
  Output_section* found = NULL;
4708
  uint64_t found_lma = 0;
4709
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4710
    this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4711
                                      &found_lma);
4712
  return found;
4713
}
4714
 
4715
// Look through a list for a section with a lower load address.
4716
 
4717
void
4718
Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4719
                                            Output_section** found,
4720
                                            uint64_t* found_lma) const
4721
{
4722
  for (Output_data_list::const_iterator p = pdl->begin();
4723
       p != pdl->end();
4724
       ++p)
4725
    {
4726
      if (!(*p)->is_section())
4727
        continue;
4728
      Output_section* os = static_cast<Output_section*>(*p);
4729
      uint64_t lma = (os->has_load_address()
4730
                      ? os->load_address()
4731
                      : os->address());
4732
      if (*found == NULL || lma < *found_lma)
4733
        {
4734
          *found = os;
4735
          *found_lma = lma;
4736
        }
4737
    }
4738
}
4739
 
4740
// Write the segment data into *OPHDR.
4741
 
4742
template<int size, bool big_endian>
4743
void
4744
Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4745
{
4746
  ophdr->put_p_type(this->type_);
4747
  ophdr->put_p_offset(this->offset_);
4748
  ophdr->put_p_vaddr(this->vaddr_);
4749
  ophdr->put_p_paddr(this->paddr_);
4750
  ophdr->put_p_filesz(this->filesz_);
4751
  ophdr->put_p_memsz(this->memsz_);
4752
  ophdr->put_p_flags(this->flags_);
4753
  ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4754
}
4755
 
4756
// Write the section headers into V.
4757
 
4758
template<int size, bool big_endian>
4759
unsigned char*
4760
Output_segment::write_section_headers(const Layout* layout,
4761
                                      const Stringpool* secnamepool,
4762
                                      unsigned char* v,
4763
                                      unsigned int* pshndx) const
4764
{
4765
  // Every section that is attached to a segment must be attached to a
4766
  // PT_LOAD segment, so we only write out section headers for PT_LOAD
4767
  // segments.
4768
  if (this->type_ != elfcpp::PT_LOAD)
4769
    return v;
4770
 
4771
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4772
    {
4773
      const Output_data_list* pdl = &this->output_lists_[i];
4774
      v = this->write_section_headers_list<size, big_endian>(layout,
4775
                                                             secnamepool,
4776
                                                             pdl,
4777
                                                             v, pshndx);
4778
    }
4779
 
4780
  return v;
4781
}
4782
 
4783
template<int size, bool big_endian>
4784
unsigned char*
4785
Output_segment::write_section_headers_list(const Layout* layout,
4786
                                           const Stringpool* secnamepool,
4787
                                           const Output_data_list* pdl,
4788
                                           unsigned char* v,
4789
                                           unsigned int* pshndx) const
4790
{
4791
  const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4792
  for (Output_data_list::const_iterator p = pdl->begin();
4793
       p != pdl->end();
4794
       ++p)
4795
    {
4796
      if ((*p)->is_section())
4797
        {
4798
          const Output_section* ps = static_cast<const Output_section*>(*p);
4799
          gold_assert(*pshndx == ps->out_shndx());
4800
          elfcpp::Shdr_write<size, big_endian> oshdr(v);
4801
          ps->write_header(layout, secnamepool, &oshdr);
4802
          v += shdr_size;
4803
          ++*pshndx;
4804
        }
4805
    }
4806
  return v;
4807
}
4808
 
4809
// Print the output sections to the map file.
4810
 
4811
void
4812
Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4813
{
4814
  if (this->type() != elfcpp::PT_LOAD)
4815
    return;
4816
  for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4817
    this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4818
}
4819
 
4820
// Print an output section list to the map file.
4821
 
4822
void
4823
Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4824
                                              const Output_data_list* pdl) const
4825
{
4826
  for (Output_data_list::const_iterator p = pdl->begin();
4827
       p != pdl->end();
4828
       ++p)
4829
    (*p)->print_to_mapfile(mapfile);
4830
}
4831
 
4832
// Output_file methods.
4833
 
4834
Output_file::Output_file(const char* name)
4835
  : name_(name),
4836
    o_(-1),
4837
    file_size_(0),
4838
    base_(NULL),
4839
    map_is_anonymous_(false),
4840
    map_is_allocated_(false),
4841
    is_temporary_(false)
4842
{
4843
}
4844
 
4845
// Try to open an existing file.  Returns false if the file doesn't
4846
// exist, has a size of 0 or can't be mmapped.  If BASE_NAME is not
4847
// NULL, open that file as the base for incremental linking, and
4848
// copy its contents to the new output file.  This routine can
4849
// be called for incremental updates, in which case WRITABLE should
4850
// be true, or by the incremental-dump utility, in which case
4851
// WRITABLE should be false.
4852
 
4853
bool
4854
Output_file::open_base_file(const char* base_name, bool writable)
4855
{
4856
  // The name "-" means "stdout".
4857
  if (strcmp(this->name_, "-") == 0)
4858
    return false;
4859
 
4860
  bool use_base_file = base_name != NULL;
4861
  if (!use_base_file)
4862
    base_name = this->name_;
4863
  else if (strcmp(base_name, this->name_) == 0)
4864
    gold_fatal(_("%s: incremental base and output file name are the same"),
4865
               base_name);
4866
 
4867
  // Don't bother opening files with a size of zero.
4868
  struct stat s;
4869
  if (::stat(base_name, &s) != 0)
4870
    {
4871
      gold_info(_("%s: stat: %s"), base_name, strerror(errno));
4872
      return false;
4873
    }
4874
  if (s.st_size == 0)
4875
    {
4876
      gold_info(_("%s: incremental base file is empty"), base_name);
4877
      return false;
4878
    }
4879
 
4880
  // If we're using a base file, we want to open it read-only.
4881
  if (use_base_file)
4882
    writable = false;
4883
 
4884
  int oflags = writable ? O_RDWR : O_RDONLY;
4885
  int o = open_descriptor(-1, base_name, oflags, 0);
4886
  if (o < 0)
4887
    {
4888
      gold_info(_("%s: open: %s"), base_name, strerror(errno));
4889
      return false;
4890
    }
4891
 
4892
  // If the base file and the output file are different, open a
4893
  // new output file and read the contents from the base file into
4894
  // the newly-mapped region.
4895
  if (use_base_file)
4896
    {
4897
      this->open(s.st_size);
4898 163 khays
      ssize_t bytes_to_read = s.st_size;
4899
      unsigned char* p = this->base_;
4900
      while (bytes_to_read > 0)
4901
        {
4902
          ssize_t len = ::read(o, p, bytes_to_read);
4903
          if (len < 0)
4904
            {
4905
              gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
4906
              return false;
4907
            }
4908
          if (len == 0)
4909
            {
4910
              gold_info(_("%s: file too short: read only %lld of %lld bytes"),
4911
                        base_name,
4912
                        static_cast<long long>(s.st_size - bytes_to_read),
4913
                        static_cast<long long>(s.st_size));
4914
              return false;
4915
            }
4916
          p += len;
4917
          bytes_to_read -= len;
4918
        }
4919 27 khays
      ::close(o);
4920
      return true;
4921
    }
4922
 
4923
  this->o_ = o;
4924
  this->file_size_ = s.st_size;
4925
 
4926
  if (!this->map_no_anonymous(writable))
4927
    {
4928
      release_descriptor(o, true);
4929
      this->o_ = -1;
4930
      this->file_size_ = 0;
4931
      return false;
4932
    }
4933
 
4934
  return true;
4935
}
4936
 
4937
// Open the output file.
4938
 
4939
void
4940
Output_file::open(off_t file_size)
4941
{
4942
  this->file_size_ = file_size;
4943
 
4944
  // Unlink the file first; otherwise the open() may fail if the file
4945
  // is busy (e.g. it's an executable that's currently being executed).
4946
  //
4947
  // However, the linker may be part of a system where a zero-length
4948
  // file is created for it to write to, with tight permissions (gcc
4949
  // 2.95 did something like this).  Unlinking the file would work
4950
  // around those permission controls, so we only unlink if the file
4951
  // has a non-zero size.  We also unlink only regular files to avoid
4952
  // trouble with directories/etc.
4953
  //
4954
  // If we fail, continue; this command is merely a best-effort attempt
4955
  // to improve the odds for open().
4956
 
4957
  // We let the name "-" mean "stdout"
4958
  if (!this->is_temporary_)
4959
    {
4960
      if (strcmp(this->name_, "-") == 0)
4961
        this->o_ = STDOUT_FILENO;
4962
      else
4963
        {
4964
          struct stat s;
4965
          if (::stat(this->name_, &s) == 0
4966
              && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4967
            {
4968
              if (s.st_size != 0)
4969
                ::unlink(this->name_);
4970
              else if (!parameters->options().relocatable())
4971
                {
4972
                  // If we don't unlink the existing file, add execute
4973
                  // permission where read permissions already exist
4974
                  // and where the umask permits.
4975
                  int mask = ::umask(0);
4976
                  ::umask(mask);
4977
                  s.st_mode |= (s.st_mode & 0444) >> 2;
4978
                  ::chmod(this->name_, s.st_mode & ~mask);
4979
                }
4980
            }
4981
 
4982
          int mode = parameters->options().relocatable() ? 0666 : 0777;
4983
          int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4984
                                  mode);
4985
          if (o < 0)
4986
            gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4987
          this->o_ = o;
4988
        }
4989
    }
4990
 
4991
  this->map();
4992
}
4993
 
4994
// Resize the output file.
4995
 
4996
void
4997
Output_file::resize(off_t file_size)
4998
{
4999
  // If the mmap is mapping an anonymous memory buffer, this is easy:
5000
  // just mremap to the new size.  If it's mapping to a file, we want
5001
  // to unmap to flush to the file, then remap after growing the file.
5002
  if (this->map_is_anonymous_)
5003
    {
5004
      void* base;
5005
      if (!this->map_is_allocated_)
5006
        {
5007
          base = ::mremap(this->base_, this->file_size_, file_size,
5008
                          MREMAP_MAYMOVE);
5009
          if (base == MAP_FAILED)
5010
            gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
5011
        }
5012
      else
5013
        {
5014
          base = realloc(this->base_, file_size);
5015
          if (base == NULL)
5016
            gold_nomem();
5017
          if (file_size > this->file_size_)
5018
            memset(static_cast<char*>(base) + this->file_size_, 0,
5019
                   file_size - this->file_size_);
5020
        }
5021
      this->base_ = static_cast<unsigned char*>(base);
5022
      this->file_size_ = file_size;
5023
    }
5024
  else
5025
    {
5026
      this->unmap();
5027
      this->file_size_ = file_size;
5028
      if (!this->map_no_anonymous(true))
5029
        gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
5030
    }
5031
}
5032
 
5033
// Map an anonymous block of memory which will later be written to the
5034
// file.  Return whether the map succeeded.
5035
 
5036
bool
5037
Output_file::map_anonymous()
5038
{
5039
  void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
5040
                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
5041
  if (base == MAP_FAILED)
5042
    {
5043
      base = malloc(this->file_size_);
5044
      if (base == NULL)
5045
        return false;
5046
      memset(base, 0, this->file_size_);
5047
      this->map_is_allocated_ = true;
5048
    }
5049
  this->base_ = static_cast<unsigned char*>(base);
5050
  this->map_is_anonymous_ = true;
5051
  return true;
5052
}
5053
 
5054
// Map the file into memory.  Return whether the mapping succeeded.
5055
// If WRITABLE is true, map with write access.
5056
 
5057
bool
5058
Output_file::map_no_anonymous(bool writable)
5059
{
5060
  const int o = this->o_;
5061
 
5062
  // If the output file is not a regular file, don't try to mmap it;
5063
  // instead, we'll mmap a block of memory (an anonymous buffer), and
5064
  // then later write the buffer to the file.
5065
  void* base;
5066
  struct stat statbuf;
5067
  if (o == STDOUT_FILENO || o == STDERR_FILENO
5068
      || ::fstat(o, &statbuf) != 0
5069
      || !S_ISREG(statbuf.st_mode)
5070
      || this->is_temporary_)
5071
    return false;
5072
 
5073
  // Ensure that we have disk space available for the file.  If we
5074
  // don't do this, it is possible that we will call munmap, close,
5075
  // and exit with dirty buffers still in the cache with no assigned
5076
  // disk blocks.  If the disk is out of space at that point, the
5077
  // output file will wind up incomplete, but we will have already
5078
  // exited.  The alternative to fallocate would be to use fdatasync,
5079
  // but that would be a more significant performance hit.
5080 163 khays
  if (writable)
5081
    {
5082
      int err = ::posix_fallocate(o, 0, this->file_size_);
5083
      if (err != 0)
5084
       gold_fatal(_("%s: %s"), this->name_, strerror(err));
5085
    }
5086 27 khays
 
5087
  // Map the file into memory.
5088
  int prot = PROT_READ;
5089
  if (writable)
5090
    prot |= PROT_WRITE;
5091
  base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
5092
 
5093
  // The mmap call might fail because of file system issues: the file
5094
  // system might not support mmap at all, or it might not support
5095
  // mmap with PROT_WRITE.
5096
  if (base == MAP_FAILED)
5097
    return false;
5098
 
5099
  this->map_is_anonymous_ = false;
5100
  this->base_ = static_cast<unsigned char*>(base);
5101
  return true;
5102
}
5103
 
5104
// Map the file into memory.
5105
 
5106
void
5107
Output_file::map()
5108
{
5109
  if (this->map_no_anonymous(true))
5110
    return;
5111
 
5112
  // The mmap call might fail because of file system issues: the file
5113
  // system might not support mmap at all, or it might not support
5114
  // mmap with PROT_WRITE.  I'm not sure which errno values we will
5115
  // see in all cases, so if the mmap fails for any reason and we
5116
  // don't care about file contents, try for an anonymous map.
5117
  if (this->map_anonymous())
5118
    return;
5119
 
5120
  gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5121
             this->name_, static_cast<unsigned long>(this->file_size_),
5122
             strerror(errno));
5123
}
5124
 
5125
// Unmap the file from memory.
5126
 
5127
void
5128
Output_file::unmap()
5129
{
5130
  if (this->map_is_anonymous_)
5131
    {
5132
      // We've already written out the data, so there is no reason to
5133
      // waste time unmapping or freeing the memory.
5134
    }
5135
  else
5136
    {
5137
      if (::munmap(this->base_, this->file_size_) < 0)
5138
        gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5139
    }
5140
  this->base_ = NULL;
5141
}
5142
 
5143
// Close the output file.
5144
 
5145
void
5146
Output_file::close()
5147
{
5148
  // If the map isn't file-backed, we need to write it now.
5149
  if (this->map_is_anonymous_ && !this->is_temporary_)
5150
    {
5151
      size_t bytes_to_write = this->file_size_;
5152
      size_t offset = 0;
5153
      while (bytes_to_write > 0)
5154
        {
5155
          ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5156
                                          bytes_to_write);
5157
          if (bytes_written == 0)
5158
            gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5159
          else if (bytes_written < 0)
5160
            gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5161
          else
5162
            {
5163
              bytes_to_write -= bytes_written;
5164
              offset += bytes_written;
5165
            }
5166
        }
5167
    }
5168
  this->unmap();
5169
 
5170
  // We don't close stdout or stderr
5171
  if (this->o_ != STDOUT_FILENO
5172
      && this->o_ != STDERR_FILENO
5173
      && !this->is_temporary_)
5174
    if (::close(this->o_) < 0)
5175
      gold_error(_("%s: close: %s"), this->name_, strerror(errno));
5176
  this->o_ = -1;
5177
}
5178
 
5179
// Instantiate the templates we need.  We could use the configure
5180
// script to restrict this to only the ones for implemented targets.
5181
 
5182
#ifdef HAVE_TARGET_32_LITTLE
5183
template
5184
off_t
5185
Output_section::add_input_section<32, false>(
5186
    Layout* layout,
5187
    Sized_relobj_file<32, false>* object,
5188
    unsigned int shndx,
5189
    const char* secname,
5190
    const elfcpp::Shdr<32, false>& shdr,
5191
    unsigned int reloc_shndx,
5192
    bool have_sections_script);
5193
#endif
5194
 
5195
#ifdef HAVE_TARGET_32_BIG
5196
template
5197
off_t
5198
Output_section::add_input_section<32, true>(
5199
    Layout* layout,
5200
    Sized_relobj_file<32, true>* object,
5201
    unsigned int shndx,
5202
    const char* secname,
5203
    const elfcpp::Shdr<32, true>& shdr,
5204
    unsigned int reloc_shndx,
5205
    bool have_sections_script);
5206
#endif
5207
 
5208
#ifdef HAVE_TARGET_64_LITTLE
5209
template
5210
off_t
5211
Output_section::add_input_section<64, false>(
5212
    Layout* layout,
5213
    Sized_relobj_file<64, false>* object,
5214
    unsigned int shndx,
5215
    const char* secname,
5216
    const elfcpp::Shdr<64, false>& shdr,
5217
    unsigned int reloc_shndx,
5218
    bool have_sections_script);
5219
#endif
5220
 
5221
#ifdef HAVE_TARGET_64_BIG
5222
template
5223
off_t
5224
Output_section::add_input_section<64, true>(
5225
    Layout* layout,
5226
    Sized_relobj_file<64, true>* object,
5227
    unsigned int shndx,
5228
    const char* secname,
5229
    const elfcpp::Shdr<64, true>& shdr,
5230
    unsigned int reloc_shndx,
5231
    bool have_sections_script);
5232
#endif
5233
 
5234
#ifdef HAVE_TARGET_32_LITTLE
5235
template
5236
class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5237
#endif
5238
 
5239
#ifdef HAVE_TARGET_32_BIG
5240
template
5241
class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5242
#endif
5243
 
5244
#ifdef HAVE_TARGET_64_LITTLE
5245
template
5246
class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5247
#endif
5248
 
5249
#ifdef HAVE_TARGET_64_BIG
5250
template
5251
class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5252
#endif
5253
 
5254
#ifdef HAVE_TARGET_32_LITTLE
5255
template
5256
class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5257
#endif
5258
 
5259
#ifdef HAVE_TARGET_32_BIG
5260
template
5261
class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5262
#endif
5263
 
5264
#ifdef HAVE_TARGET_64_LITTLE
5265
template
5266
class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5267
#endif
5268
 
5269
#ifdef HAVE_TARGET_64_BIG
5270
template
5271
class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5272
#endif
5273
 
5274
#ifdef HAVE_TARGET_32_LITTLE
5275
template
5276
class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5277
#endif
5278
 
5279
#ifdef HAVE_TARGET_32_BIG
5280
template
5281
class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5282
#endif
5283
 
5284
#ifdef HAVE_TARGET_64_LITTLE
5285
template
5286
class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5287
#endif
5288
 
5289
#ifdef HAVE_TARGET_64_BIG
5290
template
5291
class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5292
#endif
5293
 
5294
#ifdef HAVE_TARGET_32_LITTLE
5295
template
5296
class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5297
#endif
5298
 
5299
#ifdef HAVE_TARGET_32_BIG
5300
template
5301
class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5302
#endif
5303
 
5304
#ifdef HAVE_TARGET_64_LITTLE
5305
template
5306
class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5307
#endif
5308
 
5309
#ifdef HAVE_TARGET_64_BIG
5310
template
5311
class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5312
#endif
5313
 
5314
#ifdef HAVE_TARGET_32_LITTLE
5315
template
5316
class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
5317
#endif
5318
 
5319
#ifdef HAVE_TARGET_32_BIG
5320
template
5321
class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
5322
#endif
5323
 
5324
#ifdef HAVE_TARGET_64_LITTLE
5325
template
5326
class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
5327
#endif
5328
 
5329
#ifdef HAVE_TARGET_64_BIG
5330
template
5331
class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
5332
#endif
5333
 
5334
#ifdef HAVE_TARGET_32_LITTLE
5335
template
5336
class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
5337
#endif
5338
 
5339
#ifdef HAVE_TARGET_32_BIG
5340
template
5341
class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
5342
#endif
5343
 
5344
#ifdef HAVE_TARGET_64_LITTLE
5345
template
5346
class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
5347
#endif
5348
 
5349
#ifdef HAVE_TARGET_64_BIG
5350
template
5351
class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
5352
#endif
5353
 
5354
#ifdef HAVE_TARGET_32_LITTLE
5355
template
5356
class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
5357
#endif
5358
 
5359
#ifdef HAVE_TARGET_32_BIG
5360
template
5361
class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5362
#endif
5363
 
5364
#ifdef HAVE_TARGET_64_LITTLE
5365
template
5366
class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5367
#endif
5368
 
5369
#ifdef HAVE_TARGET_64_BIG
5370
template
5371
class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5372
#endif
5373
 
5374
#ifdef HAVE_TARGET_32_LITTLE
5375
template
5376
class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5377
#endif
5378
 
5379
#ifdef HAVE_TARGET_32_BIG
5380
template
5381
class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5382
#endif
5383
 
5384
#ifdef HAVE_TARGET_64_LITTLE
5385
template
5386
class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5387
#endif
5388
 
5389
#ifdef HAVE_TARGET_64_BIG
5390
template
5391
class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5392
#endif
5393
 
5394
#ifdef HAVE_TARGET_32_LITTLE
5395
template
5396
class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5397
#endif
5398
 
5399
#ifdef HAVE_TARGET_32_BIG
5400
template
5401
class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5402
#endif
5403
 
5404
#ifdef HAVE_TARGET_64_LITTLE
5405
template
5406
class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5407
#endif
5408
 
5409
#ifdef HAVE_TARGET_64_BIG
5410
template
5411
class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5412
#endif
5413
 
5414
#ifdef HAVE_TARGET_32_LITTLE
5415
template
5416
class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5417
#endif
5418
 
5419
#ifdef HAVE_TARGET_32_BIG
5420
template
5421
class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5422
#endif
5423
 
5424
#ifdef HAVE_TARGET_64_LITTLE
5425
template
5426
class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5427
#endif
5428
 
5429
#ifdef HAVE_TARGET_64_BIG
5430
template
5431
class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5432
#endif
5433
 
5434
#ifdef HAVE_TARGET_32_LITTLE
5435
template
5436
class Output_data_group<32, false>;
5437
#endif
5438
 
5439
#ifdef HAVE_TARGET_32_BIG
5440
template
5441
class Output_data_group<32, true>;
5442
#endif
5443
 
5444
#ifdef HAVE_TARGET_64_LITTLE
5445
template
5446
class Output_data_group<64, false>;
5447
#endif
5448
 
5449
#ifdef HAVE_TARGET_64_BIG
5450
template
5451
class Output_data_group<64, true>;
5452
#endif
5453
 
5454
#ifdef HAVE_TARGET_32_LITTLE
5455
template
5456
class Output_data_got<32, false>;
5457
#endif
5458
 
5459
#ifdef HAVE_TARGET_32_BIG
5460
template
5461
class Output_data_got<32, true>;
5462
#endif
5463
 
5464
#ifdef HAVE_TARGET_64_LITTLE
5465
template
5466
class Output_data_got<64, false>;
5467
#endif
5468
 
5469
#ifdef HAVE_TARGET_64_BIG
5470
template
5471
class Output_data_got<64, true>;
5472
#endif
5473
 
5474
} // End namespace gold.

powered by: WebSVN 2.1.0

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