1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
30 #include "parameters.h"
40 // Layout_task_runner methods.
42 // Lay out the sections. This is called after all the input objects
46 Layout_task_runner::run(Workqueue* workqueue)
48 off_t file_size = this->layout_->finalize(this->input_objects_,
51 // Now we know the final size of the output file and we know where
52 // each piece of information goes.
53 Output_file* of = new Output_file(this->options_,
54 this->input_objects_->target());
57 // Queue up the final set of tasks.
58 gold::queue_final_tasks(this->options_, this->input_objects_,
59 this->symtab_, this->layout_, workqueue, of);
64 Layout::Layout(const General_options& options)
65 : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
66 section_name_map_(), segment_list_(), section_list_(),
67 unattached_section_list_(), special_output_list_(),
68 tls_segment_(NULL), symtab_section_(NULL),
69 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
70 eh_frame_section_(NULL), output_file_size_(-1)
72 // Make space for more than enough segments for a typical file.
73 // This is just for efficiency--it's OK if we wind up needing more.
74 this->segment_list_.reserve(12);
76 // We expect three unattached Output_data objects: the file header,
77 // the segment headers, and the section headers.
78 this->special_output_list_.reserve(3);
81 // Hash a key we use to look up an output section mapping.
84 Layout::Hash_key::operator()(const Layout::Key& k) const
86 return k.first + k.second.first + k.second.second;
89 // Return whether PREFIX is a prefix of STR.
92 is_prefix_of(const char* prefix, const char* str)
94 return strncmp(prefix, str, strlen(prefix)) == 0;
97 // Whether to include this section in the link.
99 template<int size, bool big_endian>
101 Layout::include_section(Object*, const char* name,
102 const elfcpp::Shdr<size, big_endian>& shdr)
104 // Some section types are never linked. Some are only linked when
105 // doing a relocateable link.
106 switch (shdr.get_sh_type())
108 case elfcpp::SHT_NULL:
109 case elfcpp::SHT_SYMTAB:
110 case elfcpp::SHT_DYNSYM:
111 case elfcpp::SHT_STRTAB:
112 case elfcpp::SHT_HASH:
113 case elfcpp::SHT_DYNAMIC:
114 case elfcpp::SHT_SYMTAB_SHNDX:
117 case elfcpp::SHT_RELA:
118 case elfcpp::SHT_REL:
119 case elfcpp::SHT_GROUP:
120 return parameters->output_is_object();
122 case elfcpp::SHT_PROGBITS:
123 if (parameters->strip_debug()
124 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
126 // Debugging sections can only be recognized by name.
127 if (is_prefix_of(".debug", name)
128 || is_prefix_of(".gnu.linkonce.wi.", name)
129 || is_prefix_of(".line", name)
130 || is_prefix_of(".stab", name))
140 // Return an output section named NAME, or NULL if there is none.
143 Layout::find_output_section(const char* name) const
145 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
146 p != this->section_name_map_.end();
148 if (strcmp(p->second->name(), name) == 0)
153 // Return an output segment of type TYPE, with segment flags SET set
154 // and segment flags CLEAR clear. Return NULL if there is none.
157 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
158 elfcpp::Elf_Word clear) const
160 for (Segment_list::const_iterator p = this->segment_list_.begin();
161 p != this->segment_list_.end();
163 if (static_cast<elfcpp::PT>((*p)->type()) == type
164 && ((*p)->flags() & set) == set
165 && ((*p)->flags() & clear) == 0)
170 // Return the output section to use for section NAME with type TYPE
171 // and section flags FLAGS.
174 Layout::get_output_section(const char* name, Stringpool::Key name_key,
175 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
177 // We should ignore some flags.
178 flags &= ~ (elfcpp::SHF_INFO_LINK
179 | elfcpp::SHF_LINK_ORDER
182 | elfcpp::SHF_STRINGS);
184 const Key key(name_key, std::make_pair(type, flags));
185 const std::pair<Key, Output_section*> v(key, NULL);
186 std::pair<Section_name_map::iterator, bool> ins(
187 this->section_name_map_.insert(v));
190 return ins.first->second;
193 // This is the first time we've seen this name/type/flags
195 Output_section* os = this->make_output_section(name, type, flags);
196 ins.first->second = os;
201 // Return the output section to use for input section SHNDX, with name
202 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
203 // offset of this input section without the output section.
205 template<int size, bool big_endian>
207 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
208 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
210 if (!this->include_section(object, name, shdr))
213 // If we are not doing a relocateable link, choose the name to use
214 // for the output section.
215 size_t len = strlen(name);
216 if (!parameters->output_is_object())
217 name = Layout::output_section_name(name, &len);
219 // FIXME: Handle SHF_OS_NONCONFORMING here.
221 // Canonicalize the section name.
222 Stringpool::Key name_key;
223 name = this->namepool_.add(name, len, &name_key);
225 // Find the output section. The output section is selected based on
226 // the section name, type, and flags.
227 Output_section* os = this->get_output_section(name, name_key,
229 shdr.get_sh_flags());
231 // Special GNU handling of sections named .eh_frame.
232 if (!parameters->output_is_object()
233 && strcmp(name, ".eh_frame") == 0
234 && shdr.get_sh_size() > 0
235 && shdr.get_sh_type() == elfcpp::SHT_PROGBITS
236 && shdr.get_sh_flags() == elfcpp::SHF_ALLOC)
238 this->layout_eh_frame(object, shndx, name, shdr, os, off);
242 // FIXME: Handle SHF_LINK_ORDER somewhere.
244 *off = os->add_input_section(object, shndx, name, shdr);
249 // Special GNU handling of sections named .eh_frame. They will
250 // normally hold exception frame data.
252 template<int size, bool big_endian>
254 Layout::layout_eh_frame(Relobj* object,
257 const elfcpp::Shdr<size, big_endian>& shdr,
258 Output_section* os, off_t* off)
260 if (this->eh_frame_section_ == NULL)
262 this->eh_frame_section_ = os;
264 if (this->options_.create_eh_frame_hdr())
266 Stringpool::Key hdr_name_key;
267 const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
269 Output_section* hdr_os =
270 this->get_output_section(hdr_name, hdr_name_key,
271 elfcpp::SHT_PROGBITS,
274 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os);
275 hdr_os->add_output_section_data(hdr_posd);
277 Output_segment* hdr_oseg =
278 new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
279 this->segment_list_.push_back(hdr_oseg);
280 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
284 gold_assert(this->eh_frame_section_ == os);
286 *off = os->add_input_section(object, shndx, name, shdr);
289 // Add POSD to an output section using NAME, TYPE, and FLAGS.
292 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
293 elfcpp::Elf_Xword flags,
294 Output_section_data* posd)
296 // Canonicalize the name.
297 Stringpool::Key name_key;
298 name = this->namepool_.add(name, &name_key);
300 Output_section* os = this->get_output_section(name, name_key, type, flags);
301 os->add_output_section_data(posd);
304 // Map section flags to segment flags.
307 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
309 elfcpp::Elf_Word ret = elfcpp::PF_R;
310 if ((flags & elfcpp::SHF_WRITE) != 0)
312 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
317 // Make a new Output_section, and attach it to segments as
321 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
322 elfcpp::Elf_Xword flags)
324 Output_section* os = new Output_section(name, type, flags);
325 this->section_list_.push_back(os);
327 if ((flags & elfcpp::SHF_ALLOC) == 0)
328 this->unattached_section_list_.push_back(os);
331 // This output section goes into a PT_LOAD segment.
333 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
335 // The only thing we really care about for PT_LOAD segments is
336 // whether or not they are writable, so that is how we search
337 // for them. People who need segments sorted on some other
338 // basis will have to wait until we implement a mechanism for
339 // them to describe the segments they want.
341 Segment_list::const_iterator p;
342 for (p = this->segment_list_.begin();
343 p != this->segment_list_.end();
346 if ((*p)->type() == elfcpp::PT_LOAD
347 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
349 (*p)->add_output_section(os, seg_flags);
354 if (p == this->segment_list_.end())
356 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
358 this->segment_list_.push_back(oseg);
359 oseg->add_output_section(os, seg_flags);
362 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
364 if (type == elfcpp::SHT_NOTE)
366 // See if we already have an equivalent PT_NOTE segment.
367 for (p = this->segment_list_.begin();
368 p != segment_list_.end();
371 if ((*p)->type() == elfcpp::PT_NOTE
372 && (((*p)->flags() & elfcpp::PF_W)
373 == (seg_flags & elfcpp::PF_W)))
375 (*p)->add_output_section(os, seg_flags);
380 if (p == this->segment_list_.end())
382 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
384 this->segment_list_.push_back(oseg);
385 oseg->add_output_section(os, seg_flags);
389 // If we see a loadable SHF_TLS section, we create a PT_TLS
390 // segment. There can only be one such segment.
391 if ((flags & elfcpp::SHF_TLS) != 0)
393 if (this->tls_segment_ == NULL)
395 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
397 this->segment_list_.push_back(this->tls_segment_);
399 this->tls_segment_->add_output_section(os, seg_flags);
406 // Create the dynamic sections which are needed before we read the
410 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
411 Symbol_table* symtab)
413 if (!input_objects->any_dynamic())
416 const char* dynamic_name = this->namepool_.add(".dynamic", NULL);
417 this->dynamic_section_ = this->make_output_section(dynamic_name,
420 | elfcpp::SHF_WRITE));
422 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
423 this->dynamic_section_, 0, 0,
424 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
425 elfcpp::STV_HIDDEN, 0, false, false);
427 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
429 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
432 // For each output section whose name can be represented as C symbol,
433 // define __start and __stop symbols for the section. This is a GNU
437 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
439 for (Section_list::const_iterator p = this->section_list_.begin();
440 p != this->section_list_.end();
443 const char* const name = (*p)->name();
444 if (name[strspn(name,
446 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
447 "abcdefghijklmnopqrstuvwxyz"
451 const std::string name_string(name);
452 const std::string start_name("__start_" + name_string);
453 const std::string stop_name("__stop_" + name_string);
455 symtab->define_in_output_data(target,
465 false, // offset_is_from_end
466 false); // only_if_ref
468 symtab->define_in_output_data(target,
478 true, // offset_is_from_end
479 false); // only_if_ref
484 // Find the first read-only PT_LOAD segment, creating one if
488 Layout::find_first_load_seg()
490 for (Segment_list::const_iterator p = this->segment_list_.begin();
491 p != this->segment_list_.end();
494 if ((*p)->type() == elfcpp::PT_LOAD
495 && ((*p)->flags() & elfcpp::PF_R) != 0
496 && ((*p)->flags() & elfcpp::PF_W) == 0)
500 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
501 this->segment_list_.push_back(load_seg);
505 // Finalize the layout. When this is called, we have created all the
506 // output sections and all the output segments which are based on
507 // input sections. We have several things to do, and we have to do
508 // them in the right order, so that we get the right results correctly
511 // 1) Finalize the list of output segments and create the segment
514 // 2) Finalize the dynamic symbol table and associated sections.
516 // 3) Determine the final file offset of all the output segments.
518 // 4) Determine the final file offset of all the SHF_ALLOC output
521 // 5) Create the symbol table sections and the section name table
524 // 6) Finalize the symbol table: set symbol values to their final
525 // value and make a final determination of which symbols are going
526 // into the output symbol table.
528 // 7) Create the section table header.
530 // 8) Determine the final file offset of all the output sections which
531 // are not SHF_ALLOC, including the section table header.
533 // 9) Finalize the ELF file header.
535 // This function returns the size of the output file.
538 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
540 Target* const target = input_objects->target();
542 target->finalize_sections(this);
544 this->create_note_section();
546 Output_segment* phdr_seg = NULL;
547 if (input_objects->any_dynamic())
549 // There was a dynamic object in the link. We need to create
550 // some information for the dynamic linker.
552 // Create the PT_PHDR segment which will hold the program
554 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
555 this->segment_list_.push_back(phdr_seg);
557 // Create the dynamic symbol table, including the hash table.
558 Output_section* dynstr;
559 std::vector<Symbol*> dynamic_symbols;
560 unsigned int local_dynamic_count;
562 this->create_dynamic_symtab(target, symtab, &dynstr,
563 &local_dynamic_count, &dynamic_symbols,
566 // Create the .interp section to hold the name of the
567 // interpreter, and put it in a PT_INTERP segment.
568 this->create_interp(target);
570 // Finish the .dynamic section to hold the dynamic data, and put
571 // it in a PT_DYNAMIC segment.
572 this->finish_dynamic_section(input_objects, symtab);
574 // We should have added everything we need to the dynamic string
576 this->dynpool_.set_string_offsets();
578 // Create the version sections. We can't do this until the
579 // dynamic string table is complete.
580 this->create_version_sections(&versions, local_dynamic_count,
581 dynamic_symbols, dynstr);
584 // FIXME: Handle PT_GNU_STACK.
586 Output_segment* load_seg = this->find_first_load_seg();
588 // Lay out the segment headers.
589 Output_segment_headers* segment_headers;
590 segment_headers = new Output_segment_headers(this->segment_list_);
591 load_seg->add_initial_output_data(segment_headers);
592 this->special_output_list_.push_back(segment_headers);
593 if (phdr_seg != NULL)
594 phdr_seg->add_initial_output_data(segment_headers);
596 // Lay out the file header.
597 Output_file_header* file_header;
598 file_header = new Output_file_header(target, symtab, segment_headers);
599 load_seg->add_initial_output_data(file_header);
600 this->special_output_list_.push_back(file_header);
602 // We set the output section indexes in set_segment_offsets and
603 // set_section_offsets.
604 unsigned int shndx = 1;
606 // Set the file offsets of all the segments, and all the sections
608 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
610 // Create the symbol table sections.
611 this->create_symtab_sections(input_objects, symtab, &off);
613 // Create the .shstrtab section.
614 Output_section* shstrtab_section = this->create_shstrtab();
616 // Set the file offsets of all the sections not associated with
618 off = this->set_section_offsets(off, &shndx);
620 // Create the section table header.
621 Output_section_headers* oshdrs = this->create_shdrs(&off);
623 file_header->set_section_info(oshdrs, shstrtab_section);
625 // Now we know exactly where everything goes in the output file.
626 Output_data::layout_complete();
628 this->output_file_size_ = off;
633 // Create a .note section for an executable or shared library. This
634 // records the version of gold used to create the binary.
637 Layout::create_note_section()
639 if (parameters->output_is_object())
642 const int size = parameters->get_size();
644 // The contents of the .note section.
645 const char* name = "GNU";
646 std::string desc(std::string("gold ") + gold::get_version_string());
647 size_t namesz = strlen(name) + 1;
648 size_t aligned_namesz = align_address(namesz, size / 8);
649 size_t descsz = desc.length() + 1;
650 size_t aligned_descsz = align_address(descsz, size / 8);
651 const int note_type = 4;
653 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
655 unsigned char buffer[128];
656 gold_assert(sizeof buffer >= notesz);
657 memset(buffer, 0, notesz);
659 bool is_big_endian = parameters->is_big_endian();
665 elfcpp::Swap<32, false>::writeval(buffer, namesz);
666 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
667 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
671 elfcpp::Swap<32, true>::writeval(buffer, namesz);
672 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
673 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
680 elfcpp::Swap<64, false>::writeval(buffer, namesz);
681 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
682 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
686 elfcpp::Swap<64, true>::writeval(buffer, namesz);
687 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
688 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
694 memcpy(buffer + 3 * (size / 8), name, namesz);
695 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
697 const char* note_name = this->namepool_.add(".note", NULL);
698 Output_section* os = this->make_output_section(note_name,
701 Output_section_data* posd = new Output_data_const(buffer, notesz,
703 os->add_output_section_data(posd);
706 // Return whether SEG1 should be before SEG2 in the output file. This
707 // is based entirely on the segment type and flags. When this is
708 // called the segment addresses has normally not yet been set.
711 Layout::segment_precedes(const Output_segment* seg1,
712 const Output_segment* seg2)
714 elfcpp::Elf_Word type1 = seg1->type();
715 elfcpp::Elf_Word type2 = seg2->type();
717 // The single PT_PHDR segment is required to precede any loadable
718 // segment. We simply make it always first.
719 if (type1 == elfcpp::PT_PHDR)
721 gold_assert(type2 != elfcpp::PT_PHDR);
724 if (type2 == elfcpp::PT_PHDR)
727 // The single PT_INTERP segment is required to precede any loadable
728 // segment. We simply make it always second.
729 if (type1 == elfcpp::PT_INTERP)
731 gold_assert(type2 != elfcpp::PT_INTERP);
734 if (type2 == elfcpp::PT_INTERP)
737 // We then put PT_LOAD segments before any other segments.
738 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
740 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
743 // We put the PT_TLS segment last, because that is where the dynamic
744 // linker expects to find it (this is just for efficiency; other
745 // positions would also work correctly).
746 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
748 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
751 const elfcpp::Elf_Word flags1 = seg1->flags();
752 const elfcpp::Elf_Word flags2 = seg2->flags();
754 // The order of non-PT_LOAD segments is unimportant. We simply sort
755 // by the numeric segment type and flags values. There should not
756 // be more than one segment with the same type and flags.
757 if (type1 != elfcpp::PT_LOAD)
760 return type1 < type2;
761 gold_assert(flags1 != flags2);
762 return flags1 < flags2;
765 // We sort PT_LOAD segments based on the flags. Readonly segments
766 // come before writable segments. Then executable segments come
767 // before non-executable segments. Then the unlikely case of a
768 // non-readable segment comes before the normal case of a readable
769 // segment. If there are multiple segments with the same type and
770 // flags, we require that the address be set, and we sort by
771 // virtual address and then physical address.
772 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
773 return (flags1 & elfcpp::PF_W) == 0;
774 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
775 return (flags1 & elfcpp::PF_X) != 0;
776 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
777 return (flags1 & elfcpp::PF_R) == 0;
779 uint64_t vaddr1 = seg1->vaddr();
780 uint64_t vaddr2 = seg2->vaddr();
781 if (vaddr1 != vaddr2)
782 return vaddr1 < vaddr2;
784 uint64_t paddr1 = seg1->paddr();
785 uint64_t paddr2 = seg2->paddr();
786 gold_assert(paddr1 != paddr2);
787 return paddr1 < paddr2;
790 // Set the file offsets of all the segments, and all the sections they
791 // contain. They have all been created. LOAD_SEG must be be laid out
792 // first. Return the offset of the data to follow.
795 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
796 unsigned int *pshndx)
798 // Sort them into the final order.
799 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
800 Layout::Compare_segments());
802 // Find the PT_LOAD segments, and set their addresses and offsets
803 // and their section's addresses and offsets.
804 uint64_t addr = target->text_segment_address();
806 bool was_readonly = false;
807 for (Segment_list::iterator p = this->segment_list_.begin();
808 p != this->segment_list_.end();
811 if ((*p)->type() == elfcpp::PT_LOAD)
813 if (load_seg != NULL && load_seg != *p)
817 // If the last segment was readonly, and this one is not,
818 // then skip the address forward one page, maintaining the
819 // same position within the page. This lets us store both
820 // segments overlapping on a single page in the file, but
821 // the loader will put them on different pages in memory.
823 uint64_t orig_addr = addr;
824 uint64_t orig_off = off;
826 uint64_t aligned_addr = addr;
827 uint64_t abi_pagesize = target->abi_pagesize();
829 // FIXME: This should depend on the -n and -N options.
830 (*p)->set_minimum_addralign(target->common_pagesize());
832 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
834 uint64_t align = (*p)->addralign();
836 addr = align_address(addr, align);
838 if ((addr & (abi_pagesize - 1)) != 0)
839 addr = addr + abi_pagesize;
842 unsigned int shndx_hold = *pshndx;
843 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
844 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
846 // Now that we know the size of this segment, we may be able
847 // to save a page in memory, at the cost of wasting some
848 // file space, by instead aligning to the start of a new
849 // page. Here we use the real machine page size rather than
850 // the ABI mandated page size.
852 if (aligned_addr != addr)
854 uint64_t common_pagesize = target->common_pagesize();
855 uint64_t first_off = (common_pagesize
857 & (common_pagesize - 1)));
858 uint64_t last_off = new_addr & (common_pagesize - 1);
861 && ((aligned_addr & ~ (common_pagesize - 1))
862 != (new_addr & ~ (common_pagesize - 1)))
863 && first_off + last_off <= common_pagesize)
865 *pshndx = shndx_hold;
866 addr = align_address(aligned_addr, common_pagesize);
867 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
868 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
874 if (((*p)->flags() & elfcpp::PF_W) == 0)
879 // Handle the non-PT_LOAD segments, setting their offsets from their
880 // section's offsets.
881 for (Segment_list::iterator p = this->segment_list_.begin();
882 p != this->segment_list_.end();
885 if ((*p)->type() != elfcpp::PT_LOAD)
892 // Set the file offset of all the sections not associated with a
896 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
898 for (Section_list::iterator p = this->unattached_section_list_.begin();
899 p != this->unattached_section_list_.end();
902 (*p)->set_out_shndx(*pshndx);
904 if ((*p)->offset() != -1)
906 off = align_address(off, (*p)->addralign());
907 (*p)->set_address(0, off);
908 off += (*p)->data_size();
913 // Create the symbol table sections. Here we also set the final
914 // values of the symbols. At this point all the loadable sections are
918 Layout::create_symtab_sections(const Input_objects* input_objects,
919 Symbol_table* symtab,
924 if (parameters->get_size() == 32)
926 symsize = elfcpp::Elf_sizes<32>::sym_size;
929 else if (parameters->get_size() == 64)
931 symsize = elfcpp::Elf_sizes<64>::sym_size;
938 off = align_address(off, align);
939 off_t startoff = off;
941 // Save space for the dummy symbol at the start of the section. We
942 // never bother to write this out--it will just be left as zero.
944 unsigned int local_symbol_index = 1;
946 // Add STT_SECTION symbols for each Output section which needs one.
947 for (Section_list::iterator p = this->section_list_.begin();
948 p != this->section_list_.end();
951 if (!(*p)->needs_symtab_index())
952 (*p)->set_symtab_index(-1U);
955 (*p)->set_symtab_index(local_symbol_index);
956 ++local_symbol_index;
961 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
962 p != input_objects->relobj_end();
965 Task_lock_obj<Object> tlo(**p);
966 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
969 off += (index - local_symbol_index) * symsize;
970 local_symbol_index = index;
973 unsigned int local_symcount = local_symbol_index;
974 gold_assert(local_symcount * symsize == off - startoff);
977 size_t dyn_global_index;
979 if (this->dynsym_section_ == NULL)
982 dyn_global_index = 0;
987 dyn_global_index = this->dynsym_section_->info();
988 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
989 dynoff = this->dynsym_section_->offset() + locsize;
990 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
991 gold_assert(static_cast<off_t>(dyncount * symsize)
992 == this->dynsym_section_->data_size() - locsize);
995 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
996 dyncount, &this->sympool_);
998 if (!parameters->strip_all())
1000 this->sympool_.set_string_offsets();
1002 const char* symtab_name = this->namepool_.add(".symtab", NULL);
1003 Output_section* osymtab = this->make_output_section(symtab_name,
1006 this->symtab_section_ = osymtab;
1008 Output_section_data* pos = new Output_data_space(off - startoff,
1010 osymtab->add_output_section_data(pos);
1012 const char* strtab_name = this->namepool_.add(".strtab", NULL);
1013 Output_section* ostrtab = this->make_output_section(strtab_name,
1017 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1018 ostrtab->add_output_section_data(pstr);
1020 osymtab->set_address(0, startoff);
1021 osymtab->set_link_section(ostrtab);
1022 osymtab->set_info(local_symcount);
1023 osymtab->set_entsize(symsize);
1029 // Create the .shstrtab section, which holds the names of the
1030 // sections. At the time this is called, we have created all the
1031 // output sections except .shstrtab itself.
1034 Layout::create_shstrtab()
1036 // FIXME: We don't need to create a .shstrtab section if we are
1037 // stripping everything.
1039 const char* name = this->namepool_.add(".shstrtab", NULL);
1041 this->namepool_.set_string_offsets();
1043 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1045 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1046 os->add_output_section_data(posd);
1051 // Create the section headers. SIZE is 32 or 64. OFF is the file
1054 Output_section_headers*
1055 Layout::create_shdrs(off_t* poff)
1057 Output_section_headers* oshdrs;
1058 oshdrs = new Output_section_headers(this,
1059 &this->segment_list_,
1060 &this->unattached_section_list_,
1062 off_t off = align_address(*poff, oshdrs->addralign());
1063 oshdrs->set_address(0, off);
1064 off += oshdrs->data_size();
1066 this->special_output_list_.push_back(oshdrs);
1070 // Create the dynamic symbol table.
1073 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
1074 Output_section **pdynstr,
1075 unsigned int* plocal_dynamic_count,
1076 std::vector<Symbol*>* pdynamic_symbols,
1077 Versions* pversions)
1079 // Count all the symbols in the dynamic symbol table, and set the
1080 // dynamic symbol indexes.
1082 // Skip symbol 0, which is always all zeroes.
1083 unsigned int index = 1;
1085 // Add STT_SECTION symbols for each Output section which needs one.
1086 for (Section_list::iterator p = this->section_list_.begin();
1087 p != this->section_list_.end();
1090 if (!(*p)->needs_dynsym_index())
1091 (*p)->set_dynsym_index(-1U);
1094 (*p)->set_dynsym_index(index);
1099 // FIXME: Some targets apparently require local symbols in the
1100 // dynamic symbol table. Here is where we will have to count them,
1101 // and set the dynamic symbol indexes, and add the names to
1104 unsigned int local_symcount = index;
1105 *plocal_dynamic_count = local_symcount;
1107 // FIXME: We have to tell set_dynsym_indexes whether the
1108 // -E/--export-dynamic option was used.
1109 index = symtab->set_dynsym_indexes(&this->options_, target, index,
1110 pdynamic_symbols, &this->dynpool_,
1115 const int size = parameters->get_size();
1118 symsize = elfcpp::Elf_sizes<32>::sym_size;
1121 else if (size == 64)
1123 symsize = elfcpp::Elf_sizes<64>::sym_size;
1129 // Create the dynamic symbol table section.
1131 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
1132 Output_section* dynsym = this->make_output_section(dynsym_name,
1136 Output_section_data* odata = new Output_data_space(index * symsize,
1138 dynsym->add_output_section_data(odata);
1140 dynsym->set_info(local_symcount);
1141 dynsym->set_entsize(symsize);
1142 dynsym->set_addralign(align);
1144 this->dynsym_section_ = dynsym;
1146 Output_data_dynamic* const odyn = this->dynamic_data_;
1147 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1148 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1150 // Create the dynamic string table section.
1152 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
1153 Output_section* dynstr = this->make_output_section(dynstr_name,
1157 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1158 dynstr->add_output_section_data(strdata);
1160 dynsym->set_link_section(dynstr);
1161 this->dynamic_section_->set_link_section(dynstr);
1163 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1164 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1168 // Create the hash tables.
1170 // FIXME: We need an option to create a GNU hash table.
1172 unsigned char* phash;
1173 unsigned int hashlen;
1174 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1177 const char* hash_name = this->namepool_.add(".hash", NULL);
1178 Output_section* hashsec = this->make_output_section(hash_name,
1182 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1185 hashsec->add_output_section_data(hashdata);
1187 hashsec->set_link_section(dynsym);
1188 hashsec->set_entsize(4);
1190 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1193 // Create the version sections.
1196 Layout::create_version_sections(const Versions* versions,
1197 unsigned int local_symcount,
1198 const std::vector<Symbol*>& dynamic_symbols,
1199 const Output_section* dynstr)
1201 if (!versions->any_defs() && !versions->any_needs())
1204 if (parameters->get_size() == 32)
1206 if (parameters->is_big_endian())
1208 #ifdef HAVE_TARGET_32_BIG
1209 this->sized_create_version_sections
1210 SELECT_SIZE_ENDIAN_NAME(32, true)(
1211 versions, local_symcount, dynamic_symbols, dynstr
1212 SELECT_SIZE_ENDIAN(32, true));
1219 #ifdef HAVE_TARGET_32_LITTLE
1220 this->sized_create_version_sections
1221 SELECT_SIZE_ENDIAN_NAME(32, false)(
1222 versions, local_symcount, dynamic_symbols, dynstr
1223 SELECT_SIZE_ENDIAN(32, false));
1229 else if (parameters->get_size() == 64)
1231 if (parameters->is_big_endian())
1233 #ifdef HAVE_TARGET_64_BIG
1234 this->sized_create_version_sections
1235 SELECT_SIZE_ENDIAN_NAME(64, true)(
1236 versions, local_symcount, dynamic_symbols, dynstr
1237 SELECT_SIZE_ENDIAN(64, true));
1244 #ifdef HAVE_TARGET_64_LITTLE
1245 this->sized_create_version_sections
1246 SELECT_SIZE_ENDIAN_NAME(64, false)(
1247 versions, local_symcount, dynamic_symbols, dynstr
1248 SELECT_SIZE_ENDIAN(64, false));
1258 // Create the version sections, sized version.
1260 template<int size, bool big_endian>
1262 Layout::sized_create_version_sections(
1263 const Versions* versions,
1264 unsigned int local_symcount,
1265 const std::vector<Symbol*>& dynamic_symbols,
1266 const Output_section* dynstr
1269 const char* vname = this->namepool_.add(".gnu.version", NULL);
1270 Output_section* vsec = this->make_output_section(vname,
1271 elfcpp::SHT_GNU_versym,
1274 unsigned char* vbuf;
1276 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1277 &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1278 SELECT_SIZE_ENDIAN(size, big_endian));
1280 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1282 vsec->add_output_section_data(vdata);
1283 vsec->set_entsize(2);
1284 vsec->set_link_section(this->dynsym_section_);
1286 Output_data_dynamic* const odyn = this->dynamic_data_;
1287 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1289 if (versions->any_defs())
1291 const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
1292 Output_section *vdsec;
1293 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1296 unsigned char* vdbuf;
1297 unsigned int vdsize;
1298 unsigned int vdentries;
1299 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1300 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1301 SELECT_SIZE_ENDIAN(size, big_endian));
1303 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1307 vdsec->add_output_section_data(vddata);
1308 vdsec->set_link_section(dynstr);
1309 vdsec->set_info(vdentries);
1311 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1312 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1315 if (versions->any_needs())
1317 const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
1318 Output_section* vnsec;
1319 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1322 unsigned char* vnbuf;
1323 unsigned int vnsize;
1324 unsigned int vnentries;
1325 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1326 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1327 SELECT_SIZE_ENDIAN(size, big_endian));
1329 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1333 vnsec->add_output_section_data(vndata);
1334 vnsec->set_link_section(dynstr);
1335 vnsec->set_info(vnentries);
1337 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1338 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1342 // Create the .interp section and PT_INTERP segment.
1345 Layout::create_interp(const Target* target)
1347 const char* interp = this->options_.dynamic_linker();
1350 interp = target->dynamic_linker();
1351 gold_assert(interp != NULL);
1354 size_t len = strlen(interp) + 1;
1356 Output_section_data* odata = new Output_data_const(interp, len, 1);
1358 const char* interp_name = this->namepool_.add(".interp", NULL);
1359 Output_section* osec = this->make_output_section(interp_name,
1360 elfcpp::SHT_PROGBITS,
1362 osec->add_output_section_data(odata);
1364 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1365 this->segment_list_.push_back(oseg);
1366 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1369 // Finish the .dynamic section and PT_DYNAMIC segment.
1372 Layout::finish_dynamic_section(const Input_objects* input_objects,
1373 const Symbol_table* symtab)
1375 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1376 elfcpp::PF_R | elfcpp::PF_W);
1377 this->segment_list_.push_back(oseg);
1378 oseg->add_initial_output_section(this->dynamic_section_,
1379 elfcpp::PF_R | elfcpp::PF_W);
1381 Output_data_dynamic* const odyn = this->dynamic_data_;
1383 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1384 p != input_objects->dynobj_end();
1387 // FIXME: Handle --as-needed.
1388 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1391 // FIXME: Support --init and --fini.
1392 Symbol* sym = symtab->lookup("_init");
1393 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1394 odyn->add_symbol(elfcpp::DT_INIT, sym);
1396 sym = symtab->lookup("_fini");
1397 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1398 odyn->add_symbol(elfcpp::DT_FINI, sym);
1400 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1402 // Add a DT_RPATH entry if needed.
1403 const General_options::Dir_list& rpath(this->options_.rpath());
1406 std::string rpath_val;
1407 for (General_options::Dir_list::const_iterator p = rpath.begin();
1411 if (rpath_val.empty())
1412 rpath_val = p->name();
1415 // Eliminate duplicates.
1416 General_options::Dir_list::const_iterator q;
1417 for (q = rpath.begin(); q != p; ++q)
1418 if (q->name() == p->name())
1423 rpath_val += p->name();
1428 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1432 // The mapping of .gnu.linkonce section names to real section names.
1434 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1435 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1437 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1438 MAPPING_INIT("t", ".text"),
1439 MAPPING_INIT("r", ".rodata"),
1440 MAPPING_INIT("d", ".data"),
1441 MAPPING_INIT("b", ".bss"),
1442 MAPPING_INIT("s", ".sdata"),
1443 MAPPING_INIT("sb", ".sbss"),
1444 MAPPING_INIT("s2", ".sdata2"),
1445 MAPPING_INIT("sb2", ".sbss2"),
1446 MAPPING_INIT("wi", ".debug_info"),
1447 MAPPING_INIT("td", ".tdata"),
1448 MAPPING_INIT("tb", ".tbss"),
1449 MAPPING_INIT("lr", ".lrodata"),
1450 MAPPING_INIT("l", ".ldata"),
1451 MAPPING_INIT("lb", ".lbss"),
1455 const int Layout::linkonce_mapping_count =
1456 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1458 // Return the name of the output section to use for a .gnu.linkonce
1459 // section. This is based on the default ELF linker script of the old
1460 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1461 // to ".text". Set *PLEN to the length of the name. *PLEN is
1462 // initialized to the length of NAME.
1465 Layout::linkonce_output_name(const char* name, size_t *plen)
1467 const char* s = name + sizeof(".gnu.linkonce") - 1;
1471 const Linkonce_mapping* plm = linkonce_mapping;
1472 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1474 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1483 // Choose the output section name to use given an input section name.
1484 // Set *PLEN to the length of the name. *PLEN is initialized to the
1488 Layout::output_section_name(const char* name, size_t* plen)
1490 if (Layout::is_linkonce(name))
1492 // .gnu.linkonce sections are laid out as though they were named
1493 // for the sections are placed into.
1494 return Layout::linkonce_output_name(name, plen);
1497 // If the section name has no '.', or only an initial '.', we use
1498 // the name unchanged (i.e., ".text" is unchanged).
1500 // Otherwise, if the section name does not include ".rel", we drop
1501 // the last '.' and everything that follows (i.e., ".text.XXX"
1502 // becomes ".text").
1504 // Otherwise, if the section name has zero or one '.' after the
1505 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1508 // Otherwise, we drop the last '.' and everything that follows
1509 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1511 const char* s = name;
1514 const char* sdot = strchr(s, '.');
1518 const char* srel = strstr(s, ".rel");
1521 *plen = sdot - name;
1525 sdot = strchr(srel + 1, '.');
1528 sdot = strchr(sdot + 1, '.');
1532 *plen = sdot - name;
1536 // Record the signature of a comdat section, and return whether to
1537 // include it in the link. If GROUP is true, this is a regular
1538 // section group. If GROUP is false, this is a group signature
1539 // derived from the name of a linkonce section. We want linkonce
1540 // signatures and group signatures to block each other, but we don't
1541 // want a linkonce signature to block another linkonce signature.
1544 Layout::add_comdat(const char* signature, bool group)
1546 std::string sig(signature);
1547 std::pair<Signatures::iterator, bool> ins(
1548 this->signatures_.insert(std::make_pair(sig, group)));
1552 // This is the first time we've seen this signature.
1556 if (ins.first->second)
1558 // We've already seen a real section group with this signature.
1563 // This is a real section group, and we've already seen a
1564 // linkonce section with this signature. Record that we've seen
1565 // a section group, and don't include this section group.
1566 ins.first->second = true;
1571 // We've already seen a linkonce section and this is a linkonce
1572 // section. These don't block each other--this may be the same
1573 // symbol name with different section types.
1578 // Write out data not associated with a section or the symbol table.
1581 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
1583 if (!parameters->strip_all())
1585 const Output_section* symtab_section = this->symtab_section_;
1586 for (Section_list::const_iterator p = this->section_list_.begin();
1587 p != this->section_list_.end();
1590 if ((*p)->needs_symtab_index())
1592 gold_assert(symtab_section != NULL);
1593 unsigned int index = (*p)->symtab_index();
1594 gold_assert(index > 0 && index != -1U);
1595 off_t off = (symtab_section->offset()
1596 + index * symtab_section->entsize());
1597 symtab->write_section_symbol(*p, of, off);
1602 const Output_section* dynsym_section = this->dynsym_section_;
1603 for (Section_list::const_iterator p = this->section_list_.begin();
1604 p != this->section_list_.end();
1607 if ((*p)->needs_dynsym_index())
1609 gold_assert(dynsym_section != NULL);
1610 unsigned int index = (*p)->dynsym_index();
1611 gold_assert(index > 0 && index != -1U);
1612 off_t off = (dynsym_section->offset()
1613 + index * dynsym_section->entsize());
1614 symtab->write_section_symbol(*p, of, off);
1618 // Write out the Output_sections. Most won't have anything to
1619 // write, since most of the data will come from input sections which
1620 // are handled elsewhere. But some Output_sections do have
1622 for (Section_list::const_iterator p = this->section_list_.begin();
1623 p != this->section_list_.end();
1627 // Write out the Output_data which are not in an Output_section.
1628 for (Data_list::const_iterator p = this->special_output_list_.begin();
1629 p != this->special_output_list_.end();
1634 // Write_data_task methods.
1636 // We can always run this task.
1638 Task::Is_runnable_type
1639 Write_data_task::is_runnable(Workqueue*)
1644 // We need to unlock FINAL_BLOCKER when finished.
1647 Write_data_task::locks(Workqueue* workqueue)
1649 return new Task_locker_block(*this->final_blocker_, workqueue);
1652 // Run the task--write out the data.
1655 Write_data_task::run(Workqueue*)
1657 this->layout_->write_data(this->symtab_, this->of_);
1660 // Write_symbols_task methods.
1662 // We can always run this task.
1664 Task::Is_runnable_type
1665 Write_symbols_task::is_runnable(Workqueue*)
1670 // We need to unlock FINAL_BLOCKER when finished.
1673 Write_symbols_task::locks(Workqueue* workqueue)
1675 return new Task_locker_block(*this->final_blocker_, workqueue);
1678 // Run the task--write out the symbols.
1681 Write_symbols_task::run(Workqueue*)
1683 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1687 // Close_task_runner methods.
1689 // Run the task--close the file.
1692 Close_task_runner::run(Workqueue*)
1697 // Instantiate the templates we need. We could use the configure
1698 // script to restrict this to only the ones for implemented targets.
1700 #ifdef HAVE_TARGET_32_LITTLE
1703 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1704 const elfcpp::Shdr<32, false>& shdr, off_t*);
1707 #ifdef HAVE_TARGET_32_BIG
1710 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1711 const elfcpp::Shdr<32, true>& shdr, off_t*);
1714 #ifdef HAVE_TARGET_64_LITTLE
1717 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1718 const elfcpp::Shdr<64, false>& shdr, off_t*);
1721 #ifdef HAVE_TARGET_64_BIG
1724 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1725 const elfcpp::Shdr<64, true>& shdr, off_t*);
1729 } // End namespace gold.