1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "libiberty.h"
36 #include "parameters.h"
40 #include "script-sections.h"
45 #include "compressed_output.h"
46 #include "reduced_debug_output.h"
48 #include "descriptors.h"
50 #include "incremental.h"
56 // Layout::Relaxation_debug_check methods.
58 // Check that sections and special data are in reset states.
59 // We do not save states for Output_sections and special Output_data.
60 // So we check that they have not assigned any addresses or offsets.
61 // clean_up_after_relaxation simply resets their addresses and offsets.
63 Layout::Relaxation_debug_check::check_output_data_for_reset_values(
64 const Layout::Section_list
& sections
,
65 const Layout::Data_list
& special_outputs
)
67 for(Layout::Section_list::const_iterator p
= sections
.begin();
70 gold_assert((*p
)->address_and_file_offset_have_reset_values());
72 for(Layout::Data_list::const_iterator p
= special_outputs
.begin();
73 p
!= special_outputs
.end();
75 gold_assert((*p
)->address_and_file_offset_have_reset_values());
78 // Save information of SECTIONS for checking later.
81 Layout::Relaxation_debug_check::read_sections(
82 const Layout::Section_list
& sections
)
84 for(Layout::Section_list::const_iterator p
= sections
.begin();
88 Output_section
* os
= *p
;
90 info
.output_section
= os
;
91 info
.address
= os
->is_address_valid() ? os
->address() : 0;
92 info
.data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
93 info
.offset
= os
->is_offset_valid()? os
->offset() : -1 ;
94 this->section_infos_
.push_back(info
);
98 // Verify SECTIONS using previously recorded information.
101 Layout::Relaxation_debug_check::verify_sections(
102 const Layout::Section_list
& sections
)
105 for(Layout::Section_list::const_iterator p
= sections
.begin();
109 Output_section
* os
= *p
;
110 uint64_t address
= os
->is_address_valid() ? os
->address() : 0;
111 off_t data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
112 off_t offset
= os
->is_offset_valid()? os
->offset() : -1 ;
114 if (i
>= this->section_infos_
.size())
116 gold_fatal("Section_info of %s missing.\n", os
->name());
118 const Section_info
& info
= this->section_infos_
[i
];
119 if (os
!= info
.output_section
)
120 gold_fatal("Section order changed. Expecting %s but see %s\n",
121 info
.output_section
->name(), os
->name());
122 if (address
!= info
.address
123 || data_size
!= info
.data_size
124 || offset
!= info
.offset
)
125 gold_fatal("Section %s changed.\n", os
->name());
129 // Layout_task_runner methods.
131 // Lay out the sections. This is called after all the input objects
135 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
137 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
142 // Now we know the final size of the output file and we know where
143 // each piece of information goes.
145 if (this->mapfile_
!= NULL
)
147 this->mapfile_
->print_discarded_sections(this->input_objects_
);
148 this->layout_
->print_to_mapfile(this->mapfile_
);
151 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
152 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
153 of
->set_is_temporary();
156 // Queue up the final set of tasks.
157 gold::queue_final_tasks(this->options_
, this->input_objects_
,
158 this->symtab_
, this->layout_
, workqueue
, of
);
163 Layout::Layout(int number_of_input_files
, Script_options
* script_options
)
164 : number_of_input_files_(number_of_input_files
),
165 script_options_(script_options
),
173 unattached_section_list_(),
174 special_output_list_(),
175 section_headers_(NULL
),
177 relro_segment_(NULL
),
179 symtab_section_(NULL
),
180 symtab_xindex_(NULL
),
181 dynsym_section_(NULL
),
182 dynsym_xindex_(NULL
),
183 dynamic_section_(NULL
),
184 dynamic_symbol_(NULL
),
186 eh_frame_section_(NULL
),
187 eh_frame_data_(NULL
),
188 added_eh_frame_data_(false),
189 eh_frame_hdr_section_(NULL
),
190 build_id_note_(NULL
),
194 output_file_size_(-1),
195 sections_are_attached_(false),
196 input_requires_executable_stack_(false),
197 input_with_gnu_stack_note_(false),
198 input_without_gnu_stack_note_(false),
199 has_static_tls_(false),
200 any_postprocessing_sections_(false),
201 resized_signatures_(false),
202 have_stabstr_section_(false),
203 incremental_inputs_(NULL
),
204 record_output_section_data_from_script_(false),
205 script_output_section_data_list_(),
206 segment_states_(NULL
),
207 relaxation_debug_check_(NULL
)
209 // Make space for more than enough segments for a typical file.
210 // This is just for efficiency--it's OK if we wind up needing more.
211 this->segment_list_
.reserve(12);
213 // We expect two unattached Output_data objects: the file header and
214 // the segment headers.
215 this->special_output_list_
.reserve(2);
217 // Initialize structure needed for an incremental build.
218 if (parameters
->options().incremental())
219 this->incremental_inputs_
= new Incremental_inputs
;
221 // The section name pool is worth optimizing in all cases, because
222 // it is small, but there are often overlaps due to .rel sections.
223 this->namepool_
.set_optimize();
226 // Hash a key we use to look up an output section mapping.
229 Layout::Hash_key::operator()(const Layout::Key
& k
) const
231 return k
.first
+ k
.second
.first
+ k
.second
.second
;
234 // Returns whether the given section is in the list of
235 // debug-sections-used-by-some-version-of-gdb. Currently,
236 // we've checked versions of gdb up to and including 6.7.1.
238 static const char* gdb_sections
[] =
240 // ".debug_aranges", // not used by gdb as of 6.7.1
246 // ".debug_pubnames", // not used by gdb as of 6.7.1
251 static const char* lines_only_debug_sections
[] =
253 // ".debug_aranges", // not used by gdb as of 6.7.1
259 // ".debug_pubnames", // not used by gdb as of 6.7.1
265 is_gdb_debug_section(const char* str
)
267 // We can do this faster: binary search or a hashtable. But why bother?
268 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
269 if (strcmp(str
, gdb_sections
[i
]) == 0)
275 is_lines_only_debug_section(const char* str
)
277 // We can do this faster: binary search or a hashtable. But why bother?
279 i
< sizeof(lines_only_debug_sections
)/sizeof(*lines_only_debug_sections
);
281 if (strcmp(str
, lines_only_debug_sections
[i
]) == 0)
286 // Whether to include this section in the link.
288 template<int size
, bool big_endian
>
290 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
291 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
293 if (shdr
.get_sh_flags() & elfcpp::SHF_EXCLUDE
)
296 switch (shdr
.get_sh_type())
298 case elfcpp::SHT_NULL
:
299 case elfcpp::SHT_SYMTAB
:
300 case elfcpp::SHT_DYNSYM
:
301 case elfcpp::SHT_HASH
:
302 case elfcpp::SHT_DYNAMIC
:
303 case elfcpp::SHT_SYMTAB_SHNDX
:
306 case elfcpp::SHT_STRTAB
:
307 // Discard the sections which have special meanings in the ELF
308 // ABI. Keep others (e.g., .stabstr). We could also do this by
309 // checking the sh_link fields of the appropriate sections.
310 return (strcmp(name
, ".dynstr") != 0
311 && strcmp(name
, ".strtab") != 0
312 && strcmp(name
, ".shstrtab") != 0);
314 case elfcpp::SHT_RELA
:
315 case elfcpp::SHT_REL
:
316 case elfcpp::SHT_GROUP
:
317 // If we are emitting relocations these should be handled
319 gold_assert(!parameters
->options().relocatable()
320 && !parameters
->options().emit_relocs());
323 case elfcpp::SHT_PROGBITS
:
324 if (parameters
->options().strip_debug()
325 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
327 if (is_debug_info_section(name
))
330 if (parameters
->options().strip_debug_non_line()
331 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
333 // Debugging sections can only be recognized by name.
334 if (is_prefix_of(".debug", name
)
335 && !is_lines_only_debug_section(name
))
338 if (parameters
->options().strip_debug_gdb()
339 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
341 // Debugging sections can only be recognized by name.
342 if (is_prefix_of(".debug", name
)
343 && !is_gdb_debug_section(name
))
346 if (parameters
->options().strip_lto_sections()
347 && !parameters
->options().relocatable()
348 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
350 // Ignore LTO sections containing intermediate code.
351 if (is_prefix_of(".gnu.lto_", name
))
361 // Return an output section named NAME, or NULL if there is none.
364 Layout::find_output_section(const char* name
) const
366 for (Section_list::const_iterator p
= this->section_list_
.begin();
367 p
!= this->section_list_
.end();
369 if (strcmp((*p
)->name(), name
) == 0)
374 // Return an output segment of type TYPE, with segment flags SET set
375 // and segment flags CLEAR clear. Return NULL if there is none.
378 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
379 elfcpp::Elf_Word clear
) const
381 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
382 p
!= this->segment_list_
.end();
384 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
385 && ((*p
)->flags() & set
) == set
386 && ((*p
)->flags() & clear
) == 0)
391 // Return the output section to use for section NAME with type TYPE
392 // and section flags FLAGS. NAME must be canonicalized in the string
393 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
394 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
395 // is used by the dynamic linker. IS_RELRO is true for a relro
396 // section. IS_LAST_RELRO is true for the last relro section.
397 // IS_FIRST_NON_RELRO is true for the first non-relro section.
400 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
401 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
402 bool is_interp
, bool is_dynamic_linker_section
,
403 bool is_relro
, bool is_last_relro
,
404 bool is_first_non_relro
)
406 elfcpp::Elf_Xword lookup_flags
= flags
;
408 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
409 // read-write with read-only sections. Some other ELF linkers do
410 // not do this. FIXME: Perhaps there should be an option
412 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
414 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
415 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
416 std::pair
<Section_name_map::iterator
, bool> ins(
417 this->section_name_map_
.insert(v
));
420 return ins
.first
->second
;
423 // This is the first time we've seen this name/type/flags
424 // combination. For compatibility with the GNU linker, we
425 // combine sections with contents and zero flags with sections
426 // with non-zero flags. This is a workaround for cases where
427 // assembler code forgets to set section flags. FIXME: Perhaps
428 // there should be an option to control this.
429 Output_section
* os
= NULL
;
431 if (type
== elfcpp::SHT_PROGBITS
)
435 Output_section
* same_name
= this->find_output_section(name
);
436 if (same_name
!= NULL
437 && same_name
->type() == elfcpp::SHT_PROGBITS
438 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
441 else if ((flags
& elfcpp::SHF_TLS
) == 0)
443 elfcpp::Elf_Xword zero_flags
= 0;
444 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
445 Section_name_map::iterator p
=
446 this->section_name_map_
.find(zero_key
);
447 if (p
!= this->section_name_map_
.end())
453 os
= this->make_output_section(name
, type
, flags
, is_interp
,
454 is_dynamic_linker_section
, is_relro
,
455 is_last_relro
, is_first_non_relro
);
456 ins
.first
->second
= os
;
461 // Pick the output section to use for section NAME, in input file
462 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
463 // linker created section. IS_INPUT_SECTION is true if we are
464 // choosing an output section for an input section found in a input
465 // file. IS_INTERP is true if this is the .interp section.
466 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
467 // dynamic linker. IS_RELRO is true for a relro section.
468 // IS_LAST_RELRO is true for the last relro section.
469 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
470 // will return NULL if the input section should be discarded.
473 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
474 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
475 bool is_input_section
, bool is_interp
,
476 bool is_dynamic_linker_section
, bool is_relro
,
477 bool is_last_relro
, bool is_first_non_relro
)
479 // We should not see any input sections after we have attached
480 // sections to segments.
481 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
483 // Some flags in the input section should not be automatically
484 // copied to the output section.
485 flags
&= ~ (elfcpp::SHF_INFO_LINK
486 | elfcpp::SHF_LINK_ORDER
489 | elfcpp::SHF_STRINGS
);
491 if (this->script_options_
->saw_sections_clause())
493 // We are using a SECTIONS clause, so the output section is
494 // chosen based only on the name.
496 Script_sections
* ss
= this->script_options_
->script_sections();
497 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
498 Output_section
** output_section_slot
;
499 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
502 // The SECTIONS clause says to discard this input section.
506 // If this is an orphan section--one not mentioned in the linker
507 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
508 // default processing below.
510 if (output_section_slot
!= NULL
)
512 if (*output_section_slot
!= NULL
)
514 (*output_section_slot
)->update_flags_for_input_section(flags
);
515 return *output_section_slot
;
518 // We don't put sections found in the linker script into
519 // SECTION_NAME_MAP_. That keeps us from getting confused
520 // if an orphan section is mapped to a section with the same
521 // name as one in the linker script.
523 name
= this->namepool_
.add(name
, false, NULL
);
526 this->make_output_section(name
, type
, flags
, is_interp
,
527 is_dynamic_linker_section
, is_relro
,
528 is_last_relro
, is_first_non_relro
);
529 os
->set_found_in_sections_clause();
530 *output_section_slot
= os
;
535 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
537 // Turn NAME from the name of the input section into the name of the
540 size_t len
= strlen(name
);
542 && !this->script_options_
->saw_sections_clause()
543 && !parameters
->options().relocatable())
544 name
= Layout::output_section_name(name
, &len
);
546 Stringpool::Key name_key
;
547 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
549 // Find or make the output section. The output section is selected
550 // based on the section name, type, and flags.
551 return this->get_output_section(name
, name_key
, type
, flags
, is_interp
,
552 is_dynamic_linker_section
, is_relro
,
553 is_last_relro
, is_first_non_relro
);
556 // Return the output section to use for input section SHNDX, with name
557 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
558 // index of a relocation section which applies to this section, or 0
559 // if none, or -1U if more than one. RELOC_TYPE is the type of the
560 // relocation section if there is one. Set *OFF to the offset of this
561 // input section without the output section. Return NULL if the
562 // section should be discarded. Set *OFF to -1 if the section
563 // contents should not be written directly to the output file, but
564 // will instead receive special handling.
566 template<int size
, bool big_endian
>
568 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
569 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
570 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
574 if (!this->include_section(object
, name
, shdr
))
579 // In a relocatable link a grouped section must not be combined with
580 // any other sections.
581 if (parameters
->options().relocatable()
582 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
584 name
= this->namepool_
.add(name
, true, NULL
);
585 os
= this->make_output_section(name
, shdr
.get_sh_type(),
586 shdr
.get_sh_flags(), false, false,
587 false, false, false);
591 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
592 shdr
.get_sh_flags(), true, false,
593 false, false, false, false);
598 // By default the GNU linker sorts input sections whose names match
599 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
600 // are sorted by name. This is used to implement constructor
601 // priority ordering. We are compatible.
602 if (!this->script_options_
->saw_sections_clause()
603 && (is_prefix_of(".ctors.", name
)
604 || is_prefix_of(".dtors.", name
)
605 || is_prefix_of(".init_array.", name
)
606 || is_prefix_of(".fini_array.", name
)))
607 os
->set_must_sort_attached_input_sections();
609 // FIXME: Handle SHF_LINK_ORDER somewhere.
611 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
612 this->script_options_
->saw_sections_clause());
617 // Handle a relocation section when doing a relocatable link.
619 template<int size
, bool big_endian
>
621 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
623 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
624 Output_section
* data_section
,
625 Relocatable_relocs
* rr
)
627 gold_assert(parameters
->options().relocatable()
628 || parameters
->options().emit_relocs());
630 int sh_type
= shdr
.get_sh_type();
633 if (sh_type
== elfcpp::SHT_REL
)
635 else if (sh_type
== elfcpp::SHT_RELA
)
639 name
+= data_section
->name();
641 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
645 false, false, false);
647 os
->set_should_link_to_symtab();
648 os
->set_info_section(data_section
);
650 Output_section_data
* posd
;
651 if (sh_type
== elfcpp::SHT_REL
)
653 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
654 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
658 else if (sh_type
== elfcpp::SHT_RELA
)
660 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
661 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
668 os
->add_output_section_data(posd
);
669 rr
->set_output_data(posd
);
674 // Handle a group section when doing a relocatable link.
676 template<int size
, bool big_endian
>
678 Layout::layout_group(Symbol_table
* symtab
,
679 Sized_relobj
<size
, big_endian
>* object
,
681 const char* group_section_name
,
682 const char* signature
,
683 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
684 elfcpp::Elf_Word flags
,
685 std::vector
<unsigned int>* shndxes
)
687 gold_assert(parameters
->options().relocatable());
688 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
689 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
690 Output_section
* os
= this->make_output_section(group_section_name
,
696 // We need to find a symbol with the signature in the symbol table.
697 // If we don't find one now, we need to look again later.
698 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
700 os
->set_info_symndx(sym
);
703 // Reserve some space to minimize reallocations.
704 if (this->group_signatures_
.empty())
705 this->group_signatures_
.reserve(this->number_of_input_files_
* 16);
707 // We will wind up using a symbol whose name is the signature.
708 // So just put the signature in the symbol name pool to save it.
709 signature
= symtab
->canonicalize_name(signature
);
710 this->group_signatures_
.push_back(Group_signature(os
, signature
));
713 os
->set_should_link_to_symtab();
716 section_size_type entry_count
=
717 convert_to_section_size_type(shdr
.get_sh_size() / 4);
718 Output_section_data
* posd
=
719 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
721 os
->add_output_section_data(posd
);
724 // Special GNU handling of sections name .eh_frame. They will
725 // normally hold exception frame data as defined by the C++ ABI
726 // (http://codesourcery.com/cxx-abi/).
728 template<int size
, bool big_endian
>
730 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
731 const unsigned char* symbols
,
733 const unsigned char* symbol_names
,
734 off_t symbol_names_size
,
736 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
737 unsigned int reloc_shndx
, unsigned int reloc_type
,
740 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
741 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
743 const char* const name
= ".eh_frame";
744 Output_section
* os
= this->choose_output_section(object
,
746 elfcpp::SHT_PROGBITS
,
749 false, false, false);
753 if (this->eh_frame_section_
== NULL
)
755 this->eh_frame_section_
= os
;
756 this->eh_frame_data_
= new Eh_frame();
758 if (parameters
->options().eh_frame_hdr())
760 Output_section
* hdr_os
=
761 this->choose_output_section(NULL
,
763 elfcpp::SHT_PROGBITS
,
766 false, false, false);
770 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
771 this->eh_frame_data_
);
772 hdr_os
->add_output_section_data(hdr_posd
);
774 hdr_os
->set_after_input_sections();
776 if (!this->script_options_
->saw_phdrs_clause())
778 Output_segment
* hdr_oseg
;
779 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
781 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
, false);
784 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
789 gold_assert(this->eh_frame_section_
== os
);
791 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
800 os
->update_flags_for_input_section(shdr
.get_sh_flags());
802 // We found a .eh_frame section we are going to optimize, so now
803 // we can add the set of optimized sections to the output
804 // section. We need to postpone adding this until we've found a
805 // section we can optimize so that the .eh_frame section in
806 // crtbegin.o winds up at the start of the output section.
807 if (!this->added_eh_frame_data_
)
809 os
->add_output_section_data(this->eh_frame_data_
);
810 this->added_eh_frame_data_
= true;
816 // We couldn't handle this .eh_frame section for some reason.
817 // Add it as a normal section.
818 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
819 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
820 saw_sections_clause
);
826 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
827 // the output section.
830 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
831 elfcpp::Elf_Xword flags
,
832 Output_section_data
* posd
,
833 bool is_dynamic_linker_section
,
834 bool is_relro
, bool is_last_relro
,
835 bool is_first_non_relro
)
837 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
839 is_dynamic_linker_section
,
840 is_relro
, is_last_relro
,
843 os
->add_output_section_data(posd
);
847 // Map section flags to segment flags.
850 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
852 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
853 if ((flags
& elfcpp::SHF_WRITE
) != 0)
855 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
860 // Sometimes we compress sections. This is typically done for
861 // sections that are not part of normal program execution (such as
862 // .debug_* sections), and where the readers of these sections know
863 // how to deal with compressed sections. This routine doesn't say for
864 // certain whether we'll compress -- it depends on commandline options
865 // as well -- just whether this section is a candidate for compression.
866 // (The Output_compressed_section class decides whether to compress
867 // a given section, and picks the name of the compressed section.)
870 is_compressible_debug_section(const char* secname
)
872 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
875 // Make a new Output_section, and attach it to segments as
876 // appropriate. IS_INTERP is true if this is the .interp section.
877 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
878 // dynamic linker. IS_RELRO is true if this is a relro section.
879 // IS_LAST_RELRO is true if this is the last relro section.
880 // IS_FIRST_NON_RELRO is true if this is the first non relro section.
883 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
884 elfcpp::Elf_Xword flags
, bool is_interp
,
885 bool is_dynamic_linker_section
, bool is_relro
,
886 bool is_last_relro
, bool is_first_non_relro
)
889 if ((flags
& elfcpp::SHF_ALLOC
) == 0
890 && strcmp(parameters
->options().compress_debug_sections(), "none") != 0
891 && is_compressible_debug_section(name
))
892 os
= new Output_compressed_section(¶meters
->options(), name
, type
,
894 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
895 && parameters
->options().strip_debug_non_line()
896 && strcmp(".debug_abbrev", name
) == 0)
898 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
900 if (this->debug_info_
)
901 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
903 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
904 && parameters
->options().strip_debug_non_line()
905 && strcmp(".debug_info", name
) == 0)
907 os
= this->debug_info_
= new Output_reduced_debug_info_section(
909 if (this->debug_abbrev_
)
910 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
914 // FIXME: const_cast is ugly.
915 Target
* target
= const_cast<Target
*>(¶meters
->target());
916 os
= target
->make_output_section(name
, type
, flags
);
921 if (is_dynamic_linker_section
)
922 os
->set_is_dynamic_linker_section();
926 os
->set_is_last_relro();
927 if (is_first_non_relro
)
928 os
->set_is_first_non_relro();
930 parameters
->target().new_output_section(os
);
932 this->section_list_
.push_back(os
);
934 // The GNU linker by default sorts some sections by priority, so we
935 // do the same. We need to know that this might happen before we
936 // attach any input sections.
937 if (!this->script_options_
->saw_sections_clause()
938 && (strcmp(name
, ".ctors") == 0
939 || strcmp(name
, ".dtors") == 0
940 || strcmp(name
, ".init_array") == 0
941 || strcmp(name
, ".fini_array") == 0))
942 os
->set_may_sort_attached_input_sections();
944 // With -z relro, we have to recognize the special sections by name.
945 // There is no other way.
946 if (!this->script_options_
->saw_sections_clause()
947 && parameters
->options().relro()
948 && type
== elfcpp::SHT_PROGBITS
949 && (flags
& elfcpp::SHF_ALLOC
) != 0
950 && (flags
& elfcpp::SHF_WRITE
) != 0)
952 if (strcmp(name
, ".data.rel.ro") == 0)
954 else if (strcmp(name
, ".data.rel.ro.local") == 0)
957 os
->set_is_relro_local();
961 // Check for .stab*str sections, as .stab* sections need to link to
963 if (type
== elfcpp::SHT_STRTAB
964 && !this->have_stabstr_section_
965 && strncmp(name
, ".stab", 5) == 0
966 && strcmp(name
+ strlen(name
) - 3, "str") == 0)
967 this->have_stabstr_section_
= true;
969 // If we have already attached the sections to segments, then we
970 // need to attach this one now. This happens for sections created
971 // directly by the linker.
972 if (this->sections_are_attached_
)
973 this->attach_section_to_segment(os
);
978 // Attach output sections to segments. This is called after we have
979 // seen all the input sections.
982 Layout::attach_sections_to_segments()
984 for (Section_list::iterator p
= this->section_list_
.begin();
985 p
!= this->section_list_
.end();
987 this->attach_section_to_segment(*p
);
989 this->sections_are_attached_
= true;
992 // Attach an output section to a segment.
995 Layout::attach_section_to_segment(Output_section
* os
)
997 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
998 this->unattached_section_list_
.push_back(os
);
1000 this->attach_allocated_section_to_segment(os
);
1003 // Attach an allocated output section to a segment.
1006 Layout::attach_allocated_section_to_segment(Output_section
* os
)
1008 elfcpp::Elf_Xword flags
= os
->flags();
1009 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
1011 if (parameters
->options().relocatable())
1014 // If we have a SECTIONS clause, we can't handle the attachment to
1015 // segments until after we've seen all the sections.
1016 if (this->script_options_
->saw_sections_clause())
1019 gold_assert(!this->script_options_
->saw_phdrs_clause());
1021 // This output section goes into a PT_LOAD segment.
1023 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
1025 bool sort_sections
= !this->script_options_
->saw_sections_clause();
1027 // In general the only thing we really care about for PT_LOAD
1028 // segments is whether or not they are writable, so that is how we
1029 // search for them. Large data sections also go into their own
1030 // PT_LOAD segment. People who need segments sorted on some other
1031 // basis will have to use a linker script.
1033 Segment_list::const_iterator p
;
1034 for (p
= this->segment_list_
.begin();
1035 p
!= this->segment_list_
.end();
1038 if ((*p
)->type() != elfcpp::PT_LOAD
)
1040 if (!parameters
->options().omagic()
1041 && ((*p
)->flags() & elfcpp::PF_W
) != (seg_flags
& elfcpp::PF_W
))
1043 // If -Tbss was specified, we need to separate the data and BSS
1045 if (parameters
->options().user_set_Tbss())
1047 if ((os
->type() == elfcpp::SHT_NOBITS
)
1048 == (*p
)->has_any_data_sections())
1051 if (os
->is_large_data_section() && !(*p
)->is_large_data_segment())
1054 (*p
)->add_output_section(os
, seg_flags
, sort_sections
);
1058 if (p
== this->segment_list_
.end())
1060 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
1062 if (os
->is_large_data_section())
1063 oseg
->set_is_large_data_segment();
1064 oseg
->add_output_section(os
, seg_flags
, sort_sections
);
1067 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1069 if (os
->type() == elfcpp::SHT_NOTE
)
1071 // See if we already have an equivalent PT_NOTE segment.
1072 for (p
= this->segment_list_
.begin();
1073 p
!= segment_list_
.end();
1076 if ((*p
)->type() == elfcpp::PT_NOTE
1077 && (((*p
)->flags() & elfcpp::PF_W
)
1078 == (seg_flags
& elfcpp::PF_W
)))
1080 (*p
)->add_output_section(os
, seg_flags
, false);
1085 if (p
== this->segment_list_
.end())
1087 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
1089 oseg
->add_output_section(os
, seg_flags
, false);
1093 // If we see a loadable SHF_TLS section, we create a PT_TLS
1094 // segment. There can only be one such segment.
1095 if ((flags
& elfcpp::SHF_TLS
) != 0)
1097 if (this->tls_segment_
== NULL
)
1098 this->make_output_segment(elfcpp::PT_TLS
, seg_flags
);
1099 this->tls_segment_
->add_output_section(os
, seg_flags
, false);
1102 // If -z relro is in effect, and we see a relro section, we create a
1103 // PT_GNU_RELRO segment. There can only be one such segment.
1104 if (os
->is_relro() && parameters
->options().relro())
1106 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
1107 if (this->relro_segment_
== NULL
)
1108 this->make_output_segment(elfcpp::PT_GNU_RELRO
, seg_flags
);
1109 this->relro_segment_
->add_output_section(os
, seg_flags
, false);
1113 // Make an output section for a script.
1116 Layout::make_output_section_for_script(const char* name
)
1118 name
= this->namepool_
.add(name
, false, NULL
);
1119 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
1120 elfcpp::SHF_ALLOC
, false,
1121 false, false, false, false);
1122 os
->set_found_in_sections_clause();
1126 // Return the number of segments we expect to see.
1129 Layout::expected_segment_count() const
1131 size_t ret
= this->segment_list_
.size();
1133 // If we didn't see a SECTIONS clause in a linker script, we should
1134 // already have the complete list of segments. Otherwise we ask the
1135 // SECTIONS clause how many segments it expects, and add in the ones
1136 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1138 if (!this->script_options_
->saw_sections_clause())
1142 const Script_sections
* ss
= this->script_options_
->script_sections();
1143 return ret
+ ss
->expected_segment_count(this);
1147 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1148 // is whether we saw a .note.GNU-stack section in the object file.
1149 // GNU_STACK_FLAGS is the section flags. The flags give the
1150 // protection required for stack memory. We record this in an
1151 // executable as a PT_GNU_STACK segment. If an object file does not
1152 // have a .note.GNU-stack segment, we must assume that it is an old
1153 // object. On some targets that will force an executable stack.
1156 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
1158 if (!seen_gnu_stack
)
1159 this->input_without_gnu_stack_note_
= true;
1162 this->input_with_gnu_stack_note_
= true;
1163 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
1164 this->input_requires_executable_stack_
= true;
1168 // Create automatic note sections.
1171 Layout::create_notes()
1173 this->create_gold_note();
1174 this->create_executable_stack_info();
1175 this->create_build_id();
1178 // Create the dynamic sections which are needed before we read the
1182 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
1184 if (parameters
->doing_static_link())
1187 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1188 elfcpp::SHT_DYNAMIC
,
1190 | elfcpp::SHF_WRITE
),
1192 true, false, false);
1194 this->dynamic_symbol_
=
1195 symtab
->define_in_output_data("_DYNAMIC", NULL
, Symbol_table::PREDEFINED
,
1196 this->dynamic_section_
, 0, 0,
1197 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1198 elfcpp::STV_HIDDEN
, 0, false, false);
1200 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1202 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1205 // For each output section whose name can be represented as C symbol,
1206 // define __start and __stop symbols for the section. This is a GNU
1210 Layout::define_section_symbols(Symbol_table
* symtab
)
1212 for (Section_list::const_iterator p
= this->section_list_
.begin();
1213 p
!= this->section_list_
.end();
1216 const char* const name
= (*p
)->name();
1217 if (name
[strspn(name
,
1219 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
1220 "abcdefghijklmnopqrstuvwxyz"
1224 const std::string
name_string(name
);
1225 const std::string
start_name("__start_" + name_string
);
1226 const std::string
stop_name("__stop_" + name_string
);
1228 symtab
->define_in_output_data(start_name
.c_str(),
1230 Symbol_table::PREDEFINED
,
1236 elfcpp::STV_DEFAULT
,
1238 false, // offset_is_from_end
1239 true); // only_if_ref
1241 symtab
->define_in_output_data(stop_name
.c_str(),
1243 Symbol_table::PREDEFINED
,
1249 elfcpp::STV_DEFAULT
,
1251 true, // offset_is_from_end
1252 true); // only_if_ref
1257 // Define symbols for group signatures.
1260 Layout::define_group_signatures(Symbol_table
* symtab
)
1262 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1263 p
!= this->group_signatures_
.end();
1266 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1268 p
->section
->set_info_symndx(sym
);
1271 // Force the name of the group section to the group
1272 // signature, and use the group's section symbol as the
1273 // signature symbol.
1274 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1276 const char* name
= this->namepool_
.add(p
->signature
,
1278 p
->section
->set_name(name
);
1280 p
->section
->set_needs_symtab_index();
1281 p
->section
->set_info_section_symndx(p
->section
);
1285 this->group_signatures_
.clear();
1288 // Find the first read-only PT_LOAD segment, creating one if
1292 Layout::find_first_load_seg()
1294 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1295 p
!= this->segment_list_
.end();
1298 if ((*p
)->type() == elfcpp::PT_LOAD
1299 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1300 && (parameters
->options().omagic()
1301 || ((*p
)->flags() & elfcpp::PF_W
) == 0))
1305 gold_assert(!this->script_options_
->saw_phdrs_clause());
1307 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1312 // Save states of all current output segments. Store saved states
1313 // in SEGMENT_STATES.
1316 Layout::save_segments(Segment_states
* segment_states
)
1318 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1319 p
!= this->segment_list_
.end();
1322 Output_segment
* segment
= *p
;
1324 Output_segment
* copy
= new Output_segment(*segment
);
1325 (*segment_states
)[segment
] = copy
;
1329 // Restore states of output segments and delete any segment not found in
1333 Layout::restore_segments(const Segment_states
* segment_states
)
1335 // Go through the segment list and remove any segment added in the
1337 this->tls_segment_
= NULL
;
1338 this->relro_segment_
= NULL
;
1339 Segment_list::iterator list_iter
= this->segment_list_
.begin();
1340 while (list_iter
!= this->segment_list_
.end())
1342 Output_segment
* segment
= *list_iter
;
1343 Segment_states::const_iterator states_iter
=
1344 segment_states
->find(segment
);
1345 if (states_iter
!= segment_states
->end())
1347 const Output_segment
* copy
= states_iter
->second
;
1348 // Shallow copy to restore states.
1351 // Also fix up TLS and RELRO segment pointers as appropriate.
1352 if (segment
->type() == elfcpp::PT_TLS
)
1353 this->tls_segment_
= segment
;
1354 else if (segment
->type() == elfcpp::PT_GNU_RELRO
)
1355 this->relro_segment_
= segment
;
1361 list_iter
= this->segment_list_
.erase(list_iter
);
1362 // This is a segment created during section layout. It should be
1363 // safe to remove it since we should have removed all pointers to it.
1369 // Clean up after relaxation so that sections can be laid out again.
1372 Layout::clean_up_after_relaxation()
1374 // Restore the segments to point state just prior to the relaxation loop.
1375 Script_sections
* script_section
= this->script_options_
->script_sections();
1376 script_section
->release_segments();
1377 this->restore_segments(this->segment_states_
);
1379 // Reset section addresses and file offsets
1380 for (Section_list::iterator p
= this->section_list_
.begin();
1381 p
!= this->section_list_
.end();
1384 (*p
)->reset_address_and_file_offset();
1385 (*p
)->restore_states();
1388 // Reset special output object address and file offsets.
1389 for (Data_list::iterator p
= this->special_output_list_
.begin();
1390 p
!= this->special_output_list_
.end();
1392 (*p
)->reset_address_and_file_offset();
1394 // A linker script may have created some output section data objects.
1395 // They are useless now.
1396 for (Output_section_data_list::const_iterator p
=
1397 this->script_output_section_data_list_
.begin();
1398 p
!= this->script_output_section_data_list_
.end();
1401 this->script_output_section_data_list_
.clear();
1404 // Prepare for relaxation.
1407 Layout::prepare_for_relaxation()
1409 // Create an relaxation debug check if in debugging mode.
1410 if (is_debugging_enabled(DEBUG_RELAXATION
))
1411 this->relaxation_debug_check_
= new Relaxation_debug_check();
1413 // Save segment states.
1414 this->segment_states_
= new Segment_states();
1415 this->save_segments(this->segment_states_
);
1417 for(Section_list::const_iterator p
= this->section_list_
.begin();
1418 p
!= this->section_list_
.end();
1420 (*p
)->save_states();
1422 if (is_debugging_enabled(DEBUG_RELAXATION
))
1423 this->relaxation_debug_check_
->check_output_data_for_reset_values(
1424 this->section_list_
, this->special_output_list_
);
1426 // Also enable recording of output section data from scripts.
1427 this->record_output_section_data_from_script_
= true;
1430 // Relaxation loop body: If target has no relaxation, this runs only once
1431 // Otherwise, the target relaxation hook is called at the end of
1432 // each iteration. If the hook returns true, it means re-layout of
1433 // section is required.
1435 // The number of segments created by a linking script without a PHDRS
1436 // clause may be affected by section sizes and alignments. There is
1437 // a remote chance that relaxation causes different number of PT_LOAD
1438 // segments are created and sections are attached to different segments.
1439 // Therefore, we always throw away all segments created during section
1440 // layout. In order to be able to restart the section layout, we keep
1441 // a copy of the segment list right before the relaxation loop and use
1442 // that to restore the segments.
1444 // PASS is the current relaxation pass number.
1445 // SYMTAB is a symbol table.
1446 // PLOAD_SEG is the address of a pointer for the load segment.
1447 // PHDR_SEG is a pointer to the PHDR segment.
1448 // SEGMENT_HEADERS points to the output segment header.
1449 // FILE_HEADER points to the output file header.
1450 // PSHNDX is the address to store the output section index.
1453 Layout::relaxation_loop_body(
1456 Symbol_table
* symtab
,
1457 Output_segment
** pload_seg
,
1458 Output_segment
* phdr_seg
,
1459 Output_segment_headers
* segment_headers
,
1460 Output_file_header
* file_header
,
1461 unsigned int* pshndx
)
1463 // If this is not the first iteration, we need to clean up after
1464 // relaxation so that we can lay out the sections again.
1466 this->clean_up_after_relaxation();
1468 // If there is a SECTIONS clause, put all the input sections into
1469 // the required order.
1470 Output_segment
* load_seg
;
1471 if (this->script_options_
->saw_sections_clause())
1472 load_seg
= this->set_section_addresses_from_script(symtab
);
1473 else if (parameters
->options().relocatable())
1476 load_seg
= this->find_first_load_seg();
1478 if (parameters
->options().oformat_enum()
1479 != General_options::OBJECT_FORMAT_ELF
)
1482 gold_assert(phdr_seg
== NULL
1484 || this->script_options_
->saw_sections_clause());
1486 // Lay out the segment headers.
1487 if (!parameters
->options().relocatable())
1489 gold_assert(segment_headers
!= NULL
);
1490 if (load_seg
!= NULL
)
1491 load_seg
->add_initial_output_data(segment_headers
);
1492 if (phdr_seg
!= NULL
)
1493 phdr_seg
->add_initial_output_data(segment_headers
);
1496 // Lay out the file header.
1497 if (load_seg
!= NULL
)
1498 load_seg
->add_initial_output_data(file_header
);
1500 if (this->script_options_
->saw_phdrs_clause()
1501 && !parameters
->options().relocatable())
1503 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1504 // clause in a linker script.
1505 Script_sections
* ss
= this->script_options_
->script_sections();
1506 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1509 // We set the output section indexes in set_segment_offsets and
1510 // set_section_indexes.
1513 // Set the file offsets of all the segments, and all the sections
1516 if (!parameters
->options().relocatable())
1517 off
= this->set_segment_offsets(target
, load_seg
, pshndx
);
1519 off
= this->set_relocatable_section_offsets(file_header
, pshndx
);
1521 // Verify that the dummy relaxation does not change anything.
1522 if (is_debugging_enabled(DEBUG_RELAXATION
))
1525 this->relaxation_debug_check_
->read_sections(this->section_list_
);
1527 this->relaxation_debug_check_
->verify_sections(this->section_list_
);
1530 *pload_seg
= load_seg
;
1534 // Finalize the layout. When this is called, we have created all the
1535 // output sections and all the output segments which are based on
1536 // input sections. We have several things to do, and we have to do
1537 // them in the right order, so that we get the right results correctly
1540 // 1) Finalize the list of output segments and create the segment
1543 // 2) Finalize the dynamic symbol table and associated sections.
1545 // 3) Determine the final file offset of all the output segments.
1547 // 4) Determine the final file offset of all the SHF_ALLOC output
1550 // 5) Create the symbol table sections and the section name table
1553 // 6) Finalize the symbol table: set symbol values to their final
1554 // value and make a final determination of which symbols are going
1555 // into the output symbol table.
1557 // 7) Create the section table header.
1559 // 8) Determine the final file offset of all the output sections which
1560 // are not SHF_ALLOC, including the section table header.
1562 // 9) Finalize the ELF file header.
1564 // This function returns the size of the output file.
1567 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1568 Target
* target
, const Task
* task
)
1570 target
->finalize_sections(this, input_objects
, symtab
);
1572 this->count_local_symbols(task
, input_objects
);
1574 this->link_stabs_sections();
1576 Output_segment
* phdr_seg
= NULL
;
1577 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1579 // There was a dynamic object in the link. We need to create
1580 // some information for the dynamic linker.
1582 // Create the PT_PHDR segment which will hold the program
1584 if (!this->script_options_
->saw_phdrs_clause())
1585 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1587 // Create the dynamic symbol table, including the hash table.
1588 Output_section
* dynstr
;
1589 std::vector
<Symbol
*> dynamic_symbols
;
1590 unsigned int local_dynamic_count
;
1591 Versions
versions(*this->script_options()->version_script_info(),
1593 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1594 &local_dynamic_count
, &dynamic_symbols
,
1597 // Create the .interp section to hold the name of the
1598 // interpreter, and put it in a PT_INTERP segment.
1599 if (!parameters
->options().shared())
1600 this->create_interp(target
);
1602 // Finish the .dynamic section to hold the dynamic data, and put
1603 // it in a PT_DYNAMIC segment.
1604 this->finish_dynamic_section(input_objects
, symtab
);
1606 // We should have added everything we need to the dynamic string
1608 this->dynpool_
.set_string_offsets();
1610 // Create the version sections. We can't do this until the
1611 // dynamic string table is complete.
1612 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1613 dynamic_symbols
, dynstr
);
1615 // Set the size of the _DYNAMIC symbol. We can't do this until
1616 // after we call create_version_sections.
1617 this->set_dynamic_symbol_size(symtab
);
1620 if (this->incremental_inputs_
)
1622 this->incremental_inputs_
->finalize();
1623 this->create_incremental_info_sections();
1626 // Create segment headers.
1627 Output_segment_headers
* segment_headers
=
1628 (parameters
->options().relocatable()
1630 : new Output_segment_headers(this->segment_list_
));
1632 // Lay out the file header.
1633 Output_file_header
* file_header
1634 = new Output_file_header(target
, symtab
, segment_headers
,
1635 parameters
->options().entry());
1637 this->special_output_list_
.push_back(file_header
);
1638 if (segment_headers
!= NULL
)
1639 this->special_output_list_
.push_back(segment_headers
);
1641 // Find approriate places for orphan output sections if we are using
1643 if (this->script_options_
->saw_sections_clause())
1644 this->place_orphan_sections_in_script();
1646 Output_segment
* load_seg
;
1651 // Take a snapshot of the section layout as needed.
1652 if (target
->may_relax())
1653 this->prepare_for_relaxation();
1655 // Run the relaxation loop to lay out sections.
1658 off
= this->relaxation_loop_body(pass
, target
, symtab
, &load_seg
,
1659 phdr_seg
, segment_headers
, file_header
,
1663 while (target
->may_relax()
1664 && target
->relax(pass
, input_objects
, symtab
, this));
1666 // Set the file offsets of all the non-data sections we've seen so
1667 // far which don't have to wait for the input sections. We need
1668 // this in order to finalize local symbols in non-allocated
1670 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1672 // Set the section indexes of all unallocated sections seen so far,
1673 // in case any of them are somehow referenced by a symbol.
1674 shndx
= this->set_section_indexes(shndx
);
1676 // Create the symbol table sections.
1677 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1678 if (!parameters
->doing_static_link())
1679 this->assign_local_dynsym_offsets(input_objects
);
1681 // Process any symbol assignments from a linker script. This must
1682 // be called after the symbol table has been finalized.
1683 this->script_options_
->finalize_symbols(symtab
, this);
1685 // Create the .shstrtab section.
1686 Output_section
* shstrtab_section
= this->create_shstrtab();
1688 // Set the file offsets of the rest of the non-data sections which
1689 // don't have to wait for the input sections.
1690 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1692 // Now that all sections have been created, set the section indexes
1693 // for any sections which haven't been done yet.
1694 shndx
= this->set_section_indexes(shndx
);
1696 // Create the section table header.
1697 this->create_shdrs(shstrtab_section
, &off
);
1699 // If there are no sections which require postprocessing, we can
1700 // handle the section names now, and avoid a resize later.
1701 if (!this->any_postprocessing_sections_
)
1702 off
= this->set_section_offsets(off
,
1703 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1705 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1707 // Now we know exactly where everything goes in the output file
1708 // (except for non-allocated sections which require postprocessing).
1709 Output_data::layout_complete();
1711 this->output_file_size_
= off
;
1716 // Create a note header following the format defined in the ELF ABI.
1717 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1718 // of the section to create, DESCSZ is the size of the descriptor.
1719 // ALLOCATE is true if the section should be allocated in memory.
1720 // This returns the new note section. It sets *TRAILING_PADDING to
1721 // the number of trailing zero bytes required.
1724 Layout::create_note(const char* name
, int note_type
,
1725 const char* section_name
, size_t descsz
,
1726 bool allocate
, size_t* trailing_padding
)
1728 // Authorities all agree that the values in a .note field should
1729 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1730 // they differ on what the alignment is for 64-bit binaries.
1731 // The GABI says unambiguously they take 8-byte alignment:
1732 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1733 // Other documentation says alignment should always be 4 bytes:
1734 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1735 // GNU ld and GNU readelf both support the latter (at least as of
1736 // version 2.16.91), and glibc always generates the latter for
1737 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1739 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1740 const int size
= parameters
->target().get_size();
1742 const int size
= 32;
1745 // The contents of the .note section.
1746 size_t namesz
= strlen(name
) + 1;
1747 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1748 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1750 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1752 unsigned char* buffer
= new unsigned char[notehdrsz
];
1753 memset(buffer
, 0, notehdrsz
);
1755 bool is_big_endian
= parameters
->target().is_big_endian();
1761 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1762 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1763 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1767 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1768 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1769 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1772 else if (size
== 64)
1776 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1777 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1778 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1782 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1783 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1784 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1790 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1792 elfcpp::Elf_Xword flags
= 0;
1794 flags
= elfcpp::SHF_ALLOC
;
1795 Output_section
* os
= this->choose_output_section(NULL
, section_name
,
1797 flags
, false, false,
1798 false, false, false, false);
1802 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1805 os
->add_output_section_data(posd
);
1807 *trailing_padding
= aligned_descsz
- descsz
;
1812 // For an executable or shared library, create a note to record the
1813 // version of gold used to create the binary.
1816 Layout::create_gold_note()
1818 if (parameters
->options().relocatable())
1821 std::string desc
= std::string("gold ") + gold::get_version_string();
1823 size_t trailing_padding
;
1824 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1825 ".note.gnu.gold-version", desc
.size(),
1826 false, &trailing_padding
);
1830 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1831 os
->add_output_section_data(posd
);
1833 if (trailing_padding
> 0)
1835 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1836 os
->add_output_section_data(posd
);
1840 // Record whether the stack should be executable. This can be set
1841 // from the command line using the -z execstack or -z noexecstack
1842 // options. Otherwise, if any input file has a .note.GNU-stack
1843 // section with the SHF_EXECINSTR flag set, the stack should be
1844 // executable. Otherwise, if at least one input file a
1845 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1846 // section, we use the target default for whether the stack should be
1847 // executable. Otherwise, we don't generate a stack note. When
1848 // generating a object file, we create a .note.GNU-stack section with
1849 // the appropriate marking. When generating an executable or shared
1850 // library, we create a PT_GNU_STACK segment.
1853 Layout::create_executable_stack_info()
1855 bool is_stack_executable
;
1856 if (parameters
->options().is_execstack_set())
1857 is_stack_executable
= parameters
->options().is_stack_executable();
1858 else if (!this->input_with_gnu_stack_note_
)
1862 if (this->input_requires_executable_stack_
)
1863 is_stack_executable
= true;
1864 else if (this->input_without_gnu_stack_note_
)
1865 is_stack_executable
=
1866 parameters
->target().is_default_stack_executable();
1868 is_stack_executable
= false;
1871 if (parameters
->options().relocatable())
1873 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1874 elfcpp::Elf_Xword flags
= 0;
1875 if (is_stack_executable
)
1876 flags
|= elfcpp::SHF_EXECINSTR
;
1877 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
, false,
1878 false, false, false, false);
1882 if (this->script_options_
->saw_phdrs_clause())
1884 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1885 if (is_stack_executable
)
1886 flags
|= elfcpp::PF_X
;
1887 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1891 // If --build-id was used, set up the build ID note.
1894 Layout::create_build_id()
1896 if (!parameters
->options().user_set_build_id())
1899 const char* style
= parameters
->options().build_id();
1900 if (strcmp(style
, "none") == 0)
1903 // Set DESCSZ to the size of the note descriptor. When possible,
1904 // set DESC to the note descriptor contents.
1907 if (strcmp(style
, "md5") == 0)
1909 else if (strcmp(style
, "sha1") == 0)
1911 else if (strcmp(style
, "uuid") == 0)
1913 const size_t uuidsz
= 128 / 8;
1915 char buffer
[uuidsz
];
1916 memset(buffer
, 0, uuidsz
);
1918 int descriptor
= open_descriptor(-1, "/dev/urandom", O_RDONLY
);
1920 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1924 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1925 release_descriptor(descriptor
, true);
1927 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1928 else if (static_cast<size_t>(got
) != uuidsz
)
1929 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1933 desc
.assign(buffer
, uuidsz
);
1936 else if (strncmp(style
, "0x", 2) == 0)
1939 const char* p
= style
+ 2;
1942 if (hex_p(p
[0]) && hex_p(p
[1]))
1944 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1948 else if (*p
== '-' || *p
== ':')
1951 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1954 descsz
= desc
.size();
1957 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1960 size_t trailing_padding
;
1961 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1962 ".note.gnu.build-id", descsz
, true,
1969 // We know the value already, so we fill it in now.
1970 gold_assert(desc
.size() == descsz
);
1972 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1973 os
->add_output_section_data(posd
);
1975 if (trailing_padding
!= 0)
1977 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1978 os
->add_output_section_data(posd
);
1983 // We need to compute a checksum after we have completed the
1985 gold_assert(trailing_padding
== 0);
1986 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
1987 os
->add_output_section_data(this->build_id_note_
);
1991 // If we have both .stabXX and .stabXXstr sections, then the sh_link
1992 // field of the former should point to the latter. I'm not sure who
1993 // started this, but the GNU linker does it, and some tools depend
1997 Layout::link_stabs_sections()
1999 if (!this->have_stabstr_section_
)
2002 for (Section_list::iterator p
= this->section_list_
.begin();
2003 p
!= this->section_list_
.end();
2006 if ((*p
)->type() != elfcpp::SHT_STRTAB
)
2009 const char* name
= (*p
)->name();
2010 if (strncmp(name
, ".stab", 5) != 0)
2013 size_t len
= strlen(name
);
2014 if (strcmp(name
+ len
- 3, "str") != 0)
2017 std::string
stab_name(name
, len
- 3);
2018 Output_section
* stab_sec
;
2019 stab_sec
= this->find_output_section(stab_name
.c_str());
2020 if (stab_sec
!= NULL
)
2021 stab_sec
->set_link_section(*p
);
2025 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2026 // for the next run of incremental linking to check what has changed.
2029 Layout::create_incremental_info_sections()
2031 gold_assert(this->incremental_inputs_
!= NULL
);
2033 // Add the .gnu_incremental_inputs section.
2034 const char *incremental_inputs_name
=
2035 this->namepool_
.add(".gnu_incremental_inputs", false, NULL
);
2036 Output_section
* inputs_os
=
2037 this->make_output_section(incremental_inputs_name
,
2038 elfcpp::SHT_GNU_INCREMENTAL_INPUTS
, 0,
2039 false, false, false, false, false);
2040 Output_section_data
* posd
=
2041 this->incremental_inputs_
->create_incremental_inputs_section_data();
2042 inputs_os
->add_output_section_data(posd
);
2044 // Add the .gnu_incremental_strtab section.
2045 const char *incremental_strtab_name
=
2046 this->namepool_
.add(".gnu_incremental_strtab", false, NULL
);
2047 Output_section
* strtab_os
= this->make_output_section(incremental_strtab_name
,
2050 false, false, false);
2051 Output_data_strtab
* strtab_data
=
2052 new Output_data_strtab(this->incremental_inputs_
->get_stringpool());
2053 strtab_os
->add_output_section_data(strtab_data
);
2055 inputs_os
->set_link_section(strtab_data
);
2058 // Return whether SEG1 should be before SEG2 in the output file. This
2059 // is based entirely on the segment type and flags. When this is
2060 // called the segment addresses has normally not yet been set.
2063 Layout::segment_precedes(const Output_segment
* seg1
,
2064 const Output_segment
* seg2
)
2066 elfcpp::Elf_Word type1
= seg1
->type();
2067 elfcpp::Elf_Word type2
= seg2
->type();
2069 // The single PT_PHDR segment is required to precede any loadable
2070 // segment. We simply make it always first.
2071 if (type1
== elfcpp::PT_PHDR
)
2073 gold_assert(type2
!= elfcpp::PT_PHDR
);
2076 if (type2
== elfcpp::PT_PHDR
)
2079 // The single PT_INTERP segment is required to precede any loadable
2080 // segment. We simply make it always second.
2081 if (type1
== elfcpp::PT_INTERP
)
2083 gold_assert(type2
!= elfcpp::PT_INTERP
);
2086 if (type2
== elfcpp::PT_INTERP
)
2089 // We then put PT_LOAD segments before any other segments.
2090 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
2092 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
2095 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2096 // segment, because that is where the dynamic linker expects to find
2097 // it (this is just for efficiency; other positions would also work
2099 if (type1
== elfcpp::PT_TLS
2100 && type2
!= elfcpp::PT_TLS
2101 && type2
!= elfcpp::PT_GNU_RELRO
)
2103 if (type2
== elfcpp::PT_TLS
2104 && type1
!= elfcpp::PT_TLS
2105 && type1
!= elfcpp::PT_GNU_RELRO
)
2108 // We put the PT_GNU_RELRO segment last, because that is where the
2109 // dynamic linker expects to find it (as with PT_TLS, this is just
2111 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
2113 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
2116 const elfcpp::Elf_Word flags1
= seg1
->flags();
2117 const elfcpp::Elf_Word flags2
= seg2
->flags();
2119 // The order of non-PT_LOAD segments is unimportant. We simply sort
2120 // by the numeric segment type and flags values. There should not
2121 // be more than one segment with the same type and flags.
2122 if (type1
!= elfcpp::PT_LOAD
)
2125 return type1
< type2
;
2126 gold_assert(flags1
!= flags2
);
2127 return flags1
< flags2
;
2130 // If the addresses are set already, sort by load address.
2131 if (seg1
->are_addresses_set())
2133 if (!seg2
->are_addresses_set())
2136 unsigned int section_count1
= seg1
->output_section_count();
2137 unsigned int section_count2
= seg2
->output_section_count();
2138 if (section_count1
== 0 && section_count2
> 0)
2140 if (section_count1
> 0 && section_count2
== 0)
2143 uint64_t paddr1
= seg1
->first_section_load_address();
2144 uint64_t paddr2
= seg2
->first_section_load_address();
2145 if (paddr1
!= paddr2
)
2146 return paddr1
< paddr2
;
2148 else if (seg2
->are_addresses_set())
2151 // A segment which holds large data comes after a segment which does
2152 // not hold large data.
2153 if (seg1
->is_large_data_segment())
2155 if (!seg2
->is_large_data_segment())
2158 else if (seg2
->is_large_data_segment())
2161 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2162 // segments come before writable segments. Then writable segments
2163 // with data come before writable segments without data. Then
2164 // executable segments come before non-executable segments. Then
2165 // the unlikely case of a non-readable segment comes before the
2166 // normal case of a readable segment. If there are multiple
2167 // segments with the same type and flags, we require that the
2168 // address be set, and we sort by virtual address and then physical
2170 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
2171 return (flags1
& elfcpp::PF_W
) == 0;
2172 if ((flags1
& elfcpp::PF_W
) != 0
2173 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
2174 return seg1
->has_any_data_sections();
2175 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
2176 return (flags1
& elfcpp::PF_X
) != 0;
2177 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
2178 return (flags1
& elfcpp::PF_R
) == 0;
2180 // We shouldn't get here--we shouldn't create segments which we
2181 // can't distinguish.
2185 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2188 align_file_offset(off_t off
, uint64_t addr
, uint64_t abi_pagesize
)
2190 uint64_t unsigned_off
= off
;
2191 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
2192 | (addr
& (abi_pagesize
- 1)));
2193 if (aligned_off
< unsigned_off
)
2194 aligned_off
+= abi_pagesize
;
2198 // Set the file offsets of all the segments, and all the sections they
2199 // contain. They have all been created. LOAD_SEG must be be laid out
2200 // first. Return the offset of the data to follow.
2203 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
2204 unsigned int *pshndx
)
2206 // Sort them into the final order.
2207 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
2208 Layout::Compare_segments());
2210 // Find the PT_LOAD segments, and set their addresses and offsets
2211 // and their section's addresses and offsets.
2213 if (parameters
->options().user_set_Ttext())
2214 addr
= parameters
->options().Ttext();
2215 else if (parameters
->options().output_is_position_independent())
2218 addr
= target
->default_text_segment_address();
2221 // If LOAD_SEG is NULL, then the file header and segment headers
2222 // will not be loadable. But they still need to be at offset 0 in
2223 // the file. Set their offsets now.
2224 if (load_seg
== NULL
)
2226 for (Data_list::iterator p
= this->special_output_list_
.begin();
2227 p
!= this->special_output_list_
.end();
2230 off
= align_address(off
, (*p
)->addralign());
2231 (*p
)->set_address_and_file_offset(0, off
);
2232 off
+= (*p
)->data_size();
2236 unsigned int increase_relro
= this->increase_relro_
;
2237 if (this->script_options_
->saw_sections_clause())
2240 const bool check_sections
= parameters
->options().check_sections();
2241 Output_segment
* last_load_segment
= NULL
;
2243 bool was_readonly
= false;
2244 for (Segment_list::iterator p
= this->segment_list_
.begin();
2245 p
!= this->segment_list_
.end();
2248 if ((*p
)->type() == elfcpp::PT_LOAD
)
2250 if (load_seg
!= NULL
&& load_seg
!= *p
)
2254 bool are_addresses_set
= (*p
)->are_addresses_set();
2255 if (are_addresses_set
)
2257 // When it comes to setting file offsets, we care about
2258 // the physical address.
2259 addr
= (*p
)->paddr();
2261 else if (parameters
->options().user_set_Tdata()
2262 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2263 && (!parameters
->options().user_set_Tbss()
2264 || (*p
)->has_any_data_sections()))
2266 addr
= parameters
->options().Tdata();
2267 are_addresses_set
= true;
2269 else if (parameters
->options().user_set_Tbss()
2270 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2271 && !(*p
)->has_any_data_sections())
2273 addr
= parameters
->options().Tbss();
2274 are_addresses_set
= true;
2277 uint64_t orig_addr
= addr
;
2278 uint64_t orig_off
= off
;
2280 uint64_t aligned_addr
= 0;
2281 uint64_t abi_pagesize
= target
->abi_pagesize();
2282 uint64_t common_pagesize
= target
->common_pagesize();
2284 if (!parameters
->options().nmagic()
2285 && !parameters
->options().omagic())
2286 (*p
)->set_minimum_p_align(common_pagesize
);
2288 if (!are_addresses_set
)
2290 // If the last segment was readonly, and this one is
2291 // not, then skip the address forward one page,
2292 // maintaining the same position within the page. This
2293 // lets us store both segments overlapping on a single
2294 // page in the file, but the loader will put them on
2295 // different pages in memory.
2297 addr
= align_address(addr
, (*p
)->maximum_alignment());
2298 aligned_addr
= addr
;
2300 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
2302 if ((addr
& (abi_pagesize
- 1)) != 0)
2303 addr
= addr
+ abi_pagesize
;
2306 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2309 if (!parameters
->options().nmagic()
2310 && !parameters
->options().omagic())
2311 off
= align_file_offset(off
, addr
, abi_pagesize
);
2312 else if (load_seg
== NULL
)
2314 // This is -N or -n with a section script which prevents
2315 // us from using a load segment. We need to ensure that
2316 // the file offset is aligned to the alignment of the
2317 // segment. This is because the linker script
2318 // implicitly assumed a zero offset. If we don't align
2319 // here, then the alignment of the sections in the
2320 // linker script may not match the alignment of the
2321 // sections in the set_section_addresses call below,
2322 // causing an error about dot moving backward.
2323 off
= align_address(off
, (*p
)->maximum_alignment());
2326 unsigned int shndx_hold
= *pshndx
;
2327 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
2331 // Now that we know the size of this segment, we may be able
2332 // to save a page in memory, at the cost of wasting some
2333 // file space, by instead aligning to the start of a new
2334 // page. Here we use the real machine page size rather than
2335 // the ABI mandated page size.
2337 if (!are_addresses_set
&& aligned_addr
!= addr
)
2339 uint64_t first_off
= (common_pagesize
2341 & (common_pagesize
- 1)));
2342 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
2345 && ((aligned_addr
& ~ (common_pagesize
- 1))
2346 != (new_addr
& ~ (common_pagesize
- 1)))
2347 && first_off
+ last_off
<= common_pagesize
)
2349 *pshndx
= shndx_hold
;
2350 addr
= align_address(aligned_addr
, common_pagesize
);
2351 addr
= align_address(addr
, (*p
)->maximum_alignment());
2352 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2353 off
= align_file_offset(off
, addr
, abi_pagesize
);
2354 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
2362 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
2363 was_readonly
= true;
2365 // Implement --check-sections. We know that the segments
2366 // are sorted by LMA.
2367 if (check_sections
&& last_load_segment
!= NULL
)
2369 gold_assert(last_load_segment
->paddr() <= (*p
)->paddr());
2370 if (last_load_segment
->paddr() + last_load_segment
->memsz()
2373 unsigned long long lb1
= last_load_segment
->paddr();
2374 unsigned long long le1
= lb1
+ last_load_segment
->memsz();
2375 unsigned long long lb2
= (*p
)->paddr();
2376 unsigned long long le2
= lb2
+ (*p
)->memsz();
2377 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2378 "[0x%llx -> 0x%llx]"),
2379 lb1
, le1
, lb2
, le2
);
2382 last_load_segment
= *p
;
2386 // Handle the non-PT_LOAD segments, setting their offsets from their
2387 // section's offsets.
2388 for (Segment_list::iterator p
= this->segment_list_
.begin();
2389 p
!= this->segment_list_
.end();
2392 if ((*p
)->type() != elfcpp::PT_LOAD
)
2393 (*p
)->set_offset((*p
)->type() == elfcpp::PT_GNU_RELRO
2398 // Set the TLS offsets for each section in the PT_TLS segment.
2399 if (this->tls_segment_
!= NULL
)
2400 this->tls_segment_
->set_tls_offsets();
2405 // Set the offsets of all the allocated sections when doing a
2406 // relocatable link. This does the same jobs as set_segment_offsets,
2407 // only for a relocatable link.
2410 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
2411 unsigned int *pshndx
)
2415 file_header
->set_address_and_file_offset(0, 0);
2416 off
+= file_header
->data_size();
2418 for (Section_list::iterator p
= this->section_list_
.begin();
2419 p
!= this->section_list_
.end();
2422 // We skip unallocated sections here, except that group sections
2423 // have to come first.
2424 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
2425 && (*p
)->type() != elfcpp::SHT_GROUP
)
2428 off
= align_address(off
, (*p
)->addralign());
2430 // The linker script might have set the address.
2431 if (!(*p
)->is_address_valid())
2432 (*p
)->set_address(0);
2433 (*p
)->set_file_offset(off
);
2434 (*p
)->finalize_data_size();
2435 off
+= (*p
)->data_size();
2437 (*p
)->set_out_shndx(*pshndx
);
2444 // Set the file offset of all the sections not associated with a
2448 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
2450 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2451 p
!= this->unattached_section_list_
.end();
2454 // The symtab section is handled in create_symtab_sections.
2455 if (*p
== this->symtab_section_
)
2458 // If we've already set the data size, don't set it again.
2459 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
2462 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2463 && (*p
)->requires_postprocessing())
2465 (*p
)->create_postprocessing_buffer();
2466 this->any_postprocessing_sections_
= true;
2469 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2470 && (*p
)->after_input_sections())
2472 else if (pass
== POSTPROCESSING_SECTIONS_PASS
2473 && (!(*p
)->after_input_sections()
2474 || (*p
)->type() == elfcpp::SHT_STRTAB
))
2476 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2477 && (!(*p
)->after_input_sections()
2478 || (*p
)->type() != elfcpp::SHT_STRTAB
))
2481 off
= align_address(off
, (*p
)->addralign());
2482 (*p
)->set_file_offset(off
);
2483 (*p
)->finalize_data_size();
2484 off
+= (*p
)->data_size();
2486 // At this point the name must be set.
2487 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
2488 this->namepool_
.add((*p
)->name(), false, NULL
);
2493 // Set the section indexes of all the sections not associated with a
2497 Layout::set_section_indexes(unsigned int shndx
)
2499 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2500 p
!= this->unattached_section_list_
.end();
2503 if (!(*p
)->has_out_shndx())
2505 (*p
)->set_out_shndx(shndx
);
2512 // Set the section addresses according to the linker script. This is
2513 // only called when we see a SECTIONS clause. This returns the
2514 // program segment which should hold the file header and segment
2515 // headers, if any. It will return NULL if they should not be in a
2519 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
2521 Script_sections
* ss
= this->script_options_
->script_sections();
2522 gold_assert(ss
->saw_sections_clause());
2523 return this->script_options_
->set_section_addresses(symtab
, this);
2526 // Place the orphan sections in the linker script.
2529 Layout::place_orphan_sections_in_script()
2531 Script_sections
* ss
= this->script_options_
->script_sections();
2532 gold_assert(ss
->saw_sections_clause());
2534 // Place each orphaned output section in the script.
2535 for (Section_list::iterator p
= this->section_list_
.begin();
2536 p
!= this->section_list_
.end();
2539 if (!(*p
)->found_in_sections_clause())
2540 ss
->place_orphan(*p
);
2544 // Count the local symbols in the regular symbol table and the dynamic
2545 // symbol table, and build the respective string pools.
2548 Layout::count_local_symbols(const Task
* task
,
2549 const Input_objects
* input_objects
)
2551 // First, figure out an upper bound on the number of symbols we'll
2552 // be inserting into each pool. This helps us create the pools with
2553 // the right size, to avoid unnecessary hashtable resizing.
2554 unsigned int symbol_count
= 0;
2555 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2556 p
!= input_objects
->relobj_end();
2558 symbol_count
+= (*p
)->local_symbol_count();
2560 // Go from "upper bound" to "estimate." We overcount for two
2561 // reasons: we double-count symbols that occur in more than one
2562 // object file, and we count symbols that are dropped from the
2563 // output. Add it all together and assume we overcount by 100%.
2566 // We assume all symbols will go into both the sympool and dynpool.
2567 this->sympool_
.reserve(symbol_count
);
2568 this->dynpool_
.reserve(symbol_count
);
2570 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2571 p
!= input_objects
->relobj_end();
2574 Task_lock_obj
<Object
> tlo(task
, *p
);
2575 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2579 // Create the symbol table sections. Here we also set the final
2580 // values of the symbols. At this point all the loadable sections are
2581 // fully laid out. SHNUM is the number of sections so far.
2584 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2585 Symbol_table
* symtab
,
2591 if (parameters
->target().get_size() == 32)
2593 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2596 else if (parameters
->target().get_size() == 64)
2598 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2605 off
= align_address(off
, align
);
2606 off_t startoff
= off
;
2608 // Save space for the dummy symbol at the start of the section. We
2609 // never bother to write this out--it will just be left as zero.
2611 unsigned int local_symbol_index
= 1;
2613 // Add STT_SECTION symbols for each Output section which needs one.
2614 for (Section_list::iterator p
= this->section_list_
.begin();
2615 p
!= this->section_list_
.end();
2618 if (!(*p
)->needs_symtab_index())
2619 (*p
)->set_symtab_index(-1U);
2622 (*p
)->set_symtab_index(local_symbol_index
);
2623 ++local_symbol_index
;
2628 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2629 p
!= input_objects
->relobj_end();
2632 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2634 off
+= (index
- local_symbol_index
) * symsize
;
2635 local_symbol_index
= index
;
2638 unsigned int local_symcount
= local_symbol_index
;
2639 gold_assert(static_cast<off_t
>(local_symcount
* symsize
) == off
- startoff
);
2642 size_t dyn_global_index
;
2644 if (this->dynsym_section_
== NULL
)
2647 dyn_global_index
= 0;
2652 dyn_global_index
= this->dynsym_section_
->info();
2653 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2654 dynoff
= this->dynsym_section_
->offset() + locsize
;
2655 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2656 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2657 == this->dynsym_section_
->data_size() - locsize
);
2660 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2661 &this->sympool_
, &local_symcount
);
2663 if (!parameters
->options().strip_all())
2665 this->sympool_
.set_string_offsets();
2667 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2668 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2671 false, false, false);
2672 this->symtab_section_
= osymtab
;
2674 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2677 osymtab
->add_output_section_data(pos
);
2679 // We generate a .symtab_shndx section if we have more than
2680 // SHN_LORESERVE sections. Technically it is possible that we
2681 // don't need one, because it is possible that there are no
2682 // symbols in any of sections with indexes larger than
2683 // SHN_LORESERVE. That is probably unusual, though, and it is
2684 // easier to always create one than to compute section indexes
2685 // twice (once here, once when writing out the symbols).
2686 if (shnum
>= elfcpp::SHN_LORESERVE
)
2688 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2690 Output_section
* osymtab_xindex
=
2691 this->make_output_section(symtab_xindex_name
,
2692 elfcpp::SHT_SYMTAB_SHNDX
, 0, false,
2693 false, false, false, false);
2695 size_t symcount
= (off
- startoff
) / symsize
;
2696 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2698 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2700 osymtab_xindex
->set_link_section(osymtab
);
2701 osymtab_xindex
->set_addralign(4);
2702 osymtab_xindex
->set_entsize(4);
2704 osymtab_xindex
->set_after_input_sections();
2706 // This tells the driver code to wait until the symbol table
2707 // has written out before writing out the postprocessing
2708 // sections, including the .symtab_shndx section.
2709 this->any_postprocessing_sections_
= true;
2712 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2713 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2716 false, false, false);
2718 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2719 ostrtab
->add_output_section_data(pstr
);
2721 osymtab
->set_file_offset(startoff
);
2722 osymtab
->finalize_data_size();
2723 osymtab
->set_link_section(ostrtab
);
2724 osymtab
->set_info(local_symcount
);
2725 osymtab
->set_entsize(symsize
);
2731 // Create the .shstrtab section, which holds the names of the
2732 // sections. At the time this is called, we have created all the
2733 // output sections except .shstrtab itself.
2736 Layout::create_shstrtab()
2738 // FIXME: We don't need to create a .shstrtab section if we are
2739 // stripping everything.
2741 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2743 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0,
2744 false, false, false, false,
2747 if (strcmp(parameters
->options().compress_debug_sections(), "none") != 0)
2749 // We can't write out this section until we've set all the
2750 // section names, and we don't set the names of compressed
2751 // output sections until relocations are complete. FIXME: With
2752 // the current names we use, this is unnecessary.
2753 os
->set_after_input_sections();
2756 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2757 os
->add_output_section_data(posd
);
2762 // Create the section headers. SIZE is 32 or 64. OFF is the file
2766 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2768 Output_section_headers
* oshdrs
;
2769 oshdrs
= new Output_section_headers(this,
2770 &this->segment_list_
,
2771 &this->section_list_
,
2772 &this->unattached_section_list_
,
2775 off_t off
= align_address(*poff
, oshdrs
->addralign());
2776 oshdrs
->set_address_and_file_offset(0, off
);
2777 off
+= oshdrs
->data_size();
2779 this->section_headers_
= oshdrs
;
2782 // Count the allocated sections.
2785 Layout::allocated_output_section_count() const
2787 size_t section_count
= 0;
2788 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2789 p
!= this->segment_list_
.end();
2791 section_count
+= (*p
)->output_section_count();
2792 return section_count
;
2795 // Create the dynamic symbol table.
2798 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2799 Symbol_table
* symtab
,
2800 Output_section
**pdynstr
,
2801 unsigned int* plocal_dynamic_count
,
2802 std::vector
<Symbol
*>* pdynamic_symbols
,
2803 Versions
* pversions
)
2805 // Count all the symbols in the dynamic symbol table, and set the
2806 // dynamic symbol indexes.
2808 // Skip symbol 0, which is always all zeroes.
2809 unsigned int index
= 1;
2811 // Add STT_SECTION symbols for each Output section which needs one.
2812 for (Section_list::iterator p
= this->section_list_
.begin();
2813 p
!= this->section_list_
.end();
2816 if (!(*p
)->needs_dynsym_index())
2817 (*p
)->set_dynsym_index(-1U);
2820 (*p
)->set_dynsym_index(index
);
2825 // Count the local symbols that need to go in the dynamic symbol table,
2826 // and set the dynamic symbol indexes.
2827 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2828 p
!= input_objects
->relobj_end();
2831 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2835 unsigned int local_symcount
= index
;
2836 *plocal_dynamic_count
= local_symcount
;
2838 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2839 &this->dynpool_
, pversions
);
2843 const int size
= parameters
->target().get_size();
2846 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2849 else if (size
== 64)
2851 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2857 // Create the dynamic symbol table section.
2859 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2863 false, false, false);
2865 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2868 dynsym
->add_output_section_data(odata
);
2870 dynsym
->set_info(local_symcount
);
2871 dynsym
->set_entsize(symsize
);
2872 dynsym
->set_addralign(align
);
2874 this->dynsym_section_
= dynsym
;
2876 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2877 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2878 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2880 // If there are more than SHN_LORESERVE allocated sections, we
2881 // create a .dynsym_shndx section. It is possible that we don't
2882 // need one, because it is possible that there are no dynamic
2883 // symbols in any of the sections with indexes larger than
2884 // SHN_LORESERVE. This is probably unusual, though, and at this
2885 // time we don't know the actual section indexes so it is
2886 // inconvenient to check.
2887 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2889 Output_section
* dynsym_xindex
=
2890 this->choose_output_section(NULL
, ".dynsym_shndx",
2891 elfcpp::SHT_SYMTAB_SHNDX
,
2893 false, false, true, false, false, false);
2895 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2897 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2899 dynsym_xindex
->set_link_section(dynsym
);
2900 dynsym_xindex
->set_addralign(4);
2901 dynsym_xindex
->set_entsize(4);
2903 dynsym_xindex
->set_after_input_sections();
2905 // This tells the driver code to wait until the symbol table has
2906 // written out before writing out the postprocessing sections,
2907 // including the .dynsym_shndx section.
2908 this->any_postprocessing_sections_
= true;
2911 // Create the dynamic string table section.
2913 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2917 false, false, false);
2919 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2920 dynstr
->add_output_section_data(strdata
);
2922 dynsym
->set_link_section(dynstr
);
2923 this->dynamic_section_
->set_link_section(dynstr
);
2925 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2926 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2930 // Create the hash tables.
2932 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2933 || strcmp(parameters
->options().hash_style(), "both") == 0)
2935 unsigned char* phash
;
2936 unsigned int hashlen
;
2937 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2940 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2947 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2951 hashsec
->add_output_section_data(hashdata
);
2953 hashsec
->set_link_section(dynsym
);
2954 hashsec
->set_entsize(4);
2956 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2959 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2960 || strcmp(parameters
->options().hash_style(), "both") == 0)
2962 unsigned char* phash
;
2963 unsigned int hashlen
;
2964 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2967 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2968 elfcpp::SHT_GNU_HASH
,
2974 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2978 hashsec
->add_output_section_data(hashdata
);
2980 hashsec
->set_link_section(dynsym
);
2981 hashsec
->set_entsize(4);
2983 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
2987 // Assign offsets to each local portion of the dynamic symbol table.
2990 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
2992 Output_section
* dynsym
= this->dynsym_section_
;
2993 gold_assert(dynsym
!= NULL
);
2995 off_t off
= dynsym
->offset();
2997 // Skip the dummy symbol at the start of the section.
2998 off
+= dynsym
->entsize();
3000 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
3001 p
!= input_objects
->relobj_end();
3004 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
3005 off
+= count
* dynsym
->entsize();
3009 // Create the version sections.
3012 Layout::create_version_sections(const Versions
* versions
,
3013 const Symbol_table
* symtab
,
3014 unsigned int local_symcount
,
3015 const std::vector
<Symbol
*>& dynamic_symbols
,
3016 const Output_section
* dynstr
)
3018 if (!versions
->any_defs() && !versions
->any_needs())
3021 switch (parameters
->size_and_endianness())
3023 #ifdef HAVE_TARGET_32_LITTLE
3024 case Parameters::TARGET_32_LITTLE
:
3025 this->sized_create_version_sections
<32, false>(versions
, symtab
,
3027 dynamic_symbols
, dynstr
);
3030 #ifdef HAVE_TARGET_32_BIG
3031 case Parameters::TARGET_32_BIG
:
3032 this->sized_create_version_sections
<32, true>(versions
, symtab
,
3034 dynamic_symbols
, dynstr
);
3037 #ifdef HAVE_TARGET_64_LITTLE
3038 case Parameters::TARGET_64_LITTLE
:
3039 this->sized_create_version_sections
<64, false>(versions
, symtab
,
3041 dynamic_symbols
, dynstr
);
3044 #ifdef HAVE_TARGET_64_BIG
3045 case Parameters::TARGET_64_BIG
:
3046 this->sized_create_version_sections
<64, true>(versions
, symtab
,
3048 dynamic_symbols
, dynstr
);
3056 // Create the version sections, sized version.
3058 template<int size
, bool big_endian
>
3060 Layout::sized_create_version_sections(
3061 const Versions
* versions
,
3062 const Symbol_table
* symtab
,
3063 unsigned int local_symcount
,
3064 const std::vector
<Symbol
*>& dynamic_symbols
,
3065 const Output_section
* dynstr
)
3067 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
3068 elfcpp::SHT_GNU_versym
,
3071 false, false, false);
3073 unsigned char* vbuf
;
3075 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
3080 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
3083 vsec
->add_output_section_data(vdata
);
3084 vsec
->set_entsize(2);
3085 vsec
->set_link_section(this->dynsym_section_
);
3087 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3088 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
3090 if (versions
->any_defs())
3092 Output_section
* vdsec
;
3093 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
3094 elfcpp::SHT_GNU_verdef
,
3096 false, false, true, false, false,
3099 unsigned char* vdbuf
;
3100 unsigned int vdsize
;
3101 unsigned int vdentries
;
3102 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
3103 &vdsize
, &vdentries
);
3105 Output_section_data
* vddata
=
3106 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
3108 vdsec
->add_output_section_data(vddata
);
3109 vdsec
->set_link_section(dynstr
);
3110 vdsec
->set_info(vdentries
);
3112 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
3113 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
3116 if (versions
->any_needs())
3118 Output_section
* vnsec
;
3119 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
3120 elfcpp::SHT_GNU_verneed
,
3122 false, false, true, false, false,
3125 unsigned char* vnbuf
;
3126 unsigned int vnsize
;
3127 unsigned int vnentries
;
3128 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
3132 Output_section_data
* vndata
=
3133 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
3135 vnsec
->add_output_section_data(vndata
);
3136 vnsec
->set_link_section(dynstr
);
3137 vnsec
->set_info(vnentries
);
3139 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
3140 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
3144 // Create the .interp section and PT_INTERP segment.
3147 Layout::create_interp(const Target
* target
)
3149 const char* interp
= parameters
->options().dynamic_linker();
3152 interp
= target
->dynamic_linker();
3153 gold_assert(interp
!= NULL
);
3156 size_t len
= strlen(interp
) + 1;
3158 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
3160 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
3161 elfcpp::SHT_PROGBITS
,
3164 false, false, false);
3165 osec
->add_output_section_data(odata
);
3167 if (!this->script_options_
->saw_phdrs_clause())
3169 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
3171 oseg
->add_output_section(osec
, elfcpp::PF_R
, false);
3175 // Finish the .dynamic section and PT_DYNAMIC segment.
3178 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
3179 const Symbol_table
* symtab
)
3181 if (!this->script_options_
->saw_phdrs_clause())
3183 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
3186 oseg
->add_output_section(this->dynamic_section_
,
3187 elfcpp::PF_R
| elfcpp::PF_W
,
3191 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3193 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
3194 p
!= input_objects
->dynobj_end();
3197 if (!(*p
)->is_needed()
3198 && (*p
)->input_file()->options().as_needed())
3200 // This dynamic object was linked with --as-needed, but it
3205 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
3208 if (parameters
->options().shared())
3210 const char* soname
= parameters
->options().soname();
3212 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
3215 Symbol
* sym
= symtab
->lookup(parameters
->options().init());
3216 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3217 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
3219 sym
= symtab
->lookup(parameters
->options().fini());
3220 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3221 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
3223 // Look for .init_array, .preinit_array and .fini_array by checking
3225 for(Layout::Section_list::const_iterator p
= this->section_list_
.begin();
3226 p
!= this->section_list_
.end();
3228 switch((*p
)->type())
3230 case elfcpp::SHT_FINI_ARRAY
:
3231 odyn
->add_section_address(elfcpp::DT_FINI_ARRAY
, *p
);
3232 odyn
->add_section_size(elfcpp::DT_FINI_ARRAYSZ
, *p
);
3234 case elfcpp::SHT_INIT_ARRAY
:
3235 odyn
->add_section_address(elfcpp::DT_INIT_ARRAY
, *p
);
3236 odyn
->add_section_size(elfcpp::DT_INIT_ARRAYSZ
, *p
);
3238 case elfcpp::SHT_PREINIT_ARRAY
:
3239 odyn
->add_section_address(elfcpp::DT_PREINIT_ARRAY
, *p
);
3240 odyn
->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ
, *p
);
3246 // Add a DT_RPATH entry if needed.
3247 const General_options::Dir_list
& rpath(parameters
->options().rpath());
3250 std::string rpath_val
;
3251 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
3255 if (rpath_val
.empty())
3256 rpath_val
= p
->name();
3259 // Eliminate duplicates.
3260 General_options::Dir_list::const_iterator q
;
3261 for (q
= rpath
.begin(); q
!= p
; ++q
)
3262 if (q
->name() == p
->name())
3267 rpath_val
+= p
->name();
3272 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
3273 if (parameters
->options().enable_new_dtags())
3274 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
3277 // Look for text segments that have dynamic relocations.
3278 bool have_textrel
= false;
3279 if (!this->script_options_
->saw_sections_clause())
3281 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3282 p
!= this->segment_list_
.end();
3285 if (((*p
)->flags() & elfcpp::PF_W
) == 0
3286 && (*p
)->dynamic_reloc_count() > 0)
3288 have_textrel
= true;
3295 // We don't know the section -> segment mapping, so we are
3296 // conservative and just look for readonly sections with
3297 // relocations. If those sections wind up in writable segments,
3298 // then we have created an unnecessary DT_TEXTREL entry.
3299 for (Section_list::const_iterator p
= this->section_list_
.begin();
3300 p
!= this->section_list_
.end();
3303 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
3304 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
3305 && ((*p
)->dynamic_reloc_count() > 0))
3307 have_textrel
= true;
3313 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3314 // post-link tools can easily modify these flags if desired.
3315 unsigned int flags
= 0;
3318 // Add a DT_TEXTREL for compatibility with older loaders.
3319 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
3320 flags
|= elfcpp::DF_TEXTREL
;
3322 if (parameters
->options().shared() && this->has_static_tls())
3323 flags
|= elfcpp::DF_STATIC_TLS
;
3324 if (parameters
->options().origin())
3325 flags
|= elfcpp::DF_ORIGIN
;
3326 if (parameters
->options().Bsymbolic())
3328 flags
|= elfcpp::DF_SYMBOLIC
;
3329 // Add DT_SYMBOLIC for compatibility with older loaders.
3330 odyn
->add_constant(elfcpp::DT_SYMBOLIC
, 0);
3332 if (parameters
->options().now())
3333 flags
|= elfcpp::DF_BIND_NOW
;
3334 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
3337 if (parameters
->options().initfirst())
3338 flags
|= elfcpp::DF_1_INITFIRST
;
3339 if (parameters
->options().interpose())
3340 flags
|= elfcpp::DF_1_INTERPOSE
;
3341 if (parameters
->options().loadfltr())
3342 flags
|= elfcpp::DF_1_LOADFLTR
;
3343 if (parameters
->options().nodefaultlib())
3344 flags
|= elfcpp::DF_1_NODEFLIB
;
3345 if (parameters
->options().nodelete())
3346 flags
|= elfcpp::DF_1_NODELETE
;
3347 if (parameters
->options().nodlopen())
3348 flags
|= elfcpp::DF_1_NOOPEN
;
3349 if (parameters
->options().nodump())
3350 flags
|= elfcpp::DF_1_NODUMP
;
3351 if (!parameters
->options().shared())
3352 flags
&= ~(elfcpp::DF_1_INITFIRST
3353 | elfcpp::DF_1_NODELETE
3354 | elfcpp::DF_1_NOOPEN
);
3355 if (parameters
->options().origin())
3356 flags
|= elfcpp::DF_1_ORIGIN
;
3357 if (parameters
->options().now())
3358 flags
|= elfcpp::DF_1_NOW
;
3360 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
3363 // Set the size of the _DYNAMIC symbol table to be the size of the
3367 Layout::set_dynamic_symbol_size(const Symbol_table
* symtab
)
3369 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3370 odyn
->finalize_data_size();
3371 off_t data_size
= odyn
->data_size();
3372 const int size
= parameters
->target().get_size();
3374 symtab
->get_sized_symbol
<32>(this->dynamic_symbol_
)->set_symsize(data_size
);
3375 else if (size
== 64)
3376 symtab
->get_sized_symbol
<64>(this->dynamic_symbol_
)->set_symsize(data_size
);
3381 // The mapping of input section name prefixes to output section names.
3382 // In some cases one prefix is itself a prefix of another prefix; in
3383 // such a case the longer prefix must come first. These prefixes are
3384 // based on the GNU linker default ELF linker script.
3386 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3387 const Layout::Section_name_mapping
Layout::section_name_mapping
[] =
3389 MAPPING_INIT(".text.", ".text"),
3390 MAPPING_INIT(".ctors.", ".ctors"),
3391 MAPPING_INIT(".dtors.", ".dtors"),
3392 MAPPING_INIT(".rodata.", ".rodata"),
3393 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3394 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3395 MAPPING_INIT(".data.", ".data"),
3396 MAPPING_INIT(".bss.", ".bss"),
3397 MAPPING_INIT(".tdata.", ".tdata"),
3398 MAPPING_INIT(".tbss.", ".tbss"),
3399 MAPPING_INIT(".init_array.", ".init_array"),
3400 MAPPING_INIT(".fini_array.", ".fini_array"),
3401 MAPPING_INIT(".sdata.", ".sdata"),
3402 MAPPING_INIT(".sbss.", ".sbss"),
3403 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3404 // differently depending on whether it is creating a shared library.
3405 MAPPING_INIT(".sdata2.", ".sdata"),
3406 MAPPING_INIT(".sbss2.", ".sbss"),
3407 MAPPING_INIT(".lrodata.", ".lrodata"),
3408 MAPPING_INIT(".ldata.", ".ldata"),
3409 MAPPING_INIT(".lbss.", ".lbss"),
3410 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3411 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3412 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3413 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3414 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3415 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3416 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3417 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3418 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3419 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3420 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3421 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3422 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3423 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3424 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3425 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3426 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3427 MAPPING_INIT(".ARM.extab.", ".ARM.extab"),
3428 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3429 MAPPING_INIT(".ARM.exidx.", ".ARM.exidx"),
3430 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3434 const int Layout::section_name_mapping_count
=
3435 (sizeof(Layout::section_name_mapping
)
3436 / sizeof(Layout::section_name_mapping
[0]));
3438 // Choose the output section name to use given an input section name.
3439 // Set *PLEN to the length of the name. *PLEN is initialized to the
3443 Layout::output_section_name(const char* name
, size_t* plen
)
3445 // gcc 4.3 generates the following sorts of section names when it
3446 // needs a section name specific to a function:
3452 // .data.rel.local.FN
3454 // .data.rel.ro.local.FN
3461 // The GNU linker maps all of those to the part before the .FN,
3462 // except that .data.rel.local.FN is mapped to .data, and
3463 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3464 // beginning with .data.rel.ro.local are grouped together.
3466 // For an anonymous namespace, the string FN can contain a '.'.
3468 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3469 // GNU linker maps to .rodata.
3471 // The .data.rel.ro sections are used with -z relro. The sections
3472 // are recognized by name. We use the same names that the GNU
3473 // linker does for these sections.
3475 // It is hard to handle this in a principled way, so we don't even
3476 // try. We use a table of mappings. If the input section name is
3477 // not found in the table, we simply use it as the output section
3480 const Section_name_mapping
* psnm
= section_name_mapping
;
3481 for (int i
= 0; i
< section_name_mapping_count
; ++i
, ++psnm
)
3483 if (strncmp(name
, psnm
->from
, psnm
->fromlen
) == 0)
3485 *plen
= psnm
->tolen
;
3493 // Check if a comdat group or .gnu.linkonce section with the given
3494 // NAME is selected for the link. If there is already a section,
3495 // *KEPT_SECTION is set to point to the existing section and the
3496 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3497 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3498 // *KEPT_SECTION is set to the internal copy and the function returns
3502 Layout::find_or_add_kept_section(const std::string
& name
,
3507 Kept_section
** kept_section
)
3509 // It's normal to see a couple of entries here, for the x86 thunk
3510 // sections. If we see more than a few, we're linking a C++
3511 // program, and we resize to get more space to minimize rehashing.
3512 if (this->signatures_
.size() > 4
3513 && !this->resized_signatures_
)
3515 reserve_unordered_map(&this->signatures_
,
3516 this->number_of_input_files_
* 64);
3517 this->resized_signatures_
= true;
3520 Kept_section candidate
;
3521 std::pair
<Signatures::iterator
, bool> ins
=
3522 this->signatures_
.insert(std::make_pair(name
, candidate
));
3524 if (kept_section
!= NULL
)
3525 *kept_section
= &ins
.first
->second
;
3528 // This is the first time we've seen this signature.
3529 ins
.first
->second
.set_object(object
);
3530 ins
.first
->second
.set_shndx(shndx
);
3532 ins
.first
->second
.set_is_comdat();
3534 ins
.first
->second
.set_is_group_name();
3538 // We have already seen this signature.
3540 if (ins
.first
->second
.is_group_name())
3542 // We've already seen a real section group with this signature.
3543 // If the kept group is from a plugin object, and we're in the
3544 // replacement phase, accept the new one as a replacement.
3545 if (ins
.first
->second
.object() == NULL
3546 && parameters
->options().plugins()->in_replacement_phase())
3548 ins
.first
->second
.set_object(object
);
3549 ins
.first
->second
.set_shndx(shndx
);
3554 else if (is_group_name
)
3556 // This is a real section group, and we've already seen a
3557 // linkonce section with this signature. Record that we've seen
3558 // a section group, and don't include this section group.
3559 ins
.first
->second
.set_is_group_name();
3564 // We've already seen a linkonce section and this is a linkonce
3565 // section. These don't block each other--this may be the same
3566 // symbol name with different section types.
3571 // Store the allocated sections into the section list.
3574 Layout::get_allocated_sections(Section_list
* section_list
) const
3576 for (Section_list::const_iterator p
= this->section_list_
.begin();
3577 p
!= this->section_list_
.end();
3579 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
3580 section_list
->push_back(*p
);
3583 // Create an output segment.
3586 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3588 gold_assert(!parameters
->options().relocatable());
3589 Output_segment
* oseg
= new Output_segment(type
, flags
);
3590 this->segment_list_
.push_back(oseg
);
3592 if (type
== elfcpp::PT_TLS
)
3593 this->tls_segment_
= oseg
;
3594 else if (type
== elfcpp::PT_GNU_RELRO
)
3595 this->relro_segment_
= oseg
;
3600 // Write out the Output_sections. Most won't have anything to write,
3601 // since most of the data will come from input sections which are
3602 // handled elsewhere. But some Output_sections do have Output_data.
3605 Layout::write_output_sections(Output_file
* of
) const
3607 for (Section_list::const_iterator p
= this->section_list_
.begin();
3608 p
!= this->section_list_
.end();
3611 if (!(*p
)->after_input_sections())
3616 // Write out data not associated with a section or the symbol table.
3619 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
3621 if (!parameters
->options().strip_all())
3623 const Output_section
* symtab_section
= this->symtab_section_
;
3624 for (Section_list::const_iterator p
= this->section_list_
.begin();
3625 p
!= this->section_list_
.end();
3628 if ((*p
)->needs_symtab_index())
3630 gold_assert(symtab_section
!= NULL
);
3631 unsigned int index
= (*p
)->symtab_index();
3632 gold_assert(index
> 0 && index
!= -1U);
3633 off_t off
= (symtab_section
->offset()
3634 + index
* symtab_section
->entsize());
3635 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
3640 const Output_section
* dynsym_section
= this->dynsym_section_
;
3641 for (Section_list::const_iterator p
= this->section_list_
.begin();
3642 p
!= this->section_list_
.end();
3645 if ((*p
)->needs_dynsym_index())
3647 gold_assert(dynsym_section
!= NULL
);
3648 unsigned int index
= (*p
)->dynsym_index();
3649 gold_assert(index
> 0 && index
!= -1U);
3650 off_t off
= (dynsym_section
->offset()
3651 + index
* dynsym_section
->entsize());
3652 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
3656 // Write out the Output_data which are not in an Output_section.
3657 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
3658 p
!= this->special_output_list_
.end();
3663 // Write out the Output_sections which can only be written after the
3664 // input sections are complete.
3667 Layout::write_sections_after_input_sections(Output_file
* of
)
3669 // Determine the final section offsets, and thus the final output
3670 // file size. Note we finalize the .shstrab last, to allow the
3671 // after_input_section sections to modify their section-names before
3673 if (this->any_postprocessing_sections_
)
3675 off_t off
= this->output_file_size_
;
3676 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
3678 // Now that we've finalized the names, we can finalize the shstrab.
3680 this->set_section_offsets(off
,
3681 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
3683 if (off
> this->output_file_size_
)
3686 this->output_file_size_
= off
;
3690 for (Section_list::const_iterator p
= this->section_list_
.begin();
3691 p
!= this->section_list_
.end();
3694 if ((*p
)->after_input_sections())
3698 this->section_headers_
->write(of
);
3701 // If the build ID requires computing a checksum, do so here, and
3702 // write it out. We compute a checksum over the entire file because
3703 // that is simplest.
3706 Layout::write_build_id(Output_file
* of
) const
3708 if (this->build_id_note_
== NULL
)
3711 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3713 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3714 this->build_id_note_
->data_size());
3716 const char* style
= parameters
->options().build_id();
3717 if (strcmp(style
, "sha1") == 0)
3720 sha1_init_ctx(&ctx
);
3721 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3722 sha1_finish_ctx(&ctx
, ov
);
3724 else if (strcmp(style
, "md5") == 0)
3728 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3729 md5_finish_ctx(&ctx
, ov
);
3734 of
->write_output_view(this->build_id_note_
->offset(),
3735 this->build_id_note_
->data_size(),
3738 of
->free_input_view(0, this->output_file_size_
, iv
);
3741 // Write out a binary file. This is called after the link is
3742 // complete. IN is the temporary output file we used to generate the
3743 // ELF code. We simply walk through the segments, read them from
3744 // their file offset in IN, and write them to their load address in
3745 // the output file. FIXME: with a bit more work, we could support
3746 // S-records and/or Intel hex format here.
3749 Layout::write_binary(Output_file
* in
) const
3751 gold_assert(parameters
->options().oformat_enum()
3752 == General_options::OBJECT_FORMAT_BINARY
);
3754 // Get the size of the binary file.
3755 uint64_t max_load_address
= 0;
3756 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3757 p
!= this->segment_list_
.end();
3760 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3762 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3763 if (max_paddr
> max_load_address
)
3764 max_load_address
= max_paddr
;
3768 Output_file
out(parameters
->options().output_file_name());
3769 out
.open(max_load_address
);
3771 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3772 p
!= this->segment_list_
.end();
3775 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3777 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3779 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3781 memcpy(vout
, vin
, (*p
)->filesz());
3782 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3783 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3790 // Print the output sections to the map file.
3793 Layout::print_to_mapfile(Mapfile
* mapfile
) const
3795 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3796 p
!= this->segment_list_
.end();
3798 (*p
)->print_sections_to_mapfile(mapfile
);
3801 // Print statistical information to stderr. This is used for --stats.
3804 Layout::print_stats() const
3806 this->namepool_
.print_stats("section name pool");
3807 this->sympool_
.print_stats("output symbol name pool");
3808 this->dynpool_
.print_stats("dynamic name pool");
3810 for (Section_list::const_iterator p
= this->section_list_
.begin();
3811 p
!= this->section_list_
.end();
3813 (*p
)->print_merge_stats();
3816 // Write_sections_task methods.
3818 // We can always run this task.
3821 Write_sections_task::is_runnable()
3826 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3830 Write_sections_task::locks(Task_locker
* tl
)
3832 tl
->add(this, this->output_sections_blocker_
);
3833 tl
->add(this, this->final_blocker_
);
3836 // Run the task--write out the data.
3839 Write_sections_task::run(Workqueue
*)
3841 this->layout_
->write_output_sections(this->of_
);
3844 // Write_data_task methods.
3846 // We can always run this task.
3849 Write_data_task::is_runnable()
3854 // We need to unlock FINAL_BLOCKER when finished.
3857 Write_data_task::locks(Task_locker
* tl
)
3859 tl
->add(this, this->final_blocker_
);
3862 // Run the task--write out the data.
3865 Write_data_task::run(Workqueue
*)
3867 this->layout_
->write_data(this->symtab_
, this->of_
);
3870 // Write_symbols_task methods.
3872 // We can always run this task.
3875 Write_symbols_task::is_runnable()
3880 // We need to unlock FINAL_BLOCKER when finished.
3883 Write_symbols_task::locks(Task_locker
* tl
)
3885 tl
->add(this, this->final_blocker_
);
3888 // Run the task--write out the symbols.
3891 Write_symbols_task::run(Workqueue
*)
3893 this->symtab_
->write_globals(this->sympool_
, this->dynpool_
,
3894 this->layout_
->symtab_xindex(),
3895 this->layout_
->dynsym_xindex(), this->of_
);
3898 // Write_after_input_sections_task methods.
3900 // We can only run this task after the input sections have completed.
3903 Write_after_input_sections_task::is_runnable()
3905 if (this->input_sections_blocker_
->is_blocked())
3906 return this->input_sections_blocker_
;
3910 // We need to unlock FINAL_BLOCKER when finished.
3913 Write_after_input_sections_task::locks(Task_locker
* tl
)
3915 tl
->add(this, this->final_blocker_
);
3921 Write_after_input_sections_task::run(Workqueue
*)
3923 this->layout_
->write_sections_after_input_sections(this->of_
);
3926 // Close_task_runner methods.
3928 // Run the task--close the file.
3931 Close_task_runner::run(Workqueue
*, const Task
*)
3933 // If we need to compute a checksum for the BUILD if, we do so here.
3934 this->layout_
->write_build_id(this->of_
);
3936 // If we've been asked to create a binary file, we do so here.
3937 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3938 this->layout_
->write_binary(this->of_
);
3943 // Instantiate the templates we need. We could use the configure
3944 // script to restrict this to only the ones for implemented targets.
3946 #ifdef HAVE_TARGET_32_LITTLE
3949 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3951 const elfcpp::Shdr
<32, false>& shdr
,
3952 unsigned int, unsigned int, off_t
*);
3955 #ifdef HAVE_TARGET_32_BIG
3958 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3960 const elfcpp::Shdr
<32, true>& shdr
,
3961 unsigned int, unsigned int, off_t
*);
3964 #ifdef HAVE_TARGET_64_LITTLE
3967 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3969 const elfcpp::Shdr
<64, false>& shdr
,
3970 unsigned int, unsigned int, off_t
*);
3973 #ifdef HAVE_TARGET_64_BIG
3976 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3978 const elfcpp::Shdr
<64, true>& shdr
,
3979 unsigned int, unsigned int, off_t
*);
3982 #ifdef HAVE_TARGET_32_LITTLE
3985 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
3986 unsigned int reloc_shndx
,
3987 const elfcpp::Shdr
<32, false>& shdr
,
3988 Output_section
* data_section
,
3989 Relocatable_relocs
* rr
);
3992 #ifdef HAVE_TARGET_32_BIG
3995 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
3996 unsigned int reloc_shndx
,
3997 const elfcpp::Shdr
<32, true>& shdr
,
3998 Output_section
* data_section
,
3999 Relocatable_relocs
* rr
);
4002 #ifdef HAVE_TARGET_64_LITTLE
4005 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
4006 unsigned int reloc_shndx
,
4007 const elfcpp::Shdr
<64, false>& shdr
,
4008 Output_section
* data_section
,
4009 Relocatable_relocs
* rr
);
4012 #ifdef HAVE_TARGET_64_BIG
4015 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
4016 unsigned int reloc_shndx
,
4017 const elfcpp::Shdr
<64, true>& shdr
,
4018 Output_section
* data_section
,
4019 Relocatable_relocs
* rr
);
4022 #ifdef HAVE_TARGET_32_LITTLE
4025 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
4026 Sized_relobj
<32, false>* object
,
4028 const char* group_section_name
,
4029 const char* signature
,
4030 const elfcpp::Shdr
<32, false>& shdr
,
4031 elfcpp::Elf_Word flags
,
4032 std::vector
<unsigned int>* shndxes
);
4035 #ifdef HAVE_TARGET_32_BIG
4038 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
4039 Sized_relobj
<32, true>* object
,
4041 const char* group_section_name
,
4042 const char* signature
,
4043 const elfcpp::Shdr
<32, true>& shdr
,
4044 elfcpp::Elf_Word flags
,
4045 std::vector
<unsigned int>* shndxes
);
4048 #ifdef HAVE_TARGET_64_LITTLE
4051 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
4052 Sized_relobj
<64, false>* object
,
4054 const char* group_section_name
,
4055 const char* signature
,
4056 const elfcpp::Shdr
<64, false>& shdr
,
4057 elfcpp::Elf_Word flags
,
4058 std::vector
<unsigned int>* shndxes
);
4061 #ifdef HAVE_TARGET_64_BIG
4064 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
4065 Sized_relobj
<64, true>* object
,
4067 const char* group_section_name
,
4068 const char* signature
,
4069 const elfcpp::Shdr
<64, true>& shdr
,
4070 elfcpp::Elf_Word flags
,
4071 std::vector
<unsigned int>* shndxes
);
4074 #ifdef HAVE_TARGET_32_LITTLE
4077 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
4078 const unsigned char* symbols
,
4080 const unsigned char* symbol_names
,
4081 off_t symbol_names_size
,
4083 const elfcpp::Shdr
<32, false>& shdr
,
4084 unsigned int reloc_shndx
,
4085 unsigned int reloc_type
,
4089 #ifdef HAVE_TARGET_32_BIG
4092 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
4093 const unsigned char* symbols
,
4095 const unsigned char* symbol_names
,
4096 off_t symbol_names_size
,
4098 const elfcpp::Shdr
<32, true>& shdr
,
4099 unsigned int reloc_shndx
,
4100 unsigned int reloc_type
,
4104 #ifdef HAVE_TARGET_64_LITTLE
4107 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
4108 const unsigned char* symbols
,
4110 const unsigned char* symbol_names
,
4111 off_t symbol_names_size
,
4113 const elfcpp::Shdr
<64, false>& shdr
,
4114 unsigned int reloc_shndx
,
4115 unsigned int reloc_type
,
4119 #ifdef HAVE_TARGET_64_BIG
4122 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
4123 const unsigned char* symbols
,
4125 const unsigned char* symbol_names
,
4126 off_t symbol_names_size
,
4128 const elfcpp::Shdr
<64, true>& shdr
,
4129 unsigned int reloc_shndx
,
4130 unsigned int reloc_type
,
4134 } // End namespace gold.