2006-11-29 Paul Brook <paul@codesourcery.com>
[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"
f6ce93d6 12#include "symtab.h"
a2fb1b05
ILT
13#include "layout.h"
14
15namespace gold
16{
17
92e059d8 18// Layout_task_runner methods.
a2fb1b05
ILT
19
20// Lay out the sections. This is called after all the input objects
21// have been read.
22
23void
92e059d8 24Layout_task_runner::run(Workqueue* workqueue)
a2fb1b05 25{
12e14209
ILT
26 off_t file_size = this->layout_->finalize(this->input_objects_,
27 this->symtab_);
61ba1cf9
ILT
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_,
12e14209 36 this->symtab_, this->layout_, workqueue, of);
a2fb1b05
ILT
37}
38
39// Layout methods.
40
54dc6425 41Layout::Layout(const General_options& options)
ead1e424 42 : options_(options), namepool_(), sympool_(), signatures_(),
61ba1cf9 43 section_name_map_(), segment_list_(), section_list_(),
92e059d8 44 special_output_list_(), tls_segment_(NULL)
54dc6425
ILT
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
a2fb1b05
ILT
51// Hash a key we use to look up an output section mapping.
52
53size_t
54Layout::Hash_key::operator()(const Layout::Key& k) const
55{
f0641a0b 56 return k.first + k.second.first + k.second.second;
a2fb1b05
ILT
57}
58
59// Whether to include this section in the link.
60
61template<int size, bool big_endian>
62bool
63Layout::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
ead1e424 90// Return an output section named NAME, or NULL if there is none.
a2fb1b05 91
a2fb1b05 92Output_section*
ead1e424 93Layout::find_output_section(const char* name) const
a2fb1b05 94{
ead1e424
ILT
95 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
96 p != this->section_name_map_.end();
97 ++p)
f0641a0b 98 if (strcmp(p->second->name(), name) == 0)
ead1e424
ILT
99 return p->second;
100 return NULL;
101}
a2fb1b05 102
ead1e424
ILT
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.
a2fb1b05 105
ead1e424
ILT
106Output_segment*
107Layout::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}
a2fb1b05 119
ead1e424
ILT
120// Return the output section to use for section NAME with type TYPE
121// and section flags FLAGS.
a2fb1b05 122
ead1e424 123Output_section*
f0641a0b
ILT
124Layout::get_output_section(const char* name, Stringpool::Key name_key,
125 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
ead1e424
ILT
126{
127 // We should ignore some flags.
128 flags &= ~ (elfcpp::SHF_INFO_LINK
129 | elfcpp::SHF_LINK_ORDER
130 | elfcpp::SHF_GROUP);
a2fb1b05 131
f0641a0b 132 const Key key(name_key, std::make_pair(type, flags));
a2fb1b05
ILT
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
a2fb1b05 137 if (!ins.second)
ead1e424 138 return ins.first->second;
a2fb1b05
ILT
139 else
140 {
141 // This is the first time we've seen this name/type/flags
142 // combination.
ead1e424 143 Output_section* os = this->make_output_section(name, type, flags);
a2fb1b05 144 ins.first->second = os;
ead1e424 145 return os;
a2fb1b05 146 }
ead1e424
ILT
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
153template<int size, bool big_endian>
154Output_section*
f6ce93d6 155Layout::layout(Relobj* object, unsigned int shndx, const char* name,
ead1e424
ILT
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.
f0641a0b
ILT
170 Stringpool::Key name_key;
171 name = this->namepool_.add(name, len, &name_key);
ead1e424
ILT
172
173 // Find the output section. The output section is selected based on
174 // the section name, type, and flags.
f0641a0b
ILT
175 Output_section* os = this->get_output_section(name, name_key,
176 shdr.get_sh_type(),
ead1e424 177 shdr.get_sh_flags());
a2fb1b05
ILT
178
179 // FIXME: Handle SHF_LINK_ORDER somewhere.
180
ead1e424 181 *off = os->add_input_section(object, shndx, name, shdr);
a2fb1b05
ILT
182
183 return os;
184}
185
ead1e424
ILT
186// Add POSD to an output section using NAME, TYPE, and FLAGS.
187
188void
189Layout::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.
f0641a0b
ILT
194 Stringpool::Key name_key;
195 name = this->namepool_.add(name, &name_key);
ead1e424 196
f0641a0b 197 Output_section* os = this->get_output_section(name, name_key, type, flags);
ead1e424
ILT
198 os->add_output_section_data(posd);
199}
200
a2fb1b05
ILT
201// Map section flags to segment flags.
202
203elfcpp::Elf_Word
204Layout::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
217Output_section*
218Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
219 elfcpp::Elf_Xword flags)
220{
ead1e424 221 Output_section* os = new Output_section(name, type, flags, true);
a2fb1b05
ILT
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 {
75f65a3e 245 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
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);
75f65a3e 255 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
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 {
75f65a3e 271 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
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);
75f65a3e 281 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
282 }
283 }
54dc6425
ILT
284
285 // If we see a loadable SHF_TLS section, we create a PT_TLS
92e059d8 286 // segment. There can only be one such segment.
54dc6425
ILT
287 if ((flags & elfcpp::SHF_TLS) != 0)
288 {
92e059d8 289 if (this->tls_segment_ == NULL)
54dc6425 290 {
92e059d8
ILT
291 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
292 seg_flags);
293 this->segment_list_.push_back(this->tls_segment_);
54dc6425 294 }
92e059d8 295 this->tls_segment_->add_output_section(os, seg_flags);
54dc6425 296 }
a2fb1b05
ILT
297 }
298
299 return os;
300}
301
75f65a3e
ILT
302// Find the first read-only PT_LOAD segment, creating one if
303// necessary.
54dc6425 304
75f65a3e
ILT
305Output_segment*
306Layout::find_first_load_seg()
54dc6425 307{
75f65a3e
ILT
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;
54dc6425
ILT
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
75f65a3e
ILT
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
54dc6425
ILT
343// value and make a final determination of which symbols are going
344// into the output symbol table.
345
54dc6425
ILT
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
75f65a3e
ILT
353// This function returns the size of the output file.
354
355off_t
356Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
54dc6425 357{
dbe717ef
ILT
358 const int size = input_objects->target()->get_size();
359
360 Output_segment* phdr_seg = NULL;
54dc6425
ILT
361 if (input_objects->any_dynamic())
362 {
dbe717ef
ILT
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());
54dc6425
ILT
382 }
383
384 // FIXME: Handle PT_GNU_STACK.
385
75f65a3e
ILT
386 Output_segment* load_seg = this->find_first_load_seg();
387
388 // Lay out the segment headers.
61ba1cf9 389 bool big_endian = input_objects->target()->is_big_endian();
75f65a3e 390 Output_segment_headers* segment_headers;
61ba1cf9
ILT
391 segment_headers = new Output_segment_headers(size, big_endian,
392 this->segment_list_);
75f65a3e 393 load_seg->add_initial_output_data(segment_headers);
61ba1cf9 394 this->special_output_list_.push_back(segment_headers);
dbe717ef
ILT
395 if (phdr_seg != NULL)
396 phdr_seg->add_initial_output_data(segment_headers);
75f65a3e
ILT
397
398 // Lay out the file header.
399 Output_file_header* file_header;
400 file_header = new Output_file_header(size,
61ba1cf9 401 big_endian,
75f65a3e
ILT
402 this->options_,
403 input_objects->target(),
404 symtab,
405 segment_headers);
406 load_seg->add_initial_output_data(file_header);
61ba1cf9 407 this->special_output_list_.push_back(file_header);
75f65a3e 408
ead1e424
ILT
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);
75f65a3e
ILT
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;
61ba1cf9
ILT
422 this->create_symtab_sections(size, input_objects, symtab, &off,
423 &osymtab, &ostrtab);
75f65a3e
ILT
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.
ead1e424
ILT
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());
75f65a3e
ILT
434
435 // Create the section table header.
61ba1cf9 436 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
75f65a3e
ILT
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
449bool
450Layout::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
92e059d8
ILT
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
75f65a3e
ILT
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
ead1e424
ILT
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.
75f65a3e
ILT
532
533off_t
ead1e424
ILT
534Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
535 unsigned int *pshndx)
75f65a3e
ILT
536{
537 // Sort them into the final order.
54dc6425
ILT
538 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
539 Layout::Compare_segments());
540
75f65a3e
ILT
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 {
ead1e424 569 uint64_t align = (*p)->addralign();
75f65a3e 570
ead1e424 571 addr = align_address(addr, align);
75f65a3e
ILT
572 aligned_addr = addr;
573 if ((addr & (abi_pagesize - 1)) != 0)
574 addr = addr + abi_pagesize;
575 }
576
ead1e424 577 unsigned int shndx_hold = *pshndx;
75f65a3e 578 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 579 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
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 {
ead1e424
ILT
600 *pshndx = shndx_hold;
601 addr = align_address(aligned_addr, common_pagesize);
75f65a3e 602 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 603 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
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
630off_t
ead1e424 631Layout::set_section_offsets(off_t off, unsigned int* pshndx)
75f65a3e
ILT
632{
633 for (Layout::Section_list::iterator p = this->section_list_.begin();
634 p != this->section_list_.end();
635 ++p)
636 {
ead1e424
ILT
637 (*p)->set_out_shndx(*pshndx);
638 ++*pshndx;
61ba1cf9
ILT
639 if ((*p)->offset() != -1)
640 continue;
ead1e424 641 off = align_address(off, (*p)->addralign());
75f65a3e
ILT
642 (*p)->set_address(0, off);
643 off += (*p)->data_size();
644 }
645 return off;
646}
647
648// Create the symbol table sections.
649
650void
61ba1cf9 651Layout::create_symtab_sections(int size, const Input_objects* input_objects,
75f65a3e 652 Symbol_table* symtab,
61ba1cf9 653 off_t* poff,
75f65a3e
ILT
654 Output_section** posymtab,
655 Output_section** postrtab)
656{
61ba1cf9
ILT
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;
ead1e424 673 off = align_address(off, align);
61ba1cf9
ILT
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;
c06b7b0b 679 unsigned int local_symbol_index = 1;
61ba1cf9 680
f6ce93d6
ILT
681 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
682 p != input_objects->relobj_end();
75f65a3e
ILT
683 ++p)
684 {
685 Task_lock_obj<Object> tlo(**p);
c06b7b0b
ILT
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;
75f65a3e
ILT
691 }
692
c06b7b0b 693 unsigned int local_symcount = local_symbol_index;
61ba1cf9
ILT
694 assert(local_symcount * symsize == off - startoff);
695
c06b7b0b 696 off = symtab->finalize(local_symcount, off, &this->sympool_);
75f65a3e 697
61ba1cf9
ILT
698 this->sympool_.set_string_offsets();
699
f0641a0b 700 const char* symtab_name = this->namepool_.add(".symtab", NULL);
61ba1cf9 701 Output_section* osymtab = new Output_section_symtab(symtab_name,
ead1e424 702 off - startoff);
61ba1cf9
ILT
703 this->section_list_.push_back(osymtab);
704
f0641a0b 705 const char* strtab_name = this->namepool_.add(".strtab", NULL);
61ba1cf9 706 Output_section *ostrtab = new Output_section_strtab(strtab_name,
ead1e424 707 &this->sympool_);
61ba1cf9
ILT
708 this->section_list_.push_back(ostrtab);
709 this->special_output_list_.push_back(ostrtab);
710
711 osymtab->set_address(0, startoff);
61ba1cf9
ILT
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;
75f65a3e
ILT
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
725Output_section*
726Layout::create_shstrtab()
727{
728 // FIXME: We don't need to create a .shstrtab section if we are
729 // stripping everything.
730
f0641a0b 731 const char* name = this->namepool_.add(".shstrtab", NULL);
75f65a3e 732
61ba1cf9
ILT
733 this->namepool_.set_string_offsets();
734
ead1e424 735 Output_section* os = new Output_section_strtab(name, &this->namepool_);
75f65a3e
ILT
736
737 this->section_list_.push_back(os);
61ba1cf9 738 this->special_output_list_.push_back(os);
75f65a3e
ILT
739
740 return os;
741}
742
743// Create the section headers. SIZE is 32 or 64. OFF is the file
744// offset.
745
746Output_section_headers*
61ba1cf9 747Layout::create_shdrs(int size, bool big_endian, off_t* poff)
75f65a3e
ILT
748{
749 Output_section_headers* oshdrs;
61ba1cf9
ILT
750 oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
751 this->section_list_,
752 &this->namepool_);
ead1e424 753 off_t off = align_address(*poff, oshdrs->addralign());
75f65a3e 754 oshdrs->set_address(0, off);
61ba1cf9
ILT
755 off += oshdrs->data_size();
756 *poff = off;
757 this->special_output_list_.push_back(oshdrs);
75f65a3e 758 return oshdrs;
54dc6425
ILT
759}
760
dbe717ef
ILT
761// Create the dynamic symbol table.
762
763void
764Layout::create_dynamic_symtab(int, Symbol_table*)
765{
766 abort();
767}
768
769// Create the .dynamic section and PT_DYNAMIC segment.
770
771void
772Layout::create_dynamic_section()
773{
774 abort();
775}
776
777// Create the .interp section and PT_INTERP segment.
778
779void
780Layout::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
a2fb1b05
ILT
804// The mapping of .gnu.linkonce section names to real section names.
805
ead1e424 806#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
a2fb1b05
ILT
807const 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
827const 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"
ead1e424
ILT
833// to ".text". Set *PLEN to the length of the name. *PLEN is
834// initialized to the length of NAME.
a2fb1b05
ILT
835
836const char*
ead1e424 837Layout::linkonce_output_name(const char* name, size_t *plen)
a2fb1b05
ILT
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] == '.')
ead1e424
ILT
847 {
848 *plen = plm->tolen;
849 return plm->to;
850 }
a2fb1b05
ILT
851 }
852 return name;
853}
854
ead1e424
ILT
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
859const char*
860Layout::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
a2fb1b05
ILT
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
915bool
916Layout::add_comdat(const char* signature, bool group)
917{
918 std::string sig(signature);
919 std::pair<Signatures::iterator, bool> ins(
ead1e424 920 this->signatures_.insert(std::make_pair(sig, group)));
a2fb1b05
ILT
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
61ba1cf9
ILT
950// Write out data not associated with a section or the symbol table.
951
952void
953Layout::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
965Task::Is_runnable_type
966Write_data_task::is_runnable(Workqueue*)
967{
968 return IS_RUNNABLE;
969}
970
971// We need to unlock FINAL_BLOCKER when finished.
972
973Task_locker*
974Write_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
981void
982Write_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
991Task::Is_runnable_type
992Write_symbols_task::is_runnable(Workqueue*)
993{
994 return IS_RUNNABLE;
995}
996
997// We need to unlock FINAL_BLOCKER when finished.
998
999Task_locker*
1000Write_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
1007void
1008Write_symbols_task::run(Workqueue*)
1009{
1010 this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
1011}
1012
92e059d8 1013// Close_task_runner methods.
61ba1cf9
ILT
1014
1015// Run the task--close the file.
1016
1017void
92e059d8 1018Close_task_runner::run(Workqueue*)
61ba1cf9
ILT
1019{
1020 this->of_->close();
1021}
1022
a2fb1b05
ILT
1023// Instantiate the templates we need. We could use the configure
1024// script to restrict this to only the ones for implemented targets.
1025
1026template
1027Output_section*
f6ce93d6 1028Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05
ILT
1029 const elfcpp::Shdr<32, false>& shdr, off_t*);
1030
1031template
1032Output_section*
f6ce93d6 1033Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05
ILT
1034 const elfcpp::Shdr<32, true>& shdr, off_t*);
1035
1036template
1037Output_section*
f6ce93d6 1038Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05
ILT
1039 const elfcpp::Shdr<64, false>& shdr, off_t*);
1040
1041template
1042Output_section*
f6ce93d6 1043Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05
ILT
1044 const elfcpp::Shdr<64, true>& shdr, off_t*);
1045
1046
1047} // End namespace gold.
This page took 0.159926 seconds and 4 git commands to generate.