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 "parameters.h"
42 // Output_data variables.
44 bool Output_data::sizes_are_fixed
;
46 // Output_data methods.
48 Output_data::~Output_data()
52 // Set the address and offset.
55 Output_data::set_address(uint64_t addr
, off_t off
)
57 this->address_
= addr
;
60 // Let the child class know.
61 this->do_set_address(addr
, off
);
64 // Return the default alignment for a size--32 or 64.
67 Output_data::default_alignment(int size
)
77 // Output_section_header methods. This currently assumes that the
78 // segment and section lists are complete at construction time.
80 Output_section_headers::Output_section_headers(
84 const Layout::Segment_list
* segment_list
,
85 const Layout::Section_list
* unattached_section_list
,
86 const Stringpool
* secnamepool
)
88 big_endian_(big_endian
),
90 segment_list_(segment_list
),
91 unattached_section_list_(unattached_section_list
),
92 secnamepool_(secnamepool
)
94 // Count all the sections. Start with 1 for the null section.
96 for (Layout::Segment_list::const_iterator p
= segment_list
->begin();
97 p
!= segment_list
->end();
99 if ((*p
)->type() == elfcpp::PT_LOAD
)
100 count
+= (*p
)->output_section_count();
101 count
+= unattached_section_list
->size();
105 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
107 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
111 this->set_data_size(count
* shdr_size
);
114 // Write out the section headers.
117 Output_section_headers::do_write(Output_file
* of
)
119 if (this->size_
== 32)
121 if (this->big_endian_
)
122 this->do_sized_write
<32, true>(of
);
124 this->do_sized_write
<32, false>(of
);
126 else if (this->size_
== 64)
128 if (this->big_endian_
)
129 this->do_sized_write
<64, true>(of
);
131 this->do_sized_write
<64, false>(of
);
137 template<int size
, bool big_endian
>
139 Output_section_headers::do_sized_write(Output_file
* of
)
141 off_t all_shdrs_size
= this->data_size();
142 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
144 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
145 unsigned char* v
= view
;
148 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
149 oshdr
.put_sh_name(0);
150 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
151 oshdr
.put_sh_flags(0);
152 oshdr
.put_sh_addr(0);
153 oshdr
.put_sh_offset(0);
154 oshdr
.put_sh_size(0);
155 oshdr
.put_sh_link(0);
156 oshdr
.put_sh_info(0);
157 oshdr
.put_sh_addralign(0);
158 oshdr
.put_sh_entsize(0);
164 for (Layout::Segment_list::const_iterator p
= this->segment_list_
->begin();
165 p
!= this->segment_list_
->end();
167 v
= (*p
)->write_section_headers
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
168 this->layout_
, this->secnamepool_
, v
, &shndx
169 SELECT_SIZE_ENDIAN(size
, big_endian
));
170 for (Layout::Section_list::const_iterator p
=
171 this->unattached_section_list_
->begin();
172 p
!= this->unattached_section_list_
->end();
175 gold_assert(shndx
== (*p
)->out_shndx());
176 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
177 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
182 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
185 // Output_segment_header methods.
187 Output_segment_headers::Output_segment_headers(
190 const Layout::Segment_list
& segment_list
)
191 : size_(size
), big_endian_(big_endian
), segment_list_(segment_list
)
195 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
197 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
201 this->set_data_size(segment_list
.size() * phdr_size
);
205 Output_segment_headers::do_write(Output_file
* of
)
207 if (this->size_
== 32)
209 if (this->big_endian_
)
210 this->do_sized_write
<32, true>(of
);
212 this->do_sized_write
<32, false>(of
);
214 else if (this->size_
== 64)
216 if (this->big_endian_
)
217 this->do_sized_write
<64, true>(of
);
219 this->do_sized_write
<64, false>(of
);
225 template<int size
, bool big_endian
>
227 Output_segment_headers::do_sized_write(Output_file
* of
)
229 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
230 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
231 unsigned char* view
= of
->get_output_view(this->offset(),
233 unsigned char* v
= view
;
234 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
235 p
!= this->segment_list_
.end();
238 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
239 (*p
)->write_header(&ophdr
);
243 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
246 // Output_file_header methods.
248 Output_file_header::Output_file_header(int size
,
250 const Target
* target
,
251 const Symbol_table
* symtab
,
252 const Output_segment_headers
* osh
)
254 big_endian_(big_endian
),
257 segment_header_(osh
),
258 section_header_(NULL
),
263 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
265 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
269 this->set_data_size(ehdr_size
);
272 // Set the section table information for a file header.
275 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
276 const Output_section
* shstrtab
)
278 this->section_header_
= shdrs
;
279 this->shstrtab_
= shstrtab
;
282 // Write out the file header.
285 Output_file_header::do_write(Output_file
* of
)
287 if (this->size_
== 32)
289 if (this->big_endian_
)
290 this->do_sized_write
<32, true>(of
);
292 this->do_sized_write
<32, false>(of
);
294 else if (this->size_
== 64)
296 if (this->big_endian_
)
297 this->do_sized_write
<64, true>(of
);
299 this->do_sized_write
<64, false>(of
);
305 // Write out the file header with appropriate size and endianess.
307 template<int size
, bool big_endian
>
309 Output_file_header::do_sized_write(Output_file
* of
)
311 gold_assert(this->offset() == 0);
313 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
314 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
315 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
317 unsigned char e_ident
[elfcpp::EI_NIDENT
];
318 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
319 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
320 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
321 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
322 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
324 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
326 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
329 e_ident
[elfcpp::EI_DATA
] = (big_endian
330 ? elfcpp::ELFDATA2MSB
331 : elfcpp::ELFDATA2LSB
);
332 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
333 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
334 oehdr
.put_e_ident(e_ident
);
338 if (parameters
->output_is_object())
339 e_type
= elfcpp::ET_REL
;
341 e_type
= elfcpp::ET_EXEC
;
342 oehdr
.put_e_type(e_type
);
344 oehdr
.put_e_machine(this->target_
->machine_code());
345 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
347 // FIXME: Need to support -e, and target specific entry symbol.
348 Symbol
* sym
= this->symtab_
->lookup("_start");
349 typename Sized_symbol
<size
>::Value_type v
;
354 Sized_symbol
<size
>* ssym
;
355 ssym
= this->symtab_
->get_sized_symbol
SELECT_SIZE_NAME(size
) (
356 sym
SELECT_SIZE(size
));
359 oehdr
.put_e_entry(v
);
361 oehdr
.put_e_phoff(this->segment_header_
->offset());
362 oehdr
.put_e_shoff(this->section_header_
->offset());
364 // FIXME: The target needs to set the flags.
365 oehdr
.put_e_flags(0);
367 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
368 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
369 oehdr
.put_e_phnum(this->segment_header_
->data_size()
370 / elfcpp::Elf_sizes
<size
>::phdr_size
);
371 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
372 oehdr
.put_e_shnum(this->section_header_
->data_size()
373 / elfcpp::Elf_sizes
<size
>::shdr_size
);
374 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
376 of
->write_output_view(0, ehdr_size
, view
);
379 // Output_data_const methods.
382 Output_data_const::do_write(Output_file
* of
)
384 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
387 // Output_data_const_buffer methods.
390 Output_data_const_buffer::do_write(Output_file
* of
)
392 of
->write(this->offset(), this->p_
, this->data_size());
395 // Output_section_data methods.
397 // Record the output section, and set the entry size and such.
400 Output_section_data::set_output_section(Output_section
* os
)
402 gold_assert(this->output_section_
== NULL
);
403 this->output_section_
= os
;
404 this->do_adjust_output_section(os
);
407 // Return the section index of the output section.
410 Output_section_data::do_out_shndx() const
412 gold_assert(this->output_section_
!= NULL
);
413 return this->output_section_
->out_shndx();
416 // Output_data_strtab methods.
418 // Set the address. We don't actually care about the address, but we
419 // do set our final size.
422 Output_data_strtab::do_set_address(uint64_t, off_t
)
424 this->strtab_
->set_string_offsets();
425 this->set_data_size(this->strtab_
->get_strtab_size());
428 // Write out a string table.
431 Output_data_strtab::do_write(Output_file
* of
)
433 this->strtab_
->write(of
, this->offset());
436 // Output_reloc methods.
438 // Get the symbol index of a relocation.
440 template<bool dynamic
, int size
, bool big_endian
>
442 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
446 switch (this->local_sym_index_
)
452 if (this->u1_
.gsym
== NULL
)
455 index
= this->u1_
.gsym
->dynsym_index();
457 index
= this->u1_
.gsym
->symtab_index();
462 index
= this->u1_
.os
->dynsym_index();
464 index
= this->u1_
.os
->symtab_index();
470 // FIXME: It seems that some targets may need to generate
471 // dynamic relocations against local symbols for some
472 // reasons. This will have to be addressed at some point.
476 index
= this->u1_
.relobj
->symtab_index(this->local_sym_index_
);
479 gold_assert(index
!= -1U);
483 // Write out the offset and info fields of a Rel or Rela relocation
486 template<bool dynamic
, int size
, bool big_endian
>
487 template<typename Write_rel
>
489 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
492 Address address
= this->address_
;
493 if (this->shndx_
!= INVALID_CODE
)
496 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
498 gold_assert(os
!= NULL
);
499 address
+= os
->address() + off
;
501 else if (this->u2_
.od
!= NULL
)
502 address
+= this->u2_
.od
->address();
503 wr
->put_r_offset(address
);
504 wr
->put_r_info(elfcpp::elf_r_info
<size
>(this->get_symbol_index(),
508 // Write out a Rel relocation.
510 template<bool dynamic
, int size
, bool big_endian
>
512 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
513 unsigned char* pov
) const
515 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
516 this->write_rel(&orel
);
519 // Write out a Rela relocation.
521 template<bool dynamic
, int size
, bool big_endian
>
523 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
524 unsigned char* pov
) const
526 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
527 this->rel_
.write_rel(&orel
);
528 orel
.put_r_addend(this->addend_
);
531 // Output_data_reloc_base methods.
533 // Adjust the output section.
535 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
537 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
538 ::do_adjust_output_section(Output_section
* os
)
540 if (sh_type
== elfcpp::SHT_REL
)
541 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
542 else if (sh_type
== elfcpp::SHT_RELA
)
543 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
547 os
->set_should_link_to_dynsym();
549 os
->set_should_link_to_symtab();
552 // Write out relocation data.
554 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
556 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
559 const off_t off
= this->offset();
560 const off_t oview_size
= this->data_size();
561 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
563 unsigned char* pov
= oview
;
564 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
565 p
!= this->relocs_
.end();
572 gold_assert(pov
- oview
== oview_size
);
574 of
->write_output_view(off
, oview_size
, oview
);
576 // We no longer need the relocation entries.
577 this->relocs_
.clear();
580 // Output_data_got::Got_entry methods.
582 // Write out the entry.
584 template<int size
, bool big_endian
>
586 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
590 switch (this->local_sym_index_
)
594 Symbol
* gsym
= this->u_
.gsym
;
596 // If the symbol is resolved locally, we need to write out its
597 // value. Otherwise we just write zero. The target code is
598 // responsible for creating a relocation entry to fill in the
600 if (gsym
->final_value_is_known())
602 Sized_symbol
<size
>* sgsym
;
603 // This cast is a bit ugly. We don't want to put a
604 // virtual method in Symbol, because we want Symbol to be
605 // as small as possible.
606 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
607 val
= sgsym
->value();
613 val
= this->u_
.constant
;
620 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
623 // Output_data_got methods.
625 // Add an entry for a global symbol to the GOT. This returns true if
626 // this is a new GOT entry, false if the symbol already had a GOT
629 template<int size
, bool big_endian
>
631 Output_data_got
<size
, big_endian
>::add_global(Symbol
* gsym
)
633 if (gsym
->has_got_offset())
636 this->entries_
.push_back(Got_entry(gsym
));
637 this->set_got_size();
638 gsym
->set_got_offset(this->last_got_offset());
642 // Write out the GOT.
644 template<int size
, bool big_endian
>
646 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
648 const int add
= size
/ 8;
650 const off_t off
= this->offset();
651 const off_t oview_size
= this->data_size();
652 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
654 unsigned char* pov
= oview
;
655 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
656 p
!= this->entries_
.end();
663 gold_assert(pov
- oview
== oview_size
);
665 of
->write_output_view(off
, oview_size
, oview
);
667 // We no longer need the GOT entries.
668 this->entries_
.clear();
671 // Output_data_dynamic::Dynamic_entry methods.
673 // Write out the entry.
675 template<int size
, bool big_endian
>
677 Output_data_dynamic::Dynamic_entry::write(
679 const Stringpool
* pool
680 ACCEPT_SIZE_ENDIAN
) const
682 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
683 switch (this->classification_
)
689 case DYNAMIC_SECTION_ADDRESS
:
690 val
= this->u_
.od
->address();
693 case DYNAMIC_SECTION_SIZE
:
694 val
= this->u_
.od
->data_size();
699 const Sized_symbol
<size
>* s
=
700 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
706 val
= pool
->get_offset(this->u_
.str
);
713 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
714 dw
.put_d_tag(this->tag_
);
718 // Output_data_dynamic methods.
720 // Adjust the output section to set the entry size.
723 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
725 if (this->target_
->get_size() == 32)
726 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
727 else if (this->target_
->get_size() == 64)
728 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
733 // Set the final data size.
736 Output_data_dynamic::do_set_address(uint64_t, off_t
)
738 // Add the terminating entry.
739 this->add_constant(elfcpp::DT_NULL
, 0);
742 if (this->target_
->get_size() == 32)
743 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
744 else if (this->target_
->get_size() == 64)
745 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
748 this->set_data_size(this->entries_
.size() * dyn_size
);
751 // Write out the dynamic entries.
754 Output_data_dynamic::do_write(Output_file
* of
)
756 if (this->target_
->get_size() == 32)
758 if (this->target_
->is_big_endian())
759 this->sized_write
<32, true>(of
);
761 this->sized_write
<32, false>(of
);
763 else if (this->target_
->get_size() == 64)
765 if (this->target_
->is_big_endian())
766 this->sized_write
<64, true>(of
);
768 this->sized_write
<64, false>(of
);
774 template<int size
, bool big_endian
>
776 Output_data_dynamic::sized_write(Output_file
* of
)
778 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
780 const off_t offset
= this->offset();
781 const off_t oview_size
= this->data_size();
782 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
784 unsigned char* pov
= oview
;
785 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
786 p
!= this->entries_
.end();
789 p
->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
790 pov
, this->pool_
SELECT_SIZE_ENDIAN(size
, big_endian
));
794 gold_assert(pov
- oview
== oview_size
);
796 of
->write_output_view(offset
, oview_size
, oview
);
798 // We no longer need the dynamic entries.
799 this->entries_
.clear();
802 // Output_section::Input_section methods.
804 // Return the data size. For an input section we store the size here.
805 // For an Output_section_data, we have to ask it for the size.
808 Output_section::Input_section::data_size() const
810 if (this->is_input_section())
811 return this->u1_
.data_size
;
813 return this->u2_
.posd
->data_size();
816 // Set the address and file offset.
819 Output_section::Input_section::set_address(uint64_t addr
, off_t off
,
822 if (this->is_input_section())
823 this->u2_
.object
->set_section_offset(this->shndx_
, off
- secoff
);
825 this->u2_
.posd
->set_address(addr
, off
);
828 // Try to turn an input address into an output address.
831 Output_section::Input_section::output_address(const Relobj
* object
,
834 uint64_t output_section_address
,
835 uint64_t *poutput
) const
837 if (!this->is_input_section())
838 return this->u2_
.posd
->output_address(object
, shndx
, offset
,
839 output_section_address
, poutput
);
842 if (this->shndx_
!= shndx
843 || this->u2_
.object
!= object
)
846 Output_section
* os
= object
->output_section(shndx
, &output_offset
);
847 gold_assert(os
!= NULL
);
848 *poutput
= output_section_address
+ output_offset
+ offset
;
853 // Write out the data. We don't have to do anything for an input
854 // section--they are handled via Object::relocate--but this is where
855 // we write out the data for an Output_section_data.
858 Output_section::Input_section::write(Output_file
* of
)
860 if (!this->is_input_section())
861 this->u2_
.posd
->write(of
);
864 // Output_section methods.
866 // Construct an Output_section. NAME will point into a Stringpool.
868 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
869 elfcpp::Elf_Xword flags
)
883 first_input_offset_(0),
885 needs_symtab_index_(false),
886 needs_dynsym_index_(false),
887 should_link_to_symtab_(false),
888 should_link_to_dynsym_(false)
892 Output_section::~Output_section()
896 // Set the entry size.
899 Output_section::set_entsize(uint64_t v
)
901 if (this->entsize_
== 0)
904 gold_assert(this->entsize_
== v
);
907 // Add the input section SHNDX, with header SHDR, named SECNAME, in
908 // OBJECT, to the Output_section. Return the offset of the input
909 // section within the output section. We don't always keep track of
910 // input sections for an Output_section. Instead, each Object keeps
911 // track of the Output_section for each of its input sections.
913 template<int size
, bool big_endian
>
915 Output_section::add_input_section(Relobj
* object
, unsigned int shndx
,
917 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
919 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
920 if ((addralign
& (addralign
- 1)) != 0)
922 fprintf(stderr
, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
923 program_name
, object
->name().c_str(),
924 static_cast<unsigned long>(addralign
), secname
);
928 if (addralign
> this->addralign_
)
929 this->addralign_
= addralign
;
931 // If this is a SHF_MERGE section, we pass all the input sections to
932 // a Output_data_merge.
933 if ((shdr
.get_sh_flags() & elfcpp::SHF_MERGE
) != 0)
935 if (this->add_merge_input_section(object
, shndx
, shdr
.get_sh_flags(),
936 shdr
.get_sh_entsize(),
939 // Tell the relocation routines that they need to call the
940 // output_address method to determine the final address.
945 off_t offset_in_section
= this->data_size();
946 off_t aligned_offset_in_section
= align_address(offset_in_section
,
949 if (aligned_offset_in_section
> offset_in_section
950 && (shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0
951 && object
->target()->has_code_fill())
953 // We need to add some fill data. Using fill_list_ when
954 // possible is an optimization, since we will often have fill
955 // sections without input sections.
956 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
957 if (this->input_sections_
.empty())
958 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
961 // FIXME: When relaxing, the size needs to adjust to
962 // maintain a constant alignment.
963 std::string
fill_data(object
->target()->code_fill(fill_len
));
964 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
965 this->input_sections_
.push_back(Input_section(odc
));
969 this->set_data_size(aligned_offset_in_section
+ shdr
.get_sh_size());
971 // We need to keep track of this section if we are already keeping
972 // track of sections, or if we are relaxing. FIXME: Add test for
974 if (!this->input_sections_
.empty())
975 this->input_sections_
.push_back(Input_section(object
, shndx
,
979 return aligned_offset_in_section
;
982 // Add arbitrary data to an output section.
985 Output_section::add_output_section_data(Output_section_data
* posd
)
987 Input_section
inp(posd
);
988 this->add_output_section_data(&inp
);
991 // Add arbitrary data to an output section by Input_section.
994 Output_section::add_output_section_data(Input_section
* inp
)
996 if (this->input_sections_
.empty())
997 this->first_input_offset_
= this->data_size();
999 this->input_sections_
.push_back(*inp
);
1001 uint64_t addralign
= inp
->addralign();
1002 if (addralign
> this->addralign_
)
1003 this->addralign_
= addralign
;
1005 inp
->set_output_section(this);
1008 // Add a merge section to an output section.
1011 Output_section::add_output_merge_section(Output_section_data
* posd
,
1012 bool is_string
, uint64_t entsize
)
1014 Input_section
inp(posd
, is_string
, entsize
);
1015 this->add_output_section_data(&inp
);
1018 // Add an input section to a SHF_MERGE section.
1021 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1022 uint64_t flags
, uint64_t entsize
,
1025 // We only merge constants if the alignment is not more than the
1026 // entry size. This could be handled, but it's unusual.
1027 if (addralign
> entsize
)
1030 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1031 Input_section_list::iterator p
;
1032 for (p
= this->input_sections_
.begin();
1033 p
!= this->input_sections_
.end();
1035 if (p
->is_merge_section(is_string
, entsize
))
1038 // We handle the actual constant merging in Output_merge_data or
1039 // Output_merge_string_data.
1040 if (p
!= this->input_sections_
.end())
1041 p
->add_input_section(object
, shndx
);
1044 Output_section_data
* posd
;
1046 posd
= new Output_merge_data(entsize
);
1047 else if (entsize
== 1)
1048 posd
= new Output_merge_string
<char>();
1049 else if (entsize
== 2)
1050 posd
= new Output_merge_string
<uint16_t>();
1051 else if (entsize
== 4)
1052 posd
= new Output_merge_string
<uint32_t>();
1056 this->add_output_merge_section(posd
, is_string
, entsize
);
1057 posd
->add_input_section(object
, shndx
);
1063 // Return the output virtual address of OFFSET relative to the start
1064 // of input section SHNDX in object OBJECT.
1067 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1070 uint64_t addr
= this->address() + this->first_input_offset_
;
1071 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1072 p
!= this->input_sections_
.end();
1075 addr
= align_address(addr
, p
->addralign());
1077 if (p
->output_address(object
, shndx
, offset
, addr
, &output
))
1079 addr
+= p
->data_size();
1082 // If we get here, it means that we don't know the mapping for this
1083 // input section. This might happen in principle if
1084 // add_input_section were called before add_output_section_data.
1085 // But it should never actually happen.
1090 // Set the address of an Output_section. This is where we handle
1091 // setting the addresses of any Output_section_data objects.
1094 Output_section::do_set_address(uint64_t address
, off_t startoff
)
1096 if (this->input_sections_
.empty())
1099 off_t off
= startoff
+ this->first_input_offset_
;
1100 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1101 p
!= this->input_sections_
.end();
1104 off
= align_address(off
, p
->addralign());
1105 p
->set_address(address
+ (off
- startoff
), off
, startoff
);
1106 off
+= p
->data_size();
1109 this->set_data_size(off
- startoff
);
1112 // Write the section header to *OSHDR.
1114 template<int size
, bool big_endian
>
1116 Output_section::write_header(const Layout
* layout
,
1117 const Stringpool
* secnamepool
,
1118 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
1120 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
1121 oshdr
->put_sh_type(this->type_
);
1122 oshdr
->put_sh_flags(this->flags_
);
1123 oshdr
->put_sh_addr(this->address());
1124 oshdr
->put_sh_offset(this->offset());
1125 oshdr
->put_sh_size(this->data_size());
1126 if (this->link_section_
!= NULL
)
1127 oshdr
->put_sh_link(this->link_section_
->out_shndx());
1128 else if (this->should_link_to_symtab_
)
1129 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
1130 else if (this->should_link_to_dynsym_
)
1131 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
1133 oshdr
->put_sh_link(this->link_
);
1134 if (this->info_section_
!= NULL
)
1135 oshdr
->put_sh_info(this->info_section_
->out_shndx());
1137 oshdr
->put_sh_info(this->info_
);
1138 oshdr
->put_sh_addralign(this->addralign_
);
1139 oshdr
->put_sh_entsize(this->entsize_
);
1142 // Write out the data. For input sections the data is written out by
1143 // Object::relocate, but we have to handle Output_section_data objects
1147 Output_section::do_write(Output_file
* of
)
1149 off_t output_section_file_offset
= this->offset();
1150 for (Fill_list::iterator p
= this->fills_
.begin();
1151 p
!= this->fills_
.end();
1154 std::string
fill_data(of
->target()->code_fill(p
->length()));
1155 of
->write(output_section_file_offset
+ p
->section_offset(),
1156 fill_data
.data(), fill_data
.size());
1159 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1160 p
!= this->input_sections_
.end();
1165 // Output segment methods.
1167 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
1178 is_align_known_(false)
1182 // Add an Output_section to an Output_segment.
1185 Output_segment::add_output_section(Output_section
* os
,
1186 elfcpp::Elf_Word seg_flags
,
1189 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
1190 gold_assert(!this->is_align_known_
);
1192 // Update the segment flags.
1193 this->flags_
|= seg_flags
;
1195 Output_segment::Output_data_list
* pdl
;
1196 if (os
->type() == elfcpp::SHT_NOBITS
)
1197 pdl
= &this->output_bss_
;
1199 pdl
= &this->output_data_
;
1201 // So that PT_NOTE segments will work correctly, we need to ensure
1202 // that all SHT_NOTE sections are adjacent. This will normally
1203 // happen automatically, because all the SHT_NOTE input sections
1204 // will wind up in the same output section. However, it is possible
1205 // for multiple SHT_NOTE input sections to have different section
1206 // flags, and thus be in different output sections, but for the
1207 // different section flags to map into the same segment flags and
1208 // thus the same output segment.
1210 // Note that while there may be many input sections in an output
1211 // section, there are normally only a few output sections in an
1212 // output segment. This loop is expected to be fast.
1214 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
1216 Output_segment::Output_data_list::iterator p
= pdl
->end();
1220 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
1222 // We don't worry about the FRONT parameter.
1228 while (p
!= pdl
->begin());
1231 // Similarly, so that PT_TLS segments will work, we need to group
1232 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1233 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1234 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1236 if ((os
->flags() & elfcpp::SHF_TLS
) != 0 && !this->output_data_
.empty())
1238 pdl
= &this->output_data_
;
1239 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
1240 bool sawtls
= false;
1241 Output_segment::Output_data_list::iterator p
= pdl
->end();
1246 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
1249 // Put a NOBITS section after the first TLS section.
1250 // But a PROGBITS section after the first TLS/PROGBITS
1252 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
1256 // If we've gone past the TLS sections, but we've seen a
1257 // TLS section, then we need to insert this section now.
1263 // We don't worry about the FRONT parameter.
1269 while (p
!= pdl
->begin());
1271 // There are no TLS sections yet; put this one at the requested
1272 // location in the section list.
1276 pdl
->push_front(os
);
1281 // Add an Output_data (which is not an Output_section) to the start of
1285 Output_segment::add_initial_output_data(Output_data
* od
)
1287 gold_assert(!this->is_align_known_
);
1288 this->output_data_
.push_front(od
);
1291 // Return the maximum alignment of the Output_data in Output_segment.
1292 // Once we compute this, we prohibit new sections from being added.
1295 Output_segment::addralign()
1297 if (!this->is_align_known_
)
1301 addralign
= Output_segment::maximum_alignment(&this->output_data_
);
1302 if (addralign
> this->align_
)
1303 this->align_
= addralign
;
1305 addralign
= Output_segment::maximum_alignment(&this->output_bss_
);
1306 if (addralign
> this->align_
)
1307 this->align_
= addralign
;
1309 this->is_align_known_
= true;
1312 return this->align_
;
1315 // Return the maximum alignment of a list of Output_data.
1318 Output_segment::maximum_alignment(const Output_data_list
* pdl
)
1321 for (Output_data_list::const_iterator p
= pdl
->begin();
1325 uint64_t addralign
= (*p
)->addralign();
1326 if (addralign
> ret
)
1332 // Set the section addresses for an Output_segment. ADDR is the
1333 // address and *POFF is the file offset. Set the section indexes
1334 // starting with *PSHNDX. Return the address of the immediately
1335 // following segment. Update *POFF and *PSHNDX.
1338 Output_segment::set_section_addresses(uint64_t addr
, off_t
* poff
,
1339 unsigned int* pshndx
)
1341 gold_assert(this->type_
== elfcpp::PT_LOAD
);
1343 this->vaddr_
= addr
;
1344 this->paddr_
= addr
;
1346 off_t orig_off
= *poff
;
1347 this->offset_
= orig_off
;
1349 *poff
= align_address(*poff
, this->addralign());
1351 addr
= this->set_section_list_addresses(&this->output_data_
, addr
, poff
,
1353 this->filesz_
= *poff
- orig_off
;
1357 uint64_t ret
= this->set_section_list_addresses(&this->output_bss_
, addr
,
1359 this->memsz_
= *poff
- orig_off
;
1361 // Ignore the file offset adjustments made by the BSS Output_data
1368 // Set the addresses and file offsets in a list of Output_data
1372 Output_segment::set_section_list_addresses(Output_data_list
* pdl
,
1373 uint64_t addr
, off_t
* poff
,
1374 unsigned int* pshndx
)
1376 off_t startoff
= *poff
;
1378 off_t off
= startoff
;
1379 for (Output_data_list::iterator p
= pdl
->begin();
1383 off
= align_address(off
, (*p
)->addralign());
1384 (*p
)->set_address(addr
+ (off
- startoff
), off
);
1386 // Unless this is a PT_TLS segment, we want to ignore the size
1387 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1388 // affect the size of a PT_LOAD segment.
1389 if (this->type_
== elfcpp::PT_TLS
1390 || !(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
1391 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
1392 off
+= (*p
)->data_size();
1394 if ((*p
)->is_section())
1396 (*p
)->set_out_shndx(*pshndx
);
1402 return addr
+ (off
- startoff
);
1405 // For a non-PT_LOAD segment, set the offset from the sections, if
1409 Output_segment::set_offset()
1411 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
1413 if (this->output_data_
.empty() && this->output_bss_
.empty())
1424 const Output_data
* first
;
1425 if (this->output_data_
.empty())
1426 first
= this->output_bss_
.front();
1428 first
= this->output_data_
.front();
1429 this->vaddr_
= first
->address();
1430 this->paddr_
= this->vaddr_
;
1431 this->offset_
= first
->offset();
1433 if (this->output_data_
.empty())
1437 const Output_data
* last_data
= this->output_data_
.back();
1438 this->filesz_
= (last_data
->address()
1439 + last_data
->data_size()
1443 const Output_data
* last
;
1444 if (this->output_bss_
.empty())
1445 last
= this->output_data_
.back();
1447 last
= this->output_bss_
.back();
1448 this->memsz_
= (last
->address()
1453 // Return the number of Output_sections in an Output_segment.
1456 Output_segment::output_section_count() const
1458 return (this->output_section_count_list(&this->output_data_
)
1459 + this->output_section_count_list(&this->output_bss_
));
1462 // Return the number of Output_sections in an Output_data_list.
1465 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
1467 unsigned int count
= 0;
1468 for (Output_data_list::const_iterator p
= pdl
->begin();
1472 if ((*p
)->is_section())
1478 // Write the segment data into *OPHDR.
1480 template<int size
, bool big_endian
>
1482 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
1484 ophdr
->put_p_type(this->type_
);
1485 ophdr
->put_p_offset(this->offset_
);
1486 ophdr
->put_p_vaddr(this->vaddr_
);
1487 ophdr
->put_p_paddr(this->paddr_
);
1488 ophdr
->put_p_filesz(this->filesz_
);
1489 ophdr
->put_p_memsz(this->memsz_
);
1490 ophdr
->put_p_flags(this->flags_
);
1491 ophdr
->put_p_align(this->addralign());
1494 // Write the section headers into V.
1496 template<int size
, bool big_endian
>
1498 Output_segment::write_section_headers(const Layout
* layout
,
1499 const Stringpool
* secnamepool
,
1501 unsigned int *pshndx
1502 ACCEPT_SIZE_ENDIAN
) const
1504 // Every section that is attached to a segment must be attached to a
1505 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1507 if (this->type_
!= elfcpp::PT_LOAD
)
1510 v
= this->write_section_headers_list
1511 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1512 layout
, secnamepool
, &this->output_data_
, v
, pshndx
1513 SELECT_SIZE_ENDIAN(size
, big_endian
));
1514 v
= this->write_section_headers_list
1515 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1516 layout
, secnamepool
, &this->output_bss_
, v
, pshndx
1517 SELECT_SIZE_ENDIAN(size
, big_endian
));
1521 template<int size
, bool big_endian
>
1523 Output_segment::write_section_headers_list(const Layout
* layout
,
1524 const Stringpool
* secnamepool
,
1525 const Output_data_list
* pdl
,
1527 unsigned int* pshndx
1528 ACCEPT_SIZE_ENDIAN
) const
1530 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1531 for (Output_data_list::const_iterator p
= pdl
->begin();
1535 if ((*p
)->is_section())
1537 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
1538 gold_assert(*pshndx
== ps
->out_shndx());
1539 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
1540 ps
->write_header(layout
, secnamepool
, &oshdr
);
1548 // Output_file methods.
1550 Output_file::Output_file(const General_options
& options
, Target
* target
)
1551 : options_(options
),
1553 name_(options
.output_file_name()),
1560 // Open the output file.
1563 Output_file::open(off_t file_size
)
1565 this->file_size_
= file_size
;
1567 int mode
= parameters
->output_is_object() ? 0666 : 0777;
1568 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
1571 fprintf(stderr
, _("%s: %s: open: %s\n"),
1572 program_name
, this->name_
, strerror(errno
));
1577 // Write out one byte to make the file the right size.
1578 if (::lseek(o
, file_size
- 1, SEEK_SET
) < 0)
1580 fprintf(stderr
, _("%s: %s: lseek: %s\n"),
1581 program_name
, this->name_
, strerror(errno
));
1585 if (::write(o
, &b
, 1) != 1)
1587 fprintf(stderr
, _("%s: %s: write: %s\n"),
1588 program_name
, this->name_
, strerror(errno
));
1592 // Map the file into memory.
1593 void* base
= ::mmap(NULL
, file_size
, PROT_READ
| PROT_WRITE
,
1595 if (base
== MAP_FAILED
)
1597 fprintf(stderr
, _("%s: %s: mmap: %s\n"),
1598 program_name
, this->name_
, strerror(errno
));
1601 this->base_
= static_cast<unsigned char*>(base
);
1604 // Close the output file.
1607 Output_file::close()
1609 if (::munmap(this->base_
, this->file_size_
) < 0)
1611 fprintf(stderr
, _("%s: %s: munmap: %s\n"),
1612 program_name
, this->name_
, strerror(errno
));
1617 if (::close(this->o_
) < 0)
1619 fprintf(stderr
, _("%s: %s: close: %s\n"),
1620 program_name
, this->name_
, strerror(errno
));
1626 // Instantiate the templates we need. We could use the configure
1627 // script to restrict this to only the ones for implemented targets.
1629 #ifdef HAVE_TARGET_32_LITTLE
1632 Output_section::add_input_section
<32, false>(
1635 const char* secname
,
1636 const elfcpp::Shdr
<32, false>& shdr
);
1639 #ifdef HAVE_TARGET_32_BIG
1642 Output_section::add_input_section
<32, true>(
1645 const char* secname
,
1646 const elfcpp::Shdr
<32, true>& shdr
);
1649 #ifdef HAVE_TARGET_64_LITTLE
1652 Output_section::add_input_section
<64, false>(
1655 const char* secname
,
1656 const elfcpp::Shdr
<64, false>& shdr
);
1659 #ifdef HAVE_TARGET_64_BIG
1662 Output_section::add_input_section
<64, true>(
1665 const char* secname
,
1666 const elfcpp::Shdr
<64, true>& shdr
);
1669 #ifdef HAVE_TARGET_32_LITTLE
1671 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
1674 #ifdef HAVE_TARGET_32_BIG
1676 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
1679 #ifdef HAVE_TARGET_64_LITTLE
1681 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
1684 #ifdef HAVE_TARGET_64_BIG
1686 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
1689 #ifdef HAVE_TARGET_32_LITTLE
1691 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
1694 #ifdef HAVE_TARGET_32_BIG
1696 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
1699 #ifdef HAVE_TARGET_64_LITTLE
1701 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
1704 #ifdef HAVE_TARGET_64_BIG
1706 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
1709 #ifdef HAVE_TARGET_32_LITTLE
1711 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
1714 #ifdef HAVE_TARGET_32_BIG
1716 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
1719 #ifdef HAVE_TARGET_64_LITTLE
1721 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
1724 #ifdef HAVE_TARGET_64_BIG
1726 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
1729 #ifdef HAVE_TARGET_32_LITTLE
1731 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
1734 #ifdef HAVE_TARGET_32_BIG
1736 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
1739 #ifdef HAVE_TARGET_64_LITTLE
1741 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
1744 #ifdef HAVE_TARGET_64_BIG
1746 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
1749 #ifdef HAVE_TARGET_32_LITTLE
1751 class Output_data_got
<32, false>;
1754 #ifdef HAVE_TARGET_32_BIG
1756 class Output_data_got
<32, true>;
1759 #ifdef HAVE_TARGET_64_LITTLE
1761 class Output_data_got
<64, false>;
1764 #ifdef HAVE_TARGET_64_BIG
1766 class Output_data_got
<64, true>;
1769 } // End namespace gold.