[gdb/build] Add CXX_DIALECT to CXX
[deliverable/binutils-gdb.git] / gold / output.cc
CommitLineData
a2fb1b05
ILT
1// output.cc -- manage the output file for gold
2
250d07de 3// Copyright (C) 2006-2021 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(
bd73a62d
AM
1370 unsigned int got_indx,
1371 unsigned char* pov) const
ead1e424
ILT
1372{
1373 Valtype val = 0;
1374
1375 switch (this->local_sym_index_)
1376 {
1377 case GSYM_CODE:
1378 {
e8c846c3
ILT
1379 // If the symbol is resolved locally, we need to write out the
1380 // link-time value, which will be relocated dynamically by a
1381 // RELATIVE relocation.
ead1e424 1382 Symbol* gsym = this->u_.gsym;
bd73a62d 1383 if (this->use_plt_or_tls_offset_ && gsym->has_plt_offset())
19fec8c1 1384 val = parameters->target().plt_address_for_global(gsym);
7223e9ca
ILT
1385 else
1386 {
d77a0555
ILT
1387 switch (parameters->size_and_endianness())
1388 {
1389#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1390 case Parameters::TARGET_32_LITTLE:
1391 case Parameters::TARGET_32_BIG:
1392 {
1393 // This cast is ugly. We don't want to put a
1394 // virtual method in Symbol, because we want Symbol
1395 // to be as small as possible.
1396 Sized_symbol<32>::Value_type v;
1397 v = static_cast<Sized_symbol<32>*>(gsym)->value();
1398 val = convert_types<Valtype, Sized_symbol<32>::Value_type>(v);
1399 }
1400 break;
1401#endif
1402#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1403 case Parameters::TARGET_64_LITTLE:
1404 case Parameters::TARGET_64_BIG:
1405 {
1406 Sized_symbol<64>::Value_type v;
1407 v = static_cast<Sized_symbol<64>*>(gsym)->value();
1408 val = convert_types<Valtype, Sized_symbol<64>::Value_type>(v);
1409 }
1410 break;
1411#endif
1412 default:
1413 gold_unreachable();
1414 }
1415 if (this->use_plt_or_tls_offset_
1416 && gsym->type() == elfcpp::STT_TLS)
bd73a62d
AM
1417 val += parameters->target().tls_offset_for_global(gsym,
1418 got_indx);
7223e9ca 1419 }
ead1e424
ILT
1420 }
1421 break;
1422
1423 case CONSTANT_CODE:
1424 val = this->u_.constant;
1425 break;
1426
4829d394
CC
1427 case RESERVED_CODE:
1428 // If we're doing an incremental update, don't touch this GOT entry.
1429 if (parameters->incremental_update())
eb426534 1430 return;
4829d394
CC
1431 val = this->u_.constant;
1432 break;
1433
ead1e424 1434 default:
d1f003c6 1435 {
d77a0555 1436 const Relobj* object = this->u_.object;
eb426534 1437 const unsigned int lsi = this->local_sym_index_;
d77a0555 1438 bool is_tls = object->local_is_tls(lsi);
bd73a62d 1439 if (this->use_plt_or_tls_offset_ && !is_tls)
19fec8c1 1440 val = parameters->target().plt_address_for_local(object, lsi);
bd73a62d
AM
1441 else
1442 {
7ef8ae7c 1443 uint64_t lval = object->local_symbol_value(lsi, this->addend_);
bd73a62d
AM
1444 val = convert_types<Valtype, uint64_t>(lval);
1445 if (this->use_plt_or_tls_offset_ && is_tls)
1446 val += parameters->target().tls_offset_for_local(object, lsi,
1447 got_indx);
1448 }
d1f003c6 1449 }
e727fa71 1450 break;
ead1e424
ILT
1451 }
1452
d77a0555 1453 elfcpp::Swap<got_size, big_endian>::writeval(pov, val);
ead1e424
ILT
1454}
1455
dbe717ef 1456// Output_data_got methods.
ead1e424 1457
dbe717ef
ILT
1458// Add an entry for a global symbol to the GOT. This returns true if
1459// this is a new GOT entry, false if the symbol already had a GOT
1460// entry.
1461
d77a0555 1462template<int got_size, bool big_endian>
dbe717ef 1463bool
d77a0555 1464Output_data_got<got_size, big_endian>::add_global(
0a65a3a7
CC
1465 Symbol* gsym,
1466 unsigned int got_type)
ead1e424 1467{
0a65a3a7 1468 if (gsym->has_got_offset(got_type))
dbe717ef 1469 return false;
ead1e424 1470
4829d394
CC
1471 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1472 gsym->set_got_offset(got_type, got_offset);
7223e9ca
ILT
1473 return true;
1474}
1475
1476// Like add_global, but use the PLT offset.
1477
d77a0555 1478template<int got_size, bool big_endian>
7223e9ca 1479bool
d77a0555
ILT
1480Output_data_got<got_size, big_endian>::add_global_plt(Symbol* gsym,
1481 unsigned int got_type)
7223e9ca
ILT
1482{
1483 if (gsym->has_got_offset(got_type))
1484 return false;
1485
4829d394
CC
1486 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1487 gsym->set_got_offset(got_type, got_offset);
dbe717ef
ILT
1488 return true;
1489}
ead1e424 1490
7bf1f802
ILT
1491// Add an entry for a global symbol to the GOT, and add a dynamic
1492// relocation of type R_TYPE for the GOT entry.
7223e9ca 1493
d77a0555 1494template<int got_size, bool big_endian>
7bf1f802 1495void
d77a0555 1496Output_data_got<got_size, big_endian>::add_global_with_rel(
7bf1f802 1497 Symbol* gsym,
0a65a3a7 1498 unsigned int got_type,
83896202 1499 Output_data_reloc_generic* rel_dyn,
7bf1f802
ILT
1500 unsigned int r_type)
1501{
0a65a3a7 1502 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1503 return;
1504
4829d394 1505 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941 1506 gsym->set_got_offset(got_type, got_offset);
83896202 1507 rel_dyn->add_global_generic(gsym, r_type, this, got_offset, 0);
7bf1f802
ILT
1508}
1509
0a65a3a7
CC
1510// Add a pair of entries for a global symbol to the GOT, and add
1511// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1512// If R_TYPE_2 == 0, add the second entry with no relocation.
d77a0555 1513template<int got_size, bool big_endian>
7bf1f802 1514void
d77a0555 1515Output_data_got<got_size, big_endian>::add_global_pair_with_rel(
0a65a3a7
CC
1516 Symbol* gsym,
1517 unsigned int got_type,
83896202 1518 Output_data_reloc_generic* rel_dyn,
0a65a3a7
CC
1519 unsigned int r_type_1,
1520 unsigned int r_type_2)
7bf1f802 1521{
0a65a3a7 1522 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1523 return;
1524
4829d394 1525 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
2ea97941 1526 gsym->set_got_offset(got_type, got_offset);
83896202 1527 rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, 0);
0a65a3a7 1528
0a65a3a7 1529 if (r_type_2 != 0)
83896202 1530 rel_dyn->add_global_generic(gsym, r_type_2, this,
d77a0555 1531 got_offset + got_size / 8, 0);
7bf1f802
ILT
1532}
1533
0a65a3a7
CC
1534// Add an entry for a local symbol to the GOT. This returns true if
1535// this is a new GOT entry, false if the symbol already has a GOT
1536// entry.
07f397ab 1537
d77a0555 1538template<int got_size, bool big_endian>
07f397ab 1539bool
d77a0555 1540Output_data_got<got_size, big_endian>::add_local(
83896202 1541 Relobj* object,
0a65a3a7
CC
1542 unsigned int symndx,
1543 unsigned int got_type)
07f397ab 1544{
0a65a3a7 1545 if (object->local_has_got_offset(symndx, got_type))
07f397ab
ILT
1546 return false;
1547
4829d394
CC
1548 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1549 false));
1550 object->set_local_got_offset(symndx, got_type, got_offset);
7223e9ca
ILT
1551 return true;
1552}
1553
7ef8ae7c
VR
1554// Add an entry for a local symbol plus ADDEND to the GOT. This returns
1555// true if this is a new GOT entry, false if the symbol already has a GOT
1556// entry.
1557
1558template<int got_size, bool big_endian>
1559bool
1560Output_data_got<got_size, big_endian>::add_local(
1561 Relobj* object,
1562 unsigned int symndx,
1563 unsigned int got_type,
1564 uint64_t addend)
1565{
1566 if (object->local_has_got_offset(symndx, got_type, addend))
1567 return false;
1568
1569 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1570 false, addend));
1571 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1572 return true;
1573}
1574
7223e9ca
ILT
1575// Like add_local, but use the PLT offset.
1576
d77a0555 1577template<int got_size, bool big_endian>
7223e9ca 1578bool
d77a0555 1579Output_data_got<got_size, big_endian>::add_local_plt(
83896202 1580 Relobj* object,
7223e9ca
ILT
1581 unsigned int symndx,
1582 unsigned int got_type)
1583{
1584 if (object->local_has_got_offset(symndx, got_type))
1585 return false;
1586
4829d394
CC
1587 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1588 true));
1589 object->set_local_got_offset(symndx, got_type, got_offset);
07f397ab
ILT
1590 return true;
1591}
1592
0a65a3a7
CC
1593// Add an entry for a local symbol to the GOT, and add a dynamic
1594// relocation of type R_TYPE for the GOT entry.
7223e9ca 1595
d77a0555 1596template<int got_size, bool big_endian>
7bf1f802 1597void
d77a0555 1598Output_data_got<got_size, big_endian>::add_local_with_rel(
83896202 1599 Relobj* object,
0a65a3a7
CC
1600 unsigned int symndx,
1601 unsigned int got_type,
83896202 1602 Output_data_reloc_generic* rel_dyn,
7bf1f802
ILT
1603 unsigned int r_type)
1604{
0a65a3a7 1605 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1606 return;
1607
4829d394 1608 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941 1609 object->set_local_got_offset(symndx, got_type, got_offset);
83896202 1610 rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset, 0);
07f397ab
ILT
1611}
1612
7ef8ae7c
VR
1613// Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
1614// relocation of type R_TYPE for the GOT entry.
1615
1616template<int got_size, bool big_endian>
1617void
1618Output_data_got<got_size, big_endian>::add_local_with_rel(
1619 Relobj* object,
1620 unsigned int symndx,
1621 unsigned int got_type,
1622 Output_data_reloc_generic* rel_dyn,
1623 unsigned int r_type, uint64_t addend)
1624{
1625 if (object->local_has_got_offset(symndx, got_type, addend))
1626 return;
1627
1628 unsigned int got_offset = this->add_got_entry(Got_entry());
1629 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1630 rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset,
1631 addend);
1632}
1633
0a65a3a7 1634// Add a pair of entries for a local symbol to the GOT, and add
bd73a62d
AM
1635// a dynamic relocation of type R_TYPE using the section symbol of
1636// the output section to which input section SHNDX maps, on the first.
1637// The first got entry will have a value of zero, the second the
1638// value of the local symbol.
d77a0555 1639template<int got_size, bool big_endian>
7bf1f802 1640void
d77a0555 1641Output_data_got<got_size, big_endian>::add_local_pair_with_rel(
83896202 1642 Relobj* object,
7bf1f802
ILT
1643 unsigned int symndx,
1644 unsigned int shndx,
0a65a3a7 1645 unsigned int got_type,
83896202 1646 Output_data_reloc_generic* rel_dyn,
bd73a62d 1647 unsigned int r_type)
7bf1f802 1648{
0a65a3a7 1649 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1650 return;
1651
4829d394
CC
1652 unsigned int got_offset =
1653 this->add_got_entry_pair(Got_entry(),
1654 Got_entry(object, symndx, false));
2ea97941 1655 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1656 Output_section* os = object->output_section(shndx);
bd73a62d
AM
1657 rel_dyn->add_output_section_generic(os, r_type, this, got_offset, 0);
1658}
7bf1f802 1659
7ef8ae7c
VR
1660// Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
1661// a dynamic relocation of type R_TYPE using the section symbol of
1662// the output section to which input section SHNDX maps, on the first.
1663// The first got entry will have a value of zero, the second the
1664// value of the local symbol.
1665template<int got_size, bool big_endian>
1666void
1667Output_data_got<got_size, big_endian>::add_local_pair_with_rel(
1668 Relobj* object,
1669 unsigned int symndx,
1670 unsigned int shndx,
1671 unsigned int got_type,
1672 Output_data_reloc_generic* rel_dyn,
1673 unsigned int r_type, uint64_t addend)
1674{
1675 if (object->local_has_got_offset(symndx, got_type, addend))
1676 return;
1677
1678 unsigned int got_offset =
1679 this->add_got_entry_pair(Got_entry(),
1680 Got_entry(object, symndx, false, addend));
1681 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1682 Output_section* os = object->output_section(shndx);
1683 rel_dyn->add_output_section_generic(os, r_type, this, got_offset, addend);
1684}
1685
bd73a62d
AM
1686// Add a pair of entries for a local symbol to the GOT, and add
1687// a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
1688// The first got entry will have a value of zero, the second the
1689// value of the local symbol offset by Target::tls_offset_for_local.
d77a0555 1690template<int got_size, bool big_endian>
bd73a62d 1691void
d77a0555 1692Output_data_got<got_size, big_endian>::add_local_tls_pair(
bd73a62d
AM
1693 Relobj* object,
1694 unsigned int symndx,
1695 unsigned int got_type,
1696 Output_data_reloc_generic* rel_dyn,
1697 unsigned int r_type)
1698{
1699 if (object->local_has_got_offset(symndx, got_type))
1700 return;
1701
1702 unsigned int got_offset
1703 = this->add_got_entry_pair(Got_entry(),
1704 Got_entry(object, symndx, true));
1705 object->set_local_got_offset(symndx, got_type, got_offset);
1706 rel_dyn->add_local_generic(object, 0, r_type, this, got_offset, 0);
4829d394
CC
1707}
1708
1709// Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1710
d77a0555 1711template<int got_size, bool big_endian>
4829d394 1712void
d77a0555 1713Output_data_got<got_size, big_endian>::reserve_local(
6fa2a40b 1714 unsigned int i,
83896202 1715 Relobj* object,
6fa2a40b
CC
1716 unsigned int sym_index,
1717 unsigned int got_type)
4829d394 1718{
dd74ae06 1719 this->do_reserve_slot(i);
6fa2a40b 1720 object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
4829d394 1721}
7bf1f802 1722
4829d394
CC
1723// Reserve a slot in the GOT for a global symbol.
1724
d77a0555 1725template<int got_size, bool big_endian>
4829d394 1726void
d77a0555 1727Output_data_got<got_size, big_endian>::reserve_global(
4829d394
CC
1728 unsigned int i,
1729 Symbol* gsym,
1730 unsigned int got_type)
1731{
dd74ae06 1732 this->do_reserve_slot(i);
4829d394 1733 gsym->set_got_offset(got_type, this->got_offset(i));
7bf1f802
ILT
1734}
1735
ead1e424
ILT
1736// Write out the GOT.
1737
d77a0555 1738template<int got_size, bool big_endian>
ead1e424 1739void
d77a0555 1740Output_data_got<got_size, big_endian>::do_write(Output_file* of)
ead1e424 1741{
d77a0555 1742 const int add = got_size / 8;
ead1e424
ILT
1743
1744 const off_t off = this->offset();
c06b7b0b 1745 const off_t oview_size = this->data_size();
ead1e424
ILT
1746 unsigned char* const oview = of->get_output_view(off, oview_size);
1747
1748 unsigned char* pov = oview;
bd73a62d 1749 for (unsigned int i = 0; i < this->entries_.size(); ++i)
ead1e424 1750 {
bd73a62d 1751 this->entries_[i].write(i, pov);
ead1e424
ILT
1752 pov += add;
1753 }
1754
a3ad94ed 1755 gold_assert(pov - oview == oview_size);
c06b7b0b 1756
ead1e424
ILT
1757 of->write_output_view(off, oview_size, oview);
1758
1759 // We no longer need the GOT entries.
1760 this->entries_.clear();
1761}
1762
4829d394
CC
1763// Create a new GOT entry and return its offset.
1764
d77a0555 1765template<int got_size, bool big_endian>
4829d394 1766unsigned int
d77a0555 1767Output_data_got<got_size, big_endian>::add_got_entry(Got_entry got_entry)
4829d394
CC
1768{
1769 if (!this->is_data_size_valid())
1770 {
1771 this->entries_.push_back(got_entry);
1772 this->set_got_size();
1773 return this->last_got_offset();
1774 }
1775 else
1776 {
1777 // For an incremental update, find an available slot.
d77a0555
ILT
1778 off_t got_offset = this->free_list_.allocate(got_size / 8,
1779 got_size / 8, 0);
4829d394 1780 if (got_offset == -1)
e6455dfb
CC
1781 gold_fallback(_("out of patch space (GOT);"
1782 " relink with --incremental-full"));
d77a0555 1783 unsigned int got_index = got_offset / (got_size / 8);
4829d394
CC
1784 gold_assert(got_index < this->entries_.size());
1785 this->entries_[got_index] = got_entry;
1786 return static_cast<unsigned int>(got_offset);
1787 }
1788}
1789
1790// Create a pair of new GOT entries and return the offset of the first.
1791
d77a0555 1792template<int got_size, bool big_endian>
4829d394 1793unsigned int
d77a0555
ILT
1794Output_data_got<got_size, big_endian>::add_got_entry_pair(
1795 Got_entry got_entry_1,
1796 Got_entry got_entry_2)
4829d394
CC
1797{
1798 if (!this->is_data_size_valid())
1799 {
1800 unsigned int got_offset;
1801 this->entries_.push_back(got_entry_1);
1802 got_offset = this->last_got_offset();
1803 this->entries_.push_back(got_entry_2);
1804 this->set_got_size();
1805 return got_offset;
1806 }
1807 else
1808 {
1809 // For an incremental update, find an available pair of slots.
d77a0555
ILT
1810 off_t got_offset = this->free_list_.allocate(2 * got_size / 8,
1811 got_size / 8, 0);
4829d394 1812 if (got_offset == -1)
e6455dfb
CC
1813 gold_fallback(_("out of patch space (GOT);"
1814 " relink with --incremental-full"));
d77a0555 1815 unsigned int got_index = got_offset / (got_size / 8);
4829d394
CC
1816 gold_assert(got_index < this->entries_.size());
1817 this->entries_[got_index] = got_entry_1;
1818 this->entries_[got_index + 1] = got_entry_2;
1819 return static_cast<unsigned int>(got_offset);
1820 }
1821}
1822
cf43a2fe
AM
1823// Replace GOT entry I with a new value.
1824
d77a0555 1825template<int got_size, bool big_endian>
cf43a2fe 1826void
d77a0555 1827Output_data_got<got_size, big_endian>::replace_got_entry(
cf43a2fe
AM
1828 unsigned int i,
1829 Got_entry got_entry)
1830{
1831 gold_assert(i < this->entries_.size());
1832 this->entries_[i] = got_entry;
1833}
1834
a3ad94ed
ILT
1835// Output_data_dynamic::Dynamic_entry methods.
1836
1837// Write out the entry.
1838
1839template<int size, bool big_endian>
1840void
1841Output_data_dynamic::Dynamic_entry::write(
1842 unsigned char* pov,
7d1a9ebb 1843 const Stringpool* pool) const
a3ad94ed
ILT
1844{
1845 typename elfcpp::Elf_types<size>::Elf_WXword val;
c2b45e22 1846 switch (this->offset_)
a3ad94ed
ILT
1847 {
1848 case DYNAMIC_NUMBER:
1849 val = this->u_.val;
1850 break;
1851
a3ad94ed 1852 case DYNAMIC_SECTION_SIZE:
16649710 1853 val = this->u_.od->data_size();
612a8d3d
DM
1854 if (this->od2 != NULL)
1855 val += this->od2->data_size();
a3ad94ed
ILT
1856 break;
1857
1858 case DYNAMIC_SYMBOL:
1859 {
16649710
ILT
1860 const Sized_symbol<size>* s =
1861 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1862 val = s->value();
1863 }
1864 break;
1865
1866 case DYNAMIC_STRING:
1867 val = pool->get_offset(this->u_.str);
1868 break;
1869
918fc1f8
CC
1870 case DYNAMIC_CUSTOM:
1871 val = parameters->target().dynamic_tag_custom_value(this->tag_);
1872 break;
1873
a3ad94ed 1874 default:
c2b45e22
CC
1875 val = this->u_.od->address() + this->offset_;
1876 break;
a3ad94ed
ILT
1877 }
1878
1879 elfcpp::Dyn_write<size, big_endian> dw(pov);
1880 dw.put_d_tag(this->tag_);
1881 dw.put_d_val(val);
1882}
1883
1884// Output_data_dynamic methods.
1885
16649710
ILT
1886// Adjust the output section to set the entry size.
1887
1888void
1889Output_data_dynamic::do_adjust_output_section(Output_section* os)
1890{
8851ecca 1891 if (parameters->target().get_size() == 32)
16649710 1892 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1893 else if (parameters->target().get_size() == 64)
16649710
ILT
1894 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1895 else
1896 gold_unreachable();
1897}
1898
a8ecc9fe
VR
1899// Get a dynamic entry offset.
1900
1901unsigned int
1902Output_data_dynamic::get_entry_offset(elfcpp::DT tag) const
1903{
1904 int dyn_size;
1905
1906 if (parameters->target().get_size() == 32)
1907 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1908 else if (parameters->target().get_size() == 64)
1909 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1910 else
1911 gold_unreachable();
1912
1913 for (size_t i = 0; i < entries_.size(); ++i)
1914 if (entries_[i].tag() == tag)
1915 return i * dyn_size;
1916
1917 return -1U;
1918}
1919
a3ad94ed
ILT
1920// Set the final data size.
1921
1922void
27bc2bce 1923Output_data_dynamic::set_final_data_size()
a3ad94ed 1924{
20e6d0d6
DK
1925 // Add the terminating entry if it hasn't been added.
1926 // Because of relaxation, we can run this multiple times.
9e9e071b
ILT
1927 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1928 {
1929 int extra = parameters->options().spare_dynamic_tags();
1930 for (int i = 0; i < extra; ++i)
1931 this->add_constant(elfcpp::DT_NULL, 0);
1932 this->add_constant(elfcpp::DT_NULL, 0);
1933 }
a3ad94ed
ILT
1934
1935 int dyn_size;
8851ecca 1936 if (parameters->target().get_size() == 32)
a3ad94ed 1937 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1938 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1939 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1940 else
1941 gold_unreachable();
1942 this->set_data_size(this->entries_.size() * dyn_size);
1943}
1944
1945// Write out the dynamic entries.
1946
1947void
1948Output_data_dynamic::do_write(Output_file* of)
1949{
8851ecca 1950 switch (parameters->size_and_endianness())
a3ad94ed 1951 {
9025d29d 1952#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1953 case Parameters::TARGET_32_LITTLE:
1954 this->sized_write<32, false>(of);
1955 break;
9025d29d 1956#endif
8851ecca
ILT
1957#ifdef HAVE_TARGET_32_BIG
1958 case Parameters::TARGET_32_BIG:
1959 this->sized_write<32, true>(of);
1960 break;
9025d29d 1961#endif
9025d29d 1962#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1963 case Parameters::TARGET_64_LITTLE:
1964 this->sized_write<64, false>(of);
1965 break;
9025d29d 1966#endif
8851ecca
ILT
1967#ifdef HAVE_TARGET_64_BIG
1968 case Parameters::TARGET_64_BIG:
1969 this->sized_write<64, true>(of);
1970 break;
1971#endif
1972 default:
1973 gold_unreachable();
a3ad94ed 1974 }
a3ad94ed
ILT
1975}
1976
1977template<int size, bool big_endian>
1978void
1979Output_data_dynamic::sized_write(Output_file* of)
1980{
1981 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1982
2ea97941 1983 const off_t offset = this->offset();
a3ad94ed 1984 const off_t oview_size = this->data_size();
2ea97941 1985 unsigned char* const oview = of->get_output_view(offset, oview_size);
a3ad94ed
ILT
1986
1987 unsigned char* pov = oview;
1988 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1989 p != this->entries_.end();
1990 ++p)
1991 {
7d1a9ebb 1992 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1993 pov += dyn_size;
1994 }
1995
1996 gold_assert(pov - oview == oview_size);
1997
2ea97941 1998 of->write_output_view(offset, oview_size, oview);
a3ad94ed
ILT
1999
2000 // We no longer need the dynamic entries.
2001 this->entries_.clear();
2002}
2003
d491d34e
ILT
2004// Class Output_symtab_xindex.
2005
2006void
2007Output_symtab_xindex::do_write(Output_file* of)
2008{
2ea97941 2009 const off_t offset = this->offset();
d491d34e 2010 const off_t oview_size = this->data_size();
2ea97941 2011 unsigned char* const oview = of->get_output_view(offset, oview_size);
d491d34e
ILT
2012
2013 memset(oview, 0, oview_size);
2014
2015 if (parameters->target().is_big_endian())
2016 this->endian_do_write<true>(oview);
2017 else
2018 this->endian_do_write<false>(oview);
2019
2ea97941 2020 of->write_output_view(offset, oview_size, oview);
d491d34e
ILT
2021
2022 // We no longer need the data.
2023 this->entries_.clear();
2024}
2025
2026template<bool big_endian>
2027void
2028Output_symtab_xindex::endian_do_write(unsigned char* const oview)
2029{
2030 for (Xindex_entries::const_iterator p = this->entries_.begin();
2031 p != this->entries_.end();
2032 ++p)
20e6d0d6
DK
2033 {
2034 unsigned int symndx = p->first;
50ed5eb1 2035 gold_assert(static_cast<off_t>(symndx) * 4 < this->data_size());
20e6d0d6
DK
2036 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
2037 }
d491d34e
ILT
2038}
2039
8ea8cd50
CC
2040// Output_fill_debug_info methods.
2041
2042// Return the minimum size needed for a dummy compilation unit header.
2043
2044size_t
2045Output_fill_debug_info::do_minimum_hole_size() const
2046{
2047 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
2048 // address_size.
2049 const size_t len = 4 + 2 + 4 + 1;
2050 // For type units, add type_signature, type_offset.
2051 if (this->is_debug_types_)
2052 return len + 8 + 4;
2053 return len;
2054}
2055
2056// Write a dummy compilation unit header to fill a hole in the
2057// .debug_info or .debug_types section.
2058
2059void
2060Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
2061{
66570254
CC
2062 gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
2063 static_cast<long>(off), static_cast<long>(len));
8ea8cd50
CC
2064
2065 gold_assert(len >= this->do_minimum_hole_size());
2066
2067 unsigned char* const oview = of->get_output_view(off, len);
2068 unsigned char* pov = oview;
2069
2070 // Write header fields: unit_length, version, debug_abbrev_offset,
2071 // address_size.
2072 if (this->is_big_endian())
2073 {
24c6c55a
DM
2074 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2075 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2076 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
8ea8cd50
CC
2077 }
2078 else
2079 {
24c6c55a
DM
2080 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2081 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2082 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
8ea8cd50
CC
2083 }
2084 pov += 4 + 2 + 4;
2085 *pov++ = 4;
2086
2087 // For type units, the additional header fields -- type_signature,
2088 // type_offset -- can be filled with zeroes.
2089
2090 // Fill the remainder of the free space with zeroes. The first
2091 // zero should tell the consumer there are no DIEs to read in this
2092 // compilation unit.
2093 if (pov < oview + len)
2094 memset(pov, 0, oview + len - pov);
2095
2096 of->write_output_view(off, len, oview);
2097}
2098
2099// Output_fill_debug_line methods.
2100
2101// Return the minimum size needed for a dummy line number program header.
2102
2103size_t
2104Output_fill_debug_line::do_minimum_hole_size() const
2105{
2106 // Line number program header fields: unit_length, version, header_length,
2107 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2108 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2109 const size_t len = 4 + 2 + 4 + this->header_length;
2110 return len;
2111}
2112
2113// Write a dummy line number program header to fill a hole in the
2114// .debug_line section.
2115
2116void
2117Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
2118{
66570254
CC
2119 gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
2120 static_cast<long>(off), static_cast<long>(len));
8ea8cd50
CC
2121
2122 gold_assert(len >= this->do_minimum_hole_size());
2123
2124 unsigned char* const oview = of->get_output_view(off, len);
2125 unsigned char* pov = oview;
2126
2127 // Write header fields: unit_length, version, header_length,
2128 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2129 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2130 // We set the header_length field to cover the entire hole, so the
2131 // line number program is empty.
2132 if (this->is_big_endian())
2133 {
24c6c55a
DM
2134 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2135 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2136 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
8ea8cd50
CC
2137 }
2138 else
2139 {
24c6c55a
DM
2140 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2141 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2142 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
8ea8cd50
CC
2143 }
2144 pov += 4 + 2 + 4;
2145 *pov++ = 1; // minimum_instruction_length
2146 *pov++ = 0; // default_is_stmt
2147 *pov++ = 0; // line_base
2148 *pov++ = 5; // line_range
2149 *pov++ = 13; // opcode_base
2150 *pov++ = 0; // standard_opcode_lengths[1]
2151 *pov++ = 1; // standard_opcode_lengths[2]
2152 *pov++ = 1; // standard_opcode_lengths[3]
2153 *pov++ = 1; // standard_opcode_lengths[4]
2154 *pov++ = 1; // standard_opcode_lengths[5]
2155 *pov++ = 0; // standard_opcode_lengths[6]
2156 *pov++ = 0; // standard_opcode_lengths[7]
2157 *pov++ = 0; // standard_opcode_lengths[8]
2158 *pov++ = 1; // standard_opcode_lengths[9]
2159 *pov++ = 0; // standard_opcode_lengths[10]
2160 *pov++ = 0; // standard_opcode_lengths[11]
2161 *pov++ = 1; // standard_opcode_lengths[12]
2162 *pov++ = 0; // include_directories (empty)
2163 *pov++ = 0; // filenames (empty)
2164
2165 // Some consumers don't check the header_length field, and simply
2166 // start reading the line number program immediately following the
2167 // header. For those consumers, we fill the remainder of the free
2168 // space with DW_LNS_set_basic_block opcodes. These are effectively
2169 // no-ops: the resulting line table program will not create any rows.
2170 if (pov < oview + len)
2171 memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2172
2173 of->write_output_view(off, len, oview);
2174}
2175
ead1e424
ILT
2176// Output_section::Input_section methods.
2177
cdc29364
CC
2178// Return the current data size. For an input section we store the size here.
2179// For an Output_section_data, we have to ask it for the size.
2180
2181off_t
2182Output_section::Input_section::current_data_size() const
2183{
2184 if (this->is_input_section())
2185 return this->u1_.data_size;
2186 else
2187 {
2188 this->u2_.posd->pre_finalize_data_size();
2189 return this->u2_.posd->current_data_size();
2190 }
2191}
2192
ead1e424
ILT
2193// Return the data size. For an input section we store the size here.
2194// For an Output_section_data, we have to ask it for the size.
2195
2196off_t
2197Output_section::Input_section::data_size() const
2198{
2199 if (this->is_input_section())
b8e6aad9 2200 return this->u1_.data_size;
ead1e424 2201 else
b8e6aad9 2202 return this->u2_.posd->data_size();
ead1e424
ILT
2203}
2204
0439c796
DK
2205// Return the object for an input section.
2206
2207Relobj*
2208Output_section::Input_section::relobj() const
2209{
2210 if (this->is_input_section())
2211 return this->u2_.object;
2212 else if (this->is_merge_section())
2213 {
2214 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2215 return this->u2_.pomb->first_relobj();
2216 }
2217 else if (this->is_relaxed_input_section())
2218 return this->u2_.poris->relobj();
2219 else
2220 gold_unreachable();
2221}
2222
2223// Return the input section index for an input section.
2224
2225unsigned int
2226Output_section::Input_section::shndx() const
2227{
2228 if (this->is_input_section())
2229 return this->shndx_;
2230 else if (this->is_merge_section())
2231 {
2232 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2233 return this->u2_.pomb->first_shndx();
2234 }
2235 else if (this->is_relaxed_input_section())
2236 return this->u2_.poris->shndx();
2237 else
2238 gold_unreachable();
2239}
2240
ead1e424
ILT
2241// Set the address and file offset.
2242
2243void
96803768
ILT
2244Output_section::Input_section::set_address_and_file_offset(
2245 uint64_t address,
2246 off_t file_offset,
2247 off_t section_file_offset)
ead1e424
ILT
2248{
2249 if (this->is_input_section())
96803768
ILT
2250 this->u2_.object->set_section_offset(this->shndx_,
2251 file_offset - section_file_offset);
ead1e424 2252 else
96803768
ILT
2253 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2254}
2255
a445fddf
ILT
2256// Reset the address and file offset.
2257
2258void
2259Output_section::Input_section::reset_address_and_file_offset()
2260{
2261 if (!this->is_input_section())
2262 this->u2_.posd->reset_address_and_file_offset();
2263}
2264
96803768
ILT
2265// Finalize the data size.
2266
2267void
2268Output_section::Input_section::finalize_data_size()
2269{
2270 if (!this->is_input_section())
2271 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
2272}
2273
1e983657
ILT
2274// Try to turn an input offset into an output offset. We want to
2275// return the output offset relative to the start of this
2276// Input_section in the output section.
b8e6aad9 2277
8f00aeb8 2278inline bool
8383303e
ILT
2279Output_section::Input_section::output_offset(
2280 const Relobj* object,
2ea97941
ILT
2281 unsigned int shndx,
2282 section_offset_type offset,
ca09d69a 2283 section_offset_type* poutput) const
b8e6aad9
ILT
2284{
2285 if (!this->is_input_section())
2ea97941 2286 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
2287 else
2288 {
2ea97941 2289 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 2290 return false;
2ea97941 2291 *poutput = offset;
b8e6aad9
ILT
2292 return true;
2293 }
ead1e424
ILT
2294}
2295
2296// Write out the data. We don't have to do anything for an input
2297// section--they are handled via Object::relocate--but this is where
2298// we write out the data for an Output_section_data.
2299
2300void
2301Output_section::Input_section::write(Output_file* of)
2302{
2303 if (!this->is_input_section())
b8e6aad9 2304 this->u2_.posd->write(of);
ead1e424
ILT
2305}
2306
96803768
ILT
2307// Write the data to a buffer. As for write(), we don't have to do
2308// anything for an input section.
2309
2310void
2311Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2312{
2313 if (!this->is_input_section())
2314 this->u2_.posd->write_to_buffer(buffer);
2315}
2316
7d9e3d98
ILT
2317// Print to a map file.
2318
2319void
2320Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2321{
2322 switch (this->shndx_)
2323 {
2324 case OUTPUT_SECTION_CODE:
2325 case MERGE_DATA_SECTION_CODE:
2326 case MERGE_STRING_SECTION_CODE:
2327 this->u2_.posd->print_to_mapfile(mapfile);
2328 break;
2329
20e6d0d6
DK
2330 case RELAXED_INPUT_SECTION_CODE:
2331 {
eb426534 2332 Output_relaxed_input_section* relaxed_section =
20e6d0d6 2333 this->relaxed_input_section();
eb426534 2334 mapfile->print_input_section(relaxed_section->relobj(),
20e6d0d6
DK
2335 relaxed_section->shndx());
2336 }
2337 break;
7d9e3d98
ILT
2338 default:
2339 mapfile->print_input_section(this->u2_.object, this->shndx_);
2340 break;
2341 }
2342}
2343
a2fb1b05
ILT
2344// Output_section methods.
2345
2346// Construct an Output_section. NAME will point into a Stringpool.
2347
2ea97941
ILT
2348Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2349 elfcpp::Elf_Xword flags)
2350 : name_(name),
a2fb1b05
ILT
2351 addralign_(0),
2352 entsize_(0),
a445fddf 2353 load_address_(0),
16649710 2354 link_section_(NULL),
a2fb1b05 2355 link_(0),
16649710 2356 info_section_(NULL),
6a74a719 2357 info_symndx_(NULL),
a2fb1b05 2358 info_(0),
2ea97941
ILT
2359 type_(type),
2360 flags_(flags),
22f0da72 2361 order_(ORDER_INVALID),
91ea499d 2362 out_shndx_(-1U),
c06b7b0b
ILT
2363 symtab_index_(0),
2364 dynsym_index_(0),
ead1e424
ILT
2365 input_sections_(),
2366 first_input_offset_(0),
c51e6221 2367 fills_(),
96803768 2368 postprocessing_buffer_(NULL),
a3ad94ed 2369 needs_symtab_index_(false),
16649710
ILT
2370 needs_dynsym_index_(false),
2371 should_link_to_symtab_(false),
730cdc88 2372 should_link_to_dynsym_(false),
27bc2bce 2373 after_input_sections_(false),
7bf1f802 2374 requires_postprocessing_(false),
a445fddf
ILT
2375 found_in_sections_clause_(false),
2376 has_load_address_(false),
755ab8af 2377 info_uses_section_index_(false),
6e9ba2ca 2378 input_section_order_specified_(false),
2fd32231
ILT
2379 may_sort_attached_input_sections_(false),
2380 must_sort_attached_input_sections_(false),
2381 attached_input_sections_are_sorted_(false),
9f1d377b 2382 is_relro_(false),
8a5e3e08
ILT
2383 is_small_section_(false),
2384 is_large_section_(false),
f5c870d2 2385 generate_code_fills_at_write_(false),
e8cd95c7 2386 is_entsize_zero_(false),
8923b24c 2387 section_offsets_need_adjustment_(false),
1e5d2fb1 2388 is_noload_(false),
131687b4 2389 always_keeps_input_sections_(false),
cdc29364 2390 has_fixed_layout_(false),
9fbd3822 2391 is_patch_space_allowed_(false),
16164a6b 2392 is_unique_segment_(false),
20e6d0d6 2393 tls_offset_(0),
16164a6b
ST
2394 extra_segment_flags_(0),
2395 segment_alignment_(0),
c0a62865 2396 checkpoint_(NULL),
cdc29364 2397 lookup_maps_(new Output_section_lookup_maps),
9fbd3822 2398 free_list_(),
8ea8cd50 2399 free_space_fill_(NULL),
bce5a025
CC
2400 patch_space_(0),
2401 reloc_section_(NULL)
a2fb1b05 2402{
27bc2bce
ILT
2403 // An unallocated section has no address. Forcing this means that
2404 // we don't need special treatment for symbols defined in debug
2405 // sections.
2ea97941 2406 if ((flags & elfcpp::SHF_ALLOC) == 0)
27bc2bce 2407 this->set_address(0);
a2fb1b05
ILT
2408}
2409
54dc6425
ILT
2410Output_section::~Output_section()
2411{
20e6d0d6 2412 delete this->checkpoint_;
54dc6425
ILT
2413}
2414
16649710
ILT
2415// Set the entry size.
2416
2417void
2418Output_section::set_entsize(uint64_t v)
2419{
e8cd95c7
ILT
2420 if (this->is_entsize_zero_)
2421 ;
2422 else if (this->entsize_ == 0)
16649710 2423 this->entsize_ = v;
e8cd95c7
ILT
2424 else if (this->entsize_ != v)
2425 {
2426 this->entsize_ = 0;
2427 this->is_entsize_zero_ = 1;
2428 }
16649710
ILT
2429}
2430
ead1e424 2431// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
2432// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2433// relocation section which applies to this section, or 0 if none, or
2434// -1U if more than one. Return the offset of the input section
2435// within the output section. Return -1 if the input section will
2436// receive special handling. In the normal case we don't always keep
2437// track of input sections for an Output_section. Instead, each
2438// Object keeps track of the Output_section for each of its input
a445fddf
ILT
2439// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2440// track of input sections here; this is used when SECTIONS appears in
2441// a linker script.
a2fb1b05
ILT
2442
2443template<int size, bool big_endian>
2444off_t
6e9ba2ca 2445Output_section::add_input_section(Layout* layout,
6fa2a40b 2446 Sized_relobj_file<size, big_endian>* object,
2ea97941 2447 unsigned int shndx,
ead1e424 2448 const char* secname,
730cdc88 2449 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
2450 unsigned int reloc_shndx,
2451 bool have_sections_script)
a2fb1b05 2452{
5f6c22ae
L
2453 section_size_type input_section_size = shdr.get_sh_size();
2454 section_size_type uncompressed_size;
2ea97941 2455 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
5f6c22ae
L
2456 if (object->section_is_compressed(shndx, &uncompressed_size,
2457 &addralign))
2458 input_section_size = uncompressed_size;
2459
2ea97941 2460 if ((addralign & (addralign - 1)) != 0)
a2fb1b05 2461 {
75f2446e 2462 object->error(_("invalid alignment %lu for section \"%s\""),
2ea97941
ILT
2463 static_cast<unsigned long>(addralign), secname);
2464 addralign = 1;
a2fb1b05 2465 }
a2fb1b05 2466
2ea97941
ILT
2467 if (addralign > this->addralign_)
2468 this->addralign_ = addralign;
a2fb1b05 2469
44a43cf9 2470 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2ea97941 2471 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
2472
2473 // .debug_str is a mergeable string section, but is not always so
2474 // marked by compilers. Mark manually here so we can optimize.
2475 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
2476 {
2477 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2ea97941 2478 entsize = 1;
4f833eee 2479 }
44a43cf9 2480
e8cd95c7
ILT
2481 this->update_flags_for_input_section(sh_flags);
2482 this->set_entsize(entsize);
2483
b8e6aad9 2484 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88 2485 // a Output_data_merge. We don't try to handle relocations for such
e0b64032
ILT
2486 // a section. We don't try to handle empty merge sections--they
2487 // mess up the mappings, and are useless anyhow.
cdc29364 2488 // FIXME: Need to handle merge sections during incremental update.
44a43cf9 2489 if ((sh_flags & elfcpp::SHF_MERGE) != 0
e0b64032 2490 && reloc_shndx == 0
cdc29364
CC
2491 && shdr.get_sh_size() > 0
2492 && !parameters->incremental())
b8e6aad9 2493 {
0439c796
DK
2494 // Keep information about merged input sections for rebuilding fast
2495 // lookup maps if we have sections-script or we do relaxation.
131687b4
DK
2496 bool keeps_input_sections = (this->always_keeps_input_sections_
2497 || have_sections_script
2498 || parameters->target().may_relax());
2499
0439c796
DK
2500 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2501 addralign, keeps_input_sections))
b8e6aad9
ILT
2502 {
2503 // Tell the relocation routines that they need to call the
730cdc88 2504 // output_offset method to determine the final address.
b8e6aad9
ILT
2505 return -1;
2506 }
2507 }
2508
cdc29364 2509 off_t offset_in_section;
3136a00e 2510
cdc29364
CC
2511 if (this->has_fixed_layout())
2512 {
2513 // For incremental updates, find a chunk of unused space in the section.
2514 offset_in_section = this->free_list_.allocate(input_section_size,
2515 addralign, 0);
2516 if (offset_in_section == -1)
eb426534 2517 gold_fallback(_("out of patch space in section %s; "
9fbd3822
CC
2518 "relink with --incremental-full"),
2519 this->name());
3136a00e 2520 return offset_in_section;
cdc29364 2521 }
c51e6221 2522
3136a00e
CC
2523 offset_in_section = this->current_data_size_for_child();
2524 off_t aligned_offset_in_section = align_address(offset_in_section,
2525 addralign);
2526 this->set_current_data_size_for_child(aligned_offset_in_section
2527 + input_section_size);
2528
c0a62865
DK
2529 // Determine if we want to delay code-fill generation until the output
2530 // section is written. When the target is relaxing, we want to delay fill
4cf7a849
ST
2531 // generating to avoid adjusting them during relaxation. Also, if we are
2532 // sorting input sections we must delay fill generation.
c0a62865
DK
2533 if (!this->generate_code_fills_at_write_
2534 && !have_sections_script
2535 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2536 && parameters->target().has_code_fill()
4cf7a849 2537 && (parameters->target().may_relax()
eb426534 2538 || layout->is_section_ordering_specified()))
c0a62865
DK
2539 {
2540 gold_assert(this->fills_.empty());
2541 this->generate_code_fills_at_write_ = true;
2542 }
2543
c51e6221 2544 if (aligned_offset_in_section > offset_in_section
c0a62865 2545 && !this->generate_code_fills_at_write_
a445fddf 2546 && !have_sections_script
44a43cf9 2547 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
029ba973 2548 && parameters->target().has_code_fill())
c51e6221
ILT
2549 {
2550 // We need to add some fill data. Using fill_list_ when
2551 // possible is an optimization, since we will often have fill
2552 // sections without input sections.
2553 off_t fill_len = aligned_offset_in_section - offset_in_section;
2554 if (this->input_sections_.empty())
eb426534 2555 this->fills_.push_back(Fill(offset_in_section, fill_len));
c51e6221 2556 else
eb426534
RM
2557 {
2558 std::string fill_data(parameters->target().code_fill(fill_len));
2559 Output_data_const* odc = new Output_data_const(fill_data, 1);
2560 this->input_sections_.push_back(Input_section(odc));
2561 }
c51e6221
ILT
2562 }
2563
ead1e424 2564 // We need to keep track of this section if we are already keeping
2fd32231
ILT
2565 // track of sections, or if we are relaxing. Also, if this is a
2566 // section which requires sorting, or which may require sorting in
6e9ba2ca
ST
2567 // the future, we keep track of the sections. If the
2568 // --section-ordering-file option is used to specify the order of
2569 // sections, we need to keep track of sections.
131687b4
DK
2570 if (this->always_keeps_input_sections_
2571 || have_sections_script
2fd32231
ILT
2572 || !this->input_sections_.empty()
2573 || this->may_sort_attached_input_sections()
7d9e3d98 2574 || this->must_sort_attached_input_sections()
20e6d0d6 2575 || parameters->options().user_set_Map()
6e9ba2ca 2576 || parameters->target().may_relax()
edcac0c1 2577 || layout->is_section_ordering_specified())
6e9ba2ca 2578 {
6fc6ea19 2579 Input_section isecn(object, shndx, input_section_size, addralign);
f0558624
ST
2580 /* If section ordering is requested by specifying a ordering file,
2581 using --section-ordering-file, match the section name with
2582 a pattern. */
2583 if (parameters->options().section_ordering_file())
eb426534
RM
2584 {
2585 unsigned int section_order_index =
2586 layout->find_section_order_index(std::string(secname));
6e9ba2ca 2587 if (section_order_index != 0)
eb426534
RM
2588 {
2589 isecn.set_section_order_index(section_order_index);
2590 this->set_input_section_order_specified();
2591 }
2592 }
6e9ba2ca
ST
2593 this->input_sections_.push_back(isecn);
2594 }
54dc6425 2595
c51e6221 2596 return aligned_offset_in_section;
61ba1cf9
ILT
2597}
2598
ead1e424
ILT
2599// Add arbitrary data to an output section.
2600
2601void
2602Output_section::add_output_section_data(Output_section_data* posd)
2603{
b8e6aad9
ILT
2604 Input_section inp(posd);
2605 this->add_output_section_data(&inp);
a445fddf
ILT
2606
2607 if (posd->is_data_size_valid())
2608 {
cdc29364
CC
2609 off_t offset_in_section;
2610 if (this->has_fixed_layout())
2611 {
2612 // For incremental updates, find a chunk of unused space.
2613 offset_in_section = this->free_list_.allocate(posd->data_size(),
2614 posd->addralign(), 0);
2615 if (offset_in_section == -1)
9fbd3822
CC
2616 gold_fallback(_("out of patch space in section %s; "
2617 "relink with --incremental-full"),
2618 this->name());
cdc29364
CC
2619 // Finalize the address and offset now.
2620 uint64_t addr = this->address();
2621 off_t offset = this->offset();
2622 posd->set_address_and_file_offset(addr + offset_in_section,
2623 offset + offset_in_section);
2624 }
2625 else
2626 {
2627 offset_in_section = this->current_data_size_for_child();
2628 off_t aligned_offset_in_section = align_address(offset_in_section,
2629 posd->addralign());
2630 this->set_current_data_size_for_child(aligned_offset_in_section
2631 + posd->data_size());
2632 }
2633 }
2634 else if (this->has_fixed_layout())
2635 {
2636 // For incremental updates, arrange for the data to have a fixed layout.
2637 // This will mean that additions to the data must be allocated from
2638 // free space within the containing output section.
2639 uint64_t addr = this->address();
2640 posd->set_address(addr);
2641 posd->set_file_offset(0);
4829d394
CC
2642 // FIXME: This should eventually be unreachable.
2643 // gold_unreachable();
a445fddf 2644 }
b8e6aad9
ILT
2645}
2646
c0a62865
DK
2647// Add a relaxed input section.
2648
2649void
d06fb4d1
DK
2650Output_section::add_relaxed_input_section(Layout* layout,
2651 Output_relaxed_input_section* poris,
2652 const std::string& name)
c0a62865
DK
2653{
2654 Input_section inp(poris);
d06fb4d1
DK
2655
2656 // If the --section-ordering-file option is used to specify the order of
2657 // sections, we need to keep track of sections.
e9552f7e 2658 if (layout->is_section_ordering_specified())
d06fb4d1
DK
2659 {
2660 unsigned int section_order_index =
eb426534 2661 layout->find_section_order_index(name);
d06fb4d1 2662 if (section_order_index != 0)
eb426534
RM
2663 {
2664 inp.set_section_order_index(section_order_index);
2665 this->set_input_section_order_specified();
2666 }
d06fb4d1
DK
2667 }
2668
c0a62865 2669 this->add_output_section_data(&inp);
0439c796
DK
2670 if (this->lookup_maps_->is_valid())
2671 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2672 poris->shndx(), poris);
c0a62865
DK
2673
2674 // For a relaxed section, we use the current data size. Linker scripts
2675 // get all the input sections, including relaxed one from an output
19fec8c1 2676 // section and add them back to the same output section to compute the
c0a62865 2677 // output section size. If we do not account for sizes of relaxed input
19fec8c1 2678 // sections, an output section would be incorrectly sized.
c0a62865
DK
2679 off_t offset_in_section = this->current_data_size_for_child();
2680 off_t aligned_offset_in_section = align_address(offset_in_section,
2681 poris->addralign());
2682 this->set_current_data_size_for_child(aligned_offset_in_section
2683 + poris->current_data_size());
2684}
2685
b8e6aad9 2686// Add arbitrary data to an output section by Input_section.
c06b7b0b 2687
b8e6aad9
ILT
2688void
2689Output_section::add_output_section_data(Input_section* inp)
2690{
ead1e424 2691 if (this->input_sections_.empty())
27bc2bce 2692 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 2693
b8e6aad9 2694 this->input_sections_.push_back(*inp);
c06b7b0b 2695
2ea97941
ILT
2696 uint64_t addralign = inp->addralign();
2697 if (addralign > this->addralign_)
2698 this->addralign_ = addralign;
c06b7b0b 2699
b8e6aad9
ILT
2700 inp->set_output_section(this);
2701}
2702
2703// Add a merge section to an output section.
2704
2705void
2706Output_section::add_output_merge_section(Output_section_data* posd,
2ea97941 2707 bool is_string, uint64_t entsize)
b8e6aad9 2708{
2ea97941 2709 Input_section inp(posd, is_string, entsize);
b8e6aad9
ILT
2710 this->add_output_section_data(&inp);
2711}
2712
2713// Add an input section to a SHF_MERGE section.
2714
2715bool
2ea97941
ILT
2716Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2717 uint64_t flags, uint64_t entsize,
0439c796
DK
2718 uint64_t addralign,
2719 bool keeps_input_sections)
b8e6aad9 2720{
2c7b626c
CC
2721 // We cannot merge sections with entsize == 0.
2722 if (entsize == 0)
2723 return false;
2724
2ea97941 2725 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
87f95776 2726
20e6d0d6
DK
2727 // We cannot restore merged input section states.
2728 gold_assert(this->checkpoint_ == NULL);
2729
c0a62865 2730 // Look up merge sections by required properties.
0439c796
DK
2731 // Currently, we only invalidate the lookup maps in script processing
2732 // and relaxation. We should not have done either when we reach here.
2733 // So we assume that the lookup maps are valid to simply code.
2734 gold_assert(this->lookup_maps_->is_valid());
2ea97941 2735 Merge_section_properties msp(is_string, entsize, addralign);
0439c796
DK
2736 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2737 bool is_new = false;
2738 if (pomb != NULL)
c0a62865 2739 {
6bf924b0
DK
2740 gold_assert(pomb->is_string() == is_string
2741 && pomb->entsize() == entsize
2742 && pomb->addralign() == addralign);
c0a62865 2743 }
b8e6aad9
ILT
2744 else
2745 {
6bf924b0
DK
2746 // Create a new Output_merge_data or Output_merge_string_data.
2747 if (!is_string)
2748 pomb = new Output_merge_data(entsize, addralign);
2749 else
9a0910c3 2750 {
6bf924b0
DK
2751 switch (entsize)
2752 {
2753 case 1:
2754 pomb = new Output_merge_string<char>(addralign);
2755 break;
2756 case 2:
2757 pomb = new Output_merge_string<uint16_t>(addralign);
2758 break;
2759 case 4:
2760 pomb = new Output_merge_string<uint32_t>(addralign);
2761 break;
2762 default:
2763 return false;
2764 }
9a0910c3 2765 }
0439c796
DK
2766 // If we need to do script processing or relaxation, we need to keep
2767 // the original input sections to rebuild the fast lookup maps.
2768 if (keeps_input_sections)
2769 pomb->set_keeps_input_sections();
2770 is_new = true;
b8e6aad9
ILT
2771 }
2772
6bf924b0
DK
2773 if (pomb->add_input_section(object, shndx))
2774 {
0439c796
DK
2775 // Add new merge section to this output section and link merge
2776 // section properties to new merge section in map.
2777 if (is_new)
2778 {
2779 this->add_output_merge_section(pomb, is_string, entsize);
2780 this->lookup_maps_->add_merge_section(msp, pomb);
2781 }
2782
6bf924b0
DK
2783 return true;
2784 }
2785 else
0439c796
DK
2786 {
2787 // If add_input_section failed, delete new merge section to avoid
2788 // exporting empty merge sections in Output_section::get_input_section.
2789 if (is_new)
2790 delete pomb;
2791 return false;
2792 }
b8e6aad9
ILT
2793}
2794
c0a62865 2795// Build a relaxation map to speed up relaxation of existing input sections.
2ea97941 2796// Look up to the first LIMIT elements in INPUT_SECTIONS.
c0a62865 2797
20e6d0d6 2798void
c0a62865 2799Output_section::build_relaxation_map(
2ea97941 2800 const Input_section_list& input_sections,
c0a62865
DK
2801 size_t limit,
2802 Relaxation_map* relaxation_map) const
20e6d0d6 2803{
c0a62865
DK
2804 for (size_t i = 0; i < limit; ++i)
2805 {
2ea97941 2806 const Input_section& is(input_sections[i]);
c0a62865
DK
2807 if (is.is_input_section() || is.is_relaxed_input_section())
2808 {
5ac169d4
DK
2809 Section_id sid(is.relobj(), is.shndx());
2810 (*relaxation_map)[sid] = i;
c0a62865
DK
2811 }
2812 }
2813}
2814
2815// Convert regular input sections in INPUT_SECTIONS into relaxed input
5ac169d4
DK
2816// sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2817// indices of INPUT_SECTIONS.
20e6d0d6 2818
c0a62865
DK
2819void
2820Output_section::convert_input_sections_in_list_to_relaxed_sections(
2821 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2822 const Relaxation_map& map,
2ea97941 2823 Input_section_list* input_sections)
c0a62865
DK
2824{
2825 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2826 {
2827 Output_relaxed_input_section* poris = relaxed_sections[i];
5ac169d4
DK
2828 Section_id sid(poris->relobj(), poris->shndx());
2829 Relaxation_map::const_iterator p = map.find(sid);
c0a62865 2830 gold_assert(p != map.end());
2ea97941 2831 gold_assert((*input_sections)[p->second].is_input_section());
d06fb4d1
DK
2832
2833 // Remember section order index of original input section
2834 // if it is set. Copy it to the relaxed input section.
2835 unsigned int soi =
2836 (*input_sections)[p->second].section_order_index();
2ea97941 2837 (*input_sections)[p->second] = Input_section(poris);
d06fb4d1 2838 (*input_sections)[p->second].set_section_order_index(soi);
c0a62865
DK
2839 }
2840}
50ed5eb1 2841
c0a62865
DK
2842// Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2843// is a vector of pointers to Output_relaxed_input_section or its derived
2844// classes. The relaxed sections must correspond to existing input sections.
2845
2846void
2847Output_section::convert_input_sections_to_relaxed_sections(
2848 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2849{
029ba973 2850 gold_assert(parameters->target().may_relax());
20e6d0d6 2851
c0a62865
DK
2852 // We want to make sure that restore_states does not undo the effect of
2853 // this. If there is no checkpoint active, just search the current
2854 // input section list and replace the sections there. If there is
2855 // a checkpoint, also replace the sections there.
50ed5eb1 2856
c0a62865
DK
2857 // By default, we look at the whole list.
2858 size_t limit = this->input_sections_.size();
2859
2860 if (this->checkpoint_ != NULL)
20e6d0d6 2861 {
c0a62865
DK
2862 // Replace input sections with relaxed input section in the saved
2863 // copy of the input section list.
2864 if (this->checkpoint_->input_sections_saved())
20e6d0d6 2865 {
c0a62865
DK
2866 Relaxation_map map;
2867 this->build_relaxation_map(
2868 *(this->checkpoint_->input_sections()),
2869 this->checkpoint_->input_sections()->size(),
2870 &map);
2871 this->convert_input_sections_in_list_to_relaxed_sections(
2872 relaxed_sections,
2873 map,
2874 this->checkpoint_->input_sections());
2875 }
2876 else
2877 {
2878 // We have not copied the input section list yet. Instead, just
2879 // look at the portion that would be saved.
2880 limit = this->checkpoint_->input_sections_size();
20e6d0d6 2881 }
20e6d0d6 2882 }
c0a62865
DK
2883
2884 // Convert input sections in input_section_list.
2885 Relaxation_map map;
2886 this->build_relaxation_map(this->input_sections_, limit, &map);
2887 this->convert_input_sections_in_list_to_relaxed_sections(
2888 relaxed_sections,
2889 map,
2890 &this->input_sections_);
41263c05
DK
2891
2892 // Update fast look-up map.
0439c796 2893 if (this->lookup_maps_->is_valid())
41263c05
DK
2894 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2895 {
2896 Output_relaxed_input_section* poris = relaxed_sections[i];
0439c796
DK
2897 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2898 poris->shndx(), poris);
41263c05 2899 }
20e6d0d6
DK
2900}
2901
9c547ec3
ILT
2902// Update the output section flags based on input section flags.
2903
2904void
2ea97941 2905Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
9c547ec3
ILT
2906{
2907 // If we created the section with SHF_ALLOC clear, we set the
2908 // address. If we are now setting the SHF_ALLOC flag, we need to
2909 // undo that.
2910 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2ea97941 2911 && (flags & elfcpp::SHF_ALLOC) != 0)
9c547ec3
ILT
2912 this->mark_address_invalid();
2913
2ea97941 2914 this->flags_ |= (flags
9c547ec3
ILT
2915 & (elfcpp::SHF_WRITE
2916 | elfcpp::SHF_ALLOC
2917 | elfcpp::SHF_EXECINSTR));
e8cd95c7
ILT
2918
2919 if ((flags & elfcpp::SHF_MERGE) == 0)
2920 this->flags_ &=~ elfcpp::SHF_MERGE;
2921 else
2922 {
2923 if (this->current_data_size_for_child() == 0)
2924 this->flags_ |= elfcpp::SHF_MERGE;
2925 }
2926
2927 if ((flags & elfcpp::SHF_STRINGS) == 0)
2928 this->flags_ &=~ elfcpp::SHF_STRINGS;
2929 else
2930 {
2931 if (this->current_data_size_for_child() == 0)
2932 this->flags_ |= elfcpp::SHF_STRINGS;
2933 }
9c547ec3
ILT
2934}
2935
2ea97941 2936// Find the merge section into which an input section with index SHNDX in
c0a62865
DK
2937// OBJECT has been added. Return NULL if none found.
2938
67f95b96 2939const Output_section_data*
c0a62865 2940Output_section::find_merge_section(const Relobj* object,
2ea97941 2941 unsigned int shndx) const
c0a62865 2942{
67f95b96 2943 return object->find_merge_section(shndx);
0439c796
DK
2944}
2945
67f95b96
RÁE
2946// Build the lookup maps for relaxed sections. This needs
2947// to be declared as a const method so that it is callable with a const
0439c796
DK
2948// Output_section pointer. The method only updates states of the maps.
2949
2950void
2951Output_section::build_lookup_maps() const
2952{
2953 this->lookup_maps_->clear();
2954 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2955 p != this->input_sections_.end();
2956 ++p)
c0a62865 2957 {
67f95b96 2958 if (p->is_relaxed_input_section())
0439c796
DK
2959 {
2960 Output_relaxed_input_section* poris = p->relaxed_input_section();
2961 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2962 poris->shndx(), poris);
2963 }
c0a62865 2964 }
c0a62865
DK
2965}
2966
2967// Find an relaxed input section corresponding to an input section
2ea97941 2968// in OBJECT with index SHNDX.
c0a62865 2969
d6344fb5 2970const Output_relaxed_input_section*
c0a62865 2971Output_section::find_relaxed_input_section(const Relobj* object,
2ea97941 2972 unsigned int shndx) const
c0a62865 2973{
0439c796
DK
2974 if (!this->lookup_maps_->is_valid())
2975 this->build_lookup_maps();
2976 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
c0a62865
DK
2977}
2978
2ea97941
ILT
2979// Given an address OFFSET relative to the start of input section
2980// SHNDX in OBJECT, return whether this address is being included in
2981// the final link. This should only be called if SHNDX in OBJECT has
730cdc88
ILT
2982// a special mapping.
2983
2984bool
2985Output_section::is_input_address_mapped(const Relobj* object,
2ea97941
ILT
2986 unsigned int shndx,
2987 off_t offset) const
730cdc88 2988{
c0a62865 2989 // Look at the Output_section_data_maps first.
2ea97941 2990 const Output_section_data* posd = this->find_merge_section(object, shndx);
c0a62865 2991 if (posd == NULL)
2ea97941 2992 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
2993
2994 if (posd != NULL)
2995 {
2ea97941
ILT
2996 section_offset_type output_offset;
2997 bool found = posd->output_offset(object, shndx, offset, &output_offset);
cfbf0e3c
RÁE
2998 // By default we assume that the address is mapped. See comment at the
2999 // end.
67f95b96 3000 if (!found)
cfbf0e3c 3001 return true;
2ea97941 3002 return output_offset != -1;
c0a62865
DK
3003 }
3004
3005 // Fall back to the slow look-up.
730cdc88
ILT
3006 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3007 p != this->input_sections_.end();
3008 ++p)
3009 {
2ea97941
ILT
3010 section_offset_type output_offset;
3011 if (p->output_offset(object, shndx, offset, &output_offset))
3012 return output_offset != -1;
730cdc88
ILT
3013 }
3014
3015 // By default we assume that the address is mapped. This should
3016 // only be called after we have passed all sections to Layout. At
3017 // that point we should know what we are discarding.
3018 return true;
3019}
3020
2ea97941
ILT
3021// Given an address OFFSET relative to the start of input section
3022// SHNDX in object OBJECT, return the output offset relative to the
1e983657 3023// start of the input section in the output section. This should only
2ea97941 3024// be called if SHNDX in OBJECT has a special mapping.
730cdc88 3025
8383303e 3026section_offset_type
2ea97941
ILT
3027Output_section::output_offset(const Relobj* object, unsigned int shndx,
3028 section_offset_type offset) const
730cdc88 3029{
c0a62865
DK
3030 // This can only be called meaningfully when we know the data size
3031 // of this.
3032 gold_assert(this->is_data_size_valid());
730cdc88 3033
c0a62865 3034 // Look at the Output_section_data_maps first.
2ea97941 3035 const Output_section_data* posd = this->find_merge_section(object, shndx);
50ed5eb1 3036 if (posd == NULL)
2ea97941 3037 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
3038 if (posd != NULL)
3039 {
2ea97941
ILT
3040 section_offset_type output_offset;
3041 bool found = posd->output_offset(object, shndx, offset, &output_offset);
50ed5eb1 3042 gold_assert(found);
2ea97941 3043 return output_offset;
c0a62865
DK
3044 }
3045
3046 // Fall back to the slow look-up.
730cdc88
ILT
3047 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3048 p != this->input_sections_.end();
3049 ++p)
3050 {
2ea97941
ILT
3051 section_offset_type output_offset;
3052 if (p->output_offset(object, shndx, offset, &output_offset))
3053 return output_offset;
730cdc88
ILT
3054 }
3055 gold_unreachable();
3056}
3057
2ea97941
ILT
3058// Return the output virtual address of OFFSET relative to the start
3059// of input section SHNDX in object OBJECT.
b8e6aad9
ILT
3060
3061uint64_t
2ea97941
ILT
3062Output_section::output_address(const Relobj* object, unsigned int shndx,
3063 off_t offset) const
b8e6aad9
ILT
3064{
3065 uint64_t addr = this->address() + this->first_input_offset_;
c0a62865
DK
3066
3067 // Look at the Output_section_data_maps first.
2ea97941 3068 const Output_section_data* posd = this->find_merge_section(object, shndx);
50ed5eb1 3069 if (posd == NULL)
2ea97941 3070 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
3071 if (posd != NULL && posd->is_address_valid())
3072 {
2ea97941
ILT
3073 section_offset_type output_offset;
3074 bool found = posd->output_offset(object, shndx, offset, &output_offset);
c0a62865 3075 gold_assert(found);
2ea97941 3076 return posd->address() + output_offset;
c0a62865
DK
3077 }
3078
3079 // Fall back to the slow look-up.
b8e6aad9
ILT
3080 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3081 p != this->input_sections_.end();
3082 ++p)
3083 {
3084 addr = align_address(addr, p->addralign());
2ea97941
ILT
3085 section_offset_type output_offset;
3086 if (p->output_offset(object, shndx, offset, &output_offset))
730cdc88 3087 {
2ea97941 3088 if (output_offset == -1)
eff45813 3089 return -1ULL;
2ea97941 3090 return addr + output_offset;
730cdc88 3091 }
b8e6aad9
ILT
3092 addr += p->data_size();
3093 }
3094
3095 // If we get here, it means that we don't know the mapping for this
3096 // input section. This might happen in principle if
3097 // add_input_section were called before add_output_section_data.
3098 // But it should never actually happen.
3099
3100 gold_unreachable();
ead1e424
ILT
3101}
3102
e29e076a 3103// Find the output address of the start of the merged section for
2ea97941 3104// input section SHNDX in object OBJECT.
a9a60db6 3105
e29e076a
ILT
3106bool
3107Output_section::find_starting_output_address(const Relobj* object,
2ea97941 3108 unsigned int shndx,
e29e076a 3109 uint64_t* paddr) const
a9a60db6 3110{
67f95b96
RÁE
3111 const Output_section_data* data = this->find_merge_section(object, shndx);
3112 if (data == NULL)
3113 return false;
3114
c0a62865
DK
3115 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3116 // Looking up the merge section map does not always work as we sometimes
3117 // find a merge section without its address set.
a9a60db6
ILT
3118 uint64_t addr = this->address() + this->first_input_offset_;
3119 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3120 p != this->input_sections_.end();
3121 ++p)
3122 {
3123 addr = align_address(addr, p->addralign());
3124
3125 // It would be nice if we could use the existing output_offset
3126 // method to get the output offset of input offset 0.
3127 // Unfortunately we don't know for sure that input offset 0 is
3128 // mapped at all.
67f95b96 3129 if (!p->is_input_section() && p->output_section_data() == data)
e29e076a
ILT
3130 {
3131 *paddr = addr;
3132 return true;
3133 }
a9a60db6
ILT
3134
3135 addr += p->data_size();
3136 }
e29e076a
ILT
3137
3138 // We couldn't find a merge output section for this input section.
3139 return false;
a9a60db6
ILT
3140}
3141
cdc29364
CC
3142// Update the data size of an Output_section.
3143
3144void
3145Output_section::update_data_size()
3146{
3147 if (this->input_sections_.empty())
3148 return;
3149
3150 if (this->must_sort_attached_input_sections()
3151 || this->input_section_order_specified())
3152 this->sort_attached_input_sections();
3153
3154 off_t off = this->first_input_offset_;
3155 for (Input_section_list::iterator p = this->input_sections_.begin();
3156 p != this->input_sections_.end();
3157 ++p)
3158 {
3159 off = align_address(off, p->addralign());
3160 off += p->current_data_size();
3161 }
3162
3163 this->set_current_data_size_for_child(off);
3164}
3165
27bc2bce 3166// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
3167// setting the addresses of any Output_section_data objects.
3168
3169void
27bc2bce 3170Output_section::set_final_data_size()
ead1e424 3171{
9fbd3822
CC
3172 off_t data_size;
3173
ead1e424 3174 if (this->input_sections_.empty())
9fbd3822
CC
3175 data_size = this->current_data_size_for_child();
3176 else
27bc2bce 3177 {
9fbd3822
CC
3178 if (this->must_sort_attached_input_sections()
3179 || this->input_section_order_specified())
3180 this->sort_attached_input_sections();
ead1e424 3181
9fbd3822
CC
3182 uint64_t address = this->address();
3183 off_t startoff = this->offset();
5d9f66cb 3184 off_t off = this->first_input_offset_;
9fbd3822
CC
3185 for (Input_section_list::iterator p = this->input_sections_.begin();
3186 p != this->input_sections_.end();
3187 ++p)
3188 {
3189 off = align_address(off, p->addralign());
5d9f66cb 3190 p->set_address_and_file_offset(address + off, startoff + off,
9fbd3822
CC
3191 startoff);
3192 off += p->data_size();
3193 }
5d9f66cb 3194 data_size = off;
9fbd3822 3195 }
2fd32231 3196
9fbd3822
CC
3197 // For full incremental links, we want to allocate some patch space
3198 // in most sections for subsequent incremental updates.
3199 if (this->is_patch_space_allowed_ && parameters->incremental_full())
ead1e424 3200 {
9fbd3822 3201 double pct = parameters->options().incremental_patch();
8ea8cd50
CC
3202 size_t extra = static_cast<size_t>(data_size * pct);
3203 if (this->free_space_fill_ != NULL
eb426534 3204 && this->free_space_fill_->minimum_hole_size() > extra)
8ea8cd50 3205 extra = this->free_space_fill_->minimum_hole_size();
9fbd3822
CC
3206 off_t new_size = align_address(data_size + extra, this->addralign());
3207 this->patch_space_ = new_size - data_size;
3208 gold_debug(DEBUG_INCREMENTAL,
3209 "set_final_data_size: %08lx + %08lx: section %s",
3210 static_cast<long>(data_size),
3211 static_cast<long>(this->patch_space_),
3212 this->name());
3213 data_size = new_size;
ead1e424
ILT
3214 }
3215
9fbd3822 3216 this->set_data_size(data_size);
ead1e424 3217}
9a0910c3 3218
a445fddf
ILT
3219// Reset the address and file offset.
3220
3221void
3222Output_section::do_reset_address_and_file_offset()
3223{
20e6d0d6
DK
3224 // An unallocated section has no address. Forcing this means that
3225 // we don't need special treatment for symbols defined in debug
1e5d2fb1
DK
3226 // sections. We do the same in the constructor. This does not
3227 // apply to NOLOAD sections though.
3228 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
20e6d0d6
DK
3229 this->set_address(0);
3230
a445fddf
ILT
3231 for (Input_section_list::iterator p = this->input_sections_.begin();
3232 p != this->input_sections_.end();
3233 ++p)
3234 p->reset_address_and_file_offset();
9fbd3822
CC
3235
3236 // Remove any patch space that was added in set_final_data_size.
3237 if (this->patch_space_ > 0)
3238 {
3239 this->set_current_data_size_for_child(this->current_data_size_for_child()
3240 - this->patch_space_);
3241 this->patch_space_ = 0;
3242 }
a445fddf 3243}
9fbd3822 3244
20e6d0d6
DK
3245// Return true if address and file offset have the values after reset.
3246
3247bool
3248Output_section::do_address_and_file_offset_have_reset_values() const
3249{
3250 if (this->is_offset_valid())
3251 return false;
3252
3253 // An unallocated section has address 0 after its construction or a reset.
3254 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3255 return this->is_address_valid() && this->address() == 0;
3256 else
3257 return !this->is_address_valid();
3258}
a445fddf 3259
7bf1f802
ILT
3260// Set the TLS offset. Called only for SHT_TLS sections.
3261
3262void
3263Output_section::do_set_tls_offset(uint64_t tls_base)
3264{
3265 this->tls_offset_ = this->address() - tls_base;
3266}
3267
2fd32231
ILT
3268// In a few cases we need to sort the input sections attached to an
3269// output section. This is used to implement the type of constructor
3270// priority ordering implemented by the GNU linker, in which the
3271// priority becomes part of the section name and the sections are
3272// sorted by name. We only do this for an output section if we see an
5393d741 3273// attached input section matching ".ctors.*", ".dtors.*",
2fd32231
ILT
3274// ".init_array.*" or ".fini_array.*".
3275
3276class Output_section::Input_section_sort_entry
3277{
3278 public:
3279 Input_section_sort_entry()
9860cbcf 3280 : input_section_(), index_(-1U), section_name_()
2fd32231
ILT
3281 { }
3282
2ea97941 3283 Input_section_sort_entry(const Input_section& input_section,
6e9ba2ca 3284 unsigned int index,
9860cbcf
CC
3285 bool must_sort_attached_input_sections,
3286 const char* output_section_name)
3287 : input_section_(input_section), index_(index), section_name_()
2fd32231 3288 {
9860cbcf
CC
3289 if ((input_section.is_input_section()
3290 || input_section.is_relaxed_input_section())
eb426534 3291 && must_sort_attached_input_sections)
2fd32231
ILT
3292 {
3293 // This is only called single-threaded from Layout::finalize,
3294 // so it is OK to lock. Unfortunately we have no way to pass
3295 // in a Task token.
3296 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2ea97941
ILT
3297 Object* obj = (input_section.is_input_section()
3298 ? input_section.relobj()
3299 : input_section.relaxed_input_section()->relobj());
2fd32231
ILT
3300 Task_lock_obj<Object> tl(dummy_task, obj);
3301
3302 // This is a slow operation, which should be cached in
3303 // Layout::layout if this becomes a speed problem.
2ea97941 3304 this->section_name_ = obj->section_name(input_section.shndx());
2fd32231 3305 }
9860cbcf
CC
3306 else if (input_section.is_output_section_data()
3307 && must_sort_attached_input_sections)
3308 {
3309 // For linker-generated sections, use the output section name.
3310 this->section_name_.assign(output_section_name);
3311 }
2fd32231
ILT
3312 }
3313
3314 // Return the Input_section.
3315 const Input_section&
3316 input_section() const
3317 {
3318 gold_assert(this->index_ != -1U);
3319 return this->input_section_;
3320 }
3321
3322 // The index of this entry in the original list. This is used to
3323 // make the sort stable.
3324 unsigned int
3325 index() const
3326 {
3327 gold_assert(this->index_ != -1U);
3328 return this->index_;
3329 }
3330
2fd32231
ILT
3331 // The section name.
3332 const std::string&
3333 section_name() const
3334 {
2fd32231
ILT
3335 return this->section_name_;
3336 }
3337
ab794b6b
ILT
3338 // Return true if the section name has a priority. This is assumed
3339 // to be true if it has a dot after the initial dot.
2fd32231 3340 bool
ab794b6b 3341 has_priority() const
2fd32231 3342 {
2a0ff005 3343 return this->section_name_.find('.', 1) != std::string::npos;
2fd32231
ILT
3344 }
3345
5393d741
ILT
3346 // Return the priority. Believe it or not, gcc encodes the priority
3347 // differently for .ctors/.dtors and .init_array/.fini_array
3348 // sections.
3349 unsigned int
3350 get_priority() const
3351 {
5393d741
ILT
3352 bool is_ctors;
3353 if (is_prefix_of(".ctors.", this->section_name_.c_str())
3354 || is_prefix_of(".dtors.", this->section_name_.c_str()))
3355 is_ctors = true;
3356 else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3357 || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3358 is_ctors = false;
3359 else
3360 return 0;
3361 char* end;
3362 unsigned long prio = strtoul((this->section_name_.c_str()
3363 + (is_ctors ? 7 : 12)),
3364 &end, 10);
3365 if (*end != '\0')
3366 return 0;
3367 else if (is_ctors)
3368 return 65535 - prio;
3369 else
3370 return prio;
3371 }
3372
ab794b6b
ILT
3373 // Return true if this an input file whose base name matches
3374 // FILE_NAME. The base name must have an extension of ".o", and
3375 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3376 // This is to match crtbegin.o as well as crtbeginS.o without
3377 // getting confused by other possibilities. Overall matching the
3378 // file name this way is a dreadful hack, but the GNU linker does it
3379 // in order to better support gcc, and we need to be compatible.
2fd32231 3380 bool
5393d741 3381 match_file_name(const char* file_name) const
edcac0c1
ILT
3382 {
3383 if (this->input_section_.is_output_section_data())
3384 return false;
3385 return Layout::match_file_name(this->input_section_.relobj(), file_name);
3386 }
2fd32231 3387
8fe2a369
ST
3388 // Returns 1 if THIS should appear before S in section order, -1 if S
3389 // appears before THIS and 0 if they are not comparable.
6e9ba2ca
ST
3390 int
3391 compare_section_ordering(const Input_section_sort_entry& s) const
3392 {
8fe2a369
ST
3393 unsigned int this_secn_index = this->input_section_.section_order_index();
3394 unsigned int s_secn_index = s.input_section().section_order_index();
3395 if (this_secn_index > 0 && s_secn_index > 0)
3396 {
eb426534
RM
3397 if (this_secn_index < s_secn_index)
3398 return 1;
3399 else if (this_secn_index > s_secn_index)
3400 return -1;
8fe2a369
ST
3401 }
3402 return 0;
6e9ba2ca
ST
3403 }
3404
2fd32231
ILT
3405 private:
3406 // The Input_section we are sorting.
3407 Input_section input_section_;
3408 // The index of this Input_section in the original list.
3409 unsigned int index_;
2fd32231
ILT
3410 // The section name if there is one.
3411 std::string section_name_;
3412};
3413
3414// Return true if S1 should come before S2 in the output section.
3415
3416bool
3417Output_section::Input_section_sort_compare::operator()(
3418 const Output_section::Input_section_sort_entry& s1,
3419 const Output_section::Input_section_sort_entry& s2) const
3420{
ab794b6b
ILT
3421 // crtbegin.o must come first.
3422 bool s1_begin = s1.match_file_name("crtbegin");
3423 bool s2_begin = s2.match_file_name("crtbegin");
2fd32231
ILT
3424 if (s1_begin || s2_begin)
3425 {
3426 if (!s1_begin)
3427 return false;
3428 if (!s2_begin)
3429 return true;
3430 return s1.index() < s2.index();
3431 }
3432
ab794b6b
ILT
3433 // crtend.o must come last.
3434 bool s1_end = s1.match_file_name("crtend");
3435 bool s2_end = s2.match_file_name("crtend");
2fd32231
ILT
3436 if (s1_end || s2_end)
3437 {
3438 if (!s1_end)
3439 return true;
3440 if (!s2_end)
3441 return false;
3442 return s1.index() < s2.index();
3443 }
3444
ab794b6b 3445 // A section with a priority follows a section without a priority.
ab794b6b
ILT
3446 bool s1_has_priority = s1.has_priority();
3447 bool s2_has_priority = s2.has_priority();
3448 if (s1_has_priority && !s2_has_priority)
2fd32231 3449 return false;
ab794b6b 3450 if (!s1_has_priority && s2_has_priority)
2fd32231
ILT
3451 return true;
3452
6e9ba2ca
ST
3453 // Check if a section order exists for these sections through a section
3454 // ordering file. If sequence_num is 0, an order does not exist.
3455 int sequence_num = s1.compare_section_ordering(s2);
3456 if (sequence_num != 0)
3457 return sequence_num == 1;
3458
2fd32231
ILT
3459 // Otherwise we sort by name.
3460 int compare = s1.section_name().compare(s2.section_name());
3461 if (compare != 0)
3462 return compare < 0;
3463
3464 // Otherwise we keep the input order.
3465 return s1.index() < s2.index();
3466}
3467
2a0ff005
DK
3468// Return true if S1 should come before S2 in an .init_array or .fini_array
3469// output section.
3470
3471bool
3472Output_section::Input_section_sort_init_fini_compare::operator()(
3473 const Output_section::Input_section_sort_entry& s1,
3474 const Output_section::Input_section_sort_entry& s2) const
3475{
2a0ff005
DK
3476 // A section without a priority follows a section with a priority.
3477 // This is the reverse of .ctors and .dtors sections.
3478 bool s1_has_priority = s1.has_priority();
3479 bool s2_has_priority = s2.has_priority();
3480 if (s1_has_priority && !s2_has_priority)
3481 return true;
3482 if (!s1_has_priority && s2_has_priority)
3483 return false;
3484
5393d741
ILT
3485 // .ctors and .dtors sections without priority come after
3486 // .init_array and .fini_array sections without priority.
3487 if (!s1_has_priority
3488 && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3489 && s1.section_name() != s2.section_name())
3490 return false;
3491 if (!s2_has_priority
3492 && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3493 && s2.section_name() != s1.section_name())
3494 return true;
3495
3496 // Sort by priority if we can.
3497 if (s1_has_priority)
3498 {
3499 unsigned int s1_prio = s1.get_priority();
3500 unsigned int s2_prio = s2.get_priority();
3501 if (s1_prio < s2_prio)
3502 return true;
3503 else if (s1_prio > s2_prio)
3504 return false;
3505 }
3506
6e9ba2ca
ST
3507 // Check if a section order exists for these sections through a section
3508 // ordering file. If sequence_num is 0, an order does not exist.
3509 int sequence_num = s1.compare_section_ordering(s2);
3510 if (sequence_num != 0)
3511 return sequence_num == 1;
3512
2a0ff005
DK
3513 // Otherwise we sort by name.
3514 int compare = s1.section_name().compare(s2.section_name());
3515 if (compare != 0)
3516 return compare < 0;
3517
3518 // Otherwise we keep the input order.
3519 return s1.index() < s2.index();
3520}
3521
8fe2a369
ST
3522// Return true if S1 should come before S2. Sections that do not match
3523// any pattern in the section ordering file are placed ahead of the sections
edcac0c1 3524// that match some pattern.
8fe2a369 3525
6e9ba2ca
ST
3526bool
3527Output_section::Input_section_sort_section_order_index_compare::operator()(
3528 const Output_section::Input_section_sort_entry& s1,
3529 const Output_section::Input_section_sort_entry& s2) const
3530{
8fe2a369
ST
3531 unsigned int s1_secn_index = s1.input_section().section_order_index();
3532 unsigned int s2_secn_index = s2.input_section().section_order_index();
6e9ba2ca 3533
edcac0c1
ILT
3534 // Keep input order if section ordering cannot determine order.
3535 if (s1_secn_index == s2_secn_index)
28f2a4ac 3536 return s1.index() < s2.index();
edcac0c1
ILT
3537
3538 return s1_secn_index < s2_secn_index;
6e9ba2ca
ST
3539}
3540
c6ac678d
ST
3541// Return true if S1 should come before S2. This is the sort comparison
3542// function for .text to sort sections with prefixes
3543// .text.{unlikely,exit,startup,hot} before other sections.
6934001a 3544
c6ac678d 3545bool
6934001a 3546Output_section::Input_section_sort_section_prefix_special_ordering_compare
c6ac678d
ST
3547 ::operator()(
3548 const Output_section::Input_section_sort_entry& s1,
3549 const Output_section::Input_section_sort_entry& s2) const
3550{
c6ac678d 3551 // Some input section names have special ordering requirements.
5fa5f8f5
ML
3552 const char *s1_section_name = s1.section_name().c_str();
3553 const char *s2_section_name = s2.section_name().c_str();
3554 int o1 = Layout::special_ordering_of_input_section(s1_section_name);
3555 int o2 = Layout::special_ordering_of_input_section(s2_section_name);
c6ac678d
ST
3556 if (o1 != o2)
3557 {
3558 if (o1 < 0)
3559 return false;
3560 else if (o2 < 0)
3561 return true;
3562 else
3563 return o1 < o2;
3564 }
5fa5f8f5
ML
3565 else if (is_prefix_of(".text.sorted", s1_section_name))
3566 return strcmp(s1_section_name, s2_section_name) <= 0;
c6ac678d
ST
3567
3568 // Keep input order otherwise.
6934001a
CC
3569 return s1.index() < s2.index();
3570}
3571
3572// Return true if S1 should come before S2. This is the sort comparison
3573// function for sections to sort them by name.
3574
3575bool
3576Output_section::Input_section_sort_section_name_compare
3577 ::operator()(
3578 const Output_section::Input_section_sort_entry& s1,
3579 const Output_section::Input_section_sort_entry& s2) const
3580{
6934001a
CC
3581 // We sort by name.
3582 int compare = s1.section_name().compare(s2.section_name());
3583 if (compare != 0)
3584 return compare < 0;
3585
3586 // Keep input order otherwise.
3587 return s1.index() < s2.index();
c6ac678d
ST
3588}
3589
e9552f7e
ST
3590// This updates the section order index of input sections according to the
3591// the order specified in the mapping from Section id to order index.
3592
3593void
3594Output_section::update_section_layout(
f0558624 3595 const Section_layout_order* order_map)
e9552f7e
ST
3596{
3597 for (Input_section_list::iterator p = this->input_sections_.begin();
3598 p != this->input_sections_.end();
3599 ++p)
3600 {
3601 if (p->is_input_section()
3602 || p->is_relaxed_input_section())
eb426534 3603 {
efc6fa12 3604 Relobj* obj = (p->is_input_section()
e9552f7e 3605 ? p->relobj()
eb426534 3606 : p->relaxed_input_section()->relobj());
e9552f7e
ST
3607 unsigned int shndx = p->shndx();
3608 Section_layout_order::const_iterator it
f0558624
ST
3609 = order_map->find(Section_id(obj, shndx));
3610 if (it == order_map->end())
e9552f7e
ST
3611 continue;
3612 unsigned int section_order_index = it->second;
3613 if (section_order_index != 0)
eb426534
RM
3614 {
3615 p->set_section_order_index(section_order_index);
3616 this->set_input_section_order_specified();
e9552f7e 3617 }
eb426534 3618 }
e9552f7e
ST
3619 }
3620}
3621
2fd32231
ILT
3622// Sort the input sections attached to an output section.
3623
3624void
3625Output_section::sort_attached_input_sections()
3626{
3627 if (this->attached_input_sections_are_sorted_)
3628 return;
3629
20e6d0d6
DK
3630 if (this->checkpoint_ != NULL
3631 && !this->checkpoint_->input_sections_saved())
3632 this->checkpoint_->save_input_sections();
3633
2fd32231
ILT
3634 // The only thing we know about an input section is the object and
3635 // the section index. We need the section name. Recomputing this
3636 // is slow but this is an unusual case. If this becomes a speed
3637 // problem we can cache the names as required in Layout::layout.
3638
3639 // We start by building a larger vector holding a copy of each
3640 // Input_section, plus its current index in the list and its name.
3641 std::vector<Input_section_sort_entry> sort_list;
3642
3643 unsigned int i = 0;
3644 for (Input_section_list::iterator p = this->input_sections_.begin();
3645 p != this->input_sections_.end();
3646 ++p, ++i)
6e9ba2ca 3647 sort_list.push_back(Input_section_sort_entry(*p, i,
9860cbcf
CC
3648 this->must_sort_attached_input_sections(),
3649 this->name()));
2fd32231
ILT
3650
3651 // Sort the input sections.
6e9ba2ca
ST
3652 if (this->must_sort_attached_input_sections())
3653 {
3654 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
eb426534
RM
3655 || this->type() == elfcpp::SHT_INIT_ARRAY
3656 || this->type() == elfcpp::SHT_FINI_ARRAY)
3657 std::sort(sort_list.begin(), sort_list.end(),
3658 Input_section_sort_init_fini_compare());
6934001a
CC
3659 else if (strcmp(parameters->options().sort_section(), "name") == 0)
3660 std::sort(sort_list.begin(), sort_list.end(),
3661 Input_section_sort_section_name_compare());
c6ac678d 3662 else if (strcmp(this->name(), ".text") == 0)
6934001a
CC
3663 std::sort(sort_list.begin(), sort_list.end(),
3664 Input_section_sort_section_prefix_special_ordering_compare());
6e9ba2ca 3665 else
eb426534
RM
3666 std::sort(sort_list.begin(), sort_list.end(),
3667 Input_section_sort_compare());
6e9ba2ca 3668 }
2a0ff005 3669 else
6e9ba2ca 3670 {
e9552f7e 3671 gold_assert(this->input_section_order_specified());
6e9ba2ca 3672 std::sort(sort_list.begin(), sort_list.end(),
eb426534 3673 Input_section_sort_section_order_index_compare());
6e9ba2ca 3674 }
2fd32231
ILT
3675
3676 // Copy the sorted input sections back to our list.
3677 this->input_sections_.clear();
3678 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3679 p != sort_list.end();
3680 ++p)
3681 this->input_sections_.push_back(p->input_section());
6e9ba2ca 3682 sort_list.clear();
2fd32231
ILT
3683
3684 // Remember that we sorted the input sections, since we might get
3685 // called again.
3686 this->attached_input_sections_are_sorted_ = true;
3687}
3688
61ba1cf9
ILT
3689// Write the section header to *OSHDR.
3690
3691template<int size, bool big_endian>
3692void
16649710
ILT
3693Output_section::write_header(const Layout* layout,
3694 const Stringpool* secnamepool,
61ba1cf9
ILT
3695 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3696{
3697 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3698 oshdr->put_sh_type(this->type_);
6a74a719 3699
2ea97941 3700 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 3701 if (this->info_section_ != NULL && this->info_uses_section_index_)
2ea97941
ILT
3702 flags |= elfcpp::SHF_INFO_LINK;
3703 oshdr->put_sh_flags(flags);
6a74a719 3704
61ba1cf9
ILT
3705 oshdr->put_sh_addr(this->address());
3706 oshdr->put_sh_offset(this->offset());
3707 oshdr->put_sh_size(this->data_size());
16649710
ILT
3708 if (this->link_section_ != NULL)
3709 oshdr->put_sh_link(this->link_section_->out_shndx());
3710 else if (this->should_link_to_symtab_)
886f533a 3711 oshdr->put_sh_link(layout->symtab_section_shndx());
16649710
ILT
3712 else if (this->should_link_to_dynsym_)
3713 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3714 else
3715 oshdr->put_sh_link(this->link_);
755ab8af 3716
2ea97941 3717 elfcpp::Elf_Word info;
16649710 3718 if (this->info_section_ != NULL)
755ab8af
ILT
3719 {
3720 if (this->info_uses_section_index_)
2ea97941 3721 info = this->info_section_->out_shndx();
755ab8af 3722 else
2ea97941 3723 info = this->info_section_->symtab_index();
755ab8af 3724 }
6a74a719 3725 else if (this->info_symndx_ != NULL)
2ea97941 3726 info = this->info_symndx_->symtab_index();
16649710 3727 else
2ea97941
ILT
3728 info = this->info_;
3729 oshdr->put_sh_info(info);
755ab8af 3730
61ba1cf9
ILT
3731 oshdr->put_sh_addralign(this->addralign_);
3732 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
3733}
3734
ead1e424
ILT
3735// Write out the data. For input sections the data is written out by
3736// Object::relocate, but we have to handle Output_section_data objects
3737// here.
3738
3739void
3740Output_section::do_write(Output_file* of)
3741{
96803768
ILT
3742 gold_assert(!this->requires_postprocessing());
3743
c0a62865
DK
3744 // If the target performs relaxation, we delay filler generation until now.
3745 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3746
c51e6221
ILT
3747 off_t output_section_file_offset = this->offset();
3748 for (Fill_list::iterator p = this->fills_.begin();
3749 p != this->fills_.end();
3750 ++p)
3751 {
8851ecca 3752 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 3753 of->write(output_section_file_offset + p->section_offset(),
a445fddf 3754 fill_data.data(), fill_data.size());
c51e6221
ILT
3755 }
3756
c0a62865 3757 off_t off = this->offset() + this->first_input_offset_;
ead1e424
ILT
3758 for (Input_section_list::iterator p = this->input_sections_.begin();
3759 p != this->input_sections_.end();
3760 ++p)
c0a62865
DK
3761 {
3762 off_t aligned_off = align_address(off, p->addralign());
3763 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3764 {
3765 size_t fill_len = aligned_off - off;
3766 std::string fill_data(parameters->target().code_fill(fill_len));
3767 of->write(off, fill_data.data(), fill_data.size());
3768 }
3769
3770 p->write(of);
3771 off = aligned_off + p->data_size();
3772 }
8ea8cd50
CC
3773
3774 // For incremental links, fill in unused chunks in debug sections
3775 // with dummy compilation unit headers.
3776 if (this->free_space_fill_ != NULL)
3777 {
3778 for (Free_list::Const_iterator p = this->free_list_.begin();
3779 p != this->free_list_.end();
3780 ++p)
3781 {
3782 off_t off = p->start_;
3783 size_t len = p->end_ - off;
3784 this->free_space_fill_->write(of, this->offset() + off, len);
3785 }
3786 if (this->patch_space_ > 0)
3787 {
3788 off_t off = this->current_data_size_for_child() - this->patch_space_;
3789 this->free_space_fill_->write(of, this->offset() + off,
3790 this->patch_space_);
3791 }
3792 }
ead1e424
ILT
3793}
3794
96803768
ILT
3795// If a section requires postprocessing, create the buffer to use.
3796
3797void
3798Output_section::create_postprocessing_buffer()
3799{
3800 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
3801
3802 if (this->postprocessing_buffer_ != NULL)
3803 return;
96803768
ILT
3804
3805 if (!this->input_sections_.empty())
3806 {
3807 off_t off = this->first_input_offset_;
3808 for (Input_section_list::iterator p = this->input_sections_.begin();
3809 p != this->input_sections_.end();
3810 ++p)
3811 {
3812 off = align_address(off, p->addralign());
3813 p->finalize_data_size();
3814 off += p->data_size();
3815 }
3816 this->set_current_data_size_for_child(off);
3817 }
3818
3819 off_t buffer_size = this->current_data_size_for_child();
3820 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3821}
3822
3823// Write all the data of an Output_section into the postprocessing
3824// buffer. This is used for sections which require postprocessing,
3825// such as compression. Input sections are handled by
3826// Object::Relocate.
3827
3828void
3829Output_section::write_to_postprocessing_buffer()
3830{
3831 gold_assert(this->requires_postprocessing());
3832
c0a62865
DK
3833 // If the target performs relaxation, we delay filler generation until now.
3834 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3835
96803768
ILT
3836 unsigned char* buffer = this->postprocessing_buffer();
3837 for (Fill_list::iterator p = this->fills_.begin();
3838 p != this->fills_.end();
3839 ++p)
3840 {
8851ecca 3841 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
3842 memcpy(buffer + p->section_offset(), fill_data.data(),
3843 fill_data.size());
96803768
ILT
3844 }
3845
3846 off_t off = this->first_input_offset_;
3847 for (Input_section_list::iterator p = this->input_sections_.begin();
3848 p != this->input_sections_.end();
3849 ++p)
3850 {
c0a62865
DK
3851 off_t aligned_off = align_address(off, p->addralign());
3852 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3853 {
3854 size_t fill_len = aligned_off - off;
3855 std::string fill_data(parameters->target().code_fill(fill_len));
3856 memcpy(buffer + off, fill_data.data(), fill_data.size());
3857 }
3858
3859 p->write_to_buffer(buffer + aligned_off);
3860 off = aligned_off + p->data_size();
96803768
ILT
3861 }
3862}
3863
a445fddf
ILT
3864// Get the input sections for linker script processing. We leave
3865// behind the Output_section_data entries. Note that this may be
3866// slightly incorrect for merge sections. We will leave them behind,
3867// but it is possible that the script says that they should follow
3868// some other input sections, as in:
3869// .rodata { *(.rodata) *(.rodata.cst*) }
3870// For that matter, we don't handle this correctly:
3871// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3872// With luck this will never matter.
3873
3874uint64_t
3875Output_section::get_input_sections(
2ea97941 3876 uint64_t address,
a445fddf 3877 const std::string& fill,
6625d24e 3878 std::list<Input_section>* input_sections)
a445fddf 3879{
20e6d0d6
DK
3880 if (this->checkpoint_ != NULL
3881 && !this->checkpoint_->input_sections_saved())
3882 this->checkpoint_->save_input_sections();
3883
0439c796
DK
3884 // Invalidate fast look-up maps.
3885 this->lookup_maps_->invalidate();
c0a62865 3886
2ea97941 3887 uint64_t orig_address = address;
a445fddf 3888
2ea97941 3889 address = align_address(address, this->addralign());
a445fddf
ILT
3890
3891 Input_section_list remaining;
3892 for (Input_section_list::iterator p = this->input_sections_.begin();
3893 p != this->input_sections_.end();
3894 ++p)
3895 {
0439c796
DK
3896 if (p->is_input_section()
3897 || p->is_relaxed_input_section()
3898 || p->is_merge_section())
6625d24e 3899 input_sections->push_back(*p);
a445fddf
ILT
3900 else
3901 {
2ea97941
ILT
3902 uint64_t aligned_address = align_address(address, p->addralign());
3903 if (aligned_address != address && !fill.empty())
a445fddf
ILT
3904 {
3905 section_size_type length =
2ea97941 3906 convert_to_section_size_type(aligned_address - address);
a445fddf
ILT
3907 std::string this_fill;
3908 this_fill.reserve(length);
3909 while (this_fill.length() + fill.length() <= length)
3910 this_fill += fill;
3911 if (this_fill.length() < length)
3912 this_fill.append(fill, 0, length - this_fill.length());
3913
3914 Output_section_data* posd = new Output_data_const(this_fill, 0);
3915 remaining.push_back(Input_section(posd));
3916 }
2ea97941 3917 address = aligned_address;
a445fddf
ILT
3918
3919 remaining.push_back(*p);
3920
3921 p->finalize_data_size();
2ea97941 3922 address += p->data_size();
a445fddf
ILT
3923 }
3924 }
3925
3926 this->input_sections_.swap(remaining);
3927 this->first_input_offset_ = 0;
3928
2ea97941
ILT
3929 uint64_t data_size = address - orig_address;
3930 this->set_current_data_size_for_child(data_size);
3931 return data_size;
a445fddf
ILT
3932}
3933
6625d24e
DK
3934// Add a script input section. SIS is an Output_section::Input_section,
3935// which can be either a plain input section or a special input section like
3936// a relaxed input section. For a special input section, its size must be
3937// finalized.
a445fddf
ILT
3938
3939void
6625d24e 3940Output_section::add_script_input_section(const Input_section& sis)
a445fddf 3941{
6625d24e
DK
3942 uint64_t data_size = sis.data_size();
3943 uint64_t addralign = sis.addralign();
2ea97941
ILT
3944 if (addralign > this->addralign_)
3945 this->addralign_ = addralign;
a445fddf
ILT
3946
3947 off_t offset_in_section = this->current_data_size_for_child();
3948 off_t aligned_offset_in_section = align_address(offset_in_section,
2ea97941 3949 addralign);
a445fddf
ILT
3950
3951 this->set_current_data_size_for_child(aligned_offset_in_section
2ea97941 3952 + data_size);
a445fddf 3953
6625d24e 3954 this->input_sections_.push_back(sis);
0439c796 3955
50ed5eb1 3956 // Update fast lookup maps if necessary.
0439c796
DK
3957 if (this->lookup_maps_->is_valid())
3958 {
67f95b96 3959 if (sis.is_relaxed_input_section())
0439c796
DK
3960 {
3961 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3962 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3963 poris->shndx(), poris);
3964 }
3965 }
20e6d0d6
DK
3966}
3967
8923b24c 3968// Save states for relaxation.
20e6d0d6
DK
3969
3970void
3971Output_section::save_states()
3972{
3973 gold_assert(this->checkpoint_ == NULL);
3974 Checkpoint_output_section* checkpoint =
3975 new Checkpoint_output_section(this->addralign_, this->flags_,
3976 this->input_sections_,
3977 this->first_input_offset_,
3978 this->attached_input_sections_are_sorted_);
3979 this->checkpoint_ = checkpoint;
3980 gold_assert(this->fills_.empty());
3981}
3982
8923b24c
DK
3983void
3984Output_section::discard_states()
3985{
3986 gold_assert(this->checkpoint_ != NULL);
3987 delete this->checkpoint_;
3988 this->checkpoint_ = NULL;
3989 gold_assert(this->fills_.empty());
3990
0439c796
DK
3991 // Simply invalidate the fast lookup maps since we do not keep
3992 // track of them.
3993 this->lookup_maps_->invalidate();
8923b24c
DK
3994}
3995
20e6d0d6
DK
3996void
3997Output_section::restore_states()
3998{
3999 gold_assert(this->checkpoint_ != NULL);
4000 Checkpoint_output_section* checkpoint = this->checkpoint_;
4001
4002 this->addralign_ = checkpoint->addralign();
4003 this->flags_ = checkpoint->flags();
4004 this->first_input_offset_ = checkpoint->first_input_offset();
4005
4006 if (!checkpoint->input_sections_saved())
4007 {
4008 // If we have not copied the input sections, just resize it.
4009 size_t old_size = checkpoint->input_sections_size();
4010 gold_assert(this->input_sections_.size() >= old_size);
4011 this->input_sections_.resize(old_size);
4012 }
4013 else
4014 {
4015 // We need to copy the whole list. This is not efficient for
4016 // extremely large output with hundreads of thousands of input
4017 // objects. We may need to re-think how we should pass sections
4018 // to scripts.
c0a62865 4019 this->input_sections_ = *checkpoint->input_sections();
20e6d0d6
DK
4020 }
4021
4022 this->attached_input_sections_are_sorted_ =
4023 checkpoint->attached_input_sections_are_sorted();
c0a62865 4024
0439c796
DK
4025 // Simply invalidate the fast lookup maps since we do not keep
4026 // track of them.
4027 this->lookup_maps_->invalidate();
a445fddf
ILT
4028}
4029
8923b24c
DK
4030// Update the section offsets of input sections in this. This is required if
4031// relaxation causes some input sections to change sizes.
4032
4033void
4034Output_section::adjust_section_offsets()
4035{
4036 if (!this->section_offsets_need_adjustment_)
4037 return;
4038
4039 off_t off = 0;
4040 for (Input_section_list::iterator p = this->input_sections_.begin();
4041 p != this->input_sections_.end();
4042 ++p)
4043 {
4044 off = align_address(off, p->addralign());
4045 if (p->is_input_section())
4046 p->relobj()->set_section_offset(p->shndx(), off);
4047 off += p->data_size();
4048 }
4049
4050 this->section_offsets_need_adjustment_ = false;
4051}
4052
7d9e3d98
ILT
4053// Print to the map file.
4054
4055void
4056Output_section::do_print_to_mapfile(Mapfile* mapfile) const
4057{
4058 mapfile->print_output_section(this);
4059
4060 for (Input_section_list::const_iterator p = this->input_sections_.begin();
4061 p != this->input_sections_.end();
4062 ++p)
4063 p->print_to_mapfile(mapfile);
4064}
4065
38c5e8b4
ILT
4066// Print stats for merge sections to stderr.
4067
4068void
4069Output_section::print_merge_stats()
4070{
4071 Input_section_list::iterator p;
4072 for (p = this->input_sections_.begin();
4073 p != this->input_sections_.end();
4074 ++p)
4075 p->print_merge_stats(this->name_);
4076}
4077
cdc29364
CC
4078// Set a fixed layout for the section. Used for incremental update links.
4079
4080void
4081Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
4082 off_t sh_size, uint64_t sh_addralign)
4083{
4084 this->addralign_ = sh_addralign;
4085 this->set_current_data_size(sh_size);
4086 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
4087 this->set_address(sh_addr);
4088 this->set_file_offset(sh_offset);
4089 this->finalize_data_size();
4090 this->free_list_.init(sh_size, false);
4091 this->has_fixed_layout_ = true;
4092}
4093
4094// Reserve space within the fixed layout for the section. Used for
4095// incremental update links.
5146f448 4096
cdc29364
CC
4097void
4098Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
4099{
4100 this->free_list_.remove(sh_offset, sh_offset + sh_size);
4101}
4102
5146f448
CC
4103// Allocate space from the free list for the section. Used for
4104// incremental update links.
4105
4106off_t
4107Output_section::allocate(off_t len, uint64_t addralign)
4108{
4109 return this->free_list_.allocate(len, addralign, 0);
4110}
4111
a2fb1b05
ILT
4112// Output segment methods.
4113
2ea97941 4114Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
22f0da72 4115 : vaddr_(0),
a2fb1b05
ILT
4116 paddr_(0),
4117 memsz_(0),
6bf4a340 4118 align_(0),
a445fddf
ILT
4119 max_align_(0),
4120 min_p_align_(0),
a2fb1b05
ILT
4121 offset_(0),
4122 filesz_(0),
2ea97941
ILT
4123 type_(type),
4124 flags_(flags),
a445fddf 4125 is_max_align_known_(false),
8a5e3e08 4126 are_addresses_set_(false),
16164a6b
ST
4127 is_large_data_segment_(false),
4128 is_unique_segment_(false)
a2fb1b05 4129{
bb321bb1
ILT
4130 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4131 // the flags.
4132 if (type == elfcpp::PT_TLS)
4133 this->flags_ = elfcpp::PF_R;
a2fb1b05
ILT
4134}
4135
22f0da72 4136// Add an Output_section to a PT_LOAD Output_segment.
a2fb1b05
ILT
4137
4138void
22f0da72
ILT
4139Output_segment::add_output_section_to_load(Layout* layout,
4140 Output_section* os,
4141 elfcpp::Elf_Word seg_flags)
a2fb1b05 4142{
22f0da72 4143 gold_assert(this->type() == elfcpp::PT_LOAD);
a3ad94ed 4144 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 4145 gold_assert(!this->is_max_align_known_);
8a5e3e08 4146 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
75f65a3e 4147
a192ba05 4148 this->update_flags_for_output_section(seg_flags);
75f65a3e 4149
22f0da72
ILT
4150 // We don't want to change the ordering if we have a linker script
4151 // with a SECTIONS clause.
4152 Output_section_order order = os->order();
4153 if (layout->script_options()->saw_sections_clause())
4154 order = static_cast<Output_section_order>(0);
75f65a3e 4155 else
22f0da72 4156 gold_assert(order != ORDER_INVALID);
54dc6425 4157
22f0da72
ILT
4158 this->output_lists_[order].push_back(os);
4159}
9f1d377b 4160
22f0da72 4161// Add an Output_section to a non-PT_LOAD Output_segment.
1a2dff53 4162
22f0da72
ILT
4163void
4164Output_segment::add_output_section_to_nonload(Output_section* os,
4165 elfcpp::Elf_Word seg_flags)
4166{
4167 gold_assert(this->type() != elfcpp::PT_LOAD);
4168 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4169 gold_assert(!this->is_max_align_known_);
1a2dff53 4170
22f0da72 4171 this->update_flags_for_output_section(seg_flags);
9f1d377b 4172
22f0da72
ILT
4173 this->output_lists_[0].push_back(os);
4174}
8a5e3e08 4175
22f0da72
ILT
4176// Remove an Output_section from this segment. It is an error if it
4177// is not present.
8a5e3e08 4178
22f0da72
ILT
4179void
4180Output_segment::remove_output_section(Output_section* os)
4181{
4182 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
8a5e3e08 4183 {
22f0da72
ILT
4184 Output_data_list* pdl = &this->output_lists_[i];
4185 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
8a5e3e08 4186 {
22f0da72 4187 if (*p == os)
8a5e3e08 4188 {
22f0da72 4189 pdl->erase(p);
8a5e3e08
ILT
4190 return;
4191 }
4192 }
f5c870d2 4193 }
1650c4ff
ILT
4194 gold_unreachable();
4195}
4196
a192ba05
ILT
4197// Add an Output_data (which need not be an Output_section) to the
4198// start of a segment.
75f65a3e
ILT
4199
4200void
4201Output_segment::add_initial_output_data(Output_data* od)
4202{
a445fddf 4203 gold_assert(!this->is_max_align_known_);
22f0da72
ILT
4204 Output_data_list::iterator p = this->output_lists_[0].begin();
4205 this->output_lists_[0].insert(p, od);
4206}
4207
4208// Return true if this segment has any sections which hold actual
4209// data, rather than being a BSS section.
4210
4211bool
4212Output_segment::has_any_data_sections() const
4213{
4214 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4215 {
4216 const Output_data_list* pdl = &this->output_lists_[i];
4217 for (Output_data_list::const_iterator p = pdl->begin();
4218 p != pdl->end();
4219 ++p)
4220 {
4221 if (!(*p)->is_section())
4222 return true;
4223 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4224 return true;
4225 }
4226 }
4227 return false;
75f65a3e
ILT
4228}
4229
5bc2f5be
CC
4230// Return whether the first data section (not counting TLS sections)
4231// is a relro section.
9f1d377b
ILT
4232
4233bool
4234Output_segment::is_first_section_relro() const
4235{
22f0da72
ILT
4236 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4237 {
bccffdfd 4238 if (i == static_cast<int>(ORDER_TLS_BSS))
5bc2f5be 4239 continue;
22f0da72
ILT
4240 const Output_data_list* pdl = &this->output_lists_[i];
4241 if (!pdl->empty())
4242 {
4243 Output_data* p = pdl->front();
4244 return p->is_section() && p->output_section()->is_relro();
4245 }
4246 }
4247 return false;
9f1d377b
ILT
4248}
4249
75f65a3e 4250// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
4251
4252uint64_t
a445fddf 4253Output_segment::maximum_alignment()
75f65a3e 4254{
a445fddf 4255 if (!this->is_max_align_known_)
ead1e424 4256 {
22f0da72 4257 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
50ed5eb1 4258 {
22f0da72
ILT
4259 const Output_data_list* pdl = &this->output_lists_[i];
4260 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4261 if (addralign > this->max_align_)
4262 this->max_align_ = addralign;
4263 }
a445fddf 4264 this->is_max_align_known_ = true;
ead1e424
ILT
4265 }
4266
a445fddf 4267 return this->max_align_;
75f65a3e
ILT
4268}
4269
ead1e424
ILT
4270// Return the maximum alignment of a list of Output_data.
4271
4272uint64_t
a445fddf 4273Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
4274{
4275 uint64_t ret = 0;
4276 for (Output_data_list::const_iterator p = pdl->begin();
4277 p != pdl->end();
4278 ++p)
4279 {
2ea97941
ILT
4280 uint64_t addralign = (*p)->addralign();
4281 if (addralign > ret)
4282 ret = addralign;
ead1e424
ILT
4283 }
4284 return ret;
4285}
4286
22f0da72 4287// Return whether this segment has any dynamic relocs.
4f4c5f80 4288
22f0da72
ILT
4289bool
4290Output_segment::has_dynamic_reloc() const
4f4c5f80 4291{
22f0da72
ILT
4292 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4293 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4294 return true;
4295 return false;
4f4c5f80
ILT
4296}
4297
22f0da72 4298// Return whether this Output_data_list has any dynamic relocs.
4f4c5f80 4299
22f0da72
ILT
4300bool
4301Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4f4c5f80 4302{
4f4c5f80
ILT
4303 for (Output_data_list::const_iterator p = pdl->begin();
4304 p != pdl->end();
4305 ++p)
22f0da72
ILT
4306 if ((*p)->has_dynamic_reloc())
4307 return true;
4308 return false;
4f4c5f80
ILT
4309}
4310
a445fddf
ILT
4311// Set the section addresses for an Output_segment. If RESET is true,
4312// reset the addresses first. ADDR is the address and *POFF is the
4313// file offset. Set the section indexes starting with *PSHNDX.
5bc2f5be
CC
4314// INCREASE_RELRO is the size of the portion of the first non-relro
4315// section that should be included in the PT_GNU_RELRO segment.
4316// If this segment has relro sections, and has been aligned for
4317// that purpose, set *HAS_RELRO to TRUE. Return the address of
4318// the immediately following segment. Update *HAS_RELRO, *POFF,
4319// and *PSHNDX.
75f65a3e
ILT
4320
4321uint64_t
eb426534
RM
4322Output_segment::set_section_addresses(const Target* target,
4323 Layout* layout, bool reset,
4324 uint64_t addr,
fd064a5b 4325 unsigned int* increase_relro,
fc497986 4326 bool* has_relro,
1a2dff53 4327 off_t* poff,
ead1e424 4328 unsigned int* pshndx)
75f65a3e 4329{
a3ad94ed 4330 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 4331
fc497986 4332 uint64_t last_relro_pad = 0;
1a2dff53
ILT
4333 off_t orig_off = *poff;
4334
5bc2f5be
CC
4335 bool in_tls = false;
4336
1a2dff53 4337 // If we have relro sections, we need to pad forward now so that the
815a1205 4338 // relro sections plus INCREASE_RELRO end on an abi page boundary.
1a2dff53
ILT
4339 if (parameters->options().relro()
4340 && this->is_first_section_relro()
4341 && (!this->are_addresses_set_ || reset))
4342 {
4343 uint64_t relro_size = 0;
4344 off_t off = *poff;
fc497986 4345 uint64_t max_align = 0;
5bc2f5be 4346 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
1a2dff53 4347 {
22f0da72
ILT
4348 Output_data_list* pdl = &this->output_lists_[i];
4349 Output_data_list::iterator p;
4350 for (p = pdl->begin(); p != pdl->end(); ++p)
1a2dff53 4351 {
22f0da72
ILT
4352 if (!(*p)->is_section())
4353 break;
fc497986
CC
4354 uint64_t align = (*p)->addralign();
4355 if (align > max_align)
4356 max_align = align;
5bc2f5be
CC
4357 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4358 in_tls = true;
4359 else if (in_tls)
4360 {
4361 // Align the first non-TLS section to the alignment
4362 // of the TLS segment.
4363 align = max_align;
4364 in_tls = false;
4365 }
5bc2f5be
CC
4366 // Ignore the size of the .tbss section.
4367 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4368 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4369 continue;
bccffdfd 4370 relro_size = align_address(relro_size, align);
22f0da72
ILT
4371 if ((*p)->is_address_valid())
4372 relro_size += (*p)->data_size();
4373 else
4374 {
4375 // FIXME: This could be faster.
5485698a
CC
4376 (*p)->set_address_and_file_offset(relro_size,
4377 relro_size);
22f0da72
ILT
4378 relro_size += (*p)->data_size();
4379 (*p)->reset_address_and_file_offset();
4380 }
1a2dff53 4381 }
22f0da72
ILT
4382 if (p != pdl->end())
4383 break;
1a2dff53 4384 }
fd064a5b 4385 relro_size += *increase_relro;
fc497986
CC
4386 // Pad the total relro size to a multiple of the maximum
4387 // section alignment seen.
4388 uint64_t aligned_size = align_address(relro_size, max_align);
4389 // Note the amount of padding added after the last relro section.
4390 last_relro_pad = aligned_size - relro_size;
fc497986 4391 *has_relro = true;
1a2dff53 4392
815a1205 4393 uint64_t page_align = parameters->target().abi_pagesize();
1a2dff53
ILT
4394
4395 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
fc497986 4396 uint64_t desired_align = page_align - (aligned_size % page_align);
5485698a
CC
4397 if (desired_align < off % page_align)
4398 off += page_align;
4399 off += desired_align - off % page_align;
4400 addr += off - orig_off;
4401 orig_off = off;
4402 *poff = off;
1a2dff53
ILT
4403 }
4404
a445fddf
ILT
4405 if (!reset && this->are_addresses_set_)
4406 {
4407 gold_assert(this->paddr_ == addr);
4408 addr = this->vaddr_;
4409 }
4410 else
4411 {
4412 this->vaddr_ = addr;
4413 this->paddr_ = addr;
4414 this->are_addresses_set_ = true;
4415 }
75f65a3e 4416
5bc2f5be 4417 in_tls = false;
96a2b4e4 4418
75f65a3e
ILT
4419 this->offset_ = orig_off;
4420
22f0da72 4421 off_t off = 0;
5d9f66cb 4422 off_t foff = *poff;
24228130 4423 uint64_t ret = 0;
22f0da72
ILT
4424 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4425 {
fc497986
CC
4426 if (i == static_cast<int>(ORDER_RELRO_LAST))
4427 {
4428 *poff += last_relro_pad;
5d9f66cb 4429 foff += last_relro_pad;
fc497986 4430 addr += last_relro_pad;
fd064a5b
CC
4431 if (this->output_lists_[i].empty())
4432 {
4433 // If there is nothing in the ORDER_RELRO_LAST list,
4434 // the padding will occur at the end of the relro
4435 // segment, and we need to add it to *INCREASE_RELRO.
4436 *increase_relro += last_relro_pad;
4437 }
fc497986 4438 }
5bc2f5be
CC
4439 addr = this->set_section_list_addresses(layout, reset,
4440 &this->output_lists_[i],
5d9f66cb
CC
4441 addr, poff, &foff, pshndx,
4442 &in_tls);
4443
4444 // FOFF tracks the last offset used for the file image,
4445 // and *POFF tracks the last offset used for the memory image.
4446 // When not using a linker script, bss sections should all
4447 // be processed in the ORDER_SMALL_BSS and later buckets.
4448 gold_assert(*poff == foff
4449 || i == static_cast<int>(ORDER_TLS_BSS)
4450 || i >= static_cast<int>(ORDER_SMALL_BSS)
4451 || layout->script_options()->saw_sections_clause());
4452
4453 this->filesz_ = foff - orig_off;
4454 off = foff;
75f65a3e 4455
22f0da72
ILT
4456 ret = addr;
4457 }
96a2b4e4
ILT
4458
4459 // If the last section was a TLS section, align upward to the
4460 // alignment of the TLS segment, so that the overall size of the TLS
4461 // segment is aligned.
4462 if (in_tls)
4463 {
4464 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4465 *poff = align_address(*poff, segment_align);
4466 }
4467
75f65a3e
ILT
4468 this->memsz_ = *poff - orig_off;
4469
4470 // Ignore the file offset adjustments made by the BSS Output_data
4471 // objects.
4472 *poff = off;
61ba1cf9 4473
eb426534
RM
4474 // If code segments must contain only code, and this code segment is
4475 // page-aligned in the file, then fill it out to a whole page with
4476 // code fill (the tail of the segment will not be within any section).
4477 // Thus the entire code segment can be mapped from the file as whole
4478 // pages and that mapping will contain only valid instructions.
4479 if (target->isolate_execinstr() && (this->flags() & elfcpp::PF_X) != 0)
4480 {
4481 uint64_t abi_pagesize = target->abi_pagesize();
4482 if (orig_off % abi_pagesize == 0 && off % abi_pagesize != 0)
4483 {
4484 size_t fill_size = abi_pagesize - (off % abi_pagesize);
4485
4486 std::string fill_data;
4487 if (target->has_code_fill())
4488 fill_data = target->code_fill(fill_size);
4489 else
4490 fill_data.resize(fill_size); // Zero fill.
4491
4492 Output_data_const* fill = new Output_data_const(fill_data, 0);
4493 fill->set_address(this->vaddr_ + this->memsz_);
4494 fill->set_file_offset(off);
4495 layout->add_relax_output(fill);
4496
4497 off += fill_size;
4498 gold_assert(off % abi_pagesize == 0);
4499 ret += fill_size;
4500 gold_assert(ret % abi_pagesize == 0);
4501
4502 gold_assert((uint64_t) this->filesz_ == this->memsz_);
4503 this->memsz_ = this->filesz_ += fill_size;
4504
4505 *poff = off;
4506 }
4507 }
4508
61ba1cf9 4509 return ret;
75f65a3e
ILT
4510}
4511
b8e6aad9
ILT
4512// Set the addresses and file offsets in a list of Output_data
4513// structures.
75f65a3e
ILT
4514
4515uint64_t
cdc29364 4516Output_segment::set_section_list_addresses(Layout* layout, bool reset,
eb426534 4517 Output_data_list* pdl,
ead1e424 4518 uint64_t addr, off_t* poff,
5d9f66cb 4519 off_t* pfoff,
96a2b4e4 4520 unsigned int* pshndx,
eb426534 4521 bool* in_tls)
75f65a3e 4522{
ead1e424 4523 off_t startoff = *poff;
cdc29364
CC
4524 // For incremental updates, we may allocate non-fixed sections from
4525 // free space in the file. This keeps track of the high-water mark.
4526 off_t maxoff = startoff;
75f65a3e 4527
ead1e424 4528 off_t off = startoff;
5d9f66cb 4529 off_t foff = *pfoff;
75f65a3e
ILT
4530 for (Output_data_list::iterator p = pdl->begin();
4531 p != pdl->end();
4532 ++p)
4533 {
5d9f66cb
CC
4534 bool is_bss = (*p)->is_section_type(elfcpp::SHT_NOBITS);
4535 bool is_tls = (*p)->is_section_flag_set(elfcpp::SHF_TLS);
4536
a445fddf
ILT
4537 if (reset)
4538 (*p)->reset_address_and_file_offset();
4539
cdc29364
CC
4540 // When doing an incremental update or when using a linker script,
4541 // the section will most likely already have an address.
a445fddf 4542 if (!(*p)->is_address_valid())
3802b2dd 4543 {
eb426534
RM
4544 uint64_t align = (*p)->addralign();
4545
5d9f66cb 4546 if (is_tls)
eb426534
RM
4547 {
4548 // Give the first TLS section the alignment of the
4549 // entire TLS segment. Otherwise the TLS segment as a
4550 // whole may be misaligned.
4551 if (!*in_tls)
4552 {
4553 Output_segment* tls_segment = layout->tls_segment();
4554 gold_assert(tls_segment != NULL);
4555 uint64_t segment_align = tls_segment->maximum_alignment();
4556 gold_assert(segment_align >= align);
4557 align = segment_align;
4558
4559 *in_tls = true;
4560 }
4561 }
4562 else
4563 {
4564 // If this is the first section after the TLS segment,
4565 // align it to at least the alignment of the TLS
4566 // segment, so that the size of the overall TLS segment
4567 // is aligned.
4568 if (*in_tls)
4569 {
4570 uint64_t segment_align =
4571 layout->tls_segment()->maximum_alignment();
4572 if (segment_align > align)
4573 align = segment_align;
4574
4575 *in_tls = false;
4576 }
4577 }
96a2b4e4 4578
fb0e076f 4579 if (!parameters->incremental_update())
cdc29364 4580 {
5d9f66cb 4581 gold_assert(off == foff || is_bss);
cdc29364 4582 off = align_address(off, align);
5d9f66cb
CC
4583 if (is_tls || !is_bss)
4584 foff = off;
4585 (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
cdc29364
CC
4586 }
4587 else
4588 {
4589 // Incremental update: allocate file space from free list.
4590 (*p)->pre_finalize_data_size();
4591 off_t current_size = (*p)->current_data_size();
4592 off = layout->allocate(current_size, align, startoff);
5d9f66cb 4593 foff = off;
cdc29364 4594 if (off == -1)
eb426534 4595 {
cdc29364 4596 gold_assert((*p)->output_section() != NULL);
e6455dfb
CC
4597 gold_fallback(_("out of patch space for section %s; "
4598 "relink with --incremental-full"),
4599 (*p)->output_section()->name());
eb426534 4600 }
5d9f66cb 4601 (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
cdc29364
CC
4602 if ((*p)->data_size() > current_size)
4603 {
4604 gold_assert((*p)->output_section() != NULL);
e6455dfb
CC
4605 gold_fallback(_("%s: section changed size; "
4606 "relink with --incremental-full"),
4607 (*p)->output_section()->name());
cdc29364
CC
4608 }
4609 }
3802b2dd 4610 }
cdc29364 4611 else if (parameters->incremental_update())
eb426534
RM
4612 {
4613 // For incremental updates, use the fixed offset for the
4614 // high-water mark computation.
4615 off = (*p)->offset();
5d9f66cb 4616 foff = off;
eb426534 4617 }
a445fddf
ILT
4618 else
4619 {
4620 // The script may have inserted a skip forward, but it
4621 // better not have moved backward.
661be1e2 4622 if ((*p)->address() >= addr + (off - startoff))
5d9f66cb
CC
4623 {
4624 if (!is_bss && off > foff)
4625 gold_warning(_("script places BSS section in the middle "
4626 "of a LOAD segment; space will be allocated "
4627 "in the file"));
4628 off += (*p)->address() - (addr + (off - startoff));
4629 if (is_tls || !is_bss)
4630 foff = off;
4631 }
661be1e2
ILT
4632 else
4633 {
4634 if (!layout->script_options()->saw_sections_clause())
4635 gold_unreachable();
4636 else
4637 {
4638 Output_section* os = (*p)->output_section();
64b1ae37
DK
4639
4640 // Cast to unsigned long long to avoid format warnings.
4641 unsigned long long previous_dot =
4642 static_cast<unsigned long long>(addr + (off - startoff));
4643 unsigned long long dot =
4644 static_cast<unsigned long long>((*p)->address());
4645
661be1e2
ILT
4646 if (os == NULL)
4647 gold_error(_("dot moves backward in linker script "
64b1ae37 4648 "from 0x%llx to 0x%llx"), previous_dot, dot);
661be1e2
ILT
4649 else
4650 gold_error(_("address of section '%s' moves backward "
4651 "from 0x%llx to 0x%llx"),
64b1ae37 4652 os->name(), previous_dot, dot);
661be1e2
ILT
4653 }
4654 }
5d9f66cb 4655 (*p)->set_file_offset(foff);
a445fddf
ILT
4656 (*p)->finalize_data_size();
4657 }
ead1e424 4658
9fbd3822
CC
4659 if (parameters->incremental_update())
4660 gold_debug(DEBUG_INCREMENTAL,
4661 "set_section_list_addresses: %08lx %08lx %s",
4662 static_cast<long>(off),
4663 static_cast<long>((*p)->data_size()),
4664 ((*p)->output_section() != NULL
4665 ? (*p)->output_section()->name() : "(special)"));
4666
4667 // We want to ignore the size of a SHF_TLS SHT_NOBITS
96a2b4e4
ILT
4668 // section. Such a section does not affect the size of a
4669 // PT_LOAD segment.
5d9f66cb 4670 if (!is_tls || !is_bss)
ead1e424 4671 off += (*p)->data_size();
75f65a3e 4672
5d9f66cb
CC
4673 // We don't allocate space in the file for SHT_NOBITS sections,
4674 // unless a script has force-placed one in the middle of a segment.
4675 if (!is_bss)
4676 foff = off;
4677
cdc29364 4678 if (off > maxoff)
eb426534 4679 maxoff = off;
cdc29364 4680
ead1e424
ILT
4681 if ((*p)->is_section())
4682 {
4683 (*p)->set_out_shndx(*pshndx);
4684 ++*pshndx;
4685 }
75f65a3e
ILT
4686 }
4687
cdc29364 4688 *poff = maxoff;
5d9f66cb 4689 *pfoff = foff;
cdc29364 4690 return addr + (maxoff - startoff);
75f65a3e
ILT
4691}
4692
4693// For a non-PT_LOAD segment, set the offset from the sections, if
1a2dff53 4694// any. Add INCREASE to the file size and the memory size.
75f65a3e
ILT
4695
4696void
1a2dff53 4697Output_segment::set_offset(unsigned int increase)
75f65a3e 4698{
a3ad94ed 4699 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 4700
a445fddf
ILT
4701 gold_assert(!this->are_addresses_set_);
4702
22f0da72
ILT
4703 // A non-load section only uses output_lists_[0].
4704
4705 Output_data_list* pdl = &this->output_lists_[0];
4706
4707 if (pdl->empty())
75f65a3e 4708 {
1a2dff53 4709 gold_assert(increase == 0);
75f65a3e
ILT
4710 this->vaddr_ = 0;
4711 this->paddr_ = 0;
a445fddf 4712 this->are_addresses_set_ = true;
75f65a3e 4713 this->memsz_ = 0;
a445fddf 4714 this->min_p_align_ = 0;
75f65a3e
ILT
4715 this->offset_ = 0;
4716 this->filesz_ = 0;
4717 return;
4718 }
4719
22f0da72 4720 // Find the first and last section by address.
5f1ab67a
ILT
4721 const Output_data* first = NULL;
4722 const Output_data* last_data = NULL;
4723 const Output_data* last_bss = NULL;
22f0da72
ILT
4724 for (Output_data_list::const_iterator p = pdl->begin();
4725 p != pdl->end();
4726 ++p)
4727 {
4728 if (first == NULL
4729 || (*p)->address() < first->address()
4730 || ((*p)->address() == first->address()
4731 && (*p)->data_size() < first->data_size()))
4732 first = *p;
4733 const Output_data** plast;
4734 if ((*p)->is_section()
4735 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4736 plast = &last_bss;
4737 else
4738 plast = &last_data;
4739 if (*plast == NULL
4740 || (*p)->address() > (*plast)->address()
4741 || ((*p)->address() == (*plast)->address()
4742 && (*p)->data_size() > (*plast)->data_size()))
4743 *plast = *p;
4744 }
5f1ab67a 4745
75f65a3e 4746 this->vaddr_ = first->address();
a445fddf
ILT
4747 this->paddr_ = (first->has_load_address()
4748 ? first->load_address()
4749 : this->vaddr_);
4750 this->are_addresses_set_ = true;
75f65a3e
ILT
4751 this->offset_ = first->offset();
4752
22f0da72 4753 if (last_data == NULL)
75f65a3e
ILT
4754 this->filesz_ = 0;
4755 else
5f1ab67a
ILT
4756 this->filesz_ = (last_data->address()
4757 + last_data->data_size()
4758 - this->vaddr_);
75f65a3e 4759
5f1ab67a 4760 const Output_data* last = last_bss != NULL ? last_bss : last_data;
75f65a3e
ILT
4761 this->memsz_ = (last->address()
4762 + last->data_size()
4763 - this->vaddr_);
96a2b4e4 4764
1a2dff53
ILT
4765 this->filesz_ += increase;
4766 this->memsz_ += increase;
4767
fd064a5b
CC
4768 // If this is a RELRO segment, verify that the segment ends at a
4769 // page boundary.
4770 if (this->type_ == elfcpp::PT_GNU_RELRO)
4771 {
815a1205 4772 uint64_t page_align = parameters->target().abi_pagesize();
fd064a5b 4773 uint64_t segment_end = this->vaddr_ + this->memsz_;
cdc29364
CC
4774 if (parameters->incremental_update())
4775 {
4776 // The INCREASE_RELRO calculation is bypassed for an incremental
4777 // update, so we need to adjust the segment size manually here.
4778 segment_end = align_address(segment_end, page_align);
4779 this->memsz_ = segment_end - this->vaddr_;
4780 }
4781 else
4782 gold_assert(segment_end == align_address(segment_end, page_align));
fd064a5b
CC
4783 }
4784
96a2b4e4
ILT
4785 // If this is a TLS segment, align the memory size. The code in
4786 // set_section_list ensures that the section after the TLS segment
4787 // is aligned to give us room.
4788 if (this->type_ == elfcpp::PT_TLS)
4789 {
4790 uint64_t segment_align = this->maximum_alignment();
4791 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4792 this->memsz_ = align_address(this->memsz_, segment_align);
4793 }
75f65a3e
ILT
4794}
4795
7bf1f802
ILT
4796// Set the TLS offsets of the sections in the PT_TLS segment.
4797
4798void
4799Output_segment::set_tls_offsets()
4800{
4801 gold_assert(this->type_ == elfcpp::PT_TLS);
4802
22f0da72
ILT
4803 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4804 p != this->output_lists_[0].end();
7bf1f802
ILT
4805 ++p)
4806 (*p)->set_tls_offset(this->vaddr_);
4807}
4808
7404fe1b 4809// Return the first section.
a445fddf 4810
7404fe1b
AM
4811Output_section*
4812Output_segment::first_section() const
a445fddf 4813{
22f0da72
ILT
4814 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4815 {
4816 const Output_data_list* pdl = &this->output_lists_[i];
4817 for (Output_data_list::const_iterator p = pdl->begin();
4818 p != pdl->end();
4819 ++p)
4820 {
4821 if ((*p)->is_section())
7404fe1b 4822 return (*p)->output_section();
22f0da72
ILT
4823 }
4824 }
eb390844 4825 return NULL;
a445fddf
ILT
4826}
4827
75f65a3e
ILT
4828// Return the number of Output_sections in an Output_segment.
4829
4830unsigned int
4831Output_segment::output_section_count() const
4832{
22f0da72
ILT
4833 unsigned int ret = 0;
4834 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4835 ret += this->output_section_count_list(&this->output_lists_[i]);
4836 return ret;
75f65a3e
ILT
4837}
4838
4839// Return the number of Output_sections in an Output_data_list.
4840
4841unsigned int
4842Output_segment::output_section_count_list(const Output_data_list* pdl) const
4843{
4844 unsigned int count = 0;
4845 for (Output_data_list::const_iterator p = pdl->begin();
4846 p != pdl->end();
4847 ++p)
4848 {
4849 if ((*p)->is_section())
4850 ++count;
4851 }
4852 return count;
a2fb1b05
ILT
4853}
4854
1c4f3631
ILT
4855// Return the section attached to the list segment with the lowest
4856// load address. This is used when handling a PHDRS clause in a
4857// linker script.
4858
4859Output_section*
4860Output_segment::section_with_lowest_load_address() const
4861{
4862 Output_section* found = NULL;
4863 uint64_t found_lma = 0;
22f0da72
ILT
4864 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4865 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4866 &found_lma);
1c4f3631
ILT
4867 return found;
4868}
4869
4870// Look through a list for a section with a lower load address.
4871
4872void
4873Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4874 Output_section** found,
4875 uint64_t* found_lma) const
4876{
4877 for (Output_data_list::const_iterator p = pdl->begin();
4878 p != pdl->end();
4879 ++p)
4880 {
4881 if (!(*p)->is_section())
4882 continue;
4883 Output_section* os = static_cast<Output_section*>(*p);
4884 uint64_t lma = (os->has_load_address()
4885 ? os->load_address()
4886 : os->address());
4887 if (*found == NULL || lma < *found_lma)
4888 {
4889 *found = os;
4890 *found_lma = lma;
4891 }
4892 }
4893}
4894
61ba1cf9
ILT
4895// Write the segment data into *OPHDR.
4896
4897template<int size, bool big_endian>
4898void
ead1e424 4899Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
4900{
4901 ophdr->put_p_type(this->type_);
4902 ophdr->put_p_offset(this->offset_);
4903 ophdr->put_p_vaddr(this->vaddr_);
4904 ophdr->put_p_paddr(this->paddr_);
4905 ophdr->put_p_filesz(this->filesz_);
4906 ophdr->put_p_memsz(this->memsz_);
4907 ophdr->put_p_flags(this->flags_);
a445fddf 4908 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
4909}
4910
4911// Write the section headers into V.
4912
4913template<int size, bool big_endian>
4914unsigned char*
16649710
ILT
4915Output_segment::write_section_headers(const Layout* layout,
4916 const Stringpool* secnamepool,
ead1e424 4917 unsigned char* v,
ca09d69a 4918 unsigned int* pshndx) const
5482377d 4919{
ead1e424
ILT
4920 // Every section that is attached to a segment must be attached to a
4921 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4922 // segments.
4923 if (this->type_ != elfcpp::PT_LOAD)
4924 return v;
4925
22f0da72
ILT
4926 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4927 {
4928 const Output_data_list* pdl = &this->output_lists_[i];
4929 v = this->write_section_headers_list<size, big_endian>(layout,
4930 secnamepool,
4931 pdl,
4932 v, pshndx);
4933 }
4934
61ba1cf9
ILT
4935 return v;
4936}
4937
4938template<int size, bool big_endian>
4939unsigned char*
16649710
ILT
4940Output_segment::write_section_headers_list(const Layout* layout,
4941 const Stringpool* secnamepool,
61ba1cf9 4942 const Output_data_list* pdl,
ead1e424 4943 unsigned char* v,
7d1a9ebb 4944 unsigned int* pshndx) const
61ba1cf9
ILT
4945{
4946 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4947 for (Output_data_list::const_iterator p = pdl->begin();
4948 p != pdl->end();
4949 ++p)
4950 {
4951 if ((*p)->is_section())
4952 {
5482377d 4953 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 4954 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 4955 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 4956 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 4957 v += shdr_size;
ead1e424 4958 ++*pshndx;
61ba1cf9
ILT
4959 }
4960 }
4961 return v;
4962}
4963
7d9e3d98
ILT
4964// Print the output sections to the map file.
4965
4966void
4967Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4968{
4969 if (this->type() != elfcpp::PT_LOAD)
4970 return;
22f0da72
ILT
4971 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4972 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
7d9e3d98
ILT
4973}
4974
4975// Print an output section list to the map file.
4976
4977void
4978Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4979 const Output_data_list* pdl) const
4980{
4981 for (Output_data_list::const_iterator p = pdl->begin();
4982 p != pdl->end();
4983 ++p)
4984 (*p)->print_to_mapfile(mapfile);
4985}
4986
a2fb1b05
ILT
4987// Output_file methods.
4988
14144f39
ILT
4989Output_file::Output_file(const char* name)
4990 : name_(name),
61ba1cf9
ILT
4991 o_(-1),
4992 file_size_(0),
c420411f 4993 base_(NULL),
516cb3d0 4994 map_is_anonymous_(false),
88597d34 4995 map_is_allocated_(false),
516cb3d0 4996 is_temporary_(false)
61ba1cf9
ILT
4997{
4998}
4999
404c2abb 5000// Try to open an existing file. Returns false if the file doesn't
aa92d6ed
CC
5001// exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
5002// NULL, open that file as the base for incremental linking, and
5003// copy its contents to the new output file. This routine can
5004// be called for incremental updates, in which case WRITABLE should
5005// be true, or by the incremental-dump utility, in which case
5006// WRITABLE should be false.
404c2abb
ILT
5007
5008bool
aa92d6ed 5009Output_file::open_base_file(const char* base_name, bool writable)
404c2abb
ILT
5010{
5011 // The name "-" means "stdout".
5012 if (strcmp(this->name_, "-") == 0)
5013 return false;
5014
aa92d6ed
CC
5015 bool use_base_file = base_name != NULL;
5016 if (!use_base_file)
5017 base_name = this->name_;
5018 else if (strcmp(base_name, this->name_) == 0)
5019 gold_fatal(_("%s: incremental base and output file name are the same"),
5020 base_name);
5021
404c2abb
ILT
5022 // Don't bother opening files with a size of zero.
5023 struct stat s;
aa92d6ed
CC
5024 if (::stat(base_name, &s) != 0)
5025 {
5026 gold_info(_("%s: stat: %s"), base_name, strerror(errno));
5027 return false;
5028 }
5029 if (s.st_size == 0)
5030 {
5031 gold_info(_("%s: incremental base file is empty"), base_name);
5032 return false;
5033 }
5034
5035 // If we're using a base file, we want to open it read-only.
5036 if (use_base_file)
5037 writable = false;
404c2abb 5038
aa92d6ed
CC
5039 int oflags = writable ? O_RDWR : O_RDONLY;
5040 int o = open_descriptor(-1, base_name, oflags, 0);
404c2abb 5041 if (o < 0)
aa92d6ed
CC
5042 {
5043 gold_info(_("%s: open: %s"), base_name, strerror(errno));
5044 return false;
5045 }
5046
5047 // If the base file and the output file are different, open a
5048 // new output file and read the contents from the base file into
5049 // the newly-mapped region.
5050 if (use_base_file)
5051 {
5052 this->open(s.st_size);
dfb45471
CC
5053 ssize_t bytes_to_read = s.st_size;
5054 unsigned char* p = this->base_;
5055 while (bytes_to_read > 0)
5056 {
5057 ssize_t len = ::read(o, p, bytes_to_read);
5058 if (len < 0)
5059 {
5060 gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
5061 return false;
5062 }
5063 if (len == 0)
5064 {
5065 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
5066 base_name,
5067 static_cast<long long>(s.st_size - bytes_to_read),
5068 static_cast<long long>(s.st_size));
5069 return false;
5070 }
5071 p += len;
5072 bytes_to_read -= len;
5073 }
aa92d6ed
CC
5074 ::close(o);
5075 return true;
5076 }
5077
404c2abb
ILT
5078 this->o_ = o;
5079 this->file_size_ = s.st_size;
5080
aa92d6ed 5081 if (!this->map_no_anonymous(writable))
404c2abb
ILT
5082 {
5083 release_descriptor(o, true);
5084 this->o_ = -1;
5085 this->file_size_ = 0;
5086 return false;
5087 }
5088
5089 return true;
5090}
5091
61ba1cf9
ILT
5092// Open the output file.
5093
a2fb1b05 5094void
61ba1cf9 5095Output_file::open(off_t file_size)
a2fb1b05 5096{
61ba1cf9
ILT
5097 this->file_size_ = file_size;
5098
4e9d8586
ILT
5099 // Unlink the file first; otherwise the open() may fail if the file
5100 // is busy (e.g. it's an executable that's currently being executed).
5101 //
5102 // However, the linker may be part of a system where a zero-length
5103 // file is created for it to write to, with tight permissions (gcc
5104 // 2.95 did something like this). Unlinking the file would work
5105 // around those permission controls, so we only unlink if the file
5106 // has a non-zero size. We also unlink only regular files to avoid
5107 // trouble with directories/etc.
5108 //
5109 // If we fail, continue; this command is merely a best-effort attempt
5110 // to improve the odds for open().
5111
42a1b686 5112 // We let the name "-" mean "stdout"
516cb3d0 5113 if (!this->is_temporary_)
42a1b686 5114 {
516cb3d0
ILT
5115 if (strcmp(this->name_, "-") == 0)
5116 this->o_ = STDOUT_FILENO;
5117 else
5118 {
5119 struct stat s;
6a89f575
CC
5120 if (::stat(this->name_, &s) == 0
5121 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
5122 {
5123 if (s.st_size != 0)
5124 ::unlink(this->name_);
5125 else if (!parameters->options().relocatable())
5126 {
5127 // If we don't unlink the existing file, add execute
5128 // permission where read permissions already exist
5129 // and where the umask permits.
5130 int mask = ::umask(0);
5131 ::umask(mask);
5132 s.st_mode |= (s.st_mode & 0444) >> 2;
5133 ::chmod(this->name_, s.st_mode & ~mask);
5134 }
5135 }
516cb3d0 5136
8851ecca 5137 int mode = parameters->options().relocatable() ? 0666 : 0777;
2a00e4fb
ILT
5138 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
5139 mode);
516cb3d0
ILT
5140 if (o < 0)
5141 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
5142 this->o_ = o;
5143 }
42a1b686 5144 }
61ba1cf9 5145
27bc2bce
ILT
5146 this->map();
5147}
5148
5149// Resize the output file.
5150
5151void
5152Output_file::resize(off_t file_size)
5153{
c420411f
ILT
5154 // If the mmap is mapping an anonymous memory buffer, this is easy:
5155 // just mremap to the new size. If it's mapping to a file, we want
5156 // to unmap to flush to the file, then remap after growing the file.
5157 if (this->map_is_anonymous_)
5158 {
88597d34
ILT
5159 void* base;
5160 if (!this->map_is_allocated_)
5161 {
5162 base = ::mremap(this->base_, this->file_size_, file_size,
5163 MREMAP_MAYMOVE);
5164 if (base == MAP_FAILED)
5165 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
5166 }
5167 else
5168 {
5169 base = realloc(this->base_, file_size);
5170 if (base == NULL)
5171 gold_nomem();
5172 if (file_size > this->file_size_)
5173 memset(static_cast<char*>(base) + this->file_size_, 0,
5174 file_size - this->file_size_);
5175 }
c420411f
ILT
5176 this->base_ = static_cast<unsigned char*>(base);
5177 this->file_size_ = file_size;
5178 }
5179 else
5180 {
5181 this->unmap();
5182 this->file_size_ = file_size;
aa92d6ed 5183 if (!this->map_no_anonymous(true))
fdcac5af 5184 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
c420411f 5185 }
27bc2bce
ILT
5186}
5187
404c2abb
ILT
5188// Map an anonymous block of memory which will later be written to the
5189// file. Return whether the map succeeded.
26736d8e 5190
404c2abb 5191bool
26736d8e
ILT
5192Output_file::map_anonymous()
5193{
404c2abb
ILT
5194 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
5195 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
88597d34 5196 if (base == MAP_FAILED)
404c2abb 5197 {
88597d34
ILT
5198 base = malloc(this->file_size_);
5199 if (base == NULL)
5200 return false;
5201 memset(base, 0, this->file_size_);
5202 this->map_is_allocated_ = true;
404c2abb 5203 }
88597d34
ILT
5204 this->base_ = static_cast<unsigned char*>(base);
5205 this->map_is_anonymous_ = true;
5206 return true;
26736d8e
ILT
5207}
5208
404c2abb 5209// Map the file into memory. Return whether the mapping succeeded.
aa92d6ed 5210// If WRITABLE is true, map with write access.
27bc2bce 5211
404c2abb 5212bool
aa92d6ed 5213Output_file::map_no_anonymous(bool writable)
27bc2bce 5214{
c420411f 5215 const int o = this->o_;
61ba1cf9 5216
c420411f
ILT
5217 // If the output file is not a regular file, don't try to mmap it;
5218 // instead, we'll mmap a block of memory (an anonymous buffer), and
5219 // then later write the buffer to the file.
5220 void* base;
5221 struct stat statbuf;
42a1b686
ILT
5222 if (o == STDOUT_FILENO || o == STDERR_FILENO
5223 || ::fstat(o, &statbuf) != 0
516cb3d0
ILT
5224 || !S_ISREG(statbuf.st_mode)
5225 || this->is_temporary_)
404c2abb
ILT
5226 return false;
5227
5228 // Ensure that we have disk space available for the file. If we
5229 // don't do this, it is possible that we will call munmap, close,
5230 // and exit with dirty buffers still in the cache with no assigned
5231 // disk blocks. If the disk is out of space at that point, the
5232 // output file will wind up incomplete, but we will have already
5233 // exited. The alternative to fallocate would be to use fdatasync,
5234 // but that would be a more significant performance hit.
bab9090f
CC
5235 if (writable)
5236 {
7c0640fa 5237 int err = gold_fallocate(o, 0, this->file_size_);
bab9090f
CC
5238 if (err != 0)
5239 gold_fatal(_("%s: %s"), this->name_, strerror(err));
5240 }
404c2abb
ILT
5241
5242 // Map the file into memory.
aa92d6ed
CC
5243 int prot = PROT_READ;
5244 if (writable)
5245 prot |= PROT_WRITE;
5246 base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
404c2abb
ILT
5247
5248 // The mmap call might fail because of file system issues: the file
5249 // system might not support mmap at all, or it might not support
5250 // mmap with PROT_WRITE.
61ba1cf9 5251 if (base == MAP_FAILED)
404c2abb
ILT
5252 return false;
5253
5254 this->map_is_anonymous_ = false;
61ba1cf9 5255 this->base_ = static_cast<unsigned char*>(base);
404c2abb
ILT
5256 return true;
5257}
5258
5259// Map the file into memory.
5260
5261void
5262Output_file::map()
5263{
7c0640fa
CC
5264 if (parameters->options().mmap_output_file()
5265 && this->map_no_anonymous(true))
404c2abb
ILT
5266 return;
5267
5268 // The mmap call might fail because of file system issues: the file
5269 // system might not support mmap at all, or it might not support
5270 // mmap with PROT_WRITE. I'm not sure which errno values we will
5271 // see in all cases, so if the mmap fails for any reason and we
5272 // don't care about file contents, try for an anonymous map.
5273 if (this->map_anonymous())
5274 return;
5275
5276 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
eb426534
RM
5277 this->name_, static_cast<unsigned long>(this->file_size_),
5278 strerror(errno));
61ba1cf9
ILT
5279}
5280
c420411f 5281// Unmap the file from memory.
61ba1cf9
ILT
5282
5283void
c420411f 5284Output_file::unmap()
61ba1cf9 5285{
88597d34
ILT
5286 if (this->map_is_anonymous_)
5287 {
5288 // We've already written out the data, so there is no reason to
5289 // waste time unmapping or freeing the memory.
5290 }
5291 else
5292 {
5293 if (::munmap(this->base_, this->file_size_) < 0)
5294 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5295 }
61ba1cf9 5296 this->base_ = NULL;
c420411f
ILT
5297}
5298
5299// Close the output file.
5300
5301void
5302Output_file::close()
5303{
5304 // If the map isn't file-backed, we need to write it now.
516cb3d0 5305 if (this->map_is_anonymous_ && !this->is_temporary_)
c420411f
ILT
5306 {
5307 size_t bytes_to_write = this->file_size_;
6d1e3092 5308 size_t offset = 0;
c420411f 5309 while (bytes_to_write > 0)
eb426534
RM
5310 {
5311 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5312 bytes_to_write);
5313 if (bytes_written == 0)
5314 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5315 else if (bytes_written < 0)
5316 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5317 else
5318 {
5319 bytes_to_write -= bytes_written;
5320 offset += bytes_written;
5321 }
5322 }
c420411f
ILT
5323 }
5324 this->unmap();
61ba1cf9 5325
42a1b686 5326 // We don't close stdout or stderr
516cb3d0
ILT
5327 if (this->o_ != STDOUT_FILENO
5328 && this->o_ != STDERR_FILENO
5329 && !this->is_temporary_)
42a1b686
ILT
5330 if (::close(this->o_) < 0)
5331 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 5332 this->o_ = -1;
a2fb1b05
ILT
5333}
5334
5335// Instantiate the templates we need. We could use the configure
5336// script to restrict this to only the ones for implemented targets.
5337
193a53d9 5338#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
5339template
5340off_t
5341Output_section::add_input_section<32, false>(
6e9ba2ca 5342 Layout* layout,
6fa2a40b 5343 Sized_relobj_file<32, false>* object,
2ea97941 5344 unsigned int shndx,
a2fb1b05 5345 const char* secname,
730cdc88 5346 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
5347 unsigned int reloc_shndx,
5348 bool have_sections_script);
193a53d9 5349#endif
a2fb1b05 5350
193a53d9 5351#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
5352template
5353off_t
5354Output_section::add_input_section<32, true>(
6e9ba2ca 5355 Layout* layout,
6fa2a40b 5356 Sized_relobj_file<32, true>* object,
2ea97941 5357 unsigned int shndx,
a2fb1b05 5358 const char* secname,
730cdc88 5359 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
5360 unsigned int reloc_shndx,
5361 bool have_sections_script);
193a53d9 5362#endif
a2fb1b05 5363
193a53d9 5364#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
5365template
5366off_t
5367Output_section::add_input_section<64, false>(
6e9ba2ca 5368 Layout* layout,
6fa2a40b 5369 Sized_relobj_file<64, false>* object,
2ea97941 5370 unsigned int shndx,
a2fb1b05 5371 const char* secname,
730cdc88 5372 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
5373 unsigned int reloc_shndx,
5374 bool have_sections_script);
193a53d9 5375#endif
a2fb1b05 5376
193a53d9 5377#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
5378template
5379off_t
5380Output_section::add_input_section<64, true>(
6e9ba2ca 5381 Layout* layout,
6fa2a40b 5382 Sized_relobj_file<64, true>* object,
2ea97941 5383 unsigned int shndx,
a2fb1b05 5384 const char* secname,
730cdc88 5385 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
5386 unsigned int reloc_shndx,
5387 bool have_sections_script);
193a53d9 5388#endif
a2fb1b05 5389
bbbfea06
CC
5390#ifdef HAVE_TARGET_32_LITTLE
5391template
5392class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5393#endif
5394
5395#ifdef HAVE_TARGET_32_BIG
5396template
5397class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5398#endif
5399
5400#ifdef HAVE_TARGET_64_LITTLE
5401template
5402class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5403#endif
5404
5405#ifdef HAVE_TARGET_64_BIG
5406template
5407class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5408#endif
5409
5410#ifdef HAVE_TARGET_32_LITTLE
5411template
5412class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5413#endif
5414
5415#ifdef HAVE_TARGET_32_BIG
5416template
5417class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5418#endif
5419
5420#ifdef HAVE_TARGET_64_LITTLE
5421template
5422class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5423#endif
5424
5425#ifdef HAVE_TARGET_64_BIG
5426template
5427class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5428#endif
5429
5430#ifdef HAVE_TARGET_32_LITTLE
5431template
5432class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5433#endif
5434
5435#ifdef HAVE_TARGET_32_BIG
5436template
5437class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5438#endif
5439
5440#ifdef HAVE_TARGET_64_LITTLE
5441template
5442class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5443#endif
5444
5445#ifdef HAVE_TARGET_64_BIG
5446template
5447class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5448#endif
5449
5450#ifdef HAVE_TARGET_32_LITTLE
5451template
5452class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5453#endif
5454
5455#ifdef HAVE_TARGET_32_BIG
5456template
5457class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5458#endif
5459
5460#ifdef HAVE_TARGET_64_LITTLE
5461template
5462class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5463#endif
5464
5465#ifdef HAVE_TARGET_64_BIG
5466template
5467class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5468#endif
5469
193a53d9 5470#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
5471template
5472class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 5473#endif
c06b7b0b 5474
193a53d9 5475#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
5476template
5477class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 5478#endif
c06b7b0b 5479
193a53d9 5480#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
5481template
5482class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 5483#endif
c06b7b0b 5484
193a53d9 5485#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
5486template
5487class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 5488#endif
c06b7b0b 5489
193a53d9 5490#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
5491template
5492class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 5493#endif
c06b7b0b 5494
193a53d9 5495#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
5496template
5497class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 5498#endif
c06b7b0b 5499
193a53d9 5500#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
5501template
5502class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 5503#endif
c06b7b0b 5504
193a53d9 5505#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
5506template
5507class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 5508#endif
c06b7b0b 5509
193a53d9 5510#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
5511template
5512class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 5513#endif
c06b7b0b 5514
193a53d9 5515#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
5516template
5517class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 5518#endif
c06b7b0b 5519
193a53d9 5520#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
5521template
5522class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 5523#endif
c06b7b0b 5524
193a53d9 5525#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
5526template
5527class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 5528#endif
c06b7b0b 5529
193a53d9 5530#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
5531template
5532class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 5533#endif
c06b7b0b 5534
193a53d9 5535#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
5536template
5537class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 5538#endif
c06b7b0b 5539
193a53d9 5540#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
5541template
5542class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 5543#endif
c06b7b0b 5544
193a53d9 5545#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
5546template
5547class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 5548#endif
c06b7b0b 5549
6a74a719
ILT
5550#ifdef HAVE_TARGET_32_LITTLE
5551template
5552class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5553#endif
5554
5555#ifdef HAVE_TARGET_32_BIG
5556template
5557class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5558#endif
5559
5560#ifdef HAVE_TARGET_64_LITTLE
5561template
5562class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5563#endif
5564
5565#ifdef HAVE_TARGET_64_BIG
5566template
5567class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5568#endif
5569
5570#ifdef HAVE_TARGET_32_LITTLE
5571template
5572class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5573#endif
5574
5575#ifdef HAVE_TARGET_32_BIG
5576template
5577class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5578#endif
5579
5580#ifdef HAVE_TARGET_64_LITTLE
5581template
5582class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5583#endif
5584
5585#ifdef HAVE_TARGET_64_BIG
5586template
5587class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5588#endif
5589
5590#ifdef HAVE_TARGET_32_LITTLE
5591template
5592class Output_data_group<32, false>;
5593#endif
5594
5595#ifdef HAVE_TARGET_32_BIG
5596template
5597class Output_data_group<32, true>;
5598#endif
5599
5600#ifdef HAVE_TARGET_64_LITTLE
5601template
5602class Output_data_group<64, false>;
5603#endif
5604
5605#ifdef HAVE_TARGET_64_BIG
5606template
5607class Output_data_group<64, true>;
5608#endif
5609
ead1e424 5610template
dbe717ef 5611class Output_data_got<32, false>;
ead1e424
ILT
5612
5613template
dbe717ef 5614class Output_data_got<32, true>;
ead1e424
ILT
5615
5616template
dbe717ef 5617class Output_data_got<64, false>;
ead1e424
ILT
5618
5619template
dbe717ef 5620class Output_data_got<64, true>;
ead1e424 5621
a2fb1b05 5622} // End namespace gold.
This page took 0.990706 seconds and 4 git commands to generate.