1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008 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"
53 // Layout_task_runner methods.
55 // Lay out the sections. This is called after all the input objects
59 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
61 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
66 // Now we know the final size of the output file and we know where
67 // each piece of information goes.
69 if (this->mapfile_
!= NULL
)
71 this->mapfile_
->print_discarded_sections(this->input_objects_
);
72 this->layout_
->print_to_mapfile(this->mapfile_
);
75 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
76 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
77 of
->set_is_temporary();
80 // Queue up the final set of tasks.
81 gold::queue_final_tasks(this->options_
, this->input_objects_
,
82 this->symtab_
, this->layout_
, workqueue
, of
);
87 Layout::Layout(const General_options
& options
, Script_options
* script_options
)
89 script_options_(script_options
),
97 unattached_section_list_(),
98 sections_are_attached_(false),
99 special_output_list_(),
100 section_headers_(NULL
),
102 relro_segment_(NULL
),
103 symtab_section_(NULL
),
104 symtab_xindex_(NULL
),
105 dynsym_section_(NULL
),
106 dynsym_xindex_(NULL
),
107 dynamic_section_(NULL
),
109 eh_frame_section_(NULL
),
110 eh_frame_data_(NULL
),
111 added_eh_frame_data_(false),
112 eh_frame_hdr_section_(NULL
),
113 build_id_note_(NULL
),
117 output_file_size_(-1),
118 input_requires_executable_stack_(false),
119 input_with_gnu_stack_note_(false),
120 input_without_gnu_stack_note_(false),
121 has_static_tls_(false),
122 any_postprocessing_sections_(false)
124 // Make space for more than enough segments for a typical file.
125 // This is just for efficiency--it's OK if we wind up needing more.
126 this->segment_list_
.reserve(12);
128 // We expect two unattached Output_data objects: the file header and
129 // the segment headers.
130 this->special_output_list_
.reserve(2);
133 // Hash a key we use to look up an output section mapping.
136 Layout::Hash_key::operator()(const Layout::Key
& k
) const
138 return k
.first
+ k
.second
.first
+ k
.second
.second
;
141 // Return whether PREFIX is a prefix of STR.
144 is_prefix_of(const char* prefix
, const char* str
)
146 return strncmp(prefix
, str
, strlen(prefix
)) == 0;
149 // Returns whether the given section is in the list of
150 // debug-sections-used-by-some-version-of-gdb. Currently,
151 // we've checked versions of gdb up to and including 6.7.1.
153 static const char* gdb_sections
[] =
155 // ".debug_aranges", // not used by gdb as of 6.7.1
161 // ".debug_pubnames", // not used by gdb as of 6.7.1
166 static const char* lines_only_debug_sections
[] =
168 // ".debug_aranges", // not used by gdb as of 6.7.1
174 // ".debug_pubnames", // not used by gdb as of 6.7.1
180 is_gdb_debug_section(const char* str
)
182 // We can do this faster: binary search or a hashtable. But why bother?
183 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
184 if (strcmp(str
, gdb_sections
[i
]) == 0)
190 is_lines_only_debug_section(const char* str
)
192 // We can do this faster: binary search or a hashtable. But why bother?
194 i
< sizeof(lines_only_debug_sections
)/sizeof(*lines_only_debug_sections
);
196 if (strcmp(str
, lines_only_debug_sections
[i
]) == 0)
201 // Whether to include this section in the link.
203 template<int size
, bool big_endian
>
205 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
206 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
208 switch (shdr
.get_sh_type())
210 case elfcpp::SHT_NULL
:
211 case elfcpp::SHT_SYMTAB
:
212 case elfcpp::SHT_DYNSYM
:
213 case elfcpp::SHT_HASH
:
214 case elfcpp::SHT_DYNAMIC
:
215 case elfcpp::SHT_SYMTAB_SHNDX
:
218 case elfcpp::SHT_STRTAB
:
219 // Discard the sections which have special meanings in the ELF
220 // ABI. Keep others (e.g., .stabstr). We could also do this by
221 // checking the sh_link fields of the appropriate sections.
222 return (strcmp(name
, ".dynstr") != 0
223 && strcmp(name
, ".strtab") != 0
224 && strcmp(name
, ".shstrtab") != 0);
226 case elfcpp::SHT_RELA
:
227 case elfcpp::SHT_REL
:
228 case elfcpp::SHT_GROUP
:
229 // If we are emitting relocations these should be handled
231 gold_assert(!parameters
->options().relocatable()
232 && !parameters
->options().emit_relocs());
235 case elfcpp::SHT_PROGBITS
:
236 if (parameters
->options().strip_debug()
237 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
239 if (is_debug_info_section(name
))
242 if (parameters
->options().strip_debug_non_line()
243 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
245 // Debugging sections can only be recognized by name.
246 if (is_prefix_of(".debug", name
)
247 && !is_lines_only_debug_section(name
))
250 if (parameters
->options().strip_debug_gdb()
251 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
253 // Debugging sections can only be recognized by name.
254 if (is_prefix_of(".debug", name
)
255 && !is_gdb_debug_section(name
))
265 // Return an output section named NAME, or NULL if there is none.
268 Layout::find_output_section(const char* name
) const
270 for (Section_list::const_iterator p
= this->section_list_
.begin();
271 p
!= this->section_list_
.end();
273 if (strcmp((*p
)->name(), name
) == 0)
278 // Return an output segment of type TYPE, with segment flags SET set
279 // and segment flags CLEAR clear. Return NULL if there is none.
282 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
283 elfcpp::Elf_Word clear
) const
285 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
286 p
!= this->segment_list_
.end();
288 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
289 && ((*p
)->flags() & set
) == set
290 && ((*p
)->flags() & clear
) == 0)
295 // Return the output section to use for section NAME with type TYPE
296 // and section flags FLAGS. NAME must be canonicalized in the string
297 // pool, and NAME_KEY is the key.
300 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
301 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
303 elfcpp::Elf_Xword lookup_flags
= flags
;
305 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
306 // read-write with read-only sections. Some other ELF linkers do
307 // not do this. FIXME: Perhaps there should be an option
309 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
311 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
312 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
313 std::pair
<Section_name_map::iterator
, bool> ins(
314 this->section_name_map_
.insert(v
));
317 return ins
.first
->second
;
320 // This is the first time we've seen this name/type/flags
321 // combination. For compatibility with the GNU linker, we
322 // combine sections with contents and zero flags with sections
323 // with non-zero flags. This is a workaround for cases where
324 // assembler code forgets to set section flags. FIXME: Perhaps
325 // there should be an option to control this.
326 Output_section
* os
= NULL
;
328 if (type
== elfcpp::SHT_PROGBITS
)
332 Output_section
* same_name
= this->find_output_section(name
);
333 if (same_name
!= NULL
334 && same_name
->type() == elfcpp::SHT_PROGBITS
335 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
338 else if ((flags
& elfcpp::SHF_TLS
) == 0)
340 elfcpp::Elf_Xword zero_flags
= 0;
341 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
342 Section_name_map::iterator p
=
343 this->section_name_map_
.find(zero_key
);
344 if (p
!= this->section_name_map_
.end())
350 os
= this->make_output_section(name
, type
, flags
);
351 ins
.first
->second
= os
;
356 // Pick the output section to use for section NAME, in input file
357 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
358 // linker created section. IS_INPUT_SECTION is true if we are
359 // choosing an output section for an input section found in a input
360 // file. This will return NULL if the input section should be
364 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
365 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
366 bool is_input_section
)
368 // We should not see any input sections after we have attached
369 // sections to segments.
370 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
372 // Some flags in the input section should not be automatically
373 // copied to the output section.
374 flags
&= ~ (elfcpp::SHF_INFO_LINK
375 | elfcpp::SHF_LINK_ORDER
378 | elfcpp::SHF_STRINGS
);
380 if (this->script_options_
->saw_sections_clause())
382 // We are using a SECTIONS clause, so the output section is
383 // chosen based only on the name.
385 Script_sections
* ss
= this->script_options_
->script_sections();
386 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
387 Output_section
** output_section_slot
;
388 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
391 // The SECTIONS clause says to discard this input section.
395 // If this is an orphan section--one not mentioned in the linker
396 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
397 // default processing below.
399 if (output_section_slot
!= NULL
)
401 if (*output_section_slot
!= NULL
)
402 return *output_section_slot
;
404 // We don't put sections found in the linker script into
405 // SECTION_NAME_MAP_. That keeps us from getting confused
406 // if an orphan section is mapped to a section with the same
407 // name as one in the linker script.
409 name
= this->namepool_
.add(name
, false, NULL
);
411 Output_section
* os
= this->make_output_section(name
, type
, flags
);
412 os
->set_found_in_sections_clause();
413 *output_section_slot
= os
;
418 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
420 // Turn NAME from the name of the input section into the name of the
423 size_t len
= strlen(name
);
424 if (is_input_section
&& !parameters
->options().relocatable())
425 name
= Layout::output_section_name(name
, &len
);
427 Stringpool::Key name_key
;
428 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
430 // Find or make the output section. The output section is selected
431 // based on the section name, type, and flags.
432 return this->get_output_section(name
, name_key
, type
, flags
);
435 // Return the output section to use for input section SHNDX, with name
436 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
437 // index of a relocation section which applies to this section, or 0
438 // if none, or -1U if more than one. RELOC_TYPE is the type of the
439 // relocation section if there is one. Set *OFF to the offset of this
440 // input section without the output section. Return NULL if the
441 // section should be discarded. Set *OFF to -1 if the section
442 // contents should not be written directly to the output file, but
443 // will instead receive special handling.
445 template<int size
, bool big_endian
>
447 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
448 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
449 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
451 if (!this->include_section(object
, name
, shdr
))
456 // In a relocatable link a grouped section must not be combined with
457 // any other sections.
458 if (parameters
->options().relocatable()
459 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
461 name
= this->namepool_
.add(name
, true, NULL
);
462 os
= this->make_output_section(name
, shdr
.get_sh_type(),
463 shdr
.get_sh_flags());
467 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
468 shdr
.get_sh_flags(), true);
473 // By default the GNU linker sorts input sections whose names match
474 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
475 // are sorted by name. This is used to implement constructor
476 // priority ordering. We are compatible.
477 if (!this->script_options_
->saw_sections_clause()
478 && (is_prefix_of(".ctors.", name
)
479 || is_prefix_of(".dtors.", name
)
480 || is_prefix_of(".init_array.", name
)
481 || is_prefix_of(".fini_array.", name
)))
482 os
->set_must_sort_attached_input_sections();
484 // FIXME: Handle SHF_LINK_ORDER somewhere.
486 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
487 this->script_options_
->saw_sections_clause());
492 // Handle a relocation section when doing a relocatable link.
494 template<int size
, bool big_endian
>
496 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
498 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
499 Output_section
* data_section
,
500 Relocatable_relocs
* rr
)
502 gold_assert(parameters
->options().relocatable()
503 || parameters
->options().emit_relocs());
505 int sh_type
= shdr
.get_sh_type();
508 if (sh_type
== elfcpp::SHT_REL
)
510 else if (sh_type
== elfcpp::SHT_RELA
)
514 name
+= data_section
->name();
516 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
521 os
->set_should_link_to_symtab();
522 os
->set_info_section(data_section
);
524 Output_section_data
* posd
;
525 if (sh_type
== elfcpp::SHT_REL
)
527 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
528 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
532 else if (sh_type
== elfcpp::SHT_RELA
)
534 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
535 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
542 os
->add_output_section_data(posd
);
543 rr
->set_output_data(posd
);
548 // Handle a group section when doing a relocatable link.
550 template<int size
, bool big_endian
>
552 Layout::layout_group(Symbol_table
* symtab
,
553 Sized_relobj
<size
, big_endian
>* object
,
555 const char* group_section_name
,
556 const char* signature
,
557 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
558 elfcpp::Elf_Word flags
,
559 std::vector
<unsigned int>* shndxes
)
561 gold_assert(parameters
->options().relocatable());
562 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
563 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
564 Output_section
* os
= this->make_output_section(group_section_name
,
566 shdr
.get_sh_flags());
568 // We need to find a symbol with the signature in the symbol table.
569 // If we don't find one now, we need to look again later.
570 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
572 os
->set_info_symndx(sym
);
575 // We will wind up using a symbol whose name is the signature.
576 // So just put the signature in the symbol name pool to save it.
577 signature
= symtab
->canonicalize_name(signature
);
578 this->group_signatures_
.push_back(Group_signature(os
, signature
));
581 os
->set_should_link_to_symtab();
584 section_size_type entry_count
=
585 convert_to_section_size_type(shdr
.get_sh_size() / 4);
586 Output_section_data
* posd
=
587 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
589 os
->add_output_section_data(posd
);
592 // Special GNU handling of sections name .eh_frame. They will
593 // normally hold exception frame data as defined by the C++ ABI
594 // (http://codesourcery.com/cxx-abi/).
596 template<int size
, bool big_endian
>
598 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
599 const unsigned char* symbols
,
601 const unsigned char* symbol_names
,
602 off_t symbol_names_size
,
604 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
605 unsigned int reloc_shndx
, unsigned int reloc_type
,
608 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
609 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
611 const char* const name
= ".eh_frame";
612 Output_section
* os
= this->choose_output_section(object
,
614 elfcpp::SHT_PROGBITS
,
620 if (this->eh_frame_section_
== NULL
)
622 this->eh_frame_section_
= os
;
623 this->eh_frame_data_
= new Eh_frame();
625 if (this->options_
.eh_frame_hdr())
627 Output_section
* hdr_os
=
628 this->choose_output_section(NULL
,
630 elfcpp::SHT_PROGBITS
,
636 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
637 this->eh_frame_data_
);
638 hdr_os
->add_output_section_data(hdr_posd
);
640 hdr_os
->set_after_input_sections();
642 if (!this->script_options_
->saw_phdrs_clause())
644 Output_segment
* hdr_oseg
;
645 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
647 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
);
650 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
655 gold_assert(this->eh_frame_section_
== os
);
657 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
666 os
->update_flags_for_input_section(shdr
.get_sh_flags());
668 // We found a .eh_frame section we are going to optimize, so now
669 // we can add the set of optimized sections to the output
670 // section. We need to postpone adding this until we've found a
671 // section we can optimize so that the .eh_frame section in
672 // crtbegin.o winds up at the start of the output section.
673 if (!this->added_eh_frame_data_
)
675 os
->add_output_section_data(this->eh_frame_data_
);
676 this->added_eh_frame_data_
= true;
682 // We couldn't handle this .eh_frame section for some reason.
683 // Add it as a normal section.
684 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
685 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
686 saw_sections_clause
);
692 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
693 // the output section.
696 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
697 elfcpp::Elf_Xword flags
,
698 Output_section_data
* posd
)
700 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
703 os
->add_output_section_data(posd
);
707 // Map section flags to segment flags.
710 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
712 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
713 if ((flags
& elfcpp::SHF_WRITE
) != 0)
715 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
720 // Sometimes we compress sections. This is typically done for
721 // sections that are not part of normal program execution (such as
722 // .debug_* sections), and where the readers of these sections know
723 // how to deal with compressed sections. (To make it easier for them,
724 // we will rename the ouput section in such cases from .foo to
725 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
726 // doesn't say for certain whether we'll compress -- it depends on
727 // commandline options as well -- just whether this section is a
728 // candidate for compression.
731 is_compressible_debug_section(const char* secname
)
733 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
736 // Make a new Output_section, and attach it to segments as
740 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
741 elfcpp::Elf_Xword flags
)
744 if ((flags
& elfcpp::SHF_ALLOC
) == 0
745 && strcmp(this->options_
.compress_debug_sections(), "none") != 0
746 && is_compressible_debug_section(name
))
747 os
= new Output_compressed_section(&this->options_
, name
, type
, flags
);
749 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
750 && this->options_
.strip_debug_non_line()
751 && strcmp(".debug_abbrev", name
) == 0)
753 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
755 if (this->debug_info_
)
756 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
758 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
759 && this->options_
.strip_debug_non_line()
760 && strcmp(".debug_info", name
) == 0)
762 os
= this->debug_info_
= new Output_reduced_debug_info_section(
764 if (this->debug_abbrev_
)
765 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
768 os
= new Output_section(name
, type
, flags
);
770 this->section_list_
.push_back(os
);
772 // The GNU linker by default sorts some sections by priority, so we
773 // do the same. We need to know that this might happen before we
774 // attach any input sections.
775 if (!this->script_options_
->saw_sections_clause()
776 && (strcmp(name
, ".ctors") == 0
777 || strcmp(name
, ".dtors") == 0
778 || strcmp(name
, ".init_array") == 0
779 || strcmp(name
, ".fini_array") == 0))
780 os
->set_may_sort_attached_input_sections();
782 // With -z relro, we have to recognize the special sections by name.
783 // There is no other way.
784 if (!this->script_options_
->saw_sections_clause()
785 && parameters
->options().relro()
786 && type
== elfcpp::SHT_PROGBITS
787 && (flags
& elfcpp::SHF_ALLOC
) != 0
788 && (flags
& elfcpp::SHF_WRITE
) != 0)
790 if (strcmp(name
, ".data.rel.ro") == 0)
792 else if (strcmp(name
, ".data.rel.ro.local") == 0)
795 os
->set_is_relro_local();
799 // If we have already attached the sections to segments, then we
800 // need to attach this one now. This happens for sections created
801 // directly by the linker.
802 if (this->sections_are_attached_
)
803 this->attach_section_to_segment(os
);
808 // Attach output sections to segments. This is called after we have
809 // seen all the input sections.
812 Layout::attach_sections_to_segments()
814 for (Section_list::iterator p
= this->section_list_
.begin();
815 p
!= this->section_list_
.end();
817 this->attach_section_to_segment(*p
);
819 this->sections_are_attached_
= true;
822 // Attach an output section to a segment.
825 Layout::attach_section_to_segment(Output_section
* os
)
827 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
828 this->unattached_section_list_
.push_back(os
);
830 this->attach_allocated_section_to_segment(os
);
833 // Attach an allocated output section to a segment.
836 Layout::attach_allocated_section_to_segment(Output_section
* os
)
838 elfcpp::Elf_Xword flags
= os
->flags();
839 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
841 if (parameters
->options().relocatable())
844 // If we have a SECTIONS clause, we can't handle the attachment to
845 // segments until after we've seen all the sections.
846 if (this->script_options_
->saw_sections_clause())
849 gold_assert(!this->script_options_
->saw_phdrs_clause());
851 // This output section goes into a PT_LOAD segment.
853 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
855 // In general the only thing we really care about for PT_LOAD
856 // segments is whether or not they are writable, so that is how we
857 // search for them. People who need segments sorted on some other
858 // basis will have to use a linker script.
860 Segment_list::const_iterator p
;
861 for (p
= this->segment_list_
.begin();
862 p
!= this->segment_list_
.end();
865 if ((*p
)->type() == elfcpp::PT_LOAD
866 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
868 // If -Tbss was specified, we need to separate the data
870 if (this->options_
.user_set_Tbss())
872 if ((os
->type() == elfcpp::SHT_NOBITS
)
873 == (*p
)->has_any_data_sections())
877 (*p
)->add_output_section(os
, seg_flags
);
882 if (p
== this->segment_list_
.end())
884 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
886 oseg
->add_output_section(os
, seg_flags
);
889 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
891 if (os
->type() == elfcpp::SHT_NOTE
)
893 // See if we already have an equivalent PT_NOTE segment.
894 for (p
= this->segment_list_
.begin();
895 p
!= segment_list_
.end();
898 if ((*p
)->type() == elfcpp::PT_NOTE
899 && (((*p
)->flags() & elfcpp::PF_W
)
900 == (seg_flags
& elfcpp::PF_W
)))
902 (*p
)->add_output_section(os
, seg_flags
);
907 if (p
== this->segment_list_
.end())
909 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
911 oseg
->add_output_section(os
, seg_flags
);
915 // If we see a loadable SHF_TLS section, we create a PT_TLS
916 // segment. There can only be one such segment.
917 if ((flags
& elfcpp::SHF_TLS
) != 0)
919 if (this->tls_segment_
== NULL
)
920 this->tls_segment_
= this->make_output_segment(elfcpp::PT_TLS
,
922 this->tls_segment_
->add_output_section(os
, seg_flags
);
925 // If -z relro is in effect, and we see a relro section, we create a
926 // PT_GNU_RELRO segment. There can only be one such segment.
927 if (os
->is_relro() && parameters
->options().relro())
929 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
930 if (this->relro_segment_
== NULL
)
931 this->relro_segment_
= this->make_output_segment(elfcpp::PT_GNU_RELRO
,
933 this->relro_segment_
->add_output_section(os
, seg_flags
);
937 // Make an output section for a script.
940 Layout::make_output_section_for_script(const char* name
)
942 name
= this->namepool_
.add(name
, false, NULL
);
943 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
945 os
->set_found_in_sections_clause();
949 // Return the number of segments we expect to see.
952 Layout::expected_segment_count() const
954 size_t ret
= this->segment_list_
.size();
956 // If we didn't see a SECTIONS clause in a linker script, we should
957 // already have the complete list of segments. Otherwise we ask the
958 // SECTIONS clause how many segments it expects, and add in the ones
959 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
961 if (!this->script_options_
->saw_sections_clause())
965 const Script_sections
* ss
= this->script_options_
->script_sections();
966 return ret
+ ss
->expected_segment_count(this);
970 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
971 // is whether we saw a .note.GNU-stack section in the object file.
972 // GNU_STACK_FLAGS is the section flags. The flags give the
973 // protection required for stack memory. We record this in an
974 // executable as a PT_GNU_STACK segment. If an object file does not
975 // have a .note.GNU-stack segment, we must assume that it is an old
976 // object. On some targets that will force an executable stack.
979 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
982 this->input_without_gnu_stack_note_
= true;
985 this->input_with_gnu_stack_note_
= true;
986 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
987 this->input_requires_executable_stack_
= true;
991 // Create the dynamic sections which are needed before we read the
995 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
997 if (parameters
->doing_static_link())
1000 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1001 elfcpp::SHT_DYNAMIC
,
1003 | elfcpp::SHF_WRITE
),
1005 this->dynamic_section_
->set_is_relro();
1007 symtab
->define_in_output_data("_DYNAMIC", NULL
, this->dynamic_section_
, 0, 0,
1008 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1009 elfcpp::STV_HIDDEN
, 0, false, false);
1011 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1013 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1016 // For each output section whose name can be represented as C symbol,
1017 // define __start and __stop symbols for the section. This is a GNU
1021 Layout::define_section_symbols(Symbol_table
* symtab
)
1023 for (Section_list::const_iterator p
= this->section_list_
.begin();
1024 p
!= this->section_list_
.end();
1027 const char* const name
= (*p
)->name();
1028 if (name
[strspn(name
,
1030 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
1031 "abcdefghijklmnopqrstuvwxyz"
1035 const std::string
name_string(name
);
1036 const std::string
start_name("__start_" + name_string
);
1037 const std::string
stop_name("__stop_" + name_string
);
1039 symtab
->define_in_output_data(start_name
.c_str(),
1046 elfcpp::STV_DEFAULT
,
1048 false, // offset_is_from_end
1049 true); // only_if_ref
1051 symtab
->define_in_output_data(stop_name
.c_str(),
1058 elfcpp::STV_DEFAULT
,
1060 true, // offset_is_from_end
1061 true); // only_if_ref
1066 // Define symbols for group signatures.
1069 Layout::define_group_signatures(Symbol_table
* symtab
)
1071 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1072 p
!= this->group_signatures_
.end();
1075 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1077 p
->section
->set_info_symndx(sym
);
1080 // Force the name of the group section to the group
1081 // signature, and use the group's section symbol as the
1082 // signature symbol.
1083 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1085 const char* name
= this->namepool_
.add(p
->signature
,
1087 p
->section
->set_name(name
);
1089 p
->section
->set_needs_symtab_index();
1090 p
->section
->set_info_section_symndx(p
->section
);
1094 this->group_signatures_
.clear();
1097 // Find the first read-only PT_LOAD segment, creating one if
1101 Layout::find_first_load_seg()
1103 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1104 p
!= this->segment_list_
.end();
1107 if ((*p
)->type() == elfcpp::PT_LOAD
1108 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1109 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
1113 gold_assert(!this->script_options_
->saw_phdrs_clause());
1115 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1120 // Finalize the layout. When this is called, we have created all the
1121 // output sections and all the output segments which are based on
1122 // input sections. We have several things to do, and we have to do
1123 // them in the right order, so that we get the right results correctly
1126 // 1) Finalize the list of output segments and create the segment
1129 // 2) Finalize the dynamic symbol table and associated sections.
1131 // 3) Determine the final file offset of all the output segments.
1133 // 4) Determine the final file offset of all the SHF_ALLOC output
1136 // 5) Create the symbol table sections and the section name table
1139 // 6) Finalize the symbol table: set symbol values to their final
1140 // value and make a final determination of which symbols are going
1141 // into the output symbol table.
1143 // 7) Create the section table header.
1145 // 8) Determine the final file offset of all the output sections which
1146 // are not SHF_ALLOC, including the section table header.
1148 // 9) Finalize the ELF file header.
1150 // This function returns the size of the output file.
1153 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1154 Target
* target
, const Task
* task
)
1156 target
->finalize_sections(this);
1158 this->count_local_symbols(task
, input_objects
);
1160 this->create_gold_note();
1161 this->create_executable_stack_info(target
);
1162 this->create_build_id();
1164 Output_segment
* phdr_seg
= NULL
;
1165 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1167 // There was a dynamic object in the link. We need to create
1168 // some information for the dynamic linker.
1170 // Create the PT_PHDR segment which will hold the program
1172 if (!this->script_options_
->saw_phdrs_clause())
1173 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1175 // Create the dynamic symbol table, including the hash table.
1176 Output_section
* dynstr
;
1177 std::vector
<Symbol
*> dynamic_symbols
;
1178 unsigned int local_dynamic_count
;
1179 Versions
versions(*this->script_options()->version_script_info(),
1181 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1182 &local_dynamic_count
, &dynamic_symbols
,
1185 // Create the .interp section to hold the name of the
1186 // interpreter, and put it in a PT_INTERP segment.
1187 if (!parameters
->options().shared())
1188 this->create_interp(target
);
1190 // Finish the .dynamic section to hold the dynamic data, and put
1191 // it in a PT_DYNAMIC segment.
1192 this->finish_dynamic_section(input_objects
, symtab
);
1194 // We should have added everything we need to the dynamic string
1196 this->dynpool_
.set_string_offsets();
1198 // Create the version sections. We can't do this until the
1199 // dynamic string table is complete.
1200 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1201 dynamic_symbols
, dynstr
);
1204 // If there is a SECTIONS clause, put all the input sections into
1205 // the required order.
1206 Output_segment
* load_seg
;
1207 if (this->script_options_
->saw_sections_clause())
1208 load_seg
= this->set_section_addresses_from_script(symtab
);
1209 else if (parameters
->options().relocatable())
1212 load_seg
= this->find_first_load_seg();
1214 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
1217 gold_assert(phdr_seg
== NULL
|| load_seg
!= NULL
);
1219 // Lay out the segment headers.
1220 Output_segment_headers
* segment_headers
;
1221 if (parameters
->options().relocatable())
1222 segment_headers
= NULL
;
1225 segment_headers
= new Output_segment_headers(this->segment_list_
);
1226 if (load_seg
!= NULL
)
1227 load_seg
->add_initial_output_data(segment_headers
);
1228 if (phdr_seg
!= NULL
)
1229 phdr_seg
->add_initial_output_data(segment_headers
);
1232 // Lay out the file header.
1233 Output_file_header
* file_header
;
1234 file_header
= new Output_file_header(target
, symtab
, segment_headers
,
1235 this->options_
.entry());
1236 if (load_seg
!= NULL
)
1237 load_seg
->add_initial_output_data(file_header
);
1239 this->special_output_list_
.push_back(file_header
);
1240 if (segment_headers
!= NULL
)
1241 this->special_output_list_
.push_back(segment_headers
);
1243 if (this->script_options_
->saw_phdrs_clause()
1244 && !parameters
->options().relocatable())
1246 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1247 // clause in a linker script.
1248 Script_sections
* ss
= this->script_options_
->script_sections();
1249 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1252 // We set the output section indexes in set_segment_offsets and
1253 // set_section_indexes.
1254 unsigned int shndx
= 1;
1256 // Set the file offsets of all the segments, and all the sections
1259 if (!parameters
->options().relocatable())
1260 off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
1262 off
= this->set_relocatable_section_offsets(file_header
, &shndx
);
1264 // Set the file offsets of all the non-data sections we've seen so
1265 // far which don't have to wait for the input sections. We need
1266 // this in order to finalize local symbols in non-allocated
1268 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1270 // Set the section indexes of all unallocated sections seen so far,
1271 // in case any of them are somehow referenced by a symbol.
1272 shndx
= this->set_section_indexes(shndx
);
1274 // Create the symbol table sections.
1275 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1276 if (!parameters
->doing_static_link())
1277 this->assign_local_dynsym_offsets(input_objects
);
1279 // Process any symbol assignments from a linker script. This must
1280 // be called after the symbol table has been finalized.
1281 this->script_options_
->finalize_symbols(symtab
, this);
1283 // Create the .shstrtab section.
1284 Output_section
* shstrtab_section
= this->create_shstrtab();
1286 // Set the file offsets of the rest of the non-data sections which
1287 // don't have to wait for the input sections.
1288 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1290 // Now that all sections have been created, set the section indexes
1291 // for any sections which haven't been done yet.
1292 shndx
= this->set_section_indexes(shndx
);
1294 // Create the section table header.
1295 this->create_shdrs(shstrtab_section
, &off
);
1297 // If there are no sections which require postprocessing, we can
1298 // handle the section names now, and avoid a resize later.
1299 if (!this->any_postprocessing_sections_
)
1300 off
= this->set_section_offsets(off
,
1301 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1303 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1305 // Now we know exactly where everything goes in the output file
1306 // (except for non-allocated sections which require postprocessing).
1307 Output_data::layout_complete();
1309 this->output_file_size_
= off
;
1314 // Create a note header following the format defined in the ELF ABI.
1315 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1316 // descriptor. ALLOCATE is true if the section should be allocated in
1317 // memory. This returns the new note section. It sets
1318 // *TRAILING_PADDING to the number of trailing zero bytes required.
1321 Layout::create_note(const char* name
, int note_type
, size_t descsz
,
1322 bool allocate
, size_t* trailing_padding
)
1324 // Authorities all agree that the values in a .note field should
1325 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1326 // they differ on what the alignment is for 64-bit binaries.
1327 // The GABI says unambiguously they take 8-byte alignment:
1328 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1329 // Other documentation says alignment should always be 4 bytes:
1330 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1331 // GNU ld and GNU readelf both support the latter (at least as of
1332 // version 2.16.91), and glibc always generates the latter for
1333 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1335 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1336 const int size
= parameters
->target().get_size();
1338 const int size
= 32;
1341 // The contents of the .note section.
1342 size_t namesz
= strlen(name
) + 1;
1343 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1344 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1346 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1348 unsigned char* buffer
= new unsigned char[notehdrsz
];
1349 memset(buffer
, 0, notehdrsz
);
1351 bool is_big_endian
= parameters
->target().is_big_endian();
1357 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1358 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1359 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1363 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1364 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1365 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1368 else if (size
== 64)
1372 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1373 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1374 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1378 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1379 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1380 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1386 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1388 const char* note_name
= this->namepool_
.add(".note", false, NULL
);
1389 elfcpp::Elf_Xword flags
= 0;
1391 flags
= elfcpp::SHF_ALLOC
;
1392 Output_section
* os
= this->make_output_section(note_name
,
1395 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1398 os
->add_output_section_data(posd
);
1400 *trailing_padding
= aligned_descsz
- descsz
;
1405 // For an executable or shared library, create a note to record the
1406 // version of gold used to create the binary.
1409 Layout::create_gold_note()
1411 if (parameters
->options().relocatable())
1414 std::string desc
= std::string("gold ") + gold::get_version_string();
1416 size_t trailing_padding
;
1417 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1418 desc
.size(), false, &trailing_padding
);
1420 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1421 os
->add_output_section_data(posd
);
1423 if (trailing_padding
> 0)
1425 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1426 os
->add_output_section_data(posd
);
1430 // Record whether the stack should be executable. This can be set
1431 // from the command line using the -z execstack or -z noexecstack
1432 // options. Otherwise, if any input file has a .note.GNU-stack
1433 // section with the SHF_EXECINSTR flag set, the stack should be
1434 // executable. Otherwise, if at least one input file a
1435 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1436 // section, we use the target default for whether the stack should be
1437 // executable. Otherwise, we don't generate a stack note. When
1438 // generating a object file, we create a .note.GNU-stack section with
1439 // the appropriate marking. When generating an executable or shared
1440 // library, we create a PT_GNU_STACK segment.
1443 Layout::create_executable_stack_info(const Target
* target
)
1445 bool is_stack_executable
;
1446 if (this->options_
.is_execstack_set())
1447 is_stack_executable
= this->options_
.is_stack_executable();
1448 else if (!this->input_with_gnu_stack_note_
)
1452 if (this->input_requires_executable_stack_
)
1453 is_stack_executable
= true;
1454 else if (this->input_without_gnu_stack_note_
)
1455 is_stack_executable
= target
->is_default_stack_executable();
1457 is_stack_executable
= false;
1460 if (parameters
->options().relocatable())
1462 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1463 elfcpp::Elf_Xword flags
= 0;
1464 if (is_stack_executable
)
1465 flags
|= elfcpp::SHF_EXECINSTR
;
1466 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
);
1470 if (this->script_options_
->saw_phdrs_clause())
1472 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1473 if (is_stack_executable
)
1474 flags
|= elfcpp::PF_X
;
1475 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1479 // If --build-id was used, set up the build ID note.
1482 Layout::create_build_id()
1484 if (!parameters
->options().user_set_build_id())
1487 const char* style
= parameters
->options().build_id();
1488 if (strcmp(style
, "none") == 0)
1491 // Set DESCSZ to the size of the note descriptor. When possible,
1492 // set DESC to the note descriptor contents.
1495 if (strcmp(style
, "md5") == 0)
1497 else if (strcmp(style
, "sha1") == 0)
1499 else if (strcmp(style
, "uuid") == 0)
1501 const size_t uuidsz
= 128 / 8;
1503 char buffer
[uuidsz
];
1504 memset(buffer
, 0, uuidsz
);
1506 int descriptor
= ::open("/dev/urandom", O_RDONLY
);
1508 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1512 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1513 ::close(descriptor
);
1515 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1516 else if (static_cast<size_t>(got
) != uuidsz
)
1517 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1521 desc
.assign(buffer
, uuidsz
);
1524 else if (strncmp(style
, "0x", 2) == 0)
1527 const char* p
= style
+ 2;
1530 if (hex_p(p
[0]) && hex_p(p
[1]))
1532 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1536 else if (*p
== '-' || *p
== ':')
1539 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1542 descsz
= desc
.size();
1545 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1548 size_t trailing_padding
;
1549 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1550 descsz
, true, &trailing_padding
);
1554 // We know the value already, so we fill it in now.
1555 gold_assert(desc
.size() == descsz
);
1557 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1558 os
->add_output_section_data(posd
);
1560 if (trailing_padding
!= 0)
1562 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1563 os
->add_output_section_data(posd
);
1568 // We need to compute a checksum after we have completed the
1570 gold_assert(trailing_padding
== 0);
1571 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
1572 os
->add_output_section_data(this->build_id_note_
);
1573 os
->set_after_input_sections();
1577 // Return whether SEG1 should be before SEG2 in the output file. This
1578 // is based entirely on the segment type and flags. When this is
1579 // called the segment addresses has normally not yet been set.
1582 Layout::segment_precedes(const Output_segment
* seg1
,
1583 const Output_segment
* seg2
)
1585 elfcpp::Elf_Word type1
= seg1
->type();
1586 elfcpp::Elf_Word type2
= seg2
->type();
1588 // The single PT_PHDR segment is required to precede any loadable
1589 // segment. We simply make it always first.
1590 if (type1
== elfcpp::PT_PHDR
)
1592 gold_assert(type2
!= elfcpp::PT_PHDR
);
1595 if (type2
== elfcpp::PT_PHDR
)
1598 // The single PT_INTERP segment is required to precede any loadable
1599 // segment. We simply make it always second.
1600 if (type1
== elfcpp::PT_INTERP
)
1602 gold_assert(type2
!= elfcpp::PT_INTERP
);
1605 if (type2
== elfcpp::PT_INTERP
)
1608 // We then put PT_LOAD segments before any other segments.
1609 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
1611 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
1614 // We put the PT_TLS segment last except for the PT_GNU_RELRO
1615 // segment, because that is where the dynamic linker expects to find
1616 // it (this is just for efficiency; other positions would also work
1618 if (type1
== elfcpp::PT_TLS
1619 && type2
!= elfcpp::PT_TLS
1620 && type2
!= elfcpp::PT_GNU_RELRO
)
1622 if (type2
== elfcpp::PT_TLS
1623 && type1
!= elfcpp::PT_TLS
1624 && type1
!= elfcpp::PT_GNU_RELRO
)
1627 // We put the PT_GNU_RELRO segment last, because that is where the
1628 // dynamic linker expects to find it (as with PT_TLS, this is just
1630 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
1632 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
1635 const elfcpp::Elf_Word flags1
= seg1
->flags();
1636 const elfcpp::Elf_Word flags2
= seg2
->flags();
1638 // The order of non-PT_LOAD segments is unimportant. We simply sort
1639 // by the numeric segment type and flags values. There should not
1640 // be more than one segment with the same type and flags.
1641 if (type1
!= elfcpp::PT_LOAD
)
1644 return type1
< type2
;
1645 gold_assert(flags1
!= flags2
);
1646 return flags1
< flags2
;
1649 // If the addresses are set already, sort by load address.
1650 if (seg1
->are_addresses_set())
1652 if (!seg2
->are_addresses_set())
1655 unsigned int section_count1
= seg1
->output_section_count();
1656 unsigned int section_count2
= seg2
->output_section_count();
1657 if (section_count1
== 0 && section_count2
> 0)
1659 if (section_count1
> 0 && section_count2
== 0)
1662 uint64_t paddr1
= seg1
->first_section_load_address();
1663 uint64_t paddr2
= seg2
->first_section_load_address();
1664 if (paddr1
!= paddr2
)
1665 return paddr1
< paddr2
;
1667 else if (seg2
->are_addresses_set())
1670 // We sort PT_LOAD segments based on the flags. Readonly segments
1671 // come before writable segments. Then writable segments with data
1672 // come before writable segments without data. Then executable
1673 // segments come before non-executable segments. Then the unlikely
1674 // case of a non-readable segment comes before the normal case of a
1675 // readable segment. If there are multiple segments with the same
1676 // type and flags, we require that the address be set, and we sort
1677 // by virtual address and then physical address.
1678 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
1679 return (flags1
& elfcpp::PF_W
) == 0;
1680 if ((flags1
& elfcpp::PF_W
) != 0
1681 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
1682 return seg1
->has_any_data_sections();
1683 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
1684 return (flags1
& elfcpp::PF_X
) != 0;
1685 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
1686 return (flags1
& elfcpp::PF_R
) == 0;
1688 // We shouldn't get here--we shouldn't create segments which we
1689 // can't distinguish.
1693 // Set the file offsets of all the segments, and all the sections they
1694 // contain. They have all been created. LOAD_SEG must be be laid out
1695 // first. Return the offset of the data to follow.
1698 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
1699 unsigned int *pshndx
)
1701 // Sort them into the final order.
1702 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
1703 Layout::Compare_segments());
1705 // Find the PT_LOAD segments, and set their addresses and offsets
1706 // and their section's addresses and offsets.
1708 if (this->options_
.user_set_Ttext())
1709 addr
= this->options_
.Ttext();
1710 else if (parameters
->options().shared())
1713 addr
= target
->default_text_segment_address();
1716 // If LOAD_SEG is NULL, then the file header and segment headers
1717 // will not be loadable. But they still need to be at offset 0 in
1718 // the file. Set their offsets now.
1719 if (load_seg
== NULL
)
1721 for (Data_list::iterator p
= this->special_output_list_
.begin();
1722 p
!= this->special_output_list_
.end();
1725 off
= align_address(off
, (*p
)->addralign());
1726 (*p
)->set_address_and_file_offset(0, off
);
1727 off
+= (*p
)->data_size();
1731 bool was_readonly
= false;
1732 for (Segment_list::iterator p
= this->segment_list_
.begin();
1733 p
!= this->segment_list_
.end();
1736 if ((*p
)->type() == elfcpp::PT_LOAD
)
1738 if (load_seg
!= NULL
&& load_seg
!= *p
)
1742 bool are_addresses_set
= (*p
)->are_addresses_set();
1743 if (are_addresses_set
)
1745 // When it comes to setting file offsets, we care about
1746 // the physical address.
1747 addr
= (*p
)->paddr();
1749 else if (this->options_
.user_set_Tdata()
1750 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1751 && (!this->options_
.user_set_Tbss()
1752 || (*p
)->has_any_data_sections()))
1754 addr
= this->options_
.Tdata();
1755 are_addresses_set
= true;
1757 else if (this->options_
.user_set_Tbss()
1758 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1759 && !(*p
)->has_any_data_sections())
1761 addr
= this->options_
.Tbss();
1762 are_addresses_set
= true;
1765 uint64_t orig_addr
= addr
;
1766 uint64_t orig_off
= off
;
1768 uint64_t aligned_addr
= 0;
1769 uint64_t abi_pagesize
= target
->abi_pagesize();
1771 // FIXME: This should depend on the -n and -N options.
1772 (*p
)->set_minimum_p_align(target
->common_pagesize());
1774 if (are_addresses_set
)
1776 // Adjust the file offset to the same address modulo the
1778 uint64_t unsigned_off
= off
;
1779 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
1780 | (addr
& (abi_pagesize
- 1)));
1781 if (aligned_off
< unsigned_off
)
1782 aligned_off
+= abi_pagesize
;
1787 // If the last segment was readonly, and this one is
1788 // not, then skip the address forward one page,
1789 // maintaining the same position within the page. This
1790 // lets us store both segments overlapping on a single
1791 // page in the file, but the loader will put them on
1792 // different pages in memory.
1794 addr
= align_address(addr
, (*p
)->maximum_alignment());
1795 aligned_addr
= addr
;
1797 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
1799 if ((addr
& (abi_pagesize
- 1)) != 0)
1800 addr
= addr
+ abi_pagesize
;
1803 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1806 unsigned int shndx_hold
= *pshndx
;
1807 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
1810 // Now that we know the size of this segment, we may be able
1811 // to save a page in memory, at the cost of wasting some
1812 // file space, by instead aligning to the start of a new
1813 // page. Here we use the real machine page size rather than
1814 // the ABI mandated page size.
1816 if (!are_addresses_set
&& aligned_addr
!= addr
)
1818 uint64_t common_pagesize
= target
->common_pagesize();
1819 uint64_t first_off
= (common_pagesize
1821 & (common_pagesize
- 1)));
1822 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
1825 && ((aligned_addr
& ~ (common_pagesize
- 1))
1826 != (new_addr
& ~ (common_pagesize
- 1)))
1827 && first_off
+ last_off
<= common_pagesize
)
1829 *pshndx
= shndx_hold
;
1830 addr
= align_address(aligned_addr
, common_pagesize
);
1831 addr
= align_address(addr
, (*p
)->maximum_alignment());
1832 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1833 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
1840 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
1841 was_readonly
= true;
1845 // Handle the non-PT_LOAD segments, setting their offsets from their
1846 // section's offsets.
1847 for (Segment_list::iterator p
= this->segment_list_
.begin();
1848 p
!= this->segment_list_
.end();
1851 if ((*p
)->type() != elfcpp::PT_LOAD
)
1855 // Set the TLS offsets for each section in the PT_TLS segment.
1856 if (this->tls_segment_
!= NULL
)
1857 this->tls_segment_
->set_tls_offsets();
1862 // Set the offsets of all the allocated sections when doing a
1863 // relocatable link. This does the same jobs as set_segment_offsets,
1864 // only for a relocatable link.
1867 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
1868 unsigned int *pshndx
)
1872 file_header
->set_address_and_file_offset(0, 0);
1873 off
+= file_header
->data_size();
1875 for (Section_list::iterator p
= this->section_list_
.begin();
1876 p
!= this->section_list_
.end();
1879 // We skip unallocated sections here, except that group sections
1880 // have to come first.
1881 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
1882 && (*p
)->type() != elfcpp::SHT_GROUP
)
1885 off
= align_address(off
, (*p
)->addralign());
1887 // The linker script might have set the address.
1888 if (!(*p
)->is_address_valid())
1889 (*p
)->set_address(0);
1890 (*p
)->set_file_offset(off
);
1891 (*p
)->finalize_data_size();
1892 off
+= (*p
)->data_size();
1894 (*p
)->set_out_shndx(*pshndx
);
1901 // Set the file offset of all the sections not associated with a
1905 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
1907 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1908 p
!= this->unattached_section_list_
.end();
1911 // The symtab section is handled in create_symtab_sections.
1912 if (*p
== this->symtab_section_
)
1915 // If we've already set the data size, don't set it again.
1916 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
1919 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1920 && (*p
)->requires_postprocessing())
1922 (*p
)->create_postprocessing_buffer();
1923 this->any_postprocessing_sections_
= true;
1926 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1927 && (*p
)->after_input_sections())
1929 else if (pass
== POSTPROCESSING_SECTIONS_PASS
1930 && (!(*p
)->after_input_sections()
1931 || (*p
)->type() == elfcpp::SHT_STRTAB
))
1933 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1934 && (!(*p
)->after_input_sections()
1935 || (*p
)->type() != elfcpp::SHT_STRTAB
))
1938 off
= align_address(off
, (*p
)->addralign());
1939 (*p
)->set_file_offset(off
);
1940 (*p
)->finalize_data_size();
1941 off
+= (*p
)->data_size();
1943 // At this point the name must be set.
1944 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
1945 this->namepool_
.add((*p
)->name(), false, NULL
);
1950 // Set the section indexes of all the sections not associated with a
1954 Layout::set_section_indexes(unsigned int shndx
)
1956 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1957 p
!= this->unattached_section_list_
.end();
1960 if (!(*p
)->has_out_shndx())
1962 (*p
)->set_out_shndx(shndx
);
1969 // Set the section addresses according to the linker script. This is
1970 // only called when we see a SECTIONS clause. This returns the
1971 // program segment which should hold the file header and segment
1972 // headers, if any. It will return NULL if they should not be in a
1976 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
1978 Script_sections
* ss
= this->script_options_
->script_sections();
1979 gold_assert(ss
->saw_sections_clause());
1981 // Place each orphaned output section in the script.
1982 for (Section_list::iterator p
= this->section_list_
.begin();
1983 p
!= this->section_list_
.end();
1986 if (!(*p
)->found_in_sections_clause())
1987 ss
->place_orphan(*p
);
1990 return this->script_options_
->set_section_addresses(symtab
, this);
1993 // Count the local symbols in the regular symbol table and the dynamic
1994 // symbol table, and build the respective string pools.
1997 Layout::count_local_symbols(const Task
* task
,
1998 const Input_objects
* input_objects
)
2000 // First, figure out an upper bound on the number of symbols we'll
2001 // be inserting into each pool. This helps us create the pools with
2002 // the right size, to avoid unnecessary hashtable resizing.
2003 unsigned int symbol_count
= 0;
2004 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2005 p
!= input_objects
->relobj_end();
2007 symbol_count
+= (*p
)->local_symbol_count();
2009 // Go from "upper bound" to "estimate." We overcount for two
2010 // reasons: we double-count symbols that occur in more than one
2011 // object file, and we count symbols that are dropped from the
2012 // output. Add it all together and assume we overcount by 100%.
2015 // We assume all symbols will go into both the sympool and dynpool.
2016 this->sympool_
.reserve(symbol_count
);
2017 this->dynpool_
.reserve(symbol_count
);
2019 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2020 p
!= input_objects
->relobj_end();
2023 Task_lock_obj
<Object
> tlo(task
, *p
);
2024 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2028 // Create the symbol table sections. Here we also set the final
2029 // values of the symbols. At this point all the loadable sections are
2030 // fully laid out. SHNUM is the number of sections so far.
2033 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2034 Symbol_table
* symtab
,
2040 if (parameters
->target().get_size() == 32)
2042 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2045 else if (parameters
->target().get_size() == 64)
2047 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2054 off
= align_address(off
, align
);
2055 off_t startoff
= off
;
2057 // Save space for the dummy symbol at the start of the section. We
2058 // never bother to write this out--it will just be left as zero.
2060 unsigned int local_symbol_index
= 1;
2062 // Add STT_SECTION symbols for each Output section which needs one.
2063 for (Section_list::iterator p
= this->section_list_
.begin();
2064 p
!= this->section_list_
.end();
2067 if (!(*p
)->needs_symtab_index())
2068 (*p
)->set_symtab_index(-1U);
2071 (*p
)->set_symtab_index(local_symbol_index
);
2072 ++local_symbol_index
;
2077 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2078 p
!= input_objects
->relobj_end();
2081 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2083 off
+= (index
- local_symbol_index
) * symsize
;
2084 local_symbol_index
= index
;
2087 unsigned int local_symcount
= local_symbol_index
;
2088 gold_assert(local_symcount
* symsize
== off
- startoff
);
2091 size_t dyn_global_index
;
2093 if (this->dynsym_section_
== NULL
)
2096 dyn_global_index
= 0;
2101 dyn_global_index
= this->dynsym_section_
->info();
2102 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2103 dynoff
= this->dynsym_section_
->offset() + locsize
;
2104 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2105 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2106 == this->dynsym_section_
->data_size() - locsize
);
2109 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2110 &this->sympool_
, &local_symcount
);
2112 if (!parameters
->options().strip_all())
2114 this->sympool_
.set_string_offsets();
2116 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2117 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2120 this->symtab_section_
= osymtab
;
2122 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2125 osymtab
->add_output_section_data(pos
);
2127 // We generate a .symtab_shndx section if we have more than
2128 // SHN_LORESERVE sections. Technically it is possible that we
2129 // don't need one, because it is possible that there are no
2130 // symbols in any of sections with indexes larger than
2131 // SHN_LORESERVE. That is probably unusual, though, and it is
2132 // easier to always create one than to compute section indexes
2133 // twice (once here, once when writing out the symbols).
2134 if (shnum
>= elfcpp::SHN_LORESERVE
)
2136 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2138 Output_section
* osymtab_xindex
=
2139 this->make_output_section(symtab_xindex_name
,
2140 elfcpp::SHT_SYMTAB_SHNDX
, 0);
2142 size_t symcount
= (off
- startoff
) / symsize
;
2143 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2145 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2147 osymtab_xindex
->set_link_section(osymtab
);
2148 osymtab_xindex
->set_addralign(4);
2149 osymtab_xindex
->set_entsize(4);
2151 osymtab_xindex
->set_after_input_sections();
2153 // This tells the driver code to wait until the symbol table
2154 // has written out before writing out the postprocessing
2155 // sections, including the .symtab_shndx section.
2156 this->any_postprocessing_sections_
= true;
2159 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2160 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2164 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2165 ostrtab
->add_output_section_data(pstr
);
2167 osymtab
->set_file_offset(startoff
);
2168 osymtab
->finalize_data_size();
2169 osymtab
->set_link_section(ostrtab
);
2170 osymtab
->set_info(local_symcount
);
2171 osymtab
->set_entsize(symsize
);
2177 // Create the .shstrtab section, which holds the names of the
2178 // sections. At the time this is called, we have created all the
2179 // output sections except .shstrtab itself.
2182 Layout::create_shstrtab()
2184 // FIXME: We don't need to create a .shstrtab section if we are
2185 // stripping everything.
2187 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2189 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
2191 // We can't write out this section until we've set all the section
2192 // names, and we don't set the names of compressed output sections
2193 // until relocations are complete.
2194 os
->set_after_input_sections();
2196 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2197 os
->add_output_section_data(posd
);
2202 // Create the section headers. SIZE is 32 or 64. OFF is the file
2206 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2208 Output_section_headers
* oshdrs
;
2209 oshdrs
= new Output_section_headers(this,
2210 &this->segment_list_
,
2211 &this->section_list_
,
2212 &this->unattached_section_list_
,
2215 off_t off
= align_address(*poff
, oshdrs
->addralign());
2216 oshdrs
->set_address_and_file_offset(0, off
);
2217 off
+= oshdrs
->data_size();
2219 this->section_headers_
= oshdrs
;
2222 // Count the allocated sections.
2225 Layout::allocated_output_section_count() const
2227 size_t section_count
= 0;
2228 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2229 p
!= this->segment_list_
.end();
2231 section_count
+= (*p
)->output_section_count();
2232 return section_count
;
2235 // Create the dynamic symbol table.
2238 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2239 Symbol_table
* symtab
,
2240 Output_section
**pdynstr
,
2241 unsigned int* plocal_dynamic_count
,
2242 std::vector
<Symbol
*>* pdynamic_symbols
,
2243 Versions
* pversions
)
2245 // Count all the symbols in the dynamic symbol table, and set the
2246 // dynamic symbol indexes.
2248 // Skip symbol 0, which is always all zeroes.
2249 unsigned int index
= 1;
2251 // Add STT_SECTION symbols for each Output section which needs one.
2252 for (Section_list::iterator p
= this->section_list_
.begin();
2253 p
!= this->section_list_
.end();
2256 if (!(*p
)->needs_dynsym_index())
2257 (*p
)->set_dynsym_index(-1U);
2260 (*p
)->set_dynsym_index(index
);
2265 // Count the local symbols that need to go in the dynamic symbol table,
2266 // and set the dynamic symbol indexes.
2267 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2268 p
!= input_objects
->relobj_end();
2271 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2275 unsigned int local_symcount
= index
;
2276 *plocal_dynamic_count
= local_symcount
;
2278 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2279 &this->dynpool_
, pversions
);
2283 const int size
= parameters
->target().get_size();
2286 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2289 else if (size
== 64)
2291 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2297 // Create the dynamic symbol table section.
2299 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2304 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2307 dynsym
->add_output_section_data(odata
);
2309 dynsym
->set_info(local_symcount
);
2310 dynsym
->set_entsize(symsize
);
2311 dynsym
->set_addralign(align
);
2313 this->dynsym_section_
= dynsym
;
2315 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2316 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2317 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2319 // If there are more than SHN_LORESERVE allocated sections, we
2320 // create a .dynsym_shndx section. It is possible that we don't
2321 // need one, because it is possible that there are no dynamic
2322 // symbols in any of the sections with indexes larger than
2323 // SHN_LORESERVE. This is probably unusual, though, and at this
2324 // time we don't know the actual section indexes so it is
2325 // inconvenient to check.
2326 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2328 Output_section
* dynsym_xindex
=
2329 this->choose_output_section(NULL
, ".dynsym_shndx",
2330 elfcpp::SHT_SYMTAB_SHNDX
,
2334 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2336 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2338 dynsym_xindex
->set_link_section(dynsym
);
2339 dynsym_xindex
->set_addralign(4);
2340 dynsym_xindex
->set_entsize(4);
2342 dynsym_xindex
->set_after_input_sections();
2344 // This tells the driver code to wait until the symbol table has
2345 // written out before writing out the postprocessing sections,
2346 // including the .dynsym_shndx section.
2347 this->any_postprocessing_sections_
= true;
2350 // Create the dynamic string table section.
2352 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2357 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2358 dynstr
->add_output_section_data(strdata
);
2360 dynsym
->set_link_section(dynstr
);
2361 this->dynamic_section_
->set_link_section(dynstr
);
2363 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2364 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2368 // Create the hash tables.
2370 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2371 || strcmp(parameters
->options().hash_style(), "both") == 0)
2373 unsigned char* phash
;
2374 unsigned int hashlen
;
2375 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2378 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2383 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2387 hashsec
->add_output_section_data(hashdata
);
2389 hashsec
->set_link_section(dynsym
);
2390 hashsec
->set_entsize(4);
2392 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2395 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2396 || strcmp(parameters
->options().hash_style(), "both") == 0)
2398 unsigned char* phash
;
2399 unsigned int hashlen
;
2400 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2403 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2404 elfcpp::SHT_GNU_HASH
,
2408 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2412 hashsec
->add_output_section_data(hashdata
);
2414 hashsec
->set_link_section(dynsym
);
2415 hashsec
->set_entsize(4);
2417 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
2421 // Assign offsets to each local portion of the dynamic symbol table.
2424 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
2426 Output_section
* dynsym
= this->dynsym_section_
;
2427 gold_assert(dynsym
!= NULL
);
2429 off_t off
= dynsym
->offset();
2431 // Skip the dummy symbol at the start of the section.
2432 off
+= dynsym
->entsize();
2434 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2435 p
!= input_objects
->relobj_end();
2438 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
2439 off
+= count
* dynsym
->entsize();
2443 // Create the version sections.
2446 Layout::create_version_sections(const Versions
* versions
,
2447 const Symbol_table
* symtab
,
2448 unsigned int local_symcount
,
2449 const std::vector
<Symbol
*>& dynamic_symbols
,
2450 const Output_section
* dynstr
)
2452 if (!versions
->any_defs() && !versions
->any_needs())
2455 switch (parameters
->size_and_endianness())
2457 #ifdef HAVE_TARGET_32_LITTLE
2458 case Parameters::TARGET_32_LITTLE
:
2459 this->sized_create_version_sections
<32, false>(versions
, symtab
,
2461 dynamic_symbols
, dynstr
);
2464 #ifdef HAVE_TARGET_32_BIG
2465 case Parameters::TARGET_32_BIG
:
2466 this->sized_create_version_sections
<32, true>(versions
, symtab
,
2468 dynamic_symbols
, dynstr
);
2471 #ifdef HAVE_TARGET_64_LITTLE
2472 case Parameters::TARGET_64_LITTLE
:
2473 this->sized_create_version_sections
<64, false>(versions
, symtab
,
2475 dynamic_symbols
, dynstr
);
2478 #ifdef HAVE_TARGET_64_BIG
2479 case Parameters::TARGET_64_BIG
:
2480 this->sized_create_version_sections
<64, true>(versions
, symtab
,
2482 dynamic_symbols
, dynstr
);
2490 // Create the version sections, sized version.
2492 template<int size
, bool big_endian
>
2494 Layout::sized_create_version_sections(
2495 const Versions
* versions
,
2496 const Symbol_table
* symtab
,
2497 unsigned int local_symcount
,
2498 const std::vector
<Symbol
*>& dynamic_symbols
,
2499 const Output_section
* dynstr
)
2501 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
2502 elfcpp::SHT_GNU_versym
,
2506 unsigned char* vbuf
;
2508 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
2513 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
2516 vsec
->add_output_section_data(vdata
);
2517 vsec
->set_entsize(2);
2518 vsec
->set_link_section(this->dynsym_section_
);
2520 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2521 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
2523 if (versions
->any_defs())
2525 Output_section
* vdsec
;
2526 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
2527 elfcpp::SHT_GNU_verdef
,
2531 unsigned char* vdbuf
;
2532 unsigned int vdsize
;
2533 unsigned int vdentries
;
2534 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
2535 &vdsize
, &vdentries
);
2537 Output_section_data
* vddata
=
2538 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
2540 vdsec
->add_output_section_data(vddata
);
2541 vdsec
->set_link_section(dynstr
);
2542 vdsec
->set_info(vdentries
);
2544 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
2545 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
2548 if (versions
->any_needs())
2550 Output_section
* vnsec
;
2551 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
2552 elfcpp::SHT_GNU_verneed
,
2556 unsigned char* vnbuf
;
2557 unsigned int vnsize
;
2558 unsigned int vnentries
;
2559 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
2563 Output_section_data
* vndata
=
2564 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
2566 vnsec
->add_output_section_data(vndata
);
2567 vnsec
->set_link_section(dynstr
);
2568 vnsec
->set_info(vnentries
);
2570 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
2571 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
2575 // Create the .interp section and PT_INTERP segment.
2578 Layout::create_interp(const Target
* target
)
2580 const char* interp
= this->options_
.dynamic_linker();
2583 interp
= target
->dynamic_linker();
2584 gold_assert(interp
!= NULL
);
2587 size_t len
= strlen(interp
) + 1;
2589 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
2591 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
2592 elfcpp::SHT_PROGBITS
,
2595 osec
->add_output_section_data(odata
);
2597 if (!this->script_options_
->saw_phdrs_clause())
2599 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
2601 oseg
->add_output_section(osec
, elfcpp::PF_R
);
2605 // Finish the .dynamic section and PT_DYNAMIC segment.
2608 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
2609 const Symbol_table
* symtab
)
2611 if (!this->script_options_
->saw_phdrs_clause())
2613 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
2616 oseg
->add_output_section(this->dynamic_section_
,
2617 elfcpp::PF_R
| elfcpp::PF_W
);
2620 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2622 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
2623 p
!= input_objects
->dynobj_end();
2626 // FIXME: Handle --as-needed.
2627 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
2630 if (parameters
->options().shared())
2632 const char* soname
= this->options_
.soname();
2634 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
2637 // FIXME: Support --init and --fini.
2638 Symbol
* sym
= symtab
->lookup("_init");
2639 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2640 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
2642 sym
= symtab
->lookup("_fini");
2643 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2644 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
2646 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2648 // Add a DT_RPATH entry if needed.
2649 const General_options::Dir_list
& rpath(this->options_
.rpath());
2652 std::string rpath_val
;
2653 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
2657 if (rpath_val
.empty())
2658 rpath_val
= p
->name();
2661 // Eliminate duplicates.
2662 General_options::Dir_list::const_iterator q
;
2663 for (q
= rpath
.begin(); q
!= p
; ++q
)
2664 if (q
->name() == p
->name())
2669 rpath_val
+= p
->name();
2674 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
2675 if (parameters
->options().enable_new_dtags())
2676 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
2679 // Look for text segments that have dynamic relocations.
2680 bool have_textrel
= false;
2681 if (!this->script_options_
->saw_sections_clause())
2683 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2684 p
!= this->segment_list_
.end();
2687 if (((*p
)->flags() & elfcpp::PF_W
) == 0
2688 && (*p
)->dynamic_reloc_count() > 0)
2690 have_textrel
= true;
2697 // We don't know the section -> segment mapping, so we are
2698 // conservative and just look for readonly sections with
2699 // relocations. If those sections wind up in writable segments,
2700 // then we have created an unnecessary DT_TEXTREL entry.
2701 for (Section_list::const_iterator p
= this->section_list_
.begin();
2702 p
!= this->section_list_
.end();
2705 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
2706 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
2707 && ((*p
)->dynamic_reloc_count() > 0))
2709 have_textrel
= true;
2715 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2716 // post-link tools can easily modify these flags if desired.
2717 unsigned int flags
= 0;
2720 // Add a DT_TEXTREL for compatibility with older loaders.
2721 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
2722 flags
|= elfcpp::DF_TEXTREL
;
2724 if (parameters
->options().shared() && this->has_static_tls())
2725 flags
|= elfcpp::DF_STATIC_TLS
;
2726 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
2729 if (parameters
->options().initfirst())
2730 flags
|= elfcpp::DF_1_INITFIRST
;
2731 if (parameters
->options().interpose())
2732 flags
|= elfcpp::DF_1_INTERPOSE
;
2733 if (parameters
->options().loadfltr())
2734 flags
|= elfcpp::DF_1_LOADFLTR
;
2735 if (parameters
->options().nodefaultlib())
2736 flags
|= elfcpp::DF_1_NODEFLIB
;
2737 if (parameters
->options().nodelete())
2738 flags
|= elfcpp::DF_1_NODELETE
;
2739 if (parameters
->options().nodlopen())
2740 flags
|= elfcpp::DF_1_NOOPEN
;
2741 if (parameters
->options().nodump())
2742 flags
|= elfcpp::DF_1_NODUMP
;
2743 if (!parameters
->options().shared())
2744 flags
&= ~(elfcpp::DF_1_INITFIRST
2745 | elfcpp::DF_1_NODELETE
2746 | elfcpp::DF_1_NOOPEN
);
2748 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
2751 // The mapping of .gnu.linkonce section names to real section names.
2753 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2754 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
2756 MAPPING_INIT("d.rel.ro.local", ".data.rel.ro.local"), // Before "d.rel.ro".
2757 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Before "d".
2758 MAPPING_INIT("t", ".text"),
2759 MAPPING_INIT("r", ".rodata"),
2760 MAPPING_INIT("d", ".data"),
2761 MAPPING_INIT("b", ".bss"),
2762 MAPPING_INIT("s", ".sdata"),
2763 MAPPING_INIT("sb", ".sbss"),
2764 MAPPING_INIT("s2", ".sdata2"),
2765 MAPPING_INIT("sb2", ".sbss2"),
2766 MAPPING_INIT("wi", ".debug_info"),
2767 MAPPING_INIT("td", ".tdata"),
2768 MAPPING_INIT("tb", ".tbss"),
2769 MAPPING_INIT("lr", ".lrodata"),
2770 MAPPING_INIT("l", ".ldata"),
2771 MAPPING_INIT("lb", ".lbss"),
2775 const int Layout::linkonce_mapping_count
=
2776 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
2778 // Return the name of the output section to use for a .gnu.linkonce
2779 // section. This is based on the default ELF linker script of the old
2780 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2781 // to ".text". Set *PLEN to the length of the name. *PLEN is
2782 // initialized to the length of NAME.
2785 Layout::linkonce_output_name(const char* name
, size_t *plen
)
2787 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
2791 const Linkonce_mapping
* plm
= linkonce_mapping
;
2792 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
2794 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
2803 // Choose the output section name to use given an input section name.
2804 // Set *PLEN to the length of the name. *PLEN is initialized to the
2808 Layout::output_section_name(const char* name
, size_t* plen
)
2810 if (Layout::is_linkonce(name
))
2812 // .gnu.linkonce sections are laid out as though they were named
2813 // for the sections are placed into.
2814 return Layout::linkonce_output_name(name
, plen
);
2817 // gcc 4.3 generates the following sorts of section names when it
2818 // needs a section name specific to a function:
2824 // .data.rel.local.FN
2826 // .data.rel.ro.local.FN
2833 // The GNU linker maps all of those to the part before the .FN,
2834 // except that .data.rel.local.FN is mapped to .data, and
2835 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2836 // beginning with .data.rel.ro.local are grouped together.
2838 // For an anonymous namespace, the string FN can contain a '.'.
2840 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2841 // GNU linker maps to .rodata.
2843 // The .data.rel.ro sections enable a security feature triggered by
2844 // the -z relro option. Section which need to be relocated at
2845 // program startup time but which may be readonly after startup are
2846 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2847 // segment. The dynamic linker will make that segment writable,
2848 // perform relocations, and then make it read-only. FIXME: We do
2849 // not yet implement this optimization.
2851 // It is hard to handle this in a principled way.
2853 // These are the rules we follow:
2855 // If the section name has no initial '.', or no dot other than an
2856 // initial '.', we use the name unchanged (i.e., "mysection" and
2857 // ".text" are unchanged).
2859 // If the name starts with ".data.rel.ro.local" we use
2860 // ".data.rel.ro.local".
2862 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2864 // Otherwise, we drop the second '.' and everything that comes after
2865 // it (i.e., ".text.XXX" becomes ".text").
2867 const char* s
= name
;
2871 const char* sdot
= strchr(s
, '.');
2875 const char* const data_rel_ro_local
= ".data.rel.ro.local";
2876 if (strncmp(name
, data_rel_ro_local
, strlen(data_rel_ro_local
)) == 0)
2878 *plen
= strlen(data_rel_ro_local
);
2879 return data_rel_ro_local
;
2882 const char* const data_rel_ro
= ".data.rel.ro";
2883 if (strncmp(name
, data_rel_ro
, strlen(data_rel_ro
)) == 0)
2885 *plen
= strlen(data_rel_ro
);
2889 *plen
= sdot
- name
;
2893 // Record the signature of a comdat section, and return whether to
2894 // include it in the link. If GROUP is true, this is a regular
2895 // section group. If GROUP is false, this is a group signature
2896 // derived from the name of a linkonce section. We want linkonce
2897 // signatures and group signatures to block each other, but we don't
2898 // want a linkonce signature to block another linkonce signature.
2901 Layout::add_comdat(Relobj
* object
, unsigned int shndx
,
2902 const std::string
& signature
, bool group
)
2904 Kept_section
kept(object
, shndx
, group
);
2905 std::pair
<Signatures::iterator
, bool> ins(
2906 this->signatures_
.insert(std::make_pair(signature
, kept
)));
2910 // This is the first time we've seen this signature.
2914 if (ins
.first
->second
.group_
)
2916 // We've already seen a real section group with this signature.
2921 // This is a real section group, and we've already seen a
2922 // linkonce section with this signature. Record that we've seen
2923 // a section group, and don't include this section group.
2924 ins
.first
->second
.group_
= true;
2929 // We've already seen a linkonce section and this is a linkonce
2930 // section. These don't block each other--this may be the same
2931 // symbol name with different section types.
2936 // Find the given comdat signature, and return the object and section
2937 // index of the kept group.
2939 Layout::find_kept_object(const std::string
& signature
,
2940 unsigned int* pshndx
) const
2942 Signatures::const_iterator p
= this->signatures_
.find(signature
);
2943 if (p
== this->signatures_
.end())
2946 *pshndx
= p
->second
.shndx_
;
2947 return p
->second
.object_
;
2950 // Store the allocated sections into the section list.
2953 Layout::get_allocated_sections(Section_list
* section_list
) const
2955 for (Section_list::const_iterator p
= this->section_list_
.begin();
2956 p
!= this->section_list_
.end();
2958 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
2959 section_list
->push_back(*p
);
2962 // Create an output segment.
2965 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2967 gold_assert(!parameters
->options().relocatable());
2968 Output_segment
* oseg
= new Output_segment(type
, flags
);
2969 this->segment_list_
.push_back(oseg
);
2973 // Write out the Output_sections. Most won't have anything to write,
2974 // since most of the data will come from input sections which are
2975 // handled elsewhere. But some Output_sections do have Output_data.
2978 Layout::write_output_sections(Output_file
* of
) const
2980 for (Section_list::const_iterator p
= this->section_list_
.begin();
2981 p
!= this->section_list_
.end();
2984 if (!(*p
)->after_input_sections())
2989 // Write out data not associated with a section or the symbol table.
2992 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
2994 if (!parameters
->options().strip_all())
2996 const Output_section
* symtab_section
= this->symtab_section_
;
2997 for (Section_list::const_iterator p
= this->section_list_
.begin();
2998 p
!= this->section_list_
.end();
3001 if ((*p
)->needs_symtab_index())
3003 gold_assert(symtab_section
!= NULL
);
3004 unsigned int index
= (*p
)->symtab_index();
3005 gold_assert(index
> 0 && index
!= -1U);
3006 off_t off
= (symtab_section
->offset()
3007 + index
* symtab_section
->entsize());
3008 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
3013 const Output_section
* dynsym_section
= this->dynsym_section_
;
3014 for (Section_list::const_iterator p
= this->section_list_
.begin();
3015 p
!= this->section_list_
.end();
3018 if ((*p
)->needs_dynsym_index())
3020 gold_assert(dynsym_section
!= NULL
);
3021 unsigned int index
= (*p
)->dynsym_index();
3022 gold_assert(index
> 0 && index
!= -1U);
3023 off_t off
= (dynsym_section
->offset()
3024 + index
* dynsym_section
->entsize());
3025 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
3029 // Write out the Output_data which are not in an Output_section.
3030 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
3031 p
!= this->special_output_list_
.end();
3036 // Write out the Output_sections which can only be written after the
3037 // input sections are complete.
3040 Layout::write_sections_after_input_sections(Output_file
* of
)
3042 // Determine the final section offsets, and thus the final output
3043 // file size. Note we finalize the .shstrab last, to allow the
3044 // after_input_section sections to modify their section-names before
3046 if (this->any_postprocessing_sections_
)
3048 off_t off
= this->output_file_size_
;
3049 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
3051 // Now that we've finalized the names, we can finalize the shstrab.
3053 this->set_section_offsets(off
,
3054 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
3056 if (off
> this->output_file_size_
)
3059 this->output_file_size_
= off
;
3063 for (Section_list::const_iterator p
= this->section_list_
.begin();
3064 p
!= this->section_list_
.end();
3067 if ((*p
)->after_input_sections())
3071 this->section_headers_
->write(of
);
3074 // If the build ID requires computing a checksum, do so here, and
3075 // write it out. We compute a checksum over the entire file because
3076 // that is simplest.
3079 Layout::write_build_id(Output_file
* of
) const
3081 if (this->build_id_note_
== NULL
)
3084 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3086 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3087 this->build_id_note_
->data_size());
3089 const char* style
= parameters
->options().build_id();
3090 if (strcmp(style
, "sha1") == 0)
3093 sha1_init_ctx(&ctx
);
3094 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3095 sha1_finish_ctx(&ctx
, ov
);
3097 else if (strcmp(style
, "md5") == 0)
3101 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3102 md5_finish_ctx(&ctx
, ov
);
3107 of
->write_output_view(this->build_id_note_
->offset(),
3108 this->build_id_note_
->data_size(),
3111 of
->free_input_view(0, this->output_file_size_
, iv
);
3114 // Write out a binary file. This is called after the link is
3115 // complete. IN is the temporary output file we used to generate the
3116 // ELF code. We simply walk through the segments, read them from
3117 // their file offset in IN, and write them to their load address in
3118 // the output file. FIXME: with a bit more work, we could support
3119 // S-records and/or Intel hex format here.
3122 Layout::write_binary(Output_file
* in
) const
3124 gold_assert(this->options_
.oformat_enum()
3125 == General_options::OBJECT_FORMAT_BINARY
);
3127 // Get the size of the binary file.
3128 uint64_t max_load_address
= 0;
3129 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3130 p
!= this->segment_list_
.end();
3133 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3135 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3136 if (max_paddr
> max_load_address
)
3137 max_load_address
= max_paddr
;
3141 Output_file
out(parameters
->options().output_file_name());
3142 out
.open(max_load_address
);
3144 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3145 p
!= this->segment_list_
.end();
3148 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3150 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3152 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3154 memcpy(vout
, vin
, (*p
)->filesz());
3155 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3156 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3163 // Print the output sections to the map file.
3166 Layout::print_to_mapfile(Mapfile
* mapfile
) const
3168 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3169 p
!= this->segment_list_
.end();
3171 (*p
)->print_sections_to_mapfile(mapfile
);
3174 // Print statistical information to stderr. This is used for --stats.
3177 Layout::print_stats() const
3179 this->namepool_
.print_stats("section name pool");
3180 this->sympool_
.print_stats("output symbol name pool");
3181 this->dynpool_
.print_stats("dynamic name pool");
3183 for (Section_list::const_iterator p
= this->section_list_
.begin();
3184 p
!= this->section_list_
.end();
3186 (*p
)->print_merge_stats();
3189 // Write_sections_task methods.
3191 // We can always run this task.
3194 Write_sections_task::is_runnable()
3199 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3203 Write_sections_task::locks(Task_locker
* tl
)
3205 tl
->add(this, this->output_sections_blocker_
);
3206 tl
->add(this, this->final_blocker_
);
3209 // Run the task--write out the data.
3212 Write_sections_task::run(Workqueue
*)
3214 this->layout_
->write_output_sections(this->of_
);
3217 // Write_data_task methods.
3219 // We can always run this task.
3222 Write_data_task::is_runnable()
3227 // We need to unlock FINAL_BLOCKER when finished.
3230 Write_data_task::locks(Task_locker
* tl
)
3232 tl
->add(this, this->final_blocker_
);
3235 // Run the task--write out the data.
3238 Write_data_task::run(Workqueue
*)
3240 this->layout_
->write_data(this->symtab_
, this->of_
);
3243 // Write_symbols_task methods.
3245 // We can always run this task.
3248 Write_symbols_task::is_runnable()
3253 // We need to unlock FINAL_BLOCKER when finished.
3256 Write_symbols_task::locks(Task_locker
* tl
)
3258 tl
->add(this, this->final_blocker_
);
3261 // Run the task--write out the symbols.
3264 Write_symbols_task::run(Workqueue
*)
3266 this->symtab_
->write_globals(this->input_objects_
, this->sympool_
,
3267 this->dynpool_
, this->layout_
->symtab_xindex(),
3268 this->layout_
->dynsym_xindex(), this->of_
);
3271 // Write_after_input_sections_task methods.
3273 // We can only run this task after the input sections have completed.
3276 Write_after_input_sections_task::is_runnable()
3278 if (this->input_sections_blocker_
->is_blocked())
3279 return this->input_sections_blocker_
;
3283 // We need to unlock FINAL_BLOCKER when finished.
3286 Write_after_input_sections_task::locks(Task_locker
* tl
)
3288 tl
->add(this, this->final_blocker_
);
3294 Write_after_input_sections_task::run(Workqueue
*)
3296 this->layout_
->write_sections_after_input_sections(this->of_
);
3299 // Close_task_runner methods.
3301 // Run the task--close the file.
3304 Close_task_runner::run(Workqueue
*, const Task
*)
3306 // If we need to compute a checksum for the BUILD if, we do so here.
3307 this->layout_
->write_build_id(this->of_
);
3309 // If we've been asked to create a binary file, we do so here.
3310 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3311 this->layout_
->write_binary(this->of_
);
3316 // Instantiate the templates we need. We could use the configure
3317 // script to restrict this to only the ones for implemented targets.
3319 #ifdef HAVE_TARGET_32_LITTLE
3322 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3324 const elfcpp::Shdr
<32, false>& shdr
,
3325 unsigned int, unsigned int, off_t
*);
3328 #ifdef HAVE_TARGET_32_BIG
3331 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3333 const elfcpp::Shdr
<32, true>& shdr
,
3334 unsigned int, unsigned int, off_t
*);
3337 #ifdef HAVE_TARGET_64_LITTLE
3340 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3342 const elfcpp::Shdr
<64, false>& shdr
,
3343 unsigned int, unsigned int, off_t
*);
3346 #ifdef HAVE_TARGET_64_BIG
3349 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3351 const elfcpp::Shdr
<64, true>& shdr
,
3352 unsigned int, unsigned int, off_t
*);
3355 #ifdef HAVE_TARGET_32_LITTLE
3358 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
3359 unsigned int reloc_shndx
,
3360 const elfcpp::Shdr
<32, false>& shdr
,
3361 Output_section
* data_section
,
3362 Relocatable_relocs
* rr
);
3365 #ifdef HAVE_TARGET_32_BIG
3368 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
3369 unsigned int reloc_shndx
,
3370 const elfcpp::Shdr
<32, true>& shdr
,
3371 Output_section
* data_section
,
3372 Relocatable_relocs
* rr
);
3375 #ifdef HAVE_TARGET_64_LITTLE
3378 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
3379 unsigned int reloc_shndx
,
3380 const elfcpp::Shdr
<64, false>& shdr
,
3381 Output_section
* data_section
,
3382 Relocatable_relocs
* rr
);
3385 #ifdef HAVE_TARGET_64_BIG
3388 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
3389 unsigned int reloc_shndx
,
3390 const elfcpp::Shdr
<64, true>& shdr
,
3391 Output_section
* data_section
,
3392 Relocatable_relocs
* rr
);
3395 #ifdef HAVE_TARGET_32_LITTLE
3398 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
3399 Sized_relobj
<32, false>* object
,
3401 const char* group_section_name
,
3402 const char* signature
,
3403 const elfcpp::Shdr
<32, false>& shdr
,
3404 elfcpp::Elf_Word flags
,
3405 std::vector
<unsigned int>* shndxes
);
3408 #ifdef HAVE_TARGET_32_BIG
3411 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
3412 Sized_relobj
<32, true>* object
,
3414 const char* group_section_name
,
3415 const char* signature
,
3416 const elfcpp::Shdr
<32, true>& shdr
,
3417 elfcpp::Elf_Word flags
,
3418 std::vector
<unsigned int>* shndxes
);
3421 #ifdef HAVE_TARGET_64_LITTLE
3424 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
3425 Sized_relobj
<64, false>* object
,
3427 const char* group_section_name
,
3428 const char* signature
,
3429 const elfcpp::Shdr
<64, false>& shdr
,
3430 elfcpp::Elf_Word flags
,
3431 std::vector
<unsigned int>* shndxes
);
3434 #ifdef HAVE_TARGET_64_BIG
3437 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
3438 Sized_relobj
<64, true>* object
,
3440 const char* group_section_name
,
3441 const char* signature
,
3442 const elfcpp::Shdr
<64, true>& shdr
,
3443 elfcpp::Elf_Word flags
,
3444 std::vector
<unsigned int>* shndxes
);
3447 #ifdef HAVE_TARGET_32_LITTLE
3450 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
3451 const unsigned char* symbols
,
3453 const unsigned char* symbol_names
,
3454 off_t symbol_names_size
,
3456 const elfcpp::Shdr
<32, false>& shdr
,
3457 unsigned int reloc_shndx
,
3458 unsigned int reloc_type
,
3462 #ifdef HAVE_TARGET_32_BIG
3465 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
3466 const unsigned char* symbols
,
3468 const unsigned char* symbol_names
,
3469 off_t symbol_names_size
,
3471 const elfcpp::Shdr
<32, true>& shdr
,
3472 unsigned int reloc_shndx
,
3473 unsigned int reloc_type
,
3477 #ifdef HAVE_TARGET_64_LITTLE
3480 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
3481 const unsigned char* symbols
,
3483 const unsigned char* symbol_names
,
3484 off_t symbol_names_size
,
3486 const elfcpp::Shdr
<64, false>& shdr
,
3487 unsigned int reloc_shndx
,
3488 unsigned int reloc_type
,
3492 #ifdef HAVE_TARGET_64_BIG
3495 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
3496 const unsigned char* symbols
,
3498 const unsigned char* symbol_names
,
3499 off_t symbol_names_size
,
3501 const elfcpp::Shdr
<64, true>& shdr
,
3502 unsigned int reloc_shndx
,
3503 unsigned int reloc_type
,
3507 } // End namespace gold.