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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gold/] [output.cc] - Blame information for rev 847

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

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

powered by: WebSVN 2.1.0

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