1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
33 #ifdef HAVE_SYS_MMAN_H
37 #include "libiberty.h"
40 #include "parameters.h"
45 #include "descriptors.h"
49 // For systems without mmap support.
51 # define mmap gold_mmap
52 # define munmap gold_munmap
53 # define mremap gold_mremap
55 # define MAP_FAILED (reinterpret_cast<void*>(-1))
64 # define MAP_PRIVATE 0
66 # ifndef MAP_ANONYMOUS
67 # define MAP_ANONYMOUS 0
74 # define ENOSYS EINVAL
78 gold_mmap(void *, size_t, int, int, int, off_t
)
85 gold_munmap(void *, size_t)
92 gold_mremap(void *, size_t, size_t, int)
100 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
101 # define mremap gold_mremap
102 extern "C" void *gold_mremap(void *, size_t, size_t, int);
105 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
106 #ifndef MAP_ANONYMOUS
107 # define MAP_ANONYMOUS MAP_ANON
110 #ifndef MREMAP_MAYMOVE
111 # define MREMAP_MAYMOVE 1
114 #ifndef HAVE_POSIX_FALLOCATE
115 // A dummy, non general, version of posix_fallocate. Here we just set
116 // the file size and hope that there is enough disk space. FIXME: We
117 // could allocate disk space by walking block by block and writing a
118 // zero byte into each block.
120 posix_fallocate(int o
, off_t offset
, off_t len
)
122 if (ftruncate(o
, offset
+ len
) < 0)
126 #endif // !defined(HAVE_POSIX_FALLOCATE)
128 // Mingw does not have S_ISLNK.
130 # define S_ISLNK(mode) 0
136 // Output_data variables.
138 bool Output_data::allocated_sizes_are_fixed
;
140 // Output_data methods.
142 Output_data::~Output_data()
146 // Return the default alignment for the target size.
149 Output_data::default_alignment()
151 return Output_data::default_alignment_for_size(
152 parameters
->target().get_size());
155 // Return the default alignment for a size--32 or 64.
158 Output_data::default_alignment_for_size(int size
)
168 // Output_section_header methods. This currently assumes that the
169 // segment and section lists are complete at construction time.
171 Output_section_headers::Output_section_headers(
172 const Layout
* layout
,
173 const Layout::Segment_list
* segment_list
,
174 const Layout::Section_list
* section_list
,
175 const Layout::Section_list
* unattached_section_list
,
176 const Stringpool
* secnamepool
,
177 const Output_section
* shstrtab_section
)
179 segment_list_(segment_list
),
180 section_list_(section_list
),
181 unattached_section_list_(unattached_section_list
),
182 secnamepool_(secnamepool
),
183 shstrtab_section_(shstrtab_section
)
187 // Compute the current data size.
190 Output_section_headers::do_size() const
192 // Count all the sections. Start with 1 for the null section.
194 if (!parameters
->options().relocatable())
196 for (Layout::Segment_list::const_iterator p
=
197 this->segment_list_
->begin();
198 p
!= this->segment_list_
->end();
200 if ((*p
)->type() == elfcpp::PT_LOAD
)
201 count
+= (*p
)->output_section_count();
205 for (Layout::Section_list::const_iterator p
=
206 this->section_list_
->begin();
207 p
!= this->section_list_
->end();
209 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
212 count
+= this->unattached_section_list_
->size();
214 const int size
= parameters
->target().get_size();
217 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
219 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
223 return count
* shdr_size
;
226 // Write out the section headers.
229 Output_section_headers::do_write(Output_file
* of
)
231 switch (parameters
->size_and_endianness())
233 #ifdef HAVE_TARGET_32_LITTLE
234 case Parameters::TARGET_32_LITTLE
:
235 this->do_sized_write
<32, false>(of
);
238 #ifdef HAVE_TARGET_32_BIG
239 case Parameters::TARGET_32_BIG
:
240 this->do_sized_write
<32, true>(of
);
243 #ifdef HAVE_TARGET_64_LITTLE
244 case Parameters::TARGET_64_LITTLE
:
245 this->do_sized_write
<64, false>(of
);
248 #ifdef HAVE_TARGET_64_BIG
249 case Parameters::TARGET_64_BIG
:
250 this->do_sized_write
<64, true>(of
);
258 template<int size
, bool big_endian
>
260 Output_section_headers::do_sized_write(Output_file
* of
)
262 off_t all_shdrs_size
= this->data_size();
263 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
265 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
266 unsigned char* v
= view
;
269 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
270 oshdr
.put_sh_name(0);
271 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
272 oshdr
.put_sh_flags(0);
273 oshdr
.put_sh_addr(0);
274 oshdr
.put_sh_offset(0);
276 size_t section_count
= (this->data_size()
277 / elfcpp::Elf_sizes
<size
>::shdr_size
);
278 if (section_count
< elfcpp::SHN_LORESERVE
)
279 oshdr
.put_sh_size(0);
281 oshdr
.put_sh_size(section_count
);
283 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
284 if (shstrndx
< elfcpp::SHN_LORESERVE
)
285 oshdr
.put_sh_link(0);
287 oshdr
.put_sh_link(shstrndx
);
289 size_t segment_count
= this->segment_list_
->size();
290 oshdr
.put_sh_info(segment_count
>= elfcpp::PN_XNUM
? segment_count
: 0);
292 oshdr
.put_sh_addralign(0);
293 oshdr
.put_sh_entsize(0);
298 unsigned int shndx
= 1;
299 if (!parameters
->options().relocatable())
301 for (Layout::Segment_list::const_iterator p
=
302 this->segment_list_
->begin();
303 p
!= this->segment_list_
->end();
305 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
312 for (Layout::Section_list::const_iterator p
=
313 this->section_list_
->begin();
314 p
!= this->section_list_
->end();
317 // We do unallocated sections below, except that group
318 // sections have to come first.
319 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
320 && (*p
)->type() != elfcpp::SHT_GROUP
)
322 gold_assert(shndx
== (*p
)->out_shndx());
323 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
324 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
330 for (Layout::Section_list::const_iterator p
=
331 this->unattached_section_list_
->begin();
332 p
!= this->unattached_section_list_
->end();
335 // For a relocatable link, we did unallocated group sections
336 // above, since they have to come first.
337 if ((*p
)->type() == elfcpp::SHT_GROUP
338 && parameters
->options().relocatable())
340 gold_assert(shndx
== (*p
)->out_shndx());
341 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
342 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
347 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
350 // Output_segment_header methods.
352 Output_segment_headers::Output_segment_headers(
353 const Layout::Segment_list
& segment_list
)
354 : segment_list_(segment_list
)
356 this->set_current_data_size_for_child(this->do_size());
360 Output_segment_headers::do_write(Output_file
* of
)
362 switch (parameters
->size_and_endianness())
364 #ifdef HAVE_TARGET_32_LITTLE
365 case Parameters::TARGET_32_LITTLE
:
366 this->do_sized_write
<32, false>(of
);
369 #ifdef HAVE_TARGET_32_BIG
370 case Parameters::TARGET_32_BIG
:
371 this->do_sized_write
<32, true>(of
);
374 #ifdef HAVE_TARGET_64_LITTLE
375 case Parameters::TARGET_64_LITTLE
:
376 this->do_sized_write
<64, false>(of
);
379 #ifdef HAVE_TARGET_64_BIG
380 case Parameters::TARGET_64_BIG
:
381 this->do_sized_write
<64, true>(of
);
389 template<int size
, bool big_endian
>
391 Output_segment_headers::do_sized_write(Output_file
* of
)
393 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
394 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
395 gold_assert(all_phdrs_size
== this->data_size());
396 unsigned char* view
= of
->get_output_view(this->offset(),
398 unsigned char* v
= view
;
399 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
400 p
!= this->segment_list_
.end();
403 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
404 (*p
)->write_header(&ophdr
);
408 gold_assert(v
- view
== all_phdrs_size
);
410 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
414 Output_segment_headers::do_size() const
416 const int size
= parameters
->target().get_size();
419 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
421 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
425 return this->segment_list_
.size() * phdr_size
;
428 // Output_file_header methods.
430 Output_file_header::Output_file_header(const Target
* target
,
431 const Symbol_table
* symtab
,
432 const Output_segment_headers
* osh
)
435 segment_header_(osh
),
436 section_header_(NULL
),
439 this->set_data_size(this->do_size());
442 // Set the section table information for a file header.
445 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
446 const Output_section
* shstrtab
)
448 this->section_header_
= shdrs
;
449 this->shstrtab_
= shstrtab
;
452 // Write out the file header.
455 Output_file_header::do_write(Output_file
* of
)
457 gold_assert(this->offset() == 0);
459 switch (parameters
->size_and_endianness())
461 #ifdef HAVE_TARGET_32_LITTLE
462 case Parameters::TARGET_32_LITTLE
:
463 this->do_sized_write
<32, false>(of
);
466 #ifdef HAVE_TARGET_32_BIG
467 case Parameters::TARGET_32_BIG
:
468 this->do_sized_write
<32, true>(of
);
471 #ifdef HAVE_TARGET_64_LITTLE
472 case Parameters::TARGET_64_LITTLE
:
473 this->do_sized_write
<64, false>(of
);
476 #ifdef HAVE_TARGET_64_BIG
477 case Parameters::TARGET_64_BIG
:
478 this->do_sized_write
<64, true>(of
);
486 // Write out the file header with appropriate size and endianness.
488 template<int size
, bool big_endian
>
490 Output_file_header::do_sized_write(Output_file
* of
)
492 gold_assert(this->offset() == 0);
494 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
495 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
496 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
498 unsigned char e_ident
[elfcpp::EI_NIDENT
];
499 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
500 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
501 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
502 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
503 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
505 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
507 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
510 e_ident
[elfcpp::EI_DATA
] = (big_endian
511 ? elfcpp::ELFDATA2MSB
512 : elfcpp::ELFDATA2LSB
);
513 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
514 oehdr
.put_e_ident(e_ident
);
517 if (parameters
->options().relocatable())
518 e_type
= elfcpp::ET_REL
;
519 else if (parameters
->options().output_is_position_independent())
520 e_type
= elfcpp::ET_DYN
;
522 e_type
= elfcpp::ET_EXEC
;
523 oehdr
.put_e_type(e_type
);
525 oehdr
.put_e_machine(this->target_
->machine_code());
526 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
528 oehdr
.put_e_entry(this->entry
<size
>());
530 if (this->segment_header_
== NULL
)
531 oehdr
.put_e_phoff(0);
533 oehdr
.put_e_phoff(this->segment_header_
->offset());
535 oehdr
.put_e_shoff(this->section_header_
->offset());
536 oehdr
.put_e_flags(this->target_
->processor_specific_flags());
537 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
539 if (this->segment_header_
== NULL
)
541 oehdr
.put_e_phentsize(0);
542 oehdr
.put_e_phnum(0);
546 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
547 size_t phnum
= (this->segment_header_
->data_size()
548 / elfcpp::Elf_sizes
<size
>::phdr_size
);
549 if (phnum
> elfcpp::PN_XNUM
)
550 phnum
= elfcpp::PN_XNUM
;
551 oehdr
.put_e_phnum(phnum
);
554 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
555 size_t section_count
= (this->section_header_
->data_size()
556 / elfcpp::Elf_sizes
<size
>::shdr_size
);
558 if (section_count
< elfcpp::SHN_LORESERVE
)
559 oehdr
.put_e_shnum(this->section_header_
->data_size()
560 / elfcpp::Elf_sizes
<size
>::shdr_size
);
562 oehdr
.put_e_shnum(0);
564 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
565 if (shstrndx
< elfcpp::SHN_LORESERVE
)
566 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
568 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
570 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
571 // the e_ident field.
572 parameters
->target().adjust_elf_header(view
, ehdr_size
);
574 of
->write_output_view(0, ehdr_size
, view
);
577 // Return the value to use for the entry address.
580 typename
elfcpp::Elf_types
<size
>::Elf_Addr
581 Output_file_header::entry()
583 const bool should_issue_warning
= (parameters
->options().entry() != NULL
584 && !parameters
->options().relocatable()
585 && !parameters
->options().shared());
586 const char* entry
= parameters
->entry();
587 Symbol
* sym
= this->symtab_
->lookup(entry
);
589 typename Sized_symbol
<size
>::Value_type v
;
592 Sized_symbol
<size
>* ssym
;
593 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
594 if (!ssym
->is_defined() && should_issue_warning
)
595 gold_warning("entry symbol '%s' exists but is not defined", entry
);
600 // We couldn't find the entry symbol. See if we can parse it as
601 // a number. This supports, e.g., -e 0x1000.
603 v
= strtoull(entry
, &endptr
, 0);
606 if (should_issue_warning
)
607 gold_warning("cannot find entry symbol '%s'", entry
);
615 // Compute the current data size.
618 Output_file_header::do_size() const
620 const int size
= parameters
->target().get_size();
622 return elfcpp::Elf_sizes
<32>::ehdr_size
;
624 return elfcpp::Elf_sizes
<64>::ehdr_size
;
629 // Output_data_const methods.
632 Output_data_const::do_write(Output_file
* of
)
634 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
637 // Output_data_const_buffer methods.
640 Output_data_const_buffer::do_write(Output_file
* of
)
642 of
->write(this->offset(), this->p_
, this->data_size());
645 // Output_section_data methods.
647 // Record the output section, and set the entry size and such.
650 Output_section_data::set_output_section(Output_section
* os
)
652 gold_assert(this->output_section_
== NULL
);
653 this->output_section_
= os
;
654 this->do_adjust_output_section(os
);
657 // Return the section index of the output section.
660 Output_section_data::do_out_shndx() const
662 gold_assert(this->output_section_
!= NULL
);
663 return this->output_section_
->out_shndx();
666 // Set the alignment, which means we may need to update the alignment
667 // of the output section.
670 Output_section_data::set_addralign(uint64_t addralign
)
672 this->addralign_
= addralign
;
673 if (this->output_section_
!= NULL
674 && this->output_section_
->addralign() < addralign
)
675 this->output_section_
->set_addralign(addralign
);
678 // Output_data_strtab methods.
680 // Set the final data size.
683 Output_data_strtab::set_final_data_size()
685 this->strtab_
->set_string_offsets();
686 this->set_data_size(this->strtab_
->get_strtab_size());
689 // Write out a string table.
692 Output_data_strtab::do_write(Output_file
* of
)
694 this->strtab_
->write(of
, this->offset());
697 // Output_reloc methods.
699 // A reloc against a global symbol.
701 template<bool dynamic
, int size
, bool big_endian
>
702 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
710 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
711 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
712 is_section_symbol_(false), use_plt_offset_(use_plt_offset
), shndx_(INVALID_CODE
)
714 // this->type_ is a bitfield; make sure TYPE fits.
715 gold_assert(this->type_
== type
);
716 this->u1_
.gsym
= gsym
;
719 this->set_needs_dynsym_index();
722 template<bool dynamic
, int size
, bool big_endian
>
723 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
726 Sized_relobj
<size
, big_endian
>* relobj
,
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_(shndx
)
736 gold_assert(shndx
!= INVALID_CODE
);
737 // this->type_ is a bitfield; make sure TYPE fits.
738 gold_assert(this->type_
== type
);
739 this->u1_
.gsym
= gsym
;
740 this->u2_
.relobj
= relobj
;
742 this->set_needs_dynsym_index();
745 // A reloc against a local symbol.
747 template<bool dynamic
, int size
, bool big_endian
>
748 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
749 Sized_relobj
<size
, big_endian
>* relobj
,
750 unsigned int local_sym_index
,
756 bool is_section_symbol
,
758 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
759 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
760 is_section_symbol_(is_section_symbol
), use_plt_offset_(use_plt_offset
),
763 gold_assert(local_sym_index
!= GSYM_CODE
764 && local_sym_index
!= INVALID_CODE
);
765 // this->type_ is a bitfield; make sure TYPE fits.
766 gold_assert(this->type_
== type
);
767 this->u1_
.relobj
= relobj
;
770 this->set_needs_dynsym_index();
773 template<bool dynamic
, int size
, bool big_endian
>
774 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
775 Sized_relobj
<size
, big_endian
>* relobj
,
776 unsigned int local_sym_index
,
782 bool is_section_symbol
,
784 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
785 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
786 is_section_symbol_(is_section_symbol
), use_plt_offset_(use_plt_offset
),
789 gold_assert(local_sym_index
!= GSYM_CODE
790 && local_sym_index
!= INVALID_CODE
);
791 gold_assert(shndx
!= INVALID_CODE
);
792 // this->type_ is a bitfield; make sure TYPE fits.
793 gold_assert(this->type_
== type
);
794 this->u1_
.relobj
= relobj
;
795 this->u2_
.relobj
= relobj
;
797 this->set_needs_dynsym_index();
800 // A reloc against the STT_SECTION symbol of an output section.
802 template<bool dynamic
, int size
, bool big_endian
>
803 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
808 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
809 is_relative_(false), is_symbolless_(false),
810 is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE
)
812 // this->type_ is a bitfield; make sure TYPE fits.
813 gold_assert(this->type_
== type
);
817 this->set_needs_dynsym_index();
819 os
->set_needs_symtab_index();
822 template<bool dynamic
, int size
, bool big_endian
>
823 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
826 Sized_relobj
<size
, big_endian
>* relobj
,
829 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
830 is_relative_(false), is_symbolless_(false),
831 is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx
)
833 gold_assert(shndx
!= INVALID_CODE
);
834 // this->type_ is a bitfield; make sure TYPE fits.
835 gold_assert(this->type_
== type
);
837 this->u2_
.relobj
= relobj
;
839 this->set_needs_dynsym_index();
841 os
->set_needs_symtab_index();
844 // An absolute relocation.
846 template<bool dynamic
, int size
, bool big_endian
>
847 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
851 : address_(address
), local_sym_index_(0), type_(type
),
852 is_relative_(false), is_symbolless_(false),
853 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE
)
855 // this->type_ is a bitfield; make sure TYPE fits.
856 gold_assert(this->type_
== type
);
857 this->u1_
.relobj
= NULL
;
861 template<bool dynamic
, int size
, bool big_endian
>
862 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
864 Sized_relobj
<size
, big_endian
>* relobj
,
867 : address_(address
), local_sym_index_(0), type_(type
),
868 is_relative_(false), is_symbolless_(false),
869 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx
)
871 gold_assert(shndx
!= INVALID_CODE
);
872 // this->type_ is a bitfield; make sure TYPE fits.
873 gold_assert(this->type_
== type
);
874 this->u1_
.relobj
= NULL
;
875 this->u2_
.relobj
= relobj
;
878 // A target specific relocation.
880 template<bool dynamic
, int size
, bool big_endian
>
881 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
886 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
887 is_relative_(false), is_symbolless_(false),
888 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE
)
890 // this->type_ is a bitfield; make sure TYPE fits.
891 gold_assert(this->type_
== type
);
896 template<bool dynamic
, int size
, bool big_endian
>
897 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
900 Sized_relobj
<size
, big_endian
>* relobj
,
903 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
904 is_relative_(false), is_symbolless_(false),
905 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx
)
907 gold_assert(shndx
!= INVALID_CODE
);
908 // this->type_ is a bitfield; make sure TYPE fits.
909 gold_assert(this->type_
== type
);
911 this->u2_
.relobj
= relobj
;
914 // Record that we need a dynamic symbol index for this relocation.
916 template<bool dynamic
, int size
, bool big_endian
>
918 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
919 set_needs_dynsym_index()
921 if (this->is_symbolless_
)
923 switch (this->local_sym_index_
)
929 this->u1_
.gsym
->set_needs_dynsym_entry();
933 this->u1_
.os
->set_needs_dynsym_index();
937 // The target must take care of this if necessary.
945 const unsigned int lsi
= this->local_sym_index_
;
946 Sized_relobj_file
<size
, big_endian
>* relobj
=
947 this->u1_
.relobj
->sized_relobj();
948 gold_assert(relobj
!= NULL
);
949 if (!this->is_section_symbol_
)
950 relobj
->set_needs_output_dynsym_entry(lsi
);
952 relobj
->output_section(lsi
)->set_needs_dynsym_index();
958 // Get the symbol index of a relocation.
960 template<bool dynamic
, int size
, bool big_endian
>
962 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
966 if (this->is_symbolless_
)
968 switch (this->local_sym_index_
)
974 if (this->u1_
.gsym
== NULL
)
977 index
= this->u1_
.gsym
->dynsym_index();
979 index
= this->u1_
.gsym
->symtab_index();
984 index
= this->u1_
.os
->dynsym_index();
986 index
= this->u1_
.os
->symtab_index();
990 index
= parameters
->target().reloc_symbol_index(this->u1_
.arg
,
995 // Relocations without symbols use a symbol index of 0.
1001 const unsigned int lsi
= this->local_sym_index_
;
1002 Sized_relobj_file
<size
, big_endian
>* relobj
=
1003 this->u1_
.relobj
->sized_relobj();
1004 gold_assert(relobj
!= NULL
);
1005 if (!this->is_section_symbol_
)
1008 index
= relobj
->dynsym_index(lsi
);
1010 index
= relobj
->symtab_index(lsi
);
1014 Output_section
* os
= relobj
->output_section(lsi
);
1015 gold_assert(os
!= NULL
);
1017 index
= os
->dynsym_index();
1019 index
= os
->symtab_index();
1024 gold_assert(index
!= -1U);
1028 // For a local section symbol, get the address of the offset ADDEND
1029 // within the input section.
1031 template<bool dynamic
, int size
, bool big_endian
>
1032 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1033 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1034 local_section_offset(Addend addend
) const
1036 gold_assert(this->local_sym_index_
!= GSYM_CODE
1037 && this->local_sym_index_
!= SECTION_CODE
1038 && this->local_sym_index_
!= TARGET_CODE
1039 && this->local_sym_index_
!= INVALID_CODE
1040 && this->local_sym_index_
!= 0
1041 && this->is_section_symbol_
);
1042 const unsigned int lsi
= this->local_sym_index_
;
1043 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
1044 gold_assert(os
!= NULL
);
1045 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
1046 if (offset
!= invalid_address
)
1047 return offset
+ addend
;
1048 // This is a merge section.
1049 Sized_relobj_file
<size
, big_endian
>* relobj
=
1050 this->u1_
.relobj
->sized_relobj();
1051 gold_assert(relobj
!= NULL
);
1052 offset
= os
->output_address(relobj
, lsi
, addend
);
1053 gold_assert(offset
!= invalid_address
);
1057 // Get the output address of a relocation.
1059 template<bool dynamic
, int size
, bool big_endian
>
1060 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1061 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
1063 Address address
= this->address_
;
1064 if (this->shndx_
!= INVALID_CODE
)
1066 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
1067 gold_assert(os
!= NULL
);
1068 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
1069 if (off
!= invalid_address
)
1070 address
+= os
->address() + off
;
1073 Sized_relobj_file
<size
, big_endian
>* relobj
=
1074 this->u2_
.relobj
->sized_relobj();
1075 gold_assert(relobj
!= NULL
);
1076 address
= os
->output_address(relobj
, this->shndx_
, address
);
1077 gold_assert(address
!= invalid_address
);
1080 else if (this->u2_
.od
!= NULL
)
1081 address
+= this->u2_
.od
->address();
1085 // Write out the offset and info fields of a Rel or Rela relocation
1088 template<bool dynamic
, int size
, bool big_endian
>
1089 template<typename Write_rel
>
1091 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
1092 Write_rel
* wr
) const
1094 wr
->put_r_offset(this->get_address());
1095 unsigned int sym_index
= this->get_symbol_index();
1096 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
1099 // Write out a Rel relocation.
1101 template<bool dynamic
, int size
, bool big_endian
>
1103 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
1104 unsigned char* pov
) const
1106 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
1107 this->write_rel(&orel
);
1110 // Get the value of the symbol referred to by a Rel relocation.
1112 template<bool dynamic
, int size
, bool big_endian
>
1113 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1114 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
1115 Addend addend
) const
1117 if (this->local_sym_index_
== GSYM_CODE
)
1119 const Sized_symbol
<size
>* sym
;
1120 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
1121 if (this->use_plt_offset_
&& sym
->has_plt_offset())
1123 uint64_t plt_address
=
1124 parameters
->target().plt_address_for_global(sym
);
1125 return plt_address
+ sym
->plt_offset();
1128 return sym
->value() + addend
;
1130 gold_assert(this->local_sym_index_
!= SECTION_CODE
1131 && this->local_sym_index_
!= TARGET_CODE
1132 && this->local_sym_index_
!= INVALID_CODE
1133 && this->local_sym_index_
!= 0
1134 && !this->is_section_symbol_
);
1135 const unsigned int lsi
= this->local_sym_index_
;
1136 Sized_relobj_file
<size
, big_endian
>* relobj
=
1137 this->u1_
.relobj
->sized_relobj();
1138 gold_assert(relobj
!= NULL
);
1139 if (this->use_plt_offset_
)
1141 uint64_t plt_address
=
1142 parameters
->target().plt_address_for_local(relobj
, lsi
);
1143 return plt_address
+ relobj
->local_plt_offset(lsi
);
1145 const Symbol_value
<size
>* symval
= relobj
->local_symbol(lsi
);
1146 return symval
->value(relobj
, addend
);
1149 // Reloc comparison. This function sorts the dynamic relocs for the
1150 // benefit of the dynamic linker. First we sort all relative relocs
1151 // to the front. Among relative relocs, we sort by output address.
1152 // Among non-relative relocs, we sort by symbol index, then by output
1155 template<bool dynamic
, int size
, bool big_endian
>
1157 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1158 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1161 if (this->is_relative_
)
1163 if (!r2
.is_relative_
)
1165 // Otherwise sort by reloc address below.
1167 else if (r2
.is_relative_
)
1171 unsigned int sym1
= this->get_symbol_index();
1172 unsigned int sym2
= r2
.get_symbol_index();
1175 else if (sym1
> sym2
)
1177 // Otherwise sort by reloc address.
1180 section_offset_type addr1
= this->get_address();
1181 section_offset_type addr2
= r2
.get_address();
1184 else if (addr1
> addr2
)
1187 // Final tie breaker, in order to generate the same output on any
1188 // host: reloc type.
1189 unsigned int type1
= this->type_
;
1190 unsigned int type2
= r2
.type_
;
1193 else if (type1
> type2
)
1196 // These relocs appear to be exactly the same.
1200 // Write out a Rela relocation.
1202 template<bool dynamic
, int size
, bool big_endian
>
1204 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
1205 unsigned char* pov
) const
1207 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
1208 this->rel_
.write_rel(&orel
);
1209 Addend addend
= this->addend_
;
1210 if (this->rel_
.is_target_specific())
1211 addend
= parameters
->target().reloc_addend(this->rel_
.target_arg(),
1212 this->rel_
.type(), addend
);
1213 else if (this->rel_
.is_symbolless())
1214 addend
= this->rel_
.symbol_value(addend
);
1215 else if (this->rel_
.is_local_section_symbol())
1216 addend
= this->rel_
.local_section_offset(addend
);
1217 orel
.put_r_addend(addend
);
1220 // Output_data_reloc_base methods.
1222 // Adjust the output section.
1224 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1226 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
1227 ::do_adjust_output_section(Output_section
* os
)
1229 if (sh_type
== elfcpp::SHT_REL
)
1230 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1231 else if (sh_type
== elfcpp::SHT_RELA
)
1232 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1236 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1237 // static link. The backends will generate a dynamic reloc section
1238 // to hold this. In that case we don't want to link to the dynsym
1239 // section, because there isn't one.
1241 os
->set_should_link_to_symtab();
1242 else if (parameters
->doing_static_link())
1245 os
->set_should_link_to_dynsym();
1248 // Write out relocation data.
1250 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1252 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1255 const off_t off
= this->offset();
1256 const off_t oview_size
= this->data_size();
1257 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1259 if (this->sort_relocs())
1261 gold_assert(dynamic
);
1262 std::sort(this->relocs_
.begin(), this->relocs_
.end(),
1263 Sort_relocs_comparison());
1266 unsigned char* pov
= oview
;
1267 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
1268 p
!= this->relocs_
.end();
1275 gold_assert(pov
- oview
== oview_size
);
1277 of
->write_output_view(off
, oview_size
, oview
);
1279 // We no longer need the relocation entries.
1280 this->relocs_
.clear();
1283 // Class Output_relocatable_relocs.
1285 template<int sh_type
, int size
, bool big_endian
>
1287 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1289 this->set_data_size(this->rr_
->output_reloc_count()
1290 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1293 // class Output_data_group.
1295 template<int size
, bool big_endian
>
1296 Output_data_group
<size
, big_endian
>::Output_data_group(
1297 Sized_relobj_file
<size
, big_endian
>* relobj
,
1298 section_size_type entry_count
,
1299 elfcpp::Elf_Word flags
,
1300 std::vector
<unsigned int>* input_shndxes
)
1301 : Output_section_data(entry_count
* 4, 4, false),
1305 this->input_shndxes_
.swap(*input_shndxes
);
1308 // Write out the section group, which means translating the section
1309 // indexes to apply to the output file.
1311 template<int size
, bool big_endian
>
1313 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1315 const off_t off
= this->offset();
1316 const section_size_type oview_size
=
1317 convert_to_section_size_type(this->data_size());
1318 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1320 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1321 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1324 for (std::vector
<unsigned int>::const_iterator p
=
1325 this->input_shndxes_
.begin();
1326 p
!= this->input_shndxes_
.end();
1329 Output_section
* os
= this->relobj_
->output_section(*p
);
1331 unsigned int output_shndx
;
1333 output_shndx
= os
->out_shndx();
1336 this->relobj_
->error(_("section group retained but "
1337 "group element discarded"));
1341 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1344 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1345 gold_assert(wrote
== oview_size
);
1347 of
->write_output_view(off
, oview_size
, oview
);
1349 // We no longer need this information.
1350 this->input_shndxes_
.clear();
1353 // Output_data_got::Got_entry methods.
1355 // Write out the entry.
1357 template<int size
, bool big_endian
>
1359 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1363 switch (this->local_sym_index_
)
1367 // If the symbol is resolved locally, we need to write out the
1368 // link-time value, which will be relocated dynamically by a
1369 // RELATIVE relocation.
1370 Symbol
* gsym
= this->u_
.gsym
;
1371 if (this->use_plt_offset_
&& gsym
->has_plt_offset())
1372 val
= (parameters
->target().plt_address_for_global(gsym
)
1373 + gsym
->plt_offset());
1376 Sized_symbol
<size
>* sgsym
;
1377 // This cast is a bit ugly. We don't want to put a
1378 // virtual method in Symbol, because we want Symbol to be
1379 // as small as possible.
1380 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1381 val
= sgsym
->value();
1387 val
= this->u_
.constant
;
1391 // If we're doing an incremental update, don't touch this GOT entry.
1392 if (parameters
->incremental_update())
1394 val
= this->u_
.constant
;
1399 const Relobj
* object
= this->u_
.object
;
1400 const unsigned int lsi
= this->local_sym_index_
;
1401 if (!this->use_plt_offset_
)
1403 uint64_t lval
= object
->local_symbol_value(lsi
, 0);
1404 val
= convert_types
<Valtype
, uint64_t>(lval
);
1408 uint64_t plt_address
=
1409 parameters
->target().plt_address_for_local(object
, lsi
);
1410 val
= plt_address
+ object
->local_plt_offset(lsi
);
1416 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1419 // Output_data_got methods.
1421 // Add an entry for a global symbol to the GOT. This returns true if
1422 // this is a new GOT entry, false if the symbol already had a GOT
1425 template<int size
, bool big_endian
>
1427 Output_data_got
<size
, big_endian
>::add_global(
1429 unsigned int got_type
)
1431 if (gsym
->has_got_offset(got_type
))
1434 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, false));
1435 gsym
->set_got_offset(got_type
, got_offset
);
1439 // Like add_global, but use the PLT offset.
1441 template<int size
, bool big_endian
>
1443 Output_data_got
<size
, big_endian
>::add_global_plt(Symbol
* gsym
,
1444 unsigned int got_type
)
1446 if (gsym
->has_got_offset(got_type
))
1449 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, true));
1450 gsym
->set_got_offset(got_type
, got_offset
);
1454 // Add an entry for a global symbol to the GOT, and add a dynamic
1455 // relocation of type R_TYPE for the GOT entry.
1457 template<int size
, bool big_endian
>
1459 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1461 unsigned int got_type
,
1462 Output_data_reloc_generic
* rel_dyn
,
1463 unsigned int r_type
)
1465 if (gsym
->has_got_offset(got_type
))
1468 unsigned int got_offset
= this->add_got_entry(Got_entry());
1469 gsym
->set_got_offset(got_type
, got_offset
);
1470 rel_dyn
->add_global_generic(gsym
, r_type
, this, got_offset
, 0);
1473 // Add a pair of entries for a global symbol to the GOT, and add
1474 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1475 // If R_TYPE_2 == 0, add the second entry with no relocation.
1476 template<int size
, bool big_endian
>
1478 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1480 unsigned int got_type
,
1481 Output_data_reloc_generic
* rel_dyn
,
1482 unsigned int r_type_1
,
1483 unsigned int r_type_2
)
1485 if (gsym
->has_got_offset(got_type
))
1488 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1489 gsym
->set_got_offset(got_type
, got_offset
);
1490 rel_dyn
->add_global_generic(gsym
, r_type_1
, this, got_offset
, 0);
1493 rel_dyn
->add_global_generic(gsym
, r_type_2
, this,
1494 got_offset
+ size
/ 8, 0);
1497 // Add an entry for a local symbol to the GOT. This returns true if
1498 // this is a new GOT entry, false if the symbol already has a GOT
1501 template<int size
, bool big_endian
>
1503 Output_data_got
<size
, big_endian
>::add_local(
1505 unsigned int symndx
,
1506 unsigned int got_type
)
1508 if (object
->local_has_got_offset(symndx
, got_type
))
1511 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1513 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1517 // Like add_local, but use the PLT offset.
1519 template<int size
, bool big_endian
>
1521 Output_data_got
<size
, big_endian
>::add_local_plt(
1523 unsigned int symndx
,
1524 unsigned int got_type
)
1526 if (object
->local_has_got_offset(symndx
, got_type
))
1529 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1531 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1535 // Add an entry for a local symbol to the GOT, and add a dynamic
1536 // relocation of type R_TYPE for the GOT entry.
1538 template<int size
, bool big_endian
>
1540 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1542 unsigned int symndx
,
1543 unsigned int got_type
,
1544 Output_data_reloc_generic
* rel_dyn
,
1545 unsigned int r_type
)
1547 if (object
->local_has_got_offset(symndx
, got_type
))
1550 unsigned int got_offset
= this->add_got_entry(Got_entry());
1551 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1552 rel_dyn
->add_local_generic(object
, symndx
, r_type
, this, got_offset
, 0);
1555 // Add a pair of entries for a local symbol to the GOT, and add
1556 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1557 // If R_TYPE_2 == 0, add the second entry with no relocation.
1558 template<int size
, bool big_endian
>
1560 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1562 unsigned int symndx
,
1564 unsigned int got_type
,
1565 Output_data_reloc_generic
* rel_dyn
,
1566 unsigned int r_type_1
,
1567 unsigned int r_type_2
)
1569 if (object
->local_has_got_offset(symndx
, got_type
))
1572 unsigned int got_offset
=
1573 this->add_got_entry_pair(Got_entry(),
1574 Got_entry(object
, symndx
, false));
1575 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1576 Output_section
* os
= object
->output_section(shndx
);
1577 rel_dyn
->add_output_section_generic(os
, r_type_1
, this, got_offset
, 0);
1580 rel_dyn
->add_output_section_generic(os
, r_type_2
, this,
1581 got_offset
+ size
/ 8, 0);
1584 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1586 template<int size
, bool big_endian
>
1588 Output_data_got
<size
, big_endian
>::reserve_local(
1591 unsigned int sym_index
,
1592 unsigned int got_type
)
1594 this->do_reserve_slot(i
);
1595 object
->set_local_got_offset(sym_index
, got_type
, this->got_offset(i
));
1598 // Reserve a slot in the GOT for a global symbol.
1600 template<int size
, bool big_endian
>
1602 Output_data_got
<size
, big_endian
>::reserve_global(
1605 unsigned int got_type
)
1607 this->do_reserve_slot(i
);
1608 gsym
->set_got_offset(got_type
, this->got_offset(i
));
1611 // Write out the GOT.
1613 template<int size
, bool big_endian
>
1615 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1617 const int add
= size
/ 8;
1619 const off_t off
= this->offset();
1620 const off_t oview_size
= this->data_size();
1621 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1623 unsigned char* pov
= oview
;
1624 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1625 p
!= this->entries_
.end();
1632 gold_assert(pov
- oview
== oview_size
);
1634 of
->write_output_view(off
, oview_size
, oview
);
1636 // We no longer need the GOT entries.
1637 this->entries_
.clear();
1640 // Create a new GOT entry and return its offset.
1642 template<int size
, bool big_endian
>
1644 Output_data_got
<size
, big_endian
>::add_got_entry(Got_entry got_entry
)
1646 if (!this->is_data_size_valid())
1648 this->entries_
.push_back(got_entry
);
1649 this->set_got_size();
1650 return this->last_got_offset();
1654 // For an incremental update, find an available slot.
1655 off_t got_offset
= this->free_list_
.allocate(size
/ 8, size
/ 8, 0);
1656 if (got_offset
== -1)
1657 gold_fallback(_("out of patch space (GOT);"
1658 " relink with --incremental-full"));
1659 unsigned int got_index
= got_offset
/ (size
/ 8);
1660 gold_assert(got_index
< this->entries_
.size());
1661 this->entries_
[got_index
] = got_entry
;
1662 return static_cast<unsigned int>(got_offset
);
1666 // Create a pair of new GOT entries and return the offset of the first.
1668 template<int size
, bool big_endian
>
1670 Output_data_got
<size
, big_endian
>::add_got_entry_pair(Got_entry got_entry_1
,
1671 Got_entry got_entry_2
)
1673 if (!this->is_data_size_valid())
1675 unsigned int got_offset
;
1676 this->entries_
.push_back(got_entry_1
);
1677 got_offset
= this->last_got_offset();
1678 this->entries_
.push_back(got_entry_2
);
1679 this->set_got_size();
1684 // For an incremental update, find an available pair of slots.
1685 off_t got_offset
= this->free_list_
.allocate(2 * size
/ 8, size
/ 8, 0);
1686 if (got_offset
== -1)
1687 gold_fallback(_("out of patch space (GOT);"
1688 " relink with --incremental-full"));
1689 unsigned int got_index
= got_offset
/ (size
/ 8);
1690 gold_assert(got_index
< this->entries_
.size());
1691 this->entries_
[got_index
] = got_entry_1
;
1692 this->entries_
[got_index
+ 1] = got_entry_2
;
1693 return static_cast<unsigned int>(got_offset
);
1697 // Output_data_dynamic::Dynamic_entry methods.
1699 // Write out the entry.
1701 template<int size
, bool big_endian
>
1703 Output_data_dynamic::Dynamic_entry::write(
1705 const Stringpool
* pool
) const
1707 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1708 switch (this->offset_
)
1710 case DYNAMIC_NUMBER
:
1714 case DYNAMIC_SECTION_SIZE
:
1715 val
= this->u_
.od
->data_size();
1716 if (this->od2
!= NULL
)
1717 val
+= this->od2
->data_size();
1720 case DYNAMIC_SYMBOL
:
1722 const Sized_symbol
<size
>* s
=
1723 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1728 case DYNAMIC_STRING
:
1729 val
= pool
->get_offset(this->u_
.str
);
1733 val
= this->u_
.od
->address() + this->offset_
;
1737 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1738 dw
.put_d_tag(this->tag_
);
1742 // Output_data_dynamic methods.
1744 // Adjust the output section to set the entry size.
1747 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1749 if (parameters
->target().get_size() == 32)
1750 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1751 else if (parameters
->target().get_size() == 64)
1752 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1757 // Set the final data size.
1760 Output_data_dynamic::set_final_data_size()
1762 // Add the terminating entry if it hasn't been added.
1763 // Because of relaxation, we can run this multiple times.
1764 if (this->entries_
.empty() || this->entries_
.back().tag() != elfcpp::DT_NULL
)
1766 int extra
= parameters
->options().spare_dynamic_tags();
1767 for (int i
= 0; i
< extra
; ++i
)
1768 this->add_constant(elfcpp::DT_NULL
, 0);
1769 this->add_constant(elfcpp::DT_NULL
, 0);
1773 if (parameters
->target().get_size() == 32)
1774 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1775 else if (parameters
->target().get_size() == 64)
1776 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1779 this->set_data_size(this->entries_
.size() * dyn_size
);
1782 // Write out the dynamic entries.
1785 Output_data_dynamic::do_write(Output_file
* of
)
1787 switch (parameters
->size_and_endianness())
1789 #ifdef HAVE_TARGET_32_LITTLE
1790 case Parameters::TARGET_32_LITTLE
:
1791 this->sized_write
<32, false>(of
);
1794 #ifdef HAVE_TARGET_32_BIG
1795 case Parameters::TARGET_32_BIG
:
1796 this->sized_write
<32, true>(of
);
1799 #ifdef HAVE_TARGET_64_LITTLE
1800 case Parameters::TARGET_64_LITTLE
:
1801 this->sized_write
<64, false>(of
);
1804 #ifdef HAVE_TARGET_64_BIG
1805 case Parameters::TARGET_64_BIG
:
1806 this->sized_write
<64, true>(of
);
1814 template<int size
, bool big_endian
>
1816 Output_data_dynamic::sized_write(Output_file
* of
)
1818 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1820 const off_t offset
= this->offset();
1821 const off_t oview_size
= this->data_size();
1822 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1824 unsigned char* pov
= oview
;
1825 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1826 p
!= this->entries_
.end();
1829 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1833 gold_assert(pov
- oview
== oview_size
);
1835 of
->write_output_view(offset
, oview_size
, oview
);
1837 // We no longer need the dynamic entries.
1838 this->entries_
.clear();
1841 // Class Output_symtab_xindex.
1844 Output_symtab_xindex::do_write(Output_file
* of
)
1846 const off_t offset
= this->offset();
1847 const off_t oview_size
= this->data_size();
1848 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1850 memset(oview
, 0, oview_size
);
1852 if (parameters
->target().is_big_endian())
1853 this->endian_do_write
<true>(oview
);
1855 this->endian_do_write
<false>(oview
);
1857 of
->write_output_view(offset
, oview_size
, oview
);
1859 // We no longer need the data.
1860 this->entries_
.clear();
1863 template<bool big_endian
>
1865 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1867 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1868 p
!= this->entries_
.end();
1871 unsigned int symndx
= p
->first
;
1872 gold_assert(symndx
* 4 < this->data_size());
1873 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ symndx
* 4, p
->second
);
1877 // Output_fill_debug_info methods.
1879 // Return the minimum size needed for a dummy compilation unit header.
1882 Output_fill_debug_info::do_minimum_hole_size() const
1884 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1886 const size_t len
= 4 + 2 + 4 + 1;
1887 // For type units, add type_signature, type_offset.
1888 if (this->is_debug_types_
)
1893 // Write a dummy compilation unit header to fill a hole in the
1894 // .debug_info or .debug_types section.
1897 Output_fill_debug_info::do_write(Output_file
* of
, off_t off
, size_t len
) const
1899 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_info(%08lx, %08lx)",
1900 static_cast<long>(off
), static_cast<long>(len
));
1902 gold_assert(len
>= this->do_minimum_hole_size());
1904 unsigned char* const oview
= of
->get_output_view(off
, len
);
1905 unsigned char* pov
= oview
;
1907 // Write header fields: unit_length, version, debug_abbrev_offset,
1909 if (this->is_big_endian())
1911 elfcpp::Swap_unaligned
<32, true>::writeval(pov
, len
- 4);
1912 elfcpp::Swap_unaligned
<16, true>::writeval(pov
+ 4, this->version
);
1913 elfcpp::Swap_unaligned
<32, true>::writeval(pov
+ 6, 0);
1917 elfcpp::Swap_unaligned
<32, false>::writeval(pov
, len
- 4);
1918 elfcpp::Swap_unaligned
<16, false>::writeval(pov
+ 4, this->version
);
1919 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 6, 0);
1924 // For type units, the additional header fields -- type_signature,
1925 // type_offset -- can be filled with zeroes.
1927 // Fill the remainder of the free space with zeroes. The first
1928 // zero should tell the consumer there are no DIEs to read in this
1929 // compilation unit.
1930 if (pov
< oview
+ len
)
1931 memset(pov
, 0, oview
+ len
- pov
);
1933 of
->write_output_view(off
, len
, oview
);
1936 // Output_fill_debug_line methods.
1938 // Return the minimum size needed for a dummy line number program header.
1941 Output_fill_debug_line::do_minimum_hole_size() const
1943 // Line number program header fields: unit_length, version, header_length,
1944 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1945 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1946 const size_t len
= 4 + 2 + 4 + this->header_length
;
1950 // Write a dummy line number program header to fill a hole in the
1951 // .debug_line section.
1954 Output_fill_debug_line::do_write(Output_file
* of
, off_t off
, size_t len
) const
1956 gold_debug(DEBUG_INCREMENTAL
, "fill_debug_line(%08lx, %08lx)",
1957 static_cast<long>(off
), static_cast<long>(len
));
1959 gold_assert(len
>= this->do_minimum_hole_size());
1961 unsigned char* const oview
= of
->get_output_view(off
, len
);
1962 unsigned char* pov
= oview
;
1964 // Write header fields: unit_length, version, header_length,
1965 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1966 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1967 // We set the header_length field to cover the entire hole, so the
1968 // line number program is empty.
1969 if (this->is_big_endian())
1971 elfcpp::Swap_unaligned
<32, true>::writeval(pov
, len
- 4);
1972 elfcpp::Swap_unaligned
<16, true>::writeval(pov
+ 4, this->version
);
1973 elfcpp::Swap_unaligned
<32, true>::writeval(pov
+ 6, len
- (4 + 2 + 4));
1977 elfcpp::Swap_unaligned
<32, false>::writeval(pov
, len
- 4);
1978 elfcpp::Swap_unaligned
<16, false>::writeval(pov
+ 4, this->version
);
1979 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 6, len
- (4 + 2 + 4));
1982 *pov
++ = 1; // minimum_instruction_length
1983 *pov
++ = 0; // default_is_stmt
1984 *pov
++ = 0; // line_base
1985 *pov
++ = 5; // line_range
1986 *pov
++ = 13; // opcode_base
1987 *pov
++ = 0; // standard_opcode_lengths[1]
1988 *pov
++ = 1; // standard_opcode_lengths[2]
1989 *pov
++ = 1; // standard_opcode_lengths[3]
1990 *pov
++ = 1; // standard_opcode_lengths[4]
1991 *pov
++ = 1; // standard_opcode_lengths[5]
1992 *pov
++ = 0; // standard_opcode_lengths[6]
1993 *pov
++ = 0; // standard_opcode_lengths[7]
1994 *pov
++ = 0; // standard_opcode_lengths[8]
1995 *pov
++ = 1; // standard_opcode_lengths[9]
1996 *pov
++ = 0; // standard_opcode_lengths[10]
1997 *pov
++ = 0; // standard_opcode_lengths[11]
1998 *pov
++ = 1; // standard_opcode_lengths[12]
1999 *pov
++ = 0; // include_directories (empty)
2000 *pov
++ = 0; // filenames (empty)
2002 // Some consumers don't check the header_length field, and simply
2003 // start reading the line number program immediately following the
2004 // header. For those consumers, we fill the remainder of the free
2005 // space with DW_LNS_set_basic_block opcodes. These are effectively
2006 // no-ops: the resulting line table program will not create any rows.
2007 if (pov
< oview
+ len
)
2008 memset(pov
, elfcpp::DW_LNS_set_basic_block
, oview
+ len
- pov
);
2010 of
->write_output_view(off
, len
, oview
);
2013 // Output_section::Input_section methods.
2015 // Return the current data size. For an input section we store the size here.
2016 // For an Output_section_data, we have to ask it for the size.
2019 Output_section::Input_section::current_data_size() const
2021 if (this->is_input_section())
2022 return this->u1_
.data_size
;
2025 this->u2_
.posd
->pre_finalize_data_size();
2026 return this->u2_
.posd
->current_data_size();
2030 // Return the data size. For an input section we store the size here.
2031 // For an Output_section_data, we have to ask it for the size.
2034 Output_section::Input_section::data_size() const
2036 if (this->is_input_section())
2037 return this->u1_
.data_size
;
2039 return this->u2_
.posd
->data_size();
2042 // Return the object for an input section.
2045 Output_section::Input_section::relobj() const
2047 if (this->is_input_section())
2048 return this->u2_
.object
;
2049 else if (this->is_merge_section())
2051 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2052 return this->u2_
.pomb
->first_relobj();
2054 else if (this->is_relaxed_input_section())
2055 return this->u2_
.poris
->relobj();
2060 // Return the input section index for an input section.
2063 Output_section::Input_section::shndx() const
2065 if (this->is_input_section())
2066 return this->shndx_
;
2067 else if (this->is_merge_section())
2069 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
2070 return this->u2_
.pomb
->first_shndx();
2072 else if (this->is_relaxed_input_section())
2073 return this->u2_
.poris
->shndx();
2078 // Set the address and file offset.
2081 Output_section::Input_section::set_address_and_file_offset(
2084 off_t section_file_offset
)
2086 if (this->is_input_section())
2087 this->u2_
.object
->set_section_offset(this->shndx_
,
2088 file_offset
- section_file_offset
);
2090 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
2093 // Reset the address and file offset.
2096 Output_section::Input_section::reset_address_and_file_offset()
2098 if (!this->is_input_section())
2099 this->u2_
.posd
->reset_address_and_file_offset();
2102 // Finalize the data size.
2105 Output_section::Input_section::finalize_data_size()
2107 if (!this->is_input_section())
2108 this->u2_
.posd
->finalize_data_size();
2111 // Try to turn an input offset into an output offset. We want to
2112 // return the output offset relative to the start of this
2113 // Input_section in the output section.
2116 Output_section::Input_section::output_offset(
2117 const Relobj
* object
,
2119 section_offset_type offset
,
2120 section_offset_type
* poutput
) const
2122 if (!this->is_input_section())
2123 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
2126 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
2133 // Return whether this is the merge section for the input section
2137 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
2138 unsigned int shndx
) const
2140 if (this->is_input_section())
2142 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
2145 // Write out the data. We don't have to do anything for an input
2146 // section--they are handled via Object::relocate--but this is where
2147 // we write out the data for an Output_section_data.
2150 Output_section::Input_section::write(Output_file
* of
)
2152 if (!this->is_input_section())
2153 this->u2_
.posd
->write(of
);
2156 // Write the data to a buffer. As for write(), we don't have to do
2157 // anything for an input section.
2160 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
2162 if (!this->is_input_section())
2163 this->u2_
.posd
->write_to_buffer(buffer
);
2166 // Print to a map file.
2169 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
2171 switch (this->shndx_
)
2173 case OUTPUT_SECTION_CODE
:
2174 case MERGE_DATA_SECTION_CODE
:
2175 case MERGE_STRING_SECTION_CODE
:
2176 this->u2_
.posd
->print_to_mapfile(mapfile
);
2179 case RELAXED_INPUT_SECTION_CODE
:
2181 Output_relaxed_input_section
* relaxed_section
=
2182 this->relaxed_input_section();
2183 mapfile
->print_input_section(relaxed_section
->relobj(),
2184 relaxed_section
->shndx());
2188 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
2193 // Output_section methods.
2195 // Construct an Output_section. NAME will point into a Stringpool.
2197 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
2198 elfcpp::Elf_Xword flags
)
2203 link_section_(NULL
),
2205 info_section_(NULL
),
2210 order_(ORDER_INVALID
),
2215 first_input_offset_(0),
2217 postprocessing_buffer_(NULL
),
2218 needs_symtab_index_(false),
2219 needs_dynsym_index_(false),
2220 should_link_to_symtab_(false),
2221 should_link_to_dynsym_(false),
2222 after_input_sections_(false),
2223 requires_postprocessing_(false),
2224 found_in_sections_clause_(false),
2225 has_load_address_(false),
2226 info_uses_section_index_(false),
2227 input_section_order_specified_(false),
2228 may_sort_attached_input_sections_(false),
2229 must_sort_attached_input_sections_(false),
2230 attached_input_sections_are_sorted_(false),
2232 is_small_section_(false),
2233 is_large_section_(false),
2234 generate_code_fills_at_write_(false),
2235 is_entsize_zero_(false),
2236 section_offsets_need_adjustment_(false),
2238 always_keeps_input_sections_(false),
2239 has_fixed_layout_(false),
2240 is_patch_space_allowed_(false),
2243 lookup_maps_(new Output_section_lookup_maps
),
2245 free_space_fill_(NULL
),
2248 // An unallocated section has no address. Forcing this means that
2249 // we don't need special treatment for symbols defined in debug
2251 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
2252 this->set_address(0);
2255 Output_section::~Output_section()
2257 delete this->checkpoint_
;
2260 // Set the entry size.
2263 Output_section::set_entsize(uint64_t v
)
2265 if (this->is_entsize_zero_
)
2267 else if (this->entsize_
== 0)
2269 else if (this->entsize_
!= v
)
2272 this->is_entsize_zero_
= 1;
2276 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2277 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2278 // relocation section which applies to this section, or 0 if none, or
2279 // -1U if more than one. Return the offset of the input section
2280 // within the output section. Return -1 if the input section will
2281 // receive special handling. In the normal case we don't always keep
2282 // track of input sections for an Output_section. Instead, each
2283 // Object keeps track of the Output_section for each of its input
2284 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2285 // track of input sections here; this is used when SECTIONS appears in
2288 template<int size
, bool big_endian
>
2290 Output_section::add_input_section(Layout
* layout
,
2291 Sized_relobj_file
<size
, big_endian
>* object
,
2293 const char* secname
,
2294 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2295 unsigned int reloc_shndx
,
2296 bool have_sections_script
)
2298 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
2299 if ((addralign
& (addralign
- 1)) != 0)
2301 object
->error(_("invalid alignment %lu for section \"%s\""),
2302 static_cast<unsigned long>(addralign
), secname
);
2306 if (addralign
> this->addralign_
)
2307 this->addralign_
= addralign
;
2309 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
2310 uint64_t entsize
= shdr
.get_sh_entsize();
2312 // .debug_str is a mergeable string section, but is not always so
2313 // marked by compilers. Mark manually here so we can optimize.
2314 if (strcmp(secname
, ".debug_str") == 0)
2316 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
2320 this->update_flags_for_input_section(sh_flags
);
2321 this->set_entsize(entsize
);
2323 // If this is a SHF_MERGE section, we pass all the input sections to
2324 // a Output_data_merge. We don't try to handle relocations for such
2325 // a section. We don't try to handle empty merge sections--they
2326 // mess up the mappings, and are useless anyhow.
2327 // FIXME: Need to handle merge sections during incremental update.
2328 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
2330 && shdr
.get_sh_size() > 0
2331 && !parameters
->incremental())
2333 // Keep information about merged input sections for rebuilding fast
2334 // lookup maps if we have sections-script or we do relaxation.
2335 bool keeps_input_sections
= (this->always_keeps_input_sections_
2336 || have_sections_script
2337 || parameters
->target().may_relax());
2339 if (this->add_merge_input_section(object
, shndx
, sh_flags
, entsize
,
2340 addralign
, keeps_input_sections
))
2342 // Tell the relocation routines that they need to call the
2343 // output_offset method to determine the final address.
2348 section_size_type input_section_size
= shdr
.get_sh_size();
2349 section_size_type uncompressed_size
;
2350 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
2351 input_section_size
= uncompressed_size
;
2353 off_t offset_in_section
;
2354 off_t aligned_offset_in_section
;
2355 if (this->has_fixed_layout())
2357 // For incremental updates, find a chunk of unused space in the section.
2358 offset_in_section
= this->free_list_
.allocate(input_section_size
,
2360 if (offset_in_section
== -1)
2361 gold_fallback(_("out of patch space in section %s; "
2362 "relink with --incremental-full"),
2364 aligned_offset_in_section
= offset_in_section
;
2368 offset_in_section
= this->current_data_size_for_child();
2369 aligned_offset_in_section
= align_address(offset_in_section
,
2371 this->set_current_data_size_for_child(aligned_offset_in_section
2372 + input_section_size
);
2375 // Determine if we want to delay code-fill generation until the output
2376 // section is written. When the target is relaxing, we want to delay fill
2377 // generating to avoid adjusting them during relaxation. Also, if we are
2378 // sorting input sections we must delay fill generation.
2379 if (!this->generate_code_fills_at_write_
2380 && !have_sections_script
2381 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2382 && parameters
->target().has_code_fill()
2383 && (parameters
->target().may_relax()
2384 || layout
->is_section_ordering_specified()))
2386 gold_assert(this->fills_
.empty());
2387 this->generate_code_fills_at_write_
= true;
2390 if (aligned_offset_in_section
> offset_in_section
2391 && !this->generate_code_fills_at_write_
2392 && !have_sections_script
2393 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2394 && parameters
->target().has_code_fill())
2396 // We need to add some fill data. Using fill_list_ when
2397 // possible is an optimization, since we will often have fill
2398 // sections without input sections.
2399 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
2400 if (this->input_sections_
.empty())
2401 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
2404 std::string
fill_data(parameters
->target().code_fill(fill_len
));
2405 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
2406 this->input_sections_
.push_back(Input_section(odc
));
2410 // We need to keep track of this section if we are already keeping
2411 // track of sections, or if we are relaxing. Also, if this is a
2412 // section which requires sorting, or which may require sorting in
2413 // the future, we keep track of the sections. If the
2414 // --section-ordering-file option is used to specify the order of
2415 // sections, we need to keep track of sections.
2416 if (this->always_keeps_input_sections_
2417 || have_sections_script
2418 || !this->input_sections_
.empty()
2419 || this->may_sort_attached_input_sections()
2420 || this->must_sort_attached_input_sections()
2421 || parameters
->options().user_set_Map()
2422 || parameters
->target().may_relax()
2423 || layout
->is_section_ordering_specified())
2425 Input_section
isecn(object
, shndx
, input_section_size
, addralign
);
2426 /* If section ordering is requested by specifying a ordering file,
2427 using --section-ordering-file, match the section name with
2429 if (parameters
->options().section_ordering_file())
2431 unsigned int section_order_index
=
2432 layout
->find_section_order_index(std::string(secname
));
2433 if (section_order_index
!= 0)
2435 isecn
.set_section_order_index(section_order_index
);
2436 this->set_input_section_order_specified();
2439 if (this->has_fixed_layout())
2441 // For incremental updates, finalize the address and offset now.
2442 uint64_t addr
= this->address();
2443 isecn
.set_address_and_file_offset(addr
+ aligned_offset_in_section
,
2444 aligned_offset_in_section
,
2447 this->input_sections_
.push_back(isecn
);
2450 return aligned_offset_in_section
;
2453 // Add arbitrary data to an output section.
2456 Output_section::add_output_section_data(Output_section_data
* posd
)
2458 Input_section
inp(posd
);
2459 this->add_output_section_data(&inp
);
2461 if (posd
->is_data_size_valid())
2463 off_t offset_in_section
;
2464 if (this->has_fixed_layout())
2466 // For incremental updates, find a chunk of unused space.
2467 offset_in_section
= this->free_list_
.allocate(posd
->data_size(),
2468 posd
->addralign(), 0);
2469 if (offset_in_section
== -1)
2470 gold_fallback(_("out of patch space in section %s; "
2471 "relink with --incremental-full"),
2473 // Finalize the address and offset now.
2474 uint64_t addr
= this->address();
2475 off_t offset
= this->offset();
2476 posd
->set_address_and_file_offset(addr
+ offset_in_section
,
2477 offset
+ offset_in_section
);
2481 offset_in_section
= this->current_data_size_for_child();
2482 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2484 this->set_current_data_size_for_child(aligned_offset_in_section
2485 + posd
->data_size());
2488 else if (this->has_fixed_layout())
2490 // For incremental updates, arrange for the data to have a fixed layout.
2491 // This will mean that additions to the data must be allocated from
2492 // free space within the containing output section.
2493 uint64_t addr
= this->address();
2494 posd
->set_address(addr
);
2495 posd
->set_file_offset(0);
2496 // FIXME: This should eventually be unreachable.
2497 // gold_unreachable();
2501 // Add a relaxed input section.
2504 Output_section::add_relaxed_input_section(Layout
* layout
,
2505 Output_relaxed_input_section
* poris
,
2506 const std::string
& name
)
2508 Input_section
inp(poris
);
2510 // If the --section-ordering-file option is used to specify the order of
2511 // sections, we need to keep track of sections.
2512 if (layout
->is_section_ordering_specified())
2514 unsigned int section_order_index
=
2515 layout
->find_section_order_index(name
);
2516 if (section_order_index
!= 0)
2518 inp
.set_section_order_index(section_order_index
);
2519 this->set_input_section_order_specified();
2523 this->add_output_section_data(&inp
);
2524 if (this->lookup_maps_
->is_valid())
2525 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2526 poris
->shndx(), poris
);
2528 // For a relaxed section, we use the current data size. Linker scripts
2529 // get all the input sections, including relaxed one from an output
2530 // section and add them back to them same output section to compute the
2531 // output section size. If we do not account for sizes of relaxed input
2532 // sections, an output section would be incorrectly sized.
2533 off_t offset_in_section
= this->current_data_size_for_child();
2534 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2535 poris
->addralign());
2536 this->set_current_data_size_for_child(aligned_offset_in_section
2537 + poris
->current_data_size());
2540 // Add arbitrary data to an output section by Input_section.
2543 Output_section::add_output_section_data(Input_section
* inp
)
2545 if (this->input_sections_
.empty())
2546 this->first_input_offset_
= this->current_data_size_for_child();
2548 this->input_sections_
.push_back(*inp
);
2550 uint64_t addralign
= inp
->addralign();
2551 if (addralign
> this->addralign_
)
2552 this->addralign_
= addralign
;
2554 inp
->set_output_section(this);
2557 // Add a merge section to an output section.
2560 Output_section::add_output_merge_section(Output_section_data
* posd
,
2561 bool is_string
, uint64_t entsize
)
2563 Input_section
inp(posd
, is_string
, entsize
);
2564 this->add_output_section_data(&inp
);
2567 // Add an input section to a SHF_MERGE section.
2570 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
2571 uint64_t flags
, uint64_t entsize
,
2573 bool keeps_input_sections
)
2575 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
2577 // We only merge strings if the alignment is not more than the
2578 // character size. This could be handled, but it's unusual.
2579 if (is_string
&& addralign
> entsize
)
2582 // We cannot restore merged input section states.
2583 gold_assert(this->checkpoint_
== NULL
);
2585 // Look up merge sections by required properties.
2586 // Currently, we only invalidate the lookup maps in script processing
2587 // and relaxation. We should not have done either when we reach here.
2588 // So we assume that the lookup maps are valid to simply code.
2589 gold_assert(this->lookup_maps_
->is_valid());
2590 Merge_section_properties
msp(is_string
, entsize
, addralign
);
2591 Output_merge_base
* pomb
= this->lookup_maps_
->find_merge_section(msp
);
2592 bool is_new
= false;
2595 gold_assert(pomb
->is_string() == is_string
2596 && pomb
->entsize() == entsize
2597 && pomb
->addralign() == addralign
);
2601 // Create a new Output_merge_data or Output_merge_string_data.
2603 pomb
= new Output_merge_data(entsize
, addralign
);
2609 pomb
= new Output_merge_string
<char>(addralign
);
2612 pomb
= new Output_merge_string
<uint16_t>(addralign
);
2615 pomb
= new Output_merge_string
<uint32_t>(addralign
);
2621 // If we need to do script processing or relaxation, we need to keep
2622 // the original input sections to rebuild the fast lookup maps.
2623 if (keeps_input_sections
)
2624 pomb
->set_keeps_input_sections();
2628 if (pomb
->add_input_section(object
, shndx
))
2630 // Add new merge section to this output section and link merge
2631 // section properties to new merge section in map.
2634 this->add_output_merge_section(pomb
, is_string
, entsize
);
2635 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2638 // Add input section to new merge section and link input section to new
2639 // merge section in map.
2640 this->lookup_maps_
->add_merge_input_section(object
, shndx
, pomb
);
2645 // If add_input_section failed, delete new merge section to avoid
2646 // exporting empty merge sections in Output_section::get_input_section.
2653 // Build a relaxation map to speed up relaxation of existing input sections.
2654 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2657 Output_section::build_relaxation_map(
2658 const Input_section_list
& input_sections
,
2660 Relaxation_map
* relaxation_map
) const
2662 for (size_t i
= 0; i
< limit
; ++i
)
2664 const Input_section
& is(input_sections
[i
]);
2665 if (is
.is_input_section() || is
.is_relaxed_input_section())
2667 Section_id
sid(is
.relobj(), is
.shndx());
2668 (*relaxation_map
)[sid
] = i
;
2673 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2674 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2675 // indices of INPUT_SECTIONS.
2678 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2679 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
2680 const Relaxation_map
& map
,
2681 Input_section_list
* input_sections
)
2683 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2685 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2686 Section_id
sid(poris
->relobj(), poris
->shndx());
2687 Relaxation_map::const_iterator p
= map
.find(sid
);
2688 gold_assert(p
!= map
.end());
2689 gold_assert((*input_sections
)[p
->second
].is_input_section());
2691 // Remember section order index of original input section
2692 // if it is set. Copy it to the relaxed input section.
2694 (*input_sections
)[p
->second
].section_order_index();
2695 (*input_sections
)[p
->second
] = Input_section(poris
);
2696 (*input_sections
)[p
->second
].set_section_order_index(soi
);
2700 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2701 // is a vector of pointers to Output_relaxed_input_section or its derived
2702 // classes. The relaxed sections must correspond to existing input sections.
2705 Output_section::convert_input_sections_to_relaxed_sections(
2706 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
)
2708 gold_assert(parameters
->target().may_relax());
2710 // We want to make sure that restore_states does not undo the effect of
2711 // this. If there is no checkpoint active, just search the current
2712 // input section list and replace the sections there. If there is
2713 // a checkpoint, also replace the sections there.
2715 // By default, we look at the whole list.
2716 size_t limit
= this->input_sections_
.size();
2718 if (this->checkpoint_
!= NULL
)
2720 // Replace input sections with relaxed input section in the saved
2721 // copy of the input section list.
2722 if (this->checkpoint_
->input_sections_saved())
2725 this->build_relaxation_map(
2726 *(this->checkpoint_
->input_sections()),
2727 this->checkpoint_
->input_sections()->size(),
2729 this->convert_input_sections_in_list_to_relaxed_sections(
2732 this->checkpoint_
->input_sections());
2736 // We have not copied the input section list yet. Instead, just
2737 // look at the portion that would be saved.
2738 limit
= this->checkpoint_
->input_sections_size();
2742 // Convert input sections in input_section_list.
2744 this->build_relaxation_map(this->input_sections_
, limit
, &map
);
2745 this->convert_input_sections_in_list_to_relaxed_sections(
2748 &this->input_sections_
);
2750 // Update fast look-up map.
2751 if (this->lookup_maps_
->is_valid())
2752 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2754 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2755 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2756 poris
->shndx(), poris
);
2760 // Update the output section flags based on input section flags.
2763 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags
)
2765 // If we created the section with SHF_ALLOC clear, we set the
2766 // address. If we are now setting the SHF_ALLOC flag, we need to
2768 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0
2769 && (flags
& elfcpp::SHF_ALLOC
) != 0)
2770 this->mark_address_invalid();
2772 this->flags_
|= (flags
2773 & (elfcpp::SHF_WRITE
2775 | elfcpp::SHF_EXECINSTR
));
2777 if ((flags
& elfcpp::SHF_MERGE
) == 0)
2778 this->flags_
&=~ elfcpp::SHF_MERGE
;
2781 if (this->current_data_size_for_child() == 0)
2782 this->flags_
|= elfcpp::SHF_MERGE
;
2785 if ((flags
& elfcpp::SHF_STRINGS
) == 0)
2786 this->flags_
&=~ elfcpp::SHF_STRINGS
;
2789 if (this->current_data_size_for_child() == 0)
2790 this->flags_
|= elfcpp::SHF_STRINGS
;
2794 // Find the merge section into which an input section with index SHNDX in
2795 // OBJECT has been added. Return NULL if none found.
2797 Output_section_data
*
2798 Output_section::find_merge_section(const Relobj
* object
,
2799 unsigned int shndx
) const
2801 if (!this->lookup_maps_
->is_valid())
2802 this->build_lookup_maps();
2803 return this->lookup_maps_
->find_merge_section(object
, shndx
);
2806 // Build the lookup maps for merge and relaxed sections. This is needs
2807 // to be declared as a const methods so that it is callable with a const
2808 // Output_section pointer. The method only updates states of the maps.
2811 Output_section::build_lookup_maps() const
2813 this->lookup_maps_
->clear();
2814 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2815 p
!= this->input_sections_
.end();
2818 if (p
->is_merge_section())
2820 Output_merge_base
* pomb
= p
->output_merge_base();
2821 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
2823 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2824 for (Output_merge_base::Input_sections::const_iterator is
=
2825 pomb
->input_sections_begin();
2826 is
!= pomb
->input_sections_end();
2829 const Const_section_id
& csid
= *is
;
2830 this->lookup_maps_
->add_merge_input_section(csid
.first
,
2835 else if (p
->is_relaxed_input_section())
2837 Output_relaxed_input_section
* poris
= p
->relaxed_input_section();
2838 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2839 poris
->shndx(), poris
);
2844 // Find an relaxed input section corresponding to an input section
2845 // in OBJECT with index SHNDX.
2847 const Output_relaxed_input_section
*
2848 Output_section::find_relaxed_input_section(const Relobj
* object
,
2849 unsigned int shndx
) const
2851 if (!this->lookup_maps_
->is_valid())
2852 this->build_lookup_maps();
2853 return this->lookup_maps_
->find_relaxed_input_section(object
, shndx
);
2856 // Given an address OFFSET relative to the start of input section
2857 // SHNDX in OBJECT, return whether this address is being included in
2858 // the final link. This should only be called if SHNDX in OBJECT has
2859 // a special mapping.
2862 Output_section::is_input_address_mapped(const Relobj
* object
,
2866 // Look at the Output_section_data_maps first.
2867 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2869 posd
= this->find_relaxed_input_section(object
, shndx
);
2873 section_offset_type output_offset
;
2874 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2876 return output_offset
!= -1;
2879 // Fall back to the slow look-up.
2880 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2881 p
!= this->input_sections_
.end();
2884 section_offset_type output_offset
;
2885 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2886 return output_offset
!= -1;
2889 // By default we assume that the address is mapped. This should
2890 // only be called after we have passed all sections to Layout. At
2891 // that point we should know what we are discarding.
2895 // Given an address OFFSET relative to the start of input section
2896 // SHNDX in object OBJECT, return the output offset relative to the
2897 // start of the input section in the output section. This should only
2898 // be called if SHNDX in OBJECT has a special mapping.
2901 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2902 section_offset_type offset
) const
2904 // This can only be called meaningfully when we know the data size
2906 gold_assert(this->is_data_size_valid());
2908 // Look at the Output_section_data_maps first.
2909 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2911 posd
= this->find_relaxed_input_section(object
, shndx
);
2914 section_offset_type output_offset
;
2915 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2917 return output_offset
;
2920 // Fall back to the slow look-up.
2921 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2922 p
!= this->input_sections_
.end();
2925 section_offset_type output_offset
;
2926 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2927 return output_offset
;
2932 // Return the output virtual address of OFFSET relative to the start
2933 // of input section SHNDX in object OBJECT.
2936 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
2939 uint64_t addr
= this->address() + this->first_input_offset_
;
2941 // Look at the Output_section_data_maps first.
2942 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2944 posd
= this->find_relaxed_input_section(object
, shndx
);
2945 if (posd
!= NULL
&& posd
->is_address_valid())
2947 section_offset_type output_offset
;
2948 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2950 return posd
->address() + output_offset
;
2953 // Fall back to the slow look-up.
2954 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2955 p
!= this->input_sections_
.end();
2958 addr
= align_address(addr
, p
->addralign());
2959 section_offset_type output_offset
;
2960 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2962 if (output_offset
== -1)
2964 return addr
+ output_offset
;
2966 addr
+= p
->data_size();
2969 // If we get here, it means that we don't know the mapping for this
2970 // input section. This might happen in principle if
2971 // add_input_section were called before add_output_section_data.
2972 // But it should never actually happen.
2977 // Find the output address of the start of the merged section for
2978 // input section SHNDX in object OBJECT.
2981 Output_section::find_starting_output_address(const Relobj
* object
,
2983 uint64_t* paddr
) const
2985 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2986 // Looking up the merge section map does not always work as we sometimes
2987 // find a merge section without its address set.
2988 uint64_t addr
= this->address() + this->first_input_offset_
;
2989 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2990 p
!= this->input_sections_
.end();
2993 addr
= align_address(addr
, p
->addralign());
2995 // It would be nice if we could use the existing output_offset
2996 // method to get the output offset of input offset 0.
2997 // Unfortunately we don't know for sure that input offset 0 is
2999 if (p
->is_merge_section_for(object
, shndx
))
3005 addr
+= p
->data_size();
3008 // We couldn't find a merge output section for this input section.
3012 // Update the data size of an Output_section.
3015 Output_section::update_data_size()
3017 if (this->input_sections_
.empty())
3020 if (this->must_sort_attached_input_sections()
3021 || this->input_section_order_specified())
3022 this->sort_attached_input_sections();
3024 off_t off
= this->first_input_offset_
;
3025 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3026 p
!= this->input_sections_
.end();
3029 off
= align_address(off
, p
->addralign());
3030 off
+= p
->current_data_size();
3033 this->set_current_data_size_for_child(off
);
3036 // Set the data size of an Output_section. This is where we handle
3037 // setting the addresses of any Output_section_data objects.
3040 Output_section::set_final_data_size()
3044 if (this->input_sections_
.empty())
3045 data_size
= this->current_data_size_for_child();
3048 if (this->must_sort_attached_input_sections()
3049 || this->input_section_order_specified())
3050 this->sort_attached_input_sections();
3052 uint64_t address
= this->address();
3053 off_t startoff
= this->offset();
3054 off_t off
= startoff
+ this->first_input_offset_
;
3055 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3056 p
!= this->input_sections_
.end();
3059 off
= align_address(off
, p
->addralign());
3060 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
3062 off
+= p
->data_size();
3064 data_size
= off
- startoff
;
3067 // For full incremental links, we want to allocate some patch space
3068 // in most sections for subsequent incremental updates.
3069 if (this->is_patch_space_allowed_
&& parameters
->incremental_full())
3071 double pct
= parameters
->options().incremental_patch();
3072 size_t extra
= static_cast<size_t>(data_size
* pct
);
3073 if (this->free_space_fill_
!= NULL
3074 && this->free_space_fill_
->minimum_hole_size() > extra
)
3075 extra
= this->free_space_fill_
->minimum_hole_size();
3076 off_t new_size
= align_address(data_size
+ extra
, this->addralign());
3077 this->patch_space_
= new_size
- data_size
;
3078 gold_debug(DEBUG_INCREMENTAL
,
3079 "set_final_data_size: %08lx + %08lx: section %s",
3080 static_cast<long>(data_size
),
3081 static_cast<long>(this->patch_space_
),
3083 data_size
= new_size
;
3086 this->set_data_size(data_size
);
3089 // Reset the address and file offset.
3092 Output_section::do_reset_address_and_file_offset()
3094 // An unallocated section has no address. Forcing this means that
3095 // we don't need special treatment for symbols defined in debug
3096 // sections. We do the same in the constructor. This does not
3097 // apply to NOLOAD sections though.
3098 if (((this->flags_
& elfcpp::SHF_ALLOC
) == 0) && !this->is_noload_
)
3099 this->set_address(0);
3101 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3102 p
!= this->input_sections_
.end();
3104 p
->reset_address_and_file_offset();
3106 // Remove any patch space that was added in set_final_data_size.
3107 if (this->patch_space_
> 0)
3109 this->set_current_data_size_for_child(this->current_data_size_for_child()
3110 - this->patch_space_
);
3111 this->patch_space_
= 0;
3115 // Return true if address and file offset have the values after reset.
3118 Output_section::do_address_and_file_offset_have_reset_values() const
3120 if (this->is_offset_valid())
3123 // An unallocated section has address 0 after its construction or a reset.
3124 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0)
3125 return this->is_address_valid() && this->address() == 0;
3127 return !this->is_address_valid();
3130 // Set the TLS offset. Called only for SHT_TLS sections.
3133 Output_section::do_set_tls_offset(uint64_t tls_base
)
3135 this->tls_offset_
= this->address() - tls_base
;
3138 // In a few cases we need to sort the input sections attached to an
3139 // output section. This is used to implement the type of constructor
3140 // priority ordering implemented by the GNU linker, in which the
3141 // priority becomes part of the section name and the sections are
3142 // sorted by name. We only do this for an output section if we see an
3143 // attached input section matching ".ctors.*", ".dtors.*",
3144 // ".init_array.*" or ".fini_array.*".
3146 class Output_section::Input_section_sort_entry
3149 Input_section_sort_entry()
3150 : input_section_(), index_(-1U), section_has_name_(false),
3154 Input_section_sort_entry(const Input_section
& input_section
,
3156 bool must_sort_attached_input_sections
)
3157 : input_section_(input_section
), index_(index
),
3158 section_has_name_(input_section
.is_input_section()
3159 || input_section
.is_relaxed_input_section())
3161 if (this->section_has_name_
3162 && must_sort_attached_input_sections
)
3164 // This is only called single-threaded from Layout::finalize,
3165 // so it is OK to lock. Unfortunately we have no way to pass
3167 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
3168 Object
* obj
= (input_section
.is_input_section()
3169 ? input_section
.relobj()
3170 : input_section
.relaxed_input_section()->relobj());
3171 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
3173 // This is a slow operation, which should be cached in
3174 // Layout::layout if this becomes a speed problem.
3175 this->section_name_
= obj
->section_name(input_section
.shndx());
3179 // Return the Input_section.
3180 const Input_section
&
3181 input_section() const
3183 gold_assert(this->index_
!= -1U);
3184 return this->input_section_
;
3187 // The index of this entry in the original list. This is used to
3188 // make the sort stable.
3192 gold_assert(this->index_
!= -1U);
3193 return this->index_
;
3196 // Whether there is a section name.
3198 section_has_name() const
3199 { return this->section_has_name_
; }
3201 // The section name.
3203 section_name() const
3205 gold_assert(this->section_has_name_
);
3206 return this->section_name_
;
3209 // Return true if the section name has a priority. This is assumed
3210 // to be true if it has a dot after the initial dot.
3212 has_priority() const
3214 gold_assert(this->section_has_name_
);
3215 return this->section_name_
.find('.', 1) != std::string::npos
;
3218 // Return the priority. Believe it or not, gcc encodes the priority
3219 // differently for .ctors/.dtors and .init_array/.fini_array
3222 get_priority() const
3224 gold_assert(this->section_has_name_
);
3226 if (is_prefix_of(".ctors.", this->section_name_
.c_str())
3227 || is_prefix_of(".dtors.", this->section_name_
.c_str()))
3229 else if (is_prefix_of(".init_array.", this->section_name_
.c_str())
3230 || is_prefix_of(".fini_array.", this->section_name_
.c_str()))
3235 unsigned long prio
= strtoul((this->section_name_
.c_str()
3236 + (is_ctors
? 7 : 12)),
3241 return 65535 - prio
;
3246 // Return true if this an input file whose base name matches
3247 // FILE_NAME. The base name must have an extension of ".o", and
3248 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3249 // This is to match crtbegin.o as well as crtbeginS.o without
3250 // getting confused by other possibilities. Overall matching the
3251 // file name this way is a dreadful hack, but the GNU linker does it
3252 // in order to better support gcc, and we need to be compatible.
3254 match_file_name(const char* file_name
) const
3255 { return Layout::match_file_name(this->input_section_
.relobj(), file_name
); }
3257 // Returns 1 if THIS should appear before S in section order, -1 if S
3258 // appears before THIS and 0 if they are not comparable.
3260 compare_section_ordering(const Input_section_sort_entry
& s
) const
3262 unsigned int this_secn_index
= this->input_section_
.section_order_index();
3263 unsigned int s_secn_index
= s
.input_section().section_order_index();
3264 if (this_secn_index
> 0 && s_secn_index
> 0)
3266 if (this_secn_index
< s_secn_index
)
3268 else if (this_secn_index
> s_secn_index
)
3275 // The Input_section we are sorting.
3276 Input_section input_section_
;
3277 // The index of this Input_section in the original list.
3278 unsigned int index_
;
3279 // Whether this Input_section has a section name--it won't if this
3280 // is some random Output_section_data.
3281 bool section_has_name_
;
3282 // The section name if there is one.
3283 std::string section_name_
;
3286 // Return true if S1 should come before S2 in the output section.
3289 Output_section::Input_section_sort_compare::operator()(
3290 const Output_section::Input_section_sort_entry
& s1
,
3291 const Output_section::Input_section_sort_entry
& s2
) const
3293 // crtbegin.o must come first.
3294 bool s1_begin
= s1
.match_file_name("crtbegin");
3295 bool s2_begin
= s2
.match_file_name("crtbegin");
3296 if (s1_begin
|| s2_begin
)
3302 return s1
.index() < s2
.index();
3305 // crtend.o must come last.
3306 bool s1_end
= s1
.match_file_name("crtend");
3307 bool s2_end
= s2
.match_file_name("crtend");
3308 if (s1_end
|| s2_end
)
3314 return s1
.index() < s2
.index();
3317 // We sort all the sections with no names to the end.
3318 if (!s1
.section_has_name() || !s2
.section_has_name())
3320 if (s1
.section_has_name())
3322 if (s2
.section_has_name())
3324 return s1
.index() < s2
.index();
3327 // A section with a priority follows a section without a priority.
3328 bool s1_has_priority
= s1
.has_priority();
3329 bool s2_has_priority
= s2
.has_priority();
3330 if (s1_has_priority
&& !s2_has_priority
)
3332 if (!s1_has_priority
&& s2_has_priority
)
3335 // Check if a section order exists for these sections through a section
3336 // ordering file. If sequence_num is 0, an order does not exist.
3337 int sequence_num
= s1
.compare_section_ordering(s2
);
3338 if (sequence_num
!= 0)
3339 return sequence_num
== 1;
3341 // Otherwise we sort by name.
3342 int compare
= s1
.section_name().compare(s2
.section_name());
3346 // Otherwise we keep the input order.
3347 return s1
.index() < s2
.index();
3350 // Return true if S1 should come before S2 in an .init_array or .fini_array
3354 Output_section::Input_section_sort_init_fini_compare::operator()(
3355 const Output_section::Input_section_sort_entry
& s1
,
3356 const Output_section::Input_section_sort_entry
& s2
) const
3358 // We sort all the sections with no names to the end.
3359 if (!s1
.section_has_name() || !s2
.section_has_name())
3361 if (s1
.section_has_name())
3363 if (s2
.section_has_name())
3365 return s1
.index() < s2
.index();
3368 // A section without a priority follows a section with a priority.
3369 // This is the reverse of .ctors and .dtors sections.
3370 bool s1_has_priority
= s1
.has_priority();
3371 bool s2_has_priority
= s2
.has_priority();
3372 if (s1_has_priority
&& !s2_has_priority
)
3374 if (!s1_has_priority
&& s2_has_priority
)
3377 // .ctors and .dtors sections without priority come after
3378 // .init_array and .fini_array sections without priority.
3379 if (!s1_has_priority
3380 && (s1
.section_name() == ".ctors" || s1
.section_name() == ".dtors")
3381 && s1
.section_name() != s2
.section_name())
3383 if (!s2_has_priority
3384 && (s2
.section_name() == ".ctors" || s2
.section_name() == ".dtors")
3385 && s2
.section_name() != s1
.section_name())
3388 // Sort by priority if we can.
3389 if (s1_has_priority
)
3391 unsigned int s1_prio
= s1
.get_priority();
3392 unsigned int s2_prio
= s2
.get_priority();
3393 if (s1_prio
< s2_prio
)
3395 else if (s1_prio
> s2_prio
)
3399 // Check if a section order exists for these sections through a section
3400 // ordering file. If sequence_num is 0, an order does not exist.
3401 int sequence_num
= s1
.compare_section_ordering(s2
);
3402 if (sequence_num
!= 0)
3403 return sequence_num
== 1;
3405 // Otherwise we sort by name.
3406 int compare
= s1
.section_name().compare(s2
.section_name());
3410 // Otherwise we keep the input order.
3411 return s1
.index() < s2
.index();
3414 // Return true if S1 should come before S2. Sections that do not match
3415 // any pattern in the section ordering file are placed ahead of the sections
3416 // that match some pattern.
3419 Output_section::Input_section_sort_section_order_index_compare::operator()(
3420 const Output_section::Input_section_sort_entry
& s1
,
3421 const Output_section::Input_section_sort_entry
& s2
) const
3423 unsigned int s1_secn_index
= s1
.input_section().section_order_index();
3424 unsigned int s2_secn_index
= s2
.input_section().section_order_index();
3426 // Keep input order if section ordering cannot determine order.
3427 if (s1_secn_index
== s2_secn_index
)
3428 return s1
.index() < s2
.index();
3430 return s1_secn_index
< s2_secn_index
;
3433 // This updates the section order index of input sections according to the
3434 // the order specified in the mapping from Section id to order index.
3437 Output_section::update_section_layout(
3438 const Section_layout_order
* order_map
)
3440 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3441 p
!= this->input_sections_
.end();
3444 if (p
->is_input_section()
3445 || p
->is_relaxed_input_section())
3447 Object
* obj
= (p
->is_input_section()
3449 : p
->relaxed_input_section()->relobj());
3450 unsigned int shndx
= p
->shndx();
3451 Section_layout_order::const_iterator it
3452 = order_map
->find(Section_id(obj
, shndx
));
3453 if (it
== order_map
->end())
3455 unsigned int section_order_index
= it
->second
;
3456 if (section_order_index
!= 0)
3458 p
->set_section_order_index(section_order_index
);
3459 this->set_input_section_order_specified();
3465 // Sort the input sections attached to an output section.
3468 Output_section::sort_attached_input_sections()
3470 if (this->attached_input_sections_are_sorted_
)
3473 if (this->checkpoint_
!= NULL
3474 && !this->checkpoint_
->input_sections_saved())
3475 this->checkpoint_
->save_input_sections();
3477 // The only thing we know about an input section is the object and
3478 // the section index. We need the section name. Recomputing this
3479 // is slow but this is an unusual case. If this becomes a speed
3480 // problem we can cache the names as required in Layout::layout.
3482 // We start by building a larger vector holding a copy of each
3483 // Input_section, plus its current index in the list and its name.
3484 std::vector
<Input_section_sort_entry
> sort_list
;
3487 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3488 p
!= this->input_sections_
.end();
3490 sort_list
.push_back(Input_section_sort_entry(*p
, i
,
3491 this->must_sort_attached_input_sections()));
3493 // Sort the input sections.
3494 if (this->must_sort_attached_input_sections())
3496 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3497 || this->type() == elfcpp::SHT_INIT_ARRAY
3498 || this->type() == elfcpp::SHT_FINI_ARRAY
)
3499 std::sort(sort_list
.begin(), sort_list
.end(),
3500 Input_section_sort_init_fini_compare());
3502 std::sort(sort_list
.begin(), sort_list
.end(),
3503 Input_section_sort_compare());
3507 gold_assert(this->input_section_order_specified());
3508 std::sort(sort_list
.begin(), sort_list
.end(),
3509 Input_section_sort_section_order_index_compare());
3512 // Copy the sorted input sections back to our list.
3513 this->input_sections_
.clear();
3514 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
3515 p
!= sort_list
.end();
3517 this->input_sections_
.push_back(p
->input_section());
3520 // Remember that we sorted the input sections, since we might get
3522 this->attached_input_sections_are_sorted_
= true;
3525 // Write the section header to *OSHDR.
3527 template<int size
, bool big_endian
>
3529 Output_section::write_header(const Layout
* layout
,
3530 const Stringpool
* secnamepool
,
3531 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
3533 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
3534 oshdr
->put_sh_type(this->type_
);
3536 elfcpp::Elf_Xword flags
= this->flags_
;
3537 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
3538 flags
|= elfcpp::SHF_INFO_LINK
;
3539 oshdr
->put_sh_flags(flags
);
3541 oshdr
->put_sh_addr(this->address());
3542 oshdr
->put_sh_offset(this->offset());
3543 oshdr
->put_sh_size(this->data_size());
3544 if (this->link_section_
!= NULL
)
3545 oshdr
->put_sh_link(this->link_section_
->out_shndx());
3546 else if (this->should_link_to_symtab_
)
3547 oshdr
->put_sh_link(layout
->symtab_section_shndx());
3548 else if (this->should_link_to_dynsym_
)
3549 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
3551 oshdr
->put_sh_link(this->link_
);
3553 elfcpp::Elf_Word info
;
3554 if (this->info_section_
!= NULL
)
3556 if (this->info_uses_section_index_
)
3557 info
= this->info_section_
->out_shndx();
3559 info
= this->info_section_
->symtab_index();
3561 else if (this->info_symndx_
!= NULL
)
3562 info
= this->info_symndx_
->symtab_index();
3565 oshdr
->put_sh_info(info
);
3567 oshdr
->put_sh_addralign(this->addralign_
);
3568 oshdr
->put_sh_entsize(this->entsize_
);
3571 // Write out the data. For input sections the data is written out by
3572 // Object::relocate, but we have to handle Output_section_data objects
3576 Output_section::do_write(Output_file
* of
)
3578 gold_assert(!this->requires_postprocessing());
3580 // If the target performs relaxation, we delay filler generation until now.
3581 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3583 off_t output_section_file_offset
= this->offset();
3584 for (Fill_list::iterator p
= this->fills_
.begin();
3585 p
!= this->fills_
.end();
3588 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3589 of
->write(output_section_file_offset
+ p
->section_offset(),
3590 fill_data
.data(), fill_data
.size());
3593 off_t off
= this->offset() + this->first_input_offset_
;
3594 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3595 p
!= this->input_sections_
.end();
3598 off_t aligned_off
= align_address(off
, p
->addralign());
3599 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3601 size_t fill_len
= aligned_off
- off
;
3602 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3603 of
->write(off
, fill_data
.data(), fill_data
.size());
3607 off
= aligned_off
+ p
->data_size();
3610 // For incremental links, fill in unused chunks in debug sections
3611 // with dummy compilation unit headers.
3612 if (this->free_space_fill_
!= NULL
)
3614 for (Free_list::Const_iterator p
= this->free_list_
.begin();
3615 p
!= this->free_list_
.end();
3618 off_t off
= p
->start_
;
3619 size_t len
= p
->end_
- off
;
3620 this->free_space_fill_
->write(of
, this->offset() + off
, len
);
3622 if (this->patch_space_
> 0)
3624 off_t off
= this->current_data_size_for_child() - this->patch_space_
;
3625 this->free_space_fill_
->write(of
, this->offset() + off
,
3626 this->patch_space_
);
3631 // If a section requires postprocessing, create the buffer to use.
3634 Output_section::create_postprocessing_buffer()
3636 gold_assert(this->requires_postprocessing());
3638 if (this->postprocessing_buffer_
!= NULL
)
3641 if (!this->input_sections_
.empty())
3643 off_t off
= this->first_input_offset_
;
3644 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3645 p
!= this->input_sections_
.end();
3648 off
= align_address(off
, p
->addralign());
3649 p
->finalize_data_size();
3650 off
+= p
->data_size();
3652 this->set_current_data_size_for_child(off
);
3655 off_t buffer_size
= this->current_data_size_for_child();
3656 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
3659 // Write all the data of an Output_section into the postprocessing
3660 // buffer. This is used for sections which require postprocessing,
3661 // such as compression. Input sections are handled by
3662 // Object::Relocate.
3665 Output_section::write_to_postprocessing_buffer()
3667 gold_assert(this->requires_postprocessing());
3669 // If the target performs relaxation, we delay filler generation until now.
3670 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3672 unsigned char* buffer
= this->postprocessing_buffer();
3673 for (Fill_list::iterator p
= this->fills_
.begin();
3674 p
!= this->fills_
.end();
3677 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3678 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
3682 off_t off
= this->first_input_offset_
;
3683 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3684 p
!= this->input_sections_
.end();
3687 off_t aligned_off
= align_address(off
, p
->addralign());
3688 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3690 size_t fill_len
= aligned_off
- off
;
3691 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3692 memcpy(buffer
+ off
, fill_data
.data(), fill_data
.size());
3695 p
->write_to_buffer(buffer
+ aligned_off
);
3696 off
= aligned_off
+ p
->data_size();
3700 // Get the input sections for linker script processing. We leave
3701 // behind the Output_section_data entries. Note that this may be
3702 // slightly incorrect for merge sections. We will leave them behind,
3703 // but it is possible that the script says that they should follow
3704 // some other input sections, as in:
3705 // .rodata { *(.rodata) *(.rodata.cst*) }
3706 // For that matter, we don't handle this correctly:
3707 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3708 // With luck this will never matter.
3711 Output_section::get_input_sections(
3713 const std::string
& fill
,
3714 std::list
<Input_section
>* input_sections
)
3716 if (this->checkpoint_
!= NULL
3717 && !this->checkpoint_
->input_sections_saved())
3718 this->checkpoint_
->save_input_sections();
3720 // Invalidate fast look-up maps.
3721 this->lookup_maps_
->invalidate();
3723 uint64_t orig_address
= address
;
3725 address
= align_address(address
, this->addralign());
3727 Input_section_list remaining
;
3728 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3729 p
!= this->input_sections_
.end();
3732 if (p
->is_input_section()
3733 || p
->is_relaxed_input_section()
3734 || p
->is_merge_section())
3735 input_sections
->push_back(*p
);
3738 uint64_t aligned_address
= align_address(address
, p
->addralign());
3739 if (aligned_address
!= address
&& !fill
.empty())
3741 section_size_type length
=
3742 convert_to_section_size_type(aligned_address
- address
);
3743 std::string this_fill
;
3744 this_fill
.reserve(length
);
3745 while (this_fill
.length() + fill
.length() <= length
)
3747 if (this_fill
.length() < length
)
3748 this_fill
.append(fill
, 0, length
- this_fill
.length());
3750 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
3751 remaining
.push_back(Input_section(posd
));
3753 address
= aligned_address
;
3755 remaining
.push_back(*p
);
3757 p
->finalize_data_size();
3758 address
+= p
->data_size();
3762 this->input_sections_
.swap(remaining
);
3763 this->first_input_offset_
= 0;
3765 uint64_t data_size
= address
- orig_address
;
3766 this->set_current_data_size_for_child(data_size
);
3770 // Add a script input section. SIS is an Output_section::Input_section,
3771 // which can be either a plain input section or a special input section like
3772 // a relaxed input section. For a special input section, its size must be
3776 Output_section::add_script_input_section(const Input_section
& sis
)
3778 uint64_t data_size
= sis
.data_size();
3779 uint64_t addralign
= sis
.addralign();
3780 if (addralign
> this->addralign_
)
3781 this->addralign_
= addralign
;
3783 off_t offset_in_section
= this->current_data_size_for_child();
3784 off_t aligned_offset_in_section
= align_address(offset_in_section
,
3787 this->set_current_data_size_for_child(aligned_offset_in_section
3790 this->input_sections_
.push_back(sis
);
3792 // Update fast lookup maps if necessary.
3793 if (this->lookup_maps_
->is_valid())
3795 if (sis
.is_merge_section())
3797 Output_merge_base
* pomb
= sis
.output_merge_base();
3798 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
3800 this->lookup_maps_
->add_merge_section(msp
, pomb
);
3801 for (Output_merge_base::Input_sections::const_iterator p
=
3802 pomb
->input_sections_begin();
3803 p
!= pomb
->input_sections_end();
3805 this->lookup_maps_
->add_merge_input_section(p
->first
, p
->second
,
3808 else if (sis
.is_relaxed_input_section())
3810 Output_relaxed_input_section
* poris
= sis
.relaxed_input_section();
3811 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
3812 poris
->shndx(), poris
);
3817 // Save states for relaxation.
3820 Output_section::save_states()
3822 gold_assert(this->checkpoint_
== NULL
);
3823 Checkpoint_output_section
* checkpoint
=
3824 new Checkpoint_output_section(this->addralign_
, this->flags_
,
3825 this->input_sections_
,
3826 this->first_input_offset_
,
3827 this->attached_input_sections_are_sorted_
);
3828 this->checkpoint_
= checkpoint
;
3829 gold_assert(this->fills_
.empty());
3833 Output_section::discard_states()
3835 gold_assert(this->checkpoint_
!= NULL
);
3836 delete this->checkpoint_
;
3837 this->checkpoint_
= NULL
;
3838 gold_assert(this->fills_
.empty());
3840 // Simply invalidate the fast lookup maps since we do not keep
3842 this->lookup_maps_
->invalidate();
3846 Output_section::restore_states()
3848 gold_assert(this->checkpoint_
!= NULL
);
3849 Checkpoint_output_section
* checkpoint
= this->checkpoint_
;
3851 this->addralign_
= checkpoint
->addralign();
3852 this->flags_
= checkpoint
->flags();
3853 this->first_input_offset_
= checkpoint
->first_input_offset();
3855 if (!checkpoint
->input_sections_saved())
3857 // If we have not copied the input sections, just resize it.
3858 size_t old_size
= checkpoint
->input_sections_size();
3859 gold_assert(this->input_sections_
.size() >= old_size
);
3860 this->input_sections_
.resize(old_size
);
3864 // We need to copy the whole list. This is not efficient for
3865 // extremely large output with hundreads of thousands of input
3866 // objects. We may need to re-think how we should pass sections
3868 this->input_sections_
= *checkpoint
->input_sections();
3871 this->attached_input_sections_are_sorted_
=
3872 checkpoint
->attached_input_sections_are_sorted();
3874 // Simply invalidate the fast lookup maps since we do not keep
3876 this->lookup_maps_
->invalidate();
3879 // Update the section offsets of input sections in this. This is required if
3880 // relaxation causes some input sections to change sizes.
3883 Output_section::adjust_section_offsets()
3885 if (!this->section_offsets_need_adjustment_
)
3889 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3890 p
!= this->input_sections_
.end();
3893 off
= align_address(off
, p
->addralign());
3894 if (p
->is_input_section())
3895 p
->relobj()->set_section_offset(p
->shndx(), off
);
3896 off
+= p
->data_size();
3899 this->section_offsets_need_adjustment_
= false;
3902 // Print to the map file.
3905 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
3907 mapfile
->print_output_section(this);
3909 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3910 p
!= this->input_sections_
.end();
3912 p
->print_to_mapfile(mapfile
);
3915 // Print stats for merge sections to stderr.
3918 Output_section::print_merge_stats()
3920 Input_section_list::iterator p
;
3921 for (p
= this->input_sections_
.begin();
3922 p
!= this->input_sections_
.end();
3924 p
->print_merge_stats(this->name_
);
3927 // Set a fixed layout for the section. Used for incremental update links.
3930 Output_section::set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
,
3931 off_t sh_size
, uint64_t sh_addralign
)
3933 this->addralign_
= sh_addralign
;
3934 this->set_current_data_size(sh_size
);
3935 if ((this->flags_
& elfcpp::SHF_ALLOC
) != 0)
3936 this->set_address(sh_addr
);
3937 this->set_file_offset(sh_offset
);
3938 this->finalize_data_size();
3939 this->free_list_
.init(sh_size
, false);
3940 this->has_fixed_layout_
= true;
3943 // Reserve space within the fixed layout for the section. Used for
3944 // incremental update links.
3947 Output_section::reserve(uint64_t sh_offset
, uint64_t sh_size
)
3949 this->free_list_
.remove(sh_offset
, sh_offset
+ sh_size
);
3952 // Allocate space from the free list for the section. Used for
3953 // incremental update links.
3956 Output_section::allocate(off_t len
, uint64_t addralign
)
3958 return this->free_list_
.allocate(len
, addralign
, 0);
3961 // Output segment methods.
3963 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3973 is_max_align_known_(false),
3974 are_addresses_set_(false),
3975 is_large_data_segment_(false)
3977 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3979 if (type
== elfcpp::PT_TLS
)
3980 this->flags_
= elfcpp::PF_R
;
3983 // Add an Output_section to a PT_LOAD Output_segment.
3986 Output_segment::add_output_section_to_load(Layout
* layout
,
3988 elfcpp::Elf_Word seg_flags
)
3990 gold_assert(this->type() == elfcpp::PT_LOAD
);
3991 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
3992 gold_assert(!this->is_max_align_known_
);
3993 gold_assert(os
->is_large_data_section() == this->is_large_data_segment());
3995 this->update_flags_for_output_section(seg_flags
);
3997 // We don't want to change the ordering if we have a linker script
3998 // with a SECTIONS clause.
3999 Output_section_order order
= os
->order();
4000 if (layout
->script_options()->saw_sections_clause())
4001 order
= static_cast<Output_section_order
>(0);
4003 gold_assert(order
!= ORDER_INVALID
);
4005 this->output_lists_
[order
].push_back(os
);
4008 // Add an Output_section to a non-PT_LOAD Output_segment.
4011 Output_segment::add_output_section_to_nonload(Output_section
* os
,
4012 elfcpp::Elf_Word seg_flags
)
4014 gold_assert(this->type() != elfcpp::PT_LOAD
);
4015 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
4016 gold_assert(!this->is_max_align_known_
);
4018 this->update_flags_for_output_section(seg_flags
);
4020 this->output_lists_
[0].push_back(os
);
4023 // Remove an Output_section from this segment. It is an error if it
4027 Output_segment::remove_output_section(Output_section
* os
)
4029 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4031 Output_data_list
* pdl
= &this->output_lists_
[i
];
4032 for (Output_data_list::iterator p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4044 // Add an Output_data (which need not be an Output_section) to the
4045 // start of a segment.
4048 Output_segment::add_initial_output_data(Output_data
* od
)
4050 gold_assert(!this->is_max_align_known_
);
4051 Output_data_list::iterator p
= this->output_lists_
[0].begin();
4052 this->output_lists_
[0].insert(p
, od
);
4055 // Return true if this segment has any sections which hold actual
4056 // data, rather than being a BSS section.
4059 Output_segment::has_any_data_sections() const
4061 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4063 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4064 for (Output_data_list::const_iterator p
= pdl
->begin();
4068 if (!(*p
)->is_section())
4070 if ((*p
)->output_section()->type() != elfcpp::SHT_NOBITS
)
4077 // Return whether the first data section (not counting TLS sections)
4078 // is a relro section.
4081 Output_segment::is_first_section_relro() const
4083 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4085 if (i
== static_cast<int>(ORDER_TLS_DATA
)
4086 || i
== static_cast<int>(ORDER_TLS_BSS
))
4088 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4091 Output_data
* p
= pdl
->front();
4092 return p
->is_section() && p
->output_section()->is_relro();
4098 // Return the maximum alignment of the Output_data in Output_segment.
4101 Output_segment::maximum_alignment()
4103 if (!this->is_max_align_known_
)
4105 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4107 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4108 uint64_t addralign
= Output_segment::maximum_alignment_list(pdl
);
4109 if (addralign
> this->max_align_
)
4110 this->max_align_
= addralign
;
4112 this->is_max_align_known_
= true;
4115 return this->max_align_
;
4118 // Return the maximum alignment of a list of Output_data.
4121 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
4124 for (Output_data_list::const_iterator p
= pdl
->begin();
4128 uint64_t addralign
= (*p
)->addralign();
4129 if (addralign
> ret
)
4135 // Return whether this segment has any dynamic relocs.
4138 Output_segment::has_dynamic_reloc() const
4140 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4141 if (this->has_dynamic_reloc_list(&this->output_lists_
[i
]))
4146 // Return whether this Output_data_list has any dynamic relocs.
4149 Output_segment::has_dynamic_reloc_list(const Output_data_list
* pdl
) const
4151 for (Output_data_list::const_iterator p
= pdl
->begin();
4154 if ((*p
)->has_dynamic_reloc())
4159 // Set the section addresses for an Output_segment. If RESET is true,
4160 // reset the addresses first. ADDR is the address and *POFF is the
4161 // file offset. Set the section indexes starting with *PSHNDX.
4162 // INCREASE_RELRO is the size of the portion of the first non-relro
4163 // section that should be included in the PT_GNU_RELRO segment.
4164 // If this segment has relro sections, and has been aligned for
4165 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4166 // the immediately following segment. Update *HAS_RELRO, *POFF,
4170 Output_segment::set_section_addresses(Layout
* layout
, bool reset
,
4172 unsigned int* increase_relro
,
4175 unsigned int* pshndx
)
4177 gold_assert(this->type_
== elfcpp::PT_LOAD
);
4179 uint64_t last_relro_pad
= 0;
4180 off_t orig_off
= *poff
;
4182 bool in_tls
= false;
4184 // If we have relro sections, we need to pad forward now so that the
4185 // relro sections plus INCREASE_RELRO end on a common page boundary.
4186 if (parameters
->options().relro()
4187 && this->is_first_section_relro()
4188 && (!this->are_addresses_set_
|| reset
))
4190 uint64_t relro_size
= 0;
4192 uint64_t max_align
= 0;
4193 for (int i
= 0; i
<= static_cast<int>(ORDER_RELRO_LAST
); ++i
)
4195 Output_data_list
* pdl
= &this->output_lists_
[i
];
4196 Output_data_list::iterator p
;
4197 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4199 if (!(*p
)->is_section())
4201 uint64_t align
= (*p
)->addralign();
4202 if (align
> max_align
)
4204 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4208 // Align the first non-TLS section to the alignment
4209 // of the TLS segment.
4213 relro_size
= align_address(relro_size
, align
);
4214 // Ignore the size of the .tbss section.
4215 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4216 && (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4218 if ((*p
)->is_address_valid())
4219 relro_size
+= (*p
)->data_size();
4222 // FIXME: This could be faster.
4223 (*p
)->set_address_and_file_offset(addr
+ relro_size
,
4225 relro_size
+= (*p
)->data_size();
4226 (*p
)->reset_address_and_file_offset();
4229 if (p
!= pdl
->end())
4232 relro_size
+= *increase_relro
;
4233 // Pad the total relro size to a multiple of the maximum
4234 // section alignment seen.
4235 uint64_t aligned_size
= align_address(relro_size
, max_align
);
4236 // Note the amount of padding added after the last relro section.
4237 last_relro_pad
= aligned_size
- relro_size
;
4240 uint64_t page_align
= parameters
->target().common_pagesize();
4242 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4243 uint64_t desired_align
= page_align
- (aligned_size
% page_align
);
4244 if (desired_align
< *poff
% page_align
)
4245 *poff
+= page_align
- *poff
% page_align
;
4246 *poff
+= desired_align
- *poff
% page_align
;
4247 addr
+= *poff
- orig_off
;
4251 if (!reset
&& this->are_addresses_set_
)
4253 gold_assert(this->paddr_
== addr
);
4254 addr
= this->vaddr_
;
4258 this->vaddr_
= addr
;
4259 this->paddr_
= addr
;
4260 this->are_addresses_set_
= true;
4265 this->offset_
= orig_off
;
4269 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4271 if (i
== static_cast<int>(ORDER_RELRO_LAST
))
4273 *poff
+= last_relro_pad
;
4274 addr
+= last_relro_pad
;
4275 if (this->output_lists_
[i
].empty())
4277 // If there is nothing in the ORDER_RELRO_LAST list,
4278 // the padding will occur at the end of the relro
4279 // segment, and we need to add it to *INCREASE_RELRO.
4280 *increase_relro
+= last_relro_pad
;
4283 addr
= this->set_section_list_addresses(layout
, reset
,
4284 &this->output_lists_
[i
],
4285 addr
, poff
, pshndx
, &in_tls
);
4286 if (i
< static_cast<int>(ORDER_SMALL_BSS
))
4288 this->filesz_
= *poff
- orig_off
;
4295 // If the last section was a TLS section, align upward to the
4296 // alignment of the TLS segment, so that the overall size of the TLS
4297 // segment is aligned.
4300 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
4301 *poff
= align_address(*poff
, segment_align
);
4304 this->memsz_
= *poff
- orig_off
;
4306 // Ignore the file offset adjustments made by the BSS Output_data
4313 // Set the addresses and file offsets in a list of Output_data
4317 Output_segment::set_section_list_addresses(Layout
* layout
, bool reset
,
4318 Output_data_list
* pdl
,
4319 uint64_t addr
, off_t
* poff
,
4320 unsigned int* pshndx
,
4323 off_t startoff
= *poff
;
4324 // For incremental updates, we may allocate non-fixed sections from
4325 // free space in the file. This keeps track of the high-water mark.
4326 off_t maxoff
= startoff
;
4328 off_t off
= startoff
;
4329 for (Output_data_list::iterator p
= pdl
->begin();
4334 (*p
)->reset_address_and_file_offset();
4336 // When doing an incremental update or when using a linker script,
4337 // the section will most likely already have an address.
4338 if (!(*p
)->is_address_valid())
4340 uint64_t align
= (*p
)->addralign();
4342 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4344 // Give the first TLS section the alignment of the
4345 // entire TLS segment. Otherwise the TLS segment as a
4346 // whole may be misaligned.
4349 Output_segment
* tls_segment
= layout
->tls_segment();
4350 gold_assert(tls_segment
!= NULL
);
4351 uint64_t segment_align
= tls_segment
->maximum_alignment();
4352 gold_assert(segment_align
>= align
);
4353 align
= segment_align
;
4360 // If this is the first section after the TLS segment,
4361 // align it to at least the alignment of the TLS
4362 // segment, so that the size of the overall TLS segment
4366 uint64_t segment_align
=
4367 layout
->tls_segment()->maximum_alignment();
4368 if (segment_align
> align
)
4369 align
= segment_align
;
4375 if (!parameters
->incremental_update())
4377 off
= align_address(off
, align
);
4378 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4382 // Incremental update: allocate file space from free list.
4383 (*p
)->pre_finalize_data_size();
4384 off_t current_size
= (*p
)->current_data_size();
4385 off
= layout
->allocate(current_size
, align
, startoff
);
4388 gold_assert((*p
)->output_section() != NULL
);
4389 gold_fallback(_("out of patch space for section %s; "
4390 "relink with --incremental-full"),
4391 (*p
)->output_section()->name());
4393 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4394 if ((*p
)->data_size() > current_size
)
4396 gold_assert((*p
)->output_section() != NULL
);
4397 gold_fallback(_("%s: section changed size; "
4398 "relink with --incremental-full"),
4399 (*p
)->output_section()->name());
4403 else if (parameters
->incremental_update())
4405 // For incremental updates, use the fixed offset for the
4406 // high-water mark computation.
4407 off
= (*p
)->offset();
4411 // The script may have inserted a skip forward, but it
4412 // better not have moved backward.
4413 if ((*p
)->address() >= addr
+ (off
- startoff
))
4414 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
4417 if (!layout
->script_options()->saw_sections_clause())
4421 Output_section
* os
= (*p
)->output_section();
4423 // Cast to unsigned long long to avoid format warnings.
4424 unsigned long long previous_dot
=
4425 static_cast<unsigned long long>(addr
+ (off
- startoff
));
4426 unsigned long long dot
=
4427 static_cast<unsigned long long>((*p
)->address());
4430 gold_error(_("dot moves backward in linker script "
4431 "from 0x%llx to 0x%llx"), previous_dot
, dot
);
4433 gold_error(_("address of section '%s' moves backward "
4434 "from 0x%llx to 0x%llx"),
4435 os
->name(), previous_dot
, dot
);
4438 (*p
)->set_file_offset(off
);
4439 (*p
)->finalize_data_size();
4442 if (parameters
->incremental_update())
4443 gold_debug(DEBUG_INCREMENTAL
,
4444 "set_section_list_addresses: %08lx %08lx %s",
4445 static_cast<long>(off
),
4446 static_cast<long>((*p
)->data_size()),
4447 ((*p
)->output_section() != NULL
4448 ? (*p
)->output_section()->name() : "(special)"));
4450 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4451 // section. Such a section does not affect the size of a
4453 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4454 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4455 off
+= (*p
)->data_size();
4460 if ((*p
)->is_section())
4462 (*p
)->set_out_shndx(*pshndx
);
4468 return addr
+ (maxoff
- startoff
);
4471 // For a non-PT_LOAD segment, set the offset from the sections, if
4472 // any. Add INCREASE to the file size and the memory size.
4475 Output_segment::set_offset(unsigned int increase
)
4477 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
4479 gold_assert(!this->are_addresses_set_
);
4481 // A non-load section only uses output_lists_[0].
4483 Output_data_list
* pdl
= &this->output_lists_
[0];
4487 gold_assert(increase
== 0);
4490 this->are_addresses_set_
= true;
4492 this->min_p_align_
= 0;
4498 // Find the first and last section by address.
4499 const Output_data
* first
= NULL
;
4500 const Output_data
* last_data
= NULL
;
4501 const Output_data
* last_bss
= NULL
;
4502 for (Output_data_list::const_iterator p
= pdl
->begin();
4507 || (*p
)->address() < first
->address()
4508 || ((*p
)->address() == first
->address()
4509 && (*p
)->data_size() < first
->data_size()))
4511 const Output_data
** plast
;
4512 if ((*p
)->is_section()
4513 && (*p
)->output_section()->type() == elfcpp::SHT_NOBITS
)
4518 || (*p
)->address() > (*plast
)->address()
4519 || ((*p
)->address() == (*plast
)->address()
4520 && (*p
)->data_size() > (*plast
)->data_size()))
4524 this->vaddr_
= first
->address();
4525 this->paddr_
= (first
->has_load_address()
4526 ? first
->load_address()
4528 this->are_addresses_set_
= true;
4529 this->offset_
= first
->offset();
4531 if (last_data
== NULL
)
4534 this->filesz_
= (last_data
->address()
4535 + last_data
->data_size()
4538 const Output_data
* last
= last_bss
!= NULL
? last_bss
: last_data
;
4539 this->memsz_
= (last
->address()
4543 this->filesz_
+= increase
;
4544 this->memsz_
+= increase
;
4546 // If this is a RELRO segment, verify that the segment ends at a
4548 if (this->type_
== elfcpp::PT_GNU_RELRO
)
4550 uint64_t page_align
= parameters
->target().common_pagesize();
4551 uint64_t segment_end
= this->vaddr_
+ this->memsz_
;
4552 if (parameters
->incremental_update())
4554 // The INCREASE_RELRO calculation is bypassed for an incremental
4555 // update, so we need to adjust the segment size manually here.
4556 segment_end
= align_address(segment_end
, page_align
);
4557 this->memsz_
= segment_end
- this->vaddr_
;
4560 gold_assert(segment_end
== align_address(segment_end
, page_align
));
4563 // If this is a TLS segment, align the memory size. The code in
4564 // set_section_list ensures that the section after the TLS segment
4565 // is aligned to give us room.
4566 if (this->type_
== elfcpp::PT_TLS
)
4568 uint64_t segment_align
= this->maximum_alignment();
4569 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
4570 this->memsz_
= align_address(this->memsz_
, segment_align
);
4574 // Set the TLS offsets of the sections in the PT_TLS segment.
4577 Output_segment::set_tls_offsets()
4579 gold_assert(this->type_
== elfcpp::PT_TLS
);
4581 for (Output_data_list::iterator p
= this->output_lists_
[0].begin();
4582 p
!= this->output_lists_
[0].end();
4584 (*p
)->set_tls_offset(this->vaddr_
);
4587 // Return the load address of the first section.
4590 Output_segment::first_section_load_address() const
4592 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4594 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4595 for (Output_data_list::const_iterator p
= pdl
->begin();
4599 if ((*p
)->is_section())
4600 return ((*p
)->has_load_address()
4601 ? (*p
)->load_address()
4608 // Return the number of Output_sections in an Output_segment.
4611 Output_segment::output_section_count() const
4613 unsigned int ret
= 0;
4614 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4615 ret
+= this->output_section_count_list(&this->output_lists_
[i
]);
4619 // Return the number of Output_sections in an Output_data_list.
4622 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
4624 unsigned int count
= 0;
4625 for (Output_data_list::const_iterator p
= pdl
->begin();
4629 if ((*p
)->is_section())
4635 // Return the section attached to the list segment with the lowest
4636 // load address. This is used when handling a PHDRS clause in a
4640 Output_segment::section_with_lowest_load_address() const
4642 Output_section
* found
= NULL
;
4643 uint64_t found_lma
= 0;
4644 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4645 this->lowest_load_address_in_list(&this->output_lists_
[i
], &found
,
4650 // Look through a list for a section with a lower load address.
4653 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
4654 Output_section
** found
,
4655 uint64_t* found_lma
) const
4657 for (Output_data_list::const_iterator p
= pdl
->begin();
4661 if (!(*p
)->is_section())
4663 Output_section
* os
= static_cast<Output_section
*>(*p
);
4664 uint64_t lma
= (os
->has_load_address()
4665 ? os
->load_address()
4667 if (*found
== NULL
|| lma
< *found_lma
)
4675 // Write the segment data into *OPHDR.
4677 template<int size
, bool big_endian
>
4679 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
4681 ophdr
->put_p_type(this->type_
);
4682 ophdr
->put_p_offset(this->offset_
);
4683 ophdr
->put_p_vaddr(this->vaddr_
);
4684 ophdr
->put_p_paddr(this->paddr_
);
4685 ophdr
->put_p_filesz(this->filesz_
);
4686 ophdr
->put_p_memsz(this->memsz_
);
4687 ophdr
->put_p_flags(this->flags_
);
4688 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
4691 // Write the section headers into V.
4693 template<int size
, bool big_endian
>
4695 Output_segment::write_section_headers(const Layout
* layout
,
4696 const Stringpool
* secnamepool
,
4698 unsigned int* pshndx
) const
4700 // Every section that is attached to a segment must be attached to a
4701 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4703 if (this->type_
!= elfcpp::PT_LOAD
)
4706 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4708 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4709 v
= this->write_section_headers_list
<size
, big_endian
>(layout
,
4718 template<int size
, bool big_endian
>
4720 Output_segment::write_section_headers_list(const Layout
* layout
,
4721 const Stringpool
* secnamepool
,
4722 const Output_data_list
* pdl
,
4724 unsigned int* pshndx
) const
4726 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
4727 for (Output_data_list::const_iterator p
= pdl
->begin();
4731 if ((*p
)->is_section())
4733 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
4734 gold_assert(*pshndx
== ps
->out_shndx());
4735 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
4736 ps
->write_header(layout
, secnamepool
, &oshdr
);
4744 // Print the output sections to the map file.
4747 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
4749 if (this->type() != elfcpp::PT_LOAD
)
4751 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4752 this->print_section_list_to_mapfile(mapfile
, &this->output_lists_
[i
]);
4755 // Print an output section list to the map file.
4758 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
4759 const Output_data_list
* pdl
) const
4761 for (Output_data_list::const_iterator p
= pdl
->begin();
4764 (*p
)->print_to_mapfile(mapfile
);
4767 // Output_file methods.
4769 Output_file::Output_file(const char* name
)
4774 map_is_anonymous_(false),
4775 map_is_allocated_(false),
4776 is_temporary_(false)
4780 // Try to open an existing file. Returns false if the file doesn't
4781 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4782 // NULL, open that file as the base for incremental linking, and
4783 // copy its contents to the new output file. This routine can
4784 // be called for incremental updates, in which case WRITABLE should
4785 // be true, or by the incremental-dump utility, in which case
4786 // WRITABLE should be false.
4789 Output_file::open_base_file(const char* base_name
, bool writable
)
4791 // The name "-" means "stdout".
4792 if (strcmp(this->name_
, "-") == 0)
4795 bool use_base_file
= base_name
!= NULL
;
4797 base_name
= this->name_
;
4798 else if (strcmp(base_name
, this->name_
) == 0)
4799 gold_fatal(_("%s: incremental base and output file name are the same"),
4802 // Don't bother opening files with a size of zero.
4804 if (::stat(base_name
, &s
) != 0)
4806 gold_info(_("%s: stat: %s"), base_name
, strerror(errno
));
4811 gold_info(_("%s: incremental base file is empty"), base_name
);
4815 // If we're using a base file, we want to open it read-only.
4819 int oflags
= writable
? O_RDWR
: O_RDONLY
;
4820 int o
= open_descriptor(-1, base_name
, oflags
, 0);
4823 gold_info(_("%s: open: %s"), base_name
, strerror(errno
));
4827 // If the base file and the output file are different, open a
4828 // new output file and read the contents from the base file into
4829 // the newly-mapped region.
4832 this->open(s
.st_size
);
4833 ssize_t bytes_to_read
= s
.st_size
;
4834 unsigned char* p
= this->base_
;
4835 while (bytes_to_read
> 0)
4837 ssize_t len
= ::read(o
, p
, bytes_to_read
);
4840 gold_info(_("%s: read failed: %s"), base_name
, strerror(errno
));
4845 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
4847 static_cast<long long>(s
.st_size
- bytes_to_read
),
4848 static_cast<long long>(s
.st_size
));
4852 bytes_to_read
-= len
;
4859 this->file_size_
= s
.st_size
;
4861 if (!this->map_no_anonymous(writable
))
4863 release_descriptor(o
, true);
4865 this->file_size_
= 0;
4872 // Open the output file.
4875 Output_file::open(off_t file_size
)
4877 this->file_size_
= file_size
;
4879 // Unlink the file first; otherwise the open() may fail if the file
4880 // is busy (e.g. it's an executable that's currently being executed).
4882 // However, the linker may be part of a system where a zero-length
4883 // file is created for it to write to, with tight permissions (gcc
4884 // 2.95 did something like this). Unlinking the file would work
4885 // around those permission controls, so we only unlink if the file
4886 // has a non-zero size. We also unlink only regular files to avoid
4887 // trouble with directories/etc.
4889 // If we fail, continue; this command is merely a best-effort attempt
4890 // to improve the odds for open().
4892 // We let the name "-" mean "stdout"
4893 if (!this->is_temporary_
)
4895 if (strcmp(this->name_
, "-") == 0)
4896 this->o_
= STDOUT_FILENO
;
4900 if (::stat(this->name_
, &s
) == 0
4901 && (S_ISREG (s
.st_mode
) || S_ISLNK (s
.st_mode
)))
4904 ::unlink(this->name_
);
4905 else if (!parameters
->options().relocatable())
4907 // If we don't unlink the existing file, add execute
4908 // permission where read permissions already exist
4909 // and where the umask permits.
4910 int mask
= ::umask(0);
4912 s
.st_mode
|= (s
.st_mode
& 0444) >> 2;
4913 ::chmod(this->name_
, s
.st_mode
& ~mask
);
4917 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
4918 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
4921 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
4929 // Resize the output file.
4932 Output_file::resize(off_t file_size
)
4934 // If the mmap is mapping an anonymous memory buffer, this is easy:
4935 // just mremap to the new size. If it's mapping to a file, we want
4936 // to unmap to flush to the file, then remap after growing the file.
4937 if (this->map_is_anonymous_
)
4940 if (!this->map_is_allocated_
)
4942 base
= ::mremap(this->base_
, this->file_size_
, file_size
,
4944 if (base
== MAP_FAILED
)
4945 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
4949 base
= realloc(this->base_
, file_size
);
4952 if (file_size
> this->file_size_
)
4953 memset(static_cast<char*>(base
) + this->file_size_
, 0,
4954 file_size
- this->file_size_
);
4956 this->base_
= static_cast<unsigned char*>(base
);
4957 this->file_size_
= file_size
;
4962 this->file_size_
= file_size
;
4963 if (!this->map_no_anonymous(true))
4964 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
4968 // Map an anonymous block of memory which will later be written to the
4969 // file. Return whether the map succeeded.
4972 Output_file::map_anonymous()
4974 void* base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
4975 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
4976 if (base
== MAP_FAILED
)
4978 base
= malloc(this->file_size_
);
4981 memset(base
, 0, this->file_size_
);
4982 this->map_is_allocated_
= true;
4984 this->base_
= static_cast<unsigned char*>(base
);
4985 this->map_is_anonymous_
= true;
4989 // Map the file into memory. Return whether the mapping succeeded.
4990 // If WRITABLE is true, map with write access.
4993 Output_file::map_no_anonymous(bool writable
)
4995 const int o
= this->o_
;
4997 // If the output file is not a regular file, don't try to mmap it;
4998 // instead, we'll mmap a block of memory (an anonymous buffer), and
4999 // then later write the buffer to the file.
5001 struct stat statbuf
;
5002 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
5003 || ::fstat(o
, &statbuf
) != 0
5004 || !S_ISREG(statbuf
.st_mode
)
5005 || this->is_temporary_
)
5008 // Ensure that we have disk space available for the file. If we
5009 // don't do this, it is possible that we will call munmap, close,
5010 // and exit with dirty buffers still in the cache with no assigned
5011 // disk blocks. If the disk is out of space at that point, the
5012 // output file will wind up incomplete, but we will have already
5013 // exited. The alternative to fallocate would be to use fdatasync,
5014 // but that would be a more significant performance hit.
5017 int err
= ::posix_fallocate(o
, 0, this->file_size_
);
5019 gold_fatal(_("%s: %s"), this->name_
, strerror(err
));
5022 // Map the file into memory.
5023 int prot
= PROT_READ
;
5026 base
= ::mmap(NULL
, this->file_size_
, prot
, MAP_SHARED
, o
, 0);
5028 // The mmap call might fail because of file system issues: the file
5029 // system might not support mmap at all, or it might not support
5030 // mmap with PROT_WRITE.
5031 if (base
== MAP_FAILED
)
5034 this->map_is_anonymous_
= false;
5035 this->base_
= static_cast<unsigned char*>(base
);
5039 // Map the file into memory.
5044 if (this->map_no_anonymous(true))
5047 // The mmap call might fail because of file system issues: the file
5048 // system might not support mmap at all, or it might not support
5049 // mmap with PROT_WRITE. I'm not sure which errno values we will
5050 // see in all cases, so if the mmap fails for any reason and we
5051 // don't care about file contents, try for an anonymous map.
5052 if (this->map_anonymous())
5055 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5056 this->name_
, static_cast<unsigned long>(this->file_size_
),
5060 // Unmap the file from memory.
5063 Output_file::unmap()
5065 if (this->map_is_anonymous_
)
5067 // We've already written out the data, so there is no reason to
5068 // waste time unmapping or freeing the memory.
5072 if (::munmap(this->base_
, this->file_size_
) < 0)
5073 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
5078 // Close the output file.
5081 Output_file::close()
5083 // If the map isn't file-backed, we need to write it now.
5084 if (this->map_is_anonymous_
&& !this->is_temporary_
)
5086 size_t bytes_to_write
= this->file_size_
;
5088 while (bytes_to_write
> 0)
5090 ssize_t bytes_written
= ::write(this->o_
, this->base_
+ offset
,
5092 if (bytes_written
== 0)
5093 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
5094 else if (bytes_written
< 0)
5095 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
5098 bytes_to_write
-= bytes_written
;
5099 offset
+= bytes_written
;
5105 // We don't close stdout or stderr
5106 if (this->o_
!= STDOUT_FILENO
5107 && this->o_
!= STDERR_FILENO
5108 && !this->is_temporary_
)
5109 if (::close(this->o_
) < 0)
5110 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
5114 // Instantiate the templates we need. We could use the configure
5115 // script to restrict this to only the ones for implemented targets.
5117 #ifdef HAVE_TARGET_32_LITTLE
5120 Output_section::add_input_section
<32, false>(
5122 Sized_relobj_file
<32, false>* object
,
5124 const char* secname
,
5125 const elfcpp::Shdr
<32, false>& shdr
,
5126 unsigned int reloc_shndx
,
5127 bool have_sections_script
);
5130 #ifdef HAVE_TARGET_32_BIG
5133 Output_section::add_input_section
<32, true>(
5135 Sized_relobj_file
<32, true>* object
,
5137 const char* secname
,
5138 const elfcpp::Shdr
<32, true>& shdr
,
5139 unsigned int reloc_shndx
,
5140 bool have_sections_script
);
5143 #ifdef HAVE_TARGET_64_LITTLE
5146 Output_section::add_input_section
<64, false>(
5148 Sized_relobj_file
<64, false>* object
,
5150 const char* secname
,
5151 const elfcpp::Shdr
<64, false>& shdr
,
5152 unsigned int reloc_shndx
,
5153 bool have_sections_script
);
5156 #ifdef HAVE_TARGET_64_BIG
5159 Output_section::add_input_section
<64, true>(
5161 Sized_relobj_file
<64, true>* object
,
5163 const char* secname
,
5164 const elfcpp::Shdr
<64, true>& shdr
,
5165 unsigned int reloc_shndx
,
5166 bool have_sections_script
);
5169 #ifdef HAVE_TARGET_32_LITTLE
5171 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5174 #ifdef HAVE_TARGET_32_BIG
5176 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5179 #ifdef HAVE_TARGET_64_LITTLE
5181 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5184 #ifdef HAVE_TARGET_64_BIG
5186 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5189 #ifdef HAVE_TARGET_32_LITTLE
5191 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5194 #ifdef HAVE_TARGET_32_BIG
5196 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5199 #ifdef HAVE_TARGET_64_LITTLE
5201 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5204 #ifdef HAVE_TARGET_64_BIG
5206 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5209 #ifdef HAVE_TARGET_32_LITTLE
5211 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5214 #ifdef HAVE_TARGET_32_BIG
5216 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5219 #ifdef HAVE_TARGET_64_LITTLE
5221 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5224 #ifdef HAVE_TARGET_64_BIG
5226 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5229 #ifdef HAVE_TARGET_32_LITTLE
5231 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5234 #ifdef HAVE_TARGET_32_BIG
5236 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5239 #ifdef HAVE_TARGET_64_LITTLE
5241 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5244 #ifdef HAVE_TARGET_64_BIG
5246 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5249 #ifdef HAVE_TARGET_32_LITTLE
5251 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5254 #ifdef HAVE_TARGET_32_BIG
5256 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5259 #ifdef HAVE_TARGET_64_LITTLE
5261 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5264 #ifdef HAVE_TARGET_64_BIG
5266 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5269 #ifdef HAVE_TARGET_32_LITTLE
5271 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5274 #ifdef HAVE_TARGET_32_BIG
5276 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5279 #ifdef HAVE_TARGET_64_LITTLE
5281 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5284 #ifdef HAVE_TARGET_64_BIG
5286 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5289 #ifdef HAVE_TARGET_32_LITTLE
5291 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5294 #ifdef HAVE_TARGET_32_BIG
5296 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5299 #ifdef HAVE_TARGET_64_LITTLE
5301 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5304 #ifdef HAVE_TARGET_64_BIG
5306 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5309 #ifdef HAVE_TARGET_32_LITTLE
5311 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5314 #ifdef HAVE_TARGET_32_BIG
5316 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5319 #ifdef HAVE_TARGET_64_LITTLE
5321 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5324 #ifdef HAVE_TARGET_64_BIG
5326 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5329 #ifdef HAVE_TARGET_32_LITTLE
5331 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
5334 #ifdef HAVE_TARGET_32_BIG
5336 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
5339 #ifdef HAVE_TARGET_64_LITTLE
5341 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
5344 #ifdef HAVE_TARGET_64_BIG
5346 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
5349 #ifdef HAVE_TARGET_32_LITTLE
5351 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
5354 #ifdef HAVE_TARGET_32_BIG
5356 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
5359 #ifdef HAVE_TARGET_64_LITTLE
5361 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
5364 #ifdef HAVE_TARGET_64_BIG
5366 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
5369 #ifdef HAVE_TARGET_32_LITTLE
5371 class Output_data_group
<32, false>;
5374 #ifdef HAVE_TARGET_32_BIG
5376 class Output_data_group
<32, true>;
5379 #ifdef HAVE_TARGET_64_LITTLE
5381 class Output_data_group
<64, false>;
5384 #ifdef HAVE_TARGET_64_BIG
5386 class Output_data_group
<64, true>;
5389 #ifdef HAVE_TARGET_32_LITTLE
5391 class Output_data_got
<32, false>;
5394 #ifdef HAVE_TARGET_32_BIG
5396 class Output_data_got
<32, true>;
5399 #ifdef HAVE_TARGET_64_LITTLE
5401 class Output_data_got
<64, false>;
5404 #ifdef HAVE_TARGET_64_BIG
5406 class Output_data_got
<64, true>;
5409 } // End namespace gold.