Define __start and __stop symbols.
[binutils-gdb.git] / gold / layout.cc
1 // layout.cc -- lay out output file sections for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6 #include <algorithm>
7 #include <iostream>
8 #include <utility>
9
10 #include "parameters.h"
11 #include "output.h"
12 #include "symtab.h"
13 #include "dynobj.h"
14 #include "layout.h"
15
16 namespace gold
17 {
18
19 // Layout_task_runner methods.
20
21 // Lay out the sections. This is called after all the input objects
22 // have been read.
23
24 void
25 Layout_task_runner::run(Workqueue* workqueue)
26 {
27 off_t file_size = this->layout_->finalize(this->input_objects_,
28 this->symtab_);
29
30 // Now we know the final size of the output file and we know where
31 // each piece of information goes.
32 Output_file* of = new Output_file(this->options_,
33 this->input_objects_->target());
34 of->open(file_size);
35
36 // Queue up the final set of tasks.
37 gold::queue_final_tasks(this->options_, this->input_objects_,
38 this->symtab_, this->layout_, workqueue, of);
39 }
40
41 // Layout methods.
42
43 Layout::Layout(const General_options& options)
44 : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
45 section_name_map_(), segment_list_(), section_list_(),
46 unattached_section_list_(), special_output_list_(),
47 tls_segment_(NULL), symtab_section_(NULL),
48 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL)
49 {
50 // Make space for more than enough segments for a typical file.
51 // This is just for efficiency--it's OK if we wind up needing more.
52 this->segment_list_.reserve(12);
53
54 // We expect three unattached Output_data objects: the file header,
55 // the segment headers, and the section headers.
56 this->special_output_list_.reserve(3);
57 }
58
59 // Hash a key we use to look up an output section mapping.
60
61 size_t
62 Layout::Hash_key::operator()(const Layout::Key& k) const
63 {
64 return k.first + k.second.first + k.second.second;
65 }
66
67 // Whether to include this section in the link.
68
69 template<int size, bool big_endian>
70 bool
71 Layout::include_section(Object*, const char*,
72 const elfcpp::Shdr<size, big_endian>& shdr)
73 {
74 // Some section types are never linked. Some are only linked when
75 // doing a relocateable link.
76 switch (shdr.get_sh_type())
77 {
78 case elfcpp::SHT_NULL:
79 case elfcpp::SHT_SYMTAB:
80 case elfcpp::SHT_DYNSYM:
81 case elfcpp::SHT_STRTAB:
82 case elfcpp::SHT_HASH:
83 case elfcpp::SHT_DYNAMIC:
84 case elfcpp::SHT_SYMTAB_SHNDX:
85 return false;
86
87 case elfcpp::SHT_RELA:
88 case elfcpp::SHT_REL:
89 case elfcpp::SHT_GROUP:
90 return parameters->output_is_object();
91
92 default:
93 // FIXME: Handle stripping debug sections here.
94 return true;
95 }
96 }
97
98 // Return an output section named NAME, or NULL if there is none.
99
100 Output_section*
101 Layout::find_output_section(const char* name) const
102 {
103 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
104 p != this->section_name_map_.end();
105 ++p)
106 if (strcmp(p->second->name(), name) == 0)
107 return p->second;
108 return NULL;
109 }
110
111 // Return an output segment of type TYPE, with segment flags SET set
112 // and segment flags CLEAR clear. Return NULL if there is none.
113
114 Output_segment*
115 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
116 elfcpp::Elf_Word clear) const
117 {
118 for (Segment_list::const_iterator p = this->segment_list_.begin();
119 p != this->segment_list_.end();
120 ++p)
121 if (static_cast<elfcpp::PT>((*p)->type()) == type
122 && ((*p)->flags() & set) == set
123 && ((*p)->flags() & clear) == 0)
124 return *p;
125 return NULL;
126 }
127
128 // Return the output section to use for section NAME with type TYPE
129 // and section flags FLAGS.
130
131 Output_section*
132 Layout::get_output_section(const char* name, Stringpool::Key name_key,
133 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
134 {
135 // We should ignore some flags.
136 flags &= ~ (elfcpp::SHF_INFO_LINK
137 | elfcpp::SHF_LINK_ORDER
138 | elfcpp::SHF_GROUP
139 | elfcpp::SHF_MERGE
140 | elfcpp::SHF_STRINGS);
141
142 const Key key(name_key, std::make_pair(type, flags));
143 const std::pair<Key, Output_section*> v(key, NULL);
144 std::pair<Section_name_map::iterator, bool> ins(
145 this->section_name_map_.insert(v));
146
147 if (!ins.second)
148 return ins.first->second;
149 else
150 {
151 // This is the first time we've seen this name/type/flags
152 // combination.
153 Output_section* os = this->make_output_section(name, type, flags);
154 ins.first->second = os;
155 return os;
156 }
157 }
158
159 // Return the output section to use for input section SHNDX, with name
160 // NAME, with header HEADER, from object OBJECT. Set *OFF to the
161 // offset of this input section without the output section.
162
163 template<int size, bool big_endian>
164 Output_section*
165 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
166 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
167 {
168 if (!this->include_section(object, name, shdr))
169 return NULL;
170
171 // If we are not doing a relocateable link, choose the name to use
172 // for the output section.
173 size_t len = strlen(name);
174 if (!parameters->output_is_object())
175 name = Layout::output_section_name(name, &len);
176
177 // FIXME: Handle SHF_OS_NONCONFORMING here.
178
179 // Canonicalize the section name.
180 Stringpool::Key name_key;
181 name = this->namepool_.add(name, len, &name_key);
182
183 // Find the output section. The output section is selected based on
184 // the section name, type, and flags.
185 Output_section* os = this->get_output_section(name, name_key,
186 shdr.get_sh_type(),
187 shdr.get_sh_flags());
188
189 // FIXME: Handle SHF_LINK_ORDER somewhere.
190
191 *off = os->add_input_section(object, shndx, name, shdr);
192
193 return os;
194 }
195
196 // Add POSD to an output section using NAME, TYPE, and FLAGS.
197
198 void
199 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
200 elfcpp::Elf_Xword flags,
201 Output_section_data* posd)
202 {
203 // Canonicalize the name.
204 Stringpool::Key name_key;
205 name = this->namepool_.add(name, &name_key);
206
207 Output_section* os = this->get_output_section(name, name_key, type, flags);
208 os->add_output_section_data(posd);
209 }
210
211 // Map section flags to segment flags.
212
213 elfcpp::Elf_Word
214 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
215 {
216 elfcpp::Elf_Word ret = elfcpp::PF_R;
217 if ((flags & elfcpp::SHF_WRITE) != 0)
218 ret |= elfcpp::PF_W;
219 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
220 ret |= elfcpp::PF_X;
221 return ret;
222 }
223
224 // Make a new Output_section, and attach it to segments as
225 // appropriate.
226
227 Output_section*
228 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
229 elfcpp::Elf_Xword flags)
230 {
231 Output_section* os = new Output_section(name, type, flags);
232 this->section_list_.push_back(os);
233
234 if ((flags & elfcpp::SHF_ALLOC) == 0)
235 this->unattached_section_list_.push_back(os);
236 else
237 {
238 // This output section goes into a PT_LOAD segment.
239
240 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
241
242 // The only thing we really care about for PT_LOAD segments is
243 // whether or not they are writable, so that is how we search
244 // for them. People who need segments sorted on some other
245 // basis will have to wait until we implement a mechanism for
246 // them to describe the segments they want.
247
248 Segment_list::const_iterator p;
249 for (p = this->segment_list_.begin();
250 p != this->segment_list_.end();
251 ++p)
252 {
253 if ((*p)->type() == elfcpp::PT_LOAD
254 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
255 {
256 (*p)->add_output_section(os, seg_flags);
257 break;
258 }
259 }
260
261 if (p == this->segment_list_.end())
262 {
263 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
264 seg_flags);
265 this->segment_list_.push_back(oseg);
266 oseg->add_output_section(os, seg_flags);
267 }
268
269 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
270 // segment.
271 if (type == elfcpp::SHT_NOTE)
272 {
273 // See if we already have an equivalent PT_NOTE segment.
274 for (p = this->segment_list_.begin();
275 p != segment_list_.end();
276 ++p)
277 {
278 if ((*p)->type() == elfcpp::PT_NOTE
279 && (((*p)->flags() & elfcpp::PF_W)
280 == (seg_flags & elfcpp::PF_W)))
281 {
282 (*p)->add_output_section(os, seg_flags);
283 break;
284 }
285 }
286
287 if (p == this->segment_list_.end())
288 {
289 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
290 seg_flags);
291 this->segment_list_.push_back(oseg);
292 oseg->add_output_section(os, seg_flags);
293 }
294 }
295
296 // If we see a loadable SHF_TLS section, we create a PT_TLS
297 // segment. There can only be one such segment.
298 if ((flags & elfcpp::SHF_TLS) != 0)
299 {
300 if (this->tls_segment_ == NULL)
301 {
302 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
303 seg_flags);
304 this->segment_list_.push_back(this->tls_segment_);
305 }
306 this->tls_segment_->add_output_section(os, seg_flags);
307 }
308 }
309
310 return os;
311 }
312
313 // Create the dynamic sections which are needed before we read the
314 // relocs.
315
316 void
317 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
318 Symbol_table* symtab)
319 {
320 if (!input_objects->any_dynamic())
321 return;
322
323 const char* dynamic_name = this->namepool_.add(".dynamic", NULL);
324 this->dynamic_section_ = this->make_output_section(dynamic_name,
325 elfcpp::SHT_DYNAMIC,
326 (elfcpp::SHF_ALLOC
327 | elfcpp::SHF_WRITE));
328
329 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
330 this->dynamic_section_, 0, 0,
331 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
332 elfcpp::STV_HIDDEN, 0, false, false);
333
334 this->dynamic_data_ = new Output_data_dynamic(input_objects->target(),
335 &this->dynpool_);
336
337 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
338 }
339
340 // For each output section whose name can be represented as C symbol,
341 // define __start and __stop symbols for the section. This is a GNU
342 // extension.
343
344 void
345 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
346 {
347 for (Section_list::const_iterator p = this->section_list_.begin();
348 p != this->section_list_.end();
349 ++p)
350 {
351 const char* const name = (*p)->name();
352 if (name[strspn(name,
353 ("0123456789"
354 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
355 "abcdefghijklmnopqrstuvwxyz"
356 "_"))]
357 == '\0')
358 {
359 const std::string name_string(name);
360 const std::string start_name("__start_" + name_string);
361 const std::string stop_name("__stop_" + name_string);
362
363 symtab->define_in_output_data(target,
364 start_name.c_str(),
365 NULL, // version
366 *p,
367 0, // value
368 0, // symsize
369 elfcpp::STT_NOTYPE,
370 elfcpp::STB_GLOBAL,
371 elfcpp::STV_DEFAULT,
372 0, // nonvis
373 false, // offset_is_from_end
374 false); // only_if_ref
375
376 symtab->define_in_output_data(target,
377 stop_name.c_str(),
378 NULL, // version
379 *p,
380 0, // value
381 0, // symsize
382 elfcpp::STT_NOTYPE,
383 elfcpp::STB_GLOBAL,
384 elfcpp::STV_DEFAULT,
385 0, // nonvis
386 true, // offset_is_from_end
387 false); // only_if_ref
388 }
389 }
390 }
391
392 // Find the first read-only PT_LOAD segment, creating one if
393 // necessary.
394
395 Output_segment*
396 Layout::find_first_load_seg()
397 {
398 for (Segment_list::const_iterator p = this->segment_list_.begin();
399 p != this->segment_list_.end();
400 ++p)
401 {
402 if ((*p)->type() == elfcpp::PT_LOAD
403 && ((*p)->flags() & elfcpp::PF_R) != 0
404 && ((*p)->flags() & elfcpp::PF_W) == 0)
405 return *p;
406 }
407
408 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
409 this->segment_list_.push_back(load_seg);
410 return load_seg;
411 }
412
413 // Finalize the layout. When this is called, we have created all the
414 // output sections and all the output segments which are based on
415 // input sections. We have several things to do, and we have to do
416 // them in the right order, so that we get the right results correctly
417 // and efficiently.
418
419 // 1) Finalize the list of output segments and create the segment
420 // table header.
421
422 // 2) Finalize the dynamic symbol table and associated sections.
423
424 // 3) Determine the final file offset of all the output segments.
425
426 // 4) Determine the final file offset of all the SHF_ALLOC output
427 // sections.
428
429 // 5) Create the symbol table sections and the section name table
430 // section.
431
432 // 6) Finalize the symbol table: set symbol values to their final
433 // value and make a final determination of which symbols are going
434 // into the output symbol table.
435
436 // 7) Create the section table header.
437
438 // 8) Determine the final file offset of all the output sections which
439 // are not SHF_ALLOC, including the section table header.
440
441 // 9) Finalize the ELF file header.
442
443 // This function returns the size of the output file.
444
445 off_t
446 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
447 {
448 Target* const target = input_objects->target();
449 const int size = target->get_size();
450
451 target->finalize_sections(this);
452
453 Output_segment* phdr_seg = NULL;
454 if (input_objects->any_dynamic())
455 {
456 // There was a dynamic object in the link. We need to create
457 // some information for the dynamic linker.
458
459 // Create the PT_PHDR segment which will hold the program
460 // headers.
461 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
462 this->segment_list_.push_back(phdr_seg);
463
464 // Create the dynamic symbol table, including the hash table.
465 Output_section* dynstr;
466 std::vector<Symbol*> dynamic_symbols;
467 unsigned int local_dynamic_count;
468 Versions versions;
469 this->create_dynamic_symtab(target, symtab, &dynstr,
470 &local_dynamic_count, &dynamic_symbols,
471 &versions);
472
473 // Create the .interp section to hold the name of the
474 // interpreter, and put it in a PT_INTERP segment.
475 this->create_interp(target);
476
477 // Finish the .dynamic section to hold the dynamic data, and put
478 // it in a PT_DYNAMIC segment.
479 this->finish_dynamic_section(input_objects, symtab);
480
481 // We should have added everything we need to the dynamic string
482 // table.
483 this->dynpool_.set_string_offsets();
484
485 // Create the version sections. We can't do this until the
486 // dynamic string table is complete.
487 this->create_version_sections(target, &versions, local_dynamic_count,
488 dynamic_symbols, dynstr);
489 }
490
491 // FIXME: Handle PT_GNU_STACK.
492
493 Output_segment* load_seg = this->find_first_load_seg();
494
495 // Lay out the segment headers.
496 bool big_endian = target->is_big_endian();
497 Output_segment_headers* segment_headers;
498 segment_headers = new Output_segment_headers(size, big_endian,
499 this->segment_list_);
500 load_seg->add_initial_output_data(segment_headers);
501 this->special_output_list_.push_back(segment_headers);
502 if (phdr_seg != NULL)
503 phdr_seg->add_initial_output_data(segment_headers);
504
505 // Lay out the file header.
506 Output_file_header* file_header;
507 file_header = new Output_file_header(size,
508 big_endian,
509 target,
510 symtab,
511 segment_headers);
512 load_seg->add_initial_output_data(file_header);
513 this->special_output_list_.push_back(file_header);
514
515 // We set the output section indexes in set_segment_offsets and
516 // set_section_offsets.
517 unsigned int shndx = 1;
518
519 // Set the file offsets of all the segments, and all the sections
520 // they contain.
521 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
522
523 // Create the symbol table sections.
524 this->create_symtab_sections(size, input_objects, symtab, &off);
525
526 // Create the .shstrtab section.
527 Output_section* shstrtab_section = this->create_shstrtab();
528
529 // Set the file offsets of all the sections not associated with
530 // segments.
531 off = this->set_section_offsets(off, &shndx);
532
533 // Create the section table header.
534 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
535
536 file_header->set_section_info(oshdrs, shstrtab_section);
537
538 // Now we know exactly where everything goes in the output file.
539 Output_data::layout_complete();
540
541 return off;
542 }
543
544 // Return whether SEG1 should be before SEG2 in the output file. This
545 // is based entirely on the segment type and flags. When this is
546 // called the segment addresses has normally not yet been set.
547
548 bool
549 Layout::segment_precedes(const Output_segment* seg1,
550 const Output_segment* seg2)
551 {
552 elfcpp::Elf_Word type1 = seg1->type();
553 elfcpp::Elf_Word type2 = seg2->type();
554
555 // The single PT_PHDR segment is required to precede any loadable
556 // segment. We simply make it always first.
557 if (type1 == elfcpp::PT_PHDR)
558 {
559 gold_assert(type2 != elfcpp::PT_PHDR);
560 return true;
561 }
562 if (type2 == elfcpp::PT_PHDR)
563 return false;
564
565 // The single PT_INTERP segment is required to precede any loadable
566 // segment. We simply make it always second.
567 if (type1 == elfcpp::PT_INTERP)
568 {
569 gold_assert(type2 != elfcpp::PT_INTERP);
570 return true;
571 }
572 if (type2 == elfcpp::PT_INTERP)
573 return false;
574
575 // We then put PT_LOAD segments before any other segments.
576 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
577 return true;
578 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
579 return false;
580
581 // We put the PT_TLS segment last, because that is where the dynamic
582 // linker expects to find it (this is just for efficiency; other
583 // positions would also work correctly).
584 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
585 return false;
586 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
587 return true;
588
589 const elfcpp::Elf_Word flags1 = seg1->flags();
590 const elfcpp::Elf_Word flags2 = seg2->flags();
591
592 // The order of non-PT_LOAD segments is unimportant. We simply sort
593 // by the numeric segment type and flags values. There should not
594 // be more than one segment with the same type and flags.
595 if (type1 != elfcpp::PT_LOAD)
596 {
597 if (type1 != type2)
598 return type1 < type2;
599 gold_assert(flags1 != flags2);
600 return flags1 < flags2;
601 }
602
603 // We sort PT_LOAD segments based on the flags. Readonly segments
604 // come before writable segments. Then executable segments come
605 // before non-executable segments. Then the unlikely case of a
606 // non-readable segment comes before the normal case of a readable
607 // segment. If there are multiple segments with the same type and
608 // flags, we require that the address be set, and we sort by
609 // virtual address and then physical address.
610 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
611 return (flags1 & elfcpp::PF_W) == 0;
612 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
613 return (flags1 & elfcpp::PF_X) != 0;
614 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
615 return (flags1 & elfcpp::PF_R) == 0;
616
617 uint64_t vaddr1 = seg1->vaddr();
618 uint64_t vaddr2 = seg2->vaddr();
619 if (vaddr1 != vaddr2)
620 return vaddr1 < vaddr2;
621
622 uint64_t paddr1 = seg1->paddr();
623 uint64_t paddr2 = seg2->paddr();
624 gold_assert(paddr1 != paddr2);
625 return paddr1 < paddr2;
626 }
627
628 // Set the file offsets of all the segments, and all the sections they
629 // contain. They have all been created. LOAD_SEG must be be laid out
630 // first. Return the offset of the data to follow.
631
632 off_t
633 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
634 unsigned int *pshndx)
635 {
636 // Sort them into the final order.
637 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
638 Layout::Compare_segments());
639
640 // Find the PT_LOAD segments, and set their addresses and offsets
641 // and their section's addresses and offsets.
642 uint64_t addr = target->text_segment_address();
643 off_t off = 0;
644 bool was_readonly = false;
645 for (Segment_list::iterator p = this->segment_list_.begin();
646 p != this->segment_list_.end();
647 ++p)
648 {
649 if ((*p)->type() == elfcpp::PT_LOAD)
650 {
651 if (load_seg != NULL && load_seg != *p)
652 gold_unreachable();
653 load_seg = NULL;
654
655 // If the last segment was readonly, and this one is not,
656 // then skip the address forward one page, maintaining the
657 // same position within the page. This lets us store both
658 // segments overlapping on a single page in the file, but
659 // the loader will put them on different pages in memory.
660
661 uint64_t orig_addr = addr;
662 uint64_t orig_off = off;
663
664 uint64_t aligned_addr = addr;
665 uint64_t abi_pagesize = target->abi_pagesize();
666
667 // FIXME: This should depend on the -n and -N options.
668 (*p)->set_minimum_addralign(target->common_pagesize());
669
670 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
671 {
672 uint64_t align = (*p)->addralign();
673
674 addr = align_address(addr, align);
675 aligned_addr = addr;
676 if ((addr & (abi_pagesize - 1)) != 0)
677 addr = addr + abi_pagesize;
678 }
679
680 unsigned int shndx_hold = *pshndx;
681 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
682 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
683
684 // Now that we know the size of this segment, we may be able
685 // to save a page in memory, at the cost of wasting some
686 // file space, by instead aligning to the start of a new
687 // page. Here we use the real machine page size rather than
688 // the ABI mandated page size.
689
690 if (aligned_addr != addr)
691 {
692 uint64_t common_pagesize = target->common_pagesize();
693 uint64_t first_off = (common_pagesize
694 - (aligned_addr
695 & (common_pagesize - 1)));
696 uint64_t last_off = new_addr & (common_pagesize - 1);
697 if (first_off > 0
698 && last_off > 0
699 && ((aligned_addr & ~ (common_pagesize - 1))
700 != (new_addr & ~ (common_pagesize - 1)))
701 && first_off + last_off <= common_pagesize)
702 {
703 *pshndx = shndx_hold;
704 addr = align_address(aligned_addr, common_pagesize);
705 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
706 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
707 }
708 }
709
710 addr = new_addr;
711
712 if (((*p)->flags() & elfcpp::PF_W) == 0)
713 was_readonly = true;
714 }
715 }
716
717 // Handle the non-PT_LOAD segments, setting their offsets from their
718 // section's offsets.
719 for (Segment_list::iterator p = this->segment_list_.begin();
720 p != this->segment_list_.end();
721 ++p)
722 {
723 if ((*p)->type() != elfcpp::PT_LOAD)
724 (*p)->set_offset();
725 }
726
727 return off;
728 }
729
730 // Set the file offset of all the sections not associated with a
731 // segment.
732
733 off_t
734 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
735 {
736 for (Section_list::iterator p = this->unattached_section_list_.begin();
737 p != this->unattached_section_list_.end();
738 ++p)
739 {
740 (*p)->set_out_shndx(*pshndx);
741 ++*pshndx;
742 if ((*p)->offset() != -1)
743 continue;
744 off = align_address(off, (*p)->addralign());
745 (*p)->set_address(0, off);
746 off += (*p)->data_size();
747 }
748 return off;
749 }
750
751 // Create the symbol table sections. Here we also set the final
752 // values of the symbols. At this point all the loadable sections are
753 // fully laid out.
754
755 void
756 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
757 Symbol_table* symtab,
758 off_t* poff)
759 {
760 int symsize;
761 unsigned int align;
762 if (size == 32)
763 {
764 symsize = elfcpp::Elf_sizes<32>::sym_size;
765 align = 4;
766 }
767 else if (size == 64)
768 {
769 symsize = elfcpp::Elf_sizes<64>::sym_size;
770 align = 8;
771 }
772 else
773 gold_unreachable();
774
775 off_t off = *poff;
776 off = align_address(off, align);
777 off_t startoff = off;
778
779 // Save space for the dummy symbol at the start of the section. We
780 // never bother to write this out--it will just be left as zero.
781 off += symsize;
782 unsigned int local_symbol_index = 1;
783
784 // Add STT_SECTION symbols for each Output section which needs one.
785 for (Section_list::iterator p = this->section_list_.begin();
786 p != this->section_list_.end();
787 ++p)
788 {
789 if (!(*p)->needs_symtab_index())
790 (*p)->set_symtab_index(-1U);
791 else
792 {
793 (*p)->set_symtab_index(local_symbol_index);
794 ++local_symbol_index;
795 off += symsize;
796 }
797 }
798
799 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
800 p != input_objects->relobj_end();
801 ++p)
802 {
803 Task_lock_obj<Object> tlo(**p);
804 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
805 off,
806 &this->sympool_);
807 off += (index - local_symbol_index) * symsize;
808 local_symbol_index = index;
809 }
810
811 unsigned int local_symcount = local_symbol_index;
812 gold_assert(local_symcount * symsize == off - startoff);
813
814 off_t dynoff;
815 size_t dyn_global_index;
816 size_t dyncount;
817 if (this->dynsym_section_ == NULL)
818 {
819 dynoff = 0;
820 dyn_global_index = 0;
821 dyncount = 0;
822 }
823 else
824 {
825 dyn_global_index = this->dynsym_section_->info();
826 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
827 dynoff = this->dynsym_section_->offset() + locsize;
828 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
829 gold_assert(dyncount * symsize
830 == this->dynsym_section_->data_size() - locsize);
831 }
832
833 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
834 dyncount, &this->sympool_);
835
836 this->sympool_.set_string_offsets();
837
838 const char* symtab_name = this->namepool_.add(".symtab", NULL);
839 Output_section* osymtab = this->make_output_section(symtab_name,
840 elfcpp::SHT_SYMTAB,
841 0);
842 this->symtab_section_ = osymtab;
843
844 Output_section_data* pos = new Output_data_space(off - startoff,
845 align);
846 osymtab->add_output_section_data(pos);
847
848 const char* strtab_name = this->namepool_.add(".strtab", NULL);
849 Output_section* ostrtab = this->make_output_section(strtab_name,
850 elfcpp::SHT_STRTAB,
851 0);
852
853 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
854 ostrtab->add_output_section_data(pstr);
855
856 osymtab->set_address(0, startoff);
857 osymtab->set_link_section(ostrtab);
858 osymtab->set_info(local_symcount);
859 osymtab->set_entsize(symsize);
860
861 *poff = off;
862 }
863
864 // Create the .shstrtab section, which holds the names of the
865 // sections. At the time this is called, we have created all the
866 // output sections except .shstrtab itself.
867
868 Output_section*
869 Layout::create_shstrtab()
870 {
871 // FIXME: We don't need to create a .shstrtab section if we are
872 // stripping everything.
873
874 const char* name = this->namepool_.add(".shstrtab", NULL);
875
876 this->namepool_.set_string_offsets();
877
878 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
879
880 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
881 os->add_output_section_data(posd);
882
883 return os;
884 }
885
886 // Create the section headers. SIZE is 32 or 64. OFF is the file
887 // offset.
888
889 Output_section_headers*
890 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
891 {
892 Output_section_headers* oshdrs;
893 oshdrs = new Output_section_headers(size, big_endian, this,
894 &this->segment_list_,
895 &this->unattached_section_list_,
896 &this->namepool_);
897 off_t off = align_address(*poff, oshdrs->addralign());
898 oshdrs->set_address(0, off);
899 off += oshdrs->data_size();
900 *poff = off;
901 this->special_output_list_.push_back(oshdrs);
902 return oshdrs;
903 }
904
905 // Create the dynamic symbol table.
906
907 void
908 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
909 Output_section **pdynstr,
910 unsigned int* plocal_dynamic_count,
911 std::vector<Symbol*>* pdynamic_symbols,
912 Versions* pversions)
913 {
914 // Count all the symbols in the dynamic symbol table, and set the
915 // dynamic symbol indexes.
916
917 // Skip symbol 0, which is always all zeroes.
918 unsigned int index = 1;
919
920 // Add STT_SECTION symbols for each Output section which needs one.
921 for (Section_list::iterator p = this->section_list_.begin();
922 p != this->section_list_.end();
923 ++p)
924 {
925 if (!(*p)->needs_dynsym_index())
926 (*p)->set_dynsym_index(-1U);
927 else
928 {
929 (*p)->set_dynsym_index(index);
930 ++index;
931 }
932 }
933
934 // FIXME: Some targets apparently require local symbols in the
935 // dynamic symbol table. Here is where we will have to count them,
936 // and set the dynamic symbol indexes, and add the names to
937 // this->dynpool_.
938
939 unsigned int local_symcount = index;
940 *plocal_dynamic_count = local_symcount;
941
942 // FIXME: We have to tell set_dynsym_indexes whether the
943 // -E/--export-dynamic option was used.
944 index = symtab->set_dynsym_indexes(&this->options_, target, index,
945 pdynamic_symbols, &this->dynpool_,
946 pversions);
947
948 int symsize;
949 unsigned int align;
950 const int size = target->get_size();
951 if (size == 32)
952 {
953 symsize = elfcpp::Elf_sizes<32>::sym_size;
954 align = 4;
955 }
956 else if (size == 64)
957 {
958 symsize = elfcpp::Elf_sizes<64>::sym_size;
959 align = 8;
960 }
961 else
962 gold_unreachable();
963
964 // Create the dynamic symbol table section.
965
966 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
967 Output_section* dynsym = this->make_output_section(dynsym_name,
968 elfcpp::SHT_DYNSYM,
969 elfcpp::SHF_ALLOC);
970
971 Output_section_data* odata = new Output_data_space(index * symsize,
972 align);
973 dynsym->add_output_section_data(odata);
974
975 dynsym->set_info(local_symcount);
976 dynsym->set_entsize(symsize);
977 dynsym->set_addralign(align);
978
979 this->dynsym_section_ = dynsym;
980
981 Output_data_dynamic* const odyn = this->dynamic_data_;
982 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
983 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
984
985 // Create the dynamic string table section.
986
987 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
988 Output_section* dynstr = this->make_output_section(dynstr_name,
989 elfcpp::SHT_STRTAB,
990 elfcpp::SHF_ALLOC);
991
992 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
993 dynstr->add_output_section_data(strdata);
994
995 dynsym->set_link_section(dynstr);
996 this->dynamic_section_->set_link_section(dynstr);
997
998 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
999 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1000
1001 *pdynstr = dynstr;
1002
1003 // Create the hash tables.
1004
1005 // FIXME: We need an option to create a GNU hash table.
1006
1007 unsigned char* phash;
1008 unsigned int hashlen;
1009 Dynobj::create_elf_hash_table(target, *pdynamic_symbols, local_symcount,
1010 &phash, &hashlen);
1011
1012 const char* hash_name = this->namepool_.add(".hash", NULL);
1013 Output_section* hashsec = this->make_output_section(hash_name,
1014 elfcpp::SHT_HASH,
1015 elfcpp::SHF_ALLOC);
1016
1017 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1018 hashlen,
1019 align);
1020 hashsec->add_output_section_data(hashdata);
1021
1022 hashsec->set_link_section(dynsym);
1023 hashsec->set_entsize(4);
1024
1025 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1026 }
1027
1028 // Create the version sections.
1029
1030 void
1031 Layout::create_version_sections(const Target* target, const Versions* versions,
1032 unsigned int local_symcount,
1033 const std::vector<Symbol*>& dynamic_symbols,
1034 const Output_section* dynstr)
1035 {
1036 if (!versions->any_defs() && !versions->any_needs())
1037 return;
1038
1039 if (target->get_size() == 32)
1040 {
1041 if (target->is_big_endian())
1042 {
1043 #ifdef HAVE_TARGET_32_BIG
1044 this->sized_create_version_sections
1045 SELECT_SIZE_ENDIAN_NAME(32, true)(
1046 versions, local_symcount, dynamic_symbols, dynstr
1047 SELECT_SIZE_ENDIAN(32, true));
1048 #else
1049 gold_unreachable();
1050 #endif
1051 }
1052 else
1053 {
1054 #ifdef HAVE_TARGET_32_LITTLE
1055 this->sized_create_version_sections
1056 SELECT_SIZE_ENDIAN_NAME(32, false)(
1057 versions, local_symcount, dynamic_symbols, dynstr
1058 SELECT_SIZE_ENDIAN(32, false));
1059 #else
1060 gold_unreachable();
1061 #endif
1062 }
1063 }
1064 else if (target->get_size() == 64)
1065 {
1066 if (target->is_big_endian())
1067 {
1068 #ifdef HAVE_TARGET_64_BIG
1069 this->sized_create_version_sections
1070 SELECT_SIZE_ENDIAN_NAME(64, true)(
1071 versions, local_symcount, dynamic_symbols, dynstr
1072 SELECT_SIZE_ENDIAN(64, true));
1073 #else
1074 gold_unreachable();
1075 #endif
1076 }
1077 else
1078 {
1079 #ifdef HAVE_TARGET_64_LITTLE
1080 this->sized_create_version_sections
1081 SELECT_SIZE_ENDIAN_NAME(64, false)(
1082 versions, local_symcount, dynamic_symbols, dynstr
1083 SELECT_SIZE_ENDIAN(64, false));
1084 #else
1085 gold_unreachable();
1086 #endif
1087 }
1088 }
1089 else
1090 gold_unreachable();
1091 }
1092
1093 // Create the version sections, sized version.
1094
1095 template<int size, bool big_endian>
1096 void
1097 Layout::sized_create_version_sections(
1098 const Versions* versions,
1099 unsigned int local_symcount,
1100 const std::vector<Symbol*>& dynamic_symbols,
1101 const Output_section* dynstr
1102 ACCEPT_SIZE_ENDIAN)
1103 {
1104 const char* vname = this->namepool_.add(".gnu.version", NULL);
1105 Output_section* vsec = this->make_output_section(vname,
1106 elfcpp::SHT_GNU_versym,
1107 elfcpp::SHF_ALLOC);
1108
1109 unsigned char* vbuf;
1110 unsigned int vsize;
1111 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1112 &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1113 SELECT_SIZE_ENDIAN(size, big_endian));
1114
1115 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1116
1117 vsec->add_output_section_data(vdata);
1118 vsec->set_entsize(2);
1119 vsec->set_link_section(this->dynsym_section_);
1120
1121 Output_data_dynamic* const odyn = this->dynamic_data_;
1122 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1123
1124 if (versions->any_defs())
1125 {
1126 const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
1127 Output_section *vdsec;
1128 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1129 elfcpp::SHF_ALLOC);
1130
1131 unsigned char* vdbuf;
1132 unsigned int vdsize;
1133 unsigned int vdentries;
1134 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1135 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1136 SELECT_SIZE_ENDIAN(size, big_endian));
1137
1138 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1139 vdsize,
1140 4);
1141
1142 vdsec->add_output_section_data(vddata);
1143 vdsec->set_link_section(dynstr);
1144 vdsec->set_info(vdentries);
1145
1146 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1147 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1148 }
1149
1150 if (versions->any_needs())
1151 {
1152 const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
1153 Output_section* vnsec;
1154 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1155 elfcpp::SHF_ALLOC);
1156
1157 unsigned char* vnbuf;
1158 unsigned int vnsize;
1159 unsigned int vnentries;
1160 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1161 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1162 SELECT_SIZE_ENDIAN(size, big_endian));
1163
1164 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1165 vnsize,
1166 4);
1167
1168 vnsec->add_output_section_data(vndata);
1169 vnsec->set_link_section(dynstr);
1170 vnsec->set_info(vnentries);
1171
1172 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1173 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1174 }
1175 }
1176
1177 // Create the .interp section and PT_INTERP segment.
1178
1179 void
1180 Layout::create_interp(const Target* target)
1181 {
1182 const char* interp = this->options_.dynamic_linker();
1183 if (interp == NULL)
1184 {
1185 interp = target->dynamic_linker();
1186 gold_assert(interp != NULL);
1187 }
1188
1189 size_t len = strlen(interp) + 1;
1190
1191 Output_section_data* odata = new Output_data_const(interp, len, 1);
1192
1193 const char* interp_name = this->namepool_.add(".interp", NULL);
1194 Output_section* osec = this->make_output_section(interp_name,
1195 elfcpp::SHT_PROGBITS,
1196 elfcpp::SHF_ALLOC);
1197 osec->add_output_section_data(odata);
1198
1199 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1200 this->segment_list_.push_back(oseg);
1201 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1202 }
1203
1204 // Finish the .dynamic section and PT_DYNAMIC segment.
1205
1206 void
1207 Layout::finish_dynamic_section(const Input_objects* input_objects,
1208 const Symbol_table* symtab)
1209 {
1210 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1211 elfcpp::PF_R | elfcpp::PF_W);
1212 this->segment_list_.push_back(oseg);
1213 oseg->add_initial_output_section(this->dynamic_section_,
1214 elfcpp::PF_R | elfcpp::PF_W);
1215
1216 Output_data_dynamic* const odyn = this->dynamic_data_;
1217
1218 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1219 p != input_objects->dynobj_end();
1220 ++p)
1221 {
1222 // FIXME: Handle --as-needed.
1223 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1224 }
1225
1226 // FIXME: Support --init and --fini.
1227 Symbol* sym = symtab->lookup("_init");
1228 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1229 odyn->add_symbol(elfcpp::DT_INIT, sym);
1230
1231 sym = symtab->lookup("_fini");
1232 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1233 odyn->add_symbol(elfcpp::DT_FINI, sym);
1234
1235 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1236
1237 // Add a DT_RPATH entry if needed.
1238 const General_options::Dir_list& rpath(this->options_.rpath());
1239 if (!rpath.empty())
1240 {
1241 std::string rpath_val;
1242 for (General_options::Dir_list::const_iterator p = rpath.begin();
1243 p != rpath.end();
1244 ++p)
1245 {
1246 if (rpath_val.empty())
1247 rpath_val = *p;
1248 else
1249 {
1250 // Eliminate duplicates.
1251 General_options::Dir_list::const_iterator q;
1252 for (q = rpath.begin(); q != p; ++q)
1253 if (strcmp(*q, *p) == 0)
1254 break;
1255 if (q == p)
1256 {
1257 rpath_val += ':';
1258 rpath_val += *p;
1259 }
1260 }
1261 }
1262
1263 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1264 }
1265 }
1266
1267 // The mapping of .gnu.linkonce section names to real section names.
1268
1269 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1270 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1271 {
1272 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1273 MAPPING_INIT("t", ".text"),
1274 MAPPING_INIT("r", ".rodata"),
1275 MAPPING_INIT("d", ".data"),
1276 MAPPING_INIT("b", ".bss"),
1277 MAPPING_INIT("s", ".sdata"),
1278 MAPPING_INIT("sb", ".sbss"),
1279 MAPPING_INIT("s2", ".sdata2"),
1280 MAPPING_INIT("sb2", ".sbss2"),
1281 MAPPING_INIT("wi", ".debug_info"),
1282 MAPPING_INIT("td", ".tdata"),
1283 MAPPING_INIT("tb", ".tbss"),
1284 MAPPING_INIT("lr", ".lrodata"),
1285 MAPPING_INIT("l", ".ldata"),
1286 MAPPING_INIT("lb", ".lbss"),
1287 };
1288 #undef MAPPING_INIT
1289
1290 const int Layout::linkonce_mapping_count =
1291 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1292
1293 // Return the name of the output section to use for a .gnu.linkonce
1294 // section. This is based on the default ELF linker script of the old
1295 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1296 // to ".text". Set *PLEN to the length of the name. *PLEN is
1297 // initialized to the length of NAME.
1298
1299 const char*
1300 Layout::linkonce_output_name(const char* name, size_t *plen)
1301 {
1302 const char* s = name + sizeof(".gnu.linkonce") - 1;
1303 if (*s != '.')
1304 return name;
1305 ++s;
1306 const Linkonce_mapping* plm = linkonce_mapping;
1307 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1308 {
1309 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1310 {
1311 *plen = plm->tolen;
1312 return plm->to;
1313 }
1314 }
1315 return name;
1316 }
1317
1318 // Choose the output section name to use given an input section name.
1319 // Set *PLEN to the length of the name. *PLEN is initialized to the
1320 // length of NAME.
1321
1322 const char*
1323 Layout::output_section_name(const char* name, size_t* plen)
1324 {
1325 if (Layout::is_linkonce(name))
1326 {
1327 // .gnu.linkonce sections are laid out as though they were named
1328 // for the sections are placed into.
1329 return Layout::linkonce_output_name(name, plen);
1330 }
1331
1332 // If the section name has no '.', or only an initial '.', we use
1333 // the name unchanged (i.e., ".text" is unchanged).
1334
1335 // Otherwise, if the section name does not include ".rel", we drop
1336 // the last '.' and everything that follows (i.e., ".text.XXX"
1337 // becomes ".text").
1338
1339 // Otherwise, if the section name has zero or one '.' after the
1340 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1341 // unchanged).
1342
1343 // Otherwise, we drop the last '.' and everything that follows
1344 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1345
1346 const char* s = name;
1347 if (*s == '.')
1348 ++s;
1349 const char* sdot = strchr(s, '.');
1350 if (sdot == NULL)
1351 return name;
1352
1353 const char* srel = strstr(s, ".rel");
1354 if (srel == NULL)
1355 {
1356 *plen = sdot - name;
1357 return name;
1358 }
1359
1360 sdot = strchr(srel + 1, '.');
1361 if (sdot == NULL)
1362 return name;
1363 sdot = strchr(sdot + 1, '.');
1364 if (sdot == NULL)
1365 return name;
1366
1367 *plen = sdot - name;
1368 return name;
1369 }
1370
1371 // Record the signature of a comdat section, and return whether to
1372 // include it in the link. If GROUP is true, this is a regular
1373 // section group. If GROUP is false, this is a group signature
1374 // derived from the name of a linkonce section. We want linkonce
1375 // signatures and group signatures to block each other, but we don't
1376 // want a linkonce signature to block another linkonce signature.
1377
1378 bool
1379 Layout::add_comdat(const char* signature, bool group)
1380 {
1381 std::string sig(signature);
1382 std::pair<Signatures::iterator, bool> ins(
1383 this->signatures_.insert(std::make_pair(sig, group)));
1384
1385 if (ins.second)
1386 {
1387 // This is the first time we've seen this signature.
1388 return true;
1389 }
1390
1391 if (ins.first->second)
1392 {
1393 // We've already seen a real section group with this signature.
1394 return false;
1395 }
1396 else if (group)
1397 {
1398 // This is a real section group, and we've already seen a
1399 // linkonce section with tihs signature. Record that we've seen
1400 // a section group, and don't include this section group.
1401 ins.first->second = true;
1402 return false;
1403 }
1404 else
1405 {
1406 // We've already seen a linkonce section and this is a linkonce
1407 // section. These don't block each other--this may be the same
1408 // symbol name with different section types.
1409 return true;
1410 }
1411 }
1412
1413 // Write out data not associated with a section or the symbol table.
1414
1415 void
1416 Layout::write_data(const Symbol_table* symtab, const Target* target,
1417 Output_file* of) const
1418 {
1419 const Output_section* symtab_section = this->symtab_section_;
1420 for (Section_list::const_iterator p = this->section_list_.begin();
1421 p != this->section_list_.end();
1422 ++p)
1423 {
1424 if ((*p)->needs_symtab_index())
1425 {
1426 gold_assert(symtab_section != NULL);
1427 unsigned int index = (*p)->symtab_index();
1428 gold_assert(index > 0 && index != -1U);
1429 off_t off = (symtab_section->offset()
1430 + index * symtab_section->entsize());
1431 symtab->write_section_symbol(target, *p, of, off);
1432 }
1433 }
1434
1435 const Output_section* dynsym_section = this->dynsym_section_;
1436 for (Section_list::const_iterator p = this->section_list_.begin();
1437 p != this->section_list_.end();
1438 ++p)
1439 {
1440 if ((*p)->needs_dynsym_index())
1441 {
1442 gold_assert(dynsym_section != NULL);
1443 unsigned int index = (*p)->dynsym_index();
1444 gold_assert(index > 0 && index != -1U);
1445 off_t off = (dynsym_section->offset()
1446 + index * dynsym_section->entsize());
1447 symtab->write_section_symbol(target, *p, of, off);
1448 }
1449 }
1450
1451 // Write out the Output_sections. Most won't have anything to
1452 // write, since most of the data will come from input sections which
1453 // are handled elsewhere. But some Output_sections do have
1454 // Output_data.
1455 for (Section_list::const_iterator p = this->section_list_.begin();
1456 p != this->section_list_.end();
1457 ++p)
1458 (*p)->write(of);
1459
1460 // Write out the Output_data which are not in an Output_section.
1461 for (Data_list::const_iterator p = this->special_output_list_.begin();
1462 p != this->special_output_list_.end();
1463 ++p)
1464 (*p)->write(of);
1465 }
1466
1467 // Write_data_task methods.
1468
1469 // We can always run this task.
1470
1471 Task::Is_runnable_type
1472 Write_data_task::is_runnable(Workqueue*)
1473 {
1474 return IS_RUNNABLE;
1475 }
1476
1477 // We need to unlock FINAL_BLOCKER when finished.
1478
1479 Task_locker*
1480 Write_data_task::locks(Workqueue* workqueue)
1481 {
1482 return new Task_locker_block(*this->final_blocker_, workqueue);
1483 }
1484
1485 // Run the task--write out the data.
1486
1487 void
1488 Write_data_task::run(Workqueue*)
1489 {
1490 this->layout_->write_data(this->symtab_, this->target_, this->of_);
1491 }
1492
1493 // Write_symbols_task methods.
1494
1495 // We can always run this task.
1496
1497 Task::Is_runnable_type
1498 Write_symbols_task::is_runnable(Workqueue*)
1499 {
1500 return IS_RUNNABLE;
1501 }
1502
1503 // We need to unlock FINAL_BLOCKER when finished.
1504
1505 Task_locker*
1506 Write_symbols_task::locks(Workqueue* workqueue)
1507 {
1508 return new Task_locker_block(*this->final_blocker_, workqueue);
1509 }
1510
1511 // Run the task--write out the symbols.
1512
1513 void
1514 Write_symbols_task::run(Workqueue*)
1515 {
1516 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1517 this->of_);
1518 }
1519
1520 // Close_task_runner methods.
1521
1522 // Run the task--close the file.
1523
1524 void
1525 Close_task_runner::run(Workqueue*)
1526 {
1527 this->of_->close();
1528 }
1529
1530 // Instantiate the templates we need. We could use the configure
1531 // script to restrict this to only the ones for implemented targets.
1532
1533 #ifdef HAVE_TARGET_32_LITTLE
1534 template
1535 Output_section*
1536 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1537 const elfcpp::Shdr<32, false>& shdr, off_t*);
1538 #endif
1539
1540 #ifdef HAVE_TARGET_32_BIG
1541 template
1542 Output_section*
1543 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1544 const elfcpp::Shdr<32, true>& shdr, off_t*);
1545 #endif
1546
1547 #ifdef HAVE_TARGET_64_LITTLE
1548 template
1549 Output_section*
1550 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1551 const elfcpp::Shdr<64, false>& shdr, off_t*);
1552 #endif
1553
1554 #ifdef HAVE_TARGET_64_BIG
1555 template
1556 Output_section*
1557 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1558 const elfcpp::Shdr<64, true>& shdr, off_t*);
1559 #endif
1560
1561
1562 } // End namespace gold.