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