* ld-srec/srec.exp: Define __stack_chk_fail sym.
[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
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
ILT
627
628 return off;
629}
630
4f211c8b
ILT
631// Create a .note section for an executable or shared library. This
632// records the version of gold used to create the binary.
633
634void
635Layout::create_note_section()
636{
637 if (parameters->output_is_object())
638 return;
639
640 const int size = parameters->get_size();
641
642 // The contents of the .note section.
643 const char* name = "GNU";
644 std::string desc(std::string("gold ") + gold::get_version_string());
645 size_t namesz = strlen(name) + 1;
646 size_t aligned_namesz = align_address(namesz, size / 8);
647 size_t descsz = desc.length() + 1;
648 size_t aligned_descsz = align_address(descsz, size / 8);
649 const int note_type = 4;
650
651 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
652
653 unsigned char buffer[128];
654 gold_assert(sizeof buffer >= notesz);
655 memset(buffer, 0, notesz);
656
657 bool is_big_endian = parameters->is_big_endian();
658
659 if (size == 32)
660 {
661 if (!is_big_endian)
662 {
663 elfcpp::Swap<32, false>::writeval(buffer, namesz);
664 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
665 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
666 }
667 else
668 {
669 elfcpp::Swap<32, true>::writeval(buffer, namesz);
670 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
671 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
672 }
673 }
674 else if (size == 64)
675 {
676 if (!is_big_endian)
677 {
678 elfcpp::Swap<64, false>::writeval(buffer, namesz);
679 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
680 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
681 }
682 else
683 {
684 elfcpp::Swap<64, true>::writeval(buffer, namesz);
685 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
686 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
687 }
688 }
689 else
690 gold_unreachable();
691
692 memcpy(buffer + 3 * (size / 8), name, namesz);
693 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
694
695 const char* note_name = this->namepool_.add(".note", NULL);
696 Output_section* os = this->make_output_section(note_name,
697 elfcpp::SHT_NOTE,
698 0);
699 Output_section_data* posd = new Output_data_const(buffer, notesz,
700 size / 8);
701 os->add_output_section_data(posd);
702}
703
75f65a3e
ILT
704// Return whether SEG1 should be before SEG2 in the output file. This
705// is based entirely on the segment type and flags. When this is
706// called the segment addresses has normally not yet been set.
707
708bool
709Layout::segment_precedes(const Output_segment* seg1,
710 const Output_segment* seg2)
711{
712 elfcpp::Elf_Word type1 = seg1->type();
713 elfcpp::Elf_Word type2 = seg2->type();
714
715 // The single PT_PHDR segment is required to precede any loadable
716 // segment. We simply make it always first.
717 if (type1 == elfcpp::PT_PHDR)
718 {
a3ad94ed 719 gold_assert(type2 != elfcpp::PT_PHDR);
75f65a3e
ILT
720 return true;
721 }
722 if (type2 == elfcpp::PT_PHDR)
723 return false;
724
725 // The single PT_INTERP segment is required to precede any loadable
726 // segment. We simply make it always second.
727 if (type1 == elfcpp::PT_INTERP)
728 {
a3ad94ed 729 gold_assert(type2 != elfcpp::PT_INTERP);
75f65a3e
ILT
730 return true;
731 }
732 if (type2 == elfcpp::PT_INTERP)
733 return false;
734
735 // We then put PT_LOAD segments before any other segments.
736 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
737 return true;
738 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
739 return false;
740
92e059d8
ILT
741 // We put the PT_TLS segment last, because that is where the dynamic
742 // linker expects to find it (this is just for efficiency; other
743 // positions would also work correctly).
744 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
745 return false;
746 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
747 return true;
748
75f65a3e
ILT
749 const elfcpp::Elf_Word flags1 = seg1->flags();
750 const elfcpp::Elf_Word flags2 = seg2->flags();
751
752 // The order of non-PT_LOAD segments is unimportant. We simply sort
753 // by the numeric segment type and flags values. There should not
754 // be more than one segment with the same type and flags.
755 if (type1 != elfcpp::PT_LOAD)
756 {
757 if (type1 != type2)
758 return type1 < type2;
a3ad94ed 759 gold_assert(flags1 != flags2);
75f65a3e
ILT
760 return flags1 < flags2;
761 }
762
763 // We sort PT_LOAD segments based on the flags. Readonly segments
764 // come before writable segments. Then executable segments come
765 // before non-executable segments. Then the unlikely case of a
766 // non-readable segment comes before the normal case of a readable
767 // segment. If there are multiple segments with the same type and
768 // flags, we require that the address be set, and we sort by
769 // virtual address and then physical address.
770 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
771 return (flags1 & elfcpp::PF_W) == 0;
772 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
773 return (flags1 & elfcpp::PF_X) != 0;
774 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
775 return (flags1 & elfcpp::PF_R) == 0;
776
777 uint64_t vaddr1 = seg1->vaddr();
778 uint64_t vaddr2 = seg2->vaddr();
779 if (vaddr1 != vaddr2)
780 return vaddr1 < vaddr2;
781
782 uint64_t paddr1 = seg1->paddr();
783 uint64_t paddr2 = seg2->paddr();
a3ad94ed 784 gold_assert(paddr1 != paddr2);
75f65a3e
ILT
785 return paddr1 < paddr2;
786}
787
ead1e424
ILT
788// Set the file offsets of all the segments, and all the sections they
789// contain. They have all been created. LOAD_SEG must be be laid out
790// first. Return the offset of the data to follow.
75f65a3e
ILT
791
792off_t
ead1e424
ILT
793Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
794 unsigned int *pshndx)
75f65a3e
ILT
795{
796 // Sort them into the final order.
54dc6425
ILT
797 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
798 Layout::Compare_segments());
799
75f65a3e
ILT
800 // Find the PT_LOAD segments, and set their addresses and offsets
801 // and their section's addresses and offsets.
802 uint64_t addr = target->text_segment_address();
803 off_t off = 0;
804 bool was_readonly = false;
805 for (Segment_list::iterator p = this->segment_list_.begin();
806 p != this->segment_list_.end();
807 ++p)
808 {
809 if ((*p)->type() == elfcpp::PT_LOAD)
810 {
811 if (load_seg != NULL && load_seg != *p)
a3ad94ed 812 gold_unreachable();
75f65a3e
ILT
813 load_seg = NULL;
814
815 // If the last segment was readonly, and this one is not,
816 // then skip the address forward one page, maintaining the
817 // same position within the page. This lets us store both
818 // segments overlapping on a single page in the file, but
819 // the loader will put them on different pages in memory.
820
821 uint64_t orig_addr = addr;
822 uint64_t orig_off = off;
823
824 uint64_t aligned_addr = addr;
825 uint64_t abi_pagesize = target->abi_pagesize();
0496d5e5
ILT
826
827 // FIXME: This should depend on the -n and -N options.
828 (*p)->set_minimum_addralign(target->common_pagesize());
829
75f65a3e
ILT
830 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
831 {
ead1e424 832 uint64_t align = (*p)->addralign();
75f65a3e 833
ead1e424 834 addr = align_address(addr, align);
75f65a3e
ILT
835 aligned_addr = addr;
836 if ((addr & (abi_pagesize - 1)) != 0)
837 addr = addr + abi_pagesize;
838 }
839
ead1e424 840 unsigned int shndx_hold = *pshndx;
75f65a3e 841 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 842 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
843
844 // Now that we know the size of this segment, we may be able
845 // to save a page in memory, at the cost of wasting some
846 // file space, by instead aligning to the start of a new
847 // page. Here we use the real machine page size rather than
848 // the ABI mandated page size.
849
850 if (aligned_addr != addr)
851 {
852 uint64_t common_pagesize = target->common_pagesize();
853 uint64_t first_off = (common_pagesize
854 - (aligned_addr
855 & (common_pagesize - 1)));
856 uint64_t last_off = new_addr & (common_pagesize - 1);
857 if (first_off > 0
858 && last_off > 0
859 && ((aligned_addr & ~ (common_pagesize - 1))
860 != (new_addr & ~ (common_pagesize - 1)))
861 && first_off + last_off <= common_pagesize)
862 {
ead1e424
ILT
863 *pshndx = shndx_hold;
864 addr = align_address(aligned_addr, common_pagesize);
75f65a3e 865 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 866 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
867 }
868 }
869
870 addr = new_addr;
871
872 if (((*p)->flags() & elfcpp::PF_W) == 0)
873 was_readonly = true;
874 }
875 }
876
877 // Handle the non-PT_LOAD segments, setting their offsets from their
878 // section's offsets.
879 for (Segment_list::iterator p = this->segment_list_.begin();
880 p != this->segment_list_.end();
881 ++p)
882 {
883 if ((*p)->type() != elfcpp::PT_LOAD)
884 (*p)->set_offset();
885 }
886
887 return off;
888}
889
890// Set the file offset of all the sections not associated with a
891// segment.
892
893off_t
ead1e424 894Layout::set_section_offsets(off_t off, unsigned int* pshndx)
75f65a3e 895{
a3ad94ed
ILT
896 for (Section_list::iterator p = this->unattached_section_list_.begin();
897 p != this->unattached_section_list_.end();
75f65a3e
ILT
898 ++p)
899 {
ead1e424
ILT
900 (*p)->set_out_shndx(*pshndx);
901 ++*pshndx;
61ba1cf9
ILT
902 if ((*p)->offset() != -1)
903 continue;
ead1e424 904 off = align_address(off, (*p)->addralign());
75f65a3e
ILT
905 (*p)->set_address(0, off);
906 off += (*p)->data_size();
907 }
908 return off;
909}
910
b8e6aad9
ILT
911// Create the symbol table sections. Here we also set the final
912// values of the symbols. At this point all the loadable sections are
913// fully laid out.
75f65a3e
ILT
914
915void
9025d29d 916Layout::create_symtab_sections(const Input_objects* input_objects,
75f65a3e 917 Symbol_table* symtab,
16649710 918 off_t* poff)
75f65a3e 919{
61ba1cf9
ILT
920 int symsize;
921 unsigned int align;
9025d29d 922 if (parameters->get_size() == 32)
61ba1cf9
ILT
923 {
924 symsize = elfcpp::Elf_sizes<32>::sym_size;
925 align = 4;
926 }
9025d29d 927 else if (parameters->get_size() == 64)
61ba1cf9
ILT
928 {
929 symsize = elfcpp::Elf_sizes<64>::sym_size;
930 align = 8;
931 }
932 else
a3ad94ed 933 gold_unreachable();
61ba1cf9
ILT
934
935 off_t off = *poff;
ead1e424 936 off = align_address(off, align);
61ba1cf9
ILT
937 off_t startoff = off;
938
939 // Save space for the dummy symbol at the start of the section. We
940 // never bother to write this out--it will just be left as zero.
941 off += symsize;
c06b7b0b 942 unsigned int local_symbol_index = 1;
61ba1cf9 943
a3ad94ed
ILT
944 // Add STT_SECTION symbols for each Output section which needs one.
945 for (Section_list::iterator p = this->section_list_.begin();
946 p != this->section_list_.end();
947 ++p)
948 {
949 if (!(*p)->needs_symtab_index())
950 (*p)->set_symtab_index(-1U);
951 else
952 {
953 (*p)->set_symtab_index(local_symbol_index);
954 ++local_symbol_index;
955 off += symsize;
956 }
957 }
958
f6ce93d6
ILT
959 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
960 p != input_objects->relobj_end();
75f65a3e
ILT
961 ++p)
962 {
963 Task_lock_obj<Object> tlo(**p);
c06b7b0b
ILT
964 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
965 off,
966 &this->sympool_);
967 off += (index - local_symbol_index) * symsize;
968 local_symbol_index = index;
75f65a3e
ILT
969 }
970
c06b7b0b 971 unsigned int local_symcount = local_symbol_index;
a3ad94ed 972 gold_assert(local_symcount * symsize == off - startoff);
61ba1cf9 973
16649710
ILT
974 off_t dynoff;
975 size_t dyn_global_index;
976 size_t dyncount;
977 if (this->dynsym_section_ == NULL)
978 {
979 dynoff = 0;
980 dyn_global_index = 0;
981 dyncount = 0;
982 }
983 else
984 {
985 dyn_global_index = this->dynsym_section_->info();
986 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
987 dynoff = this->dynsym_section_->offset() + locsize;
988 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
f5c3f225 989 gold_assert(static_cast<off_t>(dyncount * symsize)
16649710
ILT
990 == this->dynsym_section_->data_size() - locsize);
991 }
992
993 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
994 dyncount, &this->sympool_);
75f65a3e 995
9e2dcb77
ILT
996 if (!parameters->strip_all())
997 {
998 this->sympool_.set_string_offsets();
61ba1cf9 999
9e2dcb77
ILT
1000 const char* symtab_name = this->namepool_.add(".symtab", NULL);
1001 Output_section* osymtab = this->make_output_section(symtab_name,
1002 elfcpp::SHT_SYMTAB,
1003 0);
1004 this->symtab_section_ = osymtab;
a3ad94ed 1005
9e2dcb77
ILT
1006 Output_section_data* pos = new Output_data_space(off - startoff,
1007 align);
1008 osymtab->add_output_section_data(pos);
61ba1cf9 1009
9e2dcb77
ILT
1010 const char* strtab_name = this->namepool_.add(".strtab", NULL);
1011 Output_section* ostrtab = this->make_output_section(strtab_name,
1012 elfcpp::SHT_STRTAB,
1013 0);
a3ad94ed 1014
9e2dcb77
ILT
1015 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1016 ostrtab->add_output_section_data(pstr);
61ba1cf9 1017
9e2dcb77
ILT
1018 osymtab->set_address(0, startoff);
1019 osymtab->set_link_section(ostrtab);
1020 osymtab->set_info(local_symcount);
1021 osymtab->set_entsize(symsize);
61ba1cf9 1022
9e2dcb77
ILT
1023 *poff = off;
1024 }
75f65a3e
ILT
1025}
1026
1027// Create the .shstrtab section, which holds the names of the
1028// sections. At the time this is called, we have created all the
1029// output sections except .shstrtab itself.
1030
1031Output_section*
1032Layout::create_shstrtab()
1033{
1034 // FIXME: We don't need to create a .shstrtab section if we are
1035 // stripping everything.
1036
f0641a0b 1037 const char* name = this->namepool_.add(".shstrtab", NULL);
75f65a3e 1038
61ba1cf9
ILT
1039 this->namepool_.set_string_offsets();
1040
a3ad94ed 1041 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
75f65a3e 1042
a3ad94ed
ILT
1043 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1044 os->add_output_section_data(posd);
75f65a3e
ILT
1045
1046 return os;
1047}
1048
1049// Create the section headers. SIZE is 32 or 64. OFF is the file
1050// offset.
1051
1052Output_section_headers*
9025d29d 1053Layout::create_shdrs(off_t* poff)
75f65a3e
ILT
1054{
1055 Output_section_headers* oshdrs;
9025d29d 1056 oshdrs = new Output_section_headers(this,
16649710
ILT
1057 &this->segment_list_,
1058 &this->unattached_section_list_,
61ba1cf9 1059 &this->namepool_);
ead1e424 1060 off_t off = align_address(*poff, oshdrs->addralign());
75f65a3e 1061 oshdrs->set_address(0, off);
61ba1cf9
ILT
1062 off += oshdrs->data_size();
1063 *poff = off;
1064 this->special_output_list_.push_back(oshdrs);
75f65a3e 1065 return oshdrs;
54dc6425
ILT
1066}
1067
dbe717ef
ILT
1068// Create the dynamic symbol table.
1069
1070void
14b31740
ILT
1071Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
1072 Output_section **pdynstr,
1073 unsigned int* plocal_dynamic_count,
1074 std::vector<Symbol*>* pdynamic_symbols,
1075 Versions* pversions)
dbe717ef 1076{
a3ad94ed
ILT
1077 // Count all the symbols in the dynamic symbol table, and set the
1078 // dynamic symbol indexes.
dbe717ef 1079
a3ad94ed
ILT
1080 // Skip symbol 0, which is always all zeroes.
1081 unsigned int index = 1;
dbe717ef 1082
a3ad94ed
ILT
1083 // Add STT_SECTION symbols for each Output section which needs one.
1084 for (Section_list::iterator p = this->section_list_.begin();
1085 p != this->section_list_.end();
1086 ++p)
1087 {
1088 if (!(*p)->needs_dynsym_index())
1089 (*p)->set_dynsym_index(-1U);
1090 else
1091 {
1092 (*p)->set_dynsym_index(index);
1093 ++index;
1094 }
1095 }
1096
1097 // FIXME: Some targets apparently require local symbols in the
1098 // dynamic symbol table. Here is where we will have to count them,
1099 // and set the dynamic symbol indexes, and add the names to
1100 // this->dynpool_.
1101
1102 unsigned int local_symcount = index;
14b31740 1103 *plocal_dynamic_count = local_symcount;
a3ad94ed
ILT
1104
1105 // FIXME: We have to tell set_dynsym_indexes whether the
1106 // -E/--export-dynamic option was used.
14b31740
ILT
1107 index = symtab->set_dynsym_indexes(&this->options_, target, index,
1108 pdynamic_symbols, &this->dynpool_,
1109 pversions);
a3ad94ed
ILT
1110
1111 int symsize;
1112 unsigned int align;
9025d29d 1113 const int size = parameters->get_size();
a3ad94ed
ILT
1114 if (size == 32)
1115 {
1116 symsize = elfcpp::Elf_sizes<32>::sym_size;
1117 align = 4;
1118 }
1119 else if (size == 64)
1120 {
1121 symsize = elfcpp::Elf_sizes<64>::sym_size;
1122 align = 8;
1123 }
1124 else
1125 gold_unreachable();
1126
14b31740
ILT
1127 // Create the dynamic symbol table section.
1128
a3ad94ed
ILT
1129 const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
1130 Output_section* dynsym = this->make_output_section(dynsym_name,
1131 elfcpp::SHT_DYNSYM,
1132 elfcpp::SHF_ALLOC);
1133
1134 Output_section_data* odata = new Output_data_space(index * symsize,
1135 align);
1136 dynsym->add_output_section_data(odata);
1137
1138 dynsym->set_info(local_symcount);
1139 dynsym->set_entsize(symsize);
1140 dynsym->set_addralign(align);
1141
1142 this->dynsym_section_ = dynsym;
1143
16649710 1144 Output_data_dynamic* const odyn = this->dynamic_data_;
a3ad94ed
ILT
1145 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1146 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1147
14b31740
ILT
1148 // Create the dynamic string table section.
1149
a3ad94ed
ILT
1150 const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
1151 Output_section* dynstr = this->make_output_section(dynstr_name,
1152 elfcpp::SHT_STRTAB,
1153 elfcpp::SHF_ALLOC);
1154
1155 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1156 dynstr->add_output_section_data(strdata);
1157
16649710
ILT
1158 dynsym->set_link_section(dynstr);
1159 this->dynamic_section_->set_link_section(dynstr);
1160
a3ad94ed
ILT
1161 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1162 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1163
14b31740
ILT
1164 *pdynstr = dynstr;
1165
1166 // Create the hash tables.
1167
a3ad94ed
ILT
1168 // FIXME: We need an option to create a GNU hash table.
1169
1170 unsigned char* phash;
1171 unsigned int hashlen;
9025d29d 1172 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
a3ad94ed
ILT
1173 &phash, &hashlen);
1174
1175 const char* hash_name = this->namepool_.add(".hash", NULL);
1176 Output_section* hashsec = this->make_output_section(hash_name,
1177 elfcpp::SHT_HASH,
1178 elfcpp::SHF_ALLOC);
1179
1180 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1181 hashlen,
1182 align);
1183 hashsec->add_output_section_data(hashdata);
1184
16649710 1185 hashsec->set_link_section(dynsym);
a3ad94ed 1186 hashsec->set_entsize(4);
a3ad94ed
ILT
1187
1188 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
dbe717ef
ILT
1189}
1190
14b31740
ILT
1191// Create the version sections.
1192
1193void
9025d29d 1194Layout::create_version_sections(const Versions* versions,
14b31740
ILT
1195 unsigned int local_symcount,
1196 const std::vector<Symbol*>& dynamic_symbols,
1197 const Output_section* dynstr)
1198{
1199 if (!versions->any_defs() && !versions->any_needs())
1200 return;
1201
9025d29d 1202 if (parameters->get_size() == 32)
14b31740 1203 {
9025d29d 1204 if (parameters->is_big_endian())
193a53d9
ILT
1205 {
1206#ifdef HAVE_TARGET_32_BIG
1207 this->sized_create_version_sections
1208 SELECT_SIZE_ENDIAN_NAME(32, true)(
1209 versions, local_symcount, dynamic_symbols, dynstr
1210 SELECT_SIZE_ENDIAN(32, true));
1211#else
1212 gold_unreachable();
1213#endif
1214 }
14b31740 1215 else
193a53d9
ILT
1216 {
1217#ifdef HAVE_TARGET_32_LITTLE
1218 this->sized_create_version_sections
1219 SELECT_SIZE_ENDIAN_NAME(32, false)(
1220 versions, local_symcount, dynamic_symbols, dynstr
1221 SELECT_SIZE_ENDIAN(32, false));
1222#else
1223 gold_unreachable();
1224#endif
1225 }
14b31740 1226 }
9025d29d 1227 else if (parameters->get_size() == 64)
14b31740 1228 {
9025d29d 1229 if (parameters->is_big_endian())
193a53d9
ILT
1230 {
1231#ifdef HAVE_TARGET_64_BIG
1232 this->sized_create_version_sections
1233 SELECT_SIZE_ENDIAN_NAME(64, true)(
1234 versions, local_symcount, dynamic_symbols, dynstr
1235 SELECT_SIZE_ENDIAN(64, true));
1236#else
1237 gold_unreachable();
1238#endif
1239 }
14b31740 1240 else
193a53d9
ILT
1241 {
1242#ifdef HAVE_TARGET_64_LITTLE
1243 this->sized_create_version_sections
1244 SELECT_SIZE_ENDIAN_NAME(64, false)(
1245 versions, local_symcount, dynamic_symbols, dynstr
1246 SELECT_SIZE_ENDIAN(64, false));
1247#else
1248 gold_unreachable();
1249#endif
1250 }
14b31740
ILT
1251 }
1252 else
1253 gold_unreachable();
1254}
1255
1256// Create the version sections, sized version.
1257
1258template<int size, bool big_endian>
1259void
1260Layout::sized_create_version_sections(
1261 const Versions* versions,
1262 unsigned int local_symcount,
1263 const std::vector<Symbol*>& dynamic_symbols,
91da9340
ILT
1264 const Output_section* dynstr
1265 ACCEPT_SIZE_ENDIAN)
14b31740
ILT
1266{
1267 const char* vname = this->namepool_.add(".gnu.version", NULL);
1268 Output_section* vsec = this->make_output_section(vname,
1269 elfcpp::SHT_GNU_versym,
1270 elfcpp::SHF_ALLOC);
1271
1272 unsigned char* vbuf;
1273 unsigned int vsize;
91da9340 1274 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
7e1edb90
ILT
1275 &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1276 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1277
1278 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1279
1280 vsec->add_output_section_data(vdata);
1281 vsec->set_entsize(2);
1282 vsec->set_link_section(this->dynsym_section_);
1283
1284 Output_data_dynamic* const odyn = this->dynamic_data_;
1285 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1286
1287 if (versions->any_defs())
1288 {
1289 const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
1290 Output_section *vdsec;
1291 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1292 elfcpp::SHF_ALLOC);
1293
1294 unsigned char* vdbuf;
1295 unsigned int vdsize;
1296 unsigned int vdentries;
91da9340
ILT
1297 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1298 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1299 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1300
1301 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1302 vdsize,
1303 4);
1304
1305 vdsec->add_output_section_data(vddata);
1306 vdsec->set_link_section(dynstr);
1307 vdsec->set_info(vdentries);
1308
1309 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1310 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1311 }
1312
1313 if (versions->any_needs())
1314 {
1315 const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
1316 Output_section* vnsec;
1317 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1318 elfcpp::SHF_ALLOC);
1319
1320 unsigned char* vnbuf;
1321 unsigned int vnsize;
1322 unsigned int vnentries;
91da9340
ILT
1323 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1324 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1325 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1326
1327 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1328 vnsize,
1329 4);
1330
1331 vnsec->add_output_section_data(vndata);
1332 vnsec->set_link_section(dynstr);
1333 vnsec->set_info(vnentries);
1334
1335 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1336 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1337 }
1338}
1339
dbe717ef
ILT
1340// Create the .interp section and PT_INTERP segment.
1341
1342void
1343Layout::create_interp(const Target* target)
1344{
1345 const char* interp = this->options_.dynamic_linker();
1346 if (interp == NULL)
1347 {
1348 interp = target->dynamic_linker();
a3ad94ed 1349 gold_assert(interp != NULL);
dbe717ef
ILT
1350 }
1351
1352 size_t len = strlen(interp) + 1;
1353
1354 Output_section_data* odata = new Output_data_const(interp, len, 1);
1355
1356 const char* interp_name = this->namepool_.add(".interp", NULL);
1357 Output_section* osec = this->make_output_section(interp_name,
1358 elfcpp::SHT_PROGBITS,
1359 elfcpp::SHF_ALLOC);
1360 osec->add_output_section_data(odata);
1361
1362 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1363 this->segment_list_.push_back(oseg);
1364 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1365}
1366
a3ad94ed
ILT
1367// Finish the .dynamic section and PT_DYNAMIC segment.
1368
1369void
1370Layout::finish_dynamic_section(const Input_objects* input_objects,
16649710 1371 const Symbol_table* symtab)
a3ad94ed 1372{
a3ad94ed
ILT
1373 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1374 elfcpp::PF_R | elfcpp::PF_W);
1375 this->segment_list_.push_back(oseg);
1376 oseg->add_initial_output_section(this->dynamic_section_,
1377 elfcpp::PF_R | elfcpp::PF_W);
1378
16649710
ILT
1379 Output_data_dynamic* const odyn = this->dynamic_data_;
1380
a3ad94ed
ILT
1381 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1382 p != input_objects->dynobj_end();
1383 ++p)
1384 {
1385 // FIXME: Handle --as-needed.
1386 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1387 }
1388
1389 // FIXME: Support --init and --fini.
1390 Symbol* sym = symtab->lookup("_init");
14b31740 1391 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1392 odyn->add_symbol(elfcpp::DT_INIT, sym);
1393
1394 sym = symtab->lookup("_fini");
14b31740 1395 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1396 odyn->add_symbol(elfcpp::DT_FINI, sym);
1397
1398 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
41f542e7
ILT
1399
1400 // Add a DT_RPATH entry if needed.
1401 const General_options::Dir_list& rpath(this->options_.rpath());
1402 if (!rpath.empty())
1403 {
1404 std::string rpath_val;
1405 for (General_options::Dir_list::const_iterator p = rpath.begin();
1406 p != rpath.end();
1407 ++p)
1408 {
1409 if (rpath_val.empty())
ad2d6943 1410 rpath_val = p->name();
41f542e7
ILT
1411 else
1412 {
1413 // Eliminate duplicates.
1414 General_options::Dir_list::const_iterator q;
1415 for (q = rpath.begin(); q != p; ++q)
ad2d6943 1416 if (q->name() == p->name())
41f542e7
ILT
1417 break;
1418 if (q == p)
1419 {
1420 rpath_val += ':';
ad2d6943 1421 rpath_val += p->name();
41f542e7
ILT
1422 }
1423 }
1424 }
1425
1426 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1427 }
a3ad94ed
ILT
1428}
1429
a2fb1b05
ILT
1430// The mapping of .gnu.linkonce section names to real section names.
1431
ead1e424 1432#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
a2fb1b05
ILT
1433const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1434{
1435 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1436 MAPPING_INIT("t", ".text"),
1437 MAPPING_INIT("r", ".rodata"),
1438 MAPPING_INIT("d", ".data"),
1439 MAPPING_INIT("b", ".bss"),
1440 MAPPING_INIT("s", ".sdata"),
1441 MAPPING_INIT("sb", ".sbss"),
1442 MAPPING_INIT("s2", ".sdata2"),
1443 MAPPING_INIT("sb2", ".sbss2"),
1444 MAPPING_INIT("wi", ".debug_info"),
1445 MAPPING_INIT("td", ".tdata"),
1446 MAPPING_INIT("tb", ".tbss"),
1447 MAPPING_INIT("lr", ".lrodata"),
1448 MAPPING_INIT("l", ".ldata"),
1449 MAPPING_INIT("lb", ".lbss"),
1450};
1451#undef MAPPING_INIT
1452
1453const int Layout::linkonce_mapping_count =
1454 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1455
1456// Return the name of the output section to use for a .gnu.linkonce
1457// section. This is based on the default ELF linker script of the old
1458// GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
ead1e424
ILT
1459// to ".text". Set *PLEN to the length of the name. *PLEN is
1460// initialized to the length of NAME.
a2fb1b05
ILT
1461
1462const char*
ead1e424 1463Layout::linkonce_output_name(const char* name, size_t *plen)
a2fb1b05
ILT
1464{
1465 const char* s = name + sizeof(".gnu.linkonce") - 1;
1466 if (*s != '.')
1467 return name;
1468 ++s;
1469 const Linkonce_mapping* plm = linkonce_mapping;
1470 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1471 {
1472 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
ead1e424
ILT
1473 {
1474 *plen = plm->tolen;
1475 return plm->to;
1476 }
a2fb1b05
ILT
1477 }
1478 return name;
1479}
1480
ead1e424
ILT
1481// Choose the output section name to use given an input section name.
1482// Set *PLEN to the length of the name. *PLEN is initialized to the
1483// length of NAME.
1484
1485const char*
1486Layout::output_section_name(const char* name, size_t* plen)
1487{
1488 if (Layout::is_linkonce(name))
1489 {
1490 // .gnu.linkonce sections are laid out as though they were named
1491 // for the sections are placed into.
1492 return Layout::linkonce_output_name(name, plen);
1493 }
1494
1495 // If the section name has no '.', or only an initial '.', we use
1496 // the name unchanged (i.e., ".text" is unchanged).
1497
1498 // Otherwise, if the section name does not include ".rel", we drop
1499 // the last '.' and everything that follows (i.e., ".text.XXX"
1500 // becomes ".text").
1501
1502 // Otherwise, if the section name has zero or one '.' after the
1503 // ".rel", we use the name unchanged (i.e., ".rel.text" is
1504 // unchanged).
1505
1506 // Otherwise, we drop the last '.' and everything that follows
1507 // (i.e., ".rel.text.XXX" becomes ".rel.text").
1508
1509 const char* s = name;
1510 if (*s == '.')
1511 ++s;
1512 const char* sdot = strchr(s, '.');
1513 if (sdot == NULL)
1514 return name;
1515
1516 const char* srel = strstr(s, ".rel");
1517 if (srel == NULL)
1518 {
1519 *plen = sdot - name;
1520 return name;
1521 }
1522
1523 sdot = strchr(srel + 1, '.');
1524 if (sdot == NULL)
1525 return name;
1526 sdot = strchr(sdot + 1, '.');
1527 if (sdot == NULL)
1528 return name;
1529
1530 *plen = sdot - name;
1531 return name;
1532}
1533
a2fb1b05
ILT
1534// Record the signature of a comdat section, and return whether to
1535// include it in the link. If GROUP is true, this is a regular
1536// section group. If GROUP is false, this is a group signature
1537// derived from the name of a linkonce section. We want linkonce
1538// signatures and group signatures to block each other, but we don't
1539// want a linkonce signature to block another linkonce signature.
1540
1541bool
1542Layout::add_comdat(const char* signature, bool group)
1543{
1544 std::string sig(signature);
1545 std::pair<Signatures::iterator, bool> ins(
ead1e424 1546 this->signatures_.insert(std::make_pair(sig, group)));
a2fb1b05
ILT
1547
1548 if (ins.second)
1549 {
1550 // This is the first time we've seen this signature.
1551 return true;
1552 }
1553
1554 if (ins.first->second)
1555 {
1556 // We've already seen a real section group with this signature.
1557 return false;
1558 }
1559 else if (group)
1560 {
1561 // This is a real section group, and we've already seen a
a0fa0c07 1562 // linkonce section with this signature. Record that we've seen
a2fb1b05
ILT
1563 // a section group, and don't include this section group.
1564 ins.first->second = true;
1565 return false;
1566 }
1567 else
1568 {
1569 // We've already seen a linkonce section and this is a linkonce
1570 // section. These don't block each other--this may be the same
1571 // symbol name with different section types.
1572 return true;
1573 }
1574}
1575
61ba1cf9
ILT
1576// Write out data not associated with a section or the symbol table.
1577
1578void
9025d29d 1579Layout::write_data(const Symbol_table* symtab, Output_file* of) const
61ba1cf9 1580{
9e2dcb77 1581 if (!parameters->strip_all())
a3ad94ed 1582 {
9e2dcb77
ILT
1583 const Output_section* symtab_section = this->symtab_section_;
1584 for (Section_list::const_iterator p = this->section_list_.begin();
1585 p != this->section_list_.end();
1586 ++p)
a3ad94ed 1587 {
9e2dcb77
ILT
1588 if ((*p)->needs_symtab_index())
1589 {
1590 gold_assert(symtab_section != NULL);
1591 unsigned int index = (*p)->symtab_index();
1592 gold_assert(index > 0 && index != -1U);
1593 off_t off = (symtab_section->offset()
1594 + index * symtab_section->entsize());
1595 symtab->write_section_symbol(*p, of, off);
1596 }
a3ad94ed
ILT
1597 }
1598 }
1599
1600 const Output_section* dynsym_section = this->dynsym_section_;
1601 for (Section_list::const_iterator p = this->section_list_.begin();
1602 p != this->section_list_.end();
1603 ++p)
1604 {
1605 if ((*p)->needs_dynsym_index())
1606 {
1607 gold_assert(dynsym_section != NULL);
1608 unsigned int index = (*p)->dynsym_index();
1609 gold_assert(index > 0 && index != -1U);
1610 off_t off = (dynsym_section->offset()
1611 + index * dynsym_section->entsize());
9025d29d 1612 symtab->write_section_symbol(*p, of, off);
a3ad94ed
ILT
1613 }
1614 }
1615
1616 // Write out the Output_sections. Most won't have anything to
1617 // write, since most of the data will come from input sections which
1618 // are handled elsewhere. But some Output_sections do have
1619 // Output_data.
1620 for (Section_list::const_iterator p = this->section_list_.begin();
1621 p != this->section_list_.end();
1622 ++p)
1623 (*p)->write(of);
1624
1625 // Write out the Output_data which are not in an Output_section.
61ba1cf9
ILT
1626 for (Data_list::const_iterator p = this->special_output_list_.begin();
1627 p != this->special_output_list_.end();
1628 ++p)
1629 (*p)->write(of);
1630}
1631
1632// Write_data_task methods.
1633
1634// We can always run this task.
1635
1636Task::Is_runnable_type
1637Write_data_task::is_runnable(Workqueue*)
1638{
1639 return IS_RUNNABLE;
1640}
1641
1642// We need to unlock FINAL_BLOCKER when finished.
1643
1644Task_locker*
1645Write_data_task::locks(Workqueue* workqueue)
1646{
1647 return new Task_locker_block(*this->final_blocker_, workqueue);
1648}
1649
1650// Run the task--write out the data.
1651
1652void
1653Write_data_task::run(Workqueue*)
1654{
9025d29d 1655 this->layout_->write_data(this->symtab_, this->of_);
61ba1cf9
ILT
1656}
1657
1658// Write_symbols_task methods.
1659
1660// We can always run this task.
1661
1662Task::Is_runnable_type
1663Write_symbols_task::is_runnable(Workqueue*)
1664{
1665 return IS_RUNNABLE;
1666}
1667
1668// We need to unlock FINAL_BLOCKER when finished.
1669
1670Task_locker*
1671Write_symbols_task::locks(Workqueue* workqueue)
1672{
1673 return new Task_locker_block(*this->final_blocker_, workqueue);
1674}
1675
1676// Run the task--write out the symbols.
1677
1678void
1679Write_symbols_task::run(Workqueue*)
1680{
16649710
ILT
1681 this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1682 this->of_);
61ba1cf9
ILT
1683}
1684
92e059d8 1685// Close_task_runner methods.
61ba1cf9
ILT
1686
1687// Run the task--close the file.
1688
1689void
92e059d8 1690Close_task_runner::run(Workqueue*)
61ba1cf9
ILT
1691{
1692 this->of_->close();
1693}
1694
a2fb1b05
ILT
1695// Instantiate the templates we need. We could use the configure
1696// script to restrict this to only the ones for implemented targets.
1697
193a53d9 1698#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
1699template
1700Output_section*
f6ce93d6 1701Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1702 const elfcpp::Shdr<32, false>& shdr, off_t*);
193a53d9 1703#endif
a2fb1b05 1704
193a53d9 1705#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
1706template
1707Output_section*
f6ce93d6 1708Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1709 const elfcpp::Shdr<32, true>& shdr, off_t*);
193a53d9 1710#endif
a2fb1b05 1711
193a53d9 1712#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
1713template
1714Output_section*
f6ce93d6 1715Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1716 const elfcpp::Shdr<64, false>& shdr, off_t*);
193a53d9 1717#endif
a2fb1b05 1718
193a53d9 1719#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
1720template
1721Output_section*
f6ce93d6 1722Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
a2fb1b05 1723 const elfcpp::Shdr<64, true>& shdr, off_t*);
193a53d9 1724#endif
a2fb1b05
ILT
1725
1726
1727} // End namespace gold.
This page took 0.142366 seconds and 4 git commands to generate.