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