*** empty log message ***
[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_(),
27bc2bce 68 section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
3151305a 69 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
35cdfc9a
ILT
70 eh_frame_section_(NULL), output_file_size_(-1),
71 input_requires_executable_stack_(false),
72 input_with_gnu_stack_note_(false),
4f4c5f80 73 input_without_gnu_stack_note_(false)
54dc6425
ILT
74{
75 // Make space for more than enough segments for a typical file.
76 // This is just for efficiency--it's OK if we wind up needing more.
a3ad94ed
ILT
77 this->segment_list_.reserve(12);
78
27bc2bce
ILT
79 // We expect two unattached Output_data objects: the file header and
80 // the segment headers.
81 this->special_output_list_.reserve(2);
54dc6425
ILT
82}
83
a2fb1b05
ILT
84// Hash a key we use to look up an output section mapping.
85
86size_t
87Layout::Hash_key::operator()(const Layout::Key& k) const
88{
f0641a0b 89 return k.first + k.second.first + k.second.second;
a2fb1b05
ILT
90}
91
9e2dcb77
ILT
92// Return whether PREFIX is a prefix of STR.
93
94static inline bool
95is_prefix_of(const char* prefix, const char* str)
96{
97 return strncmp(prefix, str, strlen(prefix)) == 0;
98}
99
02d2ba74
ILT
100// Returns whether the given section is in the list of
101// debug-sections-used-by-some-version-of-gdb. Currently,
102// we've checked versions of gdb up to and including 6.7.1.
103
104static const char* gdb_sections[] =
105{ ".debug_abbrev",
106 // ".debug_aranges", // not used by gdb as of 6.7.1
107 ".debug_frame",
108 ".debug_info",
109 ".debug_line",
110 ".debug_loc",
111 ".debug_macinfo",
112 // ".debug_pubnames", // not used by gdb as of 6.7.1
113 ".debug_ranges",
114 ".debug_str",
115};
116
117static inline bool
118is_gdb_debug_section(const char* str)
119{
120 // We can do this faster: binary search or a hashtable. But why bother?
121 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
122 if (strcmp(str, gdb_sections[i]) == 0)
123 return true;
124 return false;
125}
126
a2fb1b05
ILT
127// Whether to include this section in the link.
128
129template<int size, bool big_endian>
130bool
730cdc88 131Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
a2fb1b05
ILT
132 const elfcpp::Shdr<size, big_endian>& shdr)
133{
134 // Some section types are never linked. Some are only linked when
135 // doing a relocateable link.
136 switch (shdr.get_sh_type())
137 {
138 case elfcpp::SHT_NULL:
139 case elfcpp::SHT_SYMTAB:
140 case elfcpp::SHT_DYNSYM:
141 case elfcpp::SHT_STRTAB:
142 case elfcpp::SHT_HASH:
143 case elfcpp::SHT_DYNAMIC:
144 case elfcpp::SHT_SYMTAB_SHNDX:
145 return false;
146
147 case elfcpp::SHT_RELA:
148 case elfcpp::SHT_REL:
149 case elfcpp::SHT_GROUP:
7e1edb90 150 return parameters->output_is_object();
a2fb1b05 151
9e2dcb77
ILT
152 case elfcpp::SHT_PROGBITS:
153 if (parameters->strip_debug()
154 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
155 {
156 // Debugging sections can only be recognized by name.
157 if (is_prefix_of(".debug", name)
158 || is_prefix_of(".gnu.linkonce.wi.", name)
159 || is_prefix_of(".line", name)
160 || is_prefix_of(".stab", name))
161 return false;
162 }
02d2ba74
ILT
163 if (parameters->strip_debug_gdb()
164 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
165 {
166 // Debugging sections can only be recognized by name.
167 if (is_prefix_of(".debug", name)
168 && !is_gdb_debug_section(name))
169 return false;
170 }
9e2dcb77
ILT
171 return true;
172
a2fb1b05 173 default:
a2fb1b05
ILT
174 return true;
175 }
176}
177
ead1e424 178// Return an output section named NAME, or NULL if there is none.
a2fb1b05 179
a2fb1b05 180Output_section*
ead1e424 181Layout::find_output_section(const char* name) const
a2fb1b05 182{
ead1e424
ILT
183 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
184 p != this->section_name_map_.end();
185 ++p)
f0641a0b 186 if (strcmp(p->second->name(), name) == 0)
ead1e424
ILT
187 return p->second;
188 return NULL;
189}
a2fb1b05 190
ead1e424
ILT
191// Return an output segment of type TYPE, with segment flags SET set
192// and segment flags CLEAR clear. Return NULL if there is none.
a2fb1b05 193
ead1e424
ILT
194Output_segment*
195Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
196 elfcpp::Elf_Word clear) const
197{
198 for (Segment_list::const_iterator p = this->segment_list_.begin();
199 p != this->segment_list_.end();
200 ++p)
201 if (static_cast<elfcpp::PT>((*p)->type()) == type
202 && ((*p)->flags() & set) == set
203 && ((*p)->flags() & clear) == 0)
204 return *p;
205 return NULL;
206}
a2fb1b05 207
ead1e424
ILT
208// Return the output section to use for section NAME with type TYPE
209// and section flags FLAGS.
a2fb1b05 210
ead1e424 211Output_section*
f0641a0b
ILT
212Layout::get_output_section(const char* name, Stringpool::Key name_key,
213 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
ead1e424
ILT
214{
215 // We should ignore some flags.
216 flags &= ~ (elfcpp::SHF_INFO_LINK
217 | elfcpp::SHF_LINK_ORDER
b8e6aad9
ILT
218 | elfcpp::SHF_GROUP
219 | elfcpp::SHF_MERGE
220 | elfcpp::SHF_STRINGS);
a2fb1b05 221
f0641a0b 222 const Key key(name_key, std::make_pair(type, flags));
a2fb1b05
ILT
223 const std::pair<Key, Output_section*> v(key, NULL);
224 std::pair<Section_name_map::iterator, bool> ins(
225 this->section_name_map_.insert(v));
226
a2fb1b05 227 if (!ins.second)
ead1e424 228 return ins.first->second;
a2fb1b05
ILT
229 else
230 {
231 // This is the first time we've seen this name/type/flags
232 // combination.
ead1e424 233 Output_section* os = this->make_output_section(name, type, flags);
a2fb1b05 234 ins.first->second = os;
ead1e424 235 return os;
a2fb1b05 236 }
ead1e424
ILT
237}
238
239// Return the output section to use for input section SHNDX, with name
730cdc88
ILT
240// NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
241// index of a relocation section which applies to this section, or 0
242// if none, or -1U if more than one. RELOC_TYPE is the type of the
243// relocation section if there is one. Set *OFF to the offset of this
244// input section without the output section. Return NULL if the
245// section should be discarded. Set *OFF to -1 if the section
246// contents should not be written directly to the output file, but
247// will instead receive special handling.
ead1e424
ILT
248
249template<int size, bool big_endian>
250Output_section*
730cdc88
ILT
251Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
252 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
253 unsigned int reloc_shndx, unsigned int, off_t* off)
ead1e424
ILT
254{
255 if (!this->include_section(object, name, shdr))
256 return NULL;
257
258 // If we are not doing a relocateable link, choose the name to use
259 // for the output section.
260 size_t len = strlen(name);
7e1edb90 261 if (!parameters->output_is_object())
ead1e424
ILT
262 name = Layout::output_section_name(name, &len);
263
264 // FIXME: Handle SHF_OS_NONCONFORMING here.
265
266 // Canonicalize the section name.
f0641a0b 267 Stringpool::Key name_key;
cfd73a4e 268 name = this->namepool_.add_prefix(name, len, &name_key);
ead1e424
ILT
269
270 // Find the output section. The output section is selected based on
271 // the section name, type, and flags.
f0641a0b
ILT
272 Output_section* os = this->get_output_section(name, name_key,
273 shdr.get_sh_type(),
ead1e424 274 shdr.get_sh_flags());
a2fb1b05
ILT
275
276 // FIXME: Handle SHF_LINK_ORDER somewhere.
277
730cdc88 278 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
a2fb1b05
ILT
279
280 return os;
281}
282
730cdc88
ILT
283// Special GNU handling of sections name .eh_frame. They will
284// normally hold exception frame data as defined by the C++ ABI
285// (http://codesourcery.com/cxx-abi/).
3151305a
ILT
286
287template<int size, bool big_endian>
730cdc88
ILT
288Output_section*
289Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
290 const unsigned char* symbols,
291 off_t symbols_size,
292 const unsigned char* symbol_names,
293 off_t symbol_names_size,
3151305a 294 unsigned int shndx,
3151305a 295 const elfcpp::Shdr<size, big_endian>& shdr,
730cdc88
ILT
296 unsigned int reloc_shndx, unsigned int reloc_type,
297 off_t* off)
3151305a 298{
730cdc88
ILT
299 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
300 gold_assert(shdr.get_sh_flags() == elfcpp::SHF_ALLOC);
301
302 Stringpool::Key name_key;
303 const char* name = this->namepool_.add(".eh_frame", false, &name_key);
304
305 Output_section* os = this->get_output_section(name, name_key,
306 elfcpp::SHT_PROGBITS,
307 elfcpp::SHF_ALLOC);
308
3151305a
ILT
309 if (this->eh_frame_section_ == NULL)
310 {
311 this->eh_frame_section_ = os;
730cdc88
ILT
312 this->eh_frame_data_ = new Eh_frame();
313 os->add_output_section_data(this->eh_frame_data_);
3151305a
ILT
314
315 if (this->options_.create_eh_frame_hdr())
316 {
317 Stringpool::Key hdr_name_key;
318 const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
cfd73a4e 319 false,
3151305a
ILT
320 &hdr_name_key);
321 Output_section* hdr_os =
322 this->get_output_section(hdr_name, hdr_name_key,
323 elfcpp::SHT_PROGBITS,
324 elfcpp::SHF_ALLOC);
325
730cdc88 326 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os, this->eh_frame_data_);
3151305a
ILT
327 hdr_os->add_output_section_data(hdr_posd);
328
730cdc88
ILT
329 hdr_os->set_after_input_sections();
330
3151305a
ILT
331 Output_segment* hdr_oseg =
332 new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
333 this->segment_list_.push_back(hdr_oseg);
334 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
730cdc88
ILT
335
336 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
3151305a
ILT
337 }
338 }
339
340 gold_assert(this->eh_frame_section_ == os);
341
730cdc88
ILT
342 if (this->eh_frame_data_->add_ehframe_input_section(object,
343 symbols,
344 symbols_size,
345 symbol_names,
346 symbol_names_size,
347 shndx,
348 reloc_shndx,
349 reloc_type))
350 *off = -1;
351 else
352 {
353 // We couldn't handle this .eh_frame section for some reason.
354 // Add it as a normal section.
355 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
356 }
357
358 return os;
3151305a
ILT
359}
360
ead1e424
ILT
361// Add POSD to an output section using NAME, TYPE, and FLAGS.
362
363void
364Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
365 elfcpp::Elf_Xword flags,
366 Output_section_data* posd)
367{
368 // Canonicalize the name.
f0641a0b 369 Stringpool::Key name_key;
cfd73a4e 370 name = this->namepool_.add(name, true, &name_key);
ead1e424 371
f0641a0b 372 Output_section* os = this->get_output_section(name, name_key, type, flags);
ead1e424
ILT
373 os->add_output_section_data(posd);
374}
375
a2fb1b05
ILT
376// Map section flags to segment flags.
377
378elfcpp::Elf_Word
379Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
380{
381 elfcpp::Elf_Word ret = elfcpp::PF_R;
382 if ((flags & elfcpp::SHF_WRITE) != 0)
383 ret |= elfcpp::PF_W;
384 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
385 ret |= elfcpp::PF_X;
386 return ret;
387}
388
389// Make a new Output_section, and attach it to segments as
390// appropriate.
391
392Output_section*
393Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
394 elfcpp::Elf_Xword flags)
395{
b8e6aad9 396 Output_section* os = new Output_section(name, type, flags);
a3ad94ed 397 this->section_list_.push_back(os);
a2fb1b05
ILT
398
399 if ((flags & elfcpp::SHF_ALLOC) == 0)
a3ad94ed 400 this->unattached_section_list_.push_back(os);
a2fb1b05
ILT
401 else
402 {
403 // This output section goes into a PT_LOAD segment.
404
405 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
406
407 // The only thing we really care about for PT_LOAD segments is
408 // whether or not they are writable, so that is how we search
409 // for them. People who need segments sorted on some other
410 // basis will have to wait until we implement a mechanism for
411 // them to describe the segments they want.
412
413 Segment_list::const_iterator p;
414 for (p = this->segment_list_.begin();
415 p != this->segment_list_.end();
416 ++p)
417 {
418 if ((*p)->type() == elfcpp::PT_LOAD
419 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
420 {
75f65a3e 421 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
422 break;
423 }
424 }
425
426 if (p == this->segment_list_.end())
427 {
428 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
429 seg_flags);
430 this->segment_list_.push_back(oseg);
75f65a3e 431 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
432 }
433
434 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
435 // segment.
436 if (type == elfcpp::SHT_NOTE)
437 {
438 // See if we already have an equivalent PT_NOTE segment.
439 for (p = this->segment_list_.begin();
440 p != segment_list_.end();
441 ++p)
442 {
443 if ((*p)->type() == elfcpp::PT_NOTE
444 && (((*p)->flags() & elfcpp::PF_W)
445 == (seg_flags & elfcpp::PF_W)))
446 {
75f65a3e 447 (*p)->add_output_section(os, seg_flags);
a2fb1b05
ILT
448 break;
449 }
450 }
451
452 if (p == this->segment_list_.end())
453 {
454 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
455 seg_flags);
456 this->segment_list_.push_back(oseg);
75f65a3e 457 oseg->add_output_section(os, seg_flags);
a2fb1b05
ILT
458 }
459 }
54dc6425
ILT
460
461 // If we see a loadable SHF_TLS section, we create a PT_TLS
92e059d8 462 // segment. There can only be one such segment.
54dc6425
ILT
463 if ((flags & elfcpp::SHF_TLS) != 0)
464 {
92e059d8 465 if (this->tls_segment_ == NULL)
54dc6425 466 {
92e059d8
ILT
467 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
468 seg_flags);
469 this->segment_list_.push_back(this->tls_segment_);
54dc6425 470 }
92e059d8 471 this->tls_segment_->add_output_section(os, seg_flags);
54dc6425 472 }
a2fb1b05
ILT
473 }
474
475 return os;
476}
477
35cdfc9a
ILT
478// Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
479// is whether we saw a .note.GNU-stack section in the object file.
480// GNU_STACK_FLAGS is the section flags. The flags give the
481// protection required for stack memory. We record this in an
482// executable as a PT_GNU_STACK segment. If an object file does not
483// have a .note.GNU-stack segment, we must assume that it is an old
484// object. On some targets that will force an executable stack.
485
486void
487Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
488{
489 if (!seen_gnu_stack)
490 this->input_without_gnu_stack_note_ = true;
491 else
492 {
493 this->input_with_gnu_stack_note_ = true;
494 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
495 this->input_requires_executable_stack_ = true;
496 }
497}
498
a3ad94ed
ILT
499// Create the dynamic sections which are needed before we read the
500// relocs.
501
502void
503Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
504 Symbol_table* symtab)
505{
436ca963 506 if (parameters->doing_static_link())
a3ad94ed
ILT
507 return;
508
cfd73a4e 509 const char* dynamic_name = this->namepool_.add(".dynamic", false, NULL);
a3ad94ed
ILT
510 this->dynamic_section_ = this->make_output_section(dynamic_name,
511 elfcpp::SHT_DYNAMIC,
512 (elfcpp::SHF_ALLOC
513 | elfcpp::SHF_WRITE));
514
14b31740 515 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
a3ad94ed
ILT
516 this->dynamic_section_, 0, 0,
517 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
518 elfcpp::STV_HIDDEN, 0, false, false);
16649710 519
9025d29d 520 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
16649710
ILT
521
522 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
a3ad94ed
ILT
523}
524
bfd58944
ILT
525// For each output section whose name can be represented as C symbol,
526// define __start and __stop symbols for the section. This is a GNU
527// extension.
528
529void
530Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
531{
532 for (Section_list::const_iterator p = this->section_list_.begin();
533 p != this->section_list_.end();
534 ++p)
535 {
536 const char* const name = (*p)->name();
537 if (name[strspn(name,
538 ("0123456789"
539 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
540 "abcdefghijklmnopqrstuvwxyz"
541 "_"))]
542 == '\0')
543 {
544 const std::string name_string(name);
545 const std::string start_name("__start_" + name_string);
546 const std::string stop_name("__stop_" + name_string);
547
548 symtab->define_in_output_data(target,
549 start_name.c_str(),
550 NULL, // version
551 *p,
552 0, // value
553 0, // symsize
554 elfcpp::STT_NOTYPE,
555 elfcpp::STB_GLOBAL,
556 elfcpp::STV_DEFAULT,
557 0, // nonvis
558 false, // offset_is_from_end
559 false); // only_if_ref
560
561 symtab->define_in_output_data(target,
562 stop_name.c_str(),
563 NULL, // version
564 *p,
565 0, // value
566 0, // symsize
567 elfcpp::STT_NOTYPE,
568 elfcpp::STB_GLOBAL,
569 elfcpp::STV_DEFAULT,
570 0, // nonvis
571 true, // offset_is_from_end
572 false); // only_if_ref
573 }
574 }
575}
576
75f65a3e
ILT
577// Find the first read-only PT_LOAD segment, creating one if
578// necessary.
54dc6425 579
75f65a3e
ILT
580Output_segment*
581Layout::find_first_load_seg()
54dc6425 582{
75f65a3e
ILT
583 for (Segment_list::const_iterator p = this->segment_list_.begin();
584 p != this->segment_list_.end();
585 ++p)
586 {
587 if ((*p)->type() == elfcpp::PT_LOAD
588 && ((*p)->flags() & elfcpp::PF_R) != 0
589 && ((*p)->flags() & elfcpp::PF_W) == 0)
590 return *p;
591 }
592
593 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
594 this->segment_list_.push_back(load_seg);
595 return load_seg;
54dc6425
ILT
596}
597
598// Finalize the layout. When this is called, we have created all the
599// output sections and all the output segments which are based on
600// input sections. We have several things to do, and we have to do
601// them in the right order, so that we get the right results correctly
602// and efficiently.
603
604// 1) Finalize the list of output segments and create the segment
605// table header.
606
607// 2) Finalize the dynamic symbol table and associated sections.
608
609// 3) Determine the final file offset of all the output segments.
610
611// 4) Determine the final file offset of all the SHF_ALLOC output
612// sections.
613
75f65a3e
ILT
614// 5) Create the symbol table sections and the section name table
615// section.
616
617// 6) Finalize the symbol table: set symbol values to their final
54dc6425
ILT
618// value and make a final determination of which symbols are going
619// into the output symbol table.
620
54dc6425
ILT
621// 7) Create the section table header.
622
623// 8) Determine the final file offset of all the output sections which
624// are not SHF_ALLOC, including the section table header.
625
626// 9) Finalize the ELF file header.
627
75f65a3e
ILT
628// This function returns the size of the output file.
629
630off_t
631Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
54dc6425 632{
5a6f7e2d 633 Target* const target = input_objects->target();
dbe717ef 634
7e1edb90 635 target->finalize_sections(this);
5a6f7e2d 636
35cdfc9a
ILT
637 this->create_gold_note();
638 this->create_executable_stack_info(target);
4f211c8b 639
dbe717ef 640 Output_segment* phdr_seg = NULL;
436ca963 641 if (!parameters->doing_static_link())
54dc6425 642 {
dbe717ef
ILT
643 // There was a dynamic object in the link. We need to create
644 // some information for the dynamic linker.
645
646 // Create the PT_PHDR segment which will hold the program
647 // headers.
648 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
649 this->segment_list_.push_back(phdr_seg);
650
14b31740
ILT
651 // Create the dynamic symbol table, including the hash table.
652 Output_section* dynstr;
653 std::vector<Symbol*> dynamic_symbols;
654 unsigned int local_dynamic_count;
655 Versions versions;
656 this->create_dynamic_symtab(target, symtab, &dynstr,
657 &local_dynamic_count, &dynamic_symbols,
658 &versions);
dbe717ef
ILT
659
660 // Create the .interp section to hold the name of the
661 // interpreter, and put it in a PT_INTERP segment.
96f2030e
ILT
662 if (!parameters->output_is_shared())
663 this->create_interp(target);
a3ad94ed
ILT
664
665 // Finish the .dynamic section to hold the dynamic data, and put
666 // it in a PT_DYNAMIC segment.
16649710 667 this->finish_dynamic_section(input_objects, symtab);
14b31740
ILT
668
669 // We should have added everything we need to the dynamic string
670 // table.
671 this->dynpool_.set_string_offsets();
672
673 // Create the version sections. We can't do this until the
674 // dynamic string table is complete.
46fe1623 675 this->create_version_sections(&versions, symtab, local_dynamic_count,
14b31740 676 dynamic_symbols, dynstr);
54dc6425
ILT
677 }
678
679 // FIXME: Handle PT_GNU_STACK.
680
75f65a3e
ILT
681 Output_segment* load_seg = this->find_first_load_seg();
682
683 // Lay out the segment headers.
75f65a3e 684 Output_segment_headers* segment_headers;
9025d29d 685 segment_headers = new Output_segment_headers(this->segment_list_);
75f65a3e 686 load_seg->add_initial_output_data(segment_headers);
61ba1cf9 687 this->special_output_list_.push_back(segment_headers);
dbe717ef
ILT
688 if (phdr_seg != NULL)
689 phdr_seg->add_initial_output_data(segment_headers);
75f65a3e
ILT
690
691 // Lay out the file header.
692 Output_file_header* file_header;
9025d29d 693 file_header = new Output_file_header(target, symtab, segment_headers);
75f65a3e 694 load_seg->add_initial_output_data(file_header);
61ba1cf9 695 this->special_output_list_.push_back(file_header);
75f65a3e 696
ead1e424 697 // We set the output section indexes in set_segment_offsets and
27bc2bce 698 // set_section_indexes.
ead1e424
ILT
699 unsigned int shndx = 1;
700
701 // Set the file offsets of all the segments, and all the sections
702 // they contain.
a3ad94ed 703 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
75f65a3e
ILT
704
705 // Create the symbol table sections.
9025d29d 706 this->create_symtab_sections(input_objects, symtab, &off);
75f65a3e
ILT
707
708 // Create the .shstrtab section.
709 Output_section* shstrtab_section = this->create_shstrtab();
710
27bc2bce
ILT
711 // Set the file offsets of all the non-data sections which don't
712 // have to wait for the input sections.
86887060
ILT
713 off = this->set_section_offsets(off, false);
714
715 // Now that all sections have been created, set the section indexes.
716 shndx = this->set_section_indexes(shndx);
ead1e424 717
75f65a3e 718 // Create the section table header.
27bc2bce 719 this->create_shdrs(&off);
75f65a3e 720
27bc2bce 721 file_header->set_section_info(this->section_headers_, shstrtab_section);
75f65a3e 722
27bc2bce
ILT
723 // Now we know exactly where everything goes in the output file
724 // (except for non-allocated sections which require postprocessing).
a3ad94ed 725 Output_data::layout_complete();
75f65a3e 726
e44fcf3b
ILT
727 this->output_file_size_ = off;
728
75f65a3e
ILT
729 return off;
730}
731
4f211c8b
ILT
732// Create a .note section for an executable or shared library. This
733// records the version of gold used to create the binary.
734
735void
35cdfc9a 736Layout::create_gold_note()
4f211c8b
ILT
737{
738 if (parameters->output_is_object())
739 return;
740
e2305dc0
ILT
741 // Authorities all agree that the values in a .note field should
742 // be aligned on 4-byte boundaries for 32-bit binaries. However,
743 // they differ on what the alignment is for 64-bit binaries.
744 // The GABI says unambiguously they take 8-byte alignment:
745 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
746 // Other documentation says alignment should always be 4 bytes:
747 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
748 // GNU ld and GNU readelf both support the latter (at least as of
749 // version 2.16.91), and glibc always generates the latter for
750 // .note.ABI-tag (as of version 1.6), so that's the one we go with
751 // here.
35cdfc9a 752#ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
4f211c8b 753 const int size = parameters->get_size();
e2305dc0
ILT
754#else
755 const int size = 32;
756#endif
4f211c8b
ILT
757
758 // The contents of the .note section.
759 const char* name = "GNU";
760 std::string desc(std::string("gold ") + gold::get_version_string());
761 size_t namesz = strlen(name) + 1;
762 size_t aligned_namesz = align_address(namesz, size / 8);
763 size_t descsz = desc.length() + 1;
764 size_t aligned_descsz = align_address(descsz, size / 8);
765 const int note_type = 4;
766
767 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
768
769 unsigned char buffer[128];
770 gold_assert(sizeof buffer >= notesz);
771 memset(buffer, 0, notesz);
772
773 bool is_big_endian = parameters->is_big_endian();
774
775 if (size == 32)
776 {
777 if (!is_big_endian)
778 {
779 elfcpp::Swap<32, false>::writeval(buffer, namesz);
780 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
781 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
782 }
783 else
784 {
785 elfcpp::Swap<32, true>::writeval(buffer, namesz);
786 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
787 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
788 }
789 }
790 else if (size == 64)
791 {
792 if (!is_big_endian)
793 {
794 elfcpp::Swap<64, false>::writeval(buffer, namesz);
795 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
796 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
797 }
798 else
799 {
800 elfcpp::Swap<64, true>::writeval(buffer, namesz);
801 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
802 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
803 }
804 }
805 else
806 gold_unreachable();
807
808 memcpy(buffer + 3 * (size / 8), name, namesz);
809 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
810
cfd73a4e 811 const char* note_name = this->namepool_.add(".note", false, NULL);
4f211c8b
ILT
812 Output_section* os = this->make_output_section(note_name,
813 elfcpp::SHT_NOTE,
814 0);
815 Output_section_data* posd = new Output_data_const(buffer, notesz,
816 size / 8);
817 os->add_output_section_data(posd);
818}
819
35cdfc9a
ILT
820// Record whether the stack should be executable. This can be set
821// from the command line using the -z execstack or -z noexecstack
822// options. Otherwise, if any input file has a .note.GNU-stack
823// section with the SHF_EXECINSTR flag set, the stack should be
824// executable. Otherwise, if at least one input file a
825// .note.GNU-stack section, and some input file has no .note.GNU-stack
826// section, we use the target default for whether the stack should be
827// executable. Otherwise, we don't generate a stack note. When
828// generating a object file, we create a .note.GNU-stack section with
829// the appropriate marking. When generating an executable or shared
830// library, we create a PT_GNU_STACK segment.
831
832void
833Layout::create_executable_stack_info(const Target* target)
834{
835 bool is_stack_executable;
836 if (this->options_.is_execstack_set())
837 is_stack_executable = this->options_.is_stack_executable();
838 else if (!this->input_with_gnu_stack_note_)
839 return;
840 else
841 {
842 if (this->input_requires_executable_stack_)
843 is_stack_executable = true;
844 else if (this->input_without_gnu_stack_note_)
845 is_stack_executable = target->is_default_stack_executable();
846 else
847 is_stack_executable = false;
848 }
849
850 if (parameters->output_is_object())
851 {
852 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
853 elfcpp::Elf_Xword flags = 0;
854 if (is_stack_executable)
855 flags |= elfcpp::SHF_EXECINSTR;
856 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
857 }
858 else
859 {
860 int flags = elfcpp::PF_R | elfcpp::PF_W;
861 if (is_stack_executable)
862 flags |= elfcpp::PF_X;
863 Output_segment* oseg = new Output_segment(elfcpp::PT_GNU_STACK, flags);
864 this->segment_list_.push_back(oseg);
865 }
866}
867
75f65a3e
ILT
868// Return whether SEG1 should be before SEG2 in the output file. This
869// is based entirely on the segment type and flags. When this is
870// called the segment addresses has normally not yet been set.
871
872bool
873Layout::segment_precedes(const Output_segment* seg1,
874 const Output_segment* seg2)
875{
876 elfcpp::Elf_Word type1 = seg1->type();
877 elfcpp::Elf_Word type2 = seg2->type();
878
879 // The single PT_PHDR segment is required to precede any loadable
880 // segment. We simply make it always first.
881 if (type1 == elfcpp::PT_PHDR)
882 {
a3ad94ed 883 gold_assert(type2 != elfcpp::PT_PHDR);
75f65a3e
ILT
884 return true;
885 }
886 if (type2 == elfcpp::PT_PHDR)
887 return false;
888
889 // The single PT_INTERP segment is required to precede any loadable
890 // segment. We simply make it always second.
891 if (type1 == elfcpp::PT_INTERP)
892 {
a3ad94ed 893 gold_assert(type2 != elfcpp::PT_INTERP);
75f65a3e
ILT
894 return true;
895 }
896 if (type2 == elfcpp::PT_INTERP)
897 return false;
898
899 // We then put PT_LOAD segments before any other segments.
900 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
901 return true;
902 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
903 return false;
904
92e059d8
ILT
905 // We put the PT_TLS segment last, because that is where the dynamic
906 // linker expects to find it (this is just for efficiency; other
907 // positions would also work correctly).
908 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
909 return false;
910 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
911 return true;
912
75f65a3e
ILT
913 const elfcpp::Elf_Word flags1 = seg1->flags();
914 const elfcpp::Elf_Word flags2 = seg2->flags();
915
916 // The order of non-PT_LOAD segments is unimportant. We simply sort
917 // by the numeric segment type and flags values. There should not
918 // be more than one segment with the same type and flags.
919 if (type1 != elfcpp::PT_LOAD)
920 {
921 if (type1 != type2)
922 return type1 < type2;
a3ad94ed 923 gold_assert(flags1 != flags2);
75f65a3e
ILT
924 return flags1 < flags2;
925 }
926
927 // We sort PT_LOAD segments based on the flags. Readonly segments
928 // come before writable segments. Then executable segments come
929 // before non-executable segments. Then the unlikely case of a
930 // non-readable segment comes before the normal case of a readable
931 // segment. If there are multiple segments with the same type and
932 // flags, we require that the address be set, and we sort by
933 // virtual address and then physical address.
934 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
935 return (flags1 & elfcpp::PF_W) == 0;
936 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
937 return (flags1 & elfcpp::PF_X) != 0;
938 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
939 return (flags1 & elfcpp::PF_R) == 0;
940
941 uint64_t vaddr1 = seg1->vaddr();
942 uint64_t vaddr2 = seg2->vaddr();
943 if (vaddr1 != vaddr2)
944 return vaddr1 < vaddr2;
945
946 uint64_t paddr1 = seg1->paddr();
947 uint64_t paddr2 = seg2->paddr();
a3ad94ed 948 gold_assert(paddr1 != paddr2);
75f65a3e
ILT
949 return paddr1 < paddr2;
950}
951
ead1e424
ILT
952// Set the file offsets of all the segments, and all the sections they
953// contain. They have all been created. LOAD_SEG must be be laid out
954// first. Return the offset of the data to follow.
75f65a3e
ILT
955
956off_t
ead1e424
ILT
957Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
958 unsigned int *pshndx)
75f65a3e
ILT
959{
960 // Sort them into the final order.
54dc6425
ILT
961 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
962 Layout::Compare_segments());
963
75f65a3e
ILT
964 // Find the PT_LOAD segments, and set their addresses and offsets
965 // and their section's addresses and offsets.
0c5e9c22
ILT
966 uint64_t addr;
967 if (options_.user_set_text_segment_address())
968 addr = options_.text_segment_address();
969 else
970 addr = target->default_text_segment_address();
75f65a3e
ILT
971 off_t off = 0;
972 bool was_readonly = false;
973 for (Segment_list::iterator p = this->segment_list_.begin();
974 p != this->segment_list_.end();
975 ++p)
976 {
977 if ((*p)->type() == elfcpp::PT_LOAD)
978 {
979 if (load_seg != NULL && load_seg != *p)
a3ad94ed 980 gold_unreachable();
75f65a3e
ILT
981 load_seg = NULL;
982
983 // If the last segment was readonly, and this one is not,
984 // then skip the address forward one page, maintaining the
985 // same position within the page. This lets us store both
986 // segments overlapping on a single page in the file, but
987 // the loader will put them on different pages in memory.
988
989 uint64_t orig_addr = addr;
990 uint64_t orig_off = off;
991
992 uint64_t aligned_addr = addr;
993 uint64_t abi_pagesize = target->abi_pagesize();
0496d5e5
ILT
994
995 // FIXME: This should depend on the -n and -N options.
996 (*p)->set_minimum_addralign(target->common_pagesize());
997
75f65a3e
ILT
998 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
999 {
ead1e424 1000 uint64_t align = (*p)->addralign();
75f65a3e 1001
ead1e424 1002 addr = align_address(addr, align);
75f65a3e
ILT
1003 aligned_addr = addr;
1004 if ((addr & (abi_pagesize - 1)) != 0)
1005 addr = addr + abi_pagesize;
1006 }
1007
ead1e424 1008 unsigned int shndx_hold = *pshndx;
75f65a3e 1009 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 1010 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
1011
1012 // Now that we know the size of this segment, we may be able
1013 // to save a page in memory, at the cost of wasting some
1014 // file space, by instead aligning to the start of a new
1015 // page. Here we use the real machine page size rather than
1016 // the ABI mandated page size.
1017
1018 if (aligned_addr != addr)
1019 {
1020 uint64_t common_pagesize = target->common_pagesize();
1021 uint64_t first_off = (common_pagesize
1022 - (aligned_addr
1023 & (common_pagesize - 1)));
1024 uint64_t last_off = new_addr & (common_pagesize - 1);
1025 if (first_off > 0
1026 && last_off > 0
1027 && ((aligned_addr & ~ (common_pagesize - 1))
1028 != (new_addr & ~ (common_pagesize - 1)))
1029 && first_off + last_off <= common_pagesize)
1030 {
ead1e424
ILT
1031 *pshndx = shndx_hold;
1032 addr = align_address(aligned_addr, common_pagesize);
75f65a3e 1033 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
ead1e424 1034 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
75f65a3e
ILT
1035 }
1036 }
1037
1038 addr = new_addr;
1039
1040 if (((*p)->flags() & elfcpp::PF_W) == 0)
1041 was_readonly = true;
1042 }
1043 }
1044
1045 // Handle the non-PT_LOAD segments, setting their offsets from their
1046 // section's offsets.
1047 for (Segment_list::iterator p = this->segment_list_.begin();
1048 p != this->segment_list_.end();
1049 ++p)
1050 {
1051 if ((*p)->type() != elfcpp::PT_LOAD)
1052 (*p)->set_offset();
1053 }
1054
1055 return off;
1056}
1057
1058// Set the file offset of all the sections not associated with a
1059// segment.
1060
1061off_t
27bc2bce 1062Layout::set_section_offsets(off_t off, bool after_input_sections)
75f65a3e 1063{
a3ad94ed
ILT
1064 for (Section_list::iterator p = this->unattached_section_list_.begin();
1065 p != this->unattached_section_list_.end();
75f65a3e
ILT
1066 ++p)
1067 {
27bc2bce
ILT
1068 // The symtab section is handled in create_symtab_sections.
1069 if (*p == this->symtab_section_)
61ba1cf9 1070 continue;
27bc2bce
ILT
1071
1072 if ((*p)->after_input_sections() != after_input_sections)
1073 continue;
1074
ead1e424 1075 off = align_address(off, (*p)->addralign());
27bc2bce
ILT
1076 (*p)->set_file_offset(off);
1077 (*p)->finalize_data_size();
75f65a3e
ILT
1078 off += (*p)->data_size();
1079 }
1080 return off;
1081}
1082
86887060
ILT
1083// Set the section indexes of all the sections not associated with a
1084// segment.
1085
1086unsigned int
1087Layout::set_section_indexes(unsigned int shndx)
1088{
1089 for (Section_list::iterator p = this->unattached_section_list_.begin();
1090 p != this->unattached_section_list_.end();
1091 ++p)
1092 {
1093 (*p)->set_out_shndx(shndx);
1094 ++shndx;
1095 }
1096 return shndx;
1097}
1098
b8e6aad9
ILT
1099// Create the symbol table sections. Here we also set the final
1100// values of the symbols. At this point all the loadable sections are
1101// fully laid out.
75f65a3e
ILT
1102
1103void
9025d29d 1104Layout::create_symtab_sections(const Input_objects* input_objects,
75f65a3e 1105 Symbol_table* symtab,
16649710 1106 off_t* poff)
75f65a3e 1107{
61ba1cf9
ILT
1108 int symsize;
1109 unsigned int align;
9025d29d 1110 if (parameters->get_size() == 32)
61ba1cf9
ILT
1111 {
1112 symsize = elfcpp::Elf_sizes<32>::sym_size;
1113 align = 4;
1114 }
9025d29d 1115 else if (parameters->get_size() == 64)
61ba1cf9
ILT
1116 {
1117 symsize = elfcpp::Elf_sizes<64>::sym_size;
1118 align = 8;
1119 }
1120 else
a3ad94ed 1121 gold_unreachable();
61ba1cf9
ILT
1122
1123 off_t off = *poff;
ead1e424 1124 off = align_address(off, align);
61ba1cf9
ILT
1125 off_t startoff = off;
1126
1127 // Save space for the dummy symbol at the start of the section. We
1128 // never bother to write this out--it will just be left as zero.
1129 off += symsize;
c06b7b0b 1130 unsigned int local_symbol_index = 1;
61ba1cf9 1131
a3ad94ed
ILT
1132 // Add STT_SECTION symbols for each Output section which needs one.
1133 for (Section_list::iterator p = this->section_list_.begin();
1134 p != this->section_list_.end();
1135 ++p)
1136 {
1137 if (!(*p)->needs_symtab_index())
1138 (*p)->set_symtab_index(-1U);
1139 else
1140 {
1141 (*p)->set_symtab_index(local_symbol_index);
1142 ++local_symbol_index;
1143 off += symsize;
1144 }
1145 }
1146
f6ce93d6
ILT
1147 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1148 p != input_objects->relobj_end();
75f65a3e
ILT
1149 ++p)
1150 {
1151 Task_lock_obj<Object> tlo(**p);
c06b7b0b
ILT
1152 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1153 off,
1154 &this->sympool_);
1155 off += (index - local_symbol_index) * symsize;
1156 local_symbol_index = index;
75f65a3e
ILT
1157 }
1158
c06b7b0b 1159 unsigned int local_symcount = local_symbol_index;
a3ad94ed 1160 gold_assert(local_symcount * symsize == off - startoff);
61ba1cf9 1161
16649710
ILT
1162 off_t dynoff;
1163 size_t dyn_global_index;
1164 size_t dyncount;
1165 if (this->dynsym_section_ == NULL)
1166 {
1167 dynoff = 0;
1168 dyn_global_index = 0;
1169 dyncount = 0;
1170 }
1171 else
1172 {
1173 dyn_global_index = this->dynsym_section_->info();
1174 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1175 dynoff = this->dynsym_section_->offset() + locsize;
1176 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
f5c3f225 1177 gold_assert(static_cast<off_t>(dyncount * symsize)
16649710
ILT
1178 == this->dynsym_section_->data_size() - locsize);
1179 }
1180
1181 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
1182 dyncount, &this->sympool_);
75f65a3e 1183
9e2dcb77
ILT
1184 if (!parameters->strip_all())
1185 {
1186 this->sympool_.set_string_offsets();
61ba1cf9 1187
cfd73a4e 1188 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
9e2dcb77
ILT
1189 Output_section* osymtab = this->make_output_section(symtab_name,
1190 elfcpp::SHT_SYMTAB,
1191 0);
1192 this->symtab_section_ = osymtab;
a3ad94ed 1193
27bc2bce
ILT
1194 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1195 align);
9e2dcb77 1196 osymtab->add_output_section_data(pos);
61ba1cf9 1197
cfd73a4e 1198 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
9e2dcb77
ILT
1199 Output_section* ostrtab = this->make_output_section(strtab_name,
1200 elfcpp::SHT_STRTAB,
1201 0);
a3ad94ed 1202
9e2dcb77
ILT
1203 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1204 ostrtab->add_output_section_data(pstr);
61ba1cf9 1205
27bc2bce
ILT
1206 osymtab->set_file_offset(startoff);
1207 osymtab->finalize_data_size();
9e2dcb77
ILT
1208 osymtab->set_link_section(ostrtab);
1209 osymtab->set_info(local_symcount);
1210 osymtab->set_entsize(symsize);
61ba1cf9 1211
9e2dcb77
ILT
1212 *poff = off;
1213 }
75f65a3e
ILT
1214}
1215
1216// Create the .shstrtab section, which holds the names of the
1217// sections. At the time this is called, we have created all the
1218// output sections except .shstrtab itself.
1219
1220Output_section*
1221Layout::create_shstrtab()
1222{
1223 // FIXME: We don't need to create a .shstrtab section if we are
1224 // stripping everything.
1225
cfd73a4e 1226 const char* name = this->namepool_.add(".shstrtab", false, NULL);
75f65a3e 1227
a3ad94ed 1228 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
75f65a3e 1229
27bc2bce
ILT
1230 // We can't write out this section until we've set all the section
1231 // names, and we don't set the names of compressed output sections
1232 // until relocations are complete.
1233 os->set_after_input_sections();
1234
a3ad94ed
ILT
1235 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1236 os->add_output_section_data(posd);
75f65a3e
ILT
1237
1238 return os;
1239}
1240
1241// Create the section headers. SIZE is 32 or 64. OFF is the file
1242// offset.
1243
27bc2bce 1244void
9025d29d 1245Layout::create_shdrs(off_t* poff)
75f65a3e
ILT
1246{
1247 Output_section_headers* oshdrs;
9025d29d 1248 oshdrs = new Output_section_headers(this,
16649710
ILT
1249 &this->segment_list_,
1250 &this->unattached_section_list_,
61ba1cf9 1251 &this->namepool_);
ead1e424 1252 off_t off = align_address(*poff, oshdrs->addralign());
27bc2bce 1253 oshdrs->set_address_and_file_offset(0, off);
61ba1cf9
ILT
1254 off += oshdrs->data_size();
1255 *poff = off;
27bc2bce 1256 this->section_headers_ = oshdrs;
54dc6425
ILT
1257}
1258
dbe717ef
ILT
1259// Create the dynamic symbol table.
1260
1261void
14b31740
ILT
1262Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
1263 Output_section **pdynstr,
1264 unsigned int* plocal_dynamic_count,
1265 std::vector<Symbol*>* pdynamic_symbols,
1266 Versions* pversions)
dbe717ef 1267{
a3ad94ed
ILT
1268 // Count all the symbols in the dynamic symbol table, and set the
1269 // dynamic symbol indexes.
dbe717ef 1270
a3ad94ed
ILT
1271 // Skip symbol 0, which is always all zeroes.
1272 unsigned int index = 1;
dbe717ef 1273
a3ad94ed
ILT
1274 // Add STT_SECTION symbols for each Output section which needs one.
1275 for (Section_list::iterator p = this->section_list_.begin();
1276 p != this->section_list_.end();
1277 ++p)
1278 {
1279 if (!(*p)->needs_dynsym_index())
1280 (*p)->set_dynsym_index(-1U);
1281 else
1282 {
1283 (*p)->set_dynsym_index(index);
1284 ++index;
1285 }
1286 }
1287
1288 // FIXME: Some targets apparently require local symbols in the
1289 // dynamic symbol table. Here is where we will have to count them,
1290 // and set the dynamic symbol indexes, and add the names to
1291 // this->dynpool_.
1292
1293 unsigned int local_symcount = index;
14b31740 1294 *plocal_dynamic_count = local_symcount;
a3ad94ed
ILT
1295
1296 // FIXME: We have to tell set_dynsym_indexes whether the
1297 // -E/--export-dynamic option was used.
35cdfc9a
ILT
1298 index = symtab->set_dynsym_indexes(target, index, pdynamic_symbols,
1299 &this->dynpool_, pversions);
a3ad94ed
ILT
1300
1301 int symsize;
1302 unsigned int align;
9025d29d 1303 const int size = parameters->get_size();
a3ad94ed
ILT
1304 if (size == 32)
1305 {
1306 symsize = elfcpp::Elf_sizes<32>::sym_size;
1307 align = 4;
1308 }
1309 else if (size == 64)
1310 {
1311 symsize = elfcpp::Elf_sizes<64>::sym_size;
1312 align = 8;
1313 }
1314 else
1315 gold_unreachable();
1316
14b31740
ILT
1317 // Create the dynamic symbol table section.
1318
cfd73a4e 1319 const char* dynsym_name = this->namepool_.add(".dynsym", false, NULL);
a3ad94ed
ILT
1320 Output_section* dynsym = this->make_output_section(dynsym_name,
1321 elfcpp::SHT_DYNSYM,
1322 elfcpp::SHF_ALLOC);
1323
27bc2bce
ILT
1324 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
1325 align);
a3ad94ed
ILT
1326 dynsym->add_output_section_data(odata);
1327
1328 dynsym->set_info(local_symcount);
1329 dynsym->set_entsize(symsize);
1330 dynsym->set_addralign(align);
1331
1332 this->dynsym_section_ = dynsym;
1333
16649710 1334 Output_data_dynamic* const odyn = this->dynamic_data_;
a3ad94ed
ILT
1335 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1336 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1337
14b31740
ILT
1338 // Create the dynamic string table section.
1339
cfd73a4e 1340 const char* dynstr_name = this->namepool_.add(".dynstr", false, NULL);
a3ad94ed
ILT
1341 Output_section* dynstr = this->make_output_section(dynstr_name,
1342 elfcpp::SHT_STRTAB,
1343 elfcpp::SHF_ALLOC);
1344
1345 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1346 dynstr->add_output_section_data(strdata);
1347
16649710
ILT
1348 dynsym->set_link_section(dynstr);
1349 this->dynamic_section_->set_link_section(dynstr);
1350
a3ad94ed
ILT
1351 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1352 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1353
14b31740
ILT
1354 *pdynstr = dynstr;
1355
1356 // Create the hash tables.
1357
a3ad94ed
ILT
1358 // FIXME: We need an option to create a GNU hash table.
1359
1360 unsigned char* phash;
1361 unsigned int hashlen;
9025d29d 1362 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
a3ad94ed
ILT
1363 &phash, &hashlen);
1364
cfd73a4e 1365 const char* hash_name = this->namepool_.add(".hash", false, NULL);
a3ad94ed
ILT
1366 Output_section* hashsec = this->make_output_section(hash_name,
1367 elfcpp::SHT_HASH,
1368 elfcpp::SHF_ALLOC);
1369
1370 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1371 hashlen,
1372 align);
1373 hashsec->add_output_section_data(hashdata);
1374
16649710 1375 hashsec->set_link_section(dynsym);
a3ad94ed 1376 hashsec->set_entsize(4);
a3ad94ed
ILT
1377
1378 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
dbe717ef
ILT
1379}
1380
14b31740
ILT
1381// Create the version sections.
1382
1383void
9025d29d 1384Layout::create_version_sections(const Versions* versions,
46fe1623 1385 const Symbol_table* symtab,
14b31740
ILT
1386 unsigned int local_symcount,
1387 const std::vector<Symbol*>& dynamic_symbols,
1388 const Output_section* dynstr)
1389{
1390 if (!versions->any_defs() && !versions->any_needs())
1391 return;
1392
9025d29d 1393 if (parameters->get_size() == 32)
14b31740 1394 {
9025d29d 1395 if (parameters->is_big_endian())
193a53d9
ILT
1396 {
1397#ifdef HAVE_TARGET_32_BIG
1398 this->sized_create_version_sections
1399 SELECT_SIZE_ENDIAN_NAME(32, true)(
46fe1623 1400 versions, symtab, local_symcount, dynamic_symbols, dynstr
193a53d9
ILT
1401 SELECT_SIZE_ENDIAN(32, true));
1402#else
1403 gold_unreachable();
1404#endif
1405 }
14b31740 1406 else
193a53d9
ILT
1407 {
1408#ifdef HAVE_TARGET_32_LITTLE
1409 this->sized_create_version_sections
1410 SELECT_SIZE_ENDIAN_NAME(32, false)(
46fe1623 1411 versions, symtab, local_symcount, dynamic_symbols, dynstr
193a53d9
ILT
1412 SELECT_SIZE_ENDIAN(32, false));
1413#else
1414 gold_unreachable();
1415#endif
1416 }
14b31740 1417 }
9025d29d 1418 else if (parameters->get_size() == 64)
14b31740 1419 {
9025d29d 1420 if (parameters->is_big_endian())
193a53d9
ILT
1421 {
1422#ifdef HAVE_TARGET_64_BIG
1423 this->sized_create_version_sections
1424 SELECT_SIZE_ENDIAN_NAME(64, true)(
46fe1623 1425 versions, symtab, local_symcount, dynamic_symbols, dynstr
193a53d9
ILT
1426 SELECT_SIZE_ENDIAN(64, true));
1427#else
1428 gold_unreachable();
1429#endif
1430 }
14b31740 1431 else
193a53d9
ILT
1432 {
1433#ifdef HAVE_TARGET_64_LITTLE
1434 this->sized_create_version_sections
1435 SELECT_SIZE_ENDIAN_NAME(64, false)(
46fe1623 1436 versions, symtab, local_symcount, dynamic_symbols, dynstr
193a53d9
ILT
1437 SELECT_SIZE_ENDIAN(64, false));
1438#else
1439 gold_unreachable();
1440#endif
1441 }
14b31740
ILT
1442 }
1443 else
1444 gold_unreachable();
1445}
1446
1447// Create the version sections, sized version.
1448
1449template<int size, bool big_endian>
1450void
1451Layout::sized_create_version_sections(
1452 const Versions* versions,
46fe1623 1453 const Symbol_table* symtab,
14b31740
ILT
1454 unsigned int local_symcount,
1455 const std::vector<Symbol*>& dynamic_symbols,
91da9340
ILT
1456 const Output_section* dynstr
1457 ACCEPT_SIZE_ENDIAN)
14b31740 1458{
cfd73a4e 1459 const char* vname = this->namepool_.add(".gnu.version", false, NULL);
14b31740
ILT
1460 Output_section* vsec = this->make_output_section(vname,
1461 elfcpp::SHT_GNU_versym,
1462 elfcpp::SHF_ALLOC);
1463
1464 unsigned char* vbuf;
1465 unsigned int vsize;
91da9340 1466 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
46fe1623 1467 symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
7e1edb90 1468 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1469
1470 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1471
1472 vsec->add_output_section_data(vdata);
1473 vsec->set_entsize(2);
1474 vsec->set_link_section(this->dynsym_section_);
1475
1476 Output_data_dynamic* const odyn = this->dynamic_data_;
1477 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1478
1479 if (versions->any_defs())
1480 {
cfd73a4e 1481 const char* vdname = this->namepool_.add(".gnu.version_d", false, NULL);
14b31740
ILT
1482 Output_section *vdsec;
1483 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1484 elfcpp::SHF_ALLOC);
1485
1486 unsigned char* vdbuf;
1487 unsigned int vdsize;
1488 unsigned int vdentries;
91da9340
ILT
1489 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1490 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1491 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1492
1493 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1494 vdsize,
1495 4);
1496
1497 vdsec->add_output_section_data(vddata);
1498 vdsec->set_link_section(dynstr);
1499 vdsec->set_info(vdentries);
1500
1501 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1502 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1503 }
1504
1505 if (versions->any_needs())
1506 {
cfd73a4e 1507 const char* vnname = this->namepool_.add(".gnu.version_r", false, NULL);
14b31740
ILT
1508 Output_section* vnsec;
1509 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1510 elfcpp::SHF_ALLOC);
1511
1512 unsigned char* vnbuf;
1513 unsigned int vnsize;
1514 unsigned int vnentries;
91da9340
ILT
1515 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1516 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1517 SELECT_SIZE_ENDIAN(size, big_endian));
14b31740
ILT
1518
1519 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1520 vnsize,
1521 4);
1522
1523 vnsec->add_output_section_data(vndata);
1524 vnsec->set_link_section(dynstr);
1525 vnsec->set_info(vnentries);
1526
1527 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1528 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1529 }
1530}
1531
dbe717ef
ILT
1532// Create the .interp section and PT_INTERP segment.
1533
1534void
1535Layout::create_interp(const Target* target)
1536{
1537 const char* interp = this->options_.dynamic_linker();
1538 if (interp == NULL)
1539 {
1540 interp = target->dynamic_linker();
a3ad94ed 1541 gold_assert(interp != NULL);
dbe717ef
ILT
1542 }
1543
1544 size_t len = strlen(interp) + 1;
1545
1546 Output_section_data* odata = new Output_data_const(interp, len, 1);
1547
cfd73a4e 1548 const char* interp_name = this->namepool_.add(".interp", false, NULL);
dbe717ef
ILT
1549 Output_section* osec = this->make_output_section(interp_name,
1550 elfcpp::SHT_PROGBITS,
1551 elfcpp::SHF_ALLOC);
1552 osec->add_output_section_data(odata);
1553
1554 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1555 this->segment_list_.push_back(oseg);
1556 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1557}
1558
a3ad94ed
ILT
1559// Finish the .dynamic section and PT_DYNAMIC segment.
1560
1561void
1562Layout::finish_dynamic_section(const Input_objects* input_objects,
16649710 1563 const Symbol_table* symtab)
a3ad94ed 1564{
a3ad94ed
ILT
1565 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1566 elfcpp::PF_R | elfcpp::PF_W);
1567 this->segment_list_.push_back(oseg);
1568 oseg->add_initial_output_section(this->dynamic_section_,
1569 elfcpp::PF_R | elfcpp::PF_W);
1570
16649710
ILT
1571 Output_data_dynamic* const odyn = this->dynamic_data_;
1572
a3ad94ed
ILT
1573 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1574 p != input_objects->dynobj_end();
1575 ++p)
1576 {
1577 // FIXME: Handle --as-needed.
1578 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1579 }
1580
1581 // FIXME: Support --init and --fini.
1582 Symbol* sym = symtab->lookup("_init");
14b31740 1583 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1584 odyn->add_symbol(elfcpp::DT_INIT, sym);
1585
1586 sym = symtab->lookup("_fini");
14b31740 1587 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
1588 odyn->add_symbol(elfcpp::DT_FINI, sym);
1589
1590 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
41f542e7
ILT
1591
1592 // Add a DT_RPATH entry if needed.
1593 const General_options::Dir_list& rpath(this->options_.rpath());
1594 if (!rpath.empty())
1595 {
1596 std::string rpath_val;
1597 for (General_options::Dir_list::const_iterator p = rpath.begin();
1598 p != rpath.end();
1599 ++p)
1600 {
1601 if (rpath_val.empty())
ad2d6943 1602 rpath_val = p->name();
41f542e7
ILT
1603 else
1604 {
1605 // Eliminate duplicates.
1606 General_options::Dir_list::const_iterator q;
1607 for (q = rpath.begin(); q != p; ++q)
ad2d6943 1608 if (q->name() == p->name())
41f542e7
ILT
1609 break;
1610 if (q == p)
1611 {
1612 rpath_val += ':';
ad2d6943 1613 rpath_val += p->name();
41f542e7
ILT
1614 }
1615 }
1616 }
1617
1618 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1619 }
4f4c5f80
ILT
1620
1621 // Look for text segments that have dynamic relocations.
1622 bool have_textrel = false;
1623 for (Segment_list::const_iterator p = this->segment_list_.begin();
1624 p != this->segment_list_.end();
1625 ++p)
1626 {
1627 if (((*p)->flags() & elfcpp::PF_W) == 0
1628 && (*p)->dynamic_reloc_count() > 0)
1629 {
1630 have_textrel = true;
1631 break;
1632 }
1633 }
1634
1635 // Add a DT_FLAGS entry. We add it even if no flags are set so that
1636 // post-link tools can easily modify these flags if desired.
1637 unsigned int flags = 0;
1638 if (have_textrel)
1639 flags |= elfcpp::DF_TEXTREL;
1640 odyn->add_constant(elfcpp::DT_FLAGS, flags);
a3ad94ed
ILT
1641}
1642
a2fb1b05
ILT
1643// The mapping of .gnu.linkonce section names to real section names.
1644
ead1e424 1645#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
a2fb1b05
ILT
1646const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1647{
1648 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1649 MAPPING_INIT("t", ".text"),
1650 MAPPING_INIT("r", ".rodata"),
1651 MAPPING_INIT("d", ".data"),
1652 MAPPING_INIT("b", ".bss"),
1653 MAPPING_INIT("s", ".sdata"),
1654 MAPPING_INIT("sb", ".sbss"),
1655 MAPPING_INIT("s2", ".sdata2"),
1656 MAPPING_INIT("sb2", ".sbss2"),
1657 MAPPING_INIT("wi", ".debug_info"),
1658 MAPPING_INIT("td", ".tdata"),
1659 MAPPING_INIT("tb", ".tbss"),
1660 MAPPING_INIT("lr", ".lrodata"),
1661 MAPPING_INIT("l", ".ldata"),
1662 MAPPING_INIT("lb", ".lbss"),
1663};
1664#undef MAPPING_INIT
1665
1666const int Layout::linkonce_mapping_count =
1667 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1668
1669// Return the name of the output section to use for a .gnu.linkonce
1670// section. This is based on the default ELF linker script of the old
1671// GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
ead1e424
ILT
1672// to ".text". Set *PLEN to the length of the name. *PLEN is
1673// initialized to the length of NAME.
a2fb1b05
ILT
1674
1675const char*
ead1e424 1676Layout::linkonce_output_name(const char* name, size_t *plen)
a2fb1b05
ILT
1677{
1678 const char* s = name + sizeof(".gnu.linkonce") - 1;
1679 if (*s != '.')
1680 return name;
1681 ++s;
1682 const Linkonce_mapping* plm = linkonce_mapping;
1683 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1684 {
1685 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
ead1e424
ILT
1686 {
1687 *plen = plm->tolen;
1688 return plm->to;
1689 }
a2fb1b05
ILT
1690 }
1691 return name;
1692}
1693
ead1e424
ILT
1694// Choose the output section name to use given an input section name.
1695// Set *PLEN to the length of the name. *PLEN is initialized to the
1696// length of NAME.
1697
1698const char*
1699Layout::output_section_name(const char* name, size_t* plen)
1700{
1701 if (Layout::is_linkonce(name))
1702 {
1703 // .gnu.linkonce sections are laid out as though they were named
1704 // for the sections are placed into.
1705 return Layout::linkonce_output_name(name, plen);
1706 }
1707
af4a8a83
ILT
1708 // gcc 4.3 generates the following sorts of section names when it
1709 // needs a section name specific to a function:
1710 // .text.FN
1711 // .rodata.FN
1712 // .sdata2.FN
1713 // .data.FN
1714 // .data.rel.FN
1715 // .data.rel.local.FN
1716 // .data.rel.ro.FN
1717 // .data.rel.ro.local.FN
1718 // .sdata.FN
1719 // .bss.FN
1720 // .sbss.FN
1721 // .tdata.FN
1722 // .tbss.FN
1723
1724 // The GNU linker maps all of those to the part before the .FN,
1725 // except that .data.rel.local.FN is mapped to .data, and
1726 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
1727 // beginning with .data.rel.ro.local are grouped together.
1728
1729 // For an anonymous namespace, the string FN can contain a '.'.
1730
1731 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
1732 // GNU linker maps to .rodata.
1733
1734 // The .data.rel.ro sections enable a security feature triggered by
1735 // the -z relro option. Section which need to be relocated at
1736 // program startup time but which may be readonly after startup are
1737 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
1738 // segment. The dynamic linker will make that segment writable,
1739 // perform relocations, and then make it read-only. FIXME: We do
1740 // not yet implement this optimization.
1741
1742 // It is hard to handle this in a principled way.
1743
1744 // These are the rules we follow:
1745
1746 // If the section name has no initial '.', or no dot other than an
1747 // initial '.', we use the name unchanged (i.e., "mysection" and
1748 // ".text" are unchanged).
1749
1750 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
1751
1752 // Otherwise, we drop the second '.' and everything that comes after
1753 // it (i.e., ".text.XXX" becomes ".text").
ead1e424
ILT
1754
1755 const char* s = name;
af4a8a83
ILT
1756 if (*s != '.')
1757 return name;
1758 ++s;
ead1e424
ILT
1759 const char* sdot = strchr(s, '.');
1760 if (sdot == NULL)
1761 return name;
1762
af4a8a83
ILT
1763 const char* const data_rel_ro = ".data.rel.ro";
1764 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
ead1e424 1765 {
af4a8a83
ILT
1766 *plen = strlen(data_rel_ro);
1767 return data_rel_ro;
ead1e424
ILT
1768 }
1769
ead1e424
ILT
1770 *plen = sdot - name;
1771 return name;
1772}
1773
a2fb1b05
ILT
1774// Record the signature of a comdat section, and return whether to
1775// include it in the link. If GROUP is true, this is a regular
1776// section group. If GROUP is false, this is a group signature
1777// derived from the name of a linkonce section. We want linkonce
1778// signatures and group signatures to block each other, but we don't
1779// want a linkonce signature to block another linkonce signature.
1780
1781bool
1782Layout::add_comdat(const char* signature, bool group)
1783{
1784 std::string sig(signature);
1785 std::pair<Signatures::iterator, bool> ins(
ead1e424 1786 this->signatures_.insert(std::make_pair(sig, group)));
a2fb1b05
ILT
1787
1788 if (ins.second)
1789 {
1790 // This is the first time we've seen this signature.
1791 return true;
1792 }
1793
1794 if (ins.first->second)
1795 {
1796 // We've already seen a real section group with this signature.
1797 return false;
1798 }
1799 else if (group)
1800 {
1801 // This is a real section group, and we've already seen a
a0fa0c07 1802 // linkonce section with this signature. Record that we've seen
a2fb1b05
ILT
1803 // a section group, and don't include this section group.
1804 ins.first->second = true;
1805 return false;
1806 }
1807 else
1808 {
1809 // We've already seen a linkonce section and this is a linkonce
1810 // section. These don't block each other--this may be the same
1811 // symbol name with different section types.
1812 return true;
1813 }
1814}
1815
730cdc88
ILT
1816// Write out the Output_sections. Most won't have anything to write,
1817// since most of the data will come from input sections which are
1818// handled elsewhere. But some Output_sections do have Output_data.
1819
1820void
1821Layout::write_output_sections(Output_file* of) const
1822{
1823 for (Section_list::const_iterator p = this->section_list_.begin();
1824 p != this->section_list_.end();
1825 ++p)
1826 {
1827 if (!(*p)->after_input_sections())
1828 (*p)->write(of);
1829 }
1830}
1831
61ba1cf9
ILT
1832// Write out data not associated with a section or the symbol table.
1833
1834void
9025d29d 1835Layout::write_data(const Symbol_table* symtab, Output_file* of) const
61ba1cf9 1836{
9e2dcb77 1837 if (!parameters->strip_all())
a3ad94ed 1838 {
9e2dcb77
ILT
1839 const Output_section* symtab_section = this->symtab_section_;
1840 for (Section_list::const_iterator p = this->section_list_.begin();
1841 p != this->section_list_.end();
1842 ++p)
a3ad94ed 1843 {
9e2dcb77
ILT
1844 if ((*p)->needs_symtab_index())
1845 {
1846 gold_assert(symtab_section != NULL);
1847 unsigned int index = (*p)->symtab_index();
1848 gold_assert(index > 0 && index != -1U);
1849 off_t off = (symtab_section->offset()
1850 + index * symtab_section->entsize());
1851 symtab->write_section_symbol(*p, of, off);
1852 }
a3ad94ed
ILT
1853 }
1854 }
1855
1856 const Output_section* dynsym_section = this->dynsym_section_;
1857 for (Section_list::const_iterator p = this->section_list_.begin();
1858 p != this->section_list_.end();
1859 ++p)
1860 {
1861 if ((*p)->needs_dynsym_index())
1862 {
1863 gold_assert(dynsym_section != NULL);
1864 unsigned int index = (*p)->dynsym_index();
1865 gold_assert(index > 0 && index != -1U);
1866 off_t off = (dynsym_section->offset()
1867 + index * dynsym_section->entsize());
9025d29d 1868 symtab->write_section_symbol(*p, of, off);
a3ad94ed
ILT
1869 }
1870 }
1871
a3ad94ed 1872 // Write out the Output_data which are not in an Output_section.
61ba1cf9
ILT
1873 for (Data_list::const_iterator p = this->special_output_list_.begin();
1874 p != this->special_output_list_.end();
1875 ++p)
1876 (*p)->write(of);
1877}
1878
730cdc88
ILT
1879// Write out the Output_sections which can only be written after the
1880// input sections are complete.
1881
1882void
27bc2bce 1883Layout::write_sections_after_input_sections(Output_file* of)
730cdc88 1884{
27bc2bce
ILT
1885 // Determine the final section offsets, and thus the final output
1886 // file size.
1887 off_t off = this->output_file_size_;
1888 off = this->set_section_offsets(off, true);
1889 if (off > this->output_file_size_)
1890 {
1891 of->resize(off);
1892 this->output_file_size_ = off;
1893 }
1894
730cdc88
ILT
1895 for (Section_list::const_iterator p = this->section_list_.begin();
1896 p != this->section_list_.end();
1897 ++p)
1898 {
1899 if ((*p)->after_input_sections())
1900 (*p)->write(of);
1901 }
27bc2bce
ILT
1902
1903 for (Section_list::const_iterator p = this->unattached_section_list_.begin();
1904 p != this->unattached_section_list_.end();
1905 ++p)
1906 {
1907 if ((*p)->after_input_sections())
1908 (*p)->write(of);
1909 }
1910
1911 this->section_headers_->write(of);
730cdc88
ILT
1912}
1913
1914// Write_sections_task methods.
1915
1916// We can always run this task.
1917
1918Task::Is_runnable_type
1919Write_sections_task::is_runnable(Workqueue*)
1920{
1921 return IS_RUNNABLE;
1922}
1923
1924// We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
1925// when finished.
1926
1927class Write_sections_task::Write_sections_locker : public Task_locker
1928{
1929 public:
1930 Write_sections_locker(Task_token& output_sections_blocker,
1931 Task_token& final_blocker,
1932 Workqueue* workqueue)
1933 : output_sections_block_(output_sections_blocker, workqueue),
1934 final_block_(final_blocker, workqueue)
1935 { }
1936
1937 private:
1938 Task_block_token output_sections_block_;
1939 Task_block_token final_block_;
1940};
1941
1942Task_locker*
1943Write_sections_task::locks(Workqueue* workqueue)
1944{
1945 return new Write_sections_locker(*this->output_sections_blocker_,
1946 *this->final_blocker_,
1947 workqueue);
1948}
1949
1950// Run the task--write out the data.
1951
1952void
1953Write_sections_task::run(Workqueue*)
1954{
1955 this->layout_->write_output_sections(this->of_);
1956}
1957
61ba1cf9
ILT
1958// Write_data_task methods.
1959
1960// We can always run this task.
1961
1962Task::Is_runnable_type
1963Write_data_task::is_runnable(Workqueue*)
1964{
1965 return IS_RUNNABLE;
1966}
1967
1968// We need to unlock FINAL_BLOCKER when finished.
1969
1970Task_locker*
1971Write_data_task::locks(Workqueue* workqueue)
1972{
1973 return new Task_locker_block(*this->final_blocker_, workqueue);
1974}
1975
1976// Run the task--write out the data.
1977
1978void
1979Write_data_task::run(Workqueue*)
1980{
9025d29d 1981 this->layout_->write_data(this->symtab_, this->of_);
61ba1cf9
ILT
1982}
1983
1984// Write_symbols_task methods.
1985
1986// We can always run this task.
1987
1988Task::Is_runnable_type
1989Write_symbols_task::is_runnable(Workqueue*)
1990{
1991 return IS_RUNNABLE;
1992}
1993
1994// We need to unlock FINAL_BLOCKER when finished.
1995
1996Task_locker*
1997Write_symbols_task::locks(Workqueue* workqueue)
1998{
1999 return new Task_locker_block(*this->final_blocker_, workqueue);
2000}
2001
2002// Run the task--write out the symbols.
2003
2004void
2005Write_symbols_task::run(Workqueue*)
2006{
9a2d6984
ILT
2007 this->symtab_->write_globals(this->input_objects_, this->sympool_,
2008 this->dynpool_, this->of_);
61ba1cf9
ILT
2009}
2010
730cdc88
ILT
2011// Write_after_input_sections_task methods.
2012
2013// We can only run this task after the input sections have completed.
2014
2015Task::Is_runnable_type
2016Write_after_input_sections_task::is_runnable(Workqueue*)
2017{
2018 if (this->input_sections_blocker_->is_blocked())
2019 return IS_BLOCKED;
2020 return IS_RUNNABLE;
2021}
2022
2023// We need to unlock FINAL_BLOCKER when finished.
2024
2025Task_locker*
2026Write_after_input_sections_task::locks(Workqueue* workqueue)
2027{
2028 return new Task_locker_block(*this->final_blocker_, workqueue);
2029}
2030
2031// Run the task.
2032
2033void
2034Write_after_input_sections_task::run(Workqueue*)
2035{
2036 this->layout_->write_sections_after_input_sections(this->of_);
2037}
2038
92e059d8 2039// Close_task_runner methods.
61ba1cf9
ILT
2040
2041// Run the task--close the file.
2042
2043void
92e059d8 2044Close_task_runner::run(Workqueue*)
61ba1cf9
ILT
2045{
2046 this->of_->close();
2047}
2048
a2fb1b05
ILT
2049// Instantiate the templates we need. We could use the configure
2050// script to restrict this to only the ones for implemented targets.
2051
193a53d9 2052#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
2053template
2054Output_section*
730cdc88
ILT
2055Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
2056 const char* name,
2057 const elfcpp::Shdr<32, false>& shdr,
2058 unsigned int, unsigned int, off_t*);
193a53d9 2059#endif
a2fb1b05 2060
193a53d9 2061#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
2062template
2063Output_section*
730cdc88
ILT
2064Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
2065 const char* name,
2066 const elfcpp::Shdr<32, true>& shdr,
2067 unsigned int, unsigned int, off_t*);
193a53d9 2068#endif
a2fb1b05 2069
193a53d9 2070#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
2071template
2072Output_section*
730cdc88
ILT
2073Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
2074 const char* name,
2075 const elfcpp::Shdr<64, false>& shdr,
2076 unsigned int, unsigned int, off_t*);
193a53d9 2077#endif
a2fb1b05 2078
193a53d9 2079#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
2080template
2081Output_section*
730cdc88
ILT
2082Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
2083 const char* name,
2084 const elfcpp::Shdr<64, true>& shdr,
2085 unsigned int, unsigned int, off_t*);
193a53d9 2086#endif
a2fb1b05 2087
730cdc88
ILT
2088#ifdef HAVE_TARGET_32_LITTLE
2089template
2090Output_section*
2091Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
2092 const unsigned char* symbols,
2093 off_t symbols_size,
2094 const unsigned char* symbol_names,
2095 off_t symbol_names_size,
2096 unsigned int shndx,
2097 const elfcpp::Shdr<32, false>& shdr,
2098 unsigned int reloc_shndx,
2099 unsigned int reloc_type,
2100 off_t* off);
2101#endif
2102
2103#ifdef HAVE_TARGET_32_BIG
2104template
2105Output_section*
2106Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
2107 const unsigned char* symbols,
2108 off_t symbols_size,
2109 const unsigned char* symbol_names,
2110 off_t symbol_names_size,
2111 unsigned int shndx,
2112 const elfcpp::Shdr<32, true>& shdr,
2113 unsigned int reloc_shndx,
2114 unsigned int reloc_type,
2115 off_t* off);
2116#endif
2117
2118#ifdef HAVE_TARGET_64_LITTLE
2119template
2120Output_section*
2121Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
2122 const unsigned char* symbols,
2123 off_t symbols_size,
2124 const unsigned char* symbol_names,
2125 off_t symbol_names_size,
2126 unsigned int shndx,
2127 const elfcpp::Shdr<64, false>& shdr,
2128 unsigned int reloc_shndx,
2129 unsigned int reloc_type,
2130 off_t* off);
2131#endif
2132
2133#ifdef HAVE_TARGET_64_BIG
2134template
2135Output_section*
2136Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
2137 const unsigned char* symbols,
2138 off_t symbols_size,
2139 const unsigned char* symbol_names,
2140 off_t symbol_names_size,
2141 unsigned int shndx,
2142 const elfcpp::Shdr<64, true>& shdr,
2143 unsigned int reloc_shndx,
2144 unsigned int reloc_type,
2145 off_t* off);
2146#endif
a2fb1b05
ILT
2147
2148} // End namespace gold.
This page took 0.212525 seconds and 4 git commands to generate.