1 // layout.cc -- lay out output file sections for gold
18 // Layout_task_runner methods.
20 // Lay out the sections. This is called after all the input objects
24 Layout_task_runner::run(Workqueue
* workqueue
)
26 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
29 // Now we know the final size of the output file and we know where
30 // each piece of information goes.
31 Output_file
* of
= new Output_file(this->options_
);
34 // Queue up the final set of tasks.
35 gold::queue_final_tasks(this->options_
, this->input_objects_
,
36 this->symtab_
, this->layout_
, workqueue
, of
);
41 Layout::Layout(const General_options
& options
)
42 : options_(options
), namepool_(), sympool_(), dynpool_(), signatures_(),
43 section_name_map_(), segment_list_(), section_list_(),
44 unattached_section_list_(), special_output_list_(),
45 tls_segment_(NULL
), symtab_section_(NULL
),
46 dynsym_section_(NULL
), dynamic_section_(NULL
), dynamic_data_(NULL
)
48 // Make space for more than enough segments for a typical file.
49 // This is just for efficiency--it's OK if we wind up needing more.
50 this->segment_list_
.reserve(12);
52 // We expect three unattached Output_data objects: the file header,
53 // the segment headers, and the section headers.
54 this->special_output_list_
.reserve(3);
57 // Hash a key we use to look up an output section mapping.
60 Layout::Hash_key::operator()(const Layout::Key
& k
) const
62 return k
.first
+ k
.second
.first
+ k
.second
.second
;
65 // Whether to include this section in the link.
67 template<int size
, bool big_endian
>
69 Layout::include_section(Object
*, const char*,
70 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
72 // Some section types are never linked. Some are only linked when
73 // doing a relocateable link.
74 switch (shdr
.get_sh_type())
76 case elfcpp::SHT_NULL
:
77 case elfcpp::SHT_SYMTAB
:
78 case elfcpp::SHT_DYNSYM
:
79 case elfcpp::SHT_STRTAB
:
80 case elfcpp::SHT_HASH
:
81 case elfcpp::SHT_DYNAMIC
:
82 case elfcpp::SHT_SYMTAB_SHNDX
:
85 case elfcpp::SHT_RELA
:
87 case elfcpp::SHT_GROUP
:
88 return this->options_
.is_relocatable();
91 // FIXME: Handle stripping debug sections here.
96 // Return an output section named NAME, or NULL if there is none.
99 Layout::find_output_section(const char* name
) const
101 for (Section_name_map::const_iterator p
= this->section_name_map_
.begin();
102 p
!= this->section_name_map_
.end();
104 if (strcmp(p
->second
->name(), name
) == 0)
109 // Return an output segment of type TYPE, with segment flags SET set
110 // and segment flags CLEAR clear. Return NULL if there is none.
113 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
114 elfcpp::Elf_Word clear
) const
116 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
117 p
!= this->segment_list_
.end();
119 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
120 && ((*p
)->flags() & set
) == set
121 && ((*p
)->flags() & clear
) == 0)
126 // Return the output section to use for section NAME with type TYPE
127 // and section flags FLAGS.
130 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
131 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
133 // We should ignore some flags.
134 flags
&= ~ (elfcpp::SHF_INFO_LINK
135 | elfcpp::SHF_LINK_ORDER
138 | elfcpp::SHF_STRINGS
);
140 const Key
key(name_key
, std::make_pair(type
, flags
));
141 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
142 std::pair
<Section_name_map::iterator
, bool> ins(
143 this->section_name_map_
.insert(v
));
146 return ins
.first
->second
;
149 // This is the first time we've seen this name/type/flags
151 Output_section
* os
= this->make_output_section(name
, type
, flags
);
152 ins
.first
->second
= os
;
157 // Return the output section to use for input section SHNDX, with name
158 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
159 // offset of this input section without the output section.
161 template<int size
, bool big_endian
>
163 Layout::layout(Relobj
* object
, unsigned int shndx
, const char* name
,
164 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* off
)
166 if (!this->include_section(object
, name
, shdr
))
169 // If we are not doing a relocateable link, choose the name to use
170 // for the output section.
171 size_t len
= strlen(name
);
172 if (!this->options_
.is_relocatable())
173 name
= Layout::output_section_name(name
, &len
);
175 // FIXME: Handle SHF_OS_NONCONFORMING here.
177 // Canonicalize the section name.
178 Stringpool::Key name_key
;
179 name
= this->namepool_
.add(name
, len
, &name_key
);
181 // Find the output section. The output section is selected based on
182 // the section name, type, and flags.
183 Output_section
* os
= this->get_output_section(name
, name_key
,
185 shdr
.get_sh_flags());
187 // FIXME: Handle SHF_LINK_ORDER somewhere.
189 *off
= os
->add_input_section(object
, shndx
, name
, shdr
);
194 // Add POSD to an output section using NAME, TYPE, and FLAGS.
197 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
198 elfcpp::Elf_Xword flags
,
199 Output_section_data
* posd
)
201 // Canonicalize the name.
202 Stringpool::Key name_key
;
203 name
= this->namepool_
.add(name
, &name_key
);
205 Output_section
* os
= this->get_output_section(name
, name_key
, type
, flags
);
206 os
->add_output_section_data(posd
);
209 // Map section flags to segment flags.
212 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
214 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
215 if ((flags
& elfcpp::SHF_WRITE
) != 0)
217 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
222 // Make a new Output_section, and attach it to segments as
226 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
227 elfcpp::Elf_Xword flags
)
229 Output_section
* os
= new Output_section(name
, type
, flags
);
230 this->section_list_
.push_back(os
);
232 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
233 this->unattached_section_list_
.push_back(os
);
236 // This output section goes into a PT_LOAD segment.
238 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
240 // The only thing we really care about for PT_LOAD segments is
241 // whether or not they are writable, so that is how we search
242 // for them. People who need segments sorted on some other
243 // basis will have to wait until we implement a mechanism for
244 // them to describe the segments they want.
246 Segment_list::const_iterator p
;
247 for (p
= this->segment_list_
.begin();
248 p
!= this->segment_list_
.end();
251 if ((*p
)->type() == elfcpp::PT_LOAD
252 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
254 (*p
)->add_output_section(os
, seg_flags
);
259 if (p
== this->segment_list_
.end())
261 Output_segment
* oseg
= new Output_segment(elfcpp::PT_LOAD
,
263 this->segment_list_
.push_back(oseg
);
264 oseg
->add_output_section(os
, seg_flags
);
267 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
269 if (type
== elfcpp::SHT_NOTE
)
271 // See if we already have an equivalent PT_NOTE segment.
272 for (p
= this->segment_list_
.begin();
273 p
!= segment_list_
.end();
276 if ((*p
)->type() == elfcpp::PT_NOTE
277 && (((*p
)->flags() & elfcpp::PF_W
)
278 == (seg_flags
& elfcpp::PF_W
)))
280 (*p
)->add_output_section(os
, seg_flags
);
285 if (p
== this->segment_list_
.end())
287 Output_segment
* oseg
= new Output_segment(elfcpp::PT_NOTE
,
289 this->segment_list_
.push_back(oseg
);
290 oseg
->add_output_section(os
, seg_flags
);
294 // If we see a loadable SHF_TLS section, we create a PT_TLS
295 // segment. There can only be one such segment.
296 if ((flags
& elfcpp::SHF_TLS
) != 0)
298 if (this->tls_segment_
== NULL
)
300 this->tls_segment_
= new Output_segment(elfcpp::PT_TLS
,
302 this->segment_list_
.push_back(this->tls_segment_
);
304 this->tls_segment_
->add_output_section(os
, seg_flags
);
311 // Create the dynamic sections which are needed before we read the
315 Layout::create_initial_dynamic_sections(const Input_objects
* input_objects
,
316 Symbol_table
* symtab
)
318 if (!input_objects
->any_dynamic())
321 const char* dynamic_name
= this->namepool_
.add(".dynamic", NULL
);
322 this->dynamic_section_
= this->make_output_section(dynamic_name
,
325 | elfcpp::SHF_WRITE
));
327 symtab
->define_in_output_data(input_objects
->target(), "_DYNAMIC", NULL
,
328 this->dynamic_section_
, 0, 0,
329 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
330 elfcpp::STV_HIDDEN
, 0, false, false);
332 this->dynamic_data_
= new Output_data_dynamic(input_objects
->target(),
335 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
338 // Find the first read-only PT_LOAD segment, creating one if
342 Layout::find_first_load_seg()
344 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
345 p
!= this->segment_list_
.end();
348 if ((*p
)->type() == elfcpp::PT_LOAD
349 && ((*p
)->flags() & elfcpp::PF_R
) != 0
350 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
354 Output_segment
* load_seg
= new Output_segment(elfcpp::PT_LOAD
, elfcpp::PF_R
);
355 this->segment_list_
.push_back(load_seg
);
359 // Finalize the layout. When this is called, we have created all the
360 // output sections and all the output segments which are based on
361 // input sections. We have several things to do, and we have to do
362 // them in the right order, so that we get the right results correctly
365 // 1) Finalize the list of output segments and create the segment
368 // 2) Finalize the dynamic symbol table and associated sections.
370 // 3) Determine the final file offset of all the output segments.
372 // 4) Determine the final file offset of all the SHF_ALLOC output
375 // 5) Create the symbol table sections and the section name table
378 // 6) Finalize the symbol table: set symbol values to their final
379 // value and make a final determination of which symbols are going
380 // into the output symbol table.
382 // 7) Create the section table header.
384 // 8) Determine the final file offset of all the output sections which
385 // are not SHF_ALLOC, including the section table header.
387 // 9) Finalize the ELF file header.
389 // This function returns the size of the output file.
392 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
)
394 Target
* const target
= input_objects
->target();
395 const int size
= target
->get_size();
397 target
->finalize_sections(&this->options_
, this);
399 Output_segment
* phdr_seg
= NULL
;
400 if (input_objects
->any_dynamic())
402 // There was a dynamic object in the link. We need to create
403 // some information for the dynamic linker.
405 // Create the PT_PHDR segment which will hold the program
407 phdr_seg
= new Output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
408 this->segment_list_
.push_back(phdr_seg
);
410 // Create the dynamic symbol table, including the hash table.
411 Output_section
* dynstr
;
412 std::vector
<Symbol
*> dynamic_symbols
;
413 unsigned int local_dynamic_count
;
415 this->create_dynamic_symtab(target
, symtab
, &dynstr
,
416 &local_dynamic_count
, &dynamic_symbols
,
419 // Create the .interp section to hold the name of the
420 // interpreter, and put it in a PT_INTERP segment.
421 this->create_interp(target
);
423 // Finish the .dynamic section to hold the dynamic data, and put
424 // it in a PT_DYNAMIC segment.
425 this->finish_dynamic_section(input_objects
, symtab
);
427 // We should have added everything we need to the dynamic string
429 this->dynpool_
.set_string_offsets();
431 // Create the version sections. We can't do this until the
432 // dynamic string table is complete.
433 this->create_version_sections(target
, &versions
, local_dynamic_count
,
434 dynamic_symbols
, dynstr
);
437 // FIXME: Handle PT_GNU_STACK.
439 Output_segment
* load_seg
= this->find_first_load_seg();
441 // Lay out the segment headers.
442 bool big_endian
= target
->is_big_endian();
443 Output_segment_headers
* segment_headers
;
444 segment_headers
= new Output_segment_headers(size
, big_endian
,
445 this->segment_list_
);
446 load_seg
->add_initial_output_data(segment_headers
);
447 this->special_output_list_
.push_back(segment_headers
);
448 if (phdr_seg
!= NULL
)
449 phdr_seg
->add_initial_output_data(segment_headers
);
451 // Lay out the file header.
452 Output_file_header
* file_header
;
453 file_header
= new Output_file_header(size
,
459 load_seg
->add_initial_output_data(file_header
);
460 this->special_output_list_
.push_back(file_header
);
462 // We set the output section indexes in set_segment_offsets and
463 // set_section_offsets.
464 unsigned int shndx
= 1;
466 // Set the file offsets of all the segments, and all the sections
468 off_t off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
470 // Create the symbol table sections.
471 this->create_symtab_sections(size
, input_objects
, symtab
, &off
);
473 // Create the .shstrtab section.
474 Output_section
* shstrtab_section
= this->create_shstrtab();
476 // Set the file offsets of all the sections not associated with
478 off
= this->set_section_offsets(off
, &shndx
);
480 // Create the section table header.
481 Output_section_headers
* oshdrs
= this->create_shdrs(size
, big_endian
, &off
);
483 file_header
->set_section_info(oshdrs
, shstrtab_section
);
485 // Now we know exactly where everything goes in the output file.
486 Output_data::layout_complete();
491 // Return whether SEG1 should be before SEG2 in the output file. This
492 // is based entirely on the segment type and flags. When this is
493 // called the segment addresses has normally not yet been set.
496 Layout::segment_precedes(const Output_segment
* seg1
,
497 const Output_segment
* seg2
)
499 elfcpp::Elf_Word type1
= seg1
->type();
500 elfcpp::Elf_Word type2
= seg2
->type();
502 // The single PT_PHDR segment is required to precede any loadable
503 // segment. We simply make it always first.
504 if (type1
== elfcpp::PT_PHDR
)
506 gold_assert(type2
!= elfcpp::PT_PHDR
);
509 if (type2
== elfcpp::PT_PHDR
)
512 // The single PT_INTERP segment is required to precede any loadable
513 // segment. We simply make it always second.
514 if (type1
== elfcpp::PT_INTERP
)
516 gold_assert(type2
!= elfcpp::PT_INTERP
);
519 if (type2
== elfcpp::PT_INTERP
)
522 // We then put PT_LOAD segments before any other segments.
523 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
525 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
528 // We put the PT_TLS segment last, because that is where the dynamic
529 // linker expects to find it (this is just for efficiency; other
530 // positions would also work correctly).
531 if (type1
== elfcpp::PT_TLS
&& type2
!= elfcpp::PT_TLS
)
533 if (type2
== elfcpp::PT_TLS
&& type1
!= elfcpp::PT_TLS
)
536 const elfcpp::Elf_Word flags1
= seg1
->flags();
537 const elfcpp::Elf_Word flags2
= seg2
->flags();
539 // The order of non-PT_LOAD segments is unimportant. We simply sort
540 // by the numeric segment type and flags values. There should not
541 // be more than one segment with the same type and flags.
542 if (type1
!= elfcpp::PT_LOAD
)
545 return type1
< type2
;
546 gold_assert(flags1
!= flags2
);
547 return flags1
< flags2
;
550 // We sort PT_LOAD segments based on the flags. Readonly segments
551 // come before writable segments. Then executable segments come
552 // before non-executable segments. Then the unlikely case of a
553 // non-readable segment comes before the normal case of a readable
554 // segment. If there are multiple segments with the same type and
555 // flags, we require that the address be set, and we sort by
556 // virtual address and then physical address.
557 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
558 return (flags1
& elfcpp::PF_W
) == 0;
559 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
560 return (flags1
& elfcpp::PF_X
) != 0;
561 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
562 return (flags1
& elfcpp::PF_R
) == 0;
564 uint64_t vaddr1
= seg1
->vaddr();
565 uint64_t vaddr2
= seg2
->vaddr();
566 if (vaddr1
!= vaddr2
)
567 return vaddr1
< vaddr2
;
569 uint64_t paddr1
= seg1
->paddr();
570 uint64_t paddr2
= seg2
->paddr();
571 gold_assert(paddr1
!= paddr2
);
572 return paddr1
< paddr2
;
575 // Set the file offsets of all the segments, and all the sections they
576 // contain. They have all been created. LOAD_SEG must be be laid out
577 // first. Return the offset of the data to follow.
580 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
581 unsigned int *pshndx
)
583 // Sort them into the final order.
584 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
585 Layout::Compare_segments());
587 // Find the PT_LOAD segments, and set their addresses and offsets
588 // and their section's addresses and offsets.
589 uint64_t addr
= target
->text_segment_address();
591 bool was_readonly
= false;
592 for (Segment_list::iterator p
= this->segment_list_
.begin();
593 p
!= this->segment_list_
.end();
596 if ((*p
)->type() == elfcpp::PT_LOAD
)
598 if (load_seg
!= NULL
&& load_seg
!= *p
)
602 // If the last segment was readonly, and this one is not,
603 // then skip the address forward one page, maintaining the
604 // same position within the page. This lets us store both
605 // segments overlapping on a single page in the file, but
606 // the loader will put them on different pages in memory.
608 uint64_t orig_addr
= addr
;
609 uint64_t orig_off
= off
;
611 uint64_t aligned_addr
= addr
;
612 uint64_t abi_pagesize
= target
->abi_pagesize();
613 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
615 uint64_t align
= (*p
)->addralign();
617 addr
= align_address(addr
, align
);
619 if ((addr
& (abi_pagesize
- 1)) != 0)
620 addr
= addr
+ abi_pagesize
;
623 unsigned int shndx_hold
= *pshndx
;
624 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
625 uint64_t new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
627 // Now that we know the size of this segment, we may be able
628 // to save a page in memory, at the cost of wasting some
629 // file space, by instead aligning to the start of a new
630 // page. Here we use the real machine page size rather than
631 // the ABI mandated page size.
633 if (aligned_addr
!= addr
)
635 uint64_t common_pagesize
= target
->common_pagesize();
636 uint64_t first_off
= (common_pagesize
638 & (common_pagesize
- 1)));
639 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
642 && ((aligned_addr
& ~ (common_pagesize
- 1))
643 != (new_addr
& ~ (common_pagesize
- 1)))
644 && first_off
+ last_off
<= common_pagesize
)
646 *pshndx
= shndx_hold
;
647 addr
= align_address(aligned_addr
, common_pagesize
);
648 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
649 new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
655 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
660 // Handle the non-PT_LOAD segments, setting their offsets from their
661 // section's offsets.
662 for (Segment_list::iterator p
= this->segment_list_
.begin();
663 p
!= this->segment_list_
.end();
666 if ((*p
)->type() != elfcpp::PT_LOAD
)
673 // Set the file offset of all the sections not associated with a
677 Layout::set_section_offsets(off_t off
, unsigned int* pshndx
)
679 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
680 p
!= this->unattached_section_list_
.end();
683 (*p
)->set_out_shndx(*pshndx
);
685 if ((*p
)->offset() != -1)
687 off
= align_address(off
, (*p
)->addralign());
688 (*p
)->set_address(0, off
);
689 off
+= (*p
)->data_size();
694 // Create the symbol table sections. Here we also set the final
695 // values of the symbols. At this point all the loadable sections are
699 Layout::create_symtab_sections(int size
, const Input_objects
* input_objects
,
700 Symbol_table
* symtab
,
707 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
712 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
719 off
= align_address(off
, align
);
720 off_t startoff
= off
;
722 // Save space for the dummy symbol at the start of the section. We
723 // never bother to write this out--it will just be left as zero.
725 unsigned int local_symbol_index
= 1;
727 // Add STT_SECTION symbols for each Output section which needs one.
728 for (Section_list::iterator p
= this->section_list_
.begin();
729 p
!= this->section_list_
.end();
732 if (!(*p
)->needs_symtab_index())
733 (*p
)->set_symtab_index(-1U);
736 (*p
)->set_symtab_index(local_symbol_index
);
737 ++local_symbol_index
;
742 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
743 p
!= input_objects
->relobj_end();
746 Task_lock_obj
<Object
> tlo(**p
);
747 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
750 off
+= (index
- local_symbol_index
) * symsize
;
751 local_symbol_index
= index
;
754 unsigned int local_symcount
= local_symbol_index
;
755 gold_assert(local_symcount
* symsize
== off
- startoff
);
758 size_t dyn_global_index
;
760 if (this->dynsym_section_
== NULL
)
763 dyn_global_index
= 0;
768 dyn_global_index
= this->dynsym_section_
->info();
769 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
770 dynoff
= this->dynsym_section_
->offset() + locsize
;
771 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
772 gold_assert(dyncount
* symsize
773 == this->dynsym_section_
->data_size() - locsize
);
776 off
= symtab
->finalize(local_symcount
, off
, dynoff
, dyn_global_index
,
777 dyncount
, &this->sympool_
);
779 this->sympool_
.set_string_offsets();
781 const char* symtab_name
= this->namepool_
.add(".symtab", NULL
);
782 Output_section
* osymtab
= this->make_output_section(symtab_name
,
785 this->symtab_section_
= osymtab
;
787 Output_section_data
* pos
= new Output_data_space(off
- startoff
,
789 osymtab
->add_output_section_data(pos
);
791 const char* strtab_name
= this->namepool_
.add(".strtab", NULL
);
792 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
796 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
797 ostrtab
->add_output_section_data(pstr
);
799 osymtab
->set_address(0, startoff
);
800 osymtab
->set_link_section(ostrtab
);
801 osymtab
->set_info(local_symcount
);
802 osymtab
->set_entsize(symsize
);
807 // Create the .shstrtab section, which holds the names of the
808 // sections. At the time this is called, we have created all the
809 // output sections except .shstrtab itself.
812 Layout::create_shstrtab()
814 // FIXME: We don't need to create a .shstrtab section if we are
815 // stripping everything.
817 const char* name
= this->namepool_
.add(".shstrtab", NULL
);
819 this->namepool_
.set_string_offsets();
821 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
823 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
824 os
->add_output_section_data(posd
);
829 // Create the section headers. SIZE is 32 or 64. OFF is the file
832 Output_section_headers
*
833 Layout::create_shdrs(int size
, bool big_endian
, off_t
* poff
)
835 Output_section_headers
* oshdrs
;
836 oshdrs
= new Output_section_headers(size
, big_endian
, this,
837 &this->segment_list_
,
838 &this->unattached_section_list_
,
840 off_t off
= align_address(*poff
, oshdrs
->addralign());
841 oshdrs
->set_address(0, off
);
842 off
+= oshdrs
->data_size();
844 this->special_output_list_
.push_back(oshdrs
);
848 // Create the dynamic symbol table.
851 Layout::create_dynamic_symtab(const Target
* target
, Symbol_table
* symtab
,
852 Output_section
**pdynstr
,
853 unsigned int* plocal_dynamic_count
,
854 std::vector
<Symbol
*>* pdynamic_symbols
,
857 // Count all the symbols in the dynamic symbol table, and set the
858 // dynamic symbol indexes.
860 // Skip symbol 0, which is always all zeroes.
861 unsigned int index
= 1;
863 // Add STT_SECTION symbols for each Output section which needs one.
864 for (Section_list::iterator p
= this->section_list_
.begin();
865 p
!= this->section_list_
.end();
868 if (!(*p
)->needs_dynsym_index())
869 (*p
)->set_dynsym_index(-1U);
872 (*p
)->set_dynsym_index(index
);
877 // FIXME: Some targets apparently require local symbols in the
878 // dynamic symbol table. Here is where we will have to count them,
879 // and set the dynamic symbol indexes, and add the names to
882 unsigned int local_symcount
= index
;
883 *plocal_dynamic_count
= local_symcount
;
885 // FIXME: We have to tell set_dynsym_indexes whether the
886 // -E/--export-dynamic option was used.
887 index
= symtab
->set_dynsym_indexes(&this->options_
, target
, index
,
888 pdynamic_symbols
, &this->dynpool_
,
893 const int size
= target
->get_size();
896 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
901 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
907 // Create the dynamic symbol table section.
909 const char* dynsym_name
= this->namepool_
.add(".dynsym", NULL
);
910 Output_section
* dynsym
= this->make_output_section(dynsym_name
,
914 Output_section_data
* odata
= new Output_data_space(index
* symsize
,
916 dynsym
->add_output_section_data(odata
);
918 dynsym
->set_info(local_symcount
);
919 dynsym
->set_entsize(symsize
);
920 dynsym
->set_addralign(align
);
922 this->dynsym_section_
= dynsym
;
924 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
925 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
926 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
928 // Create the dynamic string table section.
930 const char* dynstr_name
= this->namepool_
.add(".dynstr", NULL
);
931 Output_section
* dynstr
= this->make_output_section(dynstr_name
,
935 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
936 dynstr
->add_output_section_data(strdata
);
938 dynsym
->set_link_section(dynstr
);
939 this->dynamic_section_
->set_link_section(dynstr
);
941 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
942 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
946 // Create the hash tables.
948 // FIXME: We need an option to create a GNU hash table.
950 unsigned char* phash
;
951 unsigned int hashlen
;
952 Dynobj::create_elf_hash_table(target
, *pdynamic_symbols
, local_symcount
,
955 const char* hash_name
= this->namepool_
.add(".hash", NULL
);
956 Output_section
* hashsec
= this->make_output_section(hash_name
,
960 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
963 hashsec
->add_output_section_data(hashdata
);
965 hashsec
->set_link_section(dynsym
);
966 hashsec
->set_entsize(4);
968 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
971 // Create the version sections.
974 Layout::create_version_sections(const Target
* target
, const Versions
* versions
,
975 unsigned int local_symcount
,
976 const std::vector
<Symbol
*>& dynamic_symbols
,
977 const Output_section
* dynstr
)
979 if (!versions
->any_defs() && !versions
->any_needs())
982 if (target
->get_size() == 32)
984 if (target
->is_big_endian())
985 this->sized_create_version_sections
SELECT_SIZE_ENDIAN_NAME(32, true)(
986 versions
, local_symcount
, dynamic_symbols
, dynstr
987 SELECT_SIZE_ENDIAN(32, true));
989 this->sized_create_version_sections
SELECT_SIZE_ENDIAN_NAME(32, false)(
990 versions
, local_symcount
, dynamic_symbols
, dynstr
991 SELECT_SIZE_ENDIAN(32, false));
993 else if (target
->get_size() == 64)
995 if (target
->is_big_endian())
996 this->sized_create_version_sections
SELECT_SIZE_ENDIAN_NAME(64, true)(
997 versions
, local_symcount
, dynamic_symbols
, dynstr
998 SELECT_SIZE_ENDIAN(64, true));
1000 this->sized_create_version_sections
SELECT_SIZE_ENDIAN_NAME(64, false)(
1001 versions
, local_symcount
, dynamic_symbols
, dynstr
1002 SELECT_SIZE_ENDIAN(64, false));
1008 // Create the version sections, sized version.
1010 template<int size
, bool big_endian
>
1012 Layout::sized_create_version_sections(
1013 const Versions
* versions
,
1014 unsigned int local_symcount
,
1015 const std::vector
<Symbol
*>& dynamic_symbols
,
1016 const Output_section
* dynstr
1019 const char* vname
= this->namepool_
.add(".gnu.version", NULL
);
1020 Output_section
* vsec
= this->make_output_section(vname
,
1021 elfcpp::SHT_GNU_versym
,
1024 unsigned char* vbuf
;
1026 versions
->symbol_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1027 &this->dynpool_
, local_symcount
, dynamic_symbols
, &vbuf
, &vsize
1028 SELECT_SIZE_ENDIAN(size
, big_endian
));
1030 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2);
1032 vsec
->add_output_section_data(vdata
);
1033 vsec
->set_entsize(2);
1034 vsec
->set_link_section(this->dynsym_section_
);
1036 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1037 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
1039 if (versions
->any_defs())
1041 const char* vdname
= this->namepool_
.add(".gnu.version_d", NULL
);
1042 Output_section
*vdsec
;
1043 vdsec
= this->make_output_section(vdname
, elfcpp::SHT_GNU_verdef
,
1046 unsigned char* vdbuf
;
1047 unsigned int vdsize
;
1048 unsigned int vdentries
;
1049 versions
->def_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1050 &this->dynpool_
, &vdbuf
, &vdsize
, &vdentries
1051 SELECT_SIZE_ENDIAN(size
, big_endian
));
1053 Output_section_data
* vddata
= new Output_data_const_buffer(vdbuf
,
1057 vdsec
->add_output_section_data(vddata
);
1058 vdsec
->set_link_section(dynstr
);
1059 vdsec
->set_info(vdentries
);
1061 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
1062 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
1065 if (versions
->any_needs())
1067 const char* vnname
= this->namepool_
.add(".gnu.version_r", NULL
);
1068 Output_section
* vnsec
;
1069 vnsec
= this->make_output_section(vnname
, elfcpp::SHT_GNU_verneed
,
1072 unsigned char* vnbuf
;
1073 unsigned int vnsize
;
1074 unsigned int vnentries
;
1075 versions
->need_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)
1076 (&this->dynpool_
, &vnbuf
, &vnsize
, &vnentries
1077 SELECT_SIZE_ENDIAN(size
, big_endian
));
1079 Output_section_data
* vndata
= new Output_data_const_buffer(vnbuf
,
1083 vnsec
->add_output_section_data(vndata
);
1084 vnsec
->set_link_section(dynstr
);
1085 vnsec
->set_info(vnentries
);
1087 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
1088 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
1092 // Create the .interp section and PT_INTERP segment.
1095 Layout::create_interp(const Target
* target
)
1097 const char* interp
= this->options_
.dynamic_linker();
1100 interp
= target
->dynamic_linker();
1101 gold_assert(interp
!= NULL
);
1104 size_t len
= strlen(interp
) + 1;
1106 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
1108 const char* interp_name
= this->namepool_
.add(".interp", NULL
);
1109 Output_section
* osec
= this->make_output_section(interp_name
,
1110 elfcpp::SHT_PROGBITS
,
1112 osec
->add_output_section_data(odata
);
1114 Output_segment
* oseg
= new Output_segment(elfcpp::PT_INTERP
, elfcpp::PF_R
);
1115 this->segment_list_
.push_back(oseg
);
1116 oseg
->add_initial_output_section(osec
, elfcpp::PF_R
);
1119 // Finish the .dynamic section and PT_DYNAMIC segment.
1122 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
1123 const Symbol_table
* symtab
)
1125 Output_segment
* oseg
= new Output_segment(elfcpp::PT_DYNAMIC
,
1126 elfcpp::PF_R
| elfcpp::PF_W
);
1127 this->segment_list_
.push_back(oseg
);
1128 oseg
->add_initial_output_section(this->dynamic_section_
,
1129 elfcpp::PF_R
| elfcpp::PF_W
);
1131 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1133 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
1134 p
!= input_objects
->dynobj_end();
1137 // FIXME: Handle --as-needed.
1138 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
1141 // FIXME: Support --init and --fini.
1142 Symbol
* sym
= symtab
->lookup("_init");
1143 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
1144 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
1146 sym
= symtab
->lookup("_fini");
1147 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
1148 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
1150 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1153 // The mapping of .gnu.linkonce section names to real section names.
1155 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1156 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
1158 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1159 MAPPING_INIT("t", ".text"),
1160 MAPPING_INIT("r", ".rodata"),
1161 MAPPING_INIT("d", ".data"),
1162 MAPPING_INIT("b", ".bss"),
1163 MAPPING_INIT("s", ".sdata"),
1164 MAPPING_INIT("sb", ".sbss"),
1165 MAPPING_INIT("s2", ".sdata2"),
1166 MAPPING_INIT("sb2", ".sbss2"),
1167 MAPPING_INIT("wi", ".debug_info"),
1168 MAPPING_INIT("td", ".tdata"),
1169 MAPPING_INIT("tb", ".tbss"),
1170 MAPPING_INIT("lr", ".lrodata"),
1171 MAPPING_INIT("l", ".ldata"),
1172 MAPPING_INIT("lb", ".lbss"),
1176 const int Layout::linkonce_mapping_count
=
1177 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
1179 // Return the name of the output section to use for a .gnu.linkonce
1180 // section. This is based on the default ELF linker script of the old
1181 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1182 // to ".text". Set *PLEN to the length of the name. *PLEN is
1183 // initialized to the length of NAME.
1186 Layout::linkonce_output_name(const char* name
, size_t *plen
)
1188 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
1192 const Linkonce_mapping
* plm
= linkonce_mapping
;
1193 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
1195 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
1204 // Choose the output section name to use given an input section name.
1205 // Set *PLEN to the length of the name. *PLEN is initialized to the
1209 Layout::output_section_name(const char* name
, size_t* plen
)
1211 if (Layout::is_linkonce(name
))
1213 // .gnu.linkonce sections are laid out as though they were named
1214 // for the sections are placed into.
1215 return Layout::linkonce_output_name(name
, plen
);
1218 // If the section name has no '.', or only an initial '.', we use
1219 // the name unchanged (i.e., ".text" is unchanged).
1221 // Otherwise, if the section name does not include ".rel", we drop
1222 // the last '.' and everything that follows (i.e., ".text.XXX"
1223 // becomes ".text").
1225 // Otherwise, if the section name has zero or one '.' after the
1226 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1229 // Otherwise, we drop the last '.' and everything that follows
1230 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1232 const char* s
= name
;
1235 const char* sdot
= strchr(s
, '.');
1239 const char* srel
= strstr(s
, ".rel");
1242 *plen
= sdot
- name
;
1246 sdot
= strchr(srel
+ 1, '.');
1249 sdot
= strchr(sdot
+ 1, '.');
1253 *plen
= sdot
- name
;
1257 // Record the signature of a comdat section, and return whether to
1258 // include it in the link. If GROUP is true, this is a regular
1259 // section group. If GROUP is false, this is a group signature
1260 // derived from the name of a linkonce section. We want linkonce
1261 // signatures and group signatures to block each other, but we don't
1262 // want a linkonce signature to block another linkonce signature.
1265 Layout::add_comdat(const char* signature
, bool group
)
1267 std::string
sig(signature
);
1268 std::pair
<Signatures::iterator
, bool> ins(
1269 this->signatures_
.insert(std::make_pair(sig
, group
)));
1273 // This is the first time we've seen this signature.
1277 if (ins
.first
->second
)
1279 // We've already seen a real section group with this signature.
1284 // This is a real section group, and we've already seen a
1285 // linkonce section with tihs signature. Record that we've seen
1286 // a section group, and don't include this section group.
1287 ins
.first
->second
= true;
1292 // We've already seen a linkonce section and this is a linkonce
1293 // section. These don't block each other--this may be the same
1294 // symbol name with different section types.
1299 // Write out data not associated with a section or the symbol table.
1302 Layout::write_data(const Symbol_table
* symtab
, const Target
* target
,
1303 Output_file
* of
) const
1305 const Output_section
* symtab_section
= this->symtab_section_
;
1306 for (Section_list::const_iterator p
= this->section_list_
.begin();
1307 p
!= this->section_list_
.end();
1310 if ((*p
)->needs_symtab_index())
1312 gold_assert(symtab_section
!= NULL
);
1313 unsigned int index
= (*p
)->symtab_index();
1314 gold_assert(index
> 0 && index
!= -1U);
1315 off_t off
= (symtab_section
->offset()
1316 + index
* symtab_section
->entsize());
1317 symtab
->write_section_symbol(target
, *p
, of
, off
);
1321 const Output_section
* dynsym_section
= this->dynsym_section_
;
1322 for (Section_list::const_iterator p
= this->section_list_
.begin();
1323 p
!= this->section_list_
.end();
1326 if ((*p
)->needs_dynsym_index())
1328 gold_assert(dynsym_section
!= NULL
);
1329 unsigned int index
= (*p
)->dynsym_index();
1330 gold_assert(index
> 0 && index
!= -1U);
1331 off_t off
= (dynsym_section
->offset()
1332 + index
* dynsym_section
->entsize());
1333 symtab
->write_section_symbol(target
, *p
, of
, off
);
1337 // Write out the Output_sections. Most won't have anything to
1338 // write, since most of the data will come from input sections which
1339 // are handled elsewhere. But some Output_sections do have
1341 for (Section_list::const_iterator p
= this->section_list_
.begin();
1342 p
!= this->section_list_
.end();
1346 // Write out the Output_data which are not in an Output_section.
1347 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
1348 p
!= this->special_output_list_
.end();
1353 // Write_data_task methods.
1355 // We can always run this task.
1357 Task::Is_runnable_type
1358 Write_data_task::is_runnable(Workqueue
*)
1363 // We need to unlock FINAL_BLOCKER when finished.
1366 Write_data_task::locks(Workqueue
* workqueue
)
1368 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1371 // Run the task--write out the data.
1374 Write_data_task::run(Workqueue
*)
1376 this->layout_
->write_data(this->symtab_
, this->target_
, this->of_
);
1379 // Write_symbols_task methods.
1381 // We can always run this task.
1383 Task::Is_runnable_type
1384 Write_symbols_task::is_runnable(Workqueue
*)
1389 // We need to unlock FINAL_BLOCKER when finished.
1392 Write_symbols_task::locks(Workqueue
* workqueue
)
1394 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1397 // Run the task--write out the symbols.
1400 Write_symbols_task::run(Workqueue
*)
1402 this->symtab_
->write_globals(this->target_
, this->sympool_
, this->dynpool_
,
1406 // Close_task_runner methods.
1408 // Run the task--close the file.
1411 Close_task_runner::run(Workqueue
*)
1416 // Instantiate the templates we need. We could use the configure
1417 // script to restrict this to only the ones for implemented targets.
1421 Layout::layout
<32, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1422 const elfcpp::Shdr
<32, false>& shdr
, off_t
*);
1426 Layout::layout
<32, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1427 const elfcpp::Shdr
<32, true>& shdr
, off_t
*);
1431 Layout::layout
<64, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1432 const elfcpp::Shdr
<64, false>& shdr
, off_t
*);
1436 Layout::layout
<64, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1437 const elfcpp::Shdr
<64, true>& shdr
, off_t
*);
1440 } // End namespace gold.