1 // layout.cc -- lay out output file sections for gold
17 // Layout_task_runner methods.
19 // Lay out the sections. This is called after all the input objects
23 Layout_task_runner::run(Workqueue* workqueue)
25 off_t file_size = this->layout_->finalize(this->input_objects_,
28 // Now we know the final size of the output file and we know where
29 // each piece of information goes.
30 Output_file* of = new Output_file(this->options_);
33 // Queue up the final set of tasks.
34 gold::queue_final_tasks(this->options_, this->input_objects_,
35 this->symtab_, this->layout_, workqueue, of);
40 Layout::Layout(const General_options& options)
41 : options_(options), last_shndx_(0), namepool_(), sympool_(), signatures_(),
42 section_name_map_(), segment_list_(), section_list_(),
43 special_output_list_(), tls_segment_(NULL)
45 // Make space for more than enough segments for a typical file.
46 // This is just for efficiency--it's OK if we wind up needing more.
47 segment_list_.reserve(12);
50 // Hash a key we use to look up an output section mapping.
53 Layout::Hash_key::operator()(const Layout::Key& k) const
55 return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
58 // Whether to include this section in the link.
60 template<int size, bool big_endian>
62 Layout::include_section(Object*, const char*,
63 const elfcpp::Shdr<size, big_endian>& shdr)
65 // Some section types are never linked. Some are only linked when
66 // doing a relocateable link.
67 switch (shdr.get_sh_type())
69 case elfcpp::SHT_NULL:
70 case elfcpp::SHT_SYMTAB:
71 case elfcpp::SHT_DYNSYM:
72 case elfcpp::SHT_STRTAB:
73 case elfcpp::SHT_HASH:
74 case elfcpp::SHT_DYNAMIC:
75 case elfcpp::SHT_SYMTAB_SHNDX:
78 case elfcpp::SHT_RELA:
80 case elfcpp::SHT_GROUP:
81 return this->options_.is_relocatable();
84 // FIXME: Handle stripping debug sections here.
89 // Return the output section to use for input section NAME, with
90 // header HEADER, from object OBJECT. Set *OFF to the offset of this
91 // input section without the output section.
93 template<int size, bool big_endian>
95 Layout::layout(Object* object, const char* name,
96 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
98 // We discard empty input sections.
99 if (shdr.get_sh_size() == 0)
102 if (!this->include_section(object, name, shdr))
105 // Unless we are doing a relocateable link, .gnu.linkonce sections
106 // are laid out as though they were named for the sections are
108 if (!this->options_.is_relocatable() && Layout::is_linkonce(name))
109 name = Layout::linkonce_output_name(name);
111 // FIXME: Handle SHF_OS_NONCONFORMING here.
113 // Canonicalize the section name.
114 name = this->namepool_.add(name);
116 // Find the output section. The output section is selected based on
117 // the section name, type, and flags.
119 // FIXME: If we want to do relaxation, we need to modify this
120 // algorithm. We also build a list of input sections for each
121 // output section. Then we relax all the input sections. Then we
122 // walk down the list and adjust all the offsets.
124 elfcpp::Elf_Word type = shdr.get_sh_type();
125 elfcpp::Elf_Xword flags = shdr.get_sh_flags();
126 const Key key(name, std::make_pair(type, flags));
127 const std::pair<Key, Output_section*> v(key, NULL);
128 std::pair<Section_name_map::iterator, bool> ins(
129 this->section_name_map_.insert(v));
133 os = ins.first->second;
136 // This is the first time we've seen this name/type/flags
138 os = this->make_output_section(name, type, flags);
139 ins.first->second = os;
142 // FIXME: Handle SHF_LINK_ORDER somewhere.
144 *off = os->add_input_section(object, name, shdr);
149 // Map section flags to segment flags.
152 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
154 elfcpp::Elf_Word ret = elfcpp::PF_R;
155 if ((flags & elfcpp::SHF_WRITE) != 0)
157 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
162 // Make a new Output_section, and attach it to segments as
166 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
167 elfcpp::Elf_Xword flags)
170 Output_section* os = new Output_section(name, type, flags,
173 if ((flags & elfcpp::SHF_ALLOC) == 0)
174 this->section_list_.push_back(os);
177 // This output section goes into a PT_LOAD segment.
179 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
181 // The only thing we really care about for PT_LOAD segments is
182 // whether or not they are writable, so that is how we search
183 // for them. People who need segments sorted on some other
184 // basis will have to wait until we implement a mechanism for
185 // them to describe the segments they want.
187 Segment_list::const_iterator p;
188 for (p = this->segment_list_.begin();
189 p != this->segment_list_.end();
192 if ((*p)->type() == elfcpp::PT_LOAD
193 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
195 (*p)->add_output_section(os, seg_flags);
200 if (p == this->segment_list_.end())
202 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
204 this->segment_list_.push_back(oseg);
205 oseg->add_output_section(os, seg_flags);
208 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
210 if (type == elfcpp::SHT_NOTE)
212 // See if we already have an equivalent PT_NOTE segment.
213 for (p = this->segment_list_.begin();
214 p != segment_list_.end();
217 if ((*p)->type() == elfcpp::PT_NOTE
218 && (((*p)->flags() & elfcpp::PF_W)
219 == (seg_flags & elfcpp::PF_W)))
221 (*p)->add_output_section(os, seg_flags);
226 if (p == this->segment_list_.end())
228 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
230 this->segment_list_.push_back(oseg);
231 oseg->add_output_section(os, seg_flags);
235 // If we see a loadable SHF_TLS section, we create a PT_TLS
236 // segment. There can only be one such segment.
237 if ((flags & elfcpp::SHF_TLS) != 0)
239 if (this->tls_segment_ == NULL)
241 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
243 this->segment_list_.push_back(this->tls_segment_);
245 this->tls_segment_->add_output_section(os, seg_flags);
252 // Find the first read-only PT_LOAD segment, creating one if
256 Layout::find_first_load_seg()
258 for (Segment_list::const_iterator p = this->segment_list_.begin();
259 p != this->segment_list_.end();
262 if ((*p)->type() == elfcpp::PT_LOAD
263 && ((*p)->flags() & elfcpp::PF_R) != 0
264 && ((*p)->flags() & elfcpp::PF_W) == 0)
268 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
269 this->segment_list_.push_back(load_seg);
273 // Finalize the layout. When this is called, we have created all the
274 // output sections and all the output segments which are based on
275 // input sections. We have several things to do, and we have to do
276 // them in the right order, so that we get the right results correctly
279 // 1) Finalize the list of output segments and create the segment
282 // 2) Finalize the dynamic symbol table and associated sections.
284 // 3) Determine the final file offset of all the output segments.
286 // 4) Determine the final file offset of all the SHF_ALLOC output
289 // 5) Create the symbol table sections and the section name table
292 // 6) Finalize the symbol table: set symbol values to their final
293 // value and make a final determination of which symbols are going
294 // into the output symbol table.
296 // 7) Create the section table header.
298 // 8) Determine the final file offset of all the output sections which
299 // are not SHF_ALLOC, including the section table header.
301 // 9) Finalize the ELF file header.
303 // This function returns the size of the output file.
306 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
308 if (input_objects->any_dynamic())
310 // If there are any dynamic objects in the link, then we need
311 // some additional segments: PT_PHDRS, PT_INTERP, and
312 // PT_DYNAMIC. We also need to finalize the dynamic symbol
313 // table and create the dynamic hash table.
317 // FIXME: Handle PT_GNU_STACK.
319 Output_segment* load_seg = this->find_first_load_seg();
321 // Lay out the segment headers.
322 int size = input_objects->target()->get_size();
323 bool big_endian = input_objects->target()->is_big_endian();
324 Output_segment_headers* segment_headers;
325 segment_headers = new Output_segment_headers(size, big_endian,
326 this->segment_list_);
327 load_seg->add_initial_output_data(segment_headers);
328 this->special_output_list_.push_back(segment_headers);
329 // FIXME: Attach them to PT_PHDRS if necessary.
331 // Lay out the file header.
332 Output_file_header* file_header;
333 file_header = new Output_file_header(size,
336 input_objects->target(),
339 load_seg->add_initial_output_data(file_header);
340 this->special_output_list_.push_back(file_header);
342 // Set the file offsets of all the segments.
343 off_t off = this->set_segment_offsets(input_objects->target(), load_seg);
345 // Create the symbol table sections.
346 // FIXME: We don't need to do this if we are stripping symbols.
347 Output_section* osymtab;
348 Output_section* ostrtab;
349 this->create_symtab_sections(size, input_objects, symtab, &off,
352 // Create the .shstrtab section.
353 Output_section* shstrtab_section = this->create_shstrtab();
355 // Set the file offsets of all the sections not associated with
357 off = this->set_section_offsets(off);
359 // Create the section table header.
360 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
362 file_header->set_section_info(oshdrs, shstrtab_section);
364 // Now we know exactly where everything goes in the output file.
369 // Return whether SEG1 should be before SEG2 in the output file. This
370 // is based entirely on the segment type and flags. When this is
371 // called the segment addresses has normally not yet been set.
374 Layout::segment_precedes(const Output_segment* seg1,
375 const Output_segment* seg2)
377 elfcpp::Elf_Word type1 = seg1->type();
378 elfcpp::Elf_Word type2 = seg2->type();
380 // The single PT_PHDR segment is required to precede any loadable
381 // segment. We simply make it always first.
382 if (type1 == elfcpp::PT_PHDR)
384 assert(type2 != elfcpp::PT_PHDR);
387 if (type2 == elfcpp::PT_PHDR)
390 // The single PT_INTERP segment is required to precede any loadable
391 // segment. We simply make it always second.
392 if (type1 == elfcpp::PT_INTERP)
394 assert(type2 != elfcpp::PT_INTERP);
397 if (type2 == elfcpp::PT_INTERP)
400 // We then put PT_LOAD segments before any other segments.
401 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
403 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
406 // We put the PT_TLS segment last, because that is where the dynamic
407 // linker expects to find it (this is just for efficiency; other
408 // positions would also work correctly).
409 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
411 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
414 const elfcpp::Elf_Word flags1 = seg1->flags();
415 const elfcpp::Elf_Word flags2 = seg2->flags();
417 // The order of non-PT_LOAD segments is unimportant. We simply sort
418 // by the numeric segment type and flags values. There should not
419 // be more than one segment with the same type and flags.
420 if (type1 != elfcpp::PT_LOAD)
423 return type1 < type2;
424 assert(flags1 != flags2);
425 return flags1 < flags2;
428 // We sort PT_LOAD segments based on the flags. Readonly segments
429 // come before writable segments. Then executable segments come
430 // before non-executable segments. Then the unlikely case of a
431 // non-readable segment comes before the normal case of a readable
432 // segment. If there are multiple segments with the same type and
433 // flags, we require that the address be set, and we sort by
434 // virtual address and then physical address.
435 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
436 return (flags1 & elfcpp::PF_W) == 0;
437 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
438 return (flags1 & elfcpp::PF_X) != 0;
439 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
440 return (flags1 & elfcpp::PF_R) == 0;
442 uint64_t vaddr1 = seg1->vaddr();
443 uint64_t vaddr2 = seg2->vaddr();
444 if (vaddr1 != vaddr2)
445 return vaddr1 < vaddr2;
447 uint64_t paddr1 = seg1->paddr();
448 uint64_t paddr2 = seg2->paddr();
449 assert(paddr1 != paddr2);
450 return paddr1 < paddr2;
453 // Set the file offsets of all the segments. They have all been
454 // created. LOAD_SEG must be be laid out first. Return the offset of
455 // the data to follow.
458 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg)
460 // Sort them into the final order.
461 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
462 Layout::Compare_segments());
464 // Find the PT_LOAD segments, and set their addresses and offsets
465 // and their section's addresses and offsets.
466 uint64_t addr = target->text_segment_address();
468 bool was_readonly = false;
469 for (Segment_list::iterator p = this->segment_list_.begin();
470 p != this->segment_list_.end();
473 if ((*p)->type() == elfcpp::PT_LOAD)
475 if (load_seg != NULL && load_seg != *p)
479 // If the last segment was readonly, and this one is not,
480 // then skip the address forward one page, maintaining the
481 // same position within the page. This lets us store both
482 // segments overlapping on a single page in the file, but
483 // the loader will put them on different pages in memory.
485 uint64_t orig_addr = addr;
486 uint64_t orig_off = off;
488 uint64_t aligned_addr = addr;
489 uint64_t abi_pagesize = target->abi_pagesize();
490 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
492 uint64_t align = (*p)->max_data_align();
494 addr = (addr + align - 1) & ~ (align - 1);
496 if ((addr & (abi_pagesize - 1)) != 0)
497 addr = addr + abi_pagesize;
500 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
501 uint64_t new_addr = (*p)->set_section_addresses(addr, &off);
503 // Now that we know the size of this segment, we may be able
504 // to save a page in memory, at the cost of wasting some
505 // file space, by instead aligning to the start of a new
506 // page. Here we use the real machine page size rather than
507 // the ABI mandated page size.
509 if (aligned_addr != addr)
511 uint64_t common_pagesize = target->common_pagesize();
512 uint64_t first_off = (common_pagesize
514 & (common_pagesize - 1)));
515 uint64_t last_off = new_addr & (common_pagesize - 1);
518 && ((aligned_addr & ~ (common_pagesize - 1))
519 != (new_addr & ~ (common_pagesize - 1)))
520 && first_off + last_off <= common_pagesize)
522 addr = ((aligned_addr + common_pagesize - 1)
523 & ~ (common_pagesize - 1));
524 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
525 new_addr = (*p)->set_section_addresses(addr, &off);
531 if (((*p)->flags() & elfcpp::PF_W) == 0)
536 // Handle the non-PT_LOAD segments, setting their offsets from their
537 // section's offsets.
538 for (Segment_list::iterator p = this->segment_list_.begin();
539 p != this->segment_list_.end();
542 if ((*p)->type() != elfcpp::PT_LOAD)
549 // Set the file offset of all the sections not associated with a
553 Layout::set_section_offsets(off_t off)
555 for (Layout::Section_list::iterator p = this->section_list_.begin();
556 p != this->section_list_.end();
559 if ((*p)->offset() != -1)
561 uint64_t addralign = (*p)->addralign();
563 off = (off + addralign - 1) & ~ (addralign - 1);
564 (*p)->set_address(0, off);
565 off += (*p)->data_size();
570 // Create the symbol table sections.
573 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
574 Symbol_table* symtab,
576 Output_section** posymtab,
577 Output_section** postrtab)
583 symsize = elfcpp::Elf_sizes<32>::sym_size;
588 symsize = elfcpp::Elf_sizes<64>::sym_size;
595 off = (off + align - 1) & ~ (align - 1);
596 off_t startoff = off;
598 // Save space for the dummy symbol at the start of the section. We
599 // never bother to write this out--it will just be left as zero.
602 for (Input_objects::Object_list::const_iterator p = input_objects->begin();
603 p != input_objects->end();
606 Task_lock_obj<Object> tlo(**p);
607 off = (*p)->finalize_local_symbols(off, &this->sympool_);
610 unsigned int local_symcount = (off - startoff) / symsize;
611 assert(local_symcount * symsize == off - startoff);
613 off = symtab->finalize(off, &this->sympool_);
615 this->sympool_.set_string_offsets();
618 const char* symtab_name = this->namepool_.add(".symtab");
619 Output_section* osymtab = new Output_section_symtab(symtab_name,
622 this->section_list_.push_back(osymtab);
625 const char* strtab_name = this->namepool_.add(".strtab");
626 Output_section *ostrtab = new Output_section_strtab(strtab_name,
629 this->section_list_.push_back(ostrtab);
630 this->special_output_list_.push_back(ostrtab);
632 osymtab->set_address(0, startoff);
633 osymtab->set_link(ostrtab->shndx());
634 osymtab->set_info(local_symcount);
635 osymtab->set_entsize(symsize);
636 osymtab->set_addralign(align);
643 // Create the .shstrtab section, which holds the names of the
644 // sections. At the time this is called, we have created all the
645 // output sections except .shstrtab itself.
648 Layout::create_shstrtab()
650 // FIXME: We don't need to create a .shstrtab section if we are
651 // stripping everything.
653 const char* name = this->namepool_.add(".shstrtab");
655 this->namepool_.set_string_offsets();
658 Output_section* os = new Output_section_strtab(name,
662 this->section_list_.push_back(os);
663 this->special_output_list_.push_back(os);
668 // Create the section headers. SIZE is 32 or 64. OFF is the file
671 Output_section_headers*
672 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
674 Output_section_headers* oshdrs;
675 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
678 uint64_t addralign = oshdrs->addralign();
679 off_t off = (*poff + addralign - 1) & ~ (addralign - 1);
680 oshdrs->set_address(0, off);
681 off += oshdrs->data_size();
683 this->special_output_list_.push_back(oshdrs);
687 // The mapping of .gnu.linkonce section names to real section names.
689 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
690 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
692 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
693 MAPPING_INIT("t", ".text"),
694 MAPPING_INIT("r", ".rodata"),
695 MAPPING_INIT("d", ".data"),
696 MAPPING_INIT("b", ".bss"),
697 MAPPING_INIT("s", ".sdata"),
698 MAPPING_INIT("sb", ".sbss"),
699 MAPPING_INIT("s2", ".sdata2"),
700 MAPPING_INIT("sb2", ".sbss2"),
701 MAPPING_INIT("wi", ".debug_info"),
702 MAPPING_INIT("td", ".tdata"),
703 MAPPING_INIT("tb", ".tbss"),
704 MAPPING_INIT("lr", ".lrodata"),
705 MAPPING_INIT("l", ".ldata"),
706 MAPPING_INIT("lb", ".lbss"),
710 const int Layout::linkonce_mapping_count =
711 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
713 // Return the name of the output section to use for a .gnu.linkonce
714 // section. This is based on the default ELF linker script of the old
715 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
719 Layout::linkonce_output_name(const char* name)
721 const char* s = name + sizeof(".gnu.linkonce") - 1;
725 const Linkonce_mapping* plm = linkonce_mapping;
726 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
728 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
734 // Record the signature of a comdat section, and return whether to
735 // include it in the link. If GROUP is true, this is a regular
736 // section group. If GROUP is false, this is a group signature
737 // derived from the name of a linkonce section. We want linkonce
738 // signatures and group signatures to block each other, but we don't
739 // want a linkonce signature to block another linkonce signature.
742 Layout::add_comdat(const char* signature, bool group)
744 std::string sig(signature);
745 std::pair<Signatures::iterator, bool> ins(
746 this->signatures_.insert(std::make_pair(signature, group)));
750 // This is the first time we've seen this signature.
754 if (ins.first->second)
756 // We've already seen a real section group with this signature.
761 // This is a real section group, and we've already seen a
762 // linkonce section with tihs signature. Record that we've seen
763 // a section group, and don't include this section group.
764 ins.first->second = true;
769 // We've already seen a linkonce section and this is a linkonce
770 // section. These don't block each other--this may be the same
771 // symbol name with different section types.
776 // Write out data not associated with a section or the symbol table.
779 Layout::write_data(Output_file* of) const
781 for (Data_list::const_iterator p = this->special_output_list_.begin();
782 p != this->special_output_list_.end();
787 // Write_data_task methods.
789 // We can always run this task.
791 Task::Is_runnable_type
792 Write_data_task::is_runnable(Workqueue*)
797 // We need to unlock FINAL_BLOCKER when finished.
800 Write_data_task::locks(Workqueue* workqueue)
802 return new Task_locker_block(*this->final_blocker_, workqueue);
805 // Run the task--write out the data.
808 Write_data_task::run(Workqueue*)
810 this->layout_->write_data(this->of_);
813 // Write_symbols_task methods.
815 // We can always run this task.
817 Task::Is_runnable_type
818 Write_symbols_task::is_runnable(Workqueue*)
823 // We need to unlock FINAL_BLOCKER when finished.
826 Write_symbols_task::locks(Workqueue* workqueue)
828 return new Task_locker_block(*this->final_blocker_, workqueue);
831 // Run the task--write out the symbols.
834 Write_symbols_task::run(Workqueue*)
836 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
839 // Close_task_runner methods.
841 // Run the task--close the file.
844 Close_task_runner::run(Workqueue*)
849 // Instantiate the templates we need. We could use the configure
850 // script to restrict this to only the ones for implemented targets.
854 Layout::layout<32, false>(Object* object, const char* name,
855 const elfcpp::Shdr<32, false>& shdr, off_t*);
859 Layout::layout<32, true>(Object* object, const char* name,
860 const elfcpp::Shdr<32, true>& shdr, off_t*);
864 Layout::layout<64, false>(Object* object, const char* name,
865 const elfcpp::Shdr<64, false>& shdr, off_t*);
869 Layout::layout<64, true>(Object* object, const char* name,
870 const elfcpp::Shdr<64, true>& shdr, off_t*);
873 } // End namespace gold.