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