1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007 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.
32 #include "libiberty.h" // for unlink_if_ordinary()
34 #include "parameters.h"
41 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
43 # define MAP_ANONYMOUS MAP_ANON
49 // Output_data variables.
51 bool Output_data::allocated_sizes_are_fixed
;
53 // Output_data methods.
55 Output_data::~Output_data()
59 // Return the default alignment for the target size.
62 Output_data::default_alignment()
64 return Output_data::default_alignment_for_size(parameters
->get_size());
67 // Return the default alignment for a size--32 or 64.
70 Output_data::default_alignment_for_size(int size
)
80 // Output_section_header methods. This currently assumes that the
81 // segment and section lists are complete at construction time.
83 Output_section_headers::Output_section_headers(
85 const Layout::Segment_list
* segment_list
,
86 const Layout::Section_list
* section_list
,
87 const Layout::Section_list
* unattached_section_list
,
88 const Stringpool
* secnamepool
)
90 segment_list_(segment_list
),
91 section_list_(section_list
),
92 unattached_section_list_(unattached_section_list
),
93 secnamepool_(secnamepool
)
95 // Count all the sections. Start with 1 for the null section.
97 if (!parameters
->output_is_object())
99 for (Layout::Segment_list::const_iterator p
= segment_list
->begin();
100 p
!= segment_list
->end();
102 if ((*p
)->type() == elfcpp::PT_LOAD
)
103 count
+= (*p
)->output_section_count();
107 for (Layout::Section_list::const_iterator p
= section_list
->begin();
108 p
!= section_list
->end();
110 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
113 count
+= unattached_section_list
->size();
115 const int size
= parameters
->get_size();
118 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
120 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
124 this->set_data_size(count
* shdr_size
);
127 // Write out the section headers.
130 Output_section_headers::do_write(Output_file
* of
)
132 if (parameters
->get_size() == 32)
134 if (parameters
->is_big_endian())
136 #ifdef HAVE_TARGET_32_BIG
137 this->do_sized_write
<32, true>(of
);
144 #ifdef HAVE_TARGET_32_LITTLE
145 this->do_sized_write
<32, false>(of
);
151 else if (parameters
->get_size() == 64)
153 if (parameters
->is_big_endian())
155 #ifdef HAVE_TARGET_64_BIG
156 this->do_sized_write
<64, true>(of
);
163 #ifdef HAVE_TARGET_64_LITTLE
164 this->do_sized_write
<64, false>(of
);
174 template<int size
, bool big_endian
>
176 Output_section_headers::do_sized_write(Output_file
* of
)
178 off_t all_shdrs_size
= this->data_size();
179 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
181 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
182 unsigned char* v
= view
;
185 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
186 oshdr
.put_sh_name(0);
187 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
188 oshdr
.put_sh_flags(0);
189 oshdr
.put_sh_addr(0);
190 oshdr
.put_sh_offset(0);
191 oshdr
.put_sh_size(0);
192 oshdr
.put_sh_link(0);
193 oshdr
.put_sh_info(0);
194 oshdr
.put_sh_addralign(0);
195 oshdr
.put_sh_entsize(0);
200 unsigned int shndx
= 1;
201 if (!parameters
->output_is_object())
203 for (Layout::Segment_list::const_iterator p
=
204 this->segment_list_
->begin();
205 p
!= this->segment_list_
->end();
207 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
214 for (Layout::Section_list::const_iterator p
=
215 this->section_list_
->begin();
216 p
!= this->section_list_
->end();
219 // We do unallocated sections below, except that group
220 // sections have to come first.
221 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
222 && (*p
)->type() != elfcpp::SHT_GROUP
)
224 gold_assert(shndx
== (*p
)->out_shndx());
225 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
226 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
232 for (Layout::Section_list::const_iterator p
=
233 this->unattached_section_list_
->begin();
234 p
!= this->unattached_section_list_
->end();
237 // For a relocatable link, we did unallocated group sections
238 // above, since they have to come first.
239 if ((*p
)->type() == elfcpp::SHT_GROUP
240 && parameters
->output_is_object())
242 gold_assert(shndx
== (*p
)->out_shndx());
243 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
244 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
249 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
252 // Output_segment_header methods.
254 Output_segment_headers::Output_segment_headers(
255 const Layout::Segment_list
& segment_list
)
256 : segment_list_(segment_list
)
258 const int size
= parameters
->get_size();
261 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
263 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
267 this->set_data_size(segment_list
.size() * phdr_size
);
271 Output_segment_headers::do_write(Output_file
* of
)
273 if (parameters
->get_size() == 32)
275 if (parameters
->is_big_endian())
277 #ifdef HAVE_TARGET_32_BIG
278 this->do_sized_write
<32, true>(of
);
285 #ifdef HAVE_TARGET_32_LITTLE
286 this->do_sized_write
<32, false>(of
);
292 else if (parameters
->get_size() == 64)
294 if (parameters
->is_big_endian())
296 #ifdef HAVE_TARGET_64_BIG
297 this->do_sized_write
<64, true>(of
);
304 #ifdef HAVE_TARGET_64_LITTLE
305 this->do_sized_write
<64, false>(of
);
315 template<int size
, bool big_endian
>
317 Output_segment_headers::do_sized_write(Output_file
* of
)
319 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
320 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
321 gold_assert(all_phdrs_size
== this->data_size());
322 unsigned char* view
= of
->get_output_view(this->offset(),
324 unsigned char* v
= view
;
325 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
326 p
!= this->segment_list_
.end();
329 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
330 (*p
)->write_header(&ophdr
);
334 gold_assert(v
- view
== all_phdrs_size
);
336 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
339 // Output_file_header methods.
341 Output_file_header::Output_file_header(const Target
* target
,
342 const Symbol_table
* symtab
,
343 const Output_segment_headers
* osh
,
347 segment_header_(osh
),
348 section_header_(NULL
),
352 const int size
= parameters
->get_size();
355 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
357 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
361 this->set_data_size(ehdr_size
);
364 // Set the section table information for a file header.
367 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
368 const Output_section
* shstrtab
)
370 this->section_header_
= shdrs
;
371 this->shstrtab_
= shstrtab
;
374 // Write out the file header.
377 Output_file_header::do_write(Output_file
* of
)
379 gold_assert(this->offset() == 0);
381 if (parameters
->get_size() == 32)
383 if (parameters
->is_big_endian())
385 #ifdef HAVE_TARGET_32_BIG
386 this->do_sized_write
<32, true>(of
);
393 #ifdef HAVE_TARGET_32_LITTLE
394 this->do_sized_write
<32, false>(of
);
400 else if (parameters
->get_size() == 64)
402 if (parameters
->is_big_endian())
404 #ifdef HAVE_TARGET_64_BIG
405 this->do_sized_write
<64, true>(of
);
412 #ifdef HAVE_TARGET_64_LITTLE
413 this->do_sized_write
<64, false>(of
);
423 // Write out the file header with appropriate size and endianess.
425 template<int size
, bool big_endian
>
427 Output_file_header::do_sized_write(Output_file
* of
)
429 gold_assert(this->offset() == 0);
431 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
432 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
433 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
435 unsigned char e_ident
[elfcpp::EI_NIDENT
];
436 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
437 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
438 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
439 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
440 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
442 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
444 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
447 e_ident
[elfcpp::EI_DATA
] = (big_endian
448 ? elfcpp::ELFDATA2MSB
449 : elfcpp::ELFDATA2LSB
);
450 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
451 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
452 oehdr
.put_e_ident(e_ident
);
455 if (parameters
->output_is_object())
456 e_type
= elfcpp::ET_REL
;
457 else if (parameters
->output_is_shared())
458 e_type
= elfcpp::ET_DYN
;
460 e_type
= elfcpp::ET_EXEC
;
461 oehdr
.put_e_type(e_type
);
463 oehdr
.put_e_machine(this->target_
->machine_code());
464 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
466 oehdr
.put_e_entry(this->entry
<size
>());
468 if (this->segment_header_
== NULL
)
469 oehdr
.put_e_phoff(0);
471 oehdr
.put_e_phoff(this->segment_header_
->offset());
473 oehdr
.put_e_shoff(this->section_header_
->offset());
475 // FIXME: The target needs to set the flags.
476 oehdr
.put_e_flags(0);
478 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
480 if (this->segment_header_
== NULL
)
482 oehdr
.put_e_phentsize(0);
483 oehdr
.put_e_phnum(0);
487 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
488 oehdr
.put_e_phnum(this->segment_header_
->data_size()
489 / elfcpp::Elf_sizes
<size
>::phdr_size
);
492 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
493 oehdr
.put_e_shnum(this->section_header_
->data_size()
494 / elfcpp::Elf_sizes
<size
>::shdr_size
);
495 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
497 of
->write_output_view(0, ehdr_size
, view
);
500 // Return the value to use for the entry address. THIS->ENTRY_ is the
501 // symbol specified on the command line, if any.
504 typename
elfcpp::Elf_types
<size
>::Elf_Addr
505 Output_file_header::entry()
507 const bool should_issue_warning
= (this->entry_
!= NULL
508 && parameters
->output_is_executable());
510 // FIXME: Need to support target specific entry symbol.
511 const char* entry
= this->entry_
;
515 Symbol
* sym
= this->symtab_
->lookup(entry
);
517 typename Sized_symbol
<size
>::Value_type v
;
520 Sized_symbol
<size
>* ssym
;
521 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
522 if (!ssym
->is_defined() && should_issue_warning
)
523 gold_warning("entry symbol '%s' exists but is not defined", entry
);
528 // We couldn't find the entry symbol. See if we can parse it as
529 // a number. This supports, e.g., -e 0x1000.
531 v
= strtoull(entry
, &endptr
, 0);
534 if (should_issue_warning
)
535 gold_warning("cannot find entry symbol '%s'", entry
);
543 // Output_data_const methods.
546 Output_data_const::do_write(Output_file
* of
)
548 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
551 // Output_data_const_buffer methods.
554 Output_data_const_buffer::do_write(Output_file
* of
)
556 of
->write(this->offset(), this->p_
, this->data_size());
559 // Output_section_data methods.
561 // Record the output section, and set the entry size and such.
564 Output_section_data::set_output_section(Output_section
* os
)
566 gold_assert(this->output_section_
== NULL
);
567 this->output_section_
= os
;
568 this->do_adjust_output_section(os
);
571 // Return the section index of the output section.
574 Output_section_data::do_out_shndx() const
576 gold_assert(this->output_section_
!= NULL
);
577 return this->output_section_
->out_shndx();
580 // Output_data_strtab methods.
582 // Set the final data size.
585 Output_data_strtab::set_final_data_size()
587 this->strtab_
->set_string_offsets();
588 this->set_data_size(this->strtab_
->get_strtab_size());
591 // Write out a string table.
594 Output_data_strtab::do_write(Output_file
* of
)
596 this->strtab_
->write(of
, this->offset());
599 // Output_reloc methods.
601 // A reloc against a global symbol.
603 template<bool dynamic
, int size
, bool big_endian
>
604 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
610 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
611 is_relative_(is_relative
), shndx_(INVALID_CODE
)
613 this->u1_
.gsym
= gsym
;
615 if (dynamic
&& !is_relative
)
616 gsym
->set_needs_dynsym_entry();
619 template<bool dynamic
, int size
, bool big_endian
>
620 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
627 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
628 is_relative_(is_relative
), shndx_(shndx
)
630 gold_assert(shndx
!= INVALID_CODE
);
631 this->u1_
.gsym
= gsym
;
632 this->u2_
.relobj
= relobj
;
633 if (dynamic
&& !is_relative
)
634 gsym
->set_needs_dynsym_entry();
637 // A reloc against a local symbol.
639 template<bool dynamic
, int size
, bool big_endian
>
640 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
641 Sized_relobj
<size
, big_endian
>* relobj
,
642 unsigned int local_sym_index
,
647 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
648 is_relative_(is_relative
), shndx_(INVALID_CODE
)
650 gold_assert(local_sym_index
!= GSYM_CODE
651 && local_sym_index
!= INVALID_CODE
);
652 this->u1_
.relobj
= relobj
;
654 if (dynamic
&& !is_relative
)
655 relobj
->set_needs_output_dynsym_entry(local_sym_index
);
658 template<bool dynamic
, int size
, bool big_endian
>
659 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
660 Sized_relobj
<size
, big_endian
>* relobj
,
661 unsigned int local_sym_index
,
666 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
667 is_relative_(is_relative
), shndx_(shndx
)
669 gold_assert(local_sym_index
!= GSYM_CODE
670 && local_sym_index
!= INVALID_CODE
);
671 gold_assert(shndx
!= INVALID_CODE
);
672 this->u1_
.relobj
= relobj
;
673 this->u2_
.relobj
= relobj
;
674 if (dynamic
&& !is_relative
)
675 relobj
->set_needs_output_dynsym_entry(local_sym_index
);
678 // A reloc against the STT_SECTION symbol of an output section.
680 template<bool dynamic
, int size
, bool big_endian
>
681 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
686 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
687 is_relative_(false), shndx_(INVALID_CODE
)
692 os
->set_needs_dynsym_index();
695 template<bool dynamic
, int size
, bool big_endian
>
696 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
702 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
703 is_relative_(false), shndx_(shndx
)
705 gold_assert(shndx
!= INVALID_CODE
);
707 this->u2_
.relobj
= relobj
;
709 os
->set_needs_dynsym_index();
712 // Get the symbol index of a relocation.
714 template<bool dynamic
, int size
, bool big_endian
>
716 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
720 switch (this->local_sym_index_
)
726 if (this->u1_
.gsym
== NULL
)
729 index
= this->u1_
.gsym
->dynsym_index();
731 index
= this->u1_
.gsym
->symtab_index();
736 index
= this->u1_
.os
->dynsym_index();
738 index
= this->u1_
.os
->symtab_index();
742 // Relocations without symbols use a symbol index of 0.
748 index
= this->u1_
.relobj
->dynsym_index(this->local_sym_index_
);
750 index
= this->u1_
.relobj
->symtab_index(this->local_sym_index_
);
753 gold_assert(index
!= -1U);
757 // Write out the offset and info fields of a Rel or Rela relocation
760 template<bool dynamic
, int size
, bool big_endian
>
761 template<typename Write_rel
>
763 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
766 Address address
= this->address_
;
767 if (this->shndx_
!= INVALID_CODE
)
769 section_offset_type off
;
770 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
772 gold_assert(os
!= NULL
);
774 address
+= os
->address() + off
;
777 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
779 gold_assert(address
!= -1U);
782 else if (this->u2_
.od
!= NULL
)
783 address
+= this->u2_
.od
->address();
784 wr
->put_r_offset(address
);
785 unsigned int sym_index
= this->is_relative_
? 0 : this->get_symbol_index();
786 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
789 // Write out a Rel relocation.
791 template<bool dynamic
, int size
, bool big_endian
>
793 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
794 unsigned char* pov
) const
796 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
797 this->write_rel(&orel
);
800 // Get the value of the symbol referred to by a Rel relocation.
802 template<bool dynamic
, int size
, bool big_endian
>
803 typename
elfcpp::Elf_types
<size
>::Elf_Addr
804 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value() const
806 if (this->local_sym_index_
== GSYM_CODE
)
808 const Sized_symbol
<size
>* sym
;
809 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
812 gold_assert(this->local_sym_index_
!= SECTION_CODE
813 && this->local_sym_index_
!= INVALID_CODE
);
814 const Sized_relobj
<size
, big_endian
>* relobj
= this->u1_
.relobj
;
815 return relobj
->local_symbol_value(this->local_sym_index_
);
818 // Write out a Rela relocation.
820 template<bool dynamic
, int size
, bool big_endian
>
822 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
823 unsigned char* pov
) const
825 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
826 this->rel_
.write_rel(&orel
);
827 Addend addend
= this->addend_
;
828 if (rel_
.is_relative())
829 addend
+= rel_
.symbol_value();
830 orel
.put_r_addend(addend
);
833 // Output_data_reloc_base methods.
835 // Adjust the output section.
837 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
839 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
840 ::do_adjust_output_section(Output_section
* os
)
842 if (sh_type
== elfcpp::SHT_REL
)
843 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
844 else if (sh_type
== elfcpp::SHT_RELA
)
845 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
849 os
->set_should_link_to_dynsym();
851 os
->set_should_link_to_symtab();
854 // Write out relocation data.
856 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
858 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
861 const off_t off
= this->offset();
862 const off_t oview_size
= this->data_size();
863 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
865 unsigned char* pov
= oview
;
866 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
867 p
!= this->relocs_
.end();
874 gold_assert(pov
- oview
== oview_size
);
876 of
->write_output_view(off
, oview_size
, oview
);
878 // We no longer need the relocation entries.
879 this->relocs_
.clear();
882 // Class Output_relocatable_relocs.
884 template<int sh_type
, int size
, bool big_endian
>
886 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
888 this->set_data_size(this->rr_
->output_reloc_count()
889 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
892 // class Output_data_group.
894 template<int size
, bool big_endian
>
895 Output_data_group
<size
, big_endian
>::Output_data_group(
896 Sized_relobj
<size
, big_endian
>* relobj
,
897 section_size_type entry_count
,
898 const elfcpp::Elf_Word
* contents
)
899 : Output_section_data(entry_count
* 4, 4),
902 this->flags_
= elfcpp::Swap
<32, big_endian
>::readval(contents
);
903 for (section_size_type i
= 1; i
< entry_count
; ++i
)
905 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
906 this->input_sections_
.push_back(shndx
);
910 // Write out the section group, which means translating the section
911 // indexes to apply to the output file.
913 template<int size
, bool big_endian
>
915 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
917 const off_t off
= this->offset();
918 const section_size_type oview_size
=
919 convert_to_section_size_type(this->data_size());
920 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
922 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
923 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
926 for (std::vector
<unsigned int>::const_iterator p
=
927 this->input_sections_
.begin();
928 p
!= this->input_sections_
.end();
931 section_offset_type dummy
;
932 Output_section
* os
= this->relobj_
->output_section(*p
, &dummy
);
934 unsigned int output_shndx
;
936 output_shndx
= os
->out_shndx();
939 this->relobj_
->error(_("section group retained but "
940 "group element discarded"));
944 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
947 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
948 gold_assert(wrote
== oview_size
);
950 of
->write_output_view(off
, oview_size
, oview
);
952 // We no longer need this information.
953 this->input_sections_
.clear();
956 // Output_data_got::Got_entry methods.
958 // Write out the entry.
960 template<int size
, bool big_endian
>
962 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
966 switch (this->local_sym_index_
)
970 // If the symbol is resolved locally, we need to write out the
971 // link-time value, which will be relocated dynamically by a
972 // RELATIVE relocation.
973 Symbol
* gsym
= this->u_
.gsym
;
974 Sized_symbol
<size
>* sgsym
;
975 // This cast is a bit ugly. We don't want to put a
976 // virtual method in Symbol, because we want Symbol to be
977 // as small as possible.
978 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
979 val
= sgsym
->value();
984 val
= this->u_
.constant
;
988 val
= this->u_
.object
->local_symbol_value(this->local_sym_index_
);
992 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
995 // Output_data_got methods.
997 // Add an entry for a global symbol to the GOT. This returns true if
998 // this is a new GOT entry, false if the symbol already had a GOT
1001 template<int size
, bool big_endian
>
1003 Output_data_got
<size
, big_endian
>::add_global(Symbol
* gsym
)
1005 if (gsym
->has_got_offset())
1008 this->entries_
.push_back(Got_entry(gsym
));
1009 this->set_got_size();
1010 gsym
->set_got_offset(this->last_got_offset());
1014 // Add an entry for a global symbol to the GOT, and add a dynamic
1015 // relocation of type R_TYPE for the GOT entry.
1016 template<int size
, bool big_endian
>
1018 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1021 unsigned int r_type
)
1023 if (gsym
->has_got_offset())
1026 this->entries_
.push_back(Got_entry());
1027 this->set_got_size();
1028 unsigned int got_offset
= this->last_got_offset();
1029 gsym
->set_got_offset(got_offset
);
1030 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1033 template<int size
, bool big_endian
>
1035 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1038 unsigned int r_type
)
1040 if (gsym
->has_got_offset())
1043 this->entries_
.push_back(Got_entry());
1044 this->set_got_size();
1045 unsigned int got_offset
= this->last_got_offset();
1046 gsym
->set_got_offset(got_offset
);
1047 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1050 // Add an entry for a local symbol to the GOT. This returns true if
1051 // this is a new GOT entry, false if the symbol already has a GOT
1054 template<int size
, bool big_endian
>
1056 Output_data_got
<size
, big_endian
>::add_local(
1057 Sized_relobj
<size
, big_endian
>* object
,
1058 unsigned int symndx
)
1060 if (object
->local_has_got_offset(symndx
))
1063 this->entries_
.push_back(Got_entry(object
, symndx
));
1064 this->set_got_size();
1065 object
->set_local_got_offset(symndx
, this->last_got_offset());
1069 // Add an entry for a local symbol to the GOT, and add a dynamic
1070 // relocation of type R_TYPE for the GOT entry.
1071 template<int size
, bool big_endian
>
1073 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1074 Sized_relobj
<size
, big_endian
>* object
,
1075 unsigned int symndx
,
1077 unsigned int r_type
)
1079 if (object
->local_has_got_offset(symndx
))
1082 this->entries_
.push_back(Got_entry());
1083 this->set_got_size();
1084 unsigned int got_offset
= this->last_got_offset();
1085 object
->set_local_got_offset(symndx
, got_offset
);
1086 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1089 template<int size
, bool big_endian
>
1091 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1092 Sized_relobj
<size
, big_endian
>* object
,
1093 unsigned int symndx
,
1095 unsigned int r_type
)
1097 if (object
->local_has_got_offset(symndx
))
1100 this->entries_
.push_back(Got_entry());
1101 this->set_got_size();
1102 unsigned int got_offset
= this->last_got_offset();
1103 object
->set_local_got_offset(symndx
, got_offset
);
1104 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1107 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
1108 // In a pair of entries, the first value in the pair will be used for the
1109 // module index, and the second value will be used for the dtv-relative
1110 // offset. This returns true if this is a new GOT entry, false if the symbol
1111 // already has a GOT entry.
1113 template<int size
, bool big_endian
>
1115 Output_data_got
<size
, big_endian
>::add_global_tls(Symbol
* gsym
, bool need_pair
)
1117 if (gsym
->has_tls_got_offset(need_pair
))
1120 this->entries_
.push_back(Got_entry(gsym
));
1121 gsym
->set_tls_got_offset(this->last_got_offset(), need_pair
);
1123 this->entries_
.push_back(Got_entry(gsym
));
1124 this->set_got_size();
1128 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1129 // relocation of type R_TYPE.
1130 template<int size
, bool big_endian
>
1132 Output_data_got
<size
, big_endian
>::add_global_tls_with_rel(
1135 unsigned int r_type
)
1137 if (gsym
->has_tls_got_offset(false))
1140 this->entries_
.push_back(Got_entry());
1141 this->set_got_size();
1142 unsigned int got_offset
= this->last_got_offset();
1143 gsym
->set_tls_got_offset(got_offset
, false);
1144 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1147 template<int size
, bool big_endian
>
1149 Output_data_got
<size
, big_endian
>::add_global_tls_with_rela(
1152 unsigned int r_type
)
1154 if (gsym
->has_tls_got_offset(false))
1157 this->entries_
.push_back(Got_entry());
1158 this->set_got_size();
1159 unsigned int got_offset
= this->last_got_offset();
1160 gsym
->set_tls_got_offset(got_offset
, false);
1161 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1164 // Add a pair of entries for a global TLS symbol to the GOT, and add
1165 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1166 template<int size
, bool big_endian
>
1168 Output_data_got
<size
, big_endian
>::add_global_tls_with_rel(
1171 unsigned int mod_r_type
,
1172 unsigned int dtv_r_type
)
1174 if (gsym
->has_tls_got_offset(true))
1177 this->entries_
.push_back(Got_entry());
1178 unsigned int got_offset
= this->last_got_offset();
1179 gsym
->set_tls_got_offset(got_offset
, true);
1180 rel_dyn
->add_global(gsym
, mod_r_type
, this, got_offset
);
1182 this->entries_
.push_back(Got_entry());
1183 this->set_got_size();
1184 got_offset
= this->last_got_offset();
1185 rel_dyn
->add_global(gsym
, dtv_r_type
, this, got_offset
);
1188 template<int size
, bool big_endian
>
1190 Output_data_got
<size
, big_endian
>::add_global_tls_with_rela(
1193 unsigned int mod_r_type
,
1194 unsigned int dtv_r_type
)
1196 if (gsym
->has_tls_got_offset(true))
1199 this->entries_
.push_back(Got_entry());
1200 unsigned int got_offset
= this->last_got_offset();
1201 gsym
->set_tls_got_offset(got_offset
, true);
1202 rela_dyn
->add_global(gsym
, mod_r_type
, this, got_offset
, 0);
1204 this->entries_
.push_back(Got_entry());
1205 this->set_got_size();
1206 got_offset
= this->last_got_offset();
1207 rela_dyn
->add_global(gsym
, dtv_r_type
, this, got_offset
, 0);
1210 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1211 // In a pair of entries, the first value in the pair will be used for the
1212 // module index, and the second value will be used for the dtv-relative
1213 // offset. This returns true if this is a new GOT entry, false if the symbol
1214 // already has a GOT entry.
1216 template<int size
, bool big_endian
>
1218 Output_data_got
<size
, big_endian
>::add_local_tls(
1219 Sized_relobj
<size
, big_endian
>* object
,
1220 unsigned int symndx
,
1223 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1226 this->entries_
.push_back(Got_entry(object
, symndx
));
1227 object
->set_local_tls_got_offset(symndx
, this->last_got_offset(), need_pair
);
1229 this->entries_
.push_back(Got_entry(object
, symndx
));
1230 this->set_got_size();
1234 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1235 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1236 // Because this is a local symbol, the first GOT entry can be relocated
1237 // relative to a section symbol, and the second GOT entry will have an
1238 // dtv-relative value that can be computed at link time.
1239 template<int size
, bool big_endian
>
1241 Output_data_got
<size
, big_endian
>::add_local_tls_with_rel(
1242 Sized_relobj
<size
, big_endian
>* object
,
1243 unsigned int symndx
,
1247 unsigned int r_type
)
1249 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1252 this->entries_
.push_back(Got_entry());
1253 unsigned int got_offset
= this->last_got_offset();
1254 object
->set_local_tls_got_offset(symndx
, got_offset
, need_pair
);
1255 section_offset_type off
;
1256 Output_section
* os
= object
->output_section(shndx
, &off
);
1257 rel_dyn
->add_output_section(os
, r_type
, this, got_offset
);
1259 // The second entry of the pair will be statically initialized
1260 // with the TLS offset of the symbol.
1262 this->entries_
.push_back(Got_entry(object
, symndx
));
1264 this->set_got_size();
1267 template<int size
, bool big_endian
>
1269 Output_data_got
<size
, big_endian
>::add_local_tls_with_rela(
1270 Sized_relobj
<size
, big_endian
>* object
,
1271 unsigned int symndx
,
1275 unsigned int r_type
)
1277 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1280 this->entries_
.push_back(Got_entry());
1281 unsigned int got_offset
= this->last_got_offset();
1282 object
->set_local_tls_got_offset(symndx
, got_offset
, need_pair
);
1283 section_offset_type off
;
1284 Output_section
* os
= object
->output_section(shndx
, &off
);
1285 rela_dyn
->add_output_section(os
, r_type
, this, got_offset
, 0);
1287 // The second entry of the pair will be statically initialized
1288 // with the TLS offset of the symbol.
1290 this->entries_
.push_back(Got_entry(object
, symndx
));
1292 this->set_got_size();
1295 // Write out the GOT.
1297 template<int size
, bool big_endian
>
1299 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1301 const int add
= size
/ 8;
1303 const off_t off
= this->offset();
1304 const off_t oview_size
= this->data_size();
1305 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1307 unsigned char* pov
= oview
;
1308 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1309 p
!= this->entries_
.end();
1316 gold_assert(pov
- oview
== oview_size
);
1318 of
->write_output_view(off
, oview_size
, oview
);
1320 // We no longer need the GOT entries.
1321 this->entries_
.clear();
1324 // Output_data_dynamic::Dynamic_entry methods.
1326 // Write out the entry.
1328 template<int size
, bool big_endian
>
1330 Output_data_dynamic::Dynamic_entry::write(
1332 const Stringpool
* pool
1333 ACCEPT_SIZE_ENDIAN
) const
1335 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1336 switch (this->classification_
)
1338 case DYNAMIC_NUMBER
:
1342 case DYNAMIC_SECTION_ADDRESS
:
1343 val
= this->u_
.od
->address();
1346 case DYNAMIC_SECTION_SIZE
:
1347 val
= this->u_
.od
->data_size();
1350 case DYNAMIC_SYMBOL
:
1352 const Sized_symbol
<size
>* s
=
1353 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1358 case DYNAMIC_STRING
:
1359 val
= pool
->get_offset(this->u_
.str
);
1366 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1367 dw
.put_d_tag(this->tag_
);
1371 // Output_data_dynamic methods.
1373 // Adjust the output section to set the entry size.
1376 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1378 if (parameters
->get_size() == 32)
1379 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1380 else if (parameters
->get_size() == 64)
1381 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1386 // Set the final data size.
1389 Output_data_dynamic::set_final_data_size()
1391 // Add the terminating entry.
1392 this->add_constant(elfcpp::DT_NULL
, 0);
1395 if (parameters
->get_size() == 32)
1396 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1397 else if (parameters
->get_size() == 64)
1398 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1401 this->set_data_size(this->entries_
.size() * dyn_size
);
1404 // Write out the dynamic entries.
1407 Output_data_dynamic::do_write(Output_file
* of
)
1409 if (parameters
->get_size() == 32)
1411 if (parameters
->is_big_endian())
1413 #ifdef HAVE_TARGET_32_BIG
1414 this->sized_write
<32, true>(of
);
1421 #ifdef HAVE_TARGET_32_LITTLE
1422 this->sized_write
<32, false>(of
);
1428 else if (parameters
->get_size() == 64)
1430 if (parameters
->is_big_endian())
1432 #ifdef HAVE_TARGET_64_BIG
1433 this->sized_write
<64, true>(of
);
1440 #ifdef HAVE_TARGET_64_LITTLE
1441 this->sized_write
<64, false>(of
);
1451 template<int size
, bool big_endian
>
1453 Output_data_dynamic::sized_write(Output_file
* of
)
1455 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1457 const off_t offset
= this->offset();
1458 const off_t oview_size
= this->data_size();
1459 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1461 unsigned char* pov
= oview
;
1462 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1463 p
!= this->entries_
.end();
1466 p
->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1467 pov
, this->pool_
SELECT_SIZE_ENDIAN(size
, big_endian
));
1471 gold_assert(pov
- oview
== oview_size
);
1473 of
->write_output_view(offset
, oview_size
, oview
);
1475 // We no longer need the dynamic entries.
1476 this->entries_
.clear();
1479 // Output_section::Input_section methods.
1481 // Return the data size. For an input section we store the size here.
1482 // For an Output_section_data, we have to ask it for the size.
1485 Output_section::Input_section::data_size() const
1487 if (this->is_input_section())
1488 return this->u1_
.data_size
;
1490 return this->u2_
.posd
->data_size();
1493 // Set the address and file offset.
1496 Output_section::Input_section::set_address_and_file_offset(
1499 off_t section_file_offset
)
1501 if (this->is_input_section())
1502 this->u2_
.object
->set_section_offset(this->shndx_
,
1503 file_offset
- section_file_offset
);
1505 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1508 // Reset the address and file offset.
1511 Output_section::Input_section::reset_address_and_file_offset()
1513 if (!this->is_input_section())
1514 this->u2_
.posd
->reset_address_and_file_offset();
1517 // Finalize the data size.
1520 Output_section::Input_section::finalize_data_size()
1522 if (!this->is_input_section())
1523 this->u2_
.posd
->finalize_data_size();
1526 // Try to turn an input offset into an output offset. We want to
1527 // return the output offset relative to the start of this
1528 // Input_section in the output section.
1531 Output_section::Input_section::output_offset(
1532 const Relobj
* object
,
1534 section_offset_type offset
,
1535 section_offset_type
*poutput
) const
1537 if (!this->is_input_section())
1538 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1541 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1548 // Return whether this is the merge section for the input section
1552 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1553 unsigned int shndx
) const
1555 if (this->is_input_section())
1557 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1560 // Write out the data. We don't have to do anything for an input
1561 // section--they are handled via Object::relocate--but this is where
1562 // we write out the data for an Output_section_data.
1565 Output_section::Input_section::write(Output_file
* of
)
1567 if (!this->is_input_section())
1568 this->u2_
.posd
->write(of
);
1571 // Write the data to a buffer. As for write(), we don't have to do
1572 // anything for an input section.
1575 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1577 if (!this->is_input_section())
1578 this->u2_
.posd
->write_to_buffer(buffer
);
1581 // Output_section methods.
1583 // Construct an Output_section. NAME will point into a Stringpool.
1585 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1586 elfcpp::Elf_Xword flags
)
1591 link_section_(NULL
),
1593 info_section_(NULL
),
1602 first_input_offset_(0),
1604 postprocessing_buffer_(NULL
),
1605 needs_symtab_index_(false),
1606 needs_dynsym_index_(false),
1607 should_link_to_symtab_(false),
1608 should_link_to_dynsym_(false),
1609 after_input_sections_(false),
1610 requires_postprocessing_(false),
1611 found_in_sections_clause_(false),
1612 has_load_address_(false),
1615 // An unallocated section has no address. Forcing this means that
1616 // we don't need special treatment for symbols defined in debug
1618 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1619 this->set_address(0);
1622 Output_section::~Output_section()
1626 // Set the entry size.
1629 Output_section::set_entsize(uint64_t v
)
1631 if (this->entsize_
== 0)
1634 gold_assert(this->entsize_
== v
);
1637 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1638 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1639 // relocation section which applies to this section, or 0 if none, or
1640 // -1U if more than one. Return the offset of the input section
1641 // within the output section. Return -1 if the input section will
1642 // receive special handling. In the normal case we don't always keep
1643 // track of input sections for an Output_section. Instead, each
1644 // Object keeps track of the Output_section for each of its input
1645 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1646 // track of input sections here; this is used when SECTIONS appears in
1649 template<int size
, bool big_endian
>
1651 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1653 const char* secname
,
1654 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1655 unsigned int reloc_shndx
,
1656 bool have_sections_script
)
1658 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1659 if ((addralign
& (addralign
- 1)) != 0)
1661 object
->error(_("invalid alignment %lu for section \"%s\""),
1662 static_cast<unsigned long>(addralign
), secname
);
1666 if (addralign
> this->addralign_
)
1667 this->addralign_
= addralign
;
1669 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1670 this->flags_
|= (sh_flags
1671 & (elfcpp::SHF_WRITE
1673 | elfcpp::SHF_EXECINSTR
));
1675 uint64_t entsize
= shdr
.get_sh_entsize();
1677 // .debug_str is a mergeable string section, but is not always so
1678 // marked by compilers. Mark manually here so we can optimize.
1679 if (strcmp(secname
, ".debug_str") == 0)
1681 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1685 // If this is a SHF_MERGE section, we pass all the input sections to
1686 // a Output_data_merge. We don't try to handle relocations for such
1688 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1689 && reloc_shndx
== 0)
1691 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1692 entsize
, addralign
))
1694 // Tell the relocation routines that they need to call the
1695 // output_offset method to determine the final address.
1700 off_t offset_in_section
= this->current_data_size_for_child();
1701 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1704 if (aligned_offset_in_section
> offset_in_section
1705 && !have_sections_script
1706 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1707 && object
->target()->has_code_fill())
1709 // We need to add some fill data. Using fill_list_ when
1710 // possible is an optimization, since we will often have fill
1711 // sections without input sections.
1712 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1713 if (this->input_sections_
.empty())
1714 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1717 // FIXME: When relaxing, the size needs to adjust to
1718 // maintain a constant alignment.
1719 std::string
fill_data(object
->target()->code_fill(fill_len
));
1720 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1721 this->input_sections_
.push_back(Input_section(odc
));
1725 this->set_current_data_size_for_child(aligned_offset_in_section
1726 + shdr
.get_sh_size());
1728 // We need to keep track of this section if we are already keeping
1729 // track of sections, or if we are relaxing. FIXME: Add test for
1731 if (have_sections_script
|| !this->input_sections_
.empty())
1732 this->input_sections_
.push_back(Input_section(object
, shndx
,
1736 return aligned_offset_in_section
;
1739 // Add arbitrary data to an output section.
1742 Output_section::add_output_section_data(Output_section_data
* posd
)
1744 Input_section
inp(posd
);
1745 this->add_output_section_data(&inp
);
1747 if (posd
->is_data_size_valid())
1749 off_t offset_in_section
= this->current_data_size_for_child();
1750 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1752 this->set_current_data_size_for_child(aligned_offset_in_section
1753 + posd
->data_size());
1757 // Add arbitrary data to an output section by Input_section.
1760 Output_section::add_output_section_data(Input_section
* inp
)
1762 if (this->input_sections_
.empty())
1763 this->first_input_offset_
= this->current_data_size_for_child();
1765 this->input_sections_
.push_back(*inp
);
1767 uint64_t addralign
= inp
->addralign();
1768 if (addralign
> this->addralign_
)
1769 this->addralign_
= addralign
;
1771 inp
->set_output_section(this);
1774 // Add a merge section to an output section.
1777 Output_section::add_output_merge_section(Output_section_data
* posd
,
1778 bool is_string
, uint64_t entsize
)
1780 Input_section
inp(posd
, is_string
, entsize
);
1781 this->add_output_section_data(&inp
);
1784 // Add an input section to a SHF_MERGE section.
1787 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1788 uint64_t flags
, uint64_t entsize
,
1791 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1793 // We only merge strings if the alignment is not more than the
1794 // character size. This could be handled, but it's unusual.
1795 if (is_string
&& addralign
> entsize
)
1798 Input_section_list::iterator p
;
1799 for (p
= this->input_sections_
.begin();
1800 p
!= this->input_sections_
.end();
1802 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1804 p
->add_input_section(object
, shndx
);
1808 // We handle the actual constant merging in Output_merge_data or
1809 // Output_merge_string_data.
1810 Output_section_data
* posd
;
1812 posd
= new Output_merge_data(entsize
, addralign
);
1818 posd
= new Output_merge_string
<char>(addralign
);
1821 posd
= new Output_merge_string
<uint16_t>(addralign
);
1824 posd
= new Output_merge_string
<uint32_t>(addralign
);
1831 this->add_output_merge_section(posd
, is_string
, entsize
);
1832 posd
->add_input_section(object
, shndx
);
1837 // Given an address OFFSET relative to the start of input section
1838 // SHNDX in OBJECT, return whether this address is being included in
1839 // the final link. This should only be called if SHNDX in OBJECT has
1840 // a special mapping.
1843 Output_section::is_input_address_mapped(const Relobj
* object
,
1847 gold_assert(object
->is_section_specially_mapped(shndx
));
1849 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1850 p
!= this->input_sections_
.end();
1853 section_offset_type output_offset
;
1854 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1855 return output_offset
!= -1;
1858 // By default we assume that the address is mapped. This should
1859 // only be called after we have passed all sections to Layout. At
1860 // that point we should know what we are discarding.
1864 // Given an address OFFSET relative to the start of input section
1865 // SHNDX in object OBJECT, return the output offset relative to the
1866 // start of the input section in the output section. This should only
1867 // be called if SHNDX in OBJECT has a special mapping.
1870 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
1871 section_offset_type offset
) const
1873 gold_assert(object
->is_section_specially_mapped(shndx
));
1874 // This can only be called meaningfully when layout is complete.
1875 gold_assert(Output_data::is_layout_complete());
1877 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1878 p
!= this->input_sections_
.end();
1881 section_offset_type output_offset
;
1882 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1883 return output_offset
;
1888 // Return the output virtual address of OFFSET relative to the start
1889 // of input section SHNDX in object OBJECT.
1892 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1895 gold_assert(object
->is_section_specially_mapped(shndx
));
1897 uint64_t addr
= this->address() + this->first_input_offset_
;
1898 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1899 p
!= this->input_sections_
.end();
1902 addr
= align_address(addr
, p
->addralign());
1903 section_offset_type output_offset
;
1904 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1906 if (output_offset
== -1)
1908 return addr
+ output_offset
;
1910 addr
+= p
->data_size();
1913 // If we get here, it means that we don't know the mapping for this
1914 // input section. This might happen in principle if
1915 // add_input_section were called before add_output_section_data.
1916 // But it should never actually happen.
1921 // Return the output address of the start of the merged section for
1922 // input section SHNDX in object OBJECT.
1925 Output_section::starting_output_address(const Relobj
* object
,
1926 unsigned int shndx
) const
1928 gold_assert(object
->is_section_specially_mapped(shndx
));
1930 uint64_t addr
= this->address() + this->first_input_offset_
;
1931 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1932 p
!= this->input_sections_
.end();
1935 addr
= align_address(addr
, p
->addralign());
1937 // It would be nice if we could use the existing output_offset
1938 // method to get the output offset of input offset 0.
1939 // Unfortunately we don't know for sure that input offset 0 is
1941 if (p
->is_merge_section_for(object
, shndx
))
1944 addr
+= p
->data_size();
1949 // Set the data size of an Output_section. This is where we handle
1950 // setting the addresses of any Output_section_data objects.
1953 Output_section::set_final_data_size()
1955 if (this->input_sections_
.empty())
1957 this->set_data_size(this->current_data_size_for_child());
1961 uint64_t address
= this->address();
1962 off_t startoff
= this->offset();
1963 off_t off
= startoff
+ this->first_input_offset_
;
1964 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1965 p
!= this->input_sections_
.end();
1968 off
= align_address(off
, p
->addralign());
1969 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
1971 off
+= p
->data_size();
1974 this->set_data_size(off
- startoff
);
1977 // Reset the address and file offset.
1980 Output_section::do_reset_address_and_file_offset()
1982 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1983 p
!= this->input_sections_
.end();
1985 p
->reset_address_and_file_offset();
1988 // Set the TLS offset. Called only for SHT_TLS sections.
1991 Output_section::do_set_tls_offset(uint64_t tls_base
)
1993 this->tls_offset_
= this->address() - tls_base
;
1996 // Write the section header to *OSHDR.
1998 template<int size
, bool big_endian
>
2000 Output_section::write_header(const Layout
* layout
,
2001 const Stringpool
* secnamepool
,
2002 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
2004 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
2005 oshdr
->put_sh_type(this->type_
);
2007 elfcpp::Elf_Xword flags
= this->flags_
;
2008 if (this->info_section_
!= NULL
)
2009 flags
|= elfcpp::SHF_INFO_LINK
;
2010 oshdr
->put_sh_flags(flags
);
2012 oshdr
->put_sh_addr(this->address());
2013 oshdr
->put_sh_offset(this->offset());
2014 oshdr
->put_sh_size(this->data_size());
2015 if (this->link_section_
!= NULL
)
2016 oshdr
->put_sh_link(this->link_section_
->out_shndx());
2017 else if (this->should_link_to_symtab_
)
2018 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
2019 else if (this->should_link_to_dynsym_
)
2020 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
2022 oshdr
->put_sh_link(this->link_
);
2023 if (this->info_section_
!= NULL
)
2024 oshdr
->put_sh_info(this->info_section_
->out_shndx());
2025 else if (this->info_symndx_
!= NULL
)
2026 oshdr
->put_sh_info(this->info_symndx_
->symtab_index());
2028 oshdr
->put_sh_info(this->info_
);
2029 oshdr
->put_sh_addralign(this->addralign_
);
2030 oshdr
->put_sh_entsize(this->entsize_
);
2033 // Write out the data. For input sections the data is written out by
2034 // Object::relocate, but we have to handle Output_section_data objects
2038 Output_section::do_write(Output_file
* of
)
2040 gold_assert(!this->requires_postprocessing());
2042 off_t output_section_file_offset
= this->offset();
2043 for (Fill_list::iterator p
= this->fills_
.begin();
2044 p
!= this->fills_
.end();
2047 std::string
fill_data(parameters
->target()->code_fill(p
->length()));
2048 of
->write(output_section_file_offset
+ p
->section_offset(),
2049 fill_data
.data(), fill_data
.size());
2052 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2053 p
!= this->input_sections_
.end();
2058 // If a section requires postprocessing, create the buffer to use.
2061 Output_section::create_postprocessing_buffer()
2063 gold_assert(this->requires_postprocessing());
2065 if (this->postprocessing_buffer_
!= NULL
)
2068 if (!this->input_sections_
.empty())
2070 off_t off
= this->first_input_offset_
;
2071 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2072 p
!= this->input_sections_
.end();
2075 off
= align_address(off
, p
->addralign());
2076 p
->finalize_data_size();
2077 off
+= p
->data_size();
2079 this->set_current_data_size_for_child(off
);
2082 off_t buffer_size
= this->current_data_size_for_child();
2083 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
2086 // Write all the data of an Output_section into the postprocessing
2087 // buffer. This is used for sections which require postprocessing,
2088 // such as compression. Input sections are handled by
2089 // Object::Relocate.
2092 Output_section::write_to_postprocessing_buffer()
2094 gold_assert(this->requires_postprocessing());
2096 Target
* target
= parameters
->target();
2097 unsigned char* buffer
= this->postprocessing_buffer();
2098 for (Fill_list::iterator p
= this->fills_
.begin();
2099 p
!= this->fills_
.end();
2102 std::string
fill_data(target
->code_fill(p
->length()));
2103 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
2107 off_t off
= this->first_input_offset_
;
2108 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2109 p
!= this->input_sections_
.end();
2112 off
= align_address(off
, p
->addralign());
2113 p
->write_to_buffer(buffer
+ off
);
2114 off
+= p
->data_size();
2118 // Get the input sections for linker script processing. We leave
2119 // behind the Output_section_data entries. Note that this may be
2120 // slightly incorrect for merge sections. We will leave them behind,
2121 // but it is possible that the script says that they should follow
2122 // some other input sections, as in:
2123 // .rodata { *(.rodata) *(.rodata.cst*) }
2124 // For that matter, we don't handle this correctly:
2125 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2126 // With luck this will never matter.
2129 Output_section::get_input_sections(
2131 const std::string
& fill
,
2132 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
2134 uint64_t orig_address
= address
;
2136 address
= align_address(address
, this->addralign());
2138 Input_section_list remaining
;
2139 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2140 p
!= this->input_sections_
.end();
2143 if (p
->is_input_section())
2144 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2147 uint64_t aligned_address
= align_address(address
, p
->addralign());
2148 if (aligned_address
!= address
&& !fill
.empty())
2150 section_size_type length
=
2151 convert_to_section_size_type(aligned_address
- address
);
2152 std::string this_fill
;
2153 this_fill
.reserve(length
);
2154 while (this_fill
.length() + fill
.length() <= length
)
2156 if (this_fill
.length() < length
)
2157 this_fill
.append(fill
, 0, length
- this_fill
.length());
2159 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2160 remaining
.push_back(Input_section(posd
));
2162 address
= aligned_address
;
2164 remaining
.push_back(*p
);
2166 p
->finalize_data_size();
2167 address
+= p
->data_size();
2171 this->input_sections_
.swap(remaining
);
2172 this->first_input_offset_
= 0;
2174 uint64_t data_size
= address
- orig_address
;
2175 this->set_current_data_size_for_child(data_size
);
2179 // Add an input section from a script.
2182 Output_section::add_input_section_for_script(Relobj
* object
,
2187 if (addralign
> this->addralign_
)
2188 this->addralign_
= addralign
;
2190 off_t offset_in_section
= this->current_data_size_for_child();
2191 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2194 this->set_current_data_size_for_child(aligned_offset_in_section
2197 this->input_sections_
.push_back(Input_section(object
, shndx
,
2198 data_size
, addralign
));
2201 // Print stats for merge sections to stderr.
2204 Output_section::print_merge_stats()
2206 Input_section_list::iterator p
;
2207 for (p
= this->input_sections_
.begin();
2208 p
!= this->input_sections_
.end();
2210 p
->print_merge_stats(this->name_
);
2213 // Output segment methods.
2215 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2227 is_max_align_known_(false),
2228 are_addresses_set_(false)
2232 // Add an Output_section to an Output_segment.
2235 Output_segment::add_output_section(Output_section
* os
,
2236 elfcpp::Elf_Word seg_flags
,
2239 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2240 gold_assert(!this->is_max_align_known_
);
2242 // Update the segment flags.
2243 this->flags_
|= seg_flags
;
2245 Output_segment::Output_data_list
* pdl
;
2246 if (os
->type() == elfcpp::SHT_NOBITS
)
2247 pdl
= &this->output_bss_
;
2249 pdl
= &this->output_data_
;
2251 // So that PT_NOTE segments will work correctly, we need to ensure
2252 // that all SHT_NOTE sections are adjacent. This will normally
2253 // happen automatically, because all the SHT_NOTE input sections
2254 // will wind up in the same output section. However, it is possible
2255 // for multiple SHT_NOTE input sections to have different section
2256 // flags, and thus be in different output sections, but for the
2257 // different section flags to map into the same segment flags and
2258 // thus the same output segment.
2260 // Note that while there may be many input sections in an output
2261 // section, there are normally only a few output sections in an
2262 // output segment. This loop is expected to be fast.
2264 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2266 Output_segment::Output_data_list::iterator p
= pdl
->end();
2270 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2272 // We don't worry about the FRONT parameter.
2278 while (p
!= pdl
->begin());
2281 // Similarly, so that PT_TLS segments will work, we need to group
2282 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2283 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2284 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2285 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2286 // and the PT_TLS segment -- we do this grouping only for the
2288 if (this->type_
!= elfcpp::PT_TLS
2289 && (os
->flags() & elfcpp::SHF_TLS
) != 0
2290 && !this->output_data_
.empty())
2292 pdl
= &this->output_data_
;
2293 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2294 bool sawtls
= false;
2295 Output_segment::Output_data_list::iterator p
= pdl
->end();
2300 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2303 // Put a NOBITS section after the first TLS section.
2304 // But a PROGBITS section after the first TLS/PROGBITS
2306 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2310 // If we've gone past the TLS sections, but we've seen a
2311 // TLS section, then we need to insert this section now.
2317 // We don't worry about the FRONT parameter.
2323 while (p
!= pdl
->begin());
2325 // There are no TLS sections yet; put this one at the requested
2326 // location in the section list.
2330 pdl
->push_front(os
);
2335 // Add an Output_data (which is not an Output_section) to the start of
2339 Output_segment::add_initial_output_data(Output_data
* od
)
2341 gold_assert(!this->is_max_align_known_
);
2342 this->output_data_
.push_front(od
);
2345 // Return the maximum alignment of the Output_data in Output_segment.
2348 Output_segment::maximum_alignment()
2350 if (!this->is_max_align_known_
)
2354 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2355 if (addralign
> this->max_align_
)
2356 this->max_align_
= addralign
;
2358 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2359 if (addralign
> this->max_align_
)
2360 this->max_align_
= addralign
;
2362 this->is_max_align_known_
= true;
2365 return this->max_align_
;
2368 // Return the maximum alignment of a list of Output_data.
2371 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2374 for (Output_data_list::const_iterator p
= pdl
->begin();
2378 uint64_t addralign
= (*p
)->addralign();
2379 if (addralign
> ret
)
2385 // Return the number of dynamic relocs applied to this segment.
2388 Output_segment::dynamic_reloc_count() const
2390 return (this->dynamic_reloc_count_list(&this->output_data_
)
2391 + this->dynamic_reloc_count_list(&this->output_bss_
));
2394 // Return the number of dynamic relocs applied to an Output_data_list.
2397 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2399 unsigned int count
= 0;
2400 for (Output_data_list::const_iterator p
= pdl
->begin();
2403 count
+= (*p
)->dynamic_reloc_count();
2407 // Set the section addresses for an Output_segment. If RESET is true,
2408 // reset the addresses first. ADDR is the address and *POFF is the
2409 // file offset. Set the section indexes starting with *PSHNDX.
2410 // Return the address of the immediately following segment. Update
2411 // *POFF and *PSHNDX.
2414 Output_segment::set_section_addresses(bool reset
, uint64_t addr
, off_t
* poff
,
2415 unsigned int* pshndx
)
2417 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2419 if (!reset
&& this->are_addresses_set_
)
2421 gold_assert(this->paddr_
== addr
);
2422 addr
= this->vaddr_
;
2426 this->vaddr_
= addr
;
2427 this->paddr_
= addr
;
2428 this->are_addresses_set_
= true;
2431 off_t orig_off
= *poff
;
2432 this->offset_
= orig_off
;
2434 addr
= this->set_section_list_addresses(reset
, &this->output_data_
,
2435 addr
, poff
, pshndx
);
2436 this->filesz_
= *poff
- orig_off
;
2440 uint64_t ret
= this->set_section_list_addresses(reset
, &this->output_bss_
,
2441 addr
, poff
, pshndx
);
2442 this->memsz_
= *poff
- orig_off
;
2444 // Ignore the file offset adjustments made by the BSS Output_data
2451 // Set the addresses and file offsets in a list of Output_data
2455 Output_segment::set_section_list_addresses(bool reset
, Output_data_list
* pdl
,
2456 uint64_t addr
, off_t
* poff
,
2457 unsigned int* pshndx
)
2459 off_t startoff
= *poff
;
2461 off_t off
= startoff
;
2462 for (Output_data_list::iterator p
= pdl
->begin();
2467 (*p
)->reset_address_and_file_offset();
2469 // When using a linker script the section will most likely
2470 // already have an address.
2471 if (!(*p
)->is_address_valid())
2473 off
= align_address(off
, (*p
)->addralign());
2474 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2478 // The script may have inserted a skip forward, but it
2479 // better not have moved backward.
2480 gold_assert((*p
)->address() >= addr
+ (off
- startoff
));
2481 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
2482 (*p
)->set_file_offset(off
);
2483 (*p
)->finalize_data_size();
2486 // Unless this is a PT_TLS segment, we want to ignore the size
2487 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2488 // affect the size of a PT_LOAD segment.
2489 if (this->type_
== elfcpp::PT_TLS
2490 || !(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2491 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
2492 off
+= (*p
)->data_size();
2494 if ((*p
)->is_section())
2496 (*p
)->set_out_shndx(*pshndx
);
2502 return addr
+ (off
- startoff
);
2505 // For a non-PT_LOAD segment, set the offset from the sections, if
2509 Output_segment::set_offset()
2511 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
2513 gold_assert(!this->are_addresses_set_
);
2515 if (this->output_data_
.empty() && this->output_bss_
.empty())
2519 this->are_addresses_set_
= true;
2521 this->min_p_align_
= 0;
2527 const Output_data
* first
;
2528 if (this->output_data_
.empty())
2529 first
= this->output_bss_
.front();
2531 first
= this->output_data_
.front();
2532 this->vaddr_
= first
->address();
2533 this->paddr_
= (first
->has_load_address()
2534 ? first
->load_address()
2536 this->are_addresses_set_
= true;
2537 this->offset_
= first
->offset();
2539 if (this->output_data_
.empty())
2543 const Output_data
* last_data
= this->output_data_
.back();
2544 this->filesz_
= (last_data
->address()
2545 + last_data
->data_size()
2549 const Output_data
* last
;
2550 if (this->output_bss_
.empty())
2551 last
= this->output_data_
.back();
2553 last
= this->output_bss_
.back();
2554 this->memsz_
= (last
->address()
2559 // Set the TLS offsets of the sections in the PT_TLS segment.
2562 Output_segment::set_tls_offsets()
2564 gold_assert(this->type_
== elfcpp::PT_TLS
);
2566 for (Output_data_list::iterator p
= this->output_data_
.begin();
2567 p
!= this->output_data_
.end();
2569 (*p
)->set_tls_offset(this->vaddr_
);
2571 for (Output_data_list::iterator p
= this->output_bss_
.begin();
2572 p
!= this->output_bss_
.end();
2574 (*p
)->set_tls_offset(this->vaddr_
);
2577 // Return the address of the first section.
2580 Output_segment::first_section_load_address() const
2582 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
2583 p
!= this->output_data_
.end();
2585 if ((*p
)->is_section())
2586 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2588 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
2589 p
!= this->output_bss_
.end();
2591 if ((*p
)->is_section())
2592 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2597 // Return the number of Output_sections in an Output_segment.
2600 Output_segment::output_section_count() const
2602 return (this->output_section_count_list(&this->output_data_
)
2603 + this->output_section_count_list(&this->output_bss_
));
2606 // Return the number of Output_sections in an Output_data_list.
2609 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
2611 unsigned int count
= 0;
2612 for (Output_data_list::const_iterator p
= pdl
->begin();
2616 if ((*p
)->is_section())
2622 // Return the section attached to the list segment with the lowest
2623 // load address. This is used when handling a PHDRS clause in a
2627 Output_segment::section_with_lowest_load_address() const
2629 Output_section
* found
= NULL
;
2630 uint64_t found_lma
= 0;
2631 this->lowest_load_address_in_list(&this->output_data_
, &found
, &found_lma
);
2633 Output_section
* found_data
= found
;
2634 this->lowest_load_address_in_list(&this->output_bss_
, &found
, &found_lma
);
2635 if (found
!= found_data
&& found_data
!= NULL
)
2637 gold_error(_("nobits section %s may not precede progbits section %s "
2639 found
->name(), found_data
->name());
2646 // Look through a list for a section with a lower load address.
2649 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
2650 Output_section
** found
,
2651 uint64_t* found_lma
) const
2653 for (Output_data_list::const_iterator p
= pdl
->begin();
2657 if (!(*p
)->is_section())
2659 Output_section
* os
= static_cast<Output_section
*>(*p
);
2660 uint64_t lma
= (os
->has_load_address()
2661 ? os
->load_address()
2663 if (*found
== NULL
|| lma
< *found_lma
)
2671 // Write the segment data into *OPHDR.
2673 template<int size
, bool big_endian
>
2675 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
2677 ophdr
->put_p_type(this->type_
);
2678 ophdr
->put_p_offset(this->offset_
);
2679 ophdr
->put_p_vaddr(this->vaddr_
);
2680 ophdr
->put_p_paddr(this->paddr_
);
2681 ophdr
->put_p_filesz(this->filesz_
);
2682 ophdr
->put_p_memsz(this->memsz_
);
2683 ophdr
->put_p_flags(this->flags_
);
2684 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
2687 // Write the section headers into V.
2689 template<int size
, bool big_endian
>
2691 Output_segment::write_section_headers(const Layout
* layout
,
2692 const Stringpool
* secnamepool
,
2694 unsigned int *pshndx
2695 ACCEPT_SIZE_ENDIAN
) const
2697 // Every section that is attached to a segment must be attached to a
2698 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2700 if (this->type_
!= elfcpp::PT_LOAD
)
2703 v
= this->write_section_headers_list
2704 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
2705 layout
, secnamepool
, &this->output_data_
, v
, pshndx
2706 SELECT_SIZE_ENDIAN(size
, big_endian
));
2707 v
= this->write_section_headers_list
2708 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
2709 layout
, secnamepool
, &this->output_bss_
, v
, pshndx
2710 SELECT_SIZE_ENDIAN(size
, big_endian
));
2714 template<int size
, bool big_endian
>
2716 Output_segment::write_section_headers_list(const Layout
* layout
,
2717 const Stringpool
* secnamepool
,
2718 const Output_data_list
* pdl
,
2720 unsigned int* pshndx
2721 ACCEPT_SIZE_ENDIAN
) const
2723 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2724 for (Output_data_list::const_iterator p
= pdl
->begin();
2728 if ((*p
)->is_section())
2730 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
2731 gold_assert(*pshndx
== ps
->out_shndx());
2732 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
2733 ps
->write_header(layout
, secnamepool
, &oshdr
);
2741 // Output_file methods.
2743 Output_file::Output_file(const char* name
)
2748 map_is_anonymous_(false)
2752 // Open the output file.
2755 Output_file::open(off_t file_size
)
2757 this->file_size_
= file_size
;
2759 // Unlink the file first; otherwise the open() may fail if the file
2760 // is busy (e.g. it's an executable that's currently being executed).
2762 // However, the linker may be part of a system where a zero-length
2763 // file is created for it to write to, with tight permissions (gcc
2764 // 2.95 did something like this). Unlinking the file would work
2765 // around those permission controls, so we only unlink if the file
2766 // has a non-zero size. We also unlink only regular files to avoid
2767 // trouble with directories/etc.
2769 // If we fail, continue; this command is merely a best-effort attempt
2770 // to improve the odds for open().
2772 // We let the name "-" mean "stdout"
2773 if (strcmp(this->name_
, "-") == 0)
2774 this->o_
= STDOUT_FILENO
;
2778 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
2779 unlink_if_ordinary(this->name_
);
2781 int mode
= parameters
->output_is_object() ? 0666 : 0777;
2782 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
2784 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
2791 // Resize the output file.
2794 Output_file::resize(off_t file_size
)
2796 // If the mmap is mapping an anonymous memory buffer, this is easy:
2797 // just mremap to the new size. If it's mapping to a file, we want
2798 // to unmap to flush to the file, then remap after growing the file.
2799 if (this->map_is_anonymous_
)
2801 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
2803 if (base
== MAP_FAILED
)
2804 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
2805 this->base_
= static_cast<unsigned char*>(base
);
2806 this->file_size_
= file_size
;
2811 this->file_size_
= file_size
;
2816 // Map the file into memory.
2821 const int o
= this->o_
;
2823 // If the output file is not a regular file, don't try to mmap it;
2824 // instead, we'll mmap a block of memory (an anonymous buffer), and
2825 // then later write the buffer to the file.
2827 struct stat statbuf
;
2828 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
2829 || ::fstat(o
, &statbuf
) != 0
2830 || !S_ISREG(statbuf
.st_mode
))
2832 this->map_is_anonymous_
= true;
2833 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
2834 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2838 // Write out one byte to make the file the right size.
2839 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
2840 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
2842 if (::write(o
, &b
, 1) != 1)
2843 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
2845 // Map the file into memory.
2846 this->map_is_anonymous_
= false;
2847 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
2850 if (base
== MAP_FAILED
)
2851 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
2852 this->base_
= static_cast<unsigned char*>(base
);
2855 // Unmap the file from memory.
2858 Output_file::unmap()
2860 if (::munmap(this->base_
, this->file_size_
) < 0)
2861 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
2865 // Close the output file.
2868 Output_file::close()
2870 // If the map isn't file-backed, we need to write it now.
2871 if (this->map_is_anonymous_
)
2873 size_t bytes_to_write
= this->file_size_
;
2874 while (bytes_to_write
> 0)
2876 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
2877 if (bytes_written
== 0)
2878 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
2879 else if (bytes_written
< 0)
2880 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
2882 bytes_to_write
-= bytes_written
;
2887 // We don't close stdout or stderr
2888 if (this->o_
!= STDOUT_FILENO
&& this->o_
!= STDERR_FILENO
)
2889 if (::close(this->o_
) < 0)
2890 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
2894 // Instantiate the templates we need. We could use the configure
2895 // script to restrict this to only the ones for implemented targets.
2897 #ifdef HAVE_TARGET_32_LITTLE
2900 Output_section::add_input_section
<32, false>(
2901 Sized_relobj
<32, false>* object
,
2903 const char* secname
,
2904 const elfcpp::Shdr
<32, false>& shdr
,
2905 unsigned int reloc_shndx
,
2906 bool have_sections_script
);
2909 #ifdef HAVE_TARGET_32_BIG
2912 Output_section::add_input_section
<32, true>(
2913 Sized_relobj
<32, true>* object
,
2915 const char* secname
,
2916 const elfcpp::Shdr
<32, true>& shdr
,
2917 unsigned int reloc_shndx
,
2918 bool have_sections_script
);
2921 #ifdef HAVE_TARGET_64_LITTLE
2924 Output_section::add_input_section
<64, false>(
2925 Sized_relobj
<64, false>* object
,
2927 const char* secname
,
2928 const elfcpp::Shdr
<64, false>& shdr
,
2929 unsigned int reloc_shndx
,
2930 bool have_sections_script
);
2933 #ifdef HAVE_TARGET_64_BIG
2936 Output_section::add_input_section
<64, true>(
2937 Sized_relobj
<64, true>* object
,
2939 const char* secname
,
2940 const elfcpp::Shdr
<64, true>& shdr
,
2941 unsigned int reloc_shndx
,
2942 bool have_sections_script
);
2945 #ifdef HAVE_TARGET_32_LITTLE
2947 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
2950 #ifdef HAVE_TARGET_32_BIG
2952 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
2955 #ifdef HAVE_TARGET_64_LITTLE
2957 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
2960 #ifdef HAVE_TARGET_64_BIG
2962 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
2965 #ifdef HAVE_TARGET_32_LITTLE
2967 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
2970 #ifdef HAVE_TARGET_32_BIG
2972 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
2975 #ifdef HAVE_TARGET_64_LITTLE
2977 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
2980 #ifdef HAVE_TARGET_64_BIG
2982 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
2985 #ifdef HAVE_TARGET_32_LITTLE
2987 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
2990 #ifdef HAVE_TARGET_32_BIG
2992 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
2995 #ifdef HAVE_TARGET_64_LITTLE
2997 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3000 #ifdef HAVE_TARGET_64_BIG
3002 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3005 #ifdef HAVE_TARGET_32_LITTLE
3007 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3010 #ifdef HAVE_TARGET_32_BIG
3012 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3015 #ifdef HAVE_TARGET_64_LITTLE
3017 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3020 #ifdef HAVE_TARGET_64_BIG
3022 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3025 #ifdef HAVE_TARGET_32_LITTLE
3027 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
3030 #ifdef HAVE_TARGET_32_BIG
3032 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
3035 #ifdef HAVE_TARGET_64_LITTLE
3037 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
3040 #ifdef HAVE_TARGET_64_BIG
3042 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
3045 #ifdef HAVE_TARGET_32_LITTLE
3047 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
3050 #ifdef HAVE_TARGET_32_BIG
3052 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
3055 #ifdef HAVE_TARGET_64_LITTLE
3057 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
3060 #ifdef HAVE_TARGET_64_BIG
3062 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
3065 #ifdef HAVE_TARGET_32_LITTLE
3067 class Output_data_group
<32, false>;
3070 #ifdef HAVE_TARGET_32_BIG
3072 class Output_data_group
<32, true>;
3075 #ifdef HAVE_TARGET_64_LITTLE
3077 class Output_data_group
<64, false>;
3080 #ifdef HAVE_TARGET_64_BIG
3082 class Output_data_group
<64, true>;
3085 #ifdef HAVE_TARGET_32_LITTLE
3087 class Output_data_got
<32, false>;
3090 #ifdef HAVE_TARGET_32_BIG
3092 class Output_data_got
<32, true>;
3095 #ifdef HAVE_TARGET_64_LITTLE
3097 class Output_data_got
<64, false>;
3100 #ifdef HAVE_TARGET_64_BIG
3102 class Output_data_got
<64, true>;
3105 } // End namespace gold.