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