opcodes/
[binutils.git] / gold / layout.cc
blobf9f55482ff44f0e2e8aab1667d11966e36637d54
1 // layout.cc -- lay out output file sections for gold
3 #include "gold.h"
5 #include <cassert>
6 #include <cstring>
7 #include <algorithm>
8 #include <iostream>
9 #include <utility>
11 #include "output.h"
12 #include "symtab.h"
13 #include "layout.h"
15 namespace gold
18 // Layout_task_runner methods.
20 // Lay out the sections. This is called after all the input objects
21 // have been read.
23 void
24 Layout_task_runner::run(Workqueue* workqueue)
26 off_t file_size = this->layout_->finalize(this->input_objects_,
27 this->symtab_);
29 // Now we know the final size of the output file and we know where
30 // each piece of information goes.
31 Output_file* of = new Output_file(this->options_);
32 of->open(file_size);
34 // Queue up the final set of tasks.
35 gold::queue_final_tasks(this->options_, this->input_objects_,
36 this->symtab_, this->layout_, workqueue, of);
39 // Layout methods.
41 Layout::Layout(const General_options& options)
42 : options_(options), namepool_(), sympool_(), signatures_(),
43 section_name_map_(), segment_list_(), section_list_(),
44 special_output_list_(), tls_segment_(NULL)
46 // Make space for more than enough segments for a typical file.
47 // This is just for efficiency--it's OK if we wind up needing more.
48 segment_list_.reserve(12);
51 // Hash a key we use to look up an output section mapping.
53 size_t
54 Layout::Hash_key::operator()(const Layout::Key& k) const
56 return k.first + k.second.first + k.second.second;
59 // Whether to include this section in the link.
61 template<int size, bool big_endian>
62 bool
63 Layout::include_section(Object*, const char*,
64 const elfcpp::Shdr<size, big_endian>& shdr)
66 // Some section types are never linked. Some are only linked when
67 // doing a relocateable link.
68 switch (shdr.get_sh_type())
70 case elfcpp::SHT_NULL:
71 case elfcpp::SHT_SYMTAB:
72 case elfcpp::SHT_DYNSYM:
73 case elfcpp::SHT_STRTAB:
74 case elfcpp::SHT_HASH:
75 case elfcpp::SHT_DYNAMIC:
76 case elfcpp::SHT_SYMTAB_SHNDX:
77 return false;
79 case elfcpp::SHT_RELA:
80 case elfcpp::SHT_REL:
81 case elfcpp::SHT_GROUP:
82 return this->options_.is_relocatable();
84 default:
85 // FIXME: Handle stripping debug sections here.
86 return true;
90 // Return an output section named NAME, or NULL if there is none.
92 Output_section*
93 Layout::find_output_section(const char* name) const
95 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
96 p != this->section_name_map_.end();
97 ++p)
98 if (strcmp(p->second->name(), name) == 0)
99 return p->second;
100 return NULL;
103 // Return an output segment of type TYPE, with segment flags SET set
104 // and segment flags CLEAR clear. Return NULL if there is none.
106 Output_segment*
107 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
108 elfcpp::Elf_Word clear) const
110 for (Segment_list::const_iterator p = this->segment_list_.begin();
111 p != this->segment_list_.end();
112 ++p)
113 if (static_cast<elfcpp::PT>((*p)->type()) == type
114 && ((*p)->flags() & set) == set
115 && ((*p)->flags() & clear) == 0)
116 return *p;
117 return NULL;
120 // Return the output section to use for section NAME with type TYPE
121 // and section flags FLAGS.
123 Output_section*
124 Layout::get_output_section(const char* name, Stringpool::Key name_key,
125 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
127 // We should ignore some flags.
128 flags &= ~ (elfcpp::SHF_INFO_LINK
129 | elfcpp::SHF_LINK_ORDER
130 | elfcpp::SHF_GROUP);
132 const Key key(name_key, std::make_pair(type, flags));
133 const std::pair<Key, Output_section*> v(key, NULL);
134 std::pair<Section_name_map::iterator, bool> ins(
135 this->section_name_map_.insert(v));
137 if (!ins.second)
138 return ins.first->second;
139 else
141 // This is the first time we've seen this name/type/flags
142 // combination.
143 Output_section* os = this->make_output_section(name, type, flags);
144 ins.first->second = os;
145 return os;
149 // Return the output section to use for input section SHNDX, with name
150 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
151 // offset of this input section without the output section.
153 template<int size, bool big_endian>
154 Output_section*
155 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
156 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
158 if (!this->include_section(object, name, shdr))
159 return NULL;
161 // If we are not doing a relocateable link, choose the name to use
162 // for the output section.
163 size_t len = strlen(name);
164 if (!this->options_.is_relocatable())
165 name = Layout::output_section_name(name, &len);
167 // FIXME: Handle SHF_OS_NONCONFORMING here.
169 // Canonicalize the section name.
170 Stringpool::Key name_key;
171 name = this->namepool_.add(name, len, &name_key);
173 // Find the output section. The output section is selected based on
174 // the section name, type, and flags.
175 Output_section* os = this->get_output_section(name, name_key,
176 shdr.get_sh_type(),
177 shdr.get_sh_flags());
179 // FIXME: Handle SHF_LINK_ORDER somewhere.
181 *off = os->add_input_section(object, shndx, name, shdr);
183 return os;
186 // Add POSD to an output section using NAME, TYPE, and FLAGS.
188 void
189 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
190 elfcpp::Elf_Xword flags,
191 Output_section_data* posd)
193 // Canonicalize the name.
194 Stringpool::Key name_key;
195 name = this->namepool_.add(name, &name_key);
197 Output_section* os = this->get_output_section(name, name_key, type, flags);
198 os->add_output_section_data(posd);
201 // Map section flags to segment flags.
203 elfcpp::Elf_Word
204 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
206 elfcpp::Elf_Word ret = elfcpp::PF_R;
207 if ((flags & elfcpp::SHF_WRITE) != 0)
208 ret |= elfcpp::PF_W;
209 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
210 ret |= elfcpp::PF_X;
211 return ret;
214 // Make a new Output_section, and attach it to segments as
215 // appropriate.
217 Output_section*
218 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
219 elfcpp::Elf_Xword flags)
221 Output_section* os = new Output_section(name, type, flags, true);
223 if ((flags & elfcpp::SHF_ALLOC) == 0)
224 this->section_list_.push_back(os);
225 else
227 // This output section goes into a PT_LOAD segment.
229 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
231 // The only thing we really care about for PT_LOAD segments is
232 // whether or not they are writable, so that is how we search
233 // for them. People who need segments sorted on some other
234 // basis will have to wait until we implement a mechanism for
235 // them to describe the segments they want.
237 Segment_list::const_iterator p;
238 for (p = this->segment_list_.begin();
239 p != this->segment_list_.end();
240 ++p)
242 if ((*p)->type() == elfcpp::PT_LOAD
243 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
245 (*p)->add_output_section(os, seg_flags);
246 break;
250 if (p == this->segment_list_.end())
252 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
253 seg_flags);
254 this->segment_list_.push_back(oseg);
255 oseg->add_output_section(os, seg_flags);
258 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
259 // segment.
260 if (type == elfcpp::SHT_NOTE)
262 // See if we already have an equivalent PT_NOTE segment.
263 for (p = this->segment_list_.begin();
264 p != segment_list_.end();
265 ++p)
267 if ((*p)->type() == elfcpp::PT_NOTE
268 && (((*p)->flags() & elfcpp::PF_W)
269 == (seg_flags & elfcpp::PF_W)))
271 (*p)->add_output_section(os, seg_flags);
272 break;
276 if (p == this->segment_list_.end())
278 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
279 seg_flags);
280 this->segment_list_.push_back(oseg);
281 oseg->add_output_section(os, seg_flags);
285 // If we see a loadable SHF_TLS section, we create a PT_TLS
286 // segment. There can only be one such segment.
287 if ((flags & elfcpp::SHF_TLS) != 0)
289 if (this->tls_segment_ == NULL)
291 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
292 seg_flags);
293 this->segment_list_.push_back(this->tls_segment_);
295 this->tls_segment_->add_output_section(os, seg_flags);
299 return os;
302 // Find the first read-only PT_LOAD segment, creating one if
303 // necessary.
305 Output_segment*
306 Layout::find_first_load_seg()
308 for (Segment_list::const_iterator p = this->segment_list_.begin();
309 p != this->segment_list_.end();
310 ++p)
312 if ((*p)->type() == elfcpp::PT_LOAD
313 && ((*p)->flags() & elfcpp::PF_R) != 0
314 && ((*p)->flags() & elfcpp::PF_W) == 0)
315 return *p;
318 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
319 this->segment_list_.push_back(load_seg);
320 return load_seg;
323 // Finalize the layout. When this is called, we have created all the
324 // output sections and all the output segments which are based on
325 // input sections. We have several things to do, and we have to do
326 // them in the right order, so that we get the right results correctly
327 // and efficiently.
329 // 1) Finalize the list of output segments and create the segment
330 // table header.
332 // 2) Finalize the dynamic symbol table and associated sections.
334 // 3) Determine the final file offset of all the output segments.
336 // 4) Determine the final file offset of all the SHF_ALLOC output
337 // sections.
339 // 5) Create the symbol table sections and the section name table
340 // section.
342 // 6) Finalize the symbol table: set symbol values to their final
343 // value and make a final determination of which symbols are going
344 // into the output symbol table.
346 // 7) Create the section table header.
348 // 8) Determine the final file offset of all the output sections which
349 // are not SHF_ALLOC, including the section table header.
351 // 9) Finalize the ELF file header.
353 // This function returns the size of the output file.
355 off_t
356 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
358 const int size = input_objects->target()->get_size();
360 Output_segment* phdr_seg = NULL;
361 if (input_objects->any_dynamic())
363 // There was a dynamic object in the link. We need to create
364 // some information for the dynamic linker.
366 // Create the PT_PHDR segment which will hold the program
367 // headers.
368 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
369 this->segment_list_.push_back(phdr_seg);
371 // Create the dynamic symbol table, including the hash table,
372 // the dynamic relocations, and the version sections.
373 this->create_dynamic_symtab(size, symtab);
375 // Create the .dynamic section to hold the dynamic data, and put
376 // it in a PT_DYNAMIC segment.
377 this->create_dynamic_section();
379 // Create the .interp section to hold the name of the
380 // interpreter, and put it in a PT_INTERP segment.
381 this->create_interp(input_objects->target());
384 // FIXME: Handle PT_GNU_STACK.
386 Output_segment* load_seg = this->find_first_load_seg();
388 // Lay out the segment headers.
389 bool big_endian = input_objects->target()->is_big_endian();
390 Output_segment_headers* segment_headers;
391 segment_headers = new Output_segment_headers(size, big_endian,
392 this->segment_list_);
393 load_seg->add_initial_output_data(segment_headers);
394 this->special_output_list_.push_back(segment_headers);
395 if (phdr_seg != NULL)
396 phdr_seg->add_initial_output_data(segment_headers);
398 // Lay out the file header.
399 Output_file_header* file_header;
400 file_header = new Output_file_header(size,
401 big_endian,
402 this->options_,
403 input_objects->target(),
404 symtab,
405 segment_headers);
406 load_seg->add_initial_output_data(file_header);
407 this->special_output_list_.push_back(file_header);
409 // We set the output section indexes in set_segment_offsets and
410 // set_section_offsets.
411 unsigned int shndx = 1;
413 // Set the file offsets of all the segments, and all the sections
414 // they contain.
415 off_t off = this->set_segment_offsets(input_objects->target(), load_seg,
416 &shndx);
418 // Create the symbol table sections.
419 // FIXME: We don't need to do this if we are stripping symbols.
420 Output_section* osymtab;
421 Output_section* ostrtab;
422 this->create_symtab_sections(size, input_objects, symtab, &off,
423 &osymtab, &ostrtab);
425 // Create the .shstrtab section.
426 Output_section* shstrtab_section = this->create_shstrtab();
428 // Set the file offsets of all the sections not associated with
429 // segments.
430 off = this->set_section_offsets(off, &shndx);
432 // Now the section index of OSTRTAB is set.
433 osymtab->set_link(ostrtab->out_shndx());
435 // Create the section table header.
436 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
438 file_header->set_section_info(oshdrs, shstrtab_section);
440 // Now we know exactly where everything goes in the output file.
442 return off;
445 // Return whether SEG1 should be before SEG2 in the output file. This
446 // is based entirely on the segment type and flags. When this is
447 // called the segment addresses has normally not yet been set.
449 bool
450 Layout::segment_precedes(const Output_segment* seg1,
451 const Output_segment* seg2)
453 elfcpp::Elf_Word type1 = seg1->type();
454 elfcpp::Elf_Word type2 = seg2->type();
456 // The single PT_PHDR segment is required to precede any loadable
457 // segment. We simply make it always first.
458 if (type1 == elfcpp::PT_PHDR)
460 assert(type2 != elfcpp::PT_PHDR);
461 return true;
463 if (type2 == elfcpp::PT_PHDR)
464 return false;
466 // The single PT_INTERP segment is required to precede any loadable
467 // segment. We simply make it always second.
468 if (type1 == elfcpp::PT_INTERP)
470 assert(type2 != elfcpp::PT_INTERP);
471 return true;
473 if (type2 == elfcpp::PT_INTERP)
474 return false;
476 // We then put PT_LOAD segments before any other segments.
477 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
478 return true;
479 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
480 return false;
482 // We put the PT_TLS segment last, because that is where the dynamic
483 // linker expects to find it (this is just for efficiency; other
484 // positions would also work correctly).
485 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
486 return false;
487 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
488 return true;
490 const elfcpp::Elf_Word flags1 = seg1->flags();
491 const elfcpp::Elf_Word flags2 = seg2->flags();
493 // The order of non-PT_LOAD segments is unimportant. We simply sort
494 // by the numeric segment type and flags values. There should not
495 // be more than one segment with the same type and flags.
496 if (type1 != elfcpp::PT_LOAD)
498 if (type1 != type2)
499 return type1 < type2;
500 assert(flags1 != flags2);
501 return flags1 < flags2;
504 // We sort PT_LOAD segments based on the flags. Readonly segments
505 // come before writable segments. Then executable segments come
506 // before non-executable segments. Then the unlikely case of a
507 // non-readable segment comes before the normal case of a readable
508 // segment. If there are multiple segments with the same type and
509 // flags, we require that the address be set, and we sort by
510 // virtual address and then physical address.
511 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
512 return (flags1 & elfcpp::PF_W) == 0;
513 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
514 return (flags1 & elfcpp::PF_X) != 0;
515 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
516 return (flags1 & elfcpp::PF_R) == 0;
518 uint64_t vaddr1 = seg1->vaddr();
519 uint64_t vaddr2 = seg2->vaddr();
520 if (vaddr1 != vaddr2)
521 return vaddr1 < vaddr2;
523 uint64_t paddr1 = seg1->paddr();
524 uint64_t paddr2 = seg2->paddr();
525 assert(paddr1 != paddr2);
526 return paddr1 < paddr2;
529 // Set the file offsets of all the segments, and all the sections they
530 // contain. They have all been created. LOAD_SEG must be be laid out
531 // first. Return the offset of the data to follow.
533 off_t
534 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
535 unsigned int *pshndx)
537 // Sort them into the final order.
538 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
539 Layout::Compare_segments());
541 // Find the PT_LOAD segments, and set their addresses and offsets
542 // and their section's addresses and offsets.
543 uint64_t addr = target->text_segment_address();
544 off_t off = 0;
545 bool was_readonly = false;
546 for (Segment_list::iterator p = this->segment_list_.begin();
547 p != this->segment_list_.end();
548 ++p)
550 if ((*p)->type() == elfcpp::PT_LOAD)
552 if (load_seg != NULL && load_seg != *p)
553 abort();
554 load_seg = NULL;
556 // If the last segment was readonly, and this one is not,
557 // then skip the address forward one page, maintaining the
558 // same position within the page. This lets us store both
559 // segments overlapping on a single page in the file, but
560 // the loader will put them on different pages in memory.
562 uint64_t orig_addr = addr;
563 uint64_t orig_off = off;
565 uint64_t aligned_addr = addr;
566 uint64_t abi_pagesize = target->abi_pagesize();
567 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
569 uint64_t align = (*p)->addralign();
571 addr = align_address(addr, align);
572 aligned_addr = addr;
573 if ((addr & (abi_pagesize - 1)) != 0)
574 addr = addr + abi_pagesize;
577 unsigned int shndx_hold = *pshndx;
578 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
579 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
581 // Now that we know the size of this segment, we may be able
582 // to save a page in memory, at the cost of wasting some
583 // file space, by instead aligning to the start of a new
584 // page. Here we use the real machine page size rather than
585 // the ABI mandated page size.
587 if (aligned_addr != addr)
589 uint64_t common_pagesize = target->common_pagesize();
590 uint64_t first_off = (common_pagesize
591 - (aligned_addr
592 & (common_pagesize - 1)));
593 uint64_t last_off = new_addr & (common_pagesize - 1);
594 if (first_off > 0
595 && last_off > 0
596 && ((aligned_addr & ~ (common_pagesize - 1))
597 != (new_addr & ~ (common_pagesize - 1)))
598 && first_off + last_off <= common_pagesize)
600 *pshndx = shndx_hold;
601 addr = align_address(aligned_addr, common_pagesize);
602 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
603 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
607 addr = new_addr;
609 if (((*p)->flags() & elfcpp::PF_W) == 0)
610 was_readonly = true;
614 // Handle the non-PT_LOAD segments, setting their offsets from their
615 // section's offsets.
616 for (Segment_list::iterator p = this->segment_list_.begin();
617 p != this->segment_list_.end();
618 ++p)
620 if ((*p)->type() != elfcpp::PT_LOAD)
621 (*p)->set_offset();
624 return off;
627 // Set the file offset of all the sections not associated with a
628 // segment.
630 off_t
631 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
633 for (Layout::Section_list::iterator p = this->section_list_.begin();
634 p != this->section_list_.end();
635 ++p)
637 (*p)->set_out_shndx(*pshndx);
638 ++*pshndx;
639 if ((*p)->offset() != -1)
640 continue;
641 off = align_address(off, (*p)->addralign());
642 (*p)->set_address(0, off);
643 off += (*p)->data_size();
645 return off;
648 // Create the symbol table sections.
650 void
651 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
652 Symbol_table* symtab,
653 off_t* poff,
654 Output_section** posymtab,
655 Output_section** postrtab)
657 int symsize;
658 unsigned int align;
659 if (size == 32)
661 symsize = elfcpp::Elf_sizes<32>::sym_size;
662 align = 4;
664 else if (size == 64)
666 symsize = elfcpp::Elf_sizes<64>::sym_size;
667 align = 8;
669 else
670 abort();
672 off_t off = *poff;
673 off = align_address(off, align);
674 off_t startoff = off;
676 // Save space for the dummy symbol at the start of the section. We
677 // never bother to write this out--it will just be left as zero.
678 off += symsize;
679 unsigned int local_symbol_index = 1;
681 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
682 p != input_objects->relobj_end();
683 ++p)
685 Task_lock_obj<Object> tlo(**p);
686 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
687 off,
688 &this->sympool_);
689 off += (index - local_symbol_index) * symsize;
690 local_symbol_index = index;
693 unsigned int local_symcount = local_symbol_index;
694 assert(local_symcount * symsize == off - startoff);
696 off = symtab->finalize(local_symcount, off, &this->sympool_);
698 this->sympool_.set_string_offsets();
700 const char* symtab_name = this->namepool_.add(".symtab", NULL);
701 Output_section* osymtab = new Output_section_symtab(symtab_name,
702 off - startoff);
703 this->section_list_.push_back(osymtab);
705 const char* strtab_name = this->namepool_.add(".strtab", NULL);
706 Output_section *ostrtab = new Output_section_strtab(strtab_name,
707 &this->sympool_);
708 this->section_list_.push_back(ostrtab);
709 this->special_output_list_.push_back(ostrtab);
711 osymtab->set_address(0, startoff);
712 osymtab->set_info(local_symcount);
713 osymtab->set_entsize(symsize);
714 osymtab->set_addralign(align);
716 *poff = off;
717 *posymtab = osymtab;
718 *postrtab = ostrtab;
721 // Create the .shstrtab section, which holds the names of the
722 // sections. At the time this is called, we have created all the
723 // output sections except .shstrtab itself.
725 Output_section*
726 Layout::create_shstrtab()
728 // FIXME: We don't need to create a .shstrtab section if we are
729 // stripping everything.
731 const char* name = this->namepool_.add(".shstrtab", NULL);
733 this->namepool_.set_string_offsets();
735 Output_section* os = new Output_section_strtab(name, &this->namepool_);
737 this->section_list_.push_back(os);
738 this->special_output_list_.push_back(os);
740 return os;
743 // Create the section headers. SIZE is 32 or 64. OFF is the file
744 // offset.
746 Output_section_headers*
747 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
749 Output_section_headers* oshdrs;
750 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
751 this->section_list_,
752 &this->namepool_);
753 off_t off = align_address(*poff, oshdrs->addralign());
754 oshdrs->set_address(0, off);
755 off += oshdrs->data_size();
756 *poff = off;
757 this->special_output_list_.push_back(oshdrs);
758 return oshdrs;
761 // Create the dynamic symbol table.
763 void
764 Layout::create_dynamic_symtab(int, Symbol_table*)
766 abort();
769 // Create the .dynamic section and PT_DYNAMIC segment.
771 void
772 Layout::create_dynamic_section()
774 abort();
777 // Create the .interp section and PT_INTERP segment.
779 void
780 Layout::create_interp(const Target* target)
782 const char* interp = this->options_.dynamic_linker();
783 if (interp == NULL)
785 interp = target->dynamic_linker();
786 assert(interp != NULL);
789 size_t len = strlen(interp) + 1;
791 Output_section_data* odata = new Output_data_const(interp, len, 1);
793 const char* interp_name = this->namepool_.add(".interp", NULL);
794 Output_section* osec = this->make_output_section(interp_name,
795 elfcpp::SHT_PROGBITS,
796 elfcpp::SHF_ALLOC);
797 osec->add_output_section_data(odata);
799 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
800 this->segment_list_.push_back(oseg);
801 oseg->add_initial_output_section(osec, elfcpp::PF_R);
804 // The mapping of .gnu.linkonce section names to real section names.
806 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
807 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
809 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
810 MAPPING_INIT("t", ".text"),
811 MAPPING_INIT("r", ".rodata"),
812 MAPPING_INIT("d", ".data"),
813 MAPPING_INIT("b", ".bss"),
814 MAPPING_INIT("s", ".sdata"),
815 MAPPING_INIT("sb", ".sbss"),
816 MAPPING_INIT("s2", ".sdata2"),
817 MAPPING_INIT("sb2", ".sbss2"),
818 MAPPING_INIT("wi", ".debug_info"),
819 MAPPING_INIT("td", ".tdata"),
820 MAPPING_INIT("tb", ".tbss"),
821 MAPPING_INIT("lr", ".lrodata"),
822 MAPPING_INIT("l", ".ldata"),
823 MAPPING_INIT("lb", ".lbss"),
825 #undef MAPPING_INIT
827 const int Layout::linkonce_mapping_count =
828 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
830 // Return the name of the output section to use for a .gnu.linkonce
831 // section. This is based on the default ELF linker script of the old
832 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
833 // to ".text". Set *PLEN to the length of the name. *PLEN is
834 // initialized to the length of NAME.
836 const char*
837 Layout::linkonce_output_name(const char* name, size_t *plen)
839 const char* s = name + sizeof(".gnu.linkonce") - 1;
840 if (*s != '.')
841 return name;
842 ++s;
843 const Linkonce_mapping* plm = linkonce_mapping;
844 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
846 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
848 *plen = plm->tolen;
849 return plm->to;
852 return name;
855 // Choose the output section name to use given an input section name.
856 // Set *PLEN to the length of the name. *PLEN is initialized to the
857 // length of NAME.
859 const char*
860 Layout::output_section_name(const char* name, size_t* plen)
862 if (Layout::is_linkonce(name))
864 // .gnu.linkonce sections are laid out as though they were named
865 // for the sections are placed into.
866 return Layout::linkonce_output_name(name, plen);
869 // If the section name has no '.', or only an initial '.', we use
870 // the name unchanged (i.e., ".text" is unchanged).
872 // Otherwise, if the section name does not include ".rel", we drop
873 // the last '.' and everything that follows (i.e., ".text.XXX"
874 // becomes ".text").
876 // Otherwise, if the section name has zero or one '.' after the
877 // ".rel", we use the name unchanged (i.e., ".rel.text" is
878 // unchanged).
880 // Otherwise, we drop the last '.' and everything that follows
881 // (i.e., ".rel.text.XXX" becomes ".rel.text").
883 const char* s = name;
884 if (*s == '.')
885 ++s;
886 const char* sdot = strchr(s, '.');
887 if (sdot == NULL)
888 return name;
890 const char* srel = strstr(s, ".rel");
891 if (srel == NULL)
893 *plen = sdot - name;
894 return name;
897 sdot = strchr(srel + 1, '.');
898 if (sdot == NULL)
899 return name;
900 sdot = strchr(sdot + 1, '.');
901 if (sdot == NULL)
902 return name;
904 *plen = sdot - name;
905 return name;
908 // Record the signature of a comdat section, and return whether to
909 // include it in the link. If GROUP is true, this is a regular
910 // section group. If GROUP is false, this is a group signature
911 // derived from the name of a linkonce section. We want linkonce
912 // signatures and group signatures to block each other, but we don't
913 // want a linkonce signature to block another linkonce signature.
915 bool
916 Layout::add_comdat(const char* signature, bool group)
918 std::string sig(signature);
919 std::pair<Signatures::iterator, bool> ins(
920 this->signatures_.insert(std::make_pair(sig, group)));
922 if (ins.second)
924 // This is the first time we've seen this signature.
925 return true;
928 if (ins.first->second)
930 // We've already seen a real section group with this signature.
931 return false;
933 else if (group)
935 // This is a real section group, and we've already seen a
936 // linkonce section with tihs signature. Record that we've seen
937 // a section group, and don't include this section group.
938 ins.first->second = true;
939 return false;
941 else
943 // We've already seen a linkonce section and this is a linkonce
944 // section. These don't block each other--this may be the same
945 // symbol name with different section types.
946 return true;
950 // Write out data not associated with a section or the symbol table.
952 void
953 Layout::write_data(Output_file* of) const
955 for (Data_list::const_iterator p = this->special_output_list_.begin();
956 p != this->special_output_list_.end();
957 ++p)
958 (*p)->write(of);
961 // Write_data_task methods.
963 // We can always run this task.
965 Task::Is_runnable_type
966 Write_data_task::is_runnable(Workqueue*)
968 return IS_RUNNABLE;
971 // We need to unlock FINAL_BLOCKER when finished.
973 Task_locker*
974 Write_data_task::locks(Workqueue* workqueue)
976 return new Task_locker_block(*this->final_blocker_, workqueue);
979 // Run the task--write out the data.
981 void
982 Write_data_task::run(Workqueue*)
984 this->layout_->write_data(this->of_);
987 // Write_symbols_task methods.
989 // We can always run this task.
991 Task::Is_runnable_type
992 Write_symbols_task::is_runnable(Workqueue*)
994 return IS_RUNNABLE;
997 // We need to unlock FINAL_BLOCKER when finished.
999 Task_locker*
1000 Write_symbols_task::locks(Workqueue* workqueue)
1002 return new Task_locker_block(*this->final_blocker_, workqueue);
1005 // Run the task--write out the symbols.
1007 void
1008 Write_symbols_task::run(Workqueue*)
1010 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
1013 // Close_task_runner methods.
1015 // Run the task--close the file.
1017 void
1018 Close_task_runner::run(Workqueue*)
1020 this->of_->close();
1023 // Instantiate the templates we need. We could use the configure
1024 // script to restrict this to only the ones for implemented targets.
1026 template
1027 Output_section*
1028 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1029 const elfcpp::Shdr<32, false>& shdr, off_t*);
1031 template
1032 Output_section*
1033 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1034 const elfcpp::Shdr<32, true>& shdr, off_t*);
1036 template
1037 Output_section*
1038 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1039 const elfcpp::Shdr<64, false>& shdr, off_t*);
1041 template
1042 Output_section*
1043 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1044 const elfcpp::Shdr<64, true>& shdr, off_t*);
1047 } // End namespace gold.