* NEWS: SPU target is already supported in GDB 6.6.
[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 "symtab.h"
13 #include "layout.h"
14
15 namespace gold
16 {
17
18 // Layout_task_runner methods.
19
20 // Lay out the sections. This is called after all the input objects
21 // have been read.
22
23 void
24 Layout_task_runner::run(Workqueue* workqueue)
25 {
26 off_t file_size = this->layout_->finalize(this->input_objects_,
27 this->symtab_);
28
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);
33
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);
37 }
38
39 // Layout methods.
40
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)
45 {
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);
49 }
50
51 // Hash a key we use to look up an output section mapping.
52
53 size_t
54 Layout::Hash_key::operator()(const Layout::Key& k) const
55 {
56 return k.first + k.second.first + k.second.second;
57 }
58
59 // Whether to include this section in the link.
60
61 template<int size, bool big_endian>
62 bool
63 Layout::include_section(Object*, const char*,
64 const elfcpp::Shdr<size, big_endian>& shdr)
65 {
66 // Some section types are never linked. Some are only linked when
67 // doing a relocateable link.
68 switch (shdr.get_sh_type())
69 {
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;
78
79 case elfcpp::SHT_RELA:
80 case elfcpp::SHT_REL:
81 case elfcpp::SHT_GROUP:
82 return this->options_.is_relocatable();
83
84 default:
85 // FIXME: Handle stripping debug sections here.
86 return true;
87 }
88 }
89
90 // Return an output section named NAME, or NULL if there is none.
91
92 Output_section*
93 Layout::find_output_section(const char* name) const
94 {
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;
101 }
102
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.
105
106 Output_segment*
107 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
108 elfcpp::Elf_Word clear) const
109 {
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;
118 }
119
120 // Return the output section to use for section NAME with type TYPE
121 // and section flags FLAGS.
122
123 Output_section*
124 Layout::get_output_section(const char* name, Stringpool::Key name_key,
125 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
126 {
127 // We should ignore some flags.
128 flags &= ~ (elfcpp::SHF_INFO_LINK
129 | elfcpp::SHF_LINK_ORDER
130 | elfcpp::SHF_GROUP);
131
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));
136
137 if (!ins.second)
138 return ins.first->second;
139 else
140 {
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;
146 }
147 }
148
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.
152
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)
157 {
158 if (!this->include_section(object, name, shdr))
159 return NULL;
160
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);
166
167 // FIXME: Handle SHF_OS_NONCONFORMING here.
168
169 // Canonicalize the section name.
170 Stringpool::Key name_key;
171 name = this->namepool_.add(name, len, &name_key);
172
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());
178
179 // FIXME: Handle SHF_LINK_ORDER somewhere.
180
181 *off = os->add_input_section(object, shndx, name, shdr);
182
183 return os;
184 }
185
186 // Add POSD to an output section using NAME, TYPE, and FLAGS.
187
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)
192 {
193 // Canonicalize the name.
194 Stringpool::Key name_key;
195 name = this->namepool_.add(name, &name_key);
196
197 Output_section* os = this->get_output_section(name, name_key, type, flags);
198 os->add_output_section_data(posd);
199 }
200
201 // Map section flags to segment flags.
202
203 elfcpp::Elf_Word
204 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
205 {
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;
212 }
213
214 // Make a new Output_section, and attach it to segments as
215 // appropriate.
216
217 Output_section*
218 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
219 elfcpp::Elf_Xword flags)
220 {
221 Output_section* os = new Output_section(name, type, flags, true);
222
223 if ((flags & elfcpp::SHF_ALLOC) == 0)
224 this->section_list_.push_back(os);
225 else
226 {
227 // This output section goes into a PT_LOAD segment.
228
229 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
230
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.
236
237 Segment_list::const_iterator p;
238 for (p = this->segment_list_.begin();
239 p != this->segment_list_.end();
240 ++p)
241 {
242 if ((*p)->type() == elfcpp::PT_LOAD
243 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
244 {
245 (*p)->add_output_section(os, seg_flags);
246 break;
247 }
248 }
249
250 if (p == this->segment_list_.end())
251 {
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);
256 }
257
258 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
259 // segment.
260 if (type == elfcpp::SHT_NOTE)
261 {
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)
266 {
267 if ((*p)->type() == elfcpp::PT_NOTE
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_NOTE,
279 seg_flags);
280 this->segment_list_.push_back(oseg);
281 oseg->add_output_section(os, seg_flags);
282 }
283 }
284
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)
288 {
289 if (this->tls_segment_ == NULL)
290 {
291 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
292 seg_flags);
293 this->segment_list_.push_back(this->tls_segment_);
294 }
295 this->tls_segment_->add_output_section(os, seg_flags);
296 }
297 }
298
299 return os;
300 }
301
302 // Find the first read-only PT_LOAD segment, creating one if
303 // necessary.
304
305 Output_segment*
306 Layout::find_first_load_seg()
307 {
308 for (Segment_list::const_iterator p = this->segment_list_.begin();
309 p != this->segment_list_.end();
310 ++p)
311 {
312 if ((*p)->type() == elfcpp::PT_LOAD
313 && ((*p)->flags() & elfcpp::PF_R) != 0
314 && ((*p)->flags() & elfcpp::PF_W) == 0)
315 return *p;
316 }
317
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;
321 }
322
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.
328
329 // 1) Finalize the list of output segments and create the segment
330 // table header.
331
332 // 2) Finalize the dynamic symbol table and associated sections.
333
334 // 3) Determine the final file offset of all the output segments.
335
336 // 4) Determine the final file offset of all the SHF_ALLOC output
337 // sections.
338
339 // 5) Create the symbol table sections and the section name table
340 // section.
341
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.
345
346 // 7) Create the section table header.
347
348 // 8) Determine the final file offset of all the output sections which
349 // are not SHF_ALLOC, including the section table header.
350
351 // 9) Finalize the ELF file header.
352
353 // This function returns the size of the output file.
354
355 off_t
356 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
357 {
358 const int size = input_objects->target()->get_size();
359
360 Output_segment* phdr_seg = NULL;
361 if (input_objects->any_dynamic())
362 {
363 // There was a dynamic object in the link. We need to create
364 // some information for the dynamic linker.
365
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);
370
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);
374
375 // Create the .dynamic section to hold the dynamic data, and put
376 // it in a PT_DYNAMIC segment.
377 this->create_dynamic_section();
378
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());
382 }
383
384 // FIXME: Handle PT_GNU_STACK.
385
386 Output_segment* load_seg = this->find_first_load_seg();
387
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);
397
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);
408
409 // We set the output section indexes in set_segment_offsets and
410 // set_section_offsets.
411 unsigned int shndx = 1;
412
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);
417
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);
424
425 // Create the .shstrtab section.
426 Output_section* shstrtab_section = this->create_shstrtab();
427
428 // Set the file offsets of all the sections not associated with
429 // segments.
430 off = this->set_section_offsets(off, &shndx);
431
432 // Now the section index of OSTRTAB is set.
433 osymtab->set_link(ostrtab->out_shndx());
434
435 // Create the section table header.
436 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
437
438 file_header->set_section_info(oshdrs, shstrtab_section);
439
440 // Now we know exactly where everything goes in the output file.
441
442 return off;
443 }
444
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.
448
449 bool
450 Layout::segment_precedes(const Output_segment* seg1,
451 const Output_segment* seg2)
452 {
453 elfcpp::Elf_Word type1 = seg1->type();
454 elfcpp::Elf_Word type2 = seg2->type();
455
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)
459 {
460 assert(type2 != elfcpp::PT_PHDR);
461 return true;
462 }
463 if (type2 == elfcpp::PT_PHDR)
464 return false;
465
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)
469 {
470 assert(type2 != elfcpp::PT_INTERP);
471 return true;
472 }
473 if (type2 == elfcpp::PT_INTERP)
474 return false;
475
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;
481
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;
489
490 const elfcpp::Elf_Word flags1 = seg1->flags();
491 const elfcpp::Elf_Word flags2 = seg2->flags();
492
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)
497 {
498 if (type1 != type2)
499 return type1 < type2;
500 assert(flags1 != flags2);
501 return flags1 < flags2;
502 }
503
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;
517
518 uint64_t vaddr1 = seg1->vaddr();
519 uint64_t vaddr2 = seg2->vaddr();
520 if (vaddr1 != vaddr2)
521 return vaddr1 < vaddr2;
522
523 uint64_t paddr1 = seg1->paddr();
524 uint64_t paddr2 = seg2->paddr();
525 assert(paddr1 != paddr2);
526 return paddr1 < paddr2;
527 }
528
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.
532
533 off_t
534 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
535 unsigned int *pshndx)
536 {
537 // Sort them into the final order.
538 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
539 Layout::Compare_segments());
540
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)
549 {
550 if ((*p)->type() == elfcpp::PT_LOAD)
551 {
552 if (load_seg != NULL && load_seg != *p)
553 abort();
554 load_seg = NULL;
555
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.
561
562 uint64_t orig_addr = addr;
563 uint64_t orig_off = off;
564
565 uint64_t aligned_addr = addr;
566 uint64_t abi_pagesize = target->abi_pagesize();
567 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
568 {
569 uint64_t align = (*p)->addralign();
570
571 addr = align_address(addr, align);
572 aligned_addr = addr;
573 if ((addr & (abi_pagesize - 1)) != 0)
574 addr = addr + abi_pagesize;
575 }
576
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);
580
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.
586
587 if (aligned_addr != addr)
588 {
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)
599 {
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);
604 }
605 }
606
607 addr = new_addr;
608
609 if (((*p)->flags() & elfcpp::PF_W) == 0)
610 was_readonly = true;
611 }
612 }
613
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)
619 {
620 if ((*p)->type() != elfcpp::PT_LOAD)
621 (*p)->set_offset();
622 }
623
624 return off;
625 }
626
627 // Set the file offset of all the sections not associated with a
628 // segment.
629
630 off_t
631 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
632 {
633 for (Layout::Section_list::iterator p = this->section_list_.begin();
634 p != this->section_list_.end();
635 ++p)
636 {
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();
644 }
645 return off;
646 }
647
648 // Create the symbol table sections.
649
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)
656 {
657 int symsize;
658 unsigned int align;
659 if (size == 32)
660 {
661 symsize = elfcpp::Elf_sizes<32>::sym_size;
662 align = 4;
663 }
664 else if (size == 64)
665 {
666 symsize = elfcpp::Elf_sizes<64>::sym_size;
667 align = 8;
668 }
669 else
670 abort();
671
672 off_t off = *poff;
673 off = align_address(off, align);
674 off_t startoff = off;
675
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;
680
681 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
682 p != input_objects->relobj_end();
683 ++p)
684 {
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;
691 }
692
693 unsigned int local_symcount = local_symbol_index;
694 assert(local_symcount * symsize == off - startoff);
695
696 off = symtab->finalize(local_symcount, off, &this->sympool_);
697
698 this->sympool_.set_string_offsets();
699
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);
704
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);
710
711 osymtab->set_address(0, startoff);
712 osymtab->set_info(local_symcount);
713 osymtab->set_entsize(symsize);
714 osymtab->set_addralign(align);
715
716 *poff = off;
717 *posymtab = osymtab;
718 *postrtab = ostrtab;
719 }
720
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.
724
725 Output_section*
726 Layout::create_shstrtab()
727 {
728 // FIXME: We don't need to create a .shstrtab section if we are
729 // stripping everything.
730
731 const char* name = this->namepool_.add(".shstrtab", NULL);
732
733 this->namepool_.set_string_offsets();
734
735 Output_section* os = new Output_section_strtab(name, &this->namepool_);
736
737 this->section_list_.push_back(os);
738 this->special_output_list_.push_back(os);
739
740 return os;
741 }
742
743 // Create the section headers. SIZE is 32 or 64. OFF is the file
744 // offset.
745
746 Output_section_headers*
747 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
748 {
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;
759 }
760
761 // Create the dynamic symbol table.
762
763 void
764 Layout::create_dynamic_symtab(int, Symbol_table*)
765 {
766 abort();
767 }
768
769 // Create the .dynamic section and PT_DYNAMIC segment.
770
771 void
772 Layout::create_dynamic_section()
773 {
774 abort();
775 }
776
777 // Create the .interp section and PT_INTERP segment.
778
779 void
780 Layout::create_interp(const Target* target)
781 {
782 const char* interp = this->options_.dynamic_linker();
783 if (interp == NULL)
784 {
785 interp = target->dynamic_linker();
786 assert(interp != NULL);
787 }
788
789 size_t len = strlen(interp) + 1;
790
791 Output_section_data* odata = new Output_data_const(interp, len, 1);
792
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);
798
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);
802 }
803
804 // The mapping of .gnu.linkonce section names to real section names.
805
806 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
807 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
808 {
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"),
824 };
825 #undef MAPPING_INIT
826
827 const int Layout::linkonce_mapping_count =
828 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
829
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.
835
836 const char*
837 Layout::linkonce_output_name(const char* name, size_t *plen)
838 {
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)
845 {
846 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
847 {
848 *plen = plm->tolen;
849 return plm->to;
850 }
851 }
852 return name;
853 }
854
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.
858
859 const char*
860 Layout::output_section_name(const char* name, size_t* plen)
861 {
862 if (Layout::is_linkonce(name))
863 {
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);
867 }
868
869 // If the section name has no '.', or only an initial '.', we use
870 // the name unchanged (i.e., ".text" is unchanged).
871
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").
875
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).
879
880 // Otherwise, we drop the last '.' and everything that follows
881 // (i.e., ".rel.text.XXX" becomes ".rel.text").
882
883 const char* s = name;
884 if (*s == '.')
885 ++s;
886 const char* sdot = strchr(s, '.');
887 if (sdot == NULL)
888 return name;
889
890 const char* srel = strstr(s, ".rel");
891 if (srel == NULL)
892 {
893 *plen = sdot - name;
894 return name;
895 }
896
897 sdot = strchr(srel + 1, '.');
898 if (sdot == NULL)
899 return name;
900 sdot = strchr(sdot + 1, '.');
901 if (sdot == NULL)
902 return name;
903
904 *plen = sdot - name;
905 return name;
906 }
907
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.
914
915 bool
916 Layout::add_comdat(const char* signature, bool group)
917 {
918 std::string sig(signature);
919 std::pair<Signatures::iterator, bool> ins(
920 this->signatures_.insert(std::make_pair(sig, group)));
921
922 if (ins.second)
923 {
924 // This is the first time we've seen this signature.
925 return true;
926 }
927
928 if (ins.first->second)
929 {
930 // We've already seen a real section group with this signature.
931 return false;
932 }
933 else if (group)
934 {
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;
940 }
941 else
942 {
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;
947 }
948 }
949
950 // Write out data not associated with a section or the symbol table.
951
952 void
953 Layout::write_data(Output_file* of) const
954 {
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);
959 }
960
961 // Write_data_task methods.
962
963 // We can always run this task.
964
965 Task::Is_runnable_type
966 Write_data_task::is_runnable(Workqueue*)
967 {
968 return IS_RUNNABLE;
969 }
970
971 // We need to unlock FINAL_BLOCKER when finished.
972
973 Task_locker*
974 Write_data_task::locks(Workqueue* workqueue)
975 {
976 return new Task_locker_block(*this->final_blocker_, workqueue);
977 }
978
979 // Run the task--write out the data.
980
981 void
982 Write_data_task::run(Workqueue*)
983 {
984 this->layout_->write_data(this->of_);
985 }
986
987 // Write_symbols_task methods.
988
989 // We can always run this task.
990
991 Task::Is_runnable_type
992 Write_symbols_task::is_runnable(Workqueue*)
993 {
994 return IS_RUNNABLE;
995 }
996
997 // We need to unlock FINAL_BLOCKER when finished.
998
999 Task_locker*
1000 Write_symbols_task::locks(Workqueue* workqueue)
1001 {
1002 return new Task_locker_block(*this->final_blocker_, workqueue);
1003 }
1004
1005 // Run the task--write out the symbols.
1006
1007 void
1008 Write_symbols_task::run(Workqueue*)
1009 {
1010 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
1011 }
1012
1013 // Close_task_runner methods.
1014
1015 // Run the task--close the file.
1016
1017 void
1018 Close_task_runner::run(Workqueue*)
1019 {
1020 this->of_->close();
1021 }
1022
1023 // Instantiate the templates we need. We could use the configure
1024 // script to restrict this to only the ones for implemented targets.
1025
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*);
1030
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*);
1035
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*);
1040
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*);
1045
1046
1047 } // End namespace gold.
This page took 0.050984 seconds and 4 git commands to generate.