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 27

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

powered by: WebSVN 2.1.0

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