Update for line number changes.
[binutils.git] / gold / layout.cc
blob9e85f192a4c8df53039815943b940d0891ff3f88
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 "layout.h"
14 namespace gold
17 // Layout_task_runner methods.
19 // Lay out the sections. This is called after all the input objects
20 // have been read.
22 void
23 Layout_task_runner::run(Workqueue* workqueue)
25 off_t file_size = this->layout_->finalize(this->input_objects_,
26 this->symtab_);
28 // Now we know the final size of the output file and we know where
29 // each piece of information goes.
30 Output_file* of = new Output_file(this->options_);
31 of->open(file_size);
33 // Queue up the final set of tasks.
34 gold::queue_final_tasks(this->options_, this->input_objects_,
35 this->symtab_, this->layout_, workqueue, of);
38 // Layout methods.
40 Layout::Layout(const General_options& options)
41 : options_(options), namepool_(), sympool_(), signatures_(),
42 section_name_map_(), segment_list_(), section_list_(),
43 special_output_list_(), tls_segment_(NULL)
45 // Make space for more than enough segments for a typical file.
46 // This is just for efficiency--it's OK if we wind up needing more.
47 segment_list_.reserve(12);
50 // Hash a key we use to look up an output section mapping.
52 size_t
53 Layout::Hash_key::operator()(const Layout::Key& k) const
55 return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
58 // Whether to include this section in the link.
60 template<int size, bool big_endian>
61 bool
62 Layout::include_section(Object*, const char*,
63 const elfcpp::Shdr<size, big_endian>& shdr)
65 // Some section types are never linked. Some are only linked when
66 // doing a relocateable link.
67 switch (shdr.get_sh_type())
69 case elfcpp::SHT_NULL:
70 case elfcpp::SHT_SYMTAB:
71 case elfcpp::SHT_DYNSYM:
72 case elfcpp::SHT_STRTAB:
73 case elfcpp::SHT_HASH:
74 case elfcpp::SHT_DYNAMIC:
75 case elfcpp::SHT_SYMTAB_SHNDX:
76 return false;
78 case elfcpp::SHT_RELA:
79 case elfcpp::SHT_REL:
80 case elfcpp::SHT_GROUP:
81 return this->options_.is_relocatable();
83 default:
84 // FIXME: Handle stripping debug sections here.
85 return true;
89 // Return an output section named NAME, or NULL if there is none.
91 Output_section*
92 Layout::find_output_section(const char* name) const
94 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
95 p != this->section_name_map_.end();
96 ++p)
97 if (strcmp(p->first.first, name) == 0)
98 return p->second;
99 return NULL;
102 // Return an output segment of type TYPE, with segment flags SET set
103 // and segment flags CLEAR clear. Return NULL if there is none.
105 Output_segment*
106 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
107 elfcpp::Elf_Word clear) const
109 for (Segment_list::const_iterator p = this->segment_list_.begin();
110 p != this->segment_list_.end();
111 ++p)
112 if (static_cast<elfcpp::PT>((*p)->type()) == type
113 && ((*p)->flags() & set) == set
114 && ((*p)->flags() & clear) == 0)
115 return *p;
116 return NULL;
119 // Return the output section to use for section NAME with type TYPE
120 // and section flags FLAGS.
122 Output_section*
123 Layout::get_output_section(const char* name, elfcpp::Elf_Word type,
124 elfcpp::Elf_Xword flags)
126 // We should ignore some flags.
127 flags &= ~ (elfcpp::SHF_INFO_LINK
128 | elfcpp::SHF_LINK_ORDER
129 | elfcpp::SHF_GROUP);
131 const Key key(name, std::make_pair(type, flags));
132 const std::pair<Key, Output_section*> v(key, NULL);
133 std::pair<Section_name_map::iterator, bool> ins(
134 this->section_name_map_.insert(v));
136 if (!ins.second)
137 return ins.first->second;
138 else
140 // This is the first time we've seen this name/type/flags
141 // combination.
142 Output_section* os = this->make_output_section(name, type, flags);
143 ins.first->second = os;
144 return os;
148 // Return the output section to use for input section SHNDX, with name
149 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
150 // offset of this input section without the output section.
152 template<int size, bool big_endian>
153 Output_section*
154 Layout::layout(Object* object, unsigned int shndx, const char* name,
155 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
157 if (!this->include_section(object, name, shdr))
158 return NULL;
160 // If we are not doing a relocateable link, choose the name to use
161 // for the output section.
162 size_t len = strlen(name);
163 if (!this->options_.is_relocatable())
164 name = Layout::output_section_name(name, &len);
166 // FIXME: Handle SHF_OS_NONCONFORMING here.
168 // Canonicalize the section name.
169 name = this->namepool_.add(name, len);
171 // Find the output section. The output section is selected based on
172 // the section name, type, and flags.
173 Output_section* os = this->get_output_section(name, shdr.get_sh_type(),
174 shdr.get_sh_flags());
176 // FIXME: Handle SHF_LINK_ORDER somewhere.
178 *off = os->add_input_section(object, shndx, name, shdr);
180 return os;
183 // Add POSD to an output section using NAME, TYPE, and FLAGS.
185 void
186 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
187 elfcpp::Elf_Xword flags,
188 Output_section_data* posd)
190 // Canonicalize the name.
191 name = this->namepool_.add(name);
193 Output_section* os = this->get_output_section(name, type, flags);
194 os->add_output_section_data(posd);
197 // Map section flags to segment flags.
199 elfcpp::Elf_Word
200 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
202 elfcpp::Elf_Word ret = elfcpp::PF_R;
203 if ((flags & elfcpp::SHF_WRITE) != 0)
204 ret |= elfcpp::PF_W;
205 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
206 ret |= elfcpp::PF_X;
207 return ret;
210 // Make a new Output_section, and attach it to segments as
211 // appropriate.
213 Output_section*
214 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
215 elfcpp::Elf_Xword flags)
217 Output_section* os = new Output_section(name, type, flags, true);
219 if ((flags & elfcpp::SHF_ALLOC) == 0)
220 this->section_list_.push_back(os);
221 else
223 // This output section goes into a PT_LOAD segment.
225 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
227 // The only thing we really care about for PT_LOAD segments is
228 // whether or not they are writable, so that is how we search
229 // for them. People who need segments sorted on some other
230 // basis will have to wait until we implement a mechanism for
231 // them to describe the segments they want.
233 Segment_list::const_iterator p;
234 for (p = this->segment_list_.begin();
235 p != this->segment_list_.end();
236 ++p)
238 if ((*p)->type() == elfcpp::PT_LOAD
239 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
241 (*p)->add_output_section(os, seg_flags);
242 break;
246 if (p == this->segment_list_.end())
248 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
249 seg_flags);
250 this->segment_list_.push_back(oseg);
251 oseg->add_output_section(os, seg_flags);
254 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
255 // segment.
256 if (type == elfcpp::SHT_NOTE)
258 // See if we already have an equivalent PT_NOTE segment.
259 for (p = this->segment_list_.begin();
260 p != segment_list_.end();
261 ++p)
263 if ((*p)->type() == elfcpp::PT_NOTE
264 && (((*p)->flags() & elfcpp::PF_W)
265 == (seg_flags & elfcpp::PF_W)))
267 (*p)->add_output_section(os, seg_flags);
268 break;
272 if (p == this->segment_list_.end())
274 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
275 seg_flags);
276 this->segment_list_.push_back(oseg);
277 oseg->add_output_section(os, seg_flags);
281 // If we see a loadable SHF_TLS section, we create a PT_TLS
282 // segment. There can only be one such segment.
283 if ((flags & elfcpp::SHF_TLS) != 0)
285 if (this->tls_segment_ == NULL)
287 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
288 seg_flags);
289 this->segment_list_.push_back(this->tls_segment_);
291 this->tls_segment_->add_output_section(os, seg_flags);
295 return os;
298 // Find the first read-only PT_LOAD segment, creating one if
299 // necessary.
301 Output_segment*
302 Layout::find_first_load_seg()
304 for (Segment_list::const_iterator p = this->segment_list_.begin();
305 p != this->segment_list_.end();
306 ++p)
308 if ((*p)->type() == elfcpp::PT_LOAD
309 && ((*p)->flags() & elfcpp::PF_R) != 0
310 && ((*p)->flags() & elfcpp::PF_W) == 0)
311 return *p;
314 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
315 this->segment_list_.push_back(load_seg);
316 return load_seg;
319 // Finalize the layout. When this is called, we have created all the
320 // output sections and all the output segments which are based on
321 // input sections. We have several things to do, and we have to do
322 // them in the right order, so that we get the right results correctly
323 // and efficiently.
325 // 1) Finalize the list of output segments and create the segment
326 // table header.
328 // 2) Finalize the dynamic symbol table and associated sections.
330 // 3) Determine the final file offset of all the output segments.
332 // 4) Determine the final file offset of all the SHF_ALLOC output
333 // sections.
335 // 5) Create the symbol table sections and the section name table
336 // section.
338 // 6) Finalize the symbol table: set symbol values to their final
339 // value and make a final determination of which symbols are going
340 // into the output symbol table.
342 // 7) Create the section table header.
344 // 8) Determine the final file offset of all the output sections which
345 // are not SHF_ALLOC, including the section table header.
347 // 9) Finalize the ELF file header.
349 // This function returns the size of the output file.
351 off_t
352 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
354 if (input_objects->any_dynamic())
356 // If there are any dynamic objects in the link, then we need
357 // some additional segments: PT_PHDRS, PT_INTERP, and
358 // PT_DYNAMIC. We also need to finalize the dynamic symbol
359 // table and create the dynamic hash table.
360 abort();
363 // FIXME: Handle PT_GNU_STACK.
365 Output_segment* load_seg = this->find_first_load_seg();
367 // Lay out the segment headers.
368 int size = input_objects->target()->get_size();
369 bool big_endian = input_objects->target()->is_big_endian();
370 Output_segment_headers* segment_headers;
371 segment_headers = new Output_segment_headers(size, big_endian,
372 this->segment_list_);
373 load_seg->add_initial_output_data(segment_headers);
374 this->special_output_list_.push_back(segment_headers);
375 // FIXME: Attach them to PT_PHDRS if necessary.
377 // Lay out the file header.
378 Output_file_header* file_header;
379 file_header = new Output_file_header(size,
380 big_endian,
381 this->options_,
382 input_objects->target(),
383 symtab,
384 segment_headers);
385 load_seg->add_initial_output_data(file_header);
386 this->special_output_list_.push_back(file_header);
388 // We set the output section indexes in set_segment_offsets and
389 // set_section_offsets.
390 unsigned int shndx = 1;
392 // Set the file offsets of all the segments, and all the sections
393 // they contain.
394 off_t off = this->set_segment_offsets(input_objects->target(), load_seg,
395 &shndx);
397 // Create the symbol table sections.
398 // FIXME: We don't need to do this if we are stripping symbols.
399 Output_section* osymtab;
400 Output_section* ostrtab;
401 this->create_symtab_sections(size, input_objects, symtab, &off,
402 &osymtab, &ostrtab);
404 // Create the .shstrtab section.
405 Output_section* shstrtab_section = this->create_shstrtab();
407 // Set the file offsets of all the sections not associated with
408 // segments.
409 off = this->set_section_offsets(off, &shndx);
411 // Now the section index of OSTRTAB is set.
412 osymtab->set_link(ostrtab->out_shndx());
414 // Create the section table header.
415 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
417 file_header->set_section_info(oshdrs, shstrtab_section);
419 // Now we know exactly where everything goes in the output file.
421 return off;
424 // Return whether SEG1 should be before SEG2 in the output file. This
425 // is based entirely on the segment type and flags. When this is
426 // called the segment addresses has normally not yet been set.
428 bool
429 Layout::segment_precedes(const Output_segment* seg1,
430 const Output_segment* seg2)
432 elfcpp::Elf_Word type1 = seg1->type();
433 elfcpp::Elf_Word type2 = seg2->type();
435 // The single PT_PHDR segment is required to precede any loadable
436 // segment. We simply make it always first.
437 if (type1 == elfcpp::PT_PHDR)
439 assert(type2 != elfcpp::PT_PHDR);
440 return true;
442 if (type2 == elfcpp::PT_PHDR)
443 return false;
445 // The single PT_INTERP segment is required to precede any loadable
446 // segment. We simply make it always second.
447 if (type1 == elfcpp::PT_INTERP)
449 assert(type2 != elfcpp::PT_INTERP);
450 return true;
452 if (type2 == elfcpp::PT_INTERP)
453 return false;
455 // We then put PT_LOAD segments before any other segments.
456 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
457 return true;
458 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
459 return false;
461 // We put the PT_TLS segment last, because that is where the dynamic
462 // linker expects to find it (this is just for efficiency; other
463 // positions would also work correctly).
464 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
465 return false;
466 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
467 return true;
469 const elfcpp::Elf_Word flags1 = seg1->flags();
470 const elfcpp::Elf_Word flags2 = seg2->flags();
472 // The order of non-PT_LOAD segments is unimportant. We simply sort
473 // by the numeric segment type and flags values. There should not
474 // be more than one segment with the same type and flags.
475 if (type1 != elfcpp::PT_LOAD)
477 if (type1 != type2)
478 return type1 < type2;
479 assert(flags1 != flags2);
480 return flags1 < flags2;
483 // We sort PT_LOAD segments based on the flags. Readonly segments
484 // come before writable segments. Then executable segments come
485 // before non-executable segments. Then the unlikely case of a
486 // non-readable segment comes before the normal case of a readable
487 // segment. If there are multiple segments with the same type and
488 // flags, we require that the address be set, and we sort by
489 // virtual address and then physical address.
490 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
491 return (flags1 & elfcpp::PF_W) == 0;
492 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
493 return (flags1 & elfcpp::PF_X) != 0;
494 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
495 return (flags1 & elfcpp::PF_R) == 0;
497 uint64_t vaddr1 = seg1->vaddr();
498 uint64_t vaddr2 = seg2->vaddr();
499 if (vaddr1 != vaddr2)
500 return vaddr1 < vaddr2;
502 uint64_t paddr1 = seg1->paddr();
503 uint64_t paddr2 = seg2->paddr();
504 assert(paddr1 != paddr2);
505 return paddr1 < paddr2;
508 // Set the file offsets of all the segments, and all the sections they
509 // contain. They have all been created. LOAD_SEG must be be laid out
510 // first. Return the offset of the data to follow.
512 off_t
513 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
514 unsigned int *pshndx)
516 // Sort them into the final order.
517 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
518 Layout::Compare_segments());
520 // Find the PT_LOAD segments, and set their addresses and offsets
521 // and their section's addresses and offsets.
522 uint64_t addr = target->text_segment_address();
523 off_t off = 0;
524 bool was_readonly = false;
525 for (Segment_list::iterator p = this->segment_list_.begin();
526 p != this->segment_list_.end();
527 ++p)
529 if ((*p)->type() == elfcpp::PT_LOAD)
531 if (load_seg != NULL && load_seg != *p)
532 abort();
533 load_seg = NULL;
535 // If the last segment was readonly, and this one is not,
536 // then skip the address forward one page, maintaining the
537 // same position within the page. This lets us store both
538 // segments overlapping on a single page in the file, but
539 // the loader will put them on different pages in memory.
541 uint64_t orig_addr = addr;
542 uint64_t orig_off = off;
544 uint64_t aligned_addr = addr;
545 uint64_t abi_pagesize = target->abi_pagesize();
546 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
548 uint64_t align = (*p)->addralign();
550 addr = align_address(addr, align);
551 aligned_addr = addr;
552 if ((addr & (abi_pagesize - 1)) != 0)
553 addr = addr + abi_pagesize;
556 unsigned int shndx_hold = *pshndx;
557 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
558 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
560 // Now that we know the size of this segment, we may be able
561 // to save a page in memory, at the cost of wasting some
562 // file space, by instead aligning to the start of a new
563 // page. Here we use the real machine page size rather than
564 // the ABI mandated page size.
566 if (aligned_addr != addr)
568 uint64_t common_pagesize = target->common_pagesize();
569 uint64_t first_off = (common_pagesize
570 - (aligned_addr
571 & (common_pagesize - 1)));
572 uint64_t last_off = new_addr & (common_pagesize - 1);
573 if (first_off > 0
574 && last_off > 0
575 && ((aligned_addr & ~ (common_pagesize - 1))
576 != (new_addr & ~ (common_pagesize - 1)))
577 && first_off + last_off <= common_pagesize)
579 *pshndx = shndx_hold;
580 addr = align_address(aligned_addr, common_pagesize);
581 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
582 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
586 addr = new_addr;
588 if (((*p)->flags() & elfcpp::PF_W) == 0)
589 was_readonly = true;
593 // Handle the non-PT_LOAD segments, setting their offsets from their
594 // section's offsets.
595 for (Segment_list::iterator p = this->segment_list_.begin();
596 p != this->segment_list_.end();
597 ++p)
599 if ((*p)->type() != elfcpp::PT_LOAD)
600 (*p)->set_offset();
603 return off;
606 // Set the file offset of all the sections not associated with a
607 // segment.
609 off_t
610 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
612 for (Layout::Section_list::iterator p = this->section_list_.begin();
613 p != this->section_list_.end();
614 ++p)
616 (*p)->set_out_shndx(*pshndx);
617 ++*pshndx;
618 if ((*p)->offset() != -1)
619 continue;
620 off = align_address(off, (*p)->addralign());
621 (*p)->set_address(0, off);
622 off += (*p)->data_size();
624 return off;
627 // Create the symbol table sections.
629 void
630 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
631 Symbol_table* symtab,
632 off_t* poff,
633 Output_section** posymtab,
634 Output_section** postrtab)
636 int symsize;
637 unsigned int align;
638 if (size == 32)
640 symsize = elfcpp::Elf_sizes<32>::sym_size;
641 align = 4;
643 else if (size == 64)
645 symsize = elfcpp::Elf_sizes<64>::sym_size;
646 align = 8;
648 else
649 abort();
651 off_t off = *poff;
652 off = align_address(off, align);
653 off_t startoff = off;
655 // Save space for the dummy symbol at the start of the section. We
656 // never bother to write this out--it will just be left as zero.
657 off += symsize;
659 for (Input_objects::Object_list::const_iterator p = input_objects->begin();
660 p != input_objects->end();
661 ++p)
663 Task_lock_obj<Object> tlo(**p);
664 off = (*p)->finalize_local_symbols(off, &this->sympool_);
667 unsigned int local_symcount = (off - startoff) / symsize;
668 assert(local_symcount * symsize == off - startoff);
670 off = symtab->finalize(off, &this->sympool_);
672 this->sympool_.set_string_offsets();
674 const char* symtab_name = this->namepool_.add(".symtab");
675 Output_section* osymtab = new Output_section_symtab(symtab_name,
676 off - startoff);
677 this->section_list_.push_back(osymtab);
679 const char* strtab_name = this->namepool_.add(".strtab");
680 Output_section *ostrtab = new Output_section_strtab(strtab_name,
681 &this->sympool_);
682 this->section_list_.push_back(ostrtab);
683 this->special_output_list_.push_back(ostrtab);
685 osymtab->set_address(0, startoff);
686 osymtab->set_info(local_symcount);
687 osymtab->set_entsize(symsize);
688 osymtab->set_addralign(align);
690 *poff = off;
691 *posymtab = osymtab;
692 *postrtab = ostrtab;
695 // Create the .shstrtab section, which holds the names of the
696 // sections. At the time this is called, we have created all the
697 // output sections except .shstrtab itself.
699 Output_section*
700 Layout::create_shstrtab()
702 // FIXME: We don't need to create a .shstrtab section if we are
703 // stripping everything.
705 const char* name = this->namepool_.add(".shstrtab");
707 this->namepool_.set_string_offsets();
709 Output_section* os = new Output_section_strtab(name, &this->namepool_);
711 this->section_list_.push_back(os);
712 this->special_output_list_.push_back(os);
714 return os;
717 // Create the section headers. SIZE is 32 or 64. OFF is the file
718 // offset.
720 Output_section_headers*
721 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
723 Output_section_headers* oshdrs;
724 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
725 this->section_list_,
726 &this->namepool_);
727 off_t off = align_address(*poff, oshdrs->addralign());
728 oshdrs->set_address(0, off);
729 off += oshdrs->data_size();
730 *poff = off;
731 this->special_output_list_.push_back(oshdrs);
732 return oshdrs;
735 // The mapping of .gnu.linkonce section names to real section names.
737 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
738 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
740 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
741 MAPPING_INIT("t", ".text"),
742 MAPPING_INIT("r", ".rodata"),
743 MAPPING_INIT("d", ".data"),
744 MAPPING_INIT("b", ".bss"),
745 MAPPING_INIT("s", ".sdata"),
746 MAPPING_INIT("sb", ".sbss"),
747 MAPPING_INIT("s2", ".sdata2"),
748 MAPPING_INIT("sb2", ".sbss2"),
749 MAPPING_INIT("wi", ".debug_info"),
750 MAPPING_INIT("td", ".tdata"),
751 MAPPING_INIT("tb", ".tbss"),
752 MAPPING_INIT("lr", ".lrodata"),
753 MAPPING_INIT("l", ".ldata"),
754 MAPPING_INIT("lb", ".lbss"),
756 #undef MAPPING_INIT
758 const int Layout::linkonce_mapping_count =
759 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
761 // Return the name of the output section to use for a .gnu.linkonce
762 // section. This is based on the default ELF linker script of the old
763 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
764 // to ".text". Set *PLEN to the length of the name. *PLEN is
765 // initialized to the length of NAME.
767 const char*
768 Layout::linkonce_output_name(const char* name, size_t *plen)
770 const char* s = name + sizeof(".gnu.linkonce") - 1;
771 if (*s != '.')
772 return name;
773 ++s;
774 const Linkonce_mapping* plm = linkonce_mapping;
775 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
777 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
779 *plen = plm->tolen;
780 return plm->to;
783 return name;
786 // Choose the output section name to use given an input section name.
787 // Set *PLEN to the length of the name. *PLEN is initialized to the
788 // length of NAME.
790 const char*
791 Layout::output_section_name(const char* name, size_t* plen)
793 if (Layout::is_linkonce(name))
795 // .gnu.linkonce sections are laid out as though they were named
796 // for the sections are placed into.
797 return Layout::linkonce_output_name(name, plen);
800 // If the section name has no '.', or only an initial '.', we use
801 // the name unchanged (i.e., ".text" is unchanged).
803 // Otherwise, if the section name does not include ".rel", we drop
804 // the last '.' and everything that follows (i.e., ".text.XXX"
805 // becomes ".text").
807 // Otherwise, if the section name has zero or one '.' after the
808 // ".rel", we use the name unchanged (i.e., ".rel.text" is
809 // unchanged).
811 // Otherwise, we drop the last '.' and everything that follows
812 // (i.e., ".rel.text.XXX" becomes ".rel.text").
814 const char* s = name;
815 if (*s == '.')
816 ++s;
817 const char* sdot = strchr(s, '.');
818 if (sdot == NULL)
819 return name;
821 const char* srel = strstr(s, ".rel");
822 if (srel == NULL)
824 *plen = sdot - name;
825 return name;
828 sdot = strchr(srel + 1, '.');
829 if (sdot == NULL)
830 return name;
831 sdot = strchr(sdot + 1, '.');
832 if (sdot == NULL)
833 return name;
835 *plen = sdot - name;
836 return name;
839 // Record the signature of a comdat section, and return whether to
840 // include it in the link. If GROUP is true, this is a regular
841 // section group. If GROUP is false, this is a group signature
842 // derived from the name of a linkonce section. We want linkonce
843 // signatures and group signatures to block each other, but we don't
844 // want a linkonce signature to block another linkonce signature.
846 bool
847 Layout::add_comdat(const char* signature, bool group)
849 std::string sig(signature);
850 std::pair<Signatures::iterator, bool> ins(
851 this->signatures_.insert(std::make_pair(sig, group)));
853 if (ins.second)
855 // This is the first time we've seen this signature.
856 return true;
859 if (ins.first->second)
861 // We've already seen a real section group with this signature.
862 return false;
864 else if (group)
866 // This is a real section group, and we've already seen a
867 // linkonce section with tihs signature. Record that we've seen
868 // a section group, and don't include this section group.
869 ins.first->second = true;
870 return false;
872 else
874 // We've already seen a linkonce section and this is a linkonce
875 // section. These don't block each other--this may be the same
876 // symbol name with different section types.
877 return true;
881 // Write out data not associated with a section or the symbol table.
883 void
884 Layout::write_data(Output_file* of) const
886 for (Data_list::const_iterator p = this->special_output_list_.begin();
887 p != this->special_output_list_.end();
888 ++p)
889 (*p)->write(of);
892 // Write_data_task methods.
894 // We can always run this task.
896 Task::Is_runnable_type
897 Write_data_task::is_runnable(Workqueue*)
899 return IS_RUNNABLE;
902 // We need to unlock FINAL_BLOCKER when finished.
904 Task_locker*
905 Write_data_task::locks(Workqueue* workqueue)
907 return new Task_locker_block(*this->final_blocker_, workqueue);
910 // Run the task--write out the data.
912 void
913 Write_data_task::run(Workqueue*)
915 this->layout_->write_data(this->of_);
918 // Write_symbols_task methods.
920 // We can always run this task.
922 Task::Is_runnable_type
923 Write_symbols_task::is_runnable(Workqueue*)
925 return IS_RUNNABLE;
928 // We need to unlock FINAL_BLOCKER when finished.
930 Task_locker*
931 Write_symbols_task::locks(Workqueue* workqueue)
933 return new Task_locker_block(*this->final_blocker_, workqueue);
936 // Run the task--write out the symbols.
938 void
939 Write_symbols_task::run(Workqueue*)
941 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
944 // Close_task_runner methods.
946 // Run the task--close the file.
948 void
949 Close_task_runner::run(Workqueue*)
951 this->of_->close();
954 // Instantiate the templates we need. We could use the configure
955 // script to restrict this to only the ones for implemented targets.
957 template
958 Output_section*
959 Layout::layout<32, false>(Object* object, unsigned int shndx, const char* name,
960 const elfcpp::Shdr<32, false>& shdr, off_t*);
962 template
963 Output_section*
964 Layout::layout<32, true>(Object* object, unsigned int shndx, const char* name,
965 const elfcpp::Shdr<32, true>& shdr, off_t*);
967 template
968 Output_section*
969 Layout::layout<64, false>(Object* object, unsigned int shndx, const char* name,
970 const elfcpp::Shdr<64, false>& shdr, off_t*);
972 template
973 Output_section*
974 Layout::layout<64, true>(Object* object, unsigned int shndx, const char* name,
975 const elfcpp::Shdr<64, true>& shdr, off_t*);
978 } // End namespace gold.