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