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 // Set the alignment, which means we may need to update the alignment
539 // of the output section.
542 Output_section_data::set_addralign(uint64_t addralign
)
544 this->addralign_
= addralign
;
545 if (this->output_section_
!= NULL
546 && this->output_section_
->addralign() < addralign
)
547 this->output_section_
->set_addralign(addralign
);
550 // Output_data_strtab methods.
552 // Set the final data size.
555 Output_data_strtab::set_final_data_size()
557 this->strtab_
->set_string_offsets();
558 this->set_data_size(this->strtab_
->get_strtab_size());
561 // Write out a string table.
564 Output_data_strtab::do_write(Output_file
* of
)
566 this->strtab_
->write(of
, this->offset());
569 // Output_reloc methods.
571 // A reloc against a global symbol.
573 template<bool dynamic
, int size
, bool big_endian
>
574 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
580 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
581 is_relative_(is_relative
), is_section_symbol_(false), shndx_(INVALID_CODE
)
583 // this->type_ is a bitfield; make sure TYPE fits.
584 gold_assert(this->type_
== type
);
585 this->u1_
.gsym
= gsym
;
588 this->set_needs_dynsym_index();
591 template<bool dynamic
, int size
, bool big_endian
>
592 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
599 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
600 is_relative_(is_relative
), is_section_symbol_(false), shndx_(shndx
)
602 gold_assert(shndx
!= INVALID_CODE
);
603 // this->type_ is a bitfield; make sure TYPE fits.
604 gold_assert(this->type_
== type
);
605 this->u1_
.gsym
= gsym
;
606 this->u2_
.relobj
= relobj
;
608 this->set_needs_dynsym_index();
611 // A reloc against a local symbol.
613 template<bool dynamic
, int size
, bool big_endian
>
614 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
615 Sized_relobj
<size
, big_endian
>* relobj
,
616 unsigned int local_sym_index
,
621 bool is_section_symbol
)
622 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
623 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
626 gold_assert(local_sym_index
!= GSYM_CODE
627 && local_sym_index
!= INVALID_CODE
);
628 // this->type_ is a bitfield; make sure TYPE fits.
629 gold_assert(this->type_
== type
);
630 this->u1_
.relobj
= relobj
;
633 this->set_needs_dynsym_index();
636 template<bool dynamic
, int size
, bool big_endian
>
637 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
638 Sized_relobj
<size
, big_endian
>* relobj
,
639 unsigned int local_sym_index
,
644 bool is_section_symbol
)
645 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
646 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
649 gold_assert(local_sym_index
!= GSYM_CODE
650 && local_sym_index
!= INVALID_CODE
);
651 gold_assert(shndx
!= INVALID_CODE
);
652 // this->type_ is a bitfield; make sure TYPE fits.
653 gold_assert(this->type_
== type
);
654 this->u1_
.relobj
= relobj
;
655 this->u2_
.relobj
= relobj
;
657 this->set_needs_dynsym_index();
660 // A reloc against the STT_SECTION symbol of an output section.
662 template<bool dynamic
, int size
, bool big_endian
>
663 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
668 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
669 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE
)
671 // this->type_ is a bitfield; make sure TYPE fits.
672 gold_assert(this->type_
== type
);
676 this->set_needs_dynsym_index();
678 os
->set_needs_symtab_index();
681 template<bool dynamic
, int size
, bool big_endian
>
682 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
688 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
689 is_relative_(false), is_section_symbol_(true), shndx_(shndx
)
691 gold_assert(shndx
!= INVALID_CODE
);
692 // this->type_ is a bitfield; make sure TYPE fits.
693 gold_assert(this->type_
== type
);
695 this->u2_
.relobj
= relobj
;
697 this->set_needs_dynsym_index();
699 os
->set_needs_symtab_index();
702 // Record that we need a dynamic symbol index for this relocation.
704 template<bool dynamic
, int size
, bool big_endian
>
706 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
707 set_needs_dynsym_index()
709 if (this->is_relative_
)
711 switch (this->local_sym_index_
)
717 this->u1_
.gsym
->set_needs_dynsym_entry();
721 this->u1_
.os
->set_needs_dynsym_index();
729 const unsigned int lsi
= this->local_sym_index_
;
730 if (!this->is_section_symbol_
)
731 this->u1_
.relobj
->set_needs_output_dynsym_entry(lsi
);
734 section_offset_type dummy
;
735 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &dummy
);
736 gold_assert(os
!= NULL
);
737 os
->set_needs_dynsym_index();
744 // Get the symbol index of a relocation.
746 template<bool dynamic
, int size
, bool big_endian
>
748 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
752 switch (this->local_sym_index_
)
758 if (this->u1_
.gsym
== NULL
)
761 index
= this->u1_
.gsym
->dynsym_index();
763 index
= this->u1_
.gsym
->symtab_index();
768 index
= this->u1_
.os
->dynsym_index();
770 index
= this->u1_
.os
->symtab_index();
774 // Relocations without symbols use a symbol index of 0.
780 const unsigned int lsi
= this->local_sym_index_
;
781 if (!this->is_section_symbol_
)
784 index
= this->u1_
.relobj
->dynsym_index(lsi
);
786 index
= this->u1_
.relobj
->symtab_index(lsi
);
790 section_offset_type dummy
;
791 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &dummy
);
792 gold_assert(os
!= NULL
);
794 index
= os
->dynsym_index();
796 index
= os
->symtab_index();
801 gold_assert(index
!= -1U);
805 // For a local section symbol, get the address of the offset ADDEND
806 // within the input section.
808 template<bool dynamic
, int size
, bool big_endian
>
810 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
811 local_section_offset(Addend addend
) const
813 gold_assert(this->local_sym_index_
!= GSYM_CODE
814 && this->local_sym_index_
!= SECTION_CODE
815 && this->local_sym_index_
!= INVALID_CODE
816 && this->is_section_symbol_
);
817 const unsigned int lsi
= this->local_sym_index_
;
818 section_offset_type offset
;
819 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
, &offset
);
820 gold_assert(os
!= NULL
);
822 return offset
+ addend
;
823 // This is a merge section.
824 offset
= os
->output_address(this->u1_
.relobj
, lsi
, addend
);
825 gold_assert(offset
!= -1);
829 // Write out the offset and info fields of a Rel or Rela relocation
832 template<bool dynamic
, int size
, bool big_endian
>
833 template<typename Write_rel
>
835 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
838 Address address
= this->address_
;
839 if (this->shndx_
!= INVALID_CODE
)
841 section_offset_type off
;
842 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
844 gold_assert(os
!= NULL
);
846 address
+= os
->address() + off
;
849 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
851 gold_assert(address
!= -1U);
854 else if (this->u2_
.od
!= NULL
)
855 address
+= this->u2_
.od
->address();
856 wr
->put_r_offset(address
);
857 unsigned int sym_index
= this->is_relative_
? 0 : this->get_symbol_index();
858 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
861 // Write out a Rel relocation.
863 template<bool dynamic
, int size
, bool big_endian
>
865 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
866 unsigned char* pov
) const
868 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
869 this->write_rel(&orel
);
872 // Get the value of the symbol referred to by a Rel relocation.
874 template<bool dynamic
, int size
, bool big_endian
>
875 typename
elfcpp::Elf_types
<size
>::Elf_Addr
876 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
879 if (this->local_sym_index_
== GSYM_CODE
)
881 const Sized_symbol
<size
>* sym
;
882 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
883 return sym
->value() + addend
;
885 gold_assert(this->local_sym_index_
!= SECTION_CODE
886 && this->local_sym_index_
!= INVALID_CODE
887 && !this->is_section_symbol_
);
888 const unsigned int lsi
= this->local_sym_index_
;
889 const Symbol_value
<size
>* symval
= this->u1_
.relobj
->local_symbol(lsi
);
890 return symval
->value(this->u1_
.relobj
, addend
);
893 // Write out a Rela relocation.
895 template<bool dynamic
, int size
, bool big_endian
>
897 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
898 unsigned char* pov
) const
900 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
901 this->rel_
.write_rel(&orel
);
902 Addend addend
= this->addend_
;
903 if (this->rel_
.is_relative())
904 addend
= this->rel_
.symbol_value(addend
);
905 else if (this->rel_
.is_local_section_symbol())
906 addend
= this->rel_
.local_section_offset(addend
);
907 orel
.put_r_addend(addend
);
910 // Output_data_reloc_base methods.
912 // Adjust the output section.
914 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
916 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
917 ::do_adjust_output_section(Output_section
* os
)
919 if (sh_type
== elfcpp::SHT_REL
)
920 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
921 else if (sh_type
== elfcpp::SHT_RELA
)
922 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
926 os
->set_should_link_to_dynsym();
928 os
->set_should_link_to_symtab();
931 // Write out relocation data.
933 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
935 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
938 const off_t off
= this->offset();
939 const off_t oview_size
= this->data_size();
940 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
942 unsigned char* pov
= oview
;
943 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
944 p
!= this->relocs_
.end();
951 gold_assert(pov
- oview
== oview_size
);
953 of
->write_output_view(off
, oview_size
, oview
);
955 // We no longer need the relocation entries.
956 this->relocs_
.clear();
959 // Class Output_relocatable_relocs.
961 template<int sh_type
, int size
, bool big_endian
>
963 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
965 this->set_data_size(this->rr_
->output_reloc_count()
966 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
969 // class Output_data_group.
971 template<int size
, bool big_endian
>
972 Output_data_group
<size
, big_endian
>::Output_data_group(
973 Sized_relobj
<size
, big_endian
>* relobj
,
974 section_size_type entry_count
,
975 const elfcpp::Elf_Word
* contents
)
976 : Output_section_data(entry_count
* 4, 4),
979 this->flags_
= elfcpp::Swap
<32, big_endian
>::readval(contents
);
980 for (section_size_type i
= 1; i
< entry_count
; ++i
)
982 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
983 this->input_sections_
.push_back(shndx
);
987 // Write out the section group, which means translating the section
988 // indexes to apply to the output file.
990 template<int size
, bool big_endian
>
992 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
994 const off_t off
= this->offset();
995 const section_size_type oview_size
=
996 convert_to_section_size_type(this->data_size());
997 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
999 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1000 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1003 for (std::vector
<unsigned int>::const_iterator p
=
1004 this->input_sections_
.begin();
1005 p
!= this->input_sections_
.end();
1008 section_offset_type dummy
;
1009 Output_section
* os
= this->relobj_
->output_section(*p
, &dummy
);
1011 unsigned int output_shndx
;
1013 output_shndx
= os
->out_shndx();
1016 this->relobj_
->error(_("section group retained but "
1017 "group element discarded"));
1021 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1024 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1025 gold_assert(wrote
== oview_size
);
1027 of
->write_output_view(off
, oview_size
, oview
);
1029 // We no longer need this information.
1030 this->input_sections_
.clear();
1033 // Output_data_got::Got_entry methods.
1035 // Write out the entry.
1037 template<int size
, bool big_endian
>
1039 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1043 switch (this->local_sym_index_
)
1047 // If the symbol is resolved locally, we need to write out the
1048 // link-time value, which will be relocated dynamically by a
1049 // RELATIVE relocation.
1050 Symbol
* gsym
= this->u_
.gsym
;
1051 Sized_symbol
<size
>* sgsym
;
1052 // This cast is a bit ugly. We don't want to put a
1053 // virtual method in Symbol, because we want Symbol to be
1054 // as small as possible.
1055 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1056 val
= sgsym
->value();
1061 val
= this->u_
.constant
;
1066 const unsigned int lsi
= this->local_sym_index_
;
1067 const Symbol_value
<size
>* symval
= this->u_
.object
->local_symbol(lsi
);
1068 val
= symval
->value(this->u_
.object
, 0);
1073 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1076 // Output_data_got methods.
1078 // Add an entry for a global symbol to the GOT. This returns true if
1079 // this is a new GOT entry, false if the symbol already had a GOT
1082 template<int size
, bool big_endian
>
1084 Output_data_got
<size
, big_endian
>::add_global(
1086 unsigned int got_type
)
1088 if (gsym
->has_got_offset(got_type
))
1091 this->entries_
.push_back(Got_entry(gsym
));
1092 this->set_got_size();
1093 gsym
->set_got_offset(got_type
, this->last_got_offset());
1097 // Add an entry for a global symbol to the GOT, and add a dynamic
1098 // relocation of type R_TYPE for the GOT entry.
1099 template<int size
, bool big_endian
>
1101 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1103 unsigned int got_type
,
1105 unsigned int r_type
)
1107 if (gsym
->has_got_offset(got_type
))
1110 this->entries_
.push_back(Got_entry());
1111 this->set_got_size();
1112 unsigned int got_offset
= this->last_got_offset();
1113 gsym
->set_got_offset(got_type
, got_offset
);
1114 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1117 template<int size
, bool big_endian
>
1119 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1121 unsigned int got_type
,
1123 unsigned int r_type
)
1125 if (gsym
->has_got_offset(got_type
))
1128 this->entries_
.push_back(Got_entry());
1129 this->set_got_size();
1130 unsigned int got_offset
= this->last_got_offset();
1131 gsym
->set_got_offset(got_type
, got_offset
);
1132 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1135 // Add a pair of entries for a global symbol to the GOT, and add
1136 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1137 // If R_TYPE_2 == 0, add the second entry with no relocation.
1138 template<int size
, bool big_endian
>
1140 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1142 unsigned int got_type
,
1144 unsigned int r_type_1
,
1145 unsigned int r_type_2
)
1147 if (gsym
->has_got_offset(got_type
))
1150 this->entries_
.push_back(Got_entry());
1151 unsigned int got_offset
= this->last_got_offset();
1152 gsym
->set_got_offset(got_type
, got_offset
);
1153 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1155 this->entries_
.push_back(Got_entry());
1158 got_offset
= this->last_got_offset();
1159 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
);
1162 this->set_got_size();
1165 template<int size
, bool big_endian
>
1167 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1169 unsigned int got_type
,
1171 unsigned int r_type_1
,
1172 unsigned int r_type_2
)
1174 if (gsym
->has_got_offset(got_type
))
1177 this->entries_
.push_back(Got_entry());
1178 unsigned int got_offset
= this->last_got_offset();
1179 gsym
->set_got_offset(got_type
, got_offset
);
1180 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1182 this->entries_
.push_back(Got_entry());
1185 got_offset
= this->last_got_offset();
1186 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
, 0);
1189 this->set_got_size();
1192 // Add an entry for a local symbol to the GOT. This returns true if
1193 // this is a new GOT entry, false if the symbol already has a GOT
1196 template<int size
, bool big_endian
>
1198 Output_data_got
<size
, big_endian
>::add_local(
1199 Sized_relobj
<size
, big_endian
>* object
,
1200 unsigned int symndx
,
1201 unsigned int got_type
)
1203 if (object
->local_has_got_offset(symndx
, got_type
))
1206 this->entries_
.push_back(Got_entry(object
, symndx
));
1207 this->set_got_size();
1208 object
->set_local_got_offset(symndx
, got_type
, this->last_got_offset());
1212 // Add an entry for a local symbol to the GOT, and add a dynamic
1213 // relocation of type R_TYPE for the GOT entry.
1214 template<int size
, bool big_endian
>
1216 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1217 Sized_relobj
<size
, big_endian
>* object
,
1218 unsigned int symndx
,
1219 unsigned int got_type
,
1221 unsigned int r_type
)
1223 if (object
->local_has_got_offset(symndx
, got_type
))
1226 this->entries_
.push_back(Got_entry());
1227 this->set_got_size();
1228 unsigned int got_offset
= this->last_got_offset();
1229 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1230 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1233 template<int size
, bool big_endian
>
1235 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1236 Sized_relobj
<size
, big_endian
>* object
,
1237 unsigned int symndx
,
1238 unsigned int got_type
,
1240 unsigned int r_type
)
1242 if (object
->local_has_got_offset(symndx
, got_type
))
1245 this->entries_
.push_back(Got_entry());
1246 this->set_got_size();
1247 unsigned int got_offset
= this->last_got_offset();
1248 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1249 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1252 // Add a pair of entries for a local symbol to the GOT, and add
1253 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1254 // If R_TYPE_2 == 0, add the second entry with no relocation.
1255 template<int size
, bool big_endian
>
1257 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1258 Sized_relobj
<size
, big_endian
>* object
,
1259 unsigned int symndx
,
1261 unsigned int got_type
,
1263 unsigned int r_type_1
,
1264 unsigned int r_type_2
)
1266 if (object
->local_has_got_offset(symndx
, got_type
))
1269 this->entries_
.push_back(Got_entry());
1270 unsigned int got_offset
= this->last_got_offset();
1271 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1272 section_offset_type off
;
1273 Output_section
* os
= object
->output_section(shndx
, &off
);
1274 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1276 this->entries_
.push_back(Got_entry(object
, symndx
));
1279 got_offset
= this->last_got_offset();
1280 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
);
1283 this->set_got_size();
1286 template<int size
, bool big_endian
>
1288 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1289 Sized_relobj
<size
, big_endian
>* object
,
1290 unsigned int symndx
,
1292 unsigned int got_type
,
1294 unsigned int r_type_1
,
1295 unsigned int r_type_2
)
1297 if (object
->local_has_got_offset(symndx
, got_type
))
1300 this->entries_
.push_back(Got_entry());
1301 unsigned int got_offset
= this->last_got_offset();
1302 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1303 section_offset_type off
;
1304 Output_section
* os
= object
->output_section(shndx
, &off
);
1305 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1307 this->entries_
.push_back(Got_entry(object
, symndx
));
1310 got_offset
= this->last_got_offset();
1311 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
, 0);
1314 this->set_got_size();
1317 // Write out the GOT.
1319 template<int size
, bool big_endian
>
1321 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1323 const int add
= size
/ 8;
1325 const off_t off
= this->offset();
1326 const off_t oview_size
= this->data_size();
1327 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1329 unsigned char* pov
= oview
;
1330 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1331 p
!= this->entries_
.end();
1338 gold_assert(pov
- oview
== oview_size
);
1340 of
->write_output_view(off
, oview_size
, oview
);
1342 // We no longer need the GOT entries.
1343 this->entries_
.clear();
1346 // Output_data_dynamic::Dynamic_entry methods.
1348 // Write out the entry.
1350 template<int size
, bool big_endian
>
1352 Output_data_dynamic::Dynamic_entry::write(
1354 const Stringpool
* pool
) const
1356 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1357 switch (this->offset_
)
1359 case DYNAMIC_NUMBER
:
1363 case DYNAMIC_SECTION_SIZE
:
1364 val
= this->u_
.od
->data_size();
1367 case DYNAMIC_SYMBOL
:
1369 const Sized_symbol
<size
>* s
=
1370 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1375 case DYNAMIC_STRING
:
1376 val
= pool
->get_offset(this->u_
.str
);
1380 val
= this->u_
.od
->address() + this->offset_
;
1384 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1385 dw
.put_d_tag(this->tag_
);
1389 // Output_data_dynamic methods.
1391 // Adjust the output section to set the entry size.
1394 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1396 if (parameters
->target().get_size() == 32)
1397 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1398 else if (parameters
->target().get_size() == 64)
1399 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1404 // Set the final data size.
1407 Output_data_dynamic::set_final_data_size()
1409 // Add the terminating entry.
1410 this->add_constant(elfcpp::DT_NULL
, 0);
1413 if (parameters
->target().get_size() == 32)
1414 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1415 else if (parameters
->target().get_size() == 64)
1416 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1419 this->set_data_size(this->entries_
.size() * dyn_size
);
1422 // Write out the dynamic entries.
1425 Output_data_dynamic::do_write(Output_file
* of
)
1427 switch (parameters
->size_and_endianness())
1429 #ifdef HAVE_TARGET_32_LITTLE
1430 case Parameters::TARGET_32_LITTLE
:
1431 this->sized_write
<32, false>(of
);
1434 #ifdef HAVE_TARGET_32_BIG
1435 case Parameters::TARGET_32_BIG
:
1436 this->sized_write
<32, true>(of
);
1439 #ifdef HAVE_TARGET_64_LITTLE
1440 case Parameters::TARGET_64_LITTLE
:
1441 this->sized_write
<64, false>(of
);
1444 #ifdef HAVE_TARGET_64_BIG
1445 case Parameters::TARGET_64_BIG
:
1446 this->sized_write
<64, true>(of
);
1454 template<int size
, bool big_endian
>
1456 Output_data_dynamic::sized_write(Output_file
* of
)
1458 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1460 const off_t offset
= this->offset();
1461 const off_t oview_size
= this->data_size();
1462 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1464 unsigned char* pov
= oview
;
1465 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1466 p
!= this->entries_
.end();
1469 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1473 gold_assert(pov
- oview
== oview_size
);
1475 of
->write_output_view(offset
, oview_size
, oview
);
1477 // We no longer need the dynamic entries.
1478 this->entries_
.clear();
1481 // Output_section::Input_section methods.
1483 // Return the data size. For an input section we store the size here.
1484 // For an Output_section_data, we have to ask it for the size.
1487 Output_section::Input_section::data_size() const
1489 if (this->is_input_section())
1490 return this->u1_
.data_size
;
1492 return this->u2_
.posd
->data_size();
1495 // Set the address and file offset.
1498 Output_section::Input_section::set_address_and_file_offset(
1501 off_t section_file_offset
)
1503 if (this->is_input_section())
1504 this->u2_
.object
->set_section_offset(this->shndx_
,
1505 file_offset
- section_file_offset
);
1507 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1510 // Reset the address and file offset.
1513 Output_section::Input_section::reset_address_and_file_offset()
1515 if (!this->is_input_section())
1516 this->u2_
.posd
->reset_address_and_file_offset();
1519 // Finalize the data size.
1522 Output_section::Input_section::finalize_data_size()
1524 if (!this->is_input_section())
1525 this->u2_
.posd
->finalize_data_size();
1528 // Try to turn an input offset into an output offset. We want to
1529 // return the output offset relative to the start of this
1530 // Input_section in the output section.
1533 Output_section::Input_section::output_offset(
1534 const Relobj
* object
,
1536 section_offset_type offset
,
1537 section_offset_type
*poutput
) const
1539 if (!this->is_input_section())
1540 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1543 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1550 // Return whether this is the merge section for the input section
1554 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1555 unsigned int shndx
) const
1557 if (this->is_input_section())
1559 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1562 // Write out the data. We don't have to do anything for an input
1563 // section--they are handled via Object::relocate--but this is where
1564 // we write out the data for an Output_section_data.
1567 Output_section::Input_section::write(Output_file
* of
)
1569 if (!this->is_input_section())
1570 this->u2_
.posd
->write(of
);
1573 // Write the data to a buffer. As for write(), we don't have to do
1574 // anything for an input section.
1577 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1579 if (!this->is_input_section())
1580 this->u2_
.posd
->write_to_buffer(buffer
);
1583 // Output_section methods.
1585 // Construct an Output_section. NAME will point into a Stringpool.
1587 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1588 elfcpp::Elf_Xword flags
)
1593 link_section_(NULL
),
1595 info_section_(NULL
),
1604 first_input_offset_(0),
1606 postprocessing_buffer_(NULL
),
1607 needs_symtab_index_(false),
1608 needs_dynsym_index_(false),
1609 should_link_to_symtab_(false),
1610 should_link_to_dynsym_(false),
1611 after_input_sections_(false),
1612 requires_postprocessing_(false),
1613 found_in_sections_clause_(false),
1614 has_load_address_(false),
1615 info_uses_section_index_(false),
1616 may_sort_attached_input_sections_(false),
1617 must_sort_attached_input_sections_(false),
1618 attached_input_sections_are_sorted_(false),
1621 // An unallocated section has no address. Forcing this means that
1622 // we don't need special treatment for symbols defined in debug
1624 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1625 this->set_address(0);
1628 Output_section::~Output_section()
1632 // Set the entry size.
1635 Output_section::set_entsize(uint64_t v
)
1637 if (this->entsize_
== 0)
1640 gold_assert(this->entsize_
== v
);
1643 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1644 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1645 // relocation section which applies to this section, or 0 if none, or
1646 // -1U if more than one. Return the offset of the input section
1647 // within the output section. Return -1 if the input section will
1648 // receive special handling. In the normal case we don't always keep
1649 // track of input sections for an Output_section. Instead, each
1650 // Object keeps track of the Output_section for each of its input
1651 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1652 // track of input sections here; this is used when SECTIONS appears in
1655 template<int size
, bool big_endian
>
1657 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1659 const char* secname
,
1660 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1661 unsigned int reloc_shndx
,
1662 bool have_sections_script
)
1664 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1665 if ((addralign
& (addralign
- 1)) != 0)
1667 object
->error(_("invalid alignment %lu for section \"%s\""),
1668 static_cast<unsigned long>(addralign
), secname
);
1672 if (addralign
> this->addralign_
)
1673 this->addralign_
= addralign
;
1675 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1676 this->flags_
|= (sh_flags
1677 & (elfcpp::SHF_WRITE
1679 | elfcpp::SHF_EXECINSTR
));
1681 uint64_t entsize
= shdr
.get_sh_entsize();
1683 // .debug_str is a mergeable string section, but is not always so
1684 // marked by compilers. Mark manually here so we can optimize.
1685 if (strcmp(secname
, ".debug_str") == 0)
1687 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1691 // If this is a SHF_MERGE section, we pass all the input sections to
1692 // a Output_data_merge. We don't try to handle relocations for such
1694 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1695 && reloc_shndx
== 0)
1697 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1698 entsize
, addralign
))
1700 // Tell the relocation routines that they need to call the
1701 // output_offset method to determine the final address.
1706 off_t offset_in_section
= this->current_data_size_for_child();
1707 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1710 if (aligned_offset_in_section
> offset_in_section
1711 && !have_sections_script
1712 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1713 && object
->target()->has_code_fill())
1715 // We need to add some fill data. Using fill_list_ when
1716 // possible is an optimization, since we will often have fill
1717 // sections without input sections.
1718 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1719 if (this->input_sections_
.empty())
1720 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1723 // FIXME: When relaxing, the size needs to adjust to
1724 // maintain a constant alignment.
1725 std::string
fill_data(object
->target()->code_fill(fill_len
));
1726 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1727 this->input_sections_
.push_back(Input_section(odc
));
1731 this->set_current_data_size_for_child(aligned_offset_in_section
1732 + shdr
.get_sh_size());
1734 // We need to keep track of this section if we are already keeping
1735 // track of sections, or if we are relaxing. Also, if this is a
1736 // section which requires sorting, or which may require sorting in
1737 // the future, we keep track of the sections. FIXME: Add test for
1739 if (have_sections_script
1740 || !this->input_sections_
.empty()
1741 || this->may_sort_attached_input_sections()
1742 || this->must_sort_attached_input_sections())
1743 this->input_sections_
.push_back(Input_section(object
, shndx
,
1747 return aligned_offset_in_section
;
1750 // Add arbitrary data to an output section.
1753 Output_section::add_output_section_data(Output_section_data
* posd
)
1755 Input_section
inp(posd
);
1756 this->add_output_section_data(&inp
);
1758 if (posd
->is_data_size_valid())
1760 off_t offset_in_section
= this->current_data_size_for_child();
1761 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1763 this->set_current_data_size_for_child(aligned_offset_in_section
1764 + posd
->data_size());
1768 // Add arbitrary data to an output section by Input_section.
1771 Output_section::add_output_section_data(Input_section
* inp
)
1773 if (this->input_sections_
.empty())
1774 this->first_input_offset_
= this->current_data_size_for_child();
1776 this->input_sections_
.push_back(*inp
);
1778 uint64_t addralign
= inp
->addralign();
1779 if (addralign
> this->addralign_
)
1780 this->addralign_
= addralign
;
1782 inp
->set_output_section(this);
1785 // Add a merge section to an output section.
1788 Output_section::add_output_merge_section(Output_section_data
* posd
,
1789 bool is_string
, uint64_t entsize
)
1791 Input_section
inp(posd
, is_string
, entsize
);
1792 this->add_output_section_data(&inp
);
1795 // Add an input section to a SHF_MERGE section.
1798 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1799 uint64_t flags
, uint64_t entsize
,
1802 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1804 // We only merge strings if the alignment is not more than the
1805 // character size. This could be handled, but it's unusual.
1806 if (is_string
&& addralign
> entsize
)
1809 Input_section_list::iterator p
;
1810 for (p
= this->input_sections_
.begin();
1811 p
!= this->input_sections_
.end();
1813 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1815 p
->add_input_section(object
, shndx
);
1819 // We handle the actual constant merging in Output_merge_data or
1820 // Output_merge_string_data.
1821 Output_section_data
* posd
;
1823 posd
= new Output_merge_data(entsize
, addralign
);
1829 posd
= new Output_merge_string
<char>(addralign
);
1832 posd
= new Output_merge_string
<uint16_t>(addralign
);
1835 posd
= new Output_merge_string
<uint32_t>(addralign
);
1842 this->add_output_merge_section(posd
, is_string
, entsize
);
1843 posd
->add_input_section(object
, shndx
);
1848 // Given an address OFFSET relative to the start of input section
1849 // SHNDX in OBJECT, return whether this address is being included in
1850 // the final link. This should only be called if SHNDX in OBJECT has
1851 // a special mapping.
1854 Output_section::is_input_address_mapped(const Relobj
* object
,
1858 gold_assert(object
->is_section_specially_mapped(shndx
));
1860 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1861 p
!= this->input_sections_
.end();
1864 section_offset_type output_offset
;
1865 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1866 return output_offset
!= -1;
1869 // By default we assume that the address is mapped. This should
1870 // only be called after we have passed all sections to Layout. At
1871 // that point we should know what we are discarding.
1875 // Given an address OFFSET relative to the start of input section
1876 // SHNDX in object OBJECT, return the output offset relative to the
1877 // start of the input section in the output section. This should only
1878 // be called if SHNDX in OBJECT has a special mapping.
1881 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
1882 section_offset_type offset
) const
1884 gold_assert(object
->is_section_specially_mapped(shndx
));
1885 // This can only be called meaningfully when layout is complete.
1886 gold_assert(Output_data::is_layout_complete());
1888 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1889 p
!= this->input_sections_
.end();
1892 section_offset_type output_offset
;
1893 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1894 return output_offset
;
1899 // Return the output virtual address of OFFSET relative to the start
1900 // of input section SHNDX in object OBJECT.
1903 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1906 gold_assert(object
->is_section_specially_mapped(shndx
));
1908 uint64_t addr
= this->address() + this->first_input_offset_
;
1909 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1910 p
!= this->input_sections_
.end();
1913 addr
= align_address(addr
, p
->addralign());
1914 section_offset_type output_offset
;
1915 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1917 if (output_offset
== -1)
1919 return addr
+ output_offset
;
1921 addr
+= p
->data_size();
1924 // If we get here, it means that we don't know the mapping for this
1925 // input section. This might happen in principle if
1926 // add_input_section were called before add_output_section_data.
1927 // But it should never actually happen.
1932 // Return the output address of the start of the merged section for
1933 // input section SHNDX in object OBJECT.
1936 Output_section::starting_output_address(const Relobj
* object
,
1937 unsigned int shndx
) const
1939 gold_assert(object
->is_section_specially_mapped(shndx
));
1941 uint64_t addr
= this->address() + this->first_input_offset_
;
1942 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1943 p
!= this->input_sections_
.end();
1946 addr
= align_address(addr
, p
->addralign());
1948 // It would be nice if we could use the existing output_offset
1949 // method to get the output offset of input offset 0.
1950 // Unfortunately we don't know for sure that input offset 0 is
1952 if (p
->is_merge_section_for(object
, shndx
))
1955 addr
+= p
->data_size();
1960 // Set the data size of an Output_section. This is where we handle
1961 // setting the addresses of any Output_section_data objects.
1964 Output_section::set_final_data_size()
1966 if (this->input_sections_
.empty())
1968 this->set_data_size(this->current_data_size_for_child());
1972 if (this->must_sort_attached_input_sections())
1973 this->sort_attached_input_sections();
1975 uint64_t address
= this->address();
1976 off_t startoff
= this->offset();
1977 off_t off
= startoff
+ this->first_input_offset_
;
1978 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1979 p
!= this->input_sections_
.end();
1982 off
= align_address(off
, p
->addralign());
1983 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
1985 off
+= p
->data_size();
1988 this->set_data_size(off
- startoff
);
1991 // Reset the address and file offset.
1994 Output_section::do_reset_address_and_file_offset()
1996 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1997 p
!= this->input_sections_
.end();
1999 p
->reset_address_and_file_offset();
2002 // Set the TLS offset. Called only for SHT_TLS sections.
2005 Output_section::do_set_tls_offset(uint64_t tls_base
)
2007 this->tls_offset_
= this->address() - tls_base
;
2010 // In a few cases we need to sort the input sections attached to an
2011 // output section. This is used to implement the type of constructor
2012 // priority ordering implemented by the GNU linker, in which the
2013 // priority becomes part of the section name and the sections are
2014 // sorted by name. We only do this for an output section if we see an
2015 // attached input section matching ".ctor.*", ".dtor.*",
2016 // ".init_array.*" or ".fini_array.*".
2018 class Output_section::Input_section_sort_entry
2021 Input_section_sort_entry()
2022 : input_section_(), index_(-1U), section_has_name_(false),
2026 Input_section_sort_entry(const Input_section
& input_section
,
2028 : input_section_(input_section
), index_(index
),
2029 section_has_name_(input_section
.is_input_section())
2031 if (this->section_has_name_
)
2033 // This is only called single-threaded from Layout::finalize,
2034 // so it is OK to lock. Unfortunately we have no way to pass
2036 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
2037 Object
* obj
= input_section
.relobj();
2038 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
2040 // This is a slow operation, which should be cached in
2041 // Layout::layout if this becomes a speed problem.
2042 this->section_name_
= obj
->section_name(input_section
.shndx());
2046 // Return the Input_section.
2047 const Input_section
&
2048 input_section() const
2050 gold_assert(this->index_
!= -1U);
2051 return this->input_section_
;
2054 // The index of this entry in the original list. This is used to
2055 // make the sort stable.
2059 gold_assert(this->index_
!= -1U);
2060 return this->index_
;
2063 // Whether there is a section name.
2065 section_has_name() const
2066 { return this->section_has_name_
; }
2068 // The section name.
2070 section_name() const
2072 gold_assert(this->section_has_name_
);
2073 return this->section_name_
;
2076 // Return true if the section name has a priority. This is assumed
2077 // to be true if it has a dot after the initial dot.
2079 has_priority() const
2081 gold_assert(this->section_has_name_
);
2082 return this->section_name_
.find('.', 1);
2085 // Return true if this an input file whose base name matches
2086 // FILE_NAME. The base name must have an extension of ".o", and
2087 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2088 // This is to match crtbegin.o as well as crtbeginS.o without
2089 // getting confused by other possibilities. Overall matching the
2090 // file name this way is a dreadful hack, but the GNU linker does it
2091 // in order to better support gcc, and we need to be compatible.
2093 match_file_name(const char* match_file_name
) const
2095 const std::string
& file_name(this->input_section_
.relobj()->name());
2096 const char* base_name
= lbasename(file_name
.c_str());
2097 size_t match_len
= strlen(match_file_name
);
2098 if (strncmp(base_name
, match_file_name
, match_len
) != 0)
2100 size_t base_len
= strlen(base_name
);
2101 if (base_len
!= match_len
+ 2 && base_len
!= match_len
+ 3)
2103 return memcmp(base_name
+ base_len
- 2, ".o", 2) == 0;
2107 // The Input_section we are sorting.
2108 Input_section input_section_
;
2109 // The index of this Input_section in the original list.
2110 unsigned int index_
;
2111 // Whether this Input_section has a section name--it won't if this
2112 // is some random Output_section_data.
2113 bool section_has_name_
;
2114 // The section name if there is one.
2115 std::string section_name_
;
2118 // Return true if S1 should come before S2 in the output section.
2121 Output_section::Input_section_sort_compare::operator()(
2122 const Output_section::Input_section_sort_entry
& s1
,
2123 const Output_section::Input_section_sort_entry
& s2
) const
2125 // crtbegin.o must come first.
2126 bool s1_begin
= s1
.match_file_name("crtbegin");
2127 bool s2_begin
= s2
.match_file_name("crtbegin");
2128 if (s1_begin
|| s2_begin
)
2134 return s1
.index() < s2
.index();
2137 // crtend.o must come last.
2138 bool s1_end
= s1
.match_file_name("crtend");
2139 bool s2_end
= s2
.match_file_name("crtend");
2140 if (s1_end
|| s2_end
)
2146 return s1
.index() < s2
.index();
2149 // We sort all the sections with no names to the end.
2150 if (!s1
.section_has_name() || !s2
.section_has_name())
2152 if (s1
.section_has_name())
2154 if (s2
.section_has_name())
2156 return s1
.index() < s2
.index();
2159 // A section with a priority follows a section without a priority.
2160 // The GNU linker does this for all but .init_array sections; until
2161 // further notice we'll assume that that is an mistake.
2162 bool s1_has_priority
= s1
.has_priority();
2163 bool s2_has_priority
= s2
.has_priority();
2164 if (s1_has_priority
&& !s2_has_priority
)
2166 if (!s1_has_priority
&& s2_has_priority
)
2169 // Otherwise we sort by name.
2170 int compare
= s1
.section_name().compare(s2
.section_name());
2174 // Otherwise we keep the input order.
2175 return s1
.index() < s2
.index();
2178 // Sort the input sections attached to an output section.
2181 Output_section::sort_attached_input_sections()
2183 if (this->attached_input_sections_are_sorted_
)
2186 // The only thing we know about an input section is the object and
2187 // the section index. We need the section name. Recomputing this
2188 // is slow but this is an unusual case. If this becomes a speed
2189 // problem we can cache the names as required in Layout::layout.
2191 // We start by building a larger vector holding a copy of each
2192 // Input_section, plus its current index in the list and its name.
2193 std::vector
<Input_section_sort_entry
> sort_list
;
2196 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2197 p
!= this->input_sections_
.end();
2199 sort_list
.push_back(Input_section_sort_entry(*p
, i
));
2201 // Sort the input sections.
2202 std::sort(sort_list
.begin(), sort_list
.end(), Input_section_sort_compare());
2204 // Copy the sorted input sections back to our list.
2205 this->input_sections_
.clear();
2206 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
2207 p
!= sort_list
.end();
2209 this->input_sections_
.push_back(p
->input_section());
2211 // Remember that we sorted the input sections, since we might get
2213 this->attached_input_sections_are_sorted_
= true;
2216 // Write the section header to *OSHDR.
2218 template<int size
, bool big_endian
>
2220 Output_section::write_header(const Layout
* layout
,
2221 const Stringpool
* secnamepool
,
2222 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
2224 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
2225 oshdr
->put_sh_type(this->type_
);
2227 elfcpp::Elf_Xword flags
= this->flags_
;
2228 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
2229 flags
|= elfcpp::SHF_INFO_LINK
;
2230 oshdr
->put_sh_flags(flags
);
2232 oshdr
->put_sh_addr(this->address());
2233 oshdr
->put_sh_offset(this->offset());
2234 oshdr
->put_sh_size(this->data_size());
2235 if (this->link_section_
!= NULL
)
2236 oshdr
->put_sh_link(this->link_section_
->out_shndx());
2237 else if (this->should_link_to_symtab_
)
2238 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
2239 else if (this->should_link_to_dynsym_
)
2240 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
2242 oshdr
->put_sh_link(this->link_
);
2244 elfcpp::Elf_Word info
;
2245 if (this->info_section_
!= NULL
)
2247 if (this->info_uses_section_index_
)
2248 info
= this->info_section_
->out_shndx();
2250 info
= this->info_section_
->symtab_index();
2252 else if (this->info_symndx_
!= NULL
)
2253 info
= this->info_symndx_
->symtab_index();
2256 oshdr
->put_sh_info(info
);
2258 oshdr
->put_sh_addralign(this->addralign_
);
2259 oshdr
->put_sh_entsize(this->entsize_
);
2262 // Write out the data. For input sections the data is written out by
2263 // Object::relocate, but we have to handle Output_section_data objects
2267 Output_section::do_write(Output_file
* of
)
2269 gold_assert(!this->requires_postprocessing());
2271 off_t output_section_file_offset
= this->offset();
2272 for (Fill_list::iterator p
= this->fills_
.begin();
2273 p
!= this->fills_
.end();
2276 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2277 of
->write(output_section_file_offset
+ p
->section_offset(),
2278 fill_data
.data(), fill_data
.size());
2281 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2282 p
!= this->input_sections_
.end();
2287 // If a section requires postprocessing, create the buffer to use.
2290 Output_section::create_postprocessing_buffer()
2292 gold_assert(this->requires_postprocessing());
2294 if (this->postprocessing_buffer_
!= NULL
)
2297 if (!this->input_sections_
.empty())
2299 off_t off
= this->first_input_offset_
;
2300 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2301 p
!= this->input_sections_
.end();
2304 off
= align_address(off
, p
->addralign());
2305 p
->finalize_data_size();
2306 off
+= p
->data_size();
2308 this->set_current_data_size_for_child(off
);
2311 off_t buffer_size
= this->current_data_size_for_child();
2312 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
2315 // Write all the data of an Output_section into the postprocessing
2316 // buffer. This is used for sections which require postprocessing,
2317 // such as compression. Input sections are handled by
2318 // Object::Relocate.
2321 Output_section::write_to_postprocessing_buffer()
2323 gold_assert(this->requires_postprocessing());
2325 unsigned char* buffer
= this->postprocessing_buffer();
2326 for (Fill_list::iterator p
= this->fills_
.begin();
2327 p
!= this->fills_
.end();
2330 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2331 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
2335 off_t off
= this->first_input_offset_
;
2336 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2337 p
!= this->input_sections_
.end();
2340 off
= align_address(off
, p
->addralign());
2341 p
->write_to_buffer(buffer
+ off
);
2342 off
+= p
->data_size();
2346 // Get the input sections for linker script processing. We leave
2347 // behind the Output_section_data entries. Note that this may be
2348 // slightly incorrect for merge sections. We will leave them behind,
2349 // but it is possible that the script says that they should follow
2350 // some other input sections, as in:
2351 // .rodata { *(.rodata) *(.rodata.cst*) }
2352 // For that matter, we don't handle this correctly:
2353 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2354 // With luck this will never matter.
2357 Output_section::get_input_sections(
2359 const std::string
& fill
,
2360 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
2362 uint64_t orig_address
= address
;
2364 address
= align_address(address
, this->addralign());
2366 Input_section_list remaining
;
2367 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2368 p
!= this->input_sections_
.end();
2371 if (p
->is_input_section())
2372 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2375 uint64_t aligned_address
= align_address(address
, p
->addralign());
2376 if (aligned_address
!= address
&& !fill
.empty())
2378 section_size_type length
=
2379 convert_to_section_size_type(aligned_address
- address
);
2380 std::string this_fill
;
2381 this_fill
.reserve(length
);
2382 while (this_fill
.length() + fill
.length() <= length
)
2384 if (this_fill
.length() < length
)
2385 this_fill
.append(fill
, 0, length
- this_fill
.length());
2387 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2388 remaining
.push_back(Input_section(posd
));
2390 address
= aligned_address
;
2392 remaining
.push_back(*p
);
2394 p
->finalize_data_size();
2395 address
+= p
->data_size();
2399 this->input_sections_
.swap(remaining
);
2400 this->first_input_offset_
= 0;
2402 uint64_t data_size
= address
- orig_address
;
2403 this->set_current_data_size_for_child(data_size
);
2407 // Add an input section from a script.
2410 Output_section::add_input_section_for_script(Relobj
* object
,
2415 if (addralign
> this->addralign_
)
2416 this->addralign_
= addralign
;
2418 off_t offset_in_section
= this->current_data_size_for_child();
2419 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2422 this->set_current_data_size_for_child(aligned_offset_in_section
2425 this->input_sections_
.push_back(Input_section(object
, shndx
,
2426 data_size
, addralign
));
2429 // Print stats for merge sections to stderr.
2432 Output_section::print_merge_stats()
2434 Input_section_list::iterator p
;
2435 for (p
= this->input_sections_
.begin();
2436 p
!= this->input_sections_
.end();
2438 p
->print_merge_stats(this->name_
);
2441 // Output segment methods.
2443 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2455 is_max_align_known_(false),
2456 are_addresses_set_(false)
2460 // Add an Output_section to an Output_segment.
2463 Output_segment::add_output_section(Output_section
* os
,
2464 elfcpp::Elf_Word seg_flags
,
2467 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2468 gold_assert(!this->is_max_align_known_
);
2470 // Update the segment flags.
2471 this->flags_
|= seg_flags
;
2473 Output_segment::Output_data_list
* pdl
;
2474 if (os
->type() == elfcpp::SHT_NOBITS
)
2475 pdl
= &this->output_bss_
;
2477 pdl
= &this->output_data_
;
2479 // So that PT_NOTE segments will work correctly, we need to ensure
2480 // that all SHT_NOTE sections are adjacent. This will normally
2481 // happen automatically, because all the SHT_NOTE input sections
2482 // will wind up in the same output section. However, it is possible
2483 // for multiple SHT_NOTE input sections to have different section
2484 // flags, and thus be in different output sections, but for the
2485 // different section flags to map into the same segment flags and
2486 // thus the same output segment.
2488 // Note that while there may be many input sections in an output
2489 // section, there are normally only a few output sections in an
2490 // output segment. This loop is expected to be fast.
2492 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2494 Output_segment::Output_data_list::iterator p
= pdl
->end();
2498 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2500 // We don't worry about the FRONT parameter.
2506 while (p
!= pdl
->begin());
2509 // Similarly, so that PT_TLS segments will work, we need to group
2510 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2511 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2512 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2513 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2514 // and the PT_TLS segment -- we do this grouping only for the
2516 if (this->type_
!= elfcpp::PT_TLS
2517 && (os
->flags() & elfcpp::SHF_TLS
) != 0
2518 && !this->output_data_
.empty())
2520 pdl
= &this->output_data_
;
2521 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2522 bool sawtls
= false;
2523 Output_segment::Output_data_list::iterator p
= pdl
->end();
2528 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2531 // Put a NOBITS section after the first TLS section.
2532 // But a PROGBITS section after the first TLS/PROGBITS
2534 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2538 // If we've gone past the TLS sections, but we've seen a
2539 // TLS section, then we need to insert this section now.
2545 // We don't worry about the FRONT parameter.
2551 while (p
!= pdl
->begin());
2553 // There are no TLS sections yet; put this one at the requested
2554 // location in the section list.
2558 pdl
->push_front(os
);
2563 // Remove an Output_section from this segment. It is an error if it
2567 Output_segment::remove_output_section(Output_section
* os
)
2569 // We only need this for SHT_PROGBITS.
2570 gold_assert(os
->type() == elfcpp::SHT_PROGBITS
);
2571 for (Output_data_list::iterator p
= this->output_data_
.begin();
2572 p
!= this->output_data_
.end();
2577 this->output_data_
.erase(p
);
2584 // Add an Output_data (which is not an Output_section) to the start of
2588 Output_segment::add_initial_output_data(Output_data
* od
)
2590 gold_assert(!this->is_max_align_known_
);
2591 this->output_data_
.push_front(od
);
2594 // Return the maximum alignment of the Output_data in Output_segment.
2597 Output_segment::maximum_alignment()
2599 if (!this->is_max_align_known_
)
2603 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2604 if (addralign
> this->max_align_
)
2605 this->max_align_
= addralign
;
2607 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2608 if (addralign
> this->max_align_
)
2609 this->max_align_
= addralign
;
2611 this->is_max_align_known_
= true;
2614 return this->max_align_
;
2617 // Return the maximum alignment of a list of Output_data.
2620 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2623 for (Output_data_list::const_iterator p
= pdl
->begin();
2627 uint64_t addralign
= (*p
)->addralign();
2628 if (addralign
> ret
)
2634 // Return the number of dynamic relocs applied to this segment.
2637 Output_segment::dynamic_reloc_count() const
2639 return (this->dynamic_reloc_count_list(&this->output_data_
)
2640 + this->dynamic_reloc_count_list(&this->output_bss_
));
2643 // Return the number of dynamic relocs applied to an Output_data_list.
2646 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2648 unsigned int count
= 0;
2649 for (Output_data_list::const_iterator p
= pdl
->begin();
2652 count
+= (*p
)->dynamic_reloc_count();
2656 // Set the section addresses for an Output_segment. If RESET is true,
2657 // reset the addresses first. ADDR is the address and *POFF is the
2658 // file offset. Set the section indexes starting with *PSHNDX.
2659 // Return the address of the immediately following segment. Update
2660 // *POFF and *PSHNDX.
2663 Output_segment::set_section_addresses(const Layout
* layout
, bool reset
,
2664 uint64_t addr
, off_t
* poff
,
2665 unsigned int* pshndx
)
2667 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2669 if (!reset
&& this->are_addresses_set_
)
2671 gold_assert(this->paddr_
== addr
);
2672 addr
= this->vaddr_
;
2676 this->vaddr_
= addr
;
2677 this->paddr_
= addr
;
2678 this->are_addresses_set_
= true;
2681 bool in_tls
= false;
2683 off_t orig_off
= *poff
;
2684 this->offset_
= orig_off
;
2686 addr
= this->set_section_list_addresses(layout
, reset
, &this->output_data_
,
2687 addr
, poff
, pshndx
, &in_tls
);
2688 this->filesz_
= *poff
- orig_off
;
2692 uint64_t ret
= this->set_section_list_addresses(layout
, reset
,
2697 // If the last section was a TLS section, align upward to the
2698 // alignment of the TLS segment, so that the overall size of the TLS
2699 // segment is aligned.
2702 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
2703 *poff
= align_address(*poff
, segment_align
);
2706 this->memsz_
= *poff
- orig_off
;
2708 // Ignore the file offset adjustments made by the BSS Output_data
2715 // Set the addresses and file offsets in a list of Output_data
2719 Output_segment::set_section_list_addresses(const Layout
* layout
, bool reset
,
2720 Output_data_list
* pdl
,
2721 uint64_t addr
, off_t
* poff
,
2722 unsigned int* pshndx
,
2725 off_t startoff
= *poff
;
2727 off_t off
= startoff
;
2728 for (Output_data_list::iterator p
= pdl
->begin();
2733 (*p
)->reset_address_and_file_offset();
2735 // When using a linker script the section will most likely
2736 // already have an address.
2737 if (!(*p
)->is_address_valid())
2739 uint64_t align
= (*p
)->addralign();
2741 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2743 // Give the first TLS section the alignment of the
2744 // entire TLS segment. Otherwise the TLS segment as a
2745 // whole may be misaligned.
2748 Output_segment
* tls_segment
= layout
->tls_segment();
2749 gold_assert(tls_segment
!= NULL
);
2750 uint64_t segment_align
= tls_segment
->maximum_alignment();
2751 gold_assert(segment_align
>= align
);
2752 align
= segment_align
;
2759 // If this is the first section after the TLS segment,
2760 // align it to at least the alignment of the TLS
2761 // segment, so that the size of the overall TLS segment
2765 uint64_t segment_align
=
2766 layout
->tls_segment()->maximum_alignment();
2767 if (segment_align
> align
)
2768 align
= segment_align
;
2774 off
= align_address(off
, align
);
2775 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2779 // The script may have inserted a skip forward, but it
2780 // better not have moved backward.
2781 gold_assert((*p
)->address() >= addr
+ (off
- startoff
));
2782 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
2783 (*p
)->set_file_offset(off
);
2784 (*p
)->finalize_data_size();
2787 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2788 // section. Such a section does not affect the size of a
2790 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2791 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
2792 off
+= (*p
)->data_size();
2794 if ((*p
)->is_section())
2796 (*p
)->set_out_shndx(*pshndx
);
2802 return addr
+ (off
- startoff
);
2805 // For a non-PT_LOAD segment, set the offset from the sections, if
2809 Output_segment::set_offset()
2811 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
2813 gold_assert(!this->are_addresses_set_
);
2815 if (this->output_data_
.empty() && this->output_bss_
.empty())
2819 this->are_addresses_set_
= true;
2821 this->min_p_align_
= 0;
2827 const Output_data
* first
;
2828 if (this->output_data_
.empty())
2829 first
= this->output_bss_
.front();
2831 first
= this->output_data_
.front();
2832 this->vaddr_
= first
->address();
2833 this->paddr_
= (first
->has_load_address()
2834 ? first
->load_address()
2836 this->are_addresses_set_
= true;
2837 this->offset_
= first
->offset();
2839 if (this->output_data_
.empty())
2843 const Output_data
* last_data
= this->output_data_
.back();
2844 this->filesz_
= (last_data
->address()
2845 + last_data
->data_size()
2849 const Output_data
* last
;
2850 if (this->output_bss_
.empty())
2851 last
= this->output_data_
.back();
2853 last
= this->output_bss_
.back();
2854 this->memsz_
= (last
->address()
2858 // If this is a TLS segment, align the memory size. The code in
2859 // set_section_list ensures that the section after the TLS segment
2860 // is aligned to give us room.
2861 if (this->type_
== elfcpp::PT_TLS
)
2863 uint64_t segment_align
= this->maximum_alignment();
2864 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
2865 this->memsz_
= align_address(this->memsz_
, segment_align
);
2869 // Set the TLS offsets of the sections in the PT_TLS segment.
2872 Output_segment::set_tls_offsets()
2874 gold_assert(this->type_
== elfcpp::PT_TLS
);
2876 for (Output_data_list::iterator p
= this->output_data_
.begin();
2877 p
!= this->output_data_
.end();
2879 (*p
)->set_tls_offset(this->vaddr_
);
2881 for (Output_data_list::iterator p
= this->output_bss_
.begin();
2882 p
!= this->output_bss_
.end();
2884 (*p
)->set_tls_offset(this->vaddr_
);
2887 // Return the address of the first section.
2890 Output_segment::first_section_load_address() const
2892 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
2893 p
!= this->output_data_
.end();
2895 if ((*p
)->is_section())
2896 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2898 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
2899 p
!= this->output_bss_
.end();
2901 if ((*p
)->is_section())
2902 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2907 // Return the number of Output_sections in an Output_segment.
2910 Output_segment::output_section_count() const
2912 return (this->output_section_count_list(&this->output_data_
)
2913 + this->output_section_count_list(&this->output_bss_
));
2916 // Return the number of Output_sections in an Output_data_list.
2919 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
2921 unsigned int count
= 0;
2922 for (Output_data_list::const_iterator p
= pdl
->begin();
2926 if ((*p
)->is_section())
2932 // Return the section attached to the list segment with the lowest
2933 // load address. This is used when handling a PHDRS clause in a
2937 Output_segment::section_with_lowest_load_address() const
2939 Output_section
* found
= NULL
;
2940 uint64_t found_lma
= 0;
2941 this->lowest_load_address_in_list(&this->output_data_
, &found
, &found_lma
);
2943 Output_section
* found_data
= found
;
2944 this->lowest_load_address_in_list(&this->output_bss_
, &found
, &found_lma
);
2945 if (found
!= found_data
&& found_data
!= NULL
)
2947 gold_error(_("nobits section %s may not precede progbits section %s "
2949 found
->name(), found_data
->name());
2956 // Look through a list for a section with a lower load address.
2959 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
2960 Output_section
** found
,
2961 uint64_t* found_lma
) const
2963 for (Output_data_list::const_iterator p
= pdl
->begin();
2967 if (!(*p
)->is_section())
2969 Output_section
* os
= static_cast<Output_section
*>(*p
);
2970 uint64_t lma
= (os
->has_load_address()
2971 ? os
->load_address()
2973 if (*found
== NULL
|| lma
< *found_lma
)
2981 // Write the segment data into *OPHDR.
2983 template<int size
, bool big_endian
>
2985 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
2987 ophdr
->put_p_type(this->type_
);
2988 ophdr
->put_p_offset(this->offset_
);
2989 ophdr
->put_p_vaddr(this->vaddr_
);
2990 ophdr
->put_p_paddr(this->paddr_
);
2991 ophdr
->put_p_filesz(this->filesz_
);
2992 ophdr
->put_p_memsz(this->memsz_
);
2993 ophdr
->put_p_flags(this->flags_
);
2994 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
2997 // Write the section headers into V.
2999 template<int size
, bool big_endian
>
3001 Output_segment::write_section_headers(const Layout
* layout
,
3002 const Stringpool
* secnamepool
,
3004 unsigned int *pshndx
) const
3006 // Every section that is attached to a segment must be attached to a
3007 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3009 if (this->type_
!= elfcpp::PT_LOAD
)
3012 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3013 &this->output_data_
,
3015 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3021 template<int size
, bool big_endian
>
3023 Output_segment::write_section_headers_list(const Layout
* layout
,
3024 const Stringpool
* secnamepool
,
3025 const Output_data_list
* pdl
,
3027 unsigned int* pshndx
) const
3029 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
3030 for (Output_data_list::const_iterator p
= pdl
->begin();
3034 if ((*p
)->is_section())
3036 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
3037 gold_assert(*pshndx
== ps
->out_shndx());
3038 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
3039 ps
->write_header(layout
, secnamepool
, &oshdr
);
3047 // Output_file methods.
3049 Output_file::Output_file(const char* name
)
3054 map_is_anonymous_(false),
3055 is_temporary_(false)
3059 // Open the output file.
3062 Output_file::open(off_t file_size
)
3064 this->file_size_
= file_size
;
3066 // Unlink the file first; otherwise the open() may fail if the file
3067 // is busy (e.g. it's an executable that's currently being executed).
3069 // However, the linker may be part of a system where a zero-length
3070 // file is created for it to write to, with tight permissions (gcc
3071 // 2.95 did something like this). Unlinking the file would work
3072 // around those permission controls, so we only unlink if the file
3073 // has a non-zero size. We also unlink only regular files to avoid
3074 // trouble with directories/etc.
3076 // If we fail, continue; this command is merely a best-effort attempt
3077 // to improve the odds for open().
3079 // We let the name "-" mean "stdout"
3080 if (!this->is_temporary_
)
3082 if (strcmp(this->name_
, "-") == 0)
3083 this->o_
= STDOUT_FILENO
;
3087 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
3088 unlink_if_ordinary(this->name_
);
3090 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
3091 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
3093 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
3101 // Resize the output file.
3104 Output_file::resize(off_t file_size
)
3106 // If the mmap is mapping an anonymous memory buffer, this is easy:
3107 // just mremap to the new size. If it's mapping to a file, we want
3108 // to unmap to flush to the file, then remap after growing the file.
3109 if (this->map_is_anonymous_
)
3111 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
3113 if (base
== MAP_FAILED
)
3114 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
3115 this->base_
= static_cast<unsigned char*>(base
);
3116 this->file_size_
= file_size
;
3121 this->file_size_
= file_size
;
3126 // Map the file into memory.
3131 const int o
= this->o_
;
3133 // If the output file is not a regular file, don't try to mmap it;
3134 // instead, we'll mmap a block of memory (an anonymous buffer), and
3135 // then later write the buffer to the file.
3137 struct stat statbuf
;
3138 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
3139 || ::fstat(o
, &statbuf
) != 0
3140 || !S_ISREG(statbuf
.st_mode
)
3141 || this->is_temporary_
)
3143 this->map_is_anonymous_
= true;
3144 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3145 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3149 // Write out one byte to make the file the right size.
3150 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
3151 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
3153 if (::write(o
, &b
, 1) != 1)
3154 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
3156 // Map the file into memory.
3157 this->map_is_anonymous_
= false;
3158 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3161 if (base
== MAP_FAILED
)
3162 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
3163 this->base_
= static_cast<unsigned char*>(base
);
3166 // Unmap the file from memory.
3169 Output_file::unmap()
3171 if (::munmap(this->base_
, this->file_size_
) < 0)
3172 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
3176 // Close the output file.
3179 Output_file::close()
3181 // If the map isn't file-backed, we need to write it now.
3182 if (this->map_is_anonymous_
&& !this->is_temporary_
)
3184 size_t bytes_to_write
= this->file_size_
;
3185 while (bytes_to_write
> 0)
3187 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
3188 if (bytes_written
== 0)
3189 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
3190 else if (bytes_written
< 0)
3191 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
3193 bytes_to_write
-= bytes_written
;
3198 // We don't close stdout or stderr
3199 if (this->o_
!= STDOUT_FILENO
3200 && this->o_
!= STDERR_FILENO
3201 && !this->is_temporary_
)
3202 if (::close(this->o_
) < 0)
3203 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
3207 // Instantiate the templates we need. We could use the configure
3208 // script to restrict this to only the ones for implemented targets.
3210 #ifdef HAVE_TARGET_32_LITTLE
3213 Output_section::add_input_section
<32, false>(
3214 Sized_relobj
<32, false>* object
,
3216 const char* secname
,
3217 const elfcpp::Shdr
<32, false>& shdr
,
3218 unsigned int reloc_shndx
,
3219 bool have_sections_script
);
3222 #ifdef HAVE_TARGET_32_BIG
3225 Output_section::add_input_section
<32, true>(
3226 Sized_relobj
<32, true>* object
,
3228 const char* secname
,
3229 const elfcpp::Shdr
<32, true>& shdr
,
3230 unsigned int reloc_shndx
,
3231 bool have_sections_script
);
3234 #ifdef HAVE_TARGET_64_LITTLE
3237 Output_section::add_input_section
<64, false>(
3238 Sized_relobj
<64, false>* object
,
3240 const char* secname
,
3241 const elfcpp::Shdr
<64, false>& shdr
,
3242 unsigned int reloc_shndx
,
3243 bool have_sections_script
);
3246 #ifdef HAVE_TARGET_64_BIG
3249 Output_section::add_input_section
<64, true>(
3250 Sized_relobj
<64, true>* object
,
3252 const char* secname
,
3253 const elfcpp::Shdr
<64, true>& shdr
,
3254 unsigned int reloc_shndx
,
3255 bool have_sections_script
);
3258 #ifdef HAVE_TARGET_32_LITTLE
3260 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
3263 #ifdef HAVE_TARGET_32_BIG
3265 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
3268 #ifdef HAVE_TARGET_64_LITTLE
3270 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
3273 #ifdef HAVE_TARGET_64_BIG
3275 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
3278 #ifdef HAVE_TARGET_32_LITTLE
3280 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
3283 #ifdef HAVE_TARGET_32_BIG
3285 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
3288 #ifdef HAVE_TARGET_64_LITTLE
3290 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
3293 #ifdef HAVE_TARGET_64_BIG
3295 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
3298 #ifdef HAVE_TARGET_32_LITTLE
3300 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
3303 #ifdef HAVE_TARGET_32_BIG
3305 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
3308 #ifdef HAVE_TARGET_64_LITTLE
3310 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3313 #ifdef HAVE_TARGET_64_BIG
3315 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3318 #ifdef HAVE_TARGET_32_LITTLE
3320 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3323 #ifdef HAVE_TARGET_32_BIG
3325 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3328 #ifdef HAVE_TARGET_64_LITTLE
3330 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3333 #ifdef HAVE_TARGET_64_BIG
3335 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3338 #ifdef HAVE_TARGET_32_LITTLE
3340 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
3343 #ifdef HAVE_TARGET_32_BIG
3345 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
3348 #ifdef HAVE_TARGET_64_LITTLE
3350 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
3353 #ifdef HAVE_TARGET_64_BIG
3355 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
3358 #ifdef HAVE_TARGET_32_LITTLE
3360 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
3363 #ifdef HAVE_TARGET_32_BIG
3365 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
3368 #ifdef HAVE_TARGET_64_LITTLE
3370 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
3373 #ifdef HAVE_TARGET_64_BIG
3375 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
3378 #ifdef HAVE_TARGET_32_LITTLE
3380 class Output_data_group
<32, false>;
3383 #ifdef HAVE_TARGET_32_BIG
3385 class Output_data_group
<32, true>;
3388 #ifdef HAVE_TARGET_64_LITTLE
3390 class Output_data_group
<64, false>;
3393 #ifdef HAVE_TARGET_64_BIG
3395 class Output_data_group
<64, true>;
3398 #ifdef HAVE_TARGET_32_LITTLE
3400 class Output_data_got
<32, false>;
3403 #ifdef HAVE_TARGET_32_BIG
3405 class Output_data_got
<32, true>;
3408 #ifdef HAVE_TARGET_64_LITTLE
3410 class Output_data_got
<64, false>;
3413 #ifdef HAVE_TARGET_64_BIG
3415 class Output_data_got
<64, true>;
3418 } // End namespace gold.