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 Target* const target = input_objects->target();
387 const int size = target->get_size();
389 target->finalize_sections(this);
391 Output_segment* phdr_seg = NULL;
392 if (input_objects->any_dynamic())
394 // There was a dynamic object in the link. We need to create
395 // some information for the dynamic linker.
397 // Create the PT_PHDR segment which will hold the program
399 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
400 this->segment_list_.push_back(phdr_seg);
402 // This holds the dynamic tags.
403 Output_data_dynamic* odyn;
404 odyn = new Output_data_dynamic(input_objects->target(),
407 // Create the dynamic symbol table, including the hash table,
408 // the dynamic relocations, and the version sections.
409 this->create_dynamic_symtab(target, odyn, symtab);
411 // Create the .interp section to hold the name of the
412 // interpreter, and put it in a PT_INTERP segment.
413 this->create_interp(target);
415 // Finish the .dynamic section to hold the dynamic data, and put
416 // it in a PT_DYNAMIC segment.
417 this->finish_dynamic_section(input_objects, symtab, odyn);
420 // FIXME: Handle PT_GNU_STACK.
422 Output_segment* load_seg = this->find_first_load_seg();
424 // Lay out the segment headers.
425 bool big_endian = target->is_big_endian();
426 Output_segment_headers* segment_headers;
427 segment_headers = new Output_segment_headers(size, big_endian,
428 this->segment_list_);
429 load_seg->add_initial_output_data(segment_headers);
430 this->special_output_list_.push_back(segment_headers);
431 if (phdr_seg != NULL)
432 phdr_seg->add_initial_output_data(segment_headers);
434 // Lay out the file header.
435 Output_file_header* file_header;
436 file_header = new Output_file_header(size,
442 load_seg->add_initial_output_data(file_header);
443 this->special_output_list_.push_back(file_header);
445 // We set the output section indexes in set_segment_offsets and
446 // set_section_offsets.
447 unsigned int shndx = 1;
449 // Set the file offsets of all the segments, and all the sections
451 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
453 // Create the symbol table sections.
454 // FIXME: We don't need to do this if we are stripping symbols.
455 Output_section* ostrtab;
456 this->create_symtab_sections(size, input_objects, symtab, &off,
459 // Create the .shstrtab section.
460 Output_section* shstrtab_section = this->create_shstrtab();
462 // Set the file offsets of all the sections not associated with
464 off = this->set_section_offsets(off, &shndx);
466 // Now the section index of OSTRTAB is set.
467 this->symtab_section_->set_link(ostrtab->out_shndx());
469 // Create the section table header.
470 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
472 file_header->set_section_info(oshdrs, shstrtab_section);
474 // Now we know exactly where everything goes in the output file.
475 Output_data::layout_complete();
480 // Return whether SEG1 should be before SEG2 in the output file. This
481 // is based entirely on the segment type and flags. When this is
482 // called the segment addresses has normally not yet been set.
485 Layout::segment_precedes(const Output_segment* seg1,
486 const Output_segment* seg2)
488 elfcpp::Elf_Word type1 = seg1->type();
489 elfcpp::Elf_Word type2 = seg2->type();
491 // The single PT_PHDR segment is required to precede any loadable
492 // segment. We simply make it always first.
493 if (type1 == elfcpp::PT_PHDR)
495 gold_assert(type2 != elfcpp::PT_PHDR);
498 if (type2 == elfcpp::PT_PHDR)
501 // The single PT_INTERP segment is required to precede any loadable
502 // segment. We simply make it always second.
503 if (type1 == elfcpp::PT_INTERP)
505 gold_assert(type2 != elfcpp::PT_INTERP);
508 if (type2 == elfcpp::PT_INTERP)
511 // We then put PT_LOAD segments before any other segments.
512 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
514 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
517 // We put the PT_TLS segment last, because that is where the dynamic
518 // linker expects to find it (this is just for efficiency; other
519 // positions would also work correctly).
520 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
522 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
525 const elfcpp::Elf_Word flags1 = seg1->flags();
526 const elfcpp::Elf_Word flags2 = seg2->flags();
528 // The order of non-PT_LOAD segments is unimportant. We simply sort
529 // by the numeric segment type and flags values. There should not
530 // be more than one segment with the same type and flags.
531 if (type1 != elfcpp::PT_LOAD)
534 return type1 < type2;
535 gold_assert(flags1 != flags2);
536 return flags1 < flags2;
539 // We sort PT_LOAD segments based on the flags. Readonly segments
540 // come before writable segments. Then executable segments come
541 // before non-executable segments. Then the unlikely case of a
542 // non-readable segment comes before the normal case of a readable
543 // segment. If there are multiple segments with the same type and
544 // flags, we require that the address be set, and we sort by
545 // virtual address and then physical address.
546 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
547 return (flags1 & elfcpp::PF_W) == 0;
548 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
549 return (flags1 & elfcpp::PF_X) != 0;
550 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
551 return (flags1 & elfcpp::PF_R) == 0;
553 uint64_t vaddr1 = seg1->vaddr();
554 uint64_t vaddr2 = seg2->vaddr();
555 if (vaddr1 != vaddr2)
556 return vaddr1 < vaddr2;
558 uint64_t paddr1 = seg1->paddr();
559 uint64_t paddr2 = seg2->paddr();
560 gold_assert(paddr1 != paddr2);
561 return paddr1 < paddr2;
564 // Set the file offsets of all the segments, and all the sections they
565 // contain. They have all been created. LOAD_SEG must be be laid out
566 // first. Return the offset of the data to follow.
569 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
570 unsigned int *pshndx)
572 // Sort them into the final order.
573 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
574 Layout::Compare_segments());
576 // Find the PT_LOAD segments, and set their addresses and offsets
577 // and their section's addresses and offsets.
578 uint64_t addr = target->text_segment_address();
580 bool was_readonly = false;
581 for (Segment_list::iterator p = this->segment_list_.begin();
582 p != this->segment_list_.end();
585 if ((*p)->type() == elfcpp::PT_LOAD)
587 if (load_seg != NULL && load_seg != *p)
591 // If the last segment was readonly, and this one is not,
592 // then skip the address forward one page, maintaining the
593 // same position within the page. This lets us store both
594 // segments overlapping on a single page in the file, but
595 // the loader will put them on different pages in memory.
597 uint64_t orig_addr = addr;
598 uint64_t orig_off = off;
600 uint64_t aligned_addr = addr;
601 uint64_t abi_pagesize = target->abi_pagesize();
602 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
604 uint64_t align = (*p)->addralign();
606 addr = align_address(addr, align);
608 if ((addr & (abi_pagesize - 1)) != 0)
609 addr = addr + abi_pagesize;
612 unsigned int shndx_hold = *pshndx;
613 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
614 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
616 // Now that we know the size of this segment, we may be able
617 // to save a page in memory, at the cost of wasting some
618 // file space, by instead aligning to the start of a new
619 // page. Here we use the real machine page size rather than
620 // the ABI mandated page size.
622 if (aligned_addr != addr)
624 uint64_t common_pagesize = target->common_pagesize();
625 uint64_t first_off = (common_pagesize
627 & (common_pagesize - 1)));
628 uint64_t last_off = new_addr & (common_pagesize - 1);
631 && ((aligned_addr & ~ (common_pagesize - 1))
632 != (new_addr & ~ (common_pagesize - 1)))
633 && first_off + last_off <= common_pagesize)
635 *pshndx = shndx_hold;
636 addr = align_address(aligned_addr, common_pagesize);
637 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
638 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
644 if (((*p)->flags() & elfcpp::PF_W) == 0)
649 // Handle the non-PT_LOAD segments, setting their offsets from their
650 // section's offsets.
651 for (Segment_list::iterator p = this->segment_list_.begin();
652 p != this->segment_list_.end();
655 if ((*p)->type() != elfcpp::PT_LOAD)
662 // Set the file offset of all the sections not associated with a
666 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
668 for (Section_list::iterator p = this->unattached_section_list_.begin();
669 p != this->unattached_section_list_.end();
672 (*p)->set_out_shndx(*pshndx);
674 if ((*p)->offset() != -1)
676 off = align_address(off, (*p)->addralign());
677 (*p)->set_address(0, off);
678 off += (*p)->data_size();
683 // Create the symbol table sections.
686 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
687 Symbol_table* symtab,
689 Output_section** postrtab)
695 symsize = elfcpp::Elf_sizes<32>::sym_size;
700 symsize = elfcpp::Elf_sizes<64>::sym_size;
707 off = align_address(off, align);
708 off_t startoff = off;
710 // Save space for the dummy symbol at the start of the section. We
711 // never bother to write this out--it will just be left as zero.
713 unsigned int local_symbol_index = 1;
715 // Add STT_SECTION symbols for each Output section which needs one.
716 for (Section_list::iterator p = this->section_list_.begin();
717 p != this->section_list_.end();
720 if (!(*p)->needs_symtab_index())
721 (*p)->set_symtab_index(-1U);
724 (*p)->set_symtab_index(local_symbol_index);
725 ++local_symbol_index;
730 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
731 p != input_objects->relobj_end();
734 Task_lock_obj<Object> tlo(**p);
735 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
738 off += (index - local_symbol_index) * symsize;
739 local_symbol_index = index;
742 unsigned int local_symcount = local_symbol_index;
743 gold_assert(local_symcount * symsize == off - startoff);
745 off = symtab->finalize(local_symcount, off, &this->sympool_);
747 this->sympool_.set_string_offsets();
749 const char* symtab_name = this->namepool_.add(".symtab", NULL);
750 Output_section* osymtab = this->make_output_section(symtab_name,
753 this->symtab_section_ = osymtab;
755 Output_section_data* pos = new Output_data_space(off - startoff,
757 osymtab->add_output_section_data(pos);
759 const char* strtab_name = this->namepool_.add(".strtab", NULL);
760 Output_section* ostrtab = this->make_output_section(strtab_name,
764 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
765 ostrtab->add_output_section_data(pstr);
767 osymtab->set_address(0, startoff);
768 osymtab->set_info(local_symcount);
769 osymtab->set_entsize(symsize);
775 // Create the .shstrtab section, which holds the names of the
776 // sections. At the time this is called, we have created all the
777 // output sections except .shstrtab itself.
780 Layout::create_shstrtab()
782 // FIXME: We don't need to create a .shstrtab section if we are
783 // stripping everything.
785 const char* name = this->namepool_.add(".shstrtab", NULL);
787 this->namepool_.set_string_offsets();
789 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
791 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
792 os->add_output_section_data(posd);
797 // Create the section headers. SIZE is 32 or 64. OFF is the file
800 Output_section_headers*
801 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
803 Output_section_headers* oshdrs;
804 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
805 this->unattached_section_list_,
807 off_t off = align_address(*poff, oshdrs->addralign());
808 oshdrs->set_address(0, off);
809 off += oshdrs->data_size();
811 this->special_output_list_.push_back(oshdrs);
815 // Create the dynamic symbol table.
818 Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
819 Symbol_table* symtab)
821 // Count all the symbols in the dynamic symbol table, and set the
822 // dynamic symbol indexes.
824 // Skip symbol 0, which is always all zeroes.
825 unsigned int index = 1;
827 // Add STT_SECTION symbols for each Output section which needs one.
828 for (Section_list::iterator p = this->section_list_.begin();
829 p != this->section_list_.end();
832 if (!(*p)->needs_dynsym_index())
833 (*p)->set_dynsym_index(-1U);
836 (*p)->set_dynsym_index(index);
841 // FIXME: Some targets apparently require local symbols in the
842 // dynamic symbol table. Here is where we will have to count them,
843 // and set the dynamic symbol indexes, and add the names to
846 unsigned int local_symcount = index;
848 std::vector<Symbol*> dynamic_symbols;
850 // FIXME: We have to tell set_dynsym_indexes whether the
851 // -E/--export-dynamic option was used.
852 index = symtab->set_dynsym_indexes(index, &dynamic_symbols,
857 const int size = target->get_size();
860 symsize = elfcpp::Elf_sizes<32>::sym_size;
865 symsize = elfcpp::Elf_sizes<64>::sym_size;
871 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
872 Output_section* dynsym = this->make_output_section(dynsym_name,
876 Output_section_data* odata = new Output_data_space(index * symsize,
878 dynsym->add_output_section_data(odata);
880 dynsym->set_info(local_symcount);
881 dynsym->set_entsize(symsize);
882 dynsym->set_addralign(align);
884 this->dynsym_section_ = dynsym;
886 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
887 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
889 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
890 Output_section* dynstr = this->make_output_section(dynstr_name,
894 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
895 dynstr->add_output_section_data(strdata);
897 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
898 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
900 // FIXME: We need an option to create a GNU hash table.
902 unsigned char* phash;
903 unsigned int hashlen;
904 Dynobj::create_elf_hash_table(target, dynamic_symbols, local_symcount,
907 const char* hash_name = this->namepool_.add(".hash", NULL);
908 Output_section* hashsec = this->make_output_section(hash_name,
912 Output_section_data* hashdata = new Output_data_const_buffer(phash,
915 hashsec->add_output_section_data(hashdata);
917 hashsec->set_entsize(4);
918 // FIXME: .hash should link to .dynsym.
920 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
923 // Create the .interp section and PT_INTERP segment.
926 Layout::create_interp(const Target* target)
928 const char* interp = this->options_.dynamic_linker();
931 interp = target->dynamic_linker();
932 gold_assert(interp != NULL);
935 size_t len = strlen(interp) + 1;
937 Output_section_data* odata = new Output_data_const(interp, len, 1);
939 const char* interp_name = this->namepool_.add(".interp", NULL);
940 Output_section* osec = this->make_output_section(interp_name,
941 elfcpp::SHT_PROGBITS,
943 osec->add_output_section_data(odata);
945 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
946 this->segment_list_.push_back(oseg);
947 oseg->add_initial_output_section(osec, elfcpp::PF_R);
950 // Finish the .dynamic section and PT_DYNAMIC segment.
953 Layout::finish_dynamic_section(const Input_objects* input_objects,
954 const Symbol_table* symtab,
955 Output_data_dynamic* odyn)
957 this->dynamic_section_->add_output_section_data(odyn);
959 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
960 elfcpp::PF_R | elfcpp::PF_W);
961 this->segment_list_.push_back(oseg);
962 oseg->add_initial_output_section(this->dynamic_section_,
963 elfcpp::PF_R | elfcpp::PF_W);
965 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
966 p != input_objects->dynobj_end();
969 // FIXME: Handle --as-needed.
970 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
973 // FIXME: Support --init and --fini.
974 Symbol* sym = symtab->lookup("_init");
975 if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
976 odyn->add_symbol(elfcpp::DT_INIT, sym);
978 sym = symtab->lookup("_fini");
979 if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
980 odyn->add_symbol(elfcpp::DT_FINI, sym);
982 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
985 // The mapping of .gnu.linkonce section names to real section names.
987 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
988 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
990 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
991 MAPPING_INIT("t", ".text"),
992 MAPPING_INIT("r", ".rodata"),
993 MAPPING_INIT("d", ".data"),
994 MAPPING_INIT("b", ".bss"),
995 MAPPING_INIT("s", ".sdata"),
996 MAPPING_INIT("sb", ".sbss"),
997 MAPPING_INIT("s2", ".sdata2"),
998 MAPPING_INIT("sb2", ".sbss2"),
999 MAPPING_INIT("wi", ".debug_info"),
1000 MAPPING_INIT("td", ".tdata"),
1001 MAPPING_INIT("tb", ".tbss"),
1002 MAPPING_INIT("lr", ".lrodata"),
1003 MAPPING_INIT("l", ".ldata"),
1004 MAPPING_INIT("lb", ".lbss"),
1008 const int Layout::linkonce_mapping_count =
1009 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1011 // Return the name of the output section to use for a .gnu.linkonce
1012 // section. This is based on the default ELF linker script of the old
1013 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1014 // to ".text". Set *PLEN to the length of the name. *PLEN is
1015 // initialized to the length of NAME.
1018 Layout::linkonce_output_name(const char* name, size_t *plen)
1020 const char* s = name + sizeof(".gnu.linkonce") - 1;
1024 const Linkonce_mapping* plm = linkonce_mapping;
1025 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1027 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1036 // Choose the output section name to use given an input section name.
1037 // Set *PLEN to the length of the name. *PLEN is initialized to the
1041 Layout::output_section_name(const char* name, size_t* plen)
1043 if (Layout::is_linkonce(name))
1045 // .gnu.linkonce sections are laid out as though they were named
1046 // for the sections are placed into.
1047 return Layout::linkonce_output_name(name, plen);
1050 // If the section name has no '.', or only an initial '.', we use
1051 // the name unchanged (i.e., ".text" is unchanged).
1053 // Otherwise, if the section name does not include ".rel", we drop
1054 // the last '.' and everything that follows (i.e., ".text.XXX"
1055 // becomes ".text").
1057 // Otherwise, if the section name has zero or one '.' after the
1058 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1061 // Otherwise, we drop the last '.' and everything that follows
1062 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1064 const char* s = name;
1067 const char* sdot = strchr(s, '.');
1071 const char* srel = strstr(s, ".rel");
1074 *plen = sdot - name;
1078 sdot = strchr(srel + 1, '.');
1081 sdot = strchr(sdot + 1, '.');
1085 *plen = sdot - name;
1089 // Record the signature of a comdat section, and return whether to
1090 // include it in the link. If GROUP is true, this is a regular
1091 // section group. If GROUP is false, this is a group signature
1092 // derived from the name of a linkonce section. We want linkonce
1093 // signatures and group signatures to block each other, but we don't
1094 // want a linkonce signature to block another linkonce signature.
1097 Layout::add_comdat(const char* signature, bool group)
1099 std::string sig(signature);
1100 std::pair<Signatures::iterator, bool> ins(
1101 this->signatures_.insert(std::make_pair(sig, group)));
1105 // This is the first time we've seen this signature.
1109 if (ins.first->second)
1111 // We've already seen a real section group with this signature.
1116 // This is a real section group, and we've already seen a
1117 // linkonce section with tihs signature. Record that we've seen
1118 // a section group, and don't include this section group.
1119 ins.first->second = true;
1124 // We've already seen a linkonce section and this is a linkonce
1125 // section. These don't block each other--this may be the same
1126 // symbol name with different section types.
1131 // Write out data not associated with a section or the symbol table.
1134 Layout::write_data(const Symbol_table* symtab, const Target* target,
1135 Output_file* of) const
1137 const Output_section* symtab_section = this->symtab_section_;
1138 for (Section_list::const_iterator p = this->section_list_.begin();
1139 p != this->section_list_.end();
1142 if ((*p)->needs_symtab_index())
1144 gold_assert(symtab_section != NULL);
1145 unsigned int index = (*p)->symtab_index();
1146 gold_assert(index > 0 && index != -1U);
1147 off_t off = (symtab_section->offset()
1148 + index * symtab_section->entsize());
1149 symtab->write_section_symbol(target, *p, of, off);
1153 const Output_section* dynsym_section = this->dynsym_section_;
1154 for (Section_list::const_iterator p = this->section_list_.begin();
1155 p != this->section_list_.end();
1158 if ((*p)->needs_dynsym_index())
1160 gold_assert(dynsym_section != NULL);
1161 unsigned int index = (*p)->dynsym_index();
1162 gold_assert(index > 0 && index != -1U);
1163 off_t off = (dynsym_section->offset()
1164 + index * dynsym_section->entsize());
1165 symtab->write_section_symbol(target, *p, of, off);
1169 // Write out the Output_sections. Most won't have anything to
1170 // write, since most of the data will come from input sections which
1171 // are handled elsewhere. But some Output_sections do have
1173 for (Section_list::const_iterator p = this->section_list_.begin();
1174 p != this->section_list_.end();
1178 // Write out the Output_data which are not in an Output_section.
1179 for (Data_list::const_iterator p = this->special_output_list_.begin();
1180 p != this->special_output_list_.end();
1185 // Write_data_task methods.
1187 // We can always run this task.
1189 Task::Is_runnable_type
1190 Write_data_task::is_runnable(Workqueue*)
1195 // We need to unlock FINAL_BLOCKER when finished.
1198 Write_data_task::locks(Workqueue* workqueue)
1200 return new Task_locker_block(*this->final_blocker_, workqueue);
1203 // Run the task--write out the data.
1206 Write_data_task::run(Workqueue*)
1208 this->layout_->write_data(this->symtab_, this->target_, this->of_);
1211 // Write_symbols_task methods.
1213 // We can always run this task.
1215 Task::Is_runnable_type
1216 Write_symbols_task::is_runnable(Workqueue*)
1221 // We need to unlock FINAL_BLOCKER when finished.
1224 Write_symbols_task::locks(Workqueue* workqueue)
1226 return new Task_locker_block(*this->final_blocker_, workqueue);
1229 // Run the task--write out the symbols.
1232 Write_symbols_task::run(Workqueue*)
1234 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
1237 // Close_task_runner methods.
1239 // Run the task--close the file.
1242 Close_task_runner::run(Workqueue*)
1247 // Instantiate the templates we need. We could use the configure
1248 // script to restrict this to only the ones for implemented targets.
1252 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1253 const elfcpp::Shdr<32, false>& shdr, off_t*);
1257 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1258 const elfcpp::Shdr<32, true>& shdr, off_t*);
1262 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1263 const elfcpp::Shdr<64, false>& shdr, off_t*);
1267 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1268 const elfcpp::Shdr<64, true>& shdr, off_t*);
1271 } // End namespace gold.