1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
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),
71 input_requires_executable_stack_(false),
72 input_with_gnu_stack_note_(false),
73 input_without_gnu_stack_note_(false)
75 // Make space for more than enough segments for a typical file.
76 // This is just for efficiency--it's OK if we wind up needing more.
77 this->segment_list_.reserve(12);
79 // We expect three unattached Output_data objects: the file header,
80 // the segment headers, and the section headers.
81 this->special_output_list_.reserve(3);
84 // Hash a key we use to look up an output section mapping.
87 Layout::Hash_key::operator()(const Layout::Key& k) const
89 return k.first + k.second.first + k.second.second;
92 // Return whether PREFIX is a prefix of STR.
95 is_prefix_of(const char* prefix, const char* str)
97 return strncmp(prefix, str, strlen(prefix)) == 0;
100 // Whether to include this section in the link.
102 template<int size, bool big_endian>
104 Layout::include_section(Object*, const char* name,
105 const elfcpp::Shdr<size, big_endian>& shdr)
107 // Some section types are never linked. Some are only linked when
108 // doing a relocateable link.
109 switch (shdr.get_sh_type())
111 case elfcpp::SHT_NULL:
112 case elfcpp::SHT_SYMTAB:
113 case elfcpp::SHT_DYNSYM:
114 case elfcpp::SHT_STRTAB:
115 case elfcpp::SHT_HASH:
116 case elfcpp::SHT_DYNAMIC:
117 case elfcpp::SHT_SYMTAB_SHNDX:
120 case elfcpp::SHT_RELA:
121 case elfcpp::SHT_REL:
122 case elfcpp::SHT_GROUP:
123 return parameters->output_is_object();
125 case elfcpp::SHT_PROGBITS:
126 if (parameters->strip_debug()
127 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
129 // Debugging sections can only be recognized by name.
130 if (is_prefix_of(".debug", name)
131 || is_prefix_of(".gnu.linkonce.wi.", name)
132 || is_prefix_of(".line", name)
133 || is_prefix_of(".stab", name))
143 // Return an output section named NAME, or NULL if there is none.
146 Layout::find_output_section(const char* name) const
148 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
149 p != this->section_name_map_.end();
151 if (strcmp(p->second->name(), name) == 0)
156 // Return an output segment of type TYPE, with segment flags SET set
157 // and segment flags CLEAR clear. Return NULL if there is none.
160 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
161 elfcpp::Elf_Word clear) const
163 for (Segment_list::const_iterator p = this->segment_list_.begin();
164 p != this->segment_list_.end();
166 if (static_cast<elfcpp::PT>((*p)->type()) == type
167 && ((*p)->flags() & set) == set
168 && ((*p)->flags() & clear) == 0)
173 // Return the output section to use for section NAME with type TYPE
174 // and section flags FLAGS.
177 Layout::get_output_section(const char* name, Stringpool::Key name_key,
178 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
180 // We should ignore some flags.
181 flags &= ~ (elfcpp::SHF_INFO_LINK
182 | elfcpp::SHF_LINK_ORDER
185 | elfcpp::SHF_STRINGS);
187 const Key key(name_key, std::make_pair(type, flags));
188 const std::pair<Key, Output_section*> v(key, NULL);
189 std::pair<Section_name_map::iterator, bool> ins(
190 this->section_name_map_.insert(v));
193 return ins.first->second;
196 // This is the first time we've seen this name/type/flags
198 Output_section* os = this->make_output_section(name, type, flags);
199 ins.first->second = os;
204 // Return the output section to use for input section SHNDX, with name
205 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
206 // offset of this input section without the output section.
208 template<int size, bool big_endian>
210 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
211 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
213 if (!this->include_section(object, name, shdr))
216 // If we are not doing a relocateable link, choose the name to use
217 // for the output section.
218 size_t len = strlen(name);
219 if (!parameters->output_is_object())
220 name = Layout::output_section_name(name, &len);
222 // FIXME: Handle SHF_OS_NONCONFORMING here.
224 // Canonicalize the section name.
225 Stringpool::Key name_key;
226 name = this->namepool_.add_prefix(name, len, &name_key);
228 // Find the output section. The output section is selected based on
229 // the section name, type, and flags.
230 Output_section* os = this->get_output_section(name, name_key,
232 shdr.get_sh_flags());
234 // Special GNU handling of sections named .eh_frame.
235 if (!parameters->output_is_object()
236 && strcmp(name, ".eh_frame") == 0
237 && shdr.get_sh_size() > 0
238 && shdr.get_sh_type() == elfcpp::SHT_PROGBITS
239 && shdr.get_sh_flags() == elfcpp::SHF_ALLOC)
241 this->layout_eh_frame(object, shndx, name, shdr, os, off);
245 // FIXME: Handle SHF_LINK_ORDER somewhere.
247 *off = os->add_input_section(object, shndx, name, shdr);
252 // Special GNU handling of sections named .eh_frame. They will
253 // normally hold exception frame data.
255 template<int size, bool big_endian>
257 Layout::layout_eh_frame(Relobj* object,
260 const elfcpp::Shdr<size, big_endian>& shdr,
261 Output_section* os, off_t* off)
263 if (this->eh_frame_section_ == NULL)
265 this->eh_frame_section_ = os;
267 if (this->options_.create_eh_frame_hdr())
269 Stringpool::Key hdr_name_key;
270 const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
273 Output_section* hdr_os =
274 this->get_output_section(hdr_name, hdr_name_key,
275 elfcpp::SHT_PROGBITS,
278 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os);
279 hdr_os->add_output_section_data(hdr_posd);
281 Output_segment* hdr_oseg =
282 new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
283 this->segment_list_.push_back(hdr_oseg);
284 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
288 gold_assert(this->eh_frame_section_ == os);
290 *off = os->add_input_section(object, shndx, name, shdr);
293 // Add POSD to an output section using NAME, TYPE, and FLAGS.
296 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
297 elfcpp::Elf_Xword flags,
298 Output_section_data* posd)
300 // Canonicalize the name.
301 Stringpool::Key name_key;
302 name = this->namepool_.add(name, true, &name_key);
304 Output_section* os = this->get_output_section(name, name_key, type, flags);
305 os->add_output_section_data(posd);
308 // Map section flags to segment flags.
311 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
313 elfcpp::Elf_Word ret = elfcpp::PF_R;
314 if ((flags & elfcpp::SHF_WRITE) != 0)
316 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
321 // Make a new Output_section, and attach it to segments as
325 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
326 elfcpp::Elf_Xword flags)
328 Output_section* os = new Output_section(name, type, flags);
329 this->section_list_.push_back(os);
331 if ((flags & elfcpp::SHF_ALLOC) == 0)
332 this->unattached_section_list_.push_back(os);
335 // This output section goes into a PT_LOAD segment.
337 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
339 // The only thing we really care about for PT_LOAD segments is
340 // whether or not they are writable, so that is how we search
341 // for them. People who need segments sorted on some other
342 // basis will have to wait until we implement a mechanism for
343 // them to describe the segments they want.
345 Segment_list::const_iterator p;
346 for (p = this->segment_list_.begin();
347 p != this->segment_list_.end();
350 if ((*p)->type() == elfcpp::PT_LOAD
351 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
353 (*p)->add_output_section(os, seg_flags);
358 if (p == this->segment_list_.end())
360 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
362 this->segment_list_.push_back(oseg);
363 oseg->add_output_section(os, seg_flags);
366 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
368 if (type == elfcpp::SHT_NOTE)
370 // See if we already have an equivalent PT_NOTE segment.
371 for (p = this->segment_list_.begin();
372 p != segment_list_.end();
375 if ((*p)->type() == elfcpp::PT_NOTE
376 && (((*p)->flags() & elfcpp::PF_W)
377 == (seg_flags & elfcpp::PF_W)))
379 (*p)->add_output_section(os, seg_flags);
384 if (p == this->segment_list_.end())
386 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
388 this->segment_list_.push_back(oseg);
389 oseg->add_output_section(os, seg_flags);
393 // If we see a loadable SHF_TLS section, we create a PT_TLS
394 // segment. There can only be one such segment.
395 if ((flags & elfcpp::SHF_TLS) != 0)
397 if (this->tls_segment_ == NULL)
399 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
401 this->segment_list_.push_back(this->tls_segment_);
403 this->tls_segment_->add_output_section(os, seg_flags);
410 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
411 // is whether we saw a .note.GNU-stack section in the object file.
412 // GNU_STACK_FLAGS is the section flags. The flags give the
413 // protection required for stack memory. We record this in an
414 // executable as a PT_GNU_STACK segment. If an object file does not
415 // have a .note.GNU-stack segment, we must assume that it is an old
416 // object. On some targets that will force an executable stack.
419 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
422 this->input_without_gnu_stack_note_ = true;
425 this->input_with_gnu_stack_note_ = true;
426 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
427 this->input_requires_executable_stack_ = true;
431 // Create the dynamic sections which are needed before we read the
435 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
436 Symbol_table* symtab)
438 if (parameters->doing_static_link())
441 const char* dynamic_name = this->namepool_.add(".dynamic", false, NULL);
442 this->dynamic_section_ = this->make_output_section(dynamic_name,
445 | elfcpp::SHF_WRITE));
447 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
448 this->dynamic_section_, 0, 0,
449 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
450 elfcpp::STV_HIDDEN, 0, false, false);
452 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
454 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
457 // For each output section whose name can be represented as C symbol,
458 // define __start and __stop symbols for the section. This is a GNU
462 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
464 for (Section_list::const_iterator p = this->section_list_.begin();
465 p != this->section_list_.end();
468 const char* const name = (*p)->name();
469 if (name[strspn(name,
471 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
472 "abcdefghijklmnopqrstuvwxyz"
476 const std::string name_string(name);
477 const std::string start_name("__start_" + name_string);
478 const std::string stop_name("__stop_" + name_string);
480 symtab->define_in_output_data(target,
490 false, // offset_is_from_end
491 false); // only_if_ref
493 symtab->define_in_output_data(target,
503 true, // offset_is_from_end
504 false); // only_if_ref
509 // Find the first read-only PT_LOAD segment, creating one if
513 Layout::find_first_load_seg()
515 for (Segment_list::const_iterator p = this->segment_list_.begin();
516 p != this->segment_list_.end();
519 if ((*p)->type() == elfcpp::PT_LOAD
520 && ((*p)->flags() & elfcpp::PF_R) != 0
521 && ((*p)->flags() & elfcpp::PF_W) == 0)
525 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
526 this->segment_list_.push_back(load_seg);
530 // Finalize the layout. When this is called, we have created all the
531 // output sections and all the output segments which are based on
532 // input sections. We have several things to do, and we have to do
533 // them in the right order, so that we get the right results correctly
536 // 1) Finalize the list of output segments and create the segment
539 // 2) Finalize the dynamic symbol table and associated sections.
541 // 3) Determine the final file offset of all the output segments.
543 // 4) Determine the final file offset of all the SHF_ALLOC output
546 // 5) Create the symbol table sections and the section name table
549 // 6) Finalize the symbol table: set symbol values to their final
550 // value and make a final determination of which symbols are going
551 // into the output symbol table.
553 // 7) Create the section table header.
555 // 8) Determine the final file offset of all the output sections which
556 // are not SHF_ALLOC, including the section table header.
558 // 9) Finalize the ELF file header.
560 // This function returns the size of the output file.
563 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
565 Target* const target = input_objects->target();
567 target->finalize_sections(this);
569 this->create_gold_note();
570 this->create_executable_stack_info(target);
572 Output_segment* phdr_seg = NULL;
573 if (!parameters->doing_static_link())
575 // There was a dynamic object in the link. We need to create
576 // some information for the dynamic linker.
578 // Create the PT_PHDR segment which will hold the program
580 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
581 this->segment_list_.push_back(phdr_seg);
583 // Create the dynamic symbol table, including the hash table.
584 Output_section* dynstr;
585 std::vector<Symbol*> dynamic_symbols;
586 unsigned int local_dynamic_count;
588 this->create_dynamic_symtab(target, symtab, &dynstr,
589 &local_dynamic_count, &dynamic_symbols,
592 // Create the .interp section to hold the name of the
593 // interpreter, and put it in a PT_INTERP segment.
594 this->create_interp(target);
596 // Finish the .dynamic section to hold the dynamic data, and put
597 // it in a PT_DYNAMIC segment.
598 this->finish_dynamic_section(input_objects, symtab);
600 // We should have added everything we need to the dynamic string
602 this->dynpool_.set_string_offsets();
604 // Create the version sections. We can't do this until the
605 // dynamic string table is complete.
606 this->create_version_sections(&versions, symtab, local_dynamic_count,
607 dynamic_symbols, dynstr);
610 // FIXME: Handle PT_GNU_STACK.
612 Output_segment* load_seg = this->find_first_load_seg();
614 // Lay out the segment headers.
615 Output_segment_headers* segment_headers;
616 segment_headers = new Output_segment_headers(this->segment_list_);
617 load_seg->add_initial_output_data(segment_headers);
618 this->special_output_list_.push_back(segment_headers);
619 if (phdr_seg != NULL)
620 phdr_seg->add_initial_output_data(segment_headers);
622 // Lay out the file header.
623 Output_file_header* file_header;
624 file_header = new Output_file_header(target, symtab, segment_headers);
625 load_seg->add_initial_output_data(file_header);
626 this->special_output_list_.push_back(file_header);
628 // We set the output section indexes in set_segment_offsets and
629 // set_section_offsets.
630 unsigned int shndx = 1;
632 // Set the file offsets of all the segments, and all the sections
634 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
636 // Set the file offsets of all the data sections not associated with
637 // segments. This makes sure that debug sections have their offsets
638 // before symbols are finalized.
639 off = this->set_section_offsets(off, &shndx, true);
641 // Create the symbol table sections.
642 this->create_symtab_sections(input_objects, symtab, &off);
644 // Create the .shstrtab section.
645 Output_section* shstrtab_section = this->create_shstrtab();
647 // Set the file offsets of all the non-data sections not associated with
649 off = this->set_section_offsets(off, &shndx, false);
651 // Create the section table header.
652 Output_section_headers* oshdrs = this->create_shdrs(&off);
654 file_header->set_section_info(oshdrs, shstrtab_section);
656 // Now we know exactly where everything goes in the output file.
657 Output_data::layout_complete();
659 this->output_file_size_ = off;
664 // Create a .note section for an executable or shared library. This
665 // records the version of gold used to create the binary.
668 Layout::create_gold_note()
670 if (parameters->output_is_object())
673 // Authorities all agree that the values in a .note field should
674 // be aligned on 4-byte boundaries for 32-bit binaries. However,
675 // they differ on what the alignment is for 64-bit binaries.
676 // The GABI says unambiguously they take 8-byte alignment:
677 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
678 // Other documentation says alignment should always be 4 bytes:
679 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
680 // GNU ld and GNU readelf both support the latter (at least as of
681 // version 2.16.91), and glibc always generates the latter for
682 // .note.ABI-tag (as of version 1.6), so that's the one we go with
684 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
685 const int size = parameters->get_size();
690 // The contents of the .note section.
691 const char* name = "GNU";
692 std::string desc(std::string("gold ") + gold::get_version_string());
693 size_t namesz = strlen(name) + 1;
694 size_t aligned_namesz = align_address(namesz, size / 8);
695 size_t descsz = desc.length() + 1;
696 size_t aligned_descsz = align_address(descsz, size / 8);
697 const int note_type = 4;
699 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
701 unsigned char buffer[128];
702 gold_assert(sizeof buffer >= notesz);
703 memset(buffer, 0, notesz);
705 bool is_big_endian = parameters->is_big_endian();
711 elfcpp::Swap<32, false>::writeval(buffer, namesz);
712 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
713 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
717 elfcpp::Swap<32, true>::writeval(buffer, namesz);
718 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
719 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
726 elfcpp::Swap<64, false>::writeval(buffer, namesz);
727 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
728 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
732 elfcpp::Swap<64, true>::writeval(buffer, namesz);
733 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
734 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
740 memcpy(buffer + 3 * (size / 8), name, namesz);
741 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
743 const char* note_name = this->namepool_.add(".note", false, NULL);
744 Output_section* os = this->make_output_section(note_name,
747 Output_section_data* posd = new Output_data_const(buffer, notesz,
749 os->add_output_section_data(posd);
752 // Record whether the stack should be executable. This can be set
753 // from the command line using the -z execstack or -z noexecstack
754 // options. Otherwise, if any input file has a .note.GNU-stack
755 // section with the SHF_EXECINSTR flag set, the stack should be
756 // executable. Otherwise, if at least one input file a
757 // .note.GNU-stack section, and some input file has no .note.GNU-stack
758 // section, we use the target default for whether the stack should be
759 // executable. Otherwise, we don't generate a stack note. When
760 // generating a object file, we create a .note.GNU-stack section with
761 // the appropriate marking. When generating an executable or shared
762 // library, we create a PT_GNU_STACK segment.
765 Layout::create_executable_stack_info(const Target* target)
767 bool is_stack_executable;
768 if (this->options_.is_execstack_set())
769 is_stack_executable = this->options_.is_stack_executable();
770 else if (!this->input_with_gnu_stack_note_)
774 if (this->input_requires_executable_stack_)
775 is_stack_executable = true;
776 else if (this->input_without_gnu_stack_note_)
777 is_stack_executable = target->is_default_stack_executable();
779 is_stack_executable = false;
782 if (parameters->output_is_object())
784 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
785 elfcpp::Elf_Xword flags = 0;
786 if (is_stack_executable)
787 flags |= elfcpp::SHF_EXECINSTR;
788 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
792 int flags = elfcpp::PF_R | elfcpp::PF_W;
793 if (is_stack_executable)
794 flags |= elfcpp::PF_X;
795 Output_segment* oseg = new Output_segment(elfcpp::PT_GNU_STACK, flags);
796 this->segment_list_.push_back(oseg);
800 // Return whether SEG1 should be before SEG2 in the output file. This
801 // is based entirely on the segment type and flags. When this is
802 // called the segment addresses has normally not yet been set.
805 Layout::segment_precedes(const Output_segment* seg1,
806 const Output_segment* seg2)
808 elfcpp::Elf_Word type1 = seg1->type();
809 elfcpp::Elf_Word type2 = seg2->type();
811 // The single PT_PHDR segment is required to precede any loadable
812 // segment. We simply make it always first.
813 if (type1 == elfcpp::PT_PHDR)
815 gold_assert(type2 != elfcpp::PT_PHDR);
818 if (type2 == elfcpp::PT_PHDR)
821 // The single PT_INTERP segment is required to precede any loadable
822 // segment. We simply make it always second.
823 if (type1 == elfcpp::PT_INTERP)
825 gold_assert(type2 != elfcpp::PT_INTERP);
828 if (type2 == elfcpp::PT_INTERP)
831 // We then put PT_LOAD segments before any other segments.
832 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
834 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
837 // We put the PT_TLS segment last, because that is where the dynamic
838 // linker expects to find it (this is just for efficiency; other
839 // positions would also work correctly).
840 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
842 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
845 const elfcpp::Elf_Word flags1 = seg1->flags();
846 const elfcpp::Elf_Word flags2 = seg2->flags();
848 // The order of non-PT_LOAD segments is unimportant. We simply sort
849 // by the numeric segment type and flags values. There should not
850 // be more than one segment with the same type and flags.
851 if (type1 != elfcpp::PT_LOAD)
854 return type1 < type2;
855 gold_assert(flags1 != flags2);
856 return flags1 < flags2;
859 // We sort PT_LOAD segments based on the flags. Readonly segments
860 // come before writable segments. Then executable segments come
861 // before non-executable segments. Then the unlikely case of a
862 // non-readable segment comes before the normal case of a readable
863 // segment. If there are multiple segments with the same type and
864 // flags, we require that the address be set, and we sort by
865 // virtual address and then physical address.
866 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
867 return (flags1 & elfcpp::PF_W) == 0;
868 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
869 return (flags1 & elfcpp::PF_X) != 0;
870 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
871 return (flags1 & elfcpp::PF_R) == 0;
873 uint64_t vaddr1 = seg1->vaddr();
874 uint64_t vaddr2 = seg2->vaddr();
875 if (vaddr1 != vaddr2)
876 return vaddr1 < vaddr2;
878 uint64_t paddr1 = seg1->paddr();
879 uint64_t paddr2 = seg2->paddr();
880 gold_assert(paddr1 != paddr2);
881 return paddr1 < paddr2;
884 // Set the file offsets of all the segments, and all the sections they
885 // contain. They have all been created. LOAD_SEG must be be laid out
886 // first. Return the offset of the data to follow.
889 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
890 unsigned int *pshndx)
892 // Sort them into the final order.
893 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
894 Layout::Compare_segments());
896 // Find the PT_LOAD segments, and set their addresses and offsets
897 // and their section's addresses and offsets.
899 if (options_.user_set_text_segment_address())
900 addr = options_.text_segment_address();
902 addr = target->default_text_segment_address();
904 bool was_readonly = false;
905 for (Segment_list::iterator p = this->segment_list_.begin();
906 p != this->segment_list_.end();
909 if ((*p)->type() == elfcpp::PT_LOAD)
911 if (load_seg != NULL && load_seg != *p)
915 // If the last segment was readonly, and this one is not,
916 // then skip the address forward one page, maintaining the
917 // same position within the page. This lets us store both
918 // segments overlapping on a single page in the file, but
919 // the loader will put them on different pages in memory.
921 uint64_t orig_addr = addr;
922 uint64_t orig_off = off;
924 uint64_t aligned_addr = addr;
925 uint64_t abi_pagesize = target->abi_pagesize();
927 // FIXME: This should depend on the -n and -N options.
928 (*p)->set_minimum_addralign(target->common_pagesize());
930 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
932 uint64_t align = (*p)->addralign();
934 addr = align_address(addr, align);
936 if ((addr & (abi_pagesize - 1)) != 0)
937 addr = addr + abi_pagesize;
940 unsigned int shndx_hold = *pshndx;
941 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
942 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
944 // Now that we know the size of this segment, we may be able
945 // to save a page in memory, at the cost of wasting some
946 // file space, by instead aligning to the start of a new
947 // page. Here we use the real machine page size rather than
948 // the ABI mandated page size.
950 if (aligned_addr != addr)
952 uint64_t common_pagesize = target->common_pagesize();
953 uint64_t first_off = (common_pagesize
955 & (common_pagesize - 1)));
956 uint64_t last_off = new_addr & (common_pagesize - 1);
959 && ((aligned_addr & ~ (common_pagesize - 1))
960 != (new_addr & ~ (common_pagesize - 1)))
961 && first_off + last_off <= common_pagesize)
963 *pshndx = shndx_hold;
964 addr = align_address(aligned_addr, common_pagesize);
965 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
966 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
972 if (((*p)->flags() & elfcpp::PF_W) == 0)
977 // Handle the non-PT_LOAD segments, setting their offsets from their
978 // section's offsets.
979 for (Segment_list::iterator p = this->segment_list_.begin();
980 p != this->segment_list_.end();
983 if ((*p)->type() != elfcpp::PT_LOAD)
990 // Set the file offset of all the sections not associated with a
994 Layout::set_section_offsets(off_t off,
995 unsigned int* pshndx,
996 bool do_bits_sections)
998 for (Section_list::iterator p = this->unattached_section_list_.begin();
999 p != this->unattached_section_list_.end();
1002 bool is_bits_section = ((*p)->type() == elfcpp::SHT_PROGBITS
1003 || (*p)->type() == elfcpp::SHT_NOBITS);
1004 if (is_bits_section != do_bits_sections)
1006 (*p)->set_out_shndx(*pshndx);
1008 if ((*p)->offset() != -1)
1010 off = align_address(off, (*p)->addralign());
1011 (*p)->set_address(0, off);
1012 off += (*p)->data_size();
1017 // Create the symbol table sections. Here we also set the final
1018 // values of the symbols. At this point all the loadable sections are
1022 Layout::create_symtab_sections(const Input_objects* input_objects,
1023 Symbol_table* symtab,
1028 if (parameters->get_size() == 32)
1030 symsize = elfcpp::Elf_sizes<32>::sym_size;
1033 else if (parameters->get_size() == 64)
1035 symsize = elfcpp::Elf_sizes<64>::sym_size;
1042 off = align_address(off, align);
1043 off_t startoff = off;
1045 // Save space for the dummy symbol at the start of the section. We
1046 // never bother to write this out--it will just be left as zero.
1048 unsigned int local_symbol_index = 1;
1050 // Add STT_SECTION symbols for each Output section which needs one.
1051 for (Section_list::iterator p = this->section_list_.begin();
1052 p != this->section_list_.end();
1055 if (!(*p)->needs_symtab_index())
1056 (*p)->set_symtab_index(-1U);
1059 (*p)->set_symtab_index(local_symbol_index);
1060 ++local_symbol_index;
1065 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1066 p != input_objects->relobj_end();
1069 Task_lock_obj<Object> tlo(**p);
1070 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1073 off += (index - local_symbol_index) * symsize;
1074 local_symbol_index = index;
1077 unsigned int local_symcount = local_symbol_index;
1078 gold_assert(local_symcount * symsize == off - startoff);
1081 size_t dyn_global_index;
1083 if (this->dynsym_section_ == NULL)
1086 dyn_global_index = 0;
1091 dyn_global_index = this->dynsym_section_->info();
1092 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1093 dynoff = this->dynsym_section_->offset() + locsize;
1094 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1095 gold_assert(static_cast<off_t>(dyncount * symsize)
1096 == this->dynsym_section_->data_size() - locsize);
1099 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
1100 dyncount, &this->sympool_);
1102 if (!parameters->strip_all())
1104 this->sympool_.set_string_offsets();
1106 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1107 Output_section* osymtab = this->make_output_section(symtab_name,
1110 this->symtab_section_ = osymtab;
1112 Output_section_data* pos = new Output_data_space(off - startoff,
1114 osymtab->add_output_section_data(pos);
1116 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1117 Output_section* ostrtab = this->make_output_section(strtab_name,
1121 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1122 ostrtab->add_output_section_data(pstr);
1124 osymtab->set_address(0, startoff);
1125 osymtab->set_link_section(ostrtab);
1126 osymtab->set_info(local_symcount);
1127 osymtab->set_entsize(symsize);
1133 // Create the .shstrtab section, which holds the names of the
1134 // sections. At the time this is called, we have created all the
1135 // output sections except .shstrtab itself.
1138 Layout::create_shstrtab()
1140 // FIXME: We don't need to create a .shstrtab section if we are
1141 // stripping everything.
1143 const char* name = this->namepool_.add(".shstrtab", false, NULL);
1145 this->namepool_.set_string_offsets();
1147 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1149 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1150 os->add_output_section_data(posd);
1155 // Create the section headers. SIZE is 32 or 64. OFF is the file
1158 Output_section_headers*
1159 Layout::create_shdrs(off_t* poff)
1161 Output_section_headers* oshdrs;
1162 oshdrs = new Output_section_headers(this,
1163 &this->segment_list_,
1164 &this->unattached_section_list_,
1166 off_t off = align_address(*poff, oshdrs->addralign());
1167 oshdrs->set_address(0, off);
1168 off += oshdrs->data_size();
1170 this->special_output_list_.push_back(oshdrs);
1174 // Create the dynamic symbol table.
1177 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
1178 Output_section **pdynstr,
1179 unsigned int* plocal_dynamic_count,
1180 std::vector<Symbol*>* pdynamic_symbols,
1181 Versions* pversions)
1183 // Count all the symbols in the dynamic symbol table, and set the
1184 // dynamic symbol indexes.
1186 // Skip symbol 0, which is always all zeroes.
1187 unsigned int index = 1;
1189 // Add STT_SECTION symbols for each Output section which needs one.
1190 for (Section_list::iterator p = this->section_list_.begin();
1191 p != this->section_list_.end();
1194 if (!(*p)->needs_dynsym_index())
1195 (*p)->set_dynsym_index(-1U);
1198 (*p)->set_dynsym_index(index);
1203 // FIXME: Some targets apparently require local symbols in the
1204 // dynamic symbol table. Here is where we will have to count them,
1205 // and set the dynamic symbol indexes, and add the names to
1208 unsigned int local_symcount = index;
1209 *plocal_dynamic_count = local_symcount;
1211 // FIXME: We have to tell set_dynsym_indexes whether the
1212 // -E/--export-dynamic option was used.
1213 index = symtab->set_dynsym_indexes(target, index, pdynamic_symbols,
1214 &this->dynpool_, pversions);
1218 const int size = parameters->get_size();
1221 symsize = elfcpp::Elf_sizes<32>::sym_size;
1224 else if (size == 64)
1226 symsize = elfcpp::Elf_sizes<64>::sym_size;
1232 // Create the dynamic symbol table section.
1234 const char* dynsym_name = this->namepool_.add(".dynsym", false, NULL);
1235 Output_section* dynsym = this->make_output_section(dynsym_name,
1239 Output_section_data* odata = new Output_data_space(index * symsize,
1241 dynsym->add_output_section_data(odata);
1243 dynsym->set_info(local_symcount);
1244 dynsym->set_entsize(symsize);
1245 dynsym->set_addralign(align);
1247 this->dynsym_section_ = dynsym;
1249 Output_data_dynamic* const odyn = this->dynamic_data_;
1250 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1251 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1253 // Create the dynamic string table section.
1255 const char* dynstr_name = this->namepool_.add(".dynstr", false, NULL);
1256 Output_section* dynstr = this->make_output_section(dynstr_name,
1260 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1261 dynstr->add_output_section_data(strdata);
1263 dynsym->set_link_section(dynstr);
1264 this->dynamic_section_->set_link_section(dynstr);
1266 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1267 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1271 // Create the hash tables.
1273 // FIXME: We need an option to create a GNU hash table.
1275 unsigned char* phash;
1276 unsigned int hashlen;
1277 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1280 const char* hash_name = this->namepool_.add(".hash", false, NULL);
1281 Output_section* hashsec = this->make_output_section(hash_name,
1285 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1288 hashsec->add_output_section_data(hashdata);
1290 hashsec->set_link_section(dynsym);
1291 hashsec->set_entsize(4);
1293 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1296 // Create the version sections.
1299 Layout::create_version_sections(const Versions* versions,
1300 const Symbol_table* symtab,
1301 unsigned int local_symcount,
1302 const std::vector<Symbol*>& dynamic_symbols,
1303 const Output_section* dynstr)
1305 if (!versions->any_defs() && !versions->any_needs())
1308 if (parameters->get_size() == 32)
1310 if (parameters->is_big_endian())
1312 #ifdef HAVE_TARGET_32_BIG
1313 this->sized_create_version_sections
1314 SELECT_SIZE_ENDIAN_NAME(32, true)(
1315 versions, symtab, local_symcount, dynamic_symbols, dynstr
1316 SELECT_SIZE_ENDIAN(32, true));
1323 #ifdef HAVE_TARGET_32_LITTLE
1324 this->sized_create_version_sections
1325 SELECT_SIZE_ENDIAN_NAME(32, false)(
1326 versions, symtab, local_symcount, dynamic_symbols, dynstr
1327 SELECT_SIZE_ENDIAN(32, false));
1333 else if (parameters->get_size() == 64)
1335 if (parameters->is_big_endian())
1337 #ifdef HAVE_TARGET_64_BIG
1338 this->sized_create_version_sections
1339 SELECT_SIZE_ENDIAN_NAME(64, true)(
1340 versions, symtab, local_symcount, dynamic_symbols, dynstr
1341 SELECT_SIZE_ENDIAN(64, true));
1348 #ifdef HAVE_TARGET_64_LITTLE
1349 this->sized_create_version_sections
1350 SELECT_SIZE_ENDIAN_NAME(64, false)(
1351 versions, symtab, local_symcount, dynamic_symbols, dynstr
1352 SELECT_SIZE_ENDIAN(64, false));
1362 // Create the version sections, sized version.
1364 template<int size, bool big_endian>
1366 Layout::sized_create_version_sections(
1367 const Versions* versions,
1368 const Symbol_table* symtab,
1369 unsigned int local_symcount,
1370 const std::vector<Symbol*>& dynamic_symbols,
1371 const Output_section* dynstr
1374 const char* vname = this->namepool_.add(".gnu.version", false, NULL);
1375 Output_section* vsec = this->make_output_section(vname,
1376 elfcpp::SHT_GNU_versym,
1379 unsigned char* vbuf;
1381 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1382 symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1383 SELECT_SIZE_ENDIAN(size, big_endian));
1385 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1387 vsec->add_output_section_data(vdata);
1388 vsec->set_entsize(2);
1389 vsec->set_link_section(this->dynsym_section_);
1391 Output_data_dynamic* const odyn = this->dynamic_data_;
1392 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1394 if (versions->any_defs())
1396 const char* vdname = this->namepool_.add(".gnu.version_d", false, NULL);
1397 Output_section *vdsec;
1398 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1401 unsigned char* vdbuf;
1402 unsigned int vdsize;
1403 unsigned int vdentries;
1404 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1405 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1406 SELECT_SIZE_ENDIAN(size, big_endian));
1408 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1412 vdsec->add_output_section_data(vddata);
1413 vdsec->set_link_section(dynstr);
1414 vdsec->set_info(vdentries);
1416 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1417 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1420 if (versions->any_needs())
1422 const char* vnname = this->namepool_.add(".gnu.version_r", false, NULL);
1423 Output_section* vnsec;
1424 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1427 unsigned char* vnbuf;
1428 unsigned int vnsize;
1429 unsigned int vnentries;
1430 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1431 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1432 SELECT_SIZE_ENDIAN(size, big_endian));
1434 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1438 vnsec->add_output_section_data(vndata);
1439 vnsec->set_link_section(dynstr);
1440 vnsec->set_info(vnentries);
1442 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1443 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1447 // Create the .interp section and PT_INTERP segment.
1450 Layout::create_interp(const Target* target)
1452 const char* interp = this->options_.dynamic_linker();
1455 interp = target->dynamic_linker();
1456 gold_assert(interp != NULL);
1459 size_t len = strlen(interp) + 1;
1461 Output_section_data* odata = new Output_data_const(interp, len, 1);
1463 const char* interp_name = this->namepool_.add(".interp", false, NULL);
1464 Output_section* osec = this->make_output_section(interp_name,
1465 elfcpp::SHT_PROGBITS,
1467 osec->add_output_section_data(odata);
1469 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1470 this->segment_list_.push_back(oseg);
1471 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1474 // Finish the .dynamic section and PT_DYNAMIC segment.
1477 Layout::finish_dynamic_section(const Input_objects* input_objects,
1478 const Symbol_table* symtab)
1480 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1481 elfcpp::PF_R | elfcpp::PF_W);
1482 this->segment_list_.push_back(oseg);
1483 oseg->add_initial_output_section(this->dynamic_section_,
1484 elfcpp::PF_R | elfcpp::PF_W);
1486 Output_data_dynamic* const odyn = this->dynamic_data_;
1488 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1489 p != input_objects->dynobj_end();
1492 // FIXME: Handle --as-needed.
1493 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1496 // FIXME: Support --init and --fini.
1497 Symbol* sym = symtab->lookup("_init");
1498 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1499 odyn->add_symbol(elfcpp::DT_INIT, sym);
1501 sym = symtab->lookup("_fini");
1502 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1503 odyn->add_symbol(elfcpp::DT_FINI, sym);
1505 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1507 // Add a DT_RPATH entry if needed.
1508 const General_options::Dir_list& rpath(this->options_.rpath());
1511 std::string rpath_val;
1512 for (General_options::Dir_list::const_iterator p = rpath.begin();
1516 if (rpath_val.empty())
1517 rpath_val = p->name();
1520 // Eliminate duplicates.
1521 General_options::Dir_list::const_iterator q;
1522 for (q = rpath.begin(); q != p; ++q)
1523 if (q->name() == p->name())
1528 rpath_val += p->name();
1533 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1537 // The mapping of .gnu.linkonce section names to real section names.
1539 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1540 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1542 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1543 MAPPING_INIT("t", ".text"),
1544 MAPPING_INIT("r", ".rodata"),
1545 MAPPING_INIT("d", ".data"),
1546 MAPPING_INIT("b", ".bss"),
1547 MAPPING_INIT("s", ".sdata"),
1548 MAPPING_INIT("sb", ".sbss"),
1549 MAPPING_INIT("s2", ".sdata2"),
1550 MAPPING_INIT("sb2", ".sbss2"),
1551 MAPPING_INIT("wi", ".debug_info"),
1552 MAPPING_INIT("td", ".tdata"),
1553 MAPPING_INIT("tb", ".tbss"),
1554 MAPPING_INIT("lr", ".lrodata"),
1555 MAPPING_INIT("l", ".ldata"),
1556 MAPPING_INIT("lb", ".lbss"),
1560 const int Layout::linkonce_mapping_count =
1561 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1563 // Return the name of the output section to use for a .gnu.linkonce
1564 // section. This is based on the default ELF linker script of the old
1565 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1566 // to ".text". Set *PLEN to the length of the name. *PLEN is
1567 // initialized to the length of NAME.
1570 Layout::linkonce_output_name(const char* name, size_t *plen)
1572 const char* s = name + sizeof(".gnu.linkonce") - 1;
1576 const Linkonce_mapping* plm = linkonce_mapping;
1577 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1579 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1588 // Choose the output section name to use given an input section name.
1589 // Set *PLEN to the length of the name. *PLEN is initialized to the
1593 Layout::output_section_name(const char* name, size_t* plen)
1595 if (Layout::is_linkonce(name))
1597 // .gnu.linkonce sections are laid out as though they were named
1598 // for the sections are placed into.
1599 return Layout::linkonce_output_name(name, plen);
1602 // gcc 4.3 generates the following sorts of section names when it
1603 // needs a section name specific to a function:
1609 // .data.rel.local.FN
1611 // .data.rel.ro.local.FN
1618 // The GNU linker maps all of those to the part before the .FN,
1619 // except that .data.rel.local.FN is mapped to .data, and
1620 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
1621 // beginning with .data.rel.ro.local are grouped together.
1623 // For an anonymous namespace, the string FN can contain a '.'.
1625 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
1626 // GNU linker maps to .rodata.
1628 // The .data.rel.ro sections enable a security feature triggered by
1629 // the -z relro option. Section which need to be relocated at
1630 // program startup time but which may be readonly after startup are
1631 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
1632 // segment. The dynamic linker will make that segment writable,
1633 // perform relocations, and then make it read-only. FIXME: We do
1634 // not yet implement this optimization.
1636 // It is hard to handle this in a principled way.
1638 // These are the rules we follow:
1640 // If the section name has no initial '.', or no dot other than an
1641 // initial '.', we use the name unchanged (i.e., "mysection" and
1642 // ".text" are unchanged).
1644 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
1646 // Otherwise, we drop the second '.' and everything that comes after
1647 // it (i.e., ".text.XXX" becomes ".text").
1649 const char* s = name;
1653 const char* sdot = strchr(s, '.');
1657 const char* const data_rel_ro = ".data.rel.ro";
1658 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
1660 *plen = strlen(data_rel_ro);
1664 *plen = sdot - name;
1668 // Record the signature of a comdat section, and return whether to
1669 // include it in the link. If GROUP is true, this is a regular
1670 // section group. If GROUP is false, this is a group signature
1671 // derived from the name of a linkonce section. We want linkonce
1672 // signatures and group signatures to block each other, but we don't
1673 // want a linkonce signature to block another linkonce signature.
1676 Layout::add_comdat(const char* signature, bool group)
1678 std::string sig(signature);
1679 std::pair<Signatures::iterator, bool> ins(
1680 this->signatures_.insert(std::make_pair(sig, group)));
1684 // This is the first time we've seen this signature.
1688 if (ins.first->second)
1690 // We've already seen a real section group with this signature.
1695 // This is a real section group, and we've already seen a
1696 // linkonce section with this signature. Record that we've seen
1697 // a section group, and don't include this section group.
1698 ins.first->second = true;
1703 // We've already seen a linkonce section and this is a linkonce
1704 // section. These don't block each other--this may be the same
1705 // symbol name with different section types.
1710 // Write out data not associated with a section or the symbol table.
1713 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
1715 if (!parameters->strip_all())
1717 const Output_section* symtab_section = this->symtab_section_;
1718 for (Section_list::const_iterator p = this->section_list_.begin();
1719 p != this->section_list_.end();
1722 if ((*p)->needs_symtab_index())
1724 gold_assert(symtab_section != NULL);
1725 unsigned int index = (*p)->symtab_index();
1726 gold_assert(index > 0 && index != -1U);
1727 off_t off = (symtab_section->offset()
1728 + index * symtab_section->entsize());
1729 symtab->write_section_symbol(*p, of, off);
1734 const Output_section* dynsym_section = this->dynsym_section_;
1735 for (Section_list::const_iterator p = this->section_list_.begin();
1736 p != this->section_list_.end();
1739 if ((*p)->needs_dynsym_index())
1741 gold_assert(dynsym_section != NULL);
1742 unsigned int index = (*p)->dynsym_index();
1743 gold_assert(index > 0 && index != -1U);
1744 off_t off = (dynsym_section->offset()
1745 + index * dynsym_section->entsize());
1746 symtab->write_section_symbol(*p, of, off);
1750 // Write out the Output_sections. Most won't have anything to
1751 // write, since most of the data will come from input sections which
1752 // are handled elsewhere. But some Output_sections do have
1754 for (Section_list::const_iterator p = this->section_list_.begin();
1755 p != this->section_list_.end();
1759 // Write out the Output_data which are not in an Output_section.
1760 for (Data_list::const_iterator p = this->special_output_list_.begin();
1761 p != this->special_output_list_.end();
1766 // Write_data_task methods.
1768 // We can always run this task.
1770 Task::Is_runnable_type
1771 Write_data_task::is_runnable(Workqueue*)
1776 // We need to unlock FINAL_BLOCKER when finished.
1779 Write_data_task::locks(Workqueue* workqueue)
1781 return new Task_locker_block(*this->final_blocker_, workqueue);
1784 // Run the task--write out the data.
1787 Write_data_task::run(Workqueue*)
1789 this->layout_->write_data(this->symtab_, this->of_);
1792 // Write_symbols_task methods.
1794 // We can always run this task.
1796 Task::Is_runnable_type
1797 Write_symbols_task::is_runnable(Workqueue*)
1802 // We need to unlock FINAL_BLOCKER when finished.
1805 Write_symbols_task::locks(Workqueue* workqueue)
1807 return new Task_locker_block(*this->final_blocker_, workqueue);
1810 // Run the task--write out the symbols.
1813 Write_symbols_task::run(Workqueue*)
1815 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1819 // Close_task_runner methods.
1821 // Run the task--close the file.
1824 Close_task_runner::run(Workqueue*)
1829 // Instantiate the templates we need. We could use the configure
1830 // script to restrict this to only the ones for implemented targets.
1832 #ifdef HAVE_TARGET_32_LITTLE
1835 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1836 const elfcpp::Shdr<32, false>& shdr, off_t*);
1839 #ifdef HAVE_TARGET_32_BIG
1842 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1843 const elfcpp::Shdr<32, true>& shdr, off_t*);
1846 #ifdef HAVE_TARGET_64_LITTLE
1849 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1850 const elfcpp::Shdr<64, false>& shdr, off_t*);
1853 #ifdef HAVE_TARGET_64_BIG
1856 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1857 const elfcpp::Shdr<64, true>& shdr, off_t*);
1861 } // End namespace gold.