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