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