1 // layout.cc -- lay out output file sections for gold
17 // Layout_task methods.
19 Layout_task::~Layout_task()
23 // This task can be run when it is unblocked.
25 Task::Is_runnable_type
26 Layout_task::is_runnable(Workqueue*)
28 if (this->this_blocker_->is_blocked())
33 // We don't need to hold any locks for the duration of this task. In
34 // fact this task will be the only one running.
37 Layout_task::locks(Workqueue*)
42 // Lay out the sections. This is called after all the input objects
46 Layout_task::run(Workqueue* workqueue)
48 // Nothing ever frees this.
49 Layout* layout = new Layout(this->options_);
51 for (Input_objects::Object_list::const_iterator p =
52 this->input_objects_->begin();
53 p != this->input_objects_->end();
56 off_t file_size = layout->finalize(this->input_objects_, this->symtab_);
58 // Now we know the final size of the output file and we know where
59 // each piece of information goes.
60 Output_file* of = new Output_file(this->options_);
63 // Queue up the final set of tasks.
64 gold::queue_final_tasks(this->options_, this->input_objects_,
65 this->symtab_, layout, workqueue, of);
70 Layout::Layout(const General_options& options)
71 : options_(options), last_shndx_(0), namepool_(), sympool_(), signatures_(),
72 section_name_map_(), segment_list_(), section_list_(),
73 special_output_list_()
77 // Prepare for doing layout.
82 // Make space for more than enough segments for a typical file.
83 // This is just for efficiency--it's OK if we wind up needing more.
84 segment_list_.reserve(12);
87 // Hash a key we use to look up an output section mapping.
90 Layout::Hash_key::operator()(const Layout::Key& k) const
92 return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
95 // Whether to include this section in the link.
97 template<int size, bool big_endian>
99 Layout::include_section(Object*, const char*,
100 const elfcpp::Shdr<size, big_endian>& shdr)
102 // Some section types are never linked. Some are only linked when
103 // doing a relocateable link.
104 switch (shdr.get_sh_type())
106 case elfcpp::SHT_NULL:
107 case elfcpp::SHT_SYMTAB:
108 case elfcpp::SHT_DYNSYM:
109 case elfcpp::SHT_STRTAB:
110 case elfcpp::SHT_HASH:
111 case elfcpp::SHT_DYNAMIC:
112 case elfcpp::SHT_SYMTAB_SHNDX:
115 case elfcpp::SHT_RELA:
116 case elfcpp::SHT_REL:
117 case elfcpp::SHT_GROUP:
118 return this->options_.is_relocatable();
121 // FIXME: Handle stripping debug sections here.
126 // Return the output section to use for input section NAME, with
127 // header HEADER, from object OBJECT. Set *OFF to the offset of this
128 // input section without the output section.
130 template<int size, bool big_endian>
132 Layout::layout(Object* object, const char* name,
133 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
135 // We discard empty input sections.
136 if (shdr.get_sh_size() == 0)
139 if (!this->include_section(object, name, shdr))
142 // Unless we are doing a relocateable link, .gnu.linkonce sections
143 // are laid out as though they were named for the sections are
145 if (!this->options_.is_relocatable() && Layout::is_linkonce(name))
146 name = Layout::linkonce_output_name(name);
148 // FIXME: Handle SHF_OS_NONCONFORMING here.
150 // Canonicalize the section name.
151 name = this->namepool_.add(name);
153 // Find the output section. The output section is selected based on
154 // the section name, type, and flags.
156 // FIXME: If we want to do relaxation, we need to modify this
157 // algorithm. We also build a list of input sections for each
158 // output section. Then we relax all the input sections. Then we
159 // walk down the list and adjust all the offsets.
161 elfcpp::Elf_Word type = shdr.get_sh_type();
162 elfcpp::Elf_Xword flags = shdr.get_sh_flags();
163 const Key key(name, std::make_pair(type, flags));
164 const std::pair<Key, Output_section*> v(key, NULL);
165 std::pair<Section_name_map::iterator, bool> ins(
166 this->section_name_map_.insert(v));
170 os = ins.first->second;
173 // This is the first time we've seen this name/type/flags
175 os = this->make_output_section(name, type, flags);
176 ins.first->second = os;
179 // FIXME: Handle SHF_LINK_ORDER somewhere.
181 *off = os->add_input_section(object, name, shdr);
186 // Map section flags to segment flags.
189 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
191 elfcpp::Elf_Word ret = elfcpp::PF_R;
192 if ((flags & elfcpp::SHF_WRITE) != 0)
194 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
199 // Make a new Output_section, and attach it to segments as
203 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
204 elfcpp::Elf_Xword flags)
207 Output_section* os = new Output_section(name, type, flags,
210 if ((flags & elfcpp::SHF_ALLOC) == 0)
211 this->section_list_.push_back(os);
214 // This output section goes into a PT_LOAD segment.
216 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
218 // The only thing we really care about for PT_LOAD segments is
219 // whether or not they are writable, so that is how we search
220 // for them. People who need segments sorted on some other
221 // basis will have to wait until we implement a mechanism for
222 // them to describe the segments they want.
224 Segment_list::const_iterator p;
225 for (p = this->segment_list_.begin();
226 p != this->segment_list_.end();
229 if ((*p)->type() == elfcpp::PT_LOAD
230 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
232 (*p)->add_output_section(os, seg_flags);
237 if (p == this->segment_list_.end())
239 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
241 this->segment_list_.push_back(oseg);
242 oseg->add_output_section(os, seg_flags);
245 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
247 if (type == elfcpp::SHT_NOTE)
249 // See if we already have an equivalent PT_NOTE segment.
250 for (p = this->segment_list_.begin();
251 p != segment_list_.end();
254 if ((*p)->type() == elfcpp::PT_NOTE
255 && (((*p)->flags() & elfcpp::PF_W)
256 == (seg_flags & elfcpp::PF_W)))
258 (*p)->add_output_section(os, seg_flags);
263 if (p == this->segment_list_.end())
265 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
267 this->segment_list_.push_back(oseg);
268 oseg->add_output_section(os, seg_flags);
272 // If we see a loadable SHF_TLS section, we create a PT_TLS
274 if ((flags & elfcpp::SHF_TLS) != 0)
276 // See if we already have an equivalent PT_TLS segment.
277 for (p = this->segment_list_.begin();
278 p != segment_list_.end();
281 if ((*p)->type() == elfcpp::PT_TLS
282 && (((*p)->flags() & elfcpp::PF_W)
283 == (seg_flags & elfcpp::PF_W)))
285 (*p)->add_output_section(os, seg_flags);
290 if (p == this->segment_list_.end())
292 Output_segment* oseg = new Output_segment(elfcpp::PT_TLS,
294 this->segment_list_.push_back(oseg);
295 oseg->add_output_section(os, seg_flags);
303 // Find the first read-only PT_LOAD segment, creating one if
307 Layout::find_first_load_seg()
309 for (Segment_list::const_iterator p = this->segment_list_.begin();
310 p != this->segment_list_.end();
313 if ((*p)->type() == elfcpp::PT_LOAD
314 && ((*p)->flags() & elfcpp::PF_R) != 0
315 && ((*p)->flags() & elfcpp::PF_W) == 0)
319 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
320 this->segment_list_.push_back(load_seg);
324 // Finalize the layout. When this is called, we have created all the
325 // output sections and all the output segments which are based on
326 // input sections. We have several things to do, and we have to do
327 // them in the right order, so that we get the right results correctly
330 // 1) Finalize the list of output segments and create the segment
333 // 2) Finalize the dynamic symbol table and associated sections.
335 // 3) Determine the final file offset of all the output segments.
337 // 4) Determine the final file offset of all the SHF_ALLOC output
340 // 5) Create the symbol table sections and the section name table
343 // 6) Finalize the symbol table: set symbol values to their final
344 // value and make a final determination of which symbols are going
345 // into the output symbol table.
347 // 7) Create the section table header.
349 // 8) Determine the final file offset of all the output sections which
350 // are not SHF_ALLOC, including the section table header.
352 // 9) Finalize the ELF file header.
354 // This function returns the size of the output file.
357 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
359 if (input_objects->any_dynamic())
361 // If there are any dynamic objects in the link, then we need
362 // some additional segments: PT_PHDRS, PT_INTERP, and
363 // PT_DYNAMIC. We also need to finalize the dynamic symbol
364 // table and create the dynamic hash table.
368 // FIXME: Handle PT_GNU_STACK.
370 Output_segment* load_seg = this->find_first_load_seg();
372 // Lay out the segment headers.
373 int size = input_objects->target()->get_size();
374 bool big_endian = input_objects->target()->is_big_endian();
375 Output_segment_headers* segment_headers;
376 segment_headers = new Output_segment_headers(size, big_endian,
377 this->segment_list_);
378 load_seg->add_initial_output_data(segment_headers);
379 this->special_output_list_.push_back(segment_headers);
380 // FIXME: Attach them to PT_PHDRS if necessary.
382 // Lay out the file header.
383 Output_file_header* file_header;
384 file_header = new Output_file_header(size,
387 input_objects->target(),
390 load_seg->add_initial_output_data(file_header);
391 this->special_output_list_.push_back(file_header);
393 // Set the file offsets of all the segments.
394 off_t off = this->set_segment_offsets(input_objects->target(), load_seg);
396 // Create the symbol table sections.
397 // FIXME: We don't need to do this if we are stripping symbols.
398 Output_section* osymtab;
399 Output_section* ostrtab;
400 this->create_symtab_sections(size, input_objects, symtab, &off,
403 // Create the .shstrtab section.
404 Output_section* shstrtab_section = this->create_shstrtab();
406 // Set the file offsets of all the sections not associated with
408 off = this->set_section_offsets(off);
410 // Create the section table header.
411 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
413 file_header->set_section_info(oshdrs, shstrtab_section);
415 // Now we know exactly where everything goes in the output file.
420 // Return whether SEG1 should be before SEG2 in the output file. This
421 // is based entirely on the segment type and flags. When this is
422 // called the segment addresses has normally not yet been set.
425 Layout::segment_precedes(const Output_segment* seg1,
426 const Output_segment* seg2)
428 elfcpp::Elf_Word type1 = seg1->type();
429 elfcpp::Elf_Word type2 = seg2->type();
431 // The single PT_PHDR segment is required to precede any loadable
432 // segment. We simply make it always first.
433 if (type1 == elfcpp::PT_PHDR)
435 assert(type2 != elfcpp::PT_PHDR);
438 if (type2 == elfcpp::PT_PHDR)
441 // The single PT_INTERP segment is required to precede any loadable
442 // segment. We simply make it always second.
443 if (type1 == elfcpp::PT_INTERP)
445 assert(type2 != elfcpp::PT_INTERP);
448 if (type2 == elfcpp::PT_INTERP)
451 // We then put PT_LOAD segments before any other segments.
452 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
454 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
457 const elfcpp::Elf_Word flags1 = seg1->flags();
458 const elfcpp::Elf_Word flags2 = seg2->flags();
460 // The order of non-PT_LOAD segments is unimportant. We simply sort
461 // by the numeric segment type and flags values. There should not
462 // be more than one segment with the same type and flags.
463 if (type1 != elfcpp::PT_LOAD)
466 return type1 < type2;
467 assert(flags1 != flags2);
468 return flags1 < flags2;
471 // We sort PT_LOAD segments based on the flags. Readonly segments
472 // come before writable segments. Then executable segments come
473 // before non-executable segments. Then the unlikely case of a
474 // non-readable segment comes before the normal case of a readable
475 // segment. If there are multiple segments with the same type and
476 // flags, we require that the address be set, and we sort by
477 // virtual address and then physical address.
478 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
479 return (flags1 & elfcpp::PF_W) == 0;
480 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
481 return (flags1 & elfcpp::PF_X) != 0;
482 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
483 return (flags1 & elfcpp::PF_R) == 0;
485 uint64_t vaddr1 = seg1->vaddr();
486 uint64_t vaddr2 = seg2->vaddr();
487 if (vaddr1 != vaddr2)
488 return vaddr1 < vaddr2;
490 uint64_t paddr1 = seg1->paddr();
491 uint64_t paddr2 = seg2->paddr();
492 assert(paddr1 != paddr2);
493 return paddr1 < paddr2;
496 // Set the file offsets of all the segments. They have all been
497 // created. LOAD_SEG must be be laid out first. Return the offset of
498 // the data to follow.
501 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg)
503 // Sort them into the final order.
504 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
505 Layout::Compare_segments());
507 // Find the PT_LOAD segments, and set their addresses and offsets
508 // and their section's addresses and offsets.
509 uint64_t addr = target->text_segment_address();
511 bool was_readonly = false;
512 for (Segment_list::iterator p = this->segment_list_.begin();
513 p != this->segment_list_.end();
516 if ((*p)->type() == elfcpp::PT_LOAD)
518 if (load_seg != NULL && load_seg != *p)
522 // If the last segment was readonly, and this one is not,
523 // then skip the address forward one page, maintaining the
524 // same position within the page. This lets us store both
525 // segments overlapping on a single page in the file, but
526 // the loader will put them on different pages in memory.
528 uint64_t orig_addr = addr;
529 uint64_t orig_off = off;
531 uint64_t aligned_addr = addr;
532 uint64_t abi_pagesize = target->abi_pagesize();
533 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
535 uint64_t align = (*p)->max_data_align();
537 addr = (addr + align - 1) & ~ (align - 1);
539 if ((addr & (abi_pagesize - 1)) != 0)
540 addr = addr + abi_pagesize;
543 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
544 uint64_t new_addr = (*p)->set_section_addresses(addr, &off);
546 // Now that we know the size of this segment, we may be able
547 // to save a page in memory, at the cost of wasting some
548 // file space, by instead aligning to the start of a new
549 // page. Here we use the real machine page size rather than
550 // the ABI mandated page size.
552 if (aligned_addr != addr)
554 uint64_t common_pagesize = target->common_pagesize();
555 uint64_t first_off = (common_pagesize
557 & (common_pagesize - 1)));
558 uint64_t last_off = new_addr & (common_pagesize - 1);
561 && ((aligned_addr & ~ (common_pagesize - 1))
562 != (new_addr & ~ (common_pagesize - 1)))
563 && first_off + last_off <= common_pagesize)
565 addr = ((aligned_addr + common_pagesize - 1)
566 & ~ (common_pagesize - 1));
567 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
568 new_addr = (*p)->set_section_addresses(addr, &off);
574 if (((*p)->flags() & elfcpp::PF_W) == 0)
579 // Handle the non-PT_LOAD segments, setting their offsets from their
580 // section's offsets.
581 for (Segment_list::iterator p = this->segment_list_.begin();
582 p != this->segment_list_.end();
585 if ((*p)->type() != elfcpp::PT_LOAD)
592 // Set the file offset of all the sections not associated with a
596 Layout::set_section_offsets(off_t off)
598 for (Layout::Section_list::iterator p = this->section_list_.begin();
599 p != this->section_list_.end();
602 if ((*p)->offset() != -1)
604 uint64_t addralign = (*p)->addralign();
606 off = (off + addralign - 1) & ~ (addralign - 1);
607 (*p)->set_address(0, off);
608 off += (*p)->data_size();
613 // Create the symbol table sections.
616 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
617 Symbol_table* symtab,
619 Output_section** posymtab,
620 Output_section** postrtab)
626 symsize = elfcpp::Elf_sizes<32>::sym_size;
631 symsize = elfcpp::Elf_sizes<64>::sym_size;
638 off = (off + align - 1) & ~ (align - 1);
639 off_t startoff = off;
641 // Save space for the dummy symbol at the start of the section. We
642 // never bother to write this out--it will just be left as zero.
645 for (Input_objects::Object_list::const_iterator p = input_objects->begin();
646 p != input_objects->end();
649 Task_lock_obj<Object> tlo(**p);
650 off = (*p)->finalize_local_symbols(off, &this->sympool_);
653 unsigned int local_symcount = (off - startoff) / symsize;
654 assert(local_symcount * symsize == off - startoff);
656 off = symtab->finalize(off, &this->sympool_);
658 this->sympool_.set_string_offsets();
661 const char* symtab_name = this->namepool_.add(".symtab");
662 Output_section* osymtab = new Output_section_symtab(symtab_name,
665 this->section_list_.push_back(osymtab);
668 const char* strtab_name = this->namepool_.add(".strtab");
669 Output_section *ostrtab = new Output_section_strtab(strtab_name,
672 this->section_list_.push_back(ostrtab);
673 this->special_output_list_.push_back(ostrtab);
675 osymtab->set_address(0, startoff);
676 osymtab->set_link(ostrtab->shndx());
677 osymtab->set_info(local_symcount);
678 osymtab->set_entsize(symsize);
679 osymtab->set_addralign(align);
686 // Create the .shstrtab section, which holds the names of the
687 // sections. At the time this is called, we have created all the
688 // output sections except .shstrtab itself.
691 Layout::create_shstrtab()
693 // FIXME: We don't need to create a .shstrtab section if we are
694 // stripping everything.
696 const char* name = this->namepool_.add(".shstrtab");
698 this->namepool_.set_string_offsets();
701 Output_section* os = new Output_section_strtab(name,
705 this->section_list_.push_back(os);
706 this->special_output_list_.push_back(os);
711 // Create the section headers. SIZE is 32 or 64. OFF is the file
714 Output_section_headers*
715 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
717 Output_section_headers* oshdrs;
718 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
721 uint64_t addralign = oshdrs->addralign();
722 off_t off = (*poff + addralign - 1) & ~ (addralign - 1);
723 oshdrs->set_address(0, off);
724 off += oshdrs->data_size();
726 this->special_output_list_.push_back(oshdrs);
730 // The mapping of .gnu.linkonce section names to real section names.
732 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
733 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
735 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
736 MAPPING_INIT("t", ".text"),
737 MAPPING_INIT("r", ".rodata"),
738 MAPPING_INIT("d", ".data"),
739 MAPPING_INIT("b", ".bss"),
740 MAPPING_INIT("s", ".sdata"),
741 MAPPING_INIT("sb", ".sbss"),
742 MAPPING_INIT("s2", ".sdata2"),
743 MAPPING_INIT("sb2", ".sbss2"),
744 MAPPING_INIT("wi", ".debug_info"),
745 MAPPING_INIT("td", ".tdata"),
746 MAPPING_INIT("tb", ".tbss"),
747 MAPPING_INIT("lr", ".lrodata"),
748 MAPPING_INIT("l", ".ldata"),
749 MAPPING_INIT("lb", ".lbss"),
753 const int Layout::linkonce_mapping_count =
754 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
756 // Return the name of the output section to use for a .gnu.linkonce
757 // section. This is based on the default ELF linker script of the old
758 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
762 Layout::linkonce_output_name(const char* name)
764 const char* s = name + sizeof(".gnu.linkonce") - 1;
768 const Linkonce_mapping* plm = linkonce_mapping;
769 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
771 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
777 // Record the signature of a comdat section, and return whether to
778 // include it in the link. If GROUP is true, this is a regular
779 // section group. If GROUP is false, this is a group signature
780 // derived from the name of a linkonce section. We want linkonce
781 // signatures and group signatures to block each other, but we don't
782 // want a linkonce signature to block another linkonce signature.
785 Layout::add_comdat(const char* signature, bool group)
787 std::string sig(signature);
788 std::pair<Signatures::iterator, bool> ins(
789 this->signatures_.insert(std::make_pair(signature, group)));
793 // This is the first time we've seen this signature.
797 if (ins.first->second)
799 // We've already seen a real section group with this signature.
804 // This is a real section group, and we've already seen a
805 // linkonce section with tihs signature. Record that we've seen
806 // a section group, and don't include this section group.
807 ins.first->second = true;
812 // We've already seen a linkonce section and this is a linkonce
813 // section. These don't block each other--this may be the same
814 // symbol name with different section types.
819 // Write out data not associated with a section or the symbol table.
822 Layout::write_data(Output_file* of) const
824 for (Data_list::const_iterator p = this->special_output_list_.begin();
825 p != this->special_output_list_.end();
830 // Write_data_task methods.
832 // We can always run this task.
834 Task::Is_runnable_type
835 Write_data_task::is_runnable(Workqueue*)
840 // We need to unlock FINAL_BLOCKER when finished.
843 Write_data_task::locks(Workqueue* workqueue)
845 return new Task_locker_block(*this->final_blocker_, workqueue);
848 // Run the task--write out the data.
851 Write_data_task::run(Workqueue*)
853 this->layout_->write_data(this->of_);
856 // Write_symbols_task methods.
858 // We can always run this task.
860 Task::Is_runnable_type
861 Write_symbols_task::is_runnable(Workqueue*)
866 // We need to unlock FINAL_BLOCKER when finished.
869 Write_symbols_task::locks(Workqueue* workqueue)
871 return new Task_locker_block(*this->final_blocker_, workqueue);
874 // Run the task--write out the symbols.
877 Write_symbols_task::run(Workqueue*)
879 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
882 // Close_task methods.
884 // We can't run until FINAL_BLOCKER is unblocked.
886 Task::Is_runnable_type
887 Close_task::is_runnable(Workqueue*)
889 if (this->final_blocker_->is_blocked())
894 // We don't lock anything.
897 Close_task::locks(Workqueue*)
902 // Run the task--close the file.
905 Close_task::run(Workqueue*)
910 // Instantiate the templates we need. We could use the configure
911 // script to restrict this to only the ones for implemented targets.
915 Layout::layout<32, false>(Object* object, const char* name,
916 const elfcpp::Shdr<32, false>& shdr, off_t*);
920 Layout::layout<32, true>(Object* object, const char* name,
921 const elfcpp::Shdr<32, true>& shdr, off_t*);
925 Layout::layout<64, false>(Object* object, const char* name,
926 const elfcpp::Shdr<64, false>& shdr, off_t*);
930 Layout::layout<64, true>(Object* object, const char* name,
931 const elfcpp::Shdr<64, true>& shdr, off_t*);
934 } // End namespace gold.