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