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