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