1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008 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" // for unlink_if_ordinary()
35 #include "parameters.h"
42 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44 # define MAP_ANONYMOUS MAP_ANON
50 // Output_data variables.
52 bool Output_data::allocated_sizes_are_fixed
;
54 // Output_data methods.
56 Output_data::~Output_data()
60 // Return the default alignment for the target size.
63 Output_data::default_alignment()
65 return Output_data::default_alignment_for_size(
66 parameters
->target().get_size());
69 // Return the default alignment for a size--32 or 64.
72 Output_data::default_alignment_for_size(int size
)
82 // Output_section_header methods. This currently assumes that the
83 // segment and section lists are complete at construction time.
85 Output_section_headers::Output_section_headers(
87 const Layout::Segment_list
* segment_list
,
88 const Layout::Section_list
* section_list
,
89 const Layout::Section_list
* unattached_section_list
,
90 const Stringpool
* secnamepool
)
92 segment_list_(segment_list
),
93 section_list_(section_list
),
94 unattached_section_list_(unattached_section_list
),
95 secnamepool_(secnamepool
)
97 // Count all the sections. Start with 1 for the null section.
99 if (!parameters
->options().relocatable())
101 for (Layout::Segment_list::const_iterator p
= segment_list
->begin();
102 p
!= segment_list
->end();
104 if ((*p
)->type() == elfcpp::PT_LOAD
)
105 count
+= (*p
)->output_section_count();
109 for (Layout::Section_list::const_iterator p
= section_list
->begin();
110 p
!= section_list
->end();
112 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
115 count
+= unattached_section_list
->size();
117 const int size
= parameters
->target().get_size();
120 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
122 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
126 this->set_data_size(count
* shdr_size
);
129 // Write out the section headers.
132 Output_section_headers::do_write(Output_file
* of
)
134 switch (parameters
->size_and_endianness())
136 #ifdef HAVE_TARGET_32_LITTLE
137 case Parameters::TARGET_32_LITTLE
:
138 this->do_sized_write
<32, false>(of
);
141 #ifdef HAVE_TARGET_32_BIG
142 case Parameters::TARGET_32_BIG
:
143 this->do_sized_write
<32, true>(of
);
146 #ifdef HAVE_TARGET_64_LITTLE
147 case Parameters::TARGET_64_LITTLE
:
148 this->do_sized_write
<64, false>(of
);
151 #ifdef HAVE_TARGET_64_BIG
152 case Parameters::TARGET_64_BIG
:
153 this->do_sized_write
<64, true>(of
);
161 template<int size
, bool big_endian
>
163 Output_section_headers::do_sized_write(Output_file
* of
)
165 off_t all_shdrs_size
= this->data_size();
166 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
168 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
169 unsigned char* v
= view
;
172 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
173 oshdr
.put_sh_name(0);
174 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
175 oshdr
.put_sh_flags(0);
176 oshdr
.put_sh_addr(0);
177 oshdr
.put_sh_offset(0);
178 oshdr
.put_sh_size(0);
179 oshdr
.put_sh_link(0);
180 oshdr
.put_sh_info(0);
181 oshdr
.put_sh_addralign(0);
182 oshdr
.put_sh_entsize(0);
187 unsigned int shndx
= 1;
188 if (!parameters
->options().relocatable())
190 for (Layout::Segment_list::const_iterator p
=
191 this->segment_list_
->begin();
192 p
!= this->segment_list_
->end();
194 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
201 for (Layout::Section_list::const_iterator p
=
202 this->section_list_
->begin();
203 p
!= this->section_list_
->end();
206 // We do unallocated sections below, except that group
207 // sections have to come first.
208 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
209 && (*p
)->type() != elfcpp::SHT_GROUP
)
211 gold_assert(shndx
== (*p
)->out_shndx());
212 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
213 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
219 for (Layout::Section_list::const_iterator p
=
220 this->unattached_section_list_
->begin();
221 p
!= this->unattached_section_list_
->end();
224 // For a relocatable link, we did unallocated group sections
225 // above, since they have to come first.
226 if ((*p
)->type() == elfcpp::SHT_GROUP
227 && parameters
->options().relocatable())
229 gold_assert(shndx
== (*p
)->out_shndx());
230 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
231 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
236 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
239 // Output_segment_header methods.
241 Output_segment_headers::Output_segment_headers(
242 const Layout::Segment_list
& segment_list
)
243 : segment_list_(segment_list
)
245 const int size
= parameters
->target().get_size();
248 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
250 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
254 this->set_data_size(segment_list
.size() * phdr_size
);
258 Output_segment_headers::do_write(Output_file
* of
)
260 switch (parameters
->size_and_endianness())
262 #ifdef HAVE_TARGET_32_LITTLE
263 case Parameters::TARGET_32_LITTLE
:
264 this->do_sized_write
<32, false>(of
);
267 #ifdef HAVE_TARGET_32_BIG
268 case Parameters::TARGET_32_BIG
:
269 this->do_sized_write
<32, true>(of
);
272 #ifdef HAVE_TARGET_64_LITTLE
273 case Parameters::TARGET_64_LITTLE
:
274 this->do_sized_write
<64, false>(of
);
277 #ifdef HAVE_TARGET_64_BIG
278 case Parameters::TARGET_64_BIG
:
279 this->do_sized_write
<64, true>(of
);
287 template<int size
, bool big_endian
>
289 Output_segment_headers::do_sized_write(Output_file
* of
)
291 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
292 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
293 gold_assert(all_phdrs_size
== this->data_size());
294 unsigned char* view
= of
->get_output_view(this->offset(),
296 unsigned char* v
= view
;
297 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
298 p
!= this->segment_list_
.end();
301 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
302 (*p
)->write_header(&ophdr
);
306 gold_assert(v
- view
== all_phdrs_size
);
308 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
311 // Output_file_header methods.
313 Output_file_header::Output_file_header(const Target
* target
,
314 const Symbol_table
* symtab
,
315 const Output_segment_headers
* osh
,
319 segment_header_(osh
),
320 section_header_(NULL
),
324 const int size
= parameters
->target().get_size();
327 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
329 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
333 this->set_data_size(ehdr_size
);
336 // Set the section table information for a file header.
339 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
340 const Output_section
* shstrtab
)
342 this->section_header_
= shdrs
;
343 this->shstrtab_
= shstrtab
;
346 // Write out the file header.
349 Output_file_header::do_write(Output_file
* of
)
351 gold_assert(this->offset() == 0);
353 switch (parameters
->size_and_endianness())
355 #ifdef HAVE_TARGET_32_LITTLE
356 case Parameters::TARGET_32_LITTLE
:
357 this->do_sized_write
<32, false>(of
);
360 #ifdef HAVE_TARGET_32_BIG
361 case Parameters::TARGET_32_BIG
:
362 this->do_sized_write
<32, true>(of
);
365 #ifdef HAVE_TARGET_64_LITTLE
366 case Parameters::TARGET_64_LITTLE
:
367 this->do_sized_write
<64, false>(of
);
370 #ifdef HAVE_TARGET_64_BIG
371 case Parameters::TARGET_64_BIG
:
372 this->do_sized_write
<64, true>(of
);
380 // Write out the file header with appropriate size and endianess.
382 template<int size
, bool big_endian
>
384 Output_file_header::do_sized_write(Output_file
* of
)
386 gold_assert(this->offset() == 0);
388 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
389 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
390 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
392 unsigned char e_ident
[elfcpp::EI_NIDENT
];
393 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
394 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
395 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
396 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
397 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
399 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
401 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
404 e_ident
[elfcpp::EI_DATA
] = (big_endian
405 ? elfcpp::ELFDATA2MSB
406 : elfcpp::ELFDATA2LSB
);
407 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
408 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
409 oehdr
.put_e_ident(e_ident
);
412 if (parameters
->options().relocatable())
413 e_type
= elfcpp::ET_REL
;
414 else if (parameters
->options().shared())
415 e_type
= elfcpp::ET_DYN
;
417 e_type
= elfcpp::ET_EXEC
;
418 oehdr
.put_e_type(e_type
);
420 oehdr
.put_e_machine(this->target_
->machine_code());
421 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
423 oehdr
.put_e_entry(this->entry
<size
>());
425 if (this->segment_header_
== NULL
)
426 oehdr
.put_e_phoff(0);
428 oehdr
.put_e_phoff(this->segment_header_
->offset());
430 oehdr
.put_e_shoff(this->section_header_
->offset());
432 // FIXME: The target needs to set the flags.
433 oehdr
.put_e_flags(0);
435 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
437 if (this->segment_header_
== NULL
)
439 oehdr
.put_e_phentsize(0);
440 oehdr
.put_e_phnum(0);
444 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
445 oehdr
.put_e_phnum(this->segment_header_
->data_size()
446 / elfcpp::Elf_sizes
<size
>::phdr_size
);
449 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
450 oehdr
.put_e_shnum(this->section_header_
->data_size()
451 / elfcpp::Elf_sizes
<size
>::shdr_size
);
452 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
454 of
->write_output_view(0, ehdr_size
, view
);
457 // Return the value to use for the entry address. THIS->ENTRY_ is the
458 // symbol specified on the command line, if any.
461 typename
elfcpp::Elf_types
<size
>::Elf_Addr
462 Output_file_header::entry()
464 const bool should_issue_warning
= (this->entry_
!= NULL
465 && !parameters
->options().relocatable()
466 && !parameters
->options().shared());
468 // FIXME: Need to support target specific entry symbol.
469 const char* entry
= this->entry_
;
473 Symbol
* sym
= this->symtab_
->lookup(entry
);
475 typename Sized_symbol
<size
>::Value_type v
;
478 Sized_symbol
<size
>* ssym
;
479 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
480 if (!ssym
->is_defined() && should_issue_warning
)
481 gold_warning("entry symbol '%s' exists but is not defined", entry
);
486 // We couldn't find the entry symbol. See if we can parse it as
487 // a number. This supports, e.g., -e 0x1000.
489 v
= strtoull(entry
, &endptr
, 0);
492 if (should_issue_warning
)
493 gold_warning("cannot find entry symbol '%s'", entry
);
501 // Output_data_const methods.
504 Output_data_const::do_write(Output_file
* of
)
506 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
509 // Output_data_const_buffer methods.
512 Output_data_const_buffer::do_write(Output_file
* of
)
514 of
->write(this->offset(), this->p_
, this->data_size());
517 // Output_section_data methods.
519 // Record the output section, and set the entry size and such.
522 Output_section_data::set_output_section(Output_section
* os
)
524 gold_assert(this->output_section_
== NULL
);
525 this->output_section_
= os
;
526 this->do_adjust_output_section(os
);
529 // Return the section index of the output section.
532 Output_section_data::do_out_shndx() const
534 gold_assert(this->output_section_
!= NULL
);
535 return this->output_section_
->out_shndx();
538 // Output_data_strtab methods.
540 // Set the final data size.
543 Output_data_strtab::set_final_data_size()
545 this->strtab_
->set_string_offsets();
546 this->set_data_size(this->strtab_
->get_strtab_size());
549 // Write out a string table.
552 Output_data_strtab::do_write(Output_file
* of
)
554 this->strtab_
->write(of
, this->offset());
557 // Output_reloc methods.
559 // A reloc against a global symbol.
561 template<bool dynamic
, int size
, bool big_endian
>
562 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
568 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
569 is_relative_(is_relative
), is_section_symbol_(false), shndx_(INVALID_CODE
)
571 // this->type_ is a bitfield; make sure TYPE fits.
572 gold_assert(this->type_
== type
);
573 this->u1_
.gsym
= gsym
;
576 this->set_needs_dynsym_index();
579 template<bool dynamic
, int size
, bool big_endian
>
580 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
587 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
588 is_relative_(is_relative
), is_section_symbol_(false), shndx_(shndx
)
590 gold_assert(shndx
!= INVALID_CODE
);
591 // this->type_ is a bitfield; make sure TYPE fits.
592 gold_assert(this->type_
== type
);
593 this->u1_
.gsym
= gsym
;
594 this->u2_
.relobj
= relobj
;
596 this->set_needs_dynsym_index();
599 // A reloc against a local symbol.
601 template<bool dynamic
, int size
, bool big_endian
>
602 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
603 Sized_relobj
<size
, big_endian
>* relobj
,
604 unsigned int local_sym_index
,
609 bool is_section_symbol
)
610 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
611 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
614 gold_assert(local_sym_index
!= GSYM_CODE
615 && local_sym_index
!= INVALID_CODE
);
616 // this->type_ is a bitfield; make sure TYPE fits.
617 gold_assert(this->type_
== type
);
618 this->u1_
.relobj
= relobj
;
621 this->set_needs_dynsym_index();
624 template<bool dynamic
, int size
, bool big_endian
>
625 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
626 Sized_relobj
<size
, big_endian
>* relobj
,
627 unsigned int local_sym_index
,
632 bool is_section_symbol
)
633 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
634 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
637 gold_assert(local_sym_index
!= GSYM_CODE
638 && local_sym_index
!= INVALID_CODE
);
639 gold_assert(shndx
!= INVALID_CODE
);
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_
== type
);
642 this->u1_
.relobj
= relobj
;
643 this->u2_
.relobj
= relobj
;
645 this->set_needs_dynsym_index();
648 // A reloc against the STT_SECTION symbol of an output section.
650 template<bool dynamic
, int size
, bool big_endian
>
651 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
656 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
657 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE
)
659 // this->type_ is a bitfield; make sure TYPE fits.
660 gold_assert(this->type_
== type
);
664 this->set_needs_dynsym_index();
666 os
->set_needs_symtab_index();
669 template<bool dynamic
, int size
, bool big_endian
>
670 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
676 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
677 is_relative_(false), is_section_symbol_(true), shndx_(shndx
)
679 gold_assert(shndx
!= INVALID_CODE
);
680 // this->type_ is a bitfield; make sure TYPE fits.
681 gold_assert(this->type_
== type
);
683 this->u2_
.relobj
= relobj
;
685 this->set_needs_dynsym_index();
687 os
->set_needs_symtab_index();
690 // Record that we need a dynamic symbol index for this relocation.
692 template<bool dynamic
, int size
, bool big_endian
>
694 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
695 set_needs_dynsym_index()
697 if (this->is_relative_
)
699 switch (this->local_sym_index_
)
705 this->u1_
.gsym
->set_needs_dynsym_entry();
709 this->u1_
.os
->set_needs_dynsym_index();
717 const unsigned int lsi
= this->local_sym_index_
;
718 if (!this->is_section_symbol_
)
719 this->u1_
.relobj
->set_needs_output_dynsym_entry(lsi
);
722 section_offset_type dummy
;
723 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &dummy
);
724 gold_assert(os
!= NULL
);
725 os
->set_needs_dynsym_index();
732 // Get the symbol index of a relocation.
734 template<bool dynamic
, int size
, bool big_endian
>
736 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
740 switch (this->local_sym_index_
)
746 if (this->u1_
.gsym
== NULL
)
749 index
= this->u1_
.gsym
->dynsym_index();
751 index
= this->u1_
.gsym
->symtab_index();
756 index
= this->u1_
.os
->dynsym_index();
758 index
= this->u1_
.os
->symtab_index();
762 // Relocations without symbols use a symbol index of 0.
768 const unsigned int lsi
= this->local_sym_index_
;
769 if (!this->is_section_symbol_
)
772 index
= this->u1_
.relobj
->dynsym_index(lsi
);
774 index
= this->u1_
.relobj
->symtab_index(lsi
);
778 section_offset_type dummy
;
779 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &dummy
);
780 gold_assert(os
!= NULL
);
782 index
= os
->dynsym_index();
784 index
= os
->symtab_index();
789 gold_assert(index
!= -1U);
793 // For a local section symbol, get the address of the offset ADDEND
794 // within the input section.
796 template<bool dynamic
, int size
, bool big_endian
>
798 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
799 local_section_offset(Addend addend
) const
801 gold_assert(this->local_sym_index_
!= GSYM_CODE
802 && this->local_sym_index_
!= SECTION_CODE
803 && this->local_sym_index_
!= INVALID_CODE
804 && this->is_section_symbol_
);
805 const unsigned int lsi
= this->local_sym_index_
;
806 section_offset_type offset
;
807 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &offset
);
808 gold_assert(os
!= NULL
);
810 return offset
+ addend
;
811 // This is a merge section.
812 offset
= os
->output_address(this->u1_
.relobj
, lsi
, addend
);
813 gold_assert(offset
!= -1);
817 // Write out the offset and info fields of a Rel or Rela relocation
820 template<bool dynamic
, int size
, bool big_endian
>
821 template<typename Write_rel
>
823 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
826 Address address
= this->address_
;
827 if (this->shndx_
!= INVALID_CODE
)
829 section_offset_type off
;
830 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
832 gold_assert(os
!= NULL
);
834 address
+= os
->address() + off
;
837 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
839 gold_assert(address
!= -1U);
842 else if (this->u2_
.od
!= NULL
)
843 address
+= this->u2_
.od
->address();
844 wr
->put_r_offset(address
);
845 unsigned int sym_index
= this->is_relative_
? 0 : this->get_symbol_index();
846 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
849 // Write out a Rel relocation.
851 template<bool dynamic
, int size
, bool big_endian
>
853 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
854 unsigned char* pov
) const
856 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
857 this->write_rel(&orel
);
860 // Get the value of the symbol referred to by a Rel relocation.
862 template<bool dynamic
, int size
, bool big_endian
>
863 typename
elfcpp::Elf_types
<size
>::Elf_Addr
864 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
867 if (this->local_sym_index_
== GSYM_CODE
)
869 const Sized_symbol
<size
>* sym
;
870 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
871 return sym
->value() + addend
;
873 gold_assert(this->local_sym_index_
!= SECTION_CODE
874 && this->local_sym_index_
!= INVALID_CODE
875 && !this->is_section_symbol_
);
876 const unsigned int lsi
= this->local_sym_index_
;
877 const Symbol_value
<size
>* symval
= this->u1_
.relobj
->local_symbol(lsi
);
878 return symval
->value(this->u1_
.relobj
, addend
);
881 // Write out a Rela relocation.
883 template<bool dynamic
, int size
, bool big_endian
>
885 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
886 unsigned char* pov
) const
888 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
889 this->rel_
.write_rel(&orel
);
890 Addend addend
= this->addend_
;
891 if (this->rel_
.is_relative())
892 addend
= this->rel_
.symbol_value(addend
);
893 else if (this->rel_
.is_local_section_symbol())
894 addend
= this->rel_
.local_section_offset(addend
);
895 orel
.put_r_addend(addend
);
898 // Output_data_reloc_base methods.
900 // Adjust the output section.
902 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
904 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
905 ::do_adjust_output_section(Output_section
* os
)
907 if (sh_type
== elfcpp::SHT_REL
)
908 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
909 else if (sh_type
== elfcpp::SHT_RELA
)
910 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
914 os
->set_should_link_to_dynsym();
916 os
->set_should_link_to_symtab();
919 // Write out relocation data.
921 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
923 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
926 const off_t off
= this->offset();
927 const off_t oview_size
= this->data_size();
928 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
930 unsigned char* pov
= oview
;
931 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
932 p
!= this->relocs_
.end();
939 gold_assert(pov
- oview
== oview_size
);
941 of
->write_output_view(off
, oview_size
, oview
);
943 // We no longer need the relocation entries.
944 this->relocs_
.clear();
947 // Class Output_relocatable_relocs.
949 template<int sh_type
, int size
, bool big_endian
>
951 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
953 this->set_data_size(this->rr_
->output_reloc_count()
954 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
957 // class Output_data_group.
959 template<int size
, bool big_endian
>
960 Output_data_group
<size
, big_endian
>::Output_data_group(
961 Sized_relobj
<size
, big_endian
>* relobj
,
962 section_size_type entry_count
,
963 const elfcpp::Elf_Word
* contents
)
964 : Output_section_data(entry_count
* 4, 4),
967 this->flags_
= elfcpp::Swap
<32, big_endian
>::readval(contents
);
968 for (section_size_type i
= 1; i
< entry_count
; ++i
)
970 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
971 this->input_sections_
.push_back(shndx
);
975 // Write out the section group, which means translating the section
976 // indexes to apply to the output file.
978 template<int size
, bool big_endian
>
980 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
982 const off_t off
= this->offset();
983 const section_size_type oview_size
=
984 convert_to_section_size_type(this->data_size());
985 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
987 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
988 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
991 for (std::vector
<unsigned int>::const_iterator p
=
992 this->input_sections_
.begin();
993 p
!= this->input_sections_
.end();
996 section_offset_type dummy
;
997 Output_section
* os
= this->relobj_
->output_section(*p
, &dummy
);
999 unsigned int output_shndx
;
1001 output_shndx
= os
->out_shndx();
1004 this->relobj_
->error(_("section group retained but "
1005 "group element discarded"));
1009 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1012 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1013 gold_assert(wrote
== oview_size
);
1015 of
->write_output_view(off
, oview_size
, oview
);
1017 // We no longer need this information.
1018 this->input_sections_
.clear();
1021 // Output_data_got::Got_entry methods.
1023 // Write out the entry.
1025 template<int size
, bool big_endian
>
1027 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1031 switch (this->local_sym_index_
)
1035 // If the symbol is resolved locally, we need to write out the
1036 // link-time value, which will be relocated dynamically by a
1037 // RELATIVE relocation.
1038 Symbol
* gsym
= this->u_
.gsym
;
1039 Sized_symbol
<size
>* sgsym
;
1040 // This cast is a bit ugly. We don't want to put a
1041 // virtual method in Symbol, because we want Symbol to be
1042 // as small as possible.
1043 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1044 val
= sgsym
->value();
1049 val
= this->u_
.constant
;
1054 const unsigned int lsi
= this->local_sym_index_
;
1055 const Symbol_value
<size
>* symval
= this->u_
.object
->local_symbol(lsi
);
1056 val
= symval
->value(this->u_
.object
, 0);
1061 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1064 // Output_data_got methods.
1066 // Add an entry for a global symbol to the GOT. This returns true if
1067 // this is a new GOT entry, false if the symbol already had a GOT
1070 template<int size
, bool big_endian
>
1072 Output_data_got
<size
, big_endian
>::add_global(
1074 unsigned int got_type
)
1076 if (gsym
->has_got_offset(got_type
))
1079 this->entries_
.push_back(Got_entry(gsym
));
1080 this->set_got_size();
1081 gsym
->set_got_offset(got_type
, this->last_got_offset());
1085 // Add an entry for a global symbol to the GOT, and add a dynamic
1086 // relocation of type R_TYPE for the GOT entry.
1087 template<int size
, bool big_endian
>
1089 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1091 unsigned int got_type
,
1093 unsigned int r_type
)
1095 if (gsym
->has_got_offset(got_type
))
1098 this->entries_
.push_back(Got_entry());
1099 this->set_got_size();
1100 unsigned int got_offset
= this->last_got_offset();
1101 gsym
->set_got_offset(got_type
, got_offset
);
1102 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1105 template<int size
, bool big_endian
>
1107 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1109 unsigned int got_type
,
1111 unsigned int r_type
)
1113 if (gsym
->has_got_offset(got_type
))
1116 this->entries_
.push_back(Got_entry());
1117 this->set_got_size();
1118 unsigned int got_offset
= this->last_got_offset();
1119 gsym
->set_got_offset(got_type
, got_offset
);
1120 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1123 // Add a pair of entries for a global symbol to the GOT, and add
1124 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1125 // If R_TYPE_2 == 0, add the second entry with no relocation.
1126 template<int size
, bool big_endian
>
1128 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1130 unsigned int got_type
,
1132 unsigned int r_type_1
,
1133 unsigned int r_type_2
)
1135 if (gsym
->has_got_offset(got_type
))
1138 this->entries_
.push_back(Got_entry());
1139 unsigned int got_offset
= this->last_got_offset();
1140 gsym
->set_got_offset(got_type
, got_offset
);
1141 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1143 this->entries_
.push_back(Got_entry());
1146 got_offset
= this->last_got_offset();
1147 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
);
1150 this->set_got_size();
1153 template<int size
, bool big_endian
>
1155 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1157 unsigned int got_type
,
1159 unsigned int r_type_1
,
1160 unsigned int r_type_2
)
1162 if (gsym
->has_got_offset(got_type
))
1165 this->entries_
.push_back(Got_entry());
1166 unsigned int got_offset
= this->last_got_offset();
1167 gsym
->set_got_offset(got_type
, got_offset
);
1168 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1170 this->entries_
.push_back(Got_entry());
1173 got_offset
= this->last_got_offset();
1174 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
, 0);
1177 this->set_got_size();
1180 // Add an entry for a local symbol to the GOT. This returns true if
1181 // this is a new GOT entry, false if the symbol already has a GOT
1184 template<int size
, bool big_endian
>
1186 Output_data_got
<size
, big_endian
>::add_local(
1187 Sized_relobj
<size
, big_endian
>* object
,
1188 unsigned int symndx
,
1189 unsigned int got_type
)
1191 if (object
->local_has_got_offset(symndx
, got_type
))
1194 this->entries_
.push_back(Got_entry(object
, symndx
));
1195 this->set_got_size();
1196 object
->set_local_got_offset(symndx
, got_type
, this->last_got_offset());
1200 // Add an entry for a local symbol to the GOT, and add a dynamic
1201 // relocation of type R_TYPE for the GOT entry.
1202 template<int size
, bool big_endian
>
1204 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1205 Sized_relobj
<size
, big_endian
>* object
,
1206 unsigned int symndx
,
1207 unsigned int got_type
,
1209 unsigned int r_type
)
1211 if (object
->local_has_got_offset(symndx
, got_type
))
1214 this->entries_
.push_back(Got_entry());
1215 this->set_got_size();
1216 unsigned int got_offset
= this->last_got_offset();
1217 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1218 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1221 template<int size
, bool big_endian
>
1223 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1224 Sized_relobj
<size
, big_endian
>* object
,
1225 unsigned int symndx
,
1226 unsigned int got_type
,
1228 unsigned int r_type
)
1230 if (object
->local_has_got_offset(symndx
, got_type
))
1233 this->entries_
.push_back(Got_entry());
1234 this->set_got_size();
1235 unsigned int got_offset
= this->last_got_offset();
1236 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1237 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1240 // Add a pair of entries for a local symbol to the GOT, and add
1241 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1242 // If R_TYPE_2 == 0, add the second entry with no relocation.
1243 template<int size
, bool big_endian
>
1245 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1246 Sized_relobj
<size
, big_endian
>* object
,
1247 unsigned int symndx
,
1249 unsigned int got_type
,
1251 unsigned int r_type_1
,
1252 unsigned int r_type_2
)
1254 if (object
->local_has_got_offset(symndx
, got_type
))
1257 this->entries_
.push_back(Got_entry());
1258 unsigned int got_offset
= this->last_got_offset();
1259 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1260 section_offset_type off
;
1261 Output_section
* os
= object
->output_section(shndx
, &off
);
1262 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1264 this->entries_
.push_back(Got_entry(object
, symndx
));
1267 got_offset
= this->last_got_offset();
1268 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
);
1271 this->set_got_size();
1274 template<int size
, bool big_endian
>
1276 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1277 Sized_relobj
<size
, big_endian
>* object
,
1278 unsigned int symndx
,
1280 unsigned int got_type
,
1282 unsigned int r_type_1
,
1283 unsigned int r_type_2
)
1285 if (object
->local_has_got_offset(symndx
, got_type
))
1288 this->entries_
.push_back(Got_entry());
1289 unsigned int got_offset
= this->last_got_offset();
1290 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1291 section_offset_type off
;
1292 Output_section
* os
= object
->output_section(shndx
, &off
);
1293 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1295 this->entries_
.push_back(Got_entry(object
, symndx
));
1298 got_offset
= this->last_got_offset();
1299 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
, 0);
1302 this->set_got_size();
1305 // Write out the GOT.
1307 template<int size
, bool big_endian
>
1309 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1311 const int add
= size
/ 8;
1313 const off_t off
= this->offset();
1314 const off_t oview_size
= this->data_size();
1315 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1317 unsigned char* pov
= oview
;
1318 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1319 p
!= this->entries_
.end();
1326 gold_assert(pov
- oview
== oview_size
);
1328 of
->write_output_view(off
, oview_size
, oview
);
1330 // We no longer need the GOT entries.
1331 this->entries_
.clear();
1334 // Output_data_dynamic::Dynamic_entry methods.
1336 // Write out the entry.
1338 template<int size
, bool big_endian
>
1340 Output_data_dynamic::Dynamic_entry::write(
1342 const Stringpool
* pool
) const
1344 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1345 switch (this->offset_
)
1347 case DYNAMIC_NUMBER
:
1351 case DYNAMIC_SECTION_SIZE
:
1352 val
= this->u_
.od
->data_size();
1355 case DYNAMIC_SYMBOL
:
1357 const Sized_symbol
<size
>* s
=
1358 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1363 case DYNAMIC_STRING
:
1364 val
= pool
->get_offset(this->u_
.str
);
1368 val
= this->u_
.od
->address() + this->offset_
;
1372 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1373 dw
.put_d_tag(this->tag_
);
1377 // Output_data_dynamic methods.
1379 // Adjust the output section to set the entry size.
1382 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1384 if (parameters
->target().get_size() == 32)
1385 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1386 else if (parameters
->target().get_size() == 64)
1387 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1392 // Set the final data size.
1395 Output_data_dynamic::set_final_data_size()
1397 // Add the terminating entry.
1398 this->add_constant(elfcpp::DT_NULL
, 0);
1401 if (parameters
->target().get_size() == 32)
1402 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1403 else if (parameters
->target().get_size() == 64)
1404 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1407 this->set_data_size(this->entries_
.size() * dyn_size
);
1410 // Write out the dynamic entries.
1413 Output_data_dynamic::do_write(Output_file
* of
)
1415 switch (parameters
->size_and_endianness())
1417 #ifdef HAVE_TARGET_32_LITTLE
1418 case Parameters::TARGET_32_LITTLE
:
1419 this->sized_write
<32, false>(of
);
1422 #ifdef HAVE_TARGET_32_BIG
1423 case Parameters::TARGET_32_BIG
:
1424 this->sized_write
<32, true>(of
);
1427 #ifdef HAVE_TARGET_64_LITTLE
1428 case Parameters::TARGET_64_LITTLE
:
1429 this->sized_write
<64, false>(of
);
1432 #ifdef HAVE_TARGET_64_BIG
1433 case Parameters::TARGET_64_BIG
:
1434 this->sized_write
<64, true>(of
);
1442 template<int size
, bool big_endian
>
1444 Output_data_dynamic::sized_write(Output_file
* of
)
1446 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1448 const off_t offset
= this->offset();
1449 const off_t oview_size
= this->data_size();
1450 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1452 unsigned char* pov
= oview
;
1453 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1454 p
!= this->entries_
.end();
1457 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1461 gold_assert(pov
- oview
== oview_size
);
1463 of
->write_output_view(offset
, oview_size
, oview
);
1465 // We no longer need the dynamic entries.
1466 this->entries_
.clear();
1469 // Output_section::Input_section methods.
1471 // Return the data size. For an input section we store the size here.
1472 // For an Output_section_data, we have to ask it for the size.
1475 Output_section::Input_section::data_size() const
1477 if (this->is_input_section())
1478 return this->u1_
.data_size
;
1480 return this->u2_
.posd
->data_size();
1483 // Set the address and file offset.
1486 Output_section::Input_section::set_address_and_file_offset(
1489 off_t section_file_offset
)
1491 if (this->is_input_section())
1492 this->u2_
.object
->set_section_offset(this->shndx_
,
1493 file_offset
- section_file_offset
);
1495 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1498 // Reset the address and file offset.
1501 Output_section::Input_section::reset_address_and_file_offset()
1503 if (!this->is_input_section())
1504 this->u2_
.posd
->reset_address_and_file_offset();
1507 // Finalize the data size.
1510 Output_section::Input_section::finalize_data_size()
1512 if (!this->is_input_section())
1513 this->u2_
.posd
->finalize_data_size();
1516 // Try to turn an input offset into an output offset. We want to
1517 // return the output offset relative to the start of this
1518 // Input_section in the output section.
1521 Output_section::Input_section::output_offset(
1522 const Relobj
* object
,
1524 section_offset_type offset
,
1525 section_offset_type
*poutput
) const
1527 if (!this->is_input_section())
1528 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1531 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1538 // Return whether this is the merge section for the input section
1542 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1543 unsigned int shndx
) const
1545 if (this->is_input_section())
1547 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1550 // Write out the data. We don't have to do anything for an input
1551 // section--they are handled via Object::relocate--but this is where
1552 // we write out the data for an Output_section_data.
1555 Output_section::Input_section::write(Output_file
* of
)
1557 if (!this->is_input_section())
1558 this->u2_
.posd
->write(of
);
1561 // Write the data to a buffer. As for write(), we don't have to do
1562 // anything for an input section.
1565 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1567 if (!this->is_input_section())
1568 this->u2_
.posd
->write_to_buffer(buffer
);
1571 // Output_section methods.
1573 // Construct an Output_section. NAME will point into a Stringpool.
1575 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1576 elfcpp::Elf_Xword flags
)
1581 link_section_(NULL
),
1583 info_section_(NULL
),
1592 first_input_offset_(0),
1594 postprocessing_buffer_(NULL
),
1595 needs_symtab_index_(false),
1596 needs_dynsym_index_(false),
1597 should_link_to_symtab_(false),
1598 should_link_to_dynsym_(false),
1599 after_input_sections_(false),
1600 requires_postprocessing_(false),
1601 found_in_sections_clause_(false),
1602 has_load_address_(false),
1603 info_uses_section_index_(false),
1604 may_sort_attached_input_sections_(false),
1605 must_sort_attached_input_sections_(false),
1606 attached_input_sections_are_sorted_(false),
1609 // An unallocated section has no address. Forcing this means that
1610 // we don't need special treatment for symbols defined in debug
1612 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1613 this->set_address(0);
1616 Output_section::~Output_section()
1620 // Set the entry size.
1623 Output_section::set_entsize(uint64_t v
)
1625 if (this->entsize_
== 0)
1628 gold_assert(this->entsize_
== v
);
1631 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1632 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1633 // relocation section which applies to this section, or 0 if none, or
1634 // -1U if more than one. Return the offset of the input section
1635 // within the output section. Return -1 if the input section will
1636 // receive special handling. In the normal case we don't always keep
1637 // track of input sections for an Output_section. Instead, each
1638 // Object keeps track of the Output_section for each of its input
1639 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1640 // track of input sections here; this is used when SECTIONS appears in
1643 template<int size
, bool big_endian
>
1645 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1647 const char* secname
,
1648 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1649 unsigned int reloc_shndx
,
1650 bool have_sections_script
)
1652 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1653 if ((addralign
& (addralign
- 1)) != 0)
1655 object
->error(_("invalid alignment %lu for section \"%s\""),
1656 static_cast<unsigned long>(addralign
), secname
);
1660 if (addralign
> this->addralign_
)
1661 this->addralign_
= addralign
;
1663 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1664 this->flags_
|= (sh_flags
1665 & (elfcpp::SHF_WRITE
1667 | elfcpp::SHF_EXECINSTR
));
1669 uint64_t entsize
= shdr
.get_sh_entsize();
1671 // .debug_str is a mergeable string section, but is not always so
1672 // marked by compilers. Mark manually here so we can optimize.
1673 if (strcmp(secname
, ".debug_str") == 0)
1675 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1679 // If this is a SHF_MERGE section, we pass all the input sections to
1680 // a Output_data_merge. We don't try to handle relocations for such
1682 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1683 && reloc_shndx
== 0)
1685 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1686 entsize
, addralign
))
1688 // Tell the relocation routines that they need to call the
1689 // output_offset method to determine the final address.
1694 off_t offset_in_section
= this->current_data_size_for_child();
1695 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1698 if (aligned_offset_in_section
> offset_in_section
1699 && !have_sections_script
1700 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1701 && object
->target()->has_code_fill())
1703 // We need to add some fill data. Using fill_list_ when
1704 // possible is an optimization, since we will often have fill
1705 // sections without input sections.
1706 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1707 if (this->input_sections_
.empty())
1708 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1711 // FIXME: When relaxing, the size needs to adjust to
1712 // maintain a constant alignment.
1713 std::string
fill_data(object
->target()->code_fill(fill_len
));
1714 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1715 this->input_sections_
.push_back(Input_section(odc
));
1719 this->set_current_data_size_for_child(aligned_offset_in_section
1720 + shdr
.get_sh_size());
1722 // We need to keep track of this section if we are already keeping
1723 // track of sections, or if we are relaxing. Also, if this is a
1724 // section which requires sorting, or which may require sorting in
1725 // the future, we keep track of the sections. FIXME: Add test for
1727 if (have_sections_script
1728 || !this->input_sections_
.empty()
1729 || this->may_sort_attached_input_sections()
1730 || this->must_sort_attached_input_sections())
1731 this->input_sections_
.push_back(Input_section(object
, shndx
,
1735 return aligned_offset_in_section
;
1738 // Add arbitrary data to an output section.
1741 Output_section::add_output_section_data(Output_section_data
* posd
)
1743 Input_section
inp(posd
);
1744 this->add_output_section_data(&inp
);
1746 if (posd
->is_data_size_valid())
1748 off_t offset_in_section
= this->current_data_size_for_child();
1749 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1751 this->set_current_data_size_for_child(aligned_offset_in_section
1752 + posd
->data_size());
1756 // Add arbitrary data to an output section by Input_section.
1759 Output_section::add_output_section_data(Input_section
* inp
)
1761 if (this->input_sections_
.empty())
1762 this->first_input_offset_
= this->current_data_size_for_child();
1764 this->input_sections_
.push_back(*inp
);
1766 uint64_t addralign
= inp
->addralign();
1767 if (addralign
> this->addralign_
)
1768 this->addralign_
= addralign
;
1770 inp
->set_output_section(this);
1773 // Add a merge section to an output section.
1776 Output_section::add_output_merge_section(Output_section_data
* posd
,
1777 bool is_string
, uint64_t entsize
)
1779 Input_section
inp(posd
, is_string
, entsize
);
1780 this->add_output_section_data(&inp
);
1783 // Add an input section to a SHF_MERGE section.
1786 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1787 uint64_t flags
, uint64_t entsize
,
1790 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1792 // We only merge strings if the alignment is not more than the
1793 // character size. This could be handled, but it's unusual.
1794 if (is_string
&& addralign
> entsize
)
1797 Input_section_list::iterator p
;
1798 for (p
= this->input_sections_
.begin();
1799 p
!= this->input_sections_
.end();
1801 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1803 p
->add_input_section(object
, shndx
);
1807 // We handle the actual constant merging in Output_merge_data or
1808 // Output_merge_string_data.
1809 Output_section_data
* posd
;
1811 posd
= new Output_merge_data(entsize
, addralign
);
1817 posd
= new Output_merge_string
<char>(addralign
);
1820 posd
= new Output_merge_string
<uint16_t>(addralign
);
1823 posd
= new Output_merge_string
<uint32_t>(addralign
);
1830 this->add_output_merge_section(posd
, is_string
, entsize
);
1831 posd
->add_input_section(object
, shndx
);
1836 // Given an address OFFSET relative to the start of input section
1837 // SHNDX in OBJECT, return whether this address is being included in
1838 // the final link. This should only be called if SHNDX in OBJECT has
1839 // a special mapping.
1842 Output_section::is_input_address_mapped(const Relobj
* object
,
1846 gold_assert(object
->is_section_specially_mapped(shndx
));
1848 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1849 p
!= this->input_sections_
.end();
1852 section_offset_type output_offset
;
1853 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1854 return output_offset
!= -1;
1857 // By default we assume that the address is mapped. This should
1858 // only be called after we have passed all sections to Layout. At
1859 // that point we should know what we are discarding.
1863 // Given an address OFFSET relative to the start of input section
1864 // SHNDX in object OBJECT, return the output offset relative to the
1865 // start of the input section in the output section. This should only
1866 // be called if SHNDX in OBJECT has a special mapping.
1869 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
1870 section_offset_type offset
) const
1872 gold_assert(object
->is_section_specially_mapped(shndx
));
1873 // This can only be called meaningfully when layout is complete.
1874 gold_assert(Output_data::is_layout_complete());
1876 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1877 p
!= this->input_sections_
.end();
1880 section_offset_type output_offset
;
1881 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1882 return output_offset
;
1887 // Return the output virtual address of OFFSET relative to the start
1888 // of input section SHNDX in object OBJECT.
1891 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1894 gold_assert(object
->is_section_specially_mapped(shndx
));
1896 uint64_t addr
= this->address() + this->first_input_offset_
;
1897 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1898 p
!= this->input_sections_
.end();
1901 addr
= align_address(addr
, p
->addralign());
1902 section_offset_type output_offset
;
1903 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1905 if (output_offset
== -1)
1907 return addr
+ output_offset
;
1909 addr
+= p
->data_size();
1912 // If we get here, it means that we don't know the mapping for this
1913 // input section. This might happen in principle if
1914 // add_input_section were called before add_output_section_data.
1915 // But it should never actually happen.
1920 // Return the output address of the start of the merged section for
1921 // input section SHNDX in object OBJECT.
1924 Output_section::starting_output_address(const Relobj
* object
,
1925 unsigned int shndx
) const
1927 gold_assert(object
->is_section_specially_mapped(shndx
));
1929 uint64_t addr
= this->address() + this->first_input_offset_
;
1930 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1931 p
!= this->input_sections_
.end();
1934 addr
= align_address(addr
, p
->addralign());
1936 // It would be nice if we could use the existing output_offset
1937 // method to get the output offset of input offset 0.
1938 // Unfortunately we don't know for sure that input offset 0 is
1940 if (p
->is_merge_section_for(object
, shndx
))
1943 addr
+= p
->data_size();
1948 // Set the data size of an Output_section. This is where we handle
1949 // setting the addresses of any Output_section_data objects.
1952 Output_section::set_final_data_size()
1954 if (this->input_sections_
.empty())
1956 this->set_data_size(this->current_data_size_for_child());
1960 if (this->must_sort_attached_input_sections())
1961 this->sort_attached_input_sections();
1963 uint64_t address
= this->address();
1964 off_t startoff
= this->offset();
1965 off_t off
= startoff
+ this->first_input_offset_
;
1966 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1967 p
!= this->input_sections_
.end();
1970 off
= align_address(off
, p
->addralign());
1971 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
1973 off
+= p
->data_size();
1976 this->set_data_size(off
- startoff
);
1979 // Reset the address and file offset.
1982 Output_section::do_reset_address_and_file_offset()
1984 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1985 p
!= this->input_sections_
.end();
1987 p
->reset_address_and_file_offset();
1990 // Set the TLS offset. Called only for SHT_TLS sections.
1993 Output_section::do_set_tls_offset(uint64_t tls_base
)
1995 this->tls_offset_
= this->address() - tls_base
;
1998 // In a few cases we need to sort the input sections attached to an
1999 // output section. This is used to implement the type of constructor
2000 // priority ordering implemented by the GNU linker, in which the
2001 // priority becomes part of the section name and the sections are
2002 // sorted by name. We only do this for an output section if we see an
2003 // attached input section matching ".ctor.*", ".dtor.*",
2004 // ".init_array.*" or ".fini_array.*".
2006 class Output_section::Input_section_sort_entry
2009 Input_section_sort_entry()
2010 : input_section_(), index_(-1U), section_has_name_(false),
2014 Input_section_sort_entry(const Input_section
& input_section
,
2016 : input_section_(input_section
), index_(index
),
2017 section_has_name_(input_section
.is_input_section())
2019 if (this->section_has_name_
)
2021 // This is only called single-threaded from Layout::finalize,
2022 // so it is OK to lock. Unfortunately we have no way to pass
2024 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
2025 Object
* obj
= input_section
.relobj();
2026 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
2028 // This is a slow operation, which should be cached in
2029 // Layout::layout if this becomes a speed problem.
2030 this->section_name_
= obj
->section_name(input_section
.shndx());
2034 // Return the Input_section.
2035 const Input_section
&
2036 input_section() const
2038 gold_assert(this->index_
!= -1U);
2039 return this->input_section_
;
2042 // The index of this entry in the original list. This is used to
2043 // make the sort stable.
2047 gold_assert(this->index_
!= -1U);
2048 return this->index_
;
2051 // Whether there is a section name.
2053 section_has_name() const
2054 { return this->section_has_name_
; }
2056 // The section name.
2058 section_name() const
2060 gold_assert(this->section_has_name_
);
2061 return this->section_name_
;
2064 // Return true if the section name has a priority. This is assumed
2065 // to be true if it has a dot after the initial dot.
2067 has_priority() const
2069 gold_assert(this->section_has_name_
);
2070 return this->section_name_
.find('.', 1);
2073 // Return true if this an input file whose base name matches
2074 // FILE_NAME. The base name must have an extension of ".o", and
2075 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2076 // This is to match crtbegin.o as well as crtbeginS.o without
2077 // getting confused by other possibilities. Overall matching the
2078 // file name this way is a dreadful hack, but the GNU linker does it
2079 // in order to better support gcc, and we need to be compatible.
2081 match_file_name(const char* match_file_name
) const
2083 const std::string
& file_name(this->input_section_
.relobj()->name());
2084 const char* base_name
= lbasename(file_name
.c_str());
2085 size_t match_len
= strlen(match_file_name
);
2086 if (strncmp(base_name
, match_file_name
, match_len
) != 0)
2088 size_t base_len
= strlen(base_name
);
2089 if (base_len
!= match_len
+ 2 && base_len
!= match_len
+ 3)
2091 return memcmp(base_name
+ base_len
- 2, ".o", 2) == 0;
2095 // The Input_section we are sorting.
2096 Input_section input_section_
;
2097 // The index of this Input_section in the original list.
2098 unsigned int index_
;
2099 // Whether this Input_section has a section name--it won't if this
2100 // is some random Output_section_data.
2101 bool section_has_name_
;
2102 // The section name if there is one.
2103 std::string section_name_
;
2106 // Return true if S1 should come before S2 in the output section.
2109 Output_section::Input_section_sort_compare::operator()(
2110 const Output_section::Input_section_sort_entry
& s1
,
2111 const Output_section::Input_section_sort_entry
& s2
) const
2113 // crtbegin.o must come first.
2114 bool s1_begin
= s1
.match_file_name("crtbegin");
2115 bool s2_begin
= s2
.match_file_name("crtbegin");
2116 if (s1_begin
|| s2_begin
)
2122 return s1
.index() < s2
.index();
2125 // crtend.o must come last.
2126 bool s1_end
= s1
.match_file_name("crtend");
2127 bool s2_end
= s2
.match_file_name("crtend");
2128 if (s1_end
|| s2_end
)
2134 return s1
.index() < s2
.index();
2137 // We sort all the sections with no names to the end.
2138 if (!s1
.section_has_name() || !s2
.section_has_name())
2140 if (s1
.section_has_name())
2142 if (s2
.section_has_name())
2144 return s1
.index() < s2
.index();
2147 // A section with a priority follows a section without a priority.
2148 // The GNU linker does this for all but .init_array sections; until
2149 // further notice we'll assume that that is an mistake.
2150 bool s1_has_priority
= s1
.has_priority();
2151 bool s2_has_priority
= s2
.has_priority();
2152 if (s1_has_priority
&& !s2_has_priority
)
2154 if (!s1_has_priority
&& s2_has_priority
)
2157 // Otherwise we sort by name.
2158 int compare
= s1
.section_name().compare(s2
.section_name());
2162 // Otherwise we keep the input order.
2163 return s1
.index() < s2
.index();
2166 // Sort the input sections attached to an output section.
2169 Output_section::sort_attached_input_sections()
2171 if (this->attached_input_sections_are_sorted_
)
2174 // The only thing we know about an input section is the object and
2175 // the section index. We need the section name. Recomputing this
2176 // is slow but this is an unusual case. If this becomes a speed
2177 // problem we can cache the names as required in Layout::layout.
2179 // We start by building a larger vector holding a copy of each
2180 // Input_section, plus its current index in the list and its name.
2181 std::vector
<Input_section_sort_entry
> sort_list
;
2184 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2185 p
!= this->input_sections_
.end();
2187 sort_list
.push_back(Input_section_sort_entry(*p
, i
));
2189 // Sort the input sections.
2190 std::sort(sort_list
.begin(), sort_list
.end(), Input_section_sort_compare());
2192 // Copy the sorted input sections back to our list.
2193 this->input_sections_
.clear();
2194 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
2195 p
!= sort_list
.end();
2197 this->input_sections_
.push_back(p
->input_section());
2199 // Remember that we sorted the input sections, since we might get
2201 this->attached_input_sections_are_sorted_
= true;
2204 // Write the section header to *OSHDR.
2206 template<int size
, bool big_endian
>
2208 Output_section::write_header(const Layout
* layout
,
2209 const Stringpool
* secnamepool
,
2210 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
2212 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
2213 oshdr
->put_sh_type(this->type_
);
2215 elfcpp::Elf_Xword flags
= this->flags_
;
2216 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
2217 flags
|= elfcpp::SHF_INFO_LINK
;
2218 oshdr
->put_sh_flags(flags
);
2220 oshdr
->put_sh_addr(this->address());
2221 oshdr
->put_sh_offset(this->offset());
2222 oshdr
->put_sh_size(this->data_size());
2223 if (this->link_section_
!= NULL
)
2224 oshdr
->put_sh_link(this->link_section_
->out_shndx());
2225 else if (this->should_link_to_symtab_
)
2226 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
2227 else if (this->should_link_to_dynsym_
)
2228 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
2230 oshdr
->put_sh_link(this->link_
);
2232 elfcpp::Elf_Word info
;
2233 if (this->info_section_
!= NULL
)
2235 if (this->info_uses_section_index_
)
2236 info
= this->info_section_
->out_shndx();
2238 info
= this->info_section_
->symtab_index();
2240 else if (this->info_symndx_
!= NULL
)
2241 info
= this->info_symndx_
->symtab_index();
2244 oshdr
->put_sh_info(info
);
2246 oshdr
->put_sh_addralign(this->addralign_
);
2247 oshdr
->put_sh_entsize(this->entsize_
);
2250 // Write out the data. For input sections the data is written out by
2251 // Object::relocate, but we have to handle Output_section_data objects
2255 Output_section::do_write(Output_file
* of
)
2257 gold_assert(!this->requires_postprocessing());
2259 off_t output_section_file_offset
= this->offset();
2260 for (Fill_list::iterator p
= this->fills_
.begin();
2261 p
!= this->fills_
.end();
2264 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2265 of
->write(output_section_file_offset
+ p
->section_offset(),
2266 fill_data
.data(), fill_data
.size());
2269 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2270 p
!= this->input_sections_
.end();
2275 // If a section requires postprocessing, create the buffer to use.
2278 Output_section::create_postprocessing_buffer()
2280 gold_assert(this->requires_postprocessing());
2282 if (this->postprocessing_buffer_
!= NULL
)
2285 if (!this->input_sections_
.empty())
2287 off_t off
= this->first_input_offset_
;
2288 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2289 p
!= this->input_sections_
.end();
2292 off
= align_address(off
, p
->addralign());
2293 p
->finalize_data_size();
2294 off
+= p
->data_size();
2296 this->set_current_data_size_for_child(off
);
2299 off_t buffer_size
= this->current_data_size_for_child();
2300 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
2303 // Write all the data of an Output_section into the postprocessing
2304 // buffer. This is used for sections which require postprocessing,
2305 // such as compression. Input sections are handled by
2306 // Object::Relocate.
2309 Output_section::write_to_postprocessing_buffer()
2311 gold_assert(this->requires_postprocessing());
2313 unsigned char* buffer
= this->postprocessing_buffer();
2314 for (Fill_list::iterator p
= this->fills_
.begin();
2315 p
!= this->fills_
.end();
2318 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2319 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
2323 off_t off
= this->first_input_offset_
;
2324 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2325 p
!= this->input_sections_
.end();
2328 off
= align_address(off
, p
->addralign());
2329 p
->write_to_buffer(buffer
+ off
);
2330 off
+= p
->data_size();
2334 // Get the input sections for linker script processing. We leave
2335 // behind the Output_section_data entries. Note that this may be
2336 // slightly incorrect for merge sections. We will leave them behind,
2337 // but it is possible that the script says that they should follow
2338 // some other input sections, as in:
2339 // .rodata { *(.rodata) *(.rodata.cst*) }
2340 // For that matter, we don't handle this correctly:
2341 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2342 // With luck this will never matter.
2345 Output_section::get_input_sections(
2347 const std::string
& fill
,
2348 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
2350 uint64_t orig_address
= address
;
2352 address
= align_address(address
, this->addralign());
2354 Input_section_list remaining
;
2355 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2356 p
!= this->input_sections_
.end();
2359 if (p
->is_input_section())
2360 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2363 uint64_t aligned_address
= align_address(address
, p
->addralign());
2364 if (aligned_address
!= address
&& !fill
.empty())
2366 section_size_type length
=
2367 convert_to_section_size_type(aligned_address
- address
);
2368 std::string this_fill
;
2369 this_fill
.reserve(length
);
2370 while (this_fill
.length() + fill
.length() <= length
)
2372 if (this_fill
.length() < length
)
2373 this_fill
.append(fill
, 0, length
- this_fill
.length());
2375 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2376 remaining
.push_back(Input_section(posd
));
2378 address
= aligned_address
;
2380 remaining
.push_back(*p
);
2382 p
->finalize_data_size();
2383 address
+= p
->data_size();
2387 this->input_sections_
.swap(remaining
);
2388 this->first_input_offset_
= 0;
2390 uint64_t data_size
= address
- orig_address
;
2391 this->set_current_data_size_for_child(data_size
);
2395 // Add an input section from a script.
2398 Output_section::add_input_section_for_script(Relobj
* object
,
2403 if (addralign
> this->addralign_
)
2404 this->addralign_
= addralign
;
2406 off_t offset_in_section
= this->current_data_size_for_child();
2407 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2410 this->set_current_data_size_for_child(aligned_offset_in_section
2413 this->input_sections_
.push_back(Input_section(object
, shndx
,
2414 data_size
, addralign
));
2417 // Print stats for merge sections to stderr.
2420 Output_section::print_merge_stats()
2422 Input_section_list::iterator p
;
2423 for (p
= this->input_sections_
.begin();
2424 p
!= this->input_sections_
.end();
2426 p
->print_merge_stats(this->name_
);
2429 // Output segment methods.
2431 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2443 is_max_align_known_(false),
2444 are_addresses_set_(false)
2448 // Add an Output_section to an Output_segment.
2451 Output_segment::add_output_section(Output_section
* os
,
2452 elfcpp::Elf_Word seg_flags
,
2455 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2456 gold_assert(!this->is_max_align_known_
);
2458 // Update the segment flags.
2459 this->flags_
|= seg_flags
;
2461 Output_segment::Output_data_list
* pdl
;
2462 if (os
->type() == elfcpp::SHT_NOBITS
)
2463 pdl
= &this->output_bss_
;
2465 pdl
= &this->output_data_
;
2467 // So that PT_NOTE segments will work correctly, we need to ensure
2468 // that all SHT_NOTE sections are adjacent. This will normally
2469 // happen automatically, because all the SHT_NOTE input sections
2470 // will wind up in the same output section. However, it is possible
2471 // for multiple SHT_NOTE input sections to have different section
2472 // flags, and thus be in different output sections, but for the
2473 // different section flags to map into the same segment flags and
2474 // thus the same output segment.
2476 // Note that while there may be many input sections in an output
2477 // section, there are normally only a few output sections in an
2478 // output segment. This loop is expected to be fast.
2480 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2482 Output_segment::Output_data_list::iterator p
= pdl
->end();
2486 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2488 // We don't worry about the FRONT parameter.
2494 while (p
!= pdl
->begin());
2497 // Similarly, so that PT_TLS segments will work, we need to group
2498 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2499 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2500 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2501 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2502 // and the PT_TLS segment -- we do this grouping only for the
2504 if (this->type_
!= elfcpp::PT_TLS
2505 && (os
->flags() & elfcpp::SHF_TLS
) != 0
2506 && !this->output_data_
.empty())
2508 pdl
= &this->output_data_
;
2509 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2510 bool sawtls
= false;
2511 Output_segment::Output_data_list::iterator p
= pdl
->end();
2516 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2519 // Put a NOBITS section after the first TLS section.
2520 // But a PROGBITS section after the first TLS/PROGBITS
2522 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2526 // If we've gone past the TLS sections, but we've seen a
2527 // TLS section, then we need to insert this section now.
2533 // We don't worry about the FRONT parameter.
2539 while (p
!= pdl
->begin());
2541 // There are no TLS sections yet; put this one at the requested
2542 // location in the section list.
2546 pdl
->push_front(os
);
2551 // Remove an Output_section from this segment. It is an error if it
2555 Output_segment::remove_output_section(Output_section
* os
)
2557 // We only need this for SHT_PROGBITS.
2558 gold_assert(os
->type() == elfcpp::SHT_PROGBITS
);
2559 for (Output_data_list::iterator p
= this->output_data_
.begin();
2560 p
!= this->output_data_
.end();
2565 this->output_data_
.erase(p
);
2572 // Add an Output_data (which is not an Output_section) to the start of
2576 Output_segment::add_initial_output_data(Output_data
* od
)
2578 gold_assert(!this->is_max_align_known_
);
2579 this->output_data_
.push_front(od
);
2582 // Return the maximum alignment of the Output_data in Output_segment.
2585 Output_segment::maximum_alignment()
2587 if (!this->is_max_align_known_
)
2591 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2592 if (addralign
> this->max_align_
)
2593 this->max_align_
= addralign
;
2595 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2596 if (addralign
> this->max_align_
)
2597 this->max_align_
= addralign
;
2599 this->is_max_align_known_
= true;
2602 return this->max_align_
;
2605 // Return the maximum alignment of a list of Output_data.
2608 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2611 for (Output_data_list::const_iterator p
= pdl
->begin();
2615 uint64_t addralign
= (*p
)->addralign();
2616 if (addralign
> ret
)
2622 // Return the number of dynamic relocs applied to this segment.
2625 Output_segment::dynamic_reloc_count() const
2627 return (this->dynamic_reloc_count_list(&this->output_data_
)
2628 + this->dynamic_reloc_count_list(&this->output_bss_
));
2631 // Return the number of dynamic relocs applied to an Output_data_list.
2634 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2636 unsigned int count
= 0;
2637 for (Output_data_list::const_iterator p
= pdl
->begin();
2640 count
+= (*p
)->dynamic_reloc_count();
2644 // Set the section addresses for an Output_segment. If RESET is true,
2645 // reset the addresses first. ADDR is the address and *POFF is the
2646 // file offset. Set the section indexes starting with *PSHNDX.
2647 // Return the address of the immediately following segment. Update
2648 // *POFF and *PSHNDX.
2651 Output_segment::set_section_addresses(const Layout
* layout
, bool reset
,
2652 uint64_t addr
, off_t
* poff
,
2653 unsigned int* pshndx
)
2655 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2657 if (!reset
&& this->are_addresses_set_
)
2659 gold_assert(this->paddr_
== addr
);
2660 addr
= this->vaddr_
;
2664 this->vaddr_
= addr
;
2665 this->paddr_
= addr
;
2666 this->are_addresses_set_
= true;
2669 bool in_tls
= false;
2671 off_t orig_off
= *poff
;
2672 this->offset_
= orig_off
;
2674 addr
= this->set_section_list_addresses(layout
, reset
, &this->output_data_
,
2675 addr
, poff
, pshndx
, &in_tls
);
2676 this->filesz_
= *poff
- orig_off
;
2680 uint64_t ret
= this->set_section_list_addresses(layout
, reset
,
2685 // If the last section was a TLS section, align upward to the
2686 // alignment of the TLS segment, so that the overall size of the TLS
2687 // segment is aligned.
2690 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
2691 *poff
= align_address(*poff
, segment_align
);
2694 this->memsz_
= *poff
- orig_off
;
2696 // Ignore the file offset adjustments made by the BSS Output_data
2703 // Set the addresses and file offsets in a list of Output_data
2707 Output_segment::set_section_list_addresses(const Layout
* layout
, bool reset
,
2708 Output_data_list
* pdl
,
2709 uint64_t addr
, off_t
* poff
,
2710 unsigned int* pshndx
,
2713 off_t startoff
= *poff
;
2715 off_t off
= startoff
;
2716 for (Output_data_list::iterator p
= pdl
->begin();
2721 (*p
)->reset_address_and_file_offset();
2723 // When using a linker script the section will most likely
2724 // already have an address.
2725 if (!(*p
)->is_address_valid())
2727 uint64_t align
= (*p
)->addralign();
2729 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2731 // Give the first TLS section the alignment of the
2732 // entire TLS segment. Otherwise the TLS segment as a
2733 // whole may be misaligned.
2736 Output_segment
* tls_segment
= layout
->tls_segment();
2737 gold_assert(tls_segment
!= NULL
);
2738 uint64_t segment_align
= tls_segment
->maximum_alignment();
2739 gold_assert(segment_align
>= align
);
2740 align
= segment_align
;
2747 // If this is the first section after the TLS segment,
2748 // align it to at least the alignment of the TLS
2749 // segment, so that the size of the overall TLS segment
2753 uint64_t segment_align
=
2754 layout
->tls_segment()->maximum_alignment();
2755 if (segment_align
> align
)
2756 align
= segment_align
;
2762 off
= align_address(off
, align
);
2763 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2767 // The script may have inserted a skip forward, but it
2768 // better not have moved backward.
2769 gold_assert((*p
)->address() >= addr
+ (off
- startoff
));
2770 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
2771 (*p
)->set_file_offset(off
);
2772 (*p
)->finalize_data_size();
2775 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2776 // section. Such a section does not affect the size of a
2778 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2779 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
2780 off
+= (*p
)->data_size();
2782 if ((*p
)->is_section())
2784 (*p
)->set_out_shndx(*pshndx
);
2790 return addr
+ (off
- startoff
);
2793 // For a non-PT_LOAD segment, set the offset from the sections, if
2797 Output_segment::set_offset()
2799 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
2801 gold_assert(!this->are_addresses_set_
);
2803 if (this->output_data_
.empty() && this->output_bss_
.empty())
2807 this->are_addresses_set_
= true;
2809 this->min_p_align_
= 0;
2815 const Output_data
* first
;
2816 if (this->output_data_
.empty())
2817 first
= this->output_bss_
.front();
2819 first
= this->output_data_
.front();
2820 this->vaddr_
= first
->address();
2821 this->paddr_
= (first
->has_load_address()
2822 ? first
->load_address()
2824 this->are_addresses_set_
= true;
2825 this->offset_
= first
->offset();
2827 if (this->output_data_
.empty())
2831 const Output_data
* last_data
= this->output_data_
.back();
2832 this->filesz_
= (last_data
->address()
2833 + last_data
->data_size()
2837 const Output_data
* last
;
2838 if (this->output_bss_
.empty())
2839 last
= this->output_data_
.back();
2841 last
= this->output_bss_
.back();
2842 this->memsz_
= (last
->address()
2846 // If this is a TLS segment, align the memory size. The code in
2847 // set_section_list ensures that the section after the TLS segment
2848 // is aligned to give us room.
2849 if (this->type_
== elfcpp::PT_TLS
)
2851 uint64_t segment_align
= this->maximum_alignment();
2852 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
2853 this->memsz_
= align_address(this->memsz_
, segment_align
);
2857 // Set the TLS offsets of the sections in the PT_TLS segment.
2860 Output_segment::set_tls_offsets()
2862 gold_assert(this->type_
== elfcpp::PT_TLS
);
2864 for (Output_data_list::iterator p
= this->output_data_
.begin();
2865 p
!= this->output_data_
.end();
2867 (*p
)->set_tls_offset(this->vaddr_
);
2869 for (Output_data_list::iterator p
= this->output_bss_
.begin();
2870 p
!= this->output_bss_
.end();
2872 (*p
)->set_tls_offset(this->vaddr_
);
2875 // Return the address of the first section.
2878 Output_segment::first_section_load_address() const
2880 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
2881 p
!= this->output_data_
.end();
2883 if ((*p
)->is_section())
2884 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2886 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
2887 p
!= this->output_bss_
.end();
2889 if ((*p
)->is_section())
2890 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2895 // Return the number of Output_sections in an Output_segment.
2898 Output_segment::output_section_count() const
2900 return (this->output_section_count_list(&this->output_data_
)
2901 + this->output_section_count_list(&this->output_bss_
));
2904 // Return the number of Output_sections in an Output_data_list.
2907 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
2909 unsigned int count
= 0;
2910 for (Output_data_list::const_iterator p
= pdl
->begin();
2914 if ((*p
)->is_section())
2920 // Return the section attached to the list segment with the lowest
2921 // load address. This is used when handling a PHDRS clause in a
2925 Output_segment::section_with_lowest_load_address() const
2927 Output_section
* found
= NULL
;
2928 uint64_t found_lma
= 0;
2929 this->lowest_load_address_in_list(&this->output_data_
, &found
, &found_lma
);
2931 Output_section
* found_data
= found
;
2932 this->lowest_load_address_in_list(&this->output_bss_
, &found
, &found_lma
);
2933 if (found
!= found_data
&& found_data
!= NULL
)
2935 gold_error(_("nobits section %s may not precede progbits section %s "
2937 found
->name(), found_data
->name());
2944 // Look through a list for a section with a lower load address.
2947 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
2948 Output_section
** found
,
2949 uint64_t* found_lma
) const
2951 for (Output_data_list::const_iterator p
= pdl
->begin();
2955 if (!(*p
)->is_section())
2957 Output_section
* os
= static_cast<Output_section
*>(*p
);
2958 uint64_t lma
= (os
->has_load_address()
2959 ? os
->load_address()
2961 if (*found
== NULL
|| lma
< *found_lma
)
2969 // Write the segment data into *OPHDR.
2971 template<int size
, bool big_endian
>
2973 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
2975 ophdr
->put_p_type(this->type_
);
2976 ophdr
->put_p_offset(this->offset_
);
2977 ophdr
->put_p_vaddr(this->vaddr_
);
2978 ophdr
->put_p_paddr(this->paddr_
);
2979 ophdr
->put_p_filesz(this->filesz_
);
2980 ophdr
->put_p_memsz(this->memsz_
);
2981 ophdr
->put_p_flags(this->flags_
);
2982 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
2985 // Write the section headers into V.
2987 template<int size
, bool big_endian
>
2989 Output_segment::write_section_headers(const Layout
* layout
,
2990 const Stringpool
* secnamepool
,
2992 unsigned int *pshndx
) const
2994 // Every section that is attached to a segment must be attached to a
2995 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2997 if (this->type_
!= elfcpp::PT_LOAD
)
3000 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3001 &this->output_data_
,
3003 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3009 template<int size
, bool big_endian
>
3011 Output_segment::write_section_headers_list(const Layout
* layout
,
3012 const Stringpool
* secnamepool
,
3013 const Output_data_list
* pdl
,
3015 unsigned int* pshndx
) const
3017 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
3018 for (Output_data_list::const_iterator p
= pdl
->begin();
3022 if ((*p
)->is_section())
3024 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
3025 gold_assert(*pshndx
== ps
->out_shndx());
3026 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
3027 ps
->write_header(layout
, secnamepool
, &oshdr
);
3035 // Output_file methods.
3037 Output_file::Output_file(const char* name
)
3042 map_is_anonymous_(false),
3043 is_temporary_(false)
3047 // Open the output file.
3050 Output_file::open(off_t file_size
)
3052 this->file_size_
= file_size
;
3054 // Unlink the file first; otherwise the open() may fail if the file
3055 // is busy (e.g. it's an executable that's currently being executed).
3057 // However, the linker may be part of a system where a zero-length
3058 // file is created for it to write to, with tight permissions (gcc
3059 // 2.95 did something like this). Unlinking the file would work
3060 // around those permission controls, so we only unlink if the file
3061 // has a non-zero size. We also unlink only regular files to avoid
3062 // trouble with directories/etc.
3064 // If we fail, continue; this command is merely a best-effort attempt
3065 // to improve the odds for open().
3067 // We let the name "-" mean "stdout"
3068 if (!this->is_temporary_
)
3070 if (strcmp(this->name_
, "-") == 0)
3071 this->o_
= STDOUT_FILENO
;
3075 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
3076 unlink_if_ordinary(this->name_
);
3078 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
3079 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
3081 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
3089 // Resize the output file.
3092 Output_file::resize(off_t file_size
)
3094 // If the mmap is mapping an anonymous memory buffer, this is easy:
3095 // just mremap to the new size. If it's mapping to a file, we want
3096 // to unmap to flush to the file, then remap after growing the file.
3097 if (this->map_is_anonymous_
)
3099 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
3101 if (base
== MAP_FAILED
)
3102 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
3103 this->base_
= static_cast<unsigned char*>(base
);
3104 this->file_size_
= file_size
;
3109 this->file_size_
= file_size
;
3114 // Map the file into memory.
3119 const int o
= this->o_
;
3121 // If the output file is not a regular file, don't try to mmap it;
3122 // instead, we'll mmap a block of memory (an anonymous buffer), and
3123 // then later write the buffer to the file.
3125 struct stat statbuf
;
3126 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
3127 || ::fstat(o
, &statbuf
) != 0
3128 || !S_ISREG(statbuf
.st_mode
)
3129 || this->is_temporary_
)
3131 this->map_is_anonymous_
= true;
3132 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3133 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3137 // Write out one byte to make the file the right size.
3138 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
3139 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
3141 if (::write(o
, &b
, 1) != 1)
3142 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
3144 // Map the file into memory.
3145 this->map_is_anonymous_
= false;
3146 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3149 if (base
== MAP_FAILED
)
3150 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
3151 this->base_
= static_cast<unsigned char*>(base
);
3154 // Unmap the file from memory.
3157 Output_file::unmap()
3159 if (::munmap(this->base_
, this->file_size_
) < 0)
3160 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
3164 // Close the output file.
3167 Output_file::close()
3169 // If the map isn't file-backed, we need to write it now.
3170 if (this->map_is_anonymous_
&& !this->is_temporary_
)
3172 size_t bytes_to_write
= this->file_size_
;
3173 while (bytes_to_write
> 0)
3175 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
3176 if (bytes_written
== 0)
3177 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
3178 else if (bytes_written
< 0)
3179 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
3181 bytes_to_write
-= bytes_written
;
3186 // We don't close stdout or stderr
3187 if (this->o_
!= STDOUT_FILENO
3188 && this->o_
!= STDERR_FILENO
3189 && !this->is_temporary_
)
3190 if (::close(this->o_
) < 0)
3191 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
3195 // Instantiate the templates we need. We could use the configure
3196 // script to restrict this to only the ones for implemented targets.
3198 #ifdef HAVE_TARGET_32_LITTLE
3201 Output_section::add_input_section
<32, false>(
3202 Sized_relobj
<32, false>* object
,
3204 const char* secname
,
3205 const elfcpp::Shdr
<32, false>& shdr
,
3206 unsigned int reloc_shndx
,
3207 bool have_sections_script
);
3210 #ifdef HAVE_TARGET_32_BIG
3213 Output_section::add_input_section
<32, true>(
3214 Sized_relobj
<32, true>* object
,
3216 const char* secname
,
3217 const elfcpp::Shdr
<32, true>& shdr
,
3218 unsigned int reloc_shndx
,
3219 bool have_sections_script
);
3222 #ifdef HAVE_TARGET_64_LITTLE
3225 Output_section::add_input_section
<64, false>(
3226 Sized_relobj
<64, false>* object
,
3228 const char* secname
,
3229 const elfcpp::Shdr
<64, false>& shdr
,
3230 unsigned int reloc_shndx
,
3231 bool have_sections_script
);
3234 #ifdef HAVE_TARGET_64_BIG
3237 Output_section::add_input_section
<64, true>(
3238 Sized_relobj
<64, true>* object
,
3240 const char* secname
,
3241 const elfcpp::Shdr
<64, true>& shdr
,
3242 unsigned int reloc_shndx
,
3243 bool have_sections_script
);
3246 #ifdef HAVE_TARGET_32_LITTLE
3248 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
3251 #ifdef HAVE_TARGET_32_BIG
3253 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
3256 #ifdef HAVE_TARGET_64_LITTLE
3258 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
3261 #ifdef HAVE_TARGET_64_BIG
3263 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
3266 #ifdef HAVE_TARGET_32_LITTLE
3268 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
3271 #ifdef HAVE_TARGET_32_BIG
3273 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
3276 #ifdef HAVE_TARGET_64_LITTLE
3278 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
3281 #ifdef HAVE_TARGET_64_BIG
3283 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
3286 #ifdef HAVE_TARGET_32_LITTLE
3288 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
3291 #ifdef HAVE_TARGET_32_BIG
3293 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
3296 #ifdef HAVE_TARGET_64_LITTLE
3298 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3301 #ifdef HAVE_TARGET_64_BIG
3303 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3306 #ifdef HAVE_TARGET_32_LITTLE
3308 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3311 #ifdef HAVE_TARGET_32_BIG
3313 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3316 #ifdef HAVE_TARGET_64_LITTLE
3318 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3321 #ifdef HAVE_TARGET_64_BIG
3323 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3326 #ifdef HAVE_TARGET_32_LITTLE
3328 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
3331 #ifdef HAVE_TARGET_32_BIG
3333 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
3336 #ifdef HAVE_TARGET_64_LITTLE
3338 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
3341 #ifdef HAVE_TARGET_64_BIG
3343 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
3346 #ifdef HAVE_TARGET_32_LITTLE
3348 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
3351 #ifdef HAVE_TARGET_32_BIG
3353 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
3356 #ifdef HAVE_TARGET_64_LITTLE
3358 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
3361 #ifdef HAVE_TARGET_64_BIG
3363 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
3366 #ifdef HAVE_TARGET_32_LITTLE
3368 class Output_data_group
<32, false>;
3371 #ifdef HAVE_TARGET_32_BIG
3373 class Output_data_group
<32, true>;
3376 #ifdef HAVE_TARGET_64_LITTLE
3378 class Output_data_group
<64, false>;
3381 #ifdef HAVE_TARGET_64_BIG
3383 class Output_data_group
<64, true>;
3386 #ifdef HAVE_TARGET_32_LITTLE
3388 class Output_data_got
<32, false>;
3391 #ifdef HAVE_TARGET_32_BIG
3393 class Output_data_got
<32, true>;
3396 #ifdef HAVE_TARGET_64_LITTLE
3398 class Output_data_got
<64, false>;
3401 #ifdef HAVE_TARGET_64_BIG
3403 class Output_data_got
<64, true>;
3406 } // End namespace gold.