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