bfd/
[deliverable/binutils-gdb.git] / gold / layout.cc
1 // layout.cc -- lay out output file sections for gold
2
3 #include "gold.h"
4
5 #include <cassert>
6 #include <cstring>
7 #include <algorithm>
8 #include <iostream>
9 #include <utility>
10
11 #include "output.h"
12 #include "layout.h"
13
14 namespace gold
15 {
16
17 // Layout_task methods.
18
19 Layout_task::~Layout_task()
20 {
21 }
22
23 // This task can be run when it is unblocked.
24
25 Task::Is_runnable_type
26 Layout_task::is_runnable(Workqueue*)
27 {
28 if (this->this_blocker_->is_blocked())
29 return IS_BLOCKED;
30 return IS_RUNNABLE;
31 }
32
33 // We don't need to hold any locks for the duration of this task. In
34 // fact this task will be the only one running.
35
36 Task_locker*
37 Layout_task::locks(Workqueue*)
38 {
39 return NULL;
40 }
41
42 // Lay out the sections. This is called after all the input objects
43 // have been read.
44
45 void
46 Layout_task::run(Workqueue* workqueue)
47 {
48 off_t file_size = this->layout_->finalize(this->input_objects_,
49 this->symtab_);
50
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 of->open(file_size);
55
56 // Queue up the final set of tasks.
57 gold::queue_final_tasks(this->options_, this->input_objects_,
58 this->symtab_, this->layout_, workqueue, of);
59 }
60
61 // Layout methods.
62
63 Layout::Layout(const General_options& options)
64 : options_(options), last_shndx_(0), namepool_(), sympool_(), signatures_(),
65 section_name_map_(), segment_list_(), section_list_(),
66 special_output_list_()
67 {
68 // Make space for more than enough segments for a typical file.
69 // This is just for efficiency--it's OK if we wind up needing more.
70 segment_list_.reserve(12);
71 }
72
73 // Hash a key we use to look up an output section mapping.
74
75 size_t
76 Layout::Hash_key::operator()(const Layout::Key& k) const
77 {
78 return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
79 }
80
81 // Whether to include this section in the link.
82
83 template<int size, bool big_endian>
84 bool
85 Layout::include_section(Object*, const char*,
86 const elfcpp::Shdr<size, big_endian>& shdr)
87 {
88 // Some section types are never linked. Some are only linked when
89 // doing a relocateable link.
90 switch (shdr.get_sh_type())
91 {
92 case elfcpp::SHT_NULL:
93 case elfcpp::SHT_SYMTAB:
94 case elfcpp::SHT_DYNSYM:
95 case elfcpp::SHT_STRTAB:
96 case elfcpp::SHT_HASH:
97 case elfcpp::SHT_DYNAMIC:
98 case elfcpp::SHT_SYMTAB_SHNDX:
99 return false;
100
101 case elfcpp::SHT_RELA:
102 case elfcpp::SHT_REL:
103 case elfcpp::SHT_GROUP:
104 return this->options_.is_relocatable();
105
106 default:
107 // FIXME: Handle stripping debug sections here.
108 return true;
109 }
110 }
111
112 // Return the output section to use for input section NAME, with
113 // header HEADER, from object OBJECT. Set *OFF to the offset of this
114 // input section without the output section.
115
116 template<int size, bool big_endian>
117 Output_section*
118 Layout::layout(Object* object, const char* name,
119 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
120 {
121 // We discard empty input sections.
122 if (shdr.get_sh_size() == 0)
123 return NULL;
124
125 if (!this->include_section(object, name, shdr))
126 return NULL;
127
128 // Unless we are doing a relocateable link, .gnu.linkonce sections
129 // are laid out as though they were named for the sections are
130 // placed into.
131 if (!this->options_.is_relocatable() && Layout::is_linkonce(name))
132 name = Layout::linkonce_output_name(name);
133
134 // FIXME: Handle SHF_OS_NONCONFORMING here.
135
136 // Canonicalize the section name.
137 name = this->namepool_.add(name);
138
139 // Find the output section. The output section is selected based on
140 // the section name, type, and flags.
141
142 // FIXME: If we want to do relaxation, we need to modify this
143 // algorithm. We also build a list of input sections for each
144 // output section. Then we relax all the input sections. Then we
145 // walk down the list and adjust all the offsets.
146
147 elfcpp::Elf_Word type = shdr.get_sh_type();
148 elfcpp::Elf_Xword flags = shdr.get_sh_flags();
149 const Key key(name, std::make_pair(type, flags));
150 const std::pair<Key, Output_section*> v(key, NULL);
151 std::pair<Section_name_map::iterator, bool> ins(
152 this->section_name_map_.insert(v));
153
154 Output_section* os;
155 if (!ins.second)
156 os = ins.first->second;
157 else
158 {
159 // This is the first time we've seen this name/type/flags
160 // combination.
161 os = this->make_output_section(name, type, flags);
162 ins.first->second = os;
163 }
164
165 // FIXME: Handle SHF_LINK_ORDER somewhere.
166
167 *off = os->add_input_section(object, name, shdr);
168
169 return os;
170 }
171
172 // Map section flags to segment flags.
173
174 elfcpp::Elf_Word
175 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
176 {
177 elfcpp::Elf_Word ret = elfcpp::PF_R;
178 if ((flags & elfcpp::SHF_WRITE) != 0)
179 ret |= elfcpp::PF_W;
180 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
181 ret |= elfcpp::PF_X;
182 return ret;
183 }
184
185 // Make a new Output_section, and attach it to segments as
186 // appropriate.
187
188 Output_section*
189 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
190 elfcpp::Elf_Xword flags)
191 {
192 ++this->last_shndx_;
193 Output_section* os = new Output_section(name, type, flags,
194 this->last_shndx_);
195
196 if ((flags & elfcpp::SHF_ALLOC) == 0)
197 this->section_list_.push_back(os);
198 else
199 {
200 // This output section goes into a PT_LOAD segment.
201
202 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
203
204 // The only thing we really care about for PT_LOAD segments is
205 // whether or not they are writable, so that is how we search
206 // for them. People who need segments sorted on some other
207 // basis will have to wait until we implement a mechanism for
208 // them to describe the segments they want.
209
210 Segment_list::const_iterator p;
211 for (p = this->segment_list_.begin();
212 p != this->segment_list_.end();
213 ++p)
214 {
215 if ((*p)->type() == elfcpp::PT_LOAD
216 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
217 {
218 (*p)->add_output_section(os, seg_flags);
219 break;
220 }
221 }
222
223 if (p == this->segment_list_.end())
224 {
225 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
226 seg_flags);
227 this->segment_list_.push_back(oseg);
228 oseg->add_output_section(os, seg_flags);
229 }
230
231 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
232 // segment.
233 if (type == elfcpp::SHT_NOTE)
234 {
235 // See if we already have an equivalent PT_NOTE segment.
236 for (p = this->segment_list_.begin();
237 p != segment_list_.end();
238 ++p)
239 {
240 if ((*p)->type() == elfcpp::PT_NOTE
241 && (((*p)->flags() & elfcpp::PF_W)
242 == (seg_flags & elfcpp::PF_W)))
243 {
244 (*p)->add_output_section(os, seg_flags);
245 break;
246 }
247 }
248
249 if (p == this->segment_list_.end())
250 {
251 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
252 seg_flags);
253 this->segment_list_.push_back(oseg);
254 oseg->add_output_section(os, seg_flags);
255 }
256 }
257
258 // If we see a loadable SHF_TLS section, we create a PT_TLS
259 // segment.
260 if ((flags & elfcpp::SHF_TLS) != 0)
261 {
262 // See if we already have an equivalent PT_TLS segment.
263 for (p = this->segment_list_.begin();
264 p != segment_list_.end();
265 ++p)
266 {
267 if ((*p)->type() == elfcpp::PT_TLS
268 && (((*p)->flags() & elfcpp::PF_W)
269 == (seg_flags & elfcpp::PF_W)))
270 {
271 (*p)->add_output_section(os, seg_flags);
272 break;
273 }
274 }
275
276 if (p == this->segment_list_.end())
277 {
278 Output_segment* oseg = new Output_segment(elfcpp::PT_TLS,
279 seg_flags);
280 this->segment_list_.push_back(oseg);
281 oseg->add_output_section(os, seg_flags);
282 }
283 }
284 }
285
286 return os;
287 }
288
289 // Find the first read-only PT_LOAD segment, creating one if
290 // necessary.
291
292 Output_segment*
293 Layout::find_first_load_seg()
294 {
295 for (Segment_list::const_iterator p = this->segment_list_.begin();
296 p != this->segment_list_.end();
297 ++p)
298 {
299 if ((*p)->type() == elfcpp::PT_LOAD
300 && ((*p)->flags() & elfcpp::PF_R) != 0
301 && ((*p)->flags() & elfcpp::PF_W) == 0)
302 return *p;
303 }
304
305 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
306 this->segment_list_.push_back(load_seg);
307 return load_seg;
308 }
309
310 // Finalize the layout. When this is called, we have created all the
311 // output sections and all the output segments which are based on
312 // input sections. We have several things to do, and we have to do
313 // them in the right order, so that we get the right results correctly
314 // and efficiently.
315
316 // 1) Finalize the list of output segments and create the segment
317 // table header.
318
319 // 2) Finalize the dynamic symbol table and associated sections.
320
321 // 3) Determine the final file offset of all the output segments.
322
323 // 4) Determine the final file offset of all the SHF_ALLOC output
324 // sections.
325
326 // 5) Create the symbol table sections and the section name table
327 // section.
328
329 // 6) Finalize the symbol table: set symbol values to their final
330 // value and make a final determination of which symbols are going
331 // into the output symbol table.
332
333 // 7) Create the section table header.
334
335 // 8) Determine the final file offset of all the output sections which
336 // are not SHF_ALLOC, including the section table header.
337
338 // 9) Finalize the ELF file header.
339
340 // This function returns the size of the output file.
341
342 off_t
343 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
344 {
345 if (input_objects->any_dynamic())
346 {
347 // If there are any dynamic objects in the link, then we need
348 // some additional segments: PT_PHDRS, PT_INTERP, and
349 // PT_DYNAMIC. We also need to finalize the dynamic symbol
350 // table and create the dynamic hash table.
351 abort();
352 }
353
354 // FIXME: Handle PT_GNU_STACK.
355
356 Output_segment* load_seg = this->find_first_load_seg();
357
358 // Lay out the segment headers.
359 int size = input_objects->target()->get_size();
360 bool big_endian = input_objects->target()->is_big_endian();
361 Output_segment_headers* segment_headers;
362 segment_headers = new Output_segment_headers(size, big_endian,
363 this->segment_list_);
364 load_seg->add_initial_output_data(segment_headers);
365 this->special_output_list_.push_back(segment_headers);
366 // FIXME: Attach them to PT_PHDRS if necessary.
367
368 // Lay out the file header.
369 Output_file_header* file_header;
370 file_header = new Output_file_header(size,
371 big_endian,
372 this->options_,
373 input_objects->target(),
374 symtab,
375 segment_headers);
376 load_seg->add_initial_output_data(file_header);
377 this->special_output_list_.push_back(file_header);
378
379 // Set the file offsets of all the segments.
380 off_t off = this->set_segment_offsets(input_objects->target(), load_seg);
381
382 // Create the symbol table sections.
383 // FIXME: We don't need to do this if we are stripping symbols.
384 Output_section* osymtab;
385 Output_section* ostrtab;
386 this->create_symtab_sections(size, input_objects, symtab, &off,
387 &osymtab, &ostrtab);
388
389 // Create the .shstrtab section.
390 Output_section* shstrtab_section = this->create_shstrtab();
391
392 // Set the file offsets of all the sections not associated with
393 // segments.
394 off = this->set_section_offsets(off);
395
396 // Create the section table header.
397 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
398
399 file_header->set_section_info(oshdrs, shstrtab_section);
400
401 // Now we know exactly where everything goes in the output file.
402
403 return off;
404 }
405
406 // Return whether SEG1 should be before SEG2 in the output file. This
407 // is based entirely on the segment type and flags. When this is
408 // called the segment addresses has normally not yet been set.
409
410 bool
411 Layout::segment_precedes(const Output_segment* seg1,
412 const Output_segment* seg2)
413 {
414 elfcpp::Elf_Word type1 = seg1->type();
415 elfcpp::Elf_Word type2 = seg2->type();
416
417 // The single PT_PHDR segment is required to precede any loadable
418 // segment. We simply make it always first.
419 if (type1 == elfcpp::PT_PHDR)
420 {
421 assert(type2 != elfcpp::PT_PHDR);
422 return true;
423 }
424 if (type2 == elfcpp::PT_PHDR)
425 return false;
426
427 // The single PT_INTERP segment is required to precede any loadable
428 // segment. We simply make it always second.
429 if (type1 == elfcpp::PT_INTERP)
430 {
431 assert(type2 != elfcpp::PT_INTERP);
432 return true;
433 }
434 if (type2 == elfcpp::PT_INTERP)
435 return false;
436
437 // We then put PT_LOAD segments before any other segments.
438 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
439 return true;
440 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
441 return false;
442
443 const elfcpp::Elf_Word flags1 = seg1->flags();
444 const elfcpp::Elf_Word flags2 = seg2->flags();
445
446 // The order of non-PT_LOAD segments is unimportant. We simply sort
447 // by the numeric segment type and flags values. There should not
448 // be more than one segment with the same type and flags.
449 if (type1 != elfcpp::PT_LOAD)
450 {
451 if (type1 != type2)
452 return type1 < type2;
453 assert(flags1 != flags2);
454 return flags1 < flags2;
455 }
456
457 // We sort PT_LOAD segments based on the flags. Readonly segments
458 // come before writable segments. Then executable segments come
459 // before non-executable segments. Then the unlikely case of a
460 // non-readable segment comes before the normal case of a readable
461 // segment. If there are multiple segments with the same type and
462 // flags, we require that the address be set, and we sort by
463 // virtual address and then physical address.
464 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
465 return (flags1 & elfcpp::PF_W) == 0;
466 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
467 return (flags1 & elfcpp::PF_X) != 0;
468 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
469 return (flags1 & elfcpp::PF_R) == 0;
470
471 uint64_t vaddr1 = seg1->vaddr();
472 uint64_t vaddr2 = seg2->vaddr();
473 if (vaddr1 != vaddr2)
474 return vaddr1 < vaddr2;
475
476 uint64_t paddr1 = seg1->paddr();
477 uint64_t paddr2 = seg2->paddr();
478 assert(paddr1 != paddr2);
479 return paddr1 < paddr2;
480 }
481
482 // Set the file offsets of all the segments. They have all been
483 // created. LOAD_SEG must be be laid out first. Return the offset of
484 // the data to follow.
485
486 off_t
487 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg)
488 {
489 // Sort them into the final order.
490 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
491 Layout::Compare_segments());
492
493 // Find the PT_LOAD segments, and set their addresses and offsets
494 // and their section's addresses and offsets.
495 uint64_t addr = target->text_segment_address();
496 off_t off = 0;
497 bool was_readonly = false;
498 for (Segment_list::iterator p = this->segment_list_.begin();
499 p != this->segment_list_.end();
500 ++p)
501 {
502 if ((*p)->type() == elfcpp::PT_LOAD)
503 {
504 if (load_seg != NULL && load_seg != *p)
505 abort();
506 load_seg = NULL;
507
508 // If the last segment was readonly, and this one is not,
509 // then skip the address forward one page, maintaining the
510 // same position within the page. This lets us store both
511 // segments overlapping on a single page in the file, but
512 // the loader will put them on different pages in memory.
513
514 uint64_t orig_addr = addr;
515 uint64_t orig_off = off;
516
517 uint64_t aligned_addr = addr;
518 uint64_t abi_pagesize = target->abi_pagesize();
519 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
520 {
521 uint64_t align = (*p)->max_data_align();
522
523 addr = (addr + align - 1) & ~ (align - 1);
524 aligned_addr = addr;
525 if ((addr & (abi_pagesize - 1)) != 0)
526 addr = addr + abi_pagesize;
527 }
528
529 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
530 uint64_t new_addr = (*p)->set_section_addresses(addr, &off);
531
532 // Now that we know the size of this segment, we may be able
533 // to save a page in memory, at the cost of wasting some
534 // file space, by instead aligning to the start of a new
535 // page. Here we use the real machine page size rather than
536 // the ABI mandated page size.
537
538 if (aligned_addr != addr)
539 {
540 uint64_t common_pagesize = target->common_pagesize();
541 uint64_t first_off = (common_pagesize
542 - (aligned_addr
543 & (common_pagesize - 1)));
544 uint64_t last_off = new_addr & (common_pagesize - 1);
545 if (first_off > 0
546 && last_off > 0
547 && ((aligned_addr & ~ (common_pagesize - 1))
548 != (new_addr & ~ (common_pagesize - 1)))
549 && first_off + last_off <= common_pagesize)
550 {
551 addr = ((aligned_addr + common_pagesize - 1)
552 & ~ (common_pagesize - 1));
553 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
554 new_addr = (*p)->set_section_addresses(addr, &off);
555 }
556 }
557
558 addr = new_addr;
559
560 if (((*p)->flags() & elfcpp::PF_W) == 0)
561 was_readonly = true;
562 }
563 }
564
565 // Handle the non-PT_LOAD segments, setting their offsets from their
566 // section's offsets.
567 for (Segment_list::iterator p = this->segment_list_.begin();
568 p != this->segment_list_.end();
569 ++p)
570 {
571 if ((*p)->type() != elfcpp::PT_LOAD)
572 (*p)->set_offset();
573 }
574
575 return off;
576 }
577
578 // Set the file offset of all the sections not associated with a
579 // segment.
580
581 off_t
582 Layout::set_section_offsets(off_t off)
583 {
584 for (Layout::Section_list::iterator p = this->section_list_.begin();
585 p != this->section_list_.end();
586 ++p)
587 {
588 if ((*p)->offset() != -1)
589 continue;
590 uint64_t addralign = (*p)->addralign();
591 if (addralign != 0)
592 off = (off + addralign - 1) & ~ (addralign - 1);
593 (*p)->set_address(0, off);
594 off += (*p)->data_size();
595 }
596 return off;
597 }
598
599 // Create the symbol table sections.
600
601 void
602 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
603 Symbol_table* symtab,
604 off_t* poff,
605 Output_section** posymtab,
606 Output_section** postrtab)
607 {
608 int symsize;
609 unsigned int align;
610 if (size == 32)
611 {
612 symsize = elfcpp::Elf_sizes<32>::sym_size;
613 align = 4;
614 }
615 else if (size == 64)
616 {
617 symsize = elfcpp::Elf_sizes<64>::sym_size;
618 align = 8;
619 }
620 else
621 abort();
622
623 off_t off = *poff;
624 off = (off + align - 1) & ~ (align - 1);
625 off_t startoff = off;
626
627 // Save space for the dummy symbol at the start of the section. We
628 // never bother to write this out--it will just be left as zero.
629 off += symsize;
630
631 for (Input_objects::Object_list::const_iterator p = input_objects->begin();
632 p != input_objects->end();
633 ++p)
634 {
635 Task_lock_obj<Object> tlo(**p);
636 off = (*p)->finalize_local_symbols(off, &this->sympool_);
637 }
638
639 unsigned int local_symcount = (off - startoff) / symsize;
640 assert(local_symcount * symsize == off - startoff);
641
642 off = symtab->finalize(off, &this->sympool_);
643
644 this->sympool_.set_string_offsets();
645
646 ++this->last_shndx_;
647 const char* symtab_name = this->namepool_.add(".symtab");
648 Output_section* osymtab = new Output_section_symtab(symtab_name,
649 off - startoff,
650 this->last_shndx_);
651 this->section_list_.push_back(osymtab);
652
653 ++this->last_shndx_;
654 const char* strtab_name = this->namepool_.add(".strtab");
655 Output_section *ostrtab = new Output_section_strtab(strtab_name,
656 &this->sympool_,
657 this->last_shndx_);
658 this->section_list_.push_back(ostrtab);
659 this->special_output_list_.push_back(ostrtab);
660
661 osymtab->set_address(0, startoff);
662 osymtab->set_link(ostrtab->shndx());
663 osymtab->set_info(local_symcount);
664 osymtab->set_entsize(symsize);
665 osymtab->set_addralign(align);
666
667 *poff = off;
668 *posymtab = osymtab;
669 *postrtab = ostrtab;
670 }
671
672 // Create the .shstrtab section, which holds the names of the
673 // sections. At the time this is called, we have created all the
674 // output sections except .shstrtab itself.
675
676 Output_section*
677 Layout::create_shstrtab()
678 {
679 // FIXME: We don't need to create a .shstrtab section if we are
680 // stripping everything.
681
682 const char* name = this->namepool_.add(".shstrtab");
683
684 this->namepool_.set_string_offsets();
685
686 ++this->last_shndx_;
687 Output_section* os = new Output_section_strtab(name,
688 &this->namepool_,
689 this->last_shndx_);
690
691 this->section_list_.push_back(os);
692 this->special_output_list_.push_back(os);
693
694 return os;
695 }
696
697 // Create the section headers. SIZE is 32 or 64. OFF is the file
698 // offset.
699
700 Output_section_headers*
701 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
702 {
703 Output_section_headers* oshdrs;
704 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
705 this->section_list_,
706 &this->namepool_);
707 uint64_t addralign = oshdrs->addralign();
708 off_t off = (*poff + addralign - 1) & ~ (addralign - 1);
709 oshdrs->set_address(0, off);
710 off += oshdrs->data_size();
711 *poff = off;
712 this->special_output_list_.push_back(oshdrs);
713 return oshdrs;
714 }
715
716 // The mapping of .gnu.linkonce section names to real section names.
717
718 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
719 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
720 {
721 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
722 MAPPING_INIT("t", ".text"),
723 MAPPING_INIT("r", ".rodata"),
724 MAPPING_INIT("d", ".data"),
725 MAPPING_INIT("b", ".bss"),
726 MAPPING_INIT("s", ".sdata"),
727 MAPPING_INIT("sb", ".sbss"),
728 MAPPING_INIT("s2", ".sdata2"),
729 MAPPING_INIT("sb2", ".sbss2"),
730 MAPPING_INIT("wi", ".debug_info"),
731 MAPPING_INIT("td", ".tdata"),
732 MAPPING_INIT("tb", ".tbss"),
733 MAPPING_INIT("lr", ".lrodata"),
734 MAPPING_INIT("l", ".ldata"),
735 MAPPING_INIT("lb", ".lbss"),
736 };
737 #undef MAPPING_INIT
738
739 const int Layout::linkonce_mapping_count =
740 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
741
742 // Return the name of the output section to use for a .gnu.linkonce
743 // section. This is based on the default ELF linker script of the old
744 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
745 // to ".text".
746
747 const char*
748 Layout::linkonce_output_name(const char* name)
749 {
750 const char* s = name + sizeof(".gnu.linkonce") - 1;
751 if (*s != '.')
752 return name;
753 ++s;
754 const Linkonce_mapping* plm = linkonce_mapping;
755 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
756 {
757 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
758 return plm->to;
759 }
760 return name;
761 }
762
763 // Record the signature of a comdat section, and return whether to
764 // include it in the link. If GROUP is true, this is a regular
765 // section group. If GROUP is false, this is a group signature
766 // derived from the name of a linkonce section. We want linkonce
767 // signatures and group signatures to block each other, but we don't
768 // want a linkonce signature to block another linkonce signature.
769
770 bool
771 Layout::add_comdat(const char* signature, bool group)
772 {
773 std::string sig(signature);
774 std::pair<Signatures::iterator, bool> ins(
775 this->signatures_.insert(std::make_pair(signature, group)));
776
777 if (ins.second)
778 {
779 // This is the first time we've seen this signature.
780 return true;
781 }
782
783 if (ins.first->second)
784 {
785 // We've already seen a real section group with this signature.
786 return false;
787 }
788 else if (group)
789 {
790 // This is a real section group, and we've already seen a
791 // linkonce section with tihs signature. Record that we've seen
792 // a section group, and don't include this section group.
793 ins.first->second = true;
794 return false;
795 }
796 else
797 {
798 // We've already seen a linkonce section and this is a linkonce
799 // section. These don't block each other--this may be the same
800 // symbol name with different section types.
801 return true;
802 }
803 }
804
805 // Write out data not associated with a section or the symbol table.
806
807 void
808 Layout::write_data(Output_file* of) const
809 {
810 for (Data_list::const_iterator p = this->special_output_list_.begin();
811 p != this->special_output_list_.end();
812 ++p)
813 (*p)->write(of);
814 }
815
816 // Write_data_task methods.
817
818 // We can always run this task.
819
820 Task::Is_runnable_type
821 Write_data_task::is_runnable(Workqueue*)
822 {
823 return IS_RUNNABLE;
824 }
825
826 // We need to unlock FINAL_BLOCKER when finished.
827
828 Task_locker*
829 Write_data_task::locks(Workqueue* workqueue)
830 {
831 return new Task_locker_block(*this->final_blocker_, workqueue);
832 }
833
834 // Run the task--write out the data.
835
836 void
837 Write_data_task::run(Workqueue*)
838 {
839 this->layout_->write_data(this->of_);
840 }
841
842 // Write_symbols_task methods.
843
844 // We can always run this task.
845
846 Task::Is_runnable_type
847 Write_symbols_task::is_runnable(Workqueue*)
848 {
849 return IS_RUNNABLE;
850 }
851
852 // We need to unlock FINAL_BLOCKER when finished.
853
854 Task_locker*
855 Write_symbols_task::locks(Workqueue* workqueue)
856 {
857 return new Task_locker_block(*this->final_blocker_, workqueue);
858 }
859
860 // Run the task--write out the symbols.
861
862 void
863 Write_symbols_task::run(Workqueue*)
864 {
865 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
866 }
867
868 // Close_task methods.
869
870 // We can't run until FINAL_BLOCKER is unblocked.
871
872 Task::Is_runnable_type
873 Close_task::is_runnable(Workqueue*)
874 {
875 if (this->final_blocker_->is_blocked())
876 return IS_BLOCKED;
877 return IS_RUNNABLE;
878 }
879
880 // We don't lock anything.
881
882 Task_locker*
883 Close_task::locks(Workqueue*)
884 {
885 return NULL;
886 }
887
888 // Run the task--close the file.
889
890 void
891 Close_task::run(Workqueue*)
892 {
893 this->of_->close();
894 }
895
896 // Instantiate the templates we need. We could use the configure
897 // script to restrict this to only the ones for implemented targets.
898
899 template
900 Output_section*
901 Layout::layout<32, false>(Object* object, const char* name,
902 const elfcpp::Shdr<32, false>& shdr, off_t*);
903
904 template
905 Output_section*
906 Layout::layout<32, true>(Object* object, const char* name,
907 const elfcpp::Shdr<32, true>& shdr, off_t*);
908
909 template
910 Output_section*
911 Layout::layout<64, false>(Object* object, const char* name,
912 const elfcpp::Shdr<64, false>& shdr, off_t*);
913
914 template
915 Output_section*
916 Layout::layout<64, true>(Object* object, const char* name,
917 const elfcpp::Shdr<64, true>& shdr, off_t*);
918
919
920 } // End namespace gold.
This page took 0.049709 seconds and 4 git commands to generate.