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