1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
33 #ifdef HAVE_SYS_MMAN_H
37 #include "libiberty.h"
40 #include "parameters.h"
45 #include "descriptors.h"
49 // For systems without mmap support.
51 # define mmap gold_mmap
52 # define munmap gold_munmap
53 # define mremap gold_mremap
55 # define MAP_FAILED (reinterpret_cast<void*>(-1))
64 # define MAP_PRIVATE 0
66 # ifndef MAP_ANONYMOUS
67 # define MAP_ANONYMOUS 0
74 # define ENOSYS EINVAL
78 gold_mmap(void *, size_t, int, int, int, off_t
)
85 gold_munmap(void *, size_t)
92 gold_mremap(void *, size_t, size_t, int)
100 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
101 # define mremap gold_mremap
102 extern "C" void *gold_mremap(void *, size_t, size_t, int);
105 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
106 #ifndef MAP_ANONYMOUS
107 # define MAP_ANONYMOUS MAP_ANON
110 #ifndef MREMAP_MAYMOVE
111 # define MREMAP_MAYMOVE 1
114 #ifndef HAVE_POSIX_FALLOCATE
115 // A dummy, non general, version of posix_fallocate. Here we just set
116 // the file size and hope that there is enough disk space. FIXME: We
117 // could allocate disk space by walking block by block and writing a
118 // zero byte into each block.
120 posix_fallocate(int o
, off_t offset
, off_t len
)
122 return ftruncate(o
, offset
+ len
);
124 #endif // !defined(HAVE_POSIX_FALLOCATE)
126 // Mingw does not have S_ISLNK.
128 # define S_ISLNK(mode) 0
134 // Output_data variables.
136 bool Output_data::allocated_sizes_are_fixed
;
138 // Output_data methods.
140 Output_data::~Output_data()
144 // Return the default alignment for the target size.
147 Output_data::default_alignment()
149 return Output_data::default_alignment_for_size(
150 parameters
->target().get_size());
153 // Return the default alignment for a size--32 or 64.
156 Output_data::default_alignment_for_size(int size
)
166 // Output_section_header methods. This currently assumes that the
167 // segment and section lists are complete at construction time.
169 Output_section_headers::Output_section_headers(
170 const Layout
* layout
,
171 const Layout::Segment_list
* segment_list
,
172 const Layout::Section_list
* section_list
,
173 const Layout::Section_list
* unattached_section_list
,
174 const Stringpool
* secnamepool
,
175 const Output_section
* shstrtab_section
)
177 segment_list_(segment_list
),
178 section_list_(section_list
),
179 unattached_section_list_(unattached_section_list
),
180 secnamepool_(secnamepool
),
181 shstrtab_section_(shstrtab_section
)
185 // Compute the current data size.
188 Output_section_headers::do_size() const
190 // Count all the sections. Start with 1 for the null section.
192 if (!parameters
->options().relocatable())
194 for (Layout::Segment_list::const_iterator p
=
195 this->segment_list_
->begin();
196 p
!= this->segment_list_
->end();
198 if ((*p
)->type() == elfcpp::PT_LOAD
)
199 count
+= (*p
)->output_section_count();
203 for (Layout::Section_list::const_iterator p
=
204 this->section_list_
->begin();
205 p
!= this->section_list_
->end();
207 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
210 count
+= this->unattached_section_list_
->size();
212 const int size
= parameters
->target().get_size();
215 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
217 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
221 return count
* shdr_size
;
224 // Write out the section headers.
227 Output_section_headers::do_write(Output_file
* of
)
229 switch (parameters
->size_and_endianness())
231 #ifdef HAVE_TARGET_32_LITTLE
232 case Parameters::TARGET_32_LITTLE
:
233 this->do_sized_write
<32, false>(of
);
236 #ifdef HAVE_TARGET_32_BIG
237 case Parameters::TARGET_32_BIG
:
238 this->do_sized_write
<32, true>(of
);
241 #ifdef HAVE_TARGET_64_LITTLE
242 case Parameters::TARGET_64_LITTLE
:
243 this->do_sized_write
<64, false>(of
);
246 #ifdef HAVE_TARGET_64_BIG
247 case Parameters::TARGET_64_BIG
:
248 this->do_sized_write
<64, true>(of
);
256 template<int size
, bool big_endian
>
258 Output_section_headers::do_sized_write(Output_file
* of
)
260 off_t all_shdrs_size
= this->data_size();
261 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
263 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
264 unsigned char* v
= view
;
267 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
268 oshdr
.put_sh_name(0);
269 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
270 oshdr
.put_sh_flags(0);
271 oshdr
.put_sh_addr(0);
272 oshdr
.put_sh_offset(0);
274 size_t section_count
= (this->data_size()
275 / elfcpp::Elf_sizes
<size
>::shdr_size
);
276 if (section_count
< elfcpp::SHN_LORESERVE
)
277 oshdr
.put_sh_size(0);
279 oshdr
.put_sh_size(section_count
);
281 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
282 if (shstrndx
< elfcpp::SHN_LORESERVE
)
283 oshdr
.put_sh_link(0);
285 oshdr
.put_sh_link(shstrndx
);
287 size_t segment_count
= this->segment_list_
->size();
288 oshdr
.put_sh_info(segment_count
>= elfcpp::PN_XNUM
? segment_count
: 0);
290 oshdr
.put_sh_addralign(0);
291 oshdr
.put_sh_entsize(0);
296 unsigned int shndx
= 1;
297 if (!parameters
->options().relocatable())
299 for (Layout::Segment_list::const_iterator p
=
300 this->segment_list_
->begin();
301 p
!= this->segment_list_
->end();
303 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
310 for (Layout::Section_list::const_iterator p
=
311 this->section_list_
->begin();
312 p
!= this->section_list_
->end();
315 // We do unallocated sections below, except that group
316 // sections have to come first.
317 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
318 && (*p
)->type() != elfcpp::SHT_GROUP
)
320 gold_assert(shndx
== (*p
)->out_shndx());
321 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
322 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
328 for (Layout::Section_list::const_iterator p
=
329 this->unattached_section_list_
->begin();
330 p
!= this->unattached_section_list_
->end();
333 // For a relocatable link, we did unallocated group sections
334 // above, since they have to come first.
335 if ((*p
)->type() == elfcpp::SHT_GROUP
336 && parameters
->options().relocatable())
338 gold_assert(shndx
== (*p
)->out_shndx());
339 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
340 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
345 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
348 // Output_segment_header methods.
350 Output_segment_headers::Output_segment_headers(
351 const Layout::Segment_list
& segment_list
)
352 : segment_list_(segment_list
)
354 this->set_current_data_size_for_child(this->do_size());
358 Output_segment_headers::do_write(Output_file
* of
)
360 switch (parameters
->size_and_endianness())
362 #ifdef HAVE_TARGET_32_LITTLE
363 case Parameters::TARGET_32_LITTLE
:
364 this->do_sized_write
<32, false>(of
);
367 #ifdef HAVE_TARGET_32_BIG
368 case Parameters::TARGET_32_BIG
:
369 this->do_sized_write
<32, true>(of
);
372 #ifdef HAVE_TARGET_64_LITTLE
373 case Parameters::TARGET_64_LITTLE
:
374 this->do_sized_write
<64, false>(of
);
377 #ifdef HAVE_TARGET_64_BIG
378 case Parameters::TARGET_64_BIG
:
379 this->do_sized_write
<64, true>(of
);
387 template<int size
, bool big_endian
>
389 Output_segment_headers::do_sized_write(Output_file
* of
)
391 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
392 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
393 gold_assert(all_phdrs_size
== this->data_size());
394 unsigned char* view
= of
->get_output_view(this->offset(),
396 unsigned char* v
= view
;
397 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
398 p
!= this->segment_list_
.end();
401 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
402 (*p
)->write_header(&ophdr
);
406 gold_assert(v
- view
== all_phdrs_size
);
408 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
412 Output_segment_headers::do_size() const
414 const int size
= parameters
->target().get_size();
417 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
419 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
423 return this->segment_list_
.size() * phdr_size
;
426 // Output_file_header methods.
428 Output_file_header::Output_file_header(const Target
* target
,
429 const Symbol_table
* symtab
,
430 const Output_segment_headers
* osh
)
433 segment_header_(osh
),
434 section_header_(NULL
),
437 this->set_data_size(this->do_size());
440 // Set the section table information for a file header.
443 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
444 const Output_section
* shstrtab
)
446 this->section_header_
= shdrs
;
447 this->shstrtab_
= shstrtab
;
450 // Write out the file header.
453 Output_file_header::do_write(Output_file
* of
)
455 gold_assert(this->offset() == 0);
457 switch (parameters
->size_and_endianness())
459 #ifdef HAVE_TARGET_32_LITTLE
460 case Parameters::TARGET_32_LITTLE
:
461 this->do_sized_write
<32, false>(of
);
464 #ifdef HAVE_TARGET_32_BIG
465 case Parameters::TARGET_32_BIG
:
466 this->do_sized_write
<32, true>(of
);
469 #ifdef HAVE_TARGET_64_LITTLE
470 case Parameters::TARGET_64_LITTLE
:
471 this->do_sized_write
<64, false>(of
);
474 #ifdef HAVE_TARGET_64_BIG
475 case Parameters::TARGET_64_BIG
:
476 this->do_sized_write
<64, true>(of
);
484 // Write out the file header with appropriate size and endianness.
486 template<int size
, bool big_endian
>
488 Output_file_header::do_sized_write(Output_file
* of
)
490 gold_assert(this->offset() == 0);
492 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
493 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
494 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
496 unsigned char e_ident
[elfcpp::EI_NIDENT
];
497 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
498 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
499 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
500 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
501 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
503 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
505 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
508 e_ident
[elfcpp::EI_DATA
] = (big_endian
509 ? elfcpp::ELFDATA2MSB
510 : elfcpp::ELFDATA2LSB
);
511 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
512 oehdr
.put_e_ident(e_ident
);
515 if (parameters
->options().relocatable())
516 e_type
= elfcpp::ET_REL
;
517 else if (parameters
->options().output_is_position_independent())
518 e_type
= elfcpp::ET_DYN
;
520 e_type
= elfcpp::ET_EXEC
;
521 oehdr
.put_e_type(e_type
);
523 oehdr
.put_e_machine(this->target_
->machine_code());
524 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
526 oehdr
.put_e_entry(this->entry
<size
>());
528 if (this->segment_header_
== NULL
)
529 oehdr
.put_e_phoff(0);
531 oehdr
.put_e_phoff(this->segment_header_
->offset());
533 oehdr
.put_e_shoff(this->section_header_
->offset());
534 oehdr
.put_e_flags(this->target_
->processor_specific_flags());
535 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
537 if (this->segment_header_
== NULL
)
539 oehdr
.put_e_phentsize(0);
540 oehdr
.put_e_phnum(0);
544 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
545 size_t phnum
= (this->segment_header_
->data_size()
546 / elfcpp::Elf_sizes
<size
>::phdr_size
);
547 if (phnum
> elfcpp::PN_XNUM
)
548 phnum
= elfcpp::PN_XNUM
;
549 oehdr
.put_e_phnum(phnum
);
552 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
553 size_t section_count
= (this->section_header_
->data_size()
554 / elfcpp::Elf_sizes
<size
>::shdr_size
);
556 if (section_count
< elfcpp::SHN_LORESERVE
)
557 oehdr
.put_e_shnum(this->section_header_
->data_size()
558 / elfcpp::Elf_sizes
<size
>::shdr_size
);
560 oehdr
.put_e_shnum(0);
562 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
563 if (shstrndx
< elfcpp::SHN_LORESERVE
)
564 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
566 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
568 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
569 // the e_ident field.
570 parameters
->target().adjust_elf_header(view
, ehdr_size
);
572 of
->write_output_view(0, ehdr_size
, view
);
575 // Return the value to use for the entry address.
578 typename
elfcpp::Elf_types
<size
>::Elf_Addr
579 Output_file_header::entry()
581 const bool should_issue_warning
= (parameters
->options().entry() != NULL
582 && !parameters
->options().relocatable()
583 && !parameters
->options().shared());
584 const char* entry
= parameters
->entry();
585 Symbol
* sym
= this->symtab_
->lookup(entry
);
587 typename Sized_symbol
<size
>::Value_type v
;
590 Sized_symbol
<size
>* ssym
;
591 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
592 if (!ssym
->is_defined() && should_issue_warning
)
593 gold_warning("entry symbol '%s' exists but is not defined", entry
);
598 // We couldn't find the entry symbol. See if we can parse it as
599 // a number. This supports, e.g., -e 0x1000.
601 v
= strtoull(entry
, &endptr
, 0);
604 if (should_issue_warning
)
605 gold_warning("cannot find entry symbol '%s'", entry
);
613 // Compute the current data size.
616 Output_file_header::do_size() const
618 const int size
= parameters
->target().get_size();
620 return elfcpp::Elf_sizes
<32>::ehdr_size
;
622 return elfcpp::Elf_sizes
<64>::ehdr_size
;
627 // Output_data_const methods.
630 Output_data_const::do_write(Output_file
* of
)
632 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
635 // Output_data_const_buffer methods.
638 Output_data_const_buffer::do_write(Output_file
* of
)
640 of
->write(this->offset(), this->p_
, this->data_size());
643 // Output_section_data methods.
645 // Record the output section, and set the entry size and such.
648 Output_section_data::set_output_section(Output_section
* os
)
650 gold_assert(this->output_section_
== NULL
);
651 this->output_section_
= os
;
652 this->do_adjust_output_section(os
);
655 // Return the section index of the output section.
658 Output_section_data::do_out_shndx() const
660 gold_assert(this->output_section_
!= NULL
);
661 return this->output_section_
->out_shndx();
664 // Set the alignment, which means we may need to update the alignment
665 // of the output section.
668 Output_section_data::set_addralign(uint64_t addralign
)
670 this->addralign_
= addralign
;
671 if (this->output_section_
!= NULL
672 && this->output_section_
->addralign() < addralign
)
673 this->output_section_
->set_addralign(addralign
);
676 // Output_data_strtab methods.
678 // Set the final data size.
681 Output_data_strtab::set_final_data_size()
683 this->strtab_
->set_string_offsets();
684 this->set_data_size(this->strtab_
->get_strtab_size());
687 // Write out a string table.
690 Output_data_strtab::do_write(Output_file
* of
)
692 this->strtab_
->write(of
, this->offset());
695 // Output_reloc methods.
697 // A reloc against a global symbol.
699 template<bool dynamic
, int size
, bool big_endian
>
700 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
707 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
708 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
709 is_section_symbol_(false), shndx_(INVALID_CODE
)
711 // this->type_ is a bitfield; make sure TYPE fits.
712 gold_assert(this->type_
== type
);
713 this->u1_
.gsym
= gsym
;
716 this->set_needs_dynsym_index();
719 template<bool dynamic
, int size
, bool big_endian
>
720 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
723 Sized_relobj
<size
, big_endian
>* relobj
,
728 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
729 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
730 is_section_symbol_(false), shndx_(shndx
)
732 gold_assert(shndx
!= INVALID_CODE
);
733 // this->type_ is a bitfield; make sure TYPE fits.
734 gold_assert(this->type_
== type
);
735 this->u1_
.gsym
= gsym
;
736 this->u2_
.relobj
= relobj
;
738 this->set_needs_dynsym_index();
741 // A reloc against a local symbol.
743 template<bool dynamic
, int size
, bool big_endian
>
744 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
745 Sized_relobj
<size
, big_endian
>* relobj
,
746 unsigned int local_sym_index
,
752 bool is_section_symbol
)
753 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
754 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
755 is_section_symbol_(is_section_symbol
), shndx_(INVALID_CODE
)
757 gold_assert(local_sym_index
!= GSYM_CODE
758 && local_sym_index
!= INVALID_CODE
);
759 // this->type_ is a bitfield; make sure TYPE fits.
760 gold_assert(this->type_
== type
);
761 this->u1_
.relobj
= relobj
;
764 this->set_needs_dynsym_index();
767 template<bool dynamic
, int size
, bool big_endian
>
768 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
769 Sized_relobj
<size
, big_endian
>* relobj
,
770 unsigned int local_sym_index
,
776 bool is_section_symbol
)
777 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
778 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
779 is_section_symbol_(is_section_symbol
), shndx_(shndx
)
781 gold_assert(local_sym_index
!= GSYM_CODE
782 && local_sym_index
!= INVALID_CODE
);
783 gold_assert(shndx
!= INVALID_CODE
);
784 // this->type_ is a bitfield; make sure TYPE fits.
785 gold_assert(this->type_
== type
);
786 this->u1_
.relobj
= relobj
;
787 this->u2_
.relobj
= relobj
;
789 this->set_needs_dynsym_index();
792 // A reloc against the STT_SECTION symbol of an output section.
794 template<bool dynamic
, int size
, bool big_endian
>
795 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
800 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
801 is_relative_(false), is_symbolless_(false),
802 is_section_symbol_(true), shndx_(INVALID_CODE
)
804 // this->type_ is a bitfield; make sure TYPE fits.
805 gold_assert(this->type_
== type
);
809 this->set_needs_dynsym_index();
811 os
->set_needs_symtab_index();
814 template<bool dynamic
, int size
, bool big_endian
>
815 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
818 Sized_relobj
<size
, big_endian
>* relobj
,
821 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
822 is_relative_(false), is_symbolless_(false),
823 is_section_symbol_(true), shndx_(shndx
)
825 gold_assert(shndx
!= INVALID_CODE
);
826 // this->type_ is a bitfield; make sure TYPE fits.
827 gold_assert(this->type_
== type
);
829 this->u2_
.relobj
= relobj
;
831 this->set_needs_dynsym_index();
833 os
->set_needs_symtab_index();
836 // An absolute relocation.
838 template<bool dynamic
, int size
, bool big_endian
>
839 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
843 : address_(address
), local_sym_index_(0), type_(type
),
844 is_relative_(false), is_symbolless_(false),
845 is_section_symbol_(false), shndx_(INVALID_CODE
)
847 // this->type_ is a bitfield; make sure TYPE fits.
848 gold_assert(this->type_
== type
);
849 this->u1_
.relobj
= NULL
;
853 template<bool dynamic
, int size
, bool big_endian
>
854 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
856 Sized_relobj
<size
, big_endian
>* relobj
,
859 : address_(address
), local_sym_index_(0), type_(type
),
860 is_relative_(false), is_symbolless_(false),
861 is_section_symbol_(false), shndx_(shndx
)
863 gold_assert(shndx
!= INVALID_CODE
);
864 // this->type_ is a bitfield; make sure TYPE fits.
865 gold_assert(this->type_
== type
);
866 this->u1_
.relobj
= NULL
;
867 this->u2_
.relobj
= relobj
;
870 // A target specific relocation.
872 template<bool dynamic
, int size
, bool big_endian
>
873 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
878 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
879 is_relative_(false), is_symbolless_(false),
880 is_section_symbol_(false), shndx_(INVALID_CODE
)
882 // this->type_ is a bitfield; make sure TYPE fits.
883 gold_assert(this->type_
== type
);
888 template<bool dynamic
, int size
, bool big_endian
>
889 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
892 Sized_relobj
<size
, big_endian
>* relobj
,
895 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
896 is_relative_(false), is_symbolless_(false),
897 is_section_symbol_(false), shndx_(shndx
)
899 gold_assert(shndx
!= INVALID_CODE
);
900 // this->type_ is a bitfield; make sure TYPE fits.
901 gold_assert(this->type_
== type
);
903 this->u2_
.relobj
= relobj
;
906 // Record that we need a dynamic symbol index for this relocation.
908 template<bool dynamic
, int size
, bool big_endian
>
910 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
911 set_needs_dynsym_index()
913 if (this->is_symbolless_
)
915 switch (this->local_sym_index_
)
921 this->u1_
.gsym
->set_needs_dynsym_entry();
925 this->u1_
.os
->set_needs_dynsym_index();
929 // The target must take care of this if necessary.
937 const unsigned int lsi
= this->local_sym_index_
;
938 Sized_relobj_file
<size
, big_endian
>* relobj
=
939 this->u1_
.relobj
->sized_relobj();
940 gold_assert(relobj
!= NULL
);
941 if (!this->is_section_symbol_
)
942 relobj
->set_needs_output_dynsym_entry(lsi
);
944 relobj
->output_section(lsi
)->set_needs_dynsym_index();
950 // Get the symbol index of a relocation.
952 template<bool dynamic
, int size
, bool big_endian
>
954 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
958 if (this->is_symbolless_
)
960 switch (this->local_sym_index_
)
966 if (this->u1_
.gsym
== NULL
)
969 index
= this->u1_
.gsym
->dynsym_index();
971 index
= this->u1_
.gsym
->symtab_index();
976 index
= this->u1_
.os
->dynsym_index();
978 index
= this->u1_
.os
->symtab_index();
982 index
= parameters
->target().reloc_symbol_index(this->u1_
.arg
,
987 // Relocations without symbols use a symbol index of 0.
993 const unsigned int lsi
= this->local_sym_index_
;
994 Sized_relobj_file
<size
, big_endian
>* relobj
=
995 this->u1_
.relobj
->sized_relobj();
996 gold_assert(relobj
!= NULL
);
997 if (!this->is_section_symbol_
)
1000 index
= relobj
->dynsym_index(lsi
);
1002 index
= relobj
->symtab_index(lsi
);
1006 Output_section
* os
= relobj
->output_section(lsi
);
1007 gold_assert(os
!= NULL
);
1009 index
= os
->dynsym_index();
1011 index
= os
->symtab_index();
1016 gold_assert(index
!= -1U);
1020 // For a local section symbol, get the address of the offset ADDEND
1021 // within the input section.
1023 template<bool dynamic
, int size
, bool big_endian
>
1024 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1025 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1026 local_section_offset(Addend addend
) const
1028 gold_assert(this->local_sym_index_
!= GSYM_CODE
1029 && this->local_sym_index_
!= SECTION_CODE
1030 && this->local_sym_index_
!= TARGET_CODE
1031 && this->local_sym_index_
!= INVALID_CODE
1032 && this->local_sym_index_
!= 0
1033 && this->is_section_symbol_
);
1034 const unsigned int lsi
= this->local_sym_index_
;
1035 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
1036 gold_assert(os
!= NULL
);
1037 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
1038 if (offset
!= invalid_address
)
1039 return offset
+ addend
;
1040 // This is a merge section.
1041 Sized_relobj_file
<size
, big_endian
>* relobj
=
1042 this->u1_
.relobj
->sized_relobj();
1043 gold_assert(relobj
!= NULL
);
1044 offset
= os
->output_address(relobj
, lsi
, addend
);
1045 gold_assert(offset
!= invalid_address
);
1049 // Get the output address of a relocation.
1051 template<bool dynamic
, int size
, bool big_endian
>
1052 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1053 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
1055 Address address
= this->address_
;
1056 if (this->shndx_
!= INVALID_CODE
)
1058 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
1059 gold_assert(os
!= NULL
);
1060 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
1061 if (off
!= invalid_address
)
1062 address
+= os
->address() + off
;
1065 Sized_relobj_file
<size
, big_endian
>* relobj
=
1066 this->u2_
.relobj
->sized_relobj();
1067 gold_assert(relobj
!= NULL
);
1068 address
= os
->output_address(relobj
, this->shndx_
, address
);
1069 gold_assert(address
!= invalid_address
);
1072 else if (this->u2_
.od
!= NULL
)
1073 address
+= this->u2_
.od
->address();
1077 // Write out the offset and info fields of a Rel or Rela relocation
1080 template<bool dynamic
, int size
, bool big_endian
>
1081 template<typename Write_rel
>
1083 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
1084 Write_rel
* wr
) const
1086 wr
->put_r_offset(this->get_address());
1087 unsigned int sym_index
= this->get_symbol_index();
1088 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
1091 // Write out a Rel relocation.
1093 template<bool dynamic
, int size
, bool big_endian
>
1095 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
1096 unsigned char* pov
) const
1098 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
1099 this->write_rel(&orel
);
1102 // Get the value of the symbol referred to by a Rel relocation.
1104 template<bool dynamic
, int size
, bool big_endian
>
1105 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1106 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
1107 Addend addend
) const
1109 if (this->local_sym_index_
== GSYM_CODE
)
1111 const Sized_symbol
<size
>* sym
;
1112 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
1113 return sym
->value() + addend
;
1115 gold_assert(this->local_sym_index_
!= SECTION_CODE
1116 && this->local_sym_index_
!= TARGET_CODE
1117 && this->local_sym_index_
!= INVALID_CODE
1118 && this->local_sym_index_
!= 0
1119 && !this->is_section_symbol_
);
1120 const unsigned int lsi
= this->local_sym_index_
;
1121 Sized_relobj_file
<size
, big_endian
>* relobj
=
1122 this->u1_
.relobj
->sized_relobj();
1123 gold_assert(relobj
!= NULL
);
1124 const Symbol_value
<size
>* symval
= relobj
->local_symbol(lsi
);
1125 return symval
->value(relobj
, addend
);
1128 // Reloc comparison. This function sorts the dynamic relocs for the
1129 // benefit of the dynamic linker. First we sort all relative relocs
1130 // to the front. Among relative relocs, we sort by output address.
1131 // Among non-relative relocs, we sort by symbol index, then by output
1134 template<bool dynamic
, int size
, bool big_endian
>
1136 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1137 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1140 if (this->is_relative_
)
1142 if (!r2
.is_relative_
)
1144 // Otherwise sort by reloc address below.
1146 else if (r2
.is_relative_
)
1150 unsigned int sym1
= this->get_symbol_index();
1151 unsigned int sym2
= r2
.get_symbol_index();
1154 else if (sym1
> sym2
)
1156 // Otherwise sort by reloc address.
1159 section_offset_type addr1
= this->get_address();
1160 section_offset_type addr2
= r2
.get_address();
1163 else if (addr1
> addr2
)
1166 // Final tie breaker, in order to generate the same output on any
1167 // host: reloc type.
1168 unsigned int type1
= this->type_
;
1169 unsigned int type2
= r2
.type_
;
1172 else if (type1
> type2
)
1175 // These relocs appear to be exactly the same.
1179 // Write out a Rela relocation.
1181 template<bool dynamic
, int size
, bool big_endian
>
1183 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
1184 unsigned char* pov
) const
1186 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
1187 this->rel_
.write_rel(&orel
);
1188 Addend addend
= this->addend_
;
1189 if (this->rel_
.is_target_specific())
1190 addend
= parameters
->target().reloc_addend(this->rel_
.target_arg(),
1191 this->rel_
.type(), addend
);
1192 else if (this->rel_
.is_symbolless())
1193 addend
= this->rel_
.symbol_value(addend
);
1194 else if (this->rel_
.is_local_section_symbol())
1195 addend
= this->rel_
.local_section_offset(addend
);
1196 orel
.put_r_addend(addend
);
1199 // Output_data_reloc_base methods.
1201 // Adjust the output section.
1203 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1205 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
1206 ::do_adjust_output_section(Output_section
* os
)
1208 if (sh_type
== elfcpp::SHT_REL
)
1209 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1210 else if (sh_type
== elfcpp::SHT_RELA
)
1211 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1215 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1216 // static link. The backends will generate a dynamic reloc section
1217 // to hold this. In that case we don't want to link to the dynsym
1218 // section, because there isn't one.
1220 os
->set_should_link_to_symtab();
1221 else if (parameters
->doing_static_link())
1224 os
->set_should_link_to_dynsym();
1227 // Write out relocation data.
1229 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1231 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1234 const off_t off
= this->offset();
1235 const off_t oview_size
= this->data_size();
1236 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1238 if (this->sort_relocs())
1240 gold_assert(dynamic
);
1241 std::sort(this->relocs_
.begin(), this->relocs_
.end(),
1242 Sort_relocs_comparison());
1245 unsigned char* pov
= oview
;
1246 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
1247 p
!= this->relocs_
.end();
1254 gold_assert(pov
- oview
== oview_size
);
1256 of
->write_output_view(off
, oview_size
, oview
);
1258 // We no longer need the relocation entries.
1259 this->relocs_
.clear();
1262 // Class Output_relocatable_relocs.
1264 template<int sh_type
, int size
, bool big_endian
>
1266 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1268 this->set_data_size(this->rr_
->output_reloc_count()
1269 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1272 // class Output_data_group.
1274 template<int size
, bool big_endian
>
1275 Output_data_group
<size
, big_endian
>::Output_data_group(
1276 Sized_relobj_file
<size
, big_endian
>* relobj
,
1277 section_size_type entry_count
,
1278 elfcpp::Elf_Word flags
,
1279 std::vector
<unsigned int>* input_shndxes
)
1280 : Output_section_data(entry_count
* 4, 4, false),
1284 this->input_shndxes_
.swap(*input_shndxes
);
1287 // Write out the section group, which means translating the section
1288 // indexes to apply to the output file.
1290 template<int size
, bool big_endian
>
1292 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1294 const off_t off
= this->offset();
1295 const section_size_type oview_size
=
1296 convert_to_section_size_type(this->data_size());
1297 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1299 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1300 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1303 for (std::vector
<unsigned int>::const_iterator p
=
1304 this->input_shndxes_
.begin();
1305 p
!= this->input_shndxes_
.end();
1308 Output_section
* os
= this->relobj_
->output_section(*p
);
1310 unsigned int output_shndx
;
1312 output_shndx
= os
->out_shndx();
1315 this->relobj_
->error(_("section group retained but "
1316 "group element discarded"));
1320 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1323 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1324 gold_assert(wrote
== oview_size
);
1326 of
->write_output_view(off
, oview_size
, oview
);
1328 // We no longer need this information.
1329 this->input_shndxes_
.clear();
1332 // Output_data_got::Got_entry methods.
1334 // Write out the entry.
1336 template<int size
, bool big_endian
>
1338 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1342 switch (this->local_sym_index_
)
1346 // If the symbol is resolved locally, we need to write out the
1347 // link-time value, which will be relocated dynamically by a
1348 // RELATIVE relocation.
1349 Symbol
* gsym
= this->u_
.gsym
;
1350 if (this->use_plt_offset_
&& gsym
->has_plt_offset())
1351 val
= (parameters
->target().plt_address_for_global(gsym
)
1352 + gsym
->plt_offset());
1355 Sized_symbol
<size
>* sgsym
;
1356 // This cast is a bit ugly. We don't want to put a
1357 // virtual method in Symbol, because we want Symbol to be
1358 // as small as possible.
1359 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1360 val
= sgsym
->value();
1366 val
= this->u_
.constant
;
1370 // If we're doing an incremental update, don't touch this GOT entry.
1371 if (parameters
->incremental_update())
1373 val
= this->u_
.constant
;
1378 const Sized_relobj_file
<size
, big_endian
>* object
= this->u_
.object
;
1379 const unsigned int lsi
= this->local_sym_index_
;
1380 const Symbol_value
<size
>* symval
= object
->local_symbol(lsi
);
1381 if (!this->use_plt_offset_
)
1382 val
= symval
->value(this->u_
.object
, 0);
1385 uint64_t plt_address
=
1386 parameters
->target().plt_address_for_local(object
, lsi
);
1387 val
= plt_address
+ object
->local_plt_offset(lsi
);
1393 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1396 // Output_data_got methods.
1398 // Add an entry for a global symbol to the GOT. This returns true if
1399 // this is a new GOT entry, false if the symbol already had a GOT
1402 template<int size
, bool big_endian
>
1404 Output_data_got
<size
, big_endian
>::add_global(
1406 unsigned int got_type
)
1408 if (gsym
->has_got_offset(got_type
))
1411 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, false));
1412 gsym
->set_got_offset(got_type
, got_offset
);
1416 // Like add_global, but use the PLT offset.
1418 template<int size
, bool big_endian
>
1420 Output_data_got
<size
, big_endian
>::add_global_plt(Symbol
* gsym
,
1421 unsigned int got_type
)
1423 if (gsym
->has_got_offset(got_type
))
1426 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, true));
1427 gsym
->set_got_offset(got_type
, got_offset
);
1431 // Add an entry for a global symbol to the GOT, and add a dynamic
1432 // relocation of type R_TYPE for the GOT entry.
1434 template<int size
, bool big_endian
>
1436 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1438 unsigned int got_type
,
1440 unsigned int r_type
)
1442 if (gsym
->has_got_offset(got_type
))
1445 unsigned int got_offset
= this->add_got_entry(Got_entry());
1446 gsym
->set_got_offset(got_type
, got_offset
);
1447 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1450 template<int size
, bool big_endian
>
1452 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1454 unsigned int got_type
,
1456 unsigned int r_type
)
1458 if (gsym
->has_got_offset(got_type
))
1461 unsigned int got_offset
= this->add_got_entry(Got_entry());
1462 gsym
->set_got_offset(got_type
, got_offset
);
1463 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1466 // Add a pair of entries for a global symbol to the GOT, and add
1467 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1468 // If R_TYPE_2 == 0, add the second entry with no relocation.
1469 template<int size
, bool big_endian
>
1471 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1473 unsigned int got_type
,
1475 unsigned int r_type_1
,
1476 unsigned int r_type_2
)
1478 if (gsym
->has_got_offset(got_type
))
1481 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1482 gsym
->set_got_offset(got_type
, got_offset
);
1483 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1486 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
+ size
/ 8);
1489 template<int size
, bool big_endian
>
1491 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1493 unsigned int got_type
,
1495 unsigned int r_type_1
,
1496 unsigned int r_type_2
)
1498 if (gsym
->has_got_offset(got_type
))
1501 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1502 gsym
->set_got_offset(got_type
, got_offset
);
1503 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1506 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
+ size
/ 8, 0);
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
1513 template<int size
, bool big_endian
>
1515 Output_data_got
<size
, big_endian
>::add_local(
1516 Sized_relobj_file
<size
, big_endian
>* object
,
1517 unsigned int symndx
,
1518 unsigned int got_type
)
1520 if (object
->local_has_got_offset(symndx
, got_type
))
1523 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1525 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1529 // Like add_local, but use the PLT offset.
1531 template<int size
, bool big_endian
>
1533 Output_data_got
<size
, big_endian
>::add_local_plt(
1534 Sized_relobj_file
<size
, big_endian
>* object
,
1535 unsigned int symndx
,
1536 unsigned int got_type
)
1538 if (object
->local_has_got_offset(symndx
, got_type
))
1541 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1543 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
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.
1550 template<int size
, bool big_endian
>
1552 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1553 Sized_relobj_file
<size
, big_endian
>* object
,
1554 unsigned int symndx
,
1555 unsigned int got_type
,
1557 unsigned int r_type
)
1559 if (object
->local_has_got_offset(symndx
, got_type
))
1562 unsigned int got_offset
= this->add_got_entry(Got_entry());
1563 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1564 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1567 template<int size
, bool big_endian
>
1569 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1570 Sized_relobj_file
<size
, big_endian
>* object
,
1571 unsigned int symndx
,
1572 unsigned int got_type
,
1574 unsigned int r_type
)
1576 if (object
->local_has_got_offset(symndx
, got_type
))
1579 unsigned int got_offset
= this->add_got_entry(Got_entry());
1580 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1581 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1584 // Add a pair of entries for a local symbol to the GOT, and add
1585 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1586 // If R_TYPE_2 == 0, add the second entry with no relocation.
1587 template<int size
, bool big_endian
>
1589 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1590 Sized_relobj_file
<size
, big_endian
>* object
,
1591 unsigned int symndx
,
1593 unsigned int got_type
,
1595 unsigned int r_type_1
,
1596 unsigned int r_type_2
)
1598 if (object
->local_has_got_offset(symndx
, got_type
))
1601 unsigned int got_offset
=
1602 this->add_got_entry_pair(Got_entry(),
1603 Got_entry(object
, symndx
, false));
1604 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1605 Output_section
* os
= object
->output_section(shndx
);
1606 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1609 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
+ size
/ 8);
1612 template<int size
, bool big_endian
>
1614 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1615 Sized_relobj_file
<size
, big_endian
>* object
,
1616 unsigned int symndx
,
1618 unsigned int got_type
,
1620 unsigned int r_type_1
,
1621 unsigned int r_type_2
)
1623 if (object
->local_has_got_offset(symndx
, got_type
))
1626 unsigned int got_offset
=
1627 this->add_got_entry_pair(Got_entry(),
1628 Got_entry(object
, symndx
, false));
1629 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1630 Output_section
* os
= object
->output_section(shndx
);
1631 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1634 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
+ size
/ 8, 0);
1637 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1639 template<int size
, bool big_endian
>
1641 Output_data_got
<size
, big_endian
>::reserve_local(
1643 Sized_relobj
<size
, big_endian
>* object
,
1644 unsigned int sym_index
,
1645 unsigned int got_type
)
1647 this->reserve_slot(i
);
1648 object
->set_local_got_offset(sym_index
, got_type
, this->got_offset(i
));
1651 // Reserve a slot in the GOT for a global symbol.
1653 template<int size
, bool big_endian
>
1655 Output_data_got
<size
, big_endian
>::reserve_global(
1658 unsigned int got_type
)
1660 this->reserve_slot(i
);
1661 gsym
->set_got_offset(got_type
, this->got_offset(i
));
1664 // Write out the GOT.
1666 template<int size
, bool big_endian
>
1668 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1670 const int add
= size
/ 8;
1672 const off_t off
= this->offset();
1673 const off_t oview_size
= this->data_size();
1674 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1676 unsigned char* pov
= oview
;
1677 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1678 p
!= this->entries_
.end();
1685 gold_assert(pov
- oview
== oview_size
);
1687 of
->write_output_view(off
, oview_size
, oview
);
1689 // We no longer need the GOT entries.
1690 this->entries_
.clear();
1693 // Create a new GOT entry and return its offset.
1695 template<int size
, bool big_endian
>
1697 Output_data_got
<size
, big_endian
>::add_got_entry(Got_entry got_entry
)
1699 if (!this->is_data_size_valid())
1701 this->entries_
.push_back(got_entry
);
1702 this->set_got_size();
1703 return this->last_got_offset();
1707 // For an incremental update, find an available slot.
1708 off_t got_offset
= this->free_list_
.allocate(size
/ 8, size
/ 8, 0);
1709 if (got_offset
== -1)
1710 gold_fallback(_("out of patch space (GOT);"
1711 " relink with --incremental-full"));
1712 unsigned int got_index
= got_offset
/ (size
/ 8);
1713 gold_assert(got_index
< this->entries_
.size());
1714 this->entries_
[got_index
] = got_entry
;
1715 return static_cast<unsigned int>(got_offset
);
1719 // Create a pair of new GOT entries and return the offset of the first.
1721 template<int size
, bool big_endian
>
1723 Output_data_got
<size
, big_endian
>::add_got_entry_pair(Got_entry got_entry_1
,
1724 Got_entry got_entry_2
)
1726 if (!this->is_data_size_valid())
1728 unsigned int got_offset
;
1729 this->entries_
.push_back(got_entry_1
);
1730 got_offset
= this->last_got_offset();
1731 this->entries_
.push_back(got_entry_2
);
1732 this->set_got_size();
1737 // For an incremental update, find an available pair of slots.
1738 off_t got_offset
= this->free_list_
.allocate(2 * size
/ 8, size
/ 8, 0);
1739 if (got_offset
== -1)
1740 gold_fallback(_("out of patch space (GOT);"
1741 " relink with --incremental-full"));
1742 unsigned int got_index
= got_offset
/ (size
/ 8);
1743 gold_assert(got_index
< this->entries_
.size());
1744 this->entries_
[got_index
] = got_entry_1
;
1745 this->entries_
[got_index
+ 1] = got_entry_2
;
1746 return static_cast<unsigned int>(got_offset
);
1750 // Output_data_dynamic::Dynamic_entry methods.
1752 // Write out the entry.
1754 template<int size
, bool big_endian
>
1756 Output_data_dynamic::Dynamic_entry::write(
1758 const Stringpool
* pool
) const
1760 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1761 switch (this->offset_
)
1763 case DYNAMIC_NUMBER
:
1767 case DYNAMIC_SECTION_SIZE
:
1768 val
= this->u_
.od
->data_size();
1769 if (this->od2
!= NULL
)
1770 val
+= this->od2
->data_size();
1773 case DYNAMIC_SYMBOL
:
1775 const Sized_symbol
<size
>* s
=
1776 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1781 case DYNAMIC_STRING
:
1782 val
= pool
->get_offset(this->u_
.str
);
1786 val
= this->u_
.od
->address() + this->offset_
;
1790 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1791 dw
.put_d_tag(this->tag_
);
1795 // Output_data_dynamic methods.
1797 // Adjust the output section to set the entry size.
1800 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1802 if (parameters
->target().get_size() == 32)
1803 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1804 else if (parameters
->target().get_size() == 64)
1805 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1810 // Set the final data size.
1813 Output_data_dynamic::set_final_data_size()
1815 // Add the terminating entry if it hasn't been added.
1816 // Because of relaxation, we can run this multiple times.
1817 if (this->entries_
.empty() || this->entries_
.back().tag() != elfcpp::DT_NULL
)
1819 int extra
= parameters
->options().spare_dynamic_tags();
1820 for (int i
= 0; i
< extra
; ++i
)
1821 this->add_constant(elfcpp::DT_NULL
, 0);
1822 this->add_constant(elfcpp::DT_NULL
, 0);
1826 if (parameters
->target().get_size() == 32)
1827 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1828 else if (parameters
->target().get_size() == 64)
1829 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1832 this->set_data_size(this->entries_
.size() * dyn_size
);
1835 // Write out the dynamic entries.
1838 Output_data_dynamic::do_write(Output_file
* of
)
1840 switch (parameters
->size_and_endianness())
1842 #ifdef HAVE_TARGET_32_LITTLE
1843 case Parameters::TARGET_32_LITTLE
:
1844 this->sized_write
<32, false>(of
);
1847 #ifdef HAVE_TARGET_32_BIG
1848 case Parameters::TARGET_32_BIG
:
1849 this->sized_write
<32, true>(of
);
1852 #ifdef HAVE_TARGET_64_LITTLE
1853 case Parameters::TARGET_64_LITTLE
:
1854 this->sized_write
<64, false>(of
);
1857 #ifdef HAVE_TARGET_64_BIG
1858 case Parameters::TARGET_64_BIG
:
1859 this->sized_write
<64, true>(of
);
1867 template<int size
, bool big_endian
>
1869 Output_data_dynamic::sized_write(Output_file
* of
)
1871 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1873 const off_t offset
= this->offset();
1874 const off_t oview_size
= this->data_size();
1875 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1877 unsigned char* pov
= oview
;
1878 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1879 p
!= this->entries_
.end();
1882 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1886 gold_assert(pov
- oview
== oview_size
);
1888 of
->write_output_view(offset
, oview_size
, oview
);
1890 // We no longer need the dynamic entries.
1891 this->entries_
.clear();
1894 // Class Output_symtab_xindex.
1897 Output_symtab_xindex::do_write(Output_file
* of
)
1899 const off_t offset
= this->offset();
1900 const off_t oview_size
= this->data_size();
1901 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1903 memset(oview
, 0, oview_size
);
1905 if (parameters
->target().is_big_endian())
1906 this->endian_do_write
<true>(oview
);
1908 this->endian_do_write
<false>(oview
);
1910 of
->write_output_view(offset
, oview_size
, oview
);
1912 // We no longer need the data.
1913 this->entries_
.clear();
1916 template<bool big_endian
>
1918 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1920 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1921 p
!= this->entries_
.end();
1924 unsigned int symndx
= p
->first
;
1925 gold_assert(symndx
* 4 < this->data_size());
1926 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ symndx
* 4, p
->second
);
1930 // Output_fill_debug_info methods.
1932 // Return the minimum size needed for a dummy compilation unit header.
1935 Output_fill_debug_info::do_minimum_hole_size() const
1937 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1939 const size_t len
= 4 + 2 + 4 + 1;
1940 // For type units, add type_signature, type_offset.
1941 if (this->is_debug_types_
)
1946 // Write a dummy compilation unit header to fill a hole in the
1947 // .debug_info or .debug_types section.
1950 Output_fill_debug_info::do_write(Output_file
* of
, off_t off
, size_t len
) const
1952 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_info(%08lx, %08lx)", off
, len
);
1954 gold_assert(len
>= this->do_minimum_hole_size());
1956 unsigned char* const oview
= of
->get_output_view(off
, len
);
1957 unsigned char* pov
= oview
;
1959 // Write header fields: unit_length, version, debug_abbrev_offset,
1961 if (this->is_big_endian())
1963 elfcpp::Swap
<32, true>::writeval(pov
, len
- 4);
1964 elfcpp::Swap
<16, true>::writeval(pov
+ 4, this->version
);
1965 elfcpp::Swap
<32, true>::writeval(pov
+ 6, 0);
1969 elfcpp::Swap
<32, false>::writeval(pov
, len
- 4);
1970 elfcpp::Swap
<16, false>::writeval(pov
+ 4, this->version
);
1971 elfcpp::Swap
<32, false>::writeval(pov
+ 6, 0);
1976 // For type units, the additional header fields -- type_signature,
1977 // type_offset -- can be filled with zeroes.
1979 // Fill the remainder of the free space with zeroes. The first
1980 // zero should tell the consumer there are no DIEs to read in this
1981 // compilation unit.
1982 if (pov
< oview
+ len
)
1983 memset(pov
, 0, oview
+ len
- pov
);
1985 of
->write_output_view(off
, len
, oview
);
1988 // Output_fill_debug_line methods.
1990 // Return the minimum size needed for a dummy line number program header.
1993 Output_fill_debug_line::do_minimum_hole_size() const
1995 // Line number program header fields: unit_length, version, header_length,
1996 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1997 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1998 const size_t len
= 4 + 2 + 4 + this->header_length
;
2002 // Write a dummy line number program header to fill a hole in the
2003 // .debug_line section.
2006 Output_fill_debug_line::do_write(Output_file
* of
, off_t off
, size_t len
) const
2008 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_line(%08lx, %08lx)", off
, len
);
2010 gold_assert(len
>= this->do_minimum_hole_size());
2012 unsigned char* const oview
= of
->get_output_view(off
, len
);
2013 unsigned char* pov
= oview
;
2015 // Write header fields: unit_length, version, header_length,
2016 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2017 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2018 // We set the header_length field to cover the entire hole, so the
2019 // line number program is empty.
2020 if (this->is_big_endian())
2022 elfcpp::Swap
<32, true>::writeval(pov
, len
- 4);
2023 elfcpp::Swap
<16, true>::writeval(pov
+ 4, this->version
);
2024 elfcpp::Swap
<32, true>::writeval(pov
+ 6, len
- (4 + 2 + 4));
2028 elfcpp::Swap
<32, false>::writeval(pov
, len
- 4);
2029 elfcpp::Swap
<16, false>::writeval(pov
+ 4, this->version
);
2030 elfcpp::Swap
<32, false>::writeval(pov
+ 6, len
- (4 + 2 + 4));
2033 *pov
++ = 1; // minimum_instruction_length
2034 *pov
++ = 0; // default_is_stmt
2035 *pov
++ = 0; // line_base
2036 *pov
++ = 5; // line_range
2037 *pov
++ = 13; // opcode_base
2038 *pov
++ = 0; // standard_opcode_lengths[1]
2039 *pov
++ = 1; // standard_opcode_lengths[2]
2040 *pov
++ = 1; // standard_opcode_lengths[3]
2041 *pov
++ = 1; // standard_opcode_lengths[4]
2042 *pov
++ = 1; // standard_opcode_lengths[5]
2043 *pov
++ = 0; // standard_opcode_lengths[6]
2044 *pov
++ = 0; // standard_opcode_lengths[7]
2045 *pov
++ = 0; // standard_opcode_lengths[8]
2046 *pov
++ = 1; // standard_opcode_lengths[9]
2047 *pov
++ = 0; // standard_opcode_lengths[10]
2048 *pov
++ = 0; // standard_opcode_lengths[11]
2049 *pov
++ = 1; // standard_opcode_lengths[12]
2050 *pov
++ = 0; // include_directories (empty)
2051 *pov
++ = 0; // filenames (empty)
2053 // Some consumers don't check the header_length field, and simply
2054 // start reading the line number program immediately following the
2055 // header. For those consumers, we fill the remainder of the free
2056 // space with DW_LNS_set_basic_block opcodes. These are effectively
2057 // no-ops: the resulting line table program will not create any rows.
2058 if (pov
< oview
+ len
)
2059 memset(pov
, elfcpp::DW_LNS_set_basic_block
, oview
+ len
- pov
);
2061 of
->write_output_view(off
, len
, oview
);
2064 // Output_section::Input_section methods.
2066 // Return the current data size. For an input section we store the size here.
2067 // For an Output_section_data, we have to ask it for the size.
2070 Output_section::Input_section::current_data_size() const
2072 if (this->is_input_section())
2073 return this->u1_
.data_size
;
2076 this->u2_
.posd
->pre_finalize_data_size();
2077 return this->u2_
.posd
->current_data_size();
2081 // Return the data size. For an input section we store the size here.
2082 // For an Output_section_data, we have to ask it for the size.
2085 Output_section::Input_section::data_size() const
2087 if (this->is_input_section())
2088 return this->u1_
.data_size
;
2090 return this->u2_
.posd
->data_size();
2093 // Return the object for an input section.
2096 Output_section::Input_section::relobj() const
2098 if (this->is_input_section())
2099 return this->u2_
.object
;
2100 else if (this->is_merge_section())
2102 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2103 return this->u2_
.pomb
->first_relobj();
2105 else if (this->is_relaxed_input_section())
2106 return this->u2_
.poris
->relobj();
2111 // Return the input section index for an input section.
2114 Output_section::Input_section::shndx() const
2116 if (this->is_input_section())
2117 return this->shndx_
;
2118 else if (this->is_merge_section())
2120 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2121 return this->u2_
.pomb
->first_shndx();
2123 else if (this->is_relaxed_input_section())
2124 return this->u2_
.poris
->shndx();
2129 // Set the address and file offset.
2132 Output_section::Input_section::set_address_and_file_offset(
2135 off_t section_file_offset
)
2137 if (this->is_input_section())
2138 this->u2_
.object
->set_section_offset(this->shndx_
,
2139 file_offset
- section_file_offset
);
2141 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
2144 // Reset the address and file offset.
2147 Output_section::Input_section::reset_address_and_file_offset()
2149 if (!this->is_input_section())
2150 this->u2_
.posd
->reset_address_and_file_offset();
2153 // Finalize the data size.
2156 Output_section::Input_section::finalize_data_size()
2158 if (!this->is_input_section())
2159 this->u2_
.posd
->finalize_data_size();
2162 // Try to turn an input offset into an output offset. We want to
2163 // return the output offset relative to the start of this
2164 // Input_section in the output section.
2167 Output_section::Input_section::output_offset(
2168 const Relobj
* object
,
2170 section_offset_type offset
,
2171 section_offset_type
* poutput
) const
2173 if (!this->is_input_section())
2174 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
2177 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
2184 // Return whether this is the merge section for the input section
2188 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
2189 unsigned int shndx
) const
2191 if (this->is_input_section())
2193 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
2196 // Write out the data. We don't have to do anything for an input
2197 // section--they are handled via Object::relocate--but this is where
2198 // we write out the data for an Output_section_data.
2201 Output_section::Input_section::write(Output_file
* of
)
2203 if (!this->is_input_section())
2204 this->u2_
.posd
->write(of
);
2207 // Write the data to a buffer. As for write(), we don't have to do
2208 // anything for an input section.
2211 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
2213 if (!this->is_input_section())
2214 this->u2_
.posd
->write_to_buffer(buffer
);
2217 // Print to a map file.
2220 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
2222 switch (this->shndx_
)
2224 case OUTPUT_SECTION_CODE
:
2225 case MERGE_DATA_SECTION_CODE
:
2226 case MERGE_STRING_SECTION_CODE
:
2227 this->u2_
.posd
->print_to_mapfile(mapfile
);
2230 case RELAXED_INPUT_SECTION_CODE
:
2232 Output_relaxed_input_section
* relaxed_section
=
2233 this->relaxed_input_section();
2234 mapfile
->print_input_section(relaxed_section
->relobj(),
2235 relaxed_section
->shndx());
2239 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
2244 // Output_section methods.
2246 // Construct an Output_section. NAME will point into a Stringpool.
2248 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
2249 elfcpp::Elf_Xword flags
)
2254 link_section_(NULL
),
2256 info_section_(NULL
),
2261 order_(ORDER_INVALID
),
2266 first_input_offset_(0),
2268 postprocessing_buffer_(NULL
),
2269 needs_symtab_index_(false),
2270 needs_dynsym_index_(false),
2271 should_link_to_symtab_(false),
2272 should_link_to_dynsym_(false),
2273 after_input_sections_(false),
2274 requires_postprocessing_(false),
2275 found_in_sections_clause_(false),
2276 has_load_address_(false),
2277 info_uses_section_index_(false),
2278 input_section_order_specified_(false),
2279 may_sort_attached_input_sections_(false),
2280 must_sort_attached_input_sections_(false),
2281 attached_input_sections_are_sorted_(false),
2283 is_small_section_(false),
2284 is_large_section_(false),
2285 generate_code_fills_at_write_(false),
2286 is_entsize_zero_(false),
2287 section_offsets_need_adjustment_(false),
2289 always_keeps_input_sections_(false),
2290 has_fixed_layout_(false),
2291 is_patch_space_allowed_(false),
2294 lookup_maps_(new Output_section_lookup_maps
),
2296 free_space_fill_(NULL
),
2299 // An unallocated section has no address. Forcing this means that
2300 // we don't need special treatment for symbols defined in debug
2302 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
2303 this->set_address(0);
2306 Output_section::~Output_section()
2308 delete this->checkpoint_
;
2311 // Set the entry size.
2314 Output_section::set_entsize(uint64_t v
)
2316 if (this->is_entsize_zero_
)
2318 else if (this->entsize_
== 0)
2320 else if (this->entsize_
!= v
)
2323 this->is_entsize_zero_
= 1;
2327 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2328 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2329 // relocation section which applies to this section, or 0 if none, or
2330 // -1U if more than one. Return the offset of the input section
2331 // within the output section. Return -1 if the input section will
2332 // receive special handling. In the normal case we don't always keep
2333 // track of input sections for an Output_section. Instead, each
2334 // Object keeps track of the Output_section for each of its input
2335 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2336 // track of input sections here; this is used when SECTIONS appears in
2339 template<int size
, bool big_endian
>
2341 Output_section::add_input_section(Layout
* layout
,
2342 Sized_relobj_file
<size
, big_endian
>* object
,
2344 const char* secname
,
2345 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2346 unsigned int reloc_shndx
,
2347 bool have_sections_script
)
2349 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
2350 if ((addralign
& (addralign
- 1)) != 0)
2352 object
->error(_("invalid alignment %lu for section \"%s\""),
2353 static_cast<unsigned long>(addralign
), secname
);
2357 if (addralign
> this->addralign_
)
2358 this->addralign_
= addralign
;
2360 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
2361 uint64_t entsize
= shdr
.get_sh_entsize();
2363 // .debug_str is a mergeable string section, but is not always so
2364 // marked by compilers. Mark manually here so we can optimize.
2365 if (strcmp(secname
, ".debug_str") == 0)
2367 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
2371 this->update_flags_for_input_section(sh_flags
);
2372 this->set_entsize(entsize
);
2374 // If this is a SHF_MERGE section, we pass all the input sections to
2375 // a Output_data_merge. We don't try to handle relocations for such
2376 // a section. We don't try to handle empty merge sections--they
2377 // mess up the mappings, and are useless anyhow.
2378 // FIXME: Need to handle merge sections during incremental update.
2379 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
2381 && shdr
.get_sh_size() > 0
2382 && !parameters
->incremental())
2384 // Keep information about merged input sections for rebuilding fast
2385 // lookup maps if we have sections-script or we do relaxation.
2386 bool keeps_input_sections
= (this->always_keeps_input_sections_
2387 || have_sections_script
2388 || parameters
->target().may_relax());
2390 if (this->add_merge_input_section(object
, shndx
, sh_flags
, entsize
,
2391 addralign
, keeps_input_sections
))
2393 // Tell the relocation routines that they need to call the
2394 // output_offset method to determine the final address.
2399 section_size_type input_section_size
= shdr
.get_sh_size();
2400 section_size_type uncompressed_size
;
2401 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
2402 input_section_size
= uncompressed_size
;
2404 off_t offset_in_section
;
2405 off_t aligned_offset_in_section
;
2406 if (this->has_fixed_layout())
2408 // For incremental updates, find a chunk of unused space in the section.
2409 offset_in_section
= this->free_list_
.allocate(input_section_size
,
2411 if (offset_in_section
== -1)
2412 gold_fallback(_("out of patch space in section %s; "
2413 "relink with --incremental-full"),
2415 aligned_offset_in_section
= offset_in_section
;
2419 offset_in_section
= this->current_data_size_for_child();
2420 aligned_offset_in_section
= align_address(offset_in_section
,
2422 this->set_current_data_size_for_child(aligned_offset_in_section
2423 + input_section_size
);
2426 // Determine if we want to delay code-fill generation until the output
2427 // section is written. When the target is relaxing, we want to delay fill
2428 // generating to avoid adjusting them during relaxation. Also, if we are
2429 // sorting input sections we must delay fill generation.
2430 if (!this->generate_code_fills_at_write_
2431 && !have_sections_script
2432 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2433 && parameters
->target().has_code_fill()
2434 && (parameters
->target().may_relax()
2435 || layout
->is_section_ordering_specified()))
2437 gold_assert(this->fills_
.empty());
2438 this->generate_code_fills_at_write_
= true;
2441 if (aligned_offset_in_section
> offset_in_section
2442 && !this->generate_code_fills_at_write_
2443 && !have_sections_script
2444 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2445 && parameters
->target().has_code_fill())
2447 // We need to add some fill data. Using fill_list_ when
2448 // possible is an optimization, since we will often have fill
2449 // sections without input sections.
2450 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
2451 if (this->input_sections_
.empty())
2452 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
2455 std::string
fill_data(parameters
->target().code_fill(fill_len
));
2456 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
2457 this->input_sections_
.push_back(Input_section(odc
));
2461 // We need to keep track of this section if we are already keeping
2462 // track of sections, or if we are relaxing. Also, if this is a
2463 // section which requires sorting, or which may require sorting in
2464 // the future, we keep track of the sections. If the
2465 // --section-ordering-file option is used to specify the order of
2466 // sections, we need to keep track of sections.
2467 if (this->always_keeps_input_sections_
2468 || have_sections_script
2469 || !this->input_sections_
.empty()
2470 || this->may_sort_attached_input_sections()
2471 || this->must_sort_attached_input_sections()
2472 || parameters
->options().user_set_Map()
2473 || parameters
->target().may_relax()
2474 || layout
->is_section_ordering_specified())
2476 Input_section
isecn(object
, shndx
, input_section_size
, addralign
);
2477 if (layout
->is_section_ordering_specified())
2479 unsigned int section_order_index
=
2480 layout
->find_section_order_index(std::string(secname
));
2481 if (section_order_index
!= 0)
2483 isecn
.set_section_order_index(section_order_index
);
2484 this->set_input_section_order_specified();
2487 if (this->has_fixed_layout())
2489 // For incremental updates, finalize the address and offset now.
2490 uint64_t addr
= this->address();
2491 isecn
.set_address_and_file_offset(addr
+ aligned_offset_in_section
,
2492 aligned_offset_in_section
,
2495 this->input_sections_
.push_back(isecn
);
2498 return aligned_offset_in_section
;
2501 // Add arbitrary data to an output section.
2504 Output_section::add_output_section_data(Output_section_data
* posd
)
2506 Input_section
inp(posd
);
2507 this->add_output_section_data(&inp
);
2509 if (posd
->is_data_size_valid())
2511 off_t offset_in_section
;
2512 if (this->has_fixed_layout())
2514 // For incremental updates, find a chunk of unused space.
2515 offset_in_section
= this->free_list_
.allocate(posd
->data_size(),
2516 posd
->addralign(), 0);
2517 if (offset_in_section
== -1)
2518 gold_fallback(_("out of patch space in section %s; "
2519 "relink with --incremental-full"),
2521 // Finalize the address and offset now.
2522 uint64_t addr
= this->address();
2523 off_t offset
= this->offset();
2524 posd
->set_address_and_file_offset(addr
+ offset_in_section
,
2525 offset
+ offset_in_section
);
2529 offset_in_section
= this->current_data_size_for_child();
2530 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2532 this->set_current_data_size_for_child(aligned_offset_in_section
2533 + posd
->data_size());
2536 else if (this->has_fixed_layout())
2538 // For incremental updates, arrange for the data to have a fixed layout.
2539 // This will mean that additions to the data must be allocated from
2540 // free space within the containing output section.
2541 uint64_t addr
= this->address();
2542 posd
->set_address(addr
);
2543 posd
->set_file_offset(0);
2544 // FIXME: This should eventually be unreachable.
2545 // gold_unreachable();
2549 // Add a relaxed input section.
2552 Output_section::add_relaxed_input_section(Layout
* layout
,
2553 Output_relaxed_input_section
* poris
,
2554 const std::string
& name
)
2556 Input_section
inp(poris
);
2558 // If the --section-ordering-file option is used to specify the order of
2559 // sections, we need to keep track of sections.
2560 if (layout
->is_section_ordering_specified())
2562 unsigned int section_order_index
=
2563 layout
->find_section_order_index(name
);
2564 if (section_order_index
!= 0)
2566 inp
.set_section_order_index(section_order_index
);
2567 this->set_input_section_order_specified();
2571 this->add_output_section_data(&inp
);
2572 if (this->lookup_maps_
->is_valid())
2573 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2574 poris
->shndx(), poris
);
2576 // For a relaxed section, we use the current data size. Linker scripts
2577 // get all the input sections, including relaxed one from an output
2578 // section and add them back to them same output section to compute the
2579 // output section size. If we do not account for sizes of relaxed input
2580 // sections, an output section would be incorrectly sized.
2581 off_t offset_in_section
= this->current_data_size_for_child();
2582 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2583 poris
->addralign());
2584 this->set_current_data_size_for_child(aligned_offset_in_section
2585 + poris
->current_data_size());
2588 // Add arbitrary data to an output section by Input_section.
2591 Output_section::add_output_section_data(Input_section
* inp
)
2593 if (this->input_sections_
.empty())
2594 this->first_input_offset_
= this->current_data_size_for_child();
2596 this->input_sections_
.push_back(*inp
);
2598 uint64_t addralign
= inp
->addralign();
2599 if (addralign
> this->addralign_
)
2600 this->addralign_
= addralign
;
2602 inp
->set_output_section(this);
2605 // Add a merge section to an output section.
2608 Output_section::add_output_merge_section(Output_section_data
* posd
,
2609 bool is_string
, uint64_t entsize
)
2611 Input_section
inp(posd
, is_string
, entsize
);
2612 this->add_output_section_data(&inp
);
2615 // Add an input section to a SHF_MERGE section.
2618 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
2619 uint64_t flags
, uint64_t entsize
,
2621 bool keeps_input_sections
)
2623 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
2625 // We only merge strings if the alignment is not more than the
2626 // character size. This could be handled, but it's unusual.
2627 if (is_string
&& addralign
> entsize
)
2630 // We cannot restore merged input section states.
2631 gold_assert(this->checkpoint_
== NULL
);
2633 // Look up merge sections by required properties.
2634 // Currently, we only invalidate the lookup maps in script processing
2635 // and relaxation. We should not have done either when we reach here.
2636 // So we assume that the lookup maps are valid to simply code.
2637 gold_assert(this->lookup_maps_
->is_valid());
2638 Merge_section_properties
msp(is_string
, entsize
, addralign
);
2639 Output_merge_base
* pomb
= this->lookup_maps_
->find_merge_section(msp
);
2640 bool is_new
= false;
2643 gold_assert(pomb
->is_string() == is_string
2644 && pomb
->entsize() == entsize
2645 && pomb
->addralign() == addralign
);
2649 // Create a new Output_merge_data or Output_merge_string_data.
2651 pomb
= new Output_merge_data(entsize
, addralign
);
2657 pomb
= new Output_merge_string
<char>(addralign
);
2660 pomb
= new Output_merge_string
<uint16_t>(addralign
);
2663 pomb
= new Output_merge_string
<uint32_t>(addralign
);
2669 // If we need to do script processing or relaxation, we need to keep
2670 // the original input sections to rebuild the fast lookup maps.
2671 if (keeps_input_sections
)
2672 pomb
->set_keeps_input_sections();
2676 if (pomb
->add_input_section(object
, shndx
))
2678 // Add new merge section to this output section and link merge
2679 // section properties to new merge section in map.
2682 this->add_output_merge_section(pomb
, is_string
, entsize
);
2683 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2686 // Add input section to new merge section and link input section to new
2687 // merge section in map.
2688 this->lookup_maps_
->add_merge_input_section(object
, shndx
, pomb
);
2693 // If add_input_section failed, delete new merge section to avoid
2694 // exporting empty merge sections in Output_section::get_input_section.
2701 // Build a relaxation map to speed up relaxation of existing input sections.
2702 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2705 Output_section::build_relaxation_map(
2706 const Input_section_list
& input_sections
,
2708 Relaxation_map
* relaxation_map
) const
2710 for (size_t i
= 0; i
< limit
; ++i
)
2712 const Input_section
& is(input_sections
[i
]);
2713 if (is
.is_input_section() || is
.is_relaxed_input_section())
2715 Section_id
sid(is
.relobj(), is
.shndx());
2716 (*relaxation_map
)[sid
] = i
;
2721 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2722 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2723 // indices of INPUT_SECTIONS.
2726 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2727 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
2728 const Relaxation_map
& map
,
2729 Input_section_list
* input_sections
)
2731 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2733 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2734 Section_id
sid(poris
->relobj(), poris
->shndx());
2735 Relaxation_map::const_iterator p
= map
.find(sid
);
2736 gold_assert(p
!= map
.end());
2737 gold_assert((*input_sections
)[p
->second
].is_input_section());
2739 // Remember section order index of original input section
2740 // if it is set. Copy it to the relaxed input section.
2742 (*input_sections
)[p
->second
].section_order_index();
2743 (*input_sections
)[p
->second
] = Input_section(poris
);
2744 (*input_sections
)[p
->second
].set_section_order_index(soi
);
2748 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2749 // is a vector of pointers to Output_relaxed_input_section or its derived
2750 // classes. The relaxed sections must correspond to existing input sections.
2753 Output_section::convert_input_sections_to_relaxed_sections(
2754 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
)
2756 gold_assert(parameters
->target().may_relax());
2758 // We want to make sure that restore_states does not undo the effect of
2759 // this. If there is no checkpoint active, just search the current
2760 // input section list and replace the sections there. If there is
2761 // a checkpoint, also replace the sections there.
2763 // By default, we look at the whole list.
2764 size_t limit
= this->input_sections_
.size();
2766 if (this->checkpoint_
!= NULL
)
2768 // Replace input sections with relaxed input section in the saved
2769 // copy of the input section list.
2770 if (this->checkpoint_
->input_sections_saved())
2773 this->build_relaxation_map(
2774 *(this->checkpoint_
->input_sections()),
2775 this->checkpoint_
->input_sections()->size(),
2777 this->convert_input_sections_in_list_to_relaxed_sections(
2780 this->checkpoint_
->input_sections());
2784 // We have not copied the input section list yet. Instead, just
2785 // look at the portion that would be saved.
2786 limit
= this->checkpoint_
->input_sections_size();
2790 // Convert input sections in input_section_list.
2792 this->build_relaxation_map(this->input_sections_
, limit
, &map
);
2793 this->convert_input_sections_in_list_to_relaxed_sections(
2796 &this->input_sections_
);
2798 // Update fast look-up map.
2799 if (this->lookup_maps_
->is_valid())
2800 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2802 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2803 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2804 poris
->shndx(), poris
);
2808 // Update the output section flags based on input section flags.
2811 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags
)
2813 // If we created the section with SHF_ALLOC clear, we set the
2814 // address. If we are now setting the SHF_ALLOC flag, we need to
2816 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0
2817 && (flags
& elfcpp::SHF_ALLOC
) != 0)
2818 this->mark_address_invalid();
2820 this->flags_
|= (flags
2821 & (elfcpp::SHF_WRITE
2823 | elfcpp::SHF_EXECINSTR
));
2825 if ((flags
& elfcpp::SHF_MERGE
) == 0)
2826 this->flags_
&=~ elfcpp::SHF_MERGE
;
2829 if (this->current_data_size_for_child() == 0)
2830 this->flags_
|= elfcpp::SHF_MERGE
;
2833 if ((flags
& elfcpp::SHF_STRINGS
) == 0)
2834 this->flags_
&=~ elfcpp::SHF_STRINGS
;
2837 if (this->current_data_size_for_child() == 0)
2838 this->flags_
|= elfcpp::SHF_STRINGS
;
2842 // Find the merge section into which an input section with index SHNDX in
2843 // OBJECT has been added. Return NULL if none found.
2845 Output_section_data
*
2846 Output_section::find_merge_section(const Relobj
* object
,
2847 unsigned int shndx
) const
2849 if (!this->lookup_maps_
->is_valid())
2850 this->build_lookup_maps();
2851 return this->lookup_maps_
->find_merge_section(object
, shndx
);
2854 // Build the lookup maps for merge and relaxed sections. This is needs
2855 // to be declared as a const methods so that it is callable with a const
2856 // Output_section pointer. The method only updates states of the maps.
2859 Output_section::build_lookup_maps() const
2861 this->lookup_maps_
->clear();
2862 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2863 p
!= this->input_sections_
.end();
2866 if (p
->is_merge_section())
2868 Output_merge_base
* pomb
= p
->output_merge_base();
2869 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
2871 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2872 for (Output_merge_base::Input_sections::const_iterator is
=
2873 pomb
->input_sections_begin();
2874 is
!= pomb
->input_sections_end();
2877 const Const_section_id
& csid
= *is
;
2878 this->lookup_maps_
->add_merge_input_section(csid
.first
,
2883 else if (p
->is_relaxed_input_section())
2885 Output_relaxed_input_section
* poris
= p
->relaxed_input_section();
2886 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2887 poris
->shndx(), poris
);
2892 // Find an relaxed input section corresponding to an input section
2893 // in OBJECT with index SHNDX.
2895 const Output_relaxed_input_section
*
2896 Output_section::find_relaxed_input_section(const Relobj
* object
,
2897 unsigned int shndx
) const
2899 if (!this->lookup_maps_
->is_valid())
2900 this->build_lookup_maps();
2901 return this->lookup_maps_
->find_relaxed_input_section(object
, shndx
);
2904 // Given an address OFFSET relative to the start of input section
2905 // SHNDX in OBJECT, return whether this address is being included in
2906 // the final link. This should only be called if SHNDX in OBJECT has
2907 // a special mapping.
2910 Output_section::is_input_address_mapped(const Relobj
* object
,
2914 // Look at the Output_section_data_maps first.
2915 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2917 posd
= this->find_relaxed_input_section(object
, shndx
);
2921 section_offset_type output_offset
;
2922 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2924 return output_offset
!= -1;
2927 // Fall back to the slow look-up.
2928 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2929 p
!= this->input_sections_
.end();
2932 section_offset_type output_offset
;
2933 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2934 return output_offset
!= -1;
2937 // By default we assume that the address is mapped. This should
2938 // only be called after we have passed all sections to Layout. At
2939 // that point we should know what we are discarding.
2943 // Given an address OFFSET relative to the start of input section
2944 // SHNDX in object OBJECT, return the output offset relative to the
2945 // start of the input section in the output section. This should only
2946 // be called if SHNDX in OBJECT has a special mapping.
2949 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2950 section_offset_type offset
) const
2952 // This can only be called meaningfully when we know the data size
2954 gold_assert(this->is_data_size_valid());
2956 // Look at the Output_section_data_maps first.
2957 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2959 posd
= this->find_relaxed_input_section(object
, shndx
);
2962 section_offset_type output_offset
;
2963 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2965 return output_offset
;
2968 // Fall back to the slow look-up.
2969 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2970 p
!= this->input_sections_
.end();
2973 section_offset_type output_offset
;
2974 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2975 return output_offset
;
2980 // Return the output virtual address of OFFSET relative to the start
2981 // of input section SHNDX in object OBJECT.
2984 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
2987 uint64_t addr
= this->address() + this->first_input_offset_
;
2989 // Look at the Output_section_data_maps first.
2990 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2992 posd
= this->find_relaxed_input_section(object
, shndx
);
2993 if (posd
!= NULL
&& posd
->is_address_valid())
2995 section_offset_type output_offset
;
2996 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2998 return posd
->address() + output_offset
;
3001 // Fall back to the slow look-up.
3002 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3003 p
!= this->input_sections_
.end();
3006 addr
= align_address(addr
, p
->addralign());
3007 section_offset_type output_offset
;
3008 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
3010 if (output_offset
== -1)
3012 return addr
+ output_offset
;
3014 addr
+= p
->data_size();
3017 // If we get here, it means that we don't know the mapping for this
3018 // input section. This might happen in principle if
3019 // add_input_section were called before add_output_section_data.
3020 // But it should never actually happen.
3025 // Find the output address of the start of the merged section for
3026 // input section SHNDX in object OBJECT.
3029 Output_section::find_starting_output_address(const Relobj
* object
,
3031 uint64_t* paddr
) const
3033 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3034 // Looking up the merge section map does not always work as we sometimes
3035 // find a merge section without its address set.
3036 uint64_t addr
= this->address() + this->first_input_offset_
;
3037 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3038 p
!= this->input_sections_
.end();
3041 addr
= align_address(addr
, p
->addralign());
3043 // It would be nice if we could use the existing output_offset
3044 // method to get the output offset of input offset 0.
3045 // Unfortunately we don't know for sure that input offset 0 is
3047 if (p
->is_merge_section_for(object
, shndx
))
3053 addr
+= p
->data_size();
3056 // We couldn't find a merge output section for this input section.
3060 // Update the data size of an Output_section.
3063 Output_section::update_data_size()
3065 if (this->input_sections_
.empty())
3068 if (this->must_sort_attached_input_sections()
3069 || this->input_section_order_specified())
3070 this->sort_attached_input_sections();
3072 off_t off
= this->first_input_offset_
;
3073 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3074 p
!= this->input_sections_
.end();
3077 off
= align_address(off
, p
->addralign());
3078 off
+= p
->current_data_size();
3081 this->set_current_data_size_for_child(off
);
3084 // Set the data size of an Output_section. This is where we handle
3085 // setting the addresses of any Output_section_data objects.
3088 Output_section::set_final_data_size()
3092 if (this->input_sections_
.empty())
3093 data_size
= this->current_data_size_for_child();
3096 if (this->must_sort_attached_input_sections()
3097 || this->input_section_order_specified())
3098 this->sort_attached_input_sections();
3100 uint64_t address
= this->address();
3101 off_t startoff
= this->offset();
3102 off_t off
= startoff
+ this->first_input_offset_
;
3103 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3104 p
!= this->input_sections_
.end();
3107 off
= align_address(off
, p
->addralign());
3108 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
3110 off
+= p
->data_size();
3112 data_size
= off
- startoff
;
3115 // For full incremental links, we want to allocate some patch space
3116 // in most sections for subsequent incremental updates.
3117 if (this->is_patch_space_allowed_
&& parameters
->incremental_full())
3119 double pct
= parameters
->options().incremental_patch();
3120 size_t extra
= static_cast<size_t>(data_size
* pct
);
3121 if (this->free_space_fill_
!= NULL
3122 && this->free_space_fill_
->minimum_hole_size() > extra
)
3123 extra
= this->free_space_fill_
->minimum_hole_size();
3124 off_t new_size
= align_address(data_size
+ extra
, this->addralign());
3125 this->patch_space_
= new_size
- data_size
;
3126 gold_debug(DEBUG_INCREMENTAL
,
3127 "set_final_data_size: %08lx + %08lx: section %s",
3128 static_cast<long>(data_size
),
3129 static_cast<long>(this->patch_space_
),
3131 data_size
= new_size
;
3134 this->set_data_size(data_size
);
3137 // Reset the address and file offset.
3140 Output_section::do_reset_address_and_file_offset()
3142 // An unallocated section has no address. Forcing this means that
3143 // we don't need special treatment for symbols defined in debug
3144 // sections. We do the same in the constructor. This does not
3145 // apply to NOLOAD sections though.
3146 if (((this->flags_
& elfcpp::SHF_ALLOC
) == 0) && !this->is_noload_
)
3147 this->set_address(0);
3149 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3150 p
!= this->input_sections_
.end();
3152 p
->reset_address_and_file_offset();
3154 // Remove any patch space that was added in set_final_data_size.
3155 if (this->patch_space_
> 0)
3157 this->set_current_data_size_for_child(this->current_data_size_for_child()
3158 - this->patch_space_
);
3159 this->patch_space_
= 0;
3163 // Return true if address and file offset have the values after reset.
3166 Output_section::do_address_and_file_offset_have_reset_values() const
3168 if (this->is_offset_valid())
3171 // An unallocated section has address 0 after its construction or a reset.
3172 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0)
3173 return this->is_address_valid() && this->address() == 0;
3175 return !this->is_address_valid();
3178 // Set the TLS offset. Called only for SHT_TLS sections.
3181 Output_section::do_set_tls_offset(uint64_t tls_base
)
3183 this->tls_offset_
= this->address() - tls_base
;
3186 // In a few cases we need to sort the input sections attached to an
3187 // output section. This is used to implement the type of constructor
3188 // priority ordering implemented by the GNU linker, in which the
3189 // priority becomes part of the section name and the sections are
3190 // sorted by name. We only do this for an output section if we see an
3191 // attached input section matching ".ctors.*", ".dtors.*",
3192 // ".init_array.*" or ".fini_array.*".
3194 class Output_section::Input_section_sort_entry
3197 Input_section_sort_entry()
3198 : input_section_(), index_(-1U), section_has_name_(false),
3202 Input_section_sort_entry(const Input_section
& input_section
,
3204 bool must_sort_attached_input_sections
)
3205 : input_section_(input_section
), index_(index
),
3206 section_has_name_(input_section
.is_input_section()
3207 || input_section
.is_relaxed_input_section())
3209 if (this->section_has_name_
3210 && must_sort_attached_input_sections
)
3212 // This is only called single-threaded from Layout::finalize,
3213 // so it is OK to lock. Unfortunately we have no way to pass
3215 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
3216 Object
* obj
= (input_section
.is_input_section()
3217 ? input_section
.relobj()
3218 : input_section
.relaxed_input_section()->relobj());
3219 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
3221 // This is a slow operation, which should be cached in
3222 // Layout::layout if this becomes a speed problem.
3223 this->section_name_
= obj
->section_name(input_section
.shndx());
3227 // Return the Input_section.
3228 const Input_section
&
3229 input_section() const
3231 gold_assert(this->index_
!= -1U);
3232 return this->input_section_
;
3235 // The index of this entry in the original list. This is used to
3236 // make the sort stable.
3240 gold_assert(this->index_
!= -1U);
3241 return this->index_
;
3244 // Whether there is a section name.
3246 section_has_name() const
3247 { return this->section_has_name_
; }
3249 // The section name.
3251 section_name() const
3253 gold_assert(this->section_has_name_
);
3254 return this->section_name_
;
3257 // Return true if the section name has a priority. This is assumed
3258 // to be true if it has a dot after the initial dot.
3260 has_priority() const
3262 gold_assert(this->section_has_name_
);
3263 return this->section_name_
.find('.', 1) != std::string::npos
;
3266 // Return the priority. Believe it or not, gcc encodes the priority
3267 // differently for .ctors/.dtors and .init_array/.fini_array
3270 get_priority() const
3272 gold_assert(this->section_has_name_
);
3274 if (is_prefix_of(".ctors.", this->section_name_
.c_str())
3275 || is_prefix_of(".dtors.", this->section_name_
.c_str()))
3277 else if (is_prefix_of(".init_array.", this->section_name_
.c_str())
3278 || is_prefix_of(".fini_array.", this->section_name_
.c_str()))
3283 unsigned long prio
= strtoul((this->section_name_
.c_str()
3284 + (is_ctors
? 7 : 12)),
3289 return 65535 - prio
;
3294 // Return true if this an input file whose base name matches
3295 // FILE_NAME. The base name must have an extension of ".o", and
3296 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3297 // This is to match crtbegin.o as well as crtbeginS.o without
3298 // getting confused by other possibilities. Overall matching the
3299 // file name this way is a dreadful hack, but the GNU linker does it
3300 // in order to better support gcc, and we need to be compatible.
3302 match_file_name(const char* file_name
) const
3303 { return Layout::match_file_name(this->input_section_
.relobj(), file_name
); }
3305 // Returns 1 if THIS should appear before S in section order, -1 if S
3306 // appears before THIS and 0 if they are not comparable.
3308 compare_section_ordering(const Input_section_sort_entry
& s
) const
3310 unsigned int this_secn_index
= this->input_section_
.section_order_index();
3311 unsigned int s_secn_index
= s
.input_section().section_order_index();
3312 if (this_secn_index
> 0 && s_secn_index
> 0)
3314 if (this_secn_index
< s_secn_index
)
3316 else if (this_secn_index
> s_secn_index
)
3323 // The Input_section we are sorting.
3324 Input_section input_section_
;
3325 // The index of this Input_section in the original list.
3326 unsigned int index_
;
3327 // Whether this Input_section has a section name--it won't if this
3328 // is some random Output_section_data.
3329 bool section_has_name_
;
3330 // The section name if there is one.
3331 std::string section_name_
;
3334 // Return true if S1 should come before S2 in the output section.
3337 Output_section::Input_section_sort_compare::operator()(
3338 const Output_section::Input_section_sort_entry
& s1
,
3339 const Output_section::Input_section_sort_entry
& s2
) const
3341 // crtbegin.o must come first.
3342 bool s1_begin
= s1
.match_file_name("crtbegin");
3343 bool s2_begin
= s2
.match_file_name("crtbegin");
3344 if (s1_begin
|| s2_begin
)
3350 return s1
.index() < s2
.index();
3353 // crtend.o must come last.
3354 bool s1_end
= s1
.match_file_name("crtend");
3355 bool s2_end
= s2
.match_file_name("crtend");
3356 if (s1_end
|| s2_end
)
3362 return s1
.index() < s2
.index();
3365 // We sort all the sections with no names to the end.
3366 if (!s1
.section_has_name() || !s2
.section_has_name())
3368 if (s1
.section_has_name())
3370 if (s2
.section_has_name())
3372 return s1
.index() < s2
.index();
3375 // A section with a priority follows a section without a priority.
3376 bool s1_has_priority
= s1
.has_priority();
3377 bool s2_has_priority
= s2
.has_priority();
3378 if (s1_has_priority
&& !s2_has_priority
)
3380 if (!s1_has_priority
&& s2_has_priority
)
3383 // Check if a section order exists for these sections through a section
3384 // ordering file. If sequence_num is 0, an order does not exist.
3385 int sequence_num
= s1
.compare_section_ordering(s2
);
3386 if (sequence_num
!= 0)
3387 return sequence_num
== 1;
3389 // Otherwise we sort by name.
3390 int compare
= s1
.section_name().compare(s2
.section_name());
3394 // Otherwise we keep the input order.
3395 return s1
.index() < s2
.index();
3398 // Return true if S1 should come before S2 in an .init_array or .fini_array
3402 Output_section::Input_section_sort_init_fini_compare::operator()(
3403 const Output_section::Input_section_sort_entry
& s1
,
3404 const Output_section::Input_section_sort_entry
& s2
) const
3406 // We sort all the sections with no names to the end.
3407 if (!s1
.section_has_name() || !s2
.section_has_name())
3409 if (s1
.section_has_name())
3411 if (s2
.section_has_name())
3413 return s1
.index() < s2
.index();
3416 // A section without a priority follows a section with a priority.
3417 // This is the reverse of .ctors and .dtors sections.
3418 bool s1_has_priority
= s1
.has_priority();
3419 bool s2_has_priority
= s2
.has_priority();
3420 if (s1_has_priority
&& !s2_has_priority
)
3422 if (!s1_has_priority
&& s2_has_priority
)
3425 // .ctors and .dtors sections without priority come after
3426 // .init_array and .fini_array sections without priority.
3427 if (!s1_has_priority
3428 && (s1
.section_name() == ".ctors" || s1
.section_name() == ".dtors")
3429 && s1
.section_name() != s2
.section_name())
3431 if (!s2_has_priority
3432 && (s2
.section_name() == ".ctors" || s2
.section_name() == ".dtors")
3433 && s2
.section_name() != s1
.section_name())
3436 // Sort by priority if we can.
3437 if (s1_has_priority
)
3439 unsigned int s1_prio
= s1
.get_priority();
3440 unsigned int s2_prio
= s2
.get_priority();
3441 if (s1_prio
< s2_prio
)
3443 else if (s1_prio
> s2_prio
)
3447 // Check if a section order exists for these sections through a section
3448 // ordering file. If sequence_num is 0, an order does not exist.
3449 int sequence_num
= s1
.compare_section_ordering(s2
);
3450 if (sequence_num
!= 0)
3451 return sequence_num
== 1;
3453 // Otherwise we sort by name.
3454 int compare
= s1
.section_name().compare(s2
.section_name());
3458 // Otherwise we keep the input order.
3459 return s1
.index() < s2
.index();
3462 // Return true if S1 should come before S2. Sections that do not match
3463 // any pattern in the section ordering file are placed ahead of the sections
3464 // that match some pattern.
3467 Output_section::Input_section_sort_section_order_index_compare::operator()(
3468 const Output_section::Input_section_sort_entry
& s1
,
3469 const Output_section::Input_section_sort_entry
& s2
) const
3471 unsigned int s1_secn_index
= s1
.input_section().section_order_index();
3472 unsigned int s2_secn_index
= s2
.input_section().section_order_index();
3474 // Keep input order if section ordering cannot determine order.
3475 if (s1_secn_index
== s2_secn_index
)
3476 return s1
.index() < s2
.index();
3478 return s1_secn_index
< s2_secn_index
;
3481 // This updates the section order index of input sections according to the
3482 // the order specified in the mapping from Section id to order index.
3485 Output_section::update_section_layout(
3486 const Section_layout_order
& order_map
)
3488 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3489 p
!= this->input_sections_
.end();
3492 if (p
->is_input_section()
3493 || p
->is_relaxed_input_section())
3495 Object
* obj
= (p
->is_input_section()
3497 : p
->relaxed_input_section()->relobj());
3498 unsigned int shndx
= p
->shndx();
3499 Section_layout_order::const_iterator it
3500 = order_map
.find(Section_id(obj
, shndx
));
3501 if (it
== order_map
.end())
3503 unsigned int section_order_index
= it
->second
;
3504 if (section_order_index
!= 0)
3506 p
->set_section_order_index(section_order_index
);
3507 this->set_input_section_order_specified();
3513 // Sort the input sections attached to an output section.
3516 Output_section::sort_attached_input_sections()
3518 if (this->attached_input_sections_are_sorted_
)
3521 if (this->checkpoint_
!= NULL
3522 && !this->checkpoint_
->input_sections_saved())
3523 this->checkpoint_
->save_input_sections();
3525 // The only thing we know about an input section is the object and
3526 // the section index. We need the section name. Recomputing this
3527 // is slow but this is an unusual case. If this becomes a speed
3528 // problem we can cache the names as required in Layout::layout.
3530 // We start by building a larger vector holding a copy of each
3531 // Input_section, plus its current index in the list and its name.
3532 std::vector
<Input_section_sort_entry
> sort_list
;
3535 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3536 p
!= this->input_sections_
.end();
3538 sort_list
.push_back(Input_section_sort_entry(*p
, i
,
3539 this->must_sort_attached_input_sections()));
3541 // Sort the input sections.
3542 if (this->must_sort_attached_input_sections())
3544 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3545 || this->type() == elfcpp::SHT_INIT_ARRAY
3546 || this->type() == elfcpp::SHT_FINI_ARRAY
)
3547 std::sort(sort_list
.begin(), sort_list
.end(),
3548 Input_section_sort_init_fini_compare());
3550 std::sort(sort_list
.begin(), sort_list
.end(),
3551 Input_section_sort_compare());
3555 gold_assert(this->input_section_order_specified());
3556 std::sort(sort_list
.begin(), sort_list
.end(),
3557 Input_section_sort_section_order_index_compare());
3560 // Copy the sorted input sections back to our list.
3561 this->input_sections_
.clear();
3562 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
3563 p
!= sort_list
.end();
3565 this->input_sections_
.push_back(p
->input_section());
3568 // Remember that we sorted the input sections, since we might get
3570 this->attached_input_sections_are_sorted_
= true;
3573 // Write the section header to *OSHDR.
3575 template<int size
, bool big_endian
>
3577 Output_section::write_header(const Layout
* layout
,
3578 const Stringpool
* secnamepool
,
3579 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
3581 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
3582 oshdr
->put_sh_type(this->type_
);
3584 elfcpp::Elf_Xword flags
= this->flags_
;
3585 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
3586 flags
|= elfcpp::SHF_INFO_LINK
;
3587 oshdr
->put_sh_flags(flags
);
3589 oshdr
->put_sh_addr(this->address());
3590 oshdr
->put_sh_offset(this->offset());
3591 oshdr
->put_sh_size(this->data_size());
3592 if (this->link_section_
!= NULL
)
3593 oshdr
->put_sh_link(this->link_section_
->out_shndx());
3594 else if (this->should_link_to_symtab_
)
3595 oshdr
->put_sh_link(layout
->symtab_section_shndx());
3596 else if (this->should_link_to_dynsym_
)
3597 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
3599 oshdr
->put_sh_link(this->link_
);
3601 elfcpp::Elf_Word info
;
3602 if (this->info_section_
!= NULL
)
3604 if (this->info_uses_section_index_
)
3605 info
= this->info_section_
->out_shndx();
3607 info
= this->info_section_
->symtab_index();
3609 else if (this->info_symndx_
!= NULL
)
3610 info
= this->info_symndx_
->symtab_index();
3613 oshdr
->put_sh_info(info
);
3615 oshdr
->put_sh_addralign(this->addralign_
);
3616 oshdr
->put_sh_entsize(this->entsize_
);
3619 // Write out the data. For input sections the data is written out by
3620 // Object::relocate, but we have to handle Output_section_data objects
3624 Output_section::do_write(Output_file
* of
)
3626 gold_assert(!this->requires_postprocessing());
3628 // If the target performs relaxation, we delay filler generation until now.
3629 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3631 off_t output_section_file_offset
= this->offset();
3632 for (Fill_list::iterator p
= this->fills_
.begin();
3633 p
!= this->fills_
.end();
3636 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3637 of
->write(output_section_file_offset
+ p
->section_offset(),
3638 fill_data
.data(), fill_data
.size());
3641 off_t off
= this->offset() + this->first_input_offset_
;
3642 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3643 p
!= this->input_sections_
.end();
3646 off_t aligned_off
= align_address(off
, p
->addralign());
3647 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3649 size_t fill_len
= aligned_off
- off
;
3650 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3651 of
->write(off
, fill_data
.data(), fill_data
.size());
3655 off
= aligned_off
+ p
->data_size();
3658 // For incremental links, fill in unused chunks in debug sections
3659 // with dummy compilation unit headers.
3660 if (this->free_space_fill_
!= NULL
)
3662 for (Free_list::Const_iterator p
= this->free_list_
.begin();
3663 p
!= this->free_list_
.end();
3666 off_t off
= p
->start_
;
3667 size_t len
= p
->end_
- off
;
3668 this->free_space_fill_
->write(of
, this->offset() + off
, len
);
3670 if (this->patch_space_
> 0)
3672 off_t off
= this->current_data_size_for_child() - this->patch_space_
;
3673 this->free_space_fill_
->write(of
, this->offset() + off
,
3674 this->patch_space_
);
3679 // If a section requires postprocessing, create the buffer to use.
3682 Output_section::create_postprocessing_buffer()
3684 gold_assert(this->requires_postprocessing());
3686 if (this->postprocessing_buffer_
!= NULL
)
3689 if (!this->input_sections_
.empty())
3691 off_t off
= this->first_input_offset_
;
3692 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3693 p
!= this->input_sections_
.end();
3696 off
= align_address(off
, p
->addralign());
3697 p
->finalize_data_size();
3698 off
+= p
->data_size();
3700 this->set_current_data_size_for_child(off
);
3703 off_t buffer_size
= this->current_data_size_for_child();
3704 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
3707 // Write all the data of an Output_section into the postprocessing
3708 // buffer. This is used for sections which require postprocessing,
3709 // such as compression. Input sections are handled by
3710 // Object::Relocate.
3713 Output_section::write_to_postprocessing_buffer()
3715 gold_assert(this->requires_postprocessing());
3717 // If the target performs relaxation, we delay filler generation until now.
3718 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3720 unsigned char* buffer
= this->postprocessing_buffer();
3721 for (Fill_list::iterator p
= this->fills_
.begin();
3722 p
!= this->fills_
.end();
3725 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3726 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
3730 off_t off
= this->first_input_offset_
;
3731 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3732 p
!= this->input_sections_
.end();
3735 off_t aligned_off
= align_address(off
, p
->addralign());
3736 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3738 size_t fill_len
= aligned_off
- off
;
3739 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3740 memcpy(buffer
+ off
, fill_data
.data(), fill_data
.size());
3743 p
->write_to_buffer(buffer
+ aligned_off
);
3744 off
= aligned_off
+ p
->data_size();
3748 // Get the input sections for linker script processing. We leave
3749 // behind the Output_section_data entries. Note that this may be
3750 // slightly incorrect for merge sections. We will leave them behind,
3751 // but it is possible that the script says that they should follow
3752 // some other input sections, as in:
3753 // .rodata { *(.rodata) *(.rodata.cst*) }
3754 // For that matter, we don't handle this correctly:
3755 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3756 // With luck this will never matter.
3759 Output_section::get_input_sections(
3761 const std::string
& fill
,
3762 std::list
<Input_section
>* input_sections
)
3764 if (this->checkpoint_
!= NULL
3765 && !this->checkpoint_
->input_sections_saved())
3766 this->checkpoint_
->save_input_sections();
3768 // Invalidate fast look-up maps.
3769 this->lookup_maps_
->invalidate();
3771 uint64_t orig_address
= address
;
3773 address
= align_address(address
, this->addralign());
3775 Input_section_list remaining
;
3776 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3777 p
!= this->input_sections_
.end();
3780 if (p
->is_input_section()
3781 || p
->is_relaxed_input_section()
3782 || p
->is_merge_section())
3783 input_sections
->push_back(*p
);
3786 uint64_t aligned_address
= align_address(address
, p
->addralign());
3787 if (aligned_address
!= address
&& !fill
.empty())
3789 section_size_type length
=
3790 convert_to_section_size_type(aligned_address
- address
);
3791 std::string this_fill
;
3792 this_fill
.reserve(length
);
3793 while (this_fill
.length() + fill
.length() <= length
)
3795 if (this_fill
.length() < length
)
3796 this_fill
.append(fill
, 0, length
- this_fill
.length());
3798 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
3799 remaining
.push_back(Input_section(posd
));
3801 address
= aligned_address
;
3803 remaining
.push_back(*p
);
3805 p
->finalize_data_size();
3806 address
+= p
->data_size();
3810 this->input_sections_
.swap(remaining
);
3811 this->first_input_offset_
= 0;
3813 uint64_t data_size
= address
- orig_address
;
3814 this->set_current_data_size_for_child(data_size
);
3818 // Add a script input section. SIS is an Output_section::Input_section,
3819 // which can be either a plain input section or a special input section like
3820 // a relaxed input section. For a special input section, its size must be
3824 Output_section::add_script_input_section(const Input_section
& sis
)
3826 uint64_t data_size
= sis
.data_size();
3827 uint64_t addralign
= sis
.addralign();
3828 if (addralign
> this->addralign_
)
3829 this->addralign_
= addralign
;
3831 off_t offset_in_section
= this->current_data_size_for_child();
3832 off_t aligned_offset_in_section
= align_address(offset_in_section
,
3835 this->set_current_data_size_for_child(aligned_offset_in_section
3838 this->input_sections_
.push_back(sis
);
3840 // Update fast lookup maps if necessary.
3841 if (this->lookup_maps_
->is_valid())
3843 if (sis
.is_merge_section())
3845 Output_merge_base
* pomb
= sis
.output_merge_base();
3846 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
3848 this->lookup_maps_
->add_merge_section(msp
, pomb
);
3849 for (Output_merge_base::Input_sections::const_iterator p
=
3850 pomb
->input_sections_begin();
3851 p
!= pomb
->input_sections_end();
3853 this->lookup_maps_
->add_merge_input_section(p
->first
, p
->second
,
3856 else if (sis
.is_relaxed_input_section())
3858 Output_relaxed_input_section
* poris
= sis
.relaxed_input_section();
3859 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
3860 poris
->shndx(), poris
);
3865 // Save states for relaxation.
3868 Output_section::save_states()
3870 gold_assert(this->checkpoint_
== NULL
);
3871 Checkpoint_output_section
* checkpoint
=
3872 new Checkpoint_output_section(this->addralign_
, this->flags_
,
3873 this->input_sections_
,
3874 this->first_input_offset_
,
3875 this->attached_input_sections_are_sorted_
);
3876 this->checkpoint_
= checkpoint
;
3877 gold_assert(this->fills_
.empty());
3881 Output_section::discard_states()
3883 gold_assert(this->checkpoint_
!= NULL
);
3884 delete this->checkpoint_
;
3885 this->checkpoint_
= NULL
;
3886 gold_assert(this->fills_
.empty());
3888 // Simply invalidate the fast lookup maps since we do not keep
3890 this->lookup_maps_
->invalidate();
3894 Output_section::restore_states()
3896 gold_assert(this->checkpoint_
!= NULL
);
3897 Checkpoint_output_section
* checkpoint
= this->checkpoint_
;
3899 this->addralign_
= checkpoint
->addralign();
3900 this->flags_
= checkpoint
->flags();
3901 this->first_input_offset_
= checkpoint
->first_input_offset();
3903 if (!checkpoint
->input_sections_saved())
3905 // If we have not copied the input sections, just resize it.
3906 size_t old_size
= checkpoint
->input_sections_size();
3907 gold_assert(this->input_sections_
.size() >= old_size
);
3908 this->input_sections_
.resize(old_size
);
3912 // We need to copy the whole list. This is not efficient for
3913 // extremely large output with hundreads of thousands of input
3914 // objects. We may need to re-think how we should pass sections
3916 this->input_sections_
= *checkpoint
->input_sections();
3919 this->attached_input_sections_are_sorted_
=
3920 checkpoint
->attached_input_sections_are_sorted();
3922 // Simply invalidate the fast lookup maps since we do not keep
3924 this->lookup_maps_
->invalidate();
3927 // Update the section offsets of input sections in this. This is required if
3928 // relaxation causes some input sections to change sizes.
3931 Output_section::adjust_section_offsets()
3933 if (!this->section_offsets_need_adjustment_
)
3937 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3938 p
!= this->input_sections_
.end();
3941 off
= align_address(off
, p
->addralign());
3942 if (p
->is_input_section())
3943 p
->relobj()->set_section_offset(p
->shndx(), off
);
3944 off
+= p
->data_size();
3947 this->section_offsets_need_adjustment_
= false;
3950 // Print to the map file.
3953 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
3955 mapfile
->print_output_section(this);
3957 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3958 p
!= this->input_sections_
.end();
3960 p
->print_to_mapfile(mapfile
);
3963 // Print stats for merge sections to stderr.
3966 Output_section::print_merge_stats()
3968 Input_section_list::iterator p
;
3969 for (p
= this->input_sections_
.begin();
3970 p
!= this->input_sections_
.end();
3972 p
->print_merge_stats(this->name_
);
3975 // Set a fixed layout for the section. Used for incremental update links.
3978 Output_section::set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
,
3979 off_t sh_size
, uint64_t sh_addralign
)
3981 this->addralign_
= sh_addralign
;
3982 this->set_current_data_size(sh_size
);
3983 if ((this->flags_
& elfcpp::SHF_ALLOC
) != 0)
3984 this->set_address(sh_addr
);
3985 this->set_file_offset(sh_offset
);
3986 this->finalize_data_size();
3987 this->free_list_
.init(sh_size
, false);
3988 this->has_fixed_layout_
= true;
3991 // Reserve space within the fixed layout for the section. Used for
3992 // incremental update links.
3995 Output_section::reserve(uint64_t sh_offset
, uint64_t sh_size
)
3997 this->free_list_
.remove(sh_offset
, sh_offset
+ sh_size
);
4000 // Allocate space from the free list for the section. Used for
4001 // incremental update links.
4004 Output_section::allocate(off_t len
, uint64_t addralign
)
4006 return this->free_list_
.allocate(len
, addralign
, 0);
4009 // Output segment methods.
4011 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
4021 is_max_align_known_(false),
4022 are_addresses_set_(false),
4023 is_large_data_segment_(false)
4025 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4027 if (type
== elfcpp::PT_TLS
)
4028 this->flags_
= elfcpp::PF_R
;
4031 // Add an Output_section to a PT_LOAD Output_segment.
4034 Output_segment::add_output_section_to_load(Layout
* layout
,
4036 elfcpp::Elf_Word seg_flags
)
4038 gold_assert(this->type() == elfcpp::PT_LOAD
);
4039 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
4040 gold_assert(!this->is_max_align_known_
);
4041 gold_assert(os
->is_large_data_section() == this->is_large_data_segment());
4043 this->update_flags_for_output_section(seg_flags
);
4045 // We don't want to change the ordering if we have a linker script
4046 // with a SECTIONS clause.
4047 Output_section_order order
= os
->order();
4048 if (layout
->script_options()->saw_sections_clause())
4049 order
= static_cast<Output_section_order
>(0);
4051 gold_assert(order
!= ORDER_INVALID
);
4053 this->output_lists_
[order
].push_back(os
);
4056 // Add an Output_section to a non-PT_LOAD Output_segment.
4059 Output_segment::add_output_section_to_nonload(Output_section
* os
,
4060 elfcpp::Elf_Word seg_flags
)
4062 gold_assert(this->type() != elfcpp::PT_LOAD
);
4063 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
4064 gold_assert(!this->is_max_align_known_
);
4066 this->update_flags_for_output_section(seg_flags
);
4068 this->output_lists_
[0].push_back(os
);
4071 // Remove an Output_section from this segment. It is an error if it
4075 Output_segment::remove_output_section(Output_section
* os
)
4077 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4079 Output_data_list
* pdl
= &this->output_lists_
[i
];
4080 for (Output_data_list::iterator p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4092 // Add an Output_data (which need not be an Output_section) to the
4093 // start of a segment.
4096 Output_segment::add_initial_output_data(Output_data
* od
)
4098 gold_assert(!this->is_max_align_known_
);
4099 Output_data_list::iterator p
= this->output_lists_
[0].begin();
4100 this->output_lists_
[0].insert(p
, od
);
4103 // Return true if this segment has any sections which hold actual
4104 // data, rather than being a BSS section.
4107 Output_segment::has_any_data_sections() const
4109 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4111 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4112 for (Output_data_list::const_iterator p
= pdl
->begin();
4116 if (!(*p
)->is_section())
4118 if ((*p
)->output_section()->type() != elfcpp::SHT_NOBITS
)
4125 // Return whether the first data section (not counting TLS sections)
4126 // is a relro section.
4129 Output_segment::is_first_section_relro() const
4131 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4133 if (i
== static_cast<int>(ORDER_TLS_DATA
)
4134 || i
== static_cast<int>(ORDER_TLS_BSS
))
4136 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4139 Output_data
* p
= pdl
->front();
4140 return p
->is_section() && p
->output_section()->is_relro();
4146 // Return the maximum alignment of the Output_data in Output_segment.
4149 Output_segment::maximum_alignment()
4151 if (!this->is_max_align_known_
)
4153 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4155 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4156 uint64_t addralign
= Output_segment::maximum_alignment_list(pdl
);
4157 if (addralign
> this->max_align_
)
4158 this->max_align_
= addralign
;
4160 this->is_max_align_known_
= true;
4163 return this->max_align_
;
4166 // Return the maximum alignment of a list of Output_data.
4169 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
4172 for (Output_data_list::const_iterator p
= pdl
->begin();
4176 uint64_t addralign
= (*p
)->addralign();
4177 if (addralign
> ret
)
4183 // Return whether this segment has any dynamic relocs.
4186 Output_segment::has_dynamic_reloc() const
4188 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4189 if (this->has_dynamic_reloc_list(&this->output_lists_
[i
]))
4194 // Return whether this Output_data_list has any dynamic relocs.
4197 Output_segment::has_dynamic_reloc_list(const Output_data_list
* pdl
) const
4199 for (Output_data_list::const_iterator p
= pdl
->begin();
4202 if ((*p
)->has_dynamic_reloc())
4207 // Set the section addresses for an Output_segment. If RESET is true,
4208 // reset the addresses first. ADDR is the address and *POFF is the
4209 // file offset. Set the section indexes starting with *PSHNDX.
4210 // INCREASE_RELRO is the size of the portion of the first non-relro
4211 // section that should be included in the PT_GNU_RELRO segment.
4212 // If this segment has relro sections, and has been aligned for
4213 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4214 // the immediately following segment. Update *HAS_RELRO, *POFF,
4218 Output_segment::set_section_addresses(Layout
* layout
, bool reset
,
4220 unsigned int* increase_relro
,
4223 unsigned int* pshndx
)
4225 gold_assert(this->type_
== elfcpp::PT_LOAD
);
4227 uint64_t last_relro_pad
= 0;
4228 off_t orig_off
= *poff
;
4230 bool in_tls
= false;
4232 // If we have relro sections, we need to pad forward now so that the
4233 // relro sections plus INCREASE_RELRO end on a common page boundary.
4234 if (parameters
->options().relro()
4235 && this->is_first_section_relro()
4236 && (!this->are_addresses_set_
|| reset
))
4238 uint64_t relro_size
= 0;
4240 uint64_t max_align
= 0;
4241 for (int i
= 0; i
<= static_cast<int>(ORDER_RELRO_LAST
); ++i
)
4243 Output_data_list
* pdl
= &this->output_lists_
[i
];
4244 Output_data_list::iterator p
;
4245 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4247 if (!(*p
)->is_section())
4249 uint64_t align
= (*p
)->addralign();
4250 if (align
> max_align
)
4252 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4256 // Align the first non-TLS section to the alignment
4257 // of the TLS segment.
4261 relro_size
= align_address(relro_size
, align
);
4262 // Ignore the size of the .tbss section.
4263 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4264 && (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4266 if ((*p
)->is_address_valid())
4267 relro_size
+= (*p
)->data_size();
4270 // FIXME: This could be faster.
4271 (*p
)->set_address_and_file_offset(addr
+ relro_size
,
4273 relro_size
+= (*p
)->data_size();
4274 (*p
)->reset_address_and_file_offset();
4277 if (p
!= pdl
->end())
4280 relro_size
+= *increase_relro
;
4281 // Pad the total relro size to a multiple of the maximum
4282 // section alignment seen.
4283 uint64_t aligned_size
= align_address(relro_size
, max_align
);
4284 // Note the amount of padding added after the last relro section.
4285 last_relro_pad
= aligned_size
- relro_size
;
4288 uint64_t page_align
= parameters
->target().common_pagesize();
4290 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4291 uint64_t desired_align
= page_align
- (aligned_size
% page_align
);
4292 if (desired_align
< *poff
% page_align
)
4293 *poff
+= page_align
- *poff
% page_align
;
4294 *poff
+= desired_align
- *poff
% page_align
;
4295 addr
+= *poff
- orig_off
;
4299 if (!reset
&& this->are_addresses_set_
)
4301 gold_assert(this->paddr_
== addr
);
4302 addr
= this->vaddr_
;
4306 this->vaddr_
= addr
;
4307 this->paddr_
= addr
;
4308 this->are_addresses_set_
= true;
4313 this->offset_
= orig_off
;
4317 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4319 if (i
== static_cast<int>(ORDER_RELRO_LAST
))
4321 *poff
+= last_relro_pad
;
4322 addr
+= last_relro_pad
;
4323 if (this->output_lists_
[i
].empty())
4325 // If there is nothing in the ORDER_RELRO_LAST list,
4326 // the padding will occur at the end of the relro
4327 // segment, and we need to add it to *INCREASE_RELRO.
4328 *increase_relro
+= last_relro_pad
;
4331 addr
= this->set_section_list_addresses(layout
, reset
,
4332 &this->output_lists_
[i
],
4333 addr
, poff
, pshndx
, &in_tls
);
4334 if (i
< static_cast<int>(ORDER_SMALL_BSS
))
4336 this->filesz_
= *poff
- orig_off
;
4343 // If the last section was a TLS section, align upward to the
4344 // alignment of the TLS segment, so that the overall size of the TLS
4345 // segment is aligned.
4348 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
4349 *poff
= align_address(*poff
, segment_align
);
4352 this->memsz_
= *poff
- orig_off
;
4354 // Ignore the file offset adjustments made by the BSS Output_data
4361 // Set the addresses and file offsets in a list of Output_data
4365 Output_segment::set_section_list_addresses(Layout
* layout
, bool reset
,
4366 Output_data_list
* pdl
,
4367 uint64_t addr
, off_t
* poff
,
4368 unsigned int* pshndx
,
4371 off_t startoff
= *poff
;
4372 // For incremental updates, we may allocate non-fixed sections from
4373 // free space in the file. This keeps track of the high-water mark.
4374 off_t maxoff
= startoff
;
4376 off_t off
= startoff
;
4377 for (Output_data_list::iterator p
= pdl
->begin();
4382 (*p
)->reset_address_and_file_offset();
4384 // When doing an incremental update or when using a linker script,
4385 // the section will most likely already have an address.
4386 if (!(*p
)->is_address_valid())
4388 uint64_t align
= (*p
)->addralign();
4390 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4392 // Give the first TLS section the alignment of the
4393 // entire TLS segment. Otherwise the TLS segment as a
4394 // whole may be misaligned.
4397 Output_segment
* tls_segment
= layout
->tls_segment();
4398 gold_assert(tls_segment
!= NULL
);
4399 uint64_t segment_align
= tls_segment
->maximum_alignment();
4400 gold_assert(segment_align
>= align
);
4401 align
= segment_align
;
4408 // If this is the first section after the TLS segment,
4409 // align it to at least the alignment of the TLS
4410 // segment, so that the size of the overall TLS segment
4414 uint64_t segment_align
=
4415 layout
->tls_segment()->maximum_alignment();
4416 if (segment_align
> align
)
4417 align
= segment_align
;
4423 if (!parameters
->incremental_update())
4425 off
= align_address(off
, align
);
4426 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4430 // Incremental update: allocate file space from free list.
4431 (*p
)->pre_finalize_data_size();
4432 off_t current_size
= (*p
)->current_data_size();
4433 off
= layout
->allocate(current_size
, align
, startoff
);
4436 gold_assert((*p
)->output_section() != NULL
);
4437 gold_fallback(_("out of patch space for section %s; "
4438 "relink with --incremental-full"),
4439 (*p
)->output_section()->name());
4441 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4442 if ((*p
)->data_size() > current_size
)
4444 gold_assert((*p
)->output_section() != NULL
);
4445 gold_fallback(_("%s: section changed size; "
4446 "relink with --incremental-full"),
4447 (*p
)->output_section()->name());
4451 else if (parameters
->incremental_update())
4453 // For incremental updates, use the fixed offset for the
4454 // high-water mark computation.
4455 off
= (*p
)->offset();
4459 // The script may have inserted a skip forward, but it
4460 // better not have moved backward.
4461 if ((*p
)->address() >= addr
+ (off
- startoff
))
4462 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
4465 if (!layout
->script_options()->saw_sections_clause())
4469 Output_section
* os
= (*p
)->output_section();
4471 // Cast to unsigned long long to avoid format warnings.
4472 unsigned long long previous_dot
=
4473 static_cast<unsigned long long>(addr
+ (off
- startoff
));
4474 unsigned long long dot
=
4475 static_cast<unsigned long long>((*p
)->address());
4478 gold_error(_("dot moves backward in linker script "
4479 "from 0x%llx to 0x%llx"), previous_dot
, dot
);
4481 gold_error(_("address of section '%s' moves backward "
4482 "from 0x%llx to 0x%llx"),
4483 os
->name(), previous_dot
, dot
);
4486 (*p
)->set_file_offset(off
);
4487 (*p
)->finalize_data_size();
4490 if (parameters
->incremental_update())
4491 gold_debug(DEBUG_INCREMENTAL
,
4492 "set_section_list_addresses: %08lx %08lx %s",
4493 static_cast<long>(off
),
4494 static_cast<long>((*p
)->data_size()),
4495 ((*p
)->output_section() != NULL
4496 ? (*p
)->output_section()->name() : "(special)"));
4498 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4499 // section. Such a section does not affect the size of a
4501 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4502 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4503 off
+= (*p
)->data_size();
4508 if ((*p
)->is_section())
4510 (*p
)->set_out_shndx(*pshndx
);
4516 return addr
+ (maxoff
- startoff
);
4519 // For a non-PT_LOAD segment, set the offset from the sections, if
4520 // any. Add INCREASE to the file size and the memory size.
4523 Output_segment::set_offset(unsigned int increase
)
4525 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
4527 gold_assert(!this->are_addresses_set_
);
4529 // A non-load section only uses output_lists_[0].
4531 Output_data_list
* pdl
= &this->output_lists_
[0];
4535 gold_assert(increase
== 0);
4538 this->are_addresses_set_
= true;
4540 this->min_p_align_
= 0;
4546 // Find the first and last section by address.
4547 const Output_data
* first
= NULL
;
4548 const Output_data
* last_data
= NULL
;
4549 const Output_data
* last_bss
= NULL
;
4550 for (Output_data_list::const_iterator p
= pdl
->begin();
4555 || (*p
)->address() < first
->address()
4556 || ((*p
)->address() == first
->address()
4557 && (*p
)->data_size() < first
->data_size()))
4559 const Output_data
** plast
;
4560 if ((*p
)->is_section()
4561 && (*p
)->output_section()->type() == elfcpp::SHT_NOBITS
)
4566 || (*p
)->address() > (*plast
)->address()
4567 || ((*p
)->address() == (*plast
)->address()
4568 && (*p
)->data_size() > (*plast
)->data_size()))
4572 this->vaddr_
= first
->address();
4573 this->paddr_
= (first
->has_load_address()
4574 ? first
->load_address()
4576 this->are_addresses_set_
= true;
4577 this->offset_
= first
->offset();
4579 if (last_data
== NULL
)
4582 this->filesz_
= (last_data
->address()
4583 + last_data
->data_size()
4586 const Output_data
* last
= last_bss
!= NULL
? last_bss
: last_data
;
4587 this->memsz_
= (last
->address()
4591 this->filesz_
+= increase
;
4592 this->memsz_
+= increase
;
4594 // If this is a RELRO segment, verify that the segment ends at a
4596 if (this->type_
== elfcpp::PT_GNU_RELRO
)
4598 uint64_t page_align
= parameters
->target().common_pagesize();
4599 uint64_t segment_end
= this->vaddr_
+ this->memsz_
;
4600 if (parameters
->incremental_update())
4602 // The INCREASE_RELRO calculation is bypassed for an incremental
4603 // update, so we need to adjust the segment size manually here.
4604 segment_end
= align_address(segment_end
, page_align
);
4605 this->memsz_
= segment_end
- this->vaddr_
;
4608 gold_assert(segment_end
== align_address(segment_end
, page_align
));
4611 // If this is a TLS segment, align the memory size. The code in
4612 // set_section_list ensures that the section after the TLS segment
4613 // is aligned to give us room.
4614 if (this->type_
== elfcpp::PT_TLS
)
4616 uint64_t segment_align
= this->maximum_alignment();
4617 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
4618 this->memsz_
= align_address(this->memsz_
, segment_align
);
4622 // Set the TLS offsets of the sections in the PT_TLS segment.
4625 Output_segment::set_tls_offsets()
4627 gold_assert(this->type_
== elfcpp::PT_TLS
);
4629 for (Output_data_list::iterator p
= this->output_lists_
[0].begin();
4630 p
!= this->output_lists_
[0].end();
4632 (*p
)->set_tls_offset(this->vaddr_
);
4635 // Return the load address of the first section.
4638 Output_segment::first_section_load_address() const
4640 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4642 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4643 for (Output_data_list::const_iterator p
= pdl
->begin();
4647 if ((*p
)->is_section())
4648 return ((*p
)->has_load_address()
4649 ? (*p
)->load_address()
4656 // Return the number of Output_sections in an Output_segment.
4659 Output_segment::output_section_count() const
4661 unsigned int ret
= 0;
4662 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4663 ret
+= this->output_section_count_list(&this->output_lists_
[i
]);
4667 // Return the number of Output_sections in an Output_data_list.
4670 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
4672 unsigned int count
= 0;
4673 for (Output_data_list::const_iterator p
= pdl
->begin();
4677 if ((*p
)->is_section())
4683 // Return the section attached to the list segment with the lowest
4684 // load address. This is used when handling a PHDRS clause in a
4688 Output_segment::section_with_lowest_load_address() const
4690 Output_section
* found
= NULL
;
4691 uint64_t found_lma
= 0;
4692 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4693 this->lowest_load_address_in_list(&this->output_lists_
[i
], &found
,
4698 // Look through a list for a section with a lower load address.
4701 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
4702 Output_section
** found
,
4703 uint64_t* found_lma
) const
4705 for (Output_data_list::const_iterator p
= pdl
->begin();
4709 if (!(*p
)->is_section())
4711 Output_section
* os
= static_cast<Output_section
*>(*p
);
4712 uint64_t lma
= (os
->has_load_address()
4713 ? os
->load_address()
4715 if (*found
== NULL
|| lma
< *found_lma
)
4723 // Write the segment data into *OPHDR.
4725 template<int size
, bool big_endian
>
4727 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
4729 ophdr
->put_p_type(this->type_
);
4730 ophdr
->put_p_offset(this->offset_
);
4731 ophdr
->put_p_vaddr(this->vaddr_
);
4732 ophdr
->put_p_paddr(this->paddr_
);
4733 ophdr
->put_p_filesz(this->filesz_
);
4734 ophdr
->put_p_memsz(this->memsz_
);
4735 ophdr
->put_p_flags(this->flags_
);
4736 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
4739 // Write the section headers into V.
4741 template<int size
, bool big_endian
>
4743 Output_segment::write_section_headers(const Layout
* layout
,
4744 const Stringpool
* secnamepool
,
4746 unsigned int* pshndx
) const
4748 // Every section that is attached to a segment must be attached to a
4749 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4751 if (this->type_
!= elfcpp::PT_LOAD
)
4754 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4756 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4757 v
= this->write_section_headers_list
<size
, big_endian
>(layout
,
4766 template<int size
, bool big_endian
>
4768 Output_segment::write_section_headers_list(const Layout
* layout
,
4769 const Stringpool
* secnamepool
,
4770 const Output_data_list
* pdl
,
4772 unsigned int* pshndx
) const
4774 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
4775 for (Output_data_list::const_iterator p
= pdl
->begin();
4779 if ((*p
)->is_section())
4781 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
4782 gold_assert(*pshndx
== ps
->out_shndx());
4783 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
4784 ps
->write_header(layout
, secnamepool
, &oshdr
);
4792 // Print the output sections to the map file.
4795 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
4797 if (this->type() != elfcpp::PT_LOAD
)
4799 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4800 this->print_section_list_to_mapfile(mapfile
, &this->output_lists_
[i
]);
4803 // Print an output section list to the map file.
4806 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
4807 const Output_data_list
* pdl
) const
4809 for (Output_data_list::const_iterator p
= pdl
->begin();
4812 (*p
)->print_to_mapfile(mapfile
);
4815 // Output_file methods.
4817 Output_file::Output_file(const char* name
)
4822 map_is_anonymous_(false),
4823 map_is_allocated_(false),
4824 is_temporary_(false)
4828 // Try to open an existing file. Returns false if the file doesn't
4829 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4830 // NULL, open that file as the base for incremental linking, and
4831 // copy its contents to the new output file. This routine can
4832 // be called for incremental updates, in which case WRITABLE should
4833 // be true, or by the incremental-dump utility, in which case
4834 // WRITABLE should be false.
4837 Output_file::open_base_file(const char* base_name
, bool writable
)
4839 // The name "-" means "stdout".
4840 if (strcmp(this->name_
, "-") == 0)
4843 bool use_base_file
= base_name
!= NULL
;
4845 base_name
= this->name_
;
4846 else if (strcmp(base_name
, this->name_
) == 0)
4847 gold_fatal(_("%s: incremental base and output file name are the same"),
4850 // Don't bother opening files with a size of zero.
4852 if (::stat(base_name
, &s
) != 0)
4854 gold_info(_("%s: stat: %s"), base_name
, strerror(errno
));
4859 gold_info(_("%s: incremental base file is empty"), base_name
);
4863 // If we're using a base file, we want to open it read-only.
4867 int oflags
= writable
? O_RDWR
: O_RDONLY
;
4868 int o
= open_descriptor(-1, base_name
, oflags
, 0);
4871 gold_info(_("%s: open: %s"), base_name
, strerror(errno
));
4875 // If the base file and the output file are different, open a
4876 // new output file and read the contents from the base file into
4877 // the newly-mapped region.
4880 this->open(s
.st_size
);
4881 ssize_t len
= ::read(o
, this->base_
, s
.st_size
);
4884 gold_info(_("%s: read failed: %s"), base_name
, strerror(errno
));
4887 if (len
< s
.st_size
)
4889 gold_info(_("%s: file too short"), base_name
);
4897 this->file_size_
= s
.st_size
;
4899 if (!this->map_no_anonymous(writable
))
4901 release_descriptor(o
, true);
4903 this->file_size_
= 0;
4910 // Open the output file.
4913 Output_file::open(off_t file_size
)
4915 this->file_size_
= file_size
;
4917 // Unlink the file first; otherwise the open() may fail if the file
4918 // is busy (e.g. it's an executable that's currently being executed).
4920 // However, the linker may be part of a system where a zero-length
4921 // file is created for it to write to, with tight permissions (gcc
4922 // 2.95 did something like this). Unlinking the file would work
4923 // around those permission controls, so we only unlink if the file
4924 // has a non-zero size. We also unlink only regular files to avoid
4925 // trouble with directories/etc.
4927 // If we fail, continue; this command is merely a best-effort attempt
4928 // to improve the odds for open().
4930 // We let the name "-" mean "stdout"
4931 if (!this->is_temporary_
)
4933 if (strcmp(this->name_
, "-") == 0)
4934 this->o_
= STDOUT_FILENO
;
4938 if (::stat(this->name_
, &s
) == 0
4939 && (S_ISREG (s
.st_mode
) || S_ISLNK (s
.st_mode
)))
4942 ::unlink(this->name_
);
4943 else if (!parameters
->options().relocatable())
4945 // If we don't unlink the existing file, add execute
4946 // permission where read permissions already exist
4947 // and where the umask permits.
4948 int mask
= ::umask(0);
4950 s
.st_mode
|= (s
.st_mode
& 0444) >> 2;
4951 ::chmod(this->name_
, s
.st_mode
& ~mask
);
4955 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
4956 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
4959 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
4967 // Resize the output file.
4970 Output_file::resize(off_t file_size
)
4972 // If the mmap is mapping an anonymous memory buffer, this is easy:
4973 // just mremap to the new size. If it's mapping to a file, we want
4974 // to unmap to flush to the file, then remap after growing the file.
4975 if (this->map_is_anonymous_
)
4978 if (!this->map_is_allocated_
)
4980 base
= ::mremap(this->base_
, this->file_size_
, file_size
,
4982 if (base
== MAP_FAILED
)
4983 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
4987 base
= realloc(this->base_
, file_size
);
4990 if (file_size
> this->file_size_
)
4991 memset(static_cast<char*>(base
) + this->file_size_
, 0,
4992 file_size
- this->file_size_
);
4994 this->base_
= static_cast<unsigned char*>(base
);
4995 this->file_size_
= file_size
;
5000 this->file_size_
= file_size
;
5001 if (!this->map_no_anonymous(true))
5002 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
5006 // Map an anonymous block of memory which will later be written to the
5007 // file. Return whether the map succeeded.
5010 Output_file::map_anonymous()
5012 void* base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
5013 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
5014 if (base
== MAP_FAILED
)
5016 base
= malloc(this->file_size_
);
5019 memset(base
, 0, this->file_size_
);
5020 this->map_is_allocated_
= true;
5022 this->base_
= static_cast<unsigned char*>(base
);
5023 this->map_is_anonymous_
= true;
5027 // Map the file into memory. Return whether the mapping succeeded.
5028 // If WRITABLE is true, map with write access.
5031 Output_file::map_no_anonymous(bool writable
)
5033 const int o
= this->o_
;
5035 // If the output file is not a regular file, don't try to mmap it;
5036 // instead, we'll mmap a block of memory (an anonymous buffer), and
5037 // then later write the buffer to the file.
5039 struct stat statbuf
;
5040 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
5041 || ::fstat(o
, &statbuf
) != 0
5042 || !S_ISREG(statbuf
.st_mode
)
5043 || this->is_temporary_
)
5046 // Ensure that we have disk space available for the file. If we
5047 // don't do this, it is possible that we will call munmap, close,
5048 // and exit with dirty buffers still in the cache with no assigned
5049 // disk blocks. If the disk is out of space at that point, the
5050 // output file will wind up incomplete, but we will have already
5051 // exited. The alternative to fallocate would be to use fdatasync,
5052 // but that would be a more significant performance hit.
5053 if (writable
&& ::posix_fallocate(o
, 0, this->file_size_
) < 0)
5054 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
5056 // Map the file into memory.
5057 int prot
= PROT_READ
;
5060 base
= ::mmap(NULL
, this->file_size_
, prot
, MAP_SHARED
, o
, 0);
5062 // The mmap call might fail because of file system issues: the file
5063 // system might not support mmap at all, or it might not support
5064 // mmap with PROT_WRITE.
5065 if (base
== MAP_FAILED
)
5068 this->map_is_anonymous_
= false;
5069 this->base_
= static_cast<unsigned char*>(base
);
5073 // Map the file into memory.
5078 if (this->map_no_anonymous(true))
5081 // The mmap call might fail because of file system issues: the file
5082 // system might not support mmap at all, or it might not support
5083 // mmap with PROT_WRITE. I'm not sure which errno values we will
5084 // see in all cases, so if the mmap fails for any reason and we
5085 // don't care about file contents, try for an anonymous map.
5086 if (this->map_anonymous())
5089 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5090 this->name_
, static_cast<unsigned long>(this->file_size_
),
5094 // Unmap the file from memory.
5097 Output_file::unmap()
5099 if (this->map_is_anonymous_
)
5101 // We've already written out the data, so there is no reason to
5102 // waste time unmapping or freeing the memory.
5106 if (::munmap(this->base_
, this->file_size_
) < 0)
5107 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
5112 // Close the output file.
5115 Output_file::close()
5117 // If the map isn't file-backed, we need to write it now.
5118 if (this->map_is_anonymous_
&& !this->is_temporary_
)
5120 size_t bytes_to_write
= this->file_size_
;
5122 while (bytes_to_write
> 0)
5124 ssize_t bytes_written
= ::write(this->o_
, this->base_
+ offset
,
5126 if (bytes_written
== 0)
5127 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
5128 else if (bytes_written
< 0)
5129 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
5132 bytes_to_write
-= bytes_written
;
5133 offset
+= bytes_written
;
5139 // We don't close stdout or stderr
5140 if (this->o_
!= STDOUT_FILENO
5141 && this->o_
!= STDERR_FILENO
5142 && !this->is_temporary_
)
5143 if (::close(this->o_
) < 0)
5144 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
5148 // Instantiate the templates we need. We could use the configure
5149 // script to restrict this to only the ones for implemented targets.
5151 #ifdef HAVE_TARGET_32_LITTLE
5154 Output_section::add_input_section
<32, false>(
5156 Sized_relobj_file
<32, false>* object
,
5158 const char* secname
,
5159 const elfcpp::Shdr
<32, false>& shdr
,
5160 unsigned int reloc_shndx
,
5161 bool have_sections_script
);
5164 #ifdef HAVE_TARGET_32_BIG
5167 Output_section::add_input_section
<32, true>(
5169 Sized_relobj_file
<32, true>* object
,
5171 const char* secname
,
5172 const elfcpp::Shdr
<32, true>& shdr
,
5173 unsigned int reloc_shndx
,
5174 bool have_sections_script
);
5177 #ifdef HAVE_TARGET_64_LITTLE
5180 Output_section::add_input_section
<64, false>(
5182 Sized_relobj_file
<64, false>* object
,
5184 const char* secname
,
5185 const elfcpp::Shdr
<64, false>& shdr
,
5186 unsigned int reloc_shndx
,
5187 bool have_sections_script
);
5190 #ifdef HAVE_TARGET_64_BIG
5193 Output_section::add_input_section
<64, true>(
5195 Sized_relobj_file
<64, true>* object
,
5197 const char* secname
,
5198 const elfcpp::Shdr
<64, true>& shdr
,
5199 unsigned int reloc_shndx
,
5200 bool have_sections_script
);
5203 #ifdef HAVE_TARGET_32_LITTLE
5205 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5208 #ifdef HAVE_TARGET_32_BIG
5210 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5213 #ifdef HAVE_TARGET_64_LITTLE
5215 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5218 #ifdef HAVE_TARGET_64_BIG
5220 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5223 #ifdef HAVE_TARGET_32_LITTLE
5225 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5228 #ifdef HAVE_TARGET_32_BIG
5230 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5233 #ifdef HAVE_TARGET_64_LITTLE
5235 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5238 #ifdef HAVE_TARGET_64_BIG
5240 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5243 #ifdef HAVE_TARGET_32_LITTLE
5245 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5248 #ifdef HAVE_TARGET_32_BIG
5250 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5253 #ifdef HAVE_TARGET_64_LITTLE
5255 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5258 #ifdef HAVE_TARGET_64_BIG
5260 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5263 #ifdef HAVE_TARGET_32_LITTLE
5265 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5268 #ifdef HAVE_TARGET_32_BIG
5270 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5273 #ifdef HAVE_TARGET_64_LITTLE
5275 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5278 #ifdef HAVE_TARGET_64_BIG
5280 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5283 #ifdef HAVE_TARGET_32_LITTLE
5285 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5288 #ifdef HAVE_TARGET_32_BIG
5290 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5293 #ifdef HAVE_TARGET_64_LITTLE
5295 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5298 #ifdef HAVE_TARGET_64_BIG
5300 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5303 #ifdef HAVE_TARGET_32_LITTLE
5305 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5308 #ifdef HAVE_TARGET_32_BIG
5310 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5313 #ifdef HAVE_TARGET_64_LITTLE
5315 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5318 #ifdef HAVE_TARGET_64_BIG
5320 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5323 #ifdef HAVE_TARGET_32_LITTLE
5325 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5328 #ifdef HAVE_TARGET_32_BIG
5330 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5333 #ifdef HAVE_TARGET_64_LITTLE
5335 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5338 #ifdef HAVE_TARGET_64_BIG
5340 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5343 #ifdef HAVE_TARGET_32_LITTLE
5345 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5348 #ifdef HAVE_TARGET_32_BIG
5350 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5353 #ifdef HAVE_TARGET_64_LITTLE
5355 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5358 #ifdef HAVE_TARGET_64_BIG
5360 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5363 #ifdef HAVE_TARGET_32_LITTLE
5365 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
5368 #ifdef HAVE_TARGET_32_BIG
5370 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
5373 #ifdef HAVE_TARGET_64_LITTLE
5375 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
5378 #ifdef HAVE_TARGET_64_BIG
5380 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
5383 #ifdef HAVE_TARGET_32_LITTLE
5385 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
5388 #ifdef HAVE_TARGET_32_BIG
5390 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
5393 #ifdef HAVE_TARGET_64_LITTLE
5395 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
5398 #ifdef HAVE_TARGET_64_BIG
5400 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
5403 #ifdef HAVE_TARGET_32_LITTLE
5405 class Output_data_group
<32, false>;
5408 #ifdef HAVE_TARGET_32_BIG
5410 class Output_data_group
<32, true>;
5413 #ifdef HAVE_TARGET_64_LITTLE
5415 class Output_data_group
<64, false>;
5418 #ifdef HAVE_TARGET_64_BIG
5420 class Output_data_group
<64, true>;
5423 #ifdef HAVE_TARGET_32_LITTLE
5425 class Output_data_got
<32, false>;
5428 #ifdef HAVE_TARGET_32_BIG
5430 class Output_data_got
<32, true>;
5433 #ifdef HAVE_TARGET_64_LITTLE
5435 class Output_data_got
<64, false>;
5438 #ifdef HAVE_TARGET_64_BIG
5440 class Output_data_got
<64, true>;
5443 } // End namespace gold.