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