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),
46 dynamic_section_(NULL), dynamic_data_(NULL)
48 // Make space for more than enough segments for a typical file.
49 // This is just for efficiency--it's OK if we wind up needing more.
50 this->segment_list_.reserve(12);
52 // We expect three unattached Output_data objects: the file header,
53 // the segment headers, and the section headers.
54 this->special_output_list_.reserve(3);
57 // Hash a key we use to look up an output section mapping.
60 Layout::Hash_key::operator()(const Layout::Key& k) const
62 return k.first + k.second.first + k.second.second;
65 // Whether to include this section in the link.
67 template<int size, bool big_endian>
69 Layout::include_section(Object*, const char*,
70 const elfcpp::Shdr<size, big_endian>& shdr)
72 // Some section types are never linked. Some are only linked when
73 // doing a relocateable link.
74 switch (shdr.get_sh_type())
76 case elfcpp::SHT_NULL:
77 case elfcpp::SHT_SYMTAB:
78 case elfcpp::SHT_DYNSYM:
79 case elfcpp::SHT_STRTAB:
80 case elfcpp::SHT_HASH:
81 case elfcpp::SHT_DYNAMIC:
82 case elfcpp::SHT_SYMTAB_SHNDX:
85 case elfcpp::SHT_RELA:
87 case elfcpp::SHT_GROUP:
88 return this->options_.is_relocatable();
91 // FIXME: Handle stripping debug sections here.
96 // Return an output section named NAME, or NULL if there is none.
99 Layout::find_output_section(const char* name) const
101 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
102 p != this->section_name_map_.end();
104 if (strcmp(p->second->name(), name) == 0)
109 // Return an output segment of type TYPE, with segment flags SET set
110 // and segment flags CLEAR clear. Return NULL if there is none.
113 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
114 elfcpp::Elf_Word clear) const
116 for (Segment_list::const_iterator p = this->segment_list_.begin();
117 p != this->segment_list_.end();
119 if (static_cast<elfcpp::PT>((*p)->type()) == type
120 && ((*p)->flags() & set) == set
121 && ((*p)->flags() & clear) == 0)
126 // Return the output section to use for section NAME with type TYPE
127 // and section flags FLAGS.
130 Layout::get_output_section(const char* name, Stringpool::Key name_key,
131 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
133 // We should ignore some flags.
134 flags &= ~ (elfcpp::SHF_INFO_LINK
135 | elfcpp::SHF_LINK_ORDER
136 | elfcpp::SHF_GROUP);
138 const Key key(name_key, std::make_pair(type, flags));
139 const std::pair<Key, Output_section*> v(key, NULL);
140 std::pair<Section_name_map::iterator, bool> ins(
141 this->section_name_map_.insert(v));
144 return ins.first->second;
147 // This is the first time we've seen this name/type/flags
149 Output_section* os = this->make_output_section(name, type, flags);
150 ins.first->second = os;
155 // Return the output section to use for input section SHNDX, with name
156 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
157 // offset of this input section without the output section.
159 template<int size, bool big_endian>
161 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
162 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
164 if (!this->include_section(object, name, shdr))
167 // If we are not doing a relocateable link, choose the name to use
168 // for the output section.
169 size_t len = strlen(name);
170 if (!this->options_.is_relocatable())
171 name = Layout::output_section_name(name, &len);
173 // FIXME: Handle SHF_OS_NONCONFORMING here.
175 // Canonicalize the section name.
176 Stringpool::Key name_key;
177 name = this->namepool_.add(name, len, &name_key);
179 // Find the output section. The output section is selected based on
180 // the section name, type, and flags.
181 Output_section* os = this->get_output_section(name, name_key,
183 shdr.get_sh_flags());
185 // FIXME: Handle SHF_LINK_ORDER somewhere.
187 *off = os->add_input_section(object, shndx, name, shdr);
192 // Add POSD to an output section using NAME, TYPE, and FLAGS.
195 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
196 elfcpp::Elf_Xword flags,
197 Output_section_data* posd)
199 // Canonicalize the name.
200 Stringpool::Key name_key;
201 name = this->namepool_.add(name, &name_key);
203 Output_section* os = this->get_output_section(name, name_key, type, flags);
204 os->add_output_section_data(posd);
207 // Map section flags to segment flags.
210 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
212 elfcpp::Elf_Word ret = elfcpp::PF_R;
213 if ((flags & elfcpp::SHF_WRITE) != 0)
215 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
220 // Make a new Output_section, and attach it to segments as
224 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
225 elfcpp::Elf_Xword flags)
227 Output_section* os = new Output_section(name, type, flags, true);
228 this->section_list_.push_back(os);
230 if ((flags & elfcpp::SHF_ALLOC) == 0)
231 this->unattached_section_list_.push_back(os);
234 // This output section goes into a PT_LOAD segment.
236 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
238 // The only thing we really care about for PT_LOAD segments is
239 // whether or not they are writable, so that is how we search
240 // for them. People who need segments sorted on some other
241 // basis will have to wait until we implement a mechanism for
242 // them to describe the segments they want.
244 Segment_list::const_iterator p;
245 for (p = this->segment_list_.begin();
246 p != this->segment_list_.end();
249 if ((*p)->type() == elfcpp::PT_LOAD
250 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
252 (*p)->add_output_section(os, seg_flags);
257 if (p == this->segment_list_.end())
259 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
261 this->segment_list_.push_back(oseg);
262 oseg->add_output_section(os, seg_flags);
265 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
267 if (type == elfcpp::SHT_NOTE)
269 // See if we already have an equivalent PT_NOTE segment.
270 for (p = this->segment_list_.begin();
271 p != segment_list_.end();
274 if ((*p)->type() == elfcpp::PT_NOTE
275 && (((*p)->flags() & elfcpp::PF_W)
276 == (seg_flags & elfcpp::PF_W)))
278 (*p)->add_output_section(os, seg_flags);
283 if (p == this->segment_list_.end())
285 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
287 this->segment_list_.push_back(oseg);
288 oseg->add_output_section(os, seg_flags);
292 // If we see a loadable SHF_TLS section, we create a PT_TLS
293 // segment. There can only be one such segment.
294 if ((flags & elfcpp::SHF_TLS) != 0)
296 if (this->tls_segment_ == NULL)
298 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
300 this->segment_list_.push_back(this->tls_segment_);
302 this->tls_segment_->add_output_section(os, seg_flags);
309 // Create the dynamic sections which are needed before we read the
313 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
314 Symbol_table* symtab)
316 if (!input_objects->any_dynamic())
319 const char* dynamic_name = this->namepool_.add(".dynamic", NULL);
320 this->dynamic_section_ = this->make_output_section(dynamic_name,
323 | elfcpp::SHF_WRITE));
325 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC",
326 this->dynamic_section_, 0, 0,
327 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
328 elfcpp::STV_HIDDEN, 0, false, false);
330 this->dynamic_data_ = new Output_data_dynamic(input_objects->target(),
333 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
336 // Find the first read-only PT_LOAD segment, creating one if
340 Layout::find_first_load_seg()
342 for (Segment_list::const_iterator p = this->segment_list_.begin();
343 p != this->segment_list_.end();
346 if ((*p)->type() == elfcpp::PT_LOAD
347 && ((*p)->flags() & elfcpp::PF_R) != 0
348 && ((*p)->flags() & elfcpp::PF_W) == 0)
352 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
353 this->segment_list_.push_back(load_seg);
357 // Finalize the layout. When this is called, we have created all the
358 // output sections and all the output segments which are based on
359 // input sections. We have several things to do, and we have to do
360 // them in the right order, so that we get the right results correctly
363 // 1) Finalize the list of output segments and create the segment
366 // 2) Finalize the dynamic symbol table and associated sections.
368 // 3) Determine the final file offset of all the output segments.
370 // 4) Determine the final file offset of all the SHF_ALLOC output
373 // 5) Create the symbol table sections and the section name table
376 // 6) Finalize the symbol table: set symbol values to their final
377 // value and make a final determination of which symbols are going
378 // into the output symbol table.
380 // 7) Create the section table header.
382 // 8) Determine the final file offset of all the output sections which
383 // are not SHF_ALLOC, including the section table header.
385 // 9) Finalize the ELF file header.
387 // This function returns the size of the output file.
390 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
392 Target* const target = input_objects->target();
393 const int size = target->get_size();
395 target->finalize_sections(&this->options_, this);
397 Output_segment* phdr_seg = NULL;
398 if (input_objects->any_dynamic())
400 // There was a dynamic object in the link. We need to create
401 // some information for the dynamic linker.
403 // Create the PT_PHDR segment which will hold the program
405 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
406 this->segment_list_.push_back(phdr_seg);
408 // Create the dynamic symbol table, including the hash table,
409 // the dynamic relocations, and the version sections.
410 this->create_dynamic_symtab(target, symtab);
412 // Create the .interp section to hold the name of the
413 // interpreter, and put it in a PT_INTERP segment.
414 this->create_interp(target);
416 // Finish the .dynamic section to hold the dynamic data, and put
417 // it in a PT_DYNAMIC segment.
418 this->finish_dynamic_section(input_objects, symtab);
421 // FIXME: Handle PT_GNU_STACK.
423 Output_segment* load_seg = this->find_first_load_seg();
425 // Lay out the segment headers.
426 bool big_endian = target->is_big_endian();
427 Output_segment_headers* segment_headers;
428 segment_headers = new Output_segment_headers(size, big_endian,
429 this->segment_list_);
430 load_seg->add_initial_output_data(segment_headers);
431 this->special_output_list_.push_back(segment_headers);
432 if (phdr_seg != NULL)
433 phdr_seg->add_initial_output_data(segment_headers);
435 // Lay out the file header.
436 Output_file_header* file_header;
437 file_header = new Output_file_header(size,
443 load_seg->add_initial_output_data(file_header);
444 this->special_output_list_.push_back(file_header);
446 // We set the output section indexes in set_segment_offsets and
447 // set_section_offsets.
448 unsigned int shndx = 1;
450 // Set the file offsets of all the segments, and all the sections
452 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
454 // Create the symbol table sections.
455 // FIXME: We don't need to do this if we are stripping symbols.
456 this->create_symtab_sections(size, input_objects, symtab, &off);
458 // Create the .shstrtab section.
459 Output_section* shstrtab_section = this->create_shstrtab();
461 // Set the file offsets of all the sections not associated with
463 off = this->set_section_offsets(off, &shndx);
465 // Create the section table header.
466 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
468 file_header->set_section_info(oshdrs, shstrtab_section);
470 // Now we know exactly where everything goes in the output file.
471 Output_data::layout_complete();
476 // Return whether SEG1 should be before SEG2 in the output file. This
477 // is based entirely on the segment type and flags. When this is
478 // called the segment addresses has normally not yet been set.
481 Layout::segment_precedes(const Output_segment* seg1,
482 const Output_segment* seg2)
484 elfcpp::Elf_Word type1 = seg1->type();
485 elfcpp::Elf_Word type2 = seg2->type();
487 // The single PT_PHDR segment is required to precede any loadable
488 // segment. We simply make it always first.
489 if (type1 == elfcpp::PT_PHDR)
491 gold_assert(type2 != elfcpp::PT_PHDR);
494 if (type2 == elfcpp::PT_PHDR)
497 // The single PT_INTERP segment is required to precede any loadable
498 // segment. We simply make it always second.
499 if (type1 == elfcpp::PT_INTERP)
501 gold_assert(type2 != elfcpp::PT_INTERP);
504 if (type2 == elfcpp::PT_INTERP)
507 // We then put PT_LOAD segments before any other segments.
508 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
510 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
513 // We put the PT_TLS segment last, because that is where the dynamic
514 // linker expects to find it (this is just for efficiency; other
515 // positions would also work correctly).
516 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
518 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
521 const elfcpp::Elf_Word flags1 = seg1->flags();
522 const elfcpp::Elf_Word flags2 = seg2->flags();
524 // The order of non-PT_LOAD segments is unimportant. We simply sort
525 // by the numeric segment type and flags values. There should not
526 // be more than one segment with the same type and flags.
527 if (type1 != elfcpp::PT_LOAD)
530 return type1 < type2;
531 gold_assert(flags1 != flags2);
532 return flags1 < flags2;
535 // We sort PT_LOAD segments based on the flags. Readonly segments
536 // come before writable segments. Then executable segments come
537 // before non-executable segments. Then the unlikely case of a
538 // non-readable segment comes before the normal case of a readable
539 // segment. If there are multiple segments with the same type and
540 // flags, we require that the address be set, and we sort by
541 // virtual address and then physical address.
542 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
543 return (flags1 & elfcpp::PF_W) == 0;
544 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
545 return (flags1 & elfcpp::PF_X) != 0;
546 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
547 return (flags1 & elfcpp::PF_R) == 0;
549 uint64_t vaddr1 = seg1->vaddr();
550 uint64_t vaddr2 = seg2->vaddr();
551 if (vaddr1 != vaddr2)
552 return vaddr1 < vaddr2;
554 uint64_t paddr1 = seg1->paddr();
555 uint64_t paddr2 = seg2->paddr();
556 gold_assert(paddr1 != paddr2);
557 return paddr1 < paddr2;
560 // Set the file offsets of all the segments, and all the sections they
561 // contain. They have all been created. LOAD_SEG must be be laid out
562 // first. Return the offset of the data to follow.
565 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
566 unsigned int *pshndx)
568 // Sort them into the final order.
569 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
570 Layout::Compare_segments());
572 // Find the PT_LOAD segments, and set their addresses and offsets
573 // and their section's addresses and offsets.
574 uint64_t addr = target->text_segment_address();
576 bool was_readonly = false;
577 for (Segment_list::iterator p = this->segment_list_.begin();
578 p != this->segment_list_.end();
581 if ((*p)->type() == elfcpp::PT_LOAD)
583 if (load_seg != NULL && load_seg != *p)
587 // If the last segment was readonly, and this one is not,
588 // then skip the address forward one page, maintaining the
589 // same position within the page. This lets us store both
590 // segments overlapping on a single page in the file, but
591 // the loader will put them on different pages in memory.
593 uint64_t orig_addr = addr;
594 uint64_t orig_off = off;
596 uint64_t aligned_addr = addr;
597 uint64_t abi_pagesize = target->abi_pagesize();
598 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
600 uint64_t align = (*p)->addralign();
602 addr = align_address(addr, align);
604 if ((addr & (abi_pagesize - 1)) != 0)
605 addr = addr + abi_pagesize;
608 unsigned int shndx_hold = *pshndx;
609 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
610 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
612 // Now that we know the size of this segment, we may be able
613 // to save a page in memory, at the cost of wasting some
614 // file space, by instead aligning to the start of a new
615 // page. Here we use the real machine page size rather than
616 // the ABI mandated page size.
618 if (aligned_addr != addr)
620 uint64_t common_pagesize = target->common_pagesize();
621 uint64_t first_off = (common_pagesize
623 & (common_pagesize - 1)));
624 uint64_t last_off = new_addr & (common_pagesize - 1);
627 && ((aligned_addr & ~ (common_pagesize - 1))
628 != (new_addr & ~ (common_pagesize - 1)))
629 && first_off + last_off <= common_pagesize)
631 *pshndx = shndx_hold;
632 addr = align_address(aligned_addr, common_pagesize);
633 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
634 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
640 if (((*p)->flags() & elfcpp::PF_W) == 0)
645 // Handle the non-PT_LOAD segments, setting their offsets from their
646 // section's offsets.
647 for (Segment_list::iterator p = this->segment_list_.begin();
648 p != this->segment_list_.end();
651 if ((*p)->type() != elfcpp::PT_LOAD)
658 // Set the file offset of all the sections not associated with a
662 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
664 for (Section_list::iterator p = this->unattached_section_list_.begin();
665 p != this->unattached_section_list_.end();
668 (*p)->set_out_shndx(*pshndx);
670 if ((*p)->offset() != -1)
672 off = align_address(off, (*p)->addralign());
673 (*p)->set_address(0, off);
674 off += (*p)->data_size();
679 // Create the symbol table sections.
682 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
683 Symbol_table* symtab,
690 symsize = elfcpp::Elf_sizes<32>::sym_size;
695 symsize = elfcpp::Elf_sizes<64>::sym_size;
702 off = align_address(off, align);
703 off_t startoff = off;
705 // Save space for the dummy symbol at the start of the section. We
706 // never bother to write this out--it will just be left as zero.
708 unsigned int local_symbol_index = 1;
710 // Add STT_SECTION symbols for each Output section which needs one.
711 for (Section_list::iterator p = this->section_list_.begin();
712 p != this->section_list_.end();
715 if (!(*p)->needs_symtab_index())
716 (*p)->set_symtab_index(-1U);
719 (*p)->set_symtab_index(local_symbol_index);
720 ++local_symbol_index;
725 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
726 p != input_objects->relobj_end();
729 Task_lock_obj<Object> tlo(**p);
730 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
733 off += (index - local_symbol_index) * symsize;
734 local_symbol_index = index;
737 unsigned int local_symcount = local_symbol_index;
738 gold_assert(local_symcount * symsize == off - startoff);
741 size_t dyn_global_index;
743 if (this->dynsym_section_ == NULL)
746 dyn_global_index = 0;
751 dyn_global_index = this->dynsym_section_->info();
752 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
753 dynoff = this->dynsym_section_->offset() + locsize;
754 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
755 gold_assert(dyncount * symsize
756 == this->dynsym_section_->data_size() - locsize);
759 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
760 dyncount, &this->sympool_);
762 this->sympool_.set_string_offsets();
764 const char* symtab_name = this->namepool_.add(".symtab", NULL);
765 Output_section* osymtab = this->make_output_section(symtab_name,
768 this->symtab_section_ = osymtab;
770 Output_section_data* pos = new Output_data_space(off - startoff,
772 osymtab->add_output_section_data(pos);
774 const char* strtab_name = this->namepool_.add(".strtab", NULL);
775 Output_section* ostrtab = this->make_output_section(strtab_name,
779 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
780 ostrtab->add_output_section_data(pstr);
782 osymtab->set_address(0, startoff);
783 osymtab->set_link_section(ostrtab);
784 osymtab->set_info(local_symcount);
785 osymtab->set_entsize(symsize);
790 // Create the .shstrtab section, which holds the names of the
791 // sections. At the time this is called, we have created all the
792 // output sections except .shstrtab itself.
795 Layout::create_shstrtab()
797 // FIXME: We don't need to create a .shstrtab section if we are
798 // stripping everything.
800 const char* name = this->namepool_.add(".shstrtab", NULL);
802 this->namepool_.set_string_offsets();
804 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
806 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
807 os->add_output_section_data(posd);
812 // Create the section headers. SIZE is 32 or 64. OFF is the file
815 Output_section_headers*
816 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
818 Output_section_headers* oshdrs;
819 oshdrs = new Output_section_headers(size, big_endian, this,
820 &this->segment_list_,
821 &this->unattached_section_list_,
823 off_t off = align_address(*poff, oshdrs->addralign());
824 oshdrs->set_address(0, off);
825 off += oshdrs->data_size();
827 this->special_output_list_.push_back(oshdrs);
831 // Create the dynamic symbol table.
834 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab)
836 // Count all the symbols in the dynamic symbol table, and set the
837 // dynamic symbol indexes.
839 // Skip symbol 0, which is always all zeroes.
840 unsigned int index = 1;
842 // Add STT_SECTION symbols for each Output section which needs one.
843 for (Section_list::iterator p = this->section_list_.begin();
844 p != this->section_list_.end();
847 if (!(*p)->needs_dynsym_index())
848 (*p)->set_dynsym_index(-1U);
851 (*p)->set_dynsym_index(index);
856 // FIXME: Some targets apparently require local symbols in the
857 // dynamic symbol table. Here is where we will have to count them,
858 // and set the dynamic symbol indexes, and add the names to
861 unsigned int local_symcount = index;
863 std::vector<Symbol*> dynamic_symbols;
865 // FIXME: We have to tell set_dynsym_indexes whether the
866 // -E/--export-dynamic option was used.
867 index = symtab->set_dynsym_indexes(index, &dynamic_symbols,
872 const int size = target->get_size();
875 symsize = elfcpp::Elf_sizes<32>::sym_size;
880 symsize = elfcpp::Elf_sizes<64>::sym_size;
886 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
887 Output_section* dynsym = this->make_output_section(dynsym_name,
891 Output_section_data* odata = new Output_data_space(index * symsize,
893 dynsym->add_output_section_data(odata);
895 dynsym->set_info(local_symcount);
896 dynsym->set_entsize(symsize);
897 dynsym->set_addralign(align);
899 this->dynsym_section_ = dynsym;
901 Output_data_dynamic* const odyn = this->dynamic_data_;
902 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
903 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
905 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
906 Output_section* dynstr = this->make_output_section(dynstr_name,
910 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
911 dynstr->add_output_section_data(strdata);
913 dynsym->set_link_section(dynstr);
914 this->dynamic_section_->set_link_section(dynstr);
916 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
917 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
919 // FIXME: We need an option to create a GNU hash table.
921 unsigned char* phash;
922 unsigned int hashlen;
923 Dynobj::create_elf_hash_table(target, dynamic_symbols, local_symcount,
926 const char* hash_name = this->namepool_.add(".hash", NULL);
927 Output_section* hashsec = this->make_output_section(hash_name,
931 Output_section_data* hashdata = new Output_data_const_buffer(phash,
934 hashsec->add_output_section_data(hashdata);
936 hashsec->set_link_section(dynsym);
937 hashsec->set_entsize(4);
939 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
942 // Create the .interp section and PT_INTERP segment.
945 Layout::create_interp(const Target* target)
947 const char* interp = this->options_.dynamic_linker();
950 interp = target->dynamic_linker();
951 gold_assert(interp != NULL);
954 size_t len = strlen(interp) + 1;
956 Output_section_data* odata = new Output_data_const(interp, len, 1);
958 const char* interp_name = this->namepool_.add(".interp", NULL);
959 Output_section* osec = this->make_output_section(interp_name,
960 elfcpp::SHT_PROGBITS,
962 osec->add_output_section_data(odata);
964 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
965 this->segment_list_.push_back(oseg);
966 oseg->add_initial_output_section(osec, elfcpp::PF_R);
969 // Finish the .dynamic section and PT_DYNAMIC segment.
972 Layout::finish_dynamic_section(const Input_objects* input_objects,
973 const Symbol_table* symtab)
975 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
976 elfcpp::PF_R | elfcpp::PF_W);
977 this->segment_list_.push_back(oseg);
978 oseg->add_initial_output_section(this->dynamic_section_,
979 elfcpp::PF_R | elfcpp::PF_W);
981 Output_data_dynamic* const odyn = this->dynamic_data_;
983 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
984 p != input_objects->dynobj_end();
987 // FIXME: Handle --as-needed.
988 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
991 // FIXME: Support --init and --fini.
992 Symbol* sym = symtab->lookup("_init");
993 if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
994 odyn->add_symbol(elfcpp::DT_INIT, sym);
996 sym = symtab->lookup("_fini");
997 if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
998 odyn->add_symbol(elfcpp::DT_FINI, sym);
1000 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1003 // The mapping of .gnu.linkonce section names to real section names.
1005 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1006 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1008 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1009 MAPPING_INIT("t", ".text"),
1010 MAPPING_INIT("r", ".rodata"),
1011 MAPPING_INIT("d", ".data"),
1012 MAPPING_INIT("b", ".bss"),
1013 MAPPING_INIT("s", ".sdata"),
1014 MAPPING_INIT("sb", ".sbss"),
1015 MAPPING_INIT("s2", ".sdata2"),
1016 MAPPING_INIT("sb2", ".sbss2"),
1017 MAPPING_INIT("wi", ".debug_info"),
1018 MAPPING_INIT("td", ".tdata"),
1019 MAPPING_INIT("tb", ".tbss"),
1020 MAPPING_INIT("lr", ".lrodata"),
1021 MAPPING_INIT("l", ".ldata"),
1022 MAPPING_INIT("lb", ".lbss"),
1026 const int Layout::linkonce_mapping_count =
1027 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1029 // Return the name of the output section to use for a .gnu.linkonce
1030 // section. This is based on the default ELF linker script of the old
1031 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1032 // to ".text". Set *PLEN to the length of the name. *PLEN is
1033 // initialized to the length of NAME.
1036 Layout::linkonce_output_name(const char* name, size_t *plen)
1038 const char* s = name + sizeof(".gnu.linkonce") - 1;
1042 const Linkonce_mapping* plm = linkonce_mapping;
1043 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1045 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1054 // Choose the output section name to use given an input section name.
1055 // Set *PLEN to the length of the name. *PLEN is initialized to the
1059 Layout::output_section_name(const char* name, size_t* plen)
1061 if (Layout::is_linkonce(name))
1063 // .gnu.linkonce sections are laid out as though they were named
1064 // for the sections are placed into.
1065 return Layout::linkonce_output_name(name, plen);
1068 // If the section name has no '.', or only an initial '.', we use
1069 // the name unchanged (i.e., ".text" is unchanged).
1071 // Otherwise, if the section name does not include ".rel", we drop
1072 // the last '.' and everything that follows (i.e., ".text.XXX"
1073 // becomes ".text").
1075 // Otherwise, if the section name has zero or one '.' after the
1076 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1079 // Otherwise, we drop the last '.' and everything that follows
1080 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1082 const char* s = name;
1085 const char* sdot = strchr(s, '.');
1089 const char* srel = strstr(s, ".rel");
1092 *plen = sdot - name;
1096 sdot = strchr(srel + 1, '.');
1099 sdot = strchr(sdot + 1, '.');
1103 *plen = sdot - name;
1107 // Record the signature of a comdat section, and return whether to
1108 // include it in the link. If GROUP is true, this is a regular
1109 // section group. If GROUP is false, this is a group signature
1110 // derived from the name of a linkonce section. We want linkonce
1111 // signatures and group signatures to block each other, but we don't
1112 // want a linkonce signature to block another linkonce signature.
1115 Layout::add_comdat(const char* signature, bool group)
1117 std::string sig(signature);
1118 std::pair<Signatures::iterator, bool> ins(
1119 this->signatures_.insert(std::make_pair(sig, group)));
1123 // This is the first time we've seen this signature.
1127 if (ins.first->second)
1129 // We've already seen a real section group with this signature.
1134 // This is a real section group, and we've already seen a
1135 // linkonce section with tihs signature. Record that we've seen
1136 // a section group, and don't include this section group.
1137 ins.first->second = true;
1142 // We've already seen a linkonce section and this is a linkonce
1143 // section. These don't block each other--this may be the same
1144 // symbol name with different section types.
1149 // Write out data not associated with a section or the symbol table.
1152 Layout::write_data(const Symbol_table* symtab, const Target* target,
1153 Output_file* of) const
1155 const Output_section* symtab_section = this->symtab_section_;
1156 for (Section_list::const_iterator p = this->section_list_.begin();
1157 p != this->section_list_.end();
1160 if ((*p)->needs_symtab_index())
1162 gold_assert(symtab_section != NULL);
1163 unsigned int index = (*p)->symtab_index();
1164 gold_assert(index > 0 && index != -1U);
1165 off_t off = (symtab_section->offset()
1166 + index * symtab_section->entsize());
1167 symtab->write_section_symbol(target, *p, of, off);
1171 const Output_section* dynsym_section = this->dynsym_section_;
1172 for (Section_list::const_iterator p = this->section_list_.begin();
1173 p != this->section_list_.end();
1176 if ((*p)->needs_dynsym_index())
1178 gold_assert(dynsym_section != NULL);
1179 unsigned int index = (*p)->dynsym_index();
1180 gold_assert(index > 0 && index != -1U);
1181 off_t off = (dynsym_section->offset()
1182 + index * dynsym_section->entsize());
1183 symtab->write_section_symbol(target, *p, of, off);
1187 // Write out the Output_sections. Most won't have anything to
1188 // write, since most of the data will come from input sections which
1189 // are handled elsewhere. But some Output_sections do have
1191 for (Section_list::const_iterator p = this->section_list_.begin();
1192 p != this->section_list_.end();
1196 // Write out the Output_data which are not in an Output_section.
1197 for (Data_list::const_iterator p = this->special_output_list_.begin();
1198 p != this->special_output_list_.end();
1203 // Write_data_task methods.
1205 // We can always run this task.
1207 Task::Is_runnable_type
1208 Write_data_task::is_runnable(Workqueue*)
1213 // We need to unlock FINAL_BLOCKER when finished.
1216 Write_data_task::locks(Workqueue* workqueue)
1218 return new Task_locker_block(*this->final_blocker_, workqueue);
1221 // Run the task--write out the data.
1224 Write_data_task::run(Workqueue*)
1226 this->layout_->write_data(this->symtab_, this->target_, this->of_);
1229 // Write_symbols_task methods.
1231 // We can always run this task.
1233 Task::Is_runnable_type
1234 Write_symbols_task::is_runnable(Workqueue*)
1239 // We need to unlock FINAL_BLOCKER when finished.
1242 Write_symbols_task::locks(Workqueue* workqueue)
1244 return new Task_locker_block(*this->final_blocker_, workqueue);
1247 // Run the task--write out the symbols.
1250 Write_symbols_task::run(Workqueue*)
1252 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1256 // Close_task_runner methods.
1258 // Run the task--close the file.
1261 Close_task_runner::run(Workqueue*)
1266 // Instantiate the templates we need. We could use the configure
1267 // script to restrict this to only the ones for implemented targets.
1271 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1272 const elfcpp::Shdr<32, false>& shdr, off_t*);
1276 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1277 const elfcpp::Shdr<32, true>& shdr, off_t*);
1281 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1282 const elfcpp::Shdr<64, false>& shdr, off_t*);
1286 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1287 const elfcpp::Shdr<64, true>& shdr, off_t*);
1290 } // End namespace gold.