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