1 // layout.cc -- lay out output file sections for gold
10 #include "parameters.h"
19 // Layout_task_runner methods.
21 // Lay out the sections. This is called after all the input objects
25 Layout_task_runner::run(Workqueue* workqueue)
27 off_t file_size = this->layout_->finalize(this->input_objects_,
30 // Now we know the final size of the output file and we know where
31 // each piece of information goes.
32 Output_file* of = new Output_file(this->options_,
33 this->input_objects_->target());
36 // Queue up the final set of tasks.
37 gold::queue_final_tasks(this->options_, this->input_objects_,
38 this->symtab_, this->layout_, workqueue, of);
43 Layout::Layout(const General_options& options)
44 : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
45 section_name_map_(), segment_list_(), section_list_(),
46 unattached_section_list_(), special_output_list_(),
47 tls_segment_(NULL), symtab_section_(NULL),
48 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL)
50 // Make space for more than enough segments for a typical file.
51 // This is just for efficiency--it's OK if we wind up needing more.
52 this->segment_list_.reserve(12);
54 // We expect three unattached Output_data objects: the file header,
55 // the segment headers, and the section headers.
56 this->special_output_list_.reserve(3);
59 // Hash a key we use to look up an output section mapping.
62 Layout::Hash_key::operator()(const Layout::Key& k) const
64 return k.first + k.second.first + k.second.second;
67 // Whether to include this section in the link.
69 template<int size, bool big_endian>
71 Layout::include_section(Object*, const char*,
72 const elfcpp::Shdr<size, big_endian>& shdr)
74 // Some section types are never linked. Some are only linked when
75 // doing a relocateable link.
76 switch (shdr.get_sh_type())
78 case elfcpp::SHT_NULL:
79 case elfcpp::SHT_SYMTAB:
80 case elfcpp::SHT_DYNSYM:
81 case elfcpp::SHT_STRTAB:
82 case elfcpp::SHT_HASH:
83 case elfcpp::SHT_DYNAMIC:
84 case elfcpp::SHT_SYMTAB_SHNDX:
87 case elfcpp::SHT_RELA:
89 case elfcpp::SHT_GROUP:
90 return parameters->output_is_object();
93 // FIXME: Handle stripping debug sections here.
98 // Return an output section named NAME, or NULL if there is none.
101 Layout::find_output_section(const char* name) const
103 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
104 p != this->section_name_map_.end();
106 if (strcmp(p->second->name(), name) == 0)
111 // Return an output segment of type TYPE, with segment flags SET set
112 // and segment flags CLEAR clear. Return NULL if there is none.
115 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
116 elfcpp::Elf_Word clear) const
118 for (Segment_list::const_iterator p = this->segment_list_.begin();
119 p != this->segment_list_.end();
121 if (static_cast<elfcpp::PT>((*p)->type()) == type
122 && ((*p)->flags() & set) == set
123 && ((*p)->flags() & clear) == 0)
128 // Return the output section to use for section NAME with type TYPE
129 // and section flags FLAGS.
132 Layout::get_output_section(const char* name, Stringpool::Key name_key,
133 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
135 // We should ignore some flags.
136 flags &= ~ (elfcpp::SHF_INFO_LINK
137 | elfcpp::SHF_LINK_ORDER
140 | elfcpp::SHF_STRINGS);
142 const Key key(name_key, std::make_pair(type, flags));
143 const std::pair<Key, Output_section*> v(key, NULL);
144 std::pair<Section_name_map::iterator, bool> ins(
145 this->section_name_map_.insert(v));
148 return ins.first->second;
151 // This is the first time we've seen this name/type/flags
153 Output_section* os = this->make_output_section(name, type, flags);
154 ins.first->second = os;
159 // Return the output section to use for input section SHNDX, with name
160 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
161 // offset of this input section without the output section.
163 template<int size, bool big_endian>
165 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
166 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
168 if (!this->include_section(object, name, shdr))
171 // If we are not doing a relocateable link, choose the name to use
172 // for the output section.
173 size_t len = strlen(name);
174 if (!parameters->output_is_object())
175 name = Layout::output_section_name(name, &len);
177 // FIXME: Handle SHF_OS_NONCONFORMING here.
179 // Canonicalize the section name.
180 Stringpool::Key name_key;
181 name = this->namepool_.add(name, len, &name_key);
183 // Find the output section. The output section is selected based on
184 // the section name, type, and flags.
185 Output_section* os = this->get_output_section(name, name_key,
187 shdr.get_sh_flags());
189 // FIXME: Handle SHF_LINK_ORDER somewhere.
191 *off = os->add_input_section(object, shndx, name, shdr);
196 // Add POSD to an output section using NAME, TYPE, and FLAGS.
199 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
200 elfcpp::Elf_Xword flags,
201 Output_section_data* posd)
203 // Canonicalize the name.
204 Stringpool::Key name_key;
205 name = this->namepool_.add(name, &name_key);
207 Output_section* os = this->get_output_section(name, name_key, type, flags);
208 os->add_output_section_data(posd);
211 // Map section flags to segment flags.
214 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
216 elfcpp::Elf_Word ret = elfcpp::PF_R;
217 if ((flags & elfcpp::SHF_WRITE) != 0)
219 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
224 // Make a new Output_section, and attach it to segments as
228 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
229 elfcpp::Elf_Xword flags)
231 Output_section* os = new Output_section(name, type, flags);
232 this->section_list_.push_back(os);
234 if ((flags & elfcpp::SHF_ALLOC) == 0)
235 this->unattached_section_list_.push_back(os);
238 // This output section goes into a PT_LOAD segment.
240 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
242 // The only thing we really care about for PT_LOAD segments is
243 // whether or not they are writable, so that is how we search
244 // for them. People who need segments sorted on some other
245 // basis will have to wait until we implement a mechanism for
246 // them to describe the segments they want.
248 Segment_list::const_iterator p;
249 for (p = this->segment_list_.begin();
250 p != this->segment_list_.end();
253 if ((*p)->type() == elfcpp::PT_LOAD
254 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
256 (*p)->add_output_section(os, seg_flags);
261 if (p == this->segment_list_.end())
263 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
265 this->segment_list_.push_back(oseg);
266 oseg->add_output_section(os, seg_flags);
269 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
271 if (type == elfcpp::SHT_NOTE)
273 // See if we already have an equivalent PT_NOTE segment.
274 for (p = this->segment_list_.begin();
275 p != segment_list_.end();
278 if ((*p)->type() == elfcpp::PT_NOTE
279 && (((*p)->flags() & elfcpp::PF_W)
280 == (seg_flags & elfcpp::PF_W)))
282 (*p)->add_output_section(os, seg_flags);
287 if (p == this->segment_list_.end())
289 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
291 this->segment_list_.push_back(oseg);
292 oseg->add_output_section(os, seg_flags);
296 // If we see a loadable SHF_TLS section, we create a PT_TLS
297 // segment. There can only be one such segment.
298 if ((flags & elfcpp::SHF_TLS) != 0)
300 if (this->tls_segment_ == NULL)
302 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
304 this->segment_list_.push_back(this->tls_segment_);
306 this->tls_segment_->add_output_section(os, seg_flags);
313 // Create the dynamic sections which are needed before we read the
317 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
318 Symbol_table* symtab)
320 if (!input_objects->any_dynamic())
323 const char* dynamic_name = this->namepool_.add(".dynamic", NULL);
324 this->dynamic_section_ = this->make_output_section(dynamic_name,
327 | elfcpp::SHF_WRITE));
329 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
330 this->dynamic_section_, 0, 0,
331 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
332 elfcpp::STV_HIDDEN, 0, false, false);
334 this->dynamic_data_ = new Output_data_dynamic(input_objects->target(),
337 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
340 // For each output section whose name can be represented as C symbol,
341 // define __start and __stop symbols for the section. This is a GNU
345 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
347 for (Section_list::const_iterator p = this->section_list_.begin();
348 p != this->section_list_.end();
351 const char* const name = (*p)->name();
352 if (name[strspn(name,
354 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
355 "abcdefghijklmnopqrstuvwxyz"
359 const std::string name_string(name);
360 const std::string start_name("__start_" + name_string);
361 const std::string stop_name("__stop_" + name_string);
363 symtab->define_in_output_data(target,
373 false, // offset_is_from_end
374 false); // only_if_ref
376 symtab->define_in_output_data(target,
386 true, // offset_is_from_end
387 false); // only_if_ref
392 // Find the first read-only PT_LOAD segment, creating one if
396 Layout::find_first_load_seg()
398 for (Segment_list::const_iterator p = this->segment_list_.begin();
399 p != this->segment_list_.end();
402 if ((*p)->type() == elfcpp::PT_LOAD
403 && ((*p)->flags() & elfcpp::PF_R) != 0
404 && ((*p)->flags() & elfcpp::PF_W) == 0)
408 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
409 this->segment_list_.push_back(load_seg);
413 // Finalize the layout. When this is called, we have created all the
414 // output sections and all the output segments which are based on
415 // input sections. We have several things to do, and we have to do
416 // them in the right order, so that we get the right results correctly
419 // 1) Finalize the list of output segments and create the segment
422 // 2) Finalize the dynamic symbol table and associated sections.
424 // 3) Determine the final file offset of all the output segments.
426 // 4) Determine the final file offset of all the SHF_ALLOC output
429 // 5) Create the symbol table sections and the section name table
432 // 6) Finalize the symbol table: set symbol values to their final
433 // value and make a final determination of which symbols are going
434 // into the output symbol table.
436 // 7) Create the section table header.
438 // 8) Determine the final file offset of all the output sections which
439 // are not SHF_ALLOC, including the section table header.
441 // 9) Finalize the ELF file header.
443 // This function returns the size of the output file.
446 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
448 Target* const target = input_objects->target();
449 const int size = target->get_size();
451 target->finalize_sections(this);
453 Output_segment* phdr_seg = NULL;
454 if (input_objects->any_dynamic())
456 // There was a dynamic object in the link. We need to create
457 // some information for the dynamic linker.
459 // Create the PT_PHDR segment which will hold the program
461 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
462 this->segment_list_.push_back(phdr_seg);
464 // Create the dynamic symbol table, including the hash table.
465 Output_section* dynstr;
466 std::vector<Symbol*> dynamic_symbols;
467 unsigned int local_dynamic_count;
469 this->create_dynamic_symtab(target, symtab, &dynstr,
470 &local_dynamic_count, &dynamic_symbols,
473 // Create the .interp section to hold the name of the
474 // interpreter, and put it in a PT_INTERP segment.
475 this->create_interp(target);
477 // Finish the .dynamic section to hold the dynamic data, and put
478 // it in a PT_DYNAMIC segment.
479 this->finish_dynamic_section(input_objects, symtab);
481 // We should have added everything we need to the dynamic string
483 this->dynpool_.set_string_offsets();
485 // Create the version sections. We can't do this until the
486 // dynamic string table is complete.
487 this->create_version_sections(target, &versions, local_dynamic_count,
488 dynamic_symbols, dynstr);
491 // FIXME: Handle PT_GNU_STACK.
493 Output_segment* load_seg = this->find_first_load_seg();
495 // Lay out the segment headers.
496 bool big_endian = target->is_big_endian();
497 Output_segment_headers* segment_headers;
498 segment_headers = new Output_segment_headers(size, big_endian,
499 this->segment_list_);
500 load_seg->add_initial_output_data(segment_headers);
501 this->special_output_list_.push_back(segment_headers);
502 if (phdr_seg != NULL)
503 phdr_seg->add_initial_output_data(segment_headers);
505 // Lay out the file header.
506 Output_file_header* file_header;
507 file_header = new Output_file_header(size,
512 load_seg->add_initial_output_data(file_header);
513 this->special_output_list_.push_back(file_header);
515 // We set the output section indexes in set_segment_offsets and
516 // set_section_offsets.
517 unsigned int shndx = 1;
519 // Set the file offsets of all the segments, and all the sections
521 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
523 // Create the symbol table sections.
524 this->create_symtab_sections(size, input_objects, symtab, &off);
526 // Create the .shstrtab section.
527 Output_section* shstrtab_section = this->create_shstrtab();
529 // Set the file offsets of all the sections not associated with
531 off = this->set_section_offsets(off, &shndx);
533 // Create the section table header.
534 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
536 file_header->set_section_info(oshdrs, shstrtab_section);
538 // Now we know exactly where everything goes in the output file.
539 Output_data::layout_complete();
544 // Return whether SEG1 should be before SEG2 in the output file. This
545 // is based entirely on the segment type and flags. When this is
546 // called the segment addresses has normally not yet been set.
549 Layout::segment_precedes(const Output_segment* seg1,
550 const Output_segment* seg2)
552 elfcpp::Elf_Word type1 = seg1->type();
553 elfcpp::Elf_Word type2 = seg2->type();
555 // The single PT_PHDR segment is required to precede any loadable
556 // segment. We simply make it always first.
557 if (type1 == elfcpp::PT_PHDR)
559 gold_assert(type2 != elfcpp::PT_PHDR);
562 if (type2 == elfcpp::PT_PHDR)
565 // The single PT_INTERP segment is required to precede any loadable
566 // segment. We simply make it always second.
567 if (type1 == elfcpp::PT_INTERP)
569 gold_assert(type2 != elfcpp::PT_INTERP);
572 if (type2 == elfcpp::PT_INTERP)
575 // We then put PT_LOAD segments before any other segments.
576 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
578 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
581 // We put the PT_TLS segment last, because that is where the dynamic
582 // linker expects to find it (this is just for efficiency; other
583 // positions would also work correctly).
584 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
586 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
589 const elfcpp::Elf_Word flags1 = seg1->flags();
590 const elfcpp::Elf_Word flags2 = seg2->flags();
592 // The order of non-PT_LOAD segments is unimportant. We simply sort
593 // by the numeric segment type and flags values. There should not
594 // be more than one segment with the same type and flags.
595 if (type1 != elfcpp::PT_LOAD)
598 return type1 < type2;
599 gold_assert(flags1 != flags2);
600 return flags1 < flags2;
603 // We sort PT_LOAD segments based on the flags. Readonly segments
604 // come before writable segments. Then executable segments come
605 // before non-executable segments. Then the unlikely case of a
606 // non-readable segment comes before the normal case of a readable
607 // segment. If there are multiple segments with the same type and
608 // flags, we require that the address be set, and we sort by
609 // virtual address and then physical address.
610 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
611 return (flags1 & elfcpp::PF_W) == 0;
612 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
613 return (flags1 & elfcpp::PF_X) != 0;
614 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
615 return (flags1 & elfcpp::PF_R) == 0;
617 uint64_t vaddr1 = seg1->vaddr();
618 uint64_t vaddr2 = seg2->vaddr();
619 if (vaddr1 != vaddr2)
620 return vaddr1 < vaddr2;
622 uint64_t paddr1 = seg1->paddr();
623 uint64_t paddr2 = seg2->paddr();
624 gold_assert(paddr1 != paddr2);
625 return paddr1 < paddr2;
628 // Set the file offsets of all the segments, and all the sections they
629 // contain. They have all been created. LOAD_SEG must be be laid out
630 // first. Return the offset of the data to follow.
633 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
634 unsigned int *pshndx)
636 // Sort them into the final order.
637 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
638 Layout::Compare_segments());
640 // Find the PT_LOAD segments, and set their addresses and offsets
641 // and their section's addresses and offsets.
642 uint64_t addr = target->text_segment_address();
644 bool was_readonly = false;
645 for (Segment_list::iterator p = this->segment_list_.begin();
646 p != this->segment_list_.end();
649 if ((*p)->type() == elfcpp::PT_LOAD)
651 if (load_seg != NULL && load_seg != *p)
655 // If the last segment was readonly, and this one is not,
656 // then skip the address forward one page, maintaining the
657 // same position within the page. This lets us store both
658 // segments overlapping on a single page in the file, but
659 // the loader will put them on different pages in memory.
661 uint64_t orig_addr = addr;
662 uint64_t orig_off = off;
664 uint64_t aligned_addr = addr;
665 uint64_t abi_pagesize = target->abi_pagesize();
667 // FIXME: This should depend on the -n and -N options.
668 (*p)->set_minimum_addralign(target->common_pagesize());
670 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
672 uint64_t align = (*p)->addralign();
674 addr = align_address(addr, align);
676 if ((addr & (abi_pagesize - 1)) != 0)
677 addr = addr + abi_pagesize;
680 unsigned int shndx_hold = *pshndx;
681 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
682 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
684 // Now that we know the size of this segment, we may be able
685 // to save a page in memory, at the cost of wasting some
686 // file space, by instead aligning to the start of a new
687 // page. Here we use the real machine page size rather than
688 // the ABI mandated page size.
690 if (aligned_addr != addr)
692 uint64_t common_pagesize = target->common_pagesize();
693 uint64_t first_off = (common_pagesize
695 & (common_pagesize - 1)));
696 uint64_t last_off = new_addr & (common_pagesize - 1);
699 && ((aligned_addr & ~ (common_pagesize - 1))
700 != (new_addr & ~ (common_pagesize - 1)))
701 && first_off + last_off <= common_pagesize)
703 *pshndx = shndx_hold;
704 addr = align_address(aligned_addr, common_pagesize);
705 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
706 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
712 if (((*p)->flags() & elfcpp::PF_W) == 0)
717 // Handle the non-PT_LOAD segments, setting their offsets from their
718 // section's offsets.
719 for (Segment_list::iterator p = this->segment_list_.begin();
720 p != this->segment_list_.end();
723 if ((*p)->type() != elfcpp::PT_LOAD)
730 // Set the file offset of all the sections not associated with a
734 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
736 for (Section_list::iterator p = this->unattached_section_list_.begin();
737 p != this->unattached_section_list_.end();
740 (*p)->set_out_shndx(*pshndx);
742 if ((*p)->offset() != -1)
744 off = align_address(off, (*p)->addralign());
745 (*p)->set_address(0, off);
746 off += (*p)->data_size();
751 // Create the symbol table sections. Here we also set the final
752 // values of the symbols. At this point all the loadable sections are
756 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
757 Symbol_table* symtab,
764 symsize = elfcpp::Elf_sizes<32>::sym_size;
769 symsize = elfcpp::Elf_sizes<64>::sym_size;
776 off = align_address(off, align);
777 off_t startoff = off;
779 // Save space for the dummy symbol at the start of the section. We
780 // never bother to write this out--it will just be left as zero.
782 unsigned int local_symbol_index = 1;
784 // Add STT_SECTION symbols for each Output section which needs one.
785 for (Section_list::iterator p = this->section_list_.begin();
786 p != this->section_list_.end();
789 if (!(*p)->needs_symtab_index())
790 (*p)->set_symtab_index(-1U);
793 (*p)->set_symtab_index(local_symbol_index);
794 ++local_symbol_index;
799 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
800 p != input_objects->relobj_end();
803 Task_lock_obj<Object> tlo(**p);
804 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
807 off += (index - local_symbol_index) * symsize;
808 local_symbol_index = index;
811 unsigned int local_symcount = local_symbol_index;
812 gold_assert(local_symcount * symsize == off - startoff);
815 size_t dyn_global_index;
817 if (this->dynsym_section_ == NULL)
820 dyn_global_index = 0;
825 dyn_global_index = this->dynsym_section_->info();
826 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
827 dynoff = this->dynsym_section_->offset() + locsize;
828 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
829 gold_assert(dyncount * symsize
830 == this->dynsym_section_->data_size() - locsize);
833 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
834 dyncount, &this->sympool_);
836 this->sympool_.set_string_offsets();
838 const char* symtab_name = this->namepool_.add(".symtab", NULL);
839 Output_section* osymtab = this->make_output_section(symtab_name,
842 this->symtab_section_ = osymtab;
844 Output_section_data* pos = new Output_data_space(off - startoff,
846 osymtab->add_output_section_data(pos);
848 const char* strtab_name = this->namepool_.add(".strtab", NULL);
849 Output_section* ostrtab = this->make_output_section(strtab_name,
853 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
854 ostrtab->add_output_section_data(pstr);
856 osymtab->set_address(0, startoff);
857 osymtab->set_link_section(ostrtab);
858 osymtab->set_info(local_symcount);
859 osymtab->set_entsize(symsize);
864 // Create the .shstrtab section, which holds the names of the
865 // sections. At the time this is called, we have created all the
866 // output sections except .shstrtab itself.
869 Layout::create_shstrtab()
871 // FIXME: We don't need to create a .shstrtab section if we are
872 // stripping everything.
874 const char* name = this->namepool_.add(".shstrtab", NULL);
876 this->namepool_.set_string_offsets();
878 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
880 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
881 os->add_output_section_data(posd);
886 // Create the section headers. SIZE is 32 or 64. OFF is the file
889 Output_section_headers*
890 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
892 Output_section_headers* oshdrs;
893 oshdrs = new Output_section_headers(size, big_endian, this,
894 &this->segment_list_,
895 &this->unattached_section_list_,
897 off_t off = align_address(*poff, oshdrs->addralign());
898 oshdrs->set_address(0, off);
899 off += oshdrs->data_size();
901 this->special_output_list_.push_back(oshdrs);
905 // Create the dynamic symbol table.
908 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
909 Output_section **pdynstr,
910 unsigned int* plocal_dynamic_count,
911 std::vector<Symbol*>* pdynamic_symbols,
914 // Count all the symbols in the dynamic symbol table, and set the
915 // dynamic symbol indexes.
917 // Skip symbol 0, which is always all zeroes.
918 unsigned int index = 1;
920 // Add STT_SECTION symbols for each Output section which needs one.
921 for (Section_list::iterator p = this->section_list_.begin();
922 p != this->section_list_.end();
925 if (!(*p)->needs_dynsym_index())
926 (*p)->set_dynsym_index(-1U);
929 (*p)->set_dynsym_index(index);
934 // FIXME: Some targets apparently require local symbols in the
935 // dynamic symbol table. Here is where we will have to count them,
936 // and set the dynamic symbol indexes, and add the names to
939 unsigned int local_symcount = index;
940 *plocal_dynamic_count = local_symcount;
942 // FIXME: We have to tell set_dynsym_indexes whether the
943 // -E/--export-dynamic option was used.
944 index = symtab->set_dynsym_indexes(&this->options_, target, index,
945 pdynamic_symbols, &this->dynpool_,
950 const int size = target->get_size();
953 symsize = elfcpp::Elf_sizes<32>::sym_size;
958 symsize = elfcpp::Elf_sizes<64>::sym_size;
964 // Create the dynamic symbol table section.
966 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
967 Output_section* dynsym = this->make_output_section(dynsym_name,
971 Output_section_data* odata = new Output_data_space(index * symsize,
973 dynsym->add_output_section_data(odata);
975 dynsym->set_info(local_symcount);
976 dynsym->set_entsize(symsize);
977 dynsym->set_addralign(align);
979 this->dynsym_section_ = dynsym;
981 Output_data_dynamic* const odyn = this->dynamic_data_;
982 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
983 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
985 // Create the dynamic string table section.
987 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
988 Output_section* dynstr = this->make_output_section(dynstr_name,
992 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
993 dynstr->add_output_section_data(strdata);
995 dynsym->set_link_section(dynstr);
996 this->dynamic_section_->set_link_section(dynstr);
998 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
999 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1003 // Create the hash tables.
1005 // FIXME: We need an option to create a GNU hash table.
1007 unsigned char* phash;
1008 unsigned int hashlen;
1009 Dynobj::create_elf_hash_table(target, *pdynamic_symbols, local_symcount,
1012 const char* hash_name = this->namepool_.add(".hash", NULL);
1013 Output_section* hashsec = this->make_output_section(hash_name,
1017 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1020 hashsec->add_output_section_data(hashdata);
1022 hashsec->set_link_section(dynsym);
1023 hashsec->set_entsize(4);
1025 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1028 // Create the version sections.
1031 Layout::create_version_sections(const Target* target, const Versions* versions,
1032 unsigned int local_symcount,
1033 const std::vector<Symbol*>& dynamic_symbols,
1034 const Output_section* dynstr)
1036 if (!versions->any_defs() && !versions->any_needs())
1039 if (target->get_size() == 32)
1041 if (target->is_big_endian())
1043 #ifdef HAVE_TARGET_32_BIG
1044 this->sized_create_version_sections
1045 SELECT_SIZE_ENDIAN_NAME(32, true)(
1046 versions, local_symcount, dynamic_symbols, dynstr
1047 SELECT_SIZE_ENDIAN(32, true));
1054 #ifdef HAVE_TARGET_32_LITTLE
1055 this->sized_create_version_sections
1056 SELECT_SIZE_ENDIAN_NAME(32, false)(
1057 versions, local_symcount, dynamic_symbols, dynstr
1058 SELECT_SIZE_ENDIAN(32, false));
1064 else if (target->get_size() == 64)
1066 if (target->is_big_endian())
1068 #ifdef HAVE_TARGET_64_BIG
1069 this->sized_create_version_sections
1070 SELECT_SIZE_ENDIAN_NAME(64, true)(
1071 versions, local_symcount, dynamic_symbols, dynstr
1072 SELECT_SIZE_ENDIAN(64, true));
1079 #ifdef HAVE_TARGET_64_LITTLE
1080 this->sized_create_version_sections
1081 SELECT_SIZE_ENDIAN_NAME(64, false)(
1082 versions, local_symcount, dynamic_symbols, dynstr
1083 SELECT_SIZE_ENDIAN(64, false));
1093 // Create the version sections, sized version.
1095 template<int size, bool big_endian>
1097 Layout::sized_create_version_sections(
1098 const Versions* versions,
1099 unsigned int local_symcount,
1100 const std::vector<Symbol*>& dynamic_symbols,
1101 const Output_section* dynstr
1104 const char* vname = this->namepool_.add(".gnu.version", NULL);
1105 Output_section* vsec = this->make_output_section(vname,
1106 elfcpp::SHT_GNU_versym,
1109 unsigned char* vbuf;
1111 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1112 &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1113 SELECT_SIZE_ENDIAN(size, big_endian));
1115 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1117 vsec->add_output_section_data(vdata);
1118 vsec->set_entsize(2);
1119 vsec->set_link_section(this->dynsym_section_);
1121 Output_data_dynamic* const odyn = this->dynamic_data_;
1122 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1124 if (versions->any_defs())
1126 const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
1127 Output_section *vdsec;
1128 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1131 unsigned char* vdbuf;
1132 unsigned int vdsize;
1133 unsigned int vdentries;
1134 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1135 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1136 SELECT_SIZE_ENDIAN(size, big_endian));
1138 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1142 vdsec->add_output_section_data(vddata);
1143 vdsec->set_link_section(dynstr);
1144 vdsec->set_info(vdentries);
1146 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1147 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1150 if (versions->any_needs())
1152 const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
1153 Output_section* vnsec;
1154 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1157 unsigned char* vnbuf;
1158 unsigned int vnsize;
1159 unsigned int vnentries;
1160 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1161 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1162 SELECT_SIZE_ENDIAN(size, big_endian));
1164 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1168 vnsec->add_output_section_data(vndata);
1169 vnsec->set_link_section(dynstr);
1170 vnsec->set_info(vnentries);
1172 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1173 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1177 // Create the .interp section and PT_INTERP segment.
1180 Layout::create_interp(const Target* target)
1182 const char* interp = this->options_.dynamic_linker();
1185 interp = target->dynamic_linker();
1186 gold_assert(interp != NULL);
1189 size_t len = strlen(interp) + 1;
1191 Output_section_data* odata = new Output_data_const(interp, len, 1);
1193 const char* interp_name = this->namepool_.add(".interp", NULL);
1194 Output_section* osec = this->make_output_section(interp_name,
1195 elfcpp::SHT_PROGBITS,
1197 osec->add_output_section_data(odata);
1199 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1200 this->segment_list_.push_back(oseg);
1201 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1204 // Finish the .dynamic section and PT_DYNAMIC segment.
1207 Layout::finish_dynamic_section(const Input_objects* input_objects,
1208 const Symbol_table* symtab)
1210 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1211 elfcpp::PF_R | elfcpp::PF_W);
1212 this->segment_list_.push_back(oseg);
1213 oseg->add_initial_output_section(this->dynamic_section_,
1214 elfcpp::PF_R | elfcpp::PF_W);
1216 Output_data_dynamic* const odyn = this->dynamic_data_;
1218 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1219 p != input_objects->dynobj_end();
1222 // FIXME: Handle --as-needed.
1223 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1226 // FIXME: Support --init and --fini.
1227 Symbol* sym = symtab->lookup("_init");
1228 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1229 odyn->add_symbol(elfcpp::DT_INIT, sym);
1231 sym = symtab->lookup("_fini");
1232 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1233 odyn->add_symbol(elfcpp::DT_FINI, sym);
1235 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1237 // Add a DT_RPATH entry if needed.
1238 const General_options::Dir_list& rpath(this->options_.rpath());
1241 std::string rpath_val;
1242 for (General_options::Dir_list::const_iterator p = rpath.begin();
1246 if (rpath_val.empty())
1250 // Eliminate duplicates.
1251 General_options::Dir_list::const_iterator q;
1252 for (q = rpath.begin(); q != p; ++q)
1253 if (strcmp(*q, *p) == 0)
1263 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1267 // The mapping of .gnu.linkonce section names to real section names.
1269 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1270 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1272 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1273 MAPPING_INIT("t", ".text"),
1274 MAPPING_INIT("r", ".rodata"),
1275 MAPPING_INIT("d", ".data"),
1276 MAPPING_INIT("b", ".bss"),
1277 MAPPING_INIT("s", ".sdata"),
1278 MAPPING_INIT("sb", ".sbss"),
1279 MAPPING_INIT("s2", ".sdata2"),
1280 MAPPING_INIT("sb2", ".sbss2"),
1281 MAPPING_INIT("wi", ".debug_info"),
1282 MAPPING_INIT("td", ".tdata"),
1283 MAPPING_INIT("tb", ".tbss"),
1284 MAPPING_INIT("lr", ".lrodata"),
1285 MAPPING_INIT("l", ".ldata"),
1286 MAPPING_INIT("lb", ".lbss"),
1290 const int Layout::linkonce_mapping_count =
1291 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1293 // Return the name of the output section to use for a .gnu.linkonce
1294 // section. This is based on the default ELF linker script of the old
1295 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1296 // to ".text". Set *PLEN to the length of the name. *PLEN is
1297 // initialized to the length of NAME.
1300 Layout::linkonce_output_name(const char* name, size_t *plen)
1302 const char* s = name + sizeof(".gnu.linkonce") - 1;
1306 const Linkonce_mapping* plm = linkonce_mapping;
1307 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1309 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1318 // Choose the output section name to use given an input section name.
1319 // Set *PLEN to the length of the name. *PLEN is initialized to the
1323 Layout::output_section_name(const char* name, size_t* plen)
1325 if (Layout::is_linkonce(name))
1327 // .gnu.linkonce sections are laid out as though they were named
1328 // for the sections are placed into.
1329 return Layout::linkonce_output_name(name, plen);
1332 // If the section name has no '.', or only an initial '.', we use
1333 // the name unchanged (i.e., ".text" is unchanged).
1335 // Otherwise, if the section name does not include ".rel", we drop
1336 // the last '.' and everything that follows (i.e., ".text.XXX"
1337 // becomes ".text").
1339 // Otherwise, if the section name has zero or one '.' after the
1340 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1343 // Otherwise, we drop the last '.' and everything that follows
1344 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1346 const char* s = name;
1349 const char* sdot = strchr(s, '.');
1353 const char* srel = strstr(s, ".rel");
1356 *plen = sdot - name;
1360 sdot = strchr(srel + 1, '.');
1363 sdot = strchr(sdot + 1, '.');
1367 *plen = sdot - name;
1371 // Record the signature of a comdat section, and return whether to
1372 // include it in the link. If GROUP is true, this is a regular
1373 // section group. If GROUP is false, this is a group signature
1374 // derived from the name of a linkonce section. We want linkonce
1375 // signatures and group signatures to block each other, but we don't
1376 // want a linkonce signature to block another linkonce signature.
1379 Layout::add_comdat(const char* signature, bool group)
1381 std::string sig(signature);
1382 std::pair<Signatures::iterator, bool> ins(
1383 this->signatures_.insert(std::make_pair(sig, group)));
1387 // This is the first time we've seen this signature.
1391 if (ins.first->second)
1393 // We've already seen a real section group with this signature.
1398 // This is a real section group, and we've already seen a
1399 // linkonce section with tihs signature. Record that we've seen
1400 // a section group, and don't include this section group.
1401 ins.first->second = true;
1406 // We've already seen a linkonce section and this is a linkonce
1407 // section. These don't block each other--this may be the same
1408 // symbol name with different section types.
1413 // Write out data not associated with a section or the symbol table.
1416 Layout::write_data(const Symbol_table* symtab, const Target* target,
1417 Output_file* of) const
1419 const Output_section* symtab_section = this->symtab_section_;
1420 for (Section_list::const_iterator p = this->section_list_.begin();
1421 p != this->section_list_.end();
1424 if ((*p)->needs_symtab_index())
1426 gold_assert(symtab_section != NULL);
1427 unsigned int index = (*p)->symtab_index();
1428 gold_assert(index > 0 && index != -1U);
1429 off_t off = (symtab_section->offset()
1430 + index * symtab_section->entsize());
1431 symtab->write_section_symbol(target, *p, of, off);
1435 const Output_section* dynsym_section = this->dynsym_section_;
1436 for (Section_list::const_iterator p = this->section_list_.begin();
1437 p != this->section_list_.end();
1440 if ((*p)->needs_dynsym_index())
1442 gold_assert(dynsym_section != NULL);
1443 unsigned int index = (*p)->dynsym_index();
1444 gold_assert(index > 0 && index != -1U);
1445 off_t off = (dynsym_section->offset()
1446 + index * dynsym_section->entsize());
1447 symtab->write_section_symbol(target, *p, of, off);
1451 // Write out the Output_sections. Most won't have anything to
1452 // write, since most of the data will come from input sections which
1453 // are handled elsewhere. But some Output_sections do have
1455 for (Section_list::const_iterator p = this->section_list_.begin();
1456 p != this->section_list_.end();
1460 // Write out the Output_data which are not in an Output_section.
1461 for (Data_list::const_iterator p = this->special_output_list_.begin();
1462 p != this->special_output_list_.end();
1467 // Write_data_task methods.
1469 // We can always run this task.
1471 Task::Is_runnable_type
1472 Write_data_task::is_runnable(Workqueue*)
1477 // We need to unlock FINAL_BLOCKER when finished.
1480 Write_data_task::locks(Workqueue* workqueue)
1482 return new Task_locker_block(*this->final_blocker_, workqueue);
1485 // Run the task--write out the data.
1488 Write_data_task::run(Workqueue*)
1490 this->layout_->write_data(this->symtab_, this->target_, this->of_);
1493 // Write_symbols_task methods.
1495 // We can always run this task.
1497 Task::Is_runnable_type
1498 Write_symbols_task::is_runnable(Workqueue*)
1503 // We need to unlock FINAL_BLOCKER when finished.
1506 Write_symbols_task::locks(Workqueue* workqueue)
1508 return new Task_locker_block(*this->final_blocker_, workqueue);
1511 // Run the task--write out the symbols.
1514 Write_symbols_task::run(Workqueue*)
1516 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1520 // Close_task_runner methods.
1522 // Run the task--close the file.
1525 Close_task_runner::run(Workqueue*)
1530 // Instantiate the templates we need. We could use the configure
1531 // script to restrict this to only the ones for implemented targets.
1533 #ifdef HAVE_TARGET_32_LITTLE
1536 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1537 const elfcpp::Shdr<32, false>& shdr, off_t*);
1540 #ifdef HAVE_TARGET_32_BIG
1543 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1544 const elfcpp::Shdr<32, true>& shdr, off_t*);
1547 #ifdef HAVE_TARGET_64_LITTLE
1550 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1551 const elfcpp::Shdr<64, false>& shdr, off_t*);
1554 #ifdef HAVE_TARGET_64_BIG
1557 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1558 const elfcpp::Shdr<64, true>& shdr, off_t*);
1562 } // End namespace gold.