Define __start and __stop symbols.
[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
a2fb1b05 5#include <cstring>
54dc6425 6#include <algorithm>
a2fb1b05
ILT
7#include <iostream>
8#include <utility>
9
7e1edb90 10#include "parameters.h"
a2fb1b05 11#include "output.h"
f6ce93d6 12#include "symtab.h"
a3ad94ed 13#include "dynobj.h"
a2fb1b05
ILT
14#include "layout.h"
15
16namespace gold
17{
18
92e059d8 19// Layout_task_runner methods.
a2fb1b05
ILT
20
21// Lay out the sections. This is called after all the input objects
22// have been read.
23
24void
92e059d8 25Layout_task_runner::run(Workqueue* workqueue)
a2fb1b05 26{
12e14209
ILT
27 off_t file_size = this->layout_->finalize(this->input_objects_,
28 this->symtab_);
61ba1cf9
ILT
29
30 // Now we know the final size of the output file and we know where
31 // each piece of information goes.
c51e6221
ILT
32 Output_file* of = new Output_file(this->options_,
33 this->input_objects_->target());
61ba1cf9
ILT
34 of->open(file_size);
35
36 // Queue up the final set of tasks.
37 gold::queue_final_tasks(this->options_, this->input_objects_,
12e14209 38 this->symtab_, this->layout_, workqueue, of);
a2fb1b05
ILT
39}
40
41// Layout methods.
42
54dc6425 43Layout::Layout(const General_options& options)
a3ad94ed 44 : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
61ba1cf9 45 section_name_map_(), segment_list_(), section_list_(),
a3ad94ed 46 unattached_section_list_(), special_output_list_(),
14b31740
ILT
47 tls_segment_(NULL), symtab_section_(NULL),
48 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL)
54dc6425
ILT
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.
a3ad94ed
ILT
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);
54dc6425
ILT
57}
58
a2fb1b05
ILT
59// Hash a key we use to look up an output section mapping.
60
61size_t
62Layout::Hash_key::operator()(const Layout::Key& k) const
63{
f0641a0b 64 return k.first + k.second.first + k.second.second;
a2fb1b05
ILT
65}
66
67// Whether to include this section in the link.
68
69template<int size, bool big_endian>
70bool
71Layout::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:
7e1edb90 90 return parameters->output_is_object();
a2fb1b05
ILT
91
92 default:
93 // FIXME: Handle stripping debug sections here.
94 return true;
95 }
96}
97
ead1e424 98// Return an output section named NAME, or NULL if there is none.
a2fb1b05 99
a2fb1b05 100Output_section*
ead1e424 101Layout::find_output_section(const char* name) const
a2fb1b05 102{
ead1e424
ILT
103 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
104 p != this->section_name_map_.end();
105 ++p)
f0641a0b 106 if (strcmp(p->second->name(), name) == 0)
ead1e424
ILT
107 return p->second;
108 return NULL;
109}
a2fb1b05 110
ead1e424
ILT
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.
a2fb1b05 113
ead1e424
ILT
114Output_segment*
115Layout::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}
a2fb1b05 127
ead1e424
ILT
128// Return the output section to use for section NAME with type TYPE
129// and section flags FLAGS.
a2fb1b05 130
ead1e424 131Output_section*
f0641a0b
ILT
132Layout::get_output_section(const char* name, Stringpool::Key name_key,
133 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
ead1e424
ILT
134{
135 // We should ignore some flags.
136 flags &= ~ (elfcpp::SHF_INFO_LINK
137 | elfcpp::SHF_LINK_ORDER
b8e6aad9
ILT
138 | elfcpp::SHF_GROUP
139 | elfcpp::SHF_MERGE
140 | elfcpp::SHF_STRINGS);
a2fb1b05 141
f0641a0b 142 const Key key(name_key, std::make_pair(type, flags));
a2fb1b05
ILT
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
a2fb1b05 147 if (!ins.second)
ead1e424 148 return ins.first->second;
a2fb1b05
ILT
149 else
150 {
151 // This is the first time we've seen this name/type/flags
152 // combination.
ead1e424 153 Output_section* os = this->make_output_section(name, type, flags);
a2fb1b05 154 ins.first->second = os;
ead1e424 155 return os;
a2fb1b05 156 }
ead1e424
ILT
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
163template<int size, bool big_endian>
164Output_section*
f6ce93d6 165Layout::layout(Relobj* object, unsigned int shndx, const char* name,
ead1e424
ILT
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);
7e1edb90 174 if (!parameters->output_is_object())
ead1e424
ILT
175 name = Layout::output_section_name(name, &len);
176
177 // FIXME: Handle SHF_OS_NONCONFORMING here.
178
179 // Canonicalize the section name.
f0641a0b
ILT
180 Stringpool::Key name_key;
181 name = this->namepool_.add(name, len, &name_key);
ead1e424
ILT
182
183 // Find the output section. The output section is selected based on
184 // the section name, type, and flags.
f0641a0b
ILT
185 Output_section* os = this->get_output_section(name, name_key,
186 shdr.get_sh_type(),
ead1e424 187 shdr.get_sh_flags());
a2fb1b05
ILT
188
189 // FIXME: Handle SHF_LINK_ORDER somewhere.
190
ead1e424 191 *off = os->add_input_section(object, shndx, name, shdr);
a2fb1b05
ILT
192
193 return os;
194}
195
ead1e424
ILT
196// Add POSD to an output section using NAME, TYPE, and FLAGS.
197
198void
199Layout::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.
f0641a0b
ILT
204 Stringpool::Key name_key;
205 name = this->namepool_.add(name, &name_key);
ead1e424 206
f0641a0b 207 Output_section* os = this->get_output_section(name, name_key, type, flags);
ead1e424
ILT
208 os->add_output_section_data(posd);
209}
210
a2fb1b05
ILT
211// Map section flags to segment flags.
212
213elfcpp::Elf_Word
214Layout::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
227Output_section*
228Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
229 elfcpp::Elf_Xword flags)
230{
b8e6aad9 231 Output_section* os = new Output_section(name, type, flags);
a3ad94ed 232 this->section_list_.push_back(os);
a2fb1b05
ILT
233
234 if ((flags & elfcpp::SHF_ALLOC) == 0)
a3ad94ed 235 this->unattached_section_list_.push_back(os);
a2fb1b05
ILT
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 {
75f65a3e 256 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
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);
75f65a3e 266 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
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 {
75f65a3e 282 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
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);
75f65a3e 292 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
293 }
294 }
54dc6425
ILT
295
296 // If we see a loadable SHF_TLS section, we create a PT_TLS
92e059d8 297 // segment. There can only be one such segment.
54dc6425
ILT
298 if ((flags & elfcpp::SHF_TLS) != 0)
299 {
92e059d8 300 if (this->tls_segment_ == NULL)
54dc6425 301 {
92e059d8
ILT
302 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
303 seg_flags);
304 this->segment_list_.push_back(this->tls_segment_);
54dc6425 305 }
92e059d8 306 this->tls_segment_->add_output_section(os, seg_flags);
54dc6425 307 }
a2fb1b05
ILT
308 }
309
310 return os;
311}
312
a3ad94ed
ILT
313// Create the dynamic sections which are needed before we read the
314// relocs.
315
316void
317Layout::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
14b31740 329 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
a3ad94ed
ILT
330 this->dynamic_section_, 0, 0,
331 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
332 elfcpp::STV_HIDDEN, 0, false, false);
16649710
ILT
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_);
a3ad94ed
ILT
338}
339
bfd58944
ILT
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
344void
345Layout::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
75f65a3e
ILT
392// Find the first read-only PT_LOAD segment, creating one if
393// necessary.
54dc6425 394
75f65a3e
ILT
395Output_segment*
396Layout::find_first_load_seg()
54dc6425 397{
75f65a3e
ILT
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;
54dc6425
ILT
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
75f65a3e
ILT
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
54dc6425
ILT
433// value and make a final determination of which symbols are going
434// into the output symbol table.
435
54dc6425
ILT
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
75f65a3e
ILT
443// This function returns the size of the output file.
444
445off_t
446Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
54dc6425 447{
5a6f7e2d 448 Target* const target = input_objects->target();
a3ad94ed 449 const int size = target->get_size();
dbe717ef 450
7e1edb90 451 target->finalize_sections(this);
5a6f7e2d 452
dbe717ef 453 Output_segment* phdr_seg = NULL;
54dc6425
ILT
454 if (input_objects->any_dynamic())
455 {
dbe717ef
ILT
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
14b31740
ILT
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);
dbe717ef
ILT
472
473 // Create the .interp section to hold the name of the
474 // interpreter, and put it in a PT_INTERP segment.
a3ad94ed
ILT
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.
16649710 479 this->finish_dynamic_section(input_objects, symtab);
14b31740
ILT
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);
54dc6425
ILT
489 }
490
491 // FIXME: Handle PT_GNU_STACK.
492
75f65a3e
ILT
493 Output_segment* load_seg = this->find_first_load_seg();
494
495 // Lay out the segment headers.
a3ad94ed 496 bool big_endian = target->is_big_endian();
75f65a3e 497 Output_segment_headers* segment_headers;
61ba1cf9
ILT
498 segment_headers = new Output_segment_headers(size, big_endian,
499 this->segment_list_);
75f65a3e 500 load_seg->add_initial_output_data(segment_headers);
61ba1cf9 501 this->special_output_list_.push_back(segment_headers);
dbe717ef
ILT
502 if (phdr_seg != NULL)
503 phdr_seg->add_initial_output_data(segment_headers);
75f65a3e
ILT
504
505 // Lay out the file header.
506 Output_file_header* file_header;
507 file_header = new Output_file_header(size,
61ba1cf9 508 big_endian,
a3ad94ed 509 target,
75f65a3e
ILT
510 symtab,
511 segment_headers);
512 load_seg->add_initial_output_data(file_header);
61ba1cf9 513 this->special_output_list_.push_back(file_header);
75f65a3e 514
ead1e424
ILT
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.
a3ad94ed 521 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
75f65a3e
ILT
522
523 // Create the symbol table sections.
16649710 524 this->create_symtab_sections(size, input_objects, symtab, &off);
75f65a3e
ILT
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.
ead1e424
ILT
531 off = this->set_section_offsets(off, &shndx);
532
75f65a3e 533 // Create the section table header.
61ba1cf9 534 Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
75f65a3e
ILT
535
536 file_header->set_section_info(oshdrs, shstrtab_section);
537
538 // Now we know exactly where everything goes in the output file.
a3ad94ed 539 Output_data::layout_complete();
75f65a3e
ILT
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
548bool
549Layout::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 {
a3ad94ed 559 gold_assert(type2 != elfcpp::PT_PHDR);
75f65a3e
ILT
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 {
a3ad94ed 569 gold_assert(type2 != elfcpp::PT_INTERP);
75f65a3e
ILT
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
92e059d8
ILT
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
75f65a3e
ILT
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;
a3ad94ed 599 gold_assert(flags1 != flags2);
75f65a3e
ILT
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();
a3ad94ed 624 gold_assert(paddr1 != paddr2);
75f65a3e
ILT
625 return paddr1 < paddr2;
626}
627
ead1e424
ILT
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.
75f65a3e
ILT
631
632off_t
ead1e424
ILT
633Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
634 unsigned int *pshndx)
75f65a3e
ILT
635{
636 // Sort them into the final order.
54dc6425
ILT
637 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
638 Layout::Compare_segments());
639
75f65a3e
ILT
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)
a3ad94ed 652 gold_unreachable();
75f65a3e
ILT
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();
0496d5e5
ILT
666
667 // FIXME: This should depend on the -n and -N options.
668 (*p)->set_minimum_addralign(target->common_pagesize());
669
75f65a3e
ILT
670 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
671 {
ead1e424 672 uint64_t align = (*p)->addralign();
75f65a3e 673
ead1e424 674 addr = align_address(addr, align);
75f65a3e
ILT
675 aligned_addr = addr;
676 if ((addr & (abi_pagesize - 1)) != 0)
677 addr = addr + abi_pagesize;
678 }
679
ead1e424 680 unsigned int shndx_hold = *pshndx;
75f65a3e 681 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 682 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
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 {
ead1e424
ILT
703 *pshndx = shndx_hold;
704 addr = align_address(aligned_addr, common_pagesize);
75f65a3e 705 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 706 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
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
733off_t
ead1e424 734Layout::set_section_offsets(off_t off, unsigned int* pshndx)
75f65a3e 735{
a3ad94ed
ILT
736 for (Section_list::iterator p = this->unattached_section_list_.begin();
737 p != this->unattached_section_list_.end();
75f65a3e
ILT
738 ++p)
739 {
ead1e424
ILT
740 (*p)->set_out_shndx(*pshndx);
741 ++*pshndx;
61ba1cf9
ILT
742 if ((*p)->offset() != -1)
743 continue;
ead1e424 744 off = align_address(off, (*p)->addralign());
75f65a3e
ILT
745 (*p)->set_address(0, off);
746 off += (*p)->data_size();
747 }
748 return off;
749}
750
b8e6aad9
ILT
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.
75f65a3e
ILT
754
755void
61ba1cf9 756Layout::create_symtab_sections(int size, const Input_objects* input_objects,
75f65a3e 757 Symbol_table* symtab,
16649710 758 off_t* poff)
75f65a3e 759{
61ba1cf9
ILT
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
a3ad94ed 773 gold_unreachable();
61ba1cf9
ILT
774
775 off_t off = *poff;
ead1e424 776 off = align_address(off, align);
61ba1cf9
ILT
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;
c06b7b0b 782 unsigned int local_symbol_index = 1;
61ba1cf9 783
a3ad94ed
ILT
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
f6ce93d6
ILT
799 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
800 p != input_objects->relobj_end();
75f65a3e
ILT
801 ++p)
802 {
803 Task_lock_obj<Object> tlo(**p);
c06b7b0b
ILT
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;
75f65a3e
ILT
809 }
810
c06b7b0b 811 unsigned int local_symcount = local_symbol_index;
a3ad94ed 812 gold_assert(local_symcount * symsize == off - startoff);
61ba1cf9 813
16649710
ILT
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_);
75f65a3e 835
61ba1cf9
ILT
836 this->sympool_.set_string_offsets();
837
f0641a0b 838 const char* symtab_name = this->namepool_.add(".symtab", NULL);
a3ad94ed
ILT
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);
61ba1cf9 847
f0641a0b 848 const char* strtab_name = this->namepool_.add(".strtab", NULL);
a3ad94ed
ILT
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);
61ba1cf9
ILT
855
856 osymtab->set_address(0, startoff);
16649710 857 osymtab->set_link_section(ostrtab);
61ba1cf9
ILT
858 osymtab->set_info(local_symcount);
859 osymtab->set_entsize(symsize);
61ba1cf9
ILT
860
861 *poff = off;
75f65a3e
ILT
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
868Output_section*
869Layout::create_shstrtab()
870{
871 // FIXME: We don't need to create a .shstrtab section if we are
872 // stripping everything.
873
f0641a0b 874 const char* name = this->namepool_.add(".shstrtab", NULL);
75f65a3e 875
61ba1cf9
ILT
876 this->namepool_.set_string_offsets();
877
a3ad94ed 878 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
75f65a3e 879
a3ad94ed
ILT
880 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
881 os->add_output_section_data(posd);
75f65a3e
ILT
882
883 return os;
884}
885
886// Create the section headers. SIZE is 32 or 64. OFF is the file
887// offset.
888
889Output_section_headers*
61ba1cf9 890Layout::create_shdrs(int size, bool big_endian, off_t* poff)
75f65a3e
ILT
891{
892 Output_section_headers* oshdrs;
16649710
ILT
893 oshdrs = new Output_section_headers(size, big_endian, this,
894 &this->segment_list_,
895 &this->unattached_section_list_,
61ba1cf9 896 &this->namepool_);
ead1e424 897 off_t off = align_address(*poff, oshdrs->addralign());
75f65a3e 898 oshdrs->set_address(0, off);
61ba1cf9
ILT
899 off += oshdrs->data_size();
900 *poff = off;
901 this->special_output_list_.push_back(oshdrs);
75f65a3e 902 return oshdrs;
54dc6425
ILT
903}
904
dbe717ef
ILT
905// Create the dynamic symbol table.
906
907void
14b31740
ILT
908Layout::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)
dbe717ef 913{
a3ad94ed
ILT
914 // Count all the symbols in the dynamic symbol table, and set the
915 // dynamic symbol indexes.
dbe717ef 916
a3ad94ed
ILT
917 // Skip symbol 0, which is always all zeroes.
918 unsigned int index = 1;
dbe717ef 919
a3ad94ed
ILT
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;
14b31740 940 *plocal_dynamic_count = local_symcount;
a3ad94ed
ILT
941
942 // FIXME: We have to tell set_dynsym_indexes whether the
943 // -E/--export-dynamic option was used.
14b31740
ILT
944 index = symtab->set_dynsym_indexes(&this->options_, target, index,
945 pdynamic_symbols, &this->dynpool_,
946 pversions);
a3ad94ed
ILT
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
14b31740
ILT
964 // Create the dynamic symbol table section.
965
a3ad94ed
ILT
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
16649710 981 Output_data_dynamic* const odyn = this->dynamic_data_;
a3ad94ed
ILT
982 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
983 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
984
14b31740
ILT
985 // Create the dynamic string table section.
986
a3ad94ed
ILT
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
16649710
ILT
995 dynsym->set_link_section(dynstr);
996 this->dynamic_section_->set_link_section(dynstr);
997
a3ad94ed
ILT
998 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
999 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1000
14b31740
ILT
1001 *pdynstr = dynstr;
1002
1003 // Create the hash tables.
1004
a3ad94ed
ILT
1005 // FIXME: We need an option to create a GNU hash table.
1006
1007 unsigned char* phash;
1008 unsigned int hashlen;
14b31740 1009 Dynobj::create_elf_hash_table(target, *pdynamic_symbols, local_symcount,
a3ad94ed
ILT
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
16649710 1022 hashsec->set_link_section(dynsym);
a3ad94ed 1023 hashsec->set_entsize(4);
a3ad94ed
ILT
1024
1025 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
dbe717ef
ILT
1026}
1027
14b31740
ILT
1028// Create the version sections.
1029
1030void
1031Layout::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())
193a53d9
ILT
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 }
14b31740 1052 else
193a53d9
ILT
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 }
14b31740
ILT
1063 }
1064 else if (target->get_size() == 64)
1065 {
1066 if (target->is_big_endian())
193a53d9
ILT
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 }
14b31740 1077 else
193a53d9
ILT
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 }
14b31740
ILT
1088 }
1089 else
1090 gold_unreachable();
1091}
1092
1093// Create the version sections, sized version.
1094
1095template<int size, bool big_endian>
1096void
1097Layout::sized_create_version_sections(
1098 const Versions* versions,
1099 unsigned int local_symcount,
1100 const std::vector<Symbol*>& dynamic_symbols,
91da9340
ILT
1101 const Output_section* dynstr
1102 ACCEPT_SIZE_ENDIAN)
14b31740
ILT
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;
91da9340 1111 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
7e1edb90
ILT
1112 &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1113 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
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;
91da9340
ILT
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));
14b31740
ILT
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;
91da9340
ILT
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));
14b31740
ILT
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
dbe717ef
ILT
1177// Create the .interp section and PT_INTERP segment.
1178
1179void
1180Layout::create_interp(const Target* target)
1181{
1182 const char* interp = this->options_.dynamic_linker();
1183 if (interp == NULL)
1184 {
1185 interp = target->dynamic_linker();
a3ad94ed 1186 gold_assert(interp != NULL);
dbe717ef
ILT
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
a3ad94ed
ILT
1204// Finish the .dynamic section and PT_DYNAMIC segment.
1205
1206void
1207Layout::finish_dynamic_section(const Input_objects* input_objects,
16649710 1208 const Symbol_table* symtab)
a3ad94ed 1209{
a3ad94ed
ILT
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
16649710
ILT
1216 Output_data_dynamic* const odyn = this->dynamic_data_;
1217
a3ad94ed
ILT
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");
14b31740 1228 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1229 odyn->add_symbol(elfcpp::DT_INIT, sym);
1230
1231 sym = symtab->lookup("_fini");
14b31740 1232 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1233 odyn->add_symbol(elfcpp::DT_FINI, sym);
1234
1235 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
41f542e7
ILT
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 }
a3ad94ed
ILT
1265}
1266
a2fb1b05
ILT
1267// The mapping of .gnu.linkonce section names to real section names.
1268
ead1e424 1269#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
a2fb1b05
ILT
1270const 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
1290const 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"
ead1e424
ILT
1296// to ".text". Set *PLEN to the length of the name. *PLEN is
1297// initialized to the length of NAME.
a2fb1b05
ILT
1298
1299const char*
ead1e424 1300Layout::linkonce_output_name(const char* name, size_t *plen)
a2fb1b05
ILT
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] == '.')
ead1e424
ILT
1310 {
1311 *plen = plm->tolen;
1312 return plm->to;
1313 }
a2fb1b05
ILT
1314 }
1315 return name;
1316}
1317
ead1e424
ILT
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
1322const char*
1323Layout::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
a2fb1b05
ILT
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
1378bool
1379Layout::add_comdat(const char* signature, bool group)
1380{
1381 std::string sig(signature);
1382 std::pair<Signatures::iterator, bool> ins(
ead1e424 1383 this->signatures_.insert(std::make_pair(sig, group)));
a2fb1b05
ILT
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
61ba1cf9
ILT
1413// Write out data not associated with a section or the symbol table.
1414
1415void
a3ad94ed
ILT
1416Layout::write_data(const Symbol_table* symtab, const Target* target,
1417 Output_file* of) const
61ba1cf9 1418{
a3ad94ed
ILT
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.
61ba1cf9
ILT
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
1471Task::Is_runnable_type
1472Write_data_task::is_runnable(Workqueue*)
1473{
1474 return IS_RUNNABLE;
1475}
1476
1477// We need to unlock FINAL_BLOCKER when finished.
1478
1479Task_locker*
1480Write_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
1487void
1488Write_data_task::run(Workqueue*)
1489{
a3ad94ed 1490 this->layout_->write_data(this->symtab_, this->target_, this->of_);
61ba1cf9
ILT
1491}
1492
1493// Write_symbols_task methods.
1494
1495// We can always run this task.
1496
1497Task::Is_runnable_type
1498Write_symbols_task::is_runnable(Workqueue*)
1499{
1500 return IS_RUNNABLE;
1501}
1502
1503// We need to unlock FINAL_BLOCKER when finished.
1504
1505Task_locker*
1506Write_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
1513void
1514Write_symbols_task::run(Workqueue*)
1515{
16649710
ILT
1516 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1517 this->of_);
61ba1cf9
ILT
1518}
1519
92e059d8 1520// Close_task_runner methods.
61ba1cf9
ILT
1521
1522// Run the task--close the file.
1523
1524void
92e059d8 1525Close_task_runner::run(Workqueue*)
61ba1cf9
ILT
1526{
1527 this->of_->close();
1528}
1529
a2fb1b05
ILT
1530// Instantiate the templates we need. We could use the configure
1531// script to restrict this to only the ones for implemented targets.
1532
193a53d9 1533#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
1534template
1535Output_section*
f6ce93d6 1536Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1537 const elfcpp::Shdr<32, false>& shdr, off_t*);
193a53d9 1538#endif
a2fb1b05 1539
193a53d9 1540#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
1541template
1542Output_section*
f6ce93d6 1543Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1544 const elfcpp::Shdr<32, true>& shdr, off_t*);
193a53d9 1545#endif
a2fb1b05 1546
193a53d9 1547#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
1548template
1549Output_section*
f6ce93d6 1550Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1551 const elfcpp::Shdr<64, false>& shdr, off_t*);
193a53d9 1552#endif
a2fb1b05 1553
193a53d9 1554#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
1555template
1556Output_section*
f6ce93d6 1557Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1558 const elfcpp::Shdr<64, true>& shdr, off_t*);
193a53d9 1559#endif
a2fb1b05
ILT
1560
1561
1562} // End namespace gold.
This page took 0.154336 seconds and 4 git commands to generate.