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->classification_
)
1347 case DYNAMIC_NUMBER
:
1351 case DYNAMIC_SECTION_ADDRESS
:
1352 val
= this->u_
.od
->address();
1355 case DYNAMIC_SECTION_SIZE
:
1356 val
= this->u_
.od
->data_size();
1359 case DYNAMIC_SYMBOL
:
1361 const Sized_symbol
<size
>* s
=
1362 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1367 case DYNAMIC_STRING
:
1368 val
= pool
->get_offset(this->u_
.str
);
1375 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1376 dw
.put_d_tag(this->tag_
);
1380 // Output_data_dynamic methods.
1382 // Adjust the output section to set the entry size.
1385 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1387 if (parameters
->target().get_size() == 32)
1388 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1389 else if (parameters
->target().get_size() == 64)
1390 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1395 // Set the final data size.
1398 Output_data_dynamic::set_final_data_size()
1400 // Add the terminating entry.
1401 this->add_constant(elfcpp::DT_NULL
, 0);
1404 if (parameters
->target().get_size() == 32)
1405 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1406 else if (parameters
->target().get_size() == 64)
1407 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1410 this->set_data_size(this->entries_
.size() * dyn_size
);
1413 // Write out the dynamic entries.
1416 Output_data_dynamic::do_write(Output_file
* of
)
1418 switch (parameters
->size_and_endianness())
1420 #ifdef HAVE_TARGET_32_LITTLE
1421 case Parameters::TARGET_32_LITTLE
:
1422 this->sized_write
<32, false>(of
);
1425 #ifdef HAVE_TARGET_32_BIG
1426 case Parameters::TARGET_32_BIG
:
1427 this->sized_write
<32, true>(of
);
1430 #ifdef HAVE_TARGET_64_LITTLE
1431 case Parameters::TARGET_64_LITTLE
:
1432 this->sized_write
<64, false>(of
);
1435 #ifdef HAVE_TARGET_64_BIG
1436 case Parameters::TARGET_64_BIG
:
1437 this->sized_write
<64, true>(of
);
1445 template<int size
, bool big_endian
>
1447 Output_data_dynamic::sized_write(Output_file
* of
)
1449 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1451 const off_t offset
= this->offset();
1452 const off_t oview_size
= this->data_size();
1453 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1455 unsigned char* pov
= oview
;
1456 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1457 p
!= this->entries_
.end();
1460 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1464 gold_assert(pov
- oview
== oview_size
);
1466 of
->write_output_view(offset
, oview_size
, oview
);
1468 // We no longer need the dynamic entries.
1469 this->entries_
.clear();
1472 // Output_section::Input_section methods.
1474 // Return the data size. For an input section we store the size here.
1475 // For an Output_section_data, we have to ask it for the size.
1478 Output_section::Input_section::data_size() const
1480 if (this->is_input_section())
1481 return this->u1_
.data_size
;
1483 return this->u2_
.posd
->data_size();
1486 // Set the address and file offset.
1489 Output_section::Input_section::set_address_and_file_offset(
1492 off_t section_file_offset
)
1494 if (this->is_input_section())
1495 this->u2_
.object
->set_section_offset(this->shndx_
,
1496 file_offset
- section_file_offset
);
1498 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1501 // Reset the address and file offset.
1504 Output_section::Input_section::reset_address_and_file_offset()
1506 if (!this->is_input_section())
1507 this->u2_
.posd
->reset_address_and_file_offset();
1510 // Finalize the data size.
1513 Output_section::Input_section::finalize_data_size()
1515 if (!this->is_input_section())
1516 this->u2_
.posd
->finalize_data_size();
1519 // Try to turn an input offset into an output offset. We want to
1520 // return the output offset relative to the start of this
1521 // Input_section in the output section.
1524 Output_section::Input_section::output_offset(
1525 const Relobj
* object
,
1527 section_offset_type offset
,
1528 section_offset_type
*poutput
) const
1530 if (!this->is_input_section())
1531 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1534 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1541 // Return whether this is the merge section for the input section
1545 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1546 unsigned int shndx
) const
1548 if (this->is_input_section())
1550 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1553 // Write out the data. We don't have to do anything for an input
1554 // section--they are handled via Object::relocate--but this is where
1555 // we write out the data for an Output_section_data.
1558 Output_section::Input_section::write(Output_file
* of
)
1560 if (!this->is_input_section())
1561 this->u2_
.posd
->write(of
);
1564 // Write the data to a buffer. As for write(), we don't have to do
1565 // anything for an input section.
1568 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1570 if (!this->is_input_section())
1571 this->u2_
.posd
->write_to_buffer(buffer
);
1574 // Output_section methods.
1576 // Construct an Output_section. NAME will point into a Stringpool.
1578 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1579 elfcpp::Elf_Xword flags
)
1584 link_section_(NULL
),
1586 info_section_(NULL
),
1595 first_input_offset_(0),
1597 postprocessing_buffer_(NULL
),
1598 needs_symtab_index_(false),
1599 needs_dynsym_index_(false),
1600 should_link_to_symtab_(false),
1601 should_link_to_dynsym_(false),
1602 after_input_sections_(false),
1603 requires_postprocessing_(false),
1604 found_in_sections_clause_(false),
1605 has_load_address_(false),
1606 info_uses_section_index_(false),
1607 may_sort_attached_input_sections_(false),
1608 must_sort_attached_input_sections_(false),
1609 attached_input_sections_are_sorted_(false),
1612 // An unallocated section has no address. Forcing this means that
1613 // we don't need special treatment for symbols defined in debug
1615 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1616 this->set_address(0);
1619 Output_section::~Output_section()
1623 // Set the entry size.
1626 Output_section::set_entsize(uint64_t v
)
1628 if (this->entsize_
== 0)
1631 gold_assert(this->entsize_
== v
);
1634 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1635 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1636 // relocation section which applies to this section, or 0 if none, or
1637 // -1U if more than one. Return the offset of the input section
1638 // within the output section. Return -1 if the input section will
1639 // receive special handling. In the normal case we don't always keep
1640 // track of input sections for an Output_section. Instead, each
1641 // Object keeps track of the Output_section for each of its input
1642 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1643 // track of input sections here; this is used when SECTIONS appears in
1646 template<int size
, bool big_endian
>
1648 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1650 const char* secname
,
1651 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1652 unsigned int reloc_shndx
,
1653 bool have_sections_script
)
1655 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1656 if ((addralign
& (addralign
- 1)) != 0)
1658 object
->error(_("invalid alignment %lu for section \"%s\""),
1659 static_cast<unsigned long>(addralign
), secname
);
1663 if (addralign
> this->addralign_
)
1664 this->addralign_
= addralign
;
1666 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1667 this->flags_
|= (sh_flags
1668 & (elfcpp::SHF_WRITE
1670 | elfcpp::SHF_EXECINSTR
));
1672 uint64_t entsize
= shdr
.get_sh_entsize();
1674 // .debug_str is a mergeable string section, but is not always so
1675 // marked by compilers. Mark manually here so we can optimize.
1676 if (strcmp(secname
, ".debug_str") == 0)
1678 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1682 // If this is a SHF_MERGE section, we pass all the input sections to
1683 // a Output_data_merge. We don't try to handle relocations for such
1685 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1686 && reloc_shndx
== 0)
1688 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1689 entsize
, addralign
))
1691 // Tell the relocation routines that they need to call the
1692 // output_offset method to determine the final address.
1697 off_t offset_in_section
= this->current_data_size_for_child();
1698 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1701 if (aligned_offset_in_section
> offset_in_section
1702 && !have_sections_script
1703 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1704 && object
->target()->has_code_fill())
1706 // We need to add some fill data. Using fill_list_ when
1707 // possible is an optimization, since we will often have fill
1708 // sections without input sections.
1709 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1710 if (this->input_sections_
.empty())
1711 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1714 // FIXME: When relaxing, the size needs to adjust to
1715 // maintain a constant alignment.
1716 std::string
fill_data(object
->target()->code_fill(fill_len
));
1717 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1718 this->input_sections_
.push_back(Input_section(odc
));
1722 this->set_current_data_size_for_child(aligned_offset_in_section
1723 + shdr
.get_sh_size());
1725 // We need to keep track of this section if we are already keeping
1726 // track of sections, or if we are relaxing. Also, if this is a
1727 // section which requires sorting, or which may require sorting in
1728 // the future, we keep track of the sections. FIXME: Add test for
1730 if (have_sections_script
1731 || !this->input_sections_
.empty()
1732 || this->may_sort_attached_input_sections()
1733 || this->must_sort_attached_input_sections())
1734 this->input_sections_
.push_back(Input_section(object
, shndx
,
1738 return aligned_offset_in_section
;
1741 // Add arbitrary data to an output section.
1744 Output_section::add_output_section_data(Output_section_data
* posd
)
1746 Input_section
inp(posd
);
1747 this->add_output_section_data(&inp
);
1749 if (posd
->is_data_size_valid())
1751 off_t offset_in_section
= this->current_data_size_for_child();
1752 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1754 this->set_current_data_size_for_child(aligned_offset_in_section
1755 + posd
->data_size());
1759 // Add arbitrary data to an output section by Input_section.
1762 Output_section::add_output_section_data(Input_section
* inp
)
1764 if (this->input_sections_
.empty())
1765 this->first_input_offset_
= this->current_data_size_for_child();
1767 this->input_sections_
.push_back(*inp
);
1769 uint64_t addralign
= inp
->addralign();
1770 if (addralign
> this->addralign_
)
1771 this->addralign_
= addralign
;
1773 inp
->set_output_section(this);
1776 // Add a merge section to an output section.
1779 Output_section::add_output_merge_section(Output_section_data
* posd
,
1780 bool is_string
, uint64_t entsize
)
1782 Input_section
inp(posd
, is_string
, entsize
);
1783 this->add_output_section_data(&inp
);
1786 // Add an input section to a SHF_MERGE section.
1789 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1790 uint64_t flags
, uint64_t entsize
,
1793 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1795 // We only merge strings if the alignment is not more than the
1796 // character size. This could be handled, but it's unusual.
1797 if (is_string
&& addralign
> entsize
)
1800 Input_section_list::iterator p
;
1801 for (p
= this->input_sections_
.begin();
1802 p
!= this->input_sections_
.end();
1804 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1806 p
->add_input_section(object
, shndx
);
1810 // We handle the actual constant merging in Output_merge_data or
1811 // Output_merge_string_data.
1812 Output_section_data
* posd
;
1814 posd
= new Output_merge_data(entsize
, addralign
);
1820 posd
= new Output_merge_string
<char>(addralign
);
1823 posd
= new Output_merge_string
<uint16_t>(addralign
);
1826 posd
= new Output_merge_string
<uint32_t>(addralign
);
1833 this->add_output_merge_section(posd
, is_string
, entsize
);
1834 posd
->add_input_section(object
, shndx
);
1839 // Given an address OFFSET relative to the start of input section
1840 // SHNDX in OBJECT, return whether this address is being included in
1841 // the final link. This should only be called if SHNDX in OBJECT has
1842 // a special mapping.
1845 Output_section::is_input_address_mapped(const Relobj
* object
,
1849 gold_assert(object
->is_section_specially_mapped(shndx
));
1851 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1852 p
!= this->input_sections_
.end();
1855 section_offset_type output_offset
;
1856 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1857 return output_offset
!= -1;
1860 // By default we assume that the address is mapped. This should
1861 // only be called after we have passed all sections to Layout. At
1862 // that point we should know what we are discarding.
1866 // Given an address OFFSET relative to the start of input section
1867 // SHNDX in object OBJECT, return the output offset relative to the
1868 // start of the input section in the output section. This should only
1869 // be called if SHNDX in OBJECT has a special mapping.
1872 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
1873 section_offset_type offset
) const
1875 gold_assert(object
->is_section_specially_mapped(shndx
));
1876 // This can only be called meaningfully when layout is complete.
1877 gold_assert(Output_data::is_layout_complete());
1879 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1880 p
!= this->input_sections_
.end();
1883 section_offset_type output_offset
;
1884 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1885 return output_offset
;
1890 // Return the output virtual address of OFFSET relative to the start
1891 // of input section SHNDX in object OBJECT.
1894 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1897 gold_assert(object
->is_section_specially_mapped(shndx
));
1899 uint64_t addr
= this->address() + this->first_input_offset_
;
1900 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1901 p
!= this->input_sections_
.end();
1904 addr
= align_address(addr
, p
->addralign());
1905 section_offset_type output_offset
;
1906 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1908 if (output_offset
== -1)
1910 return addr
+ output_offset
;
1912 addr
+= p
->data_size();
1915 // If we get here, it means that we don't know the mapping for this
1916 // input section. This might happen in principle if
1917 // add_input_section were called before add_output_section_data.
1918 // But it should never actually happen.
1923 // Return the output address of the start of the merged section for
1924 // input section SHNDX in object OBJECT.
1927 Output_section::starting_output_address(const Relobj
* object
,
1928 unsigned int shndx
) const
1930 gold_assert(object
->is_section_specially_mapped(shndx
));
1932 uint64_t addr
= this->address() + this->first_input_offset_
;
1933 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1934 p
!= this->input_sections_
.end();
1937 addr
= align_address(addr
, p
->addralign());
1939 // It would be nice if we could use the existing output_offset
1940 // method to get the output offset of input offset 0.
1941 // Unfortunately we don't know for sure that input offset 0 is
1943 if (p
->is_merge_section_for(object
, shndx
))
1946 addr
+= p
->data_size();
1951 // Set the data size of an Output_section. This is where we handle
1952 // setting the addresses of any Output_section_data objects.
1955 Output_section::set_final_data_size()
1957 if (this->input_sections_
.empty())
1959 this->set_data_size(this->current_data_size_for_child());
1963 if (this->must_sort_attached_input_sections())
1964 this->sort_attached_input_sections();
1966 uint64_t address
= this->address();
1967 off_t startoff
= this->offset();
1968 off_t off
= startoff
+ this->first_input_offset_
;
1969 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1970 p
!= this->input_sections_
.end();
1973 off
= align_address(off
, p
->addralign());
1974 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
1976 off
+= p
->data_size();
1979 this->set_data_size(off
- startoff
);
1982 // Reset the address and file offset.
1985 Output_section::do_reset_address_and_file_offset()
1987 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1988 p
!= this->input_sections_
.end();
1990 p
->reset_address_and_file_offset();
1993 // Set the TLS offset. Called only for SHT_TLS sections.
1996 Output_section::do_set_tls_offset(uint64_t tls_base
)
1998 this->tls_offset_
= this->address() - tls_base
;
2001 // In a few cases we need to sort the input sections attached to an
2002 // output section. This is used to implement the type of constructor
2003 // priority ordering implemented by the GNU linker, in which the
2004 // priority becomes part of the section name and the sections are
2005 // sorted by name. We only do this for an output section if we see an
2006 // attached input section matching ".ctor.*", ".dtor.*",
2007 // ".init_array.*" or ".fini_array.*".
2009 class Output_section::Input_section_sort_entry
2012 Input_section_sort_entry()
2013 : input_section_(), index_(-1U), section_has_name_(false),
2017 Input_section_sort_entry(const Input_section
& input_section
,
2019 : input_section_(input_section
), index_(index
),
2020 section_has_name_(input_section
.is_input_section())
2022 if (this->section_has_name_
)
2024 // This is only called single-threaded from Layout::finalize,
2025 // so it is OK to lock. Unfortunately we have no way to pass
2027 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
2028 Object
* obj
= input_section
.relobj();
2029 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
2031 // This is a slow operation, which should be cached in
2032 // Layout::layout if this becomes a speed problem.
2033 this->section_name_
= obj
->section_name(input_section
.shndx());
2037 // Return the Input_section.
2038 const Input_section
&
2039 input_section() const
2041 gold_assert(this->index_
!= -1U);
2042 return this->input_section_
;
2045 // The index of this entry in the original list. This is used to
2046 // make the sort stable.
2050 gold_assert(this->index_
!= -1U);
2051 return this->index_
;
2054 // Whether there is a section name.
2056 section_has_name() const
2057 { return this->section_has_name_
; }
2059 // The section name.
2061 section_name() const
2063 gold_assert(this->section_has_name_
);
2064 return this->section_name_
;
2067 // Return true if the section name has a priority. This is assumed
2068 // to be true if it has a dot after the initial dot.
2070 has_priority() const
2072 gold_assert(this->section_has_name_
);
2073 return this->section_name_
.find('.', 1);
2076 // Return true if this an input file whose base name matches
2077 // FILE_NAME. The base name must have an extension of ".o", and
2078 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2079 // This is to match crtbegin.o as well as crtbeginS.o without
2080 // getting confused by other possibilities. Overall matching the
2081 // file name this way is a dreadful hack, but the GNU linker does it
2082 // in order to better support gcc, and we need to be compatible.
2084 match_file_name(const char* match_file_name
) const
2086 const std::string
& file_name(this->input_section_
.relobj()->name());
2087 const char* base_name
= lbasename(file_name
.c_str());
2088 size_t match_len
= strlen(match_file_name
);
2089 if (strncmp(base_name
, match_file_name
, match_len
) != 0)
2091 size_t base_len
= strlen(base_name
);
2092 if (base_len
!= match_len
+ 2 && base_len
!= match_len
+ 3)
2094 return memcmp(base_name
+ base_len
- 2, ".o", 2) == 0;
2098 // The Input_section we are sorting.
2099 Input_section input_section_
;
2100 // The index of this Input_section in the original list.
2101 unsigned int index_
;
2102 // Whether this Input_section has a section name--it won't if this
2103 // is some random Output_section_data.
2104 bool section_has_name_
;
2105 // The section name if there is one.
2106 std::string section_name_
;
2109 // Return true if S1 should come before S2 in the output section.
2112 Output_section::Input_section_sort_compare::operator()(
2113 const Output_section::Input_section_sort_entry
& s1
,
2114 const Output_section::Input_section_sort_entry
& s2
) const
2116 // crtbegin.o must come first.
2117 bool s1_begin
= s1
.match_file_name("crtbegin");
2118 bool s2_begin
= s2
.match_file_name("crtbegin");
2119 if (s1_begin
|| s2_begin
)
2125 return s1
.index() < s2
.index();
2128 // crtend.o must come last.
2129 bool s1_end
= s1
.match_file_name("crtend");
2130 bool s2_end
= s2
.match_file_name("crtend");
2131 if (s1_end
|| s2_end
)
2137 return s1
.index() < s2
.index();
2140 // We sort all the sections with no names to the end.
2141 if (!s1
.section_has_name() || !s2
.section_has_name())
2143 if (s1
.section_has_name())
2145 if (s2
.section_has_name())
2147 return s1
.index() < s2
.index();
2150 // A section with a priority follows a section without a priority.
2151 // The GNU linker does this for all but .init_array sections; until
2152 // further notice we'll assume that that is an mistake.
2153 bool s1_has_priority
= s1
.has_priority();
2154 bool s2_has_priority
= s2
.has_priority();
2155 if (s1_has_priority
&& !s2_has_priority
)
2157 if (!s1_has_priority
&& s2_has_priority
)
2160 // Otherwise we sort by name.
2161 int compare
= s1
.section_name().compare(s2
.section_name());
2165 // Otherwise we keep the input order.
2166 return s1
.index() < s2
.index();
2169 // Sort the input sections attached to an output section.
2172 Output_section::sort_attached_input_sections()
2174 if (this->attached_input_sections_are_sorted_
)
2177 // The only thing we know about an input section is the object and
2178 // the section index. We need the section name. Recomputing this
2179 // is slow but this is an unusual case. If this becomes a speed
2180 // problem we can cache the names as required in Layout::layout.
2182 // We start by building a larger vector holding a copy of each
2183 // Input_section, plus its current index in the list and its name.
2184 std::vector
<Input_section_sort_entry
> sort_list
;
2187 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2188 p
!= this->input_sections_
.end();
2190 sort_list
.push_back(Input_section_sort_entry(*p
, i
));
2192 // Sort the input sections.
2193 std::sort(sort_list
.begin(), sort_list
.end(), Input_section_sort_compare());
2195 // Copy the sorted input sections back to our list.
2196 this->input_sections_
.clear();
2197 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
2198 p
!= sort_list
.end();
2200 this->input_sections_
.push_back(p
->input_section());
2202 // Remember that we sorted the input sections, since we might get
2204 this->attached_input_sections_are_sorted_
= true;
2207 // Write the section header to *OSHDR.
2209 template<int size
, bool big_endian
>
2211 Output_section::write_header(const Layout
* layout
,
2212 const Stringpool
* secnamepool
,
2213 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
2215 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
2216 oshdr
->put_sh_type(this->type_
);
2218 elfcpp::Elf_Xword flags
= this->flags_
;
2219 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
2220 flags
|= elfcpp::SHF_INFO_LINK
;
2221 oshdr
->put_sh_flags(flags
);
2223 oshdr
->put_sh_addr(this->address());
2224 oshdr
->put_sh_offset(this->offset());
2225 oshdr
->put_sh_size(this->data_size());
2226 if (this->link_section_
!= NULL
)
2227 oshdr
->put_sh_link(this->link_section_
->out_shndx());
2228 else if (this->should_link_to_symtab_
)
2229 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
2230 else if (this->should_link_to_dynsym_
)
2231 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
2233 oshdr
->put_sh_link(this->link_
);
2235 elfcpp::Elf_Word info
;
2236 if (this->info_section_
!= NULL
)
2238 if (this->info_uses_section_index_
)
2239 info
= this->info_section_
->out_shndx();
2241 info
= this->info_section_
->symtab_index();
2243 else if (this->info_symndx_
!= NULL
)
2244 info
= this->info_symndx_
->symtab_index();
2247 oshdr
->put_sh_info(info
);
2249 oshdr
->put_sh_addralign(this->addralign_
);
2250 oshdr
->put_sh_entsize(this->entsize_
);
2253 // Write out the data. For input sections the data is written out by
2254 // Object::relocate, but we have to handle Output_section_data objects
2258 Output_section::do_write(Output_file
* of
)
2260 gold_assert(!this->requires_postprocessing());
2262 off_t output_section_file_offset
= this->offset();
2263 for (Fill_list::iterator p
= this->fills_
.begin();
2264 p
!= this->fills_
.end();
2267 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2268 of
->write(output_section_file_offset
+ p
->section_offset(),
2269 fill_data
.data(), fill_data
.size());
2272 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2273 p
!= this->input_sections_
.end();
2278 // If a section requires postprocessing, create the buffer to use.
2281 Output_section::create_postprocessing_buffer()
2283 gold_assert(this->requires_postprocessing());
2285 if (this->postprocessing_buffer_
!= NULL
)
2288 if (!this->input_sections_
.empty())
2290 off_t off
= this->first_input_offset_
;
2291 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2292 p
!= this->input_sections_
.end();
2295 off
= align_address(off
, p
->addralign());
2296 p
->finalize_data_size();
2297 off
+= p
->data_size();
2299 this->set_current_data_size_for_child(off
);
2302 off_t buffer_size
= this->current_data_size_for_child();
2303 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
2306 // Write all the data of an Output_section into the postprocessing
2307 // buffer. This is used for sections which require postprocessing,
2308 // such as compression. Input sections are handled by
2309 // Object::Relocate.
2312 Output_section::write_to_postprocessing_buffer()
2314 gold_assert(this->requires_postprocessing());
2316 unsigned char* buffer
= this->postprocessing_buffer();
2317 for (Fill_list::iterator p
= this->fills_
.begin();
2318 p
!= this->fills_
.end();
2321 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2322 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
2326 off_t off
= this->first_input_offset_
;
2327 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2328 p
!= this->input_sections_
.end();
2331 off
= align_address(off
, p
->addralign());
2332 p
->write_to_buffer(buffer
+ off
);
2333 off
+= p
->data_size();
2337 // Get the input sections for linker script processing. We leave
2338 // behind the Output_section_data entries. Note that this may be
2339 // slightly incorrect for merge sections. We will leave them behind,
2340 // but it is possible that the script says that they should follow
2341 // some other input sections, as in:
2342 // .rodata { *(.rodata) *(.rodata.cst*) }
2343 // For that matter, we don't handle this correctly:
2344 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2345 // With luck this will never matter.
2348 Output_section::get_input_sections(
2350 const std::string
& fill
,
2351 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
2353 uint64_t orig_address
= address
;
2355 address
= align_address(address
, this->addralign());
2357 Input_section_list remaining
;
2358 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2359 p
!= this->input_sections_
.end();
2362 if (p
->is_input_section())
2363 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2366 uint64_t aligned_address
= align_address(address
, p
->addralign());
2367 if (aligned_address
!= address
&& !fill
.empty())
2369 section_size_type length
=
2370 convert_to_section_size_type(aligned_address
- address
);
2371 std::string this_fill
;
2372 this_fill
.reserve(length
);
2373 while (this_fill
.length() + fill
.length() <= length
)
2375 if (this_fill
.length() < length
)
2376 this_fill
.append(fill
, 0, length
- this_fill
.length());
2378 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2379 remaining
.push_back(Input_section(posd
));
2381 address
= aligned_address
;
2383 remaining
.push_back(*p
);
2385 p
->finalize_data_size();
2386 address
+= p
->data_size();
2390 this->input_sections_
.swap(remaining
);
2391 this->first_input_offset_
= 0;
2393 uint64_t data_size
= address
- orig_address
;
2394 this->set_current_data_size_for_child(data_size
);
2398 // Add an input section from a script.
2401 Output_section::add_input_section_for_script(Relobj
* object
,
2406 if (addralign
> this->addralign_
)
2407 this->addralign_
= addralign
;
2409 off_t offset_in_section
= this->current_data_size_for_child();
2410 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2413 this->set_current_data_size_for_child(aligned_offset_in_section
2416 this->input_sections_
.push_back(Input_section(object
, shndx
,
2417 data_size
, addralign
));
2420 // Print stats for merge sections to stderr.
2423 Output_section::print_merge_stats()
2425 Input_section_list::iterator p
;
2426 for (p
= this->input_sections_
.begin();
2427 p
!= this->input_sections_
.end();
2429 p
->print_merge_stats(this->name_
);
2432 // Output segment methods.
2434 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2446 is_max_align_known_(false),
2447 are_addresses_set_(false)
2451 // Add an Output_section to an Output_segment.
2454 Output_segment::add_output_section(Output_section
* os
,
2455 elfcpp::Elf_Word seg_flags
,
2458 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2459 gold_assert(!this->is_max_align_known_
);
2461 // Update the segment flags.
2462 this->flags_
|= seg_flags
;
2464 Output_segment::Output_data_list
* pdl
;
2465 if (os
->type() == elfcpp::SHT_NOBITS
)
2466 pdl
= &this->output_bss_
;
2468 pdl
= &this->output_data_
;
2470 // So that PT_NOTE segments will work correctly, we need to ensure
2471 // that all SHT_NOTE sections are adjacent. This will normally
2472 // happen automatically, because all the SHT_NOTE input sections
2473 // will wind up in the same output section. However, it is possible
2474 // for multiple SHT_NOTE input sections to have different section
2475 // flags, and thus be in different output sections, but for the
2476 // different section flags to map into the same segment flags and
2477 // thus the same output segment.
2479 // Note that while there may be many input sections in an output
2480 // section, there are normally only a few output sections in an
2481 // output segment. This loop is expected to be fast.
2483 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2485 Output_segment::Output_data_list::iterator p
= pdl
->end();
2489 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2491 // We don't worry about the FRONT parameter.
2497 while (p
!= pdl
->begin());
2500 // Similarly, so that PT_TLS segments will work, we need to group
2501 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2502 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2503 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2504 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2505 // and the PT_TLS segment -- we do this grouping only for the
2507 if (this->type_
!= elfcpp::PT_TLS
2508 && (os
->flags() & elfcpp::SHF_TLS
) != 0
2509 && !this->output_data_
.empty())
2511 pdl
= &this->output_data_
;
2512 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2513 bool sawtls
= false;
2514 Output_segment::Output_data_list::iterator p
= pdl
->end();
2519 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2522 // Put a NOBITS section after the first TLS section.
2523 // But a PROGBITS section after the first TLS/PROGBITS
2525 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2529 // If we've gone past the TLS sections, but we've seen a
2530 // TLS section, then we need to insert this section now.
2536 // We don't worry about the FRONT parameter.
2542 while (p
!= pdl
->begin());
2544 // There are no TLS sections yet; put this one at the requested
2545 // location in the section list.
2549 pdl
->push_front(os
);
2554 // Remove an Output_section from this segment. It is an error if it
2558 Output_segment::remove_output_section(Output_section
* os
)
2560 // We only need this for SHT_PROGBITS.
2561 gold_assert(os
->type() == elfcpp::SHT_PROGBITS
);
2562 for (Output_data_list::iterator p
= this->output_data_
.begin();
2563 p
!= this->output_data_
.end();
2568 this->output_data_
.erase(p
);
2575 // Add an Output_data (which is not an Output_section) to the start of
2579 Output_segment::add_initial_output_data(Output_data
* od
)
2581 gold_assert(!this->is_max_align_known_
);
2582 this->output_data_
.push_front(od
);
2585 // Return the maximum alignment of the Output_data in Output_segment.
2588 Output_segment::maximum_alignment()
2590 if (!this->is_max_align_known_
)
2594 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2595 if (addralign
> this->max_align_
)
2596 this->max_align_
= addralign
;
2598 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2599 if (addralign
> this->max_align_
)
2600 this->max_align_
= addralign
;
2602 this->is_max_align_known_
= true;
2605 return this->max_align_
;
2608 // Return the maximum alignment of a list of Output_data.
2611 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2614 for (Output_data_list::const_iterator p
= pdl
->begin();
2618 uint64_t addralign
= (*p
)->addralign();
2619 if (addralign
> ret
)
2625 // Return the number of dynamic relocs applied to this segment.
2628 Output_segment::dynamic_reloc_count() const
2630 return (this->dynamic_reloc_count_list(&this->output_data_
)
2631 + this->dynamic_reloc_count_list(&this->output_bss_
));
2634 // Return the number of dynamic relocs applied to an Output_data_list.
2637 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2639 unsigned int count
= 0;
2640 for (Output_data_list::const_iterator p
= pdl
->begin();
2643 count
+= (*p
)->dynamic_reloc_count();
2647 // Set the section addresses for an Output_segment. If RESET is true,
2648 // reset the addresses first. ADDR is the address and *POFF is the
2649 // file offset. Set the section indexes starting with *PSHNDX.
2650 // Return the address of the immediately following segment. Update
2651 // *POFF and *PSHNDX.
2654 Output_segment::set_section_addresses(const Layout
* layout
, bool reset
,
2655 uint64_t addr
, off_t
* poff
,
2656 unsigned int* pshndx
)
2658 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2660 if (!reset
&& this->are_addresses_set_
)
2662 gold_assert(this->paddr_
== addr
);
2663 addr
= this->vaddr_
;
2667 this->vaddr_
= addr
;
2668 this->paddr_
= addr
;
2669 this->are_addresses_set_
= true;
2672 bool in_tls
= false;
2674 off_t orig_off
= *poff
;
2675 this->offset_
= orig_off
;
2677 addr
= this->set_section_list_addresses(layout
, reset
, &this->output_data_
,
2678 addr
, poff
, pshndx
, &in_tls
);
2679 this->filesz_
= *poff
- orig_off
;
2683 uint64_t ret
= this->set_section_list_addresses(layout
, reset
,
2688 // If the last section was a TLS section, align upward to the
2689 // alignment of the TLS segment, so that the overall size of the TLS
2690 // segment is aligned.
2693 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
2694 *poff
= align_address(*poff
, segment_align
);
2697 this->memsz_
= *poff
- orig_off
;
2699 // Ignore the file offset adjustments made by the BSS Output_data
2706 // Set the addresses and file offsets in a list of Output_data
2710 Output_segment::set_section_list_addresses(const Layout
* layout
, bool reset
,
2711 Output_data_list
* pdl
,
2712 uint64_t addr
, off_t
* poff
,
2713 unsigned int* pshndx
,
2716 off_t startoff
= *poff
;
2718 off_t off
= startoff
;
2719 for (Output_data_list::iterator p
= pdl
->begin();
2724 (*p
)->reset_address_and_file_offset();
2726 // When using a linker script the section will most likely
2727 // already have an address.
2728 if (!(*p
)->is_address_valid())
2730 uint64_t align
= (*p
)->addralign();
2732 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2734 // Give the first TLS section the alignment of the
2735 // entire TLS segment. Otherwise the TLS segment as a
2736 // whole may be misaligned.
2739 Output_segment
* tls_segment
= layout
->tls_segment();
2740 gold_assert(tls_segment
!= NULL
);
2741 uint64_t segment_align
= tls_segment
->maximum_alignment();
2742 gold_assert(segment_align
>= align
);
2743 align
= segment_align
;
2750 // If this is the first section after the TLS segment,
2751 // align it to at least the alignment of the TLS
2752 // segment, so that the size of the overall TLS segment
2756 uint64_t segment_align
=
2757 layout
->tls_segment()->maximum_alignment();
2758 if (segment_align
> align
)
2759 align
= segment_align
;
2765 off
= align_address(off
, align
);
2766 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2770 // The script may have inserted a skip forward, but it
2771 // better not have moved backward.
2772 gold_assert((*p
)->address() >= addr
+ (off
- startoff
));
2773 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
2774 (*p
)->set_file_offset(off
);
2775 (*p
)->finalize_data_size();
2778 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2779 // section. Such a section does not affect the size of a
2781 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2782 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
2783 off
+= (*p
)->data_size();
2785 if ((*p
)->is_section())
2787 (*p
)->set_out_shndx(*pshndx
);
2793 return addr
+ (off
- startoff
);
2796 // For a non-PT_LOAD segment, set the offset from the sections, if
2800 Output_segment::set_offset()
2802 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
2804 gold_assert(!this->are_addresses_set_
);
2806 if (this->output_data_
.empty() && this->output_bss_
.empty())
2810 this->are_addresses_set_
= true;
2812 this->min_p_align_
= 0;
2818 const Output_data
* first
;
2819 if (this->output_data_
.empty())
2820 first
= this->output_bss_
.front();
2822 first
= this->output_data_
.front();
2823 this->vaddr_
= first
->address();
2824 this->paddr_
= (first
->has_load_address()
2825 ? first
->load_address()
2827 this->are_addresses_set_
= true;
2828 this->offset_
= first
->offset();
2830 if (this->output_data_
.empty())
2834 const Output_data
* last_data
= this->output_data_
.back();
2835 this->filesz_
= (last_data
->address()
2836 + last_data
->data_size()
2840 const Output_data
* last
;
2841 if (this->output_bss_
.empty())
2842 last
= this->output_data_
.back();
2844 last
= this->output_bss_
.back();
2845 this->memsz_
= (last
->address()
2849 // If this is a TLS segment, align the memory size. The code in
2850 // set_section_list ensures that the section after the TLS segment
2851 // is aligned to give us room.
2852 if (this->type_
== elfcpp::PT_TLS
)
2854 uint64_t segment_align
= this->maximum_alignment();
2855 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
2856 this->memsz_
= align_address(this->memsz_
, segment_align
);
2860 // Set the TLS offsets of the sections in the PT_TLS segment.
2863 Output_segment::set_tls_offsets()
2865 gold_assert(this->type_
== elfcpp::PT_TLS
);
2867 for (Output_data_list::iterator p
= this->output_data_
.begin();
2868 p
!= this->output_data_
.end();
2870 (*p
)->set_tls_offset(this->vaddr_
);
2872 for (Output_data_list::iterator p
= this->output_bss_
.begin();
2873 p
!= this->output_bss_
.end();
2875 (*p
)->set_tls_offset(this->vaddr_
);
2878 // Return the address of the first section.
2881 Output_segment::first_section_load_address() const
2883 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
2884 p
!= this->output_data_
.end();
2886 if ((*p
)->is_section())
2887 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2889 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
2890 p
!= this->output_bss_
.end();
2892 if ((*p
)->is_section())
2893 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2898 // Return the number of Output_sections in an Output_segment.
2901 Output_segment::output_section_count() const
2903 return (this->output_section_count_list(&this->output_data_
)
2904 + this->output_section_count_list(&this->output_bss_
));
2907 // Return the number of Output_sections in an Output_data_list.
2910 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
2912 unsigned int count
= 0;
2913 for (Output_data_list::const_iterator p
= pdl
->begin();
2917 if ((*p
)->is_section())
2923 // Return the section attached to the list segment with the lowest
2924 // load address. This is used when handling a PHDRS clause in a
2928 Output_segment::section_with_lowest_load_address() const
2930 Output_section
* found
= NULL
;
2931 uint64_t found_lma
= 0;
2932 this->lowest_load_address_in_list(&this->output_data_
, &found
, &found_lma
);
2934 Output_section
* found_data
= found
;
2935 this->lowest_load_address_in_list(&this->output_bss_
, &found
, &found_lma
);
2936 if (found
!= found_data
&& found_data
!= NULL
)
2938 gold_error(_("nobits section %s may not precede progbits section %s "
2940 found
->name(), found_data
->name());
2947 // Look through a list for a section with a lower load address.
2950 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
2951 Output_section
** found
,
2952 uint64_t* found_lma
) const
2954 for (Output_data_list::const_iterator p
= pdl
->begin();
2958 if (!(*p
)->is_section())
2960 Output_section
* os
= static_cast<Output_section
*>(*p
);
2961 uint64_t lma
= (os
->has_load_address()
2962 ? os
->load_address()
2964 if (*found
== NULL
|| lma
< *found_lma
)
2972 // Write the segment data into *OPHDR.
2974 template<int size
, bool big_endian
>
2976 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
2978 ophdr
->put_p_type(this->type_
);
2979 ophdr
->put_p_offset(this->offset_
);
2980 ophdr
->put_p_vaddr(this->vaddr_
);
2981 ophdr
->put_p_paddr(this->paddr_
);
2982 ophdr
->put_p_filesz(this->filesz_
);
2983 ophdr
->put_p_memsz(this->memsz_
);
2984 ophdr
->put_p_flags(this->flags_
);
2985 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
2988 // Write the section headers into V.
2990 template<int size
, bool big_endian
>
2992 Output_segment::write_section_headers(const Layout
* layout
,
2993 const Stringpool
* secnamepool
,
2995 unsigned int *pshndx
) const
2997 // Every section that is attached to a segment must be attached to a
2998 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3000 if (this->type_
!= elfcpp::PT_LOAD
)
3003 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3004 &this->output_data_
,
3006 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3012 template<int size
, bool big_endian
>
3014 Output_segment::write_section_headers_list(const Layout
* layout
,
3015 const Stringpool
* secnamepool
,
3016 const Output_data_list
* pdl
,
3018 unsigned int* pshndx
) const
3020 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
3021 for (Output_data_list::const_iterator p
= pdl
->begin();
3025 if ((*p
)->is_section())
3027 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
3028 gold_assert(*pshndx
== ps
->out_shndx());
3029 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
3030 ps
->write_header(layout
, secnamepool
, &oshdr
);
3038 // Output_file methods.
3040 Output_file::Output_file(const char* name
)
3045 map_is_anonymous_(false),
3046 is_temporary_(false)
3050 // Open the output file.
3053 Output_file::open(off_t file_size
)
3055 this->file_size_
= file_size
;
3057 // Unlink the file first; otherwise the open() may fail if the file
3058 // is busy (e.g. it's an executable that's currently being executed).
3060 // However, the linker may be part of a system where a zero-length
3061 // file is created for it to write to, with tight permissions (gcc
3062 // 2.95 did something like this). Unlinking the file would work
3063 // around those permission controls, so we only unlink if the file
3064 // has a non-zero size. We also unlink only regular files to avoid
3065 // trouble with directories/etc.
3067 // If we fail, continue; this command is merely a best-effort attempt
3068 // to improve the odds for open().
3070 // We let the name "-" mean "stdout"
3071 if (!this->is_temporary_
)
3073 if (strcmp(this->name_
, "-") == 0)
3074 this->o_
= STDOUT_FILENO
;
3078 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
3079 unlink_if_ordinary(this->name_
);
3081 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
3082 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
3084 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
3092 // Resize the output file.
3095 Output_file::resize(off_t file_size
)
3097 // If the mmap is mapping an anonymous memory buffer, this is easy:
3098 // just mremap to the new size. If it's mapping to a file, we want
3099 // to unmap to flush to the file, then remap after growing the file.
3100 if (this->map_is_anonymous_
)
3102 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
3104 if (base
== MAP_FAILED
)
3105 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
3106 this->base_
= static_cast<unsigned char*>(base
);
3107 this->file_size_
= file_size
;
3112 this->file_size_
= file_size
;
3117 // Map the file into memory.
3122 const int o
= this->o_
;
3124 // If the output file is not a regular file, don't try to mmap it;
3125 // instead, we'll mmap a block of memory (an anonymous buffer), and
3126 // then later write the buffer to the file.
3128 struct stat statbuf
;
3129 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
3130 || ::fstat(o
, &statbuf
) != 0
3131 || !S_ISREG(statbuf
.st_mode
)
3132 || this->is_temporary_
)
3134 this->map_is_anonymous_
= true;
3135 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3136 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3140 // Write out one byte to make the file the right size.
3141 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
3142 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
3144 if (::write(o
, &b
, 1) != 1)
3145 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
3147 // Map the file into memory.
3148 this->map_is_anonymous_
= false;
3149 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3152 if (base
== MAP_FAILED
)
3153 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
3154 this->base_
= static_cast<unsigned char*>(base
);
3157 // Unmap the file from memory.
3160 Output_file::unmap()
3162 if (::munmap(this->base_
, this->file_size_
) < 0)
3163 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
3167 // Close the output file.
3170 Output_file::close()
3172 // If the map isn't file-backed, we need to write it now.
3173 if (this->map_is_anonymous_
&& !this->is_temporary_
)
3175 size_t bytes_to_write
= this->file_size_
;
3176 while (bytes_to_write
> 0)
3178 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
3179 if (bytes_written
== 0)
3180 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
3181 else if (bytes_written
< 0)
3182 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
3184 bytes_to_write
-= bytes_written
;
3189 // We don't close stdout or stderr
3190 if (this->o_
!= STDOUT_FILENO
3191 && this->o_
!= STDERR_FILENO
3192 && !this->is_temporary_
)
3193 if (::close(this->o_
) < 0)
3194 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
3198 // Instantiate the templates we need. We could use the configure
3199 // script to restrict this to only the ones for implemented targets.
3201 #ifdef HAVE_TARGET_32_LITTLE
3204 Output_section::add_input_section
<32, false>(
3205 Sized_relobj
<32, false>* object
,
3207 const char* secname
,
3208 const elfcpp::Shdr
<32, false>& shdr
,
3209 unsigned int reloc_shndx
,
3210 bool have_sections_script
);
3213 #ifdef HAVE_TARGET_32_BIG
3216 Output_section::add_input_section
<32, true>(
3217 Sized_relobj
<32, true>* object
,
3219 const char* secname
,
3220 const elfcpp::Shdr
<32, true>& shdr
,
3221 unsigned int reloc_shndx
,
3222 bool have_sections_script
);
3225 #ifdef HAVE_TARGET_64_LITTLE
3228 Output_section::add_input_section
<64, false>(
3229 Sized_relobj
<64, false>* object
,
3231 const char* secname
,
3232 const elfcpp::Shdr
<64, false>& shdr
,
3233 unsigned int reloc_shndx
,
3234 bool have_sections_script
);
3237 #ifdef HAVE_TARGET_64_BIG
3240 Output_section::add_input_section
<64, true>(
3241 Sized_relobj
<64, true>* object
,
3243 const char* secname
,
3244 const elfcpp::Shdr
<64, true>& shdr
,
3245 unsigned int reloc_shndx
,
3246 bool have_sections_script
);
3249 #ifdef HAVE_TARGET_32_LITTLE
3251 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
3254 #ifdef HAVE_TARGET_32_BIG
3256 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
3259 #ifdef HAVE_TARGET_64_LITTLE
3261 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
3264 #ifdef HAVE_TARGET_64_BIG
3266 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
3269 #ifdef HAVE_TARGET_32_LITTLE
3271 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
3274 #ifdef HAVE_TARGET_32_BIG
3276 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
3279 #ifdef HAVE_TARGET_64_LITTLE
3281 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
3284 #ifdef HAVE_TARGET_64_BIG
3286 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
3289 #ifdef HAVE_TARGET_32_LITTLE
3291 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
3294 #ifdef HAVE_TARGET_32_BIG
3296 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
3299 #ifdef HAVE_TARGET_64_LITTLE
3301 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3304 #ifdef HAVE_TARGET_64_BIG
3306 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3309 #ifdef HAVE_TARGET_32_LITTLE
3311 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3314 #ifdef HAVE_TARGET_32_BIG
3316 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3319 #ifdef HAVE_TARGET_64_LITTLE
3321 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3324 #ifdef HAVE_TARGET_64_BIG
3326 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3329 #ifdef HAVE_TARGET_32_LITTLE
3331 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
3334 #ifdef HAVE_TARGET_32_BIG
3336 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
3339 #ifdef HAVE_TARGET_64_LITTLE
3341 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
3344 #ifdef HAVE_TARGET_64_BIG
3346 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
3349 #ifdef HAVE_TARGET_32_LITTLE
3351 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
3354 #ifdef HAVE_TARGET_32_BIG
3356 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
3359 #ifdef HAVE_TARGET_64_LITTLE
3361 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
3364 #ifdef HAVE_TARGET_64_BIG
3366 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
3369 #ifdef HAVE_TARGET_32_LITTLE
3371 class Output_data_group
<32, false>;
3374 #ifdef HAVE_TARGET_32_BIG
3376 class Output_data_group
<32, true>;
3379 #ifdef HAVE_TARGET_64_LITTLE
3381 class Output_data_group
<64, false>;
3384 #ifdef HAVE_TARGET_64_BIG
3386 class Output_data_group
<64, true>;
3389 #ifdef HAVE_TARGET_32_LITTLE
3391 class Output_data_got
<32, false>;
3394 #ifdef HAVE_TARGET_32_BIG
3396 class Output_data_got
<32, true>;
3399 #ifdef HAVE_TARGET_64_LITTLE
3401 class Output_data_got
<64, false>;
3404 #ifdef HAVE_TARGET_64_BIG
3406 class Output_data_got
<64, true>;
3409 } // End namespace gold.