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"
52 // Layout_task_runner methods.
54 // Lay out the sections. This is called after all the input objects
58 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
60 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
65 // Now we know the final size of the output file and we know where
66 // each piece of information goes.
68 if (this->mapfile_
!= NULL
)
70 this->mapfile_
->print_discarded_sections(this->input_objects_
);
71 this->layout_
->print_to_mapfile(this->mapfile_
);
74 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
75 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
76 of
->set_is_temporary();
79 // Queue up the final set of tasks.
80 gold::queue_final_tasks(this->options_
, this->input_objects_
,
81 this->symtab_
, this->layout_
, workqueue
, of
);
86 Layout::Layout(const General_options
& options
, Script_options
* script_options
)
88 script_options_(script_options
),
96 unattached_section_list_(),
97 sections_are_attached_(false),
98 special_output_list_(),
99 section_headers_(NULL
),
101 relro_segment_(NULL
),
102 symtab_section_(NULL
),
103 symtab_xindex_(NULL
),
104 dynsym_section_(NULL
),
105 dynsym_xindex_(NULL
),
106 dynamic_section_(NULL
),
108 eh_frame_section_(NULL
),
109 eh_frame_data_(NULL
),
110 added_eh_frame_data_(false),
111 eh_frame_hdr_section_(NULL
),
112 build_id_note_(NULL
),
114 output_file_size_(-1),
115 input_requires_executable_stack_(false),
116 input_with_gnu_stack_note_(false),
117 input_without_gnu_stack_note_(false),
118 has_static_tls_(false),
119 any_postprocessing_sections_(false)
121 // Make space for more than enough segments for a typical file.
122 // This is just for efficiency--it's OK if we wind up needing more.
123 this->segment_list_
.reserve(12);
125 // We expect two unattached Output_data objects: the file header and
126 // the segment headers.
127 this->special_output_list_
.reserve(2);
130 // Hash a key we use to look up an output section mapping.
133 Layout::Hash_key::operator()(const Layout::Key
& k
) const
135 return k
.first
+ k
.second
.first
+ k
.second
.second
;
138 // Return whether PREFIX is a prefix of STR.
141 is_prefix_of(const char* prefix
, const char* str
)
143 return strncmp(prefix
, str
, strlen(prefix
)) == 0;
146 // Returns whether the given section is in the list of
147 // debug-sections-used-by-some-version-of-gdb. Currently,
148 // we've checked versions of gdb up to and including 6.7.1.
150 static const char* gdb_sections
[] =
152 // ".debug_aranges", // not used by gdb as of 6.7.1
158 // ".debug_pubnames", // not used by gdb as of 6.7.1
164 is_gdb_debug_section(const char* str
)
166 // We can do this faster: binary search or a hashtable. But why bother?
167 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
168 if (strcmp(str
, gdb_sections
[i
]) == 0)
173 // Whether to include this section in the link.
175 template<int size
, bool big_endian
>
177 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
178 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
180 switch (shdr
.get_sh_type())
182 case elfcpp::SHT_NULL
:
183 case elfcpp::SHT_SYMTAB
:
184 case elfcpp::SHT_DYNSYM
:
185 case elfcpp::SHT_STRTAB
:
186 case elfcpp::SHT_HASH
:
187 case elfcpp::SHT_DYNAMIC
:
188 case elfcpp::SHT_SYMTAB_SHNDX
:
191 case elfcpp::SHT_RELA
:
192 case elfcpp::SHT_REL
:
193 case elfcpp::SHT_GROUP
:
194 // If we are emitting relocations these should be handled
196 gold_assert(!parameters
->options().relocatable()
197 && !parameters
->options().emit_relocs());
200 case elfcpp::SHT_PROGBITS
:
201 if (parameters
->options().strip_debug()
202 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
204 if (is_debug_info_section(name
))
207 if (parameters
->options().strip_debug_gdb()
208 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
210 // Debugging sections can only be recognized by name.
211 if (is_prefix_of(".debug", name
)
212 && !is_gdb_debug_section(name
))
222 // Return an output section named NAME, or NULL if there is none.
225 Layout::find_output_section(const char* name
) const
227 for (Section_list::const_iterator p
= this->section_list_
.begin();
228 p
!= this->section_list_
.end();
230 if (strcmp((*p
)->name(), name
) == 0)
235 // Return an output segment of type TYPE, with segment flags SET set
236 // and segment flags CLEAR clear. Return NULL if there is none.
239 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
240 elfcpp::Elf_Word clear
) const
242 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
243 p
!= this->segment_list_
.end();
245 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
246 && ((*p
)->flags() & set
) == set
247 && ((*p
)->flags() & clear
) == 0)
252 // Return the output section to use for section NAME with type TYPE
253 // and section flags FLAGS. NAME must be canonicalized in the string
254 // pool, and NAME_KEY is the key.
257 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
258 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
260 elfcpp::Elf_Xword lookup_flags
= flags
;
262 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
263 // read-write with read-only sections. Some other ELF linkers do
264 // not do this. FIXME: Perhaps there should be an option
266 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
268 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
269 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
270 std::pair
<Section_name_map::iterator
, bool> ins(
271 this->section_name_map_
.insert(v
));
274 return ins
.first
->second
;
277 // This is the first time we've seen this name/type/flags
278 // combination. For compatibility with the GNU linker, we
279 // combine sections with contents and zero flags with sections
280 // with non-zero flags. This is a workaround for cases where
281 // assembler code forgets to set section flags. FIXME: Perhaps
282 // there should be an option to control this.
283 Output_section
* os
= NULL
;
285 if (type
== elfcpp::SHT_PROGBITS
)
289 Output_section
* same_name
= this->find_output_section(name
);
290 if (same_name
!= NULL
291 && same_name
->type() == elfcpp::SHT_PROGBITS
292 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
295 else if ((flags
& elfcpp::SHF_TLS
) == 0)
297 elfcpp::Elf_Xword zero_flags
= 0;
298 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
299 Section_name_map::iterator p
=
300 this->section_name_map_
.find(zero_key
);
301 if (p
!= this->section_name_map_
.end())
307 os
= this->make_output_section(name
, type
, flags
);
308 ins
.first
->second
= os
;
313 // Pick the output section to use for section NAME, in input file
314 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
315 // linker created section. IS_INPUT_SECTION is true if we are
316 // choosing an output section for an input section found in a input
317 // file. This will return NULL if the input section should be
321 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
322 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
323 bool is_input_section
)
325 // We should not see any input sections after we have attached
326 // sections to segments.
327 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
329 // Some flags in the input section should not be automatically
330 // copied to the output section.
331 flags
&= ~ (elfcpp::SHF_INFO_LINK
332 | elfcpp::SHF_LINK_ORDER
335 | elfcpp::SHF_STRINGS
);
337 if (this->script_options_
->saw_sections_clause())
339 // We are using a SECTIONS clause, so the output section is
340 // chosen based only on the name.
342 Script_sections
* ss
= this->script_options_
->script_sections();
343 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
344 Output_section
** output_section_slot
;
345 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
348 // The SECTIONS clause says to discard this input section.
352 // If this is an orphan section--one not mentioned in the linker
353 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
354 // default processing below.
356 if (output_section_slot
!= NULL
)
358 if (*output_section_slot
!= NULL
)
359 return *output_section_slot
;
361 // We don't put sections found in the linker script into
362 // SECTION_NAME_MAP_. That keeps us from getting confused
363 // if an orphan section is mapped to a section with the same
364 // name as one in the linker script.
366 name
= this->namepool_
.add(name
, false, NULL
);
368 Output_section
* os
= this->make_output_section(name
, type
, flags
);
369 os
->set_found_in_sections_clause();
370 *output_section_slot
= os
;
375 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
377 // Turn NAME from the name of the input section into the name of the
380 size_t len
= strlen(name
);
381 if (is_input_section
&& !parameters
->options().relocatable())
382 name
= Layout::output_section_name(name
, &len
);
384 Stringpool::Key name_key
;
385 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
387 // Find or make the output section. The output section is selected
388 // based on the section name, type, and flags.
389 return this->get_output_section(name
, name_key
, type
, flags
);
392 // Return the output section to use for input section SHNDX, with name
393 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
394 // index of a relocation section which applies to this section, or 0
395 // if none, or -1U if more than one. RELOC_TYPE is the type of the
396 // relocation section if there is one. Set *OFF to the offset of this
397 // input section without the output section. Return NULL if the
398 // section should be discarded. Set *OFF to -1 if the section
399 // contents should not be written directly to the output file, but
400 // will instead receive special handling.
402 template<int size
, bool big_endian
>
404 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
405 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
406 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
408 if (!this->include_section(object
, name
, shdr
))
413 // In a relocatable link a grouped section must not be combined with
414 // any other sections.
415 if (parameters
->options().relocatable()
416 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
418 name
= this->namepool_
.add(name
, true, NULL
);
419 os
= this->make_output_section(name
, shdr
.get_sh_type(),
420 shdr
.get_sh_flags());
424 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
425 shdr
.get_sh_flags(), true);
430 // By default the GNU linker sorts input sections whose names match
431 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
432 // are sorted by name. This is used to implement constructor
433 // priority ordering. We are compatible.
434 if (!this->script_options_
->saw_sections_clause()
435 && (is_prefix_of(".ctors.", name
)
436 || is_prefix_of(".dtors.", name
)
437 || is_prefix_of(".init_array.", name
)
438 || is_prefix_of(".fini_array.", name
)))
439 os
->set_must_sort_attached_input_sections();
441 // FIXME: Handle SHF_LINK_ORDER somewhere.
443 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
444 this->script_options_
->saw_sections_clause());
449 // Handle a relocation section when doing a relocatable link.
451 template<int size
, bool big_endian
>
453 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
455 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
456 Output_section
* data_section
,
457 Relocatable_relocs
* rr
)
459 gold_assert(parameters
->options().relocatable()
460 || parameters
->options().emit_relocs());
462 int sh_type
= shdr
.get_sh_type();
465 if (sh_type
== elfcpp::SHT_REL
)
467 else if (sh_type
== elfcpp::SHT_RELA
)
471 name
+= data_section
->name();
473 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
478 os
->set_should_link_to_symtab();
479 os
->set_info_section(data_section
);
481 Output_section_data
* posd
;
482 if (sh_type
== elfcpp::SHT_REL
)
484 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
485 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
489 else if (sh_type
== elfcpp::SHT_RELA
)
491 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
492 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
499 os
->add_output_section_data(posd
);
500 rr
->set_output_data(posd
);
505 // Handle a group section when doing a relocatable link.
507 template<int size
, bool big_endian
>
509 Layout::layout_group(Symbol_table
* symtab
,
510 Sized_relobj
<size
, big_endian
>* object
,
512 const char* group_section_name
,
513 const char* signature
,
514 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
515 elfcpp::Elf_Word flags
,
516 std::vector
<unsigned int>* shndxes
)
518 gold_assert(parameters
->options().relocatable());
519 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
520 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
521 Output_section
* os
= this->make_output_section(group_section_name
,
523 shdr
.get_sh_flags());
525 // We need to find a symbol with the signature in the symbol table.
526 // If we don't find one now, we need to look again later.
527 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
529 os
->set_info_symndx(sym
);
532 // We will wind up using a symbol whose name is the signature.
533 // So just put the signature in the symbol name pool to save it.
534 signature
= symtab
->canonicalize_name(signature
);
535 this->group_signatures_
.push_back(Group_signature(os
, signature
));
538 os
->set_should_link_to_symtab();
541 section_size_type entry_count
=
542 convert_to_section_size_type(shdr
.get_sh_size() / 4);
543 Output_section_data
* posd
=
544 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
546 os
->add_output_section_data(posd
);
549 // Special GNU handling of sections name .eh_frame. They will
550 // normally hold exception frame data as defined by the C++ ABI
551 // (http://codesourcery.com/cxx-abi/).
553 template<int size
, bool big_endian
>
555 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
556 const unsigned char* symbols
,
558 const unsigned char* symbol_names
,
559 off_t symbol_names_size
,
561 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
562 unsigned int reloc_shndx
, unsigned int reloc_type
,
565 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
566 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
568 const char* const name
= ".eh_frame";
569 Output_section
* os
= this->choose_output_section(object
,
571 elfcpp::SHT_PROGBITS
,
577 if (this->eh_frame_section_
== NULL
)
579 this->eh_frame_section_
= os
;
580 this->eh_frame_data_
= new Eh_frame();
582 if (this->options_
.eh_frame_hdr())
584 Output_section
* hdr_os
=
585 this->choose_output_section(NULL
,
587 elfcpp::SHT_PROGBITS
,
593 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
594 this->eh_frame_data_
);
595 hdr_os
->add_output_section_data(hdr_posd
);
597 hdr_os
->set_after_input_sections();
599 if (!this->script_options_
->saw_phdrs_clause())
601 Output_segment
* hdr_oseg
;
602 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
604 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
);
607 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
612 gold_assert(this->eh_frame_section_
== os
);
614 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
623 os
->update_flags_for_input_section(shdr
.get_sh_flags());
625 // We found a .eh_frame section we are going to optimize, so now
626 // we can add the set of optimized sections to the output
627 // section. We need to postpone adding this until we've found a
628 // section we can optimize so that the .eh_frame section in
629 // crtbegin.o winds up at the start of the output section.
630 if (!this->added_eh_frame_data_
)
632 os
->add_output_section_data(this->eh_frame_data_
);
633 this->added_eh_frame_data_
= true;
639 // We couldn't handle this .eh_frame section for some reason.
640 // Add it as a normal section.
641 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
642 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
643 saw_sections_clause
);
649 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
650 // the output section.
653 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
654 elfcpp::Elf_Xword flags
,
655 Output_section_data
* posd
)
657 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
660 os
->add_output_section_data(posd
);
664 // Map section flags to segment flags.
667 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
669 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
670 if ((flags
& elfcpp::SHF_WRITE
) != 0)
672 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
677 // Sometimes we compress sections. This is typically done for
678 // sections that are not part of normal program execution (such as
679 // .debug_* sections), and where the readers of these sections know
680 // how to deal with compressed sections. (To make it easier for them,
681 // we will rename the ouput section in such cases from .foo to
682 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
683 // doesn't say for certain whether we'll compress -- it depends on
684 // commandline options as well -- just whether this section is a
685 // candidate for compression.
688 is_compressible_debug_section(const char* secname
)
690 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
693 // Make a new Output_section, and attach it to segments as
697 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
698 elfcpp::Elf_Xword flags
)
701 if ((flags
& elfcpp::SHF_ALLOC
) == 0
702 && strcmp(this->options_
.compress_debug_sections(), "none") != 0
703 && is_compressible_debug_section(name
))
704 os
= new Output_compressed_section(&this->options_
, name
, type
, flags
);
706 os
= new Output_section(name
, type
, flags
);
708 this->section_list_
.push_back(os
);
710 // The GNU linker by default sorts some sections by priority, so we
711 // do the same. We need to know that this might happen before we
712 // attach any input sections.
713 if (!this->script_options_
->saw_sections_clause()
714 && (strcmp(name
, ".ctors") == 0
715 || strcmp(name
, ".dtors") == 0
716 || strcmp(name
, ".init_array") == 0
717 || strcmp(name
, ".fini_array") == 0))
718 os
->set_may_sort_attached_input_sections();
720 // With -z relro, we have to recognize the special sections by name.
721 // There is no other way.
722 if (!this->script_options_
->saw_sections_clause()
723 && parameters
->options().relro()
724 && type
== elfcpp::SHT_PROGBITS
725 && (flags
& elfcpp::SHF_ALLOC
) != 0
726 && (flags
& elfcpp::SHF_WRITE
) != 0)
728 if (strcmp(name
, ".data.rel.ro") == 0)
730 else if (strcmp(name
, ".data.rel.ro.local") == 0)
733 os
->set_is_relro_local();
737 // If we have already attached the sections to segments, then we
738 // need to attach this one now. This happens for sections created
739 // directly by the linker.
740 if (this->sections_are_attached_
)
741 this->attach_section_to_segment(os
);
746 // Attach output sections to segments. This is called after we have
747 // seen all the input sections.
750 Layout::attach_sections_to_segments()
752 for (Section_list::iterator p
= this->section_list_
.begin();
753 p
!= this->section_list_
.end();
755 this->attach_section_to_segment(*p
);
757 this->sections_are_attached_
= true;
760 // Attach an output section to a segment.
763 Layout::attach_section_to_segment(Output_section
* os
)
765 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
766 this->unattached_section_list_
.push_back(os
);
768 this->attach_allocated_section_to_segment(os
);
771 // Attach an allocated output section to a segment.
774 Layout::attach_allocated_section_to_segment(Output_section
* os
)
776 elfcpp::Elf_Xword flags
= os
->flags();
777 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
779 if (parameters
->options().relocatable())
782 // If we have a SECTIONS clause, we can't handle the attachment to
783 // segments until after we've seen all the sections.
784 if (this->script_options_
->saw_sections_clause())
787 gold_assert(!this->script_options_
->saw_phdrs_clause());
789 // This output section goes into a PT_LOAD segment.
791 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
793 // In general the only thing we really care about for PT_LOAD
794 // segments is whether or not they are writable, so that is how we
795 // search for them. People who need segments sorted on some other
796 // basis will have to use a linker script.
798 Segment_list::const_iterator p
;
799 for (p
= this->segment_list_
.begin();
800 p
!= this->segment_list_
.end();
803 if ((*p
)->type() == elfcpp::PT_LOAD
804 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
806 // If -Tbss was specified, we need to separate the data
808 if (this->options_
.user_set_Tbss())
810 if ((os
->type() == elfcpp::SHT_NOBITS
)
811 == (*p
)->has_any_data_sections())
815 (*p
)->add_output_section(os
, seg_flags
);
820 if (p
== this->segment_list_
.end())
822 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
824 oseg
->add_output_section(os
, seg_flags
);
827 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
829 if (os
->type() == elfcpp::SHT_NOTE
)
831 // See if we already have an equivalent PT_NOTE segment.
832 for (p
= this->segment_list_
.begin();
833 p
!= segment_list_
.end();
836 if ((*p
)->type() == elfcpp::PT_NOTE
837 && (((*p
)->flags() & elfcpp::PF_W
)
838 == (seg_flags
& elfcpp::PF_W
)))
840 (*p
)->add_output_section(os
, seg_flags
);
845 if (p
== this->segment_list_
.end())
847 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
849 oseg
->add_output_section(os
, seg_flags
);
853 // If we see a loadable SHF_TLS section, we create a PT_TLS
854 // segment. There can only be one such segment.
855 if ((flags
& elfcpp::SHF_TLS
) != 0)
857 if (this->tls_segment_
== NULL
)
858 this->tls_segment_
= this->make_output_segment(elfcpp::PT_TLS
,
860 this->tls_segment_
->add_output_section(os
, seg_flags
);
863 // If -z relro is in effect, and we see a relro section, we create a
864 // PT_GNU_RELRO segment. There can only be one such segment.
865 if (os
->is_relro() && parameters
->options().relro())
867 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
868 if (this->relro_segment_
== NULL
)
869 this->relro_segment_
= this->make_output_segment(elfcpp::PT_GNU_RELRO
,
871 this->relro_segment_
->add_output_section(os
, seg_flags
);
875 // Make an output section for a script.
878 Layout::make_output_section_for_script(const char* name
)
880 name
= this->namepool_
.add(name
, false, NULL
);
881 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
883 os
->set_found_in_sections_clause();
887 // Return the number of segments we expect to see.
890 Layout::expected_segment_count() const
892 size_t ret
= this->segment_list_
.size();
894 // If we didn't see a SECTIONS clause in a linker script, we should
895 // already have the complete list of segments. Otherwise we ask the
896 // SECTIONS clause how many segments it expects, and add in the ones
897 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
899 if (!this->script_options_
->saw_sections_clause())
903 const Script_sections
* ss
= this->script_options_
->script_sections();
904 return ret
+ ss
->expected_segment_count(this);
908 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
909 // is whether we saw a .note.GNU-stack section in the object file.
910 // GNU_STACK_FLAGS is the section flags. The flags give the
911 // protection required for stack memory. We record this in an
912 // executable as a PT_GNU_STACK segment. If an object file does not
913 // have a .note.GNU-stack segment, we must assume that it is an old
914 // object. On some targets that will force an executable stack.
917 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
920 this->input_without_gnu_stack_note_
= true;
923 this->input_with_gnu_stack_note_
= true;
924 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
925 this->input_requires_executable_stack_
= true;
929 // Create the dynamic sections which are needed before we read the
933 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
935 if (parameters
->doing_static_link())
938 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
941 | elfcpp::SHF_WRITE
),
943 this->dynamic_section_
->set_is_relro();
945 symtab
->define_in_output_data("_DYNAMIC", NULL
, this->dynamic_section_
, 0, 0,
946 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
947 elfcpp::STV_HIDDEN
, 0, false, false);
949 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
951 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
954 // For each output section whose name can be represented as C symbol,
955 // define __start and __stop symbols for the section. This is a GNU
959 Layout::define_section_symbols(Symbol_table
* symtab
)
961 for (Section_list::const_iterator p
= this->section_list_
.begin();
962 p
!= this->section_list_
.end();
965 const char* const name
= (*p
)->name();
966 if (name
[strspn(name
,
968 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
969 "abcdefghijklmnopqrstuvwxyz"
973 const std::string
name_string(name
);
974 const std::string
start_name("__start_" + name_string
);
975 const std::string
stop_name("__stop_" + name_string
);
977 symtab
->define_in_output_data(start_name
.c_str(),
986 false, // offset_is_from_end
987 true); // only_if_ref
989 symtab
->define_in_output_data(stop_name
.c_str(),
998 true, // offset_is_from_end
999 true); // only_if_ref
1004 // Define symbols for group signatures.
1007 Layout::define_group_signatures(Symbol_table
* symtab
)
1009 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1010 p
!= this->group_signatures_
.end();
1013 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1015 p
->section
->set_info_symndx(sym
);
1018 // Force the name of the group section to the group
1019 // signature, and use the group's section symbol as the
1020 // signature symbol.
1021 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1023 const char* name
= this->namepool_
.add(p
->signature
,
1025 p
->section
->set_name(name
);
1027 p
->section
->set_needs_symtab_index();
1028 p
->section
->set_info_section_symndx(p
->section
);
1032 this->group_signatures_
.clear();
1035 // Find the first read-only PT_LOAD segment, creating one if
1039 Layout::find_first_load_seg()
1041 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1042 p
!= this->segment_list_
.end();
1045 if ((*p
)->type() == elfcpp::PT_LOAD
1046 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1047 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
1051 gold_assert(!this->script_options_
->saw_phdrs_clause());
1053 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1058 // Finalize the layout. When this is called, we have created all the
1059 // output sections and all the output segments which are based on
1060 // input sections. We have several things to do, and we have to do
1061 // them in the right order, so that we get the right results correctly
1064 // 1) Finalize the list of output segments and create the segment
1067 // 2) Finalize the dynamic symbol table and associated sections.
1069 // 3) Determine the final file offset of all the output segments.
1071 // 4) Determine the final file offset of all the SHF_ALLOC output
1074 // 5) Create the symbol table sections and the section name table
1077 // 6) Finalize the symbol table: set symbol values to their final
1078 // value and make a final determination of which symbols are going
1079 // into the output symbol table.
1081 // 7) Create the section table header.
1083 // 8) Determine the final file offset of all the output sections which
1084 // are not SHF_ALLOC, including the section table header.
1086 // 9) Finalize the ELF file header.
1088 // This function returns the size of the output file.
1091 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1092 Target
* target
, const Task
* task
)
1094 target
->finalize_sections(this);
1096 this->count_local_symbols(task
, input_objects
);
1098 this->create_gold_note();
1099 this->create_executable_stack_info(target
);
1100 this->create_build_id();
1102 Output_segment
* phdr_seg
= NULL
;
1103 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1105 // There was a dynamic object in the link. We need to create
1106 // some information for the dynamic linker.
1108 // Create the PT_PHDR segment which will hold the program
1110 if (!this->script_options_
->saw_phdrs_clause())
1111 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1113 // Create the dynamic symbol table, including the hash table.
1114 Output_section
* dynstr
;
1115 std::vector
<Symbol
*> dynamic_symbols
;
1116 unsigned int local_dynamic_count
;
1117 Versions
versions(*this->script_options()->version_script_info(),
1119 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1120 &local_dynamic_count
, &dynamic_symbols
,
1123 // Create the .interp section to hold the name of the
1124 // interpreter, and put it in a PT_INTERP segment.
1125 if (!parameters
->options().shared())
1126 this->create_interp(target
);
1128 // Finish the .dynamic section to hold the dynamic data, and put
1129 // it in a PT_DYNAMIC segment.
1130 this->finish_dynamic_section(input_objects
, symtab
);
1132 // We should have added everything we need to the dynamic string
1134 this->dynpool_
.set_string_offsets();
1136 // Create the version sections. We can't do this until the
1137 // dynamic string table is complete.
1138 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1139 dynamic_symbols
, dynstr
);
1142 // If there is a SECTIONS clause, put all the input sections into
1143 // the required order.
1144 Output_segment
* load_seg
;
1145 if (this->script_options_
->saw_sections_clause())
1146 load_seg
= this->set_section_addresses_from_script(symtab
);
1147 else if (parameters
->options().relocatable())
1150 load_seg
= this->find_first_load_seg();
1152 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
1155 gold_assert(phdr_seg
== NULL
|| load_seg
!= NULL
);
1157 // Lay out the segment headers.
1158 Output_segment_headers
* segment_headers
;
1159 if (parameters
->options().relocatable())
1160 segment_headers
= NULL
;
1163 segment_headers
= new Output_segment_headers(this->segment_list_
);
1164 if (load_seg
!= NULL
)
1165 load_seg
->add_initial_output_data(segment_headers
);
1166 if (phdr_seg
!= NULL
)
1167 phdr_seg
->add_initial_output_data(segment_headers
);
1170 // Lay out the file header.
1171 Output_file_header
* file_header
;
1172 file_header
= new Output_file_header(target
, symtab
, segment_headers
,
1173 this->options_
.entry());
1174 if (load_seg
!= NULL
)
1175 load_seg
->add_initial_output_data(file_header
);
1177 this->special_output_list_
.push_back(file_header
);
1178 if (segment_headers
!= NULL
)
1179 this->special_output_list_
.push_back(segment_headers
);
1181 if (this->script_options_
->saw_phdrs_clause()
1182 && !parameters
->options().relocatable())
1184 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1185 // clause in a linker script.
1186 Script_sections
* ss
= this->script_options_
->script_sections();
1187 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1190 // We set the output section indexes in set_segment_offsets and
1191 // set_section_indexes.
1192 unsigned int shndx
= 1;
1194 // Set the file offsets of all the segments, and all the sections
1197 if (!parameters
->options().relocatable())
1198 off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
1200 off
= this->set_relocatable_section_offsets(file_header
, &shndx
);
1202 // Set the file offsets of all the non-data sections we've seen so
1203 // far which don't have to wait for the input sections. We need
1204 // this in order to finalize local symbols in non-allocated
1206 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1208 // Set the section indexes of all unallocated sections seen so far,
1209 // in case any of them are somehow referenced by a symbol.
1210 shndx
= this->set_section_indexes(shndx
);
1212 // Create the symbol table sections.
1213 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1214 if (!parameters
->doing_static_link())
1215 this->assign_local_dynsym_offsets(input_objects
);
1217 // Process any symbol assignments from a linker script. This must
1218 // be called after the symbol table has been finalized.
1219 this->script_options_
->finalize_symbols(symtab
, this);
1221 // Create the .shstrtab section.
1222 Output_section
* shstrtab_section
= this->create_shstrtab();
1224 // Set the file offsets of the rest of the non-data sections which
1225 // don't have to wait for the input sections.
1226 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1228 // Now that all sections have been created, set the section indexes
1229 // for any sections which haven't been done yet.
1230 shndx
= this->set_section_indexes(shndx
);
1232 // Create the section table header.
1233 this->create_shdrs(shstrtab_section
, &off
);
1235 // If there are no sections which require postprocessing, we can
1236 // handle the section names now, and avoid a resize later.
1237 if (!this->any_postprocessing_sections_
)
1238 off
= this->set_section_offsets(off
,
1239 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1241 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1243 // Now we know exactly where everything goes in the output file
1244 // (except for non-allocated sections which require postprocessing).
1245 Output_data::layout_complete();
1247 this->output_file_size_
= off
;
1252 // Create a note header following the format defined in the ELF ABI.
1253 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1254 // descriptor. ALLOCATE is true if the section should be allocated in
1255 // memory. This returns the new note section. It sets
1256 // *TRAILING_PADDING to the number of trailing zero bytes required.
1259 Layout::create_note(const char* name
, int note_type
, size_t descsz
,
1260 bool allocate
, size_t* trailing_padding
)
1262 // Authorities all agree that the values in a .note field should
1263 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1264 // they differ on what the alignment is for 64-bit binaries.
1265 // The GABI says unambiguously they take 8-byte alignment:
1266 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1267 // Other documentation says alignment should always be 4 bytes:
1268 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1269 // GNU ld and GNU readelf both support the latter (at least as of
1270 // version 2.16.91), and glibc always generates the latter for
1271 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1273 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1274 const int size
= parameters
->target().get_size();
1276 const int size
= 32;
1279 // The contents of the .note section.
1280 size_t namesz
= strlen(name
) + 1;
1281 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1282 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1284 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1286 unsigned char* buffer
= new unsigned char[notehdrsz
];
1287 memset(buffer
, 0, notehdrsz
);
1289 bool is_big_endian
= parameters
->target().is_big_endian();
1295 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1296 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1297 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1301 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1302 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1303 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1306 else if (size
== 64)
1310 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1311 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1312 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1316 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1317 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1318 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1324 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1326 const char* note_name
= this->namepool_
.add(".note", false, NULL
);
1327 elfcpp::Elf_Xword flags
= 0;
1329 flags
= elfcpp::SHF_ALLOC
;
1330 Output_section
* os
= this->make_output_section(note_name
,
1333 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1336 os
->add_output_section_data(posd
);
1338 *trailing_padding
= aligned_descsz
- descsz
;
1343 // For an executable or shared library, create a note to record the
1344 // version of gold used to create the binary.
1347 Layout::create_gold_note()
1349 if (parameters
->options().relocatable())
1352 std::string desc
= std::string("gold ") + gold::get_version_string();
1354 size_t trailing_padding
;
1355 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1356 desc
.size(), false, &trailing_padding
);
1358 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1359 os
->add_output_section_data(posd
);
1361 if (trailing_padding
> 0)
1363 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1364 os
->add_output_section_data(posd
);
1368 // Record whether the stack should be executable. This can be set
1369 // from the command line using the -z execstack or -z noexecstack
1370 // options. Otherwise, if any input file has a .note.GNU-stack
1371 // section with the SHF_EXECINSTR flag set, the stack should be
1372 // executable. Otherwise, if at least one input file a
1373 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1374 // section, we use the target default for whether the stack should be
1375 // executable. Otherwise, we don't generate a stack note. When
1376 // generating a object file, we create a .note.GNU-stack section with
1377 // the appropriate marking. When generating an executable or shared
1378 // library, we create a PT_GNU_STACK segment.
1381 Layout::create_executable_stack_info(const Target
* target
)
1383 bool is_stack_executable
;
1384 if (this->options_
.is_execstack_set())
1385 is_stack_executable
= this->options_
.is_stack_executable();
1386 else if (!this->input_with_gnu_stack_note_
)
1390 if (this->input_requires_executable_stack_
)
1391 is_stack_executable
= true;
1392 else if (this->input_without_gnu_stack_note_
)
1393 is_stack_executable
= target
->is_default_stack_executable();
1395 is_stack_executable
= false;
1398 if (parameters
->options().relocatable())
1400 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1401 elfcpp::Elf_Xword flags
= 0;
1402 if (is_stack_executable
)
1403 flags
|= elfcpp::SHF_EXECINSTR
;
1404 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
);
1408 if (this->script_options_
->saw_phdrs_clause())
1410 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1411 if (is_stack_executable
)
1412 flags
|= elfcpp::PF_X
;
1413 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1417 // If --build-id was used, set up the build ID note.
1420 Layout::create_build_id()
1422 if (!parameters
->options().user_set_build_id())
1425 const char* style
= parameters
->options().build_id();
1426 if (strcmp(style
, "none") == 0)
1429 // Set DESCSZ to the size of the note descriptor. When possible,
1430 // set DESC to the note descriptor contents.
1433 if (strcmp(style
, "md5") == 0)
1435 else if (strcmp(style
, "sha1") == 0)
1437 else if (strcmp(style
, "uuid") == 0)
1439 const size_t uuidsz
= 128 / 8;
1441 char buffer
[uuidsz
];
1442 memset(buffer
, 0, uuidsz
);
1444 int descriptor
= ::open("/dev/urandom", O_RDONLY
);
1446 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1450 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1451 ::close(descriptor
);
1453 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1454 else if (static_cast<size_t>(got
) != uuidsz
)
1455 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1459 desc
.assign(buffer
, uuidsz
);
1462 else if (strncmp(style
, "0x", 2) == 0)
1465 const char* p
= style
+ 2;
1468 if (hex_p(p
[0]) && hex_p(p
[1]))
1470 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1474 else if (*p
== '-' || *p
== ':')
1477 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1480 descsz
= desc
.size();
1483 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1486 size_t trailing_padding
;
1487 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1488 descsz
, true, &trailing_padding
);
1492 // We know the value already, so we fill it in now.
1493 gold_assert(desc
.size() == descsz
);
1495 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1496 os
->add_output_section_data(posd
);
1498 if (trailing_padding
!= 0)
1500 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1501 os
->add_output_section_data(posd
);
1506 // We need to compute a checksum after we have completed the
1508 gold_assert(trailing_padding
== 0);
1509 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
1510 os
->add_output_section_data(this->build_id_note_
);
1511 os
->set_after_input_sections();
1515 // Return whether SEG1 should be before SEG2 in the output file. This
1516 // is based entirely on the segment type and flags. When this is
1517 // called the segment addresses has normally not yet been set.
1520 Layout::segment_precedes(const Output_segment
* seg1
,
1521 const Output_segment
* seg2
)
1523 elfcpp::Elf_Word type1
= seg1
->type();
1524 elfcpp::Elf_Word type2
= seg2
->type();
1526 // The single PT_PHDR segment is required to precede any loadable
1527 // segment. We simply make it always first.
1528 if (type1
== elfcpp::PT_PHDR
)
1530 gold_assert(type2
!= elfcpp::PT_PHDR
);
1533 if (type2
== elfcpp::PT_PHDR
)
1536 // The single PT_INTERP segment is required to precede any loadable
1537 // segment. We simply make it always second.
1538 if (type1
== elfcpp::PT_INTERP
)
1540 gold_assert(type2
!= elfcpp::PT_INTERP
);
1543 if (type2
== elfcpp::PT_INTERP
)
1546 // We then put PT_LOAD segments before any other segments.
1547 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
1549 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
1552 // We put the PT_TLS segment last except for the PT_GNU_RELRO
1553 // segment, because that is where the dynamic linker expects to find
1554 // it (this is just for efficiency; other positions would also work
1556 if (type1
== elfcpp::PT_TLS
1557 && type2
!= elfcpp::PT_TLS
1558 && type2
!= elfcpp::PT_GNU_RELRO
)
1560 if (type2
== elfcpp::PT_TLS
1561 && type1
!= elfcpp::PT_TLS
1562 && type1
!= elfcpp::PT_GNU_RELRO
)
1565 // We put the PT_GNU_RELRO segment last, because that is where the
1566 // dynamic linker expects to find it (as with PT_TLS, this is just
1568 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
1570 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
1573 const elfcpp::Elf_Word flags1
= seg1
->flags();
1574 const elfcpp::Elf_Word flags2
= seg2
->flags();
1576 // The order of non-PT_LOAD segments is unimportant. We simply sort
1577 // by the numeric segment type and flags values. There should not
1578 // be more than one segment with the same type and flags.
1579 if (type1
!= elfcpp::PT_LOAD
)
1582 return type1
< type2
;
1583 gold_assert(flags1
!= flags2
);
1584 return flags1
< flags2
;
1587 // If the addresses are set already, sort by load address.
1588 if (seg1
->are_addresses_set())
1590 if (!seg2
->are_addresses_set())
1593 unsigned int section_count1
= seg1
->output_section_count();
1594 unsigned int section_count2
= seg2
->output_section_count();
1595 if (section_count1
== 0 && section_count2
> 0)
1597 if (section_count1
> 0 && section_count2
== 0)
1600 uint64_t paddr1
= seg1
->first_section_load_address();
1601 uint64_t paddr2
= seg2
->first_section_load_address();
1602 if (paddr1
!= paddr2
)
1603 return paddr1
< paddr2
;
1605 else if (seg2
->are_addresses_set())
1608 // We sort PT_LOAD segments based on the flags. Readonly segments
1609 // come before writable segments. Then writable segments with data
1610 // come before writable segments without data. Then executable
1611 // segments come before non-executable segments. Then the unlikely
1612 // case of a non-readable segment comes before the normal case of a
1613 // readable segment. If there are multiple segments with the same
1614 // type and flags, we require that the address be set, and we sort
1615 // by virtual address and then physical address.
1616 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
1617 return (flags1
& elfcpp::PF_W
) == 0;
1618 if ((flags1
& elfcpp::PF_W
) != 0
1619 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
1620 return seg1
->has_any_data_sections();
1621 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
1622 return (flags1
& elfcpp::PF_X
) != 0;
1623 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
1624 return (flags1
& elfcpp::PF_R
) == 0;
1626 // We shouldn't get here--we shouldn't create segments which we
1627 // can't distinguish.
1631 // Set the file offsets of all the segments, and all the sections they
1632 // contain. They have all been created. LOAD_SEG must be be laid out
1633 // first. Return the offset of the data to follow.
1636 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
1637 unsigned int *pshndx
)
1639 // Sort them into the final order.
1640 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
1641 Layout::Compare_segments());
1643 // Find the PT_LOAD segments, and set their addresses and offsets
1644 // and their section's addresses and offsets.
1646 if (this->options_
.user_set_Ttext())
1647 addr
= this->options_
.Ttext();
1648 else if (parameters
->options().shared())
1651 addr
= target
->default_text_segment_address();
1654 // If LOAD_SEG is NULL, then the file header and segment headers
1655 // will not be loadable. But they still need to be at offset 0 in
1656 // the file. Set their offsets now.
1657 if (load_seg
== NULL
)
1659 for (Data_list::iterator p
= this->special_output_list_
.begin();
1660 p
!= this->special_output_list_
.end();
1663 off
= align_address(off
, (*p
)->addralign());
1664 (*p
)->set_address_and_file_offset(0, off
);
1665 off
+= (*p
)->data_size();
1669 bool was_readonly
= false;
1670 for (Segment_list::iterator p
= this->segment_list_
.begin();
1671 p
!= this->segment_list_
.end();
1674 if ((*p
)->type() == elfcpp::PT_LOAD
)
1676 if (load_seg
!= NULL
&& load_seg
!= *p
)
1680 bool are_addresses_set
= (*p
)->are_addresses_set();
1681 if (are_addresses_set
)
1683 // When it comes to setting file offsets, we care about
1684 // the physical address.
1685 addr
= (*p
)->paddr();
1687 else if (this->options_
.user_set_Tdata()
1688 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1689 && (!this->options_
.user_set_Tbss()
1690 || (*p
)->has_any_data_sections()))
1692 addr
= this->options_
.Tdata();
1693 are_addresses_set
= true;
1695 else if (this->options_
.user_set_Tbss()
1696 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1697 && !(*p
)->has_any_data_sections())
1699 addr
= this->options_
.Tbss();
1700 are_addresses_set
= true;
1703 uint64_t orig_addr
= addr
;
1704 uint64_t orig_off
= off
;
1706 uint64_t aligned_addr
= 0;
1707 uint64_t abi_pagesize
= target
->abi_pagesize();
1709 // FIXME: This should depend on the -n and -N options.
1710 (*p
)->set_minimum_p_align(target
->common_pagesize());
1712 if (are_addresses_set
)
1714 // Adjust the file offset to the same address modulo the
1716 uint64_t unsigned_off
= off
;
1717 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
1718 | (addr
& (abi_pagesize
- 1)));
1719 if (aligned_off
< unsigned_off
)
1720 aligned_off
+= abi_pagesize
;
1725 // If the last segment was readonly, and this one is
1726 // not, then skip the address forward one page,
1727 // maintaining the same position within the page. This
1728 // lets us store both segments overlapping on a single
1729 // page in the file, but the loader will put them on
1730 // different pages in memory.
1732 addr
= align_address(addr
, (*p
)->maximum_alignment());
1733 aligned_addr
= addr
;
1735 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
1737 if ((addr
& (abi_pagesize
- 1)) != 0)
1738 addr
= addr
+ abi_pagesize
;
1741 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1744 unsigned int shndx_hold
= *pshndx
;
1745 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
1748 // Now that we know the size of this segment, we may be able
1749 // to save a page in memory, at the cost of wasting some
1750 // file space, by instead aligning to the start of a new
1751 // page. Here we use the real machine page size rather than
1752 // the ABI mandated page size.
1754 if (!are_addresses_set
&& aligned_addr
!= addr
)
1756 uint64_t common_pagesize
= target
->common_pagesize();
1757 uint64_t first_off
= (common_pagesize
1759 & (common_pagesize
- 1)));
1760 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
1763 && ((aligned_addr
& ~ (common_pagesize
- 1))
1764 != (new_addr
& ~ (common_pagesize
- 1)))
1765 && first_off
+ last_off
<= common_pagesize
)
1767 *pshndx
= shndx_hold
;
1768 addr
= align_address(aligned_addr
, common_pagesize
);
1769 addr
= align_address(addr
, (*p
)->maximum_alignment());
1770 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1771 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
1778 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
1779 was_readonly
= true;
1783 // Handle the non-PT_LOAD segments, setting their offsets from their
1784 // section's offsets.
1785 for (Segment_list::iterator p
= this->segment_list_
.begin();
1786 p
!= this->segment_list_
.end();
1789 if ((*p
)->type() != elfcpp::PT_LOAD
)
1793 // Set the TLS offsets for each section in the PT_TLS segment.
1794 if (this->tls_segment_
!= NULL
)
1795 this->tls_segment_
->set_tls_offsets();
1800 // Set the offsets of all the allocated sections when doing a
1801 // relocatable link. This does the same jobs as set_segment_offsets,
1802 // only for a relocatable link.
1805 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
1806 unsigned int *pshndx
)
1810 file_header
->set_address_and_file_offset(0, 0);
1811 off
+= file_header
->data_size();
1813 for (Section_list::iterator p
= this->section_list_
.begin();
1814 p
!= this->section_list_
.end();
1817 // We skip unallocated sections here, except that group sections
1818 // have to come first.
1819 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
1820 && (*p
)->type() != elfcpp::SHT_GROUP
)
1823 off
= align_address(off
, (*p
)->addralign());
1825 // The linker script might have set the address.
1826 if (!(*p
)->is_address_valid())
1827 (*p
)->set_address(0);
1828 (*p
)->set_file_offset(off
);
1829 (*p
)->finalize_data_size();
1830 off
+= (*p
)->data_size();
1832 (*p
)->set_out_shndx(*pshndx
);
1839 // Set the file offset of all the sections not associated with a
1843 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
1845 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1846 p
!= this->unattached_section_list_
.end();
1849 // The symtab section is handled in create_symtab_sections.
1850 if (*p
== this->symtab_section_
)
1853 // If we've already set the data size, don't set it again.
1854 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
1857 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1858 && (*p
)->requires_postprocessing())
1860 (*p
)->create_postprocessing_buffer();
1861 this->any_postprocessing_sections_
= true;
1864 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1865 && (*p
)->after_input_sections())
1867 else if (pass
== POSTPROCESSING_SECTIONS_PASS
1868 && (!(*p
)->after_input_sections()
1869 || (*p
)->type() == elfcpp::SHT_STRTAB
))
1871 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1872 && (!(*p
)->after_input_sections()
1873 || (*p
)->type() != elfcpp::SHT_STRTAB
))
1876 off
= align_address(off
, (*p
)->addralign());
1877 (*p
)->set_file_offset(off
);
1878 (*p
)->finalize_data_size();
1879 off
+= (*p
)->data_size();
1881 // At this point the name must be set.
1882 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
1883 this->namepool_
.add((*p
)->name(), false, NULL
);
1888 // Set the section indexes of all the sections not associated with a
1892 Layout::set_section_indexes(unsigned int shndx
)
1894 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1895 p
!= this->unattached_section_list_
.end();
1898 if (!(*p
)->has_out_shndx())
1900 (*p
)->set_out_shndx(shndx
);
1907 // Set the section addresses according to the linker script. This is
1908 // only called when we see a SECTIONS clause. This returns the
1909 // program segment which should hold the file header and segment
1910 // headers, if any. It will return NULL if they should not be in a
1914 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
1916 Script_sections
* ss
= this->script_options_
->script_sections();
1917 gold_assert(ss
->saw_sections_clause());
1919 // Place each orphaned output section in the script.
1920 for (Section_list::iterator p
= this->section_list_
.begin();
1921 p
!= this->section_list_
.end();
1924 if (!(*p
)->found_in_sections_clause())
1925 ss
->place_orphan(*p
);
1928 return this->script_options_
->set_section_addresses(symtab
, this);
1931 // Count the local symbols in the regular symbol table and the dynamic
1932 // symbol table, and build the respective string pools.
1935 Layout::count_local_symbols(const Task
* task
,
1936 const Input_objects
* input_objects
)
1938 // First, figure out an upper bound on the number of symbols we'll
1939 // be inserting into each pool. This helps us create the pools with
1940 // the right size, to avoid unnecessary hashtable resizing.
1941 unsigned int symbol_count
= 0;
1942 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1943 p
!= input_objects
->relobj_end();
1945 symbol_count
+= (*p
)->local_symbol_count();
1947 // Go from "upper bound" to "estimate." We overcount for two
1948 // reasons: we double-count symbols that occur in more than one
1949 // object file, and we count symbols that are dropped from the
1950 // output. Add it all together and assume we overcount by 100%.
1953 // We assume all symbols will go into both the sympool and dynpool.
1954 this->sympool_
.reserve(symbol_count
);
1955 this->dynpool_
.reserve(symbol_count
);
1957 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1958 p
!= input_objects
->relobj_end();
1961 Task_lock_obj
<Object
> tlo(task
, *p
);
1962 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
1966 // Create the symbol table sections. Here we also set the final
1967 // values of the symbols. At this point all the loadable sections are
1968 // fully laid out. SHNUM is the number of sections so far.
1971 Layout::create_symtab_sections(const Input_objects
* input_objects
,
1972 Symbol_table
* symtab
,
1978 if (parameters
->target().get_size() == 32)
1980 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
1983 else if (parameters
->target().get_size() == 64)
1985 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
1992 off
= align_address(off
, align
);
1993 off_t startoff
= off
;
1995 // Save space for the dummy symbol at the start of the section. We
1996 // never bother to write this out--it will just be left as zero.
1998 unsigned int local_symbol_index
= 1;
2000 // Add STT_SECTION symbols for each Output section which needs one.
2001 for (Section_list::iterator p
= this->section_list_
.begin();
2002 p
!= this->section_list_
.end();
2005 if (!(*p
)->needs_symtab_index())
2006 (*p
)->set_symtab_index(-1U);
2009 (*p
)->set_symtab_index(local_symbol_index
);
2010 ++local_symbol_index
;
2015 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2016 p
!= input_objects
->relobj_end();
2019 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2021 off
+= (index
- local_symbol_index
) * symsize
;
2022 local_symbol_index
= index
;
2025 unsigned int local_symcount
= local_symbol_index
;
2026 gold_assert(local_symcount
* symsize
== off
- startoff
);
2029 size_t dyn_global_index
;
2031 if (this->dynsym_section_
== NULL
)
2034 dyn_global_index
= 0;
2039 dyn_global_index
= this->dynsym_section_
->info();
2040 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2041 dynoff
= this->dynsym_section_
->offset() + locsize
;
2042 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2043 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2044 == this->dynsym_section_
->data_size() - locsize
);
2047 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2048 &this->sympool_
, &local_symcount
);
2050 if (!parameters
->options().strip_all())
2052 this->sympool_
.set_string_offsets();
2054 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2055 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2058 this->symtab_section_
= osymtab
;
2060 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2063 osymtab
->add_output_section_data(pos
);
2065 // We generate a .symtab_shndx section if we have more than
2066 // SHN_LORESERVE sections. Technically it is possible that we
2067 // don't need one, because it is possible that there are no
2068 // symbols in any of sections with indexes larger than
2069 // SHN_LORESERVE. That is probably unusual, though, and it is
2070 // easier to always create one than to compute section indexes
2071 // twice (once here, once when writing out the symbols).
2072 if (shnum
>= elfcpp::SHN_LORESERVE
)
2074 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2076 Output_section
* osymtab_xindex
=
2077 this->make_output_section(symtab_xindex_name
,
2078 elfcpp::SHT_SYMTAB_SHNDX
, 0);
2080 size_t symcount
= (off
- startoff
) / symsize
;
2081 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2083 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2085 osymtab_xindex
->set_link_section(osymtab
);
2086 osymtab_xindex
->set_addralign(4);
2087 osymtab_xindex
->set_entsize(4);
2089 osymtab_xindex
->set_after_input_sections();
2091 // This tells the driver code to wait until the symbol table
2092 // has written out before writing out the postprocessing
2093 // sections, including the .symtab_shndx section.
2094 this->any_postprocessing_sections_
= true;
2097 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2098 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2102 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2103 ostrtab
->add_output_section_data(pstr
);
2105 osymtab
->set_file_offset(startoff
);
2106 osymtab
->finalize_data_size();
2107 osymtab
->set_link_section(ostrtab
);
2108 osymtab
->set_info(local_symcount
);
2109 osymtab
->set_entsize(symsize
);
2115 // Create the .shstrtab section, which holds the names of the
2116 // sections. At the time this is called, we have created all the
2117 // output sections except .shstrtab itself.
2120 Layout::create_shstrtab()
2122 // FIXME: We don't need to create a .shstrtab section if we are
2123 // stripping everything.
2125 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2127 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
2129 // We can't write out this section until we've set all the section
2130 // names, and we don't set the names of compressed output sections
2131 // until relocations are complete.
2132 os
->set_after_input_sections();
2134 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2135 os
->add_output_section_data(posd
);
2140 // Create the section headers. SIZE is 32 or 64. OFF is the file
2144 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2146 Output_section_headers
* oshdrs
;
2147 oshdrs
= new Output_section_headers(this,
2148 &this->segment_list_
,
2149 &this->section_list_
,
2150 &this->unattached_section_list_
,
2153 off_t off
= align_address(*poff
, oshdrs
->addralign());
2154 oshdrs
->set_address_and_file_offset(0, off
);
2155 off
+= oshdrs
->data_size();
2157 this->section_headers_
= oshdrs
;
2160 // Count the allocated sections.
2163 Layout::allocated_output_section_count() const
2165 size_t section_count
= 0;
2166 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2167 p
!= this->segment_list_
.end();
2169 section_count
+= (*p
)->output_section_count();
2170 return section_count
;
2173 // Create the dynamic symbol table.
2176 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2177 Symbol_table
* symtab
,
2178 Output_section
**pdynstr
,
2179 unsigned int* plocal_dynamic_count
,
2180 std::vector
<Symbol
*>* pdynamic_symbols
,
2181 Versions
* pversions
)
2183 // Count all the symbols in the dynamic symbol table, and set the
2184 // dynamic symbol indexes.
2186 // Skip symbol 0, which is always all zeroes.
2187 unsigned int index
= 1;
2189 // Add STT_SECTION symbols for each Output section which needs one.
2190 for (Section_list::iterator p
= this->section_list_
.begin();
2191 p
!= this->section_list_
.end();
2194 if (!(*p
)->needs_dynsym_index())
2195 (*p
)->set_dynsym_index(-1U);
2198 (*p
)->set_dynsym_index(index
);
2203 // Count the local symbols that need to go in the dynamic symbol table,
2204 // and set the dynamic symbol indexes.
2205 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2206 p
!= input_objects
->relobj_end();
2209 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2213 unsigned int local_symcount
= index
;
2214 *plocal_dynamic_count
= local_symcount
;
2216 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2217 &this->dynpool_
, pversions
);
2221 const int size
= parameters
->target().get_size();
2224 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2227 else if (size
== 64)
2229 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2235 // Create the dynamic symbol table section.
2237 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2242 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2245 dynsym
->add_output_section_data(odata
);
2247 dynsym
->set_info(local_symcount
);
2248 dynsym
->set_entsize(symsize
);
2249 dynsym
->set_addralign(align
);
2251 this->dynsym_section_
= dynsym
;
2253 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2254 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2255 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2257 // If there are more than SHN_LORESERVE allocated sections, we
2258 // create a .dynsym_shndx section. It is possible that we don't
2259 // need one, because it is possible that there are no dynamic
2260 // symbols in any of the sections with indexes larger than
2261 // SHN_LORESERVE. This is probably unusual, though, and at this
2262 // time we don't know the actual section indexes so it is
2263 // inconvenient to check.
2264 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2266 Output_section
* dynsym_xindex
=
2267 this->choose_output_section(NULL
, ".dynsym_shndx",
2268 elfcpp::SHT_SYMTAB_SHNDX
,
2272 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2274 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2276 dynsym_xindex
->set_link_section(dynsym
);
2277 dynsym_xindex
->set_addralign(4);
2278 dynsym_xindex
->set_entsize(4);
2280 dynsym_xindex
->set_after_input_sections();
2282 // This tells the driver code to wait until the symbol table has
2283 // written out before writing out the postprocessing sections,
2284 // including the .dynsym_shndx section.
2285 this->any_postprocessing_sections_
= true;
2288 // Create the dynamic string table section.
2290 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2295 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2296 dynstr
->add_output_section_data(strdata
);
2298 dynsym
->set_link_section(dynstr
);
2299 this->dynamic_section_
->set_link_section(dynstr
);
2301 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2302 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2306 // Create the hash tables.
2308 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2309 || strcmp(parameters
->options().hash_style(), "both") == 0)
2311 unsigned char* phash
;
2312 unsigned int hashlen
;
2313 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2316 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2321 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2325 hashsec
->add_output_section_data(hashdata
);
2327 hashsec
->set_link_section(dynsym
);
2328 hashsec
->set_entsize(4);
2330 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2333 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2334 || strcmp(parameters
->options().hash_style(), "both") == 0)
2336 unsigned char* phash
;
2337 unsigned int hashlen
;
2338 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2341 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2342 elfcpp::SHT_GNU_HASH
,
2346 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2350 hashsec
->add_output_section_data(hashdata
);
2352 hashsec
->set_link_section(dynsym
);
2353 hashsec
->set_entsize(4);
2355 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
2359 // Assign offsets to each local portion of the dynamic symbol table.
2362 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
2364 Output_section
* dynsym
= this->dynsym_section_
;
2365 gold_assert(dynsym
!= NULL
);
2367 off_t off
= dynsym
->offset();
2369 // Skip the dummy symbol at the start of the section.
2370 off
+= dynsym
->entsize();
2372 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2373 p
!= input_objects
->relobj_end();
2376 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
2377 off
+= count
* dynsym
->entsize();
2381 // Create the version sections.
2384 Layout::create_version_sections(const Versions
* versions
,
2385 const Symbol_table
* symtab
,
2386 unsigned int local_symcount
,
2387 const std::vector
<Symbol
*>& dynamic_symbols
,
2388 const Output_section
* dynstr
)
2390 if (!versions
->any_defs() && !versions
->any_needs())
2393 switch (parameters
->size_and_endianness())
2395 #ifdef HAVE_TARGET_32_LITTLE
2396 case Parameters::TARGET_32_LITTLE
:
2397 this->sized_create_version_sections
<32, false>(versions
, symtab
,
2399 dynamic_symbols
, dynstr
);
2402 #ifdef HAVE_TARGET_32_BIG
2403 case Parameters::TARGET_32_BIG
:
2404 this->sized_create_version_sections
<32, true>(versions
, symtab
,
2406 dynamic_symbols
, dynstr
);
2409 #ifdef HAVE_TARGET_64_LITTLE
2410 case Parameters::TARGET_64_LITTLE
:
2411 this->sized_create_version_sections
<64, false>(versions
, symtab
,
2413 dynamic_symbols
, dynstr
);
2416 #ifdef HAVE_TARGET_64_BIG
2417 case Parameters::TARGET_64_BIG
:
2418 this->sized_create_version_sections
<64, true>(versions
, symtab
,
2420 dynamic_symbols
, dynstr
);
2428 // Create the version sections, sized version.
2430 template<int size
, bool big_endian
>
2432 Layout::sized_create_version_sections(
2433 const Versions
* versions
,
2434 const Symbol_table
* symtab
,
2435 unsigned int local_symcount
,
2436 const std::vector
<Symbol
*>& dynamic_symbols
,
2437 const Output_section
* dynstr
)
2439 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
2440 elfcpp::SHT_GNU_versym
,
2444 unsigned char* vbuf
;
2446 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
2451 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
2454 vsec
->add_output_section_data(vdata
);
2455 vsec
->set_entsize(2);
2456 vsec
->set_link_section(this->dynsym_section_
);
2458 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2459 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
2461 if (versions
->any_defs())
2463 Output_section
* vdsec
;
2464 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
2465 elfcpp::SHT_GNU_verdef
,
2469 unsigned char* vdbuf
;
2470 unsigned int vdsize
;
2471 unsigned int vdentries
;
2472 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
2473 &vdsize
, &vdentries
);
2475 Output_section_data
* vddata
=
2476 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
2478 vdsec
->add_output_section_data(vddata
);
2479 vdsec
->set_link_section(dynstr
);
2480 vdsec
->set_info(vdentries
);
2482 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
2483 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
2486 if (versions
->any_needs())
2488 Output_section
* vnsec
;
2489 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
2490 elfcpp::SHT_GNU_verneed
,
2494 unsigned char* vnbuf
;
2495 unsigned int vnsize
;
2496 unsigned int vnentries
;
2497 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
2501 Output_section_data
* vndata
=
2502 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
2504 vnsec
->add_output_section_data(vndata
);
2505 vnsec
->set_link_section(dynstr
);
2506 vnsec
->set_info(vnentries
);
2508 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
2509 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
2513 // Create the .interp section and PT_INTERP segment.
2516 Layout::create_interp(const Target
* target
)
2518 const char* interp
= this->options_
.dynamic_linker();
2521 interp
= target
->dynamic_linker();
2522 gold_assert(interp
!= NULL
);
2525 size_t len
= strlen(interp
) + 1;
2527 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
2529 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
2530 elfcpp::SHT_PROGBITS
,
2533 osec
->add_output_section_data(odata
);
2535 if (!this->script_options_
->saw_phdrs_clause())
2537 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
2539 oseg
->add_output_section(osec
, elfcpp::PF_R
);
2543 // Finish the .dynamic section and PT_DYNAMIC segment.
2546 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
2547 const Symbol_table
* symtab
)
2549 if (!this->script_options_
->saw_phdrs_clause())
2551 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
2554 oseg
->add_output_section(this->dynamic_section_
,
2555 elfcpp::PF_R
| elfcpp::PF_W
);
2558 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2560 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
2561 p
!= input_objects
->dynobj_end();
2564 // FIXME: Handle --as-needed.
2565 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
2568 if (parameters
->options().shared())
2570 const char* soname
= this->options_
.soname();
2572 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
2575 // FIXME: Support --init and --fini.
2576 Symbol
* sym
= symtab
->lookup("_init");
2577 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2578 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
2580 sym
= symtab
->lookup("_fini");
2581 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2582 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
2584 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2586 // Add a DT_RPATH entry if needed.
2587 const General_options::Dir_list
& rpath(this->options_
.rpath());
2590 std::string rpath_val
;
2591 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
2595 if (rpath_val
.empty())
2596 rpath_val
= p
->name();
2599 // Eliminate duplicates.
2600 General_options::Dir_list::const_iterator q
;
2601 for (q
= rpath
.begin(); q
!= p
; ++q
)
2602 if (q
->name() == p
->name())
2607 rpath_val
+= p
->name();
2612 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
2613 if (parameters
->options().enable_new_dtags())
2614 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
2617 // Look for text segments that have dynamic relocations.
2618 bool have_textrel
= false;
2619 if (!this->script_options_
->saw_sections_clause())
2621 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2622 p
!= this->segment_list_
.end();
2625 if (((*p
)->flags() & elfcpp::PF_W
) == 0
2626 && (*p
)->dynamic_reloc_count() > 0)
2628 have_textrel
= true;
2635 // We don't know the section -> segment mapping, so we are
2636 // conservative and just look for readonly sections with
2637 // relocations. If those sections wind up in writable segments,
2638 // then we have created an unnecessary DT_TEXTREL entry.
2639 for (Section_list::const_iterator p
= this->section_list_
.begin();
2640 p
!= this->section_list_
.end();
2643 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
2644 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
2645 && ((*p
)->dynamic_reloc_count() > 0))
2647 have_textrel
= true;
2653 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2654 // post-link tools can easily modify these flags if desired.
2655 unsigned int flags
= 0;
2658 // Add a DT_TEXTREL for compatibility with older loaders.
2659 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
2660 flags
|= elfcpp::DF_TEXTREL
;
2662 if (parameters
->options().shared() && this->has_static_tls())
2663 flags
|= elfcpp::DF_STATIC_TLS
;
2664 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
2667 if (parameters
->options().initfirst())
2668 flags
|= elfcpp::DF_1_INITFIRST
;
2669 if (parameters
->options().interpose())
2670 flags
|= elfcpp::DF_1_INTERPOSE
;
2671 if (parameters
->options().loadfltr())
2672 flags
|= elfcpp::DF_1_LOADFLTR
;
2673 if (parameters
->options().nodefaultlib())
2674 flags
|= elfcpp::DF_1_NODEFLIB
;
2675 if (parameters
->options().nodelete())
2676 flags
|= elfcpp::DF_1_NODELETE
;
2677 if (parameters
->options().nodlopen())
2678 flags
|= elfcpp::DF_1_NOOPEN
;
2679 if (parameters
->options().nodump())
2680 flags
|= elfcpp::DF_1_NODUMP
;
2681 if (!parameters
->options().shared())
2682 flags
&= ~(elfcpp::DF_1_INITFIRST
2683 | elfcpp::DF_1_NODELETE
2684 | elfcpp::DF_1_NOOPEN
);
2686 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
2689 // The mapping of .gnu.linkonce section names to real section names.
2691 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2692 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
2694 MAPPING_INIT("d.rel.ro.local", ".data.rel.ro.local"), // Before "d.rel.ro".
2695 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Before "d".
2696 MAPPING_INIT("t", ".text"),
2697 MAPPING_INIT("r", ".rodata"),
2698 MAPPING_INIT("d", ".data"),
2699 MAPPING_INIT("b", ".bss"),
2700 MAPPING_INIT("s", ".sdata"),
2701 MAPPING_INIT("sb", ".sbss"),
2702 MAPPING_INIT("s2", ".sdata2"),
2703 MAPPING_INIT("sb2", ".sbss2"),
2704 MAPPING_INIT("wi", ".debug_info"),
2705 MAPPING_INIT("td", ".tdata"),
2706 MAPPING_INIT("tb", ".tbss"),
2707 MAPPING_INIT("lr", ".lrodata"),
2708 MAPPING_INIT("l", ".ldata"),
2709 MAPPING_INIT("lb", ".lbss"),
2713 const int Layout::linkonce_mapping_count
=
2714 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
2716 // Return the name of the output section to use for a .gnu.linkonce
2717 // section. This is based on the default ELF linker script of the old
2718 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2719 // to ".text". Set *PLEN to the length of the name. *PLEN is
2720 // initialized to the length of NAME.
2723 Layout::linkonce_output_name(const char* name
, size_t *plen
)
2725 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
2729 const Linkonce_mapping
* plm
= linkonce_mapping
;
2730 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
2732 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
2741 // Choose the output section name to use given an input section name.
2742 // Set *PLEN to the length of the name. *PLEN is initialized to the
2746 Layout::output_section_name(const char* name
, size_t* plen
)
2748 if (Layout::is_linkonce(name
))
2750 // .gnu.linkonce sections are laid out as though they were named
2751 // for the sections are placed into.
2752 return Layout::linkonce_output_name(name
, plen
);
2755 // gcc 4.3 generates the following sorts of section names when it
2756 // needs a section name specific to a function:
2762 // .data.rel.local.FN
2764 // .data.rel.ro.local.FN
2771 // The GNU linker maps all of those to the part before the .FN,
2772 // except that .data.rel.local.FN is mapped to .data, and
2773 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2774 // beginning with .data.rel.ro.local are grouped together.
2776 // For an anonymous namespace, the string FN can contain a '.'.
2778 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2779 // GNU linker maps to .rodata.
2781 // The .data.rel.ro sections enable a security feature triggered by
2782 // the -z relro option. Section which need to be relocated at
2783 // program startup time but which may be readonly after startup are
2784 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2785 // segment. The dynamic linker will make that segment writable,
2786 // perform relocations, and then make it read-only. FIXME: We do
2787 // not yet implement this optimization.
2789 // It is hard to handle this in a principled way.
2791 // These are the rules we follow:
2793 // If the section name has no initial '.', or no dot other than an
2794 // initial '.', we use the name unchanged (i.e., "mysection" and
2795 // ".text" are unchanged).
2797 // If the name starts with ".data.rel.ro.local" we use
2798 // ".data.rel.ro.local".
2800 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2802 // Otherwise, we drop the second '.' and everything that comes after
2803 // it (i.e., ".text.XXX" becomes ".text").
2805 const char* s
= name
;
2809 const char* sdot
= strchr(s
, '.');
2813 const char* const data_rel_ro_local
= ".data.rel.ro.local";
2814 if (strncmp(name
, data_rel_ro_local
, strlen(data_rel_ro_local
)) == 0)
2816 *plen
= strlen(data_rel_ro_local
);
2817 return data_rel_ro_local
;
2820 const char* const data_rel_ro
= ".data.rel.ro";
2821 if (strncmp(name
, data_rel_ro
, strlen(data_rel_ro
)) == 0)
2823 *plen
= strlen(data_rel_ro
);
2827 *plen
= sdot
- name
;
2831 // Record the signature of a comdat section, and return whether to
2832 // include it in the link. If GROUP is true, this is a regular
2833 // section group. If GROUP is false, this is a group signature
2834 // derived from the name of a linkonce section. We want linkonce
2835 // signatures and group signatures to block each other, but we don't
2836 // want a linkonce signature to block another linkonce signature.
2839 Layout::add_comdat(Relobj
* object
, unsigned int shndx
,
2840 const std::string
& signature
, bool group
)
2842 Kept_section
kept(object
, shndx
, group
);
2843 std::pair
<Signatures::iterator
, bool> ins(
2844 this->signatures_
.insert(std::make_pair(signature
, kept
)));
2848 // This is the first time we've seen this signature.
2852 if (ins
.first
->second
.group_
)
2854 // We've already seen a real section group with this signature.
2859 // This is a real section group, and we've already seen a
2860 // linkonce section with this signature. Record that we've seen
2861 // a section group, and don't include this section group.
2862 ins
.first
->second
.group_
= true;
2867 // We've already seen a linkonce section and this is a linkonce
2868 // section. These don't block each other--this may be the same
2869 // symbol name with different section types.
2874 // Find the given comdat signature, and return the object and section
2875 // index of the kept group.
2877 Layout::find_kept_object(const std::string
& signature
,
2878 unsigned int* pshndx
) const
2880 Signatures::const_iterator p
= this->signatures_
.find(signature
);
2881 if (p
== this->signatures_
.end())
2884 *pshndx
= p
->second
.shndx_
;
2885 return p
->second
.object_
;
2888 // Store the allocated sections into the section list.
2891 Layout::get_allocated_sections(Section_list
* section_list
) const
2893 for (Section_list::const_iterator p
= this->section_list_
.begin();
2894 p
!= this->section_list_
.end();
2896 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
2897 section_list
->push_back(*p
);
2900 // Create an output segment.
2903 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2905 gold_assert(!parameters
->options().relocatable());
2906 Output_segment
* oseg
= new Output_segment(type
, flags
);
2907 this->segment_list_
.push_back(oseg
);
2911 // Write out the Output_sections. Most won't have anything to write,
2912 // since most of the data will come from input sections which are
2913 // handled elsewhere. But some Output_sections do have Output_data.
2916 Layout::write_output_sections(Output_file
* of
) const
2918 for (Section_list::const_iterator p
= this->section_list_
.begin();
2919 p
!= this->section_list_
.end();
2922 if (!(*p
)->after_input_sections())
2927 // Write out data not associated with a section or the symbol table.
2930 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
2932 if (!parameters
->options().strip_all())
2934 const Output_section
* symtab_section
= this->symtab_section_
;
2935 for (Section_list::const_iterator p
= this->section_list_
.begin();
2936 p
!= this->section_list_
.end();
2939 if ((*p
)->needs_symtab_index())
2941 gold_assert(symtab_section
!= NULL
);
2942 unsigned int index
= (*p
)->symtab_index();
2943 gold_assert(index
> 0 && index
!= -1U);
2944 off_t off
= (symtab_section
->offset()
2945 + index
* symtab_section
->entsize());
2946 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
2951 const Output_section
* dynsym_section
= this->dynsym_section_
;
2952 for (Section_list::const_iterator p
= this->section_list_
.begin();
2953 p
!= this->section_list_
.end();
2956 if ((*p
)->needs_dynsym_index())
2958 gold_assert(dynsym_section
!= NULL
);
2959 unsigned int index
= (*p
)->dynsym_index();
2960 gold_assert(index
> 0 && index
!= -1U);
2961 off_t off
= (dynsym_section
->offset()
2962 + index
* dynsym_section
->entsize());
2963 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
2967 // Write out the Output_data which are not in an Output_section.
2968 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
2969 p
!= this->special_output_list_
.end();
2974 // Write out the Output_sections which can only be written after the
2975 // input sections are complete.
2978 Layout::write_sections_after_input_sections(Output_file
* of
)
2980 // Determine the final section offsets, and thus the final output
2981 // file size. Note we finalize the .shstrab last, to allow the
2982 // after_input_section sections to modify their section-names before
2984 if (this->any_postprocessing_sections_
)
2986 off_t off
= this->output_file_size_
;
2987 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
2989 // Now that we've finalized the names, we can finalize the shstrab.
2991 this->set_section_offsets(off
,
2992 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
2994 if (off
> this->output_file_size_
)
2997 this->output_file_size_
= off
;
3001 for (Section_list::const_iterator p
= this->section_list_
.begin();
3002 p
!= this->section_list_
.end();
3005 if ((*p
)->after_input_sections())
3009 this->section_headers_
->write(of
);
3012 // If the build ID requires computing a checksum, do so here, and
3013 // write it out. We compute a checksum over the entire file because
3014 // that is simplest.
3017 Layout::write_build_id(Output_file
* of
) const
3019 if (this->build_id_note_
== NULL
)
3022 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3024 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3025 this->build_id_note_
->data_size());
3027 const char* style
= parameters
->options().build_id();
3028 if (strcmp(style
, "sha1") == 0)
3031 sha1_init_ctx(&ctx
);
3032 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3033 sha1_finish_ctx(&ctx
, ov
);
3035 else if (strcmp(style
, "md5") == 0)
3039 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3040 md5_finish_ctx(&ctx
, ov
);
3045 of
->write_output_view(this->build_id_note_
->offset(),
3046 this->build_id_note_
->data_size(),
3049 of
->free_input_view(0, this->output_file_size_
, iv
);
3052 // Write out a binary file. This is called after the link is
3053 // complete. IN is the temporary output file we used to generate the
3054 // ELF code. We simply walk through the segments, read them from
3055 // their file offset in IN, and write them to their load address in
3056 // the output file. FIXME: with a bit more work, we could support
3057 // S-records and/or Intel hex format here.
3060 Layout::write_binary(Output_file
* in
) const
3062 gold_assert(this->options_
.oformat_enum()
3063 == General_options::OBJECT_FORMAT_BINARY
);
3065 // Get the size of the binary file.
3066 uint64_t max_load_address
= 0;
3067 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3068 p
!= this->segment_list_
.end();
3071 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3073 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3074 if (max_paddr
> max_load_address
)
3075 max_load_address
= max_paddr
;
3079 Output_file
out(parameters
->options().output_file_name());
3080 out
.open(max_load_address
);
3082 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3083 p
!= this->segment_list_
.end();
3086 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3088 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3090 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3092 memcpy(vout
, vin
, (*p
)->filesz());
3093 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3094 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3101 // Print the output sections to the map file.
3104 Layout::print_to_mapfile(Mapfile
* mapfile
) const
3106 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3107 p
!= this->segment_list_
.end();
3109 (*p
)->print_sections_to_mapfile(mapfile
);
3112 // Print statistical information to stderr. This is used for --stats.
3115 Layout::print_stats() const
3117 this->namepool_
.print_stats("section name pool");
3118 this->sympool_
.print_stats("output symbol name pool");
3119 this->dynpool_
.print_stats("dynamic name pool");
3121 for (Section_list::const_iterator p
= this->section_list_
.begin();
3122 p
!= this->section_list_
.end();
3124 (*p
)->print_merge_stats();
3127 // Write_sections_task methods.
3129 // We can always run this task.
3132 Write_sections_task::is_runnable()
3137 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3141 Write_sections_task::locks(Task_locker
* tl
)
3143 tl
->add(this, this->output_sections_blocker_
);
3144 tl
->add(this, this->final_blocker_
);
3147 // Run the task--write out the data.
3150 Write_sections_task::run(Workqueue
*)
3152 this->layout_
->write_output_sections(this->of_
);
3155 // Write_data_task methods.
3157 // We can always run this task.
3160 Write_data_task::is_runnable()
3165 // We need to unlock FINAL_BLOCKER when finished.
3168 Write_data_task::locks(Task_locker
* tl
)
3170 tl
->add(this, this->final_blocker_
);
3173 // Run the task--write out the data.
3176 Write_data_task::run(Workqueue
*)
3178 this->layout_
->write_data(this->symtab_
, this->of_
);
3181 // Write_symbols_task methods.
3183 // We can always run this task.
3186 Write_symbols_task::is_runnable()
3191 // We need to unlock FINAL_BLOCKER when finished.
3194 Write_symbols_task::locks(Task_locker
* tl
)
3196 tl
->add(this, this->final_blocker_
);
3199 // Run the task--write out the symbols.
3202 Write_symbols_task::run(Workqueue
*)
3204 this->symtab_
->write_globals(this->input_objects_
, this->sympool_
,
3205 this->dynpool_
, this->layout_
->symtab_xindex(),
3206 this->layout_
->dynsym_xindex(), this->of_
);
3209 // Write_after_input_sections_task methods.
3211 // We can only run this task after the input sections have completed.
3214 Write_after_input_sections_task::is_runnable()
3216 if (this->input_sections_blocker_
->is_blocked())
3217 return this->input_sections_blocker_
;
3221 // We need to unlock FINAL_BLOCKER when finished.
3224 Write_after_input_sections_task::locks(Task_locker
* tl
)
3226 tl
->add(this, this->final_blocker_
);
3232 Write_after_input_sections_task::run(Workqueue
*)
3234 this->layout_
->write_sections_after_input_sections(this->of_
);
3237 // Close_task_runner methods.
3239 // Run the task--close the file.
3242 Close_task_runner::run(Workqueue
*, const Task
*)
3244 // If we need to compute a checksum for the BUILD if, we do so here.
3245 this->layout_
->write_build_id(this->of_
);
3247 // If we've been asked to create a binary file, we do so here.
3248 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3249 this->layout_
->write_binary(this->of_
);
3254 // Instantiate the templates we need. We could use the configure
3255 // script to restrict this to only the ones for implemented targets.
3257 #ifdef HAVE_TARGET_32_LITTLE
3260 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3262 const elfcpp::Shdr
<32, false>& shdr
,
3263 unsigned int, unsigned int, off_t
*);
3266 #ifdef HAVE_TARGET_32_BIG
3269 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3271 const elfcpp::Shdr
<32, true>& shdr
,
3272 unsigned int, unsigned int, off_t
*);
3275 #ifdef HAVE_TARGET_64_LITTLE
3278 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3280 const elfcpp::Shdr
<64, false>& shdr
,
3281 unsigned int, unsigned int, off_t
*);
3284 #ifdef HAVE_TARGET_64_BIG
3287 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3289 const elfcpp::Shdr
<64, true>& shdr
,
3290 unsigned int, unsigned int, off_t
*);
3293 #ifdef HAVE_TARGET_32_LITTLE
3296 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
3297 unsigned int reloc_shndx
,
3298 const elfcpp::Shdr
<32, false>& shdr
,
3299 Output_section
* data_section
,
3300 Relocatable_relocs
* rr
);
3303 #ifdef HAVE_TARGET_32_BIG
3306 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
3307 unsigned int reloc_shndx
,
3308 const elfcpp::Shdr
<32, true>& shdr
,
3309 Output_section
* data_section
,
3310 Relocatable_relocs
* rr
);
3313 #ifdef HAVE_TARGET_64_LITTLE
3316 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
3317 unsigned int reloc_shndx
,
3318 const elfcpp::Shdr
<64, false>& shdr
,
3319 Output_section
* data_section
,
3320 Relocatable_relocs
* rr
);
3323 #ifdef HAVE_TARGET_64_BIG
3326 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
3327 unsigned int reloc_shndx
,
3328 const elfcpp::Shdr
<64, true>& shdr
,
3329 Output_section
* data_section
,
3330 Relocatable_relocs
* rr
);
3333 #ifdef HAVE_TARGET_32_LITTLE
3336 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
3337 Sized_relobj
<32, false>* object
,
3339 const char* group_section_name
,
3340 const char* signature
,
3341 const elfcpp::Shdr
<32, false>& shdr
,
3342 elfcpp::Elf_Word flags
,
3343 std::vector
<unsigned int>* shndxes
);
3346 #ifdef HAVE_TARGET_32_BIG
3349 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
3350 Sized_relobj
<32, true>* object
,
3352 const char* group_section_name
,
3353 const char* signature
,
3354 const elfcpp::Shdr
<32, true>& shdr
,
3355 elfcpp::Elf_Word flags
,
3356 std::vector
<unsigned int>* shndxes
);
3359 #ifdef HAVE_TARGET_64_LITTLE
3362 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
3363 Sized_relobj
<64, false>* object
,
3365 const char* group_section_name
,
3366 const char* signature
,
3367 const elfcpp::Shdr
<64, false>& shdr
,
3368 elfcpp::Elf_Word flags
,
3369 std::vector
<unsigned int>* shndxes
);
3372 #ifdef HAVE_TARGET_64_BIG
3375 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
3376 Sized_relobj
<64, true>* object
,
3378 const char* group_section_name
,
3379 const char* signature
,
3380 const elfcpp::Shdr
<64, true>& shdr
,
3381 elfcpp::Elf_Word flags
,
3382 std::vector
<unsigned int>* shndxes
);
3385 #ifdef HAVE_TARGET_32_LITTLE
3388 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
3389 const unsigned char* symbols
,
3391 const unsigned char* symbol_names
,
3392 off_t symbol_names_size
,
3394 const elfcpp::Shdr
<32, false>& shdr
,
3395 unsigned int reloc_shndx
,
3396 unsigned int reloc_type
,
3400 #ifdef HAVE_TARGET_32_BIG
3403 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
3404 const unsigned char* symbols
,
3406 const unsigned char* symbol_names
,
3407 off_t symbol_names_size
,
3409 const elfcpp::Shdr
<32, true>& shdr
,
3410 unsigned int reloc_shndx
,
3411 unsigned int reloc_type
,
3415 #ifdef HAVE_TARGET_64_LITTLE
3418 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
3419 const unsigned char* symbols
,
3421 const unsigned char* symbol_names
,
3422 off_t symbol_names_size
,
3424 const elfcpp::Shdr
<64, false>& shdr
,
3425 unsigned int reloc_shndx
,
3426 unsigned int reloc_type
,
3430 #ifdef HAVE_TARGET_64_BIG
3433 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
3434 const unsigned char* symbols
,
3436 const unsigned char* symbol_names
,
3437 off_t symbol_names_size
,
3439 const elfcpp::Shdr
<64, true>& shdr
,
3440 unsigned int reloc_shndx
,
3441 unsigned int reloc_type
,
3445 } // End namespace gold.