1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
30 #include "parameters.h"
39 // Layout_task_runner methods.
41 // Lay out the sections. This is called after all the input objects
45 Layout_task_runner::run(Workqueue
* workqueue
)
47 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
50 // Now we know the final size of the output file and we know where
51 // each piece of information goes.
52 Output_file
* of
= new Output_file(this->options_
,
53 this->input_objects_
->target());
56 // Queue up the final set of tasks.
57 gold::queue_final_tasks(this->options_
, this->input_objects_
,
58 this->symtab_
, this->layout_
, workqueue
, of
);
63 Layout::Layout(const General_options
& options
)
64 : options_(options
), namepool_(), sympool_(), dynpool_(), signatures_(),
65 section_name_map_(), segment_list_(), section_list_(),
66 unattached_section_list_(), special_output_list_(),
67 tls_segment_(NULL
), symtab_section_(NULL
),
68 dynsym_section_(NULL
), dynamic_section_(NULL
), dynamic_data_(NULL
)
70 // Make space for more than enough segments for a typical file.
71 // This is just for efficiency--it's OK if we wind up needing more.
72 this->segment_list_
.reserve(12);
74 // We expect three unattached Output_data objects: the file header,
75 // the segment headers, and the section headers.
76 this->special_output_list_
.reserve(3);
79 // Hash a key we use to look up an output section mapping.
82 Layout::Hash_key::operator()(const Layout::Key
& k
) const
84 return k
.first
+ k
.second
.first
+ k
.second
.second
;
87 // Whether to include this section in the link.
89 template<int size
, bool big_endian
>
91 Layout::include_section(Object
*, const char*,
92 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
94 // Some section types are never linked. Some are only linked when
95 // doing a relocateable link.
96 switch (shdr
.get_sh_type())
98 case elfcpp::SHT_NULL
:
99 case elfcpp::SHT_SYMTAB
:
100 case elfcpp::SHT_DYNSYM
:
101 case elfcpp::SHT_STRTAB
:
102 case elfcpp::SHT_HASH
:
103 case elfcpp::SHT_DYNAMIC
:
104 case elfcpp::SHT_SYMTAB_SHNDX
:
107 case elfcpp::SHT_RELA
:
108 case elfcpp::SHT_REL
:
109 case elfcpp::SHT_GROUP
:
110 return parameters
->output_is_object();
113 // FIXME: Handle stripping debug sections here.
118 // Return an output section named NAME, or NULL if there is none.
121 Layout::find_output_section(const char* name
) const
123 for (Section_name_map::const_iterator p
= this->section_name_map_
.begin();
124 p
!= this->section_name_map_
.end();
126 if (strcmp(p
->second
->name(), name
) == 0)
131 // Return an output segment of type TYPE, with segment flags SET set
132 // and segment flags CLEAR clear. Return NULL if there is none.
135 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
136 elfcpp::Elf_Word clear
) const
138 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
139 p
!= this->segment_list_
.end();
141 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
142 && ((*p
)->flags() & set
) == set
143 && ((*p
)->flags() & clear
) == 0)
148 // Return the output section to use for section NAME with type TYPE
149 // and section flags FLAGS.
152 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
153 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
155 // We should ignore some flags.
156 flags
&= ~ (elfcpp::SHF_INFO_LINK
157 | elfcpp::SHF_LINK_ORDER
160 | elfcpp::SHF_STRINGS
);
162 const Key
key(name_key
, std::make_pair(type
, flags
));
163 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
164 std::pair
<Section_name_map::iterator
, bool> ins(
165 this->section_name_map_
.insert(v
));
168 return ins
.first
->second
;
171 // This is the first time we've seen this name/type/flags
173 Output_section
* os
= this->make_output_section(name
, type
, flags
);
174 ins
.first
->second
= os
;
179 // Return the output section to use for input section SHNDX, with name
180 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
181 // offset of this input section without the output section.
183 template<int size
, bool big_endian
>
185 Layout::layout(Relobj
* object
, unsigned int shndx
, const char* name
,
186 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* off
)
188 if (!this->include_section(object
, name
, shdr
))
191 // If we are not doing a relocateable link, choose the name to use
192 // for the output section.
193 size_t len
= strlen(name
);
194 if (!parameters
->output_is_object())
195 name
= Layout::output_section_name(name
, &len
);
197 // FIXME: Handle SHF_OS_NONCONFORMING here.
199 // Canonicalize the section name.
200 Stringpool::Key name_key
;
201 name
= this->namepool_
.add(name
, len
, &name_key
);
203 // Find the output section. The output section is selected based on
204 // the section name, type, and flags.
205 Output_section
* os
= this->get_output_section(name
, name_key
,
207 shdr
.get_sh_flags());
209 // FIXME: Handle SHF_LINK_ORDER somewhere.
211 *off
= os
->add_input_section(object
, shndx
, name
, shdr
);
216 // Add POSD to an output section using NAME, TYPE, and FLAGS.
219 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
220 elfcpp::Elf_Xword flags
,
221 Output_section_data
* posd
)
223 // Canonicalize the name.
224 Stringpool::Key name_key
;
225 name
= this->namepool_
.add(name
, &name_key
);
227 Output_section
* os
= this->get_output_section(name
, name_key
, type
, flags
);
228 os
->add_output_section_data(posd
);
231 // Map section flags to segment flags.
234 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
236 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
237 if ((flags
& elfcpp::SHF_WRITE
) != 0)
239 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
244 // Make a new Output_section, and attach it to segments as
248 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
249 elfcpp::Elf_Xword flags
)
251 Output_section
* os
= new Output_section(name
, type
, flags
);
252 this->section_list_
.push_back(os
);
254 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
255 this->unattached_section_list_
.push_back(os
);
258 // This output section goes into a PT_LOAD segment.
260 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
262 // The only thing we really care about for PT_LOAD segments is
263 // whether or not they are writable, so that is how we search
264 // for them. People who need segments sorted on some other
265 // basis will have to wait until we implement a mechanism for
266 // them to describe the segments they want.
268 Segment_list::const_iterator p
;
269 for (p
= this->segment_list_
.begin();
270 p
!= this->segment_list_
.end();
273 if ((*p
)->type() == elfcpp::PT_LOAD
274 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
276 (*p
)->add_output_section(os
, seg_flags
);
281 if (p
== this->segment_list_
.end())
283 Output_segment
* oseg
= new Output_segment(elfcpp::PT_LOAD
,
285 this->segment_list_
.push_back(oseg
);
286 oseg
->add_output_section(os
, seg_flags
);
289 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
291 if (type
== elfcpp::SHT_NOTE
)
293 // See if we already have an equivalent PT_NOTE segment.
294 for (p
= this->segment_list_
.begin();
295 p
!= segment_list_
.end();
298 if ((*p
)->type() == elfcpp::PT_NOTE
299 && (((*p
)->flags() & elfcpp::PF_W
)
300 == (seg_flags
& elfcpp::PF_W
)))
302 (*p
)->add_output_section(os
, seg_flags
);
307 if (p
== this->segment_list_
.end())
309 Output_segment
* oseg
= new Output_segment(elfcpp::PT_NOTE
,
311 this->segment_list_
.push_back(oseg
);
312 oseg
->add_output_section(os
, seg_flags
);
316 // If we see a loadable SHF_TLS section, we create a PT_TLS
317 // segment. There can only be one such segment.
318 if ((flags
& elfcpp::SHF_TLS
) != 0)
320 if (this->tls_segment_
== NULL
)
322 this->tls_segment_
= new Output_segment(elfcpp::PT_TLS
,
324 this->segment_list_
.push_back(this->tls_segment_
);
326 this->tls_segment_
->add_output_section(os
, seg_flags
);
333 // Create the dynamic sections which are needed before we read the
337 Layout::create_initial_dynamic_sections(const Input_objects
* input_objects
,
338 Symbol_table
* symtab
)
340 if (!input_objects
->any_dynamic())
343 const char* dynamic_name
= this->namepool_
.add(".dynamic", NULL
);
344 this->dynamic_section_
= this->make_output_section(dynamic_name
,
347 | elfcpp::SHF_WRITE
));
349 symtab
->define_in_output_data(input_objects
->target(), "_DYNAMIC", NULL
,
350 this->dynamic_section_
, 0, 0,
351 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
352 elfcpp::STV_HIDDEN
, 0, false, false);
354 this->dynamic_data_
= new Output_data_dynamic(input_objects
->target(),
357 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
360 // For each output section whose name can be represented as C symbol,
361 // define __start and __stop symbols for the section. This is a GNU
365 Layout::define_section_symbols(Symbol_table
* symtab
, const Target
* target
)
367 for (Section_list::const_iterator p
= this->section_list_
.begin();
368 p
!= this->section_list_
.end();
371 const char* const name
= (*p
)->name();
372 if (name
[strspn(name
,
374 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
375 "abcdefghijklmnopqrstuvwxyz"
379 const std::string
name_string(name
);
380 const std::string
start_name("__start_" + name_string
);
381 const std::string
stop_name("__stop_" + name_string
);
383 symtab
->define_in_output_data(target
,
393 false, // offset_is_from_end
394 false); // only_if_ref
396 symtab
->define_in_output_data(target
,
406 true, // offset_is_from_end
407 false); // only_if_ref
412 // Find the first read-only PT_LOAD segment, creating one if
416 Layout::find_first_load_seg()
418 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
419 p
!= this->segment_list_
.end();
422 if ((*p
)->type() == elfcpp::PT_LOAD
423 && ((*p
)->flags() & elfcpp::PF_R
) != 0
424 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
428 Output_segment
* load_seg
= new Output_segment(elfcpp::PT_LOAD
, elfcpp::PF_R
);
429 this->segment_list_
.push_back(load_seg
);
433 // Finalize the layout. When this is called, we have created all the
434 // output sections and all the output segments which are based on
435 // input sections. We have several things to do, and we have to do
436 // them in the right order, so that we get the right results correctly
439 // 1) Finalize the list of output segments and create the segment
442 // 2) Finalize the dynamic symbol table and associated sections.
444 // 3) Determine the final file offset of all the output segments.
446 // 4) Determine the final file offset of all the SHF_ALLOC output
449 // 5) Create the symbol table sections and the section name table
452 // 6) Finalize the symbol table: set symbol values to their final
453 // value and make a final determination of which symbols are going
454 // into the output symbol table.
456 // 7) Create the section table header.
458 // 8) Determine the final file offset of all the output sections which
459 // are not SHF_ALLOC, including the section table header.
461 // 9) Finalize the ELF file header.
463 // This function returns the size of the output file.
466 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
)
468 Target
* const target
= input_objects
->target();
469 const int size
= target
->get_size();
471 target
->finalize_sections(this);
473 Output_segment
* phdr_seg
= NULL
;
474 if (input_objects
->any_dynamic())
476 // There was a dynamic object in the link. We need to create
477 // some information for the dynamic linker.
479 // Create the PT_PHDR segment which will hold the program
481 phdr_seg
= new Output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
482 this->segment_list_
.push_back(phdr_seg
);
484 // Create the dynamic symbol table, including the hash table.
485 Output_section
* dynstr
;
486 std::vector
<Symbol
*> dynamic_symbols
;
487 unsigned int local_dynamic_count
;
489 this->create_dynamic_symtab(target
, symtab
, &dynstr
,
490 &local_dynamic_count
, &dynamic_symbols
,
493 // Create the .interp section to hold the name of the
494 // interpreter, and put it in a PT_INTERP segment.
495 this->create_interp(target
);
497 // Finish the .dynamic section to hold the dynamic data, and put
498 // it in a PT_DYNAMIC segment.
499 this->finish_dynamic_section(input_objects
, symtab
);
501 // We should have added everything we need to the dynamic string
503 this->dynpool_
.set_string_offsets();
505 // Create the version sections. We can't do this until the
506 // dynamic string table is complete.
507 this->create_version_sections(target
, &versions
, local_dynamic_count
,
508 dynamic_symbols
, dynstr
);
511 // FIXME: Handle PT_GNU_STACK.
513 Output_segment
* load_seg
= this->find_first_load_seg();
515 // Lay out the segment headers.
516 bool big_endian
= target
->is_big_endian();
517 Output_segment_headers
* segment_headers
;
518 segment_headers
= new Output_segment_headers(size
, big_endian
,
519 this->segment_list_
);
520 load_seg
->add_initial_output_data(segment_headers
);
521 this->special_output_list_
.push_back(segment_headers
);
522 if (phdr_seg
!= NULL
)
523 phdr_seg
->add_initial_output_data(segment_headers
);
525 // Lay out the file header.
526 Output_file_header
* file_header
;
527 file_header
= new Output_file_header(size
,
532 load_seg
->add_initial_output_data(file_header
);
533 this->special_output_list_
.push_back(file_header
);
535 // We set the output section indexes in set_segment_offsets and
536 // set_section_offsets.
537 unsigned int shndx
= 1;
539 // Set the file offsets of all the segments, and all the sections
541 off_t off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
543 // Create the symbol table sections.
544 this->create_symtab_sections(size
, input_objects
, symtab
, &off
);
546 // Create the .shstrtab section.
547 Output_section
* shstrtab_section
= this->create_shstrtab();
549 // Set the file offsets of all the sections not associated with
551 off
= this->set_section_offsets(off
, &shndx
);
553 // Create the section table header.
554 Output_section_headers
* oshdrs
= this->create_shdrs(size
, big_endian
, &off
);
556 file_header
->set_section_info(oshdrs
, shstrtab_section
);
558 // Now we know exactly where everything goes in the output file.
559 Output_data::layout_complete();
564 // Return whether SEG1 should be before SEG2 in the output file. This
565 // is based entirely on the segment type and flags. When this is
566 // called the segment addresses has normally not yet been set.
569 Layout::segment_precedes(const Output_segment
* seg1
,
570 const Output_segment
* seg2
)
572 elfcpp::Elf_Word type1
= seg1
->type();
573 elfcpp::Elf_Word type2
= seg2
->type();
575 // The single PT_PHDR segment is required to precede any loadable
576 // segment. We simply make it always first.
577 if (type1
== elfcpp::PT_PHDR
)
579 gold_assert(type2
!= elfcpp::PT_PHDR
);
582 if (type2
== elfcpp::PT_PHDR
)
585 // The single PT_INTERP segment is required to precede any loadable
586 // segment. We simply make it always second.
587 if (type1
== elfcpp::PT_INTERP
)
589 gold_assert(type2
!= elfcpp::PT_INTERP
);
592 if (type2
== elfcpp::PT_INTERP
)
595 // We then put PT_LOAD segments before any other segments.
596 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
598 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
601 // We put the PT_TLS segment last, because that is where the dynamic
602 // linker expects to find it (this is just for efficiency; other
603 // positions would also work correctly).
604 if (type1
== elfcpp::PT_TLS
&& type2
!= elfcpp::PT_TLS
)
606 if (type2
== elfcpp::PT_TLS
&& type1
!= elfcpp::PT_TLS
)
609 const elfcpp::Elf_Word flags1
= seg1
->flags();
610 const elfcpp::Elf_Word flags2
= seg2
->flags();
612 // The order of non-PT_LOAD segments is unimportant. We simply sort
613 // by the numeric segment type and flags values. There should not
614 // be more than one segment with the same type and flags.
615 if (type1
!= elfcpp::PT_LOAD
)
618 return type1
< type2
;
619 gold_assert(flags1
!= flags2
);
620 return flags1
< flags2
;
623 // We sort PT_LOAD segments based on the flags. Readonly segments
624 // come before writable segments. Then executable segments come
625 // before non-executable segments. Then the unlikely case of a
626 // non-readable segment comes before the normal case of a readable
627 // segment. If there are multiple segments with the same type and
628 // flags, we require that the address be set, and we sort by
629 // virtual address and then physical address.
630 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
631 return (flags1
& elfcpp::PF_W
) == 0;
632 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
633 return (flags1
& elfcpp::PF_X
) != 0;
634 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
635 return (flags1
& elfcpp::PF_R
) == 0;
637 uint64_t vaddr1
= seg1
->vaddr();
638 uint64_t vaddr2
= seg2
->vaddr();
639 if (vaddr1
!= vaddr2
)
640 return vaddr1
< vaddr2
;
642 uint64_t paddr1
= seg1
->paddr();
643 uint64_t paddr2
= seg2
->paddr();
644 gold_assert(paddr1
!= paddr2
);
645 return paddr1
< paddr2
;
648 // Set the file offsets of all the segments, and all the sections they
649 // contain. They have all been created. LOAD_SEG must be be laid out
650 // first. Return the offset of the data to follow.
653 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
654 unsigned int *pshndx
)
656 // Sort them into the final order.
657 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
658 Layout::Compare_segments());
660 // Find the PT_LOAD segments, and set their addresses and offsets
661 // and their section's addresses and offsets.
662 uint64_t addr
= target
->text_segment_address();
664 bool was_readonly
= false;
665 for (Segment_list::iterator p
= this->segment_list_
.begin();
666 p
!= this->segment_list_
.end();
669 if ((*p
)->type() == elfcpp::PT_LOAD
)
671 if (load_seg
!= NULL
&& load_seg
!= *p
)
675 // If the last segment was readonly, and this one is not,
676 // then skip the address forward one page, maintaining the
677 // same position within the page. This lets us store both
678 // segments overlapping on a single page in the file, but
679 // the loader will put them on different pages in memory.
681 uint64_t orig_addr
= addr
;
682 uint64_t orig_off
= off
;
684 uint64_t aligned_addr
= addr
;
685 uint64_t abi_pagesize
= target
->abi_pagesize();
687 // FIXME: This should depend on the -n and -N options.
688 (*p
)->set_minimum_addralign(target
->common_pagesize());
690 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
692 uint64_t align
= (*p
)->addralign();
694 addr
= align_address(addr
, align
);
696 if ((addr
& (abi_pagesize
- 1)) != 0)
697 addr
= addr
+ abi_pagesize
;
700 unsigned int shndx_hold
= *pshndx
;
701 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
702 uint64_t new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
704 // Now that we know the size of this segment, we may be able
705 // to save a page in memory, at the cost of wasting some
706 // file space, by instead aligning to the start of a new
707 // page. Here we use the real machine page size rather than
708 // the ABI mandated page size.
710 if (aligned_addr
!= addr
)
712 uint64_t common_pagesize
= target
->common_pagesize();
713 uint64_t first_off
= (common_pagesize
715 & (common_pagesize
- 1)));
716 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
719 && ((aligned_addr
& ~ (common_pagesize
- 1))
720 != (new_addr
& ~ (common_pagesize
- 1)))
721 && first_off
+ last_off
<= common_pagesize
)
723 *pshndx
= shndx_hold
;
724 addr
= align_address(aligned_addr
, common_pagesize
);
725 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
726 new_addr
= (*p
)->set_section_addresses(addr
, &off
, pshndx
);
732 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
737 // Handle the non-PT_LOAD segments, setting their offsets from their
738 // section's offsets.
739 for (Segment_list::iterator p
= this->segment_list_
.begin();
740 p
!= this->segment_list_
.end();
743 if ((*p
)->type() != elfcpp::PT_LOAD
)
750 // Set the file offset of all the sections not associated with a
754 Layout::set_section_offsets(off_t off
, unsigned int* pshndx
)
756 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
757 p
!= this->unattached_section_list_
.end();
760 (*p
)->set_out_shndx(*pshndx
);
762 if ((*p
)->offset() != -1)
764 off
= align_address(off
, (*p
)->addralign());
765 (*p
)->set_address(0, off
);
766 off
+= (*p
)->data_size();
771 // Create the symbol table sections. Here we also set the final
772 // values of the symbols. At this point all the loadable sections are
776 Layout::create_symtab_sections(int size
, const Input_objects
* input_objects
,
777 Symbol_table
* symtab
,
784 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
789 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
796 off
= align_address(off
, align
);
797 off_t startoff
= off
;
799 // Save space for the dummy symbol at the start of the section. We
800 // never bother to write this out--it will just be left as zero.
802 unsigned int local_symbol_index
= 1;
804 // Add STT_SECTION symbols for each Output section which needs one.
805 for (Section_list::iterator p
= this->section_list_
.begin();
806 p
!= this->section_list_
.end();
809 if (!(*p
)->needs_symtab_index())
810 (*p
)->set_symtab_index(-1U);
813 (*p
)->set_symtab_index(local_symbol_index
);
814 ++local_symbol_index
;
819 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
820 p
!= input_objects
->relobj_end();
823 Task_lock_obj
<Object
> tlo(**p
);
824 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
827 off
+= (index
- local_symbol_index
) * symsize
;
828 local_symbol_index
= index
;
831 unsigned int local_symcount
= local_symbol_index
;
832 gold_assert(local_symcount
* symsize
== off
- startoff
);
835 size_t dyn_global_index
;
837 if (this->dynsym_section_
== NULL
)
840 dyn_global_index
= 0;
845 dyn_global_index
= this->dynsym_section_
->info();
846 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
847 dynoff
= this->dynsym_section_
->offset() + locsize
;
848 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
849 gold_assert(dyncount
* symsize
850 == this->dynsym_section_
->data_size() - locsize
);
853 off
= symtab
->finalize(local_symcount
, off
, dynoff
, dyn_global_index
,
854 dyncount
, &this->sympool_
);
856 this->sympool_
.set_string_offsets();
858 const char* symtab_name
= this->namepool_
.add(".symtab", NULL
);
859 Output_section
* osymtab
= this->make_output_section(symtab_name
,
862 this->symtab_section_
= osymtab
;
864 Output_section_data
* pos
= new Output_data_space(off
- startoff
,
866 osymtab
->add_output_section_data(pos
);
868 const char* strtab_name
= this->namepool_
.add(".strtab", NULL
);
869 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
873 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
874 ostrtab
->add_output_section_data(pstr
);
876 osymtab
->set_address(0, startoff
);
877 osymtab
->set_link_section(ostrtab
);
878 osymtab
->set_info(local_symcount
);
879 osymtab
->set_entsize(symsize
);
884 // Create the .shstrtab section, which holds the names of the
885 // sections. At the time this is called, we have created all the
886 // output sections except .shstrtab itself.
889 Layout::create_shstrtab()
891 // FIXME: We don't need to create a .shstrtab section if we are
892 // stripping everything.
894 const char* name
= this->namepool_
.add(".shstrtab", NULL
);
896 this->namepool_
.set_string_offsets();
898 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
900 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
901 os
->add_output_section_data(posd
);
906 // Create the section headers. SIZE is 32 or 64. OFF is the file
909 Output_section_headers
*
910 Layout::create_shdrs(int size
, bool big_endian
, off_t
* poff
)
912 Output_section_headers
* oshdrs
;
913 oshdrs
= new Output_section_headers(size
, big_endian
, this,
914 &this->segment_list_
,
915 &this->unattached_section_list_
,
917 off_t off
= align_address(*poff
, oshdrs
->addralign());
918 oshdrs
->set_address(0, off
);
919 off
+= oshdrs
->data_size();
921 this->special_output_list_
.push_back(oshdrs
);
925 // Create the dynamic symbol table.
928 Layout::create_dynamic_symtab(const Target
* target
, Symbol_table
* symtab
,
929 Output_section
**pdynstr
,
930 unsigned int* plocal_dynamic_count
,
931 std::vector
<Symbol
*>* pdynamic_symbols
,
934 // Count all the symbols in the dynamic symbol table, and set the
935 // dynamic symbol indexes.
937 // Skip symbol 0, which is always all zeroes.
938 unsigned int index
= 1;
940 // Add STT_SECTION symbols for each Output section which needs one.
941 for (Section_list::iterator p
= this->section_list_
.begin();
942 p
!= this->section_list_
.end();
945 if (!(*p
)->needs_dynsym_index())
946 (*p
)->set_dynsym_index(-1U);
949 (*p
)->set_dynsym_index(index
);
954 // FIXME: Some targets apparently require local symbols in the
955 // dynamic symbol table. Here is where we will have to count them,
956 // and set the dynamic symbol indexes, and add the names to
959 unsigned int local_symcount
= index
;
960 *plocal_dynamic_count
= local_symcount
;
962 // FIXME: We have to tell set_dynsym_indexes whether the
963 // -E/--export-dynamic option was used.
964 index
= symtab
->set_dynsym_indexes(&this->options_
, target
, index
,
965 pdynamic_symbols
, &this->dynpool_
,
970 const int size
= target
->get_size();
973 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
978 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
984 // Create the dynamic symbol table section.
986 const char* dynsym_name
= this->namepool_
.add(".dynsym", NULL
);
987 Output_section
* dynsym
= this->make_output_section(dynsym_name
,
991 Output_section_data
* odata
= new Output_data_space(index
* symsize
,
993 dynsym
->add_output_section_data(odata
);
995 dynsym
->set_info(local_symcount
);
996 dynsym
->set_entsize(symsize
);
997 dynsym
->set_addralign(align
);
999 this->dynsym_section_
= dynsym
;
1001 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1002 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
1003 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
1005 // Create the dynamic string table section.
1007 const char* dynstr_name
= this->namepool_
.add(".dynstr", NULL
);
1008 Output_section
* dynstr
= this->make_output_section(dynstr_name
,
1012 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
1013 dynstr
->add_output_section_data(strdata
);
1015 dynsym
->set_link_section(dynstr
);
1016 this->dynamic_section_
->set_link_section(dynstr
);
1018 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
1019 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
1023 // Create the hash tables.
1025 // FIXME: We need an option to create a GNU hash table.
1027 unsigned char* phash
;
1028 unsigned int hashlen
;
1029 Dynobj::create_elf_hash_table(target
, *pdynamic_symbols
, local_symcount
,
1032 const char* hash_name
= this->namepool_
.add(".hash", NULL
);
1033 Output_section
* hashsec
= this->make_output_section(hash_name
,
1037 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
1040 hashsec
->add_output_section_data(hashdata
);
1042 hashsec
->set_link_section(dynsym
);
1043 hashsec
->set_entsize(4);
1045 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
1048 // Create the version sections.
1051 Layout::create_version_sections(const Target
* target
, const Versions
* versions
,
1052 unsigned int local_symcount
,
1053 const std::vector
<Symbol
*>& dynamic_symbols
,
1054 const Output_section
* dynstr
)
1056 if (!versions
->any_defs() && !versions
->any_needs())
1059 if (target
->get_size() == 32)
1061 if (target
->is_big_endian())
1063 #ifdef HAVE_TARGET_32_BIG
1064 this->sized_create_version_sections
1065 SELECT_SIZE_ENDIAN_NAME(32, true)(
1066 versions
, local_symcount
, dynamic_symbols
, dynstr
1067 SELECT_SIZE_ENDIAN(32, true));
1074 #ifdef HAVE_TARGET_32_LITTLE
1075 this->sized_create_version_sections
1076 SELECT_SIZE_ENDIAN_NAME(32, false)(
1077 versions
, local_symcount
, dynamic_symbols
, dynstr
1078 SELECT_SIZE_ENDIAN(32, false));
1084 else if (target
->get_size() == 64)
1086 if (target
->is_big_endian())
1088 #ifdef HAVE_TARGET_64_BIG
1089 this->sized_create_version_sections
1090 SELECT_SIZE_ENDIAN_NAME(64, true)(
1091 versions
, local_symcount
, dynamic_symbols
, dynstr
1092 SELECT_SIZE_ENDIAN(64, true));
1099 #ifdef HAVE_TARGET_64_LITTLE
1100 this->sized_create_version_sections
1101 SELECT_SIZE_ENDIAN_NAME(64, false)(
1102 versions
, local_symcount
, dynamic_symbols
, dynstr
1103 SELECT_SIZE_ENDIAN(64, false));
1113 // Create the version sections, sized version.
1115 template<int size
, bool big_endian
>
1117 Layout::sized_create_version_sections(
1118 const Versions
* versions
,
1119 unsigned int local_symcount
,
1120 const std::vector
<Symbol
*>& dynamic_symbols
,
1121 const Output_section
* dynstr
1124 const char* vname
= this->namepool_
.add(".gnu.version", NULL
);
1125 Output_section
* vsec
= this->make_output_section(vname
,
1126 elfcpp::SHT_GNU_versym
,
1129 unsigned char* vbuf
;
1131 versions
->symbol_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1132 &this->dynpool_
, local_symcount
, dynamic_symbols
, &vbuf
, &vsize
1133 SELECT_SIZE_ENDIAN(size
, big_endian
));
1135 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2);
1137 vsec
->add_output_section_data(vdata
);
1138 vsec
->set_entsize(2);
1139 vsec
->set_link_section(this->dynsym_section_
);
1141 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1142 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
1144 if (versions
->any_defs())
1146 const char* vdname
= this->namepool_
.add(".gnu.version_d", NULL
);
1147 Output_section
*vdsec
;
1148 vdsec
= this->make_output_section(vdname
, elfcpp::SHT_GNU_verdef
,
1151 unsigned char* vdbuf
;
1152 unsigned int vdsize
;
1153 unsigned int vdentries
;
1154 versions
->def_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1155 &this->dynpool_
, &vdbuf
, &vdsize
, &vdentries
1156 SELECT_SIZE_ENDIAN(size
, big_endian
));
1158 Output_section_data
* vddata
= new Output_data_const_buffer(vdbuf
,
1162 vdsec
->add_output_section_data(vddata
);
1163 vdsec
->set_link_section(dynstr
);
1164 vdsec
->set_info(vdentries
);
1166 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
1167 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
1170 if (versions
->any_needs())
1172 const char* vnname
= this->namepool_
.add(".gnu.version_r", NULL
);
1173 Output_section
* vnsec
;
1174 vnsec
= this->make_output_section(vnname
, elfcpp::SHT_GNU_verneed
,
1177 unsigned char* vnbuf
;
1178 unsigned int vnsize
;
1179 unsigned int vnentries
;
1180 versions
->need_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)
1181 (&this->dynpool_
, &vnbuf
, &vnsize
, &vnentries
1182 SELECT_SIZE_ENDIAN(size
, big_endian
));
1184 Output_section_data
* vndata
= new Output_data_const_buffer(vnbuf
,
1188 vnsec
->add_output_section_data(vndata
);
1189 vnsec
->set_link_section(dynstr
);
1190 vnsec
->set_info(vnentries
);
1192 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
1193 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
1197 // Create the .interp section and PT_INTERP segment.
1200 Layout::create_interp(const Target
* target
)
1202 const char* interp
= this->options_
.dynamic_linker();
1205 interp
= target
->dynamic_linker();
1206 gold_assert(interp
!= NULL
);
1209 size_t len
= strlen(interp
) + 1;
1211 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
1213 const char* interp_name
= this->namepool_
.add(".interp", NULL
);
1214 Output_section
* osec
= this->make_output_section(interp_name
,
1215 elfcpp::SHT_PROGBITS
,
1217 osec
->add_output_section_data(odata
);
1219 Output_segment
* oseg
= new Output_segment(elfcpp::PT_INTERP
, elfcpp::PF_R
);
1220 this->segment_list_
.push_back(oseg
);
1221 oseg
->add_initial_output_section(osec
, elfcpp::PF_R
);
1224 // Finish the .dynamic section and PT_DYNAMIC segment.
1227 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
1228 const Symbol_table
* symtab
)
1230 Output_segment
* oseg
= new Output_segment(elfcpp::PT_DYNAMIC
,
1231 elfcpp::PF_R
| elfcpp::PF_W
);
1232 this->segment_list_
.push_back(oseg
);
1233 oseg
->add_initial_output_section(this->dynamic_section_
,
1234 elfcpp::PF_R
| elfcpp::PF_W
);
1236 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1238 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
1239 p
!= input_objects
->dynobj_end();
1242 // FIXME: Handle --as-needed.
1243 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
1246 // FIXME: Support --init and --fini.
1247 Symbol
* sym
= symtab
->lookup("_init");
1248 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
1249 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
1251 sym
= symtab
->lookup("_fini");
1252 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
1253 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
1255 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1257 // Add a DT_RPATH entry if needed.
1258 const General_options::Dir_list
& rpath(this->options_
.rpath());
1261 std::string rpath_val
;
1262 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
1266 if (rpath_val
.empty())
1270 // Eliminate duplicates.
1271 General_options::Dir_list::const_iterator q
;
1272 for (q
= rpath
.begin(); q
!= p
; ++q
)
1273 if (strcmp(*q
, *p
) == 0)
1283 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
1287 // The mapping of .gnu.linkonce section names to real section names.
1289 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1290 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
1292 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1293 MAPPING_INIT("t", ".text"),
1294 MAPPING_INIT("r", ".rodata"),
1295 MAPPING_INIT("d", ".data"),
1296 MAPPING_INIT("b", ".bss"),
1297 MAPPING_INIT("s", ".sdata"),
1298 MAPPING_INIT("sb", ".sbss"),
1299 MAPPING_INIT("s2", ".sdata2"),
1300 MAPPING_INIT("sb2", ".sbss2"),
1301 MAPPING_INIT("wi", ".debug_info"),
1302 MAPPING_INIT("td", ".tdata"),
1303 MAPPING_INIT("tb", ".tbss"),
1304 MAPPING_INIT("lr", ".lrodata"),
1305 MAPPING_INIT("l", ".ldata"),
1306 MAPPING_INIT("lb", ".lbss"),
1310 const int Layout::linkonce_mapping_count
=
1311 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
1313 // Return the name of the output section to use for a .gnu.linkonce
1314 // section. This is based on the default ELF linker script of the old
1315 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1316 // to ".text". Set *PLEN to the length of the name. *PLEN is
1317 // initialized to the length of NAME.
1320 Layout::linkonce_output_name(const char* name
, size_t *plen
)
1322 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
1326 const Linkonce_mapping
* plm
= linkonce_mapping
;
1327 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
1329 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
1338 // Choose the output section name to use given an input section name.
1339 // Set *PLEN to the length of the name. *PLEN is initialized to the
1343 Layout::output_section_name(const char* name
, size_t* plen
)
1345 if (Layout::is_linkonce(name
))
1347 // .gnu.linkonce sections are laid out as though they were named
1348 // for the sections are placed into.
1349 return Layout::linkonce_output_name(name
, plen
);
1352 // If the section name has no '.', or only an initial '.', we use
1353 // the name unchanged (i.e., ".text" is unchanged).
1355 // Otherwise, if the section name does not include ".rel", we drop
1356 // the last '.' and everything that follows (i.e., ".text.XXX"
1357 // becomes ".text").
1359 // Otherwise, if the section name has zero or one '.' after the
1360 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1363 // Otherwise, we drop the last '.' and everything that follows
1364 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1366 const char* s
= name
;
1369 const char* sdot
= strchr(s
, '.');
1373 const char* srel
= strstr(s
, ".rel");
1376 *plen
= sdot
- name
;
1380 sdot
= strchr(srel
+ 1, '.');
1383 sdot
= strchr(sdot
+ 1, '.');
1387 *plen
= sdot
- name
;
1391 // Record the signature of a comdat section, and return whether to
1392 // include it in the link. If GROUP is true, this is a regular
1393 // section group. If GROUP is false, this is a group signature
1394 // derived from the name of a linkonce section. We want linkonce
1395 // signatures and group signatures to block each other, but we don't
1396 // want a linkonce signature to block another linkonce signature.
1399 Layout::add_comdat(const char* signature
, bool group
)
1401 std::string
sig(signature
);
1402 std::pair
<Signatures::iterator
, bool> ins(
1403 this->signatures_
.insert(std::make_pair(sig
, group
)));
1407 // This is the first time we've seen this signature.
1411 if (ins
.first
->second
)
1413 // We've already seen a real section group with this signature.
1418 // This is a real section group, and we've already seen a
1419 // linkonce section with this signature. Record that we've seen
1420 // a section group, and don't include this section group.
1421 ins
.first
->second
= true;
1426 // We've already seen a linkonce section and this is a linkonce
1427 // section. These don't block each other--this may be the same
1428 // symbol name with different section types.
1433 // Write out data not associated with a section or the symbol table.
1436 Layout::write_data(const Symbol_table
* symtab
, const Target
* target
,
1437 Output_file
* of
) const
1439 const Output_section
* symtab_section
= this->symtab_section_
;
1440 for (Section_list::const_iterator p
= this->section_list_
.begin();
1441 p
!= this->section_list_
.end();
1444 if ((*p
)->needs_symtab_index())
1446 gold_assert(symtab_section
!= NULL
);
1447 unsigned int index
= (*p
)->symtab_index();
1448 gold_assert(index
> 0 && index
!= -1U);
1449 off_t off
= (symtab_section
->offset()
1450 + index
* symtab_section
->entsize());
1451 symtab
->write_section_symbol(target
, *p
, of
, off
);
1455 const Output_section
* dynsym_section
= this->dynsym_section_
;
1456 for (Section_list::const_iterator p
= this->section_list_
.begin();
1457 p
!= this->section_list_
.end();
1460 if ((*p
)->needs_dynsym_index())
1462 gold_assert(dynsym_section
!= NULL
);
1463 unsigned int index
= (*p
)->dynsym_index();
1464 gold_assert(index
> 0 && index
!= -1U);
1465 off_t off
= (dynsym_section
->offset()
1466 + index
* dynsym_section
->entsize());
1467 symtab
->write_section_symbol(target
, *p
, of
, off
);
1471 // Write out the Output_sections. Most won't have anything to
1472 // write, since most of the data will come from input sections which
1473 // are handled elsewhere. But some Output_sections do have
1475 for (Section_list::const_iterator p
= this->section_list_
.begin();
1476 p
!= this->section_list_
.end();
1480 // Write out the Output_data which are not in an Output_section.
1481 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
1482 p
!= this->special_output_list_
.end();
1487 // Write_data_task methods.
1489 // We can always run this task.
1491 Task::Is_runnable_type
1492 Write_data_task::is_runnable(Workqueue
*)
1497 // We need to unlock FINAL_BLOCKER when finished.
1500 Write_data_task::locks(Workqueue
* workqueue
)
1502 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1505 // Run the task--write out the data.
1508 Write_data_task::run(Workqueue
*)
1510 this->layout_
->write_data(this->symtab_
, this->target_
, this->of_
);
1513 // Write_symbols_task methods.
1515 // We can always run this task.
1517 Task::Is_runnable_type
1518 Write_symbols_task::is_runnable(Workqueue
*)
1523 // We need to unlock FINAL_BLOCKER when finished.
1526 Write_symbols_task::locks(Workqueue
* workqueue
)
1528 return new Task_locker_block(*this->final_blocker_
, workqueue
);
1531 // Run the task--write out the symbols.
1534 Write_symbols_task::run(Workqueue
*)
1536 this->symtab_
->write_globals(this->target_
, this->sympool_
, this->dynpool_
,
1540 // Close_task_runner methods.
1542 // Run the task--close the file.
1545 Close_task_runner::run(Workqueue
*)
1550 // Instantiate the templates we need. We could use the configure
1551 // script to restrict this to only the ones for implemented targets.
1553 #ifdef HAVE_TARGET_32_LITTLE
1556 Layout::layout
<32, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1557 const elfcpp::Shdr
<32, false>& shdr
, off_t
*);
1560 #ifdef HAVE_TARGET_32_BIG
1563 Layout::layout
<32, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1564 const elfcpp::Shdr
<32, true>& shdr
, off_t
*);
1567 #ifdef HAVE_TARGET_64_LITTLE
1570 Layout::layout
<64, false>(Relobj
* object
, unsigned int shndx
, const char* name
,
1571 const elfcpp::Shdr
<64, false>& shdr
, off_t
*);
1574 #ifdef HAVE_TARGET_64_BIG
1577 Layout::layout
<64, true>(Relobj
* object
, unsigned int shndx
, const char* name
,
1578 const elfcpp::Shdr
<64, true>& shdr
, off_t
*);
1582 } // End namespace gold.