BFD/DWARF2: Correct an `index' global shadowing error
[deliverable/binutils-gdb.git] / gold / layout.cc
CommitLineData
a2fb1b05
ILT
1// layout.cc -- lay out output file sections for gold
2
6f2750fe 3// Copyright (C) 2006-2016 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
8ed814a9 25#include <cerrno>
a2fb1b05 26#include <cstring>
54dc6425 27#include <algorithm>
a2fb1b05 28#include <iostream>
6e9ba2ca 29#include <fstream>
a2fb1b05 30#include <utility>
8ed814a9 31#include <fcntl.h>
6e9ba2ca 32#include <fnmatch.h>
8ed814a9
ILT
33#include <unistd.h>
34#include "libiberty.h"
35#include "md5.h"
36#include "sha1.h"
a2fb1b05 37
7e1edb90 38#include "parameters.h"
14144f39 39#include "options.h"
7d9e3d98 40#include "mapfile.h"
a445fddf
ILT
41#include "script.h"
42#include "script-sections.h"
a2fb1b05 43#include "output.h"
f6ce93d6 44#include "symtab.h"
a3ad94ed 45#include "dynobj.h"
3151305a 46#include "ehframe.h"
c1027032 47#include "gdb-index.h"
96803768 48#include "compressed_output.h"
62b01cb5 49#include "reduced_debug_output.h"
487b39df 50#include "object.h"
6a74a719 51#include "reloc.h"
2a00e4fb 52#include "descriptors.h"
2756a258 53#include "plugin.h"
3ce2c28e
ILT
54#include "incremental.h"
55#include "layout.h"
a2fb1b05
ILT
56
57namespace gold
58{
59
cdc29364
CC
60// Class Free_list.
61
62// The total number of free lists used.
63unsigned int Free_list::num_lists = 0;
64// The total number of free list nodes used.
65unsigned int Free_list::num_nodes = 0;
66// The total number of calls to Free_list::remove.
67unsigned int Free_list::num_removes = 0;
68// The total number of nodes visited during calls to Free_list::remove.
69unsigned int Free_list::num_remove_visits = 0;
70// The total number of calls to Free_list::allocate.
71unsigned int Free_list::num_allocates = 0;
72// The total number of nodes visited during calls to Free_list::allocate.
73unsigned int Free_list::num_allocate_visits = 0;
74
75// Initialize the free list. Creates a single free list node that
76// describes the entire region of length LEN. If EXTEND is true,
77// allocate() is allowed to extend the region beyond its initial
78// length.
79
80void
81Free_list::init(off_t len, bool extend)
82{
83 this->list_.push_front(Free_list_node(0, len));
84 this->last_remove_ = this->list_.begin();
85 this->extend_ = extend;
86 this->length_ = len;
87 ++Free_list::num_lists;
88 ++Free_list::num_nodes;
89}
90
91// Remove a chunk from the free list. Because we start with a single
92// node that covers the entire section, and remove chunks from it one
93// at a time, we do not need to coalesce chunks or handle cases that
94// span more than one free node. We expect to remove chunks from the
95// free list in order, and we expect to have only a few chunks of free
96// space left (corresponding to files that have changed since the last
97// incremental link), so a simple linear list should provide sufficient
98// performance.
99
100void
101Free_list::remove(off_t start, off_t end)
102{
103 if (start == end)
104 return;
105 gold_assert(start < end);
106
107 ++Free_list::num_removes;
108
109 Iterator p = this->last_remove_;
110 if (p->start_ > start)
111 p = this->list_.begin();
112
113 for (; p != this->list_.end(); ++p)
114 {
115 ++Free_list::num_remove_visits;
116 // Find a node that wholly contains the indicated region.
117 if (p->start_ <= start && p->end_ >= end)
118 {
119 // Case 1: the indicated region spans the whole node.
120 // Add some fuzz to avoid creating tiny free chunks.
121 if (p->start_ + 3 >= start && p->end_ <= end + 3)
122 p = this->list_.erase(p);
123 // Case 2: remove a chunk from the start of the node.
124 else if (p->start_ + 3 >= start)
125 p->start_ = end;
126 // Case 3: remove a chunk from the end of the node.
127 else if (p->end_ <= end + 3)
128 p->end_ = start;
129 // Case 4: remove a chunk from the middle, and split
130 // the node into two.
131 else
132 {
133 Free_list_node newnode(p->start_, start);
134 p->start_ = end;
135 this->list_.insert(p, newnode);
136 ++Free_list::num_nodes;
137 }
138 this->last_remove_ = p;
139 return;
140 }
141 }
142
143 // Did not find a node containing the given chunk. This could happen
144 // because a small chunk was already removed due to the fuzz.
145 gold_debug(DEBUG_INCREMENTAL,
146 "Free_list::remove(%d,%d) not found",
147 static_cast<int>(start), static_cast<int>(end));
148}
149
150// Allocate a chunk of size LEN from the free list. Returns -1ULL
151// if a sufficiently large chunk of free space is not found.
152// We use a simple first-fit algorithm.
153
154off_t
155Free_list::allocate(off_t len, uint64_t align, off_t minoff)
156{
157 gold_debug(DEBUG_INCREMENTAL,
2e702c99
RM
158 "Free_list::allocate(%08lx, %d, %08lx)",
159 static_cast<long>(len), static_cast<int>(align),
160 static_cast<long>(minoff));
cdc29364
CC
161 if (len == 0)
162 return align_address(minoff, align);
163
164 ++Free_list::num_allocates;
165
8ea8cd50
CC
166 // We usually want to drop free chunks smaller than 4 bytes.
167 // If we need to guarantee a minimum hole size, though, we need
168 // to keep track of all free chunks.
169 const int fuzz = this->min_hole_ > 0 ? 0 : 3;
170
cdc29364
CC
171 for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
172 {
173 ++Free_list::num_allocate_visits;
174 off_t start = p->start_ > minoff ? p->start_ : minoff;
175 start = align_address(start, align);
176 off_t end = start + len;
9fbd3822
CC
177 if (end > p->end_ && p->end_ == this->length_ && this->extend_)
178 {
179 this->length_ = end;
180 p->end_ = end;
181 }
8ea8cd50 182 if (end == p->end_ || (end <= p->end_ - this->min_hole_))
cdc29364 183 {
8ea8cd50 184 if (p->start_ + fuzz >= start && p->end_ <= end + fuzz)
cdc29364 185 this->list_.erase(p);
8ea8cd50 186 else if (p->start_ + fuzz >= start)
cdc29364 187 p->start_ = end;
8ea8cd50 188 else if (p->end_ <= end + fuzz)
cdc29364
CC
189 p->end_ = start;
190 else
191 {
192 Free_list_node newnode(p->start_, start);
193 p->start_ = end;
194 this->list_.insert(p, newnode);
195 ++Free_list::num_nodes;
196 }
197 return start;
198 }
199 }
9fbd3822
CC
200 if (this->extend_)
201 {
202 off_t start = align_address(this->length_, align);
203 this->length_ = start + len;
204 return start;
205 }
cdc29364
CC
206 return -1;
207}
208
209// Dump the free list (for debugging).
210void
211Free_list::dump()
212{
213 gold_info("Free list:\n start end length\n");
214 for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
215 gold_info(" %08lx %08lx %08lx", static_cast<long>(p->start_),
216 static_cast<long>(p->end_),
217 static_cast<long>(p->end_ - p->start_));
218}
219
220// Print the statistics for the free lists.
221void
222Free_list::print_stats()
223{
224 fprintf(stderr, _("%s: total free lists: %u\n"),
2e702c99 225 program_name, Free_list::num_lists);
cdc29364 226 fprintf(stderr, _("%s: total free list nodes: %u\n"),
2e702c99 227 program_name, Free_list::num_nodes);
cdc29364 228 fprintf(stderr, _("%s: calls to Free_list::remove: %u\n"),
2e702c99 229 program_name, Free_list::num_removes);
cdc29364 230 fprintf(stderr, _("%s: nodes visited: %u\n"),
2e702c99 231 program_name, Free_list::num_remove_visits);
cdc29364 232 fprintf(stderr, _("%s: calls to Free_list::allocate: %u\n"),
2e702c99 233 program_name, Free_list::num_allocates);
cdc29364 234 fprintf(stderr, _("%s: nodes visited: %u\n"),
2e702c99 235 program_name, Free_list::num_allocate_visits);
cdc29364
CC
236}
237
e7c5ea40 238// A Hash_task computes the MD5 checksum of an array of char.
e7c5ea40
CC
239
240class Hash_task : public Task
241{
242 public:
9c7fe3c5
CC
243 Hash_task(Output_file* of,
244 size_t offset,
bbc5ae17
RM
245 size_t size,
246 unsigned char* dst,
bbc5ae17 247 Task_token* final_blocker)
9c7fe3c5 248 : of_(of), offset_(offset), size_(size), dst_(dst),
e7c5ea40
CC
249 final_blocker_(final_blocker)
250 { }
251
252 void
253 run(Workqueue*)
9c7fe3c5
CC
254 {
255 const unsigned char* iv =
256 this->of_->get_input_view(this->offset_, this->size_);
257 md5_buffer(reinterpret_cast<const char*>(iv), this->size_, this->dst_);
258 this->of_->free_input_view(this->offset_, this->size_, iv);
259 }
e7c5ea40
CC
260
261 Task_token*
9c7fe3c5
CC
262 is_runnable()
263 { return NULL; }
e7c5ea40
CC
264
265 // Unblock FINAL_BLOCKER_ when done.
266 void
267 locks(Task_locker* tl)
268 { tl->add(this, this->final_blocker_); }
269
270 std::string
271 get_name() const
272 { return "Hash_task"; }
273
274 private:
9c7fe3c5
CC
275 Output_file* of_;
276 const size_t offset_;
e7c5ea40
CC
277 const size_t size_;
278 unsigned char* const dst_;
e7c5ea40
CC
279 Task_token* const final_blocker_;
280};
281
20e6d0d6
DK
282// Layout::Relaxation_debug_check methods.
283
284// Check that sections and special data are in reset states.
285// We do not save states for Output_sections and special Output_data.
286// So we check that they have not assigned any addresses or offsets.
287// clean_up_after_relaxation simply resets their addresses and offsets.
288void
289Layout::Relaxation_debug_check::check_output_data_for_reset_values(
290 const Layout::Section_list& sections,
eb426534
RM
291 const Layout::Data_list& special_outputs,
292 const Layout::Data_list& relax_outputs)
20e6d0d6
DK
293{
294 for(Layout::Section_list::const_iterator p = sections.begin();
295 p != sections.end();
296 ++p)
297 gold_assert((*p)->address_and_file_offset_have_reset_values());
298
299 for(Layout::Data_list::const_iterator p = special_outputs.begin();
300 p != special_outputs.end();
301 ++p)
302 gold_assert((*p)->address_and_file_offset_have_reset_values());
eb426534
RM
303
304 gold_assert(relax_outputs.empty());
20e6d0d6 305}
2e702c99 306
20e6d0d6
DK
307// Save information of SECTIONS for checking later.
308
309void
310Layout::Relaxation_debug_check::read_sections(
311 const Layout::Section_list& sections)
312{
313 for(Layout::Section_list::const_iterator p = sections.begin();
314 p != sections.end();
315 ++p)
316 {
317 Output_section* os = *p;
318 Section_info info;
319 info.output_section = os;
320 info.address = os->is_address_valid() ? os->address() : 0;
321 info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
322 info.offset = os->is_offset_valid()? os->offset() : -1 ;
323 this->section_infos_.push_back(info);
324 }
325}
326
327// Verify SECTIONS using previously recorded information.
328
329void
330Layout::Relaxation_debug_check::verify_sections(
331 const Layout::Section_list& sections)
332{
333 size_t i = 0;
334 for(Layout::Section_list::const_iterator p = sections.begin();
335 p != sections.end();
336 ++p, ++i)
337 {
338 Output_section* os = *p;
339 uint64_t address = os->is_address_valid() ? os->address() : 0;
340 off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
341 off_t offset = os->is_offset_valid()? os->offset() : -1 ;
342
343 if (i >= this->section_infos_.size())
344 {
345 gold_fatal("Section_info of %s missing.\n", os->name());
346 }
347 const Section_info& info = this->section_infos_[i];
348 if (os != info.output_section)
349 gold_fatal("Section order changed. Expecting %s but see %s\n",
350 info.output_section->name(), os->name());
351 if (address != info.address
352 || data_size != info.data_size
353 || offset != info.offset)
354 gold_fatal("Section %s changed.\n", os->name());
355 }
356}
357
92e059d8 358// Layout_task_runner methods.
a2fb1b05
ILT
359
360// Lay out the sections. This is called after all the input objects
361// have been read.
362
363void
17a1d0a9 364Layout_task_runner::run(Workqueue* workqueue, const Task* task)
a2fb1b05 365{
dc3714f3
AM
366 // See if any of the input definitions violate the One Definition Rule.
367 // TODO: if this is too slow, do this as a task, rather than inline.
368 this->symtab_->detect_odr_violations(task, this->options_.output_file_name());
369
94a3fc8b
CC
370 Layout* layout = this->layout_;
371 off_t file_size = layout->finalize(this->input_objects_,
372 this->symtab_,
2e702c99 373 this->target_,
94a3fc8b 374 task);
61ba1cf9
ILT
375
376 // Now we know the final size of the output file and we know where
377 // each piece of information goes.
7d9e3d98
ILT
378
379 if (this->mapfile_ != NULL)
380 {
381 this->mapfile_->print_discarded_sections(this->input_objects_);
94a3fc8b 382 layout->print_to_mapfile(this->mapfile_);
7d9e3d98
ILT
383 }
384
cdc29364 385 Output_file* of;
94a3fc8b 386 if (layout->incremental_base() == NULL)
cdc29364
CC
387 {
388 of = new Output_file(parameters->options().output_file_name());
389 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
390 of->set_is_temporary();
391 of->open(file_size);
392 }
393 else
394 {
94a3fc8b
CC
395 of = layout->incremental_base()->output_file();
396
397 // Apply the incremental relocations for symbols whose values
398 // have changed. We do this before we resize the file and start
399 // writing anything else to it, so that we can read the old
400 // incremental information from the file before (possibly)
401 // overwriting it.
402 if (parameters->incremental_update())
2e702c99
RM
403 layout->incremental_base()->apply_incremental_relocs(this->symtab_,
404 this->layout_,
94a3fc8b
CC
405 of);
406
cdc29364
CC
407 of->resize(file_size);
408 }
61ba1cf9
ILT
409
410 // Queue up the final set of tasks.
411 gold::queue_final_tasks(this->options_, this->input_objects_,
94a3fc8b 412 this->symtab_, layout, workqueue, of);
a2fb1b05
ILT
413}
414
415// Layout methods.
416
2ea97941 417Layout::Layout(int number_of_input_files, Script_options* script_options)
e55bde5e 418 : number_of_input_files_(number_of_input_files),
2ea97941 419 script_options_(script_options),
d491d34e
ILT
420 namepool_(),
421 sympool_(),
422 dynpool_(),
423 signatures_(),
424 section_name_map_(),
425 segment_list_(),
426 section_list_(),
427 unattached_section_list_(),
d491d34e 428 special_output_list_(),
eb426534 429 relax_output_list_(),
d491d34e
ILT
430 section_headers_(NULL),
431 tls_segment_(NULL),
9f1d377b 432 relro_segment_(NULL),
10b4f102 433 interp_segment_(NULL),
1a2dff53 434 increase_relro_(0),
d491d34e
ILT
435 symtab_section_(NULL),
436 symtab_xindex_(NULL),
437 dynsym_section_(NULL),
438 dynsym_xindex_(NULL),
439 dynamic_section_(NULL),
f0ba79e2 440 dynamic_symbol_(NULL),
d491d34e
ILT
441 dynamic_data_(NULL),
442 eh_frame_section_(NULL),
443 eh_frame_data_(NULL),
444 added_eh_frame_data_(false),
445 eh_frame_hdr_section_(NULL),
c1027032 446 gdb_index_data_(NULL),
d491d34e 447 build_id_note_(NULL),
62b01cb5
ILT
448 debug_abbrev_(NULL),
449 debug_info_(NULL),
d491d34e
ILT
450 group_signatures_(),
451 output_file_size_(-1),
d7bb5745 452 have_added_input_section_(false),
e55bde5e 453 sections_are_attached_(false),
35cdfc9a
ILT
454 input_requires_executable_stack_(false),
455 input_with_gnu_stack_note_(false),
535890bb 456 input_without_gnu_stack_note_(false),
17a1d0a9 457 has_static_tls_(false),
e55bde5e 458 any_postprocessing_sections_(false),
3ce2c28e 459 resized_signatures_(false),
1518dc8f 460 have_stabstr_section_(false),
e9552f7e 461 section_ordering_specified_(false),
16164a6b 462 unique_segment_for_sections_specified_(false),
20e6d0d6
DK
463 incremental_inputs_(NULL),
464 record_output_section_data_from_script_(false),
465 script_output_section_data_list_(),
466 segment_states_(NULL),
cdc29364 467 relaxation_debug_check_(NULL),
f0558624 468 section_order_map_(),
16164a6b 469 section_segment_map_(),
e9552f7e
ST
470 input_section_position_(),
471 input_section_glob_(),
cdc29364
CC
472 incremental_base_(NULL),
473 free_list_()
54dc6425
ILT
474{
475 // Make space for more than enough segments for a typical file.
476 // This is just for efficiency--it's OK if we wind up needing more.
a3ad94ed
ILT
477 this->segment_list_.reserve(12);
478
27bc2bce
ILT
479 // We expect two unattached Output_data objects: the file header and
480 // the segment headers.
481 this->special_output_list_.reserve(2);
3ce2c28e
ILT
482
483 // Initialize structure needed for an incremental build.
8c21d9d3 484 if (parameters->incremental())
3ce2c28e 485 this->incremental_inputs_ = new Incremental_inputs;
f7c8a183
ILT
486
487 // The section name pool is worth optimizing in all cases, because
488 // it is small, but there are often overlaps due to .rel sections.
489 this->namepool_.set_optimize();
54dc6425
ILT
490}
491
cdc29364
CC
492// For incremental links, record the base file to be modified.
493
494void
495Layout::set_incremental_base(Incremental_binary* base)
496{
497 this->incremental_base_ = base;
498 this->free_list_.init(base->output_file()->filesize(), true);
499}
500
a2fb1b05
ILT
501// Hash a key we use to look up an output section mapping.
502
503size_t
504Layout::Hash_key::operator()(const Layout::Key& k) const
505{
f0641a0b 506 return k.first + k.second.first + k.second.second;
a2fb1b05
ILT
507}
508
fb1b895d
CC
509// These are the debug sections that are actually used by gdb.
510// Currently, we've checked versions of gdb up to and including 7.4.
511// We only check the part of the name that follows ".debug_" or
512// ".zdebug_".
02d2ba74
ILT
513
514static const char* gdb_sections[] =
fb1b895d
CC
515{
516 "abbrev",
517 "addr", // Fission extension
518 // "aranges", // not used by gdb as of 7.4
519 "frame",
982bbd97 520 "gdb_scripts",
fb1b895d
CC
521 "info",
522 "types",
523 "line",
524 "loc",
525 "macinfo",
526 "macro",
527 // "pubnames", // not used by gdb as of 7.4
528 // "pubtypes", // not used by gdb as of 7.4
982bbd97
CC
529 // "gnu_pubnames", // Fission extension
530 // "gnu_pubtypes", // Fission extension
fb1b895d
CC
531 "ranges",
532 "str",
982bbd97 533 "str_offsets",
02d2ba74
ILT
534};
535
fb1b895d
CC
536// This is the minimum set of sections needed for line numbers.
537
62b01cb5 538static const char* lines_only_debug_sections[] =
fb1b895d
CC
539{
540 "abbrev",
541 // "addr", // Fission extension
542 // "aranges", // not used by gdb as of 7.4
543 // "frame",
982bbd97 544 // "gdb_scripts",
fb1b895d
CC
545 "info",
546 // "types",
547 "line",
548 // "loc",
549 // "macinfo",
550 // "macro",
551 // "pubnames", // not used by gdb as of 7.4
552 // "pubtypes", // not used by gdb as of 7.4
982bbd97
CC
553 // "gnu_pubnames", // Fission extension
554 // "gnu_pubtypes", // Fission extension
fb1b895d
CC
555 // "ranges",
556 "str",
982bbd97 557 "str_offsets", // Fission extension
fb1b895d
CC
558};
559
560// These sections are the DWARF fast-lookup tables, and are not needed
561// when building a .gdb_index section.
562
563static const char* gdb_fast_lookup_sections[] =
564{
565 "aranges",
566 "pubnames",
4320c691 567 "gnu_pubnames",
fb1b895d 568 "pubtypes",
4320c691 569 "gnu_pubtypes",
62b01cb5
ILT
570};
571
fb1b895d
CC
572// Returns whether the given debug section is in the list of
573// debug-sections-used-by-some-version-of-gdb. SUFFIX is the
574// portion of the name following ".debug_" or ".zdebug_".
575
02d2ba74 576static inline bool
fb1b895d 577is_gdb_debug_section(const char* suffix)
02d2ba74
ILT
578{
579 // We can do this faster: binary search or a hashtable. But why bother?
580 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
fb1b895d 581 if (strcmp(suffix, gdb_sections[i]) == 0)
02d2ba74
ILT
582 return true;
583 return false;
584}
585
fb1b895d
CC
586// Returns whether the given section is needed for lines-only debugging.
587
62b01cb5 588static inline bool
fb1b895d 589is_lines_only_debug_section(const char* suffix)
62b01cb5
ILT
590{
591 // We can do this faster: binary search or a hashtable. But why bother?
592 for (size_t i = 0;
593 i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
594 ++i)
fb1b895d
CC
595 if (strcmp(suffix, lines_only_debug_sections[i]) == 0)
596 return true;
597 return false;
598}
599
600// Returns whether the given section is a fast-lookup section that
601// will not be needed when building a .gdb_index section.
602
603static inline bool
604is_gdb_fast_lookup_section(const char* suffix)
605{
606 // We can do this faster: binary search or a hashtable. But why bother?
607 for (size_t i = 0;
608 i < sizeof(gdb_fast_lookup_sections)/sizeof(*gdb_fast_lookup_sections);
609 ++i)
610 if (strcmp(suffix, gdb_fast_lookup_sections[i]) == 0)
62b01cb5
ILT
611 return true;
612 return false;
613}
614
6fc6ea19
CC
615// Sometimes we compress sections. This is typically done for
616// sections that are not part of normal program execution (such as
617// .debug_* sections), and where the readers of these sections know
618// how to deal with compressed sections. This routine doesn't say for
619// certain whether we'll compress -- it depends on commandline options
620// as well -- just whether this section is a candidate for compression.
621// (The Output_compressed_section class decides whether to compress
622// a given section, and picks the name of the compressed section.)
623
624static bool
625is_compressible_debug_section(const char* secname)
626{
627 return (is_prefix_of(".debug", secname));
628}
629
630// We may see compressed debug sections in input files. Return TRUE
631// if this is the name of a compressed debug section.
632
633bool
634is_compressed_debug_section(const char* secname)
635{
636 return (is_prefix_of(".zdebug", secname));
637}
638
dd68f8fa
CC
639std::string
640corresponding_uncompressed_section_name(std::string secname)
641{
642 gold_assert(secname[0] == '.' && secname[1] == 'z');
643 std::string ret(".");
644 ret.append(secname, 2, std::string::npos);
645 return ret;
646}
647
a2fb1b05
ILT
648// Whether to include this section in the link.
649
650template<int size, bool big_endian>
651bool
6fa2a40b 652Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
a2fb1b05
ILT
653 const elfcpp::Shdr<size, big_endian>& shdr)
654{
ae034989
ST
655 if (!parameters->options().relocatable()
656 && (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE))
fd06b4aa
CC
657 return false;
658
99fd8cff
CC
659 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
660
661 if ((sh_type >= elfcpp::SHT_LOOS && sh_type <= elfcpp::SHT_HIOS)
662 || (sh_type >= elfcpp::SHT_LOPROC && sh_type <= elfcpp::SHT_HIPROC))
663 return parameters->target().should_include_section(sh_type);
664
665 switch (sh_type)
a2fb1b05
ILT
666 {
667 case elfcpp::SHT_NULL:
668 case elfcpp::SHT_SYMTAB:
669 case elfcpp::SHT_DYNSYM:
a2fb1b05
ILT
670 case elfcpp::SHT_HASH:
671 case elfcpp::SHT_DYNAMIC:
672 case elfcpp::SHT_SYMTAB_SHNDX:
673 return false;
674
5cb66f97
ILT
675 case elfcpp::SHT_STRTAB:
676 // Discard the sections which have special meanings in the ELF
677 // ABI. Keep others (e.g., .stabstr). We could also do this by
678 // checking the sh_link fields of the appropriate sections.
679 return (strcmp(name, ".dynstr") != 0
680 && strcmp(name, ".strtab") != 0
681 && strcmp(name, ".shstrtab") != 0);
682
a2fb1b05
ILT
683 case elfcpp::SHT_RELA:
684 case elfcpp::SHT_REL:
685 case elfcpp::SHT_GROUP:
7019cd25
ILT
686 // If we are emitting relocations these should be handled
687 // elsewhere.
1e2bee4f 688 gold_assert(!parameters->options().relocatable());
6a74a719 689 return false;
a2fb1b05 690
9e2dcb77 691 case elfcpp::SHT_PROGBITS:
8851ecca 692 if (parameters->options().strip_debug()
9e2dcb77
ILT
693 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
694 {
e94cf127 695 if (is_debug_info_section(name))
9e2dcb77
ILT
696 return false;
697 }
62b01cb5
ILT
698 if (parameters->options().strip_debug_non_line()
699 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
700 {
701 // Debugging sections can only be recognized by name.
fb1b895d
CC
702 if (is_prefix_of(".debug_", name)
703 && !is_lines_only_debug_section(name + 7))
704 return false;
705 if (is_prefix_of(".zdebug_", name)
706 && !is_lines_only_debug_section(name + 8))
62b01cb5
ILT
707 return false;
708 }
8851ecca 709 if (parameters->options().strip_debug_gdb()
02d2ba74
ILT
710 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
711 {
712 // Debugging sections can only be recognized by name.
fb1b895d
CC
713 if (is_prefix_of(".debug_", name)
714 && !is_gdb_debug_section(name + 7))
715 return false;
716 if (is_prefix_of(".zdebug_", name)
717 && !is_gdb_debug_section(name + 8))
718 return false;
719 }
720 if (parameters->options().gdb_index()
721 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
722 {
723 // When building .gdb_index, we can strip .debug_pubnames,
724 // .debug_pubtypes, and .debug_aranges sections.
725 if (is_prefix_of(".debug_", name)
726 && is_gdb_fast_lookup_section(name + 7))
727 return false;
728 if (is_prefix_of(".zdebug_", name)
729 && is_gdb_fast_lookup_section(name + 8))
02d2ba74
ILT
730 return false;
731 }
fd06b4aa 732 if (parameters->options().strip_lto_sections()
2e702c99
RM
733 && !parameters->options().relocatable()
734 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
735 {
736 // Ignore LTO sections containing intermediate code.
737 if (is_prefix_of(".gnu.lto_", name))
738 return false;
739 }
6b7dd3f3
ILT
740 // The GNU linker strips .gnu_debuglink sections, so we do too.
741 // This is a feature used to keep debugging information in
742 // separate files.
743 if (strcmp(name, ".gnu_debuglink") == 0)
744 return false;
9e2dcb77
ILT
745 return true;
746
a2fb1b05 747 default:
a2fb1b05
ILT
748 return true;
749 }
750}
751
ead1e424 752// Return an output section named NAME, or NULL if there is none.
a2fb1b05 753
a2fb1b05 754Output_section*
ead1e424 755Layout::find_output_section(const char* name) const
a2fb1b05 756{
a445fddf
ILT
757 for (Section_list::const_iterator p = this->section_list_.begin();
758 p != this->section_list_.end();
ead1e424 759 ++p)
a445fddf
ILT
760 if (strcmp((*p)->name(), name) == 0)
761 return *p;
ead1e424
ILT
762 return NULL;
763}
a2fb1b05 764
ead1e424
ILT
765// Return an output segment of type TYPE, with segment flags SET set
766// and segment flags CLEAR clear. Return NULL if there is none.
a2fb1b05 767
ead1e424
ILT
768Output_segment*
769Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
770 elfcpp::Elf_Word clear) const
771{
772 for (Segment_list::const_iterator p = this->segment_list_.begin();
773 p != this->segment_list_.end();
774 ++p)
775 if (static_cast<elfcpp::PT>((*p)->type()) == type
776 && ((*p)->flags() & set) == set
777 && ((*p)->flags() & clear) == 0)
778 return *p;
779 return NULL;
780}
a2fb1b05 781
487b39df
ILT
782// When we put a .ctors or .dtors section with more than one word into
783// a .init_array or .fini_array section, we need to reverse the words
784// in the .ctors/.dtors section. This is because .init_array executes
785// constructors front to back, where .ctors executes them back to
786// front, and vice-versa for .fini_array/.dtors. Although we do want
787// to remap .ctors/.dtors into .init_array/.fini_array because it can
788// be more efficient, we don't want to change the order in which
789// constructors/destructors are run. This set just keeps track of
790// these sections which need to be reversed. It is only changed by
791// Layout::layout. It should be a private member of Layout, but that
792// would require layout.h to #include object.h to get the definition
793// of Section_id.
794static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
795
796// Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
797// .init_array/.fini_array section.
798
799bool
800Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
801{
802 return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
803 != ctors_sections_in_init_array.end());
804}
805
ead1e424 806// Return the output section to use for section NAME with type TYPE
a445fddf 807// and section flags FLAGS. NAME must be canonicalized in the string
10b4f102
ILT
808// pool, and NAME_KEY is the key. ORDER is where this should appear
809// in the output sections. IS_RELRO is true for a relro section.
a2fb1b05 810
ead1e424 811Output_section*
f0641a0b 812Layout::get_output_section(const char* name, Stringpool::Key name_key,
f5c870d2 813 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
22f0da72 814 Output_section_order order, bool is_relro)
ead1e424 815{
5393d741
ILT
816 elfcpp::Elf_Word lookup_type = type;
817
818 // For lookup purposes, treat INIT_ARRAY, FINI_ARRAY, and
819 // PREINIT_ARRAY like PROGBITS. This ensures that we combine
820 // .init_array, .fini_array, and .preinit_array sections by name
821 // whatever their type in the input file. We do this because the
822 // types are not always right in the input files.
823 if (lookup_type == elfcpp::SHT_INIT_ARRAY
824 || lookup_type == elfcpp::SHT_FINI_ARRAY
825 || lookup_type == elfcpp::SHT_PREINIT_ARRAY)
826 lookup_type = elfcpp::SHT_PROGBITS;
827
154e0e9a
ILT
828 elfcpp::Elf_Xword lookup_flags = flags;
829
830 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
831 // read-write with read-only sections. Some other ELF linkers do
832 // not do this. FIXME: Perhaps there should be an option
833 // controlling this.
834 lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
835
5393d741 836 const Key key(name_key, std::make_pair(lookup_type, lookup_flags));
a2fb1b05
ILT
837 const std::pair<Key, Output_section*> v(key, NULL);
838 std::pair<Section_name_map::iterator, bool> ins(
839 this->section_name_map_.insert(v));
840
a2fb1b05 841 if (!ins.second)
ead1e424 842 return ins.first->second;
a2fb1b05
ILT
843 else
844 {
845 // This is the first time we've seen this name/type/flags
4e2b1697
ILT
846 // combination. For compatibility with the GNU linker, we
847 // combine sections with contents and zero flags with sections
848 // with non-zero flags. This is a workaround for cases where
849 // assembler code forgets to set section flags. FIXME: Perhaps
850 // there should be an option to control this.
15cf077e 851 Output_section* os = NULL;
4e2b1697 852
5393d741 853 if (lookup_type == elfcpp::SHT_PROGBITS)
15cf077e 854 {
2e702c99
RM
855 if (flags == 0)
856 {
857 Output_section* same_name = this->find_output_section(name);
858 if (same_name != NULL
859 && (same_name->type() == elfcpp::SHT_PROGBITS
5393d741
ILT
860 || same_name->type() == elfcpp::SHT_INIT_ARRAY
861 || same_name->type() == elfcpp::SHT_FINI_ARRAY
862 || same_name->type() == elfcpp::SHT_PREINIT_ARRAY)
2e702c99
RM
863 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
864 os = same_name;
865 }
866 else if ((flags & elfcpp::SHF_TLS) == 0)
867 {
868 elfcpp::Elf_Xword zero_flags = 0;
869 const Key zero_key(name_key, std::make_pair(lookup_type,
5393d741 870 zero_flags));
2e702c99
RM
871 Section_name_map::iterator p =
872 this->section_name_map_.find(zero_key);
873 if (p != this->section_name_map_.end())
154e0e9a 874 os = p->second;
2e702c99 875 }
15cf077e 876 }
4e2b1697 877
15cf077e 878 if (os == NULL)
22f0da72
ILT
879 os = this->make_output_section(name, type, flags, order, is_relro);
880
a2fb1b05 881 ins.first->second = os;
ead1e424 882 return os;
a2fb1b05 883 }
ead1e424
ILT
884}
885
b9b2ae8b
NC
886// Returns TRUE iff NAME (an input section from RELOBJ) will
887// be mapped to an output section that should be KEPT.
888
889bool
890Layout::keep_input_section(const Relobj* relobj, const char* name)
891{
892 if (! this->script_options_->saw_sections_clause())
893 return false;
894
895 Script_sections* ss = this->script_options_->script_sections();
896 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
897 Output_section** output_section_slot;
898 Script_sections::Section_type script_section_type;
899 bool keep;
900
901 name = ss->output_section_name(file_name, name, &output_section_slot,
902 &script_section_type, &keep);
903 return name != NULL && keep;
904}
905
16164a6b
ST
906// Clear the input section flags that should not be copied to the
907// output section.
908
909elfcpp::Elf_Xword
910Layout::get_output_section_flags(elfcpp::Elf_Xword input_section_flags)
911{
912 // Some flags in the input section should not be automatically
913 // copied to the output section.
914 input_section_flags &= ~ (elfcpp::SHF_INFO_LINK
915 | elfcpp::SHF_GROUP
48058663 916 | elfcpp::SHF_COMPRESSED
16164a6b
ST
917 | elfcpp::SHF_MERGE
918 | elfcpp::SHF_STRINGS);
919
920 // We only clear the SHF_LINK_ORDER flag in for
921 // a non-relocatable link.
922 if (!parameters->options().relocatable())
923 input_section_flags &= ~elfcpp::SHF_LINK_ORDER;
924
925 return input_section_flags;
926}
927
a445fddf
ILT
928// Pick the output section to use for section NAME, in input file
929// RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
154e0e9a
ILT
930// linker created section. IS_INPUT_SECTION is true if we are
931// choosing an output section for an input section found in a input
10b4f102
ILT
932// file. ORDER is where this section should appear in the output
933// sections. IS_RELRO is true for a relro section. This will return
934// NULL if the input section should be discarded.
a445fddf
ILT
935
936Output_section*
937Layout::choose_output_section(const Relobj* relobj, const char* name,
938 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
22f0da72
ILT
939 bool is_input_section, Output_section_order order,
940 bool is_relro)
a445fddf 941{
154e0e9a
ILT
942 // We should not see any input sections after we have attached
943 // sections to segments.
944 gold_assert(!is_input_section || !this->sections_are_attached_);
945
16164a6b 946 flags = this->get_output_section_flags(flags);
c9484ea5 947
a445fddf
ILT
948 if (this->script_options_->saw_sections_clause())
949 {
950 // We are using a SECTIONS clause, so the output section is
951 // chosen based only on the name.
952
953 Script_sections* ss = this->script_options_->script_sections();
954 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
955 Output_section** output_section_slot;
1e5d2fb1 956 Script_sections::Section_type script_section_type;
7f8cd844 957 const char* orig_name = name;
b9b2ae8b 958 bool keep;
1e5d2fb1 959 name = ss->output_section_name(file_name, name, &output_section_slot,
b9b2ae8b
NC
960 &script_section_type, &keep);
961
a445fddf
ILT
962 if (name == NULL)
963 {
7f8cd844
NC
964 gold_debug(DEBUG_SCRIPT, _("Unable to create output section '%s' "
965 "because it is not allowed by the "
966 "SECTIONS clause of the linker script"),
967 orig_name);
a445fddf
ILT
968 // The SECTIONS clause says to discard this input section.
969 return NULL;
970 }
971
1e5d2fb1
DK
972 // We can only handle script section types ST_NONE and ST_NOLOAD.
973 switch (script_section_type)
974 {
975 case Script_sections::ST_NONE:
976 break;
977 case Script_sections::ST_NOLOAD:
978 flags &= elfcpp::SHF_ALLOC;
979 break;
980 default:
981 gold_unreachable();
982 }
983
a445fddf
ILT
984 // If this is an orphan section--one not mentioned in the linker
985 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
986 // default processing below.
987
988 if (output_section_slot != NULL)
989 {
990 if (*output_section_slot != NULL)
9c547ec3
ILT
991 {
992 (*output_section_slot)->update_flags_for_input_section(flags);
993 return *output_section_slot;
994 }
a445fddf
ILT
995
996 // We don't put sections found in the linker script into
997 // SECTION_NAME_MAP_. That keeps us from getting confused
998 // if an orphan section is mapped to a section with the same
999 // name as one in the linker script.
1000
1001 name = this->namepool_.add(name, false, NULL);
1002
22f0da72
ILT
1003 Output_section* os = this->make_output_section(name, type, flags,
1004 order, is_relro);
1005
a445fddf 1006 os->set_found_in_sections_clause();
1e5d2fb1
DK
1007
1008 // Special handling for NOLOAD sections.
1009 if (script_section_type == Script_sections::ST_NOLOAD)
1010 {
1011 os->set_is_noload();
1012
1013 // The constructor of Output_section sets addresses of non-ALLOC
1014 // sections to 0 by default. We don't want that for NOLOAD
1015 // sections even if they have no SHF_ALLOC flag.
1016 if ((os->flags() & elfcpp::SHF_ALLOC) == 0
1017 && os->is_address_valid())
1018 {
1019 gold_assert(os->address() == 0
1020 && !os->is_offset_valid()
1021 && !os->is_data_size_valid());
1022 os->reset_address_and_file_offset();
1023 }
1024 }
1025
a445fddf
ILT
1026 *output_section_slot = os;
1027 return os;
1028 }
1029 }
1030
1031 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
1032
6fc6ea19 1033 size_t len = strlen(name);
dd68f8fa 1034 std::string uncompressed_name;
6fc6ea19
CC
1035
1036 // Compressed debug sections should be mapped to the corresponding
1037 // uncompressed section.
1038 if (is_compressed_debug_section(name))
1039 {
dd68f8fa
CC
1040 uncompressed_name =
1041 corresponding_uncompressed_section_name(std::string(name, len));
1042 name = uncompressed_name.c_str();
1043 len = uncompressed_name.length();
6fc6ea19
CC
1044 }
1045
a445fddf
ILT
1046 // Turn NAME from the name of the input section into the name of the
1047 // output section.
401a9a73
CC
1048 if (is_input_section
1049 && !this->script_options_->saw_sections_clause()
1050 && !parameters->options().relocatable())
921b5322
AM
1051 {
1052 const char *orig_name = name;
1053 name = parameters->target().output_section_name(relobj, name, &len);
1054 if (name == NULL)
1055 name = Layout::output_section_name(relobj, orig_name, &len);
1056 }
a445fddf
ILT
1057
1058 Stringpool::Key name_key;
1059 name = this->namepool_.add_with_length(name, len, true, &name_key);
1060
1061 // Find or make the output section. The output section is selected
1062 // based on the section name, type, and flags.
22f0da72 1063 return this->get_output_section(name, name_key, type, flags, order, is_relro);
a445fddf
ILT
1064}
1065
cdc29364
CC
1066// For incremental links, record the initial fixed layout of a section
1067// from the base file, and return a pointer to the Output_section.
1068
1069template<int size, bool big_endian>
1070Output_section*
1071Layout::init_fixed_output_section(const char* name,
1072 elfcpp::Shdr<size, big_endian>& shdr)
1073{
1074 unsigned int sh_type = shdr.get_sh_type();
1075
aa06ae28
CC
1076 // We preserve the layout of PROGBITS, NOBITS, INIT_ARRAY, FINI_ARRAY,
1077 // PRE_INIT_ARRAY, and NOTE sections.
cdc29364 1078 // All others will be created from scratch and reallocated.
aa06ae28 1079 if (!can_incremental_update(sh_type))
cdc29364
CC
1080 return NULL;
1081
c1027032
CC
1082 // If we're generating a .gdb_index section, we need to regenerate
1083 // it from scratch.
1084 if (parameters->options().gdb_index()
1085 && sh_type == elfcpp::SHT_PROGBITS
1086 && strcmp(name, ".gdb_index") == 0)
1087 return NULL;
1088
cdc29364
CC
1089 typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
1090 typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
1091 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
1092 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1093 typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
1094 shdr.get_sh_addralign();
1095
1096 // Make the output section.
1097 Stringpool::Key name_key;
1098 name = this->namepool_.add(name, true, &name_key);
1099 Output_section* os = this->get_output_section(name, name_key, sh_type,
2e702c99 1100 sh_flags, ORDER_INVALID, false);
cdc29364
CC
1101 os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
1102 if (sh_type != elfcpp::SHT_NOBITS)
1103 this->free_list_.remove(sh_offset, sh_offset + sh_size);
1104 return os;
1105}
1106
edcac0c1
ILT
1107// Return the index by which an input section should be ordered. This
1108// is used to sort some .text sections, for compatibility with GNU ld.
1109
1110int
1111Layout::special_ordering_of_input_section(const char* name)
1112{
1113 // The GNU linker has some special handling for some sections that
1114 // wind up in the .text section. Sections that start with these
1115 // prefixes must appear first, and must appear in the order listed
1116 // here.
bbc5ae17 1117 static const char* const text_section_sort[] =
edcac0c1
ILT
1118 {
1119 ".text.unlikely",
1120 ".text.exit",
1121 ".text.startup",
1122 ".text.hot"
1123 };
1124
1125 for (size_t i = 0;
1126 i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
1127 i++)
1128 if (is_prefix_of(text_section_sort[i], name))
1129 return i;
1130
1131 return -1;
1132}
1133
ead1e424 1134// Return the output section to use for input section SHNDX, with name
730cdc88
ILT
1135// NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
1136// index of a relocation section which applies to this section, or 0
1137// if none, or -1U if more than one. RELOC_TYPE is the type of the
1138// relocation section if there is one. Set *OFF to the offset of this
1139// input section without the output section. Return NULL if the
1140// section should be discarded. Set *OFF to -1 if the section
1141// contents should not be written directly to the output file, but
1142// will instead receive special handling.
ead1e424
ILT
1143
1144template<int size, bool big_endian>
1145Output_section*
6fa2a40b 1146Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
730cdc88
ILT
1147 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
1148 unsigned int reloc_shndx, unsigned int, off_t* off)
ead1e424 1149{
ef9beddf
ILT
1150 *off = 0;
1151
ead1e424
ILT
1152 if (!this->include_section(object, name, shdr))
1153 return NULL;
1154
2a0ff005 1155 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
2a0ff005 1156
6a74a719
ILT
1157 // In a relocatable link a grouped section must not be combined with
1158 // any other sections.
5393d741 1159 Output_section* os;
8851ecca 1160 if (parameters->options().relocatable()
6a74a719
ILT
1161 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
1162 {
f54f5e31
L
1163 // Some flags in the input section should not be automatically
1164 // copied to the output section.
1165 elfcpp::Elf_Xword flags = (shdr.get_sh_flags()
1166 & ~ elfcpp::SHF_COMPRESSED);
6a74a719 1167 name = this->namepool_.add(name, true, NULL);
f54f5e31 1168 os = this->make_output_section(name, sh_type, flags,
22f0da72 1169 ORDER_INVALID, false);
6a74a719
ILT
1170 }
1171 else
1172 {
16164a6b
ST
1173 // Plugins can choose to place one or more subsets of sections in
1174 // unique segments and this is done by mapping these section subsets
1175 // to unique output sections. Check if this section needs to be
1176 // remapped to a unique output section.
1177 Section_segment_map::iterator it
1178 = this->section_segment_map_.find(Const_section_id(object, shndx));
1179 if (it == this->section_segment_map_.end())
1180 {
bbc5ae17 1181 os = this->choose_output_section(object, name, sh_type,
16164a6b
ST
1182 shdr.get_sh_flags(), true,
1183 ORDER_INVALID, false);
1184 }
1185 else
1186 {
1187 // We know the name of the output section, directly call
1188 // get_output_section here by-passing choose_output_section.
1189 elfcpp::Elf_Xword flags
1190 = this->get_output_section_flags(shdr.get_sh_flags());
1191
1192 const char* os_name = it->second->name;
1193 Stringpool::Key name_key;
1194 os_name = this->namepool_.add(os_name, true, &name_key);
1195 os = this->get_output_section(os_name, name_key, sh_type, flags,
1196 ORDER_INVALID, false);
1197 if (!os->is_unique_segment())
1198 {
1199 os->set_is_unique_segment();
1200 os->set_extra_segment_flags(it->second->flags);
1201 os->set_segment_alignment(it->second->align);
1202 }
1203 }
6a74a719
ILT
1204 if (os == NULL)
1205 return NULL;
1206 }
a2fb1b05 1207
2fd32231 1208 // By default the GNU linker sorts input sections whose names match
487b39df
ILT
1209 // .ctors.*, .dtors.*, .init_array.*, or .fini_array.*. The
1210 // sections are sorted by name. This is used to implement
1211 // constructor priority ordering. We are compatible. When we put
1212 // .ctor sections in .init_array and .dtor sections in .fini_array,
1213 // we must also sort plain .ctor and .dtor sections.
2fd32231 1214 if (!this->script_options_->saw_sections_clause()
5393d741 1215 && !parameters->options().relocatable()
2fd32231
ILT
1216 && (is_prefix_of(".ctors.", name)
1217 || is_prefix_of(".dtors.", name)
1218 || is_prefix_of(".init_array.", name)
5393d741
ILT
1219 || is_prefix_of(".fini_array.", name)
1220 || (parameters->options().ctors_in_init_array()
1221 && (strcmp(name, ".ctors") == 0
1222 || strcmp(name, ".dtors") == 0))))
2fd32231
ILT
1223 os->set_must_sort_attached_input_sections();
1224
edcac0c1
ILT
1225 // By default the GNU linker sorts some special text sections ahead
1226 // of others. We are compatible.
c6ac678d
ST
1227 if (parameters->options().text_reorder()
1228 && !this->script_options_->saw_sections_clause()
7c381248 1229 && !this->is_section_ordering_specified()
edcac0c1
ILT
1230 && !parameters->options().relocatable()
1231 && Layout::special_ordering_of_input_section(name) >= 0)
1232 os->set_must_sort_attached_input_sections();
1233
487b39df
ILT
1234 // If this is a .ctors or .ctors.* section being mapped to a
1235 // .init_array section, or a .dtors or .dtors.* section being mapped
1236 // to a .fini_array section, we will need to reverse the words if
1237 // there is more than one. Record this section for later. See
1238 // ctors_sections_in_init_array above.
1239 if (!this->script_options_->saw_sections_clause()
1240 && !parameters->options().relocatable()
1241 && shdr.get_sh_size() > size / 8
1242 && (((strcmp(name, ".ctors") == 0
1243 || is_prefix_of(".ctors.", name))
1244 && strcmp(os->name(), ".init_array") == 0)
1245 || ((strcmp(name, ".dtors") == 0
1246 || is_prefix_of(".dtors.", name))
1247 && strcmp(os->name(), ".fini_array") == 0)))
1248 ctors_sections_in_init_array.insert(Section_id(object, shndx));
1249
a2fb1b05
ILT
1250 // FIXME: Handle SHF_LINK_ORDER somewhere.
1251
5b7b7d6e
ILT
1252 elfcpp::Elf_Xword orig_flags = os->flags();
1253
6e9ba2ca 1254 *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
a445fddf 1255 this->script_options_->saw_sections_clause());
5b7b7d6e
ILT
1256
1257 // If the flags changed, we may have to change the order.
1258 if ((orig_flags & elfcpp::SHF_ALLOC) != 0)
1259 {
1260 orig_flags &= (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
1261 elfcpp::Elf_Xword new_flags =
1262 os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
1263 if (orig_flags != new_flags)
1264 os->set_order(this->default_section_order(os, false));
1265 }
1266
d7bb5745 1267 this->have_added_input_section_ = true;
a2fb1b05
ILT
1268
1269 return os;
1270}
1271
16164a6b
ST
1272// Maps section SECN to SEGMENT s.
1273void
1274Layout::insert_section_segment_map(Const_section_id secn,
1275 Unique_segment_info *s)
1276{
bbc5ae17 1277 gold_assert(this->unique_segment_for_sections_specified_);
16164a6b
ST
1278 this->section_segment_map_[secn] = s;
1279}
1280
6a74a719
ILT
1281// Handle a relocation section when doing a relocatable link.
1282
1283template<int size, bool big_endian>
1284Output_section*
6fa2a40b 1285Layout::layout_reloc(Sized_relobj_file<size, big_endian>* object,
6a74a719
ILT
1286 unsigned int,
1287 const elfcpp::Shdr<size, big_endian>& shdr,
1288 Output_section* data_section,
1289 Relocatable_relocs* rr)
1290{
8851ecca
ILT
1291 gold_assert(parameters->options().relocatable()
1292 || parameters->options().emit_relocs());
6a74a719
ILT
1293
1294 int sh_type = shdr.get_sh_type();
1295
1296 std::string name;
1297 if (sh_type == elfcpp::SHT_REL)
1298 name = ".rel";
1299 else if (sh_type == elfcpp::SHT_RELA)
1300 name = ".rela";
1301 else
1302 gold_unreachable();
1303 name += data_section->name();
1304
bd288ea2
ILT
1305 // In a relocatable link relocs for a grouped section must not be
1306 // combined with other reloc sections.
1307 Output_section* os;
1308 if (!parameters->options().relocatable()
1309 || (data_section->flags() & elfcpp::SHF_GROUP) == 0)
1310 os = this->choose_output_section(object, name.c_str(), sh_type,
22f0da72
ILT
1311 shdr.get_sh_flags(), false,
1312 ORDER_INVALID, false);
bd288ea2
ILT
1313 else
1314 {
1315 const char* n = this->namepool_.add(name.c_str(), true, NULL);
1316 os = this->make_output_section(n, sh_type, shdr.get_sh_flags(),
22f0da72 1317 ORDER_INVALID, false);
bd288ea2 1318 }
6a74a719
ILT
1319
1320 os->set_should_link_to_symtab();
1321 os->set_info_section(data_section);
1322
1323 Output_section_data* posd;
1324 if (sh_type == elfcpp::SHT_REL)
1325 {
1326 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1327 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
1328 size,
1329 big_endian>(rr);
1330 }
1331 else if (sh_type == elfcpp::SHT_RELA)
1332 {
1333 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1334 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
1335 size,
1336 big_endian>(rr);
1337 }
1338 else
1339 gold_unreachable();
1340
1341 os->add_output_section_data(posd);
1342 rr->set_output_data(posd);
1343
1344 return os;
1345}
1346
1347// Handle a group section when doing a relocatable link.
1348
1349template<int size, bool big_endian>
1350void
1351Layout::layout_group(Symbol_table* symtab,
6fa2a40b 1352 Sized_relobj_file<size, big_endian>* object,
6a74a719
ILT
1353 unsigned int,
1354 const char* group_section_name,
1355 const char* signature,
1356 const elfcpp::Shdr<size, big_endian>& shdr,
8825ac63
ILT
1357 elfcpp::Elf_Word flags,
1358 std::vector<unsigned int>* shndxes)
6a74a719 1359{
8851ecca 1360 gold_assert(parameters->options().relocatable());
6a74a719
ILT
1361 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
1362 group_section_name = this->namepool_.add(group_section_name, true, NULL);
1363 Output_section* os = this->make_output_section(group_section_name,
1364 elfcpp::SHT_GROUP,
f5c870d2 1365 shdr.get_sh_flags(),
22f0da72 1366 ORDER_INVALID, false);
6a74a719
ILT
1367
1368 // We need to find a symbol with the signature in the symbol table.
755ab8af 1369 // If we don't find one now, we need to look again later.
6a74a719 1370 Symbol* sym = symtab->lookup(signature, NULL);
755ab8af
ILT
1371 if (sym != NULL)
1372 os->set_info_symndx(sym);
1373 else
1374 {
e55bde5e
ILT
1375 // Reserve some space to minimize reallocations.
1376 if (this->group_signatures_.empty())
1377 this->group_signatures_.reserve(this->number_of_input_files_ * 16);
1378
755ab8af
ILT
1379 // We will wind up using a symbol whose name is the signature.
1380 // So just put the signature in the symbol name pool to save it.
1381 signature = symtab->canonicalize_name(signature);
1382 this->group_signatures_.push_back(Group_signature(os, signature));
1383 }
6a74a719
ILT
1384
1385 os->set_should_link_to_symtab();
6a74a719
ILT
1386 os->set_entsize(4);
1387
1388 section_size_type entry_count =
1389 convert_to_section_size_type(shdr.get_sh_size() / 4);
1390 Output_section_data* posd =
8825ac63
ILT
1391 new Output_data_group<size, big_endian>(object, entry_count, flags,
1392 shndxes);
6a74a719
ILT
1393 os->add_output_section_data(posd);
1394}
1395
730cdc88
ILT
1396// Special GNU handling of sections name .eh_frame. They will
1397// normally hold exception frame data as defined by the C++ ABI
1398// (http://codesourcery.com/cxx-abi/).
3151305a
ILT
1399
1400template<int size, bool big_endian>
730cdc88 1401Output_section*
6fa2a40b 1402Layout::layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
730cdc88
ILT
1403 const unsigned char* symbols,
1404 off_t symbols_size,
1405 const unsigned char* symbol_names,
1406 off_t symbol_names_size,
3151305a 1407 unsigned int shndx,
3151305a 1408 const elfcpp::Shdr<size, big_endian>& shdr,
730cdc88
ILT
1409 unsigned int reloc_shndx, unsigned int reloc_type,
1410 off_t* off)
3151305a 1411{
4d5e4e62
ILT
1412 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1413 || shdr.get_sh_type() == elfcpp::SHT_X86_64_UNWIND);
1650c4ff 1414 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
730cdc88 1415
07a60597 1416 Output_section* os = this->make_eh_frame_section(object);
a445fddf
ILT
1417 if (os == NULL)
1418 return NULL;
730cdc88 1419
3151305a
ILT
1420 gold_assert(this->eh_frame_section_ == os);
1421
911a5072
ILT
1422 elfcpp::Elf_Xword orig_flags = os->flags();
1423
e1663197
CC
1424 Eh_frame::Eh_frame_section_disposition disp =
1425 Eh_frame::EH_UNRECOGNIZED_SECTION;
1426 if (!parameters->incremental())
1427 {
1428 disp = this->eh_frame_data_->add_ehframe_input_section(object,
1429 symbols,
1430 symbols_size,
1431 symbol_names,
1432 symbol_names_size,
1433 shndx,
1434 reloc_shndx,
1435 reloc_type);
1436 }
1437
1438 if (disp == Eh_frame::EH_OPTIMIZABLE_SECTION)
2c38906f 1439 {
154e0e9a
ILT
1440 os->update_flags_for_input_section(shdr.get_sh_flags());
1441
3bb951e5 1442 // A writable .eh_frame section is a RELRO section.
911a5072
ILT
1443 if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
1444 != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
1445 {
1446 os->set_is_relro();
1447 os->set_order(ORDER_RELRO);
1448 }
3bb951e5 1449
2c38906f 1450 *off = -1;
e1663197 1451 return os;
2c38906f 1452 }
e1663197
CC
1453
1454 if (disp == Eh_frame::EH_END_MARKER_SECTION && !this->added_eh_frame_data_)
730cdc88 1455 {
e1663197
CC
1456 // We found the end marker section, so now we can add the set of
1457 // optimized sections to the output section. We need to postpone
1458 // adding this until we've found a section we can optimize so that
1459 // the .eh_frame section in crtbeginT.o winds up at the start of
1460 // the output section.
1461 os->add_output_section_data(this->eh_frame_data_);
1462 this->added_eh_frame_data_ = true;
1463 }
1464
1465 // We couldn't handle this .eh_frame section for some reason.
1466 // Add it as a normal section.
1467 bool saw_sections_clause = this->script_options_->saw_sections_clause();
1468 *off = os->add_input_section(this, object, shndx, ".eh_frame", shdr,
1469 reloc_shndx, saw_sections_clause);
1470 this->have_added_input_section_ = true;
911a5072 1471
e1663197
CC
1472 if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
1473 != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
1474 os->set_order(this->default_section_order(os, false));
730cdc88
ILT
1475
1476 return os;
3151305a
ILT
1477}
1478
e1663197
CC
1479void
1480Layout::finalize_eh_frame_section()
1481{
1482 // If we never found an end marker section, we need to add the
1483 // optimized eh sections to the output section now.
1484 if (!parameters->incremental()
1485 && this->eh_frame_section_ != NULL
1486 && !this->added_eh_frame_data_)
1487 {
1488 this->eh_frame_section_->add_output_section_data(this->eh_frame_data_);
1489 this->added_eh_frame_data_ = true;
1490 }
1491}
1492
07a60597
ILT
1493// Create and return the magic .eh_frame section. Create
1494// .eh_frame_hdr also if appropriate. OBJECT is the object with the
1495// input .eh_frame section; it may be NULL.
1496
1497Output_section*
1498Layout::make_eh_frame_section(const Relobj* object)
1499{
1500 // FIXME: On x86_64, this could use SHT_X86_64_UNWIND rather than
1501 // SHT_PROGBITS.
1502 Output_section* os = this->choose_output_section(object, ".eh_frame",
1503 elfcpp::SHT_PROGBITS,
1504 elfcpp::SHF_ALLOC, false,
1505 ORDER_EHFRAME, false);
1506 if (os == NULL)
1507 return NULL;
1508
1509 if (this->eh_frame_section_ == NULL)
1510 {
1511 this->eh_frame_section_ = os;
1512 this->eh_frame_data_ = new Eh_frame();
1513
1514 // For incremental linking, we do not optimize .eh_frame sections
1515 // or create a .eh_frame_hdr section.
1516 if (parameters->options().eh_frame_hdr() && !parameters->incremental())
1517 {
1518 Output_section* hdr_os =
1519 this->choose_output_section(NULL, ".eh_frame_hdr",
1520 elfcpp::SHT_PROGBITS,
1521 elfcpp::SHF_ALLOC, false,
1522 ORDER_EHFRAME, false);
1523
1524 if (hdr_os != NULL)
1525 {
1526 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
1527 this->eh_frame_data_);
1528 hdr_os->add_output_section_data(hdr_posd);
1529
1530 hdr_os->set_after_input_sections();
1531
1532 if (!this->script_options_->saw_phdrs_clause())
1533 {
1534 Output_segment* hdr_oseg;
1535 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
1536 elfcpp::PF_R);
1537 hdr_oseg->add_output_section_to_nonload(hdr_os,
1538 elfcpp::PF_R);
1539 }
1540
1541 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
1542 }
1543 }
1544 }
1545
1546 return os;
1547}
1548
1549// Add an exception frame for a PLT. This is called from target code.
1550
1551void
1552Layout::add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
1553 size_t cie_length, const unsigned char* fde_data,
1554 size_t fde_length)
1555{
1556 if (parameters->incremental())
1557 {
1558 // FIXME: Maybe this could work some day....
1559 return;
1560 }
1561 Output_section* os = this->make_eh_frame_section(NULL);
1562 if (os == NULL)
1563 return;
1564 this->eh_frame_data_->add_ehframe_for_plt(plt, cie_data, cie_length,
1565 fde_data, fde_length);
1566 if (!this->added_eh_frame_data_)
1567 {
1568 os->add_output_section_data(this->eh_frame_data_);
1569 this->added_eh_frame_data_ = true;
1570 }
1571}
1572
c1027032
CC
1573// Scan a .debug_info or .debug_types section, and add summary
1574// information to the .gdb_index section.
1575
1576template<int size, bool big_endian>
1577void
1578Layout::add_to_gdb_index(bool is_type_unit,
1579 Sized_relobj<size, big_endian>* object,
1580 const unsigned char* symbols,
1581 off_t symbols_size,
1582 unsigned int shndx,
1583 unsigned int reloc_shndx,
1584 unsigned int reloc_type)
1585{
1586 if (this->gdb_index_data_ == NULL)
1587 {
1588 Output_section* os = this->choose_output_section(NULL, ".gdb_index",
1589 elfcpp::SHT_PROGBITS, 0,
1590 false, ORDER_INVALID,
1591 false);
1592 if (os == NULL)
2e702c99 1593 return;
c1027032
CC
1594
1595 this->gdb_index_data_ = new Gdb_index(os);
1596 os->add_output_section_data(this->gdb_index_data_);
1597 os->set_after_input_sections();
1598 }
1599
1600 this->gdb_index_data_->scan_debug_info(is_type_unit, object, symbols,
1601 symbols_size, shndx, reloc_shndx,
1602 reloc_type);
1603}
1604
9f1d377b
ILT
1605// Add POSD to an output section using NAME, TYPE, and FLAGS. Return
1606// the output section.
ead1e424 1607
9f1d377b 1608Output_section*
ead1e424
ILT
1609Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
1610 elfcpp::Elf_Xword flags,
f5c870d2 1611 Output_section_data* posd,
22f0da72 1612 Output_section_order order, bool is_relro)
ead1e424 1613{
a445fddf 1614 Output_section* os = this->choose_output_section(NULL, name, type, flags,
22f0da72 1615 false, order, is_relro);
a445fddf
ILT
1616 if (os != NULL)
1617 os->add_output_section_data(posd);
9f1d377b 1618 return os;
ead1e424
ILT
1619}
1620
a2fb1b05
ILT
1621// Map section flags to segment flags.
1622
1623elfcpp::Elf_Word
1624Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
1625{
1626 elfcpp::Elf_Word ret = elfcpp::PF_R;
1627 if ((flags & elfcpp::SHF_WRITE) != 0)
1628 ret |= elfcpp::PF_W;
1629 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
1630 ret |= elfcpp::PF_X;
1631 return ret;
1632}
1633
1634// Make a new Output_section, and attach it to segments as
22f0da72
ILT
1635// appropriate. ORDER is the order in which this section should
1636// appear in the output segment. IS_RELRO is true if this is a relro
1637// (read-only after relocations) section.
a2fb1b05
ILT
1638
1639Output_section*
1640Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
22f0da72
ILT
1641 elfcpp::Elf_Xword flags,
1642 Output_section_order order, bool is_relro)
a2fb1b05 1643{
96803768
ILT
1644 Output_section* os;
1645 if ((flags & elfcpp::SHF_ALLOC) == 0
e55bde5e 1646 && strcmp(parameters->options().compress_debug_sections(), "none") != 0
96803768 1647 && is_compressible_debug_section(name))
e55bde5e
ILT
1648 os = new Output_compressed_section(&parameters->options(), name, type,
1649 flags);
62b01cb5 1650 else if ((flags & elfcpp::SHF_ALLOC) == 0
2e702c99
RM
1651 && parameters->options().strip_debug_non_line()
1652 && strcmp(".debug_abbrev", name) == 0)
62b01cb5
ILT
1653 {
1654 os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
2e702c99 1655 name, type, flags);
62b01cb5 1656 if (this->debug_info_)
2e702c99 1657 this->debug_info_->set_abbreviations(this->debug_abbrev_);
62b01cb5
ILT
1658 }
1659 else if ((flags & elfcpp::SHF_ALLOC) == 0
2e702c99
RM
1660 && parameters->options().strip_debug_non_line()
1661 && strcmp(".debug_info", name) == 0)
62b01cb5
ILT
1662 {
1663 os = this->debug_info_ = new Output_reduced_debug_info_section(
2e702c99 1664 name, type, flags);
62b01cb5 1665 if (this->debug_abbrev_)
2e702c99 1666 this->debug_info_->set_abbreviations(this->debug_abbrev_);
62b01cb5 1667 }
09ec0418 1668 else
c0a62865 1669 {
5393d741
ILT
1670 // Sometimes .init_array*, .preinit_array* and .fini_array* do
1671 // not have correct section types. Force them here.
1672 if (type == elfcpp::SHT_PROGBITS)
1673 {
1674 if (is_prefix_of(".init_array", name))
1675 type = elfcpp::SHT_INIT_ARRAY;
1676 else if (is_prefix_of(".preinit_array", name))
1677 type = elfcpp::SHT_PREINIT_ARRAY;
1678 else if (is_prefix_of(".fini_array", name))
1679 type = elfcpp::SHT_FINI_ARRAY;
1680 }
1681
c0a62865
DK
1682 // FIXME: const_cast is ugly.
1683 Target* target = const_cast<Target*>(&parameters->target());
1684 os = target->make_output_section(name, type, flags);
1685 }
96803768 1686
22f0da72
ILT
1687 // With -z relro, we have to recognize the special sections by name.
1688 // There is no other way.
1689 bool is_relro_local = false;
1690 if (!this->script_options_->saw_sections_clause()
1691 && parameters->options().relro()
22f0da72
ILT
1692 && (flags & elfcpp::SHF_ALLOC) != 0
1693 && (flags & elfcpp::SHF_WRITE) != 0)
1694 {
14dc9ef7 1695 if (type == elfcpp::SHT_PROGBITS)
22f0da72 1696 {
1007b503
CC
1697 if ((flags & elfcpp::SHF_TLS) != 0)
1698 is_relro = true;
1699 else if (strcmp(name, ".data.rel.ro") == 0)
14dc9ef7
ILT
1700 is_relro = true;
1701 else if (strcmp(name, ".data.rel.ro.local") == 0)
1702 {
1703 is_relro = true;
1704 is_relro_local = true;
1705 }
1706 else if (strcmp(name, ".ctors") == 0
1707 || strcmp(name, ".dtors") == 0
1708 || strcmp(name, ".jcr") == 0)
1709 is_relro = true;
22f0da72
ILT
1710 }
1711 else if (type == elfcpp::SHT_INIT_ARRAY
1712 || type == elfcpp::SHT_FINI_ARRAY
1713 || type == elfcpp::SHT_PREINIT_ARRAY)
1714 is_relro = true;
22f0da72
ILT
1715 }
1716
1a2dff53
ILT
1717 if (is_relro)
1718 os->set_is_relro();
22f0da72
ILT
1719
1720 if (order == ORDER_INVALID && (flags & elfcpp::SHF_ALLOC) != 0)
1721 order = this->default_section_order(os, is_relro_local);
1722
1723 os->set_order(order);
f5c870d2 1724
8a5e3e08
ILT
1725 parameters->target().new_output_section(os);
1726
a3ad94ed 1727 this->section_list_.push_back(os);
a2fb1b05 1728
2fd32231
ILT
1729 // The GNU linker by default sorts some sections by priority, so we
1730 // do the same. We need to know that this might happen before we
1731 // attach any input sections.
1732 if (!this->script_options_->saw_sections_clause()
5393d741
ILT
1733 && !parameters->options().relocatable()
1734 && (strcmp(name, ".init_array") == 0
1735 || strcmp(name, ".fini_array") == 0
1736 || (!parameters->options().ctors_in_init_array()
1737 && (strcmp(name, ".ctors") == 0
1738 || strcmp(name, ".dtors") == 0))))
2fd32231
ILT
1739 os->set_may_sort_attached_input_sections();
1740
edcac0c1
ILT
1741 // The GNU linker by default sorts .text.{unlikely,exit,startup,hot}
1742 // sections before other .text sections. We are compatible. We
1743 // need to know that this might happen before we attach any input
1744 // sections.
c6ac678d
ST
1745 if (parameters->options().text_reorder()
1746 && !this->script_options_->saw_sections_clause()
7c381248 1747 && !this->is_section_ordering_specified()
edcac0c1
ILT
1748 && !parameters->options().relocatable()
1749 && strcmp(name, ".text") == 0)
1750 os->set_may_sort_attached_input_sections();
1751
6934001a
CC
1752 // GNU linker sorts section by name with --sort-section=name.
1753 if (strcmp(parameters->options().sort_section(), "name") == 0)
1754 os->set_must_sort_attached_input_sections();
1755
1518dc8f
ILT
1756 // Check for .stab*str sections, as .stab* sections need to link to
1757 // them.
1758 if (type == elfcpp::SHT_STRTAB
1759 && !this->have_stabstr_section_
1760 && strncmp(name, ".stab", 5) == 0
1761 && strcmp(name + strlen(name) - 3, "str") == 0)
1762 this->have_stabstr_section_ = true;
1763
9fbd3822
CC
1764 // During a full incremental link, we add patch space to most
1765 // PROGBITS and NOBITS sections. Flag those that may be
1766 // arbitrarily padded.
1767 if ((type == elfcpp::SHT_PROGBITS || type == elfcpp::SHT_NOBITS)
1768 && order != ORDER_INTERP
1769 && order != ORDER_INIT
1770 && order != ORDER_PLT
1771 && order != ORDER_FINI
1772 && order != ORDER_RELRO_LAST
1773 && order != ORDER_NON_RELRO_FIRST
aa06ae28 1774 && strcmp(name, ".eh_frame") != 0
9fbd3822
CC
1775 && strcmp(name, ".ctors") != 0
1776 && strcmp(name, ".dtors") != 0
1777 && strcmp(name, ".jcr") != 0)
8ea8cd50
CC
1778 {
1779 os->set_is_patch_space_allowed();
1780
1781 // Certain sections require "holes" to be filled with
1782 // specific fill patterns. These fill patterns may have
1783 // a minimum size, so we must prevent allocations from the
1784 // free list that leave a hole smaller than the minimum.
1785 if (strcmp(name, ".debug_info") == 0)
2e702c99 1786 os->set_free_space_fill(new Output_fill_debug_info(false));
8ea8cd50 1787 else if (strcmp(name, ".debug_types") == 0)
2e702c99 1788 os->set_free_space_fill(new Output_fill_debug_info(true));
8ea8cd50 1789 else if (strcmp(name, ".debug_line") == 0)
2e702c99 1790 os->set_free_space_fill(new Output_fill_debug_line());
8ea8cd50 1791 }
9fbd3822 1792
154e0e9a
ILT
1793 // If we have already attached the sections to segments, then we
1794 // need to attach this one now. This happens for sections created
1795 // directly by the linker.
1796 if (this->sections_are_attached_)
2e702c99 1797 this->attach_section_to_segment(&parameters->target(), os);
154e0e9a 1798
4e2b1697
ILT
1799 return os;
1800}
a445fddf 1801
22f0da72
ILT
1802// Return the default order in which a section should be placed in an
1803// output segment. This function captures a lot of the ideas in
1804// ld/scripttempl/elf.sc in the GNU linker. Note that the order of a
1805// linker created section is normally set when the section is created;
1806// this function is used for input sections.
1807
1808Output_section_order
1809Layout::default_section_order(Output_section* os, bool is_relro_local)
1810{
1811 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1812 bool is_write = (os->flags() & elfcpp::SHF_WRITE) != 0;
1813 bool is_execinstr = (os->flags() & elfcpp::SHF_EXECINSTR) != 0;
1814 bool is_bss = false;
1815
1816 switch (os->type())
1817 {
1818 default:
1819 case elfcpp::SHT_PROGBITS:
1820 break;
1821 case elfcpp::SHT_NOBITS:
1822 is_bss = true;
1823 break;
1824 case elfcpp::SHT_RELA:
1825 case elfcpp::SHT_REL:
1826 if (!is_write)
1827 return ORDER_DYNAMIC_RELOCS;
1828 break;
1829 case elfcpp::SHT_HASH:
1830 case elfcpp::SHT_DYNAMIC:
1831 case elfcpp::SHT_SHLIB:
1832 case elfcpp::SHT_DYNSYM:
1833 case elfcpp::SHT_GNU_HASH:
1834 case elfcpp::SHT_GNU_verdef:
1835 case elfcpp::SHT_GNU_verneed:
1836 case elfcpp::SHT_GNU_versym:
1837 if (!is_write)
1838 return ORDER_DYNAMIC_LINKER;
1839 break;
1840 case elfcpp::SHT_NOTE:
1841 return is_write ? ORDER_RW_NOTE : ORDER_RO_NOTE;
1842 }
1843
1844 if ((os->flags() & elfcpp::SHF_TLS) != 0)
1845 return is_bss ? ORDER_TLS_BSS : ORDER_TLS_DATA;
1846
1847 if (!is_bss && !is_write)
1848 {
1849 if (is_execinstr)
1850 {
1851 if (strcmp(os->name(), ".init") == 0)
1852 return ORDER_INIT;
1853 else if (strcmp(os->name(), ".fini") == 0)
1854 return ORDER_FINI;
1855 }
1856 return is_execinstr ? ORDER_TEXT : ORDER_READONLY;
1857 }
1858
1859 if (os->is_relro())
1860 return is_relro_local ? ORDER_RELRO_LOCAL : ORDER_RELRO;
1861
1862 if (os->is_small_section())
1863 return is_bss ? ORDER_SMALL_BSS : ORDER_SMALL_DATA;
1864 if (os->is_large_section())
1865 return is_bss ? ORDER_LARGE_BSS : ORDER_LARGE_DATA;
1866
1867 return is_bss ? ORDER_BSS : ORDER_DATA;
1868}
1869
154e0e9a
ILT
1870// Attach output sections to segments. This is called after we have
1871// seen all the input sections.
1872
1873void
2e702c99 1874Layout::attach_sections_to_segments(const Target* target)
154e0e9a
ILT
1875{
1876 for (Section_list::iterator p = this->section_list_.begin();
1877 p != this->section_list_.end();
1878 ++p)
2e702c99 1879 this->attach_section_to_segment(target, *p);
154e0e9a
ILT
1880
1881 this->sections_are_attached_ = true;
1882}
1883
1884// Attach an output section to a segment.
1885
1886void
2e702c99 1887Layout::attach_section_to_segment(const Target* target, Output_section* os)
154e0e9a
ILT
1888{
1889 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1890 this->unattached_section_list_.push_back(os);
1891 else
2e702c99 1892 this->attach_allocated_section_to_segment(target, os);
154e0e9a
ILT
1893}
1894
4e2b1697 1895// Attach an allocated output section to a segment.
1c4f3631 1896
4e2b1697 1897void
2e702c99
RM
1898Layout::attach_allocated_section_to_segment(const Target* target,
1899 Output_section* os)
4e2b1697 1900{
154e0e9a 1901 elfcpp::Elf_Xword flags = os->flags();
4e2b1697 1902 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
a2fb1b05 1903
4e2b1697
ILT
1904 if (parameters->options().relocatable())
1905 return;
a2fb1b05 1906
4e2b1697
ILT
1907 // If we have a SECTIONS clause, we can't handle the attachment to
1908 // segments until after we've seen all the sections.
1909 if (this->script_options_->saw_sections_clause())
1910 return;
a2fb1b05 1911
4e2b1697 1912 gold_assert(!this->script_options_->saw_phdrs_clause());
756ac4a8 1913
4e2b1697 1914 // This output section goes into a PT_LOAD segment.
a2fb1b05 1915
4e2b1697 1916 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
a2fb1b05 1917
16164a6b
ST
1918 // If this output section's segment has extra flags that need to be set,
1919 // coming from a linker plugin, do that.
1920 seg_flags |= os->extra_segment_flags();
1921
a192ba05
ILT
1922 // Check for --section-start.
1923 uint64_t addr;
1924 bool is_address_set = parameters->options().section_start(os->name(), &addr);
f5c870d2 1925
4e2b1697 1926 // In general the only thing we really care about for PT_LOAD
0f72bf6f
RÁE
1927 // segments is whether or not they are writable or executable,
1928 // so that is how we search for them.
1929 // Large data sections also go into their own PT_LOAD segment.
1930 // People who need segments sorted on some other basis will
1931 // have to use a linker script.
a2fb1b05 1932
4e2b1697 1933 Segment_list::const_iterator p;
16164a6b 1934 if (!os->is_unique_segment())
4e2b1697 1935 {
16164a6b 1936 for (p = this->segment_list_.begin();
bbc5ae17 1937 p != this->segment_list_.end();
16164a6b 1938 ++p)
a192ba05 1939 {
bbc5ae17
RM
1940 if ((*p)->type() != elfcpp::PT_LOAD)
1941 continue;
1942 if ((*p)->is_unique_segment())
1943 continue;
1944 if (!parameters->options().omagic()
1945 && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
1946 continue;
1947 if ((target->isolate_execinstr() || parameters->options().rosegment())
1948 && ((*p)->flags() & elfcpp::PF_X) != (seg_flags & elfcpp::PF_X))
1949 continue;
1950 // If -Tbss was specified, we need to separate the data and BSS
1951 // segments.
1952 if (parameters->options().user_set_Tbss())
1953 {
1954 if ((os->type() == elfcpp::SHT_NOBITS)
1955 == (*p)->has_any_data_sections())
1956 continue;
1957 }
1958 if (os->is_large_data_section() && !(*p)->is_large_data_segment())
1959 continue;
1960
1961 if (is_address_set)
1962 {
1963 if ((*p)->are_addresses_set())
1964 continue;
1965
1966 (*p)->add_initial_output_data(os);
1967 (*p)->update_flags_for_output_section(seg_flags);
1968 (*p)->set_addresses(addr, addr);
1969 break;
1970 }
1971
1972 (*p)->add_output_section_to_load(this, os, seg_flags);
1973 break;
1974 }
16164a6b
ST
1975 }
1976
1977 if (p == this->segment_list_.end()
1978 || os->is_unique_segment())
4e2b1697
ILT
1979 {
1980 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
2e702c99 1981 seg_flags);
8a5e3e08
ILT
1982 if (os->is_large_data_section())
1983 oseg->set_is_large_data_segment();
22f0da72 1984 oseg->add_output_section_to_load(this, os, seg_flags);
a192ba05
ILT
1985 if (is_address_set)
1986 oseg->set_addresses(addr, addr);
16164a6b
ST
1987 // Check if segment should be marked unique. For segments marked
1988 // unique by linker plugins, set the new alignment if specified.
1989 if (os->is_unique_segment())
1990 {
1991 oseg->set_is_unique_segment();
1992 if (os->segment_alignment() != 0)
1993 oseg->set_minimum_p_align(os->segment_alignment());
1994 }
a2fb1b05
ILT
1995 }
1996
4e2b1697
ILT
1997 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1998 // segment.
1999 if (os->type() == elfcpp::SHT_NOTE)
2000 {
2001 // See if we already have an equivalent PT_NOTE segment.
2002 for (p = this->segment_list_.begin();
2e702c99
RM
2003 p != segment_list_.end();
2004 ++p)
2005 {
2006 if ((*p)->type() == elfcpp::PT_NOTE
2007 && (((*p)->flags() & elfcpp::PF_W)
2008 == (seg_flags & elfcpp::PF_W)))
2009 {
2010 (*p)->add_output_section_to_nonload(os, seg_flags);
2011 break;
2012 }
2013 }
4e2b1697
ILT
2014
2015 if (p == this->segment_list_.end())
2e702c99
RM
2016 {
2017 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
2018 seg_flags);
2019 oseg->add_output_section_to_nonload(os, seg_flags);
2020 }
4e2b1697
ILT
2021 }
2022
2023 // If we see a loadable SHF_TLS section, we create a PT_TLS
2024 // segment. There can only be one such segment.
2025 if ((flags & elfcpp::SHF_TLS) != 0)
2026 {
2027 if (this->tls_segment_ == NULL)
2d924fd9 2028 this->make_output_segment(elfcpp::PT_TLS, seg_flags);
22f0da72 2029 this->tls_segment_->add_output_section_to_nonload(os, seg_flags);
4e2b1697 2030 }
9f1d377b
ILT
2031
2032 // If -z relro is in effect, and we see a relro section, we create a
2033 // PT_GNU_RELRO segment. There can only be one such segment.
2034 if (os->is_relro() && parameters->options().relro())
2035 {
2036 gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
2037 if (this->relro_segment_ == NULL)
2d924fd9 2038 this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
22f0da72 2039 this->relro_segment_->add_output_section_to_nonload(os, seg_flags);
9f1d377b 2040 }
10b4f102 2041
e1f74f98
ILT
2042 // If we see a section named .interp, put it into a PT_INTERP
2043 // segment. This seems broken to me, but this is what GNU ld does,
2044 // and glibc expects it.
10b4f102 2045 if (strcmp(os->name(), ".interp") == 0
e1f74f98 2046 && !this->script_options_->saw_phdrs_clause())
10b4f102
ILT
2047 {
2048 if (this->interp_segment_ == NULL)
2049 this->make_output_segment(elfcpp::PT_INTERP, seg_flags);
e1f74f98
ILT
2050 else
2051 gold_warning(_("multiple '.interp' sections in input files "
2052 "may cause confusing PT_INTERP segment"));
10b4f102
ILT
2053 this->interp_segment_->add_output_section_to_nonload(os, seg_flags);
2054 }
a2fb1b05
ILT
2055}
2056
919ed24c
ILT
2057// Make an output section for a script.
2058
2059Output_section*
1e5d2fb1
DK
2060Layout::make_output_section_for_script(
2061 const char* name,
2062 Script_sections::Section_type section_type)
919ed24c
ILT
2063{
2064 name = this->namepool_.add(name, false, NULL);
1e5d2fb1
DK
2065 elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
2066 if (section_type == Script_sections::ST_NOLOAD)
2067 sh_flags = 0;
919ed24c 2068 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
22f0da72
ILT
2069 sh_flags, ORDER_INVALID,
2070 false);
919ed24c 2071 os->set_found_in_sections_clause();
1e5d2fb1
DK
2072 if (section_type == Script_sections::ST_NOLOAD)
2073 os->set_is_noload();
919ed24c
ILT
2074 return os;
2075}
2076
3802b2dd
ILT
2077// Return the number of segments we expect to see.
2078
2079size_t
2080Layout::expected_segment_count() const
2081{
2082 size_t ret = this->segment_list_.size();
2083
2084 // If we didn't see a SECTIONS clause in a linker script, we should
2085 // already have the complete list of segments. Otherwise we ask the
2086 // SECTIONS clause how many segments it expects, and add in the ones
2087 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
2088
2089 if (!this->script_options_->saw_sections_clause())
2090 return ret;
2091 else
2092 {
2093 const Script_sections* ss = this->script_options_->script_sections();
2094 return ret + ss->expected_segment_count(this);
2095 }
2096}
2097
35cdfc9a
ILT
2098// Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
2099// is whether we saw a .note.GNU-stack section in the object file.
2100// GNU_STACK_FLAGS is the section flags. The flags give the
2101// protection required for stack memory. We record this in an
2102// executable as a PT_GNU_STACK segment. If an object file does not
2103// have a .note.GNU-stack segment, we must assume that it is an old
2104// object. On some targets that will force an executable stack.
2105
2106void
83e17bd5
CC
2107Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
2108 const Object* obj)
35cdfc9a
ILT
2109{
2110 if (!seen_gnu_stack)
83e17bd5
CC
2111 {
2112 this->input_without_gnu_stack_note_ = true;
2113 if (parameters->options().warn_execstack()
2114 && parameters->target().is_default_stack_executable())
2115 gold_warning(_("%s: missing .note.GNU-stack section"
2116 " implies executable stack"),
2117 obj->name().c_str());
2118 }
35cdfc9a
ILT
2119 else
2120 {
2121 this->input_with_gnu_stack_note_ = true;
2122 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
83e17bd5
CC
2123 {
2124 this->input_requires_executable_stack_ = true;
d8e60314 2125 if (parameters->options().warn_execstack())
83e17bd5
CC
2126 gold_warning(_("%s: requires executable stack"),
2127 obj->name().c_str());
2128 }
35cdfc9a
ILT
2129 }
2130}
2131
9c547ec3
ILT
2132// Create automatic note sections.
2133
2134void
2135Layout::create_notes()
2136{
2137 this->create_gold_note();
1130c90e 2138 this->create_stack_segment();
9c547ec3
ILT
2139 this->create_build_id();
2140}
2141
a3ad94ed
ILT
2142// Create the dynamic sections which are needed before we read the
2143// relocs.
2144
2145void
9b07f471 2146Layout::create_initial_dynamic_sections(Symbol_table* symtab)
a3ad94ed 2147{
436ca963 2148 if (parameters->doing_static_link())
a3ad94ed
ILT
2149 return;
2150
3802b2dd
ILT
2151 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
2152 elfcpp::SHT_DYNAMIC,
2153 (elfcpp::SHF_ALLOC
2154 | elfcpp::SHF_WRITE),
22f0da72
ILT
2155 false, ORDER_RELRO,
2156 true);
a3ad94ed 2157
6daf5215
ILT
2158 // A linker script may discard .dynamic, so check for NULL.
2159 if (this->dynamic_section_ != NULL)
2160 {
2161 this->dynamic_symbol_ =
2162 symtab->define_in_output_data("_DYNAMIC", NULL,
2163 Symbol_table::PREDEFINED,
2164 this->dynamic_section_, 0, 0,
2165 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
2166 elfcpp::STV_HIDDEN, 0, false, false);
16649710 2167
6daf5215 2168 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
16649710 2169
6daf5215
ILT
2170 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
2171 }
a3ad94ed
ILT
2172}
2173
bfd58944
ILT
2174// For each output section whose name can be represented as C symbol,
2175// define __start and __stop symbols for the section. This is a GNU
2176// extension.
2177
2178void
9b07f471 2179Layout::define_section_symbols(Symbol_table* symtab)
bfd58944
ILT
2180{
2181 for (Section_list::const_iterator p = this->section_list_.begin();
2182 p != this->section_list_.end();
2183 ++p)
2184 {
2185 const char* const name = (*p)->name();
f1ec9ded 2186 if (is_cident(name))
bfd58944
ILT
2187 {
2188 const std::string name_string(name);
f1ec9ded 2189 const std::string start_name(cident_section_start_prefix
2e702c99 2190 + name_string);
f1ec9ded 2191 const std::string stop_name(cident_section_stop_prefix
2e702c99 2192 + name_string);
bfd58944 2193
9b07f471 2194 symtab->define_in_output_data(start_name.c_str(),
bfd58944 2195 NULL, // version
99fff23b 2196 Symbol_table::PREDEFINED,
bfd58944
ILT
2197 *p,
2198 0, // value
2199 0, // symsize
2200 elfcpp::STT_NOTYPE,
2201 elfcpp::STB_GLOBAL,
2202 elfcpp::STV_DEFAULT,
2203 0, // nonvis
2204 false, // offset_is_from_end
a445fddf 2205 true); // only_if_ref
bfd58944 2206
9b07f471 2207 symtab->define_in_output_data(stop_name.c_str(),
bfd58944 2208 NULL, // version
99fff23b 2209 Symbol_table::PREDEFINED,
bfd58944
ILT
2210 *p,
2211 0, // value
2212 0, // symsize
2213 elfcpp::STT_NOTYPE,
2214 elfcpp::STB_GLOBAL,
2215 elfcpp::STV_DEFAULT,
2216 0, // nonvis
2217 true, // offset_is_from_end
a445fddf 2218 true); // only_if_ref
bfd58944
ILT
2219 }
2220 }
2221}
2222
755ab8af
ILT
2223// Define symbols for group signatures.
2224
2225void
2226Layout::define_group_signatures(Symbol_table* symtab)
2227{
2228 for (Group_signatures::iterator p = this->group_signatures_.begin();
2229 p != this->group_signatures_.end();
2230 ++p)
2231 {
2232 Symbol* sym = symtab->lookup(p->signature, NULL);
2233 if (sym != NULL)
2234 p->section->set_info_symndx(sym);
2235 else
2236 {
2237 // Force the name of the group section to the group
2238 // signature, and use the group's section symbol as the
2239 // signature symbol.
2240 if (strcmp(p->section->name(), p->signature) != 0)
2241 {
2242 const char* name = this->namepool_.add(p->signature,
2243 true, NULL);
2244 p->section->set_name(name);
2245 }
2246 p->section->set_needs_symtab_index();
2247 p->section->set_info_section_symndx(p->section);
2248 }
2249 }
2250
2251 this->group_signatures_.clear();
2252}
2253
75f65a3e
ILT
2254// Find the first read-only PT_LOAD segment, creating one if
2255// necessary.
54dc6425 2256
75f65a3e 2257Output_segment*
2e702c99 2258Layout::find_first_load_seg(const Target* target)
54dc6425 2259{
0f72bf6f 2260 Output_segment* best = NULL;
75f65a3e
ILT
2261 for (Segment_list::const_iterator p = this->segment_list_.begin();
2262 p != this->segment_list_.end();
2263 ++p)
2264 {
2265 if ((*p)->type() == elfcpp::PT_LOAD
2266 && ((*p)->flags() & elfcpp::PF_R) != 0
af6156ef 2267 && (parameters->options().omagic()
2e702c99
RM
2268 || ((*p)->flags() & elfcpp::PF_W) == 0)
2269 && (!target->isolate_execinstr()
2270 || ((*p)->flags() & elfcpp::PF_X) == 0))
2271 {
2272 if (best == NULL || this->segment_precedes(*p, best))
2273 best = *p;
2274 }
75f65a3e 2275 }
0f72bf6f
RÁE
2276 if (best != NULL)
2277 return best;
75f65a3e 2278
1c4f3631
ILT
2279 gold_assert(!this->script_options_->saw_phdrs_clause());
2280
3802b2dd
ILT
2281 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
2282 elfcpp::PF_R);
75f65a3e 2283 return load_seg;
54dc6425
ILT
2284}
2285
20e6d0d6
DK
2286// Save states of all current output segments. Store saved states
2287// in SEGMENT_STATES.
2288
2289void
2290Layout::save_segments(Segment_states* segment_states)
2291{
2292 for (Segment_list::const_iterator p = this->segment_list_.begin();
2293 p != this->segment_list_.end();
2294 ++p)
2295 {
2296 Output_segment* segment = *p;
2297 // Shallow copy.
2298 Output_segment* copy = new Output_segment(*segment);
2299 (*segment_states)[segment] = copy;
2300 }
2301}
2302
2303// Restore states of output segments and delete any segment not found in
2304// SEGMENT_STATES.
2305
2306void
2307Layout::restore_segments(const Segment_states* segment_states)
2308{
2309 // Go through the segment list and remove any segment added in the
2310 // relaxation loop.
2311 this->tls_segment_ = NULL;
2312 this->relro_segment_ = NULL;
2313 Segment_list::iterator list_iter = this->segment_list_.begin();
2314 while (list_iter != this->segment_list_.end())
2315 {
2316 Output_segment* segment = *list_iter;
2317 Segment_states::const_iterator states_iter =
2318 segment_states->find(segment);
2319 if (states_iter != segment_states->end())
2320 {
2321 const Output_segment* copy = states_iter->second;
2322 // Shallow copy to restore states.
2323 *segment = *copy;
2324
2325 // Also fix up TLS and RELRO segment pointers as appropriate.
2326 if (segment->type() == elfcpp::PT_TLS)
2327 this->tls_segment_ = segment;
2328 else if (segment->type() == elfcpp::PT_GNU_RELRO)
2329 this->relro_segment_ = segment;
2330
2331 ++list_iter;
2e702c99 2332 }
20e6d0d6
DK
2333 else
2334 {
2e702c99 2335 list_iter = this->segment_list_.erase(list_iter);
20e6d0d6
DK
2336 // This is a segment created during section layout. It should be
2337 // safe to remove it since we should have removed all pointers to it.
2338 delete segment;
2339 }
2340 }
2341}
2342
2343// Clean up after relaxation so that sections can be laid out again.
2344
2345void
2346Layout::clean_up_after_relaxation()
2347{
2348 // Restore the segments to point state just prior to the relaxation loop.
2349 Script_sections* script_section = this->script_options_->script_sections();
2350 script_section->release_segments();
2351 this->restore_segments(this->segment_states_);
2352
2353 // Reset section addresses and file offsets
2354 for (Section_list::iterator p = this->section_list_.begin();
2355 p != this->section_list_.end();
2356 ++p)
2357 {
20e6d0d6 2358 (*p)->restore_states();
8923b24c
DK
2359
2360 // If an input section changes size because of relaxation,
2361 // we need to adjust the section offsets of all input sections.
2362 // after such a section.
2363 if ((*p)->section_offsets_need_adjustment())
2364 (*p)->adjust_section_offsets();
2365
2366 (*p)->reset_address_and_file_offset();
20e6d0d6 2367 }
2e702c99 2368
20e6d0d6
DK
2369 // Reset special output object address and file offsets.
2370 for (Data_list::iterator p = this->special_output_list_.begin();
2371 p != this->special_output_list_.end();
2372 ++p)
2373 (*p)->reset_address_and_file_offset();
2374
2375 // A linker script may have created some output section data objects.
2376 // They are useless now.
2377 for (Output_section_data_list::const_iterator p =
2378 this->script_output_section_data_list_.begin();
2379 p != this->script_output_section_data_list_.end();
2380 ++p)
2381 delete *p;
2e702c99 2382 this->script_output_section_data_list_.clear();
eb426534
RM
2383
2384 // Special-case fill output objects are recreated each time through
2385 // the relaxation loop.
2386 this->reset_relax_output();
2387}
2388
2389void
2390Layout::reset_relax_output()
2391{
2392 for (Data_list::const_iterator p = this->relax_output_list_.begin();
2393 p != this->relax_output_list_.end();
2394 ++p)
2395 delete *p;
2396 this->relax_output_list_.clear();
20e6d0d6
DK
2397}
2398
2399// Prepare for relaxation.
2400
2401void
2402Layout::prepare_for_relaxation()
2403{
2404 // Create an relaxation debug check if in debugging mode.
2405 if (is_debugging_enabled(DEBUG_RELAXATION))
2406 this->relaxation_debug_check_ = new Relaxation_debug_check();
2407
2408 // Save segment states.
2409 this->segment_states_ = new Segment_states();
2410 this->save_segments(this->segment_states_);
2411
2412 for(Section_list::const_iterator p = this->section_list_.begin();
2413 p != this->section_list_.end();
2414 ++p)
2415 (*p)->save_states();
2416
2417 if (is_debugging_enabled(DEBUG_RELAXATION))
2418 this->relaxation_debug_check_->check_output_data_for_reset_values(
eb426534
RM
2419 this->section_list_, this->special_output_list_,
2420 this->relax_output_list_);
20e6d0d6
DK
2421
2422 // Also enable recording of output section data from scripts.
2423 this->record_output_section_data_from_script_ = true;
2424}
2425
a3ed37d8
RM
2426// If the user set the address of the text segment, that may not be
2427// compatible with putting the segment headers and file headers into
2428// that segment. For isolate_execinstr() targets, it's the rodata
2429// segment rather than text where we might put the headers.
2430static inline bool
2431load_seg_unusable_for_headers(const Target* target)
2432{
2433 const General_options& options = parameters->options();
2434 if (target->isolate_execinstr())
2435 return (options.user_set_Trodata_segment()
2436 && options.Trodata_segment() % target->abi_pagesize() != 0);
2437 else
2438 return (options.user_set_Ttext()
2439 && options.Ttext() % target->abi_pagesize() != 0);
2440}
2441
20e6d0d6
DK
2442// Relaxation loop body: If target has no relaxation, this runs only once
2443// Otherwise, the target relaxation hook is called at the end of
2444// each iteration. If the hook returns true, it means re-layout of
2e702c99 2445// section is required.
20e6d0d6
DK
2446//
2447// The number of segments created by a linking script without a PHDRS
2448// clause may be affected by section sizes and alignments. There is
2449// a remote chance that relaxation causes different number of PT_LOAD
2450// segments are created and sections are attached to different segments.
2451// Therefore, we always throw away all segments created during section
2452// layout. In order to be able to restart the section layout, we keep
2453// a copy of the segment list right before the relaxation loop and use
2454// that to restore the segments.
2e702c99
RM
2455//
2456// PASS is the current relaxation pass number.
20e6d0d6
DK
2457// SYMTAB is a symbol table.
2458// PLOAD_SEG is the address of a pointer for the load segment.
2459// PHDR_SEG is a pointer to the PHDR segment.
2460// SEGMENT_HEADERS points to the output segment header.
2461// FILE_HEADER points to the output file header.
2462// PSHNDX is the address to store the output section index.
2463
2464off_t inline
2465Layout::relaxation_loop_body(
2466 int pass,
2467 Target* target,
2468 Symbol_table* symtab,
2469 Output_segment** pload_seg,
2470 Output_segment* phdr_seg,
2471 Output_segment_headers* segment_headers,
2472 Output_file_header* file_header,
2473 unsigned int* pshndx)
2474{
2475 // If this is not the first iteration, we need to clean up after
2476 // relaxation so that we can lay out the sections again.
2477 if (pass != 0)
2478 this->clean_up_after_relaxation();
2479
2480 // If there is a SECTIONS clause, put all the input sections into
2481 // the required order.
2482 Output_segment* load_seg;
2483 if (this->script_options_->saw_sections_clause())
2484 load_seg = this->set_section_addresses_from_script(symtab);
2485 else if (parameters->options().relocatable())
2486 load_seg = NULL;
2487 else
2e702c99 2488 load_seg = this->find_first_load_seg(target);
20e6d0d6
DK
2489
2490 if (parameters->options().oformat_enum()
2491 != General_options::OBJECT_FORMAT_ELF)
2492 load_seg = NULL;
2493
a3ed37d8 2494 if (load_seg_unusable_for_headers(target))
d12a5ea8
ILT
2495 {
2496 load_seg = NULL;
2497 phdr_seg = NULL;
2498 }
403a15dd 2499
68b6574b
ILT
2500 gold_assert(phdr_seg == NULL
2501 || load_seg != NULL
2502 || this->script_options_->saw_sections_clause());
20e6d0d6 2503
a192ba05 2504 // If the address of the load segment we found has been set by
1e3811b0
ILT
2505 // --section-start rather than by a script, then adjust the VMA and
2506 // LMA downward if possible to include the file and section headers.
2507 uint64_t header_gap = 0;
a192ba05
ILT
2508 if (load_seg != NULL
2509 && load_seg->are_addresses_set()
1e3811b0
ILT
2510 && !this->script_options_->saw_sections_clause()
2511 && !parameters->options().relocatable())
2512 {
2513 file_header->finalize_data_size();
2514 segment_headers->finalize_data_size();
2515 size_t sizeof_headers = (file_header->data_size()
2516 + segment_headers->data_size());
2517 const uint64_t abi_pagesize = target->abi_pagesize();
2518 uint64_t hdr_paddr = load_seg->paddr() - sizeof_headers;
2519 hdr_paddr &= ~(abi_pagesize - 1);
2520 uint64_t subtract = load_seg->paddr() - hdr_paddr;
2521 if (load_seg->paddr() < subtract || load_seg->vaddr() < subtract)
2522 load_seg = NULL;
2523 else
2524 {
2525 load_seg->set_addresses(load_seg->vaddr() - subtract,
2526 load_seg->paddr() - subtract);
2527 header_gap = subtract - sizeof_headers;
2528 }
2529 }
a192ba05 2530
20e6d0d6
DK
2531 // Lay out the segment headers.
2532 if (!parameters->options().relocatable())
2533 {
2534 gold_assert(segment_headers != NULL);
1e3811b0
ILT
2535 if (header_gap != 0 && load_seg != NULL)
2536 {
2537 Output_data_zero_fill* z = new Output_data_zero_fill(header_gap, 1);
2538 load_seg->add_initial_output_data(z);
2539 }
20e6d0d6 2540 if (load_seg != NULL)
2e702c99 2541 load_seg->add_initial_output_data(segment_headers);
20e6d0d6 2542 if (phdr_seg != NULL)
2e702c99 2543 phdr_seg->add_initial_output_data(segment_headers);
20e6d0d6
DK
2544 }
2545
2546 // Lay out the file header.
2547 if (load_seg != NULL)
2548 load_seg->add_initial_output_data(file_header);
2549
2550 if (this->script_options_->saw_phdrs_clause()
2551 && !parameters->options().relocatable())
2552 {
2553 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
2554 // clause in a linker script.
2555 Script_sections* ss = this->script_options_->script_sections();
2556 ss->put_headers_in_phdrs(file_header, segment_headers);
2557 }
2558
2559 // We set the output section indexes in set_segment_offsets and
2560 // set_section_indexes.
2561 *pshndx = 1;
2562
2563 // Set the file offsets of all the segments, and all the sections
2564 // they contain.
2565 off_t off;
2566 if (!parameters->options().relocatable())
2567 off = this->set_segment_offsets(target, load_seg, pshndx);
2568 else
2569 off = this->set_relocatable_section_offsets(file_header, pshndx);
2570
2571 // Verify that the dummy relaxation does not change anything.
2572 if (is_debugging_enabled(DEBUG_RELAXATION))
2573 {
2574 if (pass == 0)
2575 this->relaxation_debug_check_->read_sections(this->section_list_);
2576 else
2577 this->relaxation_debug_check_->verify_sections(this->section_list_);
2578 }
2579
2580 *pload_seg = load_seg;
2581 return off;
2582}
2583
6e9ba2ca
ST
2584// Search the list of patterns and find the postion of the given section
2585// name in the output section. If the section name matches a glob
2586// pattern and a non-glob name, then the non-glob position takes
2587// precedence. Return 0 if no match is found.
2588
2589unsigned int
2590Layout::find_section_order_index(const std::string& section_name)
2591{
2592 Unordered_map<std::string, unsigned int>::iterator map_it;
2593 map_it = this->input_section_position_.find(section_name);
2594 if (map_it != this->input_section_position_.end())
2595 return map_it->second;
2596
2597 // Absolute match failed. Linear search the glob patterns.
2598 std::vector<std::string>::iterator it;
2599 for (it = this->input_section_glob_.begin();
2600 it != this->input_section_glob_.end();
2601 ++it)
2602 {
2603 if (fnmatch((*it).c_str(), section_name.c_str(), FNM_NOESCAPE) == 0)
2e702c99
RM
2604 {
2605 map_it = this->input_section_position_.find(*it);
2606 gold_assert(map_it != this->input_section_position_.end());
2607 return map_it->second;
2608 }
6e9ba2ca
ST
2609 }
2610 return 0;
2611}
2612
2613// Read the sequence of input sections from the file specified with
e9552f7e 2614// option --section-ordering-file.
6e9ba2ca
ST
2615
2616void
2617Layout::read_layout_from_file()
2618{
2619 const char* filename = parameters->options().section_ordering_file();
2620 std::ifstream in;
2621 std::string line;
2622
2623 in.open(filename);
2624 if (!in)
2625 gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
2e702c99 2626 filename, strerror(errno));
6e9ba2ca
ST
2627
2628 std::getline(in, line); // this chops off the trailing \n, if any
2629 unsigned int position = 1;
e9552f7e 2630 this->set_section_ordering_specified();
6e9ba2ca
ST
2631
2632 while (in)
2633 {
2634 if (!line.empty() && line[line.length() - 1] == '\r') // Windows
2e702c99 2635 line.resize(line.length() - 1);
6e9ba2ca
ST
2636 // Ignore comments, beginning with '#'
2637 if (line[0] == '#')
2e702c99
RM
2638 {
2639 std::getline(in, line);
2640 continue;
2641 }
6e9ba2ca
ST
2642 this->input_section_position_[line] = position;
2643 // Store all glob patterns in a vector.
2644 if (is_wildcard_string(line.c_str()))
2e702c99 2645 this->input_section_glob_.push_back(line);
6e9ba2ca
ST
2646 position++;
2647 std::getline(in, line);
2648 }
2649}
2650
54dc6425
ILT
2651// Finalize the layout. When this is called, we have created all the
2652// output sections and all the output segments which are based on
2653// input sections. We have several things to do, and we have to do
2654// them in the right order, so that we get the right results correctly
2655// and efficiently.
2656
2657// 1) Finalize the list of output segments and create the segment
2658// table header.
2659
2660// 2) Finalize the dynamic symbol table and associated sections.
2661
2662// 3) Determine the final file offset of all the output segments.
2663
2664// 4) Determine the final file offset of all the SHF_ALLOC output
2665// sections.
2666
75f65a3e
ILT
2667// 5) Create the symbol table sections and the section name table
2668// section.
2669
2670// 6) Finalize the symbol table: set symbol values to their final
54dc6425
ILT
2671// value and make a final determination of which symbols are going
2672// into the output symbol table.
2673
54dc6425
ILT
2674// 7) Create the section table header.
2675
2676// 8) Determine the final file offset of all the output sections which
2677// are not SHF_ALLOC, including the section table header.
2678
2679// 9) Finalize the ELF file header.
2680
75f65a3e
ILT
2681// This function returns the size of the output file.
2682
2683off_t
17a1d0a9 2684Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
8851ecca 2685 Target* target, const Task* task)
54dc6425 2686{
f59f41f3 2687 target->finalize_sections(this, input_objects, symtab);
5a6f7e2d 2688
17a1d0a9 2689 this->count_local_symbols(task, input_objects);
7bf1f802 2690
1518dc8f 2691 this->link_stabs_sections();
4f211c8b 2692
3802b2dd 2693 Output_segment* phdr_seg = NULL;
8851ecca 2694 if (!parameters->options().relocatable() && !parameters->doing_static_link())
54dc6425 2695 {
dbe717ef
ILT
2696 // There was a dynamic object in the link. We need to create
2697 // some information for the dynamic linker.
2698
3802b2dd
ILT
2699 // Create the PT_PHDR segment which will hold the program
2700 // headers.
1c4f3631
ILT
2701 if (!this->script_options_->saw_phdrs_clause())
2702 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
3802b2dd 2703
14b31740
ILT
2704 // Create the dynamic symbol table, including the hash table.
2705 Output_section* dynstr;
2706 std::vector<Symbol*> dynamic_symbols;
2707 unsigned int local_dynamic_count;
a5dc0706 2708 Versions versions(*this->script_options()->version_script_info(),
2e702c99 2709 &this->dynpool_);
9b07f471 2710 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
14b31740
ILT
2711 &local_dynamic_count, &dynamic_symbols,
2712 &versions);
dbe717ef
ILT
2713
2714 // Create the .interp section to hold the name of the
e1f74f98
ILT
2715 // interpreter, and put it in a PT_INTERP segment. Don't do it
2716 // if we saw a .interp section in an input file.
2717 if ((!parameters->options().shared()
2718 || parameters->options().dynamic_linker() != NULL)
2719 && this->interp_segment_ == NULL)
2e702c99 2720 this->create_interp(target);
a3ad94ed
ILT
2721
2722 // Finish the .dynamic section to hold the dynamic data, and put
2723 // it in a PT_DYNAMIC segment.
16649710 2724 this->finish_dynamic_section(input_objects, symtab);
14b31740
ILT
2725
2726 // We should have added everything we need to the dynamic string
2727 // table.
2728 this->dynpool_.set_string_offsets();
2729
2730 // Create the version sections. We can't do this until the
2731 // dynamic string table is complete.
46fe1623 2732 this->create_version_sections(&versions, symtab, local_dynamic_count,
14b31740 2733 dynamic_symbols, dynstr);
f0ba79e2
ILT
2734
2735 // Set the size of the _DYNAMIC symbol. We can't do this until
2736 // after we call create_version_sections.
2737 this->set_dynamic_symbol_size(symtab);
54dc6425 2738 }
2e702c99 2739
20e6d0d6
DK
2740 // Create segment headers.
2741 Output_segment_headers* segment_headers =
2742 (parameters->options().relocatable()
2743 ? NULL
2744 : new Output_segment_headers(this->segment_list_));
75f65a3e
ILT
2745
2746 // Lay out the file header.
a10ae760
ILT
2747 Output_file_header* file_header = new Output_file_header(target, symtab,
2748 segment_headers);
a445fddf 2749
61ba1cf9 2750 this->special_output_list_.push_back(file_header);
6a74a719
ILT
2751 if (segment_headers != NULL)
2752 this->special_output_list_.push_back(segment_headers);
75f65a3e 2753
20e6d0d6
DK
2754 // Find approriate places for orphan output sections if we are using
2755 // a linker script.
2756 if (this->script_options_->saw_sections_clause())
2757 this->place_orphan_sections_in_script();
2e702c99 2758
20e6d0d6
DK
2759 Output_segment* load_seg;
2760 off_t off;
2761 unsigned int shndx;
2762 int pass = 0;
2763
2764 // Take a snapshot of the section layout as needed.
2765 if (target->may_relax())
2766 this->prepare_for_relaxation();
2e702c99 2767
20e6d0d6
DK
2768 // Run the relaxation loop to lay out sections.
2769 do
1c4f3631 2770 {
20e6d0d6
DK
2771 off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
2772 phdr_seg, segment_headers, file_header,
2773 &shndx);
2774 pass++;
1c4f3631 2775 }
c0a62865 2776 while (target->may_relax()
f625ae50 2777 && target->relax(pass, input_objects, symtab, this, task));
75f65a3e 2778
eabc84f4
RM
2779 // If there is a load segment that contains the file and program headers,
2780 // provide a symbol __ehdr_start pointing there.
2781 // A program can use this to examine itself robustly.
d1bddd3c
CC
2782 Symbol *ehdr_start = symtab->lookup("__ehdr_start");
2783 if (ehdr_start != NULL && ehdr_start->is_predefined())
2784 {
2785 if (load_seg != NULL)
2786 ehdr_start->set_output_segment(load_seg, Symbol::SEGMENT_START);
2787 else
1130c90e 2788 ehdr_start->set_undefined();
d1bddd3c 2789 }
eabc84f4 2790
a9a60db6
ILT
2791 // Set the file offsets of all the non-data sections we've seen so
2792 // far which don't have to wait for the input sections. We need
2793 // this in order to finalize local symbols in non-allocated
2794 // sections.
2795 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
2796
d491d34e
ILT
2797 // Set the section indexes of all unallocated sections seen so far,
2798 // in case any of them are somehow referenced by a symbol.
2799 shndx = this->set_section_indexes(shndx);
2800
75f65a3e 2801 // Create the symbol table sections.
d491d34e 2802 this->create_symtab_sections(input_objects, symtab, shndx, &off);
7bf1f802
ILT
2803 if (!parameters->doing_static_link())
2804 this->assign_local_dynsym_offsets(input_objects);
75f65a3e 2805
e5756efb
ILT
2806 // Process any symbol assignments from a linker script. This must
2807 // be called after the symbol table has been finalized.
2808 this->script_options_->finalize_symbols(symtab, this);
2809
09ec0418
CC
2810 // Create the incremental inputs sections.
2811 if (this->incremental_inputs_)
2812 {
2813 this->incremental_inputs_->finalize();
2814 this->create_incremental_info_sections(symtab);
2815 }
2816
75f65a3e
ILT
2817 // Create the .shstrtab section.
2818 Output_section* shstrtab_section = this->create_shstrtab();
2819
a9a60db6
ILT
2820 // Set the file offsets of the rest of the non-data sections which
2821 // don't have to wait for the input sections.
9a0910c3 2822 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
86887060 2823
d491d34e
ILT
2824 // Now that all sections have been created, set the section indexes
2825 // for any sections which haven't been done yet.
86887060 2826 shndx = this->set_section_indexes(shndx);
ead1e424 2827
75f65a3e 2828 // Create the section table header.
d491d34e 2829 this->create_shdrs(shstrtab_section, &off);
75f65a3e 2830
17a1d0a9
ILT
2831 // If there are no sections which require postprocessing, we can
2832 // handle the section names now, and avoid a resize later.
2833 if (!this->any_postprocessing_sections_)
09ec0418
CC
2834 {
2835 off = this->set_section_offsets(off,
2836 POSTPROCESSING_SECTIONS_PASS);
2837 off =
2838 this->set_section_offsets(off,
17a1d0a9 2839 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
09ec0418 2840 }
17a1d0a9 2841
27bc2bce 2842 file_header->set_section_info(this->section_headers_, shstrtab_section);
75f65a3e 2843
27bc2bce
ILT
2844 // Now we know exactly where everything goes in the output file
2845 // (except for non-allocated sections which require postprocessing).
a3ad94ed 2846 Output_data::layout_complete();
75f65a3e 2847
e44fcf3b
ILT
2848 this->output_file_size_ = off;
2849
75f65a3e
ILT
2850 return off;
2851}
2852
8ed814a9 2853// Create a note header following the format defined in the ELF ABI.
ec3f783e
ILT
2854// NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
2855// of the section to create, DESCSZ is the size of the descriptor.
2856// ALLOCATE is true if the section should be allocated in memory.
2857// This returns the new note section. It sets *TRAILING_PADDING to
2858// the number of trailing zero bytes required.
4f211c8b 2859
8ed814a9 2860Output_section*
ef4ab7a8
PP
2861Layout::create_note(const char* name, int note_type,
2862 const char* section_name, size_t descsz,
8ed814a9 2863 bool allocate, size_t* trailing_padding)
4f211c8b 2864{
e2305dc0
ILT
2865 // Authorities all agree that the values in a .note field should
2866 // be aligned on 4-byte boundaries for 32-bit binaries. However,
2867 // they differ on what the alignment is for 64-bit binaries.
2868 // The GABI says unambiguously they take 8-byte alignment:
2869 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
2870 // Other documentation says alignment should always be 4 bytes:
2871 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
2872 // GNU ld and GNU readelf both support the latter (at least as of
2873 // version 2.16.91), and glibc always generates the latter for
2874 // .note.ABI-tag (as of version 1.6), so that's the one we go with
2875 // here.
35cdfc9a 2876#ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
8851ecca 2877 const int size = parameters->target().get_size();
e2305dc0
ILT
2878#else
2879 const int size = 32;
2880#endif
4f211c8b
ILT
2881
2882 // The contents of the .note section.
4f211c8b
ILT
2883 size_t namesz = strlen(name) + 1;
2884 size_t aligned_namesz = align_address(namesz, size / 8);
4f211c8b 2885 size_t aligned_descsz = align_address(descsz, size / 8);
4f211c8b 2886
8ed814a9 2887 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
4f211c8b 2888
8ed814a9
ILT
2889 unsigned char* buffer = new unsigned char[notehdrsz];
2890 memset(buffer, 0, notehdrsz);
4f211c8b 2891
8851ecca 2892 bool is_big_endian = parameters->target().is_big_endian();
4f211c8b
ILT
2893
2894 if (size == 32)
2895 {
2896 if (!is_big_endian)
2897 {
2898 elfcpp::Swap<32, false>::writeval(buffer, namesz);
2899 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
2900 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
2901 }
2902 else
2903 {
2904 elfcpp::Swap<32, true>::writeval(buffer, namesz);
2905 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
2906 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
2907 }
2908 }
2909 else if (size == 64)
2910 {
2911 if (!is_big_endian)
2912 {
2913 elfcpp::Swap<64, false>::writeval(buffer, namesz);
2914 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
2915 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
2916 }
2917 else
2918 {
2919 elfcpp::Swap<64, true>::writeval(buffer, namesz);
2920 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
2921 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
2922 }
2923 }
2924 else
2925 gold_unreachable();
2926
2927 memcpy(buffer + 3 * (size / 8), name, namesz);
4f211c8b 2928
8ed814a9 2929 elfcpp::Elf_Xword flags = 0;
22f0da72 2930 Output_section_order order = ORDER_INVALID;
8ed814a9 2931 if (allocate)
22f0da72
ILT
2932 {
2933 flags = elfcpp::SHF_ALLOC;
2934 order = ORDER_RO_NOTE;
2935 }
ec3f783e
ILT
2936 Output_section* os = this->choose_output_section(NULL, section_name,
2937 elfcpp::SHT_NOTE,
22f0da72 2938 flags, false, order, false);
9c547ec3
ILT
2939 if (os == NULL)
2940 return NULL;
2941
8ed814a9 2942 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
7d9e3d98
ILT
2943 size / 8,
2944 "** note header");
8ed814a9
ILT
2945 os->add_output_section_data(posd);
2946
2947 *trailing_padding = aligned_descsz - descsz;
2948
2949 return os;
2950}
2951
2952// For an executable or shared library, create a note to record the
2953// version of gold used to create the binary.
2954
2955void
2956Layout::create_gold_note()
2957{
cdc29364
CC
2958 if (parameters->options().relocatable()
2959 || parameters->incremental_update())
8ed814a9
ILT
2960 return;
2961
2962 std::string desc = std::string("gold ") + gold::get_version_string();
2963
2964 size_t trailing_padding;
ca09d69a 2965 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
ef4ab7a8
PP
2966 ".note.gnu.gold-version", desc.size(),
2967 false, &trailing_padding);
9c547ec3
ILT
2968 if (os == NULL)
2969 return;
8ed814a9
ILT
2970
2971 Output_section_data* posd = new Output_data_const(desc, 4);
4f211c8b 2972 os->add_output_section_data(posd);
8ed814a9
ILT
2973
2974 if (trailing_padding > 0)
2975 {
7d9e3d98 2976 posd = new Output_data_zero_fill(trailing_padding, 0);
8ed814a9
ILT
2977 os->add_output_section_data(posd);
2978 }
4f211c8b
ILT
2979}
2980
35cdfc9a
ILT
2981// Record whether the stack should be executable. This can be set
2982// from the command line using the -z execstack or -z noexecstack
2983// options. Otherwise, if any input file has a .note.GNU-stack
2984// section with the SHF_EXECINSTR flag set, the stack should be
2985// executable. Otherwise, if at least one input file a
2986// .note.GNU-stack section, and some input file has no .note.GNU-stack
2987// section, we use the target default for whether the stack should be
1130c90e
RM
2988// executable. If -z stack-size was used to set a p_memsz value for
2989// PT_GNU_STACK, we generate the segment regardless. Otherwise, we
2990// don't generate a stack note. When generating a object file, we
2991// create a .note.GNU-stack section with the appropriate marking.
2992// When generating an executable or shared library, we create a
2993// PT_GNU_STACK segment.
35cdfc9a
ILT
2994
2995void
1130c90e 2996Layout::create_stack_segment()
35cdfc9a
ILT
2997{
2998 bool is_stack_executable;
e55bde5e 2999 if (parameters->options().is_execstack_set())
d8e60314
CC
3000 {
3001 is_stack_executable = parameters->options().is_stack_executable();
3002 if (!is_stack_executable
1130c90e
RM
3003 && this->input_requires_executable_stack_
3004 && parameters->options().warn_execstack())
d8e60314 3005 gold_warning(_("one or more inputs require executable stack, "
1130c90e 3006 "but -z noexecstack was given"));
d8e60314 3007 }
1130c90e
RM
3008 else if (!this->input_with_gnu_stack_note_
3009 && (!parameters->options().user_set_stack_size()
3010 || parameters->options().relocatable()))
35cdfc9a
ILT
3011 return;
3012 else
3013 {
3014 if (this->input_requires_executable_stack_)
3015 is_stack_executable = true;
3016 else if (this->input_without_gnu_stack_note_)
9c547ec3
ILT
3017 is_stack_executable =
3018 parameters->target().is_default_stack_executable();
35cdfc9a
ILT
3019 else
3020 is_stack_executable = false;
3021 }
3022
8851ecca 3023 if (parameters->options().relocatable())
35cdfc9a
ILT
3024 {
3025 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
3026 elfcpp::Elf_Xword flags = 0;
3027 if (is_stack_executable)
3028 flags |= elfcpp::SHF_EXECINSTR;
22f0da72
ILT
3029 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags,
3030 ORDER_INVALID, false);
35cdfc9a
ILT
3031 }
3032 else
3033 {
1c4f3631
ILT
3034 if (this->script_options_->saw_phdrs_clause())
3035 return;
35cdfc9a
ILT
3036 int flags = elfcpp::PF_R | elfcpp::PF_W;
3037 if (is_stack_executable)
3038 flags |= elfcpp::PF_X;
1130c90e
RM
3039 Output_segment* seg =
3040 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
3041 seg->set_size(parameters->options().stack_size());
3042 // BFD lets targets override this default alignment, but the only
3043 // targets that do so are ones that Gold does not support so far.
3044 seg->set_minimum_p_align(16);
35cdfc9a
ILT
3045 }
3046}
3047
8ed814a9
ILT
3048// If --build-id was used, set up the build ID note.
3049
3050void
3051Layout::create_build_id()
3052{
3053 if (!parameters->options().user_set_build_id())
3054 return;
3055
3056 const char* style = parameters->options().build_id();
3057 if (strcmp(style, "none") == 0)
3058 return;
3059
3060 // Set DESCSZ to the size of the note descriptor. When possible,
3061 // set DESC to the note descriptor contents.
3062 size_t descsz;
3063 std::string desc;
3064 if (strcmp(style, "md5") == 0)
3065 descsz = 128 / 8;
e7c5ea40 3066 else if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
8ed814a9
ILT
3067 descsz = 160 / 8;
3068 else if (strcmp(style, "uuid") == 0)
3069 {
3070 const size_t uuidsz = 128 / 8;
3071
3072 char buffer[uuidsz];
3073 memset(buffer, 0, uuidsz);
3074
2a00e4fb 3075 int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
8ed814a9
ILT
3076 if (descriptor < 0)
3077 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
3078 strerror(errno));
3079 else
3080 {
3081 ssize_t got = ::read(descriptor, buffer, uuidsz);
2a00e4fb 3082 release_descriptor(descriptor, true);
8ed814a9
ILT
3083 if (got < 0)
3084 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
3085 else if (static_cast<size_t>(got) != uuidsz)
3086 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
3087 uuidsz, got);
3088 }
3089
3090 desc.assign(buffer, uuidsz);
3091 descsz = uuidsz;
3092 }
3093 else if (strncmp(style, "0x", 2) == 0)
3094 {
3095 hex_init();
3096 const char* p = style + 2;
3097 while (*p != '\0')
3098 {
3099 if (hex_p(p[0]) && hex_p(p[1]))
3100 {
3101 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
3102 desc += c;
3103 p += 2;
3104 }
3105 else if (*p == '-' || *p == ':')
3106 ++p;
3107 else
3108 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
3109 style);
3110 }
3111 descsz = desc.size();
3112 }
3113 else
3114 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
3115
3116 // Create the note.
3117 size_t trailing_padding;
3118 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
ef4ab7a8
PP
3119 ".note.gnu.build-id", descsz, true,
3120 &trailing_padding);
9c547ec3
ILT
3121 if (os == NULL)
3122 return;
8ed814a9
ILT
3123
3124 if (!desc.empty())
3125 {
3126 // We know the value already, so we fill it in now.
3127 gold_assert(desc.size() == descsz);
3128
3129 Output_section_data* posd = new Output_data_const(desc, 4);
3130 os->add_output_section_data(posd);
3131
3132 if (trailing_padding != 0)
3133 {
7d9e3d98 3134 posd = new Output_data_zero_fill(trailing_padding, 0);
8ed814a9
ILT
3135 os->add_output_section_data(posd);
3136 }
3137 }
3138 else
3139 {
3140 // We need to compute a checksum after we have completed the
3141 // link.
3142 gold_assert(trailing_padding == 0);
7d9e3d98 3143 this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
8ed814a9 3144 os->add_output_section_data(this->build_id_note_);
8ed814a9
ILT
3145 }
3146}
3147
1518dc8f
ILT
3148// If we have both .stabXX and .stabXXstr sections, then the sh_link
3149// field of the former should point to the latter. I'm not sure who
3150// started this, but the GNU linker does it, and some tools depend
3151// upon it.
3152
3153void
3154Layout::link_stabs_sections()
3155{
3156 if (!this->have_stabstr_section_)
3157 return;
3158
3159 for (Section_list::iterator p = this->section_list_.begin();
3160 p != this->section_list_.end();
3161 ++p)
3162 {
3163 if ((*p)->type() != elfcpp::SHT_STRTAB)
3164 continue;
3165
3166 const char* name = (*p)->name();
3167 if (strncmp(name, ".stab", 5) != 0)
3168 continue;
3169
3170 size_t len = strlen(name);
3171 if (strcmp(name + len - 3, "str") != 0)
3172 continue;
3173
3174 std::string stab_name(name, len - 3);
3175 Output_section* stab_sec;
3176 stab_sec = this->find_output_section(stab_name.c_str());
3177 if (stab_sec != NULL)
3178 stab_sec->set_link_section(*p);
3179 }
3180}
3181
09ec0418 3182// Create .gnu_incremental_inputs and related sections needed
3ce2c28e
ILT
3183// for the next run of incremental linking to check what has changed.
3184
3185void
09ec0418 3186Layout::create_incremental_info_sections(Symbol_table* symtab)
3ce2c28e 3187{
09ec0418
CC
3188 Incremental_inputs* incr = this->incremental_inputs_;
3189
3190 gold_assert(incr != NULL);
3191
3192 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
3193 incr->create_data_sections(symtab);
3ce2c28e
ILT
3194
3195 // Add the .gnu_incremental_inputs section.
ca09d69a 3196 const char* incremental_inputs_name =
3ce2c28e 3197 this->namepool_.add(".gnu_incremental_inputs", false, NULL);
09ec0418 3198 Output_section* incremental_inputs_os =
3ce2c28e 3199 this->make_output_section(incremental_inputs_name,
f5c870d2 3200 elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
22f0da72 3201 ORDER_INVALID, false);
09ec0418
CC
3202 incremental_inputs_os->add_output_section_data(incr->inputs_section());
3203
3204 // Add the .gnu_incremental_symtab section.
ca09d69a 3205 const char* incremental_symtab_name =
09ec0418
CC
3206 this->namepool_.add(".gnu_incremental_symtab", false, NULL);
3207 Output_section* incremental_symtab_os =
3208 this->make_output_section(incremental_symtab_name,
3209 elfcpp::SHT_GNU_INCREMENTAL_SYMTAB, 0,
3210 ORDER_INVALID, false);
3211 incremental_symtab_os->add_output_section_data(incr->symtab_section());
3212 incremental_symtab_os->set_entsize(4);
3213
3214 // Add the .gnu_incremental_relocs section.
ca09d69a 3215 const char* incremental_relocs_name =
09ec0418
CC
3216 this->namepool_.add(".gnu_incremental_relocs", false, NULL);
3217 Output_section* incremental_relocs_os =
3218 this->make_output_section(incremental_relocs_name,
3219 elfcpp::SHT_GNU_INCREMENTAL_RELOCS, 0,
3220 ORDER_INVALID, false);
3221 incremental_relocs_os->add_output_section_data(incr->relocs_section());
3222 incremental_relocs_os->set_entsize(incr->relocs_entsize());
3223
0e70b911 3224 // Add the .gnu_incremental_got_plt section.
ca09d69a 3225 const char* incremental_got_plt_name =
0e70b911
CC
3226 this->namepool_.add(".gnu_incremental_got_plt", false, NULL);
3227 Output_section* incremental_got_plt_os =
3228 this->make_output_section(incremental_got_plt_name,
3229 elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT, 0,
3230 ORDER_INVALID, false);
3231 incremental_got_plt_os->add_output_section_data(incr->got_plt_section());
3232
3ce2c28e 3233 // Add the .gnu_incremental_strtab section.
ca09d69a 3234 const char* incremental_strtab_name =
3ce2c28e 3235 this->namepool_.add(".gnu_incremental_strtab", false, NULL);
09ec0418 3236 Output_section* incremental_strtab_os = this->make_output_section(incremental_strtab_name,
2e702c99
RM
3237 elfcpp::SHT_STRTAB, 0,
3238 ORDER_INVALID, false);
3ce2c28e 3239 Output_data_strtab* strtab_data =
09ec0418
CC
3240 new Output_data_strtab(incr->get_stringpool());
3241 incremental_strtab_os->add_output_section_data(strtab_data);
3242
3243 incremental_inputs_os->set_after_input_sections();
3244 incremental_symtab_os->set_after_input_sections();
3245 incremental_relocs_os->set_after_input_sections();
0e70b911 3246 incremental_got_plt_os->set_after_input_sections();
09ec0418
CC
3247
3248 incremental_inputs_os->set_link_section(incremental_strtab_os);
3249 incremental_symtab_os->set_link_section(incremental_inputs_os);
3250 incremental_relocs_os->set_link_section(incremental_inputs_os);
0e70b911 3251 incremental_got_plt_os->set_link_section(incremental_inputs_os);
3ce2c28e
ILT
3252}
3253
75f65a3e
ILT
3254// Return whether SEG1 should be before SEG2 in the output file. This
3255// is based entirely on the segment type and flags. When this is
aecf301f 3256// called the segment addresses have normally not yet been set.
75f65a3e
ILT
3257
3258bool
3259Layout::segment_precedes(const Output_segment* seg1,
3260 const Output_segment* seg2)
3261{
3262 elfcpp::Elf_Word type1 = seg1->type();
3263 elfcpp::Elf_Word type2 = seg2->type();
3264
3265 // The single PT_PHDR segment is required to precede any loadable
3266 // segment. We simply make it always first.
3267 if (type1 == elfcpp::PT_PHDR)
3268 {
a3ad94ed 3269 gold_assert(type2 != elfcpp::PT_PHDR);
75f65a3e
ILT
3270 return true;
3271 }
3272 if (type2 == elfcpp::PT_PHDR)
3273 return false;
3274
3275 // The single PT_INTERP segment is required to precede any loadable
3276 // segment. We simply make it always second.
3277 if (type1 == elfcpp::PT_INTERP)
3278 {
a3ad94ed 3279 gold_assert(type2 != elfcpp::PT_INTERP);
75f65a3e
ILT
3280 return true;
3281 }
3282 if (type2 == elfcpp::PT_INTERP)
3283 return false;
3284
3285 // We then put PT_LOAD segments before any other segments.
3286 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
3287 return true;
3288 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
3289 return false;
3290
9f1d377b
ILT
3291 // We put the PT_TLS segment last except for the PT_GNU_RELRO
3292 // segment, because that is where the dynamic linker expects to find
3293 // it (this is just for efficiency; other positions would also work
3294 // correctly).
3295 if (type1 == elfcpp::PT_TLS
3296 && type2 != elfcpp::PT_TLS
3297 && type2 != elfcpp::PT_GNU_RELRO)
3298 return false;
3299 if (type2 == elfcpp::PT_TLS
3300 && type1 != elfcpp::PT_TLS
3301 && type1 != elfcpp::PT_GNU_RELRO)
3302 return true;
3303
3304 // We put the PT_GNU_RELRO segment last, because that is where the
3305 // dynamic linker expects to find it (as with PT_TLS, this is just
3306 // for efficiency).
3307 if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
92e059d8 3308 return false;
9f1d377b 3309 if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
92e059d8
ILT
3310 return true;
3311
75f65a3e
ILT
3312 const elfcpp::Elf_Word flags1 = seg1->flags();
3313 const elfcpp::Elf_Word flags2 = seg2->flags();
3314
3315 // The order of non-PT_LOAD segments is unimportant. We simply sort
3316 // by the numeric segment type and flags values. There should not
a4034750
AM
3317 // be more than one segment with the same type and flags, except
3318 // when a linker script specifies such.
75f65a3e
ILT
3319 if (type1 != elfcpp::PT_LOAD)
3320 {
3321 if (type1 != type2)
3322 return type1 < type2;
a4034750
AM
3323 gold_assert(flags1 != flags2
3324 || this->script_options_->saw_phdrs_clause());
75f65a3e
ILT
3325 return flags1 < flags2;
3326 }
3327
a445fddf
ILT
3328 // If the addresses are set already, sort by load address.
3329 if (seg1->are_addresses_set())
3330 {
3331 if (!seg2->are_addresses_set())
3332 return true;
3333
3334 unsigned int section_count1 = seg1->output_section_count();
3335 unsigned int section_count2 = seg2->output_section_count();
3336 if (section_count1 == 0 && section_count2 > 0)
3337 return true;
3338 if (section_count1 > 0 && section_count2 == 0)
3339 return false;
3340
b8fa8750
NC
3341 uint64_t paddr1 = (seg1->are_addresses_set()
3342 ? seg1->paddr()
3343 : seg1->first_section_load_address());
3344 uint64_t paddr2 = (seg2->are_addresses_set()
3345 ? seg2->paddr()
3346 : seg2->first_section_load_address());
3347
a445fddf
ILT
3348 if (paddr1 != paddr2)
3349 return paddr1 < paddr2;
3350 }
3351 else if (seg2->are_addresses_set())
3352 return false;
3353
8a5e3e08
ILT
3354 // A segment which holds large data comes after a segment which does
3355 // not hold large data.
3356 if (seg1->is_large_data_segment())
3357 {
3358 if (!seg2->is_large_data_segment())
3359 return false;
3360 }
3361 else if (seg2->is_large_data_segment())
3362 return true;
3363
3364 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
3365 // segments come before writable segments. Then writable segments
3366 // with data come before writable segments without data. Then
3367 // executable segments come before non-executable segments. Then
3368 // the unlikely case of a non-readable segment comes before the
3369 // normal case of a readable segment. If there are multiple
3370 // segments with the same type and flags, we require that the
3371 // address be set, and we sort by virtual address and then physical
3372 // address.
75f65a3e
ILT
3373 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
3374 return (flags1 & elfcpp::PF_W) == 0;
756ac4a8
ILT
3375 if ((flags1 & elfcpp::PF_W) != 0
3376 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
3377 return seg1->has_any_data_sections();
75f65a3e
ILT
3378 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
3379 return (flags1 & elfcpp::PF_X) != 0;
3380 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
3381 return (flags1 & elfcpp::PF_R) == 0;
3382
a445fddf 3383 // We shouldn't get here--we shouldn't create segments which we
aecf301f 3384 // can't distinguish. Unless of course we are using a weird linker
16164a6b
ST
3385 // script or overlapping --section-start options. We could also get
3386 // here if plugins want unique segments for subsets of sections.
ea0d8c47 3387 gold_assert(this->script_options_->saw_phdrs_clause()
16164a6b
ST
3388 || parameters->options().any_section_start()
3389 || this->is_unique_segment_for_sections_specified());
aecf301f 3390 return false;
75f65a3e
ILT
3391}
3392
8a5e3e08
ILT
3393// Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
3394
3395static off_t
3396align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
3397{
3398 uint64_t unsigned_off = off;
3399 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
3400 | (addr & (abi_pagesize - 1)));
3401 if (aligned_off < unsigned_off)
3402 aligned_off += abi_pagesize;
3403 return aligned_off;
3404}
3405
a3ed37d8
RM
3406// On targets where the text segment contains only executable code,
3407// a non-executable segment is never the text segment.
3408
3409static inline bool
3410is_text_segment(const Target* target, const Output_segment* seg)
3411{
3412 elfcpp::Elf_Xword flags = seg->flags();
3413 if ((flags & elfcpp::PF_W) != 0)
3414 return false;
3415 if ((flags & elfcpp::PF_X) == 0)
3416 return !target->isolate_execinstr();
3417 return true;
3418}
3419
ead1e424
ILT
3420// Set the file offsets of all the segments, and all the sections they
3421// contain. They have all been created. LOAD_SEG must be be laid out
3422// first. Return the offset of the data to follow.
75f65a3e
ILT
3423
3424off_t
ead1e424 3425Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
ca09d69a 3426 unsigned int* pshndx)
75f65a3e 3427{
aecf301f
ILT
3428 // Sort them into the final order. We use a stable sort so that we
3429 // don't randomize the order of indistinguishable segments created
3430 // by linker scripts.
3431 std::stable_sort(this->segment_list_.begin(), this->segment_list_.end(),
3432 Layout::Compare_segments(this));
54dc6425 3433
75f65a3e
ILT
3434 // Find the PT_LOAD segments, and set their addresses and offsets
3435 // and their section's addresses and offsets.
2e702c99 3436 uint64_t start_addr;
e55bde5e 3437 if (parameters->options().user_set_Ttext())
2e702c99 3438 start_addr = parameters->options().Ttext();
374ad285 3439 else if (parameters->options().output_is_position_independent())
2e702c99 3440 start_addr = 0;
0c5e9c22 3441 else
2e702c99
RM
3442 start_addr = target->default_text_segment_address();
3443
3444 uint64_t addr = start_addr;
75f65a3e 3445 off_t off = 0;
a445fddf
ILT
3446
3447 // If LOAD_SEG is NULL, then the file header and segment headers
3448 // will not be loadable. But they still need to be at offset 0 in
3449 // the file. Set their offsets now.
3450 if (load_seg == NULL)
3451 {
3452 for (Data_list::iterator p = this->special_output_list_.begin();
3453 p != this->special_output_list_.end();
3454 ++p)
3455 {
3456 off = align_address(off, (*p)->addralign());
3457 (*p)->set_address_and_file_offset(0, off);
3458 off += (*p)->data_size();
3459 }
3460 }
3461
1a2dff53
ILT
3462 unsigned int increase_relro = this->increase_relro_;
3463 if (this->script_options_->saw_sections_clause())
3464 increase_relro = 0;
3465
34810851
ILT
3466 const bool check_sections = parameters->options().check_sections();
3467 Output_segment* last_load_segment = NULL;
3468
2e702c99
RM
3469 unsigned int shndx_begin = *pshndx;
3470 unsigned int shndx_load_seg = *pshndx;
3471
75f65a3e
ILT
3472 for (Segment_list::iterator p = this->segment_list_.begin();
3473 p != this->segment_list_.end();
3474 ++p)
3475 {
3476 if ((*p)->type() == elfcpp::PT_LOAD)
3477 {
2e702c99
RM
3478 if (target->isolate_execinstr())
3479 {
3480 // When we hit the segment that should contain the
3481 // file headers, reset the file offset so we place
3482 // it and subsequent segments appropriately.
3483 // We'll fix up the preceding segments below.
3484 if (load_seg == *p)
3485 {
3486 if (off == 0)
3487 load_seg = NULL;
3488 else
3489 {
3490 off = 0;
3491 shndx_load_seg = *pshndx;
3492 }
3493 }
3494 }
3495 else
3496 {
3497 // Verify that the file headers fall into the first segment.
3498 if (load_seg != NULL && load_seg != *p)
3499 gold_unreachable();
3500 load_seg = NULL;
3501 }
75f65a3e 3502
756ac4a8
ILT
3503 bool are_addresses_set = (*p)->are_addresses_set();
3504 if (are_addresses_set)
3505 {
3506 // When it comes to setting file offsets, we care about
3507 // the physical address.
3508 addr = (*p)->paddr();
3509 }
9590bf25 3510 else if (parameters->options().user_set_Ttext()
ebacd51e 3511 && (parameters->options().omagic()
a3ed37d8
RM
3512 || is_text_segment(target, *p)))
3513 {
3514 are_addresses_set = true;
3515 }
3516 else if (parameters->options().user_set_Trodata_segment()
3517 && ((*p)->flags() & (elfcpp::PF_W | elfcpp::PF_X)) == 0)
9590bf25 3518 {
a3ed37d8 3519 addr = parameters->options().Trodata_segment();
9590bf25
CC
3520 are_addresses_set = true;
3521 }
e55bde5e 3522 else if (parameters->options().user_set_Tdata()
756ac4a8 3523 && ((*p)->flags() & elfcpp::PF_W) != 0
e55bde5e 3524 && (!parameters->options().user_set_Tbss()
756ac4a8
ILT
3525 || (*p)->has_any_data_sections()))
3526 {
e55bde5e 3527 addr = parameters->options().Tdata();
756ac4a8
ILT
3528 are_addresses_set = true;
3529 }
e55bde5e 3530 else if (parameters->options().user_set_Tbss()
756ac4a8
ILT
3531 && ((*p)->flags() & elfcpp::PF_W) != 0
3532 && !(*p)->has_any_data_sections())
3533 {
e55bde5e 3534 addr = parameters->options().Tbss();
756ac4a8
ILT
3535 are_addresses_set = true;
3536 }
3537
75f65a3e
ILT
3538 uint64_t orig_addr = addr;
3539 uint64_t orig_off = off;
3540
a445fddf 3541 uint64_t aligned_addr = 0;
75f65a3e 3542 uint64_t abi_pagesize = target->abi_pagesize();
af6156ef 3543 uint64_t common_pagesize = target->common_pagesize();
0496d5e5 3544
af6156ef
ILT
3545 if (!parameters->options().nmagic()
3546 && !parameters->options().omagic())
a1373b60 3547 (*p)->set_minimum_p_align(abi_pagesize);
0496d5e5 3548
8a5e3e08 3549 if (!are_addresses_set)
a445fddf 3550 {
a6577478
RÁE
3551 // Skip the address forward one page, maintaining the same
3552 // position within the page. This lets us store both segments
3553 // overlapping on a single page in the file, but the loader will
3554 // put them on different pages in memory. We will revisit this
3555 // decision once we know the size of the segment.
a445fddf 3556
a5cd8f05
CC
3557 uint64_t max_align = (*p)->maximum_alignment();
3558 if (max_align > abi_pagesize)
3559 addr = align_address(addr, max_align);
75f65a3e 3560 aligned_addr = addr;
a445fddf 3561
2e702c99
RM
3562 if (load_seg == *p)
3563 {
3564 // This is the segment that will contain the file
3565 // headers, so its offset will have to be exactly zero.
3566 gold_assert(orig_off == 0);
3567
3568 // If the target wants a fixed minimum distance from the
3569 // text segment to the read-only segment, move up now.
bbc5ae17
RM
3570 uint64_t min_addr =
3571 start_addr + (parameters->options().user_set_rosegment_gap()
3572 ? parameters->options().rosegment_gap()
3573 : target->rosegment_gap());
2e702c99
RM
3574 if (addr < min_addr)
3575 addr = min_addr;
3576
3577 // But this is not the first segment! To make its
3578 // address congruent with its offset, that address better
3579 // be aligned to the ABI-mandated page size.
3580 addr = align_address(addr, abi_pagesize);
3581 aligned_addr = addr;
3582 }
3583 else
3584 {
3585 if ((addr & (abi_pagesize - 1)) != 0)
3586 addr = addr + abi_pagesize;
a445fddf 3587
2e702c99
RM
3588 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
3589 }
75f65a3e
ILT
3590 }
3591
8a5e3e08
ILT
3592 if (!parameters->options().nmagic()
3593 && !parameters->options().omagic())
7fb47cc9
CC
3594 {
3595 // Here we are also taking care of the case when
3596 // the maximum segment alignment is larger than the page size.
3597 off = align_file_offset(off, addr,
3598 std::max(abi_pagesize,
3599 (*p)->maximum_alignment()));
3600 }
2e702c99 3601 else
661be1e2
ILT
3602 {
3603 // This is -N or -n with a section script which prevents
3604 // us from using a load segment. We need to ensure that
3605 // the file offset is aligned to the alignment of the
3606 // segment. This is because the linker script
3607 // implicitly assumed a zero offset. If we don't align
3608 // here, then the alignment of the sections in the
3609 // linker script may not match the alignment of the
3610 // sections in the set_section_addresses call below,
3611 // causing an error about dot moving backward.
3612 off = align_address(off, (*p)->maximum_alignment());
3613 }
8a5e3e08 3614
ead1e424 3615 unsigned int shndx_hold = *pshndx;
fc497986 3616 bool has_relro = false;
eb426534
RM
3617 uint64_t new_addr = (*p)->set_section_addresses(target, this,
3618 false, addr,
fd064a5b 3619 &increase_relro,
fc497986 3620 &has_relro,
2e702c99 3621 &off, pshndx);
75f65a3e
ILT
3622
3623 // Now that we know the size of this segment, we may be able
3624 // to save a page in memory, at the cost of wasting some
3625 // file space, by instead aligning to the start of a new
3626 // page. Here we use the real machine page size rather than
fc497986
CC
3627 // the ABI mandated page size. If the segment has been
3628 // aligned so that the relro data ends at a page boundary,
3629 // we do not try to realign it.
75f65a3e 3630
cdc29364
CC
3631 if (!are_addresses_set
3632 && !has_relro
3633 && aligned_addr != addr
fb0e076f 3634 && !parameters->incremental())
75f65a3e 3635 {
75f65a3e
ILT
3636 uint64_t first_off = (common_pagesize
3637 - (aligned_addr
3638 & (common_pagesize - 1)));
3639 uint64_t last_off = new_addr & (common_pagesize - 1);
3640 if (first_off > 0
3641 && last_off > 0
3642 && ((aligned_addr & ~ (common_pagesize - 1))
3643 != (new_addr & ~ (common_pagesize - 1)))
3644 && first_off + last_off <= common_pagesize)
3645 {
ead1e424
ILT
3646 *pshndx = shndx_hold;
3647 addr = align_address(aligned_addr, common_pagesize);
a445fddf 3648 addr = align_address(addr, (*p)->maximum_alignment());
815a1205
AM
3649 if ((addr & (abi_pagesize - 1)) != 0)
3650 addr = addr + abi_pagesize;
75f65a3e 3651 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
8a5e3e08 3652 off = align_file_offset(off, addr, abi_pagesize);
3bb951e5
ILT
3653
3654 increase_relro = this->increase_relro_;
3655 if (this->script_options_->saw_sections_clause())
3656 increase_relro = 0;
3657 has_relro = false;
3658
eb426534
RM
3659 new_addr = (*p)->set_section_addresses(target, this,
3660 true, addr,
fd064a5b 3661 &increase_relro,
fc497986 3662 &has_relro,
2e702c99 3663 &off, pshndx);
75f65a3e
ILT
3664 }
3665 }
3666
3667 addr = new_addr;
3668
34810851
ILT
3669 // Implement --check-sections. We know that the segments
3670 // are sorted by LMA.
3671 if (check_sections && last_load_segment != NULL)
3672 {
3673 gold_assert(last_load_segment->paddr() <= (*p)->paddr());
3674 if (last_load_segment->paddr() + last_load_segment->memsz()
3675 > (*p)->paddr())
3676 {
3677 unsigned long long lb1 = last_load_segment->paddr();
3678 unsigned long long le1 = lb1 + last_load_segment->memsz();
3679 unsigned long long lb2 = (*p)->paddr();
3680 unsigned long long le2 = lb2 + (*p)->memsz();
3681 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
3682 "[0x%llx -> 0x%llx]"),
3683 lb1, le1, lb2, le2);
3684 }
3685 }
3686 last_load_segment = *p;
75f65a3e
ILT
3687 }
3688 }
3689
2e702c99
RM
3690 if (load_seg != NULL && target->isolate_execinstr())
3691 {
3692 // Process the early segments again, setting their file offsets
3693 // so they land after the segments starting at LOAD_SEG.
3694 off = align_file_offset(off, 0, target->abi_pagesize());
3695
eb426534
RM
3696 this->reset_relax_output();
3697
2e702c99
RM
3698 for (Segment_list::iterator p = this->segment_list_.begin();
3699 *p != load_seg;
3700 ++p)
3701 {
3702 if ((*p)->type() == elfcpp::PT_LOAD)
3703 {
3704 // We repeat the whole job of assigning addresses and
3705 // offsets, but we really only want to change the offsets and
3706 // must ensure that the addresses all come out the same as
3707 // they did the first time through.
3708 bool has_relro = false;
3709 const uint64_t old_addr = (*p)->vaddr();
3710 const uint64_t old_end = old_addr + (*p)->memsz();
eb426534
RM
3711 uint64_t new_addr = (*p)->set_section_addresses(target, this,
3712 true, old_addr,
2e702c99
RM
3713 &increase_relro,
3714 &has_relro,
3715 &off,
3716 &shndx_begin);
3717 gold_assert(new_addr == old_end);
3718 }
3719 }
3720
3721 gold_assert(shndx_begin == shndx_load_seg);
3722 }
3723
75f65a3e
ILT
3724 // Handle the non-PT_LOAD segments, setting their offsets from their
3725 // section's offsets.
3726 for (Segment_list::iterator p = this->segment_list_.begin();
3727 p != this->segment_list_.end();
3728 ++p)
3729 {
1130c90e
RM
3730 // PT_GNU_STACK was set up correctly when it was created.
3731 if ((*p)->type() != elfcpp::PT_LOAD
3732 && (*p)->type() != elfcpp::PT_GNU_STACK)
1a2dff53
ILT
3733 (*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
3734 ? increase_relro
3735 : 0);
75f65a3e
ILT
3736 }
3737
7bf1f802
ILT
3738 // Set the TLS offsets for each section in the PT_TLS segment.
3739 if (this->tls_segment_ != NULL)
3740 this->tls_segment_->set_tls_offsets();
3741
75f65a3e
ILT
3742 return off;
3743}
3744
6a74a719
ILT
3745// Set the offsets of all the allocated sections when doing a
3746// relocatable link. This does the same jobs as set_segment_offsets,
3747// only for a relocatable link.
3748
3749off_t
3750Layout::set_relocatable_section_offsets(Output_data* file_header,
ca09d69a 3751 unsigned int* pshndx)
6a74a719
ILT
3752{
3753 off_t off = 0;
3754
3755 file_header->set_address_and_file_offset(0, 0);
3756 off += file_header->data_size();
3757
3758 for (Section_list::iterator p = this->section_list_.begin();
3759 p != this->section_list_.end();
3760 ++p)
3761 {
3762 // We skip unallocated sections here, except that group sections
3763 // have to come first.
3764 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
3765 && (*p)->type() != elfcpp::SHT_GROUP)
3766 continue;
3767
3768 off = align_address(off, (*p)->addralign());
3769
3770 // The linker script might have set the address.
3771 if (!(*p)->is_address_valid())
3772 (*p)->set_address(0);
3773 (*p)->set_file_offset(off);
3774 (*p)->finalize_data_size();
e79c84aa
CC
3775 if ((*p)->type() != elfcpp::SHT_NOBITS)
3776 off += (*p)->data_size();
6a74a719
ILT
3777
3778 (*p)->set_out_shndx(*pshndx);
3779 ++*pshndx;
3780 }
3781
3782 return off;
3783}
3784
75f65a3e
ILT
3785// Set the file offset of all the sections not associated with a
3786// segment.
3787
3788off_t
9a0910c3 3789Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
75f65a3e 3790{
cdc29364
CC
3791 off_t startoff = off;
3792 off_t maxoff = off;
3793
a3ad94ed
ILT
3794 for (Section_list::iterator p = this->unattached_section_list_.begin();
3795 p != this->unattached_section_list_.end();
75f65a3e
ILT
3796 ++p)
3797 {
27bc2bce
ILT
3798 // The symtab section is handled in create_symtab_sections.
3799 if (*p == this->symtab_section_)
61ba1cf9 3800 continue;
27bc2bce 3801
a9a60db6
ILT
3802 // If we've already set the data size, don't set it again.
3803 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
3804 continue;
3805
96803768
ILT
3806 if (pass == BEFORE_INPUT_SECTIONS_PASS
3807 && (*p)->requires_postprocessing())
17a1d0a9
ILT
3808 {
3809 (*p)->create_postprocessing_buffer();
3810 this->any_postprocessing_sections_ = true;
3811 }
96803768 3812
9a0910c3 3813 if (pass == BEFORE_INPUT_SECTIONS_PASS
2e702c99
RM
3814 && (*p)->after_input_sections())
3815 continue;
17a1d0a9 3816 else if (pass == POSTPROCESSING_SECTIONS_PASS
2e702c99
RM
3817 && (!(*p)->after_input_sections()
3818 || (*p)->type() == elfcpp::SHT_STRTAB))
3819 continue;
17a1d0a9 3820 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2e702c99
RM
3821 && (!(*p)->after_input_sections()
3822 || (*p)->type() != elfcpp::SHT_STRTAB))
3823 continue;
27bc2bce 3824
cdc29364
CC
3825 if (!parameters->incremental_update())
3826 {
3827 off = align_address(off, (*p)->addralign());
3828 (*p)->set_file_offset(off);
3829 (*p)->finalize_data_size();
3830 }
3831 else
3832 {
3833 // Incremental update: allocate file space from free list.
3834 (*p)->pre_finalize_data_size();
3835 off_t current_size = (*p)->current_data_size();
3836 off = this->allocate(current_size, (*p)->addralign(), startoff);
3837 if (off == -1)
3838 {
3839 if (is_debugging_enabled(DEBUG_INCREMENTAL))
2e702c99 3840 this->free_list_.dump();
cdc29364 3841 gold_assert((*p)->output_section() != NULL);
e6455dfb
CC
3842 gold_fallback(_("out of patch space for section %s; "
3843 "relink with --incremental-full"),
3844 (*p)->output_section()->name());
cdc29364
CC
3845 }
3846 (*p)->set_file_offset(off);
3847 (*p)->finalize_data_size();
3848 if ((*p)->data_size() > current_size)
3849 {
3850 gold_assert((*p)->output_section() != NULL);
e6455dfb
CC
3851 gold_fallback(_("%s: section changed size; "
3852 "relink with --incremental-full"),
3853 (*p)->output_section()->name());
cdc29364
CC
3854 }
3855 gold_debug(DEBUG_INCREMENTAL,
3856 "set_section_offsets: %08lx %08lx %s",
3857 static_cast<long>(off),
3858 static_cast<long>((*p)->data_size()),
3859 ((*p)->output_section() != NULL
3860 ? (*p)->output_section()->name() : "(special)"));
3861 }
3862
75f65a3e 3863 off += (*p)->data_size();
cdc29364 3864 if (off > maxoff)
2e702c99 3865 maxoff = off;
96803768
ILT
3866
3867 // At this point the name must be set.
17a1d0a9 3868 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
96803768 3869 this->namepool_.add((*p)->name(), false, NULL);
75f65a3e 3870 }
cdc29364 3871 return maxoff;
75f65a3e
ILT
3872}
3873
86887060
ILT
3874// Set the section indexes of all the sections not associated with a
3875// segment.
3876
3877unsigned int
3878Layout::set_section_indexes(unsigned int shndx)
3879{
3880 for (Section_list::iterator p = this->unattached_section_list_.begin();
3881 p != this->unattached_section_list_.end();
3882 ++p)
3883 {
d491d34e
ILT
3884 if (!(*p)->has_out_shndx())
3885 {
3886 (*p)->set_out_shndx(shndx);
3887 ++shndx;
3888 }
86887060
ILT
3889 }
3890 return shndx;
3891}
3892
a445fddf
ILT
3893// Set the section addresses according to the linker script. This is
3894// only called when we see a SECTIONS clause. This returns the
3895// program segment which should hold the file header and segment
3896// headers, if any. It will return NULL if they should not be in a
3897// segment.
3898
3899Output_segment*
3900Layout::set_section_addresses_from_script(Symbol_table* symtab)
20e6d0d6
DK
3901{
3902 Script_sections* ss = this->script_options_->script_sections();
3903 gold_assert(ss->saw_sections_clause());
3904 return this->script_options_->set_section_addresses(symtab, this);
3905}
3906
3907// Place the orphan sections in the linker script.
3908
3909void
3910Layout::place_orphan_sections_in_script()
a445fddf
ILT
3911{
3912 Script_sections* ss = this->script_options_->script_sections();
3913 gold_assert(ss->saw_sections_clause());
3914
3915 // Place each orphaned output section in the script.
3916 for (Section_list::iterator p = this->section_list_.begin();
3917 p != this->section_list_.end();
3918 ++p)
3919 {
3920 if (!(*p)->found_in_sections_clause())
3921 ss->place_orphan(*p);
3922 }
a445fddf
ILT
3923}
3924
7bf1f802
ILT
3925// Count the local symbols in the regular symbol table and the dynamic
3926// symbol table, and build the respective string pools.
3927
3928void
17a1d0a9
ILT
3929Layout::count_local_symbols(const Task* task,
3930 const Input_objects* input_objects)
7bf1f802 3931{
6d013333
ILT
3932 // First, figure out an upper bound on the number of symbols we'll
3933 // be inserting into each pool. This helps us create the pools with
3934 // the right size, to avoid unnecessary hashtable resizing.
3935 unsigned int symbol_count = 0;
3936 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3937 p != input_objects->relobj_end();
3938 ++p)
3939 symbol_count += (*p)->local_symbol_count();
3940
3941 // Go from "upper bound" to "estimate." We overcount for two
3942 // reasons: we double-count symbols that occur in more than one
3943 // object file, and we count symbols that are dropped from the
3944 // output. Add it all together and assume we overcount by 100%.
3945 symbol_count /= 2;
3946
3947 // We assume all symbols will go into both the sympool and dynpool.
3948 this->sympool_.reserve(symbol_count);
3949 this->dynpool_.reserve(symbol_count);
3950
7bf1f802
ILT
3951 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3952 p != input_objects->relobj_end();
3953 ++p)
3954 {
17a1d0a9 3955 Task_lock_obj<Object> tlo(task, *p);
7bf1f802
ILT
3956 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
3957 }
3958}
3959
b8e6aad9
ILT
3960// Create the symbol table sections. Here we also set the final
3961// values of the symbols. At this point all the loadable sections are
d491d34e 3962// fully laid out. SHNUM is the number of sections so far.
75f65a3e
ILT
3963
3964void
9025d29d 3965Layout::create_symtab_sections(const Input_objects* input_objects,
75f65a3e 3966 Symbol_table* symtab,
d491d34e 3967 unsigned int shnum,
16649710 3968 off_t* poff)
75f65a3e 3969{
61ba1cf9
ILT
3970 int symsize;
3971 unsigned int align;
8851ecca 3972 if (parameters->target().get_size() == 32)
61ba1cf9
ILT
3973 {
3974 symsize = elfcpp::Elf_sizes<32>::sym_size;
3975 align = 4;
3976 }
8851ecca 3977 else if (parameters->target().get_size() == 64)
61ba1cf9
ILT
3978 {
3979 symsize = elfcpp::Elf_sizes<64>::sym_size;
3980 align = 8;
3981 }
3982 else
a3ad94ed 3983 gold_unreachable();
61ba1cf9 3984
cdc29364
CC
3985 // Compute file offsets relative to the start of the symtab section.
3986 off_t off = 0;
61ba1cf9
ILT
3987
3988 // Save space for the dummy symbol at the start of the section. We
3989 // never bother to write this out--it will just be left as zero.
3990 off += symsize;
c06b7b0b 3991 unsigned int local_symbol_index = 1;
61ba1cf9 3992
a3ad94ed
ILT
3993 // Add STT_SECTION symbols for each Output section which needs one.
3994 for (Section_list::iterator p = this->section_list_.begin();
3995 p != this->section_list_.end();
3996 ++p)
3997 {
3998 if (!(*p)->needs_symtab_index())
3999 (*p)->set_symtab_index(-1U);
4000 else
4001 {
4002 (*p)->set_symtab_index(local_symbol_index);
4003 ++local_symbol_index;
4004 off += symsize;
4005 }
4006 }
4007
f6ce93d6
ILT
4008 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4009 p != input_objects->relobj_end();
75f65a3e
ILT
4010 ++p)
4011 {
c06b7b0b 4012 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
2e702c99 4013 off, symtab);
c06b7b0b
ILT
4014 off += (index - local_symbol_index) * symsize;
4015 local_symbol_index = index;
75f65a3e
ILT
4016 }
4017
c06b7b0b 4018 unsigned int local_symcount = local_symbol_index;
cdc29364 4019 gold_assert(static_cast<off_t>(local_symcount * symsize) == off);
61ba1cf9 4020
16649710
ILT
4021 off_t dynoff;
4022 size_t dyn_global_index;
4023 size_t dyncount;
4024 if (this->dynsym_section_ == NULL)
4025 {
4026 dynoff = 0;
4027 dyn_global_index = 0;
4028 dyncount = 0;
4029 }
4030 else
4031 {
4032 dyn_global_index = this->dynsym_section_->info();
4033 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
4034 dynoff = this->dynsym_section_->offset() + locsize;
4035 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
f5c3f225 4036 gold_assert(static_cast<off_t>(dyncount * symsize)
16649710
ILT
4037 == this->dynsym_section_->data_size() - locsize);
4038 }
4039
cdc29364 4040 off_t global_off = off;
55a93433
ILT
4041 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
4042 &this->sympool_, &local_symcount);
75f65a3e 4043
8851ecca 4044 if (!parameters->options().strip_all())
9e2dcb77
ILT
4045 {
4046 this->sympool_.set_string_offsets();
61ba1cf9 4047
cfd73a4e 4048 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
9e2dcb77
ILT
4049 Output_section* osymtab = this->make_output_section(symtab_name,
4050 elfcpp::SHT_SYMTAB,
22f0da72
ILT
4051 0, ORDER_INVALID,
4052 false);
9e2dcb77 4053 this->symtab_section_ = osymtab;
a3ad94ed 4054
cdc29364 4055 Output_section_data* pos = new Output_data_fixed_space(off, align,
7d9e3d98 4056 "** symtab");
9e2dcb77 4057 osymtab->add_output_section_data(pos);
61ba1cf9 4058
d491d34e
ILT
4059 // We generate a .symtab_shndx section if we have more than
4060 // SHN_LORESERVE sections. Technically it is possible that we
4061 // don't need one, because it is possible that there are no
4062 // symbols in any of sections with indexes larger than
4063 // SHN_LORESERVE. That is probably unusual, though, and it is
4064 // easier to always create one than to compute section indexes
4065 // twice (once here, once when writing out the symbols).
4066 if (shnum >= elfcpp::SHN_LORESERVE)
4067 {
4068 const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
4069 false, NULL);
4070 Output_section* osymtab_xindex =
4071 this->make_output_section(symtab_xindex_name,
22f0da72
ILT
4072 elfcpp::SHT_SYMTAB_SHNDX, 0,
4073 ORDER_INVALID, false);
d491d34e 4074
cdc29364 4075 size_t symcount = off / symsize;
d491d34e
ILT
4076 this->symtab_xindex_ = new Output_symtab_xindex(symcount);
4077
4078 osymtab_xindex->add_output_section_data(this->symtab_xindex_);
4079
4080 osymtab_xindex->set_link_section(osymtab);
4081 osymtab_xindex->set_addralign(4);
4082 osymtab_xindex->set_entsize(4);
4083
4084 osymtab_xindex->set_after_input_sections();
4085
4086 // This tells the driver code to wait until the symbol table
4087 // has written out before writing out the postprocessing
4088 // sections, including the .symtab_shndx section.
4089 this->any_postprocessing_sections_ = true;
4090 }
4091
cfd73a4e 4092 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
9e2dcb77
ILT
4093 Output_section* ostrtab = this->make_output_section(strtab_name,
4094 elfcpp::SHT_STRTAB,
22f0da72
ILT
4095 0, ORDER_INVALID,
4096 false);
a3ad94ed 4097
9e2dcb77
ILT
4098 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
4099 ostrtab->add_output_section_data(pstr);
61ba1cf9 4100
cdc29364
CC
4101 off_t symtab_off;
4102 if (!parameters->incremental_update())
4103 symtab_off = align_address(*poff, align);
4104 else
4105 {
4106 symtab_off = this->allocate(off, align, *poff);
2e702c99 4107 if (off == -1)
e6455dfb
CC
4108 gold_fallback(_("out of patch space for symbol table; "
4109 "relink with --incremental-full"));
cdc29364
CC
4110 gold_debug(DEBUG_INCREMENTAL,
4111 "create_symtab_sections: %08lx %08lx .symtab",
4112 static_cast<long>(symtab_off),
4113 static_cast<long>(off));
4114 }
4115
4116 symtab->set_file_offset(symtab_off + global_off);
4117 osymtab->set_file_offset(symtab_off);
27bc2bce 4118 osymtab->finalize_data_size();
9e2dcb77
ILT
4119 osymtab->set_link_section(ostrtab);
4120 osymtab->set_info(local_symcount);
4121 osymtab->set_entsize(symsize);
61ba1cf9 4122
cdc29364
CC
4123 if (symtab_off + off > *poff)
4124 *poff = symtab_off + off;
9e2dcb77 4125 }
75f65a3e
ILT
4126}
4127
4128// Create the .shstrtab section, which holds the names of the
4129// sections. At the time this is called, we have created all the
4130// output sections except .shstrtab itself.
4131
4132Output_section*
4133Layout::create_shstrtab()
4134{
4135 // FIXME: We don't need to create a .shstrtab section if we are
4136 // stripping everything.
4137
cfd73a4e 4138 const char* name = this->namepool_.add(".shstrtab", false, NULL);
75f65a3e 4139
f5c870d2 4140 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
22f0da72 4141 ORDER_INVALID, false);
75f65a3e 4142
0e0d5469
ILT
4143 if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
4144 {
4145 // We can't write out this section until we've set all the
4146 // section names, and we don't set the names of compressed
4147 // output sections until relocations are complete. FIXME: With
4148 // the current names we use, this is unnecessary.
4149 os->set_after_input_sections();
4150 }
27bc2bce 4151
a3ad94ed
ILT
4152 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
4153 os->add_output_section_data(posd);
75f65a3e
ILT
4154
4155 return os;
4156}
4157
4158// Create the section headers. SIZE is 32 or 64. OFF is the file
4159// offset.
4160
27bc2bce 4161void
d491d34e 4162Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
75f65a3e
ILT
4163{
4164 Output_section_headers* oshdrs;
9025d29d 4165 oshdrs = new Output_section_headers(this,
16649710 4166 &this->segment_list_,
6a74a719 4167 &this->section_list_,
16649710 4168 &this->unattached_section_list_,
d491d34e
ILT
4169 &this->namepool_,
4170 shstrtab_section);
cdc29364
CC
4171 off_t off;
4172 if (!parameters->incremental_update())
4173 off = align_address(*poff, oshdrs->addralign());
4174 else
4175 {
4176 oshdrs->pre_finalize_data_size();
4177 off = this->allocate(oshdrs->data_size(), oshdrs->addralign(), *poff);
4178 if (off == -1)
e6455dfb
CC
4179 gold_fallback(_("out of patch space for section header table; "
4180 "relink with --incremental-full"));
cdc29364
CC
4181 gold_debug(DEBUG_INCREMENTAL,
4182 "create_shdrs: %08lx %08lx (section header table)",
4183 static_cast<long>(off),
4184 static_cast<long>(off + oshdrs->data_size()));
4185 }
27bc2bce 4186 oshdrs->set_address_and_file_offset(0, off);
61ba1cf9 4187 off += oshdrs->data_size();
cdc29364
CC
4188 if (off > *poff)
4189 *poff = off;
27bc2bce 4190 this->section_headers_ = oshdrs;
54dc6425
ILT
4191}
4192
d491d34e
ILT
4193// Count the allocated sections.
4194
4195size_t
4196Layout::allocated_output_section_count() const
4197{
4198 size_t section_count = 0;
4199 for (Segment_list::const_iterator p = this->segment_list_.begin();
4200 p != this->segment_list_.end();
4201 ++p)
4202 section_count += (*p)->output_section_count();
4203 return section_count;
4204}
4205
dbe717ef
ILT
4206// Create the dynamic symbol table.
4207
4208void
7bf1f802 4209Layout::create_dynamic_symtab(const Input_objects* input_objects,
2e702c99 4210 Symbol_table* symtab,
ca09d69a 4211 Output_section** pdynstr,
14b31740
ILT
4212 unsigned int* plocal_dynamic_count,
4213 std::vector<Symbol*>* pdynamic_symbols,
4214 Versions* pversions)
dbe717ef 4215{
a3ad94ed
ILT
4216 // Count all the symbols in the dynamic symbol table, and set the
4217 // dynamic symbol indexes.
dbe717ef 4218
a3ad94ed
ILT
4219 // Skip symbol 0, which is always all zeroes.
4220 unsigned int index = 1;
dbe717ef 4221
a3ad94ed
ILT
4222 // Add STT_SECTION symbols for each Output section which needs one.
4223 for (Section_list::iterator p = this->section_list_.begin();
4224 p != this->section_list_.end();
4225 ++p)
4226 {
4227 if (!(*p)->needs_dynsym_index())
4228 (*p)->set_dynsym_index(-1U);
4229 else
4230 {
4231 (*p)->set_dynsym_index(index);
4232 ++index;
4233 }
4234 }
4235
7bf1f802
ILT
4236 // Count the local symbols that need to go in the dynamic symbol table,
4237 // and set the dynamic symbol indexes.
4238 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4239 p != input_objects->relobj_end();
4240 ++p)
4241 {
4242 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
4243 index = new_index;
4244 }
a3ad94ed
ILT
4245
4246 unsigned int local_symcount = index;
14b31740 4247 *plocal_dynamic_count = local_symcount;
a3ad94ed 4248
9b07f471 4249 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
35cdfc9a 4250 &this->dynpool_, pversions);
a3ad94ed
ILT
4251
4252 int symsize;
4253 unsigned int align;
8851ecca 4254 const int size = parameters->target().get_size();
a3ad94ed
ILT
4255 if (size == 32)
4256 {
4257 symsize = elfcpp::Elf_sizes<32>::sym_size;
4258 align = 4;
4259 }
4260 else if (size == 64)
4261 {
4262 symsize = elfcpp::Elf_sizes<64>::sym_size;
4263 align = 8;
4264 }
4265 else
4266 gold_unreachable();
4267
14b31740
ILT
4268 // Create the dynamic symbol table section.
4269
3802b2dd
ILT
4270 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
4271 elfcpp::SHT_DYNSYM,
4272 elfcpp::SHF_ALLOC,
22f0da72
ILT
4273 false,
4274 ORDER_DYNAMIC_LINKER,
4275 false);
a3ad94ed 4276
6daf5215
ILT
4277 // Check for NULL as a linker script may discard .dynsym.
4278 if (dynsym != NULL)
4279 {
4280 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
4281 align,
4282 "** dynsym");
4283 dynsym->add_output_section_data(odata);
a3ad94ed 4284
6daf5215
ILT
4285 dynsym->set_info(local_symcount);
4286 dynsym->set_entsize(symsize);
4287 dynsym->set_addralign(align);
a3ad94ed 4288
6daf5215
ILT
4289 this->dynsym_section_ = dynsym;
4290 }
a3ad94ed 4291
16649710 4292 Output_data_dynamic* const odyn = this->dynamic_data_;
6daf5215
ILT
4293 if (odyn != NULL)
4294 {
4295 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
4296 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
4297 }
a3ad94ed 4298
d491d34e
ILT
4299 // If there are more than SHN_LORESERVE allocated sections, we
4300 // create a .dynsym_shndx section. It is possible that we don't
4301 // need one, because it is possible that there are no dynamic
4302 // symbols in any of the sections with indexes larger than
4303 // SHN_LORESERVE. This is probably unusual, though, and at this
4304 // time we don't know the actual section indexes so it is
4305 // inconvenient to check.
4306 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
4307 {
2ea97941 4308 Output_section* dynsym_xindex =
d491d34e
ILT
4309 this->choose_output_section(NULL, ".dynsym_shndx",
4310 elfcpp::SHT_SYMTAB_SHNDX,
4311 elfcpp::SHF_ALLOC,
22f0da72 4312 false, ORDER_DYNAMIC_LINKER, false);
d491d34e 4313
6daf5215
ILT
4314 if (dynsym_xindex != NULL)
4315 {
4316 this->dynsym_xindex_ = new Output_symtab_xindex(index);
d491d34e 4317
6daf5215 4318 dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
d491d34e 4319
6daf5215
ILT
4320 dynsym_xindex->set_link_section(dynsym);
4321 dynsym_xindex->set_addralign(4);
4322 dynsym_xindex->set_entsize(4);
d491d34e 4323
6daf5215 4324 dynsym_xindex->set_after_input_sections();
d491d34e 4325
6daf5215
ILT
4326 // This tells the driver code to wait until the symbol table
4327 // has written out before writing out the postprocessing
4328 // sections, including the .dynsym_shndx section.
4329 this->any_postprocessing_sections_ = true;
4330 }
d491d34e
ILT
4331 }
4332
14b31740
ILT
4333 // Create the dynamic string table section.
4334
3802b2dd
ILT
4335 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
4336 elfcpp::SHT_STRTAB,
4337 elfcpp::SHF_ALLOC,
22f0da72
ILT
4338 false,
4339 ORDER_DYNAMIC_LINKER,
4340 false);
117be58f 4341 *pdynstr = dynstr;
6daf5215
ILT
4342 if (dynstr != NULL)
4343 {
4344 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
4345 dynstr->add_output_section_data(strdata);
a3ad94ed 4346
6daf5215
ILT
4347 if (dynsym != NULL)
4348 dynsym->set_link_section(dynstr);
4349 if (this->dynamic_section_ != NULL)
4350 this->dynamic_section_->set_link_section(dynstr);
16649710 4351
6daf5215
ILT
4352 if (odyn != NULL)
4353 {
4354 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
4355 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
4356 }
6daf5215 4357 }
14b31740 4358
cd6da036
KC
4359 // Create the hash tables. The Gnu-style hash table must be
4360 // built first, because it changes the order of the symbols
4361 // in the dynamic symbol table.
14b31740 4362
cd6da036 4363 if (strcmp(parameters->options().hash_style(), "gnu") == 0
13670ee6
ILT
4364 || strcmp(parameters->options().hash_style(), "both") == 0)
4365 {
4366 unsigned char* phash;
4367 unsigned int hashlen;
cd6da036 4368 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
13670ee6
ILT
4369 &phash, &hashlen);
4370
22f0da72 4371 Output_section* hashsec =
cd6da036 4372 this->choose_output_section(NULL, ".gnu.hash", elfcpp::SHT_GNU_HASH,
22f0da72
ILT
4373 elfcpp::SHF_ALLOC, false,
4374 ORDER_DYNAMIC_LINKER, false);
13670ee6
ILT
4375
4376 Output_section_data* hashdata = new Output_data_const_buffer(phash,
4377 hashlen,
7d9e3d98
ILT
4378 align,
4379 "** hash");
6daf5215
ILT
4380 if (hashsec != NULL && hashdata != NULL)
4381 hashsec->add_output_section_data(hashdata);
13670ee6 4382
6daf5215
ILT
4383 if (hashsec != NULL)
4384 {
4385 if (dynsym != NULL)
4386 hashsec->set_link_section(dynsym);
a3ad94ed 4387
cd6da036
KC
4388 // For a 64-bit target, the entries in .gnu.hash do not have
4389 // a uniform size, so we only set the entry size for a
4390 // 32-bit target.
4391 if (parameters->target().get_size() == 32)
4392 hashsec->set_entsize(4);
4393
4394 if (odyn != NULL)
4395 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
4396 }
13670ee6
ILT
4397 }
4398
cd6da036 4399 if (strcmp(parameters->options().hash_style(), "sysv") == 0
13670ee6
ILT
4400 || strcmp(parameters->options().hash_style(), "both") == 0)
4401 {
4402 unsigned char* phash;
4403 unsigned int hashlen;
cd6da036 4404 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
13670ee6 4405 &phash, &hashlen);
a3ad94ed 4406
22f0da72 4407 Output_section* hashsec =
cd6da036 4408 this->choose_output_section(NULL, ".hash", elfcpp::SHT_HASH,
22f0da72
ILT
4409 elfcpp::SHF_ALLOC, false,
4410 ORDER_DYNAMIC_LINKER, false);
a3ad94ed 4411
13670ee6
ILT
4412 Output_section_data* hashdata = new Output_data_const_buffer(phash,
4413 hashlen,
7d9e3d98
ILT
4414 align,
4415 "** hash");
6daf5215
ILT
4416 if (hashsec != NULL && hashdata != NULL)
4417 hashsec->add_output_section_data(hashdata);
a3ad94ed 4418
6daf5215
ILT
4419 if (hashsec != NULL)
4420 {
4421 if (dynsym != NULL)
4422 hashsec->set_link_section(dynsym);
8d9743bd 4423 hashsec->set_entsize(parameters->target().hash_entry_size() / 8);
6daf5215 4424 }
cd6da036
KC
4425
4426 if (odyn != NULL)
4427 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
13670ee6 4428 }
dbe717ef
ILT
4429}
4430
7bf1f802
ILT
4431// Assign offsets to each local portion of the dynamic symbol table.
4432
4433void
4434Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
4435{
4436 Output_section* dynsym = this->dynsym_section_;
6daf5215
ILT
4437 if (dynsym == NULL)
4438 return;
7bf1f802
ILT
4439
4440 off_t off = dynsym->offset();
4441
4442 // Skip the dummy symbol at the start of the section.
4443 off += dynsym->entsize();
4444
4445 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4446 p != input_objects->relobj_end();
4447 ++p)
4448 {
4449 unsigned int count = (*p)->set_local_dynsym_offset(off);
4450 off += count * dynsym->entsize();
4451 }
4452}
4453
14b31740
ILT
4454// Create the version sections.
4455
4456void
9025d29d 4457Layout::create_version_sections(const Versions* versions,
46fe1623 4458 const Symbol_table* symtab,
14b31740
ILT
4459 unsigned int local_symcount,
4460 const std::vector<Symbol*>& dynamic_symbols,
4461 const Output_section* dynstr)
4462{
4463 if (!versions->any_defs() && !versions->any_needs())
4464 return;
4465
8851ecca 4466 switch (parameters->size_and_endianness())
14b31740 4467 {
193a53d9 4468#ifdef HAVE_TARGET_32_LITTLE
8851ecca 4469 case Parameters::TARGET_32_LITTLE:
7d1a9ebb
ILT
4470 this->sized_create_version_sections<32, false>(versions, symtab,
4471 local_symcount,
4472 dynamic_symbols, dynstr);
8851ecca 4473 break;
193a53d9 4474#endif
8851ecca
ILT
4475#ifdef HAVE_TARGET_32_BIG
4476 case Parameters::TARGET_32_BIG:
7d1a9ebb
ILT
4477 this->sized_create_version_sections<32, true>(versions, symtab,
4478 local_symcount,
4479 dynamic_symbols, dynstr);
8851ecca 4480 break;
193a53d9 4481#endif
193a53d9 4482#ifdef HAVE_TARGET_64_LITTLE
8851ecca 4483 case Parameters::TARGET_64_LITTLE:
7d1a9ebb
ILT
4484 this->sized_create_version_sections<64, false>(versions, symtab,
4485 local_symcount,
4486 dynamic_symbols, dynstr);
8851ecca 4487 break;
193a53d9 4488#endif
8851ecca
ILT
4489#ifdef HAVE_TARGET_64_BIG
4490 case Parameters::TARGET_64_BIG:
7d1a9ebb
ILT
4491 this->sized_create_version_sections<64, true>(versions, symtab,
4492 local_symcount,
4493 dynamic_symbols, dynstr);
8851ecca
ILT
4494 break;
4495#endif
4496 default:
4497 gold_unreachable();
14b31740 4498 }
14b31740
ILT
4499}
4500
4501// Create the version sections, sized version.
4502
4503template<int size, bool big_endian>
4504void
4505Layout::sized_create_version_sections(
4506 const Versions* versions,
46fe1623 4507 const Symbol_table* symtab,
14b31740
ILT
4508 unsigned int local_symcount,
4509 const std::vector<Symbol*>& dynamic_symbols,
7d1a9ebb 4510 const Output_section* dynstr)
14b31740 4511{
3802b2dd
ILT
4512 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
4513 elfcpp::SHT_GNU_versym,
4514 elfcpp::SHF_ALLOC,
22f0da72
ILT
4515 false,
4516 ORDER_DYNAMIC_LINKER,
4517 false);
14b31740 4518
6daf5215
ILT
4519 // Check for NULL since a linker script may discard this section.
4520 if (vsec != NULL)
4521 {
4522 unsigned char* vbuf;
4523 unsigned int vsize;
4524 versions->symbol_section_contents<size, big_endian>(symtab,
4525 &this->dynpool_,
4526 local_symcount,
4527 dynamic_symbols,
4528 &vbuf, &vsize);
4529
4530 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
4531 "** versions");
4532
4533 vsec->add_output_section_data(vdata);
4534 vsec->set_entsize(2);
4535 vsec->set_link_section(this->dynsym_section_);
4536 }
14b31740
ILT
4537
4538 Output_data_dynamic* const odyn = this->dynamic_data_;
6daf5215
ILT
4539 if (odyn != NULL && vsec != NULL)
4540 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
14b31740
ILT
4541
4542 if (versions->any_defs())
4543 {
3802b2dd 4544 Output_section* vdsec;
6daf5215
ILT
4545 vdsec = this->choose_output_section(NULL, ".gnu.version_d",
4546 elfcpp::SHT_GNU_verdef,
4547 elfcpp::SHF_ALLOC,
4548 false, ORDER_DYNAMIC_LINKER, false);
4549
4550 if (vdsec != NULL)
4551 {
4552 unsigned char* vdbuf;
4553 unsigned int vdsize;
4554 unsigned int vdentries;
4555 versions->def_section_contents<size, big_endian>(&this->dynpool_,
4556 &vdbuf, &vdsize,
4557 &vdentries);
4558
4559 Output_section_data* vddata =
4560 new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
4561
4562 vdsec->add_output_section_data(vddata);
4563 vdsec->set_link_section(dynstr);
4564 vdsec->set_info(vdentries);
4565
4566 if (odyn != NULL)
4567 {
4568 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
4569 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
4570 }
4571 }
14b31740
ILT
4572 }
4573
4574 if (versions->any_needs())
4575 {
14b31740 4576 Output_section* vnsec;
3802b2dd
ILT
4577 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
4578 elfcpp::SHT_GNU_verneed,
4579 elfcpp::SHF_ALLOC,
22f0da72 4580 false, ORDER_DYNAMIC_LINKER, false);
14b31740 4581
6daf5215
ILT
4582 if (vnsec != NULL)
4583 {
4584 unsigned char* vnbuf;
4585 unsigned int vnsize;
4586 unsigned int vnentries;
4587 versions->need_section_contents<size, big_endian>(&this->dynpool_,
4588 &vnbuf, &vnsize,
4589 &vnentries);
14b31740 4590
6daf5215
ILT
4591 Output_section_data* vndata =
4592 new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
14b31740 4593
6daf5215
ILT
4594 vnsec->add_output_section_data(vndata);
4595 vnsec->set_link_section(dynstr);
4596 vnsec->set_info(vnentries);
14b31740 4597
6daf5215
ILT
4598 if (odyn != NULL)
4599 {
4600 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
4601 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
4602 }
4603 }
14b31740
ILT
4604 }
4605}
4606
dbe717ef
ILT
4607// Create the .interp section and PT_INTERP segment.
4608
4609void
4610Layout::create_interp(const Target* target)
4611{
10b4f102
ILT
4612 gold_assert(this->interp_segment_ == NULL);
4613
e55bde5e 4614 const char* interp = parameters->options().dynamic_linker();
dbe717ef
ILT
4615 if (interp == NULL)
4616 {
4617 interp = target->dynamic_linker();
a3ad94ed 4618 gold_assert(interp != NULL);
dbe717ef
ILT
4619 }
4620
4621 size_t len = strlen(interp) + 1;
4622
4623 Output_section_data* odata = new Output_data_const(interp, len, 1);
4624
e1f74f98
ILT
4625 Output_section* osec = this->choose_output_section(NULL, ".interp",
4626 elfcpp::SHT_PROGBITS,
4627 elfcpp::SHF_ALLOC,
4628 false, ORDER_INTERP,
4629 false);
6daf5215
ILT
4630 if (osec != NULL)
4631 osec->add_output_section_data(odata);
dbe717ef
ILT
4632}
4633
ea715a34
ILT
4634// Add dynamic tags for the PLT and the dynamic relocs. This is
4635// called by the target-specific code. This does nothing if not doing
4636// a dynamic link.
4637
4638// USE_REL is true for REL relocs rather than RELA relocs.
4639
4640// If PLT_GOT is not NULL, then DT_PLTGOT points to it.
4641
4642// If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
e291e7b9
ILT
4643// and we also set DT_PLTREL. We use PLT_REL's output section, since
4644// some targets have multiple reloc sections in PLT_REL.
ea715a34
ILT
4645
4646// If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
67181c72
ILT
4647// DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT. Again we use the output
4648// section.
ea715a34
ILT
4649
4650// If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
4651// executable.
4652
4653void
4654Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
4655 const Output_data* plt_rel,
3a44184e 4656 const Output_data_reloc_generic* dyn_rel,
612a8d3d 4657 bool add_debug, bool dynrel_includes_plt)
ea715a34
ILT
4658{
4659 Output_data_dynamic* odyn = this->dynamic_data_;
4660 if (odyn == NULL)
4661 return;
4662
4663 if (plt_got != NULL && plt_got->output_section() != NULL)
4664 odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
4665
4666 if (plt_rel != NULL && plt_rel->output_section() != NULL)
4667 {
e291e7b9
ILT
4668 odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
4669 odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
ea715a34
ILT
4670 odyn->add_constant(elfcpp::DT_PLTREL,
4671 use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
4672 }
4673
82435b3b
ILT
4674 if ((dyn_rel != NULL && dyn_rel->output_section() != NULL)
4675 || (dynrel_includes_plt
4676 && plt_rel != NULL
4677 && plt_rel->output_section() != NULL))
ea715a34 4678 {
82435b3b
ILT
4679 bool have_dyn_rel = dyn_rel != NULL && dyn_rel->output_section() != NULL;
4680 bool have_plt_rel = plt_rel != NULL && plt_rel->output_section() != NULL;
ea715a34 4681 odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
82435b3b
ILT
4682 (have_dyn_rel
4683 ? dyn_rel->output_section()
4684 : plt_rel->output_section()));
4685 elfcpp::DT size_tag = use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ;
4686 if (have_dyn_rel && have_plt_rel && dynrel_includes_plt)
4687 odyn->add_section_size(size_tag,
67181c72
ILT
4688 dyn_rel->output_section(),
4689 plt_rel->output_section());
82435b3b
ILT
4690 else if (have_dyn_rel)
4691 odyn->add_section_size(size_tag, dyn_rel->output_section());
612a8d3d 4692 else
82435b3b 4693 odyn->add_section_size(size_tag, plt_rel->output_section());
ea715a34
ILT
4694 const int size = parameters->target().get_size();
4695 elfcpp::DT rel_tag;
4696 int rel_size;
4697 if (use_rel)
4698 {
4699 rel_tag = elfcpp::DT_RELENT;
4700 if (size == 32)
4701 rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
4702 else if (size == 64)
4703 rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
4704 else
4705 gold_unreachable();
4706 }
4707 else
4708 {
4709 rel_tag = elfcpp::DT_RELAENT;
4710 if (size == 32)
4711 rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
4712 else if (size == 64)
4713 rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
4714 else
4715 gold_unreachable();
4716 }
4717 odyn->add_constant(rel_tag, rel_size);
3a44184e 4718
82435b3b 4719 if (parameters->options().combreloc() && have_dyn_rel)
3a44184e
ILT
4720 {
4721 size_t c = dyn_rel->relative_reloc_count();
4722 if (c > 0)
4723 odyn->add_constant((use_rel
4724 ? elfcpp::DT_RELCOUNT
4725 : elfcpp::DT_RELACOUNT),
4726 c);
4727 }
ea715a34
ILT
4728 }
4729
4730 if (add_debug && !parameters->options().shared())
4731 {
4732 // The value of the DT_DEBUG tag is filled in by the dynamic
4733 // linker at run time, and used by the debugger.
4734 odyn->add_constant(elfcpp::DT_DEBUG, 0);
4735 }
4736}
4737
dc1c8a16
CC
4738void
4739Layout::add_target_specific_dynamic_tag(elfcpp::DT tag, unsigned int val)
4740{
4741 Output_data_dynamic* odyn = this->dynamic_data_;
4742 if (odyn == NULL)
4743 return;
4744 odyn->add_constant(tag, val);
4745}
4746
a3ad94ed
ILT
4747// Finish the .dynamic section and PT_DYNAMIC segment.
4748
4749void
4750Layout::finish_dynamic_section(const Input_objects* input_objects,
16649710 4751 const Symbol_table* symtab)
a3ad94ed 4752{
6daf5215
ILT
4753 if (!this->script_options_->saw_phdrs_clause()
4754 && this->dynamic_section_ != NULL)
1c4f3631
ILT
4755 {
4756 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
4757 (elfcpp::PF_R
4758 | elfcpp::PF_W));
22f0da72
ILT
4759 oseg->add_output_section_to_nonload(this->dynamic_section_,
4760 elfcpp::PF_R | elfcpp::PF_W);
1c4f3631 4761 }
a3ad94ed 4762
16649710 4763 Output_data_dynamic* const odyn = this->dynamic_data_;
6daf5215
ILT
4764 if (odyn == NULL)
4765 return;
16649710 4766
a3ad94ed
ILT
4767 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4768 p != input_objects->dynobj_end();
4769 ++p)
4770 {
0f1c85a6 4771 if (!(*p)->is_needed() && (*p)->as_needed())
594c8e5e
ILT
4772 {
4773 // This dynamic object was linked with --as-needed, but it
4774 // is not needed.
4775 continue;
4776 }
4777
a3ad94ed
ILT
4778 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
4779 }
4780
8851ecca 4781 if (parameters->options().shared())
fced7afd 4782 {
e55bde5e 4783 const char* soname = parameters->options().soname();
fced7afd
ILT
4784 if (soname != NULL)
4785 odyn->add_string(elfcpp::DT_SONAME, soname);
4786 }
4787
c6585162 4788 Symbol* sym = symtab->lookup(parameters->options().init());
14b31740 4789 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
4790 odyn->add_symbol(elfcpp::DT_INIT, sym);
4791
c6585162 4792 sym = symtab->lookup(parameters->options().fini());
14b31740 4793 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
a3ad94ed
ILT
4794 odyn->add_symbol(elfcpp::DT_FINI, sym);
4795
f15f61a7
DK
4796 // Look for .init_array, .preinit_array and .fini_array by checking
4797 // section types.
4798 for(Layout::Section_list::const_iterator p = this->section_list_.begin();
4799 p != this->section_list_.end();
4800 ++p)
4801 switch((*p)->type())
4802 {
4803 case elfcpp::SHT_FINI_ARRAY:
4804 odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
2e702c99 4805 odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
f15f61a7
DK
4806 break;
4807 case elfcpp::SHT_INIT_ARRAY:
4808 odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
2e702c99 4809 odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
f15f61a7
DK
4810 break;
4811 case elfcpp::SHT_PREINIT_ARRAY:
4812 odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
2e702c99 4813 odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
f15f61a7
DK
4814 break;
4815 default:
4816 break;
4817 }
2e702c99 4818
41f542e7 4819 // Add a DT_RPATH entry if needed.
e55bde5e 4820 const General_options::Dir_list& rpath(parameters->options().rpath());
41f542e7
ILT
4821 if (!rpath.empty())
4822 {
4823 std::string rpath_val;
4824 for (General_options::Dir_list::const_iterator p = rpath.begin();
2e702c99
RM
4825 p != rpath.end();
4826 ++p)
4827 {
4828 if (rpath_val.empty())
4829 rpath_val = p->name();
4830 else
4831 {
4832 // Eliminate duplicates.
4833 General_options::Dir_list::const_iterator q;
4834 for (q = rpath.begin(); q != p; ++q)
ad2d6943 4835 if (q->name() == p->name())
2e702c99
RM
4836 break;
4837 if (q == p)
4838 {
4839 rpath_val += ':';
4840 rpath_val += p->name();
4841 }
4842 }
4843 }
41f542e7 4844
b1b00fcc
MF
4845 if (!parameters->options().enable_new_dtags())
4846 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
4847 else
7c414435 4848 odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
41f542e7 4849 }
4f4c5f80
ILT
4850
4851 // Look for text segments that have dynamic relocations.
4852 bool have_textrel = false;
4e8fe71f 4853 if (!this->script_options_->saw_sections_clause())
4f4c5f80 4854 {
4e8fe71f 4855 for (Segment_list::const_iterator p = this->segment_list_.begin();
2e702c99
RM
4856 p != this->segment_list_.end();
4857 ++p)
4858 {
4859 if ((*p)->type() == elfcpp::PT_LOAD
766f91bb 4860 && ((*p)->flags() & elfcpp::PF_W) == 0
2e702c99
RM
4861 && (*p)->has_dynamic_reloc())
4862 {
4863 have_textrel = true;
4864 break;
4865 }
4866 }
4e8fe71f
ILT
4867 }
4868 else
4869 {
4870 // We don't know the section -> segment mapping, so we are
4871 // conservative and just look for readonly sections with
4872 // relocations. If those sections wind up in writable segments,
4873 // then we have created an unnecessary DT_TEXTREL entry.
4874 for (Section_list::const_iterator p = this->section_list_.begin();
2e702c99
RM
4875 p != this->section_list_.end();
4876 ++p)
4877 {
4878 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
4879 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
4880 && (*p)->has_dynamic_reloc())
4881 {
4882 have_textrel = true;
4883 break;
4884 }
4885 }
4f4c5f80
ILT
4886 }
4887
886288f1
ILT
4888 if (parameters->options().filter() != NULL)
4889 odyn->add_string(elfcpp::DT_FILTER, parameters->options().filter());
4890 if (parameters->options().any_auxiliary())
4891 {
4892 for (options::String_set::const_iterator p =
4893 parameters->options().auxiliary_begin();
4894 p != parameters->options().auxiliary_end();
4895 ++p)
4896 odyn->add_string(elfcpp::DT_AUXILIARY, *p);
4897 }
4898
4899 // Add a DT_FLAGS entry if necessary.
4f4c5f80
ILT
4900 unsigned int flags = 0;
4901 if (have_textrel)
6a41d30b
ILT
4902 {
4903 // Add a DT_TEXTREL for compatibility with older loaders.
4904 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
4905 flags |= elfcpp::DF_TEXTREL;
b9674e17 4906
ffeef7df
ILT
4907 if (parameters->options().text())
4908 gold_error(_("read-only segment has dynamic relocations"));
4909 else if (parameters->options().warn_shared_textrel()
4910 && parameters->options().shared())
b9674e17 4911 gold_warning(_("shared library text segment is not shareable"));
6a41d30b 4912 }
8851ecca 4913 if (parameters->options().shared() && this->has_static_tls())
535890bb 4914 flags |= elfcpp::DF_STATIC_TLS;
7be8330a
CD
4915 if (parameters->options().origin())
4916 flags |= elfcpp::DF_ORIGIN;
e9c1bdad
CC
4917 if (parameters->options().Bsymbolic()
4918 && !parameters->options().have_dynamic_list())
f15f61a7
DK
4919 {
4920 flags |= elfcpp::DF_SYMBOLIC;
4921 // Add DT_SYMBOLIC for compatibility with older loaders.
4922 odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
4923 }
e1c74d60
ILT
4924 if (parameters->options().now())
4925 flags |= elfcpp::DF_BIND_NOW;
0d212c3a
ILT
4926 if (flags != 0)
4927 odyn->add_constant(elfcpp::DT_FLAGS, flags);
7c414435
DM
4928
4929 flags = 0;
fb257835
DI
4930 if (parameters->options().global())
4931 flags |= elfcpp::DF_1_GLOBAL;
7c414435
DM
4932 if (parameters->options().initfirst())
4933 flags |= elfcpp::DF_1_INITFIRST;
4934 if (parameters->options().interpose())
4935 flags |= elfcpp::DF_1_INTERPOSE;
4936 if (parameters->options().loadfltr())
4937 flags |= elfcpp::DF_1_LOADFLTR;
4938 if (parameters->options().nodefaultlib())
4939 flags |= elfcpp::DF_1_NODEFLIB;
4940 if (parameters->options().nodelete())
4941 flags |= elfcpp::DF_1_NODELETE;
4942 if (parameters->options().nodlopen())
4943 flags |= elfcpp::DF_1_NOOPEN;
4944 if (parameters->options().nodump())
4945 flags |= elfcpp::DF_1_NODUMP;
4946 if (!parameters->options().shared())
4947 flags &= ~(elfcpp::DF_1_INITFIRST
4948 | elfcpp::DF_1_NODELETE
4949 | elfcpp::DF_1_NOOPEN);
7be8330a
CD
4950 if (parameters->options().origin())
4951 flags |= elfcpp::DF_1_ORIGIN;
e1c74d60
ILT
4952 if (parameters->options().now())
4953 flags |= elfcpp::DF_1_NOW;
e2153196
ILT
4954 if (parameters->options().Bgroup())
4955 flags |= elfcpp::DF_1_GROUP;
0d212c3a 4956 if (flags != 0)
7c414435 4957 odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
a3ad94ed
ILT
4958}
4959
f0ba79e2
ILT
4960// Set the size of the _DYNAMIC symbol table to be the size of the
4961// dynamic data.
4962
4963void
4964Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
4965{
4966 Output_data_dynamic* const odyn = this->dynamic_data_;
6daf5215
ILT
4967 if (odyn == NULL)
4968 return;
f0ba79e2 4969 odyn->finalize_data_size();
6daf5215
ILT
4970 if (this->dynamic_symbol_ == NULL)
4971 return;
f0ba79e2
ILT
4972 off_t data_size = odyn->data_size();
4973 const int size = parameters->target().get_size();
4974 if (size == 32)
4975 symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
4976 else if (size == 64)
4977 symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
4978 else
4979 gold_unreachable();
4980}
4981
dff16297
ILT
4982// The mapping of input section name prefixes to output section names.
4983// In some cases one prefix is itself a prefix of another prefix; in
4984// such a case the longer prefix must come first. These prefixes are
4985// based on the GNU linker default ELF linker script.
a2fb1b05 4986
ead1e424 4987#define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1be75daa 4988#define MAPPING_INIT_EXACT(f, t) { f, 0, t, sizeof(t) - 1 }
dff16297 4989const Layout::Section_name_mapping Layout::section_name_mapping[] =
a2fb1b05 4990{
dff16297 4991 MAPPING_INIT(".text.", ".text"),
dff16297 4992 MAPPING_INIT(".rodata.", ".rodata"),
9b689de0 4993 MAPPING_INIT(".data.rel.ro.local.", ".data.rel.ro.local"),
1be75daa 4994 MAPPING_INIT_EXACT(".data.rel.ro.local", ".data.rel.ro.local"),
9b689de0 4995 MAPPING_INIT(".data.rel.ro.", ".data.rel.ro"),
1be75daa 4996 MAPPING_INIT_EXACT(".data.rel.ro", ".data.rel.ro"),
dff16297
ILT
4997 MAPPING_INIT(".data.", ".data"),
4998 MAPPING_INIT(".bss.", ".bss"),
4999 MAPPING_INIT(".tdata.", ".tdata"),
5000 MAPPING_INIT(".tbss.", ".tbss"),
5001 MAPPING_INIT(".init_array.", ".init_array"),
5002 MAPPING_INIT(".fini_array.", ".fini_array"),
5003 MAPPING_INIT(".sdata.", ".sdata"),
5004 MAPPING_INIT(".sbss.", ".sbss"),
5005 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
5006 // differently depending on whether it is creating a shared library.
5007 MAPPING_INIT(".sdata2.", ".sdata"),
5008 MAPPING_INIT(".sbss2.", ".sbss"),
5009 MAPPING_INIT(".lrodata.", ".lrodata"),
5010 MAPPING_INIT(".ldata.", ".ldata"),
5011 MAPPING_INIT(".lbss.", ".lbss"),
5012 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
5013 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
5014 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
5015 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
5016 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
5017 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
5018 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
5019 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
5020 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
5021 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
5022 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
5023 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
5024 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
5025 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
5026 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
5027 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
5028 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
4a54abbb 5029 MAPPING_INIT(".ARM.extab", ".ARM.extab"),
1dcd334d 5030 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
4a54abbb 5031 MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
1dcd334d 5032 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
a2fb1b05
ILT
5033};
5034#undef MAPPING_INIT
1be75daa 5035#undef MAPPING_INIT_EXACT
a2fb1b05 5036
dff16297
ILT
5037const int Layout::section_name_mapping_count =
5038 (sizeof(Layout::section_name_mapping)
5039 / sizeof(Layout::section_name_mapping[0]));
a2fb1b05 5040
ead1e424
ILT
5041// Choose the output section name to use given an input section name.
5042// Set *PLEN to the length of the name. *PLEN is initialized to the
5043// length of NAME.
5044
5045const char*
5393d741
ILT
5046Layout::output_section_name(const Relobj* relobj, const char* name,
5047 size_t* plen)
ead1e424 5048{
af4a8a83
ILT
5049 // gcc 4.3 generates the following sorts of section names when it
5050 // needs a section name specific to a function:
5051 // .text.FN
5052 // .rodata.FN
5053 // .sdata2.FN
5054 // .data.FN
5055 // .data.rel.FN
5056 // .data.rel.local.FN
5057 // .data.rel.ro.FN
5058 // .data.rel.ro.local.FN
5059 // .sdata.FN
5060 // .bss.FN
5061 // .sbss.FN
5062 // .tdata.FN
5063 // .tbss.FN
5064
5065 // The GNU linker maps all of those to the part before the .FN,
5066 // except that .data.rel.local.FN is mapped to .data, and
5067 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
5068 // beginning with .data.rel.ro.local are grouped together.
5069
5070 // For an anonymous namespace, the string FN can contain a '.'.
5071
5072 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
5073 // GNU linker maps to .rodata.
5074
dff16297
ILT
5075 // The .data.rel.ro sections are used with -z relro. The sections
5076 // are recognized by name. We use the same names that the GNU
5077 // linker does for these sections.
af4a8a83 5078
dff16297
ILT
5079 // It is hard to handle this in a principled way, so we don't even
5080 // try. We use a table of mappings. If the input section name is
5081 // not found in the table, we simply use it as the output section
5082 // name.
af4a8a83 5083
dff16297
ILT
5084 const Section_name_mapping* psnm = section_name_mapping;
5085 for (int i = 0; i < section_name_mapping_count; ++i, ++psnm)
ead1e424 5086 {
1be75daa 5087 if (psnm->fromlen > 0)
dff16297 5088 {
1be75daa
CC
5089 if (strncmp(name, psnm->from, psnm->fromlen) == 0)
5090 {
5091 *plen = psnm->tolen;
5092 return psnm->to;
5093 }
5094 }
5095 else
5096 {
5097 if (strcmp(name, psnm->from) == 0)
5098 {
5099 *plen = psnm->tolen;
5100 return psnm->to;
5101 }
dff16297 5102 }
ead1e424
ILT
5103 }
5104
5393d741
ILT
5105 // As an additional complication, .ctors sections are output in
5106 // either .ctors or .init_array sections, and .dtors sections are
5107 // output in either .dtors or .fini_array sections.
5108 if (is_prefix_of(".ctors.", name) || is_prefix_of(".dtors.", name))
5109 {
5110 if (parameters->options().ctors_in_init_array())
5111 {
5112 *plen = 11;
5113 return name[1] == 'c' ? ".init_array" : ".fini_array";
5114 }
5115 else
5116 {
5117 *plen = 6;
5118 return name[1] == 'c' ? ".ctors" : ".dtors";
5119 }
5120 }
5121 if (parameters->options().ctors_in_init_array()
5122 && (strcmp(name, ".ctors") == 0 || strcmp(name, ".dtors") == 0))
5123 {
5124 // To make .init_array/.fini_array work with gcc we must exclude
5125 // .ctors and .dtors sections from the crtbegin and crtend
5126 // files.
5127 if (relobj == NULL
5128 || (!Layout::match_file_name(relobj, "crtbegin")
5129 && !Layout::match_file_name(relobj, "crtend")))
5130 {
5131 *plen = 11;
5132 return name[1] == 'c' ? ".init_array" : ".fini_array";
5133 }
5134 }
5135
ead1e424
ILT
5136 return name;
5137}
5138
5393d741
ILT
5139// Return true if RELOBJ is an input file whose base name matches
5140// FILE_NAME. The base name must have an extension of ".o", and must
5141// be exactly FILE_NAME.o or FILE_NAME, one character, ".o". This is
5142// to match crtbegin.o as well as crtbeginS.o without getting confused
5143// by other possibilities. Overall matching the file name this way is
5144// a dreadful hack, but the GNU linker does it in order to better
5145// support gcc, and we need to be compatible.
5146
5147bool
5148Layout::match_file_name(const Relobj* relobj, const char* match)
5149{
5150 const std::string& file_name(relobj->name());
5151 const char* base_name = lbasename(file_name.c_str());
5152 size_t match_len = strlen(match);
5153 if (strncmp(base_name, match, match_len) != 0)
5154 return false;
5155 size_t base_len = strlen(base_name);
5156 if (base_len != match_len + 2 && base_len != match_len + 3)
5157 return false;
5158 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
5159}
5160
8a4c0b0d
ILT
5161// Check if a comdat group or .gnu.linkonce section with the given
5162// NAME is selected for the link. If there is already a section,
1ef4d87f
ILT
5163// *KEPT_SECTION is set to point to the existing section and the
5164// function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
5165// IS_GROUP_NAME are recorded for this NAME in the layout object,
5166// *KEPT_SECTION is set to the internal copy and the function returns
5167// true.
a2fb1b05
ILT
5168
5169bool
e55bde5e 5170Layout::find_or_add_kept_section(const std::string& name,
1ef4d87f
ILT
5171 Relobj* object,
5172 unsigned int shndx,
5173 bool is_comdat,
5174 bool is_group_name,
2e702c99 5175 Kept_section** kept_section)
a2fb1b05 5176{
e55bde5e
ILT
5177 // It's normal to see a couple of entries here, for the x86 thunk
5178 // sections. If we see more than a few, we're linking a C++
5179 // program, and we resize to get more space to minimize rehashing.
5180 if (this->signatures_.size() > 4
5181 && !this->resized_signatures_)
5182 {
5183 reserve_unordered_map(&this->signatures_,
5184 this->number_of_input_files_ * 64);
5185 this->resized_signatures_ = true;
5186 }
5187
1ef4d87f
ILT
5188 Kept_section candidate;
5189 std::pair<Signatures::iterator, bool> ins =
5190 this->signatures_.insert(std::make_pair(name, candidate));
a2fb1b05 5191
1ef4d87f 5192 if (kept_section != NULL)
8a4c0b0d 5193 *kept_section = &ins.first->second;
a2fb1b05
ILT
5194 if (ins.second)
5195 {
5196 // This is the first time we've seen this signature.
1ef4d87f
ILT
5197 ins.first->second.set_object(object);
5198 ins.first->second.set_shndx(shndx);
5199 if (is_comdat)
5200 ins.first->second.set_is_comdat();
5201 if (is_group_name)
5202 ins.first->second.set_is_group_name();
a2fb1b05
ILT
5203 return true;
5204 }
5205
1ef4d87f
ILT
5206 // We have already seen this signature.
5207
5208 if (ins.first->second.is_group_name())
a2fb1b05
ILT
5209 {
5210 // We've already seen a real section group with this signature.
1ef4d87f
ILT
5211 // If the kept group is from a plugin object, and we're in the
5212 // replacement phase, accept the new one as a replacement.
5213 if (ins.first->second.object() == NULL
2e702c99
RM
5214 && parameters->options().plugins()->in_replacement_phase())
5215 {
1ef4d87f
ILT
5216 ins.first->second.set_object(object);
5217 ins.first->second.set_shndx(shndx);
2e702c99
RM
5218 return true;
5219 }
a2fb1b05
ILT
5220 return false;
5221 }
1ef4d87f 5222 else if (is_group_name)
a2fb1b05
ILT
5223 {
5224 // This is a real section group, and we've already seen a
a0fa0c07 5225 // linkonce section with this signature. Record that we've seen
a2fb1b05 5226 // a section group, and don't include this section group.
1ef4d87f 5227 ins.first->second.set_is_group_name();
a2fb1b05
ILT
5228 return false;
5229 }
5230 else
5231 {
5232 // We've already seen a linkonce section and this is a linkonce
5233 // section. These don't block each other--this may be the same
5234 // symbol name with different section types.
5235 return true;
5236 }
5237}
5238
a445fddf
ILT
5239// Store the allocated sections into the section list.
5240
5241void
2ea97941 5242Layout::get_allocated_sections(Section_list* section_list) const
a445fddf
ILT
5243{
5244 for (Section_list::const_iterator p = this->section_list_.begin();
5245 p != this->section_list_.end();
5246 ++p)
5247 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2ea97941 5248 section_list->push_back(*p);
a445fddf
ILT
5249}
5250
ec661b9d
AM
5251// Store the executable sections into the section list.
5252
5253void
5254Layout::get_executable_sections(Section_list* section_list) const
5255{
5256 for (Section_list::const_iterator p = this->section_list_.begin();
5257 p != this->section_list_.end();
5258 ++p)
5259 if (((*p)->flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
5260 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
5261 section_list->push_back(*p);
5262}
5263
a445fddf
ILT
5264// Create an output segment.
5265
5266Output_segment*
5267Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
5268{
8851ecca 5269 gold_assert(!parameters->options().relocatable());
a445fddf
ILT
5270 Output_segment* oseg = new Output_segment(type, flags);
5271 this->segment_list_.push_back(oseg);
2d924fd9
ILT
5272
5273 if (type == elfcpp::PT_TLS)
5274 this->tls_segment_ = oseg;
5275 else if (type == elfcpp::PT_GNU_RELRO)
5276 this->relro_segment_ = oseg;
10b4f102
ILT
5277 else if (type == elfcpp::PT_INTERP)
5278 this->interp_segment_ = oseg;
2d924fd9 5279
a445fddf
ILT
5280 return oseg;
5281}
5282
bec5b579
CC
5283// Return the file offset of the normal symbol table.
5284
5285off_t
5286Layout::symtab_section_offset() const
5287{
5288 if (this->symtab_section_ != NULL)
5289 return this->symtab_section_->offset();
5290 return 0;
5291}
5292
886f533a
ILT
5293// Return the section index of the normal symbol table. It may have
5294// been stripped by the -s/--strip-all option.
5295
5296unsigned int
5297Layout::symtab_section_shndx() const
5298{
5299 if (this->symtab_section_ != NULL)
5300 return this->symtab_section_->out_shndx();
5301 return 0;
5302}
5303
730cdc88
ILT
5304// Write out the Output_sections. Most won't have anything to write,
5305// since most of the data will come from input sections which are
5306// handled elsewhere. But some Output_sections do have Output_data.
5307
5308void
5309Layout::write_output_sections(Output_file* of) const
5310{
5311 for (Section_list::const_iterator p = this->section_list_.begin();
5312 p != this->section_list_.end();
5313 ++p)
5314 {
5315 if (!(*p)->after_input_sections())
5316 (*p)->write(of);
5317 }
5318}
5319
61ba1cf9
ILT
5320// Write out data not associated with a section or the symbol table.
5321
5322void
9025d29d 5323Layout::write_data(const Symbol_table* symtab, Output_file* of) const
61ba1cf9 5324{
8851ecca 5325 if (!parameters->options().strip_all())
a3ad94ed 5326 {
2ea97941 5327 const Output_section* symtab_section = this->symtab_section_;
9e2dcb77
ILT
5328 for (Section_list::const_iterator p = this->section_list_.begin();
5329 p != this->section_list_.end();
5330 ++p)
a3ad94ed 5331 {
9e2dcb77
ILT
5332 if ((*p)->needs_symtab_index())
5333 {
2ea97941 5334 gold_assert(symtab_section != NULL);
9e2dcb77
ILT
5335 unsigned int index = (*p)->symtab_index();
5336 gold_assert(index > 0 && index != -1U);
2ea97941
ILT
5337 off_t off = (symtab_section->offset()
5338 + index * symtab_section->entsize());
d491d34e 5339 symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
9e2dcb77 5340 }
a3ad94ed
ILT
5341 }
5342 }
5343
2ea97941 5344 const Output_section* dynsym_section = this->dynsym_section_;
a3ad94ed
ILT
5345 for (Section_list::const_iterator p = this->section_list_.begin();
5346 p != this->section_list_.end();
5347 ++p)
5348 {
5349 if ((*p)->needs_dynsym_index())
5350 {
2ea97941 5351 gold_assert(dynsym_section != NULL);
a3ad94ed
ILT
5352 unsigned int index = (*p)->dynsym_index();
5353 gold_assert(index > 0 && index != -1U);
2ea97941
ILT
5354 off_t off = (dynsym_section->offset()
5355 + index * dynsym_section->entsize());
d491d34e 5356 symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
a3ad94ed
ILT
5357 }
5358 }
5359
a3ad94ed 5360 // Write out the Output_data which are not in an Output_section.
61ba1cf9
ILT
5361 for (Data_list::const_iterator p = this->special_output_list_.begin();
5362 p != this->special_output_list_.end();
5363 ++p)
5364 (*p)->write(of);
eb426534
RM
5365
5366 // Write out the Output_data which are not in an Output_section
5367 // and are regenerated in each iteration of relaxation.
5368 for (Data_list::const_iterator p = this->relax_output_list_.begin();
5369 p != this->relax_output_list_.end();
5370 ++p)
5371 (*p)->write(of);
61ba1cf9
ILT
5372}
5373
730cdc88
ILT
5374// Write out the Output_sections which can only be written after the
5375// input sections are complete.
5376
5377void
27bc2bce 5378Layout::write_sections_after_input_sections(Output_file* of)
730cdc88 5379{
27bc2bce 5380 // Determine the final section offsets, and thus the final output
9a0910c3
ILT
5381 // file size. Note we finalize the .shstrab last, to allow the
5382 // after_input_section sections to modify their section-names before
5383 // writing.
17a1d0a9 5384 if (this->any_postprocessing_sections_)
27bc2bce 5385 {
17a1d0a9
ILT
5386 off_t off = this->output_file_size_;
5387 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
8a4c0b0d 5388
17a1d0a9
ILT
5389 // Now that we've finalized the names, we can finalize the shstrab.
5390 off =
5391 this->set_section_offsets(off,
5392 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
5393
5394 if (off > this->output_file_size_)
5395 {
5396 of->resize(off);
5397 this->output_file_size_ = off;
5398 }
27bc2bce
ILT
5399 }
5400
730cdc88
ILT
5401 for (Section_list::const_iterator p = this->section_list_.begin();
5402 p != this->section_list_.end();
5403 ++p)
5404 {
5405 if ((*p)->after_input_sections())
5406 (*p)->write(of);
5407 }
27bc2bce 5408
27bc2bce 5409 this->section_headers_->write(of);
730cdc88
ILT
5410}
5411
e7c5ea40
CC
5412// If a tree-style build ID was requested, the parallel part of that computation
5413// is already done, and the final hash-of-hashes is computed here. For other
5414// types of build IDs, all the work is done here.
8ed814a9
ILT
5415
5416void
9c7fe3c5
CC
5417Layout::write_build_id(Output_file* of, unsigned char* array_of_hashes,
5418 size_t size_of_hashes) const
8ed814a9
ILT
5419{
5420 if (this->build_id_note_ == NULL)
5421 return;
5422
8ed814a9 5423 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
bbc5ae17 5424 this->build_id_note_->data_size());
8ed814a9 5425
9c7fe3c5 5426 if (array_of_hashes == NULL)
8ed814a9 5427 {
e7c5ea40
CC
5428 const size_t output_file_size = this->output_file_size();
5429 const unsigned char* iv = of->get_input_view(0, output_file_size);
5430 const char* style = parameters->options().build_id();
5431
5432 // If we get here with style == "tree" then the output must be
5433 // too small for chunking, and we use SHA-1 in that case.
5434 if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
bbc5ae17 5435 sha1_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
e7c5ea40 5436 else if (strcmp(style, "md5") == 0)
bbc5ae17 5437 md5_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
e7c5ea40 5438 else
bbc5ae17 5439 gold_unreachable();
e7c5ea40
CC
5440
5441 of->free_input_view(0, output_file_size, iv);
8ed814a9 5442 }
e7c5ea40 5443 else
8ed814a9 5444 {
e7c5ea40
CC
5445 // Non-overlapping substrings of the output file have been hashed.
5446 // Compute SHA-1 hash of the hashes.
9c7fe3c5
CC
5447 sha1_buffer(reinterpret_cast<const char*>(array_of_hashes),
5448 size_of_hashes, ov);
5449 delete[] array_of_hashes;
8ed814a9 5450 }
8ed814a9
ILT
5451
5452 of->write_output_view(this->build_id_note_->offset(),
5453 this->build_id_note_->data_size(),
5454 ov);
8ed814a9
ILT
5455}
5456
516cb3d0
ILT
5457// Write out a binary file. This is called after the link is
5458// complete. IN is the temporary output file we used to generate the
5459// ELF code. We simply walk through the segments, read them from
5460// their file offset in IN, and write them to their load address in
5461// the output file. FIXME: with a bit more work, we could support
5462// S-records and/or Intel hex format here.
5463
5464void
5465Layout::write_binary(Output_file* in) const
5466{
e55bde5e 5467 gold_assert(parameters->options().oformat_enum()
bc644c6c 5468 == General_options::OBJECT_FORMAT_BINARY);
516cb3d0
ILT
5469
5470 // Get the size of the binary file.
5471 uint64_t max_load_address = 0;
5472 for (Segment_list::const_iterator p = this->segment_list_.begin();
5473 p != this->segment_list_.end();
5474 ++p)
5475 {
5476 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
5477 {
5478 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
5479 if (max_paddr > max_load_address)
5480 max_load_address = max_paddr;
5481 }
5482 }
5483
8851ecca 5484 Output_file out(parameters->options().output_file_name());
516cb3d0
ILT
5485 out.open(max_load_address);
5486
5487 for (Segment_list::const_iterator p = this->segment_list_.begin();
5488 p != this->segment_list_.end();
5489 ++p)
5490 {
5491 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
5492 {
5493 const unsigned char* vin = in->get_input_view((*p)->offset(),
5494 (*p)->filesz());
5495 unsigned char* vout = out.get_output_view((*p)->paddr(),
5496 (*p)->filesz());
5497 memcpy(vout, vin, (*p)->filesz());
5498 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
5499 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
5500 }
5501 }
5502
5503 out.close();
5504}
5505
7d9e3d98
ILT
5506// Print the output sections to the map file.
5507
5508void
5509Layout::print_to_mapfile(Mapfile* mapfile) const
5510{
5511 for (Segment_list::const_iterator p = this->segment_list_.begin();
5512 p != this->segment_list_.end();
5513 ++p)
5514 (*p)->print_sections_to_mapfile(mapfile);
8f89af0a
CC
5515 for (Section_list::const_iterator p = this->unattached_section_list_.begin();
5516 p != this->unattached_section_list_.end();
5517 ++p)
5518 (*p)->print_to_mapfile(mapfile);
7d9e3d98
ILT
5519}
5520
ad8f37d1
ILT
5521// Print statistical information to stderr. This is used for --stats.
5522
5523void
5524Layout::print_stats() const
5525{
5526 this->namepool_.print_stats("section name pool");
5527 this->sympool_.print_stats("output symbol name pool");
5528 this->dynpool_.print_stats("dynamic name pool");
38c5e8b4
ILT
5529
5530 for (Section_list::const_iterator p = this->section_list_.begin();
5531 p != this->section_list_.end();
5532 ++p)
5533 (*p)->print_merge_stats();
ad8f37d1
ILT
5534}
5535
730cdc88
ILT
5536// Write_sections_task methods.
5537
5538// We can always run this task.
5539
17a1d0a9
ILT
5540Task_token*
5541Write_sections_task::is_runnable()
730cdc88 5542{
17a1d0a9 5543 return NULL;
730cdc88
ILT
5544}
5545
5546// We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
5547// when finished.
5548
17a1d0a9
ILT
5549void
5550Write_sections_task::locks(Task_locker* tl)
730cdc88 5551{
17a1d0a9 5552 tl->add(this, this->output_sections_blocker_);
635aa30e
CC
5553 if (this->input_sections_blocker_ != NULL)
5554 tl->add(this, this->input_sections_blocker_);
17a1d0a9 5555 tl->add(this, this->final_blocker_);
730cdc88
ILT
5556}
5557
5558// Run the task--write out the data.
5559
5560void
5561Write_sections_task::run(Workqueue*)
5562{
5563 this->layout_->write_output_sections(this->of_);
5564}
5565
61ba1cf9
ILT
5566// Write_data_task methods.
5567
5568// We can always run this task.
5569
17a1d0a9
ILT
5570Task_token*
5571Write_data_task::is_runnable()
61ba1cf9 5572{
17a1d0a9 5573 return NULL;
61ba1cf9
ILT
5574}
5575
5576// We need to unlock FINAL_BLOCKER when finished.
5577
17a1d0a9
ILT
5578void
5579Write_data_task::locks(Task_locker* tl)
61ba1cf9 5580{
17a1d0a9 5581 tl->add(this, this->final_blocker_);
61ba1cf9
ILT
5582}
5583
5584// Run the task--write out the data.
5585
5586void
5587Write_data_task::run(Workqueue*)
5588{
9025d29d 5589 this->layout_->write_data(this->symtab_, this->of_);
61ba1cf9
ILT
5590}
5591
5592// Write_symbols_task methods.
5593
5594// We can always run this task.
5595
17a1d0a9
ILT
5596Task_token*
5597Write_symbols_task::is_runnable()
61ba1cf9 5598{
17a1d0a9 5599 return NULL;
61ba1cf9
ILT
5600}
5601
5602// We need to unlock FINAL_BLOCKER when finished.
5603
17a1d0a9
ILT
5604void
5605Write_symbols_task::locks(Task_locker* tl)
61ba1cf9 5606{
17a1d0a9 5607 tl->add(this, this->final_blocker_);
61ba1cf9
ILT
5608}
5609
5610// Run the task--write out the symbols.
5611
5612void
5613Write_symbols_task::run(Workqueue*)
5614{
fd9d194f
ILT
5615 this->symtab_->write_globals(this->sympool_, this->dynpool_,
5616 this->layout_->symtab_xindex(),
d491d34e 5617 this->layout_->dynsym_xindex(), this->of_);
61ba1cf9
ILT
5618}
5619
730cdc88
ILT
5620// Write_after_input_sections_task methods.
5621
5622// We can only run this task after the input sections have completed.
5623
17a1d0a9
ILT
5624Task_token*
5625Write_after_input_sections_task::is_runnable()
730cdc88
ILT
5626{
5627 if (this->input_sections_blocker_->is_blocked())
17a1d0a9
ILT
5628 return this->input_sections_blocker_;
5629 return NULL;
730cdc88
ILT
5630}
5631
5632// We need to unlock FINAL_BLOCKER when finished.
5633
17a1d0a9
ILT
5634void
5635Write_after_input_sections_task::locks(Task_locker* tl)
730cdc88 5636{
17a1d0a9 5637 tl->add(this, this->final_blocker_);
730cdc88
ILT
5638}
5639
5640// Run the task.
5641
5642void
5643Write_after_input_sections_task::run(Workqueue*)
5644{
5645 this->layout_->write_sections_after_input_sections(this->of_);
5646}
5647
9c7fe3c5
CC
5648// Build IDs can be computed as a "flat" sha1 or md5 of a string of bytes,
5649// or as a "tree" where each chunk of the string is hashed and then those
5650// hashes are put into a (much smaller) string which is hashed with sha1.
5651// We compute a checksum over the entire file because that is simplest.
5652
5653void
5654Build_id_task_runner::run(Workqueue* workqueue, const Task*)
5655{
5656 Task_token* post_hash_tasks_blocker = new Task_token(true);
5657 const Layout* layout = this->layout_;
5658 Output_file* of = this->of_;
5659 const size_t filesize = (layout->output_file_size() <= 0 ? 0
5660 : static_cast<size_t>(layout->output_file_size()));
5661 unsigned char* array_of_hashes = NULL;
5662 size_t size_of_hashes = 0;
5663
5664 if (strcmp(this->options_->build_id(), "tree") == 0
5665 && this->options_->build_id_chunk_size_for_treehash() > 0
5666 && filesize > 0
5667 && (filesize >= this->options_->build_id_min_file_size_for_treehash()))
5668 {
5669 static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
5670 const size_t chunk_size =
5671 this->options_->build_id_chunk_size_for_treehash();
5672 const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
5673 post_hash_tasks_blocker->add_blockers(num_hashes);
5674 size_of_hashes = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
5675 array_of_hashes = new unsigned char[size_of_hashes];
5676 unsigned char *dst = array_of_hashes;
5677 for (size_t i = 0, src_offset = 0; i < num_hashes;
5678 i++, dst += MD5_OUTPUT_SIZE_IN_BYTES, src_offset += chunk_size)
5679 {
5680 size_t size = std::min(chunk_size, filesize - src_offset);
5681 workqueue->queue(new Hash_task(of,
5682 src_offset,
5683 size,
5684 dst,
5685 post_hash_tasks_blocker));
5686 }
5687 }
5688
5689 // Queue the final task to write the build id and close the output file.
5690 workqueue->queue(new Task_function(new Close_task_runner(this->options_,
5691 layout,
5692 of,
5693 array_of_hashes,
5694 size_of_hashes),
5695 post_hash_tasks_blocker,
5696 "Task_function Close_task_runner"));
5697}
5698
92e059d8 5699// Close_task_runner methods.
61ba1cf9 5700
e7c5ea40
CC
5701// Finish up the build ID computation, if necessary, and write a binary file,
5702// if necessary. Then close the output file.
61ba1cf9
ILT
5703
5704void
17a1d0a9 5705Close_task_runner::run(Workqueue*, const Task*)
61ba1cf9 5706{
e7c5ea40 5707 // At this point the multi-threaded part of the build ID computation,
9c7fe3c5
CC
5708 // if any, is done. See Build_id_task_runner.
5709 this->layout_->write_build_id(this->of_, this->array_of_hashes_,
5710 this->size_of_hashes_);
8ed814a9 5711
516cb3d0 5712 // If we've been asked to create a binary file, we do so here.
7cc619c3 5713 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
516cb3d0
ILT
5714 this->layout_->write_binary(this->of_);
5715
61ba1cf9
ILT
5716 this->of_->close();
5717}
5718
a2fb1b05
ILT
5719// Instantiate the templates we need. We could use the configure
5720// script to restrict this to only the ones for implemented targets.
5721
193a53d9 5722#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05 5723template
cdc29364
CC
5724Output_section*
5725Layout::init_fixed_output_section<32, false>(
5726 const char* name,
5727 elfcpp::Shdr<32, false>& shdr);
5728#endif
5729
5730#ifdef HAVE_TARGET_32_BIG
5731template
5732Output_section*
5733Layout::init_fixed_output_section<32, true>(
5734 const char* name,
5735 elfcpp::Shdr<32, true>& shdr);
5736#endif
5737
5738#ifdef HAVE_TARGET_64_LITTLE
5739template
5740Output_section*
5741Layout::init_fixed_output_section<64, false>(
5742 const char* name,
5743 elfcpp::Shdr<64, false>& shdr);
5744#endif
5745
5746#ifdef HAVE_TARGET_64_BIG
5747template
5748Output_section*
5749Layout::init_fixed_output_section<64, true>(
5750 const char* name,
5751 elfcpp::Shdr<64, true>& shdr);
5752#endif
5753
5754#ifdef HAVE_TARGET_32_LITTLE
5755template
a2fb1b05 5756Output_section*
6fa2a40b
CC
5757Layout::layout<32, false>(Sized_relobj_file<32, false>* object,
5758 unsigned int shndx,
730cdc88
ILT
5759 const char* name,
5760 const elfcpp::Shdr<32, false>& shdr,
5761 unsigned int, unsigned int, off_t*);
193a53d9 5762#endif
a2fb1b05 5763
193a53d9 5764#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
5765template
5766Output_section*
6fa2a40b
CC
5767Layout::layout<32, true>(Sized_relobj_file<32, true>* object,
5768 unsigned int shndx,
730cdc88
ILT
5769 const char* name,
5770 const elfcpp::Shdr<32, true>& shdr,
5771 unsigned int, unsigned int, off_t*);
193a53d9 5772#endif
a2fb1b05 5773
193a53d9 5774#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
5775template
5776Output_section*
6fa2a40b
CC
5777Layout::layout<64, false>(Sized_relobj_file<64, false>* object,
5778 unsigned int shndx,
730cdc88
ILT
5779 const char* name,
5780 const elfcpp::Shdr<64, false>& shdr,
5781 unsigned int, unsigned int, off_t*);
193a53d9 5782#endif
a2fb1b05 5783
193a53d9 5784#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
5785template
5786Output_section*
6fa2a40b
CC
5787Layout::layout<64, true>(Sized_relobj_file<64, true>* object,
5788 unsigned int shndx,
730cdc88
ILT
5789 const char* name,
5790 const elfcpp::Shdr<64, true>& shdr,
5791 unsigned int, unsigned int, off_t*);
193a53d9 5792#endif
a2fb1b05 5793
6a74a719
ILT
5794#ifdef HAVE_TARGET_32_LITTLE
5795template
5796Output_section*
6fa2a40b 5797Layout::layout_reloc<32, false>(Sized_relobj_file<32, false>* object,
6a74a719
ILT
5798 unsigned int reloc_shndx,
5799 const elfcpp::Shdr<32, false>& shdr,
5800 Output_section* data_section,
5801 Relocatable_relocs* rr);
5802#endif
5803
5804#ifdef HAVE_TARGET_32_BIG
5805template
5806Output_section*
6fa2a40b 5807Layout::layout_reloc<32, true>(Sized_relobj_file<32, true>* object,
6a74a719
ILT
5808 unsigned int reloc_shndx,
5809 const elfcpp::Shdr<32, true>& shdr,
5810 Output_section* data_section,
5811 Relocatable_relocs* rr);
5812#endif
5813
5814#ifdef HAVE_TARGET_64_LITTLE
5815template
5816Output_section*
6fa2a40b 5817Layout::layout_reloc<64, false>(Sized_relobj_file<64, false>* object,
6a74a719
ILT
5818 unsigned int reloc_shndx,
5819 const elfcpp::Shdr<64, false>& shdr,
5820 Output_section* data_section,
5821 Relocatable_relocs* rr);
5822#endif
5823
5824#ifdef HAVE_TARGET_64_BIG
5825template
5826Output_section*
6fa2a40b 5827Layout::layout_reloc<64, true>(Sized_relobj_file<64, true>* object,
6a74a719
ILT
5828 unsigned int reloc_shndx,
5829 const elfcpp::Shdr<64, true>& shdr,
5830 Output_section* data_section,
5831 Relocatable_relocs* rr);
5832#endif
5833
5834#ifdef HAVE_TARGET_32_LITTLE
5835template
5836void
5837Layout::layout_group<32, false>(Symbol_table* symtab,
6fa2a40b 5838 Sized_relobj_file<32, false>* object,
6a74a719
ILT
5839 unsigned int,
5840 const char* group_section_name,
5841 const char* signature,
5842 const elfcpp::Shdr<32, false>& shdr,
8825ac63
ILT
5843 elfcpp::Elf_Word flags,
5844 std::vector<unsigned int>* shndxes);
6a74a719
ILT
5845#endif
5846
5847#ifdef HAVE_TARGET_32_BIG
5848template
5849void
5850Layout::layout_group<32, true>(Symbol_table* symtab,
6fa2a40b 5851 Sized_relobj_file<32, true>* object,
6a74a719
ILT
5852 unsigned int,
5853 const char* group_section_name,
5854 const char* signature,
5855 const elfcpp::Shdr<32, true>& shdr,
8825ac63
ILT
5856 elfcpp::Elf_Word flags,
5857 std::vector<unsigned int>* shndxes);
6a74a719
ILT
5858#endif
5859
5860#ifdef HAVE_TARGET_64_LITTLE
5861template
5862void
5863Layout::layout_group<64, false>(Symbol_table* symtab,
6fa2a40b 5864 Sized_relobj_file<64, false>* object,
6a74a719
ILT
5865 unsigned int,
5866 const char* group_section_name,
5867 const char* signature,
5868 const elfcpp::Shdr<64, false>& shdr,
8825ac63
ILT
5869 elfcpp::Elf_Word flags,
5870 std::vector<unsigned int>* shndxes);
6a74a719
ILT
5871#endif
5872
5873#ifdef HAVE_TARGET_64_BIG
5874template
5875void
5876Layout::layout_group<64, true>(Symbol_table* symtab,
6fa2a40b 5877 Sized_relobj_file<64, true>* object,
6a74a719
ILT
5878 unsigned int,
5879 const char* group_section_name,
5880 const char* signature,
5881 const elfcpp::Shdr<64, true>& shdr,
8825ac63
ILT
5882 elfcpp::Elf_Word flags,
5883 std::vector<unsigned int>* shndxes);
6a74a719
ILT
5884#endif
5885
730cdc88
ILT
5886#ifdef HAVE_TARGET_32_LITTLE
5887template
5888Output_section*
6fa2a40b 5889Layout::layout_eh_frame<32, false>(Sized_relobj_file<32, false>* object,
730cdc88
ILT
5890 const unsigned char* symbols,
5891 off_t symbols_size,
5892 const unsigned char* symbol_names,
5893 off_t symbol_names_size,
5894 unsigned int shndx,
5895 const elfcpp::Shdr<32, false>& shdr,
5896 unsigned int reloc_shndx,
5897 unsigned int reloc_type,
5898 off_t* off);
5899#endif
5900
5901#ifdef HAVE_TARGET_32_BIG
5902template
5903Output_section*
6fa2a40b
CC
5904Layout::layout_eh_frame<32, true>(Sized_relobj_file<32, true>* object,
5905 const unsigned char* symbols,
5906 off_t symbols_size,
730cdc88
ILT
5907 const unsigned char* symbol_names,
5908 off_t symbol_names_size,
5909 unsigned int shndx,
5910 const elfcpp::Shdr<32, true>& shdr,
5911 unsigned int reloc_shndx,
5912 unsigned int reloc_type,
5913 off_t* off);
5914#endif
5915
5916#ifdef HAVE_TARGET_64_LITTLE
5917template
5918Output_section*
6fa2a40b 5919Layout::layout_eh_frame<64, false>(Sized_relobj_file<64, false>* object,
730cdc88
ILT
5920 const unsigned char* symbols,
5921 off_t symbols_size,
5922 const unsigned char* symbol_names,
5923 off_t symbol_names_size,
5924 unsigned int shndx,
5925 const elfcpp::Shdr<64, false>& shdr,
5926 unsigned int reloc_shndx,
5927 unsigned int reloc_type,
5928 off_t* off);
5929#endif
5930
5931#ifdef HAVE_TARGET_64_BIG
5932template
5933Output_section*
6fa2a40b
CC
5934Layout::layout_eh_frame<64, true>(Sized_relobj_file<64, true>* object,
5935 const unsigned char* symbols,
5936 off_t symbols_size,
730cdc88
ILT
5937 const unsigned char* symbol_names,
5938 off_t symbol_names_size,
5939 unsigned int shndx,
5940 const elfcpp::Shdr<64, true>& shdr,
5941 unsigned int reloc_shndx,
5942 unsigned int reloc_type,
5943 off_t* off);
5944#endif
a2fb1b05 5945
c1027032
CC
5946#ifdef HAVE_TARGET_32_LITTLE
5947template
5948void
5949Layout::add_to_gdb_index(bool is_type_unit,
5950 Sized_relobj<32, false>* object,
5951 const unsigned char* symbols,
5952 off_t symbols_size,
5953 unsigned int shndx,
5954 unsigned int reloc_shndx,
5955 unsigned int reloc_type);
5956#endif
5957
5958#ifdef HAVE_TARGET_32_BIG
5959template
5960void
5961Layout::add_to_gdb_index(bool is_type_unit,
5962 Sized_relobj<32, true>* object,
5963 const unsigned char* symbols,
5964 off_t symbols_size,
5965 unsigned int shndx,
5966 unsigned int reloc_shndx,
5967 unsigned int reloc_type);
5968#endif
5969
5970#ifdef HAVE_TARGET_64_LITTLE
5971template
5972void
5973Layout::add_to_gdb_index(bool is_type_unit,
5974 Sized_relobj<64, false>* object,
5975 const unsigned char* symbols,
5976 off_t symbols_size,
5977 unsigned int shndx,
5978 unsigned int reloc_shndx,
5979 unsigned int reloc_type);
5980#endif
5981
5982#ifdef HAVE_TARGET_64_BIG
5983template
5984void
5985Layout::add_to_gdb_index(bool is_type_unit,
5986 Sized_relobj<64, true>* object,
5987 const unsigned char* symbols,
5988 off_t symbols_size,
5989 unsigned int shndx,
5990 unsigned int reloc_shndx,
5991 unsigned int reloc_type);
5992#endif
5993
a2fb1b05 5994} // End namespace gold.
This page took 0.839315 seconds and 4 git commands to generate.