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