1 // output.cc -- manage the output file for gold
3 // Copyright (C) 2006-2024 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.
34 #ifdef HAVE_SYS_MMAN_H
38 #include "libiberty.h"
41 #include "parameters.h"
46 #include "descriptors.h"
50 // For systems without mmap support.
52 # define mmap gold_mmap
53 # define munmap gold_munmap
54 # define mremap gold_mremap
56 # define MAP_FAILED (reinterpret_cast<void*>(-1))
65 # define MAP_PRIVATE 0
67 # ifndef MAP_ANONYMOUS
68 # define MAP_ANONYMOUS 0
75 # define ENOSYS EINVAL
79 gold_mmap(void *, size_t, int, int, int, off_t
)
86 gold_munmap(void *, size_t)
93 gold_mremap(void *, size_t, size_t, int)
101 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
102 # define mremap gold_mremap
103 extern "C" void *gold_mremap(void *, size_t, size_t, int);
106 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
107 #ifndef MAP_ANONYMOUS
108 # define MAP_ANONYMOUS MAP_ANON
111 #ifndef MREMAP_MAYMOVE
112 # define MREMAP_MAYMOVE 1
115 // Mingw does not have S_ISLNK.
117 # define S_ISLNK(mode) 0
123 // A wrapper around posix_fallocate. If we don't have posix_fallocate,
124 // or the --no-posix-fallocate option is set, we try the fallocate
125 // system call directly. If that fails, we use ftruncate to set
126 // the file size and hope that there is enough disk space.
129 gold_fallocate(int o
, off_t offset
, off_t len
)
134 #ifdef HAVE_POSIX_FALLOCATE
135 if (parameters
->options().posix_fallocate())
137 int err
= ::posix_fallocate(o
, offset
, len
);
138 if (err
!= EINVAL
&& err
!= ENOSYS
&& err
!= EOPNOTSUPP
)
141 #endif // defined(HAVE_POSIX_FALLOCATE)
143 #ifdef HAVE_FALLOCATE
146 int err
= ::fallocate(o
, 0, offset
, len
);
147 if (err
< 0 && errno
!= EINVAL
&& errno
!= ENOSYS
&& errno
!= EOPNOTSUPP
)
150 #endif // defined(HAVE_FALLOCATE)
153 if (::ftruncate(o
, offset
+ len
) < 0)
158 // Output_data variables.
160 bool Output_data::allocated_sizes_are_fixed
;
162 // Output_data methods.
164 Output_data::~Output_data()
168 // Return the default alignment for the target size.
171 Output_data::default_alignment()
173 return Output_data::default_alignment_for_size(
174 parameters
->target().get_size());
177 // Return the default alignment for a size--32 or 64.
180 Output_data::default_alignment_for_size(int size
)
190 // Output_section_header methods. This currently assumes that the
191 // segment and section lists are complete at construction time.
193 Output_section_headers::Output_section_headers(
194 const Layout
* layout
,
195 const Layout::Segment_list
* segment_list
,
196 const Layout::Section_list
* section_list
,
197 const Layout::Section_list
* unattached_section_list
,
198 const Stringpool
* secnamepool
,
199 const Output_section
* shstrtab_section
)
201 segment_list_(segment_list
),
202 section_list_(section_list
),
203 unattached_section_list_(unattached_section_list
),
204 secnamepool_(secnamepool
),
205 shstrtab_section_(shstrtab_section
)
209 // Compute the current data size.
212 Output_section_headers::do_size() const
214 // Count all the sections. Start with 1 for the null section.
216 if (!parameters
->options().relocatable())
218 for (Layout::Segment_list::const_iterator p
=
219 this->segment_list_
->begin();
220 p
!= this->segment_list_
->end();
222 if ((*p
)->type() == elfcpp::PT_LOAD
)
223 count
+= (*p
)->output_section_count();
227 for (Layout::Section_list::const_iterator p
=
228 this->section_list_
->begin();
229 p
!= this->section_list_
->end();
231 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
234 count
+= this->unattached_section_list_
->size();
236 const int size
= parameters
->target().get_size();
239 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
241 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
245 return count
* shdr_size
;
248 // Write out the section headers.
251 Output_section_headers::do_write(Output_file
* of
)
253 switch (parameters
->size_and_endianness())
255 #ifdef HAVE_TARGET_32_LITTLE
256 case Parameters::TARGET_32_LITTLE
:
257 this->do_sized_write
<32, false>(of
);
260 #ifdef HAVE_TARGET_32_BIG
261 case Parameters::TARGET_32_BIG
:
262 this->do_sized_write
<32, true>(of
);
265 #ifdef HAVE_TARGET_64_LITTLE
266 case Parameters::TARGET_64_LITTLE
:
267 this->do_sized_write
<64, false>(of
);
270 #ifdef HAVE_TARGET_64_BIG
271 case Parameters::TARGET_64_BIG
:
272 this->do_sized_write
<64, true>(of
);
280 template<int size
, bool big_endian
>
282 Output_section_headers::do_sized_write(Output_file
* of
)
284 off_t all_shdrs_size
= this->data_size();
285 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
287 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
288 unsigned char* v
= view
;
291 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
292 oshdr
.put_sh_name(0);
293 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
294 oshdr
.put_sh_flags(0);
295 oshdr
.put_sh_addr(0);
296 oshdr
.put_sh_offset(0);
298 size_t section_count
= (this->data_size()
299 / elfcpp::Elf_sizes
<size
>::shdr_size
);
300 if (section_count
< elfcpp::SHN_LORESERVE
)
301 oshdr
.put_sh_size(0);
303 oshdr
.put_sh_size(section_count
);
305 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
306 if (shstrndx
< elfcpp::SHN_LORESERVE
)
307 oshdr
.put_sh_link(0);
309 oshdr
.put_sh_link(shstrndx
);
311 size_t segment_count
= this->segment_list_
->size();
312 oshdr
.put_sh_info(segment_count
>= elfcpp::PN_XNUM
? segment_count
: 0);
314 oshdr
.put_sh_addralign(0);
315 oshdr
.put_sh_entsize(0);
320 unsigned int shndx
= 1;
321 if (!parameters
->options().relocatable())
323 for (Layout::Segment_list::const_iterator p
=
324 this->segment_list_
->begin();
325 p
!= this->segment_list_
->end();
327 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
334 for (Layout::Section_list::const_iterator p
=
335 this->section_list_
->begin();
336 p
!= this->section_list_
->end();
339 // We do unallocated sections below, except that group
340 // sections have to come first.
341 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
342 && (*p
)->type() != elfcpp::SHT_GROUP
)
344 gold_assert(shndx
== (*p
)->out_shndx());
345 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
346 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
352 for (Layout::Section_list::const_iterator p
=
353 this->unattached_section_list_
->begin();
354 p
!= this->unattached_section_list_
->end();
357 // For a relocatable link, we did unallocated group sections
358 // above, since they have to come first.
359 if ((*p
)->type() == elfcpp::SHT_GROUP
360 && parameters
->options().relocatable())
362 gold_assert(shndx
== (*p
)->out_shndx());
363 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
364 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
369 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
372 // Output_segment_header methods.
374 Output_segment_headers::Output_segment_headers(
375 const Layout::Segment_list
& segment_list
)
376 : segment_list_(segment_list
)
378 this->set_current_data_size_for_child(this->do_size());
382 Output_segment_headers::do_write(Output_file
* of
)
384 switch (parameters
->size_and_endianness())
386 #ifdef HAVE_TARGET_32_LITTLE
387 case Parameters::TARGET_32_LITTLE
:
388 this->do_sized_write
<32, false>(of
);
391 #ifdef HAVE_TARGET_32_BIG
392 case Parameters::TARGET_32_BIG
:
393 this->do_sized_write
<32, true>(of
);
396 #ifdef HAVE_TARGET_64_LITTLE
397 case Parameters::TARGET_64_LITTLE
:
398 this->do_sized_write
<64, false>(of
);
401 #ifdef HAVE_TARGET_64_BIG
402 case Parameters::TARGET_64_BIG
:
403 this->do_sized_write
<64, true>(of
);
411 template<int size
, bool big_endian
>
413 Output_segment_headers::do_sized_write(Output_file
* of
)
415 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
416 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
417 gold_assert(all_phdrs_size
== this->data_size());
418 unsigned char* view
= of
->get_output_view(this->offset(),
420 unsigned char* v
= view
;
421 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
422 p
!= this->segment_list_
.end();
425 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
426 (*p
)->write_header(&ophdr
);
430 gold_assert(v
- view
== all_phdrs_size
);
432 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
436 Output_segment_headers::do_size() const
438 const int size
= parameters
->target().get_size();
441 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
443 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
447 return this->segment_list_
.size() * phdr_size
;
450 // Output_file_header methods.
452 Output_file_header::Output_file_header(Target
* target
,
453 const Symbol_table
* symtab
,
454 const Output_segment_headers
* osh
)
457 segment_header_(osh
),
458 section_header_(NULL
),
461 this->set_data_size(this->do_size());
464 // Set the section table information for a file header.
467 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
468 const Output_section
* shstrtab
)
470 this->section_header_
= shdrs
;
471 this->shstrtab_
= shstrtab
;
474 // Write out the file header.
477 Output_file_header::do_write(Output_file
* of
)
479 gold_assert(this->offset() == 0);
481 switch (parameters
->size_and_endianness())
483 #ifdef HAVE_TARGET_32_LITTLE
484 case Parameters::TARGET_32_LITTLE
:
485 this->do_sized_write
<32, false>(of
);
488 #ifdef HAVE_TARGET_32_BIG
489 case Parameters::TARGET_32_BIG
:
490 this->do_sized_write
<32, true>(of
);
493 #ifdef HAVE_TARGET_64_LITTLE
494 case Parameters::TARGET_64_LITTLE
:
495 this->do_sized_write
<64, false>(of
);
498 #ifdef HAVE_TARGET_64_BIG
499 case Parameters::TARGET_64_BIG
:
500 this->do_sized_write
<64, true>(of
);
508 // Write out the file header with appropriate size and endianness.
510 template<int size
, bool big_endian
>
512 Output_file_header::do_sized_write(Output_file
* of
)
514 gold_assert(this->offset() == 0);
516 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
517 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
518 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
520 unsigned char e_ident
[elfcpp::EI_NIDENT
];
521 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
522 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
523 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
524 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
525 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
527 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
529 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
532 e_ident
[elfcpp::EI_DATA
] = (big_endian
533 ? elfcpp::ELFDATA2MSB
534 : elfcpp::ELFDATA2LSB
);
535 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
536 oehdr
.put_e_ident(e_ident
);
539 if (parameters
->options().relocatable())
540 e_type
= elfcpp::ET_REL
;
541 else if (parameters
->options().output_is_position_independent())
542 e_type
= elfcpp::ET_DYN
;
544 e_type
= elfcpp::ET_EXEC
;
545 oehdr
.put_e_type(e_type
);
547 oehdr
.put_e_machine(this->target_
->machine_code());
548 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
550 oehdr
.put_e_entry(this->entry
<size
>());
552 if (this->segment_header_
== NULL
)
553 oehdr
.put_e_phoff(0);
555 oehdr
.put_e_phoff(this->segment_header_
->offset());
557 oehdr
.put_e_shoff(this->section_header_
->offset());
558 oehdr
.put_e_flags(this->target_
->processor_specific_flags());
559 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
561 if (this->segment_header_
== NULL
)
563 oehdr
.put_e_phentsize(0);
564 oehdr
.put_e_phnum(0);
568 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
569 size_t phnum
= (this->segment_header_
->data_size()
570 / elfcpp::Elf_sizes
<size
>::phdr_size
);
571 if (phnum
> elfcpp::PN_XNUM
)
572 phnum
= elfcpp::PN_XNUM
;
573 oehdr
.put_e_phnum(phnum
);
576 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
577 size_t section_count
= (this->section_header_
->data_size()
578 / elfcpp::Elf_sizes
<size
>::shdr_size
);
580 if (section_count
< elfcpp::SHN_LORESERVE
)
581 oehdr
.put_e_shnum(this->section_header_
->data_size()
582 / elfcpp::Elf_sizes
<size
>::shdr_size
);
584 oehdr
.put_e_shnum(0);
586 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
587 if (shstrndx
< elfcpp::SHN_LORESERVE
)
588 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
590 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
592 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
593 // the e_ident field.
594 this->target_
->adjust_elf_header(view
, ehdr_size
);
596 of
->write_output_view(0, ehdr_size
, view
);
599 // Return the value to use for the entry address.
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr
603 Output_file_header::entry()
605 const bool should_issue_warning
= (parameters
->options().entry() != NULL
606 && !parameters
->options().relocatable()
607 && !parameters
->options().shared());
608 const char* entry
= parameters
->entry();
609 Symbol
* sym
= this->symtab_
->lookup(entry
);
611 typename Sized_symbol
<size
>::Value_type v
;
614 Sized_symbol
<size
>* ssym
;
615 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
616 if (!ssym
->is_defined() && should_issue_warning
)
617 gold_warning("entry symbol '%s' exists but is not defined", entry
);
622 // We couldn't find the entry symbol. See if we can parse it as
623 // a number. This supports, e.g., -e 0x1000.
625 v
= strtoull(entry
, &endptr
, 0);
628 if (should_issue_warning
)
629 gold_warning("cannot find entry symbol '%s'", entry
);
637 // Compute the current data size.
640 Output_file_header::do_size() const
642 const int size
= parameters
->target().get_size();
644 return elfcpp::Elf_sizes
<32>::ehdr_size
;
646 return elfcpp::Elf_sizes
<64>::ehdr_size
;
651 // Output_data_const methods.
654 Output_data_const::do_write(Output_file
* of
)
656 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
659 // Output_data_const_buffer methods.
662 Output_data_const_buffer::do_write(Output_file
* of
)
664 of
->write(this->offset(), this->p_
, this->data_size());
667 // Output_section_data methods.
669 // Record the output section, and set the entry size and such.
672 Output_section_data::set_output_section(Output_section
* os
)
674 gold_assert(this->output_section_
== NULL
);
675 this->output_section_
= os
;
676 this->do_adjust_output_section(os
);
679 // Return the section index of the output section.
682 Output_section_data::do_out_shndx() const
684 gold_assert(this->output_section_
!= NULL
);
685 return this->output_section_
->out_shndx();
688 // Set the alignment, which means we may need to update the alignment
689 // of the output section.
692 Output_section_data::set_addralign(uint64_t addralign
)
694 this->addralign_
= addralign
;
695 if (this->output_section_
!= NULL
696 && this->output_section_
->addralign() < addralign
)
697 this->output_section_
->set_addralign(addralign
);
700 // Output_data_strtab methods.
702 // Set the final data size.
705 Output_data_strtab::set_final_data_size()
707 this->strtab_
->set_string_offsets();
708 this->set_data_size(this->strtab_
->get_strtab_size());
711 // Write out a string table.
714 Output_data_strtab::do_write(Output_file
* of
)
716 this->strtab_
->write(of
, this->offset());
719 // Output_reloc methods.
721 // A reloc against a global symbol.
723 template<bool dynamic
, int size
, bool big_endian
>
724 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
732 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
733 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
734 is_section_symbol_(false), use_plt_offset_(use_plt_offset
), shndx_(INVALID_CODE
)
736 // this->type_ is a bitfield; make sure TYPE fits.
737 gold_assert(this->type_
== type
);
738 this->u1_
.gsym
= gsym
;
741 this->set_needs_dynsym_index();
744 template<bool dynamic
, int size
, bool big_endian
>
745 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
748 Sized_relobj
<size
, big_endian
>* relobj
,
754 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
755 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
756 is_section_symbol_(false), use_plt_offset_(use_plt_offset
), shndx_(shndx
)
758 gold_assert(shndx
!= INVALID_CODE
);
759 // this->type_ is a bitfield; make sure TYPE fits.
760 gold_assert(this->type_
== type
);
761 this->u1_
.gsym
= gsym
;
762 this->u2_
.relobj
= relobj
;
764 this->set_needs_dynsym_index();
767 // A reloc against a local symbol.
769 template<bool dynamic
, int size
, bool big_endian
>
770 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
771 Sized_relobj
<size
, big_endian
>* relobj
,
772 unsigned int local_sym_index
,
778 bool is_section_symbol
,
780 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
781 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
782 is_section_symbol_(is_section_symbol
), use_plt_offset_(use_plt_offset
),
785 gold_assert(local_sym_index
!= GSYM_CODE
786 && local_sym_index
!= INVALID_CODE
);
787 // this->type_ is a bitfield; make sure TYPE fits.
788 gold_assert(this->type_
== type
);
789 this->u1_
.relobj
= relobj
;
792 this->set_needs_dynsym_index();
795 template<bool dynamic
, int size
, bool big_endian
>
796 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
797 Sized_relobj
<size
, big_endian
>* relobj
,
798 unsigned int local_sym_index
,
804 bool is_section_symbol
,
806 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
807 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
808 is_section_symbol_(is_section_symbol
), use_plt_offset_(use_plt_offset
),
811 gold_assert(local_sym_index
!= GSYM_CODE
812 && local_sym_index
!= INVALID_CODE
);
813 gold_assert(shndx
!= INVALID_CODE
);
814 // this->type_ is a bitfield; make sure TYPE fits.
815 gold_assert(this->type_
== type
);
816 this->u1_
.relobj
= relobj
;
817 this->u2_
.relobj
= relobj
;
819 this->set_needs_dynsym_index();
822 // A reloc against the STT_SECTION symbol of an output section.
824 template<bool dynamic
, int size
, bool big_endian
>
825 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
831 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
832 is_relative_(is_relative
), is_symbolless_(is_relative
),
833 is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE
)
835 // this->type_ is a bitfield; make sure TYPE fits.
836 gold_assert(this->type_
== type
);
840 this->set_needs_dynsym_index();
842 os
->set_needs_symtab_index();
845 template<bool dynamic
, int size
, bool big_endian
>
846 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
849 Sized_relobj
<size
, big_endian
>* relobj
,
853 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
854 is_relative_(is_relative
), is_symbolless_(is_relative
),
855 is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx
)
857 gold_assert(shndx
!= INVALID_CODE
);
858 // this->type_ is a bitfield; make sure TYPE fits.
859 gold_assert(this->type_
== type
);
861 this->u2_
.relobj
= relobj
;
863 this->set_needs_dynsym_index();
865 os
->set_needs_symtab_index();
868 // An absolute or relative relocation.
870 template<bool dynamic
, int size
, bool big_endian
>
871 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
876 : address_(address
), local_sym_index_(0), type_(type
),
877 is_relative_(is_relative
), is_symbolless_(false),
878 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE
)
880 // this->type_ is a bitfield; make sure TYPE fits.
881 gold_assert(this->type_
== type
);
882 this->u1_
.relobj
= NULL
;
886 template<bool dynamic
, int size
, bool big_endian
>
887 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
889 Sized_relobj
<size
, big_endian
>* relobj
,
893 : address_(address
), local_sym_index_(0), type_(type
),
894 is_relative_(is_relative
), is_symbolless_(false),
895 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx
)
897 gold_assert(shndx
!= INVALID_CODE
);
898 // this->type_ is a bitfield; make sure TYPE fits.
899 gold_assert(this->type_
== type
);
900 this->u1_
.relobj
= NULL
;
901 this->u2_
.relobj
= relobj
;
904 // A target specific relocation.
906 template<bool dynamic
, int size
, bool big_endian
>
907 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
912 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
913 is_relative_(false), is_symbolless_(false),
914 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE
)
916 // this->type_ is a bitfield; make sure TYPE fits.
917 gold_assert(this->type_
== type
);
922 template<bool dynamic
, int size
, bool big_endian
>
923 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
926 Sized_relobj
<size
, big_endian
>* relobj
,
929 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
930 is_relative_(false), is_symbolless_(false),
931 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx
)
933 gold_assert(shndx
!= INVALID_CODE
);
934 // this->type_ is a bitfield; make sure TYPE fits.
935 gold_assert(this->type_
== type
);
937 this->u2_
.relobj
= relobj
;
940 // Record that we need a dynamic symbol index for this relocation.
942 template<bool dynamic
, int size
, bool big_endian
>
944 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
945 set_needs_dynsym_index()
947 if (this->is_symbolless_
)
949 switch (this->local_sym_index_
)
955 this->u1_
.gsym
->set_needs_dynsym_entry();
959 this->u1_
.os
->set_needs_dynsym_index();
963 // The target must take care of this if necessary.
971 const unsigned int lsi
= this->local_sym_index_
;
972 Sized_relobj_file
<size
, big_endian
>* relobj
=
973 this->u1_
.relobj
->sized_relobj();
974 gold_assert(relobj
!= NULL
);
975 if (!this->is_section_symbol_
)
976 relobj
->set_needs_output_dynsym_entry(lsi
);
978 relobj
->output_section(lsi
)->set_needs_dynsym_index();
984 // Get the symbol index of a relocation.
986 template<bool dynamic
, int size
, bool big_endian
>
988 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
992 if (this->is_symbolless_
)
994 switch (this->local_sym_index_
)
1000 if (this->u1_
.gsym
== NULL
)
1003 index
= this->u1_
.gsym
->dynsym_index();
1005 index
= this->u1_
.gsym
->symtab_index();
1010 index
= this->u1_
.os
->dynsym_index();
1012 index
= this->u1_
.os
->symtab_index();
1016 index
= parameters
->target().reloc_symbol_index(this->u1_
.arg
,
1021 // Relocations without symbols use a symbol index of 0.
1027 const unsigned int lsi
= this->local_sym_index_
;
1028 Sized_relobj_file
<size
, big_endian
>* relobj
=
1029 this->u1_
.relobj
->sized_relobj();
1030 gold_assert(relobj
!= NULL
);
1031 if (!this->is_section_symbol_
)
1034 index
= relobj
->dynsym_index(lsi
);
1036 index
= relobj
->symtab_index(lsi
);
1040 Output_section
* os
= relobj
->output_section(lsi
);
1041 gold_assert(os
!= NULL
);
1043 index
= os
->dynsym_index();
1045 index
= os
->symtab_index();
1050 gold_assert(index
!= -1U);
1054 // For a local section symbol, get the address of the offset ADDEND
1055 // within the input section.
1057 template<bool dynamic
, int size
, bool big_endian
>
1058 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1059 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1060 local_section_offset(Addend addend
) const
1062 gold_assert(this->local_sym_index_
!= GSYM_CODE
1063 && this->local_sym_index_
!= SECTION_CODE
1064 && this->local_sym_index_
!= TARGET_CODE
1065 && this->local_sym_index_
!= INVALID_CODE
1066 && this->local_sym_index_
!= 0
1067 && this->is_section_symbol_
);
1068 const unsigned int lsi
= this->local_sym_index_
;
1069 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
1070 gold_assert(os
!= NULL
);
1071 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
1072 if (offset
!= invalid_address
)
1073 return offset
+ addend
;
1074 // This is a merge section.
1075 Sized_relobj_file
<size
, big_endian
>* relobj
=
1076 this->u1_
.relobj
->sized_relobj();
1077 gold_assert(relobj
!= NULL
);
1078 offset
= os
->output_address(relobj
, lsi
, addend
);
1079 gold_assert(offset
!= invalid_address
);
1083 // Get the output address of a relocation.
1085 template<bool dynamic
, int size
, bool big_endian
>
1086 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1087 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
1089 Address address
= this->address_
;
1090 if (this->shndx_
!= INVALID_CODE
)
1092 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
1093 gold_assert(os
!= NULL
);
1094 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
1095 if (off
!= invalid_address
)
1096 address
+= os
->address() + off
;
1099 Sized_relobj_file
<size
, big_endian
>* relobj
=
1100 this->u2_
.relobj
->sized_relobj();
1101 gold_assert(relobj
!= NULL
);
1102 address
= os
->output_address(relobj
, this->shndx_
, address
);
1103 gold_assert(address
!= invalid_address
);
1106 else if (this->u2_
.od
!= NULL
)
1107 address
+= this->u2_
.od
->address();
1111 // Write out the offset and info fields of a Rel or Rela relocation
1114 template<bool dynamic
, int size
, bool big_endian
>
1115 template<typename Write_rel
>
1117 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
1118 Write_rel
* wr
) const
1120 wr
->put_r_offset(this->get_address());
1121 unsigned int sym_index
= this->get_symbol_index();
1122 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
1125 // Write out a Rel relocation.
1127 template<bool dynamic
, int size
, bool big_endian
>
1129 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
1130 unsigned char* pov
) const
1132 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
1133 this->write_rel(&orel
);
1136 // Get the value of the symbol referred to by a Rel relocation.
1138 template<bool dynamic
, int size
, bool big_endian
>
1139 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1140 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
1141 Addend addend
) const
1143 if (this->local_sym_index_
== GSYM_CODE
)
1145 const Sized_symbol
<size
>* sym
;
1146 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
1147 if (this->use_plt_offset_
&& sym
->has_plt_offset())
1148 return parameters
->target().plt_address_for_global(sym
);
1150 return sym
->value() + addend
;
1152 if (this->local_sym_index_
== SECTION_CODE
)
1154 gold_assert(!this->use_plt_offset_
);
1155 return this->u1_
.os
->address() + addend
;
1157 gold_assert(this->local_sym_index_
!= TARGET_CODE
1158 && this->local_sym_index_
!= INVALID_CODE
1159 && this->local_sym_index_
!= 0
1160 && !this->is_section_symbol_
);
1161 const unsigned int lsi
= this->local_sym_index_
;
1162 Sized_relobj_file
<size
, big_endian
>* relobj
=
1163 this->u1_
.relobj
->sized_relobj();
1164 gold_assert(relobj
!= NULL
);
1165 if (this->use_plt_offset_
)
1166 return parameters
->target().plt_address_for_local(relobj
, lsi
);
1167 const Symbol_value
<size
>* symval
= relobj
->local_symbol(lsi
);
1168 return symval
->value(relobj
, addend
);
1171 // Reloc comparison. This function sorts the dynamic relocs for the
1172 // benefit of the dynamic linker. First we sort all relative relocs
1173 // to the front. Among relative relocs, we sort by output address.
1174 // Among non-relative relocs, we sort by symbol index, then by output
1177 template<bool dynamic
, int size
, bool big_endian
>
1179 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1180 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1183 if (this->is_relative_
)
1185 if (!r2
.is_relative_
)
1187 // Otherwise sort by reloc address below.
1189 else if (r2
.is_relative_
)
1193 unsigned int sym1
= this->get_symbol_index();
1194 unsigned int sym2
= r2
.get_symbol_index();
1197 else if (sym1
> sym2
)
1199 // Otherwise sort by reloc address.
1202 section_offset_type addr1
= this->get_address();
1203 section_offset_type addr2
= r2
.get_address();
1206 else if (addr1
> addr2
)
1209 // Final tie breaker, in order to generate the same output on any
1210 // host: reloc type.
1211 unsigned int type1
= this->type_
;
1212 unsigned int type2
= r2
.type_
;
1215 else if (type1
> type2
)
1218 // These relocs appear to be exactly the same.
1222 // Write out a Rela relocation.
1224 template<bool dynamic
, int size
, bool big_endian
>
1226 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
1227 unsigned char* pov
) const
1229 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
1230 this->rel_
.write_rel(&orel
);
1231 Addend addend
= this->addend_
;
1232 if (this->rel_
.is_target_specific())
1233 addend
= parameters
->target().reloc_addend(this->rel_
.target_arg(),
1234 this->rel_
.type(), addend
);
1235 else if (this->rel_
.is_symbolless())
1236 addend
= this->rel_
.symbol_value(addend
);
1237 else if (this->rel_
.is_local_section_symbol())
1238 addend
= this->rel_
.local_section_offset(addend
);
1239 orel
.put_r_addend(addend
);
1242 // Output_data_reloc_base methods.
1244 // Adjust the output section.
1246 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1248 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
1249 ::do_adjust_output_section(Output_section
* os
)
1251 if (sh_type
== elfcpp::SHT_REL
)
1252 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1253 else if (sh_type
== elfcpp::SHT_RELA
)
1254 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1258 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1259 // static link. The backends will generate a dynamic reloc section
1260 // to hold this. In that case we don't want to link to the dynsym
1261 // section, because there isn't one.
1263 os
->set_should_link_to_symtab();
1264 else if (parameters
->doing_static_link())
1267 os
->set_should_link_to_dynsym();
1270 // Standard relocation writer, which just calls Output_reloc::write().
1272 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1273 struct Output_reloc_writer
1275 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1276 typedef std::vector
<Output_reloc_type
> Relocs
;
1279 write(typename
Relocs::const_iterator p
, unsigned char* pov
)
1283 // Write out relocation data.
1285 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1287 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1290 typedef Output_reloc_writer
<sh_type
, dynamic
, size
, big_endian
> Writer
;
1291 this->do_write_generic
<Writer
>(of
);
1294 // Class Output_relocatable_relocs.
1296 template<int sh_type
, int size
, bool big_endian
>
1298 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1300 this->set_data_size(this->rr_
->output_reloc_count()
1301 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1304 // class Output_data_group.
1306 template<int size
, bool big_endian
>
1307 Output_data_group
<size
, big_endian
>::Output_data_group(
1308 Sized_relobj_file
<size
, big_endian
>* relobj
,
1309 section_size_type entry_count
,
1310 elfcpp::Elf_Word flags
,
1311 std::vector
<unsigned int>* input_shndxes
)
1312 : Output_section_data(entry_count
* 4, 4, false),
1316 this->input_shndxes_
.swap(*input_shndxes
);
1319 // Write out the section group, which means translating the section
1320 // indexes to apply to the output file.
1322 template<int size
, bool big_endian
>
1324 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1326 const off_t off
= this->offset();
1327 const section_size_type oview_size
=
1328 convert_to_section_size_type(this->data_size());
1329 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1331 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1332 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1335 for (std::vector
<unsigned int>::const_iterator p
=
1336 this->input_shndxes_
.begin();
1337 p
!= this->input_shndxes_
.end();
1340 Output_section
* os
= this->relobj_
->output_section(*p
);
1342 unsigned int output_shndx
;
1344 output_shndx
= os
->out_shndx();
1347 this->relobj_
->error(_("section group retained but "
1348 "group element discarded"));
1352 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1355 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1356 gold_assert(wrote
== oview_size
);
1358 of
->write_output_view(off
, oview_size
, oview
);
1360 // We no longer need this information.
1361 this->input_shndxes_
.clear();
1364 // Output_data_got::Got_entry methods.
1366 // Write out the entry.
1368 template<int got_size
, bool big_endian
>
1370 Output_data_got
<got_size
, big_endian
>::Got_entry::write(
1371 Output_data_got_base
* got
,
1372 unsigned int got_indx
,
1373 unsigned char* pov
) const
1377 switch (this->local_sym_index_
)
1381 // If the symbol is resolved locally, we need to write out the
1382 // link-time value, which will be relocated dynamically by a
1383 // RELATIVE relocation.
1384 Symbol
* gsym
= this->u_
.gsym
;
1385 if (this->use_plt_or_tls_offset_
&& gsym
->has_plt_offset())
1386 val
= parameters
->target().plt_address_for_global(gsym
);
1389 switch (parameters
->size_and_endianness())
1391 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1392 case Parameters::TARGET_32_LITTLE
:
1393 case Parameters::TARGET_32_BIG
:
1395 // This cast is ugly. We don't want to put a
1396 // virtual method in Symbol, because we want Symbol
1397 // to be as small as possible.
1398 Sized_symbol
<32>::Value_type v
;
1399 v
= static_cast<Sized_symbol
<32>*>(gsym
)->value();
1400 val
= convert_types
<Valtype
, Sized_symbol
<32>::Value_type
>(v
);
1404 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1405 case Parameters::TARGET_64_LITTLE
:
1406 case Parameters::TARGET_64_BIG
:
1408 Sized_symbol
<64>::Value_type v
;
1409 v
= static_cast<Sized_symbol
<64>*>(gsym
)->value();
1410 val
= convert_types
<Valtype
, Sized_symbol
<64>::Value_type
>(v
);
1417 // If this is a GOT entry for a known value global symbol,
1418 // then the value should include the addend. If the value
1419 // is not known leave the value as zero; The GOT entry
1420 // will be set by a dynamic relocation.
1421 if (this->addend_
&& gsym
->final_value_is_known())
1422 val
+= this->addend_
;
1423 if (this->use_plt_or_tls_offset_
1424 && gsym
->type() == elfcpp::STT_TLS
)
1425 val
+= parameters
->target().tls_offset_for_global(gsym
,
1433 val
= this->u_
.constant
;
1437 // If we're doing an incremental update, don't touch this GOT entry.
1438 if (parameters
->incremental_update())
1440 val
= this->u_
.constant
;
1445 const Relobj
* object
= this->u_
.object
;
1446 const unsigned int lsi
= this->local_sym_index_
;
1447 bool is_tls
= object
->local_is_tls(lsi
);
1448 if (this->use_plt_or_tls_offset_
&& !is_tls
)
1449 val
= parameters
->target().plt_address_for_local(object
, lsi
);
1452 uint64_t lval
= object
->local_symbol_value(lsi
, this->addend_
);
1453 val
= convert_types
<Valtype
, uint64_t>(lval
);
1454 if (this->use_plt_or_tls_offset_
&& is_tls
)
1455 val
+= parameters
->target().tls_offset_for_local(object
, lsi
,
1463 elfcpp::Swap
<got_size
, big_endian
>::writeval(pov
, val
);
1466 // Output_data_got methods.
1468 // Add an entry for a global symbol to the GOT. This returns true if
1469 // this is a new GOT entry, false if the symbol already had a GOT
1472 template<int got_size
, bool big_endian
>
1474 Output_data_got
<got_size
, big_endian
>::add_global(Symbol
* gsym
,
1475 unsigned int got_type
,
1478 if (gsym
->has_got_offset(got_type
, addend
))
1481 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, false, addend
));
1482 gsym
->set_got_offset(got_type
, got_offset
, addend
);
1486 // Like add_global, but use the PLT offset.
1488 template<int got_size
, bool big_endian
>
1490 Output_data_got
<got_size
, big_endian
>::add_global_plt(Symbol
* gsym
,
1491 unsigned int got_type
,
1494 if (gsym
->has_got_offset(got_type
, addend
))
1497 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, true, addend
));
1498 gsym
->set_got_offset(got_type
, got_offset
, addend
);
1502 // Add an entry for a global symbol to the GOT, and add a dynamic
1503 // relocation of type R_TYPE for the GOT entry.
1505 template<int got_size
, bool big_endian
>
1507 Output_data_got
<got_size
, big_endian
>::add_global_with_rel(
1509 unsigned int got_type
,
1510 Output_data_reloc_generic
* rel_dyn
,
1511 unsigned int r_type
,
1514 if (gsym
->has_got_offset(got_type
, addend
))
1517 unsigned int got_offset
= this->add_got_entry(Got_entry());
1518 gsym
->set_got_offset(got_type
, got_offset
, addend
);
1519 rel_dyn
->add_global_generic(gsym
, r_type
, this, got_offset
, addend
);
1522 // Add a pair of entries for a global symbol to the GOT, and add
1523 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1524 // If R_TYPE_2 == 0, add the second entry with no relocation.
1525 template<int got_size
, bool big_endian
>
1527 Output_data_got
<got_size
, big_endian
>::add_global_pair_with_rel(
1529 unsigned int got_type
,
1530 Output_data_reloc_generic
* rel_dyn
,
1531 unsigned int r_type_1
,
1532 unsigned int r_type_2
,
1535 if (gsym
->has_got_offset(got_type
, addend
))
1538 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1539 gsym
->set_got_offset(got_type
, got_offset
, addend
);
1540 rel_dyn
->add_global_generic(gsym
, r_type_1
, this, got_offset
, addend
);
1543 rel_dyn
->add_global_generic(gsym
, r_type_2
, this,
1544 got_offset
+ got_size
/ 8, addend
);
1547 // Add an entry for a local symbol plus ADDEND to the GOT. This returns
1548 // true if this is a new GOT entry, false if the symbol already has a GOT
1551 template<int got_size
, bool big_endian
>
1553 Output_data_got
<got_size
, big_endian
>::add_local(
1555 unsigned int symndx
,
1556 unsigned int got_type
,
1559 if (object
->local_has_got_offset(symndx
, got_type
, addend
))
1562 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1564 object
->set_local_got_offset(symndx
, got_type
, got_offset
, addend
);
1568 // Like add_local, but use the PLT offset.
1570 template<int got_size
, bool big_endian
>
1572 Output_data_got
<got_size
, big_endian
>::add_local_plt(
1574 unsigned int symndx
,
1575 unsigned int got_type
,
1578 if (object
->local_has_got_offset(symndx
, got_type
, addend
))
1581 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1583 object
->set_local_got_offset(symndx
, got_type
, got_offset
, addend
);
1587 // Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
1588 // relocation of type R_TYPE for the GOT entry.
1590 template<int got_size
, bool big_endian
>
1592 Output_data_got
<got_size
, big_endian
>::add_local_with_rel(
1594 unsigned int symndx
,
1595 unsigned int got_type
,
1596 Output_data_reloc_generic
* rel_dyn
,
1597 unsigned int r_type
,
1600 if (object
->local_has_got_offset(symndx
, got_type
, addend
))
1603 unsigned int got_offset
= this->add_got_entry(Got_entry());
1604 object
->set_local_got_offset(symndx
, got_type
, got_offset
, addend
);
1605 rel_dyn
->add_local_generic(object
, symndx
, r_type
, this, got_offset
,
1609 // Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
1610 // a dynamic relocation of type R_TYPE using the section symbol of
1611 // the output section to which input section SHNDX maps, on the first.
1612 // The first got entry will have a value of zero, the second the
1613 // value of the local symbol.
1614 template<int got_size
, bool big_endian
>
1616 Output_data_got
<got_size
, big_endian
>::add_local_pair_with_rel(
1618 unsigned int symndx
,
1620 unsigned int got_type
,
1621 Output_data_reloc_generic
* rel_dyn
,
1622 unsigned int r_type
,
1625 if (object
->local_has_got_offset(symndx
, got_type
, addend
))
1628 unsigned int got_offset
=
1629 this->add_got_entry_pair(Got_entry(),
1630 Got_entry(object
, symndx
, false, addend
));
1631 object
->set_local_got_offset(symndx
, got_type
, got_offset
, addend
);
1632 Output_section
* os
= object
->output_section(shndx
);
1633 rel_dyn
->add_output_section_generic(os
, r_type
, this, got_offset
, addend
);
1636 // Add a pair of entries for a local symbol to the GOT, and add
1637 // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
1638 // The first got entry will have a value of zero, the second the
1639 // value of the local symbol offset by Target::tls_offset_for_local.
1640 template<int got_size
, bool big_endian
>
1642 Output_data_got
<got_size
, big_endian
>::add_local_tls_pair(
1644 unsigned int symndx
,
1645 unsigned int got_type
,
1646 Output_data_reloc_generic
* rel_dyn
,
1647 unsigned int r_type
,
1650 if (object
->local_has_got_offset(symndx
, got_type
, addend
))
1653 unsigned int got_offset
1654 = this->add_got_entry_pair(Got_entry(),
1655 Got_entry(object
, symndx
, true, addend
));
1656 object
->set_local_got_offset(symndx
, got_type
, got_offset
, addend
);
1657 rel_dyn
->add_local_generic(object
, 0, r_type
, this, got_offset
, addend
);
1660 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1662 template<int got_size
, bool big_endian
>
1664 Output_data_got
<got_size
, big_endian
>::reserve_local(
1667 unsigned int sym_index
,
1668 unsigned int got_type
,
1671 this->do_reserve_slot(i
);
1672 object
->set_local_got_offset(sym_index
, got_type
, this->got_offset(i
), addend
);
1675 // Reserve a slot in the GOT for a global symbol.
1677 template<int got_size
, bool big_endian
>
1679 Output_data_got
<got_size
, big_endian
>::reserve_global(
1682 unsigned int got_type
,
1685 this->do_reserve_slot(i
);
1686 gsym
->set_got_offset(got_type
, this->got_offset(i
), addend
);
1689 // Write out the GOT.
1691 template<int got_size
, bool big_endian
>
1693 Output_data_got
<got_size
, big_endian
>::do_write(Output_file
* of
)
1695 const int add
= got_size
/ 8;
1697 const off_t off
= this->offset();
1698 const off_t oview_size
= this->data_size();
1699 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1701 unsigned char* pov
= oview
;
1702 for (unsigned int i
= 0; i
< this->entries_
.size(); ++i
)
1704 this->entries_
[i
].write(this, i
, pov
);
1708 gold_assert(pov
- oview
== oview_size
);
1710 of
->write_output_view(off
, oview_size
, oview
);
1712 // We no longer need the GOT entries.
1713 this->entries_
.clear();
1716 // Create a new GOT entry and return its offset.
1718 template<int got_size
, bool big_endian
>
1720 Output_data_got
<got_size
, big_endian
>::add_got_entry(Got_entry got_entry
)
1722 if (!this->is_data_size_valid())
1724 this->entries_
.push_back(got_entry
);
1725 this->set_got_size();
1726 return this->last_got_offset();
1730 // For an incremental update, find an available slot.
1731 off_t got_offset
= this->free_list_
.allocate(got_size
/ 8,
1733 if (got_offset
== -1)
1734 gold_fallback(_("out of patch space (GOT);"
1735 " relink with --incremental-full"));
1736 unsigned int got_index
= got_offset
/ (got_size
/ 8);
1737 gold_assert(got_index
< this->entries_
.size());
1738 this->entries_
[got_index
] = got_entry
;
1739 return static_cast<unsigned int>(got_offset
);
1743 // Create a pair of new GOT entries and return the offset of the first.
1745 template<int got_size
, bool big_endian
>
1747 Output_data_got
<got_size
, big_endian
>::add_got_entry_pair(
1748 Got_entry got_entry_1
,
1749 Got_entry got_entry_2
)
1751 if (!this->is_data_size_valid())
1753 unsigned int got_offset
;
1754 this->entries_
.push_back(got_entry_1
);
1755 got_offset
= this->last_got_offset();
1756 this->entries_
.push_back(got_entry_2
);
1757 this->set_got_size();
1762 // For an incremental update, find an available pair of slots.
1763 off_t got_offset
= this->free_list_
.allocate(2 * got_size
/ 8,
1765 if (got_offset
== -1)
1766 gold_fallback(_("out of patch space (GOT);"
1767 " relink with --incremental-full"));
1768 unsigned int got_index
= got_offset
/ (got_size
/ 8);
1769 gold_assert(got_index
< this->entries_
.size());
1770 this->entries_
[got_index
] = got_entry_1
;
1771 this->entries_
[got_index
+ 1] = got_entry_2
;
1772 return static_cast<unsigned int>(got_offset
);
1776 // Replace GOT entry I with a new value.
1778 template<int got_size
, bool big_endian
>
1780 Output_data_got
<got_size
, big_endian
>::replace_got_entry(
1782 Got_entry got_entry
)
1784 gold_assert(i
< this->entries_
.size());
1785 this->entries_
[i
] = got_entry
;
1788 // Output_data_dynamic::Dynamic_entry methods.
1790 // Write out the entry.
1792 template<int size
, bool big_endian
>
1794 Output_data_dynamic::Dynamic_entry::write(
1796 const Stringpool
* pool
) const
1798 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1799 switch (this->offset_
)
1801 case DYNAMIC_NUMBER
:
1805 case DYNAMIC_SECTION_SIZE
:
1806 val
= this->u_
.od
->data_size();
1807 if (this->od2
!= NULL
)
1808 val
+= this->od2
->data_size();
1811 case DYNAMIC_SYMBOL
:
1813 const Sized_symbol
<size
>* s
=
1814 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1819 case DYNAMIC_STRING
:
1820 val
= pool
->get_offset(this->u_
.str
);
1823 case DYNAMIC_CUSTOM
:
1824 val
= parameters
->target().dynamic_tag_custom_value(this->tag_
);
1828 val
= this->u_
.od
->address() + this->offset_
;
1832 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1833 dw
.put_d_tag(this->tag_
);
1837 // Output_data_dynamic methods.
1839 // Adjust the output section to set the entry size.
1842 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1844 if (parameters
->target().get_size() == 32)
1845 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1846 else if (parameters
->target().get_size() == 64)
1847 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1852 // Get a dynamic entry offset.
1855 Output_data_dynamic::get_entry_offset(elfcpp::DT tag
) const
1859 if (parameters
->target().get_size() == 32)
1860 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1861 else if (parameters
->target().get_size() == 64)
1862 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1866 for (size_t i
= 0; i
< entries_
.size(); ++i
)
1867 if (entries_
[i
].tag() == tag
)
1868 return i
* dyn_size
;
1873 // Set the final data size.
1876 Output_data_dynamic::set_final_data_size()
1878 // Add the terminating entry if it hasn't been added.
1879 // Because of relaxation, we can run this multiple times.
1880 if (this->entries_
.empty() || this->entries_
.back().tag() != elfcpp::DT_NULL
)
1882 int extra
= parameters
->options().spare_dynamic_tags();
1883 for (int i
= 0; i
< extra
; ++i
)
1884 this->add_constant(elfcpp::DT_NULL
, 0);
1885 this->add_constant(elfcpp::DT_NULL
, 0);
1889 if (parameters
->target().get_size() == 32)
1890 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1891 else if (parameters
->target().get_size() == 64)
1892 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1895 this->set_data_size(this->entries_
.size() * dyn_size
);
1898 // Write out the dynamic entries.
1901 Output_data_dynamic::do_write(Output_file
* of
)
1903 switch (parameters
->size_and_endianness())
1905 #ifdef HAVE_TARGET_32_LITTLE
1906 case Parameters::TARGET_32_LITTLE
:
1907 this->sized_write
<32, false>(of
);
1910 #ifdef HAVE_TARGET_32_BIG
1911 case Parameters::TARGET_32_BIG
:
1912 this->sized_write
<32, true>(of
);
1915 #ifdef HAVE_TARGET_64_LITTLE
1916 case Parameters::TARGET_64_LITTLE
:
1917 this->sized_write
<64, false>(of
);
1920 #ifdef HAVE_TARGET_64_BIG
1921 case Parameters::TARGET_64_BIG
:
1922 this->sized_write
<64, true>(of
);
1930 template<int size
, bool big_endian
>
1932 Output_data_dynamic::sized_write(Output_file
* of
)
1934 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1936 const off_t offset
= this->offset();
1937 const off_t oview_size
= this->data_size();
1938 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1940 unsigned char* pov
= oview
;
1941 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1942 p
!= this->entries_
.end();
1945 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1949 gold_assert(pov
- oview
== oview_size
);
1951 of
->write_output_view(offset
, oview_size
, oview
);
1953 // We no longer need the dynamic entries.
1954 this->entries_
.clear();
1957 // Class Output_symtab_xindex.
1960 Output_symtab_xindex::do_write(Output_file
* of
)
1962 const off_t offset
= this->offset();
1963 const off_t oview_size
= this->data_size();
1964 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1966 memset(oview
, 0, oview_size
);
1968 if (parameters
->target().is_big_endian())
1969 this->endian_do_write
<true>(oview
);
1971 this->endian_do_write
<false>(oview
);
1973 of
->write_output_view(offset
, oview_size
, oview
);
1975 // We no longer need the data.
1976 this->entries_
.clear();
1979 template<bool big_endian
>
1981 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1983 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1984 p
!= this->entries_
.end();
1987 unsigned int symndx
= p
->first
;
1988 gold_assert(static_cast<off_t
>(symndx
) * 4 < this->data_size());
1989 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ symndx
* 4, p
->second
);
1993 // Output_fill_debug_info methods.
1995 // Return the minimum size needed for a dummy compilation unit header.
1998 Output_fill_debug_info::do_minimum_hole_size() const
2000 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
2002 const size_t len
= 4 + 2 + 4 + 1;
2003 // For type units, add type_signature, type_offset.
2004 if (this->is_debug_types_
)
2009 // Write a dummy compilation unit header to fill a hole in the
2010 // .debug_info or .debug_types section.
2013 Output_fill_debug_info::do_write(Output_file
* of
, off_t off
, size_t len
) const
2015 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_info(%08lx, %08lx)",
2016 static_cast<long>(off
), static_cast<long>(len
));
2018 gold_assert(len
>= this->do_minimum_hole_size());
2020 unsigned char* const oview
= of
->get_output_view(off
, len
);
2021 unsigned char* pov
= oview
;
2023 // Write header fields: unit_length, version, debug_abbrev_offset,
2025 if (this->is_big_endian())
2027 elfcpp::Swap_unaligned
<32, true>::writeval(pov
, len
- 4);
2028 elfcpp::Swap_unaligned
<16, true>::writeval(pov
+ 4, this->version
);
2029 elfcpp::Swap_unaligned
<32, true>::writeval(pov
+ 6, 0);
2033 elfcpp::Swap_unaligned
<32, false>::writeval(pov
, len
- 4);
2034 elfcpp::Swap_unaligned
<16, false>::writeval(pov
+ 4, this->version
);
2035 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 6, 0);
2040 // For type units, the additional header fields -- type_signature,
2041 // type_offset -- can be filled with zeroes.
2043 // Fill the remainder of the free space with zeroes. The first
2044 // zero should tell the consumer there are no DIEs to read in this
2045 // compilation unit.
2046 if (pov
< oview
+ len
)
2047 memset(pov
, 0, oview
+ len
- pov
);
2049 of
->write_output_view(off
, len
, oview
);
2052 // Output_fill_debug_line methods.
2054 // Return the minimum size needed for a dummy line number program header.
2057 Output_fill_debug_line::do_minimum_hole_size() const
2059 // Line number program header fields: unit_length, version, header_length,
2060 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2061 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2062 const size_t len
= 4 + 2 + 4 + this->header_length
;
2066 // Write a dummy line number program header to fill a hole in the
2067 // .debug_line section.
2070 Output_fill_debug_line::do_write(Output_file
* of
, off_t off
, size_t len
) const
2072 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_line(%08lx, %08lx)",
2073 static_cast<long>(off
), static_cast<long>(len
));
2075 gold_assert(len
>= this->do_minimum_hole_size());
2077 unsigned char* const oview
= of
->get_output_view(off
, len
);
2078 unsigned char* pov
= oview
;
2080 // Write header fields: unit_length, version, header_length,
2081 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2082 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2083 // We set the header_length field to cover the entire hole, so the
2084 // line number program is empty.
2085 if (this->is_big_endian())
2087 elfcpp::Swap_unaligned
<32, true>::writeval(pov
, len
- 4);
2088 elfcpp::Swap_unaligned
<16, true>::writeval(pov
+ 4, this->version
);
2089 elfcpp::Swap_unaligned
<32, true>::writeval(pov
+ 6, len
- (4 + 2 + 4));
2093 elfcpp::Swap_unaligned
<32, false>::writeval(pov
, len
- 4);
2094 elfcpp::Swap_unaligned
<16, false>::writeval(pov
+ 4, this->version
);
2095 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 6, len
- (4 + 2 + 4));
2098 *pov
++ = 1; // minimum_instruction_length
2099 *pov
++ = 0; // default_is_stmt
2100 *pov
++ = 0; // line_base
2101 *pov
++ = 5; // line_range
2102 *pov
++ = 13; // opcode_base
2103 *pov
++ = 0; // standard_opcode_lengths[1]
2104 *pov
++ = 1; // standard_opcode_lengths[2]
2105 *pov
++ = 1; // standard_opcode_lengths[3]
2106 *pov
++ = 1; // standard_opcode_lengths[4]
2107 *pov
++ = 1; // standard_opcode_lengths[5]
2108 *pov
++ = 0; // standard_opcode_lengths[6]
2109 *pov
++ = 0; // standard_opcode_lengths[7]
2110 *pov
++ = 0; // standard_opcode_lengths[8]
2111 *pov
++ = 1; // standard_opcode_lengths[9]
2112 *pov
++ = 0; // standard_opcode_lengths[10]
2113 *pov
++ = 0; // standard_opcode_lengths[11]
2114 *pov
++ = 1; // standard_opcode_lengths[12]
2115 *pov
++ = 0; // include_directories (empty)
2116 *pov
++ = 0; // filenames (empty)
2118 // Some consumers don't check the header_length field, and simply
2119 // start reading the line number program immediately following the
2120 // header. For those consumers, we fill the remainder of the free
2121 // space with DW_LNS_set_basic_block opcodes. These are effectively
2122 // no-ops: the resulting line table program will not create any rows.
2123 if (pov
< oview
+ len
)
2124 memset(pov
, elfcpp::DW_LNS_set_basic_block
, oview
+ len
- pov
);
2126 of
->write_output_view(off
, len
, oview
);
2129 // Output_section::Input_section methods.
2131 // Return the current data size. For an input section we store the size here.
2132 // For an Output_section_data, we have to ask it for the size.
2135 Output_section::Input_section::current_data_size() const
2137 if (this->is_input_section())
2138 return this->u1_
.data_size
;
2141 this->u2_
.posd
->pre_finalize_data_size();
2142 return this->u2_
.posd
->current_data_size();
2146 // Return the data size. For an input section we store the size here.
2147 // For an Output_section_data, we have to ask it for the size.
2150 Output_section::Input_section::data_size() const
2152 if (this->is_input_section())
2153 return this->u1_
.data_size
;
2155 return this->u2_
.posd
->data_size();
2158 // Return the object for an input section.
2161 Output_section::Input_section::relobj() const
2163 if (this->is_input_section())
2164 return this->u2_
.object
;
2165 else if (this->is_merge_section())
2167 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2168 return this->u2_
.pomb
->first_relobj();
2170 else if (this->is_relaxed_input_section())
2171 return this->u2_
.poris
->relobj();
2176 // Return the input section index for an input section.
2179 Output_section::Input_section::shndx() const
2181 if (this->is_input_section())
2182 return this->shndx_
;
2183 else if (this->is_merge_section())
2185 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2186 return this->u2_
.pomb
->first_shndx();
2188 else if (this->is_relaxed_input_section())
2189 return this->u2_
.poris
->shndx();
2194 // Set the address and file offset.
2197 Output_section::Input_section::set_address_and_file_offset(
2200 off_t section_file_offset
)
2202 if (this->is_input_section())
2203 this->u2_
.object
->set_section_offset(this->shndx_
,
2204 file_offset
- section_file_offset
);
2206 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
2209 // Reset the address and file offset.
2212 Output_section::Input_section::reset_address_and_file_offset()
2214 if (!this->is_input_section())
2215 this->u2_
.posd
->reset_address_and_file_offset();
2218 // Finalize the data size.
2221 Output_section::Input_section::finalize_data_size()
2223 if (!this->is_input_section())
2224 this->u2_
.posd
->finalize_data_size();
2227 // Try to turn an input offset into an output offset. We want to
2228 // return the output offset relative to the start of this
2229 // Input_section in the output section.
2232 Output_section::Input_section::output_offset(
2233 const Relobj
* object
,
2235 section_offset_type offset
,
2236 section_offset_type
* poutput
) const
2238 if (!this->is_input_section())
2239 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
2242 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
2249 // Write out the data. We don't have to do anything for an input
2250 // section--they are handled via Object::relocate--but this is where
2251 // we write out the data for an Output_section_data.
2254 Output_section::Input_section::write(Output_file
* of
)
2256 if (!this->is_input_section())
2257 this->u2_
.posd
->write(of
);
2260 // Write the data to a buffer. As for write(), we don't have to do
2261 // anything for an input section.
2264 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
2266 if (!this->is_input_section())
2267 this->u2_
.posd
->write_to_buffer(buffer
);
2270 // Print to a map file.
2273 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
2275 switch (this->shndx_
)
2277 case OUTPUT_SECTION_CODE
:
2278 case MERGE_DATA_SECTION_CODE
:
2279 case MERGE_STRING_SECTION_CODE
:
2280 this->u2_
.posd
->print_to_mapfile(mapfile
);
2283 case RELAXED_INPUT_SECTION_CODE
:
2285 Output_relaxed_input_section
* relaxed_section
=
2286 this->relaxed_input_section();
2287 mapfile
->print_input_section(relaxed_section
->relobj(),
2288 relaxed_section
->shndx());
2292 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
2297 // Output_section methods.
2299 // Construct an Output_section. NAME will point into a Stringpool.
2301 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
2302 elfcpp::Elf_Xword flags
)
2307 link_section_(NULL
),
2309 info_section_(NULL
),
2314 order_(ORDER_INVALID
),
2319 first_input_offset_(0),
2321 postprocessing_buffer_(NULL
),
2322 needs_symtab_index_(false),
2323 needs_dynsym_index_(false),
2324 should_link_to_symtab_(false),
2325 should_link_to_dynsym_(false),
2326 after_input_sections_(false),
2327 requires_postprocessing_(false),
2328 found_in_sections_clause_(false),
2329 has_load_address_(false),
2330 info_uses_section_index_(false),
2331 input_section_order_specified_(false),
2332 may_sort_attached_input_sections_(false),
2333 must_sort_attached_input_sections_(false),
2334 attached_input_sections_are_sorted_(false),
2336 is_small_section_(false),
2337 is_large_section_(false),
2338 generate_code_fills_at_write_(false),
2339 is_entsize_zero_(false),
2340 section_offsets_need_adjustment_(false),
2342 always_keeps_input_sections_(false),
2343 has_fixed_layout_(false),
2344 is_patch_space_allowed_(false),
2345 is_unique_segment_(false),
2347 extra_segment_flags_(0),
2348 segment_alignment_(0),
2350 lookup_maps_(new Output_section_lookup_maps
),
2352 free_space_fill_(NULL
),
2354 reloc_section_(NULL
)
2356 // An unallocated section has no address. Forcing this means that
2357 // we don't need special treatment for symbols defined in debug
2359 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
2360 this->set_address(0);
2363 Output_section::~Output_section()
2365 delete this->checkpoint_
;
2368 // Set the entry size.
2371 Output_section::set_entsize(uint64_t v
)
2373 if (this->is_entsize_zero_
)
2375 else if (this->entsize_
== 0)
2377 else if (this->entsize_
!= v
)
2380 this->is_entsize_zero_
= 1;
2384 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2385 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2386 // relocation section which applies to this section, or 0 if none, or
2387 // -1U if more than one. Return the offset of the input section
2388 // within the output section. Return -1 if the input section will
2389 // receive special handling. In the normal case we don't always keep
2390 // track of input sections for an Output_section. Instead, each
2391 // Object keeps track of the Output_section for each of its input
2392 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2393 // track of input sections here; this is used when SECTIONS appears in
2396 template<int size
, bool big_endian
>
2398 Output_section::add_input_section(Layout
* layout
,
2399 Sized_relobj_file
<size
, big_endian
>* object
,
2401 const char* secname
,
2402 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2403 unsigned int reloc_shndx
,
2404 bool have_sections_script
)
2406 section_size_type input_section_size
= shdr
.get_sh_size();
2407 section_size_type uncompressed_size
;
2408 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
2409 if (object
->section_is_compressed(shndx
, &uncompressed_size
,
2411 input_section_size
= uncompressed_size
;
2413 if ((addralign
& (addralign
- 1)) != 0)
2415 object
->error(_("invalid alignment %lu for section \"%s\""),
2416 static_cast<unsigned long>(addralign
), secname
);
2420 if (addralign
> this->addralign_
)
2421 this->addralign_
= addralign
;
2423 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
2424 uint64_t entsize
= shdr
.get_sh_entsize();
2426 // .debug_str is a mergeable string section, but is not always so
2427 // marked by compilers. Mark manually here so we can optimize.
2428 if (strcmp(secname
, ".debug_str") == 0)
2430 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
2434 this->update_flags_for_input_section(sh_flags
);
2435 this->set_entsize(entsize
);
2437 // If this is a SHF_MERGE section, we pass all the input sections to
2438 // a Output_data_merge. We don't try to handle relocations for such
2439 // a section. We don't try to handle empty merge sections--they
2440 // mess up the mappings, and are useless anyhow.
2441 // FIXME: Need to handle merge sections during incremental update.
2442 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
2444 && shdr
.get_sh_size() > 0
2445 && !parameters
->incremental())
2447 // Keep information about merged input sections for rebuilding fast
2448 // lookup maps if we have sections-script or we do relaxation.
2449 bool keeps_input_sections
= (this->always_keeps_input_sections_
2450 || have_sections_script
2451 || parameters
->target().may_relax());
2453 if (this->add_merge_input_section(object
, shndx
, sh_flags
, entsize
,
2454 addralign
, keeps_input_sections
))
2456 // Tell the relocation routines that they need to call the
2457 // output_offset method to determine the final address.
2462 off_t offset_in_section
;
2464 if (this->has_fixed_layout())
2466 // For incremental updates, find a chunk of unused space in the section.
2467 offset_in_section
= this->free_list_
.allocate(input_section_size
,
2469 if (offset_in_section
== -1)
2470 gold_fallback(_("out of patch space in section %s; "
2471 "relink with --incremental-full"),
2473 return offset_in_section
;
2476 offset_in_section
= this->current_data_size_for_child();
2477 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2479 this->set_current_data_size_for_child(aligned_offset_in_section
2480 + input_section_size
);
2482 // Determine if we want to delay code-fill generation until the output
2483 // section is written. When the target is relaxing, we want to delay fill
2484 // generating to avoid adjusting them during relaxation. Also, if we are
2485 // sorting input sections we must delay fill generation.
2486 if (!this->generate_code_fills_at_write_
2487 && !have_sections_script
2488 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2489 && parameters
->target().has_code_fill()
2490 && (parameters
->target().may_relax()
2491 || layout
->is_section_ordering_specified()))
2493 gold_assert(this->fills_
.empty());
2494 this->generate_code_fills_at_write_
= true;
2497 if (aligned_offset_in_section
> offset_in_section
2498 && !this->generate_code_fills_at_write_
2499 && !have_sections_script
2500 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2501 && parameters
->target().has_code_fill())
2503 // We need to add some fill data. Using fill_list_ when
2504 // possible is an optimization, since we will often have fill
2505 // sections without input sections.
2506 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
2507 if (this->input_sections_
.empty())
2508 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
2511 std::string
fill_data(parameters
->target().code_fill(fill_len
));
2512 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
2513 this->input_sections_
.push_back(Input_section(odc
));
2517 // We need to keep track of this section if we are already keeping
2518 // track of sections, or if we are relaxing. Also, if this is a
2519 // section which requires sorting, or which may require sorting in
2520 // the future, we keep track of the sections. If the
2521 // --section-ordering-file option is used to specify the order of
2522 // sections, we need to keep track of sections.
2523 if (this->always_keeps_input_sections_
2524 || have_sections_script
2525 || !this->input_sections_
.empty()
2526 || this->may_sort_attached_input_sections()
2527 || this->must_sort_attached_input_sections()
2528 || parameters
->options().user_set_Map()
2529 || parameters
->target().may_relax()
2530 || layout
->is_section_ordering_specified())
2532 Input_section
isecn(object
, shndx
, input_section_size
, addralign
);
2533 /* If section ordering is requested by specifying a ordering file,
2534 using --section-ordering-file, match the section name with
2536 if (parameters
->options().section_ordering_file())
2538 unsigned int section_order_index
=
2539 layout
->find_section_order_index(std::string(secname
));
2540 if (section_order_index
!= 0)
2542 isecn
.set_section_order_index(section_order_index
);
2543 this->set_input_section_order_specified();
2546 this->input_sections_
.push_back(isecn
);
2549 return aligned_offset_in_section
;
2552 // Add arbitrary data to an output section.
2555 Output_section::add_output_section_data(Output_section_data
* posd
)
2557 Input_section
inp(posd
);
2558 this->add_output_section_data(&inp
);
2560 if (posd
->is_data_size_valid())
2562 off_t offset_in_section
;
2563 if (this->has_fixed_layout())
2565 // For incremental updates, find a chunk of unused space.
2566 offset_in_section
= this->free_list_
.allocate(posd
->data_size(),
2567 posd
->addralign(), 0);
2568 if (offset_in_section
== -1)
2569 gold_fallback(_("out of patch space in section %s; "
2570 "relink with --incremental-full"),
2572 // Finalize the address and offset now.
2573 uint64_t addr
= this->address();
2574 off_t offset
= this->offset();
2575 posd
->set_address_and_file_offset(addr
+ offset_in_section
,
2576 offset
+ offset_in_section
);
2580 offset_in_section
= this->current_data_size_for_child();
2581 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2583 this->set_current_data_size_for_child(aligned_offset_in_section
2584 + posd
->data_size());
2587 else if (this->has_fixed_layout())
2589 // For incremental updates, arrange for the data to have a fixed layout.
2590 // This will mean that additions to the data must be allocated from
2591 // free space within the containing output section.
2592 uint64_t addr
= this->address();
2593 posd
->set_address(addr
);
2594 posd
->set_file_offset(0);
2595 // FIXME: This should eventually be unreachable.
2596 // gold_unreachable();
2600 // Add a relaxed input section.
2603 Output_section::add_relaxed_input_section(Layout
* layout
,
2604 Output_relaxed_input_section
* poris
,
2605 const std::string
& name
)
2607 Input_section
inp(poris
);
2609 // If the --section-ordering-file option is used to specify the order of
2610 // sections, we need to keep track of sections.
2611 if (layout
->is_section_ordering_specified())
2613 unsigned int section_order_index
=
2614 layout
->find_section_order_index(name
);
2615 if (section_order_index
!= 0)
2617 inp
.set_section_order_index(section_order_index
);
2618 this->set_input_section_order_specified();
2622 this->add_output_section_data(&inp
);
2623 if (this->lookup_maps_
->is_valid())
2624 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2625 poris
->shndx(), poris
);
2627 // For a relaxed section, we use the current data size. Linker scripts
2628 // get all the input sections, including relaxed one from an output
2629 // section and add them back to the same output section to compute the
2630 // output section size. If we do not account for sizes of relaxed input
2631 // sections, an output section would be incorrectly sized.
2632 off_t offset_in_section
= this->current_data_size_for_child();
2633 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2634 poris
->addralign());
2635 this->set_current_data_size_for_child(aligned_offset_in_section
2636 + poris
->current_data_size());
2639 // Add arbitrary data to an output section by Input_section.
2642 Output_section::add_output_section_data(Input_section
* inp
)
2644 if (this->input_sections_
.empty())
2645 this->first_input_offset_
= this->current_data_size_for_child();
2647 this->input_sections_
.push_back(*inp
);
2649 uint64_t addralign
= inp
->addralign();
2650 if (addralign
> this->addralign_
)
2651 this->addralign_
= addralign
;
2653 inp
->set_output_section(this);
2656 // Add a merge section to an output section.
2659 Output_section::add_output_merge_section(Output_section_data
* posd
,
2660 bool is_string
, uint64_t entsize
)
2662 Input_section
inp(posd
, is_string
, entsize
);
2663 this->add_output_section_data(&inp
);
2666 // Add an input section to a SHF_MERGE section.
2669 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
2670 uint64_t flags
, uint64_t entsize
,
2672 bool keeps_input_sections
)
2674 // We cannot merge sections with entsize == 0.
2678 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
2680 // We cannot restore merged input section states.
2681 gold_assert(this->checkpoint_
== NULL
);
2683 // Look up merge sections by required properties.
2684 // Currently, we only invalidate the lookup maps in script processing
2685 // and relaxation. We should not have done either when we reach here.
2686 // So we assume that the lookup maps are valid to simply code.
2687 gold_assert(this->lookup_maps_
->is_valid());
2688 Merge_section_properties
msp(is_string
, entsize
, addralign
);
2689 Output_merge_base
* pomb
= this->lookup_maps_
->find_merge_section(msp
);
2690 bool is_new
= false;
2693 gold_assert(pomb
->is_string() == is_string
2694 && pomb
->entsize() == entsize
2695 && pomb
->addralign() == addralign
);
2699 // Create a new Output_merge_data or Output_merge_string_data.
2701 pomb
= new Output_merge_data(entsize
, addralign
);
2707 pomb
= new Output_merge_string
<char>(addralign
);
2710 pomb
= new Output_merge_string
<char16_t
>(addralign
);
2713 pomb
= new Output_merge_string
<char32_t
>(addralign
);
2719 // If we need to do script processing or relaxation, we need to keep
2720 // the original input sections to rebuild the fast lookup maps.
2721 if (keeps_input_sections
)
2722 pomb
->set_keeps_input_sections();
2726 if (pomb
->add_input_section(object
, shndx
))
2728 // Add new merge section to this output section and link merge
2729 // section properties to new merge section in map.
2732 this->add_output_merge_section(pomb
, is_string
, entsize
);
2733 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2740 // If add_input_section failed, delete new merge section to avoid
2741 // exporting empty merge sections in Output_section::get_input_section.
2748 // Build a relaxation map to speed up relaxation of existing input sections.
2749 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2752 Output_section::build_relaxation_map(
2753 const Input_section_list
& input_sections
,
2755 Relaxation_map
* relaxation_map
) const
2757 for (size_t i
= 0; i
< limit
; ++i
)
2759 const Input_section
& is(input_sections
[i
]);
2760 if (is
.is_input_section() || is
.is_relaxed_input_section())
2762 Section_id
sid(is
.relobj(), is
.shndx());
2763 (*relaxation_map
)[sid
] = i
;
2768 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2769 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2770 // indices of INPUT_SECTIONS.
2773 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2774 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
2775 const Relaxation_map
& map
,
2776 Input_section_list
* input_sections
)
2778 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2780 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2781 Section_id
sid(poris
->relobj(), poris
->shndx());
2782 Relaxation_map::const_iterator p
= map
.find(sid
);
2783 gold_assert(p
!= map
.end());
2784 gold_assert((*input_sections
)[p
->second
].is_input_section());
2786 // Remember section order index of original input section
2787 // if it is set. Copy it to the relaxed input section.
2789 (*input_sections
)[p
->second
].section_order_index();
2790 (*input_sections
)[p
->second
] = Input_section(poris
);
2791 (*input_sections
)[p
->second
].set_section_order_index(soi
);
2795 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2796 // is a vector of pointers to Output_relaxed_input_section or its derived
2797 // classes. The relaxed sections must correspond to existing input sections.
2800 Output_section::convert_input_sections_to_relaxed_sections(
2801 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
)
2803 gold_assert(parameters
->target().may_relax());
2805 // We want to make sure that restore_states does not undo the effect of
2806 // this. If there is no checkpoint active, just search the current
2807 // input section list and replace the sections there. If there is
2808 // a checkpoint, also replace the sections there.
2810 // By default, we look at the whole list.
2811 size_t limit
= this->input_sections_
.size();
2813 if (this->checkpoint_
!= NULL
)
2815 // Replace input sections with relaxed input section in the saved
2816 // copy of the input section list.
2817 if (this->checkpoint_
->input_sections_saved())
2820 this->build_relaxation_map(
2821 *(this->checkpoint_
->input_sections()),
2822 this->checkpoint_
->input_sections()->size(),
2824 this->convert_input_sections_in_list_to_relaxed_sections(
2827 this->checkpoint_
->input_sections());
2831 // We have not copied the input section list yet. Instead, just
2832 // look at the portion that would be saved.
2833 limit
= this->checkpoint_
->input_sections_size();
2837 // Convert input sections in input_section_list.
2839 this->build_relaxation_map(this->input_sections_
, limit
, &map
);
2840 this->convert_input_sections_in_list_to_relaxed_sections(
2843 &this->input_sections_
);
2845 // Update fast look-up map.
2846 if (this->lookup_maps_
->is_valid())
2847 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2849 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2850 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2851 poris
->shndx(), poris
);
2855 // Update the output section flags based on input section flags.
2858 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags
)
2860 // If we created the section with SHF_ALLOC clear, we set the
2861 // address. If we are now setting the SHF_ALLOC flag, we need to
2863 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0
2864 && (flags
& elfcpp::SHF_ALLOC
) != 0)
2865 this->mark_address_invalid();
2867 this->flags_
|= (flags
2868 & (elfcpp::SHF_WRITE
2870 | elfcpp::SHF_EXECINSTR
));
2872 if ((flags
& elfcpp::SHF_MERGE
) == 0)
2873 this->flags_
&=~ elfcpp::SHF_MERGE
;
2876 if (this->current_data_size_for_child() == 0)
2877 this->flags_
|= elfcpp::SHF_MERGE
;
2880 if ((flags
& elfcpp::SHF_STRINGS
) == 0)
2881 this->flags_
&=~ elfcpp::SHF_STRINGS
;
2884 if (this->current_data_size_for_child() == 0)
2885 this->flags_
|= elfcpp::SHF_STRINGS
;
2889 // Find the merge section into which an input section with index SHNDX in
2890 // OBJECT has been added. Return NULL if none found.
2892 const Output_section_data
*
2893 Output_section::find_merge_section(const Relobj
* object
,
2894 unsigned int shndx
) const
2896 return object
->find_merge_section(shndx
);
2899 // Build the lookup maps for relaxed sections. This needs
2900 // to be declared as a const method so that it is callable with a const
2901 // Output_section pointer. The method only updates states of the maps.
2904 Output_section::build_lookup_maps() const
2906 this->lookup_maps_
->clear();
2907 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2908 p
!= this->input_sections_
.end();
2911 if (p
->is_relaxed_input_section())
2913 Output_relaxed_input_section
* poris
= p
->relaxed_input_section();
2914 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2915 poris
->shndx(), poris
);
2920 // Find an relaxed input section corresponding to an input section
2921 // in OBJECT with index SHNDX.
2923 const Output_relaxed_input_section
*
2924 Output_section::find_relaxed_input_section(const Relobj
* object
,
2925 unsigned int shndx
) const
2927 if (!this->lookup_maps_
->is_valid())
2928 this->build_lookup_maps();
2929 return this->lookup_maps_
->find_relaxed_input_section(object
, shndx
);
2932 // Given an address OFFSET relative to the start of input section
2933 // SHNDX in OBJECT, return whether this address is being included in
2934 // the final link. This should only be called if SHNDX in OBJECT has
2935 // a special mapping.
2938 Output_section::is_input_address_mapped(const Relobj
* object
,
2942 // Look at the Output_section_data_maps first.
2943 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2945 posd
= this->find_relaxed_input_section(object
, shndx
);
2949 section_offset_type output_offset
;
2950 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2951 // By default we assume that the address is mapped. See comment at the
2955 return output_offset
!= -1;
2958 // Fall back to the slow look-up.
2959 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2960 p
!= this->input_sections_
.end();
2963 section_offset_type output_offset
;
2964 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2965 return output_offset
!= -1;
2968 // By default we assume that the address is mapped. This should
2969 // only be called after we have passed all sections to Layout. At
2970 // that point we should know what we are discarding.
2974 // Given an address OFFSET relative to the start of input section
2975 // SHNDX in object OBJECT, return the output offset relative to the
2976 // start of the input section in the output section. This should only
2977 // be called if SHNDX in OBJECT has a special mapping.
2980 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2981 section_offset_type offset
) const
2983 // This can only be called meaningfully when we know the data size
2985 gold_assert(this->is_data_size_valid());
2987 // Look at the Output_section_data_maps first.
2988 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2990 posd
= this->find_relaxed_input_section(object
, shndx
);
2993 section_offset_type output_offset
;
2994 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2996 return output_offset
;
2999 // Fall back to the slow look-up.
3000 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3001 p
!= this->input_sections_
.end();
3004 section_offset_type output_offset
;
3005 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
3006 return output_offset
;
3011 // Return the output virtual address of OFFSET relative to the start
3012 // of input section SHNDX in object OBJECT.
3015 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
3018 uint64_t addr
= this->address() + this->first_input_offset_
;
3020 // Look at the Output_section_data_maps first.
3021 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
3023 posd
= this->find_relaxed_input_section(object
, shndx
);
3024 if (posd
!= NULL
&& posd
->is_address_valid())
3026 section_offset_type output_offset
;
3027 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
3029 return posd
->address() + output_offset
;
3032 // Fall back to the slow look-up.
3033 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3034 p
!= this->input_sections_
.end();
3037 addr
= align_address(addr
, p
->addralign());
3038 section_offset_type output_offset
;
3039 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
3041 if (output_offset
== -1)
3043 return addr
+ output_offset
;
3045 addr
+= p
->data_size();
3048 // If we get here, it means that we don't know the mapping for this
3049 // input section. This might happen in principle if
3050 // add_input_section were called before add_output_section_data.
3051 // But it should never actually happen.
3056 // Find the output address of the start of the merged section for
3057 // input section SHNDX in object OBJECT.
3060 Output_section::find_starting_output_address(const Relobj
* object
,
3062 uint64_t* paddr
) const
3064 const Output_section_data
* data
= this->find_merge_section(object
, shndx
);
3068 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3069 // Looking up the merge section map does not always work as we sometimes
3070 // find a merge section without its address set.
3071 uint64_t addr
= this->address() + this->first_input_offset_
;
3072 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3073 p
!= this->input_sections_
.end();
3076 addr
= align_address(addr
, p
->addralign());
3078 // It would be nice if we could use the existing output_offset
3079 // method to get the output offset of input offset 0.
3080 // Unfortunately we don't know for sure that input offset 0 is
3082 if (!p
->is_input_section() && p
->output_section_data() == data
)
3088 addr
+= p
->data_size();
3091 // We couldn't find a merge output section for this input section.
3095 // Update the data size of an Output_section.
3098 Output_section::update_data_size()
3100 if (this->input_sections_
.empty())
3103 if (this->must_sort_attached_input_sections()
3104 || this->input_section_order_specified())
3105 this->sort_attached_input_sections();
3107 off_t off
= this->first_input_offset_
;
3108 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3109 p
!= this->input_sections_
.end();
3112 off
= align_address(off
, p
->addralign());
3113 off
+= p
->current_data_size();
3116 this->set_current_data_size_for_child(off
);
3119 // Set the data size of an Output_section. This is where we handle
3120 // setting the addresses of any Output_section_data objects.
3123 Output_section::set_final_data_size()
3127 if (this->input_sections_
.empty())
3128 data_size
= this->current_data_size_for_child();
3131 if (this->must_sort_attached_input_sections()
3132 || this->input_section_order_specified())
3133 this->sort_attached_input_sections();
3135 uint64_t address
= this->address();
3136 off_t startoff
= this->offset();
3137 off_t off
= this->first_input_offset_
;
3138 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3139 p
!= this->input_sections_
.end();
3142 off
= align_address(off
, p
->addralign());
3143 p
->set_address_and_file_offset(address
+ off
, startoff
+ off
,
3145 off
+= p
->data_size();
3150 // For full incremental links, we want to allocate some patch space
3151 // in most sections for subsequent incremental updates.
3152 if (this->is_patch_space_allowed_
&& parameters
->incremental_full())
3154 double pct
= parameters
->options().incremental_patch();
3155 size_t extra
= static_cast<size_t>(data_size
* pct
);
3156 if (this->free_space_fill_
!= NULL
3157 && this->free_space_fill_
->minimum_hole_size() > extra
)
3158 extra
= this->free_space_fill_
->minimum_hole_size();
3159 off_t new_size
= align_address(data_size
+ extra
, this->addralign());
3160 this->patch_space_
= new_size
- data_size
;
3161 gold_debug(DEBUG_INCREMENTAL
,
3162 "set_final_data_size: %08lx + %08lx: section %s",
3163 static_cast<long>(data_size
),
3164 static_cast<long>(this->patch_space_
),
3166 data_size
= new_size
;
3169 this->set_data_size(data_size
);
3172 // Reset the address and file offset.
3175 Output_section::do_reset_address_and_file_offset()
3177 // An unallocated section has no address. Forcing this means that
3178 // we don't need special treatment for symbols defined in debug
3179 // sections. We do the same in the constructor. This does not
3180 // apply to NOLOAD sections though.
3181 if (((this->flags_
& elfcpp::SHF_ALLOC
) == 0) && !this->is_noload_
)
3182 this->set_address(0);
3184 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3185 p
!= this->input_sections_
.end();
3187 p
->reset_address_and_file_offset();
3189 // Remove any patch space that was added in set_final_data_size.
3190 if (this->patch_space_
> 0)
3192 this->set_current_data_size_for_child(this->current_data_size_for_child()
3193 - this->patch_space_
);
3194 this->patch_space_
= 0;
3198 // Return true if address and file offset have the values after reset.
3201 Output_section::do_address_and_file_offset_have_reset_values() const
3203 if (this->is_offset_valid())
3206 // An unallocated section has address 0 after its construction or a reset.
3207 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0)
3208 return this->is_address_valid() && this->address() == 0;
3210 return !this->is_address_valid();
3213 // Set the TLS offset. Called only for SHT_TLS sections.
3216 Output_section::do_set_tls_offset(uint64_t tls_base
)
3218 this->tls_offset_
= this->address() - tls_base
;
3221 // In a few cases we need to sort the input sections attached to an
3222 // output section. This is used to implement the type of constructor
3223 // priority ordering implemented by the GNU linker, in which the
3224 // priority becomes part of the section name and the sections are
3225 // sorted by name. We only do this for an output section if we see an
3226 // attached input section matching ".ctors.*", ".dtors.*",
3227 // ".init_array.*" or ".fini_array.*".
3229 class Output_section::Input_section_sort_entry
3232 Input_section_sort_entry()
3233 : input_section_(), index_(-1U), section_name_()
3236 Input_section_sort_entry(const Input_section
& input_section
,
3238 bool must_sort_attached_input_sections
,
3239 const char* output_section_name
)
3240 : input_section_(input_section
), index_(index
), section_name_()
3242 if ((input_section
.is_input_section()
3243 || input_section
.is_relaxed_input_section())
3244 && must_sort_attached_input_sections
)
3246 // This is only called single-threaded from Layout::finalize,
3247 // so it is OK to lock. Unfortunately we have no way to pass
3249 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
3250 Object
* obj
= (input_section
.is_input_section()
3251 ? input_section
.relobj()
3252 : input_section
.relaxed_input_section()->relobj());
3253 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
3255 // This is a slow operation, which should be cached in
3256 // Layout::layout if this becomes a speed problem.
3257 this->section_name_
= obj
->section_name(input_section
.shndx());
3259 else if (input_section
.is_output_section_data()
3260 && must_sort_attached_input_sections
)
3262 // For linker-generated sections, use the output section name.
3263 this->section_name_
.assign(output_section_name
);
3267 // Return the Input_section.
3268 const Input_section
&
3269 input_section() const
3271 gold_assert(this->index_
!= -1U);
3272 return this->input_section_
;
3275 // The index of this entry in the original list. This is used to
3276 // make the sort stable.
3280 gold_assert(this->index_
!= -1U);
3281 return this->index_
;
3284 // The section name.
3286 section_name() const
3288 return this->section_name_
;
3291 // Return true if the section name has a priority. This is assumed
3292 // to be true if it has a dot after the initial dot.
3294 has_priority() const
3296 return this->section_name_
.find('.', 1) != std::string::npos
;
3299 // Return the priority. Believe it or not, gcc encodes the priority
3300 // differently for .ctors/.dtors and .init_array/.fini_array
3303 get_priority() const
3306 if (is_prefix_of(".ctors.", this->section_name_
.c_str())
3307 || is_prefix_of(".dtors.", this->section_name_
.c_str()))
3309 else if (is_prefix_of(".init_array.", this->section_name_
.c_str())
3310 || is_prefix_of(".fini_array.", this->section_name_
.c_str()))
3315 unsigned long prio
= strtoul((this->section_name_
.c_str()
3316 + (is_ctors
? 7 : 12)),
3321 return 65535 - prio
;
3326 // Return true if this an input file whose base name matches
3327 // FILE_NAME. The base name must have an extension of ".o", and
3328 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3329 // This is to match crtbegin.o as well as crtbeginS.o without
3330 // getting confused by other possibilities. Overall matching the
3331 // file name this way is a dreadful hack, but the GNU linker does it
3332 // in order to better support gcc, and we need to be compatible.
3334 match_file_name(const char* file_name
) const
3336 if (this->input_section_
.is_output_section_data())
3338 return Layout::match_file_name(this->input_section_
.relobj(), file_name
);
3341 // Returns 1 if THIS should appear before S in section order, -1 if S
3342 // appears before THIS and 0 if they are not comparable.
3344 compare_section_ordering(const Input_section_sort_entry
& s
) const
3346 unsigned int this_secn_index
= this->input_section_
.section_order_index();
3347 unsigned int s_secn_index
= s
.input_section().section_order_index();
3348 if (this_secn_index
> 0 && s_secn_index
> 0)
3350 if (this_secn_index
< s_secn_index
)
3352 else if (this_secn_index
> s_secn_index
)
3359 // The Input_section we are sorting.
3360 Input_section input_section_
;
3361 // The index of this Input_section in the original list.
3362 unsigned int index_
;
3363 // The section name if there is one.
3364 std::string section_name_
;
3367 // Return true if S1 should come before S2 in the output section.
3370 Output_section::Input_section_sort_compare::operator()(
3371 const Output_section::Input_section_sort_entry
& s1
,
3372 const Output_section::Input_section_sort_entry
& s2
) const
3374 // crtbegin.o must come first.
3375 bool s1_begin
= s1
.match_file_name("crtbegin");
3376 bool s2_begin
= s2
.match_file_name("crtbegin");
3377 if (s1_begin
|| s2_begin
)
3383 return s1
.index() < s2
.index();
3386 // crtend.o must come last.
3387 bool s1_end
= s1
.match_file_name("crtend");
3388 bool s2_end
= s2
.match_file_name("crtend");
3389 if (s1_end
|| s2_end
)
3395 return s1
.index() < s2
.index();
3398 // A section with a priority follows a section without a priority.
3399 bool s1_has_priority
= s1
.has_priority();
3400 bool s2_has_priority
= s2
.has_priority();
3401 if (s1_has_priority
&& !s2_has_priority
)
3403 if (!s1_has_priority
&& s2_has_priority
)
3406 // Check if a section order exists for these sections through a section
3407 // ordering file. If sequence_num is 0, an order does not exist.
3408 int sequence_num
= s1
.compare_section_ordering(s2
);
3409 if (sequence_num
!= 0)
3410 return sequence_num
== 1;
3412 // Otherwise we sort by name.
3413 int compare
= s1
.section_name().compare(s2
.section_name());
3417 // Otherwise we keep the input order.
3418 return s1
.index() < s2
.index();
3421 // Return true if S1 should come before S2 in an .init_array or .fini_array
3425 Output_section::Input_section_sort_init_fini_compare::operator()(
3426 const Output_section::Input_section_sort_entry
& s1
,
3427 const Output_section::Input_section_sort_entry
& s2
) const
3429 // A section without a priority follows a section with a priority.
3430 // This is the reverse of .ctors and .dtors sections.
3431 bool s1_has_priority
= s1
.has_priority();
3432 bool s2_has_priority
= s2
.has_priority();
3433 if (s1_has_priority
&& !s2_has_priority
)
3435 if (!s1_has_priority
&& s2_has_priority
)
3438 // .ctors and .dtors sections without priority come after
3439 // .init_array and .fini_array sections without priority.
3440 if (!s1_has_priority
3441 && (s1
.section_name() == ".ctors" || s1
.section_name() == ".dtors")
3442 && s1
.section_name() != s2
.section_name())
3444 if (!s2_has_priority
3445 && (s2
.section_name() == ".ctors" || s2
.section_name() == ".dtors")
3446 && s2
.section_name() != s1
.section_name())
3449 // Sort by priority if we can.
3450 if (s1_has_priority
)
3452 unsigned int s1_prio
= s1
.get_priority();
3453 unsigned int s2_prio
= s2
.get_priority();
3454 if (s1_prio
< s2_prio
)
3456 else if (s1_prio
> s2_prio
)
3460 // Check if a section order exists for these sections through a section
3461 // ordering file. If sequence_num is 0, an order does not exist.
3462 int sequence_num
= s1
.compare_section_ordering(s2
);
3463 if (sequence_num
!= 0)
3464 return sequence_num
== 1;
3466 // Otherwise we sort by name.
3467 int compare
= s1
.section_name().compare(s2
.section_name());
3471 // Otherwise we keep the input order.
3472 return s1
.index() < s2
.index();
3475 // Return true if S1 should come before S2. Sections that do not match
3476 // any pattern in the section ordering file are placed ahead of the sections
3477 // that match some pattern.
3480 Output_section::Input_section_sort_section_order_index_compare::operator()(
3481 const Output_section::Input_section_sort_entry
& s1
,
3482 const Output_section::Input_section_sort_entry
& s2
) const
3484 unsigned int s1_secn_index
= s1
.input_section().section_order_index();
3485 unsigned int s2_secn_index
= s2
.input_section().section_order_index();
3487 // Keep input order if section ordering cannot determine order.
3488 if (s1_secn_index
== s2_secn_index
)
3489 return s1
.index() < s2
.index();
3491 return s1_secn_index
< s2_secn_index
;
3494 // Return true if S1 should come before S2. This is the sort comparison
3495 // function for .text to sort sections with prefixes
3496 // .text.{unlikely,exit,startup,hot} before other sections.
3499 Output_section::Input_section_sort_section_prefix_special_ordering_compare
3501 const Output_section::Input_section_sort_entry
& s1
,
3502 const Output_section::Input_section_sort_entry
& s2
) const
3504 // Some input section names have special ordering requirements.
3505 const char *s1_section_name
= s1
.section_name().c_str();
3506 const char *s2_section_name
= s2
.section_name().c_str();
3507 int o1
= Layout::special_ordering_of_input_section(s1_section_name
);
3508 int o2
= Layout::special_ordering_of_input_section(s2_section_name
);
3518 else if (is_prefix_of(".text.sorted", s1_section_name
))
3519 return strcmp(s1_section_name
, s2_section_name
) <= 0;
3521 // Keep input order otherwise.
3522 return s1
.index() < s2
.index();
3525 // Return true if S1 should come before S2. This is the sort comparison
3526 // function for sections to sort them by name.
3529 Output_section::Input_section_sort_section_name_compare
3531 const Output_section::Input_section_sort_entry
& s1
,
3532 const Output_section::Input_section_sort_entry
& s2
) const
3535 int compare
= s1
.section_name().compare(s2
.section_name());
3539 // Keep input order otherwise.
3540 return s1
.index() < s2
.index();
3543 // This updates the section order index of input sections according to the
3544 // the order specified in the mapping from Section id to order index.
3547 Output_section::update_section_layout(
3548 const Section_layout_order
* order_map
)
3550 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3551 p
!= this->input_sections_
.end();
3554 if (p
->is_input_section()
3555 || p
->is_relaxed_input_section())
3557 Relobj
* obj
= (p
->is_input_section()
3559 : p
->relaxed_input_section()->relobj());
3560 unsigned int shndx
= p
->shndx();
3561 Section_layout_order::const_iterator it
3562 = order_map
->find(Section_id(obj
, shndx
));
3563 if (it
== order_map
->end())
3565 unsigned int section_order_index
= it
->second
;
3566 if (section_order_index
!= 0)
3568 p
->set_section_order_index(section_order_index
);
3569 this->set_input_section_order_specified();
3575 // Sort the input sections attached to an output section.
3578 Output_section::sort_attached_input_sections()
3580 if (this->attached_input_sections_are_sorted_
)
3583 if (this->checkpoint_
!= NULL
3584 && !this->checkpoint_
->input_sections_saved())
3585 this->checkpoint_
->save_input_sections();
3587 // The only thing we know about an input section is the object and
3588 // the section index. We need the section name. Recomputing this
3589 // is slow but this is an unusual case. If this becomes a speed
3590 // problem we can cache the names as required in Layout::layout.
3592 // We start by building a larger vector holding a copy of each
3593 // Input_section, plus its current index in the list and its name.
3594 std::vector
<Input_section_sort_entry
> sort_list
;
3597 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3598 p
!= this->input_sections_
.end();
3600 sort_list
.push_back(Input_section_sort_entry(*p
, i
,
3601 this->must_sort_attached_input_sections(),
3604 // Sort the input sections.
3605 if (this->must_sort_attached_input_sections())
3607 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3608 || this->type() == elfcpp::SHT_INIT_ARRAY
3609 || this->type() == elfcpp::SHT_FINI_ARRAY
)
3610 std::sort(sort_list
.begin(), sort_list
.end(),
3611 Input_section_sort_init_fini_compare());
3612 else if (strcmp(parameters
->options().sort_section(), "name") == 0)
3613 std::sort(sort_list
.begin(), sort_list
.end(),
3614 Input_section_sort_section_name_compare());
3615 else if (strcmp(this->name(), ".text") == 0)
3616 std::sort(sort_list
.begin(), sort_list
.end(),
3617 Input_section_sort_section_prefix_special_ordering_compare());
3619 std::sort(sort_list
.begin(), sort_list
.end(),
3620 Input_section_sort_compare());
3624 gold_assert(this->input_section_order_specified());
3625 std::sort(sort_list
.begin(), sort_list
.end(),
3626 Input_section_sort_section_order_index_compare());
3629 // Copy the sorted input sections back to our list.
3630 this->input_sections_
.clear();
3631 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
3632 p
!= sort_list
.end();
3634 this->input_sections_
.push_back(p
->input_section());
3637 // Remember that we sorted the input sections, since we might get
3639 this->attached_input_sections_are_sorted_
= true;
3642 // Write the section header to *OSHDR.
3644 template<int size
, bool big_endian
>
3646 Output_section::write_header(const Layout
* layout
,
3647 const Stringpool
* secnamepool
,
3648 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
3650 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
3651 oshdr
->put_sh_type(this->type_
);
3653 elfcpp::Elf_Xword flags
= this->flags_
;
3654 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
3655 flags
|= elfcpp::SHF_INFO_LINK
;
3656 oshdr
->put_sh_flags(flags
);
3658 oshdr
->put_sh_addr(this->address());
3659 oshdr
->put_sh_offset(this->offset());
3660 oshdr
->put_sh_size(this->data_size());
3661 if (this->link_section_
!= NULL
)
3662 oshdr
->put_sh_link(this->link_section_
->out_shndx());
3663 else if (this->should_link_to_symtab_
)
3664 oshdr
->put_sh_link(layout
->symtab_section_shndx());
3665 else if (this->should_link_to_dynsym_
)
3666 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
3668 oshdr
->put_sh_link(this->link_
);
3670 elfcpp::Elf_Word info
;
3671 if (this->info_section_
!= NULL
)
3673 if (this->info_uses_section_index_
)
3674 info
= this->info_section_
->out_shndx();
3676 info
= this->info_section_
->symtab_index();
3678 else if (this->info_symndx_
!= NULL
)
3679 info
= this->info_symndx_
->symtab_index();
3682 oshdr
->put_sh_info(info
);
3684 oshdr
->put_sh_addralign(this->addralign_
);
3685 oshdr
->put_sh_entsize(this->entsize_
);
3688 // Write out the data. For input sections the data is written out by
3689 // Object::relocate, but we have to handle Output_section_data objects
3693 Output_section::do_write(Output_file
* of
)
3695 gold_assert(!this->requires_postprocessing());
3697 // If the target performs relaxation, we delay filler generation until now.
3698 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3700 off_t output_section_file_offset
= this->offset();
3701 for (Fill_list::iterator p
= this->fills_
.begin();
3702 p
!= this->fills_
.end();
3705 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3706 of
->write(output_section_file_offset
+ p
->section_offset(),
3707 fill_data
.data(), fill_data
.size());
3710 off_t off
= this->offset() + this->first_input_offset_
;
3711 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3712 p
!= this->input_sections_
.end();
3715 off_t aligned_off
= align_address(off
, p
->addralign());
3716 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3718 size_t fill_len
= aligned_off
- off
;
3719 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3720 of
->write(off
, fill_data
.data(), fill_data
.size());
3724 off
= aligned_off
+ p
->data_size();
3727 // For incremental links, fill in unused chunks in debug sections
3728 // with dummy compilation unit headers.
3729 if (this->free_space_fill_
!= NULL
)
3731 for (Free_list::Const_iterator p
= this->free_list_
.begin();
3732 p
!= this->free_list_
.end();
3735 off_t off
= p
->start_
;
3736 size_t len
= p
->end_
- off
;
3737 this->free_space_fill_
->write(of
, this->offset() + off
, len
);
3739 if (this->patch_space_
> 0)
3741 off_t off
= this->current_data_size_for_child() - this->patch_space_
;
3742 this->free_space_fill_
->write(of
, this->offset() + off
,
3743 this->patch_space_
);
3748 // If a section requires postprocessing, create the buffer to use.
3751 Output_section::create_postprocessing_buffer()
3753 gold_assert(this->requires_postprocessing());
3755 if (this->postprocessing_buffer_
!= NULL
)
3758 if (!this->input_sections_
.empty())
3760 off_t off
= this->first_input_offset_
;
3761 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3762 p
!= this->input_sections_
.end();
3765 off
= align_address(off
, p
->addralign());
3766 p
->finalize_data_size();
3767 off
+= p
->data_size();
3769 this->set_current_data_size_for_child(off
);
3772 off_t buffer_size
= this->current_data_size_for_child();
3773 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
3776 // Write all the data of an Output_section into the postprocessing
3777 // buffer. This is used for sections which require postprocessing,
3778 // such as compression. Input sections are handled by
3779 // Object::Relocate.
3782 Output_section::write_to_postprocessing_buffer()
3784 gold_assert(this->requires_postprocessing());
3786 // If the target performs relaxation, we delay filler generation until now.
3787 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3789 unsigned char* buffer
= this->postprocessing_buffer();
3790 for (Fill_list::iterator p
= this->fills_
.begin();
3791 p
!= this->fills_
.end();
3794 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3795 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
3799 off_t off
= this->first_input_offset_
;
3800 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3801 p
!= this->input_sections_
.end();
3804 off_t aligned_off
= align_address(off
, p
->addralign());
3805 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3807 size_t fill_len
= aligned_off
- off
;
3808 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3809 memcpy(buffer
+ off
, fill_data
.data(), fill_data
.size());
3812 p
->write_to_buffer(buffer
+ aligned_off
);
3813 off
= aligned_off
+ p
->data_size();
3817 // Get the input sections for linker script processing. We leave
3818 // behind the Output_section_data entries. Note that this may be
3819 // slightly incorrect for merge sections. We will leave them behind,
3820 // but it is possible that the script says that they should follow
3821 // some other input sections, as in:
3822 // .rodata { *(.rodata) *(.rodata.cst*) }
3823 // For that matter, we don't handle this correctly:
3824 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3825 // With luck this will never matter.
3828 Output_section::get_input_sections(
3830 const std::string
& fill
,
3831 std::list
<Input_section
>* input_sections
)
3833 if (this->checkpoint_
!= NULL
3834 && !this->checkpoint_
->input_sections_saved())
3835 this->checkpoint_
->save_input_sections();
3837 // Invalidate fast look-up maps.
3838 this->lookup_maps_
->invalidate();
3840 uint64_t orig_address
= address
;
3842 address
= align_address(address
, this->addralign());
3844 Input_section_list remaining
;
3845 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3846 p
!= this->input_sections_
.end();
3849 if (p
->is_input_section()
3850 || p
->is_relaxed_input_section()
3851 || p
->is_merge_section())
3852 input_sections
->push_back(*p
);
3855 uint64_t aligned_address
= align_address(address
, p
->addralign());
3856 if (aligned_address
!= address
&& !fill
.empty())
3858 section_size_type length
=
3859 convert_to_section_size_type(aligned_address
- address
);
3860 std::string this_fill
;
3861 this_fill
.reserve(length
);
3862 while (this_fill
.length() + fill
.length() <= length
)
3864 if (this_fill
.length() < length
)
3865 this_fill
.append(fill
, 0, length
- this_fill
.length());
3867 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
3868 remaining
.push_back(Input_section(posd
));
3870 address
= aligned_address
;
3872 remaining
.push_back(*p
);
3874 p
->finalize_data_size();
3875 address
+= p
->data_size();
3879 this->input_sections_
.swap(remaining
);
3880 this->first_input_offset_
= 0;
3882 uint64_t data_size
= address
- orig_address
;
3883 this->set_current_data_size_for_child(data_size
);
3887 // Add a script input section. SIS is an Output_section::Input_section,
3888 // which can be either a plain input section or a special input section like
3889 // a relaxed input section. For a special input section, its size must be
3893 Output_section::add_script_input_section(const Input_section
& sis
)
3895 uint64_t data_size
= sis
.data_size();
3896 uint64_t addralign
= sis
.addralign();
3897 if (addralign
> this->addralign_
)
3898 this->addralign_
= addralign
;
3900 off_t offset_in_section
= this->current_data_size_for_child();
3901 off_t aligned_offset_in_section
= align_address(offset_in_section
,
3904 this->set_current_data_size_for_child(aligned_offset_in_section
3907 this->input_sections_
.push_back(sis
);
3909 // Update fast lookup maps if necessary.
3910 if (this->lookup_maps_
->is_valid())
3912 if (sis
.is_relaxed_input_section())
3914 Output_relaxed_input_section
* poris
= sis
.relaxed_input_section();
3915 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
3916 poris
->shndx(), poris
);
3921 // Save states for relaxation.
3924 Output_section::save_states()
3926 gold_assert(this->checkpoint_
== NULL
);
3927 Checkpoint_output_section
* checkpoint
=
3928 new Checkpoint_output_section(this->addralign_
, this->flags_
,
3929 this->input_sections_
,
3930 this->first_input_offset_
,
3931 this->attached_input_sections_are_sorted_
);
3932 this->checkpoint_
= checkpoint
;
3933 gold_assert(this->fills_
.empty());
3937 Output_section::discard_states()
3939 gold_assert(this->checkpoint_
!= NULL
);
3940 delete this->checkpoint_
;
3941 this->checkpoint_
= NULL
;
3942 gold_assert(this->fills_
.empty());
3944 // Simply invalidate the fast lookup maps since we do not keep
3946 this->lookup_maps_
->invalidate();
3950 Output_section::restore_states()
3952 gold_assert(this->checkpoint_
!= NULL
);
3953 Checkpoint_output_section
* checkpoint
= this->checkpoint_
;
3955 this->addralign_
= checkpoint
->addralign();
3956 this->flags_
= checkpoint
->flags();
3957 this->first_input_offset_
= checkpoint
->first_input_offset();
3959 if (!checkpoint
->input_sections_saved())
3961 // If we have not copied the input sections, just resize it.
3962 size_t old_size
= checkpoint
->input_sections_size();
3963 gold_assert(this->input_sections_
.size() >= old_size
);
3964 this->input_sections_
.resize(old_size
);
3968 // We need to copy the whole list. This is not efficient for
3969 // extremely large output with hundreads of thousands of input
3970 // objects. We may need to re-think how we should pass sections
3972 this->input_sections_
= *checkpoint
->input_sections();
3975 this->attached_input_sections_are_sorted_
=
3976 checkpoint
->attached_input_sections_are_sorted();
3978 // Simply invalidate the fast lookup maps since we do not keep
3980 this->lookup_maps_
->invalidate();
3983 // Update the section offsets of input sections in this. This is required if
3984 // relaxation causes some input sections to change sizes.
3987 Output_section::adjust_section_offsets()
3989 if (!this->section_offsets_need_adjustment_
)
3993 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3994 p
!= this->input_sections_
.end();
3997 off
= align_address(off
, p
->addralign());
3998 if (p
->is_input_section())
3999 p
->relobj()->set_section_offset(p
->shndx(), off
);
4000 off
+= p
->data_size();
4003 this->section_offsets_need_adjustment_
= false;
4006 // Print to the map file.
4009 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
4011 mapfile
->print_output_section(this);
4013 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
4014 p
!= this->input_sections_
.end();
4016 p
->print_to_mapfile(mapfile
);
4019 // Print stats for merge sections to stderr.
4022 Output_section::print_merge_stats()
4024 Input_section_list::iterator p
;
4025 for (p
= this->input_sections_
.begin();
4026 p
!= this->input_sections_
.end();
4028 p
->print_merge_stats(this->name_
);
4031 // Set a fixed layout for the section. Used for incremental update links.
4034 Output_section::set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
,
4035 off_t sh_size
, uint64_t sh_addralign
)
4037 this->addralign_
= sh_addralign
;
4038 this->set_current_data_size(sh_size
);
4039 if ((this->flags_
& elfcpp::SHF_ALLOC
) != 0)
4040 this->set_address(sh_addr
);
4041 this->set_file_offset(sh_offset
);
4042 this->finalize_data_size();
4043 this->free_list_
.init(sh_size
, false);
4044 this->has_fixed_layout_
= true;
4047 // Reserve space within the fixed layout for the section. Used for
4048 // incremental update links.
4051 Output_section::reserve(uint64_t sh_offset
, uint64_t sh_size
)
4053 this->free_list_
.remove(sh_offset
, sh_offset
+ sh_size
);
4056 // Allocate space from the free list for the section. Used for
4057 // incremental update links.
4060 Output_section::allocate(off_t len
, uint64_t addralign
)
4062 return this->free_list_
.allocate(len
, addralign
, 0);
4065 // Output segment methods.
4067 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
4078 is_max_align_known_(false),
4079 are_addresses_set_(false),
4080 is_large_data_segment_(false),
4081 is_unique_segment_(false)
4083 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4085 if (type
== elfcpp::PT_TLS
)
4086 this->flags_
= elfcpp::PF_R
;
4089 // Add an Output_section to a PT_LOAD Output_segment.
4092 Output_segment::add_output_section_to_load(Layout
* layout
,
4094 elfcpp::Elf_Word seg_flags
)
4096 gold_assert(this->type() == elfcpp::PT_LOAD
);
4097 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
4098 gold_assert(!this->is_max_align_known_
);
4099 gold_assert(os
->is_large_data_section() == this->is_large_data_segment());
4101 this->update_flags_for_output_section(seg_flags
);
4103 // We don't want to change the ordering if we have a linker script
4104 // with a SECTIONS clause.
4105 Output_section_order order
= os
->order();
4106 if (layout
->script_options()->saw_sections_clause())
4107 order
= static_cast<Output_section_order
>(0);
4109 gold_assert(order
!= ORDER_INVALID
);
4111 this->output_lists_
[order
].push_back(os
);
4114 // Add an Output_section to a non-PT_LOAD Output_segment.
4117 Output_segment::add_output_section_to_nonload(Output_section
* os
,
4118 elfcpp::Elf_Word seg_flags
)
4120 gold_assert(this->type() != elfcpp::PT_LOAD
);
4121 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
4122 gold_assert(!this->is_max_align_known_
);
4124 this->update_flags_for_output_section(seg_flags
);
4126 this->output_lists_
[0].push_back(os
);
4129 // Remove an Output_section from this segment. It is an error if it
4133 Output_segment::remove_output_section(Output_section
* os
)
4135 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4137 Output_data_list
* pdl
= &this->output_lists_
[i
];
4138 for (Output_data_list::iterator p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4150 // Add an Output_data (which need not be an Output_section) to the
4151 // start of a segment.
4154 Output_segment::add_initial_output_data(Output_data
* od
)
4156 gold_assert(!this->is_max_align_known_
);
4157 Output_data_list::iterator p
= this->output_lists_
[0].begin();
4158 this->output_lists_
[0].insert(p
, od
);
4161 // Return true if this segment has any sections which hold actual
4162 // data, rather than being a BSS section.
4165 Output_segment::has_any_data_sections() const
4167 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4169 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4170 for (Output_data_list::const_iterator p
= pdl
->begin();
4174 if (!(*p
)->is_section())
4176 if ((*p
)->output_section()->type() != elfcpp::SHT_NOBITS
)
4183 // Return whether the first data section (not counting TLS sections)
4184 // is a relro section.
4187 Output_segment::is_first_section_relro() const
4189 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4191 if (i
== static_cast<int>(ORDER_TLS_BSS
))
4193 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4196 Output_data
* p
= pdl
->front();
4197 return p
->is_section() && p
->output_section()->is_relro();
4203 // Return the maximum alignment of the Output_data in Output_segment.
4206 Output_segment::maximum_alignment()
4208 if (!this->is_max_align_known_
)
4210 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4212 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4213 uint64_t addralign
= Output_segment::maximum_alignment_list(pdl
);
4214 if (addralign
> this->max_align_
)
4215 this->max_align_
= addralign
;
4217 this->is_max_align_known_
= true;
4220 return this->max_align_
;
4223 // Return the maximum alignment of a list of Output_data.
4226 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
4229 for (Output_data_list::const_iterator p
= pdl
->begin();
4233 uint64_t addralign
= (*p
)->addralign();
4234 if (addralign
> ret
)
4240 // Return whether this segment has any dynamic relocs.
4243 Output_segment::has_dynamic_reloc() const
4245 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4246 if (this->has_dynamic_reloc_list(&this->output_lists_
[i
]))
4251 // Return whether this Output_data_list has any dynamic relocs.
4254 Output_segment::has_dynamic_reloc_list(const Output_data_list
* pdl
) const
4256 for (Output_data_list::const_iterator p
= pdl
->begin();
4259 if ((*p
)->has_dynamic_reloc())
4264 // Set the section addresses for an Output_segment. If RESET is true,
4265 // reset the addresses first. ADDR is the address and *POFF is the
4266 // file offset. Set the section indexes starting with *PSHNDX.
4267 // INCREASE_RELRO is the size of the portion of the first non-relro
4268 // section that should be included in the PT_GNU_RELRO segment.
4269 // If this segment has relro sections, and has been aligned for
4270 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4271 // the immediately following segment. Update *HAS_RELRO, *POFF,
4275 Output_segment::set_section_addresses(const Target
* target
,
4276 Layout
* layout
, bool reset
,
4278 unsigned int* increase_relro
,
4281 unsigned int* pshndx
)
4283 gold_assert(this->type_
== elfcpp::PT_LOAD
);
4285 uint64_t last_relro_pad
= 0;
4286 off_t orig_off
= *poff
;
4288 bool in_tls
= false;
4290 // If we have relro sections, we need to pad forward now so that the
4291 // relro sections plus INCREASE_RELRO end on an abi page boundary.
4292 if (parameters
->options().relro()
4293 && this->is_first_section_relro()
4294 && (!this->are_addresses_set_
|| reset
))
4296 uint64_t relro_size
= 0;
4298 uint64_t max_align
= 0;
4299 for (int i
= 0; i
<= static_cast<int>(ORDER_RELRO_LAST
); ++i
)
4301 Output_data_list
* pdl
= &this->output_lists_
[i
];
4302 Output_data_list::iterator p
;
4303 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4305 if (!(*p
)->is_section())
4307 uint64_t align
= (*p
)->addralign();
4308 if (align
> max_align
)
4310 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4314 // Align the first non-TLS section to the alignment
4315 // of the TLS segment.
4319 // Ignore the size of the .tbss section.
4320 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4321 && (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4323 relro_size
= align_address(relro_size
, align
);
4324 if ((*p
)->is_address_valid())
4325 relro_size
+= (*p
)->data_size();
4328 // FIXME: This could be faster.
4329 (*p
)->set_address_and_file_offset(relro_size
,
4331 relro_size
+= (*p
)->data_size();
4332 (*p
)->reset_address_and_file_offset();
4335 if (p
!= pdl
->end())
4338 relro_size
+= *increase_relro
;
4339 // Pad the total relro size to a multiple of the maximum
4340 // section alignment seen.
4341 uint64_t aligned_size
= align_address(relro_size
, max_align
);
4342 // Note the amount of padding added after the last relro section.
4343 last_relro_pad
= aligned_size
- relro_size
;
4346 uint64_t page_align
= parameters
->target().abi_pagesize();
4348 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4349 uint64_t desired_align
= page_align
- (aligned_size
% page_align
);
4350 if (desired_align
< off
% page_align
)
4352 off
+= desired_align
- off
% page_align
;
4353 addr
+= off
- orig_off
;
4358 if (!reset
&& this->are_addresses_set_
)
4360 gold_assert(this->paddr_
== addr
);
4361 addr
= this->vaddr_
;
4365 this->vaddr_
= addr
;
4366 this->paddr_
= addr
;
4367 this->are_addresses_set_
= true;
4372 this->offset_
= orig_off
;
4377 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4379 if (i
== static_cast<int>(ORDER_RELRO_LAST
))
4381 *poff
+= last_relro_pad
;
4382 foff
+= last_relro_pad
;
4383 addr
+= last_relro_pad
;
4384 if (this->output_lists_
[i
].empty())
4386 // If there is nothing in the ORDER_RELRO_LAST list,
4387 // the padding will occur at the end of the relro
4388 // segment, and we need to add it to *INCREASE_RELRO.
4389 *increase_relro
+= last_relro_pad
;
4392 addr
= this->set_section_list_addresses(layout
, reset
,
4393 &this->output_lists_
[i
],
4394 addr
, poff
, &foff
, pshndx
,
4397 // FOFF tracks the last offset used for the file image,
4398 // and *POFF tracks the last offset used for the memory image.
4399 // When not using a linker script, bss sections should all
4400 // be processed in the ORDER_SMALL_BSS and later buckets.
4401 gold_assert(*poff
== foff
4402 || i
== static_cast<int>(ORDER_TLS_BSS
)
4403 || i
>= static_cast<int>(ORDER_SMALL_BSS
)
4404 || layout
->script_options()->saw_sections_clause());
4406 this->filesz_
= foff
- orig_off
;
4412 // If the last section was a TLS section, align upward to the
4413 // alignment of the TLS segment, so that the overall size of the TLS
4414 // segment is aligned.
4417 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
4418 *poff
= align_address(*poff
, segment_align
);
4421 this->memsz_
= *poff
- orig_off
;
4423 // Ignore the file offset adjustments made by the BSS Output_data
4427 // If code segments must contain only code, and this code segment is
4428 // page-aligned in the file, then fill it out to a whole page with
4429 // code fill (the tail of the segment will not be within any section).
4430 // Thus the entire code segment can be mapped from the file as whole
4431 // pages and that mapping will contain only valid instructions.
4432 if (target
->isolate_execinstr() && (this->flags() & elfcpp::PF_X
) != 0)
4434 uint64_t abi_pagesize
= target
->abi_pagesize();
4435 if (orig_off
% abi_pagesize
== 0 && off
% abi_pagesize
!= 0)
4437 size_t fill_size
= abi_pagesize
- (off
% abi_pagesize
);
4439 std::string fill_data
;
4440 if (target
->has_code_fill())
4441 fill_data
= target
->code_fill(fill_size
);
4443 fill_data
.resize(fill_size
); // Zero fill.
4445 Output_data_const
* fill
= new Output_data_const(fill_data
, 0);
4446 fill
->set_address(this->vaddr_
+ this->memsz_
);
4447 fill
->set_file_offset(off
);
4448 layout
->add_relax_output(fill
);
4451 gold_assert(off
% abi_pagesize
== 0);
4453 gold_assert(ret
% abi_pagesize
== 0);
4455 gold_assert((uint64_t) this->filesz_
== this->memsz_
);
4456 this->memsz_
= this->filesz_
+= fill_size
;
4465 // Set the addresses and file offsets in a list of Output_data
4469 Output_segment::set_section_list_addresses(Layout
* layout
, bool reset
,
4470 Output_data_list
* pdl
,
4471 uint64_t addr
, off_t
* poff
,
4473 unsigned int* pshndx
,
4476 off_t startoff
= *poff
;
4477 // For incremental updates, we may allocate non-fixed sections from
4478 // free space in the file. This keeps track of the high-water mark.
4479 off_t maxoff
= startoff
;
4481 off_t off
= startoff
;
4482 off_t foff
= *pfoff
;
4483 for (Output_data_list::iterator p
= pdl
->begin();
4487 bool is_bss
= (*p
)->is_section_type(elfcpp::SHT_NOBITS
);
4488 bool is_tls
= (*p
)->is_section_flag_set(elfcpp::SHF_TLS
);
4491 (*p
)->reset_address_and_file_offset();
4493 // When doing an incremental update or when using a linker script,
4494 // the section will most likely already have an address.
4495 if (!(*p
)->is_address_valid())
4497 uint64_t align
= (*p
)->addralign();
4501 // Give the first TLS section the alignment of the
4502 // entire TLS segment. Otherwise the TLS segment as a
4503 // whole may be misaligned.
4506 Output_segment
* tls_segment
= layout
->tls_segment();
4507 gold_assert(tls_segment
!= NULL
);
4508 uint64_t segment_align
= tls_segment
->maximum_alignment();
4509 gold_assert(segment_align
>= align
);
4510 align
= segment_align
;
4517 // If this is the first section after the TLS segment,
4518 // align it to at least the alignment of the TLS
4519 // segment, so that the size of the overall TLS segment
4523 uint64_t segment_align
=
4524 layout
->tls_segment()->maximum_alignment();
4525 if (segment_align
> align
)
4526 align
= segment_align
;
4532 if (!parameters
->incremental_update())
4534 gold_assert(off
== foff
|| is_bss
);
4535 off
= align_address(off
, align
);
4536 if (is_tls
|| !is_bss
)
4538 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), foff
);
4542 // Incremental update: allocate file space from free list.
4543 (*p
)->pre_finalize_data_size();
4544 off_t current_size
= (*p
)->current_data_size();
4545 off
= layout
->allocate(current_size
, align
, startoff
);
4549 gold_assert((*p
)->output_section() != NULL
);
4550 gold_fallback(_("out of patch space for section %s; "
4551 "relink with --incremental-full"),
4552 (*p
)->output_section()->name());
4554 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), foff
);
4555 if ((*p
)->data_size() > current_size
)
4557 gold_assert((*p
)->output_section() != NULL
);
4558 gold_fallback(_("%s: section changed size; "
4559 "relink with --incremental-full"),
4560 (*p
)->output_section()->name());
4564 else if (parameters
->incremental_update())
4566 // For incremental updates, use the fixed offset for the
4567 // high-water mark computation.
4568 off
= (*p
)->offset();
4573 // The script may have inserted a skip forward, but it
4574 // better not have moved backward.
4575 if ((*p
)->address() >= addr
+ (off
- startoff
))
4577 if (!is_bss
&& off
> foff
)
4578 gold_warning(_("script places BSS section in the middle "
4579 "of a LOAD segment; space will be allocated "
4581 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
4582 if (is_tls
|| !is_bss
)
4587 if (!layout
->script_options()->saw_sections_clause())
4591 Output_section
* os
= (*p
)->output_section();
4593 // Cast to unsigned long long to avoid format warnings.
4594 unsigned long long previous_dot
=
4595 static_cast<unsigned long long>(addr
+ (off
- startoff
));
4596 unsigned long long dot
=
4597 static_cast<unsigned long long>((*p
)->address());
4600 gold_error(_("dot moves backward in linker script "
4601 "from 0x%llx to 0x%llx"), previous_dot
, dot
);
4603 gold_error(_("address of section '%s' moves backward "
4604 "from 0x%llx to 0x%llx"),
4605 os
->name(), previous_dot
, dot
);
4608 (*p
)->set_file_offset(foff
);
4609 (*p
)->finalize_data_size();
4612 if (parameters
->incremental_update())
4613 gold_debug(DEBUG_INCREMENTAL
,
4614 "set_section_list_addresses: %08lx %08lx %s",
4615 static_cast<long>(off
),
4616 static_cast<long>((*p
)->data_size()),
4617 ((*p
)->output_section() != NULL
4618 ? (*p
)->output_section()->name() : "(special)"));
4620 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4621 // section. Such a section does not affect the size of a
4623 if (!is_tls
|| !is_bss
)
4624 off
+= (*p
)->data_size();
4626 // We don't allocate space in the file for SHT_NOBITS sections,
4627 // unless a script has force-placed one in the middle of a segment.
4634 if ((*p
)->is_section())
4636 (*p
)->set_out_shndx(*pshndx
);
4643 return addr
+ (maxoff
- startoff
);
4646 // For a non-PT_LOAD segment, set the offset from the sections, if
4647 // any. Add INCREASE to the file size and the memory size.
4650 Output_segment::set_offset(unsigned int increase
)
4652 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
4654 gold_assert(!this->are_addresses_set_
);
4656 // A non-load section only uses output_lists_[0].
4658 Output_data_list
* pdl
= &this->output_lists_
[0];
4662 gold_assert(increase
== 0);
4665 this->are_addresses_set_
= true;
4667 this->min_p_align_
= 0;
4673 // Find the first and last section by address.
4674 const Output_data
* first
= NULL
;
4675 const Output_data
* last_data
= NULL
;
4676 const Output_data
* last_bss
= NULL
;
4677 for (Output_data_list::const_iterator p
= pdl
->begin();
4682 || (*p
)->address() < first
->address()
4683 || ((*p
)->address() == first
->address()
4684 && (*p
)->data_size() < first
->data_size()))
4686 const Output_data
** plast
;
4687 if ((*p
)->is_section()
4688 && (*p
)->output_section()->type() == elfcpp::SHT_NOBITS
)
4693 || (*p
)->address() > (*plast
)->address()
4694 || ((*p
)->address() == (*plast
)->address()
4695 && (*p
)->data_size() > (*plast
)->data_size()))
4699 this->vaddr_
= first
->address();
4700 this->paddr_
= (first
->has_load_address()
4701 ? first
->load_address()
4703 this->are_addresses_set_
= true;
4704 this->offset_
= first
->offset();
4706 if (last_data
== NULL
)
4709 this->filesz_
= (last_data
->address()
4710 + last_data
->data_size()
4713 const Output_data
* last
= last_bss
!= NULL
? last_bss
: last_data
;
4714 this->memsz_
= (last
->address()
4718 this->filesz_
+= increase
;
4719 this->memsz_
+= increase
;
4721 // If this is a RELRO segment, verify that the segment ends at a
4723 if (this->type_
== elfcpp::PT_GNU_RELRO
)
4725 uint64_t page_align
= parameters
->target().abi_pagesize();
4726 uint64_t segment_end
= this->vaddr_
+ this->memsz_
;
4727 if (parameters
->incremental_update())
4729 // The INCREASE_RELRO calculation is bypassed for an incremental
4730 // update, so we need to adjust the segment size manually here.
4731 segment_end
= align_address(segment_end
, page_align
);
4732 this->memsz_
= segment_end
- this->vaddr_
;
4735 gold_assert(segment_end
== align_address(segment_end
, page_align
));
4738 // If this is a TLS segment, align the memory size. The code in
4739 // set_section_list ensures that the section after the TLS segment
4740 // is aligned to give us room.
4741 if (this->type_
== elfcpp::PT_TLS
)
4743 uint64_t segment_align
= this->maximum_alignment();
4744 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
4745 this->memsz_
= align_address(this->memsz_
, segment_align
);
4749 // Set the TLS offsets of the sections in the PT_TLS segment.
4752 Output_segment::set_tls_offsets()
4754 gold_assert(this->type_
== elfcpp::PT_TLS
);
4756 for (Output_data_list::iterator p
= this->output_lists_
[0].begin();
4757 p
!= this->output_lists_
[0].end();
4759 (*p
)->set_tls_offset(this->vaddr_
);
4762 // Return the first section.
4765 Output_segment::first_section() const
4767 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4769 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4770 for (Output_data_list::const_iterator p
= pdl
->begin();
4774 if ((*p
)->is_section())
4775 return (*p
)->output_section();
4781 // Return the number of Output_sections in an Output_segment.
4784 Output_segment::output_section_count() const
4786 unsigned int ret
= 0;
4787 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4788 ret
+= this->output_section_count_list(&this->output_lists_
[i
]);
4792 // Return the number of Output_sections in an Output_data_list.
4795 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
4797 unsigned int count
= 0;
4798 for (Output_data_list::const_iterator p
= pdl
->begin();
4802 if ((*p
)->is_section())
4808 // Return the section attached to the list segment with the lowest
4809 // load address. This is used when handling a PHDRS clause in a
4813 Output_segment::section_with_lowest_load_address() const
4815 Output_section
* found
= NULL
;
4816 uint64_t found_lma
= 0;
4817 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4818 this->lowest_load_address_in_list(&this->output_lists_
[i
], &found
,
4823 // Look through a list for a section with a lower load address.
4826 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
4827 Output_section
** found
,
4828 uint64_t* found_lma
) const
4830 for (Output_data_list::const_iterator p
= pdl
->begin();
4834 if (!(*p
)->is_section())
4836 Output_section
* os
= static_cast<Output_section
*>(*p
);
4837 uint64_t lma
= (os
->has_load_address()
4838 ? os
->load_address()
4840 if (*found
== NULL
|| lma
< *found_lma
)
4848 // Write the segment data into *OPHDR.
4850 template<int size
, bool big_endian
>
4852 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
4854 ophdr
->put_p_type(this->type_
);
4855 ophdr
->put_p_offset(this->offset_
);
4856 ophdr
->put_p_vaddr(this->vaddr_
);
4857 ophdr
->put_p_paddr(this->paddr_
);
4858 ophdr
->put_p_filesz(this->filesz_
);
4859 ophdr
->put_p_memsz(this->memsz_
);
4860 ophdr
->put_p_flags(this->flags_
);
4861 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
4864 // Write the section headers into V.
4866 template<int size
, bool big_endian
>
4868 Output_segment::write_section_headers(const Layout
* layout
,
4869 const Stringpool
* secnamepool
,
4871 unsigned int* pshndx
) const
4873 // Every section that is attached to a segment must be attached to a
4874 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4876 if (this->type_
!= elfcpp::PT_LOAD
)
4879 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4881 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4882 v
= this->write_section_headers_list
<size
, big_endian
>(layout
,
4891 template<int size
, bool big_endian
>
4893 Output_segment::write_section_headers_list(const Layout
* layout
,
4894 const Stringpool
* secnamepool
,
4895 const Output_data_list
* pdl
,
4897 unsigned int* pshndx
) const
4899 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
4900 for (Output_data_list::const_iterator p
= pdl
->begin();
4904 if ((*p
)->is_section())
4906 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
4907 gold_assert(*pshndx
== ps
->out_shndx());
4908 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
4909 ps
->write_header(layout
, secnamepool
, &oshdr
);
4917 // Print the output sections to the map file.
4920 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
4922 if (this->type() != elfcpp::PT_LOAD
)
4924 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4925 this->print_section_list_to_mapfile(mapfile
, &this->output_lists_
[i
]);
4928 // Print an output section list to the map file.
4931 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
4932 const Output_data_list
* pdl
) const
4934 for (Output_data_list::const_iterator p
= pdl
->begin();
4937 (*p
)->print_to_mapfile(mapfile
);
4940 // Output_file methods.
4942 Output_file::Output_file(const char* name
)
4947 map_is_anonymous_(false),
4948 map_is_allocated_(false),
4949 is_temporary_(false)
4953 // Try to open an existing file. Returns false if the file doesn't
4954 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4955 // NULL, open that file as the base for incremental linking, and
4956 // copy its contents to the new output file. This routine can
4957 // be called for incremental updates, in which case WRITABLE should
4958 // be true, or by the incremental-dump utility, in which case
4959 // WRITABLE should be false.
4962 Output_file::open_base_file(const char* base_name
, bool writable
)
4964 // The name "-" means "stdout".
4965 if (strcmp(this->name_
, "-") == 0)
4968 bool use_base_file
= base_name
!= NULL
;
4970 base_name
= this->name_
;
4971 else if (strcmp(base_name
, this->name_
) == 0)
4972 gold_fatal(_("%s: incremental base and output file name are the same"),
4975 // Don't bother opening files with a size of zero.
4977 if (::stat(base_name
, &s
) != 0)
4979 gold_info(_("%s: stat: %s"), base_name
, strerror(errno
));
4984 gold_info(_("%s: incremental base file is empty"), base_name
);
4988 // If we're using a base file, we want to open it read-only.
4992 int oflags
= writable
? O_RDWR
: O_RDONLY
;
4993 int o
= open_descriptor(-1, base_name
, oflags
, 0);
4996 gold_info(_("%s: open: %s"), base_name
, strerror(errno
));
5000 // If the base file and the output file are different, open a
5001 // new output file and read the contents from the base file into
5002 // the newly-mapped region.
5005 this->open(s
.st_size
);
5006 ssize_t bytes_to_read
= s
.st_size
;
5007 unsigned char* p
= this->base_
;
5008 while (bytes_to_read
> 0)
5010 ssize_t len
= ::read(o
, p
, bytes_to_read
);
5013 gold_info(_("%s: read failed: %s"), base_name
, strerror(errno
));
5018 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
5020 static_cast<long long>(s
.st_size
- bytes_to_read
),
5021 static_cast<long long>(s
.st_size
));
5025 bytes_to_read
-= len
;
5032 this->file_size_
= s
.st_size
;
5034 if (!this->map_no_anonymous(writable
))
5036 release_descriptor(o
, true);
5038 this->file_size_
= 0;
5045 // Open the output file.
5048 Output_file::open(off_t file_size
)
5050 this->file_size_
= file_size
;
5052 // Unlink the file first; otherwise the open() may fail if the file
5053 // is busy (e.g. it's an executable that's currently being executed).
5055 // However, the linker may be part of a system where a zero-length
5056 // file is created for it to write to, with tight permissions (gcc
5057 // 2.95 did something like this). Unlinking the file would work
5058 // around those permission controls, so we only unlink if the file
5059 // has a non-zero size. We also unlink only regular files to avoid
5060 // trouble with directories/etc.
5062 // If we fail, continue; this command is merely a best-effort attempt
5063 // to improve the odds for open().
5065 // We let the name "-" mean "stdout"
5066 if (!this->is_temporary_
)
5068 if (strcmp(this->name_
, "-") == 0)
5069 this->o_
= STDOUT_FILENO
;
5073 if (::stat(this->name_
, &s
) == 0
5074 && (S_ISREG (s
.st_mode
) || S_ISLNK (s
.st_mode
)))
5077 ::unlink(this->name_
);
5078 else if (!parameters
->options().relocatable())
5080 // If we don't unlink the existing file, add execute
5081 // permission where read permissions already exist
5082 // and where the umask permits.
5083 int mask
= ::umask(0);
5085 s
.st_mode
|= (s
.st_mode
& 0444) >> 2;
5086 ::chmod(this->name_
, s
.st_mode
& ~mask
);
5090 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
5091 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
5094 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
5102 // Resize the output file.
5105 Output_file::resize(off_t file_size
)
5107 // If the mmap is mapping an anonymous memory buffer, this is easy:
5108 // just mremap to the new size. If it's mapping to a file, we want
5109 // to unmap to flush to the file, then remap after growing the file.
5110 if (this->map_is_anonymous_
)
5113 if (!this->map_is_allocated_
)
5115 base
= ::mremap(this->base_
, this->file_size_
, file_size
,
5117 if (base
== MAP_FAILED
)
5118 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
5122 base
= realloc(this->base_
, file_size
);
5125 if (file_size
> this->file_size_
)
5126 memset(static_cast<char*>(base
) + this->file_size_
, 0,
5127 file_size
- this->file_size_
);
5129 this->base_
= static_cast<unsigned char*>(base
);
5130 this->file_size_
= file_size
;
5135 this->file_size_
= file_size
;
5136 if (!this->map_no_anonymous(true))
5137 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
5141 // Map an anonymous block of memory which will later be written to the
5142 // file. Return whether the map succeeded.
5145 Output_file::map_anonymous()
5147 void* base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
5148 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
5149 if (base
== MAP_FAILED
)
5151 base
= malloc(this->file_size_
);
5154 memset(base
, 0, this->file_size_
);
5155 this->map_is_allocated_
= true;
5157 this->base_
= static_cast<unsigned char*>(base
);
5158 this->map_is_anonymous_
= true;
5162 // Map the file into memory. Return whether the mapping succeeded.
5163 // If WRITABLE is true, map with write access.
5166 Output_file::map_no_anonymous(bool writable
)
5168 const int o
= this->o_
;
5170 // If the output file is not a regular file, don't try to mmap it;
5171 // instead, we'll mmap a block of memory (an anonymous buffer), and
5172 // then later write the buffer to the file.
5174 struct stat statbuf
;
5175 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
5176 || ::fstat(o
, &statbuf
) != 0
5177 || !S_ISREG(statbuf
.st_mode
)
5178 || this->is_temporary_
)
5181 // Ensure that we have disk space available for the file. If we
5182 // don't do this, it is possible that we will call munmap, close,
5183 // and exit with dirty buffers still in the cache with no assigned
5184 // disk blocks. If the disk is out of space at that point, the
5185 // output file will wind up incomplete, but we will have already
5186 // exited. The alternative to fallocate would be to use fdatasync,
5187 // but that would be a more significant performance hit.
5190 int err
= gold_fallocate(o
, 0, this->file_size_
);
5192 gold_fatal(_("%s: %s"), this->name_
, strerror(err
));
5195 // Map the file into memory.
5196 int prot
= PROT_READ
;
5199 base
= ::mmap(NULL
, this->file_size_
, prot
, MAP_SHARED
, o
, 0);
5201 // The mmap call might fail because of file system issues: the file
5202 // system might not support mmap at all, or it might not support
5203 // mmap with PROT_WRITE.
5204 if (base
== MAP_FAILED
)
5207 this->map_is_anonymous_
= false;
5208 this->base_
= static_cast<unsigned char*>(base
);
5212 // Map the file into memory.
5217 if (parameters
->options().mmap_output_file()
5218 && this->map_no_anonymous(true))
5221 // The mmap call might fail because of file system issues: the file
5222 // system might not support mmap at all, or it might not support
5223 // mmap with PROT_WRITE. I'm not sure which errno values we will
5224 // see in all cases, so if the mmap fails for any reason and we
5225 // don't care about file contents, try for an anonymous map.
5226 if (this->map_anonymous())
5229 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5230 this->name_
, static_cast<unsigned long>(this->file_size_
),
5234 // Unmap the file from memory.
5237 Output_file::unmap()
5239 if (this->map_is_anonymous_
)
5241 // We've already written out the data, so there is no reason to
5242 // waste time unmapping or freeing the memory.
5246 if (::munmap(this->base_
, this->file_size_
) < 0)
5247 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
5252 // Close the output file.
5255 Output_file::close()
5257 // If the map isn't file-backed, we need to write it now.
5258 if (this->map_is_anonymous_
&& !this->is_temporary_
)
5260 size_t bytes_to_write
= this->file_size_
;
5262 while (bytes_to_write
> 0)
5264 ssize_t bytes_written
= ::write(this->o_
, this->base_
+ offset
,
5266 if (bytes_written
== 0)
5267 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
5268 else if (bytes_written
< 0)
5269 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
5272 bytes_to_write
-= bytes_written
;
5273 offset
+= bytes_written
;
5279 // We don't close stdout or stderr
5280 if (this->o_
!= STDOUT_FILENO
5281 && this->o_
!= STDERR_FILENO
5282 && !this->is_temporary_
)
5283 if (::close(this->o_
) < 0)
5284 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
5288 // Instantiate the templates we need. We could use the configure
5289 // script to restrict this to only the ones for implemented targets.
5291 #ifdef HAVE_TARGET_32_LITTLE
5294 Output_section::add_input_section
<32, false>(
5296 Sized_relobj_file
<32, false>* object
,
5298 const char* secname
,
5299 const elfcpp::Shdr
<32, false>& shdr
,
5300 unsigned int reloc_shndx
,
5301 bool have_sections_script
);
5304 #ifdef HAVE_TARGET_32_BIG
5307 Output_section::add_input_section
<32, true>(
5309 Sized_relobj_file
<32, true>* object
,
5311 const char* secname
,
5312 const elfcpp::Shdr
<32, true>& shdr
,
5313 unsigned int reloc_shndx
,
5314 bool have_sections_script
);
5317 #ifdef HAVE_TARGET_64_LITTLE
5320 Output_section::add_input_section
<64, false>(
5322 Sized_relobj_file
<64, false>* object
,
5324 const char* secname
,
5325 const elfcpp::Shdr
<64, false>& shdr
,
5326 unsigned int reloc_shndx
,
5327 bool have_sections_script
);
5330 #ifdef HAVE_TARGET_64_BIG
5333 Output_section::add_input_section
<64, true>(
5335 Sized_relobj_file
<64, true>* object
,
5337 const char* secname
,
5338 const elfcpp::Shdr
<64, true>& shdr
,
5339 unsigned int reloc_shndx
,
5340 bool have_sections_script
);
5343 #ifdef HAVE_TARGET_32_LITTLE
5345 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5348 #ifdef HAVE_TARGET_32_BIG
5350 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5353 #ifdef HAVE_TARGET_64_LITTLE
5355 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5358 #ifdef HAVE_TARGET_64_BIG
5360 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5363 #ifdef HAVE_TARGET_32_LITTLE
5365 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5368 #ifdef HAVE_TARGET_32_BIG
5370 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5373 #ifdef HAVE_TARGET_64_LITTLE
5375 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5378 #ifdef HAVE_TARGET_64_BIG
5380 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5383 #ifdef HAVE_TARGET_32_LITTLE
5385 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5388 #ifdef HAVE_TARGET_32_BIG
5390 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5393 #ifdef HAVE_TARGET_64_LITTLE
5395 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5398 #ifdef HAVE_TARGET_64_BIG
5400 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5403 #ifdef HAVE_TARGET_32_LITTLE
5405 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5408 #ifdef HAVE_TARGET_32_BIG
5410 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5413 #ifdef HAVE_TARGET_64_LITTLE
5415 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5418 #ifdef HAVE_TARGET_64_BIG
5420 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5423 #ifdef HAVE_TARGET_32_LITTLE
5425 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5428 #ifdef HAVE_TARGET_32_BIG
5430 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5433 #ifdef HAVE_TARGET_64_LITTLE
5435 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5438 #ifdef HAVE_TARGET_64_BIG
5440 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5443 #ifdef HAVE_TARGET_32_LITTLE
5445 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5448 #ifdef HAVE_TARGET_32_BIG
5450 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5453 #ifdef HAVE_TARGET_64_LITTLE
5455 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5458 #ifdef HAVE_TARGET_64_BIG
5460 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5463 #ifdef HAVE_TARGET_32_LITTLE
5465 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5468 #ifdef HAVE_TARGET_32_BIG
5470 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5473 #ifdef HAVE_TARGET_64_LITTLE
5475 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5478 #ifdef HAVE_TARGET_64_BIG
5480 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5483 #ifdef HAVE_TARGET_32_LITTLE
5485 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5488 #ifdef HAVE_TARGET_32_BIG
5490 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5493 #ifdef HAVE_TARGET_64_LITTLE
5495 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5498 #ifdef HAVE_TARGET_64_BIG
5500 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5503 #ifdef HAVE_TARGET_32_LITTLE
5505 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
5508 #ifdef HAVE_TARGET_32_BIG
5510 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
5513 #ifdef HAVE_TARGET_64_LITTLE
5515 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
5518 #ifdef HAVE_TARGET_64_BIG
5520 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
5523 #ifdef HAVE_TARGET_32_LITTLE
5525 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
5528 #ifdef HAVE_TARGET_32_BIG
5530 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
5533 #ifdef HAVE_TARGET_64_LITTLE
5535 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
5538 #ifdef HAVE_TARGET_64_BIG
5540 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
5543 #ifdef HAVE_TARGET_32_LITTLE
5545 class Output_data_group
<32, false>;
5548 #ifdef HAVE_TARGET_32_BIG
5550 class Output_data_group
<32, true>;
5553 #ifdef HAVE_TARGET_64_LITTLE
5555 class Output_data_group
<64, false>;
5558 #ifdef HAVE_TARGET_64_BIG
5560 class Output_data_group
<64, true>;
5564 class Output_data_got
<32, false>;
5567 class Output_data_got
<32, true>;
5570 class Output_data_got
<64, false>;
5573 class Output_data_got
<64, true>;
5575 } // End namespace gold.