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