*** 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,
a10ae760 428 const Output_segment_headers* osh)
9025d29d 429 : target_(target),
75f65a3e 430 symtab_(symtab),
61ba1cf9 431 segment_header_(osh),
75f65a3e 432 section_header_(NULL),
a10ae760 433 shstrtab_(NULL)
75f65a3e 434{
20e6d0d6 435 this->set_data_size(this->do_size());
75f65a3e
ILT
436}
437
438// Set the section table information for a file header.
439
440void
441Output_file_header::set_section_info(const Output_section_headers* shdrs,
442 const Output_section* shstrtab)
443{
444 this->section_header_ = shdrs;
445 this->shstrtab_ = shstrtab;
446}
447
448// Write out the file header.
449
450void
61ba1cf9 451Output_file_header::do_write(Output_file* of)
54dc6425 452{
27bc2bce
ILT
453 gold_assert(this->offset() == 0);
454
8851ecca 455 switch (parameters->size_and_endianness())
61ba1cf9 456 {
9025d29d 457#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
458 case Parameters::TARGET_32_LITTLE:
459 this->do_sized_write<32, false>(of);
460 break;
9025d29d 461#endif
8851ecca
ILT
462#ifdef HAVE_TARGET_32_BIG
463 case Parameters::TARGET_32_BIG:
464 this->do_sized_write<32, true>(of);
465 break;
9025d29d 466#endif
9025d29d 467#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
468 case Parameters::TARGET_64_LITTLE:
469 this->do_sized_write<64, false>(of);
470 break;
9025d29d 471#endif
8851ecca
ILT
472#ifdef HAVE_TARGET_64_BIG
473 case Parameters::TARGET_64_BIG:
474 this->do_sized_write<64, true>(of);
475 break;
476#endif
477 default:
478 gold_unreachable();
61ba1cf9 479 }
61ba1cf9
ILT
480}
481
cc643b88 482// Write out the file header with appropriate size and endianness.
61ba1cf9
ILT
483
484template<int size, bool big_endian>
485void
486Output_file_header::do_sized_write(Output_file* of)
487{
a3ad94ed 488 gold_assert(this->offset() == 0);
61ba1cf9
ILT
489
490 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
491 unsigned char* view = of->get_output_view(0, ehdr_size);
492 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
493
494 unsigned char e_ident[elfcpp::EI_NIDENT];
495 memset(e_ident, 0, elfcpp::EI_NIDENT);
496 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
497 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
498 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
499 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
500 if (size == 32)
501 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
502 else if (size == 64)
503 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
504 else
a3ad94ed 505 gold_unreachable();
61ba1cf9
ILT
506 e_ident[elfcpp::EI_DATA] = (big_endian
507 ? elfcpp::ELFDATA2MSB
508 : elfcpp::ELFDATA2LSB);
509 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
61ba1cf9
ILT
510 oehdr.put_e_ident(e_ident);
511
512 elfcpp::ET e_type;
8851ecca 513 if (parameters->options().relocatable())
61ba1cf9 514 e_type = elfcpp::ET_REL;
374ad285 515 else if (parameters->options().output_is_position_independent())
436ca963 516 e_type = elfcpp::ET_DYN;
61ba1cf9
ILT
517 else
518 e_type = elfcpp::ET_EXEC;
519 oehdr.put_e_type(e_type);
520
521 oehdr.put_e_machine(this->target_->machine_code());
522 oehdr.put_e_version(elfcpp::EV_CURRENT);
523
d391083d 524 oehdr.put_e_entry(this->entry<size>());
61ba1cf9 525
6a74a719
ILT
526 if (this->segment_header_ == NULL)
527 oehdr.put_e_phoff(0);
528 else
529 oehdr.put_e_phoff(this->segment_header_->offset());
530
61ba1cf9 531 oehdr.put_e_shoff(this->section_header_->offset());
d5b40221 532 oehdr.put_e_flags(this->target_->processor_specific_flags());
61ba1cf9 533 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
6a74a719
ILT
534
535 if (this->segment_header_ == NULL)
536 {
537 oehdr.put_e_phentsize(0);
538 oehdr.put_e_phnum(0);
539 }
540 else
541 {
542 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
5696ab0b
ILT
543 size_t phnum = (this->segment_header_->data_size()
544 / elfcpp::Elf_sizes<size>::phdr_size);
545 if (phnum > elfcpp::PN_XNUM)
546 phnum = elfcpp::PN_XNUM;
547 oehdr.put_e_phnum(phnum);
6a74a719
ILT
548 }
549
61ba1cf9 550 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
d491d34e
ILT
551 size_t section_count = (this->section_header_->data_size()
552 / elfcpp::Elf_sizes<size>::shdr_size);
553
554 if (section_count < elfcpp::SHN_LORESERVE)
555 oehdr.put_e_shnum(this->section_header_->data_size()
556 / elfcpp::Elf_sizes<size>::shdr_size);
557 else
558 oehdr.put_e_shnum(0);
559
560 unsigned int shstrndx = this->shstrtab_->out_shndx();
561 if (shstrndx < elfcpp::SHN_LORESERVE)
562 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
563 else
564 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
61ba1cf9 565
36959681
ILT
566 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
567 // the e_ident field.
568 parameters->target().adjust_elf_header(view, ehdr_size);
569
61ba1cf9 570 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
571}
572
a10ae760 573// Return the value to use for the entry address.
d391083d
ILT
574
575template<int size>
576typename elfcpp::Elf_types<size>::Elf_Addr
577Output_file_header::entry()
578{
a10ae760 579 const bool should_issue_warning = (parameters->options().entry() != NULL
8851ecca
ILT
580 && !parameters->options().relocatable()
581 && !parameters->options().shared());
a10ae760 582 const char* entry = parameters->entry();
2ea97941 583 Symbol* sym = this->symtab_->lookup(entry);
d391083d
ILT
584
585 typename Sized_symbol<size>::Value_type v;
586 if (sym != NULL)
587 {
588 Sized_symbol<size>* ssym;
589 ssym = this->symtab_->get_sized_symbol<size>(sym);
590 if (!ssym->is_defined() && should_issue_warning)
2ea97941 591 gold_warning("entry symbol '%s' exists but is not defined", entry);
d391083d
ILT
592 v = ssym->value();
593 }
594 else
595 {
596 // We couldn't find the entry symbol. See if we can parse it as
597 // a number. This supports, e.g., -e 0x1000.
598 char* endptr;
2ea97941 599 v = strtoull(entry, &endptr, 0);
d391083d
ILT
600 if (*endptr != '\0')
601 {
602 if (should_issue_warning)
2ea97941 603 gold_warning("cannot find entry symbol '%s'", entry);
d391083d
ILT
604 v = 0;
605 }
606 }
607
608 return v;
609}
610
20e6d0d6
DK
611// Compute the current data size.
612
613off_t
614Output_file_header::do_size() const
615{
616 const int size = parameters->target().get_size();
617 if (size == 32)
618 return elfcpp::Elf_sizes<32>::ehdr_size;
619 else if (size == 64)
620 return elfcpp::Elf_sizes<64>::ehdr_size;
621 else
622 gold_unreachable();
623}
624
dbe717ef
ILT
625// Output_data_const methods.
626
627void
a3ad94ed 628Output_data_const::do_write(Output_file* of)
dbe717ef 629{
a3ad94ed
ILT
630 of->write(this->offset(), this->data_.data(), this->data_.size());
631}
632
633// Output_data_const_buffer methods.
634
635void
636Output_data_const_buffer::do_write(Output_file* of)
637{
638 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
639}
640
641// Output_section_data methods.
642
16649710
ILT
643// Record the output section, and set the entry size and such.
644
645void
646Output_section_data::set_output_section(Output_section* os)
647{
648 gold_assert(this->output_section_ == NULL);
649 this->output_section_ = os;
650 this->do_adjust_output_section(os);
651}
652
653// Return the section index of the output section.
654
dbe717ef
ILT
655unsigned int
656Output_section_data::do_out_shndx() const
657{
a3ad94ed 658 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
659 return this->output_section_->out_shndx();
660}
661
759b1a24
ILT
662// Set the alignment, which means we may need to update the alignment
663// of the output section.
664
665void
2ea97941 666Output_section_data::set_addralign(uint64_t addralign)
759b1a24 667{
2ea97941 668 this->addralign_ = addralign;
759b1a24 669 if (this->output_section_ != NULL
2ea97941
ILT
670 && this->output_section_->addralign() < addralign)
671 this->output_section_->set_addralign(addralign);
759b1a24
ILT
672}
673
a3ad94ed
ILT
674// Output_data_strtab methods.
675
27bc2bce 676// Set the final data size.
a3ad94ed
ILT
677
678void
27bc2bce 679Output_data_strtab::set_final_data_size()
a3ad94ed
ILT
680{
681 this->strtab_->set_string_offsets();
682 this->set_data_size(this->strtab_->get_strtab_size());
683}
684
685// Write out a string table.
686
687void
688Output_data_strtab::do_write(Output_file* of)
689{
690 this->strtab_->write(of, this->offset());
691}
692
c06b7b0b
ILT
693// Output_reloc methods.
694
7bf1f802
ILT
695// A reloc against a global symbol.
696
697template<bool dynamic, int size, bool big_endian>
698Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
699 Symbol* gsym,
700 unsigned int type,
701 Output_data* od,
e8c846c3 702 Address address,
0da6fa6c
DM
703 bool is_relative,
704 bool is_symbolless)
7bf1f802 705 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
0da6fa6c
DM
706 is_relative_(is_relative), is_symbolless_(is_symbolless),
707 is_section_symbol_(false), shndx_(INVALID_CODE)
7bf1f802 708{
dceae3c1
ILT
709 // this->type_ is a bitfield; make sure TYPE fits.
710 gold_assert(this->type_ == type);
7bf1f802
ILT
711 this->u1_.gsym = gsym;
712 this->u2_.od = od;
dceae3c1
ILT
713 if (dynamic)
714 this->set_needs_dynsym_index();
7bf1f802
ILT
715}
716
717template<bool dynamic, int size, bool big_endian>
718Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
719 Symbol* gsym,
720 unsigned int type,
ef9beddf 721 Sized_relobj<size, big_endian>* relobj,
7bf1f802 722 unsigned int shndx,
e8c846c3 723 Address address,
0da6fa6c
DM
724 bool is_relative,
725 bool is_symbolless)
7bf1f802 726 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
0da6fa6c
DM
727 is_relative_(is_relative), is_symbolless_(is_symbolless),
728 is_section_symbol_(false), shndx_(shndx)
7bf1f802
ILT
729{
730 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
731 // this->type_ is a bitfield; make sure TYPE fits.
732 gold_assert(this->type_ == type);
7bf1f802
ILT
733 this->u1_.gsym = gsym;
734 this->u2_.relobj = relobj;
dceae3c1
ILT
735 if (dynamic)
736 this->set_needs_dynsym_index();
7bf1f802
ILT
737}
738
739// A reloc against a local symbol.
740
741template<bool dynamic, int size, bool big_endian>
742Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
743 Sized_relobj<size, big_endian>* relobj,
744 unsigned int local_sym_index,
745 unsigned int type,
746 Output_data* od,
e8c846c3 747 Address address,
2ea97941 748 bool is_relative,
0da6fa6c 749 bool is_symbolless,
dceae3c1 750 bool is_section_symbol)
7bf1f802 751 : address_(address), local_sym_index_(local_sym_index), type_(type),
0da6fa6c
DM
752 is_relative_(is_relative), is_symbolless_(is_symbolless),
753 is_section_symbol_(is_section_symbol), shndx_(INVALID_CODE)
7bf1f802
ILT
754{
755 gold_assert(local_sym_index != GSYM_CODE
756 && local_sym_index != INVALID_CODE);
dceae3c1
ILT
757 // this->type_ is a bitfield; make sure TYPE fits.
758 gold_assert(this->type_ == type);
7bf1f802
ILT
759 this->u1_.relobj = relobj;
760 this->u2_.od = od;
dceae3c1
ILT
761 if (dynamic)
762 this->set_needs_dynsym_index();
7bf1f802
ILT
763}
764
765template<bool dynamic, int size, bool big_endian>
766Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
767 Sized_relobj<size, big_endian>* relobj,
768 unsigned int local_sym_index,
769 unsigned int type,
770 unsigned int shndx,
e8c846c3 771 Address address,
2ea97941 772 bool is_relative,
0da6fa6c 773 bool is_symbolless,
dceae3c1 774 bool is_section_symbol)
7bf1f802 775 : address_(address), local_sym_index_(local_sym_index), type_(type),
0da6fa6c
DM
776 is_relative_(is_relative), is_symbolless_(is_symbolless),
777 is_section_symbol_(is_section_symbol), shndx_(shndx)
7bf1f802
ILT
778{
779 gold_assert(local_sym_index != GSYM_CODE
780 && local_sym_index != INVALID_CODE);
781 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
782 // this->type_ is a bitfield; make sure TYPE fits.
783 gold_assert(this->type_ == type);
7bf1f802
ILT
784 this->u1_.relobj = relobj;
785 this->u2_.relobj = relobj;
dceae3c1
ILT
786 if (dynamic)
787 this->set_needs_dynsym_index();
7bf1f802
ILT
788}
789
790// A reloc against the STT_SECTION symbol of an output section.
791
792template<bool dynamic, int size, bool big_endian>
793Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
794 Output_section* os,
795 unsigned int type,
796 Output_data* od,
797 Address address)
798 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
0da6fa6c
DM
799 is_relative_(false), is_symbolless_(false),
800 is_section_symbol_(true), shndx_(INVALID_CODE)
7bf1f802 801{
dceae3c1
ILT
802 // this->type_ is a bitfield; make sure TYPE fits.
803 gold_assert(this->type_ == type);
7bf1f802
ILT
804 this->u1_.os = os;
805 this->u2_.od = od;
806 if (dynamic)
dceae3c1
ILT
807 this->set_needs_dynsym_index();
808 else
809 os->set_needs_symtab_index();
7bf1f802
ILT
810}
811
812template<bool dynamic, int size, bool big_endian>
813Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
814 Output_section* os,
815 unsigned int type,
ef9beddf 816 Sized_relobj<size, big_endian>* relobj,
7bf1f802
ILT
817 unsigned int shndx,
818 Address address)
819 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
0da6fa6c
DM
820 is_relative_(false), is_symbolless_(false),
821 is_section_symbol_(true), shndx_(shndx)
7bf1f802
ILT
822{
823 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
824 // this->type_ is a bitfield; make sure TYPE fits.
825 gold_assert(this->type_ == type);
7bf1f802
ILT
826 this->u1_.os = os;
827 this->u2_.relobj = relobj;
828 if (dynamic)
dceae3c1
ILT
829 this->set_needs_dynsym_index();
830 else
831 os->set_needs_symtab_index();
832}
833
e291e7b9
ILT
834// An absolute relocation.
835
836template<bool dynamic, int size, bool big_endian>
837Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
838 unsigned int type,
839 Output_data* od,
840 Address address)
841 : address_(address), local_sym_index_(0), type_(type),
0da6fa6c
DM
842 is_relative_(false), is_symbolless_(false),
843 is_section_symbol_(false), shndx_(INVALID_CODE)
e291e7b9
ILT
844{
845 // this->type_ is a bitfield; make sure TYPE fits.
846 gold_assert(this->type_ == type);
847 this->u1_.relobj = NULL;
848 this->u2_.od = od;
849}
850
851template<bool dynamic, int size, bool big_endian>
852Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
853 unsigned int type,
854 Sized_relobj<size, big_endian>* relobj,
855 unsigned int shndx,
856 Address address)
857 : address_(address), local_sym_index_(0), type_(type),
0da6fa6c
DM
858 is_relative_(false), is_symbolless_(false),
859 is_section_symbol_(false), shndx_(shndx)
e291e7b9
ILT
860{
861 gold_assert(shndx != INVALID_CODE);
862 // this->type_ is a bitfield; make sure TYPE fits.
863 gold_assert(this->type_ == type);
864 this->u1_.relobj = NULL;
865 this->u2_.relobj = relobj;
866}
867
868// A target specific relocation.
869
870template<bool dynamic, int size, bool big_endian>
871Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
872 unsigned int type,
873 void* arg,
874 Output_data* od,
875 Address address)
876 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
0da6fa6c
DM
877 is_relative_(false), is_symbolless_(false),
878 is_section_symbol_(false), shndx_(INVALID_CODE)
e291e7b9
ILT
879{
880 // this->type_ is a bitfield; make sure TYPE fits.
881 gold_assert(this->type_ == type);
882 this->u1_.arg = arg;
883 this->u2_.od = od;
884}
885
886template<bool dynamic, int size, bool big_endian>
887Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
888 unsigned int type,
889 void* arg,
890 Sized_relobj<size, big_endian>* relobj,
891 unsigned int shndx,
892 Address address)
893 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
0da6fa6c
DM
894 is_relative_(false), is_symbolless_(false),
895 is_section_symbol_(false), shndx_(shndx)
e291e7b9
ILT
896{
897 gold_assert(shndx != INVALID_CODE);
898 // this->type_ is a bitfield; make sure TYPE fits.
899 gold_assert(this->type_ == type);
900 this->u1_.arg = arg;
901 this->u2_.relobj = relobj;
902}
903
dceae3c1
ILT
904// Record that we need a dynamic symbol index for this relocation.
905
906template<bool dynamic, int size, bool big_endian>
907void
908Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
909set_needs_dynsym_index()
910{
0da6fa6c 911 if (this->is_symbolless_)
dceae3c1
ILT
912 return;
913 switch (this->local_sym_index_)
914 {
915 case INVALID_CODE:
916 gold_unreachable();
917
918 case GSYM_CODE:
919 this->u1_.gsym->set_needs_dynsym_entry();
920 break;
921
922 case SECTION_CODE:
923 this->u1_.os->set_needs_dynsym_index();
924 break;
925
e291e7b9
ILT
926 case TARGET_CODE:
927 // The target must take care of this if necessary.
928 break;
929
dceae3c1
ILT
930 case 0:
931 break;
932
933 default:
934 {
935 const unsigned int lsi = this->local_sym_index_;
6fa2a40b
CC
936 Sized_relobj_file<size, big_endian>* relobj =
937 this->u1_.relobj->sized_relobj();
938 gold_assert(relobj != NULL);
dceae3c1 939 if (!this->is_section_symbol_)
6fa2a40b 940 relobj->set_needs_output_dynsym_entry(lsi);
dceae3c1 941 else
6fa2a40b 942 relobj->output_section(lsi)->set_needs_dynsym_index();
dceae3c1
ILT
943 }
944 break;
945 }
7bf1f802
ILT
946}
947
c06b7b0b
ILT
948// Get the symbol index of a relocation.
949
950template<bool dynamic, int size, bool big_endian>
951unsigned int
952Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
953 const
954{
955 unsigned int index;
0da6fa6c
DM
956 if (this->is_symbolless_)
957 return 0;
c06b7b0b
ILT
958 switch (this->local_sym_index_)
959 {
960 case INVALID_CODE:
a3ad94ed 961 gold_unreachable();
c06b7b0b
ILT
962
963 case GSYM_CODE:
5a6f7e2d 964 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
965 index = 0;
966 else if (dynamic)
5a6f7e2d 967 index = this->u1_.gsym->dynsym_index();
c06b7b0b 968 else
5a6f7e2d 969 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
970 break;
971
972 case SECTION_CODE:
973 if (dynamic)
5a6f7e2d 974 index = this->u1_.os->dynsym_index();
c06b7b0b 975 else
5a6f7e2d 976 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
977 break;
978
e291e7b9
ILT
979 case TARGET_CODE:
980 index = parameters->target().reloc_symbol_index(this->u1_.arg,
981 this->type_);
982 break;
983
436ca963
ILT
984 case 0:
985 // Relocations without symbols use a symbol index of 0.
986 index = 0;
987 break;
988
c06b7b0b 989 default:
dceae3c1
ILT
990 {
991 const unsigned int lsi = this->local_sym_index_;
6fa2a40b
CC
992 Sized_relobj_file<size, big_endian>* relobj =
993 this->u1_.relobj->sized_relobj();
994 gold_assert(relobj != NULL);
dceae3c1
ILT
995 if (!this->is_section_symbol_)
996 {
997 if (dynamic)
6fa2a40b 998 index = relobj->dynsym_index(lsi);
dceae3c1 999 else
6fa2a40b 1000 index = relobj->symtab_index(lsi);
dceae3c1
ILT
1001 }
1002 else
1003 {
6fa2a40b 1004 Output_section* os = relobj->output_section(lsi);
dceae3c1
ILT
1005 gold_assert(os != NULL);
1006 if (dynamic)
1007 index = os->dynsym_index();
1008 else
1009 index = os->symtab_index();
1010 }
1011 }
c06b7b0b
ILT
1012 break;
1013 }
a3ad94ed 1014 gold_assert(index != -1U);
c06b7b0b
ILT
1015 return index;
1016}
1017
624f8810
ILT
1018// For a local section symbol, get the address of the offset ADDEND
1019// within the input section.
dceae3c1
ILT
1020
1021template<bool dynamic, int size, bool big_endian>
ef9beddf 1022typename elfcpp::Elf_types<size>::Elf_Addr
dceae3c1 1023Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
624f8810 1024 local_section_offset(Addend addend) const
dceae3c1 1025{
624f8810
ILT
1026 gold_assert(this->local_sym_index_ != GSYM_CODE
1027 && this->local_sym_index_ != SECTION_CODE
e291e7b9 1028 && this->local_sym_index_ != TARGET_CODE
624f8810 1029 && this->local_sym_index_ != INVALID_CODE
e291e7b9 1030 && this->local_sym_index_ != 0
624f8810 1031 && this->is_section_symbol_);
dceae3c1 1032 const unsigned int lsi = this->local_sym_index_;
ef9beddf 1033 Output_section* os = this->u1_.relobj->output_section(lsi);
624f8810 1034 gold_assert(os != NULL);
ef9beddf 1035 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
eff45813 1036 if (offset != invalid_address)
624f8810
ILT
1037 return offset + addend;
1038 // This is a merge section.
6fa2a40b
CC
1039 Sized_relobj_file<size, big_endian>* relobj =
1040 this->u1_.relobj->sized_relobj();
1041 gold_assert(relobj != NULL);
1042 offset = os->output_address(relobj, lsi, addend);
eff45813 1043 gold_assert(offset != invalid_address);
dceae3c1
ILT
1044 return offset;
1045}
1046
d98bc257 1047// Get the output address of a relocation.
c06b7b0b
ILT
1048
1049template<bool dynamic, int size, bool big_endian>
a984ee1d 1050typename elfcpp::Elf_types<size>::Elf_Addr
d98bc257 1051Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
c06b7b0b 1052{
a3ad94ed 1053 Address address = this->address_;
5a6f7e2d
ILT
1054 if (this->shndx_ != INVALID_CODE)
1055 {
ef9beddf 1056 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
5a6f7e2d 1057 gold_assert(os != NULL);
ef9beddf 1058 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
eff45813 1059 if (off != invalid_address)
730cdc88
ILT
1060 address += os->address() + off;
1061 else
1062 {
6fa2a40b
CC
1063 Sized_relobj_file<size, big_endian>* relobj =
1064 this->u2_.relobj->sized_relobj();
1065 gold_assert(relobj != NULL);
1066 address = os->output_address(relobj, this->shndx_, address);
eff45813 1067 gold_assert(address != invalid_address);
730cdc88 1068 }
5a6f7e2d
ILT
1069 }
1070 else if (this->u2_.od != NULL)
1071 address += this->u2_.od->address();
d98bc257
ILT
1072 return address;
1073}
1074
1075// Write out the offset and info fields of a Rel or Rela relocation
1076// entry.
1077
1078template<bool dynamic, int size, bool big_endian>
1079template<typename Write_rel>
1080void
1081Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1082 Write_rel* wr) const
1083{
1084 wr->put_r_offset(this->get_address());
0da6fa6c 1085 unsigned int sym_index = this->get_symbol_index();
e8c846c3 1086 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
c06b7b0b
ILT
1087}
1088
1089// Write out a Rel relocation.
1090
1091template<bool dynamic, int size, bool big_endian>
1092void
1093Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1094 unsigned char* pov) const
1095{
1096 elfcpp::Rel_write<size, big_endian> orel(pov);
1097 this->write_rel(&orel);
1098}
1099
e8c846c3
ILT
1100// Get the value of the symbol referred to by a Rel relocation.
1101
1102template<bool dynamic, int size, bool big_endian>
1103typename elfcpp::Elf_types<size>::Elf_Addr
d1f003c6 1104Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
624f8810 1105 Addend addend) const
e8c846c3
ILT
1106{
1107 if (this->local_sym_index_ == GSYM_CODE)
1108 {
1109 const Sized_symbol<size>* sym;
1110 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
d1f003c6 1111 return sym->value() + addend;
e8c846c3
ILT
1112 }
1113 gold_assert(this->local_sym_index_ != SECTION_CODE
e291e7b9 1114 && this->local_sym_index_ != TARGET_CODE
d1f003c6 1115 && this->local_sym_index_ != INVALID_CODE
e291e7b9 1116 && this->local_sym_index_ != 0
d1f003c6
ILT
1117 && !this->is_section_symbol_);
1118 const unsigned int lsi = this->local_sym_index_;
6fa2a40b
CC
1119 Sized_relobj_file<size, big_endian>* relobj =
1120 this->u1_.relobj->sized_relobj();
1121 gold_assert(relobj != NULL);
1122 const Symbol_value<size>* symval = relobj->local_symbol(lsi);
1123 return symval->value(relobj, addend);
e8c846c3
ILT
1124}
1125
d98bc257
ILT
1126// Reloc comparison. This function sorts the dynamic relocs for the
1127// benefit of the dynamic linker. First we sort all relative relocs
1128// to the front. Among relative relocs, we sort by output address.
1129// Among non-relative relocs, we sort by symbol index, then by output
1130// address.
1131
1132template<bool dynamic, int size, bool big_endian>
1133int
1134Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1135 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1136 const
1137{
1138 if (this->is_relative_)
1139 {
1140 if (!r2.is_relative_)
1141 return -1;
1142 // Otherwise sort by reloc address below.
1143 }
1144 else if (r2.is_relative_)
1145 return 1;
1146 else
1147 {
1148 unsigned int sym1 = this->get_symbol_index();
1149 unsigned int sym2 = r2.get_symbol_index();
1150 if (sym1 < sym2)
1151 return -1;
1152 else if (sym1 > sym2)
1153 return 1;
1154 // Otherwise sort by reloc address.
1155 }
1156
1157 section_offset_type addr1 = this->get_address();
1158 section_offset_type addr2 = r2.get_address();
1159 if (addr1 < addr2)
1160 return -1;
1161 else if (addr1 > addr2)
1162 return 1;
1163
1164 // Final tie breaker, in order to generate the same output on any
1165 // host: reloc type.
1166 unsigned int type1 = this->type_;
1167 unsigned int type2 = r2.type_;
1168 if (type1 < type2)
1169 return -1;
1170 else if (type1 > type2)
1171 return 1;
1172
1173 // These relocs appear to be exactly the same.
1174 return 0;
1175}
1176
c06b7b0b
ILT
1177// Write out a Rela relocation.
1178
1179template<bool dynamic, int size, bool big_endian>
1180void
1181Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1182 unsigned char* pov) const
1183{
1184 elfcpp::Rela_write<size, big_endian> orel(pov);
1185 this->rel_.write_rel(&orel);
e8c846c3 1186 Addend addend = this->addend_;
e291e7b9
ILT
1187 if (this->rel_.is_target_specific())
1188 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1189 this->rel_.type(), addend);
0da6fa6c 1190 else if (this->rel_.is_symbolless())
d1f003c6
ILT
1191 addend = this->rel_.symbol_value(addend);
1192 else if (this->rel_.is_local_section_symbol())
624f8810 1193 addend = this->rel_.local_section_offset(addend);
e8c846c3 1194 orel.put_r_addend(addend);
c06b7b0b
ILT
1195}
1196
1197// Output_data_reloc_base methods.
1198
16649710
ILT
1199// Adjust the output section.
1200
1201template<int sh_type, bool dynamic, int size, bool big_endian>
1202void
1203Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1204 ::do_adjust_output_section(Output_section* os)
1205{
1206 if (sh_type == elfcpp::SHT_REL)
1207 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1208 else if (sh_type == elfcpp::SHT_RELA)
1209 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1210 else
1211 gold_unreachable();
7223e9ca
ILT
1212
1213 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1214 // static link. The backends will generate a dynamic reloc section
1215 // to hold this. In that case we don't want to link to the dynsym
1216 // section, because there isn't one.
1217 if (!dynamic)
16649710 1218 os->set_should_link_to_symtab();
7223e9ca
ILT
1219 else if (parameters->doing_static_link())
1220 ;
1221 else
1222 os->set_should_link_to_dynsym();
16649710
ILT
1223}
1224
c06b7b0b
ILT
1225// Write out relocation data.
1226
1227template<int sh_type, bool dynamic, int size, bool big_endian>
1228void
1229Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1230 Output_file* of)
1231{
1232 const off_t off = this->offset();
1233 const off_t oview_size = this->data_size();
1234 unsigned char* const oview = of->get_output_view(off, oview_size);
1235
3a44184e 1236 if (this->sort_relocs())
d98bc257
ILT
1237 {
1238 gold_assert(dynamic);
1239 std::sort(this->relocs_.begin(), this->relocs_.end(),
1240 Sort_relocs_comparison());
1241 }
1242
c06b7b0b
ILT
1243 unsigned char* pov = oview;
1244 for (typename Relocs::const_iterator p = this->relocs_.begin();
1245 p != this->relocs_.end();
1246 ++p)
1247 {
1248 p->write(pov);
1249 pov += reloc_size;
1250 }
1251
a3ad94ed 1252 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
1253
1254 of->write_output_view(off, oview_size, oview);
1255
1256 // We no longer need the relocation entries.
1257 this->relocs_.clear();
1258}
1259
6a74a719
ILT
1260// Class Output_relocatable_relocs.
1261
1262template<int sh_type, int size, bool big_endian>
1263void
1264Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1265{
1266 this->set_data_size(this->rr_->output_reloc_count()
1267 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1268}
1269
1270// class Output_data_group.
1271
1272template<int size, bool big_endian>
1273Output_data_group<size, big_endian>::Output_data_group(
6fa2a40b 1274 Sized_relobj_file<size, big_endian>* relobj,
6a74a719 1275 section_size_type entry_count,
8825ac63
ILT
1276 elfcpp::Elf_Word flags,
1277 std::vector<unsigned int>* input_shndxes)
20e6d0d6 1278 : Output_section_data(entry_count * 4, 4, false),
8825ac63
ILT
1279 relobj_(relobj),
1280 flags_(flags)
6a74a719 1281{
8825ac63 1282 this->input_shndxes_.swap(*input_shndxes);
6a74a719
ILT
1283}
1284
1285// Write out the section group, which means translating the section
1286// indexes to apply to the output file.
1287
1288template<int size, bool big_endian>
1289void
1290Output_data_group<size, big_endian>::do_write(Output_file* of)
1291{
1292 const off_t off = this->offset();
1293 const section_size_type oview_size =
1294 convert_to_section_size_type(this->data_size());
1295 unsigned char* const oview = of->get_output_view(off, oview_size);
1296
1297 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1298 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1299 ++contents;
1300
1301 for (std::vector<unsigned int>::const_iterator p =
8825ac63
ILT
1302 this->input_shndxes_.begin();
1303 p != this->input_shndxes_.end();
6a74a719
ILT
1304 ++p, ++contents)
1305 {
ef9beddf 1306 Output_section* os = this->relobj_->output_section(*p);
6a74a719
ILT
1307
1308 unsigned int output_shndx;
1309 if (os != NULL)
1310 output_shndx = os->out_shndx();
1311 else
1312 {
1313 this->relobj_->error(_("section group retained but "
1314 "group element discarded"));
1315 output_shndx = 0;
1316 }
1317
1318 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1319 }
1320
1321 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1322 gold_assert(wrote == oview_size);
1323
1324 of->write_output_view(off, oview_size, oview);
1325
1326 // We no longer need this information.
8825ac63 1327 this->input_shndxes_.clear();
6a74a719
ILT
1328}
1329
dbe717ef 1330// Output_data_got::Got_entry methods.
ead1e424
ILT
1331
1332// Write out the entry.
1333
1334template<int size, bool big_endian>
1335void
7e1edb90 1336Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
1337{
1338 Valtype val = 0;
1339
1340 switch (this->local_sym_index_)
1341 {
1342 case GSYM_CODE:
1343 {
e8c846c3
ILT
1344 // If the symbol is resolved locally, we need to write out the
1345 // link-time value, which will be relocated dynamically by a
1346 // RELATIVE relocation.
ead1e424 1347 Symbol* gsym = this->u_.gsym;
7223e9ca
ILT
1348 if (this->use_plt_offset_ && gsym->has_plt_offset())
1349 val = (parameters->target().plt_section_for_global(gsym)->address()
1350 + gsym->plt_offset());
1351 else
1352 {
1353 Sized_symbol<size>* sgsym;
1354 // This cast is a bit ugly. We don't want to put a
1355 // virtual method in Symbol, because we want Symbol to be
1356 // as small as possible.
1357 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1358 val = sgsym->value();
1359 }
ead1e424
ILT
1360 }
1361 break;
1362
1363 case CONSTANT_CODE:
1364 val = this->u_.constant;
1365 break;
1366
4829d394
CC
1367 case RESERVED_CODE:
1368 // If we're doing an incremental update, don't touch this GOT entry.
1369 if (parameters->incremental_update())
1370 return;
1371 val = this->u_.constant;
1372 break;
1373
ead1e424 1374 default:
d1f003c6 1375 {
6fa2a40b 1376 const Sized_relobj_file<size, big_endian>* object = this->u_.object;
d1f003c6 1377 const unsigned int lsi = this->local_sym_index_;
7223e9ca
ILT
1378 const Symbol_value<size>* symval = object->local_symbol(lsi);
1379 if (!this->use_plt_offset_)
1380 val = symval->value(this->u_.object, 0);
1381 else
1382 {
1383 const Output_data* plt =
1384 parameters->target().plt_section_for_local(object, lsi);
1385 val = plt->address() + object->local_plt_offset(lsi);
1386 }
d1f003c6 1387 }
e727fa71 1388 break;
ead1e424
ILT
1389 }
1390
a3ad94ed 1391 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
1392}
1393
dbe717ef 1394// Output_data_got methods.
ead1e424 1395
dbe717ef
ILT
1396// Add an entry for a global symbol to the GOT. This returns true if
1397// this is a new GOT entry, false if the symbol already had a GOT
1398// entry.
1399
1400template<int size, bool big_endian>
1401bool
0a65a3a7
CC
1402Output_data_got<size, big_endian>::add_global(
1403 Symbol* gsym,
1404 unsigned int got_type)
ead1e424 1405{
0a65a3a7 1406 if (gsym->has_got_offset(got_type))
dbe717ef 1407 return false;
ead1e424 1408
4829d394
CC
1409 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1410 gsym->set_got_offset(got_type, got_offset);
7223e9ca
ILT
1411 return true;
1412}
1413
1414// Like add_global, but use the PLT offset.
1415
1416template<int size, bool big_endian>
1417bool
1418Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1419 unsigned int got_type)
1420{
1421 if (gsym->has_got_offset(got_type))
1422 return false;
1423
4829d394
CC
1424 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1425 gsym->set_got_offset(got_type, got_offset);
dbe717ef
ILT
1426 return true;
1427}
ead1e424 1428
7bf1f802
ILT
1429// Add an entry for a global symbol to the GOT, and add a dynamic
1430// relocation of type R_TYPE for the GOT entry.
7223e9ca 1431
7bf1f802
ILT
1432template<int size, bool big_endian>
1433void
1434Output_data_got<size, big_endian>::add_global_with_rel(
1435 Symbol* gsym,
0a65a3a7 1436 unsigned int got_type,
7bf1f802
ILT
1437 Rel_dyn* rel_dyn,
1438 unsigned int r_type)
1439{
0a65a3a7 1440 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1441 return;
1442
4829d394 1443 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941
ILT
1444 gsym->set_got_offset(got_type, got_offset);
1445 rel_dyn->add_global(gsym, r_type, this, got_offset);
7bf1f802
ILT
1446}
1447
1448template<int size, bool big_endian>
1449void
1450Output_data_got<size, big_endian>::add_global_with_rela(
1451 Symbol* gsym,
0a65a3a7 1452 unsigned int got_type,
7bf1f802
ILT
1453 Rela_dyn* rela_dyn,
1454 unsigned int r_type)
1455{
0a65a3a7 1456 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1457 return;
1458
4829d394 1459 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941
ILT
1460 gsym->set_got_offset(got_type, got_offset);
1461 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
7bf1f802
ILT
1462}
1463
0a65a3a7
CC
1464// Add a pair of entries for a global symbol to the GOT, and add
1465// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1466// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1467template<int size, bool big_endian>
1468void
0a65a3a7
CC
1469Output_data_got<size, big_endian>::add_global_pair_with_rel(
1470 Symbol* gsym,
1471 unsigned int got_type,
7bf1f802 1472 Rel_dyn* rel_dyn,
0a65a3a7
CC
1473 unsigned int r_type_1,
1474 unsigned int r_type_2)
7bf1f802 1475{
0a65a3a7 1476 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1477 return;
1478
4829d394 1479 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
2ea97941
ILT
1480 gsym->set_got_offset(got_type, got_offset);
1481 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
0a65a3a7 1482
0a65a3a7 1483 if (r_type_2 != 0)
4829d394 1484 rel_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8);
7bf1f802
ILT
1485}
1486
1487template<int size, bool big_endian>
1488void
0a65a3a7
CC
1489Output_data_got<size, big_endian>::add_global_pair_with_rela(
1490 Symbol* gsym,
1491 unsigned int got_type,
7bf1f802 1492 Rela_dyn* rela_dyn,
0a65a3a7
CC
1493 unsigned int r_type_1,
1494 unsigned int r_type_2)
7bf1f802 1495{
0a65a3a7 1496 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1497 return;
1498
4829d394 1499 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
2ea97941
ILT
1500 gsym->set_got_offset(got_type, got_offset);
1501 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
0a65a3a7 1502
0a65a3a7 1503 if (r_type_2 != 0)
4829d394 1504 rela_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8, 0);
7bf1f802
ILT
1505}
1506
0a65a3a7
CC
1507// Add an entry for a local symbol to the GOT. This returns true if
1508// this is a new GOT entry, false if the symbol already has a GOT
1509// entry.
07f397ab
ILT
1510
1511template<int size, bool big_endian>
1512bool
0a65a3a7 1513Output_data_got<size, big_endian>::add_local(
6fa2a40b 1514 Sized_relobj_file<size, big_endian>* object,
0a65a3a7
CC
1515 unsigned int symndx,
1516 unsigned int got_type)
07f397ab 1517{
0a65a3a7 1518 if (object->local_has_got_offset(symndx, got_type))
07f397ab
ILT
1519 return false;
1520
4829d394
CC
1521 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1522 false));
1523 object->set_local_got_offset(symndx, got_type, got_offset);
7223e9ca
ILT
1524 return true;
1525}
1526
1527// Like add_local, but use the PLT offset.
1528
1529template<int size, bool big_endian>
1530bool
1531Output_data_got<size, big_endian>::add_local_plt(
6fa2a40b 1532 Sized_relobj_file<size, big_endian>* object,
7223e9ca
ILT
1533 unsigned int symndx,
1534 unsigned int got_type)
1535{
1536 if (object->local_has_got_offset(symndx, got_type))
1537 return false;
1538
4829d394
CC
1539 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1540 true));
1541 object->set_local_got_offset(symndx, got_type, got_offset);
07f397ab
ILT
1542 return true;
1543}
1544
0a65a3a7
CC
1545// Add an entry for a local symbol to the GOT, and add a dynamic
1546// relocation of type R_TYPE for the GOT entry.
7223e9ca 1547
7bf1f802
ILT
1548template<int size, bool big_endian>
1549void
0a65a3a7 1550Output_data_got<size, big_endian>::add_local_with_rel(
6fa2a40b 1551 Sized_relobj_file<size, big_endian>* object,
0a65a3a7
CC
1552 unsigned int symndx,
1553 unsigned int got_type,
7bf1f802
ILT
1554 Rel_dyn* rel_dyn,
1555 unsigned int r_type)
1556{
0a65a3a7 1557 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1558 return;
1559
4829d394 1560 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941
ILT
1561 object->set_local_got_offset(symndx, got_type, got_offset);
1562 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
7bf1f802
ILT
1563}
1564
1565template<int size, bool big_endian>
1566void
0a65a3a7 1567Output_data_got<size, big_endian>::add_local_with_rela(
6fa2a40b 1568 Sized_relobj_file<size, big_endian>* object,
0a65a3a7
CC
1569 unsigned int symndx,
1570 unsigned int got_type,
7bf1f802
ILT
1571 Rela_dyn* rela_dyn,
1572 unsigned int r_type)
1573{
0a65a3a7 1574 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1575 return;
1576
4829d394 1577 unsigned int got_offset = this->add_got_entry(Got_entry());
2ea97941
ILT
1578 object->set_local_got_offset(symndx, got_type, got_offset);
1579 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
07f397ab
ILT
1580}
1581
0a65a3a7
CC
1582// Add a pair of entries for a local symbol to the GOT, and add
1583// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1584// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1585template<int size, bool big_endian>
1586void
0a65a3a7 1587Output_data_got<size, big_endian>::add_local_pair_with_rel(
6fa2a40b 1588 Sized_relobj_file<size, big_endian>* object,
7bf1f802
ILT
1589 unsigned int symndx,
1590 unsigned int shndx,
0a65a3a7 1591 unsigned int got_type,
7bf1f802 1592 Rel_dyn* rel_dyn,
0a65a3a7
CC
1593 unsigned int r_type_1,
1594 unsigned int r_type_2)
7bf1f802 1595{
0a65a3a7 1596 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1597 return;
1598
4829d394
CC
1599 unsigned int got_offset =
1600 this->add_got_entry_pair(Got_entry(),
1601 Got_entry(object, symndx, false));
2ea97941 1602 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1603 Output_section* os = object->output_section(shndx);
2ea97941 1604 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
7bf1f802 1605
0a65a3a7 1606 if (r_type_2 != 0)
4829d394 1607 rel_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8);
7bf1f802
ILT
1608}
1609
1610template<int size, bool big_endian>
1611void
0a65a3a7 1612Output_data_got<size, big_endian>::add_local_pair_with_rela(
6fa2a40b 1613 Sized_relobj_file<size, big_endian>* object,
7bf1f802
ILT
1614 unsigned int symndx,
1615 unsigned int shndx,
0a65a3a7 1616 unsigned int got_type,
7bf1f802 1617 Rela_dyn* rela_dyn,
0a65a3a7
CC
1618 unsigned int r_type_1,
1619 unsigned int r_type_2)
7bf1f802 1620{
0a65a3a7 1621 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1622 return;
1623
4829d394
CC
1624 unsigned int got_offset =
1625 this->add_got_entry_pair(Got_entry(),
1626 Got_entry(object, symndx, false));
2ea97941 1627 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1628 Output_section* os = object->output_section(shndx);
2ea97941 1629 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
7bf1f802 1630
0a65a3a7 1631 if (r_type_2 != 0)
4829d394
CC
1632 rela_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8, 0);
1633}
1634
1635// Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1636
1637template<int size, bool big_endian>
1638void
6fa2a40b
CC
1639Output_data_got<size, big_endian>::reserve_local(
1640 unsigned int i,
1641 Sized_relobj<size, big_endian>* object,
1642 unsigned int sym_index,
1643 unsigned int got_type)
4829d394 1644{
6fa2a40b
CC
1645 this->reserve_slot(i);
1646 object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
4829d394 1647}
7bf1f802 1648
4829d394
CC
1649// Reserve a slot in the GOT for a global symbol.
1650
1651template<int size, bool big_endian>
1652void
6fa2a40b 1653Output_data_got<size, big_endian>::reserve_global(
4829d394
CC
1654 unsigned int i,
1655 Symbol* gsym,
1656 unsigned int got_type)
1657{
6fa2a40b 1658 this->reserve_slot(i);
4829d394 1659 gsym->set_got_offset(got_type, this->got_offset(i));
7bf1f802
ILT
1660}
1661
ead1e424
ILT
1662// Write out the GOT.
1663
1664template<int size, bool big_endian>
1665void
dbe717ef 1666Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1667{
1668 const int add = size / 8;
1669
1670 const off_t off = this->offset();
c06b7b0b 1671 const off_t oview_size = this->data_size();
ead1e424
ILT
1672 unsigned char* const oview = of->get_output_view(off, oview_size);
1673
1674 unsigned char* pov = oview;
1675 for (typename Got_entries::const_iterator p = this->entries_.begin();
1676 p != this->entries_.end();
1677 ++p)
1678 {
7e1edb90 1679 p->write(pov);
ead1e424
ILT
1680 pov += add;
1681 }
1682
a3ad94ed 1683 gold_assert(pov - oview == oview_size);
c06b7b0b 1684
ead1e424
ILT
1685 of->write_output_view(off, oview_size, oview);
1686
1687 // We no longer need the GOT entries.
1688 this->entries_.clear();
1689}
1690
4829d394
CC
1691// Create a new GOT entry and return its offset.
1692
1693template<int size, bool big_endian>
1694unsigned int
1695Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1696{
1697 if (!this->is_data_size_valid())
1698 {
1699 this->entries_.push_back(got_entry);
1700 this->set_got_size();
1701 return this->last_got_offset();
1702 }
1703 else
1704 {
1705 // For an incremental update, find an available slot.
1706 off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1707 if (got_offset == -1)
e6455dfb
CC
1708 gold_fallback(_("out of patch space (GOT);"
1709 " relink with --incremental-full"));
4829d394
CC
1710 unsigned int got_index = got_offset / (size / 8);
1711 gold_assert(got_index < this->entries_.size());
1712 this->entries_[got_index] = got_entry;
1713 return static_cast<unsigned int>(got_offset);
1714 }
1715}
1716
1717// Create a pair of new GOT entries and return the offset of the first.
1718
1719template<int size, bool big_endian>
1720unsigned int
1721Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1722 Got_entry got_entry_2)
1723{
1724 if (!this->is_data_size_valid())
1725 {
1726 unsigned int got_offset;
1727 this->entries_.push_back(got_entry_1);
1728 got_offset = this->last_got_offset();
1729 this->entries_.push_back(got_entry_2);
1730 this->set_got_size();
1731 return got_offset;
1732 }
1733 else
1734 {
1735 // For an incremental update, find an available pair of slots.
1736 off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1737 if (got_offset == -1)
e6455dfb
CC
1738 gold_fallback(_("out of patch space (GOT);"
1739 " relink with --incremental-full"));
4829d394
CC
1740 unsigned int got_index = got_offset / (size / 8);
1741 gold_assert(got_index < this->entries_.size());
1742 this->entries_[got_index] = got_entry_1;
1743 this->entries_[got_index + 1] = got_entry_2;
1744 return static_cast<unsigned int>(got_offset);
1745 }
1746}
1747
a3ad94ed
ILT
1748// Output_data_dynamic::Dynamic_entry methods.
1749
1750// Write out the entry.
1751
1752template<int size, bool big_endian>
1753void
1754Output_data_dynamic::Dynamic_entry::write(
1755 unsigned char* pov,
7d1a9ebb 1756 const Stringpool* pool) const
a3ad94ed
ILT
1757{
1758 typename elfcpp::Elf_types<size>::Elf_WXword val;
c2b45e22 1759 switch (this->offset_)
a3ad94ed
ILT
1760 {
1761 case DYNAMIC_NUMBER:
1762 val = this->u_.val;
1763 break;
1764
a3ad94ed 1765 case DYNAMIC_SECTION_SIZE:
16649710 1766 val = this->u_.od->data_size();
612a8d3d
DM
1767 if (this->od2 != NULL)
1768 val += this->od2->data_size();
a3ad94ed
ILT
1769 break;
1770
1771 case DYNAMIC_SYMBOL:
1772 {
16649710
ILT
1773 const Sized_symbol<size>* s =
1774 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1775 val = s->value();
1776 }
1777 break;
1778
1779 case DYNAMIC_STRING:
1780 val = pool->get_offset(this->u_.str);
1781 break;
1782
1783 default:
c2b45e22
CC
1784 val = this->u_.od->address() + this->offset_;
1785 break;
a3ad94ed
ILT
1786 }
1787
1788 elfcpp::Dyn_write<size, big_endian> dw(pov);
1789 dw.put_d_tag(this->tag_);
1790 dw.put_d_val(val);
1791}
1792
1793// Output_data_dynamic methods.
1794
16649710
ILT
1795// Adjust the output section to set the entry size.
1796
1797void
1798Output_data_dynamic::do_adjust_output_section(Output_section* os)
1799{
8851ecca 1800 if (parameters->target().get_size() == 32)
16649710 1801 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1802 else if (parameters->target().get_size() == 64)
16649710
ILT
1803 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1804 else
1805 gold_unreachable();
1806}
1807
a3ad94ed
ILT
1808// Set the final data size.
1809
1810void
27bc2bce 1811Output_data_dynamic::set_final_data_size()
a3ad94ed 1812{
20e6d0d6
DK
1813 // Add the terminating entry if it hasn't been added.
1814 // Because of relaxation, we can run this multiple times.
9e9e071b
ILT
1815 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1816 {
1817 int extra = parameters->options().spare_dynamic_tags();
1818 for (int i = 0; i < extra; ++i)
1819 this->add_constant(elfcpp::DT_NULL, 0);
1820 this->add_constant(elfcpp::DT_NULL, 0);
1821 }
a3ad94ed
ILT
1822
1823 int dyn_size;
8851ecca 1824 if (parameters->target().get_size() == 32)
a3ad94ed 1825 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1826 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1827 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1828 else
1829 gold_unreachable();
1830 this->set_data_size(this->entries_.size() * dyn_size);
1831}
1832
1833// Write out the dynamic entries.
1834
1835void
1836Output_data_dynamic::do_write(Output_file* of)
1837{
8851ecca 1838 switch (parameters->size_and_endianness())
a3ad94ed 1839 {
9025d29d 1840#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1841 case Parameters::TARGET_32_LITTLE:
1842 this->sized_write<32, false>(of);
1843 break;
9025d29d 1844#endif
8851ecca
ILT
1845#ifdef HAVE_TARGET_32_BIG
1846 case Parameters::TARGET_32_BIG:
1847 this->sized_write<32, true>(of);
1848 break;
9025d29d 1849#endif
9025d29d 1850#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1851 case Parameters::TARGET_64_LITTLE:
1852 this->sized_write<64, false>(of);
1853 break;
9025d29d 1854#endif
8851ecca
ILT
1855#ifdef HAVE_TARGET_64_BIG
1856 case Parameters::TARGET_64_BIG:
1857 this->sized_write<64, true>(of);
1858 break;
1859#endif
1860 default:
1861 gold_unreachable();
a3ad94ed 1862 }
a3ad94ed
ILT
1863}
1864
1865template<int size, bool big_endian>
1866void
1867Output_data_dynamic::sized_write(Output_file* of)
1868{
1869 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1870
2ea97941 1871 const off_t offset = this->offset();
a3ad94ed 1872 const off_t oview_size = this->data_size();
2ea97941 1873 unsigned char* const oview = of->get_output_view(offset, oview_size);
a3ad94ed
ILT
1874
1875 unsigned char* pov = oview;
1876 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1877 p != this->entries_.end();
1878 ++p)
1879 {
7d1a9ebb 1880 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1881 pov += dyn_size;
1882 }
1883
1884 gold_assert(pov - oview == oview_size);
1885
2ea97941 1886 of->write_output_view(offset, oview_size, oview);
a3ad94ed
ILT
1887
1888 // We no longer need the dynamic entries.
1889 this->entries_.clear();
1890}
1891
d491d34e
ILT
1892// Class Output_symtab_xindex.
1893
1894void
1895Output_symtab_xindex::do_write(Output_file* of)
1896{
2ea97941 1897 const off_t offset = this->offset();
d491d34e 1898 const off_t oview_size = this->data_size();
2ea97941 1899 unsigned char* const oview = of->get_output_view(offset, oview_size);
d491d34e
ILT
1900
1901 memset(oview, 0, oview_size);
1902
1903 if (parameters->target().is_big_endian())
1904 this->endian_do_write<true>(oview);
1905 else
1906 this->endian_do_write<false>(oview);
1907
2ea97941 1908 of->write_output_view(offset, oview_size, oview);
d491d34e
ILT
1909
1910 // We no longer need the data.
1911 this->entries_.clear();
1912}
1913
1914template<bool big_endian>
1915void
1916Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1917{
1918 for (Xindex_entries::const_iterator p = this->entries_.begin();
1919 p != this->entries_.end();
1920 ++p)
20e6d0d6
DK
1921 {
1922 unsigned int symndx = p->first;
1923 gold_assert(symndx * 4 < this->data_size());
1924 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1925 }
d491d34e
ILT
1926}
1927
ead1e424
ILT
1928// Output_section::Input_section methods.
1929
cdc29364
CC
1930// Return the current data size. For an input section we store the size here.
1931// For an Output_section_data, we have to ask it for the size.
1932
1933off_t
1934Output_section::Input_section::current_data_size() const
1935{
1936 if (this->is_input_section())
1937 return this->u1_.data_size;
1938 else
1939 {
1940 this->u2_.posd->pre_finalize_data_size();
1941 return this->u2_.posd->current_data_size();
1942 }
1943}
1944
ead1e424
ILT
1945// Return the data size. For an input section we store the size here.
1946// For an Output_section_data, we have to ask it for the size.
1947
1948off_t
1949Output_section::Input_section::data_size() const
1950{
1951 if (this->is_input_section())
b8e6aad9 1952 return this->u1_.data_size;
ead1e424 1953 else
b8e6aad9 1954 return this->u2_.posd->data_size();
ead1e424
ILT
1955}
1956
0439c796
DK
1957// Return the object for an input section.
1958
1959Relobj*
1960Output_section::Input_section::relobj() const
1961{
1962 if (this->is_input_section())
1963 return this->u2_.object;
1964 else if (this->is_merge_section())
1965 {
1966 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1967 return this->u2_.pomb->first_relobj();
1968 }
1969 else if (this->is_relaxed_input_section())
1970 return this->u2_.poris->relobj();
1971 else
1972 gold_unreachable();
1973}
1974
1975// Return the input section index for an input section.
1976
1977unsigned int
1978Output_section::Input_section::shndx() const
1979{
1980 if (this->is_input_section())
1981 return this->shndx_;
1982 else if (this->is_merge_section())
1983 {
1984 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1985 return this->u2_.pomb->first_shndx();
1986 }
1987 else if (this->is_relaxed_input_section())
1988 return this->u2_.poris->shndx();
1989 else
1990 gold_unreachable();
1991}
1992
ead1e424
ILT
1993// Set the address and file offset.
1994
1995void
96803768
ILT
1996Output_section::Input_section::set_address_and_file_offset(
1997 uint64_t address,
1998 off_t file_offset,
1999 off_t section_file_offset)
ead1e424
ILT
2000{
2001 if (this->is_input_section())
96803768
ILT
2002 this->u2_.object->set_section_offset(this->shndx_,
2003 file_offset - section_file_offset);
ead1e424 2004 else
96803768
ILT
2005 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2006}
2007
a445fddf
ILT
2008// Reset the address and file offset.
2009
2010void
2011Output_section::Input_section::reset_address_and_file_offset()
2012{
2013 if (!this->is_input_section())
2014 this->u2_.posd->reset_address_and_file_offset();
2015}
2016
96803768
ILT
2017// Finalize the data size.
2018
2019void
2020Output_section::Input_section::finalize_data_size()
2021{
2022 if (!this->is_input_section())
2023 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
2024}
2025
1e983657
ILT
2026// Try to turn an input offset into an output offset. We want to
2027// return the output offset relative to the start of this
2028// Input_section in the output section.
b8e6aad9 2029
8f00aeb8 2030inline bool
8383303e
ILT
2031Output_section::Input_section::output_offset(
2032 const Relobj* object,
2ea97941
ILT
2033 unsigned int shndx,
2034 section_offset_type offset,
ca09d69a 2035 section_offset_type* poutput) const
b8e6aad9
ILT
2036{
2037 if (!this->is_input_section())
2ea97941 2038 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
2039 else
2040 {
2ea97941 2041 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 2042 return false;
2ea97941 2043 *poutput = offset;
b8e6aad9
ILT
2044 return true;
2045 }
ead1e424
ILT
2046}
2047
a9a60db6
ILT
2048// Return whether this is the merge section for the input section
2049// SHNDX in OBJECT.
2050
2051inline bool
2052Output_section::Input_section::is_merge_section_for(const Relobj* object,
2ea97941 2053 unsigned int shndx) const
a9a60db6
ILT
2054{
2055 if (this->is_input_section())
2056 return false;
2ea97941 2057 return this->u2_.posd->is_merge_section_for(object, shndx);
a9a60db6
ILT
2058}
2059
ead1e424
ILT
2060// Write out the data. We don't have to do anything for an input
2061// section--they are handled via Object::relocate--but this is where
2062// we write out the data for an Output_section_data.
2063
2064void
2065Output_section::Input_section::write(Output_file* of)
2066{
2067 if (!this->is_input_section())
b8e6aad9 2068 this->u2_.posd->write(of);
ead1e424
ILT
2069}
2070
96803768
ILT
2071// Write the data to a buffer. As for write(), we don't have to do
2072// anything for an input section.
2073
2074void
2075Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2076{
2077 if (!this->is_input_section())
2078 this->u2_.posd->write_to_buffer(buffer);
2079}
2080
7d9e3d98
ILT
2081// Print to a map file.
2082
2083void
2084Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2085{
2086 switch (this->shndx_)
2087 {
2088 case OUTPUT_SECTION_CODE:
2089 case MERGE_DATA_SECTION_CODE:
2090 case MERGE_STRING_SECTION_CODE:
2091 this->u2_.posd->print_to_mapfile(mapfile);
2092 break;
2093
20e6d0d6
DK
2094 case RELAXED_INPUT_SECTION_CODE:
2095 {
2096 Output_relaxed_input_section* relaxed_section =
2097 this->relaxed_input_section();
2098 mapfile->print_input_section(relaxed_section->relobj(),
2099 relaxed_section->shndx());
2100 }
2101 break;
7d9e3d98
ILT
2102 default:
2103 mapfile->print_input_section(this->u2_.object, this->shndx_);
2104 break;
2105 }
2106}
2107
a2fb1b05
ILT
2108// Output_section methods.
2109
2110// Construct an Output_section. NAME will point into a Stringpool.
2111
2ea97941
ILT
2112Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2113 elfcpp::Elf_Xword flags)
2114 : name_(name),
a2fb1b05
ILT
2115 addralign_(0),
2116 entsize_(0),
a445fddf 2117 load_address_(0),
16649710 2118 link_section_(NULL),
a2fb1b05 2119 link_(0),
16649710 2120 info_section_(NULL),
6a74a719 2121 info_symndx_(NULL),
a2fb1b05 2122 info_(0),
2ea97941
ILT
2123 type_(type),
2124 flags_(flags),
22f0da72 2125 order_(ORDER_INVALID),
91ea499d 2126 out_shndx_(-1U),
c06b7b0b
ILT
2127 symtab_index_(0),
2128 dynsym_index_(0),
ead1e424
ILT
2129 input_sections_(),
2130 first_input_offset_(0),
c51e6221 2131 fills_(),
96803768 2132 postprocessing_buffer_(NULL),
a3ad94ed 2133 needs_symtab_index_(false),
16649710
ILT
2134 needs_dynsym_index_(false),
2135 should_link_to_symtab_(false),
730cdc88 2136 should_link_to_dynsym_(false),
27bc2bce 2137 after_input_sections_(false),
7bf1f802 2138 requires_postprocessing_(false),
a445fddf
ILT
2139 found_in_sections_clause_(false),
2140 has_load_address_(false),
755ab8af 2141 info_uses_section_index_(false),
6e9ba2ca 2142 input_section_order_specified_(false),
2fd32231
ILT
2143 may_sort_attached_input_sections_(false),
2144 must_sort_attached_input_sections_(false),
2145 attached_input_sections_are_sorted_(false),
9f1d377b 2146 is_relro_(false),
8a5e3e08
ILT
2147 is_small_section_(false),
2148 is_large_section_(false),
f5c870d2 2149 generate_code_fills_at_write_(false),
e8cd95c7 2150 is_entsize_zero_(false),
8923b24c 2151 section_offsets_need_adjustment_(false),
1e5d2fb1 2152 is_noload_(false),
131687b4 2153 always_keeps_input_sections_(false),
cdc29364 2154 has_fixed_layout_(false),
20e6d0d6 2155 tls_offset_(0),
c0a62865 2156 checkpoint_(NULL),
cdc29364
CC
2157 lookup_maps_(new Output_section_lookup_maps),
2158 free_list_()
a2fb1b05 2159{
27bc2bce
ILT
2160 // An unallocated section has no address. Forcing this means that
2161 // we don't need special treatment for symbols defined in debug
2162 // sections.
2ea97941 2163 if ((flags & elfcpp::SHF_ALLOC) == 0)
27bc2bce 2164 this->set_address(0);
a2fb1b05
ILT
2165}
2166
54dc6425
ILT
2167Output_section::~Output_section()
2168{
20e6d0d6 2169 delete this->checkpoint_;
54dc6425
ILT
2170}
2171
16649710
ILT
2172// Set the entry size.
2173
2174void
2175Output_section::set_entsize(uint64_t v)
2176{
e8cd95c7
ILT
2177 if (this->is_entsize_zero_)
2178 ;
2179 else if (this->entsize_ == 0)
16649710 2180 this->entsize_ = v;
e8cd95c7
ILT
2181 else if (this->entsize_ != v)
2182 {
2183 this->entsize_ = 0;
2184 this->is_entsize_zero_ = 1;
2185 }
16649710
ILT
2186}
2187
ead1e424 2188// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
2189// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2190// relocation section which applies to this section, or 0 if none, or
2191// -1U if more than one. Return the offset of the input section
2192// within the output section. Return -1 if the input section will
2193// receive special handling. In the normal case we don't always keep
2194// track of input sections for an Output_section. Instead, each
2195// Object keeps track of the Output_section for each of its input
a445fddf
ILT
2196// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2197// track of input sections here; this is used when SECTIONS appears in
2198// a linker script.
a2fb1b05
ILT
2199
2200template<int size, bool big_endian>
2201off_t
6e9ba2ca 2202Output_section::add_input_section(Layout* layout,
6fa2a40b 2203 Sized_relobj_file<size, big_endian>* object,
2ea97941 2204 unsigned int shndx,
ead1e424 2205 const char* secname,
730cdc88 2206 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
2207 unsigned int reloc_shndx,
2208 bool have_sections_script)
a2fb1b05 2209{
2ea97941
ILT
2210 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2211 if ((addralign & (addralign - 1)) != 0)
a2fb1b05 2212 {
75f2446e 2213 object->error(_("invalid alignment %lu for section \"%s\""),
2ea97941
ILT
2214 static_cast<unsigned long>(addralign), secname);
2215 addralign = 1;
a2fb1b05 2216 }
a2fb1b05 2217
2ea97941
ILT
2218 if (addralign > this->addralign_)
2219 this->addralign_ = addralign;
a2fb1b05 2220
44a43cf9 2221 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2ea97941 2222 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
2223
2224 // .debug_str is a mergeable string section, but is not always so
2225 // marked by compilers. Mark manually here so we can optimize.
2226 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
2227 {
2228 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2ea97941 2229 entsize = 1;
4f833eee 2230 }
44a43cf9 2231
e8cd95c7
ILT
2232 this->update_flags_for_input_section(sh_flags);
2233 this->set_entsize(entsize);
2234
b8e6aad9 2235 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88 2236 // a Output_data_merge. We don't try to handle relocations for such
e0b64032
ILT
2237 // a section. We don't try to handle empty merge sections--they
2238 // mess up the mappings, and are useless anyhow.
cdc29364 2239 // FIXME: Need to handle merge sections during incremental update.
44a43cf9 2240 if ((sh_flags & elfcpp::SHF_MERGE) != 0
e0b64032 2241 && reloc_shndx == 0
cdc29364
CC
2242 && shdr.get_sh_size() > 0
2243 && !parameters->incremental())
b8e6aad9 2244 {
0439c796
DK
2245 // Keep information about merged input sections for rebuilding fast
2246 // lookup maps if we have sections-script or we do relaxation.
131687b4
DK
2247 bool keeps_input_sections = (this->always_keeps_input_sections_
2248 || have_sections_script
2249 || parameters->target().may_relax());
2250
0439c796
DK
2251 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2252 addralign, keeps_input_sections))
b8e6aad9
ILT
2253 {
2254 // Tell the relocation routines that they need to call the
730cdc88 2255 // output_offset method to determine the final address.
b8e6aad9
ILT
2256 return -1;
2257 }
2258 }
2259
cdc29364
CC
2260 section_size_type input_section_size = shdr.get_sh_size();
2261 section_size_type uncompressed_size;
2262 if (object->section_is_compressed(shndx, &uncompressed_size))
2263 input_section_size = uncompressed_size;
2264
2265 off_t offset_in_section;
2266 off_t aligned_offset_in_section;
2267 if (this->has_fixed_layout())
2268 {
2269 // For incremental updates, find a chunk of unused space in the section.
2270 offset_in_section = this->free_list_.allocate(input_section_size,
2271 addralign, 0);
2272 if (offset_in_section == -1)
e6455dfb 2273 gold_fallback(_("out of patch space; relink with --incremental-full"));
cdc29364
CC
2274 aligned_offset_in_section = offset_in_section;
2275 }
2276 else
2277 {
2278 offset_in_section = this->current_data_size_for_child();
2279 aligned_offset_in_section = align_address(offset_in_section,
2280 addralign);
2281 this->set_current_data_size_for_child(aligned_offset_in_section
2282 + input_section_size);
2283 }
c51e6221 2284
c0a62865
DK
2285 // Determine if we want to delay code-fill generation until the output
2286 // section is written. When the target is relaxing, we want to delay fill
4cf7a849
ST
2287 // generating to avoid adjusting them during relaxation. Also, if we are
2288 // sorting input sections we must delay fill generation.
c0a62865
DK
2289 if (!this->generate_code_fills_at_write_
2290 && !have_sections_script
2291 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2292 && parameters->target().has_code_fill()
4cf7a849
ST
2293 && (parameters->target().may_relax()
2294 || parameters->options().section_ordering_file()))
c0a62865
DK
2295 {
2296 gold_assert(this->fills_.empty());
2297 this->generate_code_fills_at_write_ = true;
2298 }
2299
c51e6221 2300 if (aligned_offset_in_section > offset_in_section
c0a62865 2301 && !this->generate_code_fills_at_write_
a445fddf 2302 && !have_sections_script
44a43cf9 2303 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
029ba973 2304 && parameters->target().has_code_fill())
c51e6221
ILT
2305 {
2306 // We need to add some fill data. Using fill_list_ when
2307 // possible is an optimization, since we will often have fill
2308 // sections without input sections.
2309 off_t fill_len = aligned_offset_in_section - offset_in_section;
2310 if (this->input_sections_.empty())
2311 this->fills_.push_back(Fill(offset_in_section, fill_len));
2312 else
2313 {
029ba973 2314 std::string fill_data(parameters->target().code_fill(fill_len));
c51e6221
ILT
2315 Output_data_const* odc = new Output_data_const(fill_data, 1);
2316 this->input_sections_.push_back(Input_section(odc));
2317 }
2318 }
2319
ead1e424 2320 // We need to keep track of this section if we are already keeping
2fd32231
ILT
2321 // track of sections, or if we are relaxing. Also, if this is a
2322 // section which requires sorting, or which may require sorting in
6e9ba2ca
ST
2323 // the future, we keep track of the sections. If the
2324 // --section-ordering-file option is used to specify the order of
2325 // sections, we need to keep track of sections.
131687b4
DK
2326 if (this->always_keeps_input_sections_
2327 || have_sections_script
2fd32231
ILT
2328 || !this->input_sections_.empty()
2329 || this->may_sort_attached_input_sections()
7d9e3d98 2330 || this->must_sort_attached_input_sections()
20e6d0d6 2331 || parameters->options().user_set_Map()
6e9ba2ca
ST
2332 || parameters->target().may_relax()
2333 || parameters->options().section_ordering_file())
2334 {
6fc6ea19 2335 Input_section isecn(object, shndx, input_section_size, addralign);
6e9ba2ca
ST
2336 if (parameters->options().section_ordering_file())
2337 {
2338 unsigned int section_order_index =
2339 layout->find_section_order_index(std::string(secname));
2340 if (section_order_index != 0)
2341 {
2342 isecn.set_section_order_index(section_order_index);
2343 this->set_input_section_order_specified();
2344 }
2345 }
cdc29364
CC
2346 if (this->has_fixed_layout())
2347 {
2348 // For incremental updates, finalize the address and offset now.
2349 uint64_t addr = this->address();
2350 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2351 aligned_offset_in_section,
2352 this->offset());
2353 }
6e9ba2ca
ST
2354 this->input_sections_.push_back(isecn);
2355 }
54dc6425 2356
c51e6221 2357 return aligned_offset_in_section;
61ba1cf9
ILT
2358}
2359
ead1e424
ILT
2360// Add arbitrary data to an output section.
2361
2362void
2363Output_section::add_output_section_data(Output_section_data* posd)
2364{
b8e6aad9
ILT
2365 Input_section inp(posd);
2366 this->add_output_section_data(&inp);
a445fddf
ILT
2367
2368 if (posd->is_data_size_valid())
2369 {
cdc29364
CC
2370 off_t offset_in_section;
2371 if (this->has_fixed_layout())
2372 {
2373 // For incremental updates, find a chunk of unused space.
2374 offset_in_section = this->free_list_.allocate(posd->data_size(),
2375 posd->addralign(), 0);
2376 if (offset_in_section == -1)
e6455dfb
CC
2377 gold_fallback(_("out of patch space; "
2378 "relink with --incremental-full"));
cdc29364
CC
2379 // Finalize the address and offset now.
2380 uint64_t addr = this->address();
2381 off_t offset = this->offset();
2382 posd->set_address_and_file_offset(addr + offset_in_section,
2383 offset + offset_in_section);
2384 }
2385 else
2386 {
2387 offset_in_section = this->current_data_size_for_child();
2388 off_t aligned_offset_in_section = align_address(offset_in_section,
2389 posd->addralign());
2390 this->set_current_data_size_for_child(aligned_offset_in_section
2391 + posd->data_size());
2392 }
2393 }
2394 else if (this->has_fixed_layout())
2395 {
2396 // For incremental updates, arrange for the data to have a fixed layout.
2397 // This will mean that additions to the data must be allocated from
2398 // free space within the containing output section.
2399 uint64_t addr = this->address();
2400 posd->set_address(addr);
2401 posd->set_file_offset(0);
4829d394
CC
2402 // FIXME: This should eventually be unreachable.
2403 // gold_unreachable();
a445fddf 2404 }
b8e6aad9
ILT
2405}
2406
c0a62865
DK
2407// Add a relaxed input section.
2408
2409void
d06fb4d1
DK
2410Output_section::add_relaxed_input_section(Layout* layout,
2411 Output_relaxed_input_section* poris,
2412 const std::string& name)
c0a62865
DK
2413{
2414 Input_section inp(poris);
d06fb4d1
DK
2415
2416 // If the --section-ordering-file option is used to specify the order of
2417 // sections, we need to keep track of sections.
2418 if (parameters->options().section_ordering_file())
2419 {
2420 unsigned int section_order_index =
2421 layout->find_section_order_index(name);
2422 if (section_order_index != 0)
2423 {
2424 inp.set_section_order_index(section_order_index);
2425 this->set_input_section_order_specified();
2426 }
2427 }
2428
c0a62865 2429 this->add_output_section_data(&inp);
0439c796
DK
2430 if (this->lookup_maps_->is_valid())
2431 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2432 poris->shndx(), poris);
c0a62865
DK
2433
2434 // For a relaxed section, we use the current data size. Linker scripts
2435 // get all the input sections, including relaxed one from an output
2436 // section and add them back to them same output section to compute the
2437 // output section size. If we do not account for sizes of relaxed input
2438 // sections, an output section would be incorrectly sized.
2439 off_t offset_in_section = this->current_data_size_for_child();
2440 off_t aligned_offset_in_section = align_address(offset_in_section,
2441 poris->addralign());
2442 this->set_current_data_size_for_child(aligned_offset_in_section
2443 + poris->current_data_size());
2444}
2445
b8e6aad9 2446// Add arbitrary data to an output section by Input_section.
c06b7b0b 2447
b8e6aad9
ILT
2448void
2449Output_section::add_output_section_data(Input_section* inp)
2450{
ead1e424 2451 if (this->input_sections_.empty())
27bc2bce 2452 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 2453
b8e6aad9 2454 this->input_sections_.push_back(*inp);
c06b7b0b 2455
2ea97941
ILT
2456 uint64_t addralign = inp->addralign();
2457 if (addralign > this->addralign_)
2458 this->addralign_ = addralign;
c06b7b0b 2459
b8e6aad9
ILT
2460 inp->set_output_section(this);
2461}
2462
2463// Add a merge section to an output section.
2464
2465void
2466Output_section::add_output_merge_section(Output_section_data* posd,
2ea97941 2467 bool is_string, uint64_t entsize)
b8e6aad9 2468{
2ea97941 2469 Input_section inp(posd, is_string, entsize);
b8e6aad9
ILT
2470 this->add_output_section_data(&inp);
2471}
2472
2473// Add an input section to a SHF_MERGE section.
2474
2475bool
2ea97941
ILT
2476Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2477 uint64_t flags, uint64_t entsize,
0439c796
DK
2478 uint64_t addralign,
2479 bool keeps_input_sections)
b8e6aad9 2480{
2ea97941 2481 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
87f95776
ILT
2482
2483 // We only merge strings if the alignment is not more than the
2484 // character size. This could be handled, but it's unusual.
2ea97941 2485 if (is_string && addralign > entsize)
b8e6aad9
ILT
2486 return false;
2487
20e6d0d6
DK
2488 // We cannot restore merged input section states.
2489 gold_assert(this->checkpoint_ == NULL);
2490
c0a62865 2491 // Look up merge sections by required properties.
0439c796
DK
2492 // Currently, we only invalidate the lookup maps in script processing
2493 // and relaxation. We should not have done either when we reach here.
2494 // So we assume that the lookup maps are valid to simply code.
2495 gold_assert(this->lookup_maps_->is_valid());
2ea97941 2496 Merge_section_properties msp(is_string, entsize, addralign);
0439c796
DK
2497 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2498 bool is_new = false;
2499 if (pomb != NULL)
c0a62865 2500 {
6bf924b0
DK
2501 gold_assert(pomb->is_string() == is_string
2502 && pomb->entsize() == entsize
2503 && pomb->addralign() == addralign);
c0a62865 2504 }
b8e6aad9
ILT
2505 else
2506 {
6bf924b0
DK
2507 // Create a new Output_merge_data or Output_merge_string_data.
2508 if (!is_string)
2509 pomb = new Output_merge_data(entsize, addralign);
2510 else
9a0910c3 2511 {
6bf924b0
DK
2512 switch (entsize)
2513 {
2514 case 1:
2515 pomb = new Output_merge_string<char>(addralign);
2516 break;
2517 case 2:
2518 pomb = new Output_merge_string<uint16_t>(addralign);
2519 break;
2520 case 4:
2521 pomb = new Output_merge_string<uint32_t>(addralign);
2522 break;
2523 default:
2524 return false;
2525 }
9a0910c3 2526 }
0439c796
DK
2527 // If we need to do script processing or relaxation, we need to keep
2528 // the original input sections to rebuild the fast lookup maps.
2529 if (keeps_input_sections)
2530 pomb->set_keeps_input_sections();
2531 is_new = true;
b8e6aad9
ILT
2532 }
2533
6bf924b0
DK
2534 if (pomb->add_input_section(object, shndx))
2535 {
0439c796
DK
2536 // Add new merge section to this output section and link merge
2537 // section properties to new merge section in map.
2538 if (is_new)
2539 {
2540 this->add_output_merge_section(pomb, is_string, entsize);
2541 this->lookup_maps_->add_merge_section(msp, pomb);
2542 }
2543
6bf924b0
DK
2544 // Add input section to new merge section and link input section to new
2545 // merge section in map.
0439c796 2546 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
6bf924b0
DK
2547 return true;
2548 }
2549 else
0439c796
DK
2550 {
2551 // If add_input_section failed, delete new merge section to avoid
2552 // exporting empty merge sections in Output_section::get_input_section.
2553 if (is_new)
2554 delete pomb;
2555 return false;
2556 }
b8e6aad9
ILT
2557}
2558
c0a62865 2559// Build a relaxation map to speed up relaxation of existing input sections.
2ea97941 2560// Look up to the first LIMIT elements in INPUT_SECTIONS.
c0a62865 2561
20e6d0d6 2562void
c0a62865 2563Output_section::build_relaxation_map(
2ea97941 2564 const Input_section_list& input_sections,
c0a62865
DK
2565 size_t limit,
2566 Relaxation_map* relaxation_map) const
20e6d0d6 2567{
c0a62865
DK
2568 for (size_t i = 0; i < limit; ++i)
2569 {
2ea97941 2570 const Input_section& is(input_sections[i]);
c0a62865
DK
2571 if (is.is_input_section() || is.is_relaxed_input_section())
2572 {
5ac169d4
DK
2573 Section_id sid(is.relobj(), is.shndx());
2574 (*relaxation_map)[sid] = i;
c0a62865
DK
2575 }
2576 }
2577}
2578
2579// Convert regular input sections in INPUT_SECTIONS into relaxed input
5ac169d4
DK
2580// sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2581// indices of INPUT_SECTIONS.
20e6d0d6 2582
c0a62865
DK
2583void
2584Output_section::convert_input_sections_in_list_to_relaxed_sections(
2585 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2586 const Relaxation_map& map,
2ea97941 2587 Input_section_list* input_sections)
c0a62865
DK
2588{
2589 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2590 {
2591 Output_relaxed_input_section* poris = relaxed_sections[i];
5ac169d4
DK
2592 Section_id sid(poris->relobj(), poris->shndx());
2593 Relaxation_map::const_iterator p = map.find(sid);
c0a62865 2594 gold_assert(p != map.end());
2ea97941 2595 gold_assert((*input_sections)[p->second].is_input_section());
d06fb4d1
DK
2596
2597 // Remember section order index of original input section
2598 // if it is set. Copy it to the relaxed input section.
2599 unsigned int soi =
2600 (*input_sections)[p->second].section_order_index();
2ea97941 2601 (*input_sections)[p->second] = Input_section(poris);
d06fb4d1 2602 (*input_sections)[p->second].set_section_order_index(soi);
c0a62865
DK
2603 }
2604}
2605
2606// Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2607// is a vector of pointers to Output_relaxed_input_section or its derived
2608// classes. The relaxed sections must correspond to existing input sections.
2609
2610void
2611Output_section::convert_input_sections_to_relaxed_sections(
2612 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2613{
029ba973 2614 gold_assert(parameters->target().may_relax());
20e6d0d6 2615
c0a62865
DK
2616 // We want to make sure that restore_states does not undo the effect of
2617 // this. If there is no checkpoint active, just search the current
2618 // input section list and replace the sections there. If there is
2619 // a checkpoint, also replace the sections there.
2620
2621 // By default, we look at the whole list.
2622 size_t limit = this->input_sections_.size();
2623
2624 if (this->checkpoint_ != NULL)
20e6d0d6 2625 {
c0a62865
DK
2626 // Replace input sections with relaxed input section in the saved
2627 // copy of the input section list.
2628 if (this->checkpoint_->input_sections_saved())
20e6d0d6 2629 {
c0a62865
DK
2630 Relaxation_map map;
2631 this->build_relaxation_map(
2632 *(this->checkpoint_->input_sections()),
2633 this->checkpoint_->input_sections()->size(),
2634 &map);
2635 this->convert_input_sections_in_list_to_relaxed_sections(
2636 relaxed_sections,
2637 map,
2638 this->checkpoint_->input_sections());
2639 }
2640 else
2641 {
2642 // We have not copied the input section list yet. Instead, just
2643 // look at the portion that would be saved.
2644 limit = this->checkpoint_->input_sections_size();
20e6d0d6 2645 }
20e6d0d6 2646 }
c0a62865
DK
2647
2648 // Convert input sections in input_section_list.
2649 Relaxation_map map;
2650 this->build_relaxation_map(this->input_sections_, limit, &map);
2651 this->convert_input_sections_in_list_to_relaxed_sections(
2652 relaxed_sections,
2653 map,
2654 &this->input_sections_);
41263c05
DK
2655
2656 // Update fast look-up map.
0439c796 2657 if (this->lookup_maps_->is_valid())
41263c05
DK
2658 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2659 {
2660 Output_relaxed_input_section* poris = relaxed_sections[i];
0439c796
DK
2661 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2662 poris->shndx(), poris);
41263c05 2663 }
20e6d0d6
DK
2664}
2665
9c547ec3
ILT
2666// Update the output section flags based on input section flags.
2667
2668void
2ea97941 2669Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
9c547ec3
ILT
2670{
2671 // If we created the section with SHF_ALLOC clear, we set the
2672 // address. If we are now setting the SHF_ALLOC flag, we need to
2673 // undo that.
2674 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2ea97941 2675 && (flags & elfcpp::SHF_ALLOC) != 0)
9c547ec3
ILT
2676 this->mark_address_invalid();
2677
2ea97941 2678 this->flags_ |= (flags
9c547ec3
ILT
2679 & (elfcpp::SHF_WRITE
2680 | elfcpp::SHF_ALLOC
2681 | elfcpp::SHF_EXECINSTR));
e8cd95c7
ILT
2682
2683 if ((flags & elfcpp::SHF_MERGE) == 0)
2684 this->flags_ &=~ elfcpp::SHF_MERGE;
2685 else
2686 {
2687 if (this->current_data_size_for_child() == 0)
2688 this->flags_ |= elfcpp::SHF_MERGE;
2689 }
2690
2691 if ((flags & elfcpp::SHF_STRINGS) == 0)
2692 this->flags_ &=~ elfcpp::SHF_STRINGS;
2693 else
2694 {
2695 if (this->current_data_size_for_child() == 0)
2696 this->flags_ |= elfcpp::SHF_STRINGS;
2697 }
9c547ec3
ILT
2698}
2699
2ea97941 2700// Find the merge section into which an input section with index SHNDX in
c0a62865
DK
2701// OBJECT has been added. Return NULL if none found.
2702
2703Output_section_data*
2704Output_section::find_merge_section(const Relobj* object,
2ea97941 2705 unsigned int shndx) const
c0a62865 2706{
0439c796
DK
2707 if (!this->lookup_maps_->is_valid())
2708 this->build_lookup_maps();
2709 return this->lookup_maps_->find_merge_section(object, shndx);
2710}
2711
2712// Build the lookup maps for merge and relaxed sections. This is needs
2713// to be declared as a const methods so that it is callable with a const
2714// Output_section pointer. The method only updates states of the maps.
2715
2716void
2717Output_section::build_lookup_maps() const
2718{
2719 this->lookup_maps_->clear();
2720 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2721 p != this->input_sections_.end();
2722 ++p)
c0a62865 2723 {
0439c796
DK
2724 if (p->is_merge_section())
2725 {
2726 Output_merge_base* pomb = p->output_merge_base();
2727 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2728 pomb->addralign());
2729 this->lookup_maps_->add_merge_section(msp, pomb);
2730 for (Output_merge_base::Input_sections::const_iterator is =
2731 pomb->input_sections_begin();
2732 is != pomb->input_sections_end();
2733 ++is)
2734 {
2735 const Const_section_id& csid = *is;
2736 this->lookup_maps_->add_merge_input_section(csid.first,
2737 csid.second, pomb);
2738 }
2739
2740 }
2741 else if (p->is_relaxed_input_section())
2742 {
2743 Output_relaxed_input_section* poris = p->relaxed_input_section();
2744 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2745 poris->shndx(), poris);
2746 }
c0a62865 2747 }
c0a62865
DK
2748}
2749
2750// Find an relaxed input section corresponding to an input section
2ea97941 2751// in OBJECT with index SHNDX.
c0a62865 2752
d6344fb5 2753const Output_relaxed_input_section*
c0a62865 2754Output_section::find_relaxed_input_section(const Relobj* object,
2ea97941 2755 unsigned int shndx) const
c0a62865 2756{
0439c796
DK
2757 if (!this->lookup_maps_->is_valid())
2758 this->build_lookup_maps();
2759 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
c0a62865
DK
2760}
2761
2ea97941
ILT
2762// Given an address OFFSET relative to the start of input section
2763// SHNDX in OBJECT, return whether this address is being included in
2764// the final link. This should only be called if SHNDX in OBJECT has
730cdc88
ILT
2765// a special mapping.
2766
2767bool
2768Output_section::is_input_address_mapped(const Relobj* object,
2ea97941
ILT
2769 unsigned int shndx,
2770 off_t offset) const
730cdc88 2771{
c0a62865 2772 // Look at the Output_section_data_maps first.
2ea97941 2773 const Output_section_data* posd = this->find_merge_section(object, shndx);
c0a62865 2774 if (posd == NULL)
2ea97941 2775 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
2776
2777 if (posd != NULL)
2778 {
2ea97941
ILT
2779 section_offset_type output_offset;
2780 bool found = posd->output_offset(object, shndx, offset, &output_offset);
c0a62865 2781 gold_assert(found);
2ea97941 2782 return output_offset != -1;
c0a62865
DK
2783 }
2784
2785 // Fall back to the slow look-up.
730cdc88
ILT
2786 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2787 p != this->input_sections_.end();
2788 ++p)
2789 {
2ea97941
ILT
2790 section_offset_type output_offset;
2791 if (p->output_offset(object, shndx, offset, &output_offset))
2792 return output_offset != -1;
730cdc88
ILT
2793 }
2794
2795 // By default we assume that the address is mapped. This should
2796 // only be called after we have passed all sections to Layout. At
2797 // that point we should know what we are discarding.
2798 return true;
2799}
2800
2ea97941
ILT
2801// Given an address OFFSET relative to the start of input section
2802// SHNDX in object OBJECT, return the output offset relative to the
1e983657 2803// start of the input section in the output section. This should only
2ea97941 2804// be called if SHNDX in OBJECT has a special mapping.
730cdc88 2805
8383303e 2806section_offset_type
2ea97941
ILT
2807Output_section::output_offset(const Relobj* object, unsigned int shndx,
2808 section_offset_type offset) const
730cdc88 2809{
c0a62865
DK
2810 // This can only be called meaningfully when we know the data size
2811 // of this.
2812 gold_assert(this->is_data_size_valid());
730cdc88 2813
c0a62865 2814 // Look at the Output_section_data_maps first.
2ea97941 2815 const Output_section_data* posd = this->find_merge_section(object, shndx);
c0a62865 2816 if (posd == NULL)
2ea97941 2817 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
2818 if (posd != NULL)
2819 {
2ea97941
ILT
2820 section_offset_type output_offset;
2821 bool found = posd->output_offset(object, shndx, offset, &output_offset);
c0a62865 2822 gold_assert(found);
2ea97941 2823 return output_offset;
c0a62865
DK
2824 }
2825
2826 // Fall back to the slow look-up.
730cdc88
ILT
2827 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2828 p != this->input_sections_.end();
2829 ++p)
2830 {
2ea97941
ILT
2831 section_offset_type output_offset;
2832 if (p->output_offset(object, shndx, offset, &output_offset))
2833 return output_offset;
730cdc88
ILT
2834 }
2835 gold_unreachable();
2836}
2837
2ea97941
ILT
2838// Return the output virtual address of OFFSET relative to the start
2839// of input section SHNDX in object OBJECT.
b8e6aad9
ILT
2840
2841uint64_t
2ea97941
ILT
2842Output_section::output_address(const Relobj* object, unsigned int shndx,
2843 off_t offset) const
b8e6aad9
ILT
2844{
2845 uint64_t addr = this->address() + this->first_input_offset_;
c0a62865
DK
2846
2847 // Look at the Output_section_data_maps first.
2ea97941 2848 const Output_section_data* posd = this->find_merge_section(object, shndx);
c0a62865 2849 if (posd == NULL)
2ea97941 2850 posd = this->find_relaxed_input_section(object, shndx);
c0a62865
DK
2851 if (posd != NULL && posd->is_address_valid())
2852 {
2ea97941
ILT
2853 section_offset_type output_offset;
2854 bool found = posd->output_offset(object, shndx, offset, &output_offset);
c0a62865 2855 gold_assert(found);
2ea97941 2856 return posd->address() + output_offset;
c0a62865
DK
2857 }
2858
2859 // Fall back to the slow look-up.
b8e6aad9
ILT
2860 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2861 p != this->input_sections_.end();
2862 ++p)
2863 {
2864 addr = align_address(addr, p->addralign());
2ea97941
ILT
2865 section_offset_type output_offset;
2866 if (p->output_offset(object, shndx, offset, &output_offset))
730cdc88 2867 {
2ea97941 2868 if (output_offset == -1)
eff45813 2869 return -1ULL;
2ea97941 2870 return addr + output_offset;
730cdc88 2871 }
b8e6aad9
ILT
2872 addr += p->data_size();
2873 }
2874
2875 // If we get here, it means that we don't know the mapping for this
2876 // input section. This might happen in principle if
2877 // add_input_section were called before add_output_section_data.
2878 // But it should never actually happen.
2879
2880 gold_unreachable();
ead1e424
ILT
2881}
2882
e29e076a 2883// Find the output address of the start of the merged section for
2ea97941 2884// input section SHNDX in object OBJECT.
a9a60db6 2885
e29e076a
ILT
2886bool
2887Output_section::find_starting_output_address(const Relobj* object,
2ea97941 2888 unsigned int shndx,
e29e076a 2889 uint64_t* paddr) const
a9a60db6 2890{
c0a62865
DK
2891 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2892 // Looking up the merge section map does not always work as we sometimes
2893 // find a merge section without its address set.
a9a60db6
ILT
2894 uint64_t addr = this->address() + this->first_input_offset_;
2895 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2896 p != this->input_sections_.end();
2897 ++p)
2898 {
2899 addr = align_address(addr, p->addralign());
2900
2901 // It would be nice if we could use the existing output_offset
2902 // method to get the output offset of input offset 0.
2903 // Unfortunately we don't know for sure that input offset 0 is
2904 // mapped at all.
2ea97941 2905 if (p->is_merge_section_for(object, shndx))
e29e076a
ILT
2906 {
2907 *paddr = addr;
2908 return true;
2909 }
a9a60db6
ILT
2910
2911 addr += p->data_size();
2912 }
e29e076a
ILT
2913
2914 // We couldn't find a merge output section for this input section.
2915 return false;
a9a60db6
ILT
2916}
2917
cdc29364
CC
2918// Update the data size of an Output_section.
2919
2920void
2921Output_section::update_data_size()
2922{
2923 if (this->input_sections_.empty())
2924 return;
2925
2926 if (this->must_sort_attached_input_sections()
2927 || this->input_section_order_specified())
2928 this->sort_attached_input_sections();
2929
2930 off_t off = this->first_input_offset_;
2931 for (Input_section_list::iterator p = this->input_sections_.begin();
2932 p != this->input_sections_.end();
2933 ++p)
2934 {
2935 off = align_address(off, p->addralign());
2936 off += p->current_data_size();
2937 }
2938
2939 this->set_current_data_size_for_child(off);
2940}
2941
27bc2bce 2942// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
2943// setting the addresses of any Output_section_data objects.
2944
2945void
27bc2bce 2946Output_section::set_final_data_size()
ead1e424
ILT
2947{
2948 if (this->input_sections_.empty())
27bc2bce
ILT
2949 {
2950 this->set_data_size(this->current_data_size_for_child());
2951 return;
2952 }
ead1e424 2953
6e9ba2ca
ST
2954 if (this->must_sort_attached_input_sections()
2955 || this->input_section_order_specified())
2fd32231
ILT
2956 this->sort_attached_input_sections();
2957
2ea97941 2958 uint64_t address = this->address();
27bc2bce 2959 off_t startoff = this->offset();
ead1e424
ILT
2960 off_t off = startoff + this->first_input_offset_;
2961 for (Input_section_list::iterator p = this->input_sections_.begin();
2962 p != this->input_sections_.end();
2963 ++p)
2964 {
2965 off = align_address(off, p->addralign());
2ea97941 2966 p->set_address_and_file_offset(address + (off - startoff), off,
96803768 2967 startoff);
ead1e424
ILT
2968 off += p->data_size();
2969 }
2970
2971 this->set_data_size(off - startoff);
2972}
9a0910c3 2973
a445fddf
ILT
2974// Reset the address and file offset.
2975
2976void
2977Output_section::do_reset_address_and_file_offset()
2978{
20e6d0d6
DK
2979 // An unallocated section has no address. Forcing this means that
2980 // we don't need special treatment for symbols defined in debug
1e5d2fb1
DK
2981 // sections. We do the same in the constructor. This does not
2982 // apply to NOLOAD sections though.
2983 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
20e6d0d6
DK
2984 this->set_address(0);
2985
a445fddf
ILT
2986 for (Input_section_list::iterator p = this->input_sections_.begin();
2987 p != this->input_sections_.end();
2988 ++p)
2989 p->reset_address_and_file_offset();
2990}
20e6d0d6
DK
2991
2992// Return true if address and file offset have the values after reset.
2993
2994bool
2995Output_section::do_address_and_file_offset_have_reset_values() const
2996{
2997 if (this->is_offset_valid())
2998 return false;
2999
3000 // An unallocated section has address 0 after its construction or a reset.
3001 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3002 return this->is_address_valid() && this->address() == 0;
3003 else
3004 return !this->is_address_valid();
3005}
a445fddf 3006
7bf1f802
ILT
3007// Set the TLS offset. Called only for SHT_TLS sections.
3008
3009void
3010Output_section::do_set_tls_offset(uint64_t tls_base)
3011{
3012 this->tls_offset_ = this->address() - tls_base;
3013}
3014
2fd32231
ILT
3015// In a few cases we need to sort the input sections attached to an
3016// output section. This is used to implement the type of constructor
3017// priority ordering implemented by the GNU linker, in which the
3018// priority becomes part of the section name and the sections are
3019// sorted by name. We only do this for an output section if we see an
3020// attached input section matching ".ctor.*", ".dtor.*",
3021// ".init_array.*" or ".fini_array.*".
3022
3023class Output_section::Input_section_sort_entry
3024{
3025 public:
3026 Input_section_sort_entry()
3027 : input_section_(), index_(-1U), section_has_name_(false),
3028 section_name_()
3029 { }
3030
2ea97941 3031 Input_section_sort_entry(const Input_section& input_section,
6e9ba2ca
ST
3032 unsigned int index,
3033 bool must_sort_attached_input_sections)
2ea97941
ILT
3034 : input_section_(input_section), index_(index),
3035 section_has_name_(input_section.is_input_section()
3036 || input_section.is_relaxed_input_section())
2fd32231 3037 {
6e9ba2ca
ST
3038 if (this->section_has_name_
3039 && must_sort_attached_input_sections)
2fd32231
ILT
3040 {
3041 // This is only called single-threaded from Layout::finalize,
3042 // so it is OK to lock. Unfortunately we have no way to pass
3043 // in a Task token.
3044 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2ea97941
ILT
3045 Object* obj = (input_section.is_input_section()
3046 ? input_section.relobj()
3047 : input_section.relaxed_input_section()->relobj());
2fd32231
ILT
3048 Task_lock_obj<Object> tl(dummy_task, obj);
3049
3050 // This is a slow operation, which should be cached in
3051 // Layout::layout if this becomes a speed problem.
2ea97941 3052 this->section_name_ = obj->section_name(input_section.shndx());
2fd32231
ILT
3053 }
3054 }
3055
3056 // Return the Input_section.
3057 const Input_section&
3058 input_section() const
3059 {
3060 gold_assert(this->index_ != -1U);
3061 return this->input_section_;
3062 }
3063
3064 // The index of this entry in the original list. This is used to
3065 // make the sort stable.
3066 unsigned int
3067 index() const
3068 {
3069 gold_assert(this->index_ != -1U);
3070 return this->index_;
3071 }
3072
3073 // Whether there is a section name.
3074 bool
3075 section_has_name() const
3076 { return this->section_has_name_; }
3077
3078 // The section name.
3079 const std::string&
3080 section_name() const
3081 {
3082 gold_assert(this->section_has_name_);
3083 return this->section_name_;
3084 }
3085
ab794b6b
ILT
3086 // Return true if the section name has a priority. This is assumed
3087 // to be true if it has a dot after the initial dot.
2fd32231 3088 bool
ab794b6b 3089 has_priority() const
2fd32231
ILT
3090 {
3091 gold_assert(this->section_has_name_);
2a0ff005 3092 return this->section_name_.find('.', 1) != std::string::npos;
2fd32231
ILT
3093 }
3094
ab794b6b
ILT
3095 // Return true if this an input file whose base name matches
3096 // FILE_NAME. The base name must have an extension of ".o", and
3097 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3098 // This is to match crtbegin.o as well as crtbeginS.o without
3099 // getting confused by other possibilities. Overall matching the
3100 // file name this way is a dreadful hack, but the GNU linker does it
3101 // in order to better support gcc, and we need to be compatible.
2fd32231 3102 bool
2ea97941 3103 match_file_name(const char* match_file_name) const
2fd32231 3104 {
2fd32231
ILT
3105 const std::string& file_name(this->input_section_.relobj()->name());
3106 const char* base_name = lbasename(file_name.c_str());
2ea97941
ILT
3107 size_t match_len = strlen(match_file_name);
3108 if (strncmp(base_name, match_file_name, match_len) != 0)
2fd32231
ILT
3109 return false;
3110 size_t base_len = strlen(base_name);
3111 if (base_len != match_len + 2 && base_len != match_len + 3)
3112 return false;
3113 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
3114 }
3115
8fe2a369
ST
3116 // Returns 1 if THIS should appear before S in section order, -1 if S
3117 // appears before THIS and 0 if they are not comparable.
6e9ba2ca
ST
3118 int
3119 compare_section_ordering(const Input_section_sort_entry& s) const
3120 {
8fe2a369
ST
3121 unsigned int this_secn_index = this->input_section_.section_order_index();
3122 unsigned int s_secn_index = s.input_section().section_order_index();
3123 if (this_secn_index > 0 && s_secn_index > 0)
3124 {
3125 if (this_secn_index < s_secn_index)
3126 return 1;
3127 else if (this_secn_index > s_secn_index)
3128 return -1;
3129 }
3130 return 0;
6e9ba2ca
ST
3131 }
3132
2fd32231
ILT
3133 private:
3134 // The Input_section we are sorting.
3135 Input_section input_section_;
3136 // The index of this Input_section in the original list.
3137 unsigned int index_;
3138 // Whether this Input_section has a section name--it won't if this
3139 // is some random Output_section_data.
3140 bool section_has_name_;
3141 // The section name if there is one.
3142 std::string section_name_;
3143};
3144
3145// Return true if S1 should come before S2 in the output section.
3146
3147bool
3148Output_section::Input_section_sort_compare::operator()(
3149 const Output_section::Input_section_sort_entry& s1,
3150 const Output_section::Input_section_sort_entry& s2) const
3151{
ab794b6b
ILT
3152 // crtbegin.o must come first.
3153 bool s1_begin = s1.match_file_name("crtbegin");
3154 bool s2_begin = s2.match_file_name("crtbegin");
2fd32231
ILT
3155 if (s1_begin || s2_begin)
3156 {
3157 if (!s1_begin)
3158 return false;
3159 if (!s2_begin)
3160 return true;
3161 return s1.index() < s2.index();
3162 }
3163
ab794b6b
ILT
3164 // crtend.o must come last.
3165 bool s1_end = s1.match_file_name("crtend");
3166 bool s2_end = s2.match_file_name("crtend");
2fd32231
ILT
3167 if (s1_end || s2_end)
3168 {
3169 if (!s1_end)
3170 return true;
3171 if (!s2_end)
3172 return false;
3173 return s1.index() < s2.index();
3174 }
3175
ab794b6b
ILT
3176 // We sort all the sections with no names to the end.
3177 if (!s1.section_has_name() || !s2.section_has_name())
3178 {
3179 if (s1.section_has_name())
3180 return true;
3181 if (s2.section_has_name())
3182 return false;
3183 return s1.index() < s2.index();
3184 }
2fd32231 3185
ab794b6b 3186 // A section with a priority follows a section without a priority.
ab794b6b
ILT
3187 bool s1_has_priority = s1.has_priority();
3188 bool s2_has_priority = s2.has_priority();
3189 if (s1_has_priority && !s2_has_priority)
2fd32231 3190 return false;
ab794b6b 3191 if (!s1_has_priority && s2_has_priority)
2fd32231
ILT
3192 return true;
3193
6e9ba2ca
ST
3194 // Check if a section order exists for these sections through a section
3195 // ordering file. If sequence_num is 0, an order does not exist.
3196 int sequence_num = s1.compare_section_ordering(s2);
3197 if (sequence_num != 0)
3198 return sequence_num == 1;
3199
2fd32231
ILT
3200 // Otherwise we sort by name.
3201 int compare = s1.section_name().compare(s2.section_name());
3202 if (compare != 0)
3203 return compare < 0;
3204
3205 // Otherwise we keep the input order.
3206 return s1.index() < s2.index();
3207}
3208
2a0ff005
DK
3209// Return true if S1 should come before S2 in an .init_array or .fini_array
3210// output section.
3211
3212bool
3213Output_section::Input_section_sort_init_fini_compare::operator()(
3214 const Output_section::Input_section_sort_entry& s1,
3215 const Output_section::Input_section_sort_entry& s2) const
3216{
3217 // We sort all the sections with no names to the end.
3218 if (!s1.section_has_name() || !s2.section_has_name())
3219 {
3220 if (s1.section_has_name())
3221 return true;
3222 if (s2.section_has_name())
3223 return false;
3224 return s1.index() < s2.index();
3225 }
3226
3227 // A section without a priority follows a section with a priority.
3228 // This is the reverse of .ctors and .dtors sections.
3229 bool s1_has_priority = s1.has_priority();
3230 bool s2_has_priority = s2.has_priority();
3231 if (s1_has_priority && !s2_has_priority)
3232 return true;
3233 if (!s1_has_priority && s2_has_priority)
3234 return false;
3235
6e9ba2ca
ST
3236 // Check if a section order exists for these sections through a section
3237 // ordering file. If sequence_num is 0, an order does not exist.
3238 int sequence_num = s1.compare_section_ordering(s2);
3239 if (sequence_num != 0)
3240 return sequence_num == 1;
3241
2a0ff005
DK
3242 // Otherwise we sort by name.
3243 int compare = s1.section_name().compare(s2.section_name());
3244 if (compare != 0)
3245 return compare < 0;
3246
3247 // Otherwise we keep the input order.
3248 return s1.index() < s2.index();
3249}
3250
8fe2a369
ST
3251// Return true if S1 should come before S2. Sections that do not match
3252// any pattern in the section ordering file are placed ahead of the sections
3253// that match some pattern.
3254
6e9ba2ca
ST
3255bool
3256Output_section::Input_section_sort_section_order_index_compare::operator()(
3257 const Output_section::Input_section_sort_entry& s1,
3258 const Output_section::Input_section_sort_entry& s2) const
3259{
8fe2a369
ST
3260 unsigned int s1_secn_index = s1.input_section().section_order_index();
3261 unsigned int s2_secn_index = s2.input_section().section_order_index();
6e9ba2ca 3262
8fe2a369
ST
3263 // Keep input order if section ordering cannot determine order.
3264 if (s1_secn_index == s2_secn_index)
3265 return s1.index() < s2.index();
3266
3267 return s1_secn_index < s2_secn_index;
6e9ba2ca
ST
3268}
3269
2fd32231
ILT
3270// Sort the input sections attached to an output section.
3271
3272void
3273Output_section::sort_attached_input_sections()
3274{
3275 if (this->attached_input_sections_are_sorted_)
3276 return;
3277
20e6d0d6
DK
3278 if (this->checkpoint_ != NULL
3279 && !this->checkpoint_->input_sections_saved())
3280 this->checkpoint_->save_input_sections();
3281
2fd32231
ILT
3282 // The only thing we know about an input section is the object and
3283 // the section index. We need the section name. Recomputing this
3284 // is slow but this is an unusual case. If this becomes a speed
3285 // problem we can cache the names as required in Layout::layout.
3286
3287 // We start by building a larger vector holding a copy of each
3288 // Input_section, plus its current index in the list and its name.
3289 std::vector<Input_section_sort_entry> sort_list;
3290
3291 unsigned int i = 0;
3292 for (Input_section_list::iterator p = this->input_sections_.begin();
3293 p != this->input_sections_.end();
3294 ++p, ++i)
6e9ba2ca
ST
3295 sort_list.push_back(Input_section_sort_entry(*p, i,
3296 this->must_sort_attached_input_sections()));
2fd32231
ILT
3297
3298 // Sort the input sections.
6e9ba2ca
ST
3299 if (this->must_sort_attached_input_sections())
3300 {
3301 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3302 || this->type() == elfcpp::SHT_INIT_ARRAY
3303 || this->type() == elfcpp::SHT_FINI_ARRAY)
3304 std::sort(sort_list.begin(), sort_list.end(),
3305 Input_section_sort_init_fini_compare());
3306 else
3307 std::sort(sort_list.begin(), sort_list.end(),
3308 Input_section_sort_compare());
3309 }
2a0ff005 3310 else
6e9ba2ca
ST
3311 {
3312 gold_assert(parameters->options().section_ordering_file());
3313 std::sort(sort_list.begin(), sort_list.end(),
3314 Input_section_sort_section_order_index_compare());
3315 }
2fd32231
ILT
3316
3317 // Copy the sorted input sections back to our list.
3318 this->input_sections_.clear();
3319 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3320 p != sort_list.end();
3321 ++p)
3322 this->input_sections_.push_back(p->input_section());
6e9ba2ca 3323 sort_list.clear();
2fd32231
ILT
3324
3325 // Remember that we sorted the input sections, since we might get
3326 // called again.
3327 this->attached_input_sections_are_sorted_ = true;
3328}
3329
61ba1cf9
ILT
3330// Write the section header to *OSHDR.
3331
3332template<int size, bool big_endian>
3333void
16649710
ILT
3334Output_section::write_header(const Layout* layout,
3335 const Stringpool* secnamepool,
61ba1cf9
ILT
3336 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3337{
3338 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3339 oshdr->put_sh_type(this->type_);
6a74a719 3340
2ea97941 3341 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 3342 if (this->info_section_ != NULL && this->info_uses_section_index_)
2ea97941
ILT
3343 flags |= elfcpp::SHF_INFO_LINK;
3344 oshdr->put_sh_flags(flags);
6a74a719 3345
61ba1cf9
ILT
3346 oshdr->put_sh_addr(this->address());
3347 oshdr->put_sh_offset(this->offset());
3348 oshdr->put_sh_size(this->data_size());
16649710
ILT
3349 if (this->link_section_ != NULL)
3350 oshdr->put_sh_link(this->link_section_->out_shndx());
3351 else if (this->should_link_to_symtab_)
3352 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
3353 else if (this->should_link_to_dynsym_)
3354 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3355 else
3356 oshdr->put_sh_link(this->link_);
755ab8af 3357
2ea97941 3358 elfcpp::Elf_Word info;
16649710 3359 if (this->info_section_ != NULL)
755ab8af
ILT
3360 {
3361 if (this->info_uses_section_index_)
2ea97941 3362 info = this->info_section_->out_shndx();
755ab8af 3363 else
2ea97941 3364 info = this->info_section_->symtab_index();
755ab8af 3365 }
6a74a719 3366 else if (this->info_symndx_ != NULL)
2ea97941 3367 info = this->info_symndx_->symtab_index();
16649710 3368 else
2ea97941
ILT
3369 info = this->info_;
3370 oshdr->put_sh_info(info);
755ab8af 3371
61ba1cf9
ILT
3372 oshdr->put_sh_addralign(this->addralign_);
3373 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
3374}
3375
ead1e424
ILT
3376// Write out the data. For input sections the data is written out by
3377// Object::relocate, but we have to handle Output_section_data objects
3378// here.
3379
3380void
3381Output_section::do_write(Output_file* of)
3382{
96803768
ILT
3383 gold_assert(!this->requires_postprocessing());
3384
c0a62865
DK
3385 // If the target performs relaxation, we delay filler generation until now.
3386 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3387
c51e6221
ILT
3388 off_t output_section_file_offset = this->offset();
3389 for (Fill_list::iterator p = this->fills_.begin();
3390 p != this->fills_.end();
3391 ++p)
3392 {
8851ecca 3393 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 3394 of->write(output_section_file_offset + p->section_offset(),
a445fddf 3395 fill_data.data(), fill_data.size());
c51e6221
ILT
3396 }
3397
c0a62865 3398 off_t off = this->offset() + this->first_input_offset_;
ead1e424
ILT
3399 for (Input_section_list::iterator p = this->input_sections_.begin();
3400 p != this->input_sections_.end();
3401 ++p)
c0a62865
DK
3402 {
3403 off_t aligned_off = align_address(off, p->addralign());
3404 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3405 {
3406 size_t fill_len = aligned_off - off;
3407 std::string fill_data(parameters->target().code_fill(fill_len));
3408 of->write(off, fill_data.data(), fill_data.size());
3409 }
3410
3411 p->write(of);
3412 off = aligned_off + p->data_size();
3413 }
ead1e424
ILT
3414}
3415
96803768
ILT
3416// If a section requires postprocessing, create the buffer to use.
3417
3418void
3419Output_section::create_postprocessing_buffer()
3420{
3421 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
3422
3423 if (this->postprocessing_buffer_ != NULL)
3424 return;
96803768
ILT
3425
3426 if (!this->input_sections_.empty())
3427 {
3428 off_t off = this->first_input_offset_;
3429 for (Input_section_list::iterator p = this->input_sections_.begin();
3430 p != this->input_sections_.end();
3431 ++p)
3432 {
3433 off = align_address(off, p->addralign());
3434 p->finalize_data_size();
3435 off += p->data_size();
3436 }
3437 this->set_current_data_size_for_child(off);
3438 }
3439
3440 off_t buffer_size = this->current_data_size_for_child();
3441 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3442}
3443
3444// Write all the data of an Output_section into the postprocessing
3445// buffer. This is used for sections which require postprocessing,
3446// such as compression. Input sections are handled by
3447// Object::Relocate.
3448
3449void
3450Output_section::write_to_postprocessing_buffer()
3451{
3452 gold_assert(this->requires_postprocessing());
3453
c0a62865
DK
3454 // If the target performs relaxation, we delay filler generation until now.
3455 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3456
96803768
ILT
3457 unsigned char* buffer = this->postprocessing_buffer();
3458 for (Fill_list::iterator p = this->fills_.begin();
3459 p != this->fills_.end();
3460 ++p)
3461 {
8851ecca 3462 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
3463 memcpy(buffer + p->section_offset(), fill_data.data(),
3464 fill_data.size());
96803768
ILT
3465 }
3466
3467 off_t off = this->first_input_offset_;
3468 for (Input_section_list::iterator p = this->input_sections_.begin();
3469 p != this->input_sections_.end();
3470 ++p)
3471 {
c0a62865
DK
3472 off_t aligned_off = align_address(off, p->addralign());
3473 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3474 {
3475 size_t fill_len = aligned_off - off;
3476 std::string fill_data(parameters->target().code_fill(fill_len));
3477 memcpy(buffer + off, fill_data.data(), fill_data.size());
3478 }
3479
3480 p->write_to_buffer(buffer + aligned_off);
3481 off = aligned_off + p->data_size();
96803768
ILT
3482 }
3483}
3484
a445fddf
ILT
3485// Get the input sections for linker script processing. We leave
3486// behind the Output_section_data entries. Note that this may be
3487// slightly incorrect for merge sections. We will leave them behind,
3488// but it is possible that the script says that they should follow
3489// some other input sections, as in:
3490// .rodata { *(.rodata) *(.rodata.cst*) }
3491// For that matter, we don't handle this correctly:
3492// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3493// With luck this will never matter.
3494
3495uint64_t
3496Output_section::get_input_sections(
2ea97941 3497 uint64_t address,
a445fddf 3498 const std::string& fill,
6625d24e 3499 std::list<Input_section>* input_sections)
a445fddf 3500{
20e6d0d6
DK
3501 if (this->checkpoint_ != NULL
3502 && !this->checkpoint_->input_sections_saved())
3503 this->checkpoint_->save_input_sections();
3504
0439c796
DK
3505 // Invalidate fast look-up maps.
3506 this->lookup_maps_->invalidate();
c0a62865 3507
2ea97941 3508 uint64_t orig_address = address;
a445fddf 3509
2ea97941 3510 address = align_address(address, this->addralign());
a445fddf
ILT
3511
3512 Input_section_list remaining;
3513 for (Input_section_list::iterator p = this->input_sections_.begin();
3514 p != this->input_sections_.end();
3515 ++p)
3516 {
0439c796
DK
3517 if (p->is_input_section()
3518 || p->is_relaxed_input_section()
3519 || p->is_merge_section())
6625d24e 3520 input_sections->push_back(*p);
a445fddf
ILT
3521 else
3522 {
2ea97941
ILT
3523 uint64_t aligned_address = align_address(address, p->addralign());
3524 if (aligned_address != address && !fill.empty())
a445fddf
ILT
3525 {
3526 section_size_type length =
2ea97941 3527 convert_to_section_size_type(aligned_address - address);
a445fddf
ILT
3528 std::string this_fill;
3529 this_fill.reserve(length);
3530 while (this_fill.length() + fill.length() <= length)
3531 this_fill += fill;
3532 if (this_fill.length() < length)
3533 this_fill.append(fill, 0, length - this_fill.length());
3534
3535 Output_section_data* posd = new Output_data_const(this_fill, 0);
3536 remaining.push_back(Input_section(posd));
3537 }
2ea97941 3538 address = aligned_address;
a445fddf
ILT
3539
3540 remaining.push_back(*p);
3541
3542 p->finalize_data_size();
2ea97941 3543 address += p->data_size();
a445fddf
ILT
3544 }
3545 }
3546
3547 this->input_sections_.swap(remaining);
3548 this->first_input_offset_ = 0;
3549
2ea97941
ILT
3550 uint64_t data_size = address - orig_address;
3551 this->set_current_data_size_for_child(data_size);
3552 return data_size;
a445fddf
ILT
3553}
3554
6625d24e
DK
3555// Add a script input section. SIS is an Output_section::Input_section,
3556// which can be either a plain input section or a special input section like
3557// a relaxed input section. For a special input section, its size must be
3558// finalized.
a445fddf
ILT
3559
3560void
6625d24e 3561Output_section::add_script_input_section(const Input_section& sis)
a445fddf 3562{
6625d24e
DK
3563 uint64_t data_size = sis.data_size();
3564 uint64_t addralign = sis.addralign();
2ea97941
ILT
3565 if (addralign > this->addralign_)
3566 this->addralign_ = addralign;
a445fddf
ILT
3567
3568 off_t offset_in_section = this->current_data_size_for_child();
3569 off_t aligned_offset_in_section = align_address(offset_in_section,
2ea97941 3570 addralign);
a445fddf
ILT
3571
3572 this->set_current_data_size_for_child(aligned_offset_in_section
2ea97941 3573 + data_size);
a445fddf 3574
6625d24e 3575 this->input_sections_.push_back(sis);
0439c796
DK
3576
3577 // Update fast lookup maps if necessary.
3578 if (this->lookup_maps_->is_valid())
3579 {
3580 if (sis.is_merge_section())
3581 {
3582 Output_merge_base* pomb = sis.output_merge_base();
3583 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3584 pomb->addralign());
3585 this->lookup_maps_->add_merge_section(msp, pomb);
3586 for (Output_merge_base::Input_sections::const_iterator p =
3587 pomb->input_sections_begin();
3588 p != pomb->input_sections_end();
3589 ++p)
3590 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3591 pomb);
3592 }
3593 else if (sis.is_relaxed_input_section())
3594 {
3595 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3596 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3597 poris->shndx(), poris);
3598 }
3599 }
20e6d0d6
DK
3600}
3601
8923b24c 3602// Save states for relaxation.
20e6d0d6
DK
3603
3604void
3605Output_section::save_states()
3606{
3607 gold_assert(this->checkpoint_ == NULL);
3608 Checkpoint_output_section* checkpoint =
3609 new Checkpoint_output_section(this->addralign_, this->flags_,
3610 this->input_sections_,
3611 this->first_input_offset_,
3612 this->attached_input_sections_are_sorted_);
3613 this->checkpoint_ = checkpoint;
3614 gold_assert(this->fills_.empty());
3615}
3616
8923b24c
DK
3617void
3618Output_section::discard_states()
3619{
3620 gold_assert(this->checkpoint_ != NULL);
3621 delete this->checkpoint_;
3622 this->checkpoint_ = NULL;
3623 gold_assert(this->fills_.empty());
3624
0439c796
DK
3625 // Simply invalidate the fast lookup maps since we do not keep
3626 // track of them.
3627 this->lookup_maps_->invalidate();
8923b24c
DK
3628}
3629
20e6d0d6
DK
3630void
3631Output_section::restore_states()
3632{
3633 gold_assert(this->checkpoint_ != NULL);
3634 Checkpoint_output_section* checkpoint = this->checkpoint_;
3635
3636 this->addralign_ = checkpoint->addralign();
3637 this->flags_ = checkpoint->flags();
3638 this->first_input_offset_ = checkpoint->first_input_offset();
3639
3640 if (!checkpoint->input_sections_saved())
3641 {
3642 // If we have not copied the input sections, just resize it.
3643 size_t old_size = checkpoint->input_sections_size();
3644 gold_assert(this->input_sections_.size() >= old_size);
3645 this->input_sections_.resize(old_size);
3646 }
3647 else
3648 {
3649 // We need to copy the whole list. This is not efficient for
3650 // extremely large output with hundreads of thousands of input
3651 // objects. We may need to re-think how we should pass sections
3652 // to scripts.
c0a62865 3653 this->input_sections_ = *checkpoint->input_sections();
20e6d0d6
DK
3654 }
3655
3656 this->attached_input_sections_are_sorted_ =
3657 checkpoint->attached_input_sections_are_sorted();
c0a62865 3658
0439c796
DK
3659 // Simply invalidate the fast lookup maps since we do not keep
3660 // track of them.
3661 this->lookup_maps_->invalidate();
a445fddf
ILT
3662}
3663
8923b24c
DK
3664// Update the section offsets of input sections in this. This is required if
3665// relaxation causes some input sections to change sizes.
3666
3667void
3668Output_section::adjust_section_offsets()
3669{
3670 if (!this->section_offsets_need_adjustment_)
3671 return;
3672
3673 off_t off = 0;
3674 for (Input_section_list::iterator p = this->input_sections_.begin();
3675 p != this->input_sections_.end();
3676 ++p)
3677 {
3678 off = align_address(off, p->addralign());
3679 if (p->is_input_section())
3680 p->relobj()->set_section_offset(p->shndx(), off);
3681 off += p->data_size();
3682 }
3683
3684 this->section_offsets_need_adjustment_ = false;
3685}
3686
7d9e3d98
ILT
3687// Print to the map file.
3688
3689void
3690Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3691{
3692 mapfile->print_output_section(this);
3693
3694 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3695 p != this->input_sections_.end();
3696 ++p)
3697 p->print_to_mapfile(mapfile);
3698}
3699
38c5e8b4
ILT
3700// Print stats for merge sections to stderr.
3701
3702void
3703Output_section::print_merge_stats()
3704{
3705 Input_section_list::iterator p;
3706 for (p = this->input_sections_.begin();
3707 p != this->input_sections_.end();
3708 ++p)
3709 p->print_merge_stats(this->name_);
3710}
3711
cdc29364
CC
3712// Set a fixed layout for the section. Used for incremental update links.
3713
3714void
3715Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3716 off_t sh_size, uint64_t sh_addralign)
3717{
3718 this->addralign_ = sh_addralign;
3719 this->set_current_data_size(sh_size);
3720 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3721 this->set_address(sh_addr);
3722 this->set_file_offset(sh_offset);
3723 this->finalize_data_size();
3724 this->free_list_.init(sh_size, false);
3725 this->has_fixed_layout_ = true;
3726}
3727
3728// Reserve space within the fixed layout for the section. Used for
3729// incremental update links.
5146f448 3730
cdc29364
CC
3731void
3732Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3733{
3734 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3735}
3736
5146f448
CC
3737// Allocate space from the free list for the section. Used for
3738// incremental update links.
3739
3740off_t
3741Output_section::allocate(off_t len, uint64_t addralign)
3742{
3743 return this->free_list_.allocate(len, addralign, 0);
3744}
3745
a2fb1b05
ILT
3746// Output segment methods.
3747
2ea97941 3748Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
22f0da72 3749 : vaddr_(0),
a2fb1b05
ILT
3750 paddr_(0),
3751 memsz_(0),
a445fddf
ILT
3752 max_align_(0),
3753 min_p_align_(0),
a2fb1b05
ILT
3754 offset_(0),
3755 filesz_(0),
2ea97941
ILT
3756 type_(type),
3757 flags_(flags),
a445fddf 3758 is_max_align_known_(false),
8a5e3e08
ILT
3759 are_addresses_set_(false),
3760 is_large_data_segment_(false)
a2fb1b05 3761{
bb321bb1
ILT
3762 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3763 // the flags.
3764 if (type == elfcpp::PT_TLS)
3765 this->flags_ = elfcpp::PF_R;
a2fb1b05
ILT
3766}
3767
22f0da72 3768// Add an Output_section to a PT_LOAD Output_segment.
a2fb1b05
ILT
3769
3770void
22f0da72
ILT
3771Output_segment::add_output_section_to_load(Layout* layout,
3772 Output_section* os,
3773 elfcpp::Elf_Word seg_flags)
a2fb1b05 3774{
22f0da72 3775 gold_assert(this->type() == elfcpp::PT_LOAD);
a3ad94ed 3776 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 3777 gold_assert(!this->is_max_align_known_);
8a5e3e08 3778 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
75f65a3e 3779
a192ba05 3780 this->update_flags_for_output_section(seg_flags);
75f65a3e 3781
22f0da72
ILT
3782 // We don't want to change the ordering if we have a linker script
3783 // with a SECTIONS clause.
3784 Output_section_order order = os->order();
3785 if (layout->script_options()->saw_sections_clause())
3786 order = static_cast<Output_section_order>(0);
75f65a3e 3787 else
22f0da72 3788 gold_assert(order != ORDER_INVALID);
54dc6425 3789
22f0da72
ILT
3790 this->output_lists_[order].push_back(os);
3791}
9f1d377b 3792
22f0da72 3793// Add an Output_section to a non-PT_LOAD Output_segment.
1a2dff53 3794
22f0da72
ILT
3795void
3796Output_segment::add_output_section_to_nonload(Output_section* os,
3797 elfcpp::Elf_Word seg_flags)
3798{
3799 gold_assert(this->type() != elfcpp::PT_LOAD);
3800 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3801 gold_assert(!this->is_max_align_known_);
1a2dff53 3802
22f0da72 3803 this->update_flags_for_output_section(seg_flags);
9f1d377b 3804
22f0da72
ILT
3805 this->output_lists_[0].push_back(os);
3806}
8a5e3e08 3807
22f0da72
ILT
3808// Remove an Output_section from this segment. It is an error if it
3809// is not present.
8a5e3e08 3810
22f0da72
ILT
3811void
3812Output_segment::remove_output_section(Output_section* os)
3813{
3814 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
8a5e3e08 3815 {
22f0da72
ILT
3816 Output_data_list* pdl = &this->output_lists_[i];
3817 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
8a5e3e08 3818 {
22f0da72 3819 if (*p == os)
8a5e3e08 3820 {
22f0da72 3821 pdl->erase(p);
8a5e3e08
ILT
3822 return;
3823 }
3824 }
f5c870d2 3825 }
1650c4ff
ILT
3826 gold_unreachable();
3827}
3828
a192ba05
ILT
3829// Add an Output_data (which need not be an Output_section) to the
3830// start of a segment.
75f65a3e
ILT
3831
3832void
3833Output_segment::add_initial_output_data(Output_data* od)
3834{
a445fddf 3835 gold_assert(!this->is_max_align_known_);
22f0da72
ILT
3836 Output_data_list::iterator p = this->output_lists_[0].begin();
3837 this->output_lists_[0].insert(p, od);
3838}
3839
3840// Return true if this segment has any sections which hold actual
3841// data, rather than being a BSS section.
3842
3843bool
3844Output_segment::has_any_data_sections() const
3845{
3846 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3847 {
3848 const Output_data_list* pdl = &this->output_lists_[i];
3849 for (Output_data_list::const_iterator p = pdl->begin();
3850 p != pdl->end();
3851 ++p)
3852 {
3853 if (!(*p)->is_section())
3854 return true;
3855 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
3856 return true;
3857 }
3858 }
3859 return false;
75f65a3e
ILT
3860}
3861
5bc2f5be
CC
3862// Return whether the first data section (not counting TLS sections)
3863// is a relro section.
9f1d377b
ILT
3864
3865bool
3866Output_segment::is_first_section_relro() const
3867{
22f0da72
ILT
3868 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3869 {
5bc2f5be
CC
3870 if (i == static_cast<int>(ORDER_TLS_DATA)
3871 || i == static_cast<int>(ORDER_TLS_BSS))
3872 continue;
22f0da72
ILT
3873 const Output_data_list* pdl = &this->output_lists_[i];
3874 if (!pdl->empty())
3875 {
3876 Output_data* p = pdl->front();
3877 return p->is_section() && p->output_section()->is_relro();
3878 }
3879 }
3880 return false;
9f1d377b
ILT
3881}
3882
75f65a3e 3883// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
3884
3885uint64_t
a445fddf 3886Output_segment::maximum_alignment()
75f65a3e 3887{
a445fddf 3888 if (!this->is_max_align_known_)
ead1e424 3889 {
22f0da72
ILT
3890 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3891 {
3892 const Output_data_list* pdl = &this->output_lists_[i];
3893 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
3894 if (addralign > this->max_align_)
3895 this->max_align_ = addralign;
3896 }
a445fddf 3897 this->is_max_align_known_ = true;
ead1e424
ILT
3898 }
3899
a445fddf 3900 return this->max_align_;
75f65a3e
ILT
3901}
3902
ead1e424
ILT
3903// Return the maximum alignment of a list of Output_data.
3904
3905uint64_t
a445fddf 3906Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
3907{
3908 uint64_t ret = 0;
3909 for (Output_data_list::const_iterator p = pdl->begin();
3910 p != pdl->end();
3911 ++p)
3912 {
2ea97941
ILT
3913 uint64_t addralign = (*p)->addralign();
3914 if (addralign > ret)
3915 ret = addralign;
ead1e424
ILT
3916 }
3917 return ret;
3918}
3919
22f0da72 3920// Return whether this segment has any dynamic relocs.
4f4c5f80 3921
22f0da72
ILT
3922bool
3923Output_segment::has_dynamic_reloc() const
4f4c5f80 3924{
22f0da72
ILT
3925 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3926 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
3927 return true;
3928 return false;
4f4c5f80
ILT
3929}
3930
22f0da72 3931// Return whether this Output_data_list has any dynamic relocs.
4f4c5f80 3932
22f0da72
ILT
3933bool
3934Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4f4c5f80 3935{
4f4c5f80
ILT
3936 for (Output_data_list::const_iterator p = pdl->begin();
3937 p != pdl->end();
3938 ++p)
22f0da72
ILT
3939 if ((*p)->has_dynamic_reloc())
3940 return true;
3941 return false;
4f4c5f80
ILT
3942}
3943
a445fddf
ILT
3944// Set the section addresses for an Output_segment. If RESET is true,
3945// reset the addresses first. ADDR is the address and *POFF is the
3946// file offset. Set the section indexes starting with *PSHNDX.
5bc2f5be
CC
3947// INCREASE_RELRO is the size of the portion of the first non-relro
3948// section that should be included in the PT_GNU_RELRO segment.
3949// If this segment has relro sections, and has been aligned for
3950// that purpose, set *HAS_RELRO to TRUE. Return the address of
3951// the immediately following segment. Update *HAS_RELRO, *POFF,
3952// and *PSHNDX.
75f65a3e
ILT
3953
3954uint64_t
cdc29364 3955Output_segment::set_section_addresses(Layout* layout, bool reset,
1a2dff53 3956 uint64_t addr,
fd064a5b 3957 unsigned int* increase_relro,
fc497986 3958 bool* has_relro,
1a2dff53 3959 off_t* poff,
ead1e424 3960 unsigned int* pshndx)
75f65a3e 3961{
a3ad94ed 3962 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 3963
fc497986 3964 uint64_t last_relro_pad = 0;
1a2dff53
ILT
3965 off_t orig_off = *poff;
3966
5bc2f5be
CC
3967 bool in_tls = false;
3968
1a2dff53
ILT
3969 // If we have relro sections, we need to pad forward now so that the
3970 // relro sections plus INCREASE_RELRO end on a common page boundary.
3971 if (parameters->options().relro()
3972 && this->is_first_section_relro()
3973 && (!this->are_addresses_set_ || reset))
3974 {
3975 uint64_t relro_size = 0;
3976 off_t off = *poff;
fc497986 3977 uint64_t max_align = 0;
5bc2f5be 3978 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
1a2dff53 3979 {
22f0da72
ILT
3980 Output_data_list* pdl = &this->output_lists_[i];
3981 Output_data_list::iterator p;
3982 for (p = pdl->begin(); p != pdl->end(); ++p)
1a2dff53 3983 {
22f0da72
ILT
3984 if (!(*p)->is_section())
3985 break;
fc497986
CC
3986 uint64_t align = (*p)->addralign();
3987 if (align > max_align)
3988 max_align = align;
5bc2f5be
CC
3989 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3990 in_tls = true;
3991 else if (in_tls)
3992 {
3993 // Align the first non-TLS section to the alignment
3994 // of the TLS segment.
3995 align = max_align;
3996 in_tls = false;
3997 }
fc497986 3998 relro_size = align_address(relro_size, align);
5bc2f5be
CC
3999 // Ignore the size of the .tbss section.
4000 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4001 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4002 continue;
22f0da72
ILT
4003 if ((*p)->is_address_valid())
4004 relro_size += (*p)->data_size();
4005 else
4006 {
4007 // FIXME: This could be faster.
4008 (*p)->set_address_and_file_offset(addr + relro_size,
4009 off + relro_size);
4010 relro_size += (*p)->data_size();
4011 (*p)->reset_address_and_file_offset();
4012 }
1a2dff53 4013 }
22f0da72
ILT
4014 if (p != pdl->end())
4015 break;
1a2dff53 4016 }
fd064a5b 4017 relro_size += *increase_relro;
fc497986
CC
4018 // Pad the total relro size to a multiple of the maximum
4019 // section alignment seen.
4020 uint64_t aligned_size = align_address(relro_size, max_align);
4021 // Note the amount of padding added after the last relro section.
4022 last_relro_pad = aligned_size - relro_size;
fc497986 4023 *has_relro = true;
1a2dff53
ILT
4024
4025 uint64_t page_align = parameters->target().common_pagesize();
4026
4027 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
fc497986 4028 uint64_t desired_align = page_align - (aligned_size % page_align);
1a2dff53
ILT
4029 if (desired_align < *poff % page_align)
4030 *poff += page_align - *poff % page_align;
4031 *poff += desired_align - *poff % page_align;
4032 addr += *poff - orig_off;
4033 orig_off = *poff;
4034 }
4035
a445fddf
ILT
4036 if (!reset && this->are_addresses_set_)
4037 {
4038 gold_assert(this->paddr_ == addr);
4039 addr = this->vaddr_;
4040 }
4041 else
4042 {
4043 this->vaddr_ = addr;
4044 this->paddr_ = addr;
4045 this->are_addresses_set_ = true;
4046 }
75f65a3e 4047
5bc2f5be 4048 in_tls = false;
96a2b4e4 4049
75f65a3e
ILT
4050 this->offset_ = orig_off;
4051
22f0da72
ILT
4052 off_t off = 0;
4053 uint64_t ret;
4054 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4055 {
fc497986
CC
4056 if (i == static_cast<int>(ORDER_RELRO_LAST))
4057 {
4058 *poff += last_relro_pad;
4059 addr += last_relro_pad;
fd064a5b
CC
4060 if (this->output_lists_[i].empty())
4061 {
4062 // If there is nothing in the ORDER_RELRO_LAST list,
4063 // the padding will occur at the end of the relro
4064 // segment, and we need to add it to *INCREASE_RELRO.
4065 *increase_relro += last_relro_pad;
4066 }
fc497986 4067 }
5bc2f5be
CC
4068 addr = this->set_section_list_addresses(layout, reset,
4069 &this->output_lists_[i],
4070 addr, poff, pshndx, &in_tls);
22f0da72
ILT
4071 if (i < static_cast<int>(ORDER_SMALL_BSS))
4072 {
4073 this->filesz_ = *poff - orig_off;
4074 off = *poff;
4075 }
75f65a3e 4076
22f0da72
ILT
4077 ret = addr;
4078 }
96a2b4e4
ILT
4079
4080 // If the last section was a TLS section, align upward to the
4081 // alignment of the TLS segment, so that the overall size of the TLS
4082 // segment is aligned.
4083 if (in_tls)
4084 {
4085 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4086 *poff = align_address(*poff, segment_align);
4087 }
4088
75f65a3e
ILT
4089 this->memsz_ = *poff - orig_off;
4090
4091 // Ignore the file offset adjustments made by the BSS Output_data
4092 // objects.
4093 *poff = off;
61ba1cf9
ILT
4094
4095 return ret;
75f65a3e
ILT
4096}
4097
b8e6aad9
ILT
4098// Set the addresses and file offsets in a list of Output_data
4099// structures.
75f65a3e
ILT
4100
4101uint64_t
cdc29364 4102Output_segment::set_section_list_addresses(Layout* layout, bool reset,
96a2b4e4 4103 Output_data_list* pdl,
ead1e424 4104 uint64_t addr, off_t* poff,
96a2b4e4 4105 unsigned int* pshndx,
1a2dff53 4106 bool* in_tls)
75f65a3e 4107{
ead1e424 4108 off_t startoff = *poff;
cdc29364
CC
4109 // For incremental updates, we may allocate non-fixed sections from
4110 // free space in the file. This keeps track of the high-water mark.
4111 off_t maxoff = startoff;
75f65a3e 4112
ead1e424 4113 off_t off = startoff;
75f65a3e
ILT
4114 for (Output_data_list::iterator p = pdl->begin();
4115 p != pdl->end();
4116 ++p)
4117 {
a445fddf
ILT
4118 if (reset)
4119 (*p)->reset_address_and_file_offset();
4120
cdc29364
CC
4121 // When doing an incremental update or when using a linker script,
4122 // the section will most likely already have an address.
a445fddf 4123 if (!(*p)->is_address_valid())
3802b2dd 4124 {
96a2b4e4
ILT
4125 uint64_t align = (*p)->addralign();
4126
4127 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4128 {
4129 // Give the first TLS section the alignment of the
4130 // entire TLS segment. Otherwise the TLS segment as a
4131 // whole may be misaligned.
4132 if (!*in_tls)
4133 {
4134 Output_segment* tls_segment = layout->tls_segment();
4135 gold_assert(tls_segment != NULL);
4136 uint64_t segment_align = tls_segment->maximum_alignment();
4137 gold_assert(segment_align >= align);
4138 align = segment_align;
4139
4140 *in_tls = true;
4141 }
4142 }
4143 else
4144 {
4145 // If this is the first section after the TLS segment,
4146 // align it to at least the alignment of the TLS
4147 // segment, so that the size of the overall TLS segment
4148 // is aligned.
4149 if (*in_tls)
4150 {
4151 uint64_t segment_align =
4152 layout->tls_segment()->maximum_alignment();
4153 if (segment_align > align)
4154 align = segment_align;
4155
4156 *in_tls = false;
4157 }
4158 }
4159
fb0e076f 4160 if (!parameters->incremental_update())
cdc29364
CC
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);
e6455dfb
CC
4174 gold_fallback(_("out of patch space for section %s; "
4175 "relink with --incremental-full"),
4176 (*p)->output_section()->name());
cdc29364
CC
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);
e6455dfb
CC
4182 gold_fallback(_("%s: section changed size; "
4183 "relink with --incremental-full"),
4184 (*p)->output_section()->name());
cdc29364
CC
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.541888 seconds and 4 git commands to generate.