-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlayout.cc
5273 lines (4581 loc) · 163 KB
/
layout.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// layout.cc -- lay out output file sections for gold
// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <[email protected]>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "gold.h"
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <utility>
#include <fcntl.h>
#include <fnmatch.h>
#include <unistd.h>
#include "libiberty.h"
#include "md5.h"
#include "sha1.h"
#include "parameters.h"
#include "options.h"
#include "mapfile.h"
#include "script.h"
#include "script-sections.h"
#include "output.h"
#include "symtab.h"
#include "dynobj.h"
#include "ehframe.h"
#include "compressed_output.h"
#include "reduced_debug_output.h"
#include "object.h"
#include "reloc.h"
#include "descriptors.h"
#include "plugin.h"
#include "incremental.h"
#include "layout.h"
namespace gold
{
// Class Free_list.
// The total number of free lists used.
unsigned int Free_list::num_lists = 0;
// The total number of free list nodes used.
unsigned int Free_list::num_nodes = 0;
// The total number of calls to Free_list::remove.
unsigned int Free_list::num_removes = 0;
// The total number of nodes visited during calls to Free_list::remove.
unsigned int Free_list::num_remove_visits = 0;
// The total number of calls to Free_list::allocate.
unsigned int Free_list::num_allocates = 0;
// The total number of nodes visited during calls to Free_list::allocate.
unsigned int Free_list::num_allocate_visits = 0;
// Initialize the free list. Creates a single free list node that
// describes the entire region of length LEN. If EXTEND is true,
// allocate() is allowed to extend the region beyond its initial
// length.
void
Free_list::init(off_t len, bool extend)
{
this->list_.push_front(Free_list_node(0, len));
this->last_remove_ = this->list_.begin();
this->extend_ = extend;
this->length_ = len;
++Free_list::num_lists;
++Free_list::num_nodes;
}
// Remove a chunk from the free list. Because we start with a single
// node that covers the entire section, and remove chunks from it one
// at a time, we do not need to coalesce chunks or handle cases that
// span more than one free node. We expect to remove chunks from the
// free list in order, and we expect to have only a few chunks of free
// space left (corresponding to files that have changed since the last
// incremental link), so a simple linear list should provide sufficient
// performance.
void
Free_list::remove(off_t start, off_t end)
{
if (start == end)
return;
gold_assert(start < end);
++Free_list::num_removes;
Iterator p = this->last_remove_;
if (p->start_ > start)
p = this->list_.begin();
for (; p != this->list_.end(); ++p)
{
++Free_list::num_remove_visits;
// Find a node that wholly contains the indicated region.
if (p->start_ <= start && p->end_ >= end)
{
// Case 1: the indicated region spans the whole node.
// Add some fuzz to avoid creating tiny free chunks.
if (p->start_ + 3 >= start && p->end_ <= end + 3)
p = this->list_.erase(p);
// Case 2: remove a chunk from the start of the node.
else if (p->start_ + 3 >= start)
p->start_ = end;
// Case 3: remove a chunk from the end of the node.
else if (p->end_ <= end + 3)
p->end_ = start;
// Case 4: remove a chunk from the middle, and split
// the node into two.
else
{
Free_list_node newnode(p->start_, start);
p->start_ = end;
this->list_.insert(p, newnode);
++Free_list::num_nodes;
}
this->last_remove_ = p;
return;
}
}
// Did not find a node containing the given chunk. This could happen
// because a small chunk was already removed due to the fuzz.
gold_debug(DEBUG_INCREMENTAL,
"Free_list::remove(%d,%d) not found",
static_cast<int>(start), static_cast<int>(end));
}
// Allocate a chunk of size LEN from the free list. Returns -1ULL
// if a sufficiently large chunk of free space is not found.
// We use a simple first-fit algorithm.
off_t
Free_list::allocate(off_t len, uint64_t align, off_t minoff)
{
gold_debug(DEBUG_INCREMENTAL,
"Free_list::allocate(%08lx, %d, %08lx)",
static_cast<long>(len), static_cast<int>(align),
static_cast<long>(minoff));
if (len == 0)
return align_address(minoff, align);
++Free_list::num_allocates;
for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
{
++Free_list::num_allocate_visits;
off_t start = p->start_ > minoff ? p->start_ : minoff;
start = align_address(start, align);
off_t end = start + len;
if (end > p->end_ && p->end_ == this->length_ && this->extend_)
{
this->length_ = end;
p->end_ = end;
}
if (end <= p->end_)
{
if (p->start_ + 3 >= start && p->end_ <= end + 3)
this->list_.erase(p);
else if (p->start_ + 3 >= start)
p->start_ = end;
else if (p->end_ <= end + 3)
p->end_ = start;
else
{
Free_list_node newnode(p->start_, start);
p->start_ = end;
this->list_.insert(p, newnode);
++Free_list::num_nodes;
}
return start;
}
}
if (this->extend_)
{
off_t start = align_address(this->length_, align);
this->length_ = start + len;
return start;
}
return -1;
}
// Dump the free list (for debugging).
void
Free_list::dump()
{
gold_info("Free list:\n start end length\n");
for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
gold_info(" %08lx %08lx %08lx", static_cast<long>(p->start_),
static_cast<long>(p->end_),
static_cast<long>(p->end_ - p->start_));
}
// Print the statistics for the free lists.
void
Free_list::print_stats()
{
fprintf(stderr, _("%s: total free lists: %u\n"),
program_name, Free_list::num_lists);
fprintf(stderr, _("%s: total free list nodes: %u\n"),
program_name, Free_list::num_nodes);
fprintf(stderr, _("%s: calls to Free_list::remove: %u\n"),
program_name, Free_list::num_removes);
fprintf(stderr, _("%s: nodes visited: %u\n"),
program_name, Free_list::num_remove_visits);
fprintf(stderr, _("%s: calls to Free_list::allocate: %u\n"),
program_name, Free_list::num_allocates);
fprintf(stderr, _("%s: nodes visited: %u\n"),
program_name, Free_list::num_allocate_visits);
}
// Layout::Relaxation_debug_check methods.
// Check that sections and special data are in reset states.
// We do not save states for Output_sections and special Output_data.
// So we check that they have not assigned any addresses or offsets.
// clean_up_after_relaxation simply resets their addresses and offsets.
void
Layout::Relaxation_debug_check::check_output_data_for_reset_values(
const Layout::Section_list& sections,
const Layout::Data_list& special_outputs)
{
for(Layout::Section_list::const_iterator p = sections.begin();
p != sections.end();
++p)
gold_assert((*p)->address_and_file_offset_have_reset_values());
for(Layout::Data_list::const_iterator p = special_outputs.begin();
p != special_outputs.end();
++p)
gold_assert((*p)->address_and_file_offset_have_reset_values());
}
// Save information of SECTIONS for checking later.
void
Layout::Relaxation_debug_check::read_sections(
const Layout::Section_list& sections)
{
for(Layout::Section_list::const_iterator p = sections.begin();
p != sections.end();
++p)
{
Output_section* os = *p;
Section_info info;
info.output_section = os;
info.address = os->is_address_valid() ? os->address() : 0;
info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
info.offset = os->is_offset_valid()? os->offset() : -1 ;
this->section_infos_.push_back(info);
}
}
// Verify SECTIONS using previously recorded information.
void
Layout::Relaxation_debug_check::verify_sections(
const Layout::Section_list& sections)
{
size_t i = 0;
for(Layout::Section_list::const_iterator p = sections.begin();
p != sections.end();
++p, ++i)
{
Output_section* os = *p;
uint64_t address = os->is_address_valid() ? os->address() : 0;
off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
off_t offset = os->is_offset_valid()? os->offset() : -1 ;
if (i >= this->section_infos_.size())
{
gold_fatal("Section_info of %s missing.\n", os->name());
}
const Section_info& info = this->section_infos_[i];
if (os != info.output_section)
gold_fatal("Section order changed. Expecting %s but see %s\n",
info.output_section->name(), os->name());
if (address != info.address
|| data_size != info.data_size
|| offset != info.offset)
gold_fatal("Section %s changed.\n", os->name());
}
}
// Layout_task_runner methods.
// Lay out the sections. This is called after all the input objects
// have been read.
void
Layout_task_runner::run(Workqueue* workqueue, const Task* task)
{
Layout* layout = this->layout_;
off_t file_size = layout->finalize(this->input_objects_,
this->symtab_,
this->target_,
task);
// Now we know the final size of the output file and we know where
// each piece of information goes.
if (this->mapfile_ != NULL)
{
this->mapfile_->print_discarded_sections(this->input_objects_);
layout->print_to_mapfile(this->mapfile_);
}
Output_file* of;
if (layout->incremental_base() == NULL)
{
of = new Output_file(parameters->options().output_file_name());
if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
of->set_is_temporary();
of->open(file_size);
}
else
{
of = layout->incremental_base()->output_file();
// Apply the incremental relocations for symbols whose values
// have changed. We do this before we resize the file and start
// writing anything else to it, so that we can read the old
// incremental information from the file before (possibly)
// overwriting it.
if (parameters->incremental_update())
layout->incremental_base()->apply_incremental_relocs(this->symtab_,
this->layout_,
of);
of->resize(file_size);
}
// Queue up the final set of tasks.
gold::queue_final_tasks(this->options_, this->input_objects_,
this->symtab_, layout, workqueue, of);
}
// Layout methods.
Layout::Layout(int number_of_input_files, Script_options* script_options)
: number_of_input_files_(number_of_input_files),
script_options_(script_options),
namepool_(),
sympool_(),
dynpool_(),
signatures_(),
section_name_map_(),
segment_list_(),
section_list_(),
unattached_section_list_(),
special_output_list_(),
section_headers_(NULL),
tls_segment_(NULL),
relro_segment_(NULL),
interp_segment_(NULL),
increase_relro_(0),
symtab_section_(NULL),
symtab_xindex_(NULL),
dynsym_section_(NULL),
dynsym_xindex_(NULL),
dynamic_section_(NULL),
dynamic_symbol_(NULL),
dynamic_data_(NULL),
eh_frame_section_(NULL),
eh_frame_data_(NULL),
added_eh_frame_data_(false),
eh_frame_hdr_section_(NULL),
build_id_note_(NULL),
debug_abbrev_(NULL),
debug_info_(NULL),
group_signatures_(),
output_file_size_(-1),
have_added_input_section_(false),
sections_are_attached_(false),
input_requires_executable_stack_(false),
input_with_gnu_stack_note_(false),
input_without_gnu_stack_note_(false),
has_static_tls_(false),
any_postprocessing_sections_(false),
resized_signatures_(false),
have_stabstr_section_(false),
section_ordering_specified_(false),
incremental_inputs_(NULL),
record_output_section_data_from_script_(false),
script_output_section_data_list_(),
segment_states_(NULL),
relaxation_debug_check_(NULL),
input_section_position_(),
input_section_glob_(),
incremental_base_(NULL),
free_list_()
{
// Make space for more than enough segments for a typical file.
// This is just for efficiency--it's OK if we wind up needing more.
this->segment_list_.reserve(12);
// We expect two unattached Output_data objects: the file header and
// the segment headers.
this->special_output_list_.reserve(2);
// Initialize structure needed for an incremental build.
if (parameters->incremental())
this->incremental_inputs_ = new Incremental_inputs;
// The section name pool is worth optimizing in all cases, because
// it is small, but there are often overlaps due to .rel sections.
this->namepool_.set_optimize();
}
// For incremental links, record the base file to be modified.
void
Layout::set_incremental_base(Incremental_binary* base)
{
this->incremental_base_ = base;
this->free_list_.init(base->output_file()->filesize(), true);
}
// Hash a key we use to look up an output section mapping.
size_t
Layout::Hash_key::operator()(const Layout::Key& k) const
{
return k.first + k.second.first + k.second.second;
}
// Returns whether the given section is in the list of
// debug-sections-used-by-some-version-of-gdb. Currently,
// we've checked versions of gdb up to and including 6.7.1.
static const char* gdb_sections[] =
{ ".debug_abbrev",
// ".debug_aranges", // not used by gdb as of 6.7.1
".debug_frame",
".debug_info",
".debug_types",
".debug_line",
".debug_loc",
".debug_macinfo",
// ".debug_pubnames", // not used by gdb as of 6.7.1
".debug_ranges",
".debug_str",
};
static const char* lines_only_debug_sections[] =
{ ".debug_abbrev",
// ".debug_aranges", // not used by gdb as of 6.7.1
// ".debug_frame",
".debug_info",
// ".debug_types",
".debug_line",
// ".debug_loc",
// ".debug_macinfo",
// ".debug_pubnames", // not used by gdb as of 6.7.1
// ".debug_ranges",
".debug_str",
};
static inline bool
is_gdb_debug_section(const char* str)
{
// We can do this faster: binary search or a hashtable. But why bother?
for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
if (strcmp(str, gdb_sections[i]) == 0)
return true;
return false;
}
static inline bool
is_lines_only_debug_section(const char* str)
{
// We can do this faster: binary search or a hashtable. But why bother?
for (size_t i = 0;
i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
++i)
if (strcmp(str, lines_only_debug_sections[i]) == 0)
return true;
return false;
}
// Sometimes we compress sections. This is typically done for
// sections that are not part of normal program execution (such as
// .debug_* sections), and where the readers of these sections know
// how to deal with compressed sections. This routine doesn't say for
// certain whether we'll compress -- it depends on commandline options
// as well -- just whether this section is a candidate for compression.
// (The Output_compressed_section class decides whether to compress
// a given section, and picks the name of the compressed section.)
static bool
is_compressible_debug_section(const char* secname)
{
return (is_prefix_of(".debug", secname));
}
// We may see compressed debug sections in input files. Return TRUE
// if this is the name of a compressed debug section.
bool
is_compressed_debug_section(const char* secname)
{
return (is_prefix_of(".zdebug", secname));
}
// Whether to include this section in the link.
template<int size, bool big_endian>
bool
Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
const elfcpp::Shdr<size, big_endian>& shdr)
{
if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE)
return false;
switch (shdr.get_sh_type())
{
case elfcpp::SHT_NULL:
case elfcpp::SHT_SYMTAB:
case elfcpp::SHT_DYNSYM:
case elfcpp::SHT_HASH:
case elfcpp::SHT_DYNAMIC:
case elfcpp::SHT_SYMTAB_SHNDX:
return false;
case elfcpp::SHT_STRTAB:
// Discard the sections which have special meanings in the ELF
// ABI. Keep others (e.g., .stabstr). We could also do this by
// checking the sh_link fields of the appropriate sections.
return (strcmp(name, ".dynstr") != 0
&& strcmp(name, ".strtab") != 0
&& strcmp(name, ".shstrtab") != 0);
case elfcpp::SHT_RELA:
case elfcpp::SHT_REL:
case elfcpp::SHT_GROUP:
// If we are emitting relocations these should be handled
// elsewhere.
gold_assert(!parameters->options().relocatable()
&& !parameters->options().emit_relocs());
return false;
case elfcpp::SHT_PROGBITS:
if (parameters->options().strip_debug()
&& (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
{
if (is_debug_info_section(name))
return false;
}
if (parameters->options().strip_debug_non_line()
&& (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
{
// Debugging sections can only be recognized by name.
if (is_prefix_of(".debug", name)
&& !is_lines_only_debug_section(name))
return false;
}
if (parameters->options().strip_debug_gdb()
&& (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
{
// Debugging sections can only be recognized by name.
if (is_prefix_of(".debug", name)
&& !is_gdb_debug_section(name))
return false;
}
if (parameters->options().strip_lto_sections()
&& !parameters->options().relocatable()
&& (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
{
// Ignore LTO sections containing intermediate code.
if (is_prefix_of(".gnu.lto_", name))
return false;
}
// The GNU linker strips .gnu_debuglink sections, so we do too.
// This is a feature used to keep debugging information in
// separate files.
if (strcmp(name, ".gnu_debuglink") == 0)
return false;
return true;
default:
return true;
}
}
// Return an output section named NAME, or NULL if there is none.
Output_section*
Layout::find_output_section(const char* name) const
{
for (Section_list::const_iterator p = this->section_list_.begin();
p != this->section_list_.end();
++p)
if (strcmp((*p)->name(), name) == 0)
return *p;
return NULL;
}
// Return an output segment of type TYPE, with segment flags SET set
// and segment flags CLEAR clear. Return NULL if there is none.
Output_segment*
Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
elfcpp::Elf_Word clear) const
{
for (Segment_list::const_iterator p = this->segment_list_.begin();
p != this->segment_list_.end();
++p)
if (static_cast<elfcpp::PT>((*p)->type()) == type
&& ((*p)->flags() & set) == set
&& ((*p)->flags() & clear) == 0)
return *p;
return NULL;
}
// When we put a .ctors or .dtors section with more than one word into
// a .init_array or .fini_array section, we need to reverse the words
// in the .ctors/.dtors section. This is because .init_array executes
// constructors front to back, where .ctors executes them back to
// front, and vice-versa for .fini_array/.dtors. Although we do want
// to remap .ctors/.dtors into .init_array/.fini_array because it can
// be more efficient, we don't want to change the order in which
// constructors/destructors are run. This set just keeps track of
// these sections which need to be reversed. It is only changed by
// Layout::layout. It should be a private member of Layout, but that
// would require layout.h to #include object.h to get the definition
// of Section_id.
static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
// Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
// .init_array/.fini_array section.
bool
Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
{
return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
!= ctors_sections_in_init_array.end());
}
// Return the output section to use for section NAME with type TYPE
// and section flags FLAGS. NAME must be canonicalized in the string
// pool, and NAME_KEY is the key. ORDER is where this should appear
// in the output sections. IS_RELRO is true for a relro section.
Output_section*
Layout::get_output_section(const char* name, Stringpool::Key name_key,
elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
Output_section_order order, bool is_relro)
{
elfcpp::Elf_Word lookup_type = type;
// For lookup purposes, treat INIT_ARRAY, FINI_ARRAY, and
// PREINIT_ARRAY like PROGBITS. This ensures that we combine
// .init_array, .fini_array, and .preinit_array sections by name
// whatever their type in the input file. We do this because the
// types are not always right in the input files.
if (lookup_type == elfcpp::SHT_INIT_ARRAY
|| lookup_type == elfcpp::SHT_FINI_ARRAY
|| lookup_type == elfcpp::SHT_PREINIT_ARRAY)
lookup_type = elfcpp::SHT_PROGBITS;
elfcpp::Elf_Xword lookup_flags = flags;
// Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
// read-write with read-only sections. Some other ELF linkers do
// not do this. FIXME: Perhaps there should be an option
// controlling this.
lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
const Key key(name_key, std::make_pair(lookup_type, lookup_flags));
const std::pair<Key, Output_section*> v(key, NULL);
std::pair<Section_name_map::iterator, bool> ins(
this->section_name_map_.insert(v));
if (!ins.second)
return ins.first->second;
else
{
// This is the first time we've seen this name/type/flags
// combination. For compatibility with the GNU linker, we
// combine sections with contents and zero flags with sections
// with non-zero flags. This is a workaround for cases where
// assembler code forgets to set section flags. FIXME: Perhaps
// there should be an option to control this.
Output_section* os = NULL;
if (lookup_type == elfcpp::SHT_PROGBITS)
{
if (flags == 0)
{
Output_section* same_name = this->find_output_section(name);
if (same_name != NULL
&& (same_name->type() == elfcpp::SHT_PROGBITS
|| same_name->type() == elfcpp::SHT_INIT_ARRAY
|| same_name->type() == elfcpp::SHT_FINI_ARRAY
|| same_name->type() == elfcpp::SHT_PREINIT_ARRAY)
&& (same_name->flags() & elfcpp::SHF_TLS) == 0)
os = same_name;
}
else if ((flags & elfcpp::SHF_TLS) == 0)
{
elfcpp::Elf_Xword zero_flags = 0;
const Key zero_key(name_key, std::make_pair(lookup_type,
zero_flags));
Section_name_map::iterator p =
this->section_name_map_.find(zero_key);
if (p != this->section_name_map_.end())
os = p->second;
}
}
if (os == NULL)
os = this->make_output_section(name, type, flags, order, is_relro);
ins.first->second = os;
return os;
}
}
// Pick the output section to use for section NAME, in input file
// RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
// linker created section. IS_INPUT_SECTION is true if we are
// choosing an output section for an input section found in a input
// file. ORDER is where this section should appear in the output
// sections. IS_RELRO is true for a relro section. This will return
// NULL if the input section should be discarded.
Output_section*
Layout::choose_output_section(const Relobj* relobj, const char* name,
elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
bool is_input_section, Output_section_order order,
bool is_relro)
{
// We should not see any input sections after we have attached
// sections to segments.
gold_assert(!is_input_section || !this->sections_are_attached_);
// Some flags in the input section should not be automatically
// copied to the output section.
flags &= ~ (elfcpp::SHF_INFO_LINK
| elfcpp::SHF_GROUP
| elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS);
// We only clear the SHF_LINK_ORDER flag in for
// a non-relocatable link.
if (!parameters->options().relocatable())
flags &= ~elfcpp::SHF_LINK_ORDER;
if (this->script_options_->saw_sections_clause())
{
// We are using a SECTIONS clause, so the output section is
// chosen based only on the name.
Script_sections* ss = this->script_options_->script_sections();
const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
Output_section** output_section_slot;
Script_sections::Section_type script_section_type;
const char* orig_name = name;
name = ss->output_section_name(file_name, name, &output_section_slot,
&script_section_type);
if (name == NULL)
{
gold_debug(DEBUG_SCRIPT, _("Unable to create output section '%s' "
"because it is not allowed by the "
"SECTIONS clause of the linker script"),
orig_name);
// The SECTIONS clause says to discard this input section.
return NULL;
}
// We can only handle script section types ST_NONE and ST_NOLOAD.
switch (script_section_type)
{
case Script_sections::ST_NONE:
break;
case Script_sections::ST_NOLOAD:
flags &= elfcpp::SHF_ALLOC;
break;
default:
gold_unreachable();
}
// If this is an orphan section--one not mentioned in the linker
// script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
// default processing below.
if (output_section_slot != NULL)
{
if (*output_section_slot != NULL)
{
(*output_section_slot)->update_flags_for_input_section(flags);
return *output_section_slot;
}
// We don't put sections found in the linker script into
// SECTION_NAME_MAP_. That keeps us from getting confused
// if an orphan section is mapped to a section with the same
// name as one in the linker script.
name = this->namepool_.add(name, false, NULL);
Output_section* os = this->make_output_section(name, type, flags,
order, is_relro);
os->set_found_in_sections_clause();
// Special handling for NOLOAD sections.
if (script_section_type == Script_sections::ST_NOLOAD)
{
os->set_is_noload();
// The constructor of Output_section sets addresses of non-ALLOC
// sections to 0 by default. We don't want that for NOLOAD
// sections even if they have no SHF_ALLOC flag.
if ((os->flags() & elfcpp::SHF_ALLOC) == 0
&& os->is_address_valid())
{
gold_assert(os->address() == 0
&& !os->is_offset_valid()
&& !os->is_data_size_valid());
os->reset_address_and_file_offset();
}
}
*output_section_slot = os;
return os;
}
}
// FIXME: Handle SHF_OS_NONCONFORMING somewhere.
size_t len = strlen(name);
char* uncompressed_name = NULL;
// Compressed debug sections should be mapped to the corresponding
// uncompressed section.
if (is_compressed_debug_section(name))
{
uncompressed_name = new char[len];
uncompressed_name[0] = '.';
gold_assert(name[0] == '.' && name[1] == 'z');
strncpy(&uncompressed_name[1], &name[2], len - 2);
uncompressed_name[len - 1] = '\0';
len -= 1;
name = uncompressed_name;
}
// Turn NAME from the name of the input section into the name of the
// output section.
if (is_input_section
&& !this->script_options_->saw_sections_clause()
&& !parameters->options().relocatable())
name = Layout::output_section_name(relobj, name, &len);
Stringpool::Key name_key;
name = this->namepool_.add_with_length(name, len, true, &name_key);
if (uncompressed_name != NULL)
delete[] uncompressed_name;
// Find or make the output section. The output section is selected
// based on the section name, type, and flags.
return this->get_output_section(name, name_key, type, flags, order, is_relro);
}
// For incremental links, record the initial fixed layout of a section
// from the base file, and return a pointer to the Output_section.
template<int size, bool big_endian>
Output_section*
Layout::init_fixed_output_section(const char* name,
elfcpp::Shdr<size, big_endian>& shdr)
{
unsigned int sh_type = shdr.get_sh_type();
// We preserve the layout of PROGBITS, NOBITS, and NOTE sections.
// All others will be created from scratch and reallocated.
if (sh_type != elfcpp::SHT_PROGBITS
&& sh_type != elfcpp::SHT_NOBITS
&& sh_type != elfcpp::SHT_NOTE)
return NULL;
typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
shdr.get_sh_addralign();
// Make the output section.
Stringpool::Key name_key;
name = this->namepool_.add(name, true, &name_key);
Output_section* os = this->get_output_section(name, name_key, sh_type,
sh_flags, ORDER_INVALID, false);
os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
if (sh_type != elfcpp::SHT_NOBITS)
this->free_list_.remove(sh_offset, sh_offset + sh_size);
return os;
}
// Return the output section to use for input section SHNDX, with name
// NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
// index of a relocation section which applies to this section, or 0
// if none, or -1U if more than one. RELOC_TYPE is the type of the
// relocation section if there is one. Set *OFF to the offset of this
// input section without the output section. Return NULL if the
// section should be discarded. Set *OFF to -1 if the section
// contents should not be written directly to the output file, but
// will instead receive special handling.
template<int size, bool big_endian>
Output_section*
Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
unsigned int reloc_shndx, unsigned int, off_t* off)
{
*off = 0;
if (!this->include_section(object, name, shdr))
return NULL;
elfcpp::Elf_Word sh_type = shdr.get_sh_type();
// In a relocatable link a grouped section must not be combined with
// any other sections.
Output_section* os;
if (parameters->options().relocatable()
&& (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
{
name = this->namepool_.add(name, true, NULL);
os = this->make_output_section(name, sh_type, shdr.get_sh_flags(),
ORDER_INVALID, false);
}
else
{
os = this->choose_output_section(object, name, sh_type,
shdr.get_sh_flags(), true,
ORDER_INVALID, false);
if (os == NULL)
return NULL;
}
// By default the GNU linker sorts input sections whose names match
// .ctors.*, .dtors.*, .init_array.*, or .fini_array.*. The
// sections are sorted by name. This is used to implement
// constructor priority ordering. We are compatible. When we put
// .ctor sections in .init_array and .dtor sections in .fini_array,
// we must also sort plain .ctor and .dtor sections.
if (!this->script_options_->saw_sections_clause()
&& !parameters->options().relocatable()
&& (is_prefix_of(".ctors.", name)
|| is_prefix_of(".dtors.", name)
|| is_prefix_of(".init_array.", name)
|| is_prefix_of(".fini_array.", name)
|| (parameters->options().ctors_in_init_array()
&& (strcmp(name, ".ctors") == 0
|| strcmp(name, ".dtors") == 0))))
os->set_must_sort_attached_input_sections();
// If this is a .ctors or .ctors.* section being mapped to a
// .init_array section, or a .dtors or .dtors.* section being mapped
// to a .fini_array section, we will need to reverse the words if
// there is more than one. Record this section for later. See
// ctors_sections_in_init_array above.
if (!this->script_options_->saw_sections_clause()
&& !parameters->options().relocatable()
&& shdr.get_sh_size() > size / 8
&& (((strcmp(name, ".ctors") == 0
|| is_prefix_of(".ctors.", name))
&& strcmp(os->name(), ".init_array") == 0)
|| ((strcmp(name, ".dtors") == 0
|| is_prefix_of(".dtors.", name))
&& strcmp(os->name(), ".fini_array") == 0)))
ctors_sections_in_init_array.insert(Section_id(object, shndx));
// FIXME: Handle SHF_LINK_ORDER somewhere.
elfcpp::Elf_Xword orig_flags = os->flags();
*off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,