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