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