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"
55 // Layout_task_runner methods.
57 // Lay out the sections. This is called after all the input objects
61 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
63 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
68 // Now we know the final size of the output file and we know where
69 // each piece of information goes.
71 if (this->mapfile_
!= NULL
)
73 this->mapfile_
->print_discarded_sections(this->input_objects_
);
74 this->layout_
->print_to_mapfile(this->mapfile_
);
77 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
78 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
79 of
->set_is_temporary();
82 // Queue up the final set of tasks.
83 gold::queue_final_tasks(this->options_
, this->input_objects_
,
84 this->symtab_
, this->layout_
, workqueue
, of
);
89 Layout::Layout(const General_options
& options
, Script_options
* script_options
)
91 script_options_(script_options
),
99 unattached_section_list_(),
100 sections_are_attached_(false),
101 special_output_list_(),
102 section_headers_(NULL
),
104 relro_segment_(NULL
),
105 symtab_section_(NULL
),
106 symtab_xindex_(NULL
),
107 dynsym_section_(NULL
),
108 dynsym_xindex_(NULL
),
109 dynamic_section_(NULL
),
111 eh_frame_section_(NULL
),
112 eh_frame_data_(NULL
),
113 added_eh_frame_data_(false),
114 eh_frame_hdr_section_(NULL
),
115 build_id_note_(NULL
),
119 output_file_size_(-1),
120 input_requires_executable_stack_(false),
121 input_with_gnu_stack_note_(false),
122 input_without_gnu_stack_note_(false),
123 has_static_tls_(false),
124 any_postprocessing_sections_(false)
126 // Make space for more than enough segments for a typical file.
127 // This is just for efficiency--it's OK if we wind up needing more.
128 this->segment_list_
.reserve(12);
130 // We expect two unattached Output_data objects: the file header and
131 // the segment headers.
132 this->special_output_list_
.reserve(2);
135 // Hash a key we use to look up an output section mapping.
138 Layout::Hash_key::operator()(const Layout::Key
& k
) const
140 return k
.first
+ k
.second
.first
+ k
.second
.second
;
143 // Returns whether the given section is in the list of
144 // debug-sections-used-by-some-version-of-gdb. Currently,
145 // we've checked versions of gdb up to and including 6.7.1.
147 static const char* gdb_sections
[] =
149 // ".debug_aranges", // not used by gdb as of 6.7.1
155 // ".debug_pubnames", // not used by gdb as of 6.7.1
160 static const char* lines_only_debug_sections
[] =
162 // ".debug_aranges", // not used by gdb as of 6.7.1
168 // ".debug_pubnames", // not used by gdb as of 6.7.1
174 is_gdb_debug_section(const char* str
)
176 // We can do this faster: binary search or a hashtable. But why bother?
177 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
178 if (strcmp(str
, gdb_sections
[i
]) == 0)
184 is_lines_only_debug_section(const char* str
)
186 // We can do this faster: binary search or a hashtable. But why bother?
188 i
< sizeof(lines_only_debug_sections
)/sizeof(*lines_only_debug_sections
);
190 if (strcmp(str
, lines_only_debug_sections
[i
]) == 0)
195 // Whether to include this section in the link.
197 template<int size
, bool big_endian
>
199 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
200 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
202 if (shdr
.get_sh_flags() & elfcpp::SHF_EXCLUDE
)
205 switch (shdr
.get_sh_type())
207 case elfcpp::SHT_NULL
:
208 case elfcpp::SHT_SYMTAB
:
209 case elfcpp::SHT_DYNSYM
:
210 case elfcpp::SHT_HASH
:
211 case elfcpp::SHT_DYNAMIC
:
212 case elfcpp::SHT_SYMTAB_SHNDX
:
215 case elfcpp::SHT_STRTAB
:
216 // Discard the sections which have special meanings in the ELF
217 // ABI. Keep others (e.g., .stabstr). We could also do this by
218 // checking the sh_link fields of the appropriate sections.
219 return (strcmp(name
, ".dynstr") != 0
220 && strcmp(name
, ".strtab") != 0
221 && strcmp(name
, ".shstrtab") != 0);
223 case elfcpp::SHT_RELA
:
224 case elfcpp::SHT_REL
:
225 case elfcpp::SHT_GROUP
:
226 // If we are emitting relocations these should be handled
228 gold_assert(!parameters
->options().relocatable()
229 && !parameters
->options().emit_relocs());
232 case elfcpp::SHT_PROGBITS
:
233 if (parameters
->options().strip_debug()
234 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
236 if (is_debug_info_section(name
))
239 if (parameters
->options().strip_debug_non_line()
240 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
242 // Debugging sections can only be recognized by name.
243 if (is_prefix_of(".debug", name
)
244 && !is_lines_only_debug_section(name
))
247 if (parameters
->options().strip_debug_gdb()
248 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
250 // Debugging sections can only be recognized by name.
251 if (is_prefix_of(".debug", name
)
252 && !is_gdb_debug_section(name
))
255 if (parameters
->options().strip_lto_sections()
256 && !parameters
->options().relocatable()
257 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
259 // Ignore LTO sections containing intermediate code.
260 if (is_prefix_of(".gnu.lto_", name
))
270 // Return an output section named NAME, or NULL if there is none.
273 Layout::find_output_section(const char* name
) const
275 for (Section_list::const_iterator p
= this->section_list_
.begin();
276 p
!= this->section_list_
.end();
278 if (strcmp((*p
)->name(), name
) == 0)
283 // Return an output segment of type TYPE, with segment flags SET set
284 // and segment flags CLEAR clear. Return NULL if there is none.
287 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
288 elfcpp::Elf_Word clear
) const
290 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
291 p
!= this->segment_list_
.end();
293 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
294 && ((*p
)->flags() & set
) == set
295 && ((*p
)->flags() & clear
) == 0)
300 // Return the output section to use for section NAME with type TYPE
301 // and section flags FLAGS. NAME must be canonicalized in the string
302 // pool, and NAME_KEY is the key.
305 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
306 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
308 elfcpp::Elf_Xword lookup_flags
= flags
;
310 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
311 // read-write with read-only sections. Some other ELF linkers do
312 // not do this. FIXME: Perhaps there should be an option
314 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
316 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
317 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
318 std::pair
<Section_name_map::iterator
, bool> ins(
319 this->section_name_map_
.insert(v
));
322 return ins
.first
->second
;
325 // This is the first time we've seen this name/type/flags
326 // combination. For compatibility with the GNU linker, we
327 // combine sections with contents and zero flags with sections
328 // with non-zero flags. This is a workaround for cases where
329 // assembler code forgets to set section flags. FIXME: Perhaps
330 // there should be an option to control this.
331 Output_section
* os
= NULL
;
333 if (type
== elfcpp::SHT_PROGBITS
)
337 Output_section
* same_name
= this->find_output_section(name
);
338 if (same_name
!= NULL
339 && same_name
->type() == elfcpp::SHT_PROGBITS
340 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
343 else if ((flags
& elfcpp::SHF_TLS
) == 0)
345 elfcpp::Elf_Xword zero_flags
= 0;
346 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
347 Section_name_map::iterator p
=
348 this->section_name_map_
.find(zero_key
);
349 if (p
!= this->section_name_map_
.end())
355 os
= this->make_output_section(name
, type
, flags
);
356 ins
.first
->second
= os
;
361 // Pick the output section to use for section NAME, in input file
362 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
363 // linker created section. IS_INPUT_SECTION is true if we are
364 // choosing an output section for an input section found in a input
365 // file. This will return NULL if the input section should be
369 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
370 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
371 bool is_input_section
)
373 // We should not see any input sections after we have attached
374 // sections to segments.
375 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
377 // Some flags in the input section should not be automatically
378 // copied to the output section.
379 flags
&= ~ (elfcpp::SHF_INFO_LINK
380 | elfcpp::SHF_LINK_ORDER
383 | elfcpp::SHF_STRINGS
);
385 if (this->script_options_
->saw_sections_clause())
387 // We are using a SECTIONS clause, so the output section is
388 // chosen based only on the name.
390 Script_sections
* ss
= this->script_options_
->script_sections();
391 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
392 Output_section
** output_section_slot
;
393 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
396 // The SECTIONS clause says to discard this input section.
400 // If this is an orphan section--one not mentioned in the linker
401 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
402 // default processing below.
404 if (output_section_slot
!= NULL
)
406 if (*output_section_slot
!= NULL
)
407 return *output_section_slot
;
409 // We don't put sections found in the linker script into
410 // SECTION_NAME_MAP_. That keeps us from getting confused
411 // if an orphan section is mapped to a section with the same
412 // name as one in the linker script.
414 name
= this->namepool_
.add(name
, false, NULL
);
416 Output_section
* os
= this->make_output_section(name
, type
, flags
);
417 os
->set_found_in_sections_clause();
418 *output_section_slot
= os
;
423 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
425 // Turn NAME from the name of the input section into the name of the
428 size_t len
= strlen(name
);
430 && !this->script_options_
->saw_sections_clause()
431 && !parameters
->options().relocatable())
432 name
= Layout::output_section_name(name
, &len
);
434 Stringpool::Key name_key
;
435 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
437 // Find or make the output section. The output section is selected
438 // based on the section name, type, and flags.
439 return this->get_output_section(name
, name_key
, type
, flags
);
442 // Return the output section to use for input section SHNDX, with name
443 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
444 // index of a relocation section which applies to this section, or 0
445 // if none, or -1U if more than one. RELOC_TYPE is the type of the
446 // relocation section if there is one. Set *OFF to the offset of this
447 // input section without the output section. Return NULL if the
448 // section should be discarded. Set *OFF to -1 if the section
449 // contents should not be written directly to the output file, but
450 // will instead receive special handling.
452 template<int size
, bool big_endian
>
454 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
455 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
456 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
460 if (!this->include_section(object
, name
, shdr
))
465 // In a relocatable link a grouped section must not be combined with
466 // any other sections.
467 if (parameters
->options().relocatable()
468 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
470 name
= this->namepool_
.add(name
, true, NULL
);
471 os
= this->make_output_section(name
, shdr
.get_sh_type(),
472 shdr
.get_sh_flags());
476 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
477 shdr
.get_sh_flags(), true);
482 // By default the GNU linker sorts input sections whose names match
483 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
484 // are sorted by name. This is used to implement constructor
485 // priority ordering. We are compatible.
486 if (!this->script_options_
->saw_sections_clause()
487 && (is_prefix_of(".ctors.", name
)
488 || is_prefix_of(".dtors.", name
)
489 || is_prefix_of(".init_array.", name
)
490 || is_prefix_of(".fini_array.", name
)))
491 os
->set_must_sort_attached_input_sections();
493 // FIXME: Handle SHF_LINK_ORDER somewhere.
495 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
496 this->script_options_
->saw_sections_clause());
501 // Handle a relocation section when doing a relocatable link.
503 template<int size
, bool big_endian
>
505 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
507 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
508 Output_section
* data_section
,
509 Relocatable_relocs
* rr
)
511 gold_assert(parameters
->options().relocatable()
512 || parameters
->options().emit_relocs());
514 int sh_type
= shdr
.get_sh_type();
517 if (sh_type
== elfcpp::SHT_REL
)
519 else if (sh_type
== elfcpp::SHT_RELA
)
523 name
+= data_section
->name();
525 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
530 os
->set_should_link_to_symtab();
531 os
->set_info_section(data_section
);
533 Output_section_data
* posd
;
534 if (sh_type
== elfcpp::SHT_REL
)
536 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
537 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
541 else if (sh_type
== elfcpp::SHT_RELA
)
543 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
544 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
551 os
->add_output_section_data(posd
);
552 rr
->set_output_data(posd
);
557 // Handle a group section when doing a relocatable link.
559 template<int size
, bool big_endian
>
561 Layout::layout_group(Symbol_table
* symtab
,
562 Sized_relobj
<size
, big_endian
>* object
,
564 const char* group_section_name
,
565 const char* signature
,
566 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
567 elfcpp::Elf_Word flags
,
568 std::vector
<unsigned int>* shndxes
)
570 gold_assert(parameters
->options().relocatable());
571 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
572 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
573 Output_section
* os
= this->make_output_section(group_section_name
,
575 shdr
.get_sh_flags());
577 // We need to find a symbol with the signature in the symbol table.
578 // If we don't find one now, we need to look again later.
579 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
581 os
->set_info_symndx(sym
);
584 // We will wind up using a symbol whose name is the signature.
585 // So just put the signature in the symbol name pool to save it.
586 signature
= symtab
->canonicalize_name(signature
);
587 this->group_signatures_
.push_back(Group_signature(os
, signature
));
590 os
->set_should_link_to_symtab();
593 section_size_type entry_count
=
594 convert_to_section_size_type(shdr
.get_sh_size() / 4);
595 Output_section_data
* posd
=
596 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
598 os
->add_output_section_data(posd
);
601 // Special GNU handling of sections name .eh_frame. They will
602 // normally hold exception frame data as defined by the C++ ABI
603 // (http://codesourcery.com/cxx-abi/).
605 template<int size
, bool big_endian
>
607 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
608 const unsigned char* symbols
,
610 const unsigned char* symbol_names
,
611 off_t symbol_names_size
,
613 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
614 unsigned int reloc_shndx
, unsigned int reloc_type
,
617 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
618 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
620 const char* const name
= ".eh_frame";
621 Output_section
* os
= this->choose_output_section(object
,
623 elfcpp::SHT_PROGBITS
,
629 if (this->eh_frame_section_
== NULL
)
631 this->eh_frame_section_
= os
;
632 this->eh_frame_data_
= new Eh_frame();
634 if (this->options_
.eh_frame_hdr())
636 Output_section
* hdr_os
=
637 this->choose_output_section(NULL
,
639 elfcpp::SHT_PROGBITS
,
645 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
646 this->eh_frame_data_
);
647 hdr_os
->add_output_section_data(hdr_posd
);
649 hdr_os
->set_after_input_sections();
651 if (!this->script_options_
->saw_phdrs_clause())
653 Output_segment
* hdr_oseg
;
654 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
656 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
);
659 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
664 gold_assert(this->eh_frame_section_
== os
);
666 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
675 os
->update_flags_for_input_section(shdr
.get_sh_flags());
677 // We found a .eh_frame section we are going to optimize, so now
678 // we can add the set of optimized sections to the output
679 // section. We need to postpone adding this until we've found a
680 // section we can optimize so that the .eh_frame section in
681 // crtbegin.o winds up at the start of the output section.
682 if (!this->added_eh_frame_data_
)
684 os
->add_output_section_data(this->eh_frame_data_
);
685 this->added_eh_frame_data_
= true;
691 // We couldn't handle this .eh_frame section for some reason.
692 // Add it as a normal section.
693 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
694 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
695 saw_sections_clause
);
701 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
702 // the output section.
705 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
706 elfcpp::Elf_Xword flags
,
707 Output_section_data
* posd
)
709 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
712 os
->add_output_section_data(posd
);
716 // Map section flags to segment flags.
719 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
721 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
722 if ((flags
& elfcpp::SHF_WRITE
) != 0)
724 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
729 // Sometimes we compress sections. This is typically done for
730 // sections that are not part of normal program execution (such as
731 // .debug_* sections), and where the readers of these sections know
732 // how to deal with compressed sections. (To make it easier for them,
733 // we will rename the ouput section in such cases from .foo to
734 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
735 // doesn't say for certain whether we'll compress -- it depends on
736 // commandline options as well -- just whether this section is a
737 // candidate for compression.
740 is_compressible_debug_section(const char* secname
)
742 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
745 // Make a new Output_section, and attach it to segments as
749 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
750 elfcpp::Elf_Xword flags
)
753 if ((flags
& elfcpp::SHF_ALLOC
) == 0
754 && strcmp(this->options_
.compress_debug_sections(), "none") != 0
755 && is_compressible_debug_section(name
))
756 os
= new Output_compressed_section(&this->options_
, name
, type
, flags
);
758 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
759 && this->options_
.strip_debug_non_line()
760 && strcmp(".debug_abbrev", name
) == 0)
762 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
764 if (this->debug_info_
)
765 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
767 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
768 && this->options_
.strip_debug_non_line()
769 && strcmp(".debug_info", name
) == 0)
771 os
= this->debug_info_
= new Output_reduced_debug_info_section(
773 if (this->debug_abbrev_
)
774 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
777 os
= new Output_section(name
, type
, flags
);
779 this->section_list_
.push_back(os
);
781 // The GNU linker by default sorts some sections by priority, so we
782 // do the same. We need to know that this might happen before we
783 // attach any input sections.
784 if (!this->script_options_
->saw_sections_clause()
785 && (strcmp(name
, ".ctors") == 0
786 || strcmp(name
, ".dtors") == 0
787 || strcmp(name
, ".init_array") == 0
788 || strcmp(name
, ".fini_array") == 0))
789 os
->set_may_sort_attached_input_sections();
791 // With -z relro, we have to recognize the special sections by name.
792 // There is no other way.
793 if (!this->script_options_
->saw_sections_clause()
794 && parameters
->options().relro()
795 && type
== elfcpp::SHT_PROGBITS
796 && (flags
& elfcpp::SHF_ALLOC
) != 0
797 && (flags
& elfcpp::SHF_WRITE
) != 0)
799 if (strcmp(name
, ".data.rel.ro") == 0)
801 else if (strcmp(name
, ".data.rel.ro.local") == 0)
804 os
->set_is_relro_local();
808 // If we have already attached the sections to segments, then we
809 // need to attach this one now. This happens for sections created
810 // directly by the linker.
811 if (this->sections_are_attached_
)
812 this->attach_section_to_segment(os
);
817 // Attach output sections to segments. This is called after we have
818 // seen all the input sections.
821 Layout::attach_sections_to_segments()
823 for (Section_list::iterator p
= this->section_list_
.begin();
824 p
!= this->section_list_
.end();
826 this->attach_section_to_segment(*p
);
828 this->sections_are_attached_
= true;
831 // Attach an output section to a segment.
834 Layout::attach_section_to_segment(Output_section
* os
)
836 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
837 this->unattached_section_list_
.push_back(os
);
839 this->attach_allocated_section_to_segment(os
);
842 // Attach an allocated output section to a segment.
845 Layout::attach_allocated_section_to_segment(Output_section
* os
)
847 elfcpp::Elf_Xword flags
= os
->flags();
848 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
850 if (parameters
->options().relocatable())
853 // If we have a SECTIONS clause, we can't handle the attachment to
854 // segments until after we've seen all the sections.
855 if (this->script_options_
->saw_sections_clause())
858 gold_assert(!this->script_options_
->saw_phdrs_clause());
860 // This output section goes into a PT_LOAD segment.
862 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
864 // In general the only thing we really care about for PT_LOAD
865 // segments is whether or not they are writable, so that is how we
866 // search for them. People who need segments sorted on some other
867 // basis will have to use a linker script.
869 Segment_list::const_iterator p
;
870 for (p
= this->segment_list_
.begin();
871 p
!= this->segment_list_
.end();
874 if ((*p
)->type() == elfcpp::PT_LOAD
875 && (parameters
->options().omagic()
876 || ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
)))
878 // If -Tbss was specified, we need to separate the data
880 if (this->options_
.user_set_Tbss())
882 if ((os
->type() == elfcpp::SHT_NOBITS
)
883 == (*p
)->has_any_data_sections())
887 (*p
)->add_output_section(os
, seg_flags
);
892 if (p
== this->segment_list_
.end())
894 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
896 oseg
->add_output_section(os
, seg_flags
);
899 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
901 if (os
->type() == elfcpp::SHT_NOTE
)
903 // See if we already have an equivalent PT_NOTE segment.
904 for (p
= this->segment_list_
.begin();
905 p
!= segment_list_
.end();
908 if ((*p
)->type() == elfcpp::PT_NOTE
909 && (((*p
)->flags() & elfcpp::PF_W
)
910 == (seg_flags
& elfcpp::PF_W
)))
912 (*p
)->add_output_section(os
, seg_flags
);
917 if (p
== this->segment_list_
.end())
919 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
921 oseg
->add_output_section(os
, seg_flags
);
925 // If we see a loadable SHF_TLS section, we create a PT_TLS
926 // segment. There can only be one such segment.
927 if ((flags
& elfcpp::SHF_TLS
) != 0)
929 if (this->tls_segment_
== NULL
)
930 this->make_output_segment(elfcpp::PT_TLS
, seg_flags
);
931 this->tls_segment_
->add_output_section(os
, seg_flags
);
934 // If -z relro is in effect, and we see a relro section, we create a
935 // PT_GNU_RELRO segment. There can only be one such segment.
936 if (os
->is_relro() && parameters
->options().relro())
938 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
939 if (this->relro_segment_
== NULL
)
940 this->make_output_segment(elfcpp::PT_GNU_RELRO
, seg_flags
);
941 this->relro_segment_
->add_output_section(os
, seg_flags
);
945 // Make an output section for a script.
948 Layout::make_output_section_for_script(const char* name
)
950 name
= this->namepool_
.add(name
, false, NULL
);
951 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
953 os
->set_found_in_sections_clause();
957 // Return the number of segments we expect to see.
960 Layout::expected_segment_count() const
962 size_t ret
= this->segment_list_
.size();
964 // If we didn't see a SECTIONS clause in a linker script, we should
965 // already have the complete list of segments. Otherwise we ask the
966 // SECTIONS clause how many segments it expects, and add in the ones
967 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
969 if (!this->script_options_
->saw_sections_clause())
973 const Script_sections
* ss
= this->script_options_
->script_sections();
974 return ret
+ ss
->expected_segment_count(this);
978 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
979 // is whether we saw a .note.GNU-stack section in the object file.
980 // GNU_STACK_FLAGS is the section flags. The flags give the
981 // protection required for stack memory. We record this in an
982 // executable as a PT_GNU_STACK segment. If an object file does not
983 // have a .note.GNU-stack segment, we must assume that it is an old
984 // object. On some targets that will force an executable stack.
987 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
990 this->input_without_gnu_stack_note_
= true;
993 this->input_with_gnu_stack_note_
= true;
994 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
995 this->input_requires_executable_stack_
= true;
999 // Create the dynamic sections which are needed before we read the
1003 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
1005 if (parameters
->doing_static_link())
1008 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1009 elfcpp::SHT_DYNAMIC
,
1011 | elfcpp::SHF_WRITE
),
1013 this->dynamic_section_
->set_is_relro();
1015 symtab
->define_in_output_data("_DYNAMIC", NULL
, this->dynamic_section_
, 0, 0,
1016 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1017 elfcpp::STV_HIDDEN
, 0, false, false);
1019 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1021 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1024 // For each output section whose name can be represented as C symbol,
1025 // define __start and __stop symbols for the section. This is a GNU
1029 Layout::define_section_symbols(Symbol_table
* symtab
)
1031 for (Section_list::const_iterator p
= this->section_list_
.begin();
1032 p
!= this->section_list_
.end();
1035 const char* const name
= (*p
)->name();
1036 if (name
[strspn(name
,
1038 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
1039 "abcdefghijklmnopqrstuvwxyz"
1043 const std::string
name_string(name
);
1044 const std::string
start_name("__start_" + name_string
);
1045 const std::string
stop_name("__stop_" + name_string
);
1047 symtab
->define_in_output_data(start_name
.c_str(),
1054 elfcpp::STV_DEFAULT
,
1056 false, // offset_is_from_end
1057 true); // only_if_ref
1059 symtab
->define_in_output_data(stop_name
.c_str(),
1066 elfcpp::STV_DEFAULT
,
1068 true, // offset_is_from_end
1069 true); // only_if_ref
1074 // Define symbols for group signatures.
1077 Layout::define_group_signatures(Symbol_table
* symtab
)
1079 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1080 p
!= this->group_signatures_
.end();
1083 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1085 p
->section
->set_info_symndx(sym
);
1088 // Force the name of the group section to the group
1089 // signature, and use the group's section symbol as the
1090 // signature symbol.
1091 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1093 const char* name
= this->namepool_
.add(p
->signature
,
1095 p
->section
->set_name(name
);
1097 p
->section
->set_needs_symtab_index();
1098 p
->section
->set_info_section_symndx(p
->section
);
1102 this->group_signatures_
.clear();
1105 // Find the first read-only PT_LOAD segment, creating one if
1109 Layout::find_first_load_seg()
1111 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1112 p
!= this->segment_list_
.end();
1115 if ((*p
)->type() == elfcpp::PT_LOAD
1116 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1117 && (parameters
->options().omagic()
1118 || ((*p
)->flags() & elfcpp::PF_W
) == 0))
1122 gold_assert(!this->script_options_
->saw_phdrs_clause());
1124 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1129 // Finalize the layout. When this is called, we have created all the
1130 // output sections and all the output segments which are based on
1131 // input sections. We have several things to do, and we have to do
1132 // them in the right order, so that we get the right results correctly
1135 // 1) Finalize the list of output segments and create the segment
1138 // 2) Finalize the dynamic symbol table and associated sections.
1140 // 3) Determine the final file offset of all the output segments.
1142 // 4) Determine the final file offset of all the SHF_ALLOC output
1145 // 5) Create the symbol table sections and the section name table
1148 // 6) Finalize the symbol table: set symbol values to their final
1149 // value and make a final determination of which symbols are going
1150 // into the output symbol table.
1152 // 7) Create the section table header.
1154 // 8) Determine the final file offset of all the output sections which
1155 // are not SHF_ALLOC, including the section table header.
1157 // 9) Finalize the ELF file header.
1159 // This function returns the size of the output file.
1162 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1163 Target
* target
, const Task
* task
)
1165 target
->finalize_sections(this);
1167 this->count_local_symbols(task
, input_objects
);
1169 this->create_gold_note();
1170 this->create_executable_stack_info(target
);
1171 this->create_build_id();
1173 Output_segment
* phdr_seg
= NULL
;
1174 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1176 // There was a dynamic object in the link. We need to create
1177 // some information for the dynamic linker.
1179 // Create the PT_PHDR segment which will hold the program
1181 if (!this->script_options_
->saw_phdrs_clause())
1182 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1184 // Create the dynamic symbol table, including the hash table.
1185 Output_section
* dynstr
;
1186 std::vector
<Symbol
*> dynamic_symbols
;
1187 unsigned int local_dynamic_count
;
1188 Versions
versions(*this->script_options()->version_script_info(),
1190 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1191 &local_dynamic_count
, &dynamic_symbols
,
1194 // Create the .interp section to hold the name of the
1195 // interpreter, and put it in a PT_INTERP segment.
1196 if (!parameters
->options().shared())
1197 this->create_interp(target
);
1199 // Finish the .dynamic section to hold the dynamic data, and put
1200 // it in a PT_DYNAMIC segment.
1201 this->finish_dynamic_section(input_objects
, symtab
);
1203 // We should have added everything we need to the dynamic string
1205 this->dynpool_
.set_string_offsets();
1207 // Create the version sections. We can't do this until the
1208 // dynamic string table is complete.
1209 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1210 dynamic_symbols
, dynstr
);
1213 // If there is a SECTIONS clause, put all the input sections into
1214 // the required order.
1215 Output_segment
* load_seg
;
1216 if (this->script_options_
->saw_sections_clause())
1217 load_seg
= this->set_section_addresses_from_script(symtab
);
1218 else if (parameters
->options().relocatable())
1221 load_seg
= this->find_first_load_seg();
1223 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
1226 gold_assert(phdr_seg
== NULL
|| load_seg
!= NULL
);
1228 // Lay out the segment headers.
1229 Output_segment_headers
* segment_headers
;
1230 if (parameters
->options().relocatable())
1231 segment_headers
= NULL
;
1234 segment_headers
= new Output_segment_headers(this->segment_list_
);
1235 if (load_seg
!= NULL
)
1236 load_seg
->add_initial_output_data(segment_headers
);
1237 if (phdr_seg
!= NULL
)
1238 phdr_seg
->add_initial_output_data(segment_headers
);
1241 // Lay out the file header.
1242 Output_file_header
* file_header
;
1243 file_header
= new Output_file_header(target
, symtab
, segment_headers
,
1244 this->options_
.entry());
1245 if (load_seg
!= NULL
)
1246 load_seg
->add_initial_output_data(file_header
);
1248 this->special_output_list_
.push_back(file_header
);
1249 if (segment_headers
!= NULL
)
1250 this->special_output_list_
.push_back(segment_headers
);
1252 if (this->script_options_
->saw_phdrs_clause()
1253 && !parameters
->options().relocatable())
1255 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1256 // clause in a linker script.
1257 Script_sections
* ss
= this->script_options_
->script_sections();
1258 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1261 // We set the output section indexes in set_segment_offsets and
1262 // set_section_indexes.
1263 unsigned int shndx
= 1;
1265 // Set the file offsets of all the segments, and all the sections
1268 if (!parameters
->options().relocatable())
1269 off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
1271 off
= this->set_relocatable_section_offsets(file_header
, &shndx
);
1273 // Set the file offsets of all the non-data sections we've seen so
1274 // far which don't have to wait for the input sections. We need
1275 // this in order to finalize local symbols in non-allocated
1277 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1279 // Set the section indexes of all unallocated sections seen so far,
1280 // in case any of them are somehow referenced by a symbol.
1281 shndx
= this->set_section_indexes(shndx
);
1283 // Create the symbol table sections.
1284 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1285 if (!parameters
->doing_static_link())
1286 this->assign_local_dynsym_offsets(input_objects
);
1288 // Process any symbol assignments from a linker script. This must
1289 // be called after the symbol table has been finalized.
1290 this->script_options_
->finalize_symbols(symtab
, this);
1292 // Create the .shstrtab section.
1293 Output_section
* shstrtab_section
= this->create_shstrtab();
1295 // Set the file offsets of the rest of the non-data sections which
1296 // don't have to wait for the input sections.
1297 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1299 // Now that all sections have been created, set the section indexes
1300 // for any sections which haven't been done yet.
1301 shndx
= this->set_section_indexes(shndx
);
1303 // Create the section table header.
1304 this->create_shdrs(shstrtab_section
, &off
);
1306 // If there are no sections which require postprocessing, we can
1307 // handle the section names now, and avoid a resize later.
1308 if (!this->any_postprocessing_sections_
)
1309 off
= this->set_section_offsets(off
,
1310 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1312 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1314 // Now we know exactly where everything goes in the output file
1315 // (except for non-allocated sections which require postprocessing).
1316 Output_data::layout_complete();
1318 this->output_file_size_
= off
;
1323 // Create a note header following the format defined in the ELF ABI.
1324 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1325 // descriptor. ALLOCATE is true if the section should be allocated in
1326 // memory. This returns the new note section. It sets
1327 // *TRAILING_PADDING to the number of trailing zero bytes required.
1330 Layout::create_note(const char* name
, int note_type
, size_t descsz
,
1331 bool allocate
, size_t* trailing_padding
)
1333 // Authorities all agree that the values in a .note field should
1334 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1335 // they differ on what the alignment is for 64-bit binaries.
1336 // The GABI says unambiguously they take 8-byte alignment:
1337 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1338 // Other documentation says alignment should always be 4 bytes:
1339 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1340 // GNU ld and GNU readelf both support the latter (at least as of
1341 // version 2.16.91), and glibc always generates the latter for
1342 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1344 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1345 const int size
= parameters
->target().get_size();
1347 const int size
= 32;
1350 // The contents of the .note section.
1351 size_t namesz
= strlen(name
) + 1;
1352 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1353 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1355 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1357 unsigned char* buffer
= new unsigned char[notehdrsz
];
1358 memset(buffer
, 0, notehdrsz
);
1360 bool is_big_endian
= parameters
->target().is_big_endian();
1366 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1367 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1368 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1372 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1373 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1374 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1377 else if (size
== 64)
1381 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1382 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1383 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1387 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1388 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1389 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1395 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1397 const char* note_name
= this->namepool_
.add(".note", false, NULL
);
1398 elfcpp::Elf_Xword flags
= 0;
1400 flags
= elfcpp::SHF_ALLOC
;
1401 Output_section
* os
= this->make_output_section(note_name
,
1404 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1407 os
->add_output_section_data(posd
);
1409 *trailing_padding
= aligned_descsz
- descsz
;
1414 // For an executable or shared library, create a note to record the
1415 // version of gold used to create the binary.
1418 Layout::create_gold_note()
1420 if (parameters
->options().relocatable())
1423 std::string desc
= std::string("gold ") + gold::get_version_string();
1425 size_t trailing_padding
;
1426 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1427 desc
.size(), false, &trailing_padding
);
1429 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1430 os
->add_output_section_data(posd
);
1432 if (trailing_padding
> 0)
1434 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1435 os
->add_output_section_data(posd
);
1439 // Record whether the stack should be executable. This can be set
1440 // from the command line using the -z execstack or -z noexecstack
1441 // options. Otherwise, if any input file has a .note.GNU-stack
1442 // section with the SHF_EXECINSTR flag set, the stack should be
1443 // executable. Otherwise, if at least one input file a
1444 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1445 // section, we use the target default for whether the stack should be
1446 // executable. Otherwise, we don't generate a stack note. When
1447 // generating a object file, we create a .note.GNU-stack section with
1448 // the appropriate marking. When generating an executable or shared
1449 // library, we create a PT_GNU_STACK segment.
1452 Layout::create_executable_stack_info(const Target
* target
)
1454 bool is_stack_executable
;
1455 if (this->options_
.is_execstack_set())
1456 is_stack_executable
= this->options_
.is_stack_executable();
1457 else if (!this->input_with_gnu_stack_note_
)
1461 if (this->input_requires_executable_stack_
)
1462 is_stack_executable
= true;
1463 else if (this->input_without_gnu_stack_note_
)
1464 is_stack_executable
= target
->is_default_stack_executable();
1466 is_stack_executable
= false;
1469 if (parameters
->options().relocatable())
1471 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1472 elfcpp::Elf_Xword flags
= 0;
1473 if (is_stack_executable
)
1474 flags
|= elfcpp::SHF_EXECINSTR
;
1475 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
);
1479 if (this->script_options_
->saw_phdrs_clause())
1481 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1482 if (is_stack_executable
)
1483 flags
|= elfcpp::PF_X
;
1484 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1488 // If --build-id was used, set up the build ID note.
1491 Layout::create_build_id()
1493 if (!parameters
->options().user_set_build_id())
1496 const char* style
= parameters
->options().build_id();
1497 if (strcmp(style
, "none") == 0)
1500 // Set DESCSZ to the size of the note descriptor. When possible,
1501 // set DESC to the note descriptor contents.
1504 if (strcmp(style
, "md5") == 0)
1506 else if (strcmp(style
, "sha1") == 0)
1508 else if (strcmp(style
, "uuid") == 0)
1510 const size_t uuidsz
= 128 / 8;
1512 char buffer
[uuidsz
];
1513 memset(buffer
, 0, uuidsz
);
1515 int descriptor
= open_descriptor(-1, "/dev/urandom", O_RDONLY
);
1517 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1521 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1522 release_descriptor(descriptor
, true);
1524 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1525 else if (static_cast<size_t>(got
) != uuidsz
)
1526 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1530 desc
.assign(buffer
, uuidsz
);
1533 else if (strncmp(style
, "0x", 2) == 0)
1536 const char* p
= style
+ 2;
1539 if (hex_p(p
[0]) && hex_p(p
[1]))
1541 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1545 else if (*p
== '-' || *p
== ':')
1548 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1551 descsz
= desc
.size();
1554 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1557 size_t trailing_padding
;
1558 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1559 descsz
, true, &trailing_padding
);
1563 // We know the value already, so we fill it in now.
1564 gold_assert(desc
.size() == descsz
);
1566 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1567 os
->add_output_section_data(posd
);
1569 if (trailing_padding
!= 0)
1571 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1572 os
->add_output_section_data(posd
);
1577 // We need to compute a checksum after we have completed the
1579 gold_assert(trailing_padding
== 0);
1580 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
1581 os
->add_output_section_data(this->build_id_note_
);
1582 os
->set_after_input_sections();
1586 // Return whether SEG1 should be before SEG2 in the output file. This
1587 // is based entirely on the segment type and flags. When this is
1588 // called the segment addresses has normally not yet been set.
1591 Layout::segment_precedes(const Output_segment
* seg1
,
1592 const Output_segment
* seg2
)
1594 elfcpp::Elf_Word type1
= seg1
->type();
1595 elfcpp::Elf_Word type2
= seg2
->type();
1597 // The single PT_PHDR segment is required to precede any loadable
1598 // segment. We simply make it always first.
1599 if (type1
== elfcpp::PT_PHDR
)
1601 gold_assert(type2
!= elfcpp::PT_PHDR
);
1604 if (type2
== elfcpp::PT_PHDR
)
1607 // The single PT_INTERP segment is required to precede any loadable
1608 // segment. We simply make it always second.
1609 if (type1
== elfcpp::PT_INTERP
)
1611 gold_assert(type2
!= elfcpp::PT_INTERP
);
1614 if (type2
== elfcpp::PT_INTERP
)
1617 // We then put PT_LOAD segments before any other segments.
1618 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
1620 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
1623 // We put the PT_TLS segment last except for the PT_GNU_RELRO
1624 // segment, because that is where the dynamic linker expects to find
1625 // it (this is just for efficiency; other positions would also work
1627 if (type1
== elfcpp::PT_TLS
1628 && type2
!= elfcpp::PT_TLS
1629 && type2
!= elfcpp::PT_GNU_RELRO
)
1631 if (type2
== elfcpp::PT_TLS
1632 && type1
!= elfcpp::PT_TLS
1633 && type1
!= elfcpp::PT_GNU_RELRO
)
1636 // We put the PT_GNU_RELRO segment last, because that is where the
1637 // dynamic linker expects to find it (as with PT_TLS, this is just
1639 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
1641 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
1644 const elfcpp::Elf_Word flags1
= seg1
->flags();
1645 const elfcpp::Elf_Word flags2
= seg2
->flags();
1647 // The order of non-PT_LOAD segments is unimportant. We simply sort
1648 // by the numeric segment type and flags values. There should not
1649 // be more than one segment with the same type and flags.
1650 if (type1
!= elfcpp::PT_LOAD
)
1653 return type1
< type2
;
1654 gold_assert(flags1
!= flags2
);
1655 return flags1
< flags2
;
1658 // If the addresses are set already, sort by load address.
1659 if (seg1
->are_addresses_set())
1661 if (!seg2
->are_addresses_set())
1664 unsigned int section_count1
= seg1
->output_section_count();
1665 unsigned int section_count2
= seg2
->output_section_count();
1666 if (section_count1
== 0 && section_count2
> 0)
1668 if (section_count1
> 0 && section_count2
== 0)
1671 uint64_t paddr1
= seg1
->first_section_load_address();
1672 uint64_t paddr2
= seg2
->first_section_load_address();
1673 if (paddr1
!= paddr2
)
1674 return paddr1
< paddr2
;
1676 else if (seg2
->are_addresses_set())
1679 // We sort PT_LOAD segments based on the flags. Readonly segments
1680 // come before writable segments. Then writable segments with data
1681 // come before writable segments without data. Then executable
1682 // segments come before non-executable segments. Then the unlikely
1683 // case of a non-readable segment comes before the normal case of a
1684 // readable segment. If there are multiple segments with the same
1685 // type and flags, we require that the address be set, and we sort
1686 // by virtual address and then physical address.
1687 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
1688 return (flags1
& elfcpp::PF_W
) == 0;
1689 if ((flags1
& elfcpp::PF_W
) != 0
1690 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
1691 return seg1
->has_any_data_sections();
1692 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
1693 return (flags1
& elfcpp::PF_X
) != 0;
1694 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
1695 return (flags1
& elfcpp::PF_R
) == 0;
1697 // We shouldn't get here--we shouldn't create segments which we
1698 // can't distinguish.
1702 // Set the file offsets of all the segments, and all the sections they
1703 // contain. They have all been created. LOAD_SEG must be be laid out
1704 // first. Return the offset of the data to follow.
1707 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
1708 unsigned int *pshndx
)
1710 // Sort them into the final order.
1711 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
1712 Layout::Compare_segments());
1714 // Find the PT_LOAD segments, and set their addresses and offsets
1715 // and their section's addresses and offsets.
1717 if (this->options_
.user_set_Ttext())
1718 addr
= this->options_
.Ttext();
1719 else if (parameters
->options().shared())
1722 addr
= target
->default_text_segment_address();
1725 // If LOAD_SEG is NULL, then the file header and segment headers
1726 // will not be loadable. But they still need to be at offset 0 in
1727 // the file. Set their offsets now.
1728 if (load_seg
== NULL
)
1730 for (Data_list::iterator p
= this->special_output_list_
.begin();
1731 p
!= this->special_output_list_
.end();
1734 off
= align_address(off
, (*p
)->addralign());
1735 (*p
)->set_address_and_file_offset(0, off
);
1736 off
+= (*p
)->data_size();
1740 const bool check_sections
= parameters
->options().check_sections();
1741 Output_segment
* last_load_segment
= NULL
;
1743 bool was_readonly
= false;
1744 for (Segment_list::iterator p
= this->segment_list_
.begin();
1745 p
!= this->segment_list_
.end();
1748 if ((*p
)->type() == elfcpp::PT_LOAD
)
1750 if (load_seg
!= NULL
&& load_seg
!= *p
)
1754 bool are_addresses_set
= (*p
)->are_addresses_set();
1755 if (are_addresses_set
)
1757 // When it comes to setting file offsets, we care about
1758 // the physical address.
1759 addr
= (*p
)->paddr();
1761 else if (this->options_
.user_set_Tdata()
1762 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1763 && (!this->options_
.user_set_Tbss()
1764 || (*p
)->has_any_data_sections()))
1766 addr
= this->options_
.Tdata();
1767 are_addresses_set
= true;
1769 else if (this->options_
.user_set_Tbss()
1770 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1771 && !(*p
)->has_any_data_sections())
1773 addr
= this->options_
.Tbss();
1774 are_addresses_set
= true;
1777 uint64_t orig_addr
= addr
;
1778 uint64_t orig_off
= off
;
1780 uint64_t aligned_addr
= 0;
1781 uint64_t abi_pagesize
= target
->abi_pagesize();
1782 uint64_t common_pagesize
= target
->common_pagesize();
1784 if (!parameters
->options().nmagic()
1785 && !parameters
->options().omagic())
1786 (*p
)->set_minimum_p_align(common_pagesize
);
1788 if (are_addresses_set
)
1790 if (!parameters
->options().nmagic()
1791 && !parameters
->options().omagic())
1793 // Adjust the file offset to the same address modulo
1795 uint64_t unsigned_off
= off
;
1796 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
1797 | (addr
& (abi_pagesize
- 1)));
1798 if (aligned_off
< unsigned_off
)
1799 aligned_off
+= abi_pagesize
;
1805 // If the last segment was readonly, and this one is
1806 // not, then skip the address forward one page,
1807 // maintaining the same position within the page. This
1808 // lets us store both segments overlapping on a single
1809 // page in the file, but the loader will put them on
1810 // different pages in memory.
1812 addr
= align_address(addr
, (*p
)->maximum_alignment());
1813 aligned_addr
= addr
;
1815 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
1817 if ((addr
& (abi_pagesize
- 1)) != 0)
1818 addr
= addr
+ abi_pagesize
;
1821 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1824 unsigned int shndx_hold
= *pshndx
;
1825 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
1828 // Now that we know the size of this segment, we may be able
1829 // to save a page in memory, at the cost of wasting some
1830 // file space, by instead aligning to the start of a new
1831 // page. Here we use the real machine page size rather than
1832 // the ABI mandated page size.
1834 if (!are_addresses_set
&& aligned_addr
!= addr
)
1836 uint64_t first_off
= (common_pagesize
1838 & (common_pagesize
- 1)));
1839 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
1842 && ((aligned_addr
& ~ (common_pagesize
- 1))
1843 != (new_addr
& ~ (common_pagesize
- 1)))
1844 && first_off
+ last_off
<= common_pagesize
)
1846 *pshndx
= shndx_hold
;
1847 addr
= align_address(aligned_addr
, common_pagesize
);
1848 addr
= align_address(addr
, (*p
)->maximum_alignment());
1849 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1850 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
1857 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
1858 was_readonly
= true;
1860 // Implement --check-sections. We know that the segments
1861 // are sorted by LMA.
1862 if (check_sections
&& last_load_segment
!= NULL
)
1864 gold_assert(last_load_segment
->paddr() <= (*p
)->paddr());
1865 if (last_load_segment
->paddr() + last_load_segment
->memsz()
1868 unsigned long long lb1
= last_load_segment
->paddr();
1869 unsigned long long le1
= lb1
+ last_load_segment
->memsz();
1870 unsigned long long lb2
= (*p
)->paddr();
1871 unsigned long long le2
= lb2
+ (*p
)->memsz();
1872 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
1873 "[0x%llx -> 0x%llx]"),
1874 lb1
, le1
, lb2
, le2
);
1877 last_load_segment
= *p
;
1881 // Handle the non-PT_LOAD segments, setting their offsets from their
1882 // section's offsets.
1883 for (Segment_list::iterator p
= this->segment_list_
.begin();
1884 p
!= this->segment_list_
.end();
1887 if ((*p
)->type() != elfcpp::PT_LOAD
)
1891 // Set the TLS offsets for each section in the PT_TLS segment.
1892 if (this->tls_segment_
!= NULL
)
1893 this->tls_segment_
->set_tls_offsets();
1898 // Set the offsets of all the allocated sections when doing a
1899 // relocatable link. This does the same jobs as set_segment_offsets,
1900 // only for a relocatable link.
1903 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
1904 unsigned int *pshndx
)
1908 file_header
->set_address_and_file_offset(0, 0);
1909 off
+= file_header
->data_size();
1911 for (Section_list::iterator p
= this->section_list_
.begin();
1912 p
!= this->section_list_
.end();
1915 // We skip unallocated sections here, except that group sections
1916 // have to come first.
1917 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
1918 && (*p
)->type() != elfcpp::SHT_GROUP
)
1921 off
= align_address(off
, (*p
)->addralign());
1923 // The linker script might have set the address.
1924 if (!(*p
)->is_address_valid())
1925 (*p
)->set_address(0);
1926 (*p
)->set_file_offset(off
);
1927 (*p
)->finalize_data_size();
1928 off
+= (*p
)->data_size();
1930 (*p
)->set_out_shndx(*pshndx
);
1937 // Set the file offset of all the sections not associated with a
1941 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
1943 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1944 p
!= this->unattached_section_list_
.end();
1947 // The symtab section is handled in create_symtab_sections.
1948 if (*p
== this->symtab_section_
)
1951 // If we've already set the data size, don't set it again.
1952 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
1955 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1956 && (*p
)->requires_postprocessing())
1958 (*p
)->create_postprocessing_buffer();
1959 this->any_postprocessing_sections_
= true;
1962 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1963 && (*p
)->after_input_sections())
1965 else if (pass
== POSTPROCESSING_SECTIONS_PASS
1966 && (!(*p
)->after_input_sections()
1967 || (*p
)->type() == elfcpp::SHT_STRTAB
))
1969 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1970 && (!(*p
)->after_input_sections()
1971 || (*p
)->type() != elfcpp::SHT_STRTAB
))
1974 off
= align_address(off
, (*p
)->addralign());
1975 (*p
)->set_file_offset(off
);
1976 (*p
)->finalize_data_size();
1977 off
+= (*p
)->data_size();
1979 // At this point the name must be set.
1980 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
1981 this->namepool_
.add((*p
)->name(), false, NULL
);
1986 // Set the section indexes of all the sections not associated with a
1990 Layout::set_section_indexes(unsigned int shndx
)
1992 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1993 p
!= this->unattached_section_list_
.end();
1996 if (!(*p
)->has_out_shndx())
1998 (*p
)->set_out_shndx(shndx
);
2005 // Set the section addresses according to the linker script. This is
2006 // only called when we see a SECTIONS clause. This returns the
2007 // program segment which should hold the file header and segment
2008 // headers, if any. It will return NULL if they should not be in a
2012 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
2014 Script_sections
* ss
= this->script_options_
->script_sections();
2015 gold_assert(ss
->saw_sections_clause());
2017 // Place each orphaned output section in the script.
2018 for (Section_list::iterator p
= this->section_list_
.begin();
2019 p
!= this->section_list_
.end();
2022 if (!(*p
)->found_in_sections_clause())
2023 ss
->place_orphan(*p
);
2026 return this->script_options_
->set_section_addresses(symtab
, this);
2029 // Count the local symbols in the regular symbol table and the dynamic
2030 // symbol table, and build the respective string pools.
2033 Layout::count_local_symbols(const Task
* task
,
2034 const Input_objects
* input_objects
)
2036 // First, figure out an upper bound on the number of symbols we'll
2037 // be inserting into each pool. This helps us create the pools with
2038 // the right size, to avoid unnecessary hashtable resizing.
2039 unsigned int symbol_count
= 0;
2040 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2041 p
!= input_objects
->relobj_end();
2043 symbol_count
+= (*p
)->local_symbol_count();
2045 // Go from "upper bound" to "estimate." We overcount for two
2046 // reasons: we double-count symbols that occur in more than one
2047 // object file, and we count symbols that are dropped from the
2048 // output. Add it all together and assume we overcount by 100%.
2051 // We assume all symbols will go into both the sympool and dynpool.
2052 this->sympool_
.reserve(symbol_count
);
2053 this->dynpool_
.reserve(symbol_count
);
2055 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2056 p
!= input_objects
->relobj_end();
2059 Task_lock_obj
<Object
> tlo(task
, *p
);
2060 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2064 // Create the symbol table sections. Here we also set the final
2065 // values of the symbols. At this point all the loadable sections are
2066 // fully laid out. SHNUM is the number of sections so far.
2069 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2070 Symbol_table
* symtab
,
2076 if (parameters
->target().get_size() == 32)
2078 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2081 else if (parameters
->target().get_size() == 64)
2083 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2090 off
= align_address(off
, align
);
2091 off_t startoff
= off
;
2093 // Save space for the dummy symbol at the start of the section. We
2094 // never bother to write this out--it will just be left as zero.
2096 unsigned int local_symbol_index
= 1;
2098 // Add STT_SECTION symbols for each Output section which needs one.
2099 for (Section_list::iterator p
= this->section_list_
.begin();
2100 p
!= this->section_list_
.end();
2103 if (!(*p
)->needs_symtab_index())
2104 (*p
)->set_symtab_index(-1U);
2107 (*p
)->set_symtab_index(local_symbol_index
);
2108 ++local_symbol_index
;
2113 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2114 p
!= input_objects
->relobj_end();
2117 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2119 off
+= (index
- local_symbol_index
) * symsize
;
2120 local_symbol_index
= index
;
2123 unsigned int local_symcount
= local_symbol_index
;
2124 gold_assert(local_symcount
* symsize
== off
- startoff
);
2127 size_t dyn_global_index
;
2129 if (this->dynsym_section_
== NULL
)
2132 dyn_global_index
= 0;
2137 dyn_global_index
= this->dynsym_section_
->info();
2138 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2139 dynoff
= this->dynsym_section_
->offset() + locsize
;
2140 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2141 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2142 == this->dynsym_section_
->data_size() - locsize
);
2145 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2146 &this->sympool_
, &local_symcount
);
2148 if (!parameters
->options().strip_all())
2150 this->sympool_
.set_string_offsets();
2152 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2153 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2156 this->symtab_section_
= osymtab
;
2158 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2161 osymtab
->add_output_section_data(pos
);
2163 // We generate a .symtab_shndx section if we have more than
2164 // SHN_LORESERVE sections. Technically it is possible that we
2165 // don't need one, because it is possible that there are no
2166 // symbols in any of sections with indexes larger than
2167 // SHN_LORESERVE. That is probably unusual, though, and it is
2168 // easier to always create one than to compute section indexes
2169 // twice (once here, once when writing out the symbols).
2170 if (shnum
>= elfcpp::SHN_LORESERVE
)
2172 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2174 Output_section
* osymtab_xindex
=
2175 this->make_output_section(symtab_xindex_name
,
2176 elfcpp::SHT_SYMTAB_SHNDX
, 0);
2178 size_t symcount
= (off
- startoff
) / symsize
;
2179 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2181 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2183 osymtab_xindex
->set_link_section(osymtab
);
2184 osymtab_xindex
->set_addralign(4);
2185 osymtab_xindex
->set_entsize(4);
2187 osymtab_xindex
->set_after_input_sections();
2189 // This tells the driver code to wait until the symbol table
2190 // has written out before writing out the postprocessing
2191 // sections, including the .symtab_shndx section.
2192 this->any_postprocessing_sections_
= true;
2195 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2196 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2200 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2201 ostrtab
->add_output_section_data(pstr
);
2203 osymtab
->set_file_offset(startoff
);
2204 osymtab
->finalize_data_size();
2205 osymtab
->set_link_section(ostrtab
);
2206 osymtab
->set_info(local_symcount
);
2207 osymtab
->set_entsize(symsize
);
2213 // Create the .shstrtab section, which holds the names of the
2214 // sections. At the time this is called, we have created all the
2215 // output sections except .shstrtab itself.
2218 Layout::create_shstrtab()
2220 // FIXME: We don't need to create a .shstrtab section if we are
2221 // stripping everything.
2223 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2225 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
2227 // We can't write out this section until we've set all the section
2228 // names, and we don't set the names of compressed output sections
2229 // until relocations are complete.
2230 os
->set_after_input_sections();
2232 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2233 os
->add_output_section_data(posd
);
2238 // Create the section headers. SIZE is 32 or 64. OFF is the file
2242 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2244 Output_section_headers
* oshdrs
;
2245 oshdrs
= new Output_section_headers(this,
2246 &this->segment_list_
,
2247 &this->section_list_
,
2248 &this->unattached_section_list_
,
2251 off_t off
= align_address(*poff
, oshdrs
->addralign());
2252 oshdrs
->set_address_and_file_offset(0, off
);
2253 off
+= oshdrs
->data_size();
2255 this->section_headers_
= oshdrs
;
2258 // Count the allocated sections.
2261 Layout::allocated_output_section_count() const
2263 size_t section_count
= 0;
2264 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2265 p
!= this->segment_list_
.end();
2267 section_count
+= (*p
)->output_section_count();
2268 return section_count
;
2271 // Create the dynamic symbol table.
2274 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2275 Symbol_table
* symtab
,
2276 Output_section
**pdynstr
,
2277 unsigned int* plocal_dynamic_count
,
2278 std::vector
<Symbol
*>* pdynamic_symbols
,
2279 Versions
* pversions
)
2281 // Count all the symbols in the dynamic symbol table, and set the
2282 // dynamic symbol indexes.
2284 // Skip symbol 0, which is always all zeroes.
2285 unsigned int index
= 1;
2287 // Add STT_SECTION symbols for each Output section which needs one.
2288 for (Section_list::iterator p
= this->section_list_
.begin();
2289 p
!= this->section_list_
.end();
2292 if (!(*p
)->needs_dynsym_index())
2293 (*p
)->set_dynsym_index(-1U);
2296 (*p
)->set_dynsym_index(index
);
2301 // Count the local symbols that need to go in the dynamic symbol table,
2302 // and set the dynamic symbol indexes.
2303 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2304 p
!= input_objects
->relobj_end();
2307 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2311 unsigned int local_symcount
= index
;
2312 *plocal_dynamic_count
= local_symcount
;
2314 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2315 &this->dynpool_
, pversions
);
2319 const int size
= parameters
->target().get_size();
2322 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2325 else if (size
== 64)
2327 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2333 // Create the dynamic symbol table section.
2335 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2340 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2343 dynsym
->add_output_section_data(odata
);
2345 dynsym
->set_info(local_symcount
);
2346 dynsym
->set_entsize(symsize
);
2347 dynsym
->set_addralign(align
);
2349 this->dynsym_section_
= dynsym
;
2351 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2352 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2353 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2355 // If there are more than SHN_LORESERVE allocated sections, we
2356 // create a .dynsym_shndx section. It is possible that we don't
2357 // need one, because it is possible that there are no dynamic
2358 // symbols in any of the sections with indexes larger than
2359 // SHN_LORESERVE. This is probably unusual, though, and at this
2360 // time we don't know the actual section indexes so it is
2361 // inconvenient to check.
2362 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2364 Output_section
* dynsym_xindex
=
2365 this->choose_output_section(NULL
, ".dynsym_shndx",
2366 elfcpp::SHT_SYMTAB_SHNDX
,
2370 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2372 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2374 dynsym_xindex
->set_link_section(dynsym
);
2375 dynsym_xindex
->set_addralign(4);
2376 dynsym_xindex
->set_entsize(4);
2378 dynsym_xindex
->set_after_input_sections();
2380 // This tells the driver code to wait until the symbol table has
2381 // written out before writing out the postprocessing sections,
2382 // including the .dynsym_shndx section.
2383 this->any_postprocessing_sections_
= true;
2386 // Create the dynamic string table section.
2388 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2393 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2394 dynstr
->add_output_section_data(strdata
);
2396 dynsym
->set_link_section(dynstr
);
2397 this->dynamic_section_
->set_link_section(dynstr
);
2399 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2400 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2404 // Create the hash tables.
2406 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2407 || strcmp(parameters
->options().hash_style(), "both") == 0)
2409 unsigned char* phash
;
2410 unsigned int hashlen
;
2411 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2414 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2419 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2423 hashsec
->add_output_section_data(hashdata
);
2425 hashsec
->set_link_section(dynsym
);
2426 hashsec
->set_entsize(4);
2428 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2431 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2432 || strcmp(parameters
->options().hash_style(), "both") == 0)
2434 unsigned char* phash
;
2435 unsigned int hashlen
;
2436 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2439 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2440 elfcpp::SHT_GNU_HASH
,
2444 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2448 hashsec
->add_output_section_data(hashdata
);
2450 hashsec
->set_link_section(dynsym
);
2451 hashsec
->set_entsize(4);
2453 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
2457 // Assign offsets to each local portion of the dynamic symbol table.
2460 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
2462 Output_section
* dynsym
= this->dynsym_section_
;
2463 gold_assert(dynsym
!= NULL
);
2465 off_t off
= dynsym
->offset();
2467 // Skip the dummy symbol at the start of the section.
2468 off
+= dynsym
->entsize();
2470 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2471 p
!= input_objects
->relobj_end();
2474 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
2475 off
+= count
* dynsym
->entsize();
2479 // Create the version sections.
2482 Layout::create_version_sections(const Versions
* versions
,
2483 const Symbol_table
* symtab
,
2484 unsigned int local_symcount
,
2485 const std::vector
<Symbol
*>& dynamic_symbols
,
2486 const Output_section
* dynstr
)
2488 if (!versions
->any_defs() && !versions
->any_needs())
2491 switch (parameters
->size_and_endianness())
2493 #ifdef HAVE_TARGET_32_LITTLE
2494 case Parameters::TARGET_32_LITTLE
:
2495 this->sized_create_version_sections
<32, false>(versions
, symtab
,
2497 dynamic_symbols
, dynstr
);
2500 #ifdef HAVE_TARGET_32_BIG
2501 case Parameters::TARGET_32_BIG
:
2502 this->sized_create_version_sections
<32, true>(versions
, symtab
,
2504 dynamic_symbols
, dynstr
);
2507 #ifdef HAVE_TARGET_64_LITTLE
2508 case Parameters::TARGET_64_LITTLE
:
2509 this->sized_create_version_sections
<64, false>(versions
, symtab
,
2511 dynamic_symbols
, dynstr
);
2514 #ifdef HAVE_TARGET_64_BIG
2515 case Parameters::TARGET_64_BIG
:
2516 this->sized_create_version_sections
<64, true>(versions
, symtab
,
2518 dynamic_symbols
, dynstr
);
2526 // Create the version sections, sized version.
2528 template<int size
, bool big_endian
>
2530 Layout::sized_create_version_sections(
2531 const Versions
* versions
,
2532 const Symbol_table
* symtab
,
2533 unsigned int local_symcount
,
2534 const std::vector
<Symbol
*>& dynamic_symbols
,
2535 const Output_section
* dynstr
)
2537 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
2538 elfcpp::SHT_GNU_versym
,
2542 unsigned char* vbuf
;
2544 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
2549 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
2552 vsec
->add_output_section_data(vdata
);
2553 vsec
->set_entsize(2);
2554 vsec
->set_link_section(this->dynsym_section_
);
2556 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2557 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
2559 if (versions
->any_defs())
2561 Output_section
* vdsec
;
2562 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
2563 elfcpp::SHT_GNU_verdef
,
2567 unsigned char* vdbuf
;
2568 unsigned int vdsize
;
2569 unsigned int vdentries
;
2570 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
2571 &vdsize
, &vdentries
);
2573 Output_section_data
* vddata
=
2574 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
2576 vdsec
->add_output_section_data(vddata
);
2577 vdsec
->set_link_section(dynstr
);
2578 vdsec
->set_info(vdentries
);
2580 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
2581 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
2584 if (versions
->any_needs())
2586 Output_section
* vnsec
;
2587 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
2588 elfcpp::SHT_GNU_verneed
,
2592 unsigned char* vnbuf
;
2593 unsigned int vnsize
;
2594 unsigned int vnentries
;
2595 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
2599 Output_section_data
* vndata
=
2600 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
2602 vnsec
->add_output_section_data(vndata
);
2603 vnsec
->set_link_section(dynstr
);
2604 vnsec
->set_info(vnentries
);
2606 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
2607 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
2611 // Create the .interp section and PT_INTERP segment.
2614 Layout::create_interp(const Target
* target
)
2616 const char* interp
= this->options_
.dynamic_linker();
2619 interp
= target
->dynamic_linker();
2620 gold_assert(interp
!= NULL
);
2623 size_t len
= strlen(interp
) + 1;
2625 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
2627 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
2628 elfcpp::SHT_PROGBITS
,
2631 osec
->add_output_section_data(odata
);
2633 if (!this->script_options_
->saw_phdrs_clause())
2635 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
2637 oseg
->add_output_section(osec
, elfcpp::PF_R
);
2641 // Finish the .dynamic section and PT_DYNAMIC segment.
2644 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
2645 const Symbol_table
* symtab
)
2647 if (!this->script_options_
->saw_phdrs_clause())
2649 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
2652 oseg
->add_output_section(this->dynamic_section_
,
2653 elfcpp::PF_R
| elfcpp::PF_W
);
2656 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2658 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
2659 p
!= input_objects
->dynobj_end();
2662 // FIXME: Handle --as-needed.
2663 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
2666 if (parameters
->options().shared())
2668 const char* soname
= this->options_
.soname();
2670 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
2673 // FIXME: Support --init and --fini.
2674 Symbol
* sym
= symtab
->lookup("_init");
2675 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2676 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
2678 sym
= symtab
->lookup("_fini");
2679 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2680 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
2682 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2684 // Add a DT_RPATH entry if needed.
2685 const General_options::Dir_list
& rpath(this->options_
.rpath());
2688 std::string rpath_val
;
2689 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
2693 if (rpath_val
.empty())
2694 rpath_val
= p
->name();
2697 // Eliminate duplicates.
2698 General_options::Dir_list::const_iterator q
;
2699 for (q
= rpath
.begin(); q
!= p
; ++q
)
2700 if (q
->name() == p
->name())
2705 rpath_val
+= p
->name();
2710 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
2711 if (parameters
->options().enable_new_dtags())
2712 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
2715 // Look for text segments that have dynamic relocations.
2716 bool have_textrel
= false;
2717 if (!this->script_options_
->saw_sections_clause())
2719 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2720 p
!= this->segment_list_
.end();
2723 if (((*p
)->flags() & elfcpp::PF_W
) == 0
2724 && (*p
)->dynamic_reloc_count() > 0)
2726 have_textrel
= true;
2733 // We don't know the section -> segment mapping, so we are
2734 // conservative and just look for readonly sections with
2735 // relocations. If those sections wind up in writable segments,
2736 // then we have created an unnecessary DT_TEXTREL entry.
2737 for (Section_list::const_iterator p
= this->section_list_
.begin();
2738 p
!= this->section_list_
.end();
2741 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
2742 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
2743 && ((*p
)->dynamic_reloc_count() > 0))
2745 have_textrel
= true;
2751 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2752 // post-link tools can easily modify these flags if desired.
2753 unsigned int flags
= 0;
2756 // Add a DT_TEXTREL for compatibility with older loaders.
2757 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
2758 flags
|= elfcpp::DF_TEXTREL
;
2760 if (parameters
->options().shared() && this->has_static_tls())
2761 flags
|= elfcpp::DF_STATIC_TLS
;
2762 if (parameters
->options().origin())
2763 flags
|= elfcpp::DF_ORIGIN
;
2764 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
2767 if (parameters
->options().initfirst())
2768 flags
|= elfcpp::DF_1_INITFIRST
;
2769 if (parameters
->options().interpose())
2770 flags
|= elfcpp::DF_1_INTERPOSE
;
2771 if (parameters
->options().loadfltr())
2772 flags
|= elfcpp::DF_1_LOADFLTR
;
2773 if (parameters
->options().nodefaultlib())
2774 flags
|= elfcpp::DF_1_NODEFLIB
;
2775 if (parameters
->options().nodelete())
2776 flags
|= elfcpp::DF_1_NODELETE
;
2777 if (parameters
->options().nodlopen())
2778 flags
|= elfcpp::DF_1_NOOPEN
;
2779 if (parameters
->options().nodump())
2780 flags
|= elfcpp::DF_1_NODUMP
;
2781 if (!parameters
->options().shared())
2782 flags
&= ~(elfcpp::DF_1_INITFIRST
2783 | elfcpp::DF_1_NODELETE
2784 | elfcpp::DF_1_NOOPEN
);
2785 if (parameters
->options().origin())
2786 flags
|= elfcpp::DF_1_ORIGIN
;
2788 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
2791 // The mapping of .gnu.linkonce section names to real section names.
2793 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2794 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
2796 MAPPING_INIT("d.rel.ro.local", ".data.rel.ro.local"), // Before "d.rel.ro".
2797 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Before "d".
2798 MAPPING_INIT("t", ".text"),
2799 MAPPING_INIT("r", ".rodata"),
2800 MAPPING_INIT("d", ".data"),
2801 MAPPING_INIT("b", ".bss"),
2802 MAPPING_INIT("s", ".sdata"),
2803 MAPPING_INIT("sb", ".sbss"),
2804 MAPPING_INIT("s2", ".sdata2"),
2805 MAPPING_INIT("sb2", ".sbss2"),
2806 MAPPING_INIT("wi", ".debug_info"),
2807 MAPPING_INIT("td", ".tdata"),
2808 MAPPING_INIT("tb", ".tbss"),
2809 MAPPING_INIT("lr", ".lrodata"),
2810 MAPPING_INIT("l", ".ldata"),
2811 MAPPING_INIT("lb", ".lbss"),
2815 const int Layout::linkonce_mapping_count
=
2816 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
2818 // Return the name of the output section to use for a .gnu.linkonce
2819 // section. This is based on the default ELF linker script of the old
2820 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2821 // to ".text". Set *PLEN to the length of the name. *PLEN is
2822 // initialized to the length of NAME.
2825 Layout::linkonce_output_name(const char* name
, size_t *plen
)
2827 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
2831 const Linkonce_mapping
* plm
= linkonce_mapping
;
2832 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
2834 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
2843 // Choose the output section name to use given an input section name.
2844 // Set *PLEN to the length of the name. *PLEN is initialized to the
2848 Layout::output_section_name(const char* name
, size_t* plen
)
2850 if (Layout::is_linkonce(name
))
2852 // .gnu.linkonce sections are laid out as though they were named
2853 // for the sections are placed into.
2854 return Layout::linkonce_output_name(name
, plen
);
2857 // gcc 4.3 generates the following sorts of section names when it
2858 // needs a section name specific to a function:
2864 // .data.rel.local.FN
2866 // .data.rel.ro.local.FN
2873 // The GNU linker maps all of those to the part before the .FN,
2874 // except that .data.rel.local.FN is mapped to .data, and
2875 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2876 // beginning with .data.rel.ro.local are grouped together.
2878 // For an anonymous namespace, the string FN can contain a '.'.
2880 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2881 // GNU linker maps to .rodata.
2883 // The .data.rel.ro sections enable a security feature triggered by
2884 // the -z relro option. Section which need to be relocated at
2885 // program startup time but which may be readonly after startup are
2886 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2887 // segment. The dynamic linker will make that segment writable,
2888 // perform relocations, and then make it read-only. FIXME: We do
2889 // not yet implement this optimization.
2891 // It is hard to handle this in a principled way.
2893 // These are the rules we follow:
2895 // If the section name has no initial '.', or no dot other than an
2896 // initial '.', we use the name unchanged (i.e., "mysection" and
2897 // ".text" are unchanged).
2899 // If the name starts with ".data.rel.ro.local" we use
2900 // ".data.rel.ro.local".
2902 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2904 // Otherwise, we drop the second '.' and everything that comes after
2905 // it (i.e., ".text.XXX" becomes ".text").
2907 const char* s
= name
;
2911 const char* sdot
= strchr(s
, '.');
2915 const char* const data_rel_ro_local
= ".data.rel.ro.local";
2916 if (strncmp(name
, data_rel_ro_local
, strlen(data_rel_ro_local
)) == 0)
2918 *plen
= strlen(data_rel_ro_local
);
2919 return data_rel_ro_local
;
2922 const char* const data_rel_ro
= ".data.rel.ro";
2923 if (strncmp(name
, data_rel_ro
, strlen(data_rel_ro
)) == 0)
2925 *plen
= strlen(data_rel_ro
);
2929 *plen
= sdot
- name
;
2933 // Check if a comdat group or .gnu.linkonce section with the given
2934 // NAME is selected for the link. If there is already a section,
2935 // *KEPT_SECTION is set to point to the signature and the function
2936 // returns false. Otherwise, the CANDIDATE signature is recorded for
2937 // this NAME in the layout object, *KEPT_SECTION is set to the
2938 // internal copy and the function return false. In some cases, with
2939 // CANDIDATE->GROUP_ being false, KEPT_SECTION can point back to
2943 Layout::find_or_add_kept_section(const std::string name
,
2944 Kept_section
* candidate
,
2945 Kept_section
** kept_section
)
2947 std::pair
<Signatures::iterator
, bool> ins(
2948 this->signatures_
.insert(std::make_pair(name
, *candidate
)));
2951 *kept_section
= &ins
.first
->second
;
2954 // This is the first time we've seen this signature.
2958 if (ins
.first
->second
.is_group
)
2960 // We've already seen a real section group with this signature.
2961 // If the kept group is from a plugin object, and we're in
2962 // the replacement phase, accept the new one as a replacement.
2963 if (ins
.first
->second
.object
== NULL
2964 && parameters
->options().plugins()->in_replacement_phase())
2966 ins
.first
->second
= *candidate
;
2971 else if (candidate
->is_group
)
2973 // This is a real section group, and we've already seen a
2974 // linkonce section with this signature. Record that we've seen
2975 // a section group, and don't include this section group.
2976 ins
.first
->second
.is_group
= true;
2981 // We've already seen a linkonce section and this is a linkonce
2982 // section. These don't block each other--this may be the same
2983 // symbol name with different section types.
2984 *kept_section
= candidate
;
2989 // Find the given comdat signature, and return the object and section
2990 // index of the kept group.
2992 Layout::find_kept_object(const std::string
& signature
,
2993 unsigned int* pshndx
) const
2995 Signatures::const_iterator p
= this->signatures_
.find(signature
);
2996 if (p
== this->signatures_
.end())
2999 *pshndx
= p
->second
.shndx
;
3000 return p
->second
.object
;
3003 // Store the allocated sections into the section list.
3006 Layout::get_allocated_sections(Section_list
* section_list
) const
3008 for (Section_list::const_iterator p
= this->section_list_
.begin();
3009 p
!= this->section_list_
.end();
3011 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
3012 section_list
->push_back(*p
);
3015 // Create an output segment.
3018 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3020 gold_assert(!parameters
->options().relocatable());
3021 Output_segment
* oseg
= new Output_segment(type
, flags
);
3022 this->segment_list_
.push_back(oseg
);
3024 if (type
== elfcpp::PT_TLS
)
3025 this->tls_segment_
= oseg
;
3026 else if (type
== elfcpp::PT_GNU_RELRO
)
3027 this->relro_segment_
= oseg
;
3032 // Write out the Output_sections. Most won't have anything to write,
3033 // since most of the data will come from input sections which are
3034 // handled elsewhere. But some Output_sections do have Output_data.
3037 Layout::write_output_sections(Output_file
* of
) const
3039 for (Section_list::const_iterator p
= this->section_list_
.begin();
3040 p
!= this->section_list_
.end();
3043 if (!(*p
)->after_input_sections())
3048 // Write out data not associated with a section or the symbol table.
3051 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
3053 if (!parameters
->options().strip_all())
3055 const Output_section
* symtab_section
= this->symtab_section_
;
3056 for (Section_list::const_iterator p
= this->section_list_
.begin();
3057 p
!= this->section_list_
.end();
3060 if ((*p
)->needs_symtab_index())
3062 gold_assert(symtab_section
!= NULL
);
3063 unsigned int index
= (*p
)->symtab_index();
3064 gold_assert(index
> 0 && index
!= -1U);
3065 off_t off
= (symtab_section
->offset()
3066 + index
* symtab_section
->entsize());
3067 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
3072 const Output_section
* dynsym_section
= this->dynsym_section_
;
3073 for (Section_list::const_iterator p
= this->section_list_
.begin();
3074 p
!= this->section_list_
.end();
3077 if ((*p
)->needs_dynsym_index())
3079 gold_assert(dynsym_section
!= NULL
);
3080 unsigned int index
= (*p
)->dynsym_index();
3081 gold_assert(index
> 0 && index
!= -1U);
3082 off_t off
= (dynsym_section
->offset()
3083 + index
* dynsym_section
->entsize());
3084 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
3088 // Write out the Output_data which are not in an Output_section.
3089 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
3090 p
!= this->special_output_list_
.end();
3095 // Write out the Output_sections which can only be written after the
3096 // input sections are complete.
3099 Layout::write_sections_after_input_sections(Output_file
* of
)
3101 // Determine the final section offsets, and thus the final output
3102 // file size. Note we finalize the .shstrab last, to allow the
3103 // after_input_section sections to modify their section-names before
3105 if (this->any_postprocessing_sections_
)
3107 off_t off
= this->output_file_size_
;
3108 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
3110 // Now that we've finalized the names, we can finalize the shstrab.
3112 this->set_section_offsets(off
,
3113 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
3115 if (off
> this->output_file_size_
)
3118 this->output_file_size_
= off
;
3122 for (Section_list::const_iterator p
= this->section_list_
.begin();
3123 p
!= this->section_list_
.end();
3126 if ((*p
)->after_input_sections())
3130 this->section_headers_
->write(of
);
3133 // If the build ID requires computing a checksum, do so here, and
3134 // write it out. We compute a checksum over the entire file because
3135 // that is simplest.
3138 Layout::write_build_id(Output_file
* of
) const
3140 if (this->build_id_note_
== NULL
)
3143 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3145 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3146 this->build_id_note_
->data_size());
3148 const char* style
= parameters
->options().build_id();
3149 if (strcmp(style
, "sha1") == 0)
3152 sha1_init_ctx(&ctx
);
3153 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3154 sha1_finish_ctx(&ctx
, ov
);
3156 else if (strcmp(style
, "md5") == 0)
3160 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3161 md5_finish_ctx(&ctx
, ov
);
3166 of
->write_output_view(this->build_id_note_
->offset(),
3167 this->build_id_note_
->data_size(),
3170 of
->free_input_view(0, this->output_file_size_
, iv
);
3173 // Write out a binary file. This is called after the link is
3174 // complete. IN is the temporary output file we used to generate the
3175 // ELF code. We simply walk through the segments, read them from
3176 // their file offset in IN, and write them to their load address in
3177 // the output file. FIXME: with a bit more work, we could support
3178 // S-records and/or Intel hex format here.
3181 Layout::write_binary(Output_file
* in
) const
3183 gold_assert(this->options_
.oformat_enum()
3184 == General_options::OBJECT_FORMAT_BINARY
);
3186 // Get the size of the binary file.
3187 uint64_t max_load_address
= 0;
3188 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3189 p
!= this->segment_list_
.end();
3192 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3194 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3195 if (max_paddr
> max_load_address
)
3196 max_load_address
= max_paddr
;
3200 Output_file
out(parameters
->options().output_file_name());
3201 out
.open(max_load_address
);
3203 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3204 p
!= this->segment_list_
.end();
3207 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3209 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3211 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3213 memcpy(vout
, vin
, (*p
)->filesz());
3214 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3215 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3222 // Print the output sections to the map file.
3225 Layout::print_to_mapfile(Mapfile
* mapfile
) const
3227 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3228 p
!= this->segment_list_
.end();
3230 (*p
)->print_sections_to_mapfile(mapfile
);
3233 // Print statistical information to stderr. This is used for --stats.
3236 Layout::print_stats() const
3238 this->namepool_
.print_stats("section name pool");
3239 this->sympool_
.print_stats("output symbol name pool");
3240 this->dynpool_
.print_stats("dynamic name pool");
3242 for (Section_list::const_iterator p
= this->section_list_
.begin();
3243 p
!= this->section_list_
.end();
3245 (*p
)->print_merge_stats();
3248 // Write_sections_task methods.
3250 // We can always run this task.
3253 Write_sections_task::is_runnable()
3258 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3262 Write_sections_task::locks(Task_locker
* tl
)
3264 tl
->add(this, this->output_sections_blocker_
);
3265 tl
->add(this, this->final_blocker_
);
3268 // Run the task--write out the data.
3271 Write_sections_task::run(Workqueue
*)
3273 this->layout_
->write_output_sections(this->of_
);
3276 // Write_data_task methods.
3278 // We can always run this task.
3281 Write_data_task::is_runnable()
3286 // We need to unlock FINAL_BLOCKER when finished.
3289 Write_data_task::locks(Task_locker
* tl
)
3291 tl
->add(this, this->final_blocker_
);
3294 // Run the task--write out the data.
3297 Write_data_task::run(Workqueue
*)
3299 this->layout_
->write_data(this->symtab_
, this->of_
);
3302 // Write_symbols_task methods.
3304 // We can always run this task.
3307 Write_symbols_task::is_runnable()
3312 // We need to unlock FINAL_BLOCKER when finished.
3315 Write_symbols_task::locks(Task_locker
* tl
)
3317 tl
->add(this, this->final_blocker_
);
3320 // Run the task--write out the symbols.
3323 Write_symbols_task::run(Workqueue
*)
3325 this->symtab_
->write_globals(this->sympool_
, this->dynpool_
,
3326 this->layout_
->symtab_xindex(),
3327 this->layout_
->dynsym_xindex(), this->of_
);
3330 // Write_after_input_sections_task methods.
3332 // We can only run this task after the input sections have completed.
3335 Write_after_input_sections_task::is_runnable()
3337 if (this->input_sections_blocker_
->is_blocked())
3338 return this->input_sections_blocker_
;
3342 // We need to unlock FINAL_BLOCKER when finished.
3345 Write_after_input_sections_task::locks(Task_locker
* tl
)
3347 tl
->add(this, this->final_blocker_
);
3353 Write_after_input_sections_task::run(Workqueue
*)
3355 this->layout_
->write_sections_after_input_sections(this->of_
);
3358 // Close_task_runner methods.
3360 // Run the task--close the file.
3363 Close_task_runner::run(Workqueue
*, const Task
*)
3365 // If we need to compute a checksum for the BUILD if, we do so here.
3366 this->layout_
->write_build_id(this->of_
);
3368 // If we've been asked to create a binary file, we do so here.
3369 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3370 this->layout_
->write_binary(this->of_
);
3375 // Instantiate the templates we need. We could use the configure
3376 // script to restrict this to only the ones for implemented targets.
3378 #ifdef HAVE_TARGET_32_LITTLE
3381 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3383 const elfcpp::Shdr
<32, false>& shdr
,
3384 unsigned int, unsigned int, off_t
*);
3387 #ifdef HAVE_TARGET_32_BIG
3390 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3392 const elfcpp::Shdr
<32, true>& shdr
,
3393 unsigned int, unsigned int, off_t
*);
3396 #ifdef HAVE_TARGET_64_LITTLE
3399 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3401 const elfcpp::Shdr
<64, false>& shdr
,
3402 unsigned int, unsigned int, off_t
*);
3405 #ifdef HAVE_TARGET_64_BIG
3408 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3410 const elfcpp::Shdr
<64, true>& shdr
,
3411 unsigned int, unsigned int, off_t
*);
3414 #ifdef HAVE_TARGET_32_LITTLE
3417 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
3418 unsigned int reloc_shndx
,
3419 const elfcpp::Shdr
<32, false>& shdr
,
3420 Output_section
* data_section
,
3421 Relocatable_relocs
* rr
);
3424 #ifdef HAVE_TARGET_32_BIG
3427 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
3428 unsigned int reloc_shndx
,
3429 const elfcpp::Shdr
<32, true>& shdr
,
3430 Output_section
* data_section
,
3431 Relocatable_relocs
* rr
);
3434 #ifdef HAVE_TARGET_64_LITTLE
3437 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
3438 unsigned int reloc_shndx
,
3439 const elfcpp::Shdr
<64, false>& shdr
,
3440 Output_section
* data_section
,
3441 Relocatable_relocs
* rr
);
3444 #ifdef HAVE_TARGET_64_BIG
3447 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
3448 unsigned int reloc_shndx
,
3449 const elfcpp::Shdr
<64, true>& shdr
,
3450 Output_section
* data_section
,
3451 Relocatable_relocs
* rr
);
3454 #ifdef HAVE_TARGET_32_LITTLE
3457 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
3458 Sized_relobj
<32, false>* object
,
3460 const char* group_section_name
,
3461 const char* signature
,
3462 const elfcpp::Shdr
<32, false>& shdr
,
3463 elfcpp::Elf_Word flags
,
3464 std::vector
<unsigned int>* shndxes
);
3467 #ifdef HAVE_TARGET_32_BIG
3470 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
3471 Sized_relobj
<32, true>* object
,
3473 const char* group_section_name
,
3474 const char* signature
,
3475 const elfcpp::Shdr
<32, true>& shdr
,
3476 elfcpp::Elf_Word flags
,
3477 std::vector
<unsigned int>* shndxes
);
3480 #ifdef HAVE_TARGET_64_LITTLE
3483 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
3484 Sized_relobj
<64, false>* object
,
3486 const char* group_section_name
,
3487 const char* signature
,
3488 const elfcpp::Shdr
<64, false>& shdr
,
3489 elfcpp::Elf_Word flags
,
3490 std::vector
<unsigned int>* shndxes
);
3493 #ifdef HAVE_TARGET_64_BIG
3496 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
3497 Sized_relobj
<64, true>* object
,
3499 const char* group_section_name
,
3500 const char* signature
,
3501 const elfcpp::Shdr
<64, true>& shdr
,
3502 elfcpp::Elf_Word flags
,
3503 std::vector
<unsigned int>* shndxes
);
3506 #ifdef HAVE_TARGET_32_LITTLE
3509 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
3510 const unsigned char* symbols
,
3512 const unsigned char* symbol_names
,
3513 off_t symbol_names_size
,
3515 const elfcpp::Shdr
<32, false>& shdr
,
3516 unsigned int reloc_shndx
,
3517 unsigned int reloc_type
,
3521 #ifdef HAVE_TARGET_32_BIG
3524 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
3525 const unsigned char* symbols
,
3527 const unsigned char* symbol_names
,
3528 off_t symbol_names_size
,
3530 const elfcpp::Shdr
<32, true>& shdr
,
3531 unsigned int reloc_shndx
,
3532 unsigned int reloc_type
,
3536 #ifdef HAVE_TARGET_64_LITTLE
3539 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
3540 const unsigned char* symbols
,
3542 const unsigned char* symbol_names
,
3543 off_t symbol_names_size
,
3545 const elfcpp::Shdr
<64, false>& shdr
,
3546 unsigned int reloc_shndx
,
3547 unsigned int reloc_type
,
3551 #ifdef HAVE_TARGET_64_BIG
3554 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
3555 const unsigned char* symbols
,
3557 const unsigned char* symbol_names
,
3558 off_t symbol_names_size
,
3560 const elfcpp::Shdr
<64, true>& shdr
,
3561 unsigned int reloc_shndx
,
3562 unsigned int reloc_type
,
3566 } // End namespace gold.