1 // layout.cc -- lay out output file sections for gold
17 // Layout_task methods.
19 Layout_task::~Layout_task()
23 // This task can be run when it is unblocked.
25 Task::Is_runnable_type
26 Layout_task::is_runnable(Workqueue
*)
28 if (this->this_blocker_
->is_blocked())
33 // We don't need to hold any locks for the duration of this task. In
34 // fact this task will be the only one running.
37 Layout_task::locks(Workqueue
*)
42 // Lay out the sections. This is called after all the input objects
46 Layout_task::run(Workqueue
*)
48 Layout
layout(this->options_
);
50 for (Input_objects::Object_list::const_iterator p
=
51 this->input_objects_
->begin();
52 p
!= this->input_objects_
->end();
54 (*p
)->layout(&layout
);
55 layout
.finalize(this->input_objects_
, this->symtab_
);
60 Layout::Layout(const General_options
& options
)
61 : options_(options
), namepool_(), sympool_(), signatures_(),
62 section_name_map_(), segment_list_(), section_list_()
66 // Prepare for doing layout.
71 // Make space for more than enough segments for a typical file.
72 // This is just for efficiency--it's OK if we wind up needing more.
73 segment_list_
.reserve(12);
76 // Hash a key we use to look up an output section mapping.
79 Layout::Hash_key::operator()(const Layout::Key
& k
) const
81 return reinterpret_cast<size_t>(k
.first
) + k
.second
.first
+ k
.second
.second
;
84 // Whether to include this section in the link.
86 template<int size
, bool big_endian
>
88 Layout::include_section(Object
*, const char*,
89 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
91 // Some section types are never linked. Some are only linked when
92 // doing a relocateable link.
93 switch (shdr
.get_sh_type())
95 case elfcpp::SHT_NULL
:
96 case elfcpp::SHT_SYMTAB
:
97 case elfcpp::SHT_DYNSYM
:
98 case elfcpp::SHT_STRTAB
:
99 case elfcpp::SHT_HASH
:
100 case elfcpp::SHT_DYNAMIC
:
101 case elfcpp::SHT_SYMTAB_SHNDX
:
104 case elfcpp::SHT_RELA
:
105 case elfcpp::SHT_REL
:
106 case elfcpp::SHT_GROUP
:
107 return this->options_
.is_relocatable();
110 // FIXME: Handle stripping debug sections here.
115 // Return the output section to use for input section NAME, with
116 // header HEADER, from object OBJECT. Set *OFF to the offset of this
117 // input section without the output section.
119 template<int size
, bool big_endian
>
121 Layout::layout(Object
* object
, const char* name
,
122 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* off
)
124 if (!this->include_section(object
, name
, shdr
))
127 // Unless we are doing a relocateable link, .gnu.linkonce sections
128 // are laid out as though they were named for the sections are
130 if (!this->options_
.is_relocatable() && Layout::is_linkonce(name
))
131 name
= Layout::linkonce_output_name(name
);
133 // FIXME: Handle SHF_OS_NONCONFORMING here.
135 // Canonicalize the section name.
136 name
= this->namepool_
.add(name
);
138 // Find the output section. The output section is selected based on
139 // the section name, type, and flags.
141 // FIXME: If we want to do relaxation, we need to modify this
142 // algorithm. We also build a list of input sections for each
143 // output section. Then we relax all the input sections. Then we
144 // walk down the list and adjust all the offsets.
146 elfcpp::Elf_Word type
= shdr
.get_sh_type();
147 elfcpp::Elf_Xword flags
= shdr
.get_sh_flags();
148 const Key
key(name
, std::make_pair(type
, flags
));
149 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
150 std::pair
<Section_name_map::iterator
, bool> ins(
151 this->section_name_map_
.insert(v
));
155 os
= ins
.first
->second
;
158 // This is the first time we've seen this name/type/flags
160 os
= this->make_output_section(name
, type
, flags
);
161 ins
.first
->second
= os
;
164 // FIXME: Handle SHF_LINK_ORDER somewhere.
166 *off
= os
->add_input_section(object
, name
, shdr
);
171 // Map section flags to segment flags.
174 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
176 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
177 if ((flags
& elfcpp::SHF_WRITE
) != 0)
179 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
184 // Make a new Output_section, and attach it to segments as
188 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
189 elfcpp::Elf_Xword flags
)
191 Output_section
* os
= new Output_section(name
, type
, flags
);
193 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
194 this->section_list_
.push_back(os
);
197 // This output section goes into a PT_LOAD segment.
199 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
201 // The only thing we really care about for PT_LOAD segments is
202 // whether or not they are writable, so that is how we search
203 // for them. People who need segments sorted on some other
204 // basis will have to wait until we implement a mechanism for
205 // them to describe the segments they want.
207 Segment_list::const_iterator p
;
208 for (p
= this->segment_list_
.begin();
209 p
!= this->segment_list_
.end();
212 if ((*p
)->type() == elfcpp::PT_LOAD
213 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
215 (*p
)->add_output_section(os
, seg_flags
);
220 if (p
== this->segment_list_
.end())
222 Output_segment
* oseg
= new Output_segment(elfcpp::PT_LOAD
,
224 this->segment_list_
.push_back(oseg
);
225 oseg
->add_output_section(os
, seg_flags
);
228 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
230 if (type
== elfcpp::SHT_NOTE
)
232 // See if we already have an equivalent PT_NOTE segment.
233 for (p
= this->segment_list_
.begin();
234 p
!= segment_list_
.end();
237 if ((*p
)->type() == elfcpp::PT_NOTE
238 && (((*p
)->flags() & elfcpp::PF_W
)
239 == (seg_flags
& elfcpp::PF_W
)))
241 (*p
)->add_output_section(os
, seg_flags
);
246 if (p
== this->segment_list_
.end())
248 Output_segment
* oseg
= new Output_segment(elfcpp::PT_NOTE
,
250 this->segment_list_
.push_back(oseg
);
251 oseg
->add_output_section(os
, seg_flags
);
255 // If we see a loadable SHF_TLS section, we create a PT_TLS
257 if ((flags
& elfcpp::SHF_TLS
) != 0)
259 // See if we already have an equivalent PT_TLS segment.
260 for (p
= this->segment_list_
.begin();
261 p
!= segment_list_
.end();
264 if ((*p
)->type() == elfcpp::PT_TLS
265 && (((*p
)->flags() & elfcpp::PF_W
)
266 == (seg_flags
& elfcpp::PF_W
)))
268 (*p
)->add_output_section(os
, seg_flags
);
273 if (p
== this->segment_list_
.end())
275 Output_segment
* oseg
= new Output_segment(elfcpp::PT_TLS
,
277 this->segment_list_
.push_back(oseg
);
278 oseg
->add_output_section(os
, seg_flags
);
286 // Find the first read-only PT_LOAD segment, creating one if
290 Layout::find_first_load_seg()
292 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
293 p
!= this->segment_list_
.end();
296 if ((*p
)->type() == elfcpp::PT_LOAD
297 && ((*p
)->flags() & elfcpp::PF_R
) != 0
298 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
302 Output_segment
* load_seg
= new Output_segment(elfcpp::PT_LOAD
, elfcpp::PF_R
);
303 this->segment_list_
.push_back(load_seg
);
307 // Finalize the layout. When this is called, we have created all the
308 // output sections and all the output segments which are based on
309 // input sections. We have several things to do, and we have to do
310 // them in the right order, so that we get the right results correctly
313 // 1) Finalize the list of output segments and create the segment
316 // 2) Finalize the dynamic symbol table and associated sections.
318 // 3) Determine the final file offset of all the output segments.
320 // 4) Determine the final file offset of all the SHF_ALLOC output
323 // 5) Create the symbol table sections and the section name table
326 // 6) Finalize the symbol table: set symbol values to their final
327 // value and make a final determination of which symbols are going
328 // into the output symbol table.
330 // 7) Create the section table header.
332 // 8) Determine the final file offset of all the output sections which
333 // are not SHF_ALLOC, including the section table header.
335 // 9) Finalize the ELF file header.
337 // This function returns the size of the output file.
340 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
)
342 if (input_objects
->any_dynamic())
344 // If there are any dynamic objects in the link, then we need
345 // some additional segments: PT_PHDRS, PT_INTERP, and
346 // PT_DYNAMIC. We also need to finalize the dynamic symbol
347 // table and create the dynamic hash table.
351 // FIXME: Handle PT_GNU_STACK.
353 Output_segment
* load_seg
= this->find_first_load_seg();
355 // Lay out the segment headers.
356 int size
= input_objects
->target()->get_size();
357 Output_segment_headers
* segment_headers
;
358 segment_headers
= new Output_segment_headers(size
, this->segment_list_
);
359 load_seg
->add_initial_output_data(segment_headers
);
360 // FIXME: Attach them to PT_PHDRS if necessary.
362 // Lay out the file header.
363 Output_file_header
* file_header
;
364 file_header
= new Output_file_header(size
,
366 input_objects
->target(),
369 load_seg
->add_initial_output_data(file_header
);
371 // Set the file offsets of all the segments.
372 off_t off
= this->set_segment_offsets(input_objects
->target(), load_seg
);
374 // Create the symbol table sections.
375 // FIXME: We don't need to do this if we are stripping symbols.
376 Output_section
* osymtab
;
377 Output_section
* ostrtab
;
378 this->create_symtab_sections(input_objects
, symtab
, &osymtab
, &ostrtab
);
380 // Create the .shstrtab section.
381 Output_section
* shstrtab_section
= this->create_shstrtab();
383 // Set the file offsets of all the sections not associated with
385 off
= this->set_section_offsets(off
);
387 // Create the section table header.
388 Output_section_headers
* oshdrs
= this->create_shdrs(size
, off
);
389 off
+= oshdrs
->data_size();
391 file_header
->set_section_info(oshdrs
, shstrtab_section
);
393 // Now we know exactly where everything goes in the output file.
398 // Return whether SEG1 should be before SEG2 in the output file. This
399 // is based entirely on the segment type and flags. When this is
400 // called the segment addresses has normally not yet been set.
403 Layout::segment_precedes(const Output_segment
* seg1
,
404 const Output_segment
* seg2
)
406 elfcpp::Elf_Word type1
= seg1
->type();
407 elfcpp::Elf_Word type2
= seg2
->type();
409 // The single PT_PHDR segment is required to precede any loadable
410 // segment. We simply make it always first.
411 if (type1
== elfcpp::PT_PHDR
)
413 assert(type2
!= elfcpp::PT_PHDR
);
416 if (type2
== elfcpp::PT_PHDR
)
419 // The single PT_INTERP segment is required to precede any loadable
420 // segment. We simply make it always second.
421 if (type1
== elfcpp::PT_INTERP
)
423 assert(type2
!= elfcpp::PT_INTERP
);
426 if (type2
== elfcpp::PT_INTERP
)
429 // We then put PT_LOAD segments before any other segments.
430 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
432 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
435 const elfcpp::Elf_Word flags1
= seg1
->flags();
436 const elfcpp::Elf_Word flags2
= seg2
->flags();
438 // The order of non-PT_LOAD segments is unimportant. We simply sort
439 // by the numeric segment type and flags values. There should not
440 // be more than one segment with the same type and flags.
441 if (type1
!= elfcpp::PT_LOAD
)
444 return type1
< type2
;
445 assert(flags1
!= flags2
);
446 return flags1
< flags2
;
449 // We sort PT_LOAD segments based on the flags. Readonly segments
450 // come before writable segments. Then executable segments come
451 // before non-executable segments. Then the unlikely case of a
452 // non-readable segment comes before the normal case of a readable
453 // segment. If there are multiple segments with the same type and
454 // flags, we require that the address be set, and we sort by
455 // virtual address and then physical address.
456 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
457 return (flags1
& elfcpp::PF_W
) == 0;
458 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
459 return (flags1
& elfcpp::PF_X
) != 0;
460 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
461 return (flags1
& elfcpp::PF_R
) == 0;
463 uint64_t vaddr1
= seg1
->vaddr();
464 uint64_t vaddr2
= seg2
->vaddr();
465 if (vaddr1
!= vaddr2
)
466 return vaddr1
< vaddr2
;
468 uint64_t paddr1
= seg1
->paddr();
469 uint64_t paddr2
= seg2
->paddr();
470 assert(paddr1
!= paddr2
);
471 return paddr1
< paddr2
;
474 // Set the file offsets of all the segments. They have all been
475 // created. LOAD_SEG must be be laid out first. Return the offset of
476 // the data to follow.
479 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
)
481 // Sort them into the final order.
482 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
483 Layout::Compare_segments());
485 // Find the PT_LOAD segments, and set their addresses and offsets
486 // and their section's addresses and offsets.
487 uint64_t addr
= target
->text_segment_address();
489 bool was_readonly
= false;
490 for (Segment_list::iterator p
= this->segment_list_
.begin();
491 p
!= this->segment_list_
.end();
494 if ((*p
)->type() == elfcpp::PT_LOAD
)
496 if (load_seg
!= NULL
&& load_seg
!= *p
)
500 // If the last segment was readonly, and this one is not,
501 // then skip the address forward one page, maintaining the
502 // same position within the page. This lets us store both
503 // segments overlapping on a single page in the file, but
504 // the loader will put them on different pages in memory.
506 uint64_t orig_addr
= addr
;
507 uint64_t orig_off
= off
;
509 uint64_t aligned_addr
= addr
;
510 uint64_t abi_pagesize
= target
->abi_pagesize();
511 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
513 uint64_t align
= (*p
)->max_data_align();
515 addr
= (addr
+ align
- 1) & ~ (align
- 1);
517 if ((addr
& (abi_pagesize
- 1)) != 0)
518 addr
= addr
+ abi_pagesize
;
521 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
522 uint64_t new_addr
= (*p
)->set_section_addresses(addr
, &off
);
524 // Now that we know the size of this segment, we may be able
525 // to save a page in memory, at the cost of wasting some
526 // file space, by instead aligning to the start of a new
527 // page. Here we use the real machine page size rather than
528 // the ABI mandated page size.
530 if (aligned_addr
!= addr
)
532 uint64_t common_pagesize
= target
->common_pagesize();
533 uint64_t first_off
= (common_pagesize
535 & (common_pagesize
- 1)));
536 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
539 && ((aligned_addr
& ~ (common_pagesize
- 1))
540 != (new_addr
& ~ (common_pagesize
- 1)))
541 && first_off
+ last_off
<= common_pagesize
)
543 addr
= ((aligned_addr
+ common_pagesize
- 1)
544 & ~ (common_pagesize
- 1));
545 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
546 new_addr
= (*p
)->set_section_addresses(addr
, &off
);
552 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
557 // Handle the non-PT_LOAD segments, setting their offsets from their
558 // section's offsets.
559 for (Segment_list::iterator p
= this->segment_list_
.begin();
560 p
!= this->segment_list_
.end();
563 if ((*p
)->type() != elfcpp::PT_LOAD
)
570 // Set the file offset of all the sections not associated with a
574 Layout::set_section_offsets(off_t off
)
576 for (Layout::Section_list::iterator p
= this->section_list_
.begin();
577 p
!= this->section_list_
.end();
580 uint64_t addralign
= (*p
)->addralign();
581 off
= (off
+ addralign
- 1) & ~ (addralign
- 1);
582 (*p
)->set_address(0, off
);
583 off
+= (*p
)->data_size();
588 // Create the symbol table sections.
591 Layout::create_symtab_sections(const Input_objects
* input_objects
,
592 Symbol_table
* symtab
,
593 Output_section
** posymtab
,
594 Output_section
** postrtab
)
597 for (Input_objects::Object_list::const_iterator p
= input_objects
->begin();
598 p
!= input_objects
->end();
601 Task_lock_obj
<Object
> tlo(**p
);
602 off
= (*p
)->finalize_local_symbols(off
, &this->sympool_
);
605 off
= symtab
->finalize(off
, &this->sympool_
);
607 *posymtab
= new Output_section_symtab(this->namepool_
.add(".symtab"), off
);
608 *postrtab
= new Output_section_strtab(this->namepool_
.add(".strtab"),
612 // Create the .shstrtab section, which holds the names of the
613 // sections. At the time this is called, we have created all the
614 // output sections except .shstrtab itself.
617 Layout::create_shstrtab()
619 // FIXME: We don't need to create a .shstrtab section if we are
620 // stripping everything.
622 const char* name
= this->namepool_
.add(".shstrtab");
624 Output_section
* os
= new Output_section_strtab(name
,
627 this->section_list_
.push_back(os
);
632 // Create the section headers. SIZE is 32 or 64. OFF is the file
635 Output_section_headers
*
636 Layout::create_shdrs(int size
, off_t off
)
638 Output_section_headers
* oshdrs
;
639 oshdrs
= new Output_section_headers(size
, this->segment_list_
,
640 this->section_list_
);
641 uint64_t addralign
= oshdrs
->addralign();
642 off
= (off
+ addralign
- 1) & ~ (addralign
- 1);
643 oshdrs
->set_address(0, off
);
647 // The mapping of .gnu.linkonce section names to real section names.
649 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
650 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
652 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
653 MAPPING_INIT("t", ".text"),
654 MAPPING_INIT("r", ".rodata"),
655 MAPPING_INIT("d", ".data"),
656 MAPPING_INIT("b", ".bss"),
657 MAPPING_INIT("s", ".sdata"),
658 MAPPING_INIT("sb", ".sbss"),
659 MAPPING_INIT("s2", ".sdata2"),
660 MAPPING_INIT("sb2", ".sbss2"),
661 MAPPING_INIT("wi", ".debug_info"),
662 MAPPING_INIT("td", ".tdata"),
663 MAPPING_INIT("tb", ".tbss"),
664 MAPPING_INIT("lr", ".lrodata"),
665 MAPPING_INIT("l", ".ldata"),
666 MAPPING_INIT("lb", ".lbss"),
670 const int Layout::linkonce_mapping_count
=
671 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
673 // Return the name of the output section to use for a .gnu.linkonce
674 // section. This is based on the default ELF linker script of the old
675 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
679 Layout::linkonce_output_name(const char* name
)
681 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
685 const Linkonce_mapping
* plm
= linkonce_mapping
;
686 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
688 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
694 // Record the signature of a comdat section, and return whether to
695 // include it in the link. If GROUP is true, this is a regular
696 // section group. If GROUP is false, this is a group signature
697 // derived from the name of a linkonce section. We want linkonce
698 // signatures and group signatures to block each other, but we don't
699 // want a linkonce signature to block another linkonce signature.
702 Layout::add_comdat(const char* signature
, bool group
)
704 std::string
sig(signature
);
705 std::pair
<Signatures::iterator
, bool> ins(
706 this->signatures_
.insert(std::make_pair(signature
, group
)));
710 // This is the first time we've seen this signature.
714 if (ins
.first
->second
)
716 // We've already seen a real section group with this signature.
721 // This is a real section group, and we've already seen a
722 // linkonce section with tihs signature. Record that we've seen
723 // a section group, and don't include this section group.
724 ins
.first
->second
= true;
729 // We've already seen a linkonce section and this is a linkonce
730 // section. These don't block each other--this may be the same
731 // symbol name with different section types.
736 // Instantiate the templates we need. We could use the configure
737 // script to restrict this to only the ones for implemented targets.
741 Layout::layout
<32, false>(Object
* object
, const char* name
,
742 const elfcpp::Shdr
<32, false>& shdr
, off_t
*);
746 Layout::layout
<32, true>(Object
* object
, const char* name
,
747 const elfcpp::Shdr
<32, true>& shdr
, off_t
*);
751 Layout::layout
<64, false>(Object
* object
, const char* name
,
752 const elfcpp::Shdr
<64, false>& shdr
, off_t
*);
756 Layout::layout
<64, true>(Object
* object
, const char* name
,
757 const elfcpp::Shdr
<64, true>& shdr
, off_t
*);
760 } // End namespace gold.