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