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