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