]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/layout.cc
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
), dynsym_section_(NULL
)
47 // Make space for more than enough segments for a typical file.
48 // This is just for efficiency--it's OK if we wind up needing more.
49 this->segment_list_
.reserve(12);
51 // We expect three unattached Output_data objects: the file header,
52 // the segment headers, and the section headers.
53 this->special_output_list_
.reserve(3);
56 // Hash a key we use to look up an output section mapping.
59 Layout::Hash_key::operator()(const Layout::Key
& k
) const
61 return k
.first
+ k
.second
.first
+ k
.second
.second
;
64 // Whether to include this section in the link.
66 template<int size
, bool big_endian
>
68 Layout::include_section(Object
*, const char*,
69 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
71 // Some section types are never linked. Some are only linked when
72 // doing a relocateable link.
73 switch (shdr
.get_sh_type())
75 case elfcpp::SHT_NULL
:
76 case elfcpp::SHT_SYMTAB
:
77 case elfcpp::SHT_DYNSYM
:
78 case elfcpp::SHT_STRTAB
:
79 case elfcpp::SHT_HASH
:
80 case elfcpp::SHT_DYNAMIC
:
81 case elfcpp::SHT_SYMTAB_SHNDX
:
84 case elfcpp::SHT_RELA
:
86 case elfcpp::SHT_GROUP
:
87 return this->options_
.is_relocatable();
90 // FIXME: Handle stripping debug sections here.
95 // Return an output section named NAME, or NULL if there is none.
98 Layout::find_output_section(const char* name
) const
100 for (Section_name_map::const_iterator p
= this->section_name_map_
.begin();
101 p
!= this->section_name_map_
.end();
103 if (strcmp(p
->second
->name(), name
) == 0)
108 // Return an output segment of type TYPE, with segment flags SET set
109 // and segment flags CLEAR clear. Return NULL if there is none.
112 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
113 elfcpp::Elf_Word clear
) const
115 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
116 p
!= this->segment_list_
.end();
118 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
119 && ((*p
)->flags() & set
) == set
120 && ((*p
)->flags() & clear
) == 0)
125 // Return the output section to use for section NAME with type TYPE
126 // and section flags FLAGS.
129 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
130 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
132 // We should ignore some flags.
133 flags
&= ~ (elfcpp::SHF_INFO_LINK
134 | elfcpp::SHF_LINK_ORDER
135 | elfcpp::SHF_GROUP
);
137 const Key
key(name_key
, std::make_pair(type
, flags
));
138 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
139 std::pair
<Section_name_map::iterator
, bool> ins(
140 this->section_name_map_
.insert(v
));
143 return ins
.first
->second
;
146 // This is the first time we've seen this name/type/flags
148 Output_section
* os
= this->make_output_section(name
, type
, flags
);
149 ins
.first
->second
= os
;
154 // Return the output section to use for input section SHNDX, with name
155 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
156 // offset of this input section without the output section.
158 template<int size
, bool big_endian
>
160 Layout::layout(Relobj
* object
, unsigned int shndx
, const char* name
,
161 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* off
)
163 if (!this->include_section(object
, name
, shdr
))
166 // If we are not doing a relocateable link, choose the name to use
167 // for the output section.
168 size_t len
= strlen(name
);
169 if (!this->options_
.is_relocatable())
170 name
= Layout::output_section_name(name
, &len
);
172 // FIXME: Handle SHF_OS_NONCONFORMING here.
174 // Canonicalize the section name.
175 Stringpool::Key name_key
;
176 name
= this->namepool_
.add(name
, len
, &name_key
);
178 // Find the output section. The output section is selected based on
179 // the section name, type, and flags.
180 Output_section
* os
= this->get_output_section(name
, name_key
,
182 shdr
.get_sh_flags());
184 // FIXME: Handle SHF_LINK_ORDER somewhere.
186 *off
= os
->add_input_section(object
, shndx
, name
, shdr
);
191 // Add POSD to an output section using NAME, TYPE, and FLAGS.
194 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
195 elfcpp::Elf_Xword flags
,
196 Output_section_data
* posd
)
198 // Canonicalize the name.
199 Stringpool::Key name_key
;
200 name
= this->namepool_
.add(name
, &name_key
);
202 Output_section
* os
= this->get_output_section(name
, name_key
, type
, flags
);
203 os
->add_output_section_data(posd
);
206 // Map section flags to segment flags.
209 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
211 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
212 if ((flags
& elfcpp::SHF_WRITE
) != 0)
214 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
219 // Make a new Output_section, and attach it to segments as
223 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
224 elfcpp::Elf_Xword flags
)
226 Output_section
* os
= new Output_section(name
, type
, flags
, true);
227 this->section_list_
.push_back(os
);
229 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
230 this->unattached_section_list_
.push_back(os
);
233 // This output section goes into a PT_LOAD segment.
235 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
237 // The only thing we really care about for PT_LOAD segments is
238 // whether or not they are writable, so that is how we search
239 // for them. People who need segments sorted on some other
240 // basis will have to wait until we implement a mechanism for
241 // them to describe the segments they want.
243 Segment_list::const_iterator p
;
244 for (p
= this->segment_list_
.begin();
245 p
!= this->segment_list_
.end();
248 if ((*p
)->type() == elfcpp::PT_LOAD
249 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
251 (*p
)->add_output_section(os
, seg_flags
);
256 if (p
== this->segment_list_
.end())
258 Output_segment
* oseg
= new Output_segment(elfcpp::PT_LOAD
,
260 this->segment_list_
.push_back(oseg
);
261 oseg
->add_output_section(os
, seg_flags
);
264 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
266 if (type
== elfcpp::SHT_NOTE
)
268 // See if we already have an equivalent PT_NOTE segment.
269 for (p
= this->segment_list_
.begin();
270 p
!= segment_list_
.end();
273 if ((*p
)->type() == elfcpp::PT_NOTE
274 && (((*p
)->flags() & elfcpp::PF_W
)
275 == (seg_flags
& elfcpp::PF_W
)))
277 (*p
)->add_output_section(os
, seg_flags
);
282 if (p
== this->segment_list_
.end())
284 Output_segment
* oseg
= new Output_segment(elfcpp::PT_NOTE
,
286 this->segment_list_
.push_back(oseg
);
287 oseg
->add_output_section(os
, seg_flags
);
291 // If we see a loadable SHF_TLS section, we create a PT_TLS
292 // segment. There can only be one such segment.
293 if ((flags
& elfcpp::SHF_TLS
) != 0)
295 if (this->tls_segment_
== NULL
)
297 this->tls_segment_
= new Output_segment(elfcpp::PT_TLS
,
299 this->segment_list_
.push_back(this->tls_segment_
);
301 this->tls_segment_
->add_output_section(os
, seg_flags
);
308 // Create the dynamic sections which are needed before we read the
312 Layout::create_initial_dynamic_sections(const Input_objects
* input_objects
,
313 Symbol_table
* symtab
)
315 if (!input_objects
->any_dynamic())
318 const char* dynamic_name
= this->namepool_
.add(".dynamic", NULL
);
319 this->dynamic_section_
= this->make_output_section(dynamic_name
,
322 | elfcpp::SHF_WRITE
));
324 symtab
->define_in_output_data(input_objects
->target(), "_DYNAMIC",
325 this->dynamic_section_
, 0, 0,
326 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
327 elfcpp::STV_HIDDEN
, 0, false, false);
330 // Find the first read-only PT_LOAD segment, creating one if
334 Layout::find_first_load_seg()
336 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
337 p
!= this->segment_list_
.end();
340 if ((*p
)->type() == elfcpp::PT_LOAD
341 && ((*p
)->flags() & elfcpp::PF_R
) != 0
342 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
346 Output_segment
* load_seg
= new Output_segment(elfcpp::PT_LOAD
, elfcpp::PF_R
);
347 this->segment_list_
.push_back(load_seg
);
351 // Finalize the layout. When this is called, we have created all the
352 // output sections and all the output segments which are based on
353 // input sections. We have several things to do, and we have to do
354 // them in the right order, so that we get the right results correctly
357 // 1) Finalize the list of output segments and create the segment
360 // 2) Finalize the dynamic symbol table and associated sections.
362 // 3) Determine the final file offset of all the output segments.
364 // 4) Determine the final file offset of all the SHF_ALLOC output
367 // 5) Create the symbol table sections and the section name table
370 // 6) Finalize the symbol table: set symbol values to their final
371 // value and make a final determination of which symbols are going
372 // into the output symbol table.
374 // 7) Create the section table header.
376 // 8) Determine the final file offset of all the output sections which
377 // are not SHF_ALLOC, including the section table header.
379 // 9) Finalize the ELF file header.
381 // This function returns the size of the output file.
384 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
)
386 const Target
* const target
= input_objects
->target();
387 const int size
= target
->get_size();
389 Output_segment
* phdr_seg
= NULL
;
390 if (input_objects
->any_dynamic())
392 // There was a dynamic object in the link. We need to create
393 // some information for the dynamic linker.
395 // Create the PT_PHDR segment which will hold the program
397 phdr_seg
= new Output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
398 this->segment_list_
.push_back(phdr_seg
);
400 // This holds the dynamic tags.
401 Output_data_dynamic
* odyn
;
402 odyn
= new Output_data_dynamic(input_objects
->target(),
405 // Create the dynamic symbol table, including the hash table,
406 // the dynamic relocations, and the version sections.
407 this->create_dynamic_symtab(target
, odyn
, symtab
);
409 // Create the .interp section to hold the name of the
410 // interpreter, and put it in a PT_INTERP segment.
411 this->create_interp(target
);
413 // Finish the .dynamic section to hold the dynamic data, and put
414 // it in a PT_DYNAMIC segment.
415 this->finish_dynamic_section(input_objects
, symtab
, odyn
);
418 // FIXME: Handle PT_GNU_STACK.
420 Output_segment
* load_seg
= this->find_first_load_seg();
422 // Lay out the segment headers.
423 bool big_endian
= target
->is_big_endian();
424 Output_segment_headers
* segment_headers
;
425 segment_headers
= new Output_segment_headers(size
, big_endian
,
426 this->segment_list_
);
427 load_seg
->add_initial_output_data(segment_headers
);
428 this->special_output_list_
.push_back(segment_headers
);
429 if (phdr_seg
!= NULL
)
430 phdr_seg
->add_initial_output_data(segment_headers
);
432 // Lay out the file header.
433 Output_file_header
* file_header
;
434 file_header
= new Output_file_header(size
,
440 load_seg
->add_initial_output_data(file_header
);
441 this->special_output_list_
.push_back(file_header
);
443 // We set the output section indexes in set_segment_offsets and
444 // set_section_offsets.
445 unsigned int shndx
= 1;
447 // Set the file offsets of all the segments, and all the sections
449 off_t off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
451 // Create the symbol table sections.
452 // FIXME: We don't need to do this if we are stripping symbols.
453 Output_section
* ostrtab
;
454 this->create_symtab_sections(size
, input_objects
, symtab
, &off
,
457 // Create the .shstrtab section.
458 Output_section
* shstrtab_section
= this->create_shstrtab();
460 // Set the file offsets of all the sections not associated with
462 off
= this->set_section_offsets(off
, &shndx
);
464 // Now the section index of OSTRTAB is set.
465 this->symtab_section_
->set_link(ostrtab
->out_shndx());
467 // Create the section table header.
468 Output_section_headers
* oshdrs
= this->create_shdrs(size
, big_endian
, &off
);
470 file_header
->set_section_info(oshdrs
, shstrtab_section
);
472 // Now we know exactly where everything goes in the output file.
473 Output_data::layout_complete();
478 // Return whether SEG1 should be before SEG2 in the output file. This
479 // is based entirely on the segment type and flags. When this is
480 // called the segment addresses has normally not yet been set.
483 Layout::segment_precedes(const Output_segment
* seg1
,
484 const Output_segment
* seg2
)
486 elfcpp::Elf_Word type1
= seg1
->type();
487 elfcpp::Elf_Word type2
= seg2
->type();
489 // The single PT_PHDR segment is required to precede any loadable
490 // segment. We simply make it always first.
491 if (type1
== elfcpp::PT_PHDR
)
493 gold_assert(type2
!= elfcpp::PT_PHDR
);
496 if (type2
== elfcpp::PT_PHDR
)
499 // The single PT_INTERP segment is required to precede any loadable
500 // segment. We simply make it always second.
501 if (type1
== elfcpp::PT_INTERP
)
503 gold_assert(type2
!= elfcpp::PT_INTERP
);
506 if (type2
== elfcpp::PT_INTERP
)
509 // We then put PT_LOAD segments before any other segments.
510 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
512 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
515 // We put the PT_TLS segment last, because that is where the dynamic
516 // linker expects to find it (this is just for efficiency; other
517 // positions would also work correctly).
518 if (type1
== elfcpp::PT_TLS
&& type2
!= elfcpp::PT_TLS
)
520 if (type2
== elfcpp::PT_TLS
&& type1
!= elfcpp::PT_TLS
)
523 const elfcpp::Elf_Word flags1
= seg1
->flags();
524 const elfcpp::Elf_Word flags2
= seg2
->flags();
526 // The order of non-PT_LOAD segments is unimportant. We simply sort
527 // by the numeric segment type and flags values. There should not
528 // be more than one segment with the same type and flags.
529 if (type1
!= elfcpp::PT_LOAD
)
532 return type1
< type2
;
533 gold_assert(flags1
!= flags2
);
534 return flags1
< flags2
;
537 // We sort PT_LOAD segments based on the flags. Readonly segments
538 // come before writable segments. Then executable segments come
539 // before non-executable segments. Then the unlikely case of a
540 // non-readable segment comes before the normal case of a readable
541 // segment. If there are multiple segments with the same type and
542 // flags, we require that the address be set, and we sort by
543 // virtual address and then physical address.
544 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
545 return (flags1
& elfcpp::PF_W
) == 0;
546 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
547 return (flags1
& elfcpp::PF_X
) != 0;
548 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
549 return (flags1
& elfcpp::PF_R
) == 0;
551 uint64_t vaddr1
= seg1
->vaddr();
552 uint64_t vaddr2
= seg2
->vaddr();
553 if (vaddr1
!= vaddr2
)
554 return vaddr1
< vaddr2
;
556 uint64_t paddr1
= seg1
->paddr();
557 uint64_t paddr2
= seg2
->paddr();
558 gold_assert(paddr1
!= paddr2
);
559 return paddr1
< paddr2
;
562 // Set the file offsets of all the segments, and all the sections they
563 // contain. They have all been created. LOAD_SEG must be be laid out
564 // first. Return the offset of the data to follow.
567 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
568 unsigned int *pshndx
)
570 // Sort them into the final order.
571 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
572 Layout::Compare_segments());
574 // Find the PT_LOAD segments, and set their addresses and offsets
575 // and their section's addresses and offsets.
576 uint64_t addr
= target
->text_segment_address();
578 bool was_readonly
= false;
579 for (Segment_list::iterator p
= this->segment_list_
.begin();
580 p
!= this->segment_list_
.end();
583 if ((*p
)->type() == elfcpp::PT_LOAD
)
585 if (load_seg
!= NULL
&& load_seg
!= *p
)
589 // If the last segment was readonly, and this one is not,
590 // then skip the address forward one page, maintaining the
591 // same position within the page. This lets us store both
592 // segments overlapping on a single page in the file, but
593 // the loader will put them on different pages in memory.
595 uint64_t orig_addr
= addr
;
596 uint64_t orig_off
= off
;
598 uint64_t aligned_addr
= addr
;
599 uint64_t abi_pagesize
= target
->abi_pagesize();
600 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
602 uint64_t align
= (*p
)->addralign();
604 addr
= align_address(addr
, align
);
606 if ((addr
& (abi_pagesize
- 1)) != 0)
607 addr
= addr
+ abi_pagesize
;
610 unsigned int shndx_hold
= *pshndx
;
611 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
612 uint64_t new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
614 // Now that we know the size of this segment, we may be able
615 // to save a page in memory, at the cost of wasting some
616 // file space, by instead aligning to the start of a new
617 // page. Here we use the real machine page size rather than
618 // the ABI mandated page size.
620 if (aligned_addr
!= addr
)
622 uint64_t common_pagesize
= target
->common_pagesize();
623 uint64_t first_off
= (common_pagesize
625 & (common_pagesize
- 1)));
626 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
629 && ((aligned_addr
& ~ (common_pagesize
- 1))
630 != (new_addr
& ~ (common_pagesize
- 1)))
631 && first_off
+ last_off
<= common_pagesize
)
633 *pshndx
= shndx_hold
;
634 addr
= align_address(aligned_addr
, common_pagesize
);
635 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
636 new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
642 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
647 // Handle the non-PT_LOAD segments, setting their offsets from their
648 // section's offsets.
649 for (Segment_list::iterator p
= this->segment_list_
.begin();
650 p
!= this->segment_list_
.end();
653 if ((*p
)->type() != elfcpp::PT_LOAD
)
660 // Set the file offset of all the sections not associated with a
664 Layout::set_section_offsets(off_t off
, unsigned int* pshndx
)
666 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
667 p
!= this->unattached_section_list_
.end();
670 (*p
)->set_out_shndx(*pshndx
);
672 if ((*p
)->offset() != -1)
674 off
= align_address(off
, (*p
)->addralign());
675 (*p
)->set_address(0, off
);
676 off
+= (*p
)->data_size();
681 // Create the symbol table sections.
684 Layout::create_symtab_sections(int size
, const Input_objects
* input_objects
,
685 Symbol_table
* symtab
,
687 Output_section
** postrtab
)
693 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
698 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
705 off
= align_address(off
, align
);
706 off_t startoff
= off
;
708 // Save space for the dummy symbol at the start of the section. We
709 // never bother to write this out--it will just be left as zero.
711 unsigned int local_symbol_index
= 1;
713 // Add STT_SECTION symbols for each Output section which needs one.
714 for (Section_list::iterator p
= this->section_list_
.begin();
715 p
!= this->section_list_
.end();
718 if (!(*p
)->needs_symtab_index())
719 (*p
)->set_symtab_index(-1U);
722 (*p
)->set_symtab_index(local_symbol_index
);
723 ++local_symbol_index
;
728 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
729 p
!= input_objects
->relobj_end();
732 Task_lock_obj
<Object
> tlo(**p
);
733 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
736 off
+= (index
- local_symbol_index
) * symsize
;
737 local_symbol_index
= index
;
740 unsigned int local_symcount
= local_symbol_index
;
741 gold_assert(local_symcount
* symsize
== off
- startoff
);
743 off
= symtab
->finalize(local_symcount
, off
, &this->sympool_
);
745 this->sympool_
.set_string_offsets();
747 const char* symtab_name
= this->namepool_
.add(".symtab", NULL
);
748 Output_section
* osymtab
= this->make_output_section(symtab_name
,
751 this->symtab_section_
= osymtab
;
753 Output_section_data
* pos
= new Output_data_space(off
- startoff
,
755 osymtab
->add_output_section_data(pos
);
757 const char* strtab_name
= this->namepool_
.add(".strtab", NULL
);
758 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
762 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
763 ostrtab
->add_output_section_data(pstr
);
765 osymtab
->set_address(0, startoff
);
766 osymtab
->set_info(local_symcount
);
767 osymtab
->set_entsize(symsize
);
773 // Create the .shstrtab section, which holds the names of the
774 // sections. At the time this is called, we have created all the
775 // output sections except .shstrtab itself.
778 Layout::create_shstrtab()
780 // FIXME: We don't need to create a .shstrtab section if we are
781 // stripping everything.
783 const char* name
= this->namepool_
.add(".shstrtab", NULL
);
785 this->namepool_
.set_string_offsets();
787 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
789 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
790 os
->add_output_section_data(posd
);
795 // Create the section headers. SIZE is 32 or 64. OFF is the file
798 Output_section_headers
*
799 Layout::create_shdrs(int size
, bool big_endian
, off_t
* poff
)
801 Output_section_headers
* oshdrs
;
802 oshdrs
= new Output_section_headers(size
, big_endian
, this->segment_list_
,
803 this->unattached_section_list_
,
805 off_t off
= align_address(*poff
, oshdrs
->addralign());
806 oshdrs
->set_address(0, off
);
807 off
+= oshdrs
->data_size();
809 this->special_output_list_
.push_back(oshdrs
);
813 // Create the dynamic symbol table.
816 Layout::create_dynamic_symtab(const Target
* target
, Output_data_dynamic
* odyn
,
817 Symbol_table
* symtab
)
819 // Count all the symbols in the dynamic symbol table, and set the
820 // dynamic symbol indexes.
822 // Skip symbol 0, which is always all zeroes.
823 unsigned int index
= 1;
825 // Add STT_SECTION symbols for each Output section which needs one.
826 for (Section_list::iterator p
= this->section_list_
.begin();
827 p
!= this->section_list_
.end();
830 if (!(*p
)->needs_dynsym_index())
831 (*p
)->set_dynsym_index(-1U);
834 (*p
)->set_dynsym_index(index
);
839 // FIXME: Some targets apparently require local symbols in the
840 // dynamic symbol table. Here is where we will have to count them,
841 // and set the dynamic symbol indexes, and add the names to
844 unsigned int local_symcount
= index
;
846 std::vector
<Symbol
*> dynamic_symbols
;
848 // FIXME: We have to tell set_dynsym_indexes whether the
849 // -E/--export-dynamic option was used.
850 index
= symtab
->set_dynsym_indexes(index
, &dynamic_symbols
,
855 const int size
= target
->get_size();
858 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
863 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
869 const char* dynsym_name
= this->namepool_
.add(".dynsym", NULL
);
870 Output_section
* dynsym
= this->make_output_section(dynsym_name
,
874 Output_section_data
* odata
= new Output_data_space(index
* symsize
,
876 dynsym
->add_output_section_data(odata
);
878 dynsym
->set_info(local_symcount
);
879 dynsym
->set_entsize(symsize
);
880 dynsym
->set_addralign(align
);
882 this->dynsym_section_
= dynsym
;
884 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
885 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
887 const char* dynstr_name
= this->namepool_
.add(".dynstr", NULL
);
888 Output_section
* dynstr
= this->make_output_section(dynstr_name
,
892 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
893 dynstr
->add_output_section_data(strdata
);
895 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
896 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
898 // FIXME: We need an option to create a GNU hash table.
900 unsigned char* phash
;
901 unsigned int hashlen
;
902 Dynobj::create_elf_hash_table(target
, dynamic_symbols
, local_symcount
,
905 const char* hash_name
= this->namepool_
.add(".hash", NULL
);
906 Output_section
* hashsec
= this->make_output_section(hash_name
,
910 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
913 hashsec
->add_output_section_data(hashdata
);
915 hashsec
->set_entsize(4);
916 // FIXME: .hash should link to .dynsym.
918 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
921 // Create the .interp section and PT_INTERP segment.
924 Layout::create_interp(const Target
* target
)
926 const char* interp
= this->options_
.dynamic_linker();
929 interp
= target
->dynamic_linker();
930 gold_assert(interp
!= NULL
);
933 size_t len
= strlen(interp
) + 1;
935 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
937 const char* interp_name
= this->namepool_
.add(".interp", NULL
);
938 Output_section
* osec
= this->make_output_section(interp_name
,
939 elfcpp::SHT_PROGBITS
,
941 osec
->add_output_section_data(odata
);
943 Output_segment
* oseg
= new Output_segment(elfcpp::PT_INTERP
, elfcpp::PF_R
);
944 this->segment_list_
.push_back(oseg
);
945 oseg
->add_initial_output_section(osec
, elfcpp::PF_R
);
948 // Finish the .dynamic section and PT_DYNAMIC segment.
951 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
952 const Symbol_table
* symtab
,
953 Output_data_dynamic
* odyn
)
955 this->dynamic_section_
->add_output_section_data(odyn
);
957 Output_segment
* oseg
= new Output_segment(elfcpp::PT_DYNAMIC
,
958 elfcpp::PF_R
| elfcpp::PF_W
);
959 this->segment_list_
.push_back(oseg
);
960 oseg
->add_initial_output_section(this->dynamic_section_
,
961 elfcpp::PF_R
| elfcpp::PF_W
);
963 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
964 p
!= input_objects
->dynobj_end();
967 // FIXME: Handle --as-needed.
968 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
971 // FIXME: Support --init and --fini.
972 Symbol
* sym
= symtab
->lookup("_init");
973 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_defined_in_dynobj())
974 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
976 sym
= symtab
->lookup("_fini");
977 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_defined_in_dynobj())
978 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
980 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
983 // The mapping of .gnu.linkonce section names to real section names.
985 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
986 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
988 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
989 MAPPING_INIT("t", ".text"),
990 MAPPING_INIT("r", ".rodata"),
991 MAPPING_INIT("d", ".data"),
992 MAPPING_INIT("b", ".bss"),
993 MAPPING_INIT("s", ".sdata"),
994 MAPPING_INIT("sb", ".sbss"),
995 MAPPING_INIT("s2", ".sdata2"),
996 MAPPING_INIT("sb2", ".sbss2"),
997 MAPPING_INIT("wi", ".debug_info"),
998 MAPPING_INIT("td", ".tdata"),
999 MAPPING_INIT("tb", ".tbss"),
1000 MAPPING_INIT("lr", ".lrodata"),
1001 MAPPING_INIT("l", ".ldata"),
1002 MAPPING_INIT("lb", ".lbss"),
1006 const int Layout::linkonce_mapping_count
=
1007 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
1009 // Return the name of the output section to use for a .gnu.linkonce
1010 // section. This is based on the default ELF linker script of the old
1011 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1012 // to ".text". Set *PLEN to the length of the name. *PLEN is
1013 // initialized to the length of NAME.
1016 Layout::linkonce_output_name(const char* name
, size_t *plen
)
1018 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
1022 const Linkonce_mapping
* plm
= linkonce_mapping
;
1023 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
1025 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
1034 // Choose the output section name to use given an input section name.
1035 // Set *PLEN to the length of the name. *PLEN is initialized to the
1039 Layout::output_section_name(const char* name
, size_t* plen
)
1041 if (Layout::is_linkonce(name
))
1043 // .gnu.linkonce sections are laid out as though they were named
1044 // for the sections are placed into.
1045 return Layout::linkonce_output_name(name
, plen
);
1048 // If the section name has no '.', or only an initial '.', we use
1049 // the name unchanged (i.e., ".text" is unchanged).
1051 // Otherwise, if the section name does not include ".rel", we drop
1052 // the last '.' and everything that follows (i.e., ".text.XXX"
1053 // becomes ".text").
1055 // Otherwise, if the section name has zero or one '.' after the
1056 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1059 // Otherwise, we drop the last '.' and everything that follows
1060 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1062 const char* s
= name
;
1065 const char* sdot
= strchr(s
, '.');
1069 const char* srel
= strstr(s
, ".rel");
1072 *plen
= sdot
- name
;
1076 sdot
= strchr(srel
+ 1, '.');
1079 sdot
= strchr(sdot
+ 1, '.');
1083 *plen
= sdot
- name
;
1087 // Record the signature of a comdat section, and return whether to
1088 // include it in the link. If GROUP is true, this is a regular
1089 // section group. If GROUP is false, this is a group signature
1090 // derived from the name of a linkonce section. We want linkonce
1091 // signatures and group signatures to block each other, but we don't
1092 // want a linkonce signature to block another linkonce signature.
1095 Layout::add_comdat(const char* signature
, bool group
)
1097 std::string
sig(signature
);
1098 std::pair
<Signatures::iterator
, bool> ins(
1099 this->signatures_
.insert(std::make_pair(sig
, group
)));
1103 // This is the first time we've seen this signature.
1107 if (ins
.first
->second
)
1109 // We've already seen a real section group with this signature.
1114 // This is a real section group, and we've already seen a
1115 // linkonce section with tihs signature. Record that we've seen
1116 // a section group, and don't include this section group.
1117 ins
.first
->second
= true;
1122 // We've already seen a linkonce section and this is a linkonce
1123 // section. These don't block each other--this may be the same
1124 // symbol name with different section types.
1129 // Write out data not associated with a section or the symbol table.
1132 Layout::write_data(const Symbol_table
* symtab
, const Target
* target
,
1133 Output_file
* of
) const
1135 const Output_section
* symtab_section
= this->symtab_section_
;
1136 for (Section_list::const_iterator p
= this->section_list_
.begin();
1137 p
!= this->section_list_
.end();
1140 if ((*p
)->needs_symtab_index())
1142 gold_assert(symtab_section
!= NULL
);
1143 unsigned int index
= (*p
)->symtab_index();
1144 gold_assert(index
> 0 && index
!= -1U);
1145 off_t off
= (symtab_section
->offset()
1146 + index
* symtab_section
->entsize());
1147 symtab
->write_section_symbol(target
, *p
, of
, off
);
1151 const Output_section
* dynsym_section
= this->dynsym_section_
;
1152 for (Section_list::const_iterator p
= this->section_list_
.begin();
1153 p
!= this->section_list_
.end();
1156 if ((*p
)->needs_dynsym_index())
1158 gold_assert(dynsym_section
!= NULL
);
1159 unsigned int index
= (*p
)->dynsym_index();
1160 gold_assert(index
> 0 && index
!= -1U);
1161 off_t off
= (dynsym_section
->offset()
1162 + index
* dynsym_section
->entsize());
1163 symtab
->write_section_symbol(target
, *p
, of
, off
);
1167 // Write out the Output_sections. Most won't have anything to
1168 // write, since most of the data will come from input sections which
1169 // are handled elsewhere. But some Output_sections do have
1171 for (Section_list::const_iterator p
= this->section_list_
.begin();
1172 p
!= this->section_list_
.end();
1176 // Write out the Output_data which are not in an Output_section.
1177 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
1178 p
!= this->special_output_list_
.end();
1183 // Write_data_task methods.
1185 // We can always run this task.
1187 Task::Is_runnable_type
1188 Write_data_task::is_runnable(Workqueue
*)
1193 // We need to unlock FINAL_BLOCKER when finished.
1196 Write_data_task::locks(Workqueue
* workqueue
)
1198 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1201 // Run the task--write out the data.
1204 Write_data_task::run(Workqueue
*)
1206 this->layout_
->write_data(this->symtab_
, this->target_
, this->of_
);
1209 // Write_symbols_task methods.
1211 // We can always run this task.
1213 Task::Is_runnable_type
1214 Write_symbols_task::is_runnable(Workqueue
*)
1219 // We need to unlock FINAL_BLOCKER when finished.
1222 Write_symbols_task::locks(Workqueue
* workqueue
)
1224 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1227 // Run the task--write out the symbols.
1230 Write_symbols_task::run(Workqueue
*)
1232 this->symtab_
->write_globals(this->target_
, this->sympool_
, this->of_
);
1235 // Close_task_runner methods.
1237 // Run the task--close the file.
1240 Close_task_runner::run(Workqueue
*)
1245 // Instantiate the templates we need. We could use the configure
1246 // script to restrict this to only the ones for implemented targets.
1250 Layout::layout
<32, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1251 const elfcpp::Shdr
<32, false>& shdr
, off_t
*);
1255 Layout::layout
<32, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1256 const elfcpp::Shdr
<32, true>& shdr
, off_t
*);
1260 Layout::layout
<64, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1261 const elfcpp::Shdr
<64, false>& shdr
, off_t
*);
1265 Layout::layout
<64, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1266 const elfcpp::Shdr
<64, true>& shdr
, off_t
*);
1269 } // End namespace gold.