1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
33 #ifdef HAVE_SYS_MMAN_H
37 #include "libiberty.h"
39 #include "parameters.h"
44 #include "descriptors.h"
48 // For systems without mmap support.
50 # define mmap gold_mmap
51 # define munmap gold_munmap
52 # define mremap gold_mremap
54 # define MAP_FAILED (reinterpret_cast<void*>(-1))
63 # define MAP_PRIVATE 0
65 # ifndef MAP_ANONYMOUS
66 # define MAP_ANONYMOUS 0
73 # define ENOSYS EINVAL
77 gold_mmap(void *, size_t, int, int, int, off_t
)
84 gold_munmap(void *, size_t)
91 gold_mremap(void *, size_t, size_t, int)
99 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
100 # define mremap gold_mremap
101 extern "C" void *gold_mremap(void *, size_t, size_t, int);
104 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
105 #ifndef MAP_ANONYMOUS
106 # define MAP_ANONYMOUS MAP_ANON
109 #ifndef MREMAP_MAYMOVE
110 # define MREMAP_MAYMOVE 1
113 #ifndef HAVE_POSIX_FALLOCATE
114 // A dummy, non general, version of posix_fallocate. Here we just set
115 // the file size and hope that there is enough disk space. FIXME: We
116 // could allocate disk space by walking block by block and writing a
117 // zero byte into each block.
119 posix_fallocate(int o
, off_t offset
, off_t len
)
121 return ftruncate(o
, offset
+ len
);
123 #endif // !defined(HAVE_POSIX_FALLOCATE)
125 // Mingw does not have S_ISLNK.
127 # define S_ISLNK(mode) 0
133 // Output_data variables.
135 bool Output_data::allocated_sizes_are_fixed
;
137 // Output_data methods.
139 Output_data::~Output_data()
143 // Return the default alignment for the target size.
146 Output_data::default_alignment()
148 return Output_data::default_alignment_for_size(
149 parameters
->target().get_size());
152 // Return the default alignment for a size--32 or 64.
155 Output_data::default_alignment_for_size(int size
)
165 // Output_section_header methods. This currently assumes that the
166 // segment and section lists are complete at construction time.
168 Output_section_headers::Output_section_headers(
169 const Layout
* layout
,
170 const Layout::Segment_list
* segment_list
,
171 const Layout::Section_list
* section_list
,
172 const Layout::Section_list
* unattached_section_list
,
173 const Stringpool
* secnamepool
,
174 const Output_section
* shstrtab_section
)
176 segment_list_(segment_list
),
177 section_list_(section_list
),
178 unattached_section_list_(unattached_section_list
),
179 secnamepool_(secnamepool
),
180 shstrtab_section_(shstrtab_section
)
184 // Compute the current data size.
187 Output_section_headers::do_size() const
189 // Count all the sections. Start with 1 for the null section.
191 if (!parameters
->options().relocatable())
193 for (Layout::Segment_list::const_iterator p
=
194 this->segment_list_
->begin();
195 p
!= this->segment_list_
->end();
197 if ((*p
)->type() == elfcpp::PT_LOAD
)
198 count
+= (*p
)->output_section_count();
202 for (Layout::Section_list::const_iterator p
=
203 this->section_list_
->begin();
204 p
!= this->section_list_
->end();
206 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
209 count
+= this->unattached_section_list_
->size();
211 const int size
= parameters
->target().get_size();
214 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
216 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
220 return count
* shdr_size
;
223 // Write out the section headers.
226 Output_section_headers::do_write(Output_file
* of
)
228 switch (parameters
->size_and_endianness())
230 #ifdef HAVE_TARGET_32_LITTLE
231 case Parameters::TARGET_32_LITTLE
:
232 this->do_sized_write
<32, false>(of
);
235 #ifdef HAVE_TARGET_32_BIG
236 case Parameters::TARGET_32_BIG
:
237 this->do_sized_write
<32, true>(of
);
240 #ifdef HAVE_TARGET_64_LITTLE
241 case Parameters::TARGET_64_LITTLE
:
242 this->do_sized_write
<64, false>(of
);
245 #ifdef HAVE_TARGET_64_BIG
246 case Parameters::TARGET_64_BIG
:
247 this->do_sized_write
<64, true>(of
);
255 template<int size
, bool big_endian
>
257 Output_section_headers::do_sized_write(Output_file
* of
)
259 off_t all_shdrs_size
= this->data_size();
260 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
262 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
263 unsigned char* v
= view
;
266 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
267 oshdr
.put_sh_name(0);
268 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
269 oshdr
.put_sh_flags(0);
270 oshdr
.put_sh_addr(0);
271 oshdr
.put_sh_offset(0);
273 size_t section_count
= (this->data_size()
274 / elfcpp::Elf_sizes
<size
>::shdr_size
);
275 if (section_count
< elfcpp::SHN_LORESERVE
)
276 oshdr
.put_sh_size(0);
278 oshdr
.put_sh_size(section_count
);
280 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
281 if (shstrndx
< elfcpp::SHN_LORESERVE
)
282 oshdr
.put_sh_link(0);
284 oshdr
.put_sh_link(shstrndx
);
286 size_t segment_count
= this->segment_list_
->size();
287 oshdr
.put_sh_info(segment_count
>= elfcpp::PN_XNUM
? segment_count
: 0);
289 oshdr
.put_sh_addralign(0);
290 oshdr
.put_sh_entsize(0);
295 unsigned int shndx
= 1;
296 if (!parameters
->options().relocatable())
298 for (Layout::Segment_list::const_iterator p
=
299 this->segment_list_
->begin();
300 p
!= this->segment_list_
->end();
302 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
309 for (Layout::Section_list::const_iterator p
=
310 this->section_list_
->begin();
311 p
!= this->section_list_
->end();
314 // We do unallocated sections below, except that group
315 // sections have to come first.
316 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
317 && (*p
)->type() != elfcpp::SHT_GROUP
)
319 gold_assert(shndx
== (*p
)->out_shndx());
320 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
321 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
327 for (Layout::Section_list::const_iterator p
=
328 this->unattached_section_list_
->begin();
329 p
!= this->unattached_section_list_
->end();
332 // For a relocatable link, we did unallocated group sections
333 // above, since they have to come first.
334 if ((*p
)->type() == elfcpp::SHT_GROUP
335 && parameters
->options().relocatable())
337 gold_assert(shndx
== (*p
)->out_shndx());
338 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
339 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
344 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
347 // Output_segment_header methods.
349 Output_segment_headers::Output_segment_headers(
350 const Layout::Segment_list
& segment_list
)
351 : segment_list_(segment_list
)
353 this->set_current_data_size_for_child(this->do_size());
357 Output_segment_headers::do_write(Output_file
* of
)
359 switch (parameters
->size_and_endianness())
361 #ifdef HAVE_TARGET_32_LITTLE
362 case Parameters::TARGET_32_LITTLE
:
363 this->do_sized_write
<32, false>(of
);
366 #ifdef HAVE_TARGET_32_BIG
367 case Parameters::TARGET_32_BIG
:
368 this->do_sized_write
<32, true>(of
);
371 #ifdef HAVE_TARGET_64_LITTLE
372 case Parameters::TARGET_64_LITTLE
:
373 this->do_sized_write
<64, false>(of
);
376 #ifdef HAVE_TARGET_64_BIG
377 case Parameters::TARGET_64_BIG
:
378 this->do_sized_write
<64, true>(of
);
386 template<int size
, bool big_endian
>
388 Output_segment_headers::do_sized_write(Output_file
* of
)
390 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
391 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
392 gold_assert(all_phdrs_size
== this->data_size());
393 unsigned char* view
= of
->get_output_view(this->offset(),
395 unsigned char* v
= view
;
396 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
397 p
!= this->segment_list_
.end();
400 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
401 (*p
)->write_header(&ophdr
);
405 gold_assert(v
- view
== all_phdrs_size
);
407 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
411 Output_segment_headers::do_size() const
413 const int size
= parameters
->target().get_size();
416 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
418 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
422 return this->segment_list_
.size() * phdr_size
;
425 // Output_file_header methods.
427 Output_file_header::Output_file_header(const Target
* target
,
428 const Symbol_table
* symtab
,
429 const Output_segment_headers
* osh
)
432 segment_header_(osh
),
433 section_header_(NULL
),
436 this->set_data_size(this->do_size());
439 // Set the section table information for a file header.
442 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
443 const Output_section
* shstrtab
)
445 this->section_header_
= shdrs
;
446 this->shstrtab_
= shstrtab
;
449 // Write out the file header.
452 Output_file_header::do_write(Output_file
* of
)
454 gold_assert(this->offset() == 0);
456 switch (parameters
->size_and_endianness())
458 #ifdef HAVE_TARGET_32_LITTLE
459 case Parameters::TARGET_32_LITTLE
:
460 this->do_sized_write
<32, false>(of
);
463 #ifdef HAVE_TARGET_32_BIG
464 case Parameters::TARGET_32_BIG
:
465 this->do_sized_write
<32, true>(of
);
468 #ifdef HAVE_TARGET_64_LITTLE
469 case Parameters::TARGET_64_LITTLE
:
470 this->do_sized_write
<64, false>(of
);
473 #ifdef HAVE_TARGET_64_BIG
474 case Parameters::TARGET_64_BIG
:
475 this->do_sized_write
<64, true>(of
);
483 // Write out the file header with appropriate size and endianness.
485 template<int size
, bool big_endian
>
487 Output_file_header::do_sized_write(Output_file
* of
)
489 gold_assert(this->offset() == 0);
491 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
492 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
493 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
495 unsigned char e_ident
[elfcpp::EI_NIDENT
];
496 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
497 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
498 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
499 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
500 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
502 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
504 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
507 e_ident
[elfcpp::EI_DATA
] = (big_endian
508 ? elfcpp::ELFDATA2MSB
509 : elfcpp::ELFDATA2LSB
);
510 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
511 oehdr
.put_e_ident(e_ident
);
514 if (parameters
->options().relocatable())
515 e_type
= elfcpp::ET_REL
;
516 else if (parameters
->options().output_is_position_independent())
517 e_type
= elfcpp::ET_DYN
;
519 e_type
= elfcpp::ET_EXEC
;
520 oehdr
.put_e_type(e_type
);
522 oehdr
.put_e_machine(this->target_
->machine_code());
523 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
525 oehdr
.put_e_entry(this->entry
<size
>());
527 if (this->segment_header_
== NULL
)
528 oehdr
.put_e_phoff(0);
530 oehdr
.put_e_phoff(this->segment_header_
->offset());
532 oehdr
.put_e_shoff(this->section_header_
->offset());
533 oehdr
.put_e_flags(this->target_
->processor_specific_flags());
534 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
536 if (this->segment_header_
== NULL
)
538 oehdr
.put_e_phentsize(0);
539 oehdr
.put_e_phnum(0);
543 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
544 size_t phnum
= (this->segment_header_
->data_size()
545 / elfcpp::Elf_sizes
<size
>::phdr_size
);
546 if (phnum
> elfcpp::PN_XNUM
)
547 phnum
= elfcpp::PN_XNUM
;
548 oehdr
.put_e_phnum(phnum
);
551 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
552 size_t section_count
= (this->section_header_
->data_size()
553 / elfcpp::Elf_sizes
<size
>::shdr_size
);
555 if (section_count
< elfcpp::SHN_LORESERVE
)
556 oehdr
.put_e_shnum(this->section_header_
->data_size()
557 / elfcpp::Elf_sizes
<size
>::shdr_size
);
559 oehdr
.put_e_shnum(0);
561 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
562 if (shstrndx
< elfcpp::SHN_LORESERVE
)
563 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
565 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
567 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
568 // the e_ident field.
569 parameters
->target().adjust_elf_header(view
, ehdr_size
);
571 of
->write_output_view(0, ehdr_size
, view
);
574 // Return the value to use for the entry address.
577 typename
elfcpp::Elf_types
<size
>::Elf_Addr
578 Output_file_header::entry()
580 const bool should_issue_warning
= (parameters
->options().entry() != NULL
581 && !parameters
->options().relocatable()
582 && !parameters
->options().shared());
583 const char* entry
= parameters
->entry();
584 Symbol
* sym
= this->symtab_
->lookup(entry
);
586 typename Sized_symbol
<size
>::Value_type v
;
589 Sized_symbol
<size
>* ssym
;
590 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
591 if (!ssym
->is_defined() && should_issue_warning
)
592 gold_warning("entry symbol '%s' exists but is not defined", entry
);
597 // We couldn't find the entry symbol. See if we can parse it as
598 // a number. This supports, e.g., -e 0x1000.
600 v
= strtoull(entry
, &endptr
, 0);
603 if (should_issue_warning
)
604 gold_warning("cannot find entry symbol '%s'", entry
);
612 // Compute the current data size.
615 Output_file_header::do_size() const
617 const int size
= parameters
->target().get_size();
619 return elfcpp::Elf_sizes
<32>::ehdr_size
;
621 return elfcpp::Elf_sizes
<64>::ehdr_size
;
626 // Output_data_const methods.
629 Output_data_const::do_write(Output_file
* of
)
631 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
634 // Output_data_const_buffer methods.
637 Output_data_const_buffer::do_write(Output_file
* of
)
639 of
->write(this->offset(), this->p_
, this->data_size());
642 // Output_section_data methods.
644 // Record the output section, and set the entry size and such.
647 Output_section_data::set_output_section(Output_section
* os
)
649 gold_assert(this->output_section_
== NULL
);
650 this->output_section_
= os
;
651 this->do_adjust_output_section(os
);
654 // Return the section index of the output section.
657 Output_section_data::do_out_shndx() const
659 gold_assert(this->output_section_
!= NULL
);
660 return this->output_section_
->out_shndx();
663 // Set the alignment, which means we may need to update the alignment
664 // of the output section.
667 Output_section_data::set_addralign(uint64_t addralign
)
669 this->addralign_
= addralign
;
670 if (this->output_section_
!= NULL
671 && this->output_section_
->addralign() < addralign
)
672 this->output_section_
->set_addralign(addralign
);
675 // Output_data_strtab methods.
677 // Set the final data size.
680 Output_data_strtab::set_final_data_size()
682 this->strtab_
->set_string_offsets();
683 this->set_data_size(this->strtab_
->get_strtab_size());
686 // Write out a string table.
689 Output_data_strtab::do_write(Output_file
* of
)
691 this->strtab_
->write(of
, this->offset());
694 // Output_reloc methods.
696 // A reloc against a global symbol.
698 template<bool dynamic
, int size
, bool big_endian
>
699 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
706 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
707 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
708 is_section_symbol_(false), shndx_(INVALID_CODE
)
710 // this->type_ is a bitfield; make sure TYPE fits.
711 gold_assert(this->type_
== type
);
712 this->u1_
.gsym
= gsym
;
715 this->set_needs_dynsym_index();
718 template<bool dynamic
, int size
, bool big_endian
>
719 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
722 Sized_relobj
<size
, big_endian
>* relobj
,
727 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
728 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
729 is_section_symbol_(false), shndx_(shndx
)
731 gold_assert(shndx
!= INVALID_CODE
);
732 // this->type_ is a bitfield; make sure TYPE fits.
733 gold_assert(this->type_
== type
);
734 this->u1_
.gsym
= gsym
;
735 this->u2_
.relobj
= relobj
;
737 this->set_needs_dynsym_index();
740 // A reloc against a local symbol.
742 template<bool dynamic
, int size
, bool big_endian
>
743 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
744 Sized_relobj
<size
, big_endian
>* relobj
,
745 unsigned int local_sym_index
,
751 bool is_section_symbol
)
752 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
753 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
754 is_section_symbol_(is_section_symbol
), shndx_(INVALID_CODE
)
756 gold_assert(local_sym_index
!= GSYM_CODE
757 && local_sym_index
!= INVALID_CODE
);
758 // this->type_ is a bitfield; make sure TYPE fits.
759 gold_assert(this->type_
== type
);
760 this->u1_
.relobj
= relobj
;
763 this->set_needs_dynsym_index();
766 template<bool dynamic
, int size
, bool big_endian
>
767 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
768 Sized_relobj
<size
, big_endian
>* relobj
,
769 unsigned int local_sym_index
,
775 bool is_section_symbol
)
776 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
777 is_relative_(is_relative
), is_symbolless_(is_symbolless
),
778 is_section_symbol_(is_section_symbol
), shndx_(shndx
)
780 gold_assert(local_sym_index
!= GSYM_CODE
781 && local_sym_index
!= INVALID_CODE
);
782 gold_assert(shndx
!= INVALID_CODE
);
783 // this->type_ is a bitfield; make sure TYPE fits.
784 gold_assert(this->type_
== type
);
785 this->u1_
.relobj
= relobj
;
786 this->u2_
.relobj
= relobj
;
788 this->set_needs_dynsym_index();
791 // A reloc against the STT_SECTION symbol of an output section.
793 template<bool dynamic
, int size
, bool big_endian
>
794 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
799 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
800 is_relative_(false), is_symbolless_(false),
801 is_section_symbol_(true), shndx_(INVALID_CODE
)
803 // this->type_ is a bitfield; make sure TYPE fits.
804 gold_assert(this->type_
== type
);
808 this->set_needs_dynsym_index();
810 os
->set_needs_symtab_index();
813 template<bool dynamic
, int size
, bool big_endian
>
814 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
817 Sized_relobj
<size
, big_endian
>* relobj
,
820 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
821 is_relative_(false), is_symbolless_(false),
822 is_section_symbol_(true), shndx_(shndx
)
824 gold_assert(shndx
!= INVALID_CODE
);
825 // this->type_ is a bitfield; make sure TYPE fits.
826 gold_assert(this->type_
== type
);
828 this->u2_
.relobj
= relobj
;
830 this->set_needs_dynsym_index();
832 os
->set_needs_symtab_index();
835 // An absolute relocation.
837 template<bool dynamic
, int size
, bool big_endian
>
838 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
842 : address_(address
), local_sym_index_(0), type_(type
),
843 is_relative_(false), is_symbolless_(false),
844 is_section_symbol_(false), shndx_(INVALID_CODE
)
846 // this->type_ is a bitfield; make sure TYPE fits.
847 gold_assert(this->type_
== type
);
848 this->u1_
.relobj
= NULL
;
852 template<bool dynamic
, int size
, bool big_endian
>
853 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
855 Sized_relobj
<size
, big_endian
>* relobj
,
858 : address_(address
), local_sym_index_(0), type_(type
),
859 is_relative_(false), is_symbolless_(false),
860 is_section_symbol_(false), shndx_(shndx
)
862 gold_assert(shndx
!= INVALID_CODE
);
863 // this->type_ is a bitfield; make sure TYPE fits.
864 gold_assert(this->type_
== type
);
865 this->u1_
.relobj
= NULL
;
866 this->u2_
.relobj
= relobj
;
869 // A target specific relocation.
871 template<bool dynamic
, int size
, bool big_endian
>
872 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
877 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
878 is_relative_(false), is_symbolless_(false),
879 is_section_symbol_(false), shndx_(INVALID_CODE
)
881 // this->type_ is a bitfield; make sure TYPE fits.
882 gold_assert(this->type_
== type
);
887 template<bool dynamic
, int size
, bool big_endian
>
888 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
891 Sized_relobj
<size
, big_endian
>* relobj
,
894 : address_(address
), local_sym_index_(TARGET_CODE
), type_(type
),
895 is_relative_(false), is_symbolless_(false),
896 is_section_symbol_(false), shndx_(shndx
)
898 gold_assert(shndx
!= INVALID_CODE
);
899 // this->type_ is a bitfield; make sure TYPE fits.
900 gold_assert(this->type_
== type
);
902 this->u2_
.relobj
= relobj
;
905 // Record that we need a dynamic symbol index for this relocation.
907 template<bool dynamic
, int size
, bool big_endian
>
909 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
910 set_needs_dynsym_index()
912 if (this->is_symbolless_
)
914 switch (this->local_sym_index_
)
920 this->u1_
.gsym
->set_needs_dynsym_entry();
924 this->u1_
.os
->set_needs_dynsym_index();
928 // The target must take care of this if necessary.
936 const unsigned int lsi
= this->local_sym_index_
;
937 Sized_relobj_file
<size
, big_endian
>* relobj
=
938 this->u1_
.relobj
->sized_relobj();
939 gold_assert(relobj
!= NULL
);
940 if (!this->is_section_symbol_
)
941 relobj
->set_needs_output_dynsym_entry(lsi
);
943 relobj
->output_section(lsi
)->set_needs_dynsym_index();
949 // Get the symbol index of a relocation.
951 template<bool dynamic
, int size
, bool big_endian
>
953 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
957 if (this->is_symbolless_
)
959 switch (this->local_sym_index_
)
965 if (this->u1_
.gsym
== NULL
)
968 index
= this->u1_
.gsym
->dynsym_index();
970 index
= this->u1_
.gsym
->symtab_index();
975 index
= this->u1_
.os
->dynsym_index();
977 index
= this->u1_
.os
->symtab_index();
981 index
= parameters
->target().reloc_symbol_index(this->u1_
.arg
,
986 // Relocations without symbols use a symbol index of 0.
992 const unsigned int lsi
= this->local_sym_index_
;
993 Sized_relobj_file
<size
, big_endian
>* relobj
=
994 this->u1_
.relobj
->sized_relobj();
995 gold_assert(relobj
!= NULL
);
996 if (!this->is_section_symbol_
)
999 index
= relobj
->dynsym_index(lsi
);
1001 index
= relobj
->symtab_index(lsi
);
1005 Output_section
* os
= relobj
->output_section(lsi
);
1006 gold_assert(os
!= NULL
);
1008 index
= os
->dynsym_index();
1010 index
= os
->symtab_index();
1015 gold_assert(index
!= -1U);
1019 // For a local section symbol, get the address of the offset ADDEND
1020 // within the input section.
1022 template<bool dynamic
, int size
, bool big_endian
>
1023 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1024 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1025 local_section_offset(Addend addend
) const
1027 gold_assert(this->local_sym_index_
!= GSYM_CODE
1028 && this->local_sym_index_
!= SECTION_CODE
1029 && this->local_sym_index_
!= TARGET_CODE
1030 && this->local_sym_index_
!= INVALID_CODE
1031 && this->local_sym_index_
!= 0
1032 && this->is_section_symbol_
);
1033 const unsigned int lsi
= this->local_sym_index_
;
1034 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
1035 gold_assert(os
!= NULL
);
1036 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
1037 if (offset
!= invalid_address
)
1038 return offset
+ addend
;
1039 // This is a merge section.
1040 Sized_relobj_file
<size
, big_endian
>* relobj
=
1041 this->u1_
.relobj
->sized_relobj();
1042 gold_assert(relobj
!= NULL
);
1043 offset
= os
->output_address(relobj
, lsi
, addend
);
1044 gold_assert(offset
!= invalid_address
);
1048 // Get the output address of a relocation.
1050 template<bool dynamic
, int size
, bool big_endian
>
1051 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1052 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
1054 Address address
= this->address_
;
1055 if (this->shndx_
!= INVALID_CODE
)
1057 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
1058 gold_assert(os
!= NULL
);
1059 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
1060 if (off
!= invalid_address
)
1061 address
+= os
->address() + off
;
1064 Sized_relobj_file
<size
, big_endian
>* relobj
=
1065 this->u2_
.relobj
->sized_relobj();
1066 gold_assert(relobj
!= NULL
);
1067 address
= os
->output_address(relobj
, this->shndx_
, address
);
1068 gold_assert(address
!= invalid_address
);
1071 else if (this->u2_
.od
!= NULL
)
1072 address
+= this->u2_
.od
->address();
1076 // Write out the offset and info fields of a Rel or Rela relocation
1079 template<bool dynamic
, int size
, bool big_endian
>
1080 template<typename Write_rel
>
1082 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
1083 Write_rel
* wr
) const
1085 wr
->put_r_offset(this->get_address());
1086 unsigned int sym_index
= this->get_symbol_index();
1087 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
1090 // Write out a Rel relocation.
1092 template<bool dynamic
, int size
, bool big_endian
>
1094 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
1095 unsigned char* pov
) const
1097 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
1098 this->write_rel(&orel
);
1101 // Get the value of the symbol referred to by a Rel relocation.
1103 template<bool dynamic
, int size
, bool big_endian
>
1104 typename
elfcpp::Elf_types
<size
>::Elf_Addr
1105 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
1106 Addend addend
) const
1108 if (this->local_sym_index_
== GSYM_CODE
)
1110 const Sized_symbol
<size
>* sym
;
1111 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
1112 return sym
->value() + addend
;
1114 gold_assert(this->local_sym_index_
!= SECTION_CODE
1115 && this->local_sym_index_
!= TARGET_CODE
1116 && this->local_sym_index_
!= INVALID_CODE
1117 && this->local_sym_index_
!= 0
1118 && !this->is_section_symbol_
);
1119 const unsigned int lsi
= this->local_sym_index_
;
1120 Sized_relobj_file
<size
, big_endian
>* relobj
=
1121 this->u1_
.relobj
->sized_relobj();
1122 gold_assert(relobj
!= NULL
);
1123 const Symbol_value
<size
>* symval
= relobj
->local_symbol(lsi
);
1124 return symval
->value(relobj
, addend
);
1127 // Reloc comparison. This function sorts the dynamic relocs for the
1128 // benefit of the dynamic linker. First we sort all relative relocs
1129 // to the front. Among relative relocs, we sort by output address.
1130 // Among non-relative relocs, we sort by symbol index, then by output
1133 template<bool dynamic
, int size
, bool big_endian
>
1135 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
1136 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1139 if (this->is_relative_
)
1141 if (!r2
.is_relative_
)
1143 // Otherwise sort by reloc address below.
1145 else if (r2
.is_relative_
)
1149 unsigned int sym1
= this->get_symbol_index();
1150 unsigned int sym2
= r2
.get_symbol_index();
1153 else if (sym1
> sym2
)
1155 // Otherwise sort by reloc address.
1158 section_offset_type addr1
= this->get_address();
1159 section_offset_type addr2
= r2
.get_address();
1162 else if (addr1
> addr2
)
1165 // Final tie breaker, in order to generate the same output on any
1166 // host: reloc type.
1167 unsigned int type1
= this->type_
;
1168 unsigned int type2
= r2
.type_
;
1171 else if (type1
> type2
)
1174 // These relocs appear to be exactly the same.
1178 // Write out a Rela relocation.
1180 template<bool dynamic
, int size
, bool big_endian
>
1182 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
1183 unsigned char* pov
) const
1185 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
1186 this->rel_
.write_rel(&orel
);
1187 Addend addend
= this->addend_
;
1188 if (this->rel_
.is_target_specific())
1189 addend
= parameters
->target().reloc_addend(this->rel_
.target_arg(),
1190 this->rel_
.type(), addend
);
1191 else if (this->rel_
.is_symbolless())
1192 addend
= this->rel_
.symbol_value(addend
);
1193 else if (this->rel_
.is_local_section_symbol())
1194 addend
= this->rel_
.local_section_offset(addend
);
1195 orel
.put_r_addend(addend
);
1198 // Output_data_reloc_base methods.
1200 // Adjust the output section.
1202 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1204 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
1205 ::do_adjust_output_section(Output_section
* os
)
1207 if (sh_type
== elfcpp::SHT_REL
)
1208 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1209 else if (sh_type
== elfcpp::SHT_RELA
)
1210 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1214 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1215 // static link. The backends will generate a dynamic reloc section
1216 // to hold this. In that case we don't want to link to the dynsym
1217 // section, because there isn't one.
1219 os
->set_should_link_to_symtab();
1220 else if (parameters
->doing_static_link())
1223 os
->set_should_link_to_dynsym();
1226 // Write out relocation data.
1228 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1230 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1233 const off_t off
= this->offset();
1234 const off_t oview_size
= this->data_size();
1235 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1237 if (this->sort_relocs())
1239 gold_assert(dynamic
);
1240 std::sort(this->relocs_
.begin(), this->relocs_
.end(),
1241 Sort_relocs_comparison());
1244 unsigned char* pov
= oview
;
1245 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
1246 p
!= this->relocs_
.end();
1253 gold_assert(pov
- oview
== oview_size
);
1255 of
->write_output_view(off
, oview_size
, oview
);
1257 // We no longer need the relocation entries.
1258 this->relocs_
.clear();
1261 // Class Output_relocatable_relocs.
1263 template<int sh_type
, int size
, bool big_endian
>
1265 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1267 this->set_data_size(this->rr_
->output_reloc_count()
1268 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1271 // class Output_data_group.
1273 template<int size
, bool big_endian
>
1274 Output_data_group
<size
, big_endian
>::Output_data_group(
1275 Sized_relobj_file
<size
, big_endian
>* relobj
,
1276 section_size_type entry_count
,
1277 elfcpp::Elf_Word flags
,
1278 std::vector
<unsigned int>* input_shndxes
)
1279 : Output_section_data(entry_count
* 4, 4, false),
1283 this->input_shndxes_
.swap(*input_shndxes
);
1286 // Write out the section group, which means translating the section
1287 // indexes to apply to the output file.
1289 template<int size
, bool big_endian
>
1291 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1293 const off_t off
= this->offset();
1294 const section_size_type oview_size
=
1295 convert_to_section_size_type(this->data_size());
1296 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1298 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1299 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1302 for (std::vector
<unsigned int>::const_iterator p
=
1303 this->input_shndxes_
.begin();
1304 p
!= this->input_shndxes_
.end();
1307 Output_section
* os
= this->relobj_
->output_section(*p
);
1309 unsigned int output_shndx
;
1311 output_shndx
= os
->out_shndx();
1314 this->relobj_
->error(_("section group retained but "
1315 "group element discarded"));
1319 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1322 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1323 gold_assert(wrote
== oview_size
);
1325 of
->write_output_view(off
, oview_size
, oview
);
1327 // We no longer need this information.
1328 this->input_shndxes_
.clear();
1331 // Output_data_got::Got_entry methods.
1333 // Write out the entry.
1335 template<int size
, bool big_endian
>
1337 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1341 switch (this->local_sym_index_
)
1345 // If the symbol is resolved locally, we need to write out the
1346 // link-time value, which will be relocated dynamically by a
1347 // RELATIVE relocation.
1348 Symbol
* gsym
= this->u_
.gsym
;
1349 if (this->use_plt_offset_
&& gsym
->has_plt_offset())
1350 val
= (parameters
->target().plt_section_for_global(gsym
)->address()
1351 + gsym
->plt_offset());
1354 Sized_symbol
<size
>* sgsym
;
1355 // This cast is a bit ugly. We don't want to put a
1356 // virtual method in Symbol, because we want Symbol to be
1357 // as small as possible.
1358 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1359 val
= sgsym
->value();
1365 val
= this->u_
.constant
;
1369 // If we're doing an incremental update, don't touch this GOT entry.
1370 if (parameters
->incremental_update())
1372 val
= this->u_
.constant
;
1377 const Sized_relobj_file
<size
, big_endian
>* object
= this->u_
.object
;
1378 const unsigned int lsi
= this->local_sym_index_
;
1379 const Symbol_value
<size
>* symval
= object
->local_symbol(lsi
);
1380 if (!this->use_plt_offset_
)
1381 val
= symval
->value(this->u_
.object
, 0);
1384 const Output_data
* plt
=
1385 parameters
->target().plt_section_for_local(object
, lsi
);
1386 val
= plt
->address() + object
->local_plt_offset(lsi
);
1392 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1395 // Output_data_got methods.
1397 // Add an entry for a global symbol to the GOT. This returns true if
1398 // this is a new GOT entry, false if the symbol already had a GOT
1401 template<int size
, bool big_endian
>
1403 Output_data_got
<size
, big_endian
>::add_global(
1405 unsigned int got_type
)
1407 if (gsym
->has_got_offset(got_type
))
1410 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, false));
1411 gsym
->set_got_offset(got_type
, got_offset
);
1415 // Like add_global, but use the PLT offset.
1417 template<int size
, bool big_endian
>
1419 Output_data_got
<size
, big_endian
>::add_global_plt(Symbol
* gsym
,
1420 unsigned int got_type
)
1422 if (gsym
->has_got_offset(got_type
))
1425 unsigned int got_offset
= this->add_got_entry(Got_entry(gsym
, true));
1426 gsym
->set_got_offset(got_type
, got_offset
);
1430 // Add an entry for a global symbol to the GOT, and add a dynamic
1431 // relocation of type R_TYPE for the GOT entry.
1433 template<int size
, bool big_endian
>
1435 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1437 unsigned int got_type
,
1439 unsigned int r_type
)
1441 if (gsym
->has_got_offset(got_type
))
1444 unsigned int got_offset
= this->add_got_entry(Got_entry());
1445 gsym
->set_got_offset(got_type
, got_offset
);
1446 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1449 template<int size
, bool big_endian
>
1451 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1453 unsigned int got_type
,
1455 unsigned int r_type
)
1457 if (gsym
->has_got_offset(got_type
))
1460 unsigned int got_offset
= this->add_got_entry(Got_entry());
1461 gsym
->set_got_offset(got_type
, got_offset
);
1462 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1465 // Add a pair of entries for a global symbol to the GOT, and add
1466 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1467 // If R_TYPE_2 == 0, add the second entry with no relocation.
1468 template<int size
, bool big_endian
>
1470 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1472 unsigned int got_type
,
1474 unsigned int r_type_1
,
1475 unsigned int r_type_2
)
1477 if (gsym
->has_got_offset(got_type
))
1480 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1481 gsym
->set_got_offset(got_type
, got_offset
);
1482 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1485 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
+ size
/ 8);
1488 template<int size
, bool big_endian
>
1490 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1492 unsigned int got_type
,
1494 unsigned int r_type_1
,
1495 unsigned int r_type_2
)
1497 if (gsym
->has_got_offset(got_type
))
1500 unsigned int got_offset
= this->add_got_entry_pair(Got_entry(), Got_entry());
1501 gsym
->set_got_offset(got_type
, got_offset
);
1502 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1505 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
+ size
/ 8, 0);
1508 // Add an entry for a local symbol to the GOT. This returns true if
1509 // this is a new GOT entry, false if the symbol already has a GOT
1512 template<int size
, bool big_endian
>
1514 Output_data_got
<size
, big_endian
>::add_local(
1515 Sized_relobj_file
<size
, big_endian
>* object
,
1516 unsigned int symndx
,
1517 unsigned int got_type
)
1519 if (object
->local_has_got_offset(symndx
, got_type
))
1522 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1524 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1528 // Like add_local, but use the PLT offset.
1530 template<int size
, bool big_endian
>
1532 Output_data_got
<size
, big_endian
>::add_local_plt(
1533 Sized_relobj_file
<size
, big_endian
>* object
,
1534 unsigned int symndx
,
1535 unsigned int got_type
)
1537 if (object
->local_has_got_offset(symndx
, got_type
))
1540 unsigned int got_offset
= this->add_got_entry(Got_entry(object
, symndx
,
1542 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1546 // Add an entry for a local symbol to the GOT, and add a dynamic
1547 // relocation of type R_TYPE for the GOT entry.
1549 template<int size
, bool big_endian
>
1551 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1552 Sized_relobj_file
<size
, big_endian
>* object
,
1553 unsigned int symndx
,
1554 unsigned int got_type
,
1556 unsigned int r_type
)
1558 if (object
->local_has_got_offset(symndx
, got_type
))
1561 unsigned int got_offset
= this->add_got_entry(Got_entry());
1562 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1563 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1566 template<int size
, bool big_endian
>
1568 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1569 Sized_relobj_file
<size
, big_endian
>* object
,
1570 unsigned int symndx
,
1571 unsigned int got_type
,
1573 unsigned int r_type
)
1575 if (object
->local_has_got_offset(symndx
, got_type
))
1578 unsigned int got_offset
= this->add_got_entry(Got_entry());
1579 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1580 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1583 // Add a pair of entries for a local symbol to the GOT, and add
1584 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1585 // If R_TYPE_2 == 0, add the second entry with no relocation.
1586 template<int size
, bool big_endian
>
1588 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1589 Sized_relobj_file
<size
, big_endian
>* object
,
1590 unsigned int symndx
,
1592 unsigned int got_type
,
1594 unsigned int r_type_1
,
1595 unsigned int r_type_2
)
1597 if (object
->local_has_got_offset(symndx
, got_type
))
1600 unsigned int got_offset
=
1601 this->add_got_entry_pair(Got_entry(),
1602 Got_entry(object
, symndx
, false));
1603 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1604 Output_section
* os
= object
->output_section(shndx
);
1605 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1608 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
+ size
/ 8);
1611 template<int size
, bool big_endian
>
1613 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1614 Sized_relobj_file
<size
, big_endian
>* object
,
1615 unsigned int symndx
,
1617 unsigned int got_type
,
1619 unsigned int r_type_1
,
1620 unsigned int r_type_2
)
1622 if (object
->local_has_got_offset(symndx
, got_type
))
1625 unsigned int got_offset
=
1626 this->add_got_entry_pair(Got_entry(),
1627 Got_entry(object
, symndx
, false));
1628 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1629 Output_section
* os
= object
->output_section(shndx
);
1630 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1633 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
+ size
/ 8, 0);
1636 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1638 template<int size
, bool big_endian
>
1640 Output_data_got
<size
, big_endian
>::reserve_local(
1642 Sized_relobj
<size
, big_endian
>* object
,
1643 unsigned int sym_index
,
1644 unsigned int got_type
)
1646 this->reserve_slot(i
);
1647 object
->set_local_got_offset(sym_index
, got_type
, this->got_offset(i
));
1650 // Reserve a slot in the GOT for a global symbol.
1652 template<int size
, bool big_endian
>
1654 Output_data_got
<size
, big_endian
>::reserve_global(
1657 unsigned int got_type
)
1659 this->reserve_slot(i
);
1660 gsym
->set_got_offset(got_type
, this->got_offset(i
));
1663 // Write out the GOT.
1665 template<int size
, bool big_endian
>
1667 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1669 const int add
= size
/ 8;
1671 const off_t off
= this->offset();
1672 const off_t oview_size
= this->data_size();
1673 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1675 unsigned char* pov
= oview
;
1676 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1677 p
!= this->entries_
.end();
1684 gold_assert(pov
- oview
== oview_size
);
1686 of
->write_output_view(off
, oview_size
, oview
);
1688 // We no longer need the GOT entries.
1689 this->entries_
.clear();
1692 // Create a new GOT entry and return its offset.
1694 template<int size
, bool big_endian
>
1696 Output_data_got
<size
, big_endian
>::add_got_entry(Got_entry got_entry
)
1698 if (!this->is_data_size_valid())
1700 this->entries_
.push_back(got_entry
);
1701 this->set_got_size();
1702 return this->last_got_offset();
1706 // For an incremental update, find an available slot.
1707 off_t got_offset
= this->free_list_
.allocate(size
/ 8, size
/ 8, 0);
1708 if (got_offset
== -1)
1709 gold_fallback(_("out of patch space (GOT);"
1710 " relink with --incremental-full"));
1711 unsigned int got_index
= got_offset
/ (size
/ 8);
1712 gold_assert(got_index
< this->entries_
.size());
1713 this->entries_
[got_index
] = got_entry
;
1714 return static_cast<unsigned int>(got_offset
);
1718 // Create a pair of new GOT entries and return the offset of the first.
1720 template<int size
, bool big_endian
>
1722 Output_data_got
<size
, big_endian
>::add_got_entry_pair(Got_entry got_entry_1
,
1723 Got_entry got_entry_2
)
1725 if (!this->is_data_size_valid())
1727 unsigned int got_offset
;
1728 this->entries_
.push_back(got_entry_1
);
1729 got_offset
= this->last_got_offset();
1730 this->entries_
.push_back(got_entry_2
);
1731 this->set_got_size();
1736 // For an incremental update, find an available pair of slots.
1737 off_t got_offset
= this->free_list_
.allocate(2 * size
/ 8, size
/ 8, 0);
1738 if (got_offset
== -1)
1739 gold_fallback(_("out of patch space (GOT);"
1740 " relink with --incremental-full"));
1741 unsigned int got_index
= got_offset
/ (size
/ 8);
1742 gold_assert(got_index
< this->entries_
.size());
1743 this->entries_
[got_index
] = got_entry_1
;
1744 this->entries_
[got_index
+ 1] = got_entry_2
;
1745 return static_cast<unsigned int>(got_offset
);
1749 // Output_data_dynamic::Dynamic_entry methods.
1751 // Write out the entry.
1753 template<int size
, bool big_endian
>
1755 Output_data_dynamic::Dynamic_entry::write(
1757 const Stringpool
* pool
) const
1759 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1760 switch (this->offset_
)
1762 case DYNAMIC_NUMBER
:
1766 case DYNAMIC_SECTION_SIZE
:
1767 val
= this->u_
.od
->data_size();
1768 if (this->od2
!= NULL
)
1769 val
+= this->od2
->data_size();
1772 case DYNAMIC_SYMBOL
:
1774 const Sized_symbol
<size
>* s
=
1775 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1780 case DYNAMIC_STRING
:
1781 val
= pool
->get_offset(this->u_
.str
);
1785 val
= this->u_
.od
->address() + this->offset_
;
1789 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1790 dw
.put_d_tag(this->tag_
);
1794 // Output_data_dynamic methods.
1796 // Adjust the output section to set the entry size.
1799 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1801 if (parameters
->target().get_size() == 32)
1802 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1803 else if (parameters
->target().get_size() == 64)
1804 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1809 // Set the final data size.
1812 Output_data_dynamic::set_final_data_size()
1814 // Add the terminating entry if it hasn't been added.
1815 // Because of relaxation, we can run this multiple times.
1816 if (this->entries_
.empty() || this->entries_
.back().tag() != elfcpp::DT_NULL
)
1818 int extra
= parameters
->options().spare_dynamic_tags();
1819 for (int i
= 0; i
< extra
; ++i
)
1820 this->add_constant(elfcpp::DT_NULL
, 0);
1821 this->add_constant(elfcpp::DT_NULL
, 0);
1825 if (parameters
->target().get_size() == 32)
1826 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1827 else if (parameters
->target().get_size() == 64)
1828 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1831 this->set_data_size(this->entries_
.size() * dyn_size
);
1834 // Write out the dynamic entries.
1837 Output_data_dynamic::do_write(Output_file
* of
)
1839 switch (parameters
->size_and_endianness())
1841 #ifdef HAVE_TARGET_32_LITTLE
1842 case Parameters::TARGET_32_LITTLE
:
1843 this->sized_write
<32, false>(of
);
1846 #ifdef HAVE_TARGET_32_BIG
1847 case Parameters::TARGET_32_BIG
:
1848 this->sized_write
<32, true>(of
);
1851 #ifdef HAVE_TARGET_64_LITTLE
1852 case Parameters::TARGET_64_LITTLE
:
1853 this->sized_write
<64, false>(of
);
1856 #ifdef HAVE_TARGET_64_BIG
1857 case Parameters::TARGET_64_BIG
:
1858 this->sized_write
<64, true>(of
);
1866 template<int size
, bool big_endian
>
1868 Output_data_dynamic::sized_write(Output_file
* of
)
1870 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1872 const off_t offset
= this->offset();
1873 const off_t oview_size
= this->data_size();
1874 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1876 unsigned char* pov
= oview
;
1877 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1878 p
!= this->entries_
.end();
1881 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1885 gold_assert(pov
- oview
== oview_size
);
1887 of
->write_output_view(offset
, oview_size
, oview
);
1889 // We no longer need the dynamic entries.
1890 this->entries_
.clear();
1893 // Class Output_symtab_xindex.
1896 Output_symtab_xindex::do_write(Output_file
* of
)
1898 const off_t offset
= this->offset();
1899 const off_t oview_size
= this->data_size();
1900 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1902 memset(oview
, 0, oview_size
);
1904 if (parameters
->target().is_big_endian())
1905 this->endian_do_write
<true>(oview
);
1907 this->endian_do_write
<false>(oview
);
1909 of
->write_output_view(offset
, oview_size
, oview
);
1911 // We no longer need the data.
1912 this->entries_
.clear();
1915 template<bool big_endian
>
1917 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1919 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1920 p
!= this->entries_
.end();
1923 unsigned int symndx
= p
->first
;
1924 gold_assert(symndx
* 4 < this->data_size());
1925 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ symndx
* 4, p
->second
);
1929 // Output_section::Input_section methods.
1931 // Return the current data size. For an input section we store the size here.
1932 // For an Output_section_data, we have to ask it for the size.
1935 Output_section::Input_section::current_data_size() const
1937 if (this->is_input_section())
1938 return this->u1_
.data_size
;
1941 this->u2_
.posd
->pre_finalize_data_size();
1942 return this->u2_
.posd
->current_data_size();
1946 // Return the data size. For an input section we store the size here.
1947 // For an Output_section_data, we have to ask it for the size.
1950 Output_section::Input_section::data_size() const
1952 if (this->is_input_section())
1953 return this->u1_
.data_size
;
1955 return this->u2_
.posd
->data_size();
1958 // Return the object for an input section.
1961 Output_section::Input_section::relobj() const
1963 if (this->is_input_section())
1964 return this->u2_
.object
;
1965 else if (this->is_merge_section())
1967 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
1968 return this->u2_
.pomb
->first_relobj();
1970 else if (this->is_relaxed_input_section())
1971 return this->u2_
.poris
->relobj();
1976 // Return the input section index for an input section.
1979 Output_section::Input_section::shndx() const
1981 if (this->is_input_section())
1982 return this->shndx_
;
1983 else if (this->is_merge_section())
1985 gold_assert(this->u2_
.pomb
->first_relobj() != NULL
);
1986 return this->u2_
.pomb
->first_shndx();
1988 else if (this->is_relaxed_input_section())
1989 return this->u2_
.poris
->shndx();
1994 // Set the address and file offset.
1997 Output_section::Input_section::set_address_and_file_offset(
2000 off_t section_file_offset
)
2002 if (this->is_input_section())
2003 this->u2_
.object
->set_section_offset(this->shndx_
,
2004 file_offset
- section_file_offset
);
2006 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
2009 // Reset the address and file offset.
2012 Output_section::Input_section::reset_address_and_file_offset()
2014 if (!this->is_input_section())
2015 this->u2_
.posd
->reset_address_and_file_offset();
2018 // Finalize the data size.
2021 Output_section::Input_section::finalize_data_size()
2023 if (!this->is_input_section())
2024 this->u2_
.posd
->finalize_data_size();
2027 // Try to turn an input offset into an output offset. We want to
2028 // return the output offset relative to the start of this
2029 // Input_section in the output section.
2032 Output_section::Input_section::output_offset(
2033 const Relobj
* object
,
2035 section_offset_type offset
,
2036 section_offset_type
* poutput
) const
2038 if (!this->is_input_section())
2039 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
2042 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
2049 // Return whether this is the merge section for the input section
2053 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
2054 unsigned int shndx
) const
2056 if (this->is_input_section())
2058 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
2061 // Write out the data. We don't have to do anything for an input
2062 // section--they are handled via Object::relocate--but this is where
2063 // we write out the data for an Output_section_data.
2066 Output_section::Input_section::write(Output_file
* of
)
2068 if (!this->is_input_section())
2069 this->u2_
.posd
->write(of
);
2072 // Write the data to a buffer. As for write(), we don't have to do
2073 // anything for an input section.
2076 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
2078 if (!this->is_input_section())
2079 this->u2_
.posd
->write_to_buffer(buffer
);
2082 // Print to a map file.
2085 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
2087 switch (this->shndx_
)
2089 case OUTPUT_SECTION_CODE
:
2090 case MERGE_DATA_SECTION_CODE
:
2091 case MERGE_STRING_SECTION_CODE
:
2092 this->u2_
.posd
->print_to_mapfile(mapfile
);
2095 case RELAXED_INPUT_SECTION_CODE
:
2097 Output_relaxed_input_section
* relaxed_section
=
2098 this->relaxed_input_section();
2099 mapfile
->print_input_section(relaxed_section
->relobj(),
2100 relaxed_section
->shndx());
2104 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
2109 // Output_section methods.
2111 // Construct an Output_section. NAME will point into a Stringpool.
2113 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
2114 elfcpp::Elf_Xword flags
)
2119 link_section_(NULL
),
2121 info_section_(NULL
),
2126 order_(ORDER_INVALID
),
2131 first_input_offset_(0),
2133 postprocessing_buffer_(NULL
),
2134 needs_symtab_index_(false),
2135 needs_dynsym_index_(false),
2136 should_link_to_symtab_(false),
2137 should_link_to_dynsym_(false),
2138 after_input_sections_(false),
2139 requires_postprocessing_(false),
2140 found_in_sections_clause_(false),
2141 has_load_address_(false),
2142 info_uses_section_index_(false),
2143 input_section_order_specified_(false),
2144 may_sort_attached_input_sections_(false),
2145 must_sort_attached_input_sections_(false),
2146 attached_input_sections_are_sorted_(false),
2148 is_small_section_(false),
2149 is_large_section_(false),
2150 generate_code_fills_at_write_(false),
2151 is_entsize_zero_(false),
2152 section_offsets_need_adjustment_(false),
2154 always_keeps_input_sections_(false),
2155 has_fixed_layout_(false),
2156 is_patch_space_allowed_(false),
2159 lookup_maps_(new Output_section_lookup_maps
),
2163 // An unallocated section has no address. Forcing this means that
2164 // we don't need special treatment for symbols defined in debug
2166 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
2167 this->set_address(0);
2170 Output_section::~Output_section()
2172 delete this->checkpoint_
;
2175 // Set the entry size.
2178 Output_section::set_entsize(uint64_t v
)
2180 if (this->is_entsize_zero_
)
2182 else if (this->entsize_
== 0)
2184 else if (this->entsize_
!= v
)
2187 this->is_entsize_zero_
= 1;
2191 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2192 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2193 // relocation section which applies to this section, or 0 if none, or
2194 // -1U if more than one. Return the offset of the input section
2195 // within the output section. Return -1 if the input section will
2196 // receive special handling. In the normal case we don't always keep
2197 // track of input sections for an Output_section. Instead, each
2198 // Object keeps track of the Output_section for each of its input
2199 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2200 // track of input sections here; this is used when SECTIONS appears in
2203 template<int size
, bool big_endian
>
2205 Output_section::add_input_section(Layout
* layout
,
2206 Sized_relobj_file
<size
, big_endian
>* object
,
2208 const char* secname
,
2209 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2210 unsigned int reloc_shndx
,
2211 bool have_sections_script
)
2213 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
2214 if ((addralign
& (addralign
- 1)) != 0)
2216 object
->error(_("invalid alignment %lu for section \"%s\""),
2217 static_cast<unsigned long>(addralign
), secname
);
2221 if (addralign
> this->addralign_
)
2222 this->addralign_
= addralign
;
2224 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
2225 uint64_t entsize
= shdr
.get_sh_entsize();
2227 // .debug_str is a mergeable string section, but is not always so
2228 // marked by compilers. Mark manually here so we can optimize.
2229 if (strcmp(secname
, ".debug_str") == 0)
2231 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
2235 this->update_flags_for_input_section(sh_flags
);
2236 this->set_entsize(entsize
);
2238 // If this is a SHF_MERGE section, we pass all the input sections to
2239 // a Output_data_merge. We don't try to handle relocations for such
2240 // a section. We don't try to handle empty merge sections--they
2241 // mess up the mappings, and are useless anyhow.
2242 // FIXME: Need to handle merge sections during incremental update.
2243 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
2245 && shdr
.get_sh_size() > 0
2246 && !parameters
->incremental())
2248 // Keep information about merged input sections for rebuilding fast
2249 // lookup maps if we have sections-script or we do relaxation.
2250 bool keeps_input_sections
= (this->always_keeps_input_sections_
2251 || have_sections_script
2252 || parameters
->target().may_relax());
2254 if (this->add_merge_input_section(object
, shndx
, sh_flags
, entsize
,
2255 addralign
, keeps_input_sections
))
2257 // Tell the relocation routines that they need to call the
2258 // output_offset method to determine the final address.
2263 section_size_type input_section_size
= shdr
.get_sh_size();
2264 section_size_type uncompressed_size
;
2265 if (object
->section_is_compressed(shndx
, &uncompressed_size
))
2266 input_section_size
= uncompressed_size
;
2268 off_t offset_in_section
;
2269 off_t aligned_offset_in_section
;
2270 if (this->has_fixed_layout())
2272 // For incremental updates, find a chunk of unused space in the section.
2273 offset_in_section
= this->free_list_
.allocate(input_section_size
,
2275 if (offset_in_section
== -1)
2276 gold_fallback(_("out of patch space in section %s; "
2277 "relink with --incremental-full"),
2279 aligned_offset_in_section
= offset_in_section
;
2283 offset_in_section
= this->current_data_size_for_child();
2284 aligned_offset_in_section
= align_address(offset_in_section
,
2286 this->set_current_data_size_for_child(aligned_offset_in_section
2287 + input_section_size
);
2290 // Determine if we want to delay code-fill generation until the output
2291 // section is written. When the target is relaxing, we want to delay fill
2292 // generating to avoid adjusting them during relaxation. Also, if we are
2293 // sorting input sections we must delay fill generation.
2294 if (!this->generate_code_fills_at_write_
2295 && !have_sections_script
2296 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2297 && parameters
->target().has_code_fill()
2298 && (parameters
->target().may_relax()
2299 || parameters
->options().section_ordering_file()))
2301 gold_assert(this->fills_
.empty());
2302 this->generate_code_fills_at_write_
= true;
2305 if (aligned_offset_in_section
> offset_in_section
2306 && !this->generate_code_fills_at_write_
2307 && !have_sections_script
2308 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
2309 && parameters
->target().has_code_fill())
2311 // We need to add some fill data. Using fill_list_ when
2312 // possible is an optimization, since we will often have fill
2313 // sections without input sections.
2314 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
2315 if (this->input_sections_
.empty())
2316 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
2319 std::string
fill_data(parameters
->target().code_fill(fill_len
));
2320 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
2321 this->input_sections_
.push_back(Input_section(odc
));
2325 // We need to keep track of this section if we are already keeping
2326 // track of sections, or if we are relaxing. Also, if this is a
2327 // section which requires sorting, or which may require sorting in
2328 // the future, we keep track of the sections. If the
2329 // --section-ordering-file option is used to specify the order of
2330 // sections, we need to keep track of sections.
2331 if (this->always_keeps_input_sections_
2332 || have_sections_script
2333 || !this->input_sections_
.empty()
2334 || this->may_sort_attached_input_sections()
2335 || this->must_sort_attached_input_sections()
2336 || parameters
->options().user_set_Map()
2337 || parameters
->target().may_relax()
2338 || parameters
->options().section_ordering_file())
2340 Input_section
isecn(object
, shndx
, input_section_size
, addralign
);
2341 if (parameters
->options().section_ordering_file())
2343 unsigned int section_order_index
=
2344 layout
->find_section_order_index(std::string(secname
));
2345 if (section_order_index
!= 0)
2347 isecn
.set_section_order_index(section_order_index
);
2348 this->set_input_section_order_specified();
2351 if (this->has_fixed_layout())
2353 // For incremental updates, finalize the address and offset now.
2354 uint64_t addr
= this->address();
2355 isecn
.set_address_and_file_offset(addr
+ aligned_offset_in_section
,
2356 aligned_offset_in_section
,
2359 this->input_sections_
.push_back(isecn
);
2362 return aligned_offset_in_section
;
2365 // Add arbitrary data to an output section.
2368 Output_section::add_output_section_data(Output_section_data
* posd
)
2370 Input_section
inp(posd
);
2371 this->add_output_section_data(&inp
);
2373 if (posd
->is_data_size_valid())
2375 off_t offset_in_section
;
2376 if (this->has_fixed_layout())
2378 // For incremental updates, find a chunk of unused space.
2379 offset_in_section
= this->free_list_
.allocate(posd
->data_size(),
2380 posd
->addralign(), 0);
2381 if (offset_in_section
== -1)
2382 gold_fallback(_("out of patch space in section %s; "
2383 "relink with --incremental-full"),
2385 // Finalize the address and offset now.
2386 uint64_t addr
= this->address();
2387 off_t offset
= this->offset();
2388 posd
->set_address_and_file_offset(addr
+ offset_in_section
,
2389 offset
+ offset_in_section
);
2393 offset_in_section
= this->current_data_size_for_child();
2394 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2396 this->set_current_data_size_for_child(aligned_offset_in_section
2397 + posd
->data_size());
2400 else if (this->has_fixed_layout())
2402 // For incremental updates, arrange for the data to have a fixed layout.
2403 // This will mean that additions to the data must be allocated from
2404 // free space within the containing output section.
2405 uint64_t addr
= this->address();
2406 posd
->set_address(addr
);
2407 posd
->set_file_offset(0);
2408 // FIXME: This should eventually be unreachable.
2409 // gold_unreachable();
2413 // Add a relaxed input section.
2416 Output_section::add_relaxed_input_section(Layout
* layout
,
2417 Output_relaxed_input_section
* poris
,
2418 const std::string
& name
)
2420 Input_section
inp(poris
);
2422 // If the --section-ordering-file option is used to specify the order of
2423 // sections, we need to keep track of sections.
2424 if (parameters
->options().section_ordering_file())
2426 unsigned int section_order_index
=
2427 layout
->find_section_order_index(name
);
2428 if (section_order_index
!= 0)
2430 inp
.set_section_order_index(section_order_index
);
2431 this->set_input_section_order_specified();
2435 this->add_output_section_data(&inp
);
2436 if (this->lookup_maps_
->is_valid())
2437 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2438 poris
->shndx(), poris
);
2440 // For a relaxed section, we use the current data size. Linker scripts
2441 // get all the input sections, including relaxed one from an output
2442 // section and add them back to them same output section to compute the
2443 // output section size. If we do not account for sizes of relaxed input
2444 // sections, an output section would be incorrectly sized.
2445 off_t offset_in_section
= this->current_data_size_for_child();
2446 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2447 poris
->addralign());
2448 this->set_current_data_size_for_child(aligned_offset_in_section
2449 + poris
->current_data_size());
2452 // Add arbitrary data to an output section by Input_section.
2455 Output_section::add_output_section_data(Input_section
* inp
)
2457 if (this->input_sections_
.empty())
2458 this->first_input_offset_
= this->current_data_size_for_child();
2460 this->input_sections_
.push_back(*inp
);
2462 uint64_t addralign
= inp
->addralign();
2463 if (addralign
> this->addralign_
)
2464 this->addralign_
= addralign
;
2466 inp
->set_output_section(this);
2469 // Add a merge section to an output section.
2472 Output_section::add_output_merge_section(Output_section_data
* posd
,
2473 bool is_string
, uint64_t entsize
)
2475 Input_section
inp(posd
, is_string
, entsize
);
2476 this->add_output_section_data(&inp
);
2479 // Add an input section to a SHF_MERGE section.
2482 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
2483 uint64_t flags
, uint64_t entsize
,
2485 bool keeps_input_sections
)
2487 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
2489 // We only merge strings if the alignment is not more than the
2490 // character size. This could be handled, but it's unusual.
2491 if (is_string
&& addralign
> entsize
)
2494 // We cannot restore merged input section states.
2495 gold_assert(this->checkpoint_
== NULL
);
2497 // Look up merge sections by required properties.
2498 // Currently, we only invalidate the lookup maps in script processing
2499 // and relaxation. We should not have done either when we reach here.
2500 // So we assume that the lookup maps are valid to simply code.
2501 gold_assert(this->lookup_maps_
->is_valid());
2502 Merge_section_properties
msp(is_string
, entsize
, addralign
);
2503 Output_merge_base
* pomb
= this->lookup_maps_
->find_merge_section(msp
);
2504 bool is_new
= false;
2507 gold_assert(pomb
->is_string() == is_string
2508 && pomb
->entsize() == entsize
2509 && pomb
->addralign() == addralign
);
2513 // Create a new Output_merge_data or Output_merge_string_data.
2515 pomb
= new Output_merge_data(entsize
, addralign
);
2521 pomb
= new Output_merge_string
<char>(addralign
);
2524 pomb
= new Output_merge_string
<uint16_t>(addralign
);
2527 pomb
= new Output_merge_string
<uint32_t>(addralign
);
2533 // If we need to do script processing or relaxation, we need to keep
2534 // the original input sections to rebuild the fast lookup maps.
2535 if (keeps_input_sections
)
2536 pomb
->set_keeps_input_sections();
2540 if (pomb
->add_input_section(object
, shndx
))
2542 // Add new merge section to this output section and link merge
2543 // section properties to new merge section in map.
2546 this->add_output_merge_section(pomb
, is_string
, entsize
);
2547 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2550 // Add input section to new merge section and link input section to new
2551 // merge section in map.
2552 this->lookup_maps_
->add_merge_input_section(object
, shndx
, pomb
);
2557 // If add_input_section failed, delete new merge section to avoid
2558 // exporting empty merge sections in Output_section::get_input_section.
2565 // Build a relaxation map to speed up relaxation of existing input sections.
2566 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2569 Output_section::build_relaxation_map(
2570 const Input_section_list
& input_sections
,
2572 Relaxation_map
* relaxation_map
) const
2574 for (size_t i
= 0; i
< limit
; ++i
)
2576 const Input_section
& is(input_sections
[i
]);
2577 if (is
.is_input_section() || is
.is_relaxed_input_section())
2579 Section_id
sid(is
.relobj(), is
.shndx());
2580 (*relaxation_map
)[sid
] = i
;
2585 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2586 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2587 // indices of INPUT_SECTIONS.
2590 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2591 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
2592 const Relaxation_map
& map
,
2593 Input_section_list
* input_sections
)
2595 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2597 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2598 Section_id
sid(poris
->relobj(), poris
->shndx());
2599 Relaxation_map::const_iterator p
= map
.find(sid
);
2600 gold_assert(p
!= map
.end());
2601 gold_assert((*input_sections
)[p
->second
].is_input_section());
2603 // Remember section order index of original input section
2604 // if it is set. Copy it to the relaxed input section.
2606 (*input_sections
)[p
->second
].section_order_index();
2607 (*input_sections
)[p
->second
] = Input_section(poris
);
2608 (*input_sections
)[p
->second
].set_section_order_index(soi
);
2612 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2613 // is a vector of pointers to Output_relaxed_input_section or its derived
2614 // classes. The relaxed sections must correspond to existing input sections.
2617 Output_section::convert_input_sections_to_relaxed_sections(
2618 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
)
2620 gold_assert(parameters
->target().may_relax());
2622 // We want to make sure that restore_states does not undo the effect of
2623 // this. If there is no checkpoint active, just search the current
2624 // input section list and replace the sections there. If there is
2625 // a checkpoint, also replace the sections there.
2627 // By default, we look at the whole list.
2628 size_t limit
= this->input_sections_
.size();
2630 if (this->checkpoint_
!= NULL
)
2632 // Replace input sections with relaxed input section in the saved
2633 // copy of the input section list.
2634 if (this->checkpoint_
->input_sections_saved())
2637 this->build_relaxation_map(
2638 *(this->checkpoint_
->input_sections()),
2639 this->checkpoint_
->input_sections()->size(),
2641 this->convert_input_sections_in_list_to_relaxed_sections(
2644 this->checkpoint_
->input_sections());
2648 // We have not copied the input section list yet. Instead, just
2649 // look at the portion that would be saved.
2650 limit
= this->checkpoint_
->input_sections_size();
2654 // Convert input sections in input_section_list.
2656 this->build_relaxation_map(this->input_sections_
, limit
, &map
);
2657 this->convert_input_sections_in_list_to_relaxed_sections(
2660 &this->input_sections_
);
2662 // Update fast look-up map.
2663 if (this->lookup_maps_
->is_valid())
2664 for (size_t i
= 0; i
< relaxed_sections
.size(); ++i
)
2666 Output_relaxed_input_section
* poris
= relaxed_sections
[i
];
2667 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2668 poris
->shndx(), poris
);
2672 // Update the output section flags based on input section flags.
2675 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags
)
2677 // If we created the section with SHF_ALLOC clear, we set the
2678 // address. If we are now setting the SHF_ALLOC flag, we need to
2680 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0
2681 && (flags
& elfcpp::SHF_ALLOC
) != 0)
2682 this->mark_address_invalid();
2684 this->flags_
|= (flags
2685 & (elfcpp::SHF_WRITE
2687 | elfcpp::SHF_EXECINSTR
));
2689 if ((flags
& elfcpp::SHF_MERGE
) == 0)
2690 this->flags_
&=~ elfcpp::SHF_MERGE
;
2693 if (this->current_data_size_for_child() == 0)
2694 this->flags_
|= elfcpp::SHF_MERGE
;
2697 if ((flags
& elfcpp::SHF_STRINGS
) == 0)
2698 this->flags_
&=~ elfcpp::SHF_STRINGS
;
2701 if (this->current_data_size_for_child() == 0)
2702 this->flags_
|= elfcpp::SHF_STRINGS
;
2706 // Find the merge section into which an input section with index SHNDX in
2707 // OBJECT has been added. Return NULL if none found.
2709 Output_section_data
*
2710 Output_section::find_merge_section(const Relobj
* object
,
2711 unsigned int shndx
) const
2713 if (!this->lookup_maps_
->is_valid())
2714 this->build_lookup_maps();
2715 return this->lookup_maps_
->find_merge_section(object
, shndx
);
2718 // Build the lookup maps for merge and relaxed sections. This is needs
2719 // to be declared as a const methods so that it is callable with a const
2720 // Output_section pointer. The method only updates states of the maps.
2723 Output_section::build_lookup_maps() const
2725 this->lookup_maps_
->clear();
2726 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2727 p
!= this->input_sections_
.end();
2730 if (p
->is_merge_section())
2732 Output_merge_base
* pomb
= p
->output_merge_base();
2733 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
2735 this->lookup_maps_
->add_merge_section(msp
, pomb
);
2736 for (Output_merge_base::Input_sections::const_iterator is
=
2737 pomb
->input_sections_begin();
2738 is
!= pomb
->input_sections_end();
2741 const Const_section_id
& csid
= *is
;
2742 this->lookup_maps_
->add_merge_input_section(csid
.first
,
2747 else if (p
->is_relaxed_input_section())
2749 Output_relaxed_input_section
* poris
= p
->relaxed_input_section();
2750 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
2751 poris
->shndx(), poris
);
2756 // Find an relaxed input section corresponding to an input section
2757 // in OBJECT with index SHNDX.
2759 const Output_relaxed_input_section
*
2760 Output_section::find_relaxed_input_section(const Relobj
* object
,
2761 unsigned int shndx
) const
2763 if (!this->lookup_maps_
->is_valid())
2764 this->build_lookup_maps();
2765 return this->lookup_maps_
->find_relaxed_input_section(object
, shndx
);
2768 // Given an address OFFSET relative to the start of input section
2769 // SHNDX in OBJECT, return whether this address is being included in
2770 // the final link. This should only be called if SHNDX in OBJECT has
2771 // a special mapping.
2774 Output_section::is_input_address_mapped(const Relobj
* object
,
2778 // Look at the Output_section_data_maps first.
2779 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2781 posd
= this->find_relaxed_input_section(object
, shndx
);
2785 section_offset_type output_offset
;
2786 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2788 return output_offset
!= -1;
2791 // Fall back to the slow look-up.
2792 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2793 p
!= this->input_sections_
.end();
2796 section_offset_type output_offset
;
2797 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2798 return output_offset
!= -1;
2801 // By default we assume that the address is mapped. This should
2802 // only be called after we have passed all sections to Layout. At
2803 // that point we should know what we are discarding.
2807 // Given an address OFFSET relative to the start of input section
2808 // SHNDX in object OBJECT, return the output offset relative to the
2809 // start of the input section in the output section. This should only
2810 // be called if SHNDX in OBJECT has a special mapping.
2813 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2814 section_offset_type offset
) const
2816 // This can only be called meaningfully when we know the data size
2818 gold_assert(this->is_data_size_valid());
2820 // Look at the Output_section_data_maps first.
2821 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2823 posd
= this->find_relaxed_input_section(object
, shndx
);
2826 section_offset_type output_offset
;
2827 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2829 return output_offset
;
2832 // Fall back to the slow look-up.
2833 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2834 p
!= this->input_sections_
.end();
2837 section_offset_type output_offset
;
2838 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2839 return output_offset
;
2844 // Return the output virtual address of OFFSET relative to the start
2845 // of input section SHNDX in object OBJECT.
2848 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
2851 uint64_t addr
= this->address() + this->first_input_offset_
;
2853 // Look at the Output_section_data_maps first.
2854 const Output_section_data
* posd
= this->find_merge_section(object
, shndx
);
2856 posd
= this->find_relaxed_input_section(object
, shndx
);
2857 if (posd
!= NULL
&& posd
->is_address_valid())
2859 section_offset_type output_offset
;
2860 bool found
= posd
->output_offset(object
, shndx
, offset
, &output_offset
);
2862 return posd
->address() + output_offset
;
2865 // Fall back to the slow look-up.
2866 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2867 p
!= this->input_sections_
.end();
2870 addr
= align_address(addr
, p
->addralign());
2871 section_offset_type output_offset
;
2872 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2874 if (output_offset
== -1)
2876 return addr
+ output_offset
;
2878 addr
+= p
->data_size();
2881 // If we get here, it means that we don't know the mapping for this
2882 // input section. This might happen in principle if
2883 // add_input_section were called before add_output_section_data.
2884 // But it should never actually happen.
2889 // Find the output address of the start of the merged section for
2890 // input section SHNDX in object OBJECT.
2893 Output_section::find_starting_output_address(const Relobj
* object
,
2895 uint64_t* paddr
) const
2897 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2898 // Looking up the merge section map does not always work as we sometimes
2899 // find a merge section without its address set.
2900 uint64_t addr
= this->address() + this->first_input_offset_
;
2901 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2902 p
!= this->input_sections_
.end();
2905 addr
= align_address(addr
, p
->addralign());
2907 // It would be nice if we could use the existing output_offset
2908 // method to get the output offset of input offset 0.
2909 // Unfortunately we don't know for sure that input offset 0 is
2911 if (p
->is_merge_section_for(object
, shndx
))
2917 addr
+= p
->data_size();
2920 // We couldn't find a merge output section for this input section.
2924 // Update the data size of an Output_section.
2927 Output_section::update_data_size()
2929 if (this->input_sections_
.empty())
2932 if (this->must_sort_attached_input_sections()
2933 || this->input_section_order_specified())
2934 this->sort_attached_input_sections();
2936 off_t off
= this->first_input_offset_
;
2937 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2938 p
!= this->input_sections_
.end();
2941 off
= align_address(off
, p
->addralign());
2942 off
+= p
->current_data_size();
2945 this->set_current_data_size_for_child(off
);
2948 // Set the data size of an Output_section. This is where we handle
2949 // setting the addresses of any Output_section_data objects.
2952 Output_section::set_final_data_size()
2956 if (this->input_sections_
.empty())
2957 data_size
= this->current_data_size_for_child();
2960 if (this->must_sort_attached_input_sections()
2961 || this->input_section_order_specified())
2962 this->sort_attached_input_sections();
2964 uint64_t address
= this->address();
2965 off_t startoff
= this->offset();
2966 off_t off
= startoff
+ this->first_input_offset_
;
2967 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2968 p
!= this->input_sections_
.end();
2971 off
= align_address(off
, p
->addralign());
2972 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
2974 off
+= p
->data_size();
2976 data_size
= off
- startoff
;
2979 // For full incremental links, we want to allocate some patch space
2980 // in most sections for subsequent incremental updates.
2981 if (this->is_patch_space_allowed_
&& parameters
->incremental_full())
2983 double pct
= parameters
->options().incremental_patch();
2984 off_t extra
= static_cast<off_t
>(data_size
* pct
);
2985 off_t new_size
= align_address(data_size
+ extra
, this->addralign());
2986 this->patch_space_
= new_size
- data_size
;
2987 gold_debug(DEBUG_INCREMENTAL
,
2988 "set_final_data_size: %08lx + %08lx: section %s",
2989 static_cast<long>(data_size
),
2990 static_cast<long>(this->patch_space_
),
2992 data_size
= new_size
;
2995 this->set_data_size(data_size
);
2998 // Reset the address and file offset.
3001 Output_section::do_reset_address_and_file_offset()
3003 // An unallocated section has no address. Forcing this means that
3004 // we don't need special treatment for symbols defined in debug
3005 // sections. We do the same in the constructor. This does not
3006 // apply to NOLOAD sections though.
3007 if (((this->flags_
& elfcpp::SHF_ALLOC
) == 0) && !this->is_noload_
)
3008 this->set_address(0);
3010 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3011 p
!= this->input_sections_
.end();
3013 p
->reset_address_and_file_offset();
3015 // Remove any patch space that was added in set_final_data_size.
3016 if (this->patch_space_
> 0)
3018 this->set_current_data_size_for_child(this->current_data_size_for_child()
3019 - this->patch_space_
);
3020 this->patch_space_
= 0;
3024 // Return true if address and file offset have the values after reset.
3027 Output_section::do_address_and_file_offset_have_reset_values() const
3029 if (this->is_offset_valid())
3032 // An unallocated section has address 0 after its construction or a reset.
3033 if ((this->flags_
& elfcpp::SHF_ALLOC
) == 0)
3034 return this->is_address_valid() && this->address() == 0;
3036 return !this->is_address_valid();
3039 // Set the TLS offset. Called only for SHT_TLS sections.
3042 Output_section::do_set_tls_offset(uint64_t tls_base
)
3044 this->tls_offset_
= this->address() - tls_base
;
3047 // In a few cases we need to sort the input sections attached to an
3048 // output section. This is used to implement the type of constructor
3049 // priority ordering implemented by the GNU linker, in which the
3050 // priority becomes part of the section name and the sections are
3051 // sorted by name. We only do this for an output section if we see an
3052 // attached input section matching ".ctors.*", ".dtors.*",
3053 // ".init_array.*" or ".fini_array.*".
3055 class Output_section::Input_section_sort_entry
3058 Input_section_sort_entry()
3059 : input_section_(), index_(-1U), section_has_name_(false),
3063 Input_section_sort_entry(const Input_section
& input_section
,
3065 bool must_sort_attached_input_sections
)
3066 : input_section_(input_section
), index_(index
),
3067 section_has_name_(input_section
.is_input_section()
3068 || input_section
.is_relaxed_input_section())
3070 if (this->section_has_name_
3071 && must_sort_attached_input_sections
)
3073 // This is only called single-threaded from Layout::finalize,
3074 // so it is OK to lock. Unfortunately we have no way to pass
3076 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
3077 Object
* obj
= (input_section
.is_input_section()
3078 ? input_section
.relobj()
3079 : input_section
.relaxed_input_section()->relobj());
3080 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
3082 // This is a slow operation, which should be cached in
3083 // Layout::layout if this becomes a speed problem.
3084 this->section_name_
= obj
->section_name(input_section
.shndx());
3088 // Return the Input_section.
3089 const Input_section
&
3090 input_section() const
3092 gold_assert(this->index_
!= -1U);
3093 return this->input_section_
;
3096 // The index of this entry in the original list. This is used to
3097 // make the sort stable.
3101 gold_assert(this->index_
!= -1U);
3102 return this->index_
;
3105 // Whether there is a section name.
3107 section_has_name() const
3108 { return this->section_has_name_
; }
3110 // The section name.
3112 section_name() const
3114 gold_assert(this->section_has_name_
);
3115 return this->section_name_
;
3118 // Return true if the section name has a priority. This is assumed
3119 // to be true if it has a dot after the initial dot.
3121 has_priority() const
3123 gold_assert(this->section_has_name_
);
3124 return this->section_name_
.find('.', 1) != std::string::npos
;
3127 // Return the priority. Believe it or not, gcc encodes the priority
3128 // differently for .ctors/.dtors and .init_array/.fini_array
3131 get_priority() const
3133 gold_assert(this->section_has_name_
);
3135 if (is_prefix_of(".ctors.", this->section_name_
.c_str())
3136 || is_prefix_of(".dtors.", this->section_name_
.c_str()))
3138 else if (is_prefix_of(".init_array.", this->section_name_
.c_str())
3139 || is_prefix_of(".fini_array.", this->section_name_
.c_str()))
3144 unsigned long prio
= strtoul((this->section_name_
.c_str()
3145 + (is_ctors
? 7 : 12)),
3150 return 65535 - prio
;
3155 // Return true if this an input file whose base name matches
3156 // FILE_NAME. The base name must have an extension of ".o", and
3157 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3158 // This is to match crtbegin.o as well as crtbeginS.o without
3159 // getting confused by other possibilities. Overall matching the
3160 // file name this way is a dreadful hack, but the GNU linker does it
3161 // in order to better support gcc, and we need to be compatible.
3163 match_file_name(const char* file_name
) const
3164 { return Layout::match_file_name(this->input_section_
.relobj(), file_name
); }
3166 // Returns 1 if THIS should appear before S in section order, -1 if S
3167 // appears before THIS and 0 if they are not comparable.
3169 compare_section_ordering(const Input_section_sort_entry
& s
) const
3171 unsigned int this_secn_index
= this->input_section_
.section_order_index();
3172 unsigned int s_secn_index
= s
.input_section().section_order_index();
3173 if (this_secn_index
> 0 && s_secn_index
> 0)
3175 if (this_secn_index
< s_secn_index
)
3177 else if (this_secn_index
> s_secn_index
)
3184 // The Input_section we are sorting.
3185 Input_section input_section_
;
3186 // The index of this Input_section in the original list.
3187 unsigned int index_
;
3188 // Whether this Input_section has a section name--it won't if this
3189 // is some random Output_section_data.
3190 bool section_has_name_
;
3191 // The section name if there is one.
3192 std::string section_name_
;
3195 // Return true if S1 should come before S2 in the output section.
3198 Output_section::Input_section_sort_compare::operator()(
3199 const Output_section::Input_section_sort_entry
& s1
,
3200 const Output_section::Input_section_sort_entry
& s2
) const
3202 // crtbegin.o must come first.
3203 bool s1_begin
= s1
.match_file_name("crtbegin");
3204 bool s2_begin
= s2
.match_file_name("crtbegin");
3205 if (s1_begin
|| s2_begin
)
3211 return s1
.index() < s2
.index();
3214 // crtend.o must come last.
3215 bool s1_end
= s1
.match_file_name("crtend");
3216 bool s2_end
= s2
.match_file_name("crtend");
3217 if (s1_end
|| s2_end
)
3223 return s1
.index() < s2
.index();
3226 // We sort all the sections with no names to the end.
3227 if (!s1
.section_has_name() || !s2
.section_has_name())
3229 if (s1
.section_has_name())
3231 if (s2
.section_has_name())
3233 return s1
.index() < s2
.index();
3236 // A section with a priority follows a section without a priority.
3237 bool s1_has_priority
= s1
.has_priority();
3238 bool s2_has_priority
= s2
.has_priority();
3239 if (s1_has_priority
&& !s2_has_priority
)
3241 if (!s1_has_priority
&& s2_has_priority
)
3244 // Check if a section order exists for these sections through a section
3245 // ordering file. If sequence_num is 0, an order does not exist.
3246 int sequence_num
= s1
.compare_section_ordering(s2
);
3247 if (sequence_num
!= 0)
3248 return sequence_num
== 1;
3250 // Otherwise we sort by name.
3251 int compare
= s1
.section_name().compare(s2
.section_name());
3255 // Otherwise we keep the input order.
3256 return s1
.index() < s2
.index();
3259 // Return true if S1 should come before S2 in an .init_array or .fini_array
3263 Output_section::Input_section_sort_init_fini_compare::operator()(
3264 const Output_section::Input_section_sort_entry
& s1
,
3265 const Output_section::Input_section_sort_entry
& s2
) const
3267 // We sort all the sections with no names to the end.
3268 if (!s1
.section_has_name() || !s2
.section_has_name())
3270 if (s1
.section_has_name())
3272 if (s2
.section_has_name())
3274 return s1
.index() < s2
.index();
3277 // A section without a priority follows a section with a priority.
3278 // This is the reverse of .ctors and .dtors sections.
3279 bool s1_has_priority
= s1
.has_priority();
3280 bool s2_has_priority
= s2
.has_priority();
3281 if (s1_has_priority
&& !s2_has_priority
)
3283 if (!s1_has_priority
&& s2_has_priority
)
3286 // .ctors and .dtors sections without priority come after
3287 // .init_array and .fini_array sections without priority.
3288 if (!s1_has_priority
3289 && (s1
.section_name() == ".ctors" || s1
.section_name() == ".dtors")
3290 && s1
.section_name() != s2
.section_name())
3292 if (!s2_has_priority
3293 && (s2
.section_name() == ".ctors" || s2
.section_name() == ".dtors")
3294 && s2
.section_name() != s1
.section_name())
3297 // Sort by priority if we can.
3298 if (s1_has_priority
)
3300 unsigned int s1_prio
= s1
.get_priority();
3301 unsigned int s2_prio
= s2
.get_priority();
3302 if (s1_prio
< s2_prio
)
3304 else if (s1_prio
> s2_prio
)
3308 // Check if a section order exists for these sections through a section
3309 // ordering file. If sequence_num is 0, an order does not exist.
3310 int sequence_num
= s1
.compare_section_ordering(s2
);
3311 if (sequence_num
!= 0)
3312 return sequence_num
== 1;
3314 // Otherwise we sort by name.
3315 int compare
= s1
.section_name().compare(s2
.section_name());
3319 // Otherwise we keep the input order.
3320 return s1
.index() < s2
.index();
3323 // Return true if S1 should come before S2. Sections that do not match
3324 // any pattern in the section ordering file are placed ahead of the sections
3325 // that match some pattern.
3328 Output_section::Input_section_sort_section_order_index_compare::operator()(
3329 const Output_section::Input_section_sort_entry
& s1
,
3330 const Output_section::Input_section_sort_entry
& s2
) const
3332 unsigned int s1_secn_index
= s1
.input_section().section_order_index();
3333 unsigned int s2_secn_index
= s2
.input_section().section_order_index();
3335 // Keep input order if section ordering cannot determine order.
3336 if (s1_secn_index
== s2_secn_index
)
3337 return s1
.index() < s2
.index();
3339 return s1_secn_index
< s2_secn_index
;
3342 // Sort the input sections attached to an output section.
3345 Output_section::sort_attached_input_sections()
3347 if (this->attached_input_sections_are_sorted_
)
3350 if (this->checkpoint_
!= NULL
3351 && !this->checkpoint_
->input_sections_saved())
3352 this->checkpoint_
->save_input_sections();
3354 // The only thing we know about an input section is the object and
3355 // the section index. We need the section name. Recomputing this
3356 // is slow but this is an unusual case. If this becomes a speed
3357 // problem we can cache the names as required in Layout::layout.
3359 // We start by building a larger vector holding a copy of each
3360 // Input_section, plus its current index in the list and its name.
3361 std::vector
<Input_section_sort_entry
> sort_list
;
3364 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3365 p
!= this->input_sections_
.end();
3367 sort_list
.push_back(Input_section_sort_entry(*p
, i
,
3368 this->must_sort_attached_input_sections()));
3370 // Sort the input sections.
3371 if (this->must_sort_attached_input_sections())
3373 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3374 || this->type() == elfcpp::SHT_INIT_ARRAY
3375 || this->type() == elfcpp::SHT_FINI_ARRAY
)
3376 std::sort(sort_list
.begin(), sort_list
.end(),
3377 Input_section_sort_init_fini_compare());
3379 std::sort(sort_list
.begin(), sort_list
.end(),
3380 Input_section_sort_compare());
3384 gold_assert(parameters
->options().section_ordering_file());
3385 std::sort(sort_list
.begin(), sort_list
.end(),
3386 Input_section_sort_section_order_index_compare());
3389 // Copy the sorted input sections back to our list.
3390 this->input_sections_
.clear();
3391 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
3392 p
!= sort_list
.end();
3394 this->input_sections_
.push_back(p
->input_section());
3397 // Remember that we sorted the input sections, since we might get
3399 this->attached_input_sections_are_sorted_
= true;
3402 // Write the section header to *OSHDR.
3404 template<int size
, bool big_endian
>
3406 Output_section::write_header(const Layout
* layout
,
3407 const Stringpool
* secnamepool
,
3408 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
3410 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
3411 oshdr
->put_sh_type(this->type_
);
3413 elfcpp::Elf_Xword flags
= this->flags_
;
3414 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
3415 flags
|= elfcpp::SHF_INFO_LINK
;
3416 oshdr
->put_sh_flags(flags
);
3418 oshdr
->put_sh_addr(this->address());
3419 oshdr
->put_sh_offset(this->offset());
3420 oshdr
->put_sh_size(this->data_size());
3421 if (this->link_section_
!= NULL
)
3422 oshdr
->put_sh_link(this->link_section_
->out_shndx());
3423 else if (this->should_link_to_symtab_
)
3424 oshdr
->put_sh_link(layout
->symtab_section_shndx());
3425 else if (this->should_link_to_dynsym_
)
3426 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
3428 oshdr
->put_sh_link(this->link_
);
3430 elfcpp::Elf_Word info
;
3431 if (this->info_section_
!= NULL
)
3433 if (this->info_uses_section_index_
)
3434 info
= this->info_section_
->out_shndx();
3436 info
= this->info_section_
->symtab_index();
3438 else if (this->info_symndx_
!= NULL
)
3439 info
= this->info_symndx_
->symtab_index();
3442 oshdr
->put_sh_info(info
);
3444 oshdr
->put_sh_addralign(this->addralign_
);
3445 oshdr
->put_sh_entsize(this->entsize_
);
3448 // Write out the data. For input sections the data is written out by
3449 // Object::relocate, but we have to handle Output_section_data objects
3453 Output_section::do_write(Output_file
* of
)
3455 gold_assert(!this->requires_postprocessing());
3457 // If the target performs relaxation, we delay filler generation until now.
3458 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3460 off_t output_section_file_offset
= this->offset();
3461 for (Fill_list::iterator p
= this->fills_
.begin();
3462 p
!= this->fills_
.end();
3465 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3466 of
->write(output_section_file_offset
+ p
->section_offset(),
3467 fill_data
.data(), fill_data
.size());
3470 off_t off
= this->offset() + this->first_input_offset_
;
3471 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3472 p
!= this->input_sections_
.end();
3475 off_t aligned_off
= align_address(off
, p
->addralign());
3476 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3478 size_t fill_len
= aligned_off
- off
;
3479 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3480 of
->write(off
, fill_data
.data(), fill_data
.size());
3484 off
= aligned_off
+ p
->data_size();
3488 // If a section requires postprocessing, create the buffer to use.
3491 Output_section::create_postprocessing_buffer()
3493 gold_assert(this->requires_postprocessing());
3495 if (this->postprocessing_buffer_
!= NULL
)
3498 if (!this->input_sections_
.empty())
3500 off_t off
= this->first_input_offset_
;
3501 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3502 p
!= this->input_sections_
.end();
3505 off
= align_address(off
, p
->addralign());
3506 p
->finalize_data_size();
3507 off
+= p
->data_size();
3509 this->set_current_data_size_for_child(off
);
3512 off_t buffer_size
= this->current_data_size_for_child();
3513 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
3516 // Write all the data of an Output_section into the postprocessing
3517 // buffer. This is used for sections which require postprocessing,
3518 // such as compression. Input sections are handled by
3519 // Object::Relocate.
3522 Output_section::write_to_postprocessing_buffer()
3524 gold_assert(this->requires_postprocessing());
3526 // If the target performs relaxation, we delay filler generation until now.
3527 gold_assert(!this->generate_code_fills_at_write_
|| this->fills_
.empty());
3529 unsigned char* buffer
= this->postprocessing_buffer();
3530 for (Fill_list::iterator p
= this->fills_
.begin();
3531 p
!= this->fills_
.end();
3534 std::string
fill_data(parameters
->target().code_fill(p
->length()));
3535 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
3539 off_t off
= this->first_input_offset_
;
3540 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3541 p
!= this->input_sections_
.end();
3544 off_t aligned_off
= align_address(off
, p
->addralign());
3545 if (this->generate_code_fills_at_write_
&& (off
!= aligned_off
))
3547 size_t fill_len
= aligned_off
- off
;
3548 std::string
fill_data(parameters
->target().code_fill(fill_len
));
3549 memcpy(buffer
+ off
, fill_data
.data(), fill_data
.size());
3552 p
->write_to_buffer(buffer
+ aligned_off
);
3553 off
= aligned_off
+ p
->data_size();
3557 // Get the input sections for linker script processing. We leave
3558 // behind the Output_section_data entries. Note that this may be
3559 // slightly incorrect for merge sections. We will leave them behind,
3560 // but it is possible that the script says that they should follow
3561 // some other input sections, as in:
3562 // .rodata { *(.rodata) *(.rodata.cst*) }
3563 // For that matter, we don't handle this correctly:
3564 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3565 // With luck this will never matter.
3568 Output_section::get_input_sections(
3570 const std::string
& fill
,
3571 std::list
<Input_section
>* input_sections
)
3573 if (this->checkpoint_
!= NULL
3574 && !this->checkpoint_
->input_sections_saved())
3575 this->checkpoint_
->save_input_sections();
3577 // Invalidate fast look-up maps.
3578 this->lookup_maps_
->invalidate();
3580 uint64_t orig_address
= address
;
3582 address
= align_address(address
, this->addralign());
3584 Input_section_list remaining
;
3585 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3586 p
!= this->input_sections_
.end();
3589 if (p
->is_input_section()
3590 || p
->is_relaxed_input_section()
3591 || p
->is_merge_section())
3592 input_sections
->push_back(*p
);
3595 uint64_t aligned_address
= align_address(address
, p
->addralign());
3596 if (aligned_address
!= address
&& !fill
.empty())
3598 section_size_type length
=
3599 convert_to_section_size_type(aligned_address
- address
);
3600 std::string this_fill
;
3601 this_fill
.reserve(length
);
3602 while (this_fill
.length() + fill
.length() <= length
)
3604 if (this_fill
.length() < length
)
3605 this_fill
.append(fill
, 0, length
- this_fill
.length());
3607 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
3608 remaining
.push_back(Input_section(posd
));
3610 address
= aligned_address
;
3612 remaining
.push_back(*p
);
3614 p
->finalize_data_size();
3615 address
+= p
->data_size();
3619 this->input_sections_
.swap(remaining
);
3620 this->first_input_offset_
= 0;
3622 uint64_t data_size
= address
- orig_address
;
3623 this->set_current_data_size_for_child(data_size
);
3627 // Add a script input section. SIS is an Output_section::Input_section,
3628 // which can be either a plain input section or a special input section like
3629 // a relaxed input section. For a special input section, its size must be
3633 Output_section::add_script_input_section(const Input_section
& sis
)
3635 uint64_t data_size
= sis
.data_size();
3636 uint64_t addralign
= sis
.addralign();
3637 if (addralign
> this->addralign_
)
3638 this->addralign_
= addralign
;
3640 off_t offset_in_section
= this->current_data_size_for_child();
3641 off_t aligned_offset_in_section
= align_address(offset_in_section
,
3644 this->set_current_data_size_for_child(aligned_offset_in_section
3647 this->input_sections_
.push_back(sis
);
3649 // Update fast lookup maps if necessary.
3650 if (this->lookup_maps_
->is_valid())
3652 if (sis
.is_merge_section())
3654 Output_merge_base
* pomb
= sis
.output_merge_base();
3655 Merge_section_properties
msp(pomb
->is_string(), pomb
->entsize(),
3657 this->lookup_maps_
->add_merge_section(msp
, pomb
);
3658 for (Output_merge_base::Input_sections::const_iterator p
=
3659 pomb
->input_sections_begin();
3660 p
!= pomb
->input_sections_end();
3662 this->lookup_maps_
->add_merge_input_section(p
->first
, p
->second
,
3665 else if (sis
.is_relaxed_input_section())
3667 Output_relaxed_input_section
* poris
= sis
.relaxed_input_section();
3668 this->lookup_maps_
->add_relaxed_input_section(poris
->relobj(),
3669 poris
->shndx(), poris
);
3674 // Save states for relaxation.
3677 Output_section::save_states()
3679 gold_assert(this->checkpoint_
== NULL
);
3680 Checkpoint_output_section
* checkpoint
=
3681 new Checkpoint_output_section(this->addralign_
, this->flags_
,
3682 this->input_sections_
,
3683 this->first_input_offset_
,
3684 this->attached_input_sections_are_sorted_
);
3685 this->checkpoint_
= checkpoint
;
3686 gold_assert(this->fills_
.empty());
3690 Output_section::discard_states()
3692 gold_assert(this->checkpoint_
!= NULL
);
3693 delete this->checkpoint_
;
3694 this->checkpoint_
= NULL
;
3695 gold_assert(this->fills_
.empty());
3697 // Simply invalidate the fast lookup maps since we do not keep
3699 this->lookup_maps_
->invalidate();
3703 Output_section::restore_states()
3705 gold_assert(this->checkpoint_
!= NULL
);
3706 Checkpoint_output_section
* checkpoint
= this->checkpoint_
;
3708 this->addralign_
= checkpoint
->addralign();
3709 this->flags_
= checkpoint
->flags();
3710 this->first_input_offset_
= checkpoint
->first_input_offset();
3712 if (!checkpoint
->input_sections_saved())
3714 // If we have not copied the input sections, just resize it.
3715 size_t old_size
= checkpoint
->input_sections_size();
3716 gold_assert(this->input_sections_
.size() >= old_size
);
3717 this->input_sections_
.resize(old_size
);
3721 // We need to copy the whole list. This is not efficient for
3722 // extremely large output with hundreads of thousands of input
3723 // objects. We may need to re-think how we should pass sections
3725 this->input_sections_
= *checkpoint
->input_sections();
3728 this->attached_input_sections_are_sorted_
=
3729 checkpoint
->attached_input_sections_are_sorted();
3731 // Simply invalidate the fast lookup maps since we do not keep
3733 this->lookup_maps_
->invalidate();
3736 // Update the section offsets of input sections in this. This is required if
3737 // relaxation causes some input sections to change sizes.
3740 Output_section::adjust_section_offsets()
3742 if (!this->section_offsets_need_adjustment_
)
3746 for (Input_section_list::iterator p
= this->input_sections_
.begin();
3747 p
!= this->input_sections_
.end();
3750 off
= align_address(off
, p
->addralign());
3751 if (p
->is_input_section())
3752 p
->relobj()->set_section_offset(p
->shndx(), off
);
3753 off
+= p
->data_size();
3756 this->section_offsets_need_adjustment_
= false;
3759 // Print to the map file.
3762 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
3764 mapfile
->print_output_section(this);
3766 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
3767 p
!= this->input_sections_
.end();
3769 p
->print_to_mapfile(mapfile
);
3772 // Print stats for merge sections to stderr.
3775 Output_section::print_merge_stats()
3777 Input_section_list::iterator p
;
3778 for (p
= this->input_sections_
.begin();
3779 p
!= this->input_sections_
.end();
3781 p
->print_merge_stats(this->name_
);
3784 // Set a fixed layout for the section. Used for incremental update links.
3787 Output_section::set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
,
3788 off_t sh_size
, uint64_t sh_addralign
)
3790 this->addralign_
= sh_addralign
;
3791 this->set_current_data_size(sh_size
);
3792 if ((this->flags_
& elfcpp::SHF_ALLOC
) != 0)
3793 this->set_address(sh_addr
);
3794 this->set_file_offset(sh_offset
);
3795 this->finalize_data_size();
3796 this->free_list_
.init(sh_size
, false);
3797 this->has_fixed_layout_
= true;
3800 // Reserve space within the fixed layout for the section. Used for
3801 // incremental update links.
3804 Output_section::reserve(uint64_t sh_offset
, uint64_t sh_size
)
3806 this->free_list_
.remove(sh_offset
, sh_offset
+ sh_size
);
3809 // Allocate space from the free list for the section. Used for
3810 // incremental update links.
3813 Output_section::allocate(off_t len
, uint64_t addralign
)
3815 return this->free_list_
.allocate(len
, addralign
, 0);
3818 // Output segment methods.
3820 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3830 is_max_align_known_(false),
3831 are_addresses_set_(false),
3832 is_large_data_segment_(false)
3834 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3836 if (type
== elfcpp::PT_TLS
)
3837 this->flags_
= elfcpp::PF_R
;
3840 // Add an Output_section to a PT_LOAD Output_segment.
3843 Output_segment::add_output_section_to_load(Layout
* layout
,
3845 elfcpp::Elf_Word seg_flags
)
3847 gold_assert(this->type() == elfcpp::PT_LOAD
);
3848 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
3849 gold_assert(!this->is_max_align_known_
);
3850 gold_assert(os
->is_large_data_section() == this->is_large_data_segment());
3852 this->update_flags_for_output_section(seg_flags
);
3854 // We don't want to change the ordering if we have a linker script
3855 // with a SECTIONS clause.
3856 Output_section_order order
= os
->order();
3857 if (layout
->script_options()->saw_sections_clause())
3858 order
= static_cast<Output_section_order
>(0);
3860 gold_assert(order
!= ORDER_INVALID
);
3862 this->output_lists_
[order
].push_back(os
);
3865 // Add an Output_section to a non-PT_LOAD Output_segment.
3868 Output_segment::add_output_section_to_nonload(Output_section
* os
,
3869 elfcpp::Elf_Word seg_flags
)
3871 gold_assert(this->type() != elfcpp::PT_LOAD
);
3872 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
3873 gold_assert(!this->is_max_align_known_
);
3875 this->update_flags_for_output_section(seg_flags
);
3877 this->output_lists_
[0].push_back(os
);
3880 // Remove an Output_section from this segment. It is an error if it
3884 Output_segment::remove_output_section(Output_section
* os
)
3886 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3888 Output_data_list
* pdl
= &this->output_lists_
[i
];
3889 for (Output_data_list::iterator p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
3901 // Add an Output_data (which need not be an Output_section) to the
3902 // start of a segment.
3905 Output_segment::add_initial_output_data(Output_data
* od
)
3907 gold_assert(!this->is_max_align_known_
);
3908 Output_data_list::iterator p
= this->output_lists_
[0].begin();
3909 this->output_lists_
[0].insert(p
, od
);
3912 // Return true if this segment has any sections which hold actual
3913 // data, rather than being a BSS section.
3916 Output_segment::has_any_data_sections() const
3918 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3920 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3921 for (Output_data_list::const_iterator p
= pdl
->begin();
3925 if (!(*p
)->is_section())
3927 if ((*p
)->output_section()->type() != elfcpp::SHT_NOBITS
)
3934 // Return whether the first data section (not counting TLS sections)
3935 // is a relro section.
3938 Output_segment::is_first_section_relro() const
3940 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3942 if (i
== static_cast<int>(ORDER_TLS_DATA
)
3943 || i
== static_cast<int>(ORDER_TLS_BSS
))
3945 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3948 Output_data
* p
= pdl
->front();
3949 return p
->is_section() && p
->output_section()->is_relro();
3955 // Return the maximum alignment of the Output_data in Output_segment.
3958 Output_segment::maximum_alignment()
3960 if (!this->is_max_align_known_
)
3962 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3964 const Output_data_list
* pdl
= &this->output_lists_
[i
];
3965 uint64_t addralign
= Output_segment::maximum_alignment_list(pdl
);
3966 if (addralign
> this->max_align_
)
3967 this->max_align_
= addralign
;
3969 this->is_max_align_known_
= true;
3972 return this->max_align_
;
3975 // Return the maximum alignment of a list of Output_data.
3978 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
3981 for (Output_data_list::const_iterator p
= pdl
->begin();
3985 uint64_t addralign
= (*p
)->addralign();
3986 if (addralign
> ret
)
3992 // Return whether this segment has any dynamic relocs.
3995 Output_segment::has_dynamic_reloc() const
3997 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
3998 if (this->has_dynamic_reloc_list(&this->output_lists_
[i
]))
4003 // Return whether this Output_data_list has any dynamic relocs.
4006 Output_segment::has_dynamic_reloc_list(const Output_data_list
* pdl
) const
4008 for (Output_data_list::const_iterator p
= pdl
->begin();
4011 if ((*p
)->has_dynamic_reloc())
4016 // Set the section addresses for an Output_segment. If RESET is true,
4017 // reset the addresses first. ADDR is the address and *POFF is the
4018 // file offset. Set the section indexes starting with *PSHNDX.
4019 // INCREASE_RELRO is the size of the portion of the first non-relro
4020 // section that should be included in the PT_GNU_RELRO segment.
4021 // If this segment has relro sections, and has been aligned for
4022 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4023 // the immediately following segment. Update *HAS_RELRO, *POFF,
4027 Output_segment::set_section_addresses(Layout
* layout
, bool reset
,
4029 unsigned int* increase_relro
,
4032 unsigned int* pshndx
)
4034 gold_assert(this->type_
== elfcpp::PT_LOAD
);
4036 uint64_t last_relro_pad
= 0;
4037 off_t orig_off
= *poff
;
4039 bool in_tls
= false;
4041 // If we have relro sections, we need to pad forward now so that the
4042 // relro sections plus INCREASE_RELRO end on a common page boundary.
4043 if (parameters
->options().relro()
4044 && this->is_first_section_relro()
4045 && (!this->are_addresses_set_
|| reset
))
4047 uint64_t relro_size
= 0;
4049 uint64_t max_align
= 0;
4050 for (int i
= 0; i
<= static_cast<int>(ORDER_RELRO_LAST
); ++i
)
4052 Output_data_list
* pdl
= &this->output_lists_
[i
];
4053 Output_data_list::iterator p
;
4054 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
4056 if (!(*p
)->is_section())
4058 uint64_t align
= (*p
)->addralign();
4059 if (align
> max_align
)
4061 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4065 // Align the first non-TLS section to the alignment
4066 // of the TLS segment.
4070 relro_size
= align_address(relro_size
, align
);
4071 // Ignore the size of the .tbss section.
4072 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4073 && (*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4075 if ((*p
)->is_address_valid())
4076 relro_size
+= (*p
)->data_size();
4079 // FIXME: This could be faster.
4080 (*p
)->set_address_and_file_offset(addr
+ relro_size
,
4082 relro_size
+= (*p
)->data_size();
4083 (*p
)->reset_address_and_file_offset();
4086 if (p
!= pdl
->end())
4089 relro_size
+= *increase_relro
;
4090 // Pad the total relro size to a multiple of the maximum
4091 // section alignment seen.
4092 uint64_t aligned_size
= align_address(relro_size
, max_align
);
4093 // Note the amount of padding added after the last relro section.
4094 last_relro_pad
= aligned_size
- relro_size
;
4097 uint64_t page_align
= parameters
->target().common_pagesize();
4099 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4100 uint64_t desired_align
= page_align
- (aligned_size
% page_align
);
4101 if (desired_align
< *poff
% page_align
)
4102 *poff
+= page_align
- *poff
% page_align
;
4103 *poff
+= desired_align
- *poff
% page_align
;
4104 addr
+= *poff
- orig_off
;
4108 if (!reset
&& this->are_addresses_set_
)
4110 gold_assert(this->paddr_
== addr
);
4111 addr
= this->vaddr_
;
4115 this->vaddr_
= addr
;
4116 this->paddr_
= addr
;
4117 this->are_addresses_set_
= true;
4122 this->offset_
= orig_off
;
4126 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4128 if (i
== static_cast<int>(ORDER_RELRO_LAST
))
4130 *poff
+= last_relro_pad
;
4131 addr
+= last_relro_pad
;
4132 if (this->output_lists_
[i
].empty())
4134 // If there is nothing in the ORDER_RELRO_LAST list,
4135 // the padding will occur at the end of the relro
4136 // segment, and we need to add it to *INCREASE_RELRO.
4137 *increase_relro
+= last_relro_pad
;
4140 addr
= this->set_section_list_addresses(layout
, reset
,
4141 &this->output_lists_
[i
],
4142 addr
, poff
, pshndx
, &in_tls
);
4143 if (i
< static_cast<int>(ORDER_SMALL_BSS
))
4145 this->filesz_
= *poff
- orig_off
;
4152 // If the last section was a TLS section, align upward to the
4153 // alignment of the TLS segment, so that the overall size of the TLS
4154 // segment is aligned.
4157 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
4158 *poff
= align_address(*poff
, segment_align
);
4161 this->memsz_
= *poff
- orig_off
;
4163 // Ignore the file offset adjustments made by the BSS Output_data
4170 // Set the addresses and file offsets in a list of Output_data
4174 Output_segment::set_section_list_addresses(Layout
* layout
, bool reset
,
4175 Output_data_list
* pdl
,
4176 uint64_t addr
, off_t
* poff
,
4177 unsigned int* pshndx
,
4180 off_t startoff
= *poff
;
4181 // For incremental updates, we may allocate non-fixed sections from
4182 // free space in the file. This keeps track of the high-water mark.
4183 off_t maxoff
= startoff
;
4185 off_t off
= startoff
;
4186 for (Output_data_list::iterator p
= pdl
->begin();
4191 (*p
)->reset_address_and_file_offset();
4193 // When doing an incremental update or when using a linker script,
4194 // the section will most likely already have an address.
4195 if (!(*p
)->is_address_valid())
4197 uint64_t align
= (*p
)->addralign();
4199 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
4201 // Give the first TLS section the alignment of the
4202 // entire TLS segment. Otherwise the TLS segment as a
4203 // whole may be misaligned.
4206 Output_segment
* tls_segment
= layout
->tls_segment();
4207 gold_assert(tls_segment
!= NULL
);
4208 uint64_t segment_align
= tls_segment
->maximum_alignment();
4209 gold_assert(segment_align
>= align
);
4210 align
= segment_align
;
4217 // If this is the first section after the TLS segment,
4218 // align it to at least the alignment of the TLS
4219 // segment, so that the size of the overall TLS segment
4223 uint64_t segment_align
=
4224 layout
->tls_segment()->maximum_alignment();
4225 if (segment_align
> align
)
4226 align
= segment_align
;
4232 if (!parameters
->incremental_update())
4234 off
= align_address(off
, align
);
4235 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4239 // Incremental update: allocate file space from free list.
4240 (*p
)->pre_finalize_data_size();
4241 off_t current_size
= (*p
)->current_data_size();
4242 off
= layout
->allocate(current_size
, align
, startoff
);
4245 gold_assert((*p
)->output_section() != NULL
);
4246 gold_fallback(_("out of patch space for section %s; "
4247 "relink with --incremental-full"),
4248 (*p
)->output_section()->name());
4250 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
4251 if ((*p
)->data_size() > current_size
)
4253 gold_assert((*p
)->output_section() != NULL
);
4254 gold_fallback(_("%s: section changed size; "
4255 "relink with --incremental-full"),
4256 (*p
)->output_section()->name());
4260 else if (parameters
->incremental_update())
4262 // For incremental updates, use the fixed offset for the
4263 // high-water mark computation.
4264 off
= (*p
)->offset();
4268 // The script may have inserted a skip forward, but it
4269 // better not have moved backward.
4270 if ((*p
)->address() >= addr
+ (off
- startoff
))
4271 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
4274 if (!layout
->script_options()->saw_sections_clause())
4278 Output_section
* os
= (*p
)->output_section();
4280 // Cast to unsigned long long to avoid format warnings.
4281 unsigned long long previous_dot
=
4282 static_cast<unsigned long long>(addr
+ (off
- startoff
));
4283 unsigned long long dot
=
4284 static_cast<unsigned long long>((*p
)->address());
4287 gold_error(_("dot moves backward in linker script "
4288 "from 0x%llx to 0x%llx"), previous_dot
, dot
);
4290 gold_error(_("address of section '%s' moves backward "
4291 "from 0x%llx to 0x%llx"),
4292 os
->name(), previous_dot
, dot
);
4295 (*p
)->set_file_offset(off
);
4296 (*p
)->finalize_data_size();
4299 if (parameters
->incremental_update())
4300 gold_debug(DEBUG_INCREMENTAL
,
4301 "set_section_list_addresses: %08lx %08lx %s",
4302 static_cast<long>(off
),
4303 static_cast<long>((*p
)->data_size()),
4304 ((*p
)->output_section() != NULL
4305 ? (*p
)->output_section()->name() : "(special)"));
4307 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4308 // section. Such a section does not affect the size of a
4310 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
4311 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
4312 off
+= (*p
)->data_size();
4317 if ((*p
)->is_section())
4319 (*p
)->set_out_shndx(*pshndx
);
4325 return addr
+ (maxoff
- startoff
);
4328 // For a non-PT_LOAD segment, set the offset from the sections, if
4329 // any. Add INCREASE to the file size and the memory size.
4332 Output_segment::set_offset(unsigned int increase
)
4334 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
4336 gold_assert(!this->are_addresses_set_
);
4338 // A non-load section only uses output_lists_[0].
4340 Output_data_list
* pdl
= &this->output_lists_
[0];
4344 gold_assert(increase
== 0);
4347 this->are_addresses_set_
= true;
4349 this->min_p_align_
= 0;
4355 // Find the first and last section by address.
4356 const Output_data
* first
= NULL
;
4357 const Output_data
* last_data
= NULL
;
4358 const Output_data
* last_bss
= NULL
;
4359 for (Output_data_list::const_iterator p
= pdl
->begin();
4364 || (*p
)->address() < first
->address()
4365 || ((*p
)->address() == first
->address()
4366 && (*p
)->data_size() < first
->data_size()))
4368 const Output_data
** plast
;
4369 if ((*p
)->is_section()
4370 && (*p
)->output_section()->type() == elfcpp::SHT_NOBITS
)
4375 || (*p
)->address() > (*plast
)->address()
4376 || ((*p
)->address() == (*plast
)->address()
4377 && (*p
)->data_size() > (*plast
)->data_size()))
4381 this->vaddr_
= first
->address();
4382 this->paddr_
= (first
->has_load_address()
4383 ? first
->load_address()
4385 this->are_addresses_set_
= true;
4386 this->offset_
= first
->offset();
4388 if (last_data
== NULL
)
4391 this->filesz_
= (last_data
->address()
4392 + last_data
->data_size()
4395 const Output_data
* last
= last_bss
!= NULL
? last_bss
: last_data
;
4396 this->memsz_
= (last
->address()
4400 this->filesz_
+= increase
;
4401 this->memsz_
+= increase
;
4403 // If this is a RELRO segment, verify that the segment ends at a
4405 if (this->type_
== elfcpp::PT_GNU_RELRO
)
4407 uint64_t page_align
= parameters
->target().common_pagesize();
4408 uint64_t segment_end
= this->vaddr_
+ this->memsz_
;
4409 if (parameters
->incremental_update())
4411 // The INCREASE_RELRO calculation is bypassed for an incremental
4412 // update, so we need to adjust the segment size manually here.
4413 segment_end
= align_address(segment_end
, page_align
);
4414 this->memsz_
= segment_end
- this->vaddr_
;
4417 gold_assert(segment_end
== align_address(segment_end
, page_align
));
4420 // If this is a TLS segment, align the memory size. The code in
4421 // set_section_list ensures that the section after the TLS segment
4422 // is aligned to give us room.
4423 if (this->type_
== elfcpp::PT_TLS
)
4425 uint64_t segment_align
= this->maximum_alignment();
4426 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
4427 this->memsz_
= align_address(this->memsz_
, segment_align
);
4431 // Set the TLS offsets of the sections in the PT_TLS segment.
4434 Output_segment::set_tls_offsets()
4436 gold_assert(this->type_
== elfcpp::PT_TLS
);
4438 for (Output_data_list::iterator p
= this->output_lists_
[0].begin();
4439 p
!= this->output_lists_
[0].end();
4441 (*p
)->set_tls_offset(this->vaddr_
);
4444 // Return the load address of the first section.
4447 Output_segment::first_section_load_address() const
4449 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4451 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4452 for (Output_data_list::const_iterator p
= pdl
->begin();
4456 if ((*p
)->is_section())
4457 return ((*p
)->has_load_address()
4458 ? (*p
)->load_address()
4465 // Return the number of Output_sections in an Output_segment.
4468 Output_segment::output_section_count() const
4470 unsigned int ret
= 0;
4471 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4472 ret
+= this->output_section_count_list(&this->output_lists_
[i
]);
4476 // Return the number of Output_sections in an Output_data_list.
4479 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
4481 unsigned int count
= 0;
4482 for (Output_data_list::const_iterator p
= pdl
->begin();
4486 if ((*p
)->is_section())
4492 // Return the section attached to the list segment with the lowest
4493 // load address. This is used when handling a PHDRS clause in a
4497 Output_segment::section_with_lowest_load_address() const
4499 Output_section
* found
= NULL
;
4500 uint64_t found_lma
= 0;
4501 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4502 this->lowest_load_address_in_list(&this->output_lists_
[i
], &found
,
4507 // Look through a list for a section with a lower load address.
4510 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
4511 Output_section
** found
,
4512 uint64_t* found_lma
) const
4514 for (Output_data_list::const_iterator p
= pdl
->begin();
4518 if (!(*p
)->is_section())
4520 Output_section
* os
= static_cast<Output_section
*>(*p
);
4521 uint64_t lma
= (os
->has_load_address()
4522 ? os
->load_address()
4524 if (*found
== NULL
|| lma
< *found_lma
)
4532 // Write the segment data into *OPHDR.
4534 template<int size
, bool big_endian
>
4536 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
4538 ophdr
->put_p_type(this->type_
);
4539 ophdr
->put_p_offset(this->offset_
);
4540 ophdr
->put_p_vaddr(this->vaddr_
);
4541 ophdr
->put_p_paddr(this->paddr_
);
4542 ophdr
->put_p_filesz(this->filesz_
);
4543 ophdr
->put_p_memsz(this->memsz_
);
4544 ophdr
->put_p_flags(this->flags_
);
4545 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
4548 // Write the section headers into V.
4550 template<int size
, bool big_endian
>
4552 Output_segment::write_section_headers(const Layout
* layout
,
4553 const Stringpool
* secnamepool
,
4555 unsigned int* pshndx
) const
4557 // Every section that is attached to a segment must be attached to a
4558 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4560 if (this->type_
!= elfcpp::PT_LOAD
)
4563 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4565 const Output_data_list
* pdl
= &this->output_lists_
[i
];
4566 v
= this->write_section_headers_list
<size
, big_endian
>(layout
,
4575 template<int size
, bool big_endian
>
4577 Output_segment::write_section_headers_list(const Layout
* layout
,
4578 const Stringpool
* secnamepool
,
4579 const Output_data_list
* pdl
,
4581 unsigned int* pshndx
) const
4583 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
4584 for (Output_data_list::const_iterator p
= pdl
->begin();
4588 if ((*p
)->is_section())
4590 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
4591 gold_assert(*pshndx
== ps
->out_shndx());
4592 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
4593 ps
->write_header(layout
, secnamepool
, &oshdr
);
4601 // Print the output sections to the map file.
4604 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
4606 if (this->type() != elfcpp::PT_LOAD
)
4608 for (int i
= 0; i
< static_cast<int>(ORDER_MAX
); ++i
)
4609 this->print_section_list_to_mapfile(mapfile
, &this->output_lists_
[i
]);
4612 // Print an output section list to the map file.
4615 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
4616 const Output_data_list
* pdl
) const
4618 for (Output_data_list::const_iterator p
= pdl
->begin();
4621 (*p
)->print_to_mapfile(mapfile
);
4624 // Output_file methods.
4626 Output_file::Output_file(const char* name
)
4631 map_is_anonymous_(false),
4632 map_is_allocated_(false),
4633 is_temporary_(false)
4637 // Try to open an existing file. Returns false if the file doesn't
4638 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4639 // NULL, open that file as the base for incremental linking, and
4640 // copy its contents to the new output file. This routine can
4641 // be called for incremental updates, in which case WRITABLE should
4642 // be true, or by the incremental-dump utility, in which case
4643 // WRITABLE should be false.
4646 Output_file::open_base_file(const char* base_name
, bool writable
)
4648 // The name "-" means "stdout".
4649 if (strcmp(this->name_
, "-") == 0)
4652 bool use_base_file
= base_name
!= NULL
;
4654 base_name
= this->name_
;
4655 else if (strcmp(base_name
, this->name_
) == 0)
4656 gold_fatal(_("%s: incremental base and output file name are the same"),
4659 // Don't bother opening files with a size of zero.
4661 if (::stat(base_name
, &s
) != 0)
4663 gold_info(_("%s: stat: %s"), base_name
, strerror(errno
));
4668 gold_info(_("%s: incremental base file is empty"), base_name
);
4672 // If we're using a base file, we want to open it read-only.
4676 int oflags
= writable
? O_RDWR
: O_RDONLY
;
4677 int o
= open_descriptor(-1, base_name
, oflags
, 0);
4680 gold_info(_("%s: open: %s"), base_name
, strerror(errno
));
4684 // If the base file and the output file are different, open a
4685 // new output file and read the contents from the base file into
4686 // the newly-mapped region.
4689 this->open(s
.st_size
);
4690 ssize_t len
= ::read(o
, this->base_
, s
.st_size
);
4693 gold_info(_("%s: read failed: %s"), base_name
, strerror(errno
));
4696 if (len
< s
.st_size
)
4698 gold_info(_("%s: file too short"), base_name
);
4706 this->file_size_
= s
.st_size
;
4708 if (!this->map_no_anonymous(writable
))
4710 release_descriptor(o
, true);
4712 this->file_size_
= 0;
4719 // Open the output file.
4722 Output_file::open(off_t file_size
)
4724 this->file_size_
= file_size
;
4726 // Unlink the file first; otherwise the open() may fail if the file
4727 // is busy (e.g. it's an executable that's currently being executed).
4729 // However, the linker may be part of a system where a zero-length
4730 // file is created for it to write to, with tight permissions (gcc
4731 // 2.95 did something like this). Unlinking the file would work
4732 // around those permission controls, so we only unlink if the file
4733 // has a non-zero size. We also unlink only regular files to avoid
4734 // trouble with directories/etc.
4736 // If we fail, continue; this command is merely a best-effort attempt
4737 // to improve the odds for open().
4739 // We let the name "-" mean "stdout"
4740 if (!this->is_temporary_
)
4742 if (strcmp(this->name_
, "-") == 0)
4743 this->o_
= STDOUT_FILENO
;
4747 if (::stat(this->name_
, &s
) == 0
4748 && (S_ISREG (s
.st_mode
) || S_ISLNK (s
.st_mode
)))
4751 ::unlink(this->name_
);
4752 else if (!parameters
->options().relocatable())
4754 // If we don't unlink the existing file, add execute
4755 // permission where read permissions already exist
4756 // and where the umask permits.
4757 int mask
= ::umask(0);
4759 s
.st_mode
|= (s
.st_mode
& 0444) >> 2;
4760 ::chmod(this->name_
, s
.st_mode
& ~mask
);
4764 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
4765 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
4768 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
4776 // Resize the output file.
4779 Output_file::resize(off_t file_size
)
4781 // If the mmap is mapping an anonymous memory buffer, this is easy:
4782 // just mremap to the new size. If it's mapping to a file, we want
4783 // to unmap to flush to the file, then remap after growing the file.
4784 if (this->map_is_anonymous_
)
4787 if (!this->map_is_allocated_
)
4789 base
= ::mremap(this->base_
, this->file_size_
, file_size
,
4791 if (base
== MAP_FAILED
)
4792 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
4796 base
= realloc(this->base_
, file_size
);
4799 if (file_size
> this->file_size_
)
4800 memset(static_cast<char*>(base
) + this->file_size_
, 0,
4801 file_size
- this->file_size_
);
4803 this->base_
= static_cast<unsigned char*>(base
);
4804 this->file_size_
= file_size
;
4809 this->file_size_
= file_size
;
4810 if (!this->map_no_anonymous(true))
4811 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
4815 // Map an anonymous block of memory which will later be written to the
4816 // file. Return whether the map succeeded.
4819 Output_file::map_anonymous()
4821 void* base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
4822 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
4823 if (base
== MAP_FAILED
)
4825 base
= malloc(this->file_size_
);
4828 memset(base
, 0, this->file_size_
);
4829 this->map_is_allocated_
= true;
4831 this->base_
= static_cast<unsigned char*>(base
);
4832 this->map_is_anonymous_
= true;
4836 // Map the file into memory. Return whether the mapping succeeded.
4837 // If WRITABLE is true, map with write access.
4840 Output_file::map_no_anonymous(bool writable
)
4842 const int o
= this->o_
;
4844 // If the output file is not a regular file, don't try to mmap it;
4845 // instead, we'll mmap a block of memory (an anonymous buffer), and
4846 // then later write the buffer to the file.
4848 struct stat statbuf
;
4849 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
4850 || ::fstat(o
, &statbuf
) != 0
4851 || !S_ISREG(statbuf
.st_mode
)
4852 || this->is_temporary_
)
4855 // Ensure that we have disk space available for the file. If we
4856 // don't do this, it is possible that we will call munmap, close,
4857 // and exit with dirty buffers still in the cache with no assigned
4858 // disk blocks. If the disk is out of space at that point, the
4859 // output file will wind up incomplete, but we will have already
4860 // exited. The alternative to fallocate would be to use fdatasync,
4861 // but that would be a more significant performance hit.
4862 if (writable
&& ::posix_fallocate(o
, 0, this->file_size_
) < 0)
4863 gold_fatal(_("%s: %s"), this->name_
, strerror(errno
));
4865 // Map the file into memory.
4866 int prot
= PROT_READ
;
4869 base
= ::mmap(NULL
, this->file_size_
, prot
, MAP_SHARED
, o
, 0);
4871 // The mmap call might fail because of file system issues: the file
4872 // system might not support mmap at all, or it might not support
4873 // mmap with PROT_WRITE.
4874 if (base
== MAP_FAILED
)
4877 this->map_is_anonymous_
= false;
4878 this->base_
= static_cast<unsigned char*>(base
);
4882 // Map the file into memory.
4887 if (this->map_no_anonymous(true))
4890 // The mmap call might fail because of file system issues: the file
4891 // system might not support mmap at all, or it might not support
4892 // mmap with PROT_WRITE. I'm not sure which errno values we will
4893 // see in all cases, so if the mmap fails for any reason and we
4894 // don't care about file contents, try for an anonymous map.
4895 if (this->map_anonymous())
4898 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4899 this->name_
, static_cast<unsigned long>(this->file_size_
),
4903 // Unmap the file from memory.
4906 Output_file::unmap()
4908 if (this->map_is_anonymous_
)
4910 // We've already written out the data, so there is no reason to
4911 // waste time unmapping or freeing the memory.
4915 if (::munmap(this->base_
, this->file_size_
) < 0)
4916 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
4921 // Close the output file.
4924 Output_file::close()
4926 // If the map isn't file-backed, we need to write it now.
4927 if (this->map_is_anonymous_
&& !this->is_temporary_
)
4929 size_t bytes_to_write
= this->file_size_
;
4931 while (bytes_to_write
> 0)
4933 ssize_t bytes_written
= ::write(this->o_
, this->base_
+ offset
,
4935 if (bytes_written
== 0)
4936 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
4937 else if (bytes_written
< 0)
4938 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
4941 bytes_to_write
-= bytes_written
;
4942 offset
+= bytes_written
;
4948 // We don't close stdout or stderr
4949 if (this->o_
!= STDOUT_FILENO
4950 && this->o_
!= STDERR_FILENO
4951 && !this->is_temporary_
)
4952 if (::close(this->o_
) < 0)
4953 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
4957 // Instantiate the templates we need. We could use the configure
4958 // script to restrict this to only the ones for implemented targets.
4960 #ifdef HAVE_TARGET_32_LITTLE
4963 Output_section::add_input_section
<32, false>(
4965 Sized_relobj_file
<32, false>* object
,
4967 const char* secname
,
4968 const elfcpp::Shdr
<32, false>& shdr
,
4969 unsigned int reloc_shndx
,
4970 bool have_sections_script
);
4973 #ifdef HAVE_TARGET_32_BIG
4976 Output_section::add_input_section
<32, true>(
4978 Sized_relobj_file
<32, true>* object
,
4980 const char* secname
,
4981 const elfcpp::Shdr
<32, true>& shdr
,
4982 unsigned int reloc_shndx
,
4983 bool have_sections_script
);
4986 #ifdef HAVE_TARGET_64_LITTLE
4989 Output_section::add_input_section
<64, false>(
4991 Sized_relobj_file
<64, false>* object
,
4993 const char* secname
,
4994 const elfcpp::Shdr
<64, false>& shdr
,
4995 unsigned int reloc_shndx
,
4996 bool have_sections_script
);
4999 #ifdef HAVE_TARGET_64_BIG
5002 Output_section::add_input_section
<64, true>(
5004 Sized_relobj_file
<64, true>* object
,
5006 const char* secname
,
5007 const elfcpp::Shdr
<64, true>& shdr
,
5008 unsigned int reloc_shndx
,
5009 bool have_sections_script
);
5012 #ifdef HAVE_TARGET_32_LITTLE
5014 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5017 #ifdef HAVE_TARGET_32_BIG
5019 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5022 #ifdef HAVE_TARGET_64_LITTLE
5024 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5027 #ifdef HAVE_TARGET_64_BIG
5029 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5032 #ifdef HAVE_TARGET_32_LITTLE
5034 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5037 #ifdef HAVE_TARGET_32_BIG
5039 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5042 #ifdef HAVE_TARGET_64_LITTLE
5044 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5047 #ifdef HAVE_TARGET_64_BIG
5049 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5052 #ifdef HAVE_TARGET_32_LITTLE
5054 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5057 #ifdef HAVE_TARGET_32_BIG
5059 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5062 #ifdef HAVE_TARGET_64_LITTLE
5064 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5067 #ifdef HAVE_TARGET_64_BIG
5069 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5072 #ifdef HAVE_TARGET_32_LITTLE
5074 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5077 #ifdef HAVE_TARGET_32_BIG
5079 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5082 #ifdef HAVE_TARGET_64_LITTLE
5084 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5087 #ifdef HAVE_TARGET_64_BIG
5089 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5092 #ifdef HAVE_TARGET_32_LITTLE
5094 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
5097 #ifdef HAVE_TARGET_32_BIG
5099 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
5102 #ifdef HAVE_TARGET_64_LITTLE
5104 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
5107 #ifdef HAVE_TARGET_64_BIG
5109 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
5112 #ifdef HAVE_TARGET_32_LITTLE
5114 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
5117 #ifdef HAVE_TARGET_32_BIG
5119 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
5122 #ifdef HAVE_TARGET_64_LITTLE
5124 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
5127 #ifdef HAVE_TARGET_64_BIG
5129 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
5132 #ifdef HAVE_TARGET_32_LITTLE
5134 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
5137 #ifdef HAVE_TARGET_32_BIG
5139 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
5142 #ifdef HAVE_TARGET_64_LITTLE
5144 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
5147 #ifdef HAVE_TARGET_64_BIG
5149 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
5152 #ifdef HAVE_TARGET_32_LITTLE
5154 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
5157 #ifdef HAVE_TARGET_32_BIG
5159 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
5162 #ifdef HAVE_TARGET_64_LITTLE
5164 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
5167 #ifdef HAVE_TARGET_64_BIG
5169 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
5172 #ifdef HAVE_TARGET_32_LITTLE
5174 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
5177 #ifdef HAVE_TARGET_32_BIG
5179 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
5182 #ifdef HAVE_TARGET_64_LITTLE
5184 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
5187 #ifdef HAVE_TARGET_64_BIG
5189 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
5192 #ifdef HAVE_TARGET_32_LITTLE
5194 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
5197 #ifdef HAVE_TARGET_32_BIG
5199 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
5202 #ifdef HAVE_TARGET_64_LITTLE
5204 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
5207 #ifdef HAVE_TARGET_64_BIG
5209 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
5212 #ifdef HAVE_TARGET_32_LITTLE
5214 class Output_data_group
<32, false>;
5217 #ifdef HAVE_TARGET_32_BIG
5219 class Output_data_group
<32, true>;
5222 #ifdef HAVE_TARGET_64_LITTLE
5224 class Output_data_group
<64, false>;
5227 #ifdef HAVE_TARGET_64_BIG
5229 class Output_data_group
<64, true>;
5232 #ifdef HAVE_TARGET_32_LITTLE
5234 class Output_data_got
<32, false>;
5237 #ifdef HAVE_TARGET_32_BIG
5239 class Output_data_got
<32, true>;
5242 #ifdef HAVE_TARGET_64_LITTLE
5244 class Output_data_got
<64, false>;
5247 #ifdef HAVE_TARGET_64_BIG
5249 class Output_data_got
<64, true>;
5252 } // End namespace gold.