1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 #include "libiberty.h"
35 #include "parameters.h"
40 #include "descriptors.h"
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
45 # define MAP_ANONYMOUS MAP_ANON
48 #ifndef HAVE_POSIX_FALLOCATE
49 // A dummy, non general, version of posix_fallocate. Here we just set
50 // the file size and hope that there is enough disk space. FIXME: We
51 // could allocate disk space by walking block by block and writing a
52 // zero byte into each block.
54 posix_fallocate(int o
, off_t offset
, off_t len
)
56 return ftruncate(o
, offset
+ len
);
58 #endif // !defined(HAVE_POSIX_FALLOCATE)
63 // Output_data variables.
65 bool Output_data::allocated_sizes_are_fixed
;
67 // Output_data methods.
69 Output_data::~Output_data()
73 // Return the default alignment for the target size.
76 Output_data::default_alignment()
78 return Output_data::default_alignment_for_size(
79 parameters
->target().get_size());
82 // Return the default alignment for a size--32 or 64.
85 Output_data::default_alignment_for_size(int size
)
95 // Output_section_header methods. This currently assumes that the
96 // segment and section lists are complete at construction time.
98 Output_section_headers::Output_section_headers(
100 const Layout::Segment_list
* segment_list
,
101 const Layout::Section_list
* section_list
,
102 const Layout::Section_list
* unattached_section_list
,
103 const Stringpool
* secnamepool
,
104 const Output_section
* shstrtab_section
)
106 segment_list_(segment_list
),
107 section_list_(section_list
),
108 unattached_section_list_(unattached_section_list
),
109 secnamepool_(secnamepool
),
110 shstrtab_section_(shstrtab_section
)
114 // Compute the current data size.
117 Output_section_headers::do_size() const
119 // Count all the sections. Start with 1 for the null section.
121 if (!parameters
->options().relocatable())
123 for (Layout::Segment_list::const_iterator p
=
124 this->segment_list_
->begin();
125 p
!= this->segment_list_
->end();
127 if ((*p
)->type() == elfcpp::PT_LOAD
)
128 count
+= (*p
)->output_section_count();
132 for (Layout::Section_list::const_iterator p
=
133 this->section_list_
->begin();
134 p
!= this->section_list_
->end();
136 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
139 count
+= this->unattached_section_list_
->size();
141 const int size
= parameters
->target().get_size();
144 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
146 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
150 return count
* shdr_size
;
153 // Write out the section headers.
156 Output_section_headers::do_write(Output_file
* of
)
158 switch (parameters
->size_and_endianness())
160 #ifdef HAVE_TARGET_32_LITTLE
161 case Parameters::TARGET_32_LITTLE
:
162 this->do_sized_write
<32, false>(of
);
165 #ifdef HAVE_TARGET_32_BIG
166 case Parameters::TARGET_32_BIG
:
167 this->do_sized_write
<32, true>(of
);
170 #ifdef HAVE_TARGET_64_LITTLE
171 case Parameters::TARGET_64_LITTLE
:
172 this->do_sized_write
<64, false>(of
);
175 #ifdef HAVE_TARGET_64_BIG
176 case Parameters::TARGET_64_BIG
:
177 this->do_sized_write
<64, true>(of
);
185 template<int size
, bool big_endian
>
187 Output_section_headers::do_sized_write(Output_file
* of
)
189 off_t all_shdrs_size
= this->data_size();
190 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
192 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
193 unsigned char* v
= view
;
196 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
197 oshdr
.put_sh_name(0);
198 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
199 oshdr
.put_sh_flags(0);
200 oshdr
.put_sh_addr(0);
201 oshdr
.put_sh_offset(0);
203 size_t section_count
= (this->data_size()
204 / elfcpp::Elf_sizes
<size
>::shdr_size
);
205 if (section_count
< elfcpp::SHN_LORESERVE
)
206 oshdr
.put_sh_size(0);
208 oshdr
.put_sh_size(section_count
);
210 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
211 if (shstrndx
< elfcpp::SHN_LORESERVE
)
212 oshdr
.put_sh_link(0);
214 oshdr
.put_sh_link(shstrndx
);
216 size_t segment_count
= this->segment_list_
->size();
217 oshdr
.put_sh_info(segment_count
>= elfcpp::PN_XNUM
? segment_count
: 0);
219 oshdr
.put_sh_addralign(0);
220 oshdr
.put_sh_entsize(0);
225 unsigned int shndx
= 1;
226 if (!parameters
->options().relocatable())
228 for (Layout::Segment_list::const_iterator p
=
229 this->segment_list_
->begin();
230 p
!= this->segment_list_
->end();
232 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
239 for (Layout::Section_list::const_iterator p
=
240 this->section_list_
->begin();
241 p
!= this->section_list_
->end();
244 // We do unallocated sections below, except that group
245 // sections have to come first.
246 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
247 && (*p
)->type() != elfcpp::SHT_GROUP
)
249 gold_assert(shndx
== (*p
)->out_shndx());
250 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
251 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
257 for (Layout::Section_list::const_iterator p
=
258 this->unattached_section_list_
->begin();
259 p
!= this->unattached_section_list_
->end();
262 // For a relocatable link, we did unallocated group sections
263 // above, since they have to come first.
264 if ((*p
)->type() == elfcpp::SHT_GROUP
265 && parameters
->options().relocatable())
267 gold_assert(shndx
== (*p
)->out_shndx());
268 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
269 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
274 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
277 // Output_segment_header methods.
279 Output_segment_headers::Output_segment_headers(
280 const Layout::Segment_list
& segment_list
)
281 : segment_list_(segment_list
)
283 this->set_current_data_size_for_child(this->do_size());
287 Output_segment_headers::do_write(Output_file
* of
)
289 switch (parameters
->size_and_endianness())
291 #ifdef HAVE_TARGET_32_LITTLE
292 case Parameters::TARGET_32_LITTLE
:
293 this->do_sized_write
<32, false>(of
);
296 #ifdef HAVE_TARGET_32_BIG
297 case Parameters::TARGET_32_BIG
:
298 this->do_sized_write
<32, true>(of
);
301 #ifdef HAVE_TARGET_64_LITTLE
302 case Parameters::TARGET_64_LITTLE
:
303 this->do_sized_write
<64, false>(of
);
306 #ifdef HAVE_TARGET_64_BIG
307 case Parameters::TARGET_64_BIG
:
308 this->do_sized_write
<64, true>(of
);
316 template<int size
, bool big_endian
>
318 Output_segment_headers::do_sized_write(Output_file
* of
)
320 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
321 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
322 gold_assert(all_phdrs_size
== this->data_size());
323 unsigned char* view
= of
->get_output_view(this->offset(),
325 unsigned char* v
= view
;
326 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
327 p
!= this->segment_list_
.end();
330 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
331 (*p
)->write_header(&ophdr
);
335 gold_assert(v
- view
== all_phdrs_size
);
337 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
341 Output_segment_headers::do_size() const
343 const int size
= parameters
->target().get_size();
346 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
348 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
352 return this->segment_list_
.size() * phdr_size
;
355 // Output_file_header methods.
357 Output_file_header::Output_file_header(const Target
* target
,
358 const Symbol_table
* symtab
,
359 const Output_segment_headers
* osh
,
363 segment_header_(osh
),
364 section_header_(NULL
),
368 this->set_data_size(this->do_size());
371 // Set the section table information for a file header.
374 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
375 const Output_section
* shstrtab
)
377 this->section_header_
= shdrs
;
378 this->shstrtab_
= shstrtab
;
381 // Write out the file header.
384 Output_file_header::do_write(Output_file
* of
)
386 gold_assert(this->offset() == 0);
388 switch (parameters
->size_and_endianness())
390 #ifdef HAVE_TARGET_32_LITTLE
391 case Parameters::TARGET_32_LITTLE
:
392 this->do_sized_write
<32, false>(of
);
395 #ifdef HAVE_TARGET_32_BIG
396 case Parameters::TARGET_32_BIG
:
397 this->do_sized_write
<32, true>(of
);
400 #ifdef HAVE_TARGET_64_LITTLE
401 case Parameters::TARGET_64_LITTLE
:
402 this->do_sized_write
<64, false>(of
);
405 #ifdef HAVE_TARGET_64_BIG
406 case Parameters::TARGET_64_BIG
:
407 this->do_sized_write
<64, true>(of
);
415 // Write out the file header with appropriate size and endianess.
417 template<int size
, bool big_endian
>
419 Output_file_header::do_sized_write(Output_file
* of
)
421 gold_assert(this->offset() == 0);
423 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
424 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
425 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
427 unsigned char e_ident
[elfcpp::EI_NIDENT
];
428 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
429 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
430 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
431 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
432 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
434 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
436 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
439 e_ident
[elfcpp::EI_DATA
] = (big_endian
440 ? elfcpp::ELFDATA2MSB
441 : elfcpp::ELFDATA2LSB
);
442 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
443 oehdr
.put_e_ident(e_ident
);
446 if (parameters
->options().relocatable())
447 e_type
= elfcpp::ET_REL
;
448 else if (parameters
->options().output_is_position_independent())
449 e_type
= elfcpp::ET_DYN
;
451 e_type
= elfcpp::ET_EXEC
;
452 oehdr
.put_e_type(e_type
);
454 oehdr
.put_e_machine(this->target_
->machine_code());
455 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
457 oehdr
.put_e_entry(this->entry
<size
>());
459 if (this->segment_header_
== NULL
)
460 oehdr
.put_e_phoff(0);
462 oehdr
.put_e_phoff(this->segment_header_
->offset());
464 oehdr
.put_e_shoff(this->section_header_
->offset());
465 oehdr
.put_e_flags(this->target_
->processor_specific_flags());
466 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
468 if (this->segment_header_
== NULL
)
470 oehdr
.put_e_phentsize(0);
471 oehdr
.put_e_phnum(0);
475 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
476 size_t phnum
= (this->segment_header_
->data_size()
477 / elfcpp::Elf_sizes
<size
>::phdr_size
);
478 if (phnum
> elfcpp::PN_XNUM
)
479 phnum
= elfcpp::PN_XNUM
;
480 oehdr
.put_e_phnum(phnum
);
483 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
484 size_t section_count
= (this->section_header_
->data_size()
485 / elfcpp::Elf_sizes
<size
>::shdr_size
);
487 if (section_count
< elfcpp::SHN_LORESERVE
)
488 oehdr
.put_e_shnum(this->section_header_
->data_size()
489 / elfcpp::Elf_sizes
<size
>::shdr_size
);
491 oehdr
.put_e_shnum(0);
493 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
494 if (shstrndx
< elfcpp::SHN_LORESERVE
)
495 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
497 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
499 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
500 // the e_ident field.
501 parameters
->target().adjust_elf_header(view
, ehdr_size
);
503 of
->write_output_view(0, ehdr_size
, view
);
506 // Return the value to use for the entry address. THIS->ENTRY_ is the
507 // symbol specified on the command line, if any.
510 typename
elfcpp::Elf_types
<size
>::Elf_Addr
511 Output_file_header::entry()
513 const bool should_issue_warning
= (this->entry_
!= NULL
514 && !parameters
->options().relocatable()
515 && !parameters
->options().shared());
517 // FIXME: Need to support target specific entry symbol.
518 const char* entry
= this->entry_
;
522 Symbol
* sym
= this->symtab_
->lookup(entry
);
524 typename Sized_symbol
<size
>::Value_type v
;
527 Sized_symbol
<size
>* ssym
;
528 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
529 if (!ssym
->is_defined() && should_issue_warning
)
530 gold_warning("entry symbol '%s' exists but is not defined", entry
);
535 // We couldn't find the entry symbol. See if we can parse it as
536 // a number. This supports, e.g., -e 0x1000.
538 v
= strtoull(entry
, &endptr
, 0);
541 if (should_issue_warning
)
542 gold_warning("cannot find entry symbol '%s'", entry
);
550 // Compute the current data size.
553 Output_file_header::do_size() const
555 const int size
= parameters
->target().get_size();
557 return elfcpp::Elf_sizes
<32>::ehdr_size
;
559 return elfcpp::Elf_sizes
<64>::ehdr_size
;
564 // Output_data_const methods.
567 Output_data_const::do_write(Output_file
* of
)
569 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
572 // Output_data_const_buffer methods.
575 Output_data_const_buffer::do_write(Output_file
* of
)
577 of
->write(this->offset(), this->p_
, this->data_size());
580 // Output_section_data methods.
582 // Record the output section, and set the entry size and such.
585 Output_section_data::set_output_section(Output_section
* os
)
587 gold_assert(this->output_section_
== NULL
);
588 this->output_section_
= os
;
589 this->do_adjust_output_section(os
);
592 // Return the section index of the output section.
595 Output_section_data::do_out_shndx() const
597 gold_assert(this->output_section_
!= NULL
);
598 return this->output_section_
->out_shndx();
601 // Set the alignment, which means we may need to update the alignment
602 // of the output section.
605 Output_section_data::set_addralign(uint64_t addralign
)
607 this->addralign_
= addralign
;
608 if (this->output_section_
!= NULL
609 && this->output_section_
->addralign() < addralign
)
610 this->output_section_
->set_addralign(addralign
);
613 // Output_data_strtab methods.
615 // Set the final data size.
618 Output_data_strtab::set_final_data_size()
620 this->strtab_
->set_string_offsets();
621 this->set_data_size(this->strtab_
->get_strtab_size());
624 // Write out a string table.
627 Output_data_strtab::do_write(Output_file
* of
)
629 this->strtab_
->write(of
, this->offset());
632 // Output_reloc methods.
634 // A reloc against a global symbol.
636 template<bool dynamic
, int size
, bool big_endian
>
637 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
644 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
645 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
646 is_section_symbol_(false), shndx_(INVALID_CODE
)
648 // this->type_ is a bitfield; make sure TYPE fits.
649 gold_assert(this->type_
== type
);
650 this->u1_
.gsym
= gsym
;
653 this->set_needs_dynsym_index();
656 template<bool dynamic
, int size
, bool big_endian
>
657 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
660 Sized_relobj
<size
, big_endian
>* relobj
,
665 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
666 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
667 is_section_symbol_(false), shndx_(shndx
)
669 gold_assert(shndx
!= INVALID_CODE
);
670 // this->type_ is a bitfield; make sure TYPE fits.
671 gold_assert(this->type_
== type
);
672 this->u1_
.gsym
= gsym
;
673 this->u2_
.relobj
= relobj
;
675 this->set_needs_dynsym_index();
678 // A reloc against a local symbol.
680 template<bool dynamic
, int size
, bool big_endian
>
681 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
682 Sized_relobj
<size
, big_endian
>* relobj
,
683 unsigned int local_sym_index
,
689 bool is_section_symbol
)
690 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
691 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
692 is_section_symbol_(is_section_symbol
), shndx_(INVALID_CODE
)
694 gold_assert(local_sym_index
!= GSYM_CODE
695 && local_sym_index
!= INVALID_CODE
);
696 // this->type_ is a bitfield; make sure TYPE fits.
697 gold_assert(this->type_
== type
);
698 this->u1_
.relobj
= relobj
;
701 this->set_needs_dynsym_index();
704 template<bool dynamic
, int size
, bool big_endian
>
705 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
706 Sized_relobj
<size
, big_endian
>* relobj
,
707 unsigned int local_sym_index
,
713 bool is_section_symbol
)
714 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
715 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
716 is_section_symbol_(is_section_symbol
), shndx_(shndx
)
718 gold_assert(local_sym_index
!= GSYM_CODE
719 && local_sym_index
!= INVALID_CODE
);
720 gold_assert(shndx
!= INVALID_CODE
);
721 // this->type_ is a bitfield; make sure TYPE fits.
722 gold_assert(this->type_
== type
);
723 this->u1_
.relobj
= relobj
;
724 this->u2_
.relobj
= relobj
;
726 this->set_needs_dynsym_index();
729 // A reloc against the STT_SECTION symbol of an output section.
731 template<bool dynamic
, int size
, bool big_endian
>
732 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
737 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
738 is_relative_(false), is_symbolless_(false),
739 is_section_symbol_(true), shndx_(INVALID_CODE
)
741 // this->type_ is a bitfield; make sure TYPE fits.
742 gold_assert(this->type_
== type
);
746 this->set_needs_dynsym_index();
748 os
->set_needs_symtab_index();
751 template<bool dynamic
, int size
, bool big_endian
>
752 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
755 Sized_relobj
<size
, big_endian
>* relobj
,
758 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
759 is_relative_(false), is_symbolless_(false),
760 is_section_symbol_(true), shndx_(shndx
)
762 gold_assert(shndx
!= INVALID_CODE
);
763 // this->type_ is a bitfield; make sure TYPE fits.
764 gold_assert(this->type_
== type
);
766 this->u2_
.relobj
= relobj
;
768 this->set_needs_dynsym_index();
770 os
->set_needs_symtab_index();
773 // An absolute relocation.
775 template<bool dynamic
, int size
, bool big_endian
>
776 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
780 : address_(address
), local_sym_index_(0), type_(type
),
781 is_relative_(false), is_symbolless_(false),
782 is_section_symbol_(false), shndx_(INVALID_CODE
)
784 // this->type_ is a bitfield; make sure TYPE fits.
785 gold_assert(this->type_
== type
);
786 this->u1_
.relobj
= NULL
;
790 template<bool dynamic
, int size
, bool big_endian
>
791 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
793 Sized_relobj
<size
, big_endian
>* relobj
,
796 : address_(address
), local_sym_index_(0), type_(type
),
797 is_relative_(false), is_symbolless_(false),
798 is_section_symbol_(false), shndx_(shndx
)
800 gold_assert(shndx
!= INVALID_CODE
);
801 // this->type_ is a bitfield; make sure TYPE fits.
802 gold_assert(this->type_
== type
);
803 this->u1_
.relobj
= NULL
;
804 this->u2_
.relobj
= relobj
;
807 // A target specific relocation.
809 template<bool dynamic
, int size
, bool big_endian
>
810 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
815 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
816 is_relative_(false), is_symbolless_(false),
817 is_section_symbol_(false), shndx_(INVALID_CODE
)
819 // this->type_ is a bitfield; make sure TYPE fits.
820 gold_assert(this->type_
== type
);
825 template<bool dynamic
, int size
, bool big_endian
>
826 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
829 Sized_relobj
<size
, big_endian
>* relobj
,
832 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
833 is_relative_(false), is_symbolless_(false),
834 is_section_symbol_(false), shndx_(shndx
)
836 gold_assert(shndx
!= INVALID_CODE
);
837 // this->type_ is a bitfield; make sure TYPE fits.
838 gold_assert(this->type_
== type
);
840 this->u2_
.relobj
= relobj
;
843 // Record that we need a dynamic symbol index for this relocation.
845 template<bool dynamic
, int size
, bool big_endian
>
847 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
848 set_needs_dynsym_index()
850 if (this->is_symbolless_
)
852 switch (this->local_sym_index_
)
858 this->u1_
.gsym
->set_needs_dynsym_entry();
862 this->u1_
.os
->set_needs_dynsym_index();
866 // The target must take care of this if necessary.
874 const unsigned int lsi
= this->local_sym_index_
;
875 if (!this->is_section_symbol_
)
876 this->u1_
.relobj
->set_needs_output_dynsym_entry(lsi
);
878 this->u1_
.relobj
->output_section(lsi
)->set_needs_dynsym_index();
884 // Get the symbol index of a relocation.
886 template<bool dynamic
, int size
, bool big_endian
>
888 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
892 if (this->is_symbolless_
)
894 switch (this->local_sym_index_
)
900 if (this->u1_
.gsym
== NULL
)
903 index
= this->u1_
.gsym
->dynsym_index();
905 index
= this->u1_
.gsym
->symtab_index();
910 index
= this->u1_
.os
->dynsym_index();
912 index
= this->u1_
.os
->symtab_index();
916 index
= parameters
->target().reloc_symbol_index(this->u1_
.arg
,
921 // Relocations without symbols use a symbol index of 0.
927 const unsigned int lsi
= this->local_sym_index_
;
928 if (!this->is_section_symbol_
)
931 index
= this->u1_
.relobj
->dynsym_index(lsi
);
933 index
= this->u1_
.relobj
->symtab_index(lsi
);
937 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
938 gold_assert(os
!= NULL
);
940 index
= os
->dynsym_index();
942 index
= os
->symtab_index();
947 gold_assert(index
!= -1U);
951 // For a local section symbol, get the address of the offset ADDEND
952 // within the input section.
954 template<bool dynamic
, int size
, bool big_endian
>
955 typename
elfcpp::Elf_types
<size
>::Elf_Addr
956 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
957 local_section_offset(Addend addend
) const
959 gold_assert(this->local_sym_index_
!= GSYM_CODE
960 && this->local_sym_index_
!= SECTION_CODE
961 && this->local_sym_index_
!= TARGET_CODE
962 && this->local_sym_index_
!= INVALID_CODE
963 && this->local_sym_index_
!= 0
964 && this->is_section_symbol_
);
965 const unsigned int lsi
= this->local_sym_index_
;
966 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
967 gold_assert(os
!= NULL
);
968 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
969 if (offset
!= invalid_address
)
970 return offset
+ addend
;
971 // This is a merge section.
972 offset
= os
->output_address(this->u1_
.relobj
, lsi
, addend
);
973 gold_assert(offset
!= invalid_address
);
977 // Get the output address of a relocation.
979 template<bool dynamic
, int size
, bool big_endian
>
980 typename
elfcpp::Elf_types
<size
>::Elf_Addr
981 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
983 Address address
= this->address_
;
984 if (this->shndx_
!= INVALID_CODE
)
986 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
987 gold_assert(os
!= NULL
);
988 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
989 if (off
!= invalid_address
)
990 address
+= os
->address() + off
;
993 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
995 gold_assert(address
!= invalid_address
);
998 else if (this->u2_
.od
!= NULL
)
999 address
+= this->u2_
.od
->address();
1003 // Write out the offset and info fields of a Rel or Rela relocation
1006 template<bool dynamic
, int size
, bool big_endian
>
1007 template<typename Write_rel
>
1009 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
1010 Write_rel
* wr
) const
1012 wr
->put_r_offset(this->get_address());
1013 unsigned int sym_index
= this->get_symbol_index();
1014 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
1017 // Write out a Rel relocation.
1019 template<bool dynamic
, int size
, bool big_endian
>
1021 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
1022 unsigned char* pov
) const
1024 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
1025 this->write_rel(&orel
);
1028 // Get the value of the symbol referred to by a Rel relocation.
1030 template<bool dynamic
, int size
, bool big_endian
>
1031 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1032 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
1033 Addend addend
) const
1035 if (this->local_sym_index_
== GSYM_CODE
)
1037 const Sized_symbol
<size
>* sym
;
1038 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
1039 return sym
->value() + addend
;
1041 gold_assert(this->local_sym_index_
!= SECTION_CODE
1042 && this->local_sym_index_
!= TARGET_CODE
1043 && this->local_sym_index_
!= INVALID_CODE
1044 && this->local_sym_index_
!= 0
1045 && !this->is_section_symbol_
);
1046 const unsigned int lsi
= this->local_sym_index_
;
1047 const Symbol_value
<size
>* symval
= this->u1_
.relobj
->local_symbol(lsi
);
1048 return symval
->value(this->u1_
.relobj
, addend
);
1051 // Reloc comparison. This function sorts the dynamic relocs for the
1052 // benefit of the dynamic linker. First we sort all relative relocs
1053 // to the front. Among relative relocs, we sort by output address.
1054 // Among non-relative relocs, we sort by symbol index, then by output
1057 template<bool dynamic
, int size
, bool big_endian
>
1059 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1060 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1063 if (this->is_relative_
)
1065 if (!r2
.is_relative_
)
1067 // Otherwise sort by reloc address below.
1069 else if (r2
.is_relative_
)
1073 unsigned int sym1
= this->get_symbol_index();
1074 unsigned int sym2
= r2
.get_symbol_index();
1077 else if (sym1
> sym2
)
1079 // Otherwise sort by reloc address.
1082 section_offset_type addr1
= this->get_address();
1083 section_offset_type addr2
= r2
.get_address();
1086 else if (addr1
> addr2
)
1089 // Final tie breaker, in order to generate the same output on any
1090 // host: reloc type.
1091 unsigned int type1
= this->type_
;
1092 unsigned int type2
= r2
.type_
;
1095 else if (type1
> type2
)
1098 // These relocs appear to be exactly the same.
1102 // Write out a Rela relocation.
1104 template<bool dynamic
, int size
, bool big_endian
>
1106 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
1107 unsigned char* pov
) const
1109 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
1110 this->rel_
.write_rel(&orel
);
1111 Addend addend
= this->addend_
;
1112 if (this->rel_
.is_target_specific())
1113 addend
= parameters
->target().reloc_addend(this->rel_
.target_arg(),
1114 this->rel_
.type(), addend
);
1115 else if (this->rel_
.is_symbolless())
1116 addend
= this->rel_
.symbol_value(addend
);
1117 else if (this->rel_
.is_local_section_symbol())
1118 addend
= this->rel_
.local_section_offset(addend
);
1119 orel
.put_r_addend(addend
);
1122 // Output_data_reloc_base methods.
1124 // Adjust the output section.
1126 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1128 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
1129 ::do_adjust_output_section(Output_section
* os
)
1131 if (sh_type
== elfcpp::SHT_REL
)
1132 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1133 else if (sh_type
== elfcpp::SHT_RELA
)
1134 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1138 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1139 // static link. The backends will generate a dynamic reloc section
1140 // to hold this. In that case we don't want to link to the dynsym
1141 // section, because there isn't one.
1143 os
->set_should_link_to_symtab();
1144 else if (parameters
->doing_static_link())
1147 os
->set_should_link_to_dynsym();
1150 // Write out relocation data.
1152 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1154 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1157 const off_t off
= this->offset();
1158 const off_t oview_size
= this->data_size();
1159 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1161 if (this->sort_relocs())
1163 gold_assert(dynamic
);
1164 std::sort(this->relocs_
.begin(), this->relocs_
.end(),
1165 Sort_relocs_comparison());
1168 unsigned char* pov
= oview
;
1169 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
1170 p
!= this->relocs_
.end();
1177 gold_assert(pov
- oview
== oview_size
);
1179 of
->write_output_view(off
, oview_size
, oview
);
1181 // We no longer need the relocation entries.
1182 this->relocs_
.clear();
1185 // Class Output_relocatable_relocs.
1187 template<int sh_type
, int size
, bool big_endian
>
1189 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1191 this->set_data_size(this->rr_
->output_reloc_count()
1192 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1195 // class Output_data_group.
1197 template<int size
, bool big_endian
>
1198 Output_data_group
<size
, big_endian
>::Output_data_group(
1199 Sized_relobj
<size
, big_endian
>* relobj
,
1200 section_size_type entry_count
,
1201 elfcpp::Elf_Word flags
,
1202 std::vector
<unsigned int>* input_shndxes
)
1203 : Output_section_data(entry_count
* 4, 4, false),
1207 this->input_shndxes_
.swap(*input_shndxes
);
1210 // Write out the section group, which means translating the section
1211 // indexes to apply to the output file.
1213 template<int size
, bool big_endian
>
1215 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1217 const off_t off
= this->offset();
1218 const section_size_type oview_size
=
1219 convert_to_section_size_type(this->data_size());
1220 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1222 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1223 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1226 for (std::vector
<unsigned int>::const_iterator p
=
1227 this->input_shndxes_
.begin();
1228 p
!= this->input_shndxes_
.end();
1231 Output_section
* os
= this->relobj_
->output_section(*p
);
1233 unsigned int output_shndx
;
1235 output_shndx
= os
->out_shndx();
1238 this->relobj_
->error(_("section group retained but "
1239 "group element discarded"));
1243 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1246 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1247 gold_assert(wrote
== oview_size
);
1249 of
->write_output_view(off
, oview_size
, oview
);
1251 // We no longer need this information.
1252 this->input_shndxes_
.clear();
1255 // Output_data_got::Got_entry methods.
1257 // Write out the entry.
1259 template<int size
, bool big_endian
>
1261 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1265 switch (this->local_sym_index_
)
1269 // If the symbol is resolved locally, we need to write out the
1270 // link-time value, which will be relocated dynamically by a
1271 // RELATIVE relocation.
1272 Symbol
* gsym
= this->u_
.gsym
;
1273 if (this->use_plt_offset_
&& gsym
->has_plt_offset())
1274 val
= (parameters
->target().plt_section_for_global(gsym
)->address()
1275 + gsym
->plt_offset());
1278 Sized_symbol
<size
>* sgsym
;
1279 // This cast is a bit ugly. We don't want to put a
1280 // virtual method in Symbol, because we want Symbol to be
1281 // as small as possible.
1282 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1283 val
= sgsym
->value();
1289 val
= this->u_
.constant
;
1294 const Sized_relobj
<size
, big_endian
>* object
= this->u_
.object
;
1295 const unsigned int lsi
= this->local_sym_index_
;
1296 const Symbol_value
<size
>* symval
= object
->local_symbol(lsi
);
1297 if (!this->use_plt_offset_
)
1298 val
= symval
->value(this->u_
.object
, 0);
1301 const Output_data
* plt
=
1302 parameters
->target().plt_section_for_local(object
, lsi
);
1303 val
= plt
->address() + object
->local_plt_offset(lsi
);
1309 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1312 // Output_data_got methods.
1314 // Add an entry for a global symbol to the GOT. This returns true if
1315 // this is a new GOT entry, false if the symbol already had a GOT
1318 template<int size
, bool big_endian
>
1320 Output_data_got
<size
, big_endian
>::add_global(
1322 unsigned int got_type
)
1324 if (gsym
->has_got_offset(got_type
))
1327 this->entries_
.push_back(Got_entry(gsym
, false));
1328 this->set_got_size();
1329 gsym
->set_got_offset(got_type
, this->last_got_offset());
1333 // Like add_global, but use the PLT offset.
1335 template<int size
, bool big_endian
>
1337 Output_data_got
<size
, big_endian
>::add_global_plt(Symbol
* gsym
,
1338 unsigned int got_type
)
1340 if (gsym
->has_got_offset(got_type
))
1343 this->entries_
.push_back(Got_entry(gsym
, true));
1344 this->set_got_size();
1345 gsym
->set_got_offset(got_type
, this->last_got_offset());
1349 // Add an entry for a global symbol to the GOT, and add a dynamic
1350 // relocation of type R_TYPE for the GOT entry.
1352 template<int size
, bool big_endian
>
1354 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1356 unsigned int got_type
,
1358 unsigned int r_type
)
1360 if (gsym
->has_got_offset(got_type
))
1363 this->entries_
.push_back(Got_entry());
1364 this->set_got_size();
1365 unsigned int got_offset
= this->last_got_offset();
1366 gsym
->set_got_offset(got_type
, got_offset
);
1367 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1370 template<int size
, bool big_endian
>
1372 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1374 unsigned int got_type
,
1376 unsigned int r_type
)
1378 if (gsym
->has_got_offset(got_type
))
1381 this->entries_
.push_back(Got_entry());
1382 this->set_got_size();
1383 unsigned int got_offset
= this->last_got_offset();
1384 gsym
->set_got_offset(got_type
, got_offset
);
1385 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1388 // Add a pair of entries for a global symbol to the GOT, and add
1389 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1390 // If R_TYPE_2 == 0, add the second entry with no relocation.
1391 template<int size
, bool big_endian
>
1393 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1395 unsigned int got_type
,
1397 unsigned int r_type_1
,
1398 unsigned int r_type_2
)
1400 if (gsym
->has_got_offset(got_type
))
1403 this->entries_
.push_back(Got_entry());
1404 unsigned int got_offset
= this->last_got_offset();
1405 gsym
->set_got_offset(got_type
, got_offset
);
1406 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1408 this->entries_
.push_back(Got_entry());
1411 got_offset
= this->last_got_offset();
1412 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
);
1415 this->set_got_size();
1418 template<int size
, bool big_endian
>
1420 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1422 unsigned int got_type
,
1424 unsigned int r_type_1
,
1425 unsigned int r_type_2
)
1427 if (gsym
->has_got_offset(got_type
))
1430 this->entries_
.push_back(Got_entry());
1431 unsigned int got_offset
= this->last_got_offset();
1432 gsym
->set_got_offset(got_type
, got_offset
);
1433 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1435 this->entries_
.push_back(Got_entry());
1438 got_offset
= this->last_got_offset();
1439 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
, 0);
1442 this->set_got_size();
1445 // Add an entry for a local symbol to the GOT. This returns true if
1446 // this is a new GOT entry, false if the symbol already has a GOT
1449 template<int size
, bool big_endian
>
1451 Output_data_got
<size
, big_endian
>::add_local(
1452 Sized_relobj
<size
, big_endian
>* object
,
1453 unsigned int symndx
,
1454 unsigned int got_type
)
1456 if (object
->local_has_got_offset(symndx
, got_type
))
1459 this->entries_
.push_back(Got_entry(object
, symndx
, false));
1460 this->set_got_size();
1461 object
->set_local_got_offset(symndx
, got_type
, this->last_got_offset());
1465 // Like add_local, but use the PLT offset.
1467 template<int size
, bool big_endian
>
1469 Output_data_got
<size
, big_endian
>::add_local_plt(
1470 Sized_relobj
<size
, big_endian
>* object
,
1471 unsigned int symndx
,
1472 unsigned int got_type
)
1474 if (object
->local_has_got_offset(symndx
, got_type
))
1477 this->entries_
.push_back(Got_entry(object
, symndx
, true));
1478 this->set_got_size();
1479 object
->set_local_got_offset(symndx
, got_type
, this->last_got_offset());
1483 // Add an entry for a local symbol to the GOT, and add a dynamic
1484 // relocation of type R_TYPE for the GOT entry.
1486 template<int size
, bool big_endian
>
1488 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1489 Sized_relobj
<size
, big_endian
>* object
,
1490 unsigned int symndx
,
1491 unsigned int got_type
,
1493 unsigned int r_type
)
1495 if (object
->local_has_got_offset(symndx
, got_type
))
1498 this->entries_
.push_back(Got_entry());
1499 this->set_got_size();
1500 unsigned int got_offset
= this->last_got_offset();
1501 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1502 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1505 template<int size
, bool big_endian
>
1507 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1508 Sized_relobj
<size
, big_endian
>* object
,
1509 unsigned int symndx
,
1510 unsigned int got_type
,
1512 unsigned int r_type
)
1514 if (object
->local_has_got_offset(symndx
, got_type
))
1517 this->entries_
.push_back(Got_entry());
1518 this->set_got_size();
1519 unsigned int got_offset
= this->last_got_offset();
1520 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1521 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1524 // Add a pair of entries for a local symbol to the GOT, and add
1525 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1526 // If R_TYPE_2 == 0, add the second entry with no relocation.
1527 template<int size
, bool big_endian
>
1529 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1530 Sized_relobj
<size
, big_endian
>* object
,
1531 unsigned int symndx
,
1533 unsigned int got_type
,
1535 unsigned int r_type_1
,
1536 unsigned int r_type_2
)
1538 if (object
->local_has_got_offset(symndx
, got_type
))
1541 this->entries_
.push_back(Got_entry());
1542 unsigned int got_offset
= this->last_got_offset();
1543 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1544 Output_section
* os
= object
->output_section(shndx
);
1545 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1547 this->entries_
.push_back(Got_entry(object
, symndx
, false));
1550 got_offset
= this->last_got_offset();
1551 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
);
1554 this->set_got_size();
1557 template<int size
, bool big_endian
>
1559 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1560 Sized_relobj
<size
, big_endian
>* object
,
1561 unsigned int symndx
,
1563 unsigned int got_type
,
1565 unsigned int r_type_1
,
1566 unsigned int r_type_2
)
1568 if (object
->local_has_got_offset(symndx
, got_type
))
1571 this->entries_
.push_back(Got_entry());
1572 unsigned int got_offset
= this->last_got_offset();
1573 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1574 Output_section
* os
= object
->output_section(shndx
);
1575 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1577 this->entries_
.push_back(Got_entry(object
, symndx
, false));
1580 got_offset
= this->last_got_offset();
1581 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
, 0);
1584 this->set_got_size();
1587 // Write out the GOT.
1589 template<int size
, bool big_endian
>
1591 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1593 const int add
= size
/ 8;
1595 const off_t off
= this->offset();
1596 const off_t oview_size
= this->data_size();
1597 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1599 unsigned char* pov
= oview
;
1600 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1601 p
!= this->entries_
.end();
1608 gold_assert(pov
- oview
== oview_size
);
1610 of
->write_output_view(off
, oview_size
, oview
);
1612 // We no longer need the GOT entries.
1613 this->entries_
.clear();
1616 // Output_data_dynamic::Dynamic_entry methods.
1618 // Write out the entry.
1620 template<int size
, bool big_endian
>
1622 Output_data_dynamic::Dynamic_entry::write(
1624 const Stringpool
* pool
) const
1626 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1627 switch (this->offset_
)
1629 case DYNAMIC_NUMBER
:
1633 case DYNAMIC_SECTION_SIZE
:
1634 val
= this->u_
.od
->data_size();
1635 if (this->od2
!= NULL
)
1636 val
+= this->od2
->data_size();
1639 case DYNAMIC_SYMBOL
:
1641 const Sized_symbol
<size
>* s
=
1642 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1647 case DYNAMIC_STRING
:
1648 val
= pool
->get_offset(this->u_
.str
);
1652 val
= this->u_
.od
->address() + this->offset_
;
1656 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1657 dw
.put_d_tag(this->tag_
);
1661 // Output_data_dynamic methods.
1663 // Adjust the output section to set the entry size.
1666 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1668 if (parameters
->target().get_size() == 32)
1669 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1670 else if (parameters
->target().get_size() == 64)
1671 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1676 // Set the final data size.
1679 Output_data_dynamic::set_final_data_size()
1681 // Add the terminating entry if it hasn't been added.
1682 // Because of relaxation, we can run this multiple times.
1683 if (this->entries_
.empty() || this->entries_
.back().tag() != elfcpp::DT_NULL
)
1685 int extra
= parameters
->options().spare_dynamic_tags();
1686 for (int i
= 0; i
< extra
; ++i
)
1687 this->add_constant(elfcpp::DT_NULL
, 0);
1688 this->add_constant(elfcpp::DT_NULL
, 0);
1692 if (parameters
->target().get_size() == 32)
1693 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1694 else if (parameters
->target().get_size() == 64)
1695 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1698 this->set_data_size(this->entries_
.size() * dyn_size
);
1701 // Write out the dynamic entries.
1704 Output_data_dynamic::do_write(Output_file
* of
)
1706 switch (parameters
->size_and_endianness())
1708 #ifdef HAVE_TARGET_32_LITTLE
1709 case Parameters::TARGET_32_LITTLE
:
1710 this->sized_write
<32, false>(of
);
1713 #ifdef HAVE_TARGET_32_BIG
1714 case Parameters::TARGET_32_BIG
:
1715 this->sized_write
<32, true>(of
);
1718 #ifdef HAVE_TARGET_64_LITTLE
1719 case Parameters::TARGET_64_LITTLE
:
1720 this->sized_write
<64, false>(of
);
1723 #ifdef HAVE_TARGET_64_BIG
1724 case Parameters::TARGET_64_BIG
:
1725 this->sized_write
<64, true>(of
);
1733 template<int size
, bool big_endian
>
1735 Output_data_dynamic::sized_write(Output_file
* of
)
1737 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1739 const off_t offset
= this->offset();
1740 const off_t oview_size
= this->data_size();
1741 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1743 unsigned char* pov
= oview
;
1744 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1745 p
!= this->entries_
.end();
1748 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1752 gold_assert(pov
- oview
== oview_size
);
1754 of
->write_output_view(offset
, oview_size
, oview
);
1756 // We no longer need the dynamic entries.
1757 this->entries_
.clear();
1760 // Class Output_symtab_xindex.
1763 Output_symtab_xindex::do_write(Output_file
* of
)
1765 const off_t offset
= this->offset();
1766 const off_t oview_size
= this->data_size();
1767 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1769 memset(oview
, 0, oview_size
);
1771 if (parameters
->target().is_big_endian())
1772 this->endian_do_write
<true>(oview
);
1774 this->endian_do_write
<false>(oview
);
1776 of
->write_output_view(offset
, oview_size
, oview
);
1778 // We no longer need the data.
1779 this->entries_
.clear();
1782 template<bool big_endian
>
1784 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1786 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1787 p
!= this->entries_
.end();
1790 unsigned int symndx
= p
->first
;
1791 gold_assert(symndx
* 4 < this->data_size());
1792 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ symndx
* 4, p
->second
);
1796 // Output_section::Input_section methods.
1798 // Return the current data size. For an input section we store the size here.
1799 // For an Output_section_data, we have to ask it for the size.
1802 Output_section::Input_section::current_data_size() const
1804 if (this->is_input_section())
1805 return this->u1_
.data_size
;
1808 this->u2_
.posd
->pre_finalize_data_size();
1809 return this->u2_
.posd
->current_data_size();
1813 // Return the data size. For an input section we store the size here.
1814 // For an Output_section_data, we have to ask it for the size.
1817 Output_section::Input_section::data_size() const
1819 if (this->is_input_section())
1820 return this->u1_
.data_size
;
1822 return this->u2_
.posd
->data_size();
1825 // Return the object for an input section.
1828 Output_section::Input_section::relobj() const
1830 if (this->is_input_section())
1831 return this->u2_
.object
;
1832 else if (this->is_merge_section())
1834 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
1835 return this->u2_
.pomb
->first_relobj();
1837 else if (this->is_relaxed_input_section())
1838 return this->u2_
.poris
->relobj();
1843 // Return the input section index for an input section.
1846 Output_section::Input_section::shndx() const
1848 if (this->is_input_section())
1849 return this->shndx_
;
1850 else if (this->is_merge_section())
1852 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
1853 return this->u2_
.pomb
->first_shndx();
1855 else if (this->is_relaxed_input_section())
1856 return this->u2_
.poris
->shndx();
1861 // Set the address and file offset.
1864 Output_section::Input_section::set_address_and_file_offset(
1867 off_t section_file_offset
)
1869 if (this->is_input_section())
1870 this->u2_
.object
->set_section_offset(this->shndx_
,
1871 file_offset
- section_file_offset
);
1873 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1876 // Reset the address and file offset.
1879 Output_section::Input_section::reset_address_and_file_offset()
1881 if (!this->is_input_section())
1882 this->u2_
.posd
->reset_address_and_file_offset();
1885 // Finalize the data size.
1888 Output_section::Input_section::finalize_data_size()
1890 if (!this->is_input_section())
1891 this->u2_
.posd
->finalize_data_size();
1894 // Try to turn an input offset into an output offset. We want to
1895 // return the output offset relative to the start of this
1896 // Input_section in the output section.
1899 Output_section::Input_section::output_offset(
1900 const Relobj
* object
,
1902 section_offset_type offset
,
1903 section_offset_type
* poutput
) const
1905 if (!this->is_input_section())
1906 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1909 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1916 // Return whether this is the merge section for the input section
1920 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1921 unsigned int shndx
) const
1923 if (this->is_input_section())
1925 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1928 // Write out the data. We don't have to do anything for an input
1929 // section--they are handled via Object::relocate--but this is where
1930 // we write out the data for an Output_section_data.
1933 Output_section::Input_section::write(Output_file
* of
)
1935 if (!this->is_input_section())
1936 this->u2_
.posd
->write(of
);
1939 // Write the data to a buffer. As for write(), we don't have to do
1940 // anything for an input section.
1943 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1945 if (!this->is_input_section())
1946 this->u2_
.posd
->write_to_buffer(buffer
);
1949 // Print to a map file.
1952 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
1954 switch (this->shndx_
)
1956 case OUTPUT_SECTION_CODE
:
1957 case MERGE_DATA_SECTION_CODE
:
1958 case MERGE_STRING_SECTION_CODE
:
1959 this->u2_
.posd
->print_to_mapfile(mapfile
);
1962 case RELAXED_INPUT_SECTION_CODE
:
1964 Output_relaxed_input_section
* relaxed_section
=
1965 this->relaxed_input_section();
1966 mapfile
->print_input_section(relaxed_section
->relobj(),
1967 relaxed_section
->shndx());
1971 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
1976 // Output_section methods.
1978 // Construct an Output_section. NAME will point into a Stringpool.
1980 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1981 elfcpp::Elf_Xword flags
)
1986 link_section_(NULL
),
1988 info_section_(NULL
),
1993 order_(ORDER_INVALID
),
1998 first_input_offset_(0),
2000 postprocessing_buffer_(NULL
),
2001 needs_symtab_index_(false),
2002 needs_dynsym_index_(false),
2003 should_link_to_symtab_(false),
2004 should_link_to_dynsym_(false),
2005 after_input_sections_(false),
2006 requires_postprocessing_(false),
2007 found_in_sections_clause_(false),
2008 has_load_address_(false),
2009 info_uses_section_index_(false),
2010 input_section_order_specified_(false),
2011 may_sort_attached_input_sections_(false),
2012 must_sort_attached_input_sections_(false),
2013 attached_input_sections_are_sorted_(false),
2015 is_small_section_(false),
2016 is_large_section_(false),
2017 generate_code_fills_at_write_(false),
2018 is_entsize_zero_(false),
2019 section_offsets_need_adjustment_(false),
2021 always_keeps_input_sections_(false),
2022 has_fixed_layout_(false),
2025 lookup_maps_(new Output_section_lookup_maps
),
2028 // An unallocated section has no address. Forcing this means that
2029 // we don't need special treatment for symbols defined in debug
2031 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
2032 this->set_address(0);
2035 Output_section::~Output_section()
2037 delete this->checkpoint_
;
2040 // Set the entry size.
2043 Output_section::set_entsize(uint64_t v
)
2045 if (this->is_entsize_zero_
)
2047 else if (this->entsize_
== 0)
2049 else if (this->entsize_
!= v
)
2052 this->is_entsize_zero_
= 1;
2056 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2057 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2058 // relocation section which applies to this section, or 0 if none, or
2059 // -1U if more than one. Return the offset of the input section
2060 // within the output section. Return -1 if the input section will
2061 // receive special handling. In the normal case we don't always keep
2062 // track of input sections for an Output_section. Instead, each
2063 // Object keeps track of the Output_section for each of its input
2064 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2065 // track of input sections here; this is used when SECTIONS appears in
2068 template<int size
, bool big_endian
>
2070 Output_section::add_input_section(Layout
* layout
,
2071 Sized_relobj
<size
, big_endian
>* object
,
2073 const char* secname
,
2074 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2075 unsigned int reloc_shndx
,
2076 bool have_sections_script
)
2078 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
2079 if ((addralign
& (addralign
- 1)) != 0)
2081 object
->error(_("invalid alignment %lu for section \"%s\""),
2082 static_cast<unsigned long>(addralign
), secname
);
2086 if (addralign
> this->addralign_
)
2087 this->addralign_
= addralign
;
2089 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
2090 uint64_t entsize
= shdr
.get_sh_entsize();
2092 // .debug_str is a mergeable string section, but is not always so
2093 // marked by compilers. Mark manually here so we can optimize.
2094 if (strcmp(secname
, ".debug_str") == 0)
2096 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
2100 this->update_flags_for_input_section(sh_flags
);
2101 this->set_entsize(entsize
);
2103 // If this is a SHF_MERGE section, we pass all the input sections to
2104 // a Output_data_merge. We don't try to handle relocations for such
2105 // a section. We don't try to handle empty merge sections--they
2106 // mess up the mappings, and are useless anyhow.
2107 // FIXME: Need to handle merge sections during incremental update.
2108 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
2110 && shdr
.get_sh_size() > 0
2111 && !parameters
->incremental())
2113 // Keep information about merged input sections for rebuilding fast
2114 // lookup maps if we have sections-script or we do relaxation.
2115 bool keeps_input_sections
= (this->always_keeps_input_sections_
2116 || have_sections_script
2117 || parameters
->target().may_relax());
2119 if (this->add_merge_input_section(object
, shndx
, sh_flags
, entsize
,
2120 addralign
, keeps_input_sections
))
2122 // Tell the relocation routines that they need to call the
2123 // output_offset method to determine the final address.
2128 section_size_type input_section_size
= shdr
.get_sh_size();
2129 section_size_type uncompressed_size
;
2130 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
2131 input_section_size
= uncompressed_size
;
2133 off_t offset_in_section
;
2134 off_t aligned_offset_in_section
;
2135 if (this->has_fixed_layout())
2137 // For incremental updates, find a chunk of unused space in the section.
2138 offset_in_section
= this->free_list_
.allocate(input_section_size
,
2140 if (offset_in_section
== -1)
2141 gold_fatal(_("out of patch space; relink with --incremental-full"));
2142 aligned_offset_in_section
= offset_in_section
;
2146 offset_in_section
= this->current_data_size_for_child();
2147 aligned_offset_in_section
= align_address(offset_in_section
,
2149 this->set_current_data_size_for_child(aligned_offset_in_section
2150 + input_section_size
);
2153 // Determine if we want to delay code-fill generation until the output
2154 // section is written. When the target is relaxing, we want to delay fill
2155 // generating to avoid adjusting them during relaxation. Also, if we are
2156 // sorting input sections we must delay fill generation.
2157 if (!this->generate_code_fills_at_write_
2158 && !have_sections_script
2159 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2160 && parameters
->target().has_code_fill()
2161 && (parameters
->target().may_relax()
2162 || parameters
->options().section_ordering_file()))
2164 gold_assert(this->fills_
.empty());
2165 this->generate_code_fills_at_write_
= true;
2168 if (aligned_offset_in_section
> offset_in_section
2169 && !this->generate_code_fills_at_write_
2170 && !have_sections_script
2171 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2172 && parameters
->target().has_code_fill())
2174 // We need to add some fill data. Using fill_list_ when
2175 // possible is an optimization, since we will often have fill
2176 // sections without input sections.
2177 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
2178 if (this->input_sections_
.empty())
2179 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
2182 std::string
fill_data(parameters
->target().code_fill(fill_len
));
2183 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
2184 this->input_sections_
.push_back(Input_section(odc
));
2188 // We need to keep track of this section if we are already keeping
2189 // track of sections, or if we are relaxing. Also, if this is a
2190 // section which requires sorting, or which may require sorting in
2191 // the future, we keep track of the sections. If the
2192 // --section-ordering-file option is used to specify the order of
2193 // sections, we need to keep track of sections.
2194 if (this->always_keeps_input_sections_
2195 || have_sections_script
2196 || !this->input_sections_
.empty()
2197 || this->may_sort_attached_input_sections()
2198 || this->must_sort_attached_input_sections()
2199 || parameters
->options().user_set_Map()
2200 || parameters
->target().may_relax()
2201 || parameters
->options().section_ordering_file())
2203 Input_section
isecn(object
, shndx
, input_section_size
, addralign
);
2204 if (parameters
->options().section_ordering_file())
2206 unsigned int section_order_index
=
2207 layout
->find_section_order_index(std::string(secname
));
2208 if (section_order_index
!= 0)
2210 isecn
.set_section_order_index(section_order_index
);
2211 this->set_input_section_order_specified();
2214 if (this->has_fixed_layout())
2216 // For incremental updates, finalize the address and offset now.
2217 uint64_t addr
= this->address();
2218 isecn
.set_address_and_file_offset(addr
+ aligned_offset_in_section
,
2219 aligned_offset_in_section
,
2222 this->input_sections_
.push_back(isecn
);
2225 return aligned_offset_in_section
;
2228 // Add arbitrary data to an output section.
2231 Output_section::add_output_section_data(Output_section_data
* posd
)
2233 Input_section
inp(posd
);
2234 this->add_output_section_data(&inp
);
2236 if (posd
->is_data_size_valid())
2238 off_t offset_in_section
;
2239 if (this->has_fixed_layout())
2241 // For incremental updates, find a chunk of unused space.
2242 offset_in_section
= this->free_list_
.allocate(posd
->data_size(),
2243 posd
->addralign(), 0);
2244 if (offset_in_section
== -1)
2245 gold_fatal(_("out of patch space; relink with --incremental-full"));
2246 // Finalize the address and offset now.
2247 uint64_t addr
= this->address();
2248 off_t offset
= this->offset();
2249 posd
->set_address_and_file_offset(addr
+ offset_in_section
,
2250 offset
+ offset_in_section
);
2254 offset_in_section
= this->current_data_size_for_child();
2255 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2257 this->set_current_data_size_for_child(aligned_offset_in_section
2258 + posd
->data_size());
2261 else if (this->has_fixed_layout())
2263 // For incremental updates, arrange for the data to have a fixed layout.
2264 // This will mean that additions to the data must be allocated from
2265 // free space within the containing output section.
2266 uint64_t addr
= this->address();
2267 posd
->set_address(addr
);
2268 posd
->set_file_offset(0);
2269 // FIXME: Mark *POSD as part of a fixed-layout section.
2273 // Add a relaxed input section.
2276 Output_section::add_relaxed_input_section(Layout
* layout
,
2277 Output_relaxed_input_section
* poris
,
2278 const std::string
& name
)
2280 Input_section
inp(poris
);
2282 // If the --section-ordering-file option is used to specify the order of
2283 // sections, we need to keep track of sections.
2284 if (parameters
->options().section_ordering_file())
2286 unsigned int section_order_index
=
2287 layout
->find_section_order_index(name
);
2288 if (section_order_index
!= 0)
2290 inp
.set_section_order_index(section_order_index
);
2291 this->set_input_section_order_specified();
2295 this->add_output_section_data(&inp
);
2296 if (this->lookup_maps_
->is_valid())
2297 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2298 poris
->shndx(), poris
);
2300 // For a relaxed section, we use the current data size. Linker scripts
2301 // get all the input sections, including relaxed one from an output
2302 // section and add them back to them same output section to compute the
2303 // output section size. If we do not account for sizes of relaxed input
2304 // sections, an output section would be incorrectly sized.
2305 off_t offset_in_section
= this->current_data_size_for_child();
2306 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2307 poris
->addralign());
2308 this->set_current_data_size_for_child(aligned_offset_in_section
2309 + poris
->current_data_size());
2312 // Add arbitrary data to an output section by Input_section.
2315 Output_section::add_output_section_data(Input_section
* inp
)
2317 if (this->input_sections_
.empty())
2318 this->first_input_offset_
= this->current_data_size_for_child();
2320 this->input_sections_
.push_back(*inp
);
2322 uint64_t addralign
= inp
->addralign();
2323 if (addralign
> this->addralign_
)
2324 this->addralign_
= addralign
;
2326 inp
->set_output_section(this);
2329 // Add a merge section to an output section.
2332 Output_section::add_output_merge_section(Output_section_data
* posd
,
2333 bool is_string
, uint64_t entsize
)
2335 Input_section
inp(posd
, is_string
, entsize
);
2336 this->add_output_section_data(&inp
);
2339 // Add an input section to a SHF_MERGE section.
2342 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
2343 uint64_t flags
, uint64_t entsize
,
2345 bool keeps_input_sections
)
2347 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
2349 // We only merge strings if the alignment is not more than the
2350 // character size. This could be handled, but it's unusual.
2351 if (is_string
&& addralign
> entsize
)
2354 // We cannot restore merged input section states.
2355 gold_assert(this->checkpoint_
== NULL
);
2357 // Look up merge sections by required properties.
2358 // Currently, we only invalidate the lookup maps in script processing
2359 // and relaxation. We should not have done either when we reach here.
2360 // So we assume that the lookup maps are valid to simply code.
2361 gold_assert(this->lookup_maps_
->is_valid());
2362 Merge_section_properties
msp(is_string
, entsize
, addralign
);
2363 Output_merge_base
* pomb
= this->lookup_maps_
->find_merge_section(msp
);
2364 bool is_new
= false;
2367 gold_assert(pomb
->is_string() == is_string
2368 && pomb
->entsize() == entsize
2369 && pomb
->addralign() == addralign
);
2373 // Create a new Output_merge_data or Output_merge_string_data.
2375 pomb
= new Output_merge_data(entsize
, addralign
);
2381 pomb
= new Output_merge_string
<char>(addralign
);
2384 pomb
= new Output_merge_string
<uint16_t>(addralign
);
2387 pomb
= new Output_merge_string
<uint32_t>(addralign
);
2393 // If we need to do script processing or relaxation, we need to keep
2394 // the original input sections to rebuild the fast lookup maps.
2395 if (keeps_input_sections
)
2396 pomb
->set_keeps_input_sections();
2400 if (pomb
->add_input_section(object
, shndx
))
2402 // Add new merge section to this output section and link merge
2403 // section properties to new merge section in map.
2406 this->add_output_merge_section(pomb
, is_string
, entsize
);
2407 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2410 // Add input section to new merge section and link input section to new
2411 // merge section in map.
2412 this->lookup_maps_
->add_merge_input_section(object
, shndx
, pomb
);
2417 // If add_input_section failed, delete new merge section to avoid
2418 // exporting empty merge sections in Output_section::get_input_section.
2425 // Build a relaxation map to speed up relaxation of existing input sections.
2426 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2429 Output_section::build_relaxation_map(
2430 const Input_section_list
& input_sections
,
2432 Relaxation_map
* relaxation_map
) const
2434 for (size_t i
= 0; i
< limit
; ++i
)
2436 const Input_section
& is(input_sections
[i
]);
2437 if (is
.is_input_section() || is
.is_relaxed_input_section())
2439 Section_id
sid(is
.relobj(), is
.shndx());
2440 (*relaxation_map
)[sid
] = i
;
2445 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2446 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2447 // indices of INPUT_SECTIONS.
2450 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2451 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
2452 const Relaxation_map
& map
,
2453 Input_section_list
* input_sections
)
2455 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2457 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2458 Section_id
sid(poris
->relobj(), poris
->shndx());
2459 Relaxation_map::const_iterator p
= map
.find(sid
);
2460 gold_assert(p
!= map
.end());
2461 gold_assert((*input_sections
)[p
->second
].is_input_section());
2463 // Remember section order index of original input section
2464 // if it is set. Copy it to the relaxed input section.
2466 (*input_sections
)[p
->second
].section_order_index();
2467 (*input_sections
)[p
->second
] = Input_section(poris
);
2468 (*input_sections
)[p
->second
].set_section_order_index(soi
);
2472 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2473 // is a vector of pointers to Output_relaxed_input_section or its derived
2474 // classes. The relaxed sections must correspond to existing input sections.
2477 Output_section::convert_input_sections_to_relaxed_sections(
2478 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
)
2480 gold_assert(parameters
->target().may_relax());
2482 // We want to make sure that restore_states does not undo the effect of
2483 // this. If there is no checkpoint active, just search the current
2484 // input section list and replace the sections there. If there is
2485 // a checkpoint, also replace the sections there.
2487 // By default, we look at the whole list.
2488 size_t limit
= this->input_sections_
.size();
2490 if (this->checkpoint_
!= NULL
)
2492 // Replace input sections with relaxed input section in the saved
2493 // copy of the input section list.
2494 if (this->checkpoint_
->input_sections_saved())
2497 this->build_relaxation_map(
2498 *(this->checkpoint_
->input_sections()),
2499 this->checkpoint_
->input_sections()->size(),
2501 this->convert_input_sections_in_list_to_relaxed_sections(
2504 this->checkpoint_
->input_sections());
2508 // We have not copied the input section list yet. Instead, just
2509 // look at the portion that would be saved.
2510 limit
= this->checkpoint_
->input_sections_size();
2514 // Convert input sections in input_section_list.
2516 this->build_relaxation_map(this->input_sections_
, limit
, &map
);
2517 this->convert_input_sections_in_list_to_relaxed_sections(
2520 &this->input_sections_
);
2522 // Update fast look-up map.
2523 if (this->lookup_maps_
->is_valid())
2524 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2526 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2527 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2528 poris
->shndx(), poris
);
2532 // Update the output section flags based on input section flags.
2535 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags
)
2537 // If we created the section with SHF_ALLOC clear, we set the
2538 // address. If we are now setting the SHF_ALLOC flag, we need to
2540 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0
2541 && (flags
& elfcpp::SHF_ALLOC
) != 0)
2542 this->mark_address_invalid();
2544 this->flags_
|= (flags
2545 & (elfcpp::SHF_WRITE
2547 | elfcpp::SHF_EXECINSTR
));
2549 if ((flags
& elfcpp::SHF_MERGE
) == 0)
2550 this->flags_
&=~ elfcpp::SHF_MERGE
;
2553 if (this->current_data_size_for_child() == 0)
2554 this->flags_
|= elfcpp::SHF_MERGE
;
2557 if ((flags
& elfcpp::SHF_STRINGS
) == 0)
2558 this->flags_
&=~ elfcpp::SHF_STRINGS
;
2561 if (this->current_data_size_for_child() == 0)
2562 this->flags_
|= elfcpp::SHF_STRINGS
;
2566 // Find the merge section into which an input section with index SHNDX in
2567 // OBJECT has been added. Return NULL if none found.
2569 Output_section_data
*
2570 Output_section::find_merge_section(const Relobj
* object
,
2571 unsigned int shndx
) const
2573 if (!this->lookup_maps_
->is_valid())
2574 this->build_lookup_maps();
2575 return this->lookup_maps_
->find_merge_section(object
, shndx
);
2578 // Build the lookup maps for merge and relaxed sections. This is needs
2579 // to be declared as a const methods so that it is callable with a const
2580 // Output_section pointer. The method only updates states of the maps.
2583 Output_section::build_lookup_maps() const
2585 this->lookup_maps_
->clear();
2586 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2587 p
!= this->input_sections_
.end();
2590 if (p
->is_merge_section())
2592 Output_merge_base
* pomb
= p
->output_merge_base();
2593 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
2595 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2596 for (Output_merge_base::Input_sections::const_iterator is
=
2597 pomb
->input_sections_begin();
2598 is
!= pomb
->input_sections_end();
2601 const Const_section_id
& csid
= *is
;
2602 this->lookup_maps_
->add_merge_input_section(csid
.first
,
2607 else if (p
->is_relaxed_input_section())
2609 Output_relaxed_input_section
* poris
= p
->relaxed_input_section();
2610 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2611 poris
->shndx(), poris
);
2616 // Find an relaxed input section corresponding to an input section
2617 // in OBJECT with index SHNDX.
2619 const Output_relaxed_input_section
*
2620 Output_section::find_relaxed_input_section(const Relobj
* object
,
2621 unsigned int shndx
) const
2623 if (!this->lookup_maps_
->is_valid())
2624 this->build_lookup_maps();
2625 return this->lookup_maps_
->find_relaxed_input_section(object
, shndx
);
2628 // Given an address OFFSET relative to the start of input section
2629 // SHNDX in OBJECT, return whether this address is being included in
2630 // the final link. This should only be called if SHNDX in OBJECT has
2631 // a special mapping.
2634 Output_section::is_input_address_mapped(const Relobj
* object
,
2638 // Look at the Output_section_data_maps first.
2639 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2641 posd
= this->find_relaxed_input_section(object
, shndx
);
2645 section_offset_type output_offset
;
2646 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2648 return output_offset
!= -1;
2651 // Fall back to the slow look-up.
2652 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2653 p
!= this->input_sections_
.end();
2656 section_offset_type output_offset
;
2657 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2658 return output_offset
!= -1;
2661 // By default we assume that the address is mapped. This should
2662 // only be called after we have passed all sections to Layout. At
2663 // that point we should know what we are discarding.
2667 // Given an address OFFSET relative to the start of input section
2668 // SHNDX in object OBJECT, return the output offset relative to the
2669 // start of the input section in the output section. This should only
2670 // be called if SHNDX in OBJECT has a special mapping.
2673 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2674 section_offset_type offset
) const
2676 // This can only be called meaningfully when we know the data size
2678 gold_assert(this->is_data_size_valid());
2680 // Look at the Output_section_data_maps first.
2681 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2683 posd
= this->find_relaxed_input_section(object
, shndx
);
2686 section_offset_type output_offset
;
2687 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2689 return output_offset
;
2692 // Fall back to the slow look-up.
2693 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2694 p
!= this->input_sections_
.end();
2697 section_offset_type output_offset
;
2698 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2699 return output_offset
;
2704 // Return the output virtual address of OFFSET relative to the start
2705 // of input section SHNDX in object OBJECT.
2708 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
2711 uint64_t addr
= this->address() + this->first_input_offset_
;
2713 // Look at the Output_section_data_maps first.
2714 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2716 posd
= this->find_relaxed_input_section(object
, shndx
);
2717 if (posd
!= NULL
&& posd
->is_address_valid())
2719 section_offset_type output_offset
;
2720 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2722 return posd
->address() + output_offset
;
2725 // Fall back to the slow look-up.
2726 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2727 p
!= this->input_sections_
.end();
2730 addr
= align_address(addr
, p
->addralign());
2731 section_offset_type output_offset
;
2732 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2734 if (output_offset
== -1)
2736 return addr
+ output_offset
;
2738 addr
+= p
->data_size();
2741 // If we get here, it means that we don't know the mapping for this
2742 // input section. This might happen in principle if
2743 // add_input_section were called before add_output_section_data.
2744 // But it should never actually happen.
2749 // Find the output address of the start of the merged section for
2750 // input section SHNDX in object OBJECT.
2753 Output_section::find_starting_output_address(const Relobj
* object
,
2755 uint64_t* paddr
) const
2757 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2758 // Looking up the merge section map does not always work as we sometimes
2759 // find a merge section without its address set.
2760 uint64_t addr
= this->address() + this->first_input_offset_
;
2761 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2762 p
!= this->input_sections_
.end();
2765 addr
= align_address(addr
, p
->addralign());
2767 // It would be nice if we could use the existing output_offset
2768 // method to get the output offset of input offset 0.
2769 // Unfortunately we don't know for sure that input offset 0 is
2771 if (p
->is_merge_section_for(object
, shndx
))
2777 addr
+= p
->data_size();
2780 // We couldn't find a merge output section for this input section.
2784 // Update the data size of an Output_section.
2787 Output_section::update_data_size()
2789 if (this->input_sections_
.empty())
2792 if (this->must_sort_attached_input_sections()
2793 || this->input_section_order_specified())
2794 this->sort_attached_input_sections();
2796 off_t off
= this->first_input_offset_
;
2797 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2798 p
!= this->input_sections_
.end();
2801 off
= align_address(off
, p
->addralign());
2802 off
+= p
->current_data_size();
2805 this->set_current_data_size_for_child(off
);
2808 // Set the data size of an Output_section. This is where we handle
2809 // setting the addresses of any Output_section_data objects.
2812 Output_section::set_final_data_size()
2814 if (this->input_sections_
.empty())
2816 this->set_data_size(this->current_data_size_for_child());
2820 if (this->must_sort_attached_input_sections()
2821 || this->input_section_order_specified())
2822 this->sort_attached_input_sections();
2824 uint64_t address
= this->address();
2825 off_t startoff
= this->offset();
2826 off_t off
= startoff
+ this->first_input_offset_
;
2827 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2828 p
!= this->input_sections_
.end();
2831 off
= align_address(off
, p
->addralign());
2832 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
2834 off
+= p
->data_size();
2837 this->set_data_size(off
- startoff
);
2840 // Reset the address and file offset.
2843 Output_section::do_reset_address_and_file_offset()
2845 // An unallocated section has no address. Forcing this means that
2846 // we don't need special treatment for symbols defined in debug
2847 // sections. We do the same in the constructor. This does not
2848 // apply to NOLOAD sections though.
2849 if (((this->flags_
& elfcpp::SHF_ALLOC
) == 0) && !this->is_noload_
)
2850 this->set_address(0);
2852 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2853 p
!= this->input_sections_
.end();
2855 p
->reset_address_and_file_offset();
2858 // Return true if address and file offset have the values after reset.
2861 Output_section::do_address_and_file_offset_have_reset_values() const
2863 if (this->is_offset_valid())
2866 // An unallocated section has address 0 after its construction or a reset.
2867 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0)
2868 return this->is_address_valid() && this->address() == 0;
2870 return !this->is_address_valid();
2873 // Set the TLS offset. Called only for SHT_TLS sections.
2876 Output_section::do_set_tls_offset(uint64_t tls_base
)
2878 this->tls_offset_
= this->address() - tls_base
;
2881 // In a few cases we need to sort the input sections attached to an
2882 // output section. This is used to implement the type of constructor
2883 // priority ordering implemented by the GNU linker, in which the
2884 // priority becomes part of the section name and the sections are
2885 // sorted by name. We only do this for an output section if we see an
2886 // attached input section matching ".ctor.*", ".dtor.*",
2887 // ".init_array.*" or ".fini_array.*".
2889 class Output_section::Input_section_sort_entry
2892 Input_section_sort_entry()
2893 : input_section_(), index_(-1U), section_has_name_(false),
2897 Input_section_sort_entry(const Input_section
& input_section
,
2899 bool must_sort_attached_input_sections
)
2900 : input_section_(input_section
), index_(index
),
2901 section_has_name_(input_section
.is_input_section()
2902 || input_section
.is_relaxed_input_section())
2904 if (this->section_has_name_
2905 && must_sort_attached_input_sections
)
2907 // This is only called single-threaded from Layout::finalize,
2908 // so it is OK to lock. Unfortunately we have no way to pass
2910 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
2911 Object
* obj
= (input_section
.is_input_section()
2912 ? input_section
.relobj()
2913 : input_section
.relaxed_input_section()->relobj());
2914 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
2916 // This is a slow operation, which should be cached in
2917 // Layout::layout if this becomes a speed problem.
2918 this->section_name_
= obj
->section_name(input_section
.shndx());
2922 // Return the Input_section.
2923 const Input_section
&
2924 input_section() const
2926 gold_assert(this->index_
!= -1U);
2927 return this->input_section_
;
2930 // The index of this entry in the original list. This is used to
2931 // make the sort stable.
2935 gold_assert(this->index_
!= -1U);
2936 return this->index_
;
2939 // Whether there is a section name.
2941 section_has_name() const
2942 { return this->section_has_name_
; }
2944 // The section name.
2946 section_name() const
2948 gold_assert(this->section_has_name_
);
2949 return this->section_name_
;
2952 // Return true if the section name has a priority. This is assumed
2953 // to be true if it has a dot after the initial dot.
2955 has_priority() const
2957 gold_assert(this->section_has_name_
);
2958 return this->section_name_
.find('.', 1) != std::string::npos
;
2961 // Return true if this an input file whose base name matches
2962 // FILE_NAME. The base name must have an extension of ".o", and
2963 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2964 // This is to match crtbegin.o as well as crtbeginS.o without
2965 // getting confused by other possibilities. Overall matching the
2966 // file name this way is a dreadful hack, but the GNU linker does it
2967 // in order to better support gcc, and we need to be compatible.
2969 match_file_name(const char* match_file_name
) const
2971 const std::string
& file_name(this->input_section_
.relobj()->name());
2972 const char* base_name
= lbasename(file_name
.c_str());
2973 size_t match_len
= strlen(match_file_name
);
2974 if (strncmp(base_name
, match_file_name
, match_len
) != 0)
2976 size_t base_len
= strlen(base_name
);
2977 if (base_len
!= match_len
+ 2 && base_len
!= match_len
+ 3)
2979 return memcmp(base_name
+ base_len
- 2, ".o", 2) == 0;
2982 // Returns 1 if THIS should appear before S in section order, -1 if S
2983 // appears before THIS and 0 if they are not comparable.
2985 compare_section_ordering(const Input_section_sort_entry
& s
) const
2987 unsigned int this_secn_index
= this->input_section_
.section_order_index();
2988 unsigned int s_secn_index
= s
.input_section().section_order_index();
2989 if (this_secn_index
> 0 && s_secn_index
> 0)
2991 if (this_secn_index
< s_secn_index
)
2993 else if (this_secn_index
> s_secn_index
)
3000 // The Input_section we are sorting.
3001 Input_section input_section_
;
3002 // The index of this Input_section in the original list.
3003 unsigned int index_
;
3004 // Whether this Input_section has a section name--it won't if this
3005 // is some random Output_section_data.
3006 bool section_has_name_
;
3007 // The section name if there is one.
3008 std::string section_name_
;
3011 // Return true if S1 should come before S2 in the output section.
3014 Output_section::Input_section_sort_compare::operator()(
3015 const Output_section::Input_section_sort_entry
& s1
,
3016 const Output_section::Input_section_sort_entry
& s2
) const
3018 // crtbegin.o must come first.
3019 bool s1_begin
= s1
.match_file_name("crtbegin");
3020 bool s2_begin
= s2
.match_file_name("crtbegin");
3021 if (s1_begin
|| s2_begin
)
3027 return s1
.index() < s2
.index();
3030 // crtend.o must come last.
3031 bool s1_end
= s1
.match_file_name("crtend");
3032 bool s2_end
= s2
.match_file_name("crtend");
3033 if (s1_end
|| s2_end
)
3039 return s1
.index() < s2
.index();
3042 // We sort all the sections with no names to the end.
3043 if (!s1
.section_has_name() || !s2
.section_has_name())
3045 if (s1
.section_has_name())
3047 if (s2
.section_has_name())
3049 return s1
.index() < s2
.index();
3052 // A section with a priority follows a section without a priority.
3053 bool s1_has_priority
= s1
.has_priority();
3054 bool s2_has_priority
= s2
.has_priority();
3055 if (s1_has_priority
&& !s2_has_priority
)
3057 if (!s1_has_priority
&& s2_has_priority
)
3060 // Check if a section order exists for these sections through a section
3061 // ordering file. If sequence_num is 0, an order does not exist.
3062 int sequence_num
= s1
.compare_section_ordering(s2
);
3063 if (sequence_num
!= 0)
3064 return sequence_num
== 1;
3066 // Otherwise we sort by name.
3067 int compare
= s1
.section_name().compare(s2
.section_name());
3071 // Otherwise we keep the input order.
3072 return s1
.index() < s2
.index();
3075 // Return true if S1 should come before S2 in an .init_array or .fini_array
3079 Output_section::Input_section_sort_init_fini_compare::operator()(
3080 const Output_section::Input_section_sort_entry
& s1
,
3081 const Output_section::Input_section_sort_entry
& s2
) const
3083 // We sort all the sections with no names to the end.
3084 if (!s1
.section_has_name() || !s2
.section_has_name())
3086 if (s1
.section_has_name())
3088 if (s2
.section_has_name())
3090 return s1
.index() < s2
.index();
3093 // A section without a priority follows a section with a priority.
3094 // This is the reverse of .ctors and .dtors sections.
3095 bool s1_has_priority
= s1
.has_priority();
3096 bool s2_has_priority
= s2
.has_priority();
3097 if (s1_has_priority
&& !s2_has_priority
)
3099 if (!s1_has_priority
&& s2_has_priority
)
3102 // Check if a section order exists for these sections through a section
3103 // ordering file. If sequence_num is 0, an order does not exist.
3104 int sequence_num
= s1
.compare_section_ordering(s2
);
3105 if (sequence_num
!= 0)
3106 return sequence_num
== 1;
3108 // Otherwise we sort by name.
3109 int compare
= s1
.section_name().compare(s2
.section_name());
3113 // Otherwise we keep the input order.
3114 return s1
.index() < s2
.index();
3117 // Return true if S1 should come before S2. Sections that do not match
3118 // any pattern in the section ordering file are placed ahead of the sections
3119 // that match some pattern.
3122 Output_section::Input_section_sort_section_order_index_compare::operator()(
3123 const Output_section::Input_section_sort_entry
& s1
,
3124 const Output_section::Input_section_sort_entry
& s2
) const
3126 unsigned int s1_secn_index
= s1
.input_section().section_order_index();
3127 unsigned int s2_secn_index
= s2
.input_section().section_order_index();
3129 // Keep input order if section ordering cannot determine order.
3130 if (s1_secn_index
== s2_secn_index
)
3131 return s1
.index() < s2
.index();
3133 return s1_secn_index
< s2_secn_index
;
3136 // Sort the input sections attached to an output section.
3139 Output_section::sort_attached_input_sections()
3141 if (this->attached_input_sections_are_sorted_
)
3144 if (this->checkpoint_
!= NULL
3145 && !this->checkpoint_
->input_sections_saved())
3146 this->checkpoint_
->save_input_sections();
3148 // The only thing we know about an input section is the object and
3149 // the section index. We need the section name. Recomputing this
3150 // is slow but this is an unusual case. If this becomes a speed
3151 // problem we can cache the names as required in Layout::layout.
3153 // We start by building a larger vector holding a copy of each
3154 // Input_section, plus its current index in the list and its name.
3155 std::vector
<Input_section_sort_entry
> sort_list
;
3158 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3159 p
!= this->input_sections_
.end();
3161 sort_list
.push_back(Input_section_sort_entry(*p
, i
,
3162 this->must_sort_attached_input_sections()));
3164 // Sort the input sections.
3165 if (this->must_sort_attached_input_sections())
3167 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3168 || this->type() == elfcpp::SHT_INIT_ARRAY
3169 || this->type() == elfcpp::SHT_FINI_ARRAY
)
3170 std::sort(sort_list
.begin(), sort_list
.end(),
3171 Input_section_sort_init_fini_compare());
3173 std::sort(sort_list
.begin(), sort_list
.end(),
3174 Input_section_sort_compare());
3178 gold_assert(parameters
->options().section_ordering_file());
3179 std::sort(sort_list
.begin(), sort_list
.end(),
3180 Input_section_sort_section_order_index_compare());
3183 // Copy the sorted input sections back to our list.
3184 this->input_sections_
.clear();
3185 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
3186 p
!= sort_list
.end();
3188 this->input_sections_
.push_back(p
->input_section());
3191 // Remember that we sorted the input sections, since we might get
3193 this->attached_input_sections_are_sorted_
= true;
3196 // Write the section header to *OSHDR.
3198 template<int size
, bool big_endian
>
3200 Output_section::write_header(const Layout
* layout
,
3201 const Stringpool
* secnamepool
,
3202 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
3204 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
3205 oshdr
->put_sh_type(this->type_
);
3207 elfcpp::Elf_Xword flags
= this->flags_
;
3208 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
3209 flags
|= elfcpp::SHF_INFO_LINK
;
3210 oshdr
->put_sh_flags(flags
);
3212 oshdr
->put_sh_addr(this->address());
3213 oshdr
->put_sh_offset(this->offset());
3214 oshdr
->put_sh_size(this->data_size());
3215 if (this->link_section_
!= NULL
)
3216 oshdr
->put_sh_link(this->link_section_
->out_shndx());
3217 else if (this->should_link_to_symtab_
)
3218 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
3219 else if (this->should_link_to_dynsym_
)
3220 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
3222 oshdr
->put_sh_link(this->link_
);
3224 elfcpp::Elf_Word info
;
3225 if (this->info_section_
!= NULL
)
3227 if (this->info_uses_section_index_
)
3228 info
= this->info_section_
->out_shndx();
3230 info
= this->info_section_
->symtab_index();
3232 else if (this->info_symndx_
!= NULL
)
3233 info
= this->info_symndx_
->symtab_index();
3236 oshdr
->put_sh_info(info
);
3238 oshdr
->put_sh_addralign(this->addralign_
);
3239 oshdr
->put_sh_entsize(this->entsize_
);
3242 // Write out the data. For input sections the data is written out by
3243 // Object::relocate, but we have to handle Output_section_data objects
3247 Output_section::do_write(Output_file
* of
)
3249 gold_assert(!this->requires_postprocessing());
3251 // If the target performs relaxation, we delay filler generation until now.
3252 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3254 off_t output_section_file_offset
= this->offset();
3255 for (Fill_list::iterator p
= this->fills_
.begin();
3256 p
!= this->fills_
.end();
3259 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3260 of
->write(output_section_file_offset
+ p
->section_offset(),
3261 fill_data
.data(), fill_data
.size());
3264 off_t off
= this->offset() + this->first_input_offset_
;
3265 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3266 p
!= this->input_sections_
.end();
3269 off_t aligned_off
= align_address(off
, p
->addralign());
3270 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3272 size_t fill_len
= aligned_off
- off
;
3273 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3274 of
->write(off
, fill_data
.data(), fill_data
.size());
3278 off
= aligned_off
+ p
->data_size();
3282 // If a section requires postprocessing, create the buffer to use.
3285 Output_section::create_postprocessing_buffer()
3287 gold_assert(this->requires_postprocessing());
3289 if (this->postprocessing_buffer_
!= NULL
)
3292 if (!this->input_sections_
.empty())
3294 off_t off
= this->first_input_offset_
;
3295 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3296 p
!= this->input_sections_
.end();
3299 off
= align_address(off
, p
->addralign());
3300 p
->finalize_data_size();
3301 off
+= p
->data_size();
3303 this->set_current_data_size_for_child(off
);
3306 off_t buffer_size
= this->current_data_size_for_child();
3307 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
3310 // Write all the data of an Output_section into the postprocessing
3311 // buffer. This is used for sections which require postprocessing,
3312 // such as compression. Input sections are handled by
3313 // Object::Relocate.
3316 Output_section::write_to_postprocessing_buffer()
3318 gold_assert(this->requires_postprocessing());
3320 // If the target performs relaxation, we delay filler generation until now.
3321 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3323 unsigned char* buffer
= this->postprocessing_buffer();
3324 for (Fill_list::iterator p
= this->fills_
.begin();
3325 p
!= this->fills_
.end();
3328 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3329 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
3333 off_t off
= this->first_input_offset_
;
3334 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3335 p
!= this->input_sections_
.end();
3338 off_t aligned_off
= align_address(off
, p
->addralign());
3339 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3341 size_t fill_len
= aligned_off
- off
;
3342 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3343 memcpy(buffer
+ off
, fill_data
.data(), fill_data
.size());
3346 p
->write_to_buffer(buffer
+ aligned_off
);
3347 off
= aligned_off
+ p
->data_size();
3351 // Get the input sections for linker script processing. We leave
3352 // behind the Output_section_data entries. Note that this may be
3353 // slightly incorrect for merge sections. We will leave them behind,
3354 // but it is possible that the script says that they should follow
3355 // some other input sections, as in:
3356 // .rodata { *(.rodata) *(.rodata.cst*) }
3357 // For that matter, we don't handle this correctly:
3358 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3359 // With luck this will never matter.
3362 Output_section::get_input_sections(
3364 const std::string
& fill
,
3365 std::list
<Input_section
>* input_sections
)
3367 if (this->checkpoint_
!= NULL
3368 && !this->checkpoint_
->input_sections_saved())
3369 this->checkpoint_
->save_input_sections();
3371 // Invalidate fast look-up maps.
3372 this->lookup_maps_
->invalidate();
3374 uint64_t orig_address
= address
;
3376 address
= align_address(address
, this->addralign());
3378 Input_section_list remaining
;
3379 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3380 p
!= this->input_sections_
.end();
3383 if (p
->is_input_section()
3384 || p
->is_relaxed_input_section()
3385 || p
->is_merge_section())
3386 input_sections
->push_back(*p
);
3389 uint64_t aligned_address
= align_address(address
, p
->addralign());
3390 if (aligned_address
!= address
&& !fill
.empty())
3392 section_size_type length
=
3393 convert_to_section_size_type(aligned_address
- address
);
3394 std::string this_fill
;
3395 this_fill
.reserve(length
);
3396 while (this_fill
.length() + fill
.length() <= length
)
3398 if (this_fill
.length() < length
)
3399 this_fill
.append(fill
, 0, length
- this_fill
.length());
3401 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
3402 remaining
.push_back(Input_section(posd
));
3404 address
= aligned_address
;
3406 remaining
.push_back(*p
);
3408 p
->finalize_data_size();
3409 address
+= p
->data_size();
3413 this->input_sections_
.swap(remaining
);
3414 this->first_input_offset_
= 0;
3416 uint64_t data_size
= address
- orig_address
;
3417 this->set_current_data_size_for_child(data_size
);
3421 // Add a script input section. SIS is an Output_section::Input_section,
3422 // which can be either a plain input section or a special input section like
3423 // a relaxed input section. For a special input section, its size must be
3427 Output_section::add_script_input_section(const Input_section
& sis
)
3429 uint64_t data_size
= sis
.data_size();
3430 uint64_t addralign
= sis
.addralign();
3431 if (addralign
> this->addralign_
)
3432 this->addralign_
= addralign
;
3434 off_t offset_in_section
= this->current_data_size_for_child();
3435 off_t aligned_offset_in_section
= align_address(offset_in_section
,
3438 this->set_current_data_size_for_child(aligned_offset_in_section
3441 this->input_sections_
.push_back(sis
);
3443 // Update fast lookup maps if necessary.
3444 if (this->lookup_maps_
->is_valid())
3446 if (sis
.is_merge_section())
3448 Output_merge_base
* pomb
= sis
.output_merge_base();
3449 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
3451 this->lookup_maps_
->add_merge_section(msp
, pomb
);
3452 for (Output_merge_base::Input_sections::const_iterator p
=
3453 pomb
->input_sections_begin();
3454 p
!= pomb
->input_sections_end();
3456 this->lookup_maps_
->add_merge_input_section(p
->first
, p
->second
,
3459 else if (sis
.is_relaxed_input_section())
3461 Output_relaxed_input_section
* poris
= sis
.relaxed_input_section();
3462 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
3463 poris
->shndx(), poris
);
3468 // Save states for relaxation.
3471 Output_section::save_states()
3473 gold_assert(this->checkpoint_
== NULL
);
3474 Checkpoint_output_section
* checkpoint
=
3475 new Checkpoint_output_section(this->addralign_
, this->flags_
,
3476 this->input_sections_
,
3477 this->first_input_offset_
,
3478 this->attached_input_sections_are_sorted_
);
3479 this->checkpoint_
= checkpoint
;
3480 gold_assert(this->fills_
.empty());
3484 Output_section::discard_states()
3486 gold_assert(this->checkpoint_
!= NULL
);
3487 delete this->checkpoint_
;
3488 this->checkpoint_
= NULL
;
3489 gold_assert(this->fills_
.empty());
3491 // Simply invalidate the fast lookup maps since we do not keep
3493 this->lookup_maps_
->invalidate();
3497 Output_section::restore_states()
3499 gold_assert(this->checkpoint_
!= NULL
);
3500 Checkpoint_output_section
* checkpoint
= this->checkpoint_
;
3502 this->addralign_
= checkpoint
->addralign();
3503 this->flags_
= checkpoint
->flags();
3504 this->first_input_offset_
= checkpoint
->first_input_offset();
3506 if (!checkpoint
->input_sections_saved())
3508 // If we have not copied the input sections, just resize it.
3509 size_t old_size
= checkpoint
->input_sections_size();
3510 gold_assert(this->input_sections_
.size() >= old_size
);
3511 this->input_sections_
.resize(old_size
);
3515 // We need to copy the whole list. This is not efficient for
3516 // extremely large output with hundreads of thousands of input
3517 // objects. We may need to re-think how we should pass sections
3519 this->input_sections_
= *checkpoint
->input_sections();
3522 this->attached_input_sections_are_sorted_
=
3523 checkpoint
->attached_input_sections_are_sorted();
3525 // Simply invalidate the fast lookup maps since we do not keep
3527 this->lookup_maps_
->invalidate();
3530 // Update the section offsets of input sections in this. This is required if
3531 // relaxation causes some input sections to change sizes.
3534 Output_section::adjust_section_offsets()
3536 if (!this->section_offsets_need_adjustment_
)
3540 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3541 p
!= this->input_sections_
.end();
3544 off
= align_address(off
, p
->addralign());
3545 if (p
->is_input_section())
3546 p
->relobj()->set_section_offset(p
->shndx(), off
);
3547 off
+= p
->data_size();
3550 this->section_offsets_need_adjustment_
= false;
3553 // Print to the map file.
3556 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
3558 mapfile
->print_output_section(this);
3560 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3561 p
!= this->input_sections_
.end();
3563 p
->print_to_mapfile(mapfile
);
3566 // Print stats for merge sections to stderr.
3569 Output_section::print_merge_stats()
3571 Input_section_list::iterator p
;
3572 for (p
= this->input_sections_
.begin();
3573 p
!= this->input_sections_
.end();
3575 p
->print_merge_stats(this->name_
);
3578 // Set a fixed layout for the section. Used for incremental update links.
3581 Output_section::set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
,
3582 off_t sh_size
, uint64_t sh_addralign
)
3584 this->addralign_
= sh_addralign
;
3585 this->set_current_data_size(sh_size
);
3586 if ((this->flags_
& elfcpp::SHF_ALLOC
) != 0)
3587 this->set_address(sh_addr
);
3588 this->set_file_offset(sh_offset
);
3589 this->finalize_data_size();
3590 this->free_list_
.init(sh_size
, false);
3591 this->has_fixed_layout_
= true;
3594 // Reserve space within the fixed layout for the section. Used for
3595 // incremental update links.
3597 Output_section::reserve(uint64_t sh_offset
, uint64_t sh_size
)
3599 this->free_list_
.remove(sh_offset
, sh_offset
+ sh_size
);
3602 // Output segment methods.
3604 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3614 is_max_align_known_(false),
3615 are_addresses_set_(false),
3616 is_large_data_segment_(false)
3618 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3620 if (type
== elfcpp::PT_TLS
)
3621 this->flags_
= elfcpp::PF_R
;
3624 // Add an Output_section to a PT_LOAD Output_segment.
3627 Output_segment::add_output_section_to_load(Layout
* layout
,
3629 elfcpp::Elf_Word seg_flags
)
3631 gold_assert(this->type() == elfcpp::PT_LOAD
);
3632 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
3633 gold_assert(!this->is_max_align_known_
);
3634 gold_assert(os
->is_large_data_section() == this->is_large_data_segment());
3636 this->update_flags_for_output_section(seg_flags
);
3638 // We don't want to change the ordering if we have a linker script
3639 // with a SECTIONS clause.
3640 Output_section_order order
= os
->order();
3641 if (layout
->script_options()->saw_sections_clause())
3642 order
= static_cast<Output_section_order
>(0);
3644 gold_assert(order
!= ORDER_INVALID
);
3646 this->output_lists_
[order
].push_back(os
);
3649 // Add an Output_section to a non-PT_LOAD Output_segment.
3652 Output_segment::add_output_section_to_nonload(Output_section
* os
,
3653 elfcpp::Elf_Word seg_flags
)
3655 gold_assert(this->type() != elfcpp::PT_LOAD
);
3656 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
3657 gold_assert(!this->is_max_align_known_
);
3659 this->update_flags_for_output_section(seg_flags
);
3661 this->output_lists_
[0].push_back(os
);
3664 // Remove an Output_section from this segment. It is an error if it
3668 Output_segment::remove_output_section(Output_section
* os
)
3670 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3672 Output_data_list
* pdl
= &this->output_lists_
[i
];
3673 for (Output_data_list::iterator p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
3685 // Add an Output_data (which need not be an Output_section) to the
3686 // start of a segment.
3689 Output_segment::add_initial_output_data(Output_data
* od
)
3691 gold_assert(!this->is_max_align_known_
);
3692 Output_data_list::iterator p
= this->output_lists_
[0].begin();
3693 this->output_lists_
[0].insert(p
, od
);
3696 // Return true if this segment has any sections which hold actual
3697 // data, rather than being a BSS section.
3700 Output_segment::has_any_data_sections() const
3702 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3704 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3705 for (Output_data_list::const_iterator p
= pdl
->begin();
3709 if (!(*p
)->is_section())
3711 if ((*p
)->output_section()->type() != elfcpp::SHT_NOBITS
)
3718 // Return whether the first data section (not counting TLS sections)
3719 // is a relro section.
3722 Output_segment::is_first_section_relro() const
3724 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3726 if (i
== static_cast<int>(ORDER_TLS_DATA
)
3727 || i
== static_cast<int>(ORDER_TLS_BSS
))
3729 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3732 Output_data
* p
= pdl
->front();
3733 return p
->is_section() && p
->output_section()->is_relro();
3739 // Return the maximum alignment of the Output_data in Output_segment.
3742 Output_segment::maximum_alignment()
3744 if (!this->is_max_align_known_
)
3746 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3748 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3749 uint64_t addralign
= Output_segment::maximum_alignment_list(pdl
);
3750 if (addralign
> this->max_align_
)
3751 this->max_align_
= addralign
;
3753 this->is_max_align_known_
= true;
3756 return this->max_align_
;
3759 // Return the maximum alignment of a list of Output_data.
3762 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
3765 for (Output_data_list::const_iterator p
= pdl
->begin();
3769 uint64_t addralign
= (*p
)->addralign();
3770 if (addralign
> ret
)
3776 // Return whether this segment has any dynamic relocs.
3779 Output_segment::has_dynamic_reloc() const
3781 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3782 if (this->has_dynamic_reloc_list(&this->output_lists_
[i
]))
3787 // Return whether this Output_data_list has any dynamic relocs.
3790 Output_segment::has_dynamic_reloc_list(const Output_data_list
* pdl
) const
3792 for (Output_data_list::const_iterator p
= pdl
->begin();
3795 if ((*p
)->has_dynamic_reloc())
3800 // Set the section addresses for an Output_segment. If RESET is true,
3801 // reset the addresses first. ADDR is the address and *POFF is the
3802 // file offset. Set the section indexes starting with *PSHNDX.
3803 // INCREASE_RELRO is the size of the portion of the first non-relro
3804 // section that should be included in the PT_GNU_RELRO segment.
3805 // If this segment has relro sections, and has been aligned for
3806 // that purpose, set *HAS_RELRO to TRUE. Return the address of
3807 // the immediately following segment. Update *HAS_RELRO, *POFF,
3811 Output_segment::set_section_addresses(Layout
* layout
, bool reset
,
3813 unsigned int* increase_relro
,
3816 unsigned int* pshndx
)
3818 gold_assert(this->type_
== elfcpp::PT_LOAD
);
3820 uint64_t last_relro_pad
= 0;
3821 off_t orig_off
= *poff
;
3823 bool in_tls
= false;
3825 // If we have relro sections, we need to pad forward now so that the
3826 // relro sections plus INCREASE_RELRO end on a common page boundary.
3827 if (parameters
->options().relro()
3828 && this->is_first_section_relro()
3829 && (!this->are_addresses_set_
|| reset
))
3831 uint64_t relro_size
= 0;
3833 uint64_t max_align
= 0;
3834 for (int i
= 0; i
<= static_cast<int>(ORDER_RELRO_LAST
); ++i
)
3836 Output_data_list
* pdl
= &this->output_lists_
[i
];
3837 Output_data_list::iterator p
;
3838 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
3840 if (!(*p
)->is_section())
3842 uint64_t align
= (*p
)->addralign();
3843 if (align
> max_align
)
3845 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
3849 // Align the first non-TLS section to the alignment
3850 // of the TLS segment.
3854 relro_size
= align_address(relro_size
, align
);
3855 // Ignore the size of the .tbss section.
3856 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
3857 && (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
3859 if ((*p
)->is_address_valid())
3860 relro_size
+= (*p
)->data_size();
3863 // FIXME: This could be faster.
3864 (*p
)->set_address_and_file_offset(addr
+ relro_size
,
3866 relro_size
+= (*p
)->data_size();
3867 (*p
)->reset_address_and_file_offset();
3870 if (p
!= pdl
->end())
3873 relro_size
+= *increase_relro
;
3874 // Pad the total relro size to a multiple of the maximum
3875 // section alignment seen.
3876 uint64_t aligned_size
= align_address(relro_size
, max_align
);
3877 // Note the amount of padding added after the last relro section.
3878 last_relro_pad
= aligned_size
- relro_size
;
3881 uint64_t page_align
= parameters
->target().common_pagesize();
3883 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
3884 uint64_t desired_align
= page_align
- (aligned_size
% page_align
);
3885 if (desired_align
< *poff
% page_align
)
3886 *poff
+= page_align
- *poff
% page_align
;
3887 *poff
+= desired_align
- *poff
% page_align
;
3888 addr
+= *poff
- orig_off
;
3892 if (!reset
&& this->are_addresses_set_
)
3894 gold_assert(this->paddr_
== addr
);
3895 addr
= this->vaddr_
;
3899 this->vaddr_
= addr
;
3900 this->paddr_
= addr
;
3901 this->are_addresses_set_
= true;
3906 this->offset_
= orig_off
;
3910 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3912 if (i
== static_cast<int>(ORDER_RELRO_LAST
))
3914 *poff
+= last_relro_pad
;
3915 addr
+= last_relro_pad
;
3916 if (this->output_lists_
[i
].empty())
3918 // If there is nothing in the ORDER_RELRO_LAST list,
3919 // the padding will occur at the end of the relro
3920 // segment, and we need to add it to *INCREASE_RELRO.
3921 *increase_relro
+= last_relro_pad
;
3924 addr
= this->set_section_list_addresses(layout
, reset
,
3925 &this->output_lists_
[i
],
3926 addr
, poff
, pshndx
, &in_tls
);
3927 if (i
< static_cast<int>(ORDER_SMALL_BSS
))
3929 this->filesz_
= *poff
- orig_off
;
3936 // If the last section was a TLS section, align upward to the
3937 // alignment of the TLS segment, so that the overall size of the TLS
3938 // segment is aligned.
3941 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
3942 *poff
= align_address(*poff
, segment_align
);
3945 this->memsz_
= *poff
- orig_off
;
3947 // Ignore the file offset adjustments made by the BSS Output_data
3954 // Set the addresses and file offsets in a list of Output_data
3958 Output_segment::set_section_list_addresses(Layout
* layout
, bool reset
,
3959 Output_data_list
* pdl
,
3960 uint64_t addr
, off_t
* poff
,
3961 unsigned int* pshndx
,
3964 off_t startoff
= *poff
;
3965 // For incremental updates, we may allocate non-fixed sections from
3966 // free space in the file. This keeps track of the high-water mark.
3967 off_t maxoff
= startoff
;
3969 off_t off
= startoff
;
3970 for (Output_data_list::iterator p
= pdl
->begin();
3975 (*p
)->reset_address_and_file_offset();
3977 // When doing an incremental update or when using a linker script,
3978 // the section will most likely already have an address.
3979 if (!(*p
)->is_address_valid())
3981 uint64_t align
= (*p
)->addralign();
3983 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
3985 // Give the first TLS section the alignment of the
3986 // entire TLS segment. Otherwise the TLS segment as a
3987 // whole may be misaligned.
3990 Output_segment
* tls_segment
= layout
->tls_segment();
3991 gold_assert(tls_segment
!= NULL
);
3992 uint64_t segment_align
= tls_segment
->maximum_alignment();
3993 gold_assert(segment_align
>= align
);
3994 align
= segment_align
;
4001 // If this is the first section after the TLS segment,
4002 // align it to at least the alignment of the TLS
4003 // segment, so that the size of the overall TLS segment
4007 uint64_t segment_align
=
4008 layout
->tls_segment()->maximum_alignment();
4009 if (segment_align
> align
)
4010 align
= segment_align
;
4016 // FIXME: Need to handle TLS and .bss with incremental update.
4017 if (!parameters
->incremental_update()
4018 || (*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4019 || (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4021 off
= align_address(off
, align
);
4022 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4026 // Incremental update: allocate file space from free list.
4027 (*p
)->pre_finalize_data_size();
4028 off_t current_size
= (*p
)->current_data_size();
4029 off
= layout
->allocate(current_size
, align
, startoff
);
4032 gold_assert((*p
)->output_section() != NULL
);
4033 gold_fatal(_("out of patch space for section %s; "
4034 "relink with --incremental-full"),
4035 (*p
)->output_section()->name());
4037 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4038 if ((*p
)->data_size() > current_size
)
4040 gold_assert((*p
)->output_section() != NULL
);
4041 gold_fatal(_("%s: section changed size; "
4042 "relink with --incremental-full"),
4043 (*p
)->output_section()->name());
4047 else if (parameters
->incremental_update())
4049 // For incremental updates, use the fixed offset for the
4050 // high-water mark computation.
4051 off
= (*p
)->offset();
4055 // The script may have inserted a skip forward, but it
4056 // better not have moved backward.
4057 if ((*p
)->address() >= addr
+ (off
- startoff
))
4058 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
4061 if (!layout
->script_options()->saw_sections_clause())
4065 Output_section
* os
= (*p
)->output_section();
4067 // Cast to unsigned long long to avoid format warnings.
4068 unsigned long long previous_dot
=
4069 static_cast<unsigned long long>(addr
+ (off
- startoff
));
4070 unsigned long long dot
=
4071 static_cast<unsigned long long>((*p
)->address());
4074 gold_error(_("dot moves backward in linker script "
4075 "from 0x%llx to 0x%llx"), previous_dot
, dot
);
4077 gold_error(_("address of section '%s' moves backward "
4078 "from 0x%llx to 0x%llx"),
4079 os
->name(), previous_dot
, dot
);
4082 (*p
)->set_file_offset(off
);
4083 (*p
)->finalize_data_size();
4086 gold_debug(DEBUG_INCREMENTAL
,
4087 "set_section_list_addresses: %08lx %08lx %s",
4088 static_cast<long>(off
),
4089 static_cast<long>((*p
)->data_size()),
4090 ((*p
)->output_section() != NULL
4091 ? (*p
)->output_section()->name() : "(special)"));
4093 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
4094 // section. Such a section does not affect the size of a
4096 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4097 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4098 off
+= (*p
)->data_size();
4103 if ((*p
)->is_section())
4105 (*p
)->set_out_shndx(*pshndx
);
4111 return addr
+ (maxoff
- startoff
);
4114 // For a non-PT_LOAD segment, set the offset from the sections, if
4115 // any. Add INCREASE to the file size and the memory size.
4118 Output_segment::set_offset(unsigned int increase
)
4120 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
4122 gold_assert(!this->are_addresses_set_
);
4124 // A non-load section only uses output_lists_[0].
4126 Output_data_list
* pdl
= &this->output_lists_
[0];
4130 gold_assert(increase
== 0);
4133 this->are_addresses_set_
= true;
4135 this->min_p_align_
= 0;
4141 // Find the first and last section by address.
4142 const Output_data
* first
= NULL
;
4143 const Output_data
* last_data
= NULL
;
4144 const Output_data
* last_bss
= NULL
;
4145 for (Output_data_list::const_iterator p
= pdl
->begin();
4150 || (*p
)->address() < first
->address()
4151 || ((*p
)->address() == first
->address()
4152 && (*p
)->data_size() < first
->data_size()))
4154 const Output_data
** plast
;
4155 if ((*p
)->is_section()
4156 && (*p
)->output_section()->type() == elfcpp::SHT_NOBITS
)
4161 || (*p
)->address() > (*plast
)->address()
4162 || ((*p
)->address() == (*plast
)->address()
4163 && (*p
)->data_size() > (*plast
)->data_size()))
4167 this->vaddr_
= first
->address();
4168 this->paddr_
= (first
->has_load_address()
4169 ? first
->load_address()
4171 this->are_addresses_set_
= true;
4172 this->offset_
= first
->offset();
4174 if (last_data
== NULL
)
4177 this->filesz_
= (last_data
->address()
4178 + last_data
->data_size()
4181 const Output_data
* last
= last_bss
!= NULL
? last_bss
: last_data
;
4182 this->memsz_
= (last
->address()
4186 this->filesz_
+= increase
;
4187 this->memsz_
+= increase
;
4189 // If this is a RELRO segment, verify that the segment ends at a
4191 if (this->type_
== elfcpp::PT_GNU_RELRO
)
4193 uint64_t page_align
= parameters
->target().common_pagesize();
4194 uint64_t segment_end
= this->vaddr_
+ this->memsz_
;
4195 if (parameters
->incremental_update())
4197 // The INCREASE_RELRO calculation is bypassed for an incremental
4198 // update, so we need to adjust the segment size manually here.
4199 segment_end
= align_address(segment_end
, page_align
);
4200 this->memsz_
= segment_end
- this->vaddr_
;
4203 gold_assert(segment_end
== align_address(segment_end
, page_align
));
4206 // If this is a TLS segment, align the memory size. The code in
4207 // set_section_list ensures that the section after the TLS segment
4208 // is aligned to give us room.
4209 if (this->type_
== elfcpp::PT_TLS
)
4211 uint64_t segment_align
= this->maximum_alignment();
4212 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
4213 this->memsz_
= align_address(this->memsz_
, segment_align
);
4217 // Set the TLS offsets of the sections in the PT_TLS segment.
4220 Output_segment::set_tls_offsets()
4222 gold_assert(this->type_
== elfcpp::PT_TLS
);
4224 for (Output_data_list::iterator p
= this->output_lists_
[0].begin();
4225 p
!= this->output_lists_
[0].end();
4227 (*p
)->set_tls_offset(this->vaddr_
);
4230 // Return the load address of the first section.
4233 Output_segment::first_section_load_address() const
4235 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4237 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4238 for (Output_data_list::const_iterator p
= pdl
->begin();
4242 if ((*p
)->is_section())
4243 return ((*p
)->has_load_address()
4244 ? (*p
)->load_address()
4251 // Return the number of Output_sections in an Output_segment.
4254 Output_segment::output_section_count() const
4256 unsigned int ret
= 0;
4257 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4258 ret
+= this->output_section_count_list(&this->output_lists_
[i
]);
4262 // Return the number of Output_sections in an Output_data_list.
4265 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
4267 unsigned int count
= 0;
4268 for (Output_data_list::const_iterator p
= pdl
->begin();
4272 if ((*p
)->is_section())
4278 // Return the section attached to the list segment with the lowest
4279 // load address. This is used when handling a PHDRS clause in a
4283 Output_segment::section_with_lowest_load_address() const
4285 Output_section
* found
= NULL
;
4286 uint64_t found_lma
= 0;
4287 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4288 this->lowest_load_address_in_list(&this->output_lists_
[i
], &found
,
4293 // Look through a list for a section with a lower load address.
4296 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
4297 Output_section
** found
,
4298 uint64_t* found_lma
) const
4300 for (Output_data_list::const_iterator p
= pdl
->begin();
4304 if (!(*p
)->is_section())
4306 Output_section
* os
= static_cast<Output_section
*>(*p
);
4307 uint64_t lma
= (os
->has_load_address()
4308 ? os
->load_address()
4310 if (*found
== NULL
|| lma
< *found_lma
)
4318 // Write the segment data into *OPHDR.
4320 template<int size
, bool big_endian
>
4322 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
4324 ophdr
->put_p_type(this->type_
);
4325 ophdr
->put_p_offset(this->offset_
);
4326 ophdr
->put_p_vaddr(this->vaddr_
);
4327 ophdr
->put_p_paddr(this->paddr_
);
4328 ophdr
->put_p_filesz(this->filesz_
);
4329 ophdr
->put_p_memsz(this->memsz_
);
4330 ophdr
->put_p_flags(this->flags_
);
4331 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
4334 // Write the section headers into V.
4336 template<int size
, bool big_endian
>
4338 Output_segment::write_section_headers(const Layout
* layout
,
4339 const Stringpool
* secnamepool
,
4341 unsigned int* pshndx
) const
4343 // Every section that is attached to a segment must be attached to a
4344 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4346 if (this->type_
!= elfcpp::PT_LOAD
)
4349 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4351 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4352 v
= this->write_section_headers_list
<size
, big_endian
>(layout
,
4361 template<int size
, bool big_endian
>
4363 Output_segment::write_section_headers_list(const Layout
* layout
,
4364 const Stringpool
* secnamepool
,
4365 const Output_data_list
* pdl
,
4367 unsigned int* pshndx
) const
4369 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
4370 for (Output_data_list::const_iterator p
= pdl
->begin();
4374 if ((*p
)->is_section())
4376 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
4377 gold_assert(*pshndx
== ps
->out_shndx());
4378 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
4379 ps
->write_header(layout
, secnamepool
, &oshdr
);
4387 // Print the output sections to the map file.
4390 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
4392 if (this->type() != elfcpp::PT_LOAD
)
4394 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4395 this->print_section_list_to_mapfile(mapfile
, &this->output_lists_
[i
]);
4398 // Print an output section list to the map file.
4401 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
4402 const Output_data_list
* pdl
) const
4404 for (Output_data_list::const_iterator p
= pdl
->begin();
4407 (*p
)->print_to_mapfile(mapfile
);
4410 // Output_file methods.
4412 Output_file::Output_file(const char* name
)
4417 map_is_anonymous_(false),
4418 is_temporary_(false)
4422 // Try to open an existing file. Returns false if the file doesn't
4423 // exist, has a size of 0 or can't be mmapped.
4426 Output_file::open_for_modification()
4428 // The name "-" means "stdout".
4429 if (strcmp(this->name_
, "-") == 0)
4432 // Don't bother opening files with a size of zero.
4434 if (::stat(this->name_
, &s
) != 0 || s
.st_size
== 0)
4437 int o
= open_descriptor(-1, this->name_
, O_RDWR
, 0);
4439 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
4441 this->file_size_
= s
.st_size
;
4443 // If the file can't be mmapped, copying the content to an anonymous
4444 // map will probably negate the performance benefits of incremental
4445 // linking. This could be helped by using views and loading only
4446 // the necessary parts, but this is not supported as of now.
4447 if (!this->map_no_anonymous())
4449 release_descriptor(o
, true);
4451 this->file_size_
= 0;
4458 // Open the output file.
4461 Output_file::open(off_t file_size
)
4463 this->file_size_
= file_size
;
4465 // Unlink the file first; otherwise the open() may fail if the file
4466 // is busy (e.g. it's an executable that's currently being executed).
4468 // However, the linker may be part of a system where a zero-length
4469 // file is created for it to write to, with tight permissions (gcc
4470 // 2.95 did something like this). Unlinking the file would work
4471 // around those permission controls, so we only unlink if the file
4472 // has a non-zero size. We also unlink only regular files to avoid
4473 // trouble with directories/etc.
4475 // If we fail, continue; this command is merely a best-effort attempt
4476 // to improve the odds for open().
4478 // We let the name "-" mean "stdout"
4479 if (!this->is_temporary_
)
4481 if (strcmp(this->name_
, "-") == 0)
4482 this->o_
= STDOUT_FILENO
;
4486 if (::stat(this->name_
, &s
) == 0
4487 && (S_ISREG (s
.st_mode
) || S_ISLNK (s
.st_mode
)))
4490 ::unlink(this->name_
);
4491 else if (!parameters
->options().relocatable())
4493 // If we don't unlink the existing file, add execute
4494 // permission where read permissions already exist
4495 // and where the umask permits.
4496 int mask
= ::umask(0);
4498 s
.st_mode
|= (s
.st_mode
& 0444) >> 2;
4499 ::chmod(this->name_
, s
.st_mode
& ~mask
);
4503 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
4504 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
4507 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
4515 // Resize the output file.
4518 Output_file::resize(off_t file_size
)
4520 // If the mmap is mapping an anonymous memory buffer, this is easy:
4521 // just mremap to the new size. If it's mapping to a file, we want
4522 // to unmap to flush to the file, then remap after growing the file.
4523 if (this->map_is_anonymous_
)
4525 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
4527 if (base
== MAP_FAILED
)
4528 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
4529 this->base_
= static_cast<unsigned char*>(base
);
4530 this->file_size_
= file_size
;
4535 this->file_size_
= file_size
;
4536 if (!this->map_no_anonymous())
4537 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
4541 // Map an anonymous block of memory which will later be written to the
4542 // file. Return whether the map succeeded.
4545 Output_file::map_anonymous()
4547 void* base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
4548 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
4549 if (base
!= MAP_FAILED
)
4551 this->map_is_anonymous_
= true;
4552 this->base_
= static_cast<unsigned char*>(base
);
4558 // Map the file into memory. Return whether the mapping succeeded.
4561 Output_file::map_no_anonymous()
4563 const int o
= this->o_
;
4565 // If the output file is not a regular file, don't try to mmap it;
4566 // instead, we'll mmap a block of memory (an anonymous buffer), and
4567 // then later write the buffer to the file.
4569 struct stat statbuf
;
4570 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
4571 || ::fstat(o
, &statbuf
) != 0
4572 || !S_ISREG(statbuf
.st_mode
)
4573 || this->is_temporary_
)
4576 // Ensure that we have disk space available for the file. If we
4577 // don't do this, it is possible that we will call munmap, close,
4578 // and exit with dirty buffers still in the cache with no assigned
4579 // disk blocks. If the disk is out of space at that point, the
4580 // output file will wind up incomplete, but we will have already
4581 // exited. The alternative to fallocate would be to use fdatasync,
4582 // but that would be a more significant performance hit.
4583 if (::posix_fallocate(o
, 0, this->file_size_
) < 0)
4584 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
4586 // Map the file into memory.
4587 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
4590 // The mmap call might fail because of file system issues: the file
4591 // system might not support mmap at all, or it might not support
4592 // mmap with PROT_WRITE.
4593 if (base
== MAP_FAILED
)
4596 this->map_is_anonymous_
= false;
4597 this->base_
= static_cast<unsigned char*>(base
);
4601 // Map the file into memory.
4606 if (this->map_no_anonymous())
4609 // The mmap call might fail because of file system issues: the file
4610 // system might not support mmap at all, or it might not support
4611 // mmap with PROT_WRITE. I'm not sure which errno values we will
4612 // see in all cases, so if the mmap fails for any reason and we
4613 // don't care about file contents, try for an anonymous map.
4614 if (this->map_anonymous())
4617 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4618 this->name_
, static_cast<unsigned long>(this->file_size_
),
4622 // Unmap the file from memory.
4625 Output_file::unmap()
4627 if (::munmap(this->base_
, this->file_size_
) < 0)
4628 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
4632 // Close the output file.
4635 Output_file::close()
4637 // If the map isn't file-backed, we need to write it now.
4638 if (this->map_is_anonymous_
&& !this->is_temporary_
)
4640 size_t bytes_to_write
= this->file_size_
;
4642 while (bytes_to_write
> 0)
4644 ssize_t bytes_written
= ::write(this->o_
, this->base_
+ offset
,
4646 if (bytes_written
== 0)
4647 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
4648 else if (bytes_written
< 0)
4649 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
4652 bytes_to_write
-= bytes_written
;
4653 offset
+= bytes_written
;
4659 // We don't close stdout or stderr
4660 if (this->o_
!= STDOUT_FILENO
4661 && this->o_
!= STDERR_FILENO
4662 && !this->is_temporary_
)
4663 if (::close(this->o_
) < 0)
4664 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
4668 // Instantiate the templates we need. We could use the configure
4669 // script to restrict this to only the ones for implemented targets.
4671 #ifdef HAVE_TARGET_32_LITTLE
4674 Output_section::add_input_section
<32, false>(
4676 Sized_relobj
<32, false>* object
,
4678 const char* secname
,
4679 const elfcpp::Shdr
<32, false>& shdr
,
4680 unsigned int reloc_shndx
,
4681 bool have_sections_script
);
4684 #ifdef HAVE_TARGET_32_BIG
4687 Output_section::add_input_section
<32, true>(
4689 Sized_relobj
<32, true>* object
,
4691 const char* secname
,
4692 const elfcpp::Shdr
<32, true>& shdr
,
4693 unsigned int reloc_shndx
,
4694 bool have_sections_script
);
4697 #ifdef HAVE_TARGET_64_LITTLE
4700 Output_section::add_input_section
<64, false>(
4702 Sized_relobj
<64, false>* object
,
4704 const char* secname
,
4705 const elfcpp::Shdr
<64, false>& shdr
,
4706 unsigned int reloc_shndx
,
4707 bool have_sections_script
);
4710 #ifdef HAVE_TARGET_64_BIG
4713 Output_section::add_input_section
<64, true>(
4715 Sized_relobj
<64, true>* object
,
4717 const char* secname
,
4718 const elfcpp::Shdr
<64, true>& shdr
,
4719 unsigned int reloc_shndx
,
4720 bool have_sections_script
);
4723 #ifdef HAVE_TARGET_32_LITTLE
4725 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
4728 #ifdef HAVE_TARGET_32_BIG
4730 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
4733 #ifdef HAVE_TARGET_64_LITTLE
4735 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
4738 #ifdef HAVE_TARGET_64_BIG
4740 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
4743 #ifdef HAVE_TARGET_32_LITTLE
4745 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
4748 #ifdef HAVE_TARGET_32_BIG
4750 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
4753 #ifdef HAVE_TARGET_64_LITTLE
4755 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
4758 #ifdef HAVE_TARGET_64_BIG
4760 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
4763 #ifdef HAVE_TARGET_32_LITTLE
4765 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
4768 #ifdef HAVE_TARGET_32_BIG
4770 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
4773 #ifdef HAVE_TARGET_64_LITTLE
4775 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
4778 #ifdef HAVE_TARGET_64_BIG
4780 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
4783 #ifdef HAVE_TARGET_32_LITTLE
4785 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
4788 #ifdef HAVE_TARGET_32_BIG
4790 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
4793 #ifdef HAVE_TARGET_64_LITTLE
4795 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
4798 #ifdef HAVE_TARGET_64_BIG
4800 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
4803 #ifdef HAVE_TARGET_32_LITTLE
4805 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
4808 #ifdef HAVE_TARGET_32_BIG
4810 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
4813 #ifdef HAVE_TARGET_64_LITTLE
4815 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
4818 #ifdef HAVE_TARGET_64_BIG
4820 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
4823 #ifdef HAVE_TARGET_32_LITTLE
4825 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
4828 #ifdef HAVE_TARGET_32_BIG
4830 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
4833 #ifdef HAVE_TARGET_64_LITTLE
4835 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
4838 #ifdef HAVE_TARGET_64_BIG
4840 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
4843 #ifdef HAVE_TARGET_32_LITTLE
4845 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
4848 #ifdef HAVE_TARGET_32_BIG
4850 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
4853 #ifdef HAVE_TARGET_64_LITTLE
4855 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
4858 #ifdef HAVE_TARGET_64_BIG
4860 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
4863 #ifdef HAVE_TARGET_32_LITTLE
4865 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
4868 #ifdef HAVE_TARGET_32_BIG
4870 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
4873 #ifdef HAVE_TARGET_64_LITTLE
4875 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
4878 #ifdef HAVE_TARGET_64_BIG
4880 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
4883 #ifdef HAVE_TARGET_32_LITTLE
4885 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
4888 #ifdef HAVE_TARGET_32_BIG
4890 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
4893 #ifdef HAVE_TARGET_64_LITTLE
4895 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
4898 #ifdef HAVE_TARGET_64_BIG
4900 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
4903 #ifdef HAVE_TARGET_32_LITTLE
4905 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
4908 #ifdef HAVE_TARGET_32_BIG
4910 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
4913 #ifdef HAVE_TARGET_64_LITTLE
4915 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
4918 #ifdef HAVE_TARGET_64_BIG
4920 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
4923 #ifdef HAVE_TARGET_32_LITTLE
4925 class Output_data_group
<32, false>;
4928 #ifdef HAVE_TARGET_32_BIG
4930 class Output_data_group
<32, true>;
4933 #ifdef HAVE_TARGET_64_LITTLE
4935 class Output_data_group
<64, false>;
4938 #ifdef HAVE_TARGET_64_BIG
4940 class Output_data_group
<64, true>;
4943 #ifdef HAVE_TARGET_32_LITTLE
4945 class Output_data_got
<32, false>;
4948 #ifdef HAVE_TARGET_32_BIG
4950 class Output_data_got
<32, true>;
4953 #ifdef HAVE_TARGET_64_LITTLE
4955 class Output_data_got
<64, false>;
4958 #ifdef HAVE_TARGET_64_BIG
4960 class Output_data_got
<64, true>;
4963 } // End namespace gold.