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