forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_file.cc
1498 lines (1257 loc) · 46.5 KB
/
object_file.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
#include "mold.h"
#include <cstring>
#include <regex>
#include <unistd.h>
#include <zlib.h>
template <typename E>
InputFile<E>::InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb)
: mb(mb), filename(mb->name) {
if (mb->size() < sizeof(ElfEhdr<E>))
Fatal(ctx) << *this << ": file too small";
if (memcmp(mb->data(ctx), "\177ELF", 4))
Fatal(ctx) << *this << ": not an ELF file";
ElfEhdr<E> &ehdr = *(ElfEhdr<E> *)mb->data(ctx);
is_dso = (ehdr.e_type == ET_DYN);
ElfShdr<E> *sh_begin = (ElfShdr<E> *)(mb->data(ctx) + ehdr.e_shoff);
// e_shnum contains the total number of sections in an object file.
// Since it is a 16-bit integer field, it's not large enough to
// represent >65535 sections. If an object file contains more than 65535
// sections, the actual number is stored to sh_size field.
i64 num_sections = (ehdr.e_shnum == 0) ? sh_begin->sh_size : ehdr.e_shnum;
if (mb->data(ctx) + mb->size() < (u8 *)(sh_begin + num_sections))
Fatal(ctx) << *this << ": e_shoff or e_shnum corrupted: "
<< mb->size() << " " << num_sections;
elf_sections = {sh_begin, sh_begin + num_sections};
// e_shstrndx is a 16-bit field. If .shstrtab's section index is
// too large, the actual number is stored to sh_link field.
i64 shstrtab_idx = (ehdr.e_shstrndx == SHN_XINDEX)
? sh_begin->sh_link : ehdr.e_shstrndx;
shstrtab = this->get_string(ctx, shstrtab_idx);
}
template <typename E>
ElfShdr<E> *InputFile<E>::find_section(i64 type) {
for (ElfShdr<E> &sec : elf_sections)
if (sec.sh_type == type)
return &sec;
return nullptr;
}
template <typename E>
ObjectFile<E>::ObjectFile(Context<E> &ctx, MemoryMappedFile<E> *mb,
std::string archive_name, bool is_in_lib)
: InputFile<E>(ctx, mb), archive_name(archive_name), is_in_lib(is_in_lib) {
this->is_alive = !is_in_lib;
}
template <typename E>
ObjectFile<E>::ObjectFile() {}
template <typename E>
ObjectFile<E> *
ObjectFile<E>::create(Context<E> &ctx, MemoryMappedFile<E> *mb,
std::string archive_name, bool is_in_lib) {
ObjectFile<E> *obj = new ObjectFile<E>(ctx, mb, archive_name, is_in_lib);
ctx.owning_objs.push_back(std::unique_ptr<ObjectFile<E>>(obj));
return obj;
}
template <typename E>
static bool is_debug_section(const ElfShdr<E> &shdr, std::string_view name) {
return !(shdr.sh_flags & SHF_ALLOC) &&
(name.starts_with(".debug") || name.starts_with(".zdebug"));
}
template <typename E>
u32 ObjectFile<E>::read_note_gnu_property(Context<E> &ctx,
const ElfShdr<E> &shdr) {
std::string_view data = this->get_string(ctx, shdr);
u32 ret = 0;
while (!data.empty()) {
ElfNhdr<E> &hdr = *(ElfNhdr<E> *)data.data();
data = data.substr(sizeof(hdr));
std::string_view name = data.substr(0, hdr.n_namesz - 1);
data = data.substr(align_to(hdr.n_namesz, 4));
std::string_view desc = data.substr(0, hdr.n_descsz);
data = data.substr(align_to(hdr.n_descsz, E::wordsize));
if (hdr.n_type != NT_GNU_PROPERTY_TYPE_0 || name != "GNU")
continue;
while (!desc.empty()) {
u32 type = *(u32 *)desc.data();
u32 size = *(u32 *)(desc.data() + 4);
desc = desc.substr(8);
if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
ret |= *(u32 *)desc.data();
desc = desc.substr(align_to(size, E::wordsize));
}
}
return ret;
}
template <typename E>
std::pair<std::string_view, const ElfShdr<E> *>
ObjectFile<E>::uncompress_contents(Context<E> &ctx, const ElfShdr<E> &shdr,
std::string_view name) {
if (shdr.sh_type == SHT_NOBITS)
return {{}, &shdr};
auto do_uncompress = [&](std::string_view data, u64 size) {
u8 *buf = new u8[size];
ctx.owning_bufs.push_back(std::unique_ptr<u8[]>(buf));
unsigned long size2 = size;
if (uncompress(buf, &size2, (u8 *)&data[0], data.size()) != Z_OK)
Fatal(ctx) << *this << ": " << name << ": uncompress failed";
if (size != size2)
Fatal(ctx) << *this << ": " << name << ": uncompress: invalid size";
return std::string_view((char *)buf, size);
};
auto copy_shdr = [&](const ElfShdr<E> &shdr) {
ElfShdr<E> *ret = new ElfShdr<E>;
ctx.owning_shdrs.push_back(std::unique_ptr<ElfShdr<E>>(ret));
*ret = shdr;
return ret;
};
if (name.starts_with(".zdebug")) {
// Old-style compressed section
std::string_view data = this->get_string(ctx, shdr);
if (!data.starts_with("ZLIB") || data.size() <= 12)
Fatal(ctx) << *this << ": " << name << ": corrupted compressed section";
u64 size = read64be((u8 *)&data[4]);
std::string_view contents = do_uncompress(data.substr(12), size);
ElfShdr<E> *shdr2 = copy_shdr(shdr);
shdr2->sh_size = size;
return {contents, shdr2};
}
if (shdr.sh_flags & SHF_COMPRESSED) {
// New-style compressed section
std::string_view data = this->get_string(ctx, shdr);
if (data.size() < sizeof(ElfChdr<E>))
Fatal(ctx) << *this << ": " << name << ": corrupted compressed section";
ElfChdr<E> &hdr = *(ElfChdr<E> *)&data[0];
data = data.substr(sizeof(ElfChdr<E>));
if (hdr.ch_type != ELFCOMPRESS_ZLIB)
Fatal(ctx) << *this << ": " << name << ": unsupported compression type";
ElfShdr<E> *shdr2 = copy_shdr(shdr);
shdr2->sh_flags &= ~(u64)(SHF_COMPRESSED);
shdr2->sh_size = hdr.ch_size;
shdr2->sh_addralign = hdr.ch_addralign;
std::string_view contents = do_uncompress(data, hdr.ch_size);
return {contents, shdr2};
}
return {this->get_string(ctx, shdr), &shdr};
}
template <typename E>
void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
// Read sections
for (i64 i = 0; i < this->elf_sections.size(); i++) {
const ElfShdr<E> &shdr = this->elf_sections[i];
if ((shdr.sh_flags & SHF_EXCLUDE) && !(shdr.sh_flags & SHF_ALLOC))
continue;
switch (shdr.sh_type) {
case SHT_GROUP: {
// Get the signature of this section group.
if (shdr.sh_info >= elf_syms.size())
Fatal(ctx) << *this << ": invalid symbol index";
const ElfSym<E> &sym = elf_syms[shdr.sh_info];
std::string_view signature = symbol_strtab.data() + sym.st_name;
// Get comdat group members.
std::span<u32> entries = this->template get_data<u32>(ctx, shdr);
if (entries.empty())
Fatal(ctx) << *this << ": empty SHT_GROUP";
if (entries[0] == 0)
continue;
if (entries[0] != GRP_COMDAT)
Fatal(ctx) << *this << ": unsupported SHT_GROUP format";
ComdatGroup *group = ctx.comdat_groups.insert(signature, ComdatGroup());
comdat_groups.push_back({group, entries.subspan(1)});
break;
}
case SHT_SYMTAB_SHNDX:
symtab_shndx_sec = this->template get_data<u32>(ctx, shdr);
break;
case SHT_SYMTAB:
case SHT_STRTAB:
case SHT_REL:
case SHT_RELA:
case SHT_NULL:
break;
default: {
std::string_view name = this->shstrtab.data() + shdr.sh_name;
if (name == ".note.GNU-stack" || name.starts_with(".gnu.warning."))
continue;
if (name == ".note.gnu.property") {
this->features = read_note_gnu_property(ctx, shdr);
continue;
}
if ((ctx.arg.strip_all || ctx.arg.strip_debug) &&
is_debug_section(shdr, name))
continue;
std::string_view contents;
const ElfShdr<E> *shdr2;
std::tie(contents, shdr2) = uncompress_contents(ctx, shdr, name);
this->sections[i] =
std::make_unique<InputSection<E>>(ctx, *this, *shdr2, name,
contents, i);
static Counter counter("regular_sections");
counter++;
break;
}
}
}
// Attach relocation sections to their target sections.
for (i64 i = 0; i < this->elf_sections.size(); i++) {
const ElfShdr<E> &shdr = this->elf_sections[i];
if (shdr.sh_type != (E::is_rel ? SHT_REL : SHT_RELA))
continue;
if (shdr.sh_info >= sections.size())
Fatal(ctx) << *this << ": invalid relocated section index: "
<< (u32)shdr.sh_info;
if (std::unique_ptr<InputSection<E>> &target = sections[shdr.sh_info]) {
ASSERT(target->relsec_idx == -1);
target->relsec_idx = i;
if (target->shdr.sh_flags & SHF_ALLOC) {
i64 size = shdr.sh_size / sizeof(ElfRel<E>);
target->rel_exprs.reset(new u8[size]);
memset(&target->rel_exprs[0], 0, size);
}
}
}
}
template <typename E>
void ObjectFile<E>::initialize_ehframe_sections(Context<E> &ctx) {
for (i64 i = 0; i < sections.size(); i++) {
std::unique_ptr<InputSection<E>> &isec = sections[i];
if (isec && isec->is_alive && isec->name() == ".eh_frame") {
read_ehframe(ctx, *isec);
isec->is_ehframe = true;
isec->is_alive = false;
}
}
for (FdeRecord<E> &fde : fdes)
fde.cie = &cies[fde.cie_idx];
}
// .eh_frame contains data records explaining how to handle exceptions.
// When an exception is thrown, the runtime searches a record from
// .eh_frame with the current program counter as a key. A record that
// covers the current PC explains how to find a handler and how to
// transfer the control ot it.
//
// Unlike the most other sections, linker has to parse .eh_frame contents
// because of the following reasons:
//
// - There's usually only one .eh_frame section for each object file,
// which explains how to handle exceptions for all functions in the same
// object. If we just copy them, the resulting .eh_frame section will
// contain lots of records for dead sections (i.e. de-duplicated inline
// functions). We want to copy only records for live functions.
//
// - .eh_frame contains two types of records: CIE and FDE. There's usually
// only one CIE at beginning of .eh_frame section followed by FDEs.
// Compiler usually emits the identical CIE record for all object files.
// We want to merge identical CIEs in an output .eh_frame section to
// reduce the section size.
//
// - Scanning a .eh_frame section to find a record is an O(n) operation
// where n is the number of records in the section. To reduce it to
// O(log n), linker creates a .eh_frame_hdr section. The section
// contains a sorted list of [an address in .text, an FDE address whose
// coverage starts at the .text address] to make binary search doable.
// In order to create .eh_frame_hdr, linker has to read .eh_frame.
//
// This function parses an input .eh_frame section.
template <typename E>
void ObjectFile<E>::read_ehframe(Context<E> &ctx, InputSection<E> &isec) {
std::span<ElfRel<E>> rels = isec.get_rels(ctx);
i64 cies_begin = cies.size();
i64 fdes_begin = fdes.size();
// Verify relocations.
for (i64 i = 1; i < rels.size(); i++)
if (rels[i].r_type != E::R_NONE &&
rels[i].r_offset <= rels[i - 1].r_offset)
Fatal(ctx) << isec << ": relocation offsets must increase monotonically";
// Read CIEs and FDEs until empty.
std::string_view contents = this->get_string(ctx, isec.shdr);
i64 rel_idx = 0;
for (std::string_view data = contents; !data.empty();) {
i64 size = *(u32 *)data.data();
if (size == 0) {
if (data.size() != 4)
Fatal(ctx) << isec << ": garbage at end of section";
break;
}
i64 begin_offset = data.data() - contents.data();
i64 end_offset = begin_offset + size + 4;
i64 id = *(u32 *)(data.data() + 4);
data = data.substr(size + 4);
i64 rel_begin = rel_idx;
while (rel_idx < rels.size() && rels[rel_idx].r_offset < end_offset)
rel_idx++;
ASSERT(rel_idx == rels.size() || begin_offset <= rels[rel_begin].r_offset);
if (id == 0) {
// This is CIE.
cies.push_back(CieRecord<E>(ctx, *this, isec, begin_offset, rel_begin));
} else {
// This is FDE.
if (rel_begin == rel_idx) {
// FDE has no valid relocation, which means FDE is dead from
// the beginning. Compilers usually don't create such FDE, but
// `ld -r` tend to generate such dead FDEs.
continue;
}
if (rels[rel_begin].r_offset - begin_offset != 8)
Fatal(ctx) << isec << ": FDE's first relocation should have offset 8";
fdes.push_back(FdeRecord<E>(begin_offset, rel_begin));
}
}
// Associate CIEs to FDEs.
auto find_cie = [&](i64 offset) {
for (i64 i = cies_begin; i < cies.size(); i++)
if (cies[i].input_offset == offset)
return i;
Fatal(ctx) << isec << ": bad FDE pointer";
};
for (i64 i = fdes_begin; i < fdes.size(); i++) {
i64 cie_offset = *(i32 *)(contents.data() + fdes[i].input_offset + 4);
fdes[i].cie_idx = find_cie(fdes[i].input_offset + 4 - cie_offset);
}
auto get_isec = [&](const FdeRecord<E> &fde) -> InputSection<E> * {
return get_section(elf_syms[rels[fde.rel_idx].r_sym]);
};
// We assume that FDEs for the same input sections are contiguous
// in `fdes` vector.
std::stable_sort(fdes.begin() + fdes_begin, fdes.end(),
[&](const FdeRecord<E> &a, const FdeRecord<E> &b) {
return get_isec(a)->get_priority() < get_isec(b)->get_priority();
});
// Associate FDEs to input sections.
for (i64 i = fdes_begin; i < fdes.size();) {
InputSection<E> *isec = get_isec(fdes[i]);
ASSERT(isec->fde_begin == -1);
isec->fde_begin = i++;
while (i < fdes.size() && isec == get_isec(fdes[i]))
i++;
isec->fde_end = i;
}
}
template <typename E>
static bool should_write_to_local_symtab(Context<E> &ctx, Symbol<E> &sym) {
if (ctx.arg.discard_all || ctx.arg.strip_all || ctx.arg.retain_symbols_file)
return false;
if (sym.get_type() == STT_SECTION)
return false;
// Local symbols are discarded if --discard-local is given or they
// are not in a mergeable section. I *believe* we exclude symbols in
// mergeable sections because (1) they are too many and (2) they are
// merged, so their origins shouldn't matter, but I dont' really
// know the rationale. Anyway, this is the behavior of the
// traditional linkers.
if (sym.name().starts_with(".L")) {
if (ctx.arg.discard_locals)
return false;
if (InputSection<E> *isec = sym.input_section)
if (isec->shdr.sh_flags & SHF_MERGE)
return false;
}
return true;
}
// Returns a symbol object for a given key. This function handles
// the -wrap option.
template <typename E>
static Symbol<E> *insert_symbol(Context<E> &ctx, const ElfSym<E> &esym,
std::string_view key, std::string_view name) {
if (esym.is_undef() && name.starts_with("__real_") &&
ctx.arg.wrap.count(name.substr(7))) {
return Symbol<E>::intern(ctx, key.substr(7), name.substr(7));
}
Symbol<E> *sym = Symbol<E>::intern(ctx, key, name);
if (esym.is_undef() && sym->wrap) {
key = save_string(ctx, "__wrap_" + std::string(key));
name = save_string(ctx, "__wrap_" + std::string(name));
return Symbol<E>::intern(ctx, key, name);
}
return sym;
}
template <typename E>
void ObjectFile<E>::initialize_symbols(Context<E> &ctx) {
if (!symtab_sec)
return;
static Counter counter("all_syms");
counter += elf_syms.size();
// Initialize local symbols
this->local_syms.reset(new Symbol<E>[first_global]);
new (&this->local_syms[0]) Symbol<E>;
for (i64 i = 1; i < first_global; i++) {
const ElfSym<E> &esym = elf_syms[i];
std::string_view name = symbol_strtab.data() + esym.st_name;
if (name.empty() && esym.st_type == STT_SECTION)
if (InputSection<E> *sec = get_section(esym))
name = sec->name();
Symbol<E> &sym = this->local_syms[i];
new (&sym) Symbol<E>(name);
sym.file = this;
sym.value = esym.st_value;
sym.sym_idx = i;
if (!esym.is_abs()) {
if (esym.is_common())
Fatal(ctx) << *this << ": common local symbol?";
sym.input_section = get_section(esym);
}
if (should_write_to_local_symtab(ctx, sym)) {
sym.write_to_symtab = true;
strtab_size += sym.name().size() + 1;
num_local_symtab++;
}
}
this->symbols.resize(elf_syms.size());
i64 num_globals = elf_syms.size() - first_global;
sym_fragments.resize(elf_syms.size());
symvers.resize(num_globals);
for (i64 i = 0; i < first_global; i++)
this->symbols[i] = &this->local_syms[i];
// Initialize global symbols
for (i64 i = first_global; i < elf_syms.size(); i++) {
const ElfSym<E> &esym = elf_syms[i];
// Get a symbol name
std::string_view key = symbol_strtab.data() + esym.st_name;
std::string_view name = key;
// Parse symbol version after atsign
if (i64 pos = name.find('@'); pos != name.npos) {
std::string_view ver = name.substr(pos + 1);
name = name.substr(0, pos);
if (!ver.empty() && ver != "@") {
if (ver.starts_with('@'))
key = name;
if (esym.is_defined())
symvers[i - first_global] = ver.data();
}
}
this->symbols[i] = insert_symbol(ctx, esym, key, name);
if (esym.is_common())
has_common_symbol = true;
}
}
template <typename E>
struct MergeableSection {
std::vector<SectionFragment<E> *> fragments;
std::vector<u32> frag_offsets;
};
static size_t find_null(std::string_view data, u64 entsize) {
if (entsize == 1)
return data.find('\0');
for (i64 i = 0; i <= data.size() - entsize; i += entsize)
if (data.substr(i, i + entsize).find_first_not_of('\0') == data.npos)
return i;
return data.npos;
}
// Mergeable sections (sections with SHF_MERGE bit) typically contain
// string literals. Linker is expected to split the section contents
// into null-terminated strings, merge them with mergeable strings
// from other object files, and emit uniquified strings to an output
// file.
//
// This mechanism reduces the size of an output file. If two source
// files happen to contain the same string literal, the output will
// contain only a single copy of it.
//
// It is less common than string literals, but mergeable sections can
// contain fixed-sized read-only records too.
//
// This function splits the section contents into small pieces that we
// call "section fragments". Section fragment is a unit of merging.
//
// We do not support mergeable sections that have relocations.
template <typename E>
static MergeableSection<E>
split_section(Context<E> &ctx, InputSection<E> &sec) {
MergeableSection<E> rec;
MergedSection<E> *parent =
MergedSection<E>::get_instance(ctx, sec.name(), sec.shdr.sh_type,
sec.shdr.sh_flags);
std::string_view data = sec.contents;
const char *begin = data.data();
u64 entsize = sec.shdr.sh_entsize;
static_assert(sizeof(SectionFragment<E>::alignment) == 2);
if (sec.shdr.sh_addralign >= UINT16_MAX)
Fatal(ctx) << sec << ": alignment too large";
if (sec.shdr.sh_flags & SHF_STRINGS) {
while (!data.empty()) {
size_t end = find_null(data, entsize);
if (end == data.npos)
Fatal(ctx) << sec << ": string is not null terminated";
std::string_view substr = data.substr(0, end + entsize);
data = data.substr(end + entsize);
SectionFragment<E> *frag = parent->insert(substr, sec.shdr.sh_addralign);
rec.fragments.push_back(frag);
rec.frag_offsets.push_back(substr.data() - begin);
}
} else {
if (data.size() % entsize)
Fatal(ctx) << sec << ": section size is not multiple of sh_entsize";
while (!data.empty()) {
std::string_view substr = data.substr(0, entsize);
data = data.substr(entsize);
SectionFragment<E> *frag = parent->insert(substr, sec.shdr.sh_addralign);
rec.fragments.push_back(frag);
rec.frag_offsets.push_back(substr.data() - begin);
}
}
static Counter counter("string_fragments");
counter += rec.fragments.size();
return rec;
}
// Usually a section is an atomic unit of inclusion and exclusion.
// the Linker doesn't care its contents. However, if a section is a
// mergeable section (a section with SHF_MERGE bit set), the linker
// is expected split it into smaller pieces and merge each piece with
// other pieces from different object files. In mold, we call the
// atomic unit of mergeable section "section pieces".
//
// This feature is typically used for string literals. String literals
// are usually put into a mergeable section by a compiler. If the same
// string literal happen to occur in two different translation units,
// a linker merges them into a single instance of a string, so that
// a linker's output doens't contain duplicate string literals.
//
// Handling relocations referring mergeable sections is a bit tricky.
// Assume that we have a mergeable section with the following contents
// and symbols:
//
//
// Hello world\0foo bar\0
// ^ ^
// .rodata .L.str1
// .L.str0
//
// '\0' represents a NUL byte. This mergeable section contains two
// section pieces, "Hello world" and "foo bar". The first string is
// referred by two symbols, .rodata and .L.str0, and the second by
// .L.str1. .rodata is a section symbol and therefore a local symbol
// and refers the begining of the section.
//
// In this example, there are actually two different ways to point to
// string "foo bar", because .rodata+12 and .L.str1+0 refer the same
// place in the section. This kind of "out-of-bound" reference occurs
// only when a symbol is a section symbol. In other words, compiler
// may use an offset from the beginning of a section to refer any
// section piece in a section, but it doesn't do for any other types
// of symbols.
//
// In mold, we attach section pieces to either relocations or symbols.
// If a relocation refers a section symbol whose section is a
// mergeable section, a section piece is attached to the relocation.
// If a non-section symbol refers a section piece, the section piece
// is attached to the symbol.
template <typename E>
void ObjectFile<E>::initialize_mergeable_sections(Context<E> &ctx) {
std::vector<MergeableSection<E>> mergeable_sections(sections.size());
for (i64 i = 0; i < sections.size(); i++) {
std::unique_ptr<InputSection<E>> &isec = sections[i];
if (isec && isec->is_alive && (isec->shdr.sh_flags & SHF_MERGE) &&
isec->relsec_idx == -1) {
mergeable_sections[i] = split_section(ctx, *isec);
isec->is_alive = false;
}
}
// Initialize rel_fragments
for (std::unique_ptr<InputSection<E>> &isec : sections) {
if (!isec || !isec->is_alive)
continue;
std::span<ElfRel<E>> rels = isec->get_rels(ctx);
if (rels.empty())
continue;
// Compute the size of rel_fragments.
i64 len = 0;
for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
const ElfSym<E> &esym = elf_syms[rel.r_sym];
if (esym.st_type == STT_SECTION) {
MergeableSection<E> &m = mergeable_sections[get_shndx(esym)];
if (!m.fragments.empty())
len++;
}
}
if (len == 0)
continue;
isec->rel_fragments.reset(new SectionFragmentRef<E>[len + 1]);
i64 frag_idx = 0;
// Fill rel_fragments contents.
for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
const ElfSym<E> &esym = elf_syms[rel.r_sym];
if (esym.st_type != STT_SECTION)
continue;
MergeableSection<E> &m = mergeable_sections[get_shndx(esym)];
if (m.fragments.empty())
continue;
i64 offset = esym.st_value + isec->get_addend(rel);
std::span<u32> offsets = m.frag_offsets;
auto it = std::upper_bound(offsets.begin(), offsets.end(), offset);
if (it == offsets.begin())
Fatal(ctx) << *this << ": bad relocation at " << rel.r_sym;
i64 idx = it - 1 - offsets.begin();
isec->rel_fragments[frag_idx++] = {m.fragments[idx], (i32)i,
(i32)(offset - offsets[idx])};
}
isec->rel_fragments[frag_idx++] = {nullptr, -1, -1};
}
// Initialize sym_fragments
for (i64 i = 0; i < elf_syms.size(); i++) {
const ElfSym<E> &esym = elf_syms[i];
if (esym.is_abs() || esym.is_common())
continue;
MergeableSection<E> &m = mergeable_sections[get_shndx(esym)];
if (m.fragments.empty())
continue;
std::span<u32> offsets = m.frag_offsets;
auto it = std::upper_bound(offsets.begin(), offsets.end(), esym.st_value);
if (it == offsets.begin())
Fatal(ctx) << *this << ": bad symbol value: " << esym.st_value;
i64 idx = it - 1 - offsets.begin();
if (i < first_global)
this->symbols[i]->value = esym.st_value - offsets[idx];
sym_fragments[i].frag = m.fragments[idx];
sym_fragments[i].addend = esym.st_value - offsets[idx];
}
for (MergeableSection<E> &m : mergeable_sections)
fragments.insert(fragments.end(), m.fragments.begin(), m.fragments.end());
}
template <typename E>
void ObjectFile<E>::parse(Context<E> &ctx) {
sections.resize(this->elf_sections.size());
symtab_sec = this->find_section(SHT_SYMTAB);
if (symtab_sec) {
first_global = symtab_sec->sh_info;
elf_syms = this->template get_data<ElfSym<E>>(ctx, *symtab_sec);
symbol_strtab = this->get_string(ctx, symtab_sec->sh_link);
}
initialize_sections(ctx);
initialize_symbols(ctx);
initialize_mergeable_sections(ctx);
initialize_ehframe_sections(ctx);
}
// Symbols with higher priorities overwrites symbols with lower priorities.
// Here is the list of priorities, from the highest to the lowest.
//
// 1. Strong defined symbol
// 2. Weak defined symbol
// 3. Strong defined symbol in a DSO
// 4. Weak defined symbol in a DSO
// 5. Strong or weak defined symbol in an archive
// 6. Common symbol
// 7. Unclaimed (nonexistent) symbol
//
// Ties are broken by file priority.
template <typename E>
static u64 get_rank(InputFile<E> *file, const ElfSym<E> &esym, bool is_lazy) {
if (esym.is_common())
return (6 << 24) + file->priority;
if (is_lazy)
return (5 << 24) + file->priority;
if (file->is_dso) {
if (esym.is_weak())
return (4 << 24) + file->priority;
return (3 << 24) + file->priority;
}
if (esym.is_weak())
return (2 << 24) + file->priority;
return (1 << 24) + file->priority;
}
template <typename E>
static u64 get_rank(const Symbol<E> &sym) {
if (!sym.file)
return 7 << 24;
return get_rank(sym.file, sym.esym(), sym.is_lazy);
}
template <typename E>
void ObjectFile<E>::override_symbol(Context<E> &ctx, Symbol<E> &sym,
const ElfSym<E> &esym, i64 symidx) {
sym.file = this;
sym.input_section = esym.is_abs() ? nullptr : get_section(esym);
if (SectionFragmentRef<E> &ref = sym_fragments[symidx]; ref.frag)
sym.value = ref.addend;
else
sym.value = esym.st_value;
sym.sym_idx = symidx;
sym.ver_idx = ctx.arg.default_version;
sym.is_lazy = false;
sym.is_weak = esym.is_weak();
sym.is_imported = false;
sym.is_exported = false;
}
template <typename E>
void ObjectFile<E>::merge_visibility(Context<E> &ctx, Symbol<E> &sym,
u8 visibility) {
// Canonicalize visibility
if (visibility == STV_INTERNAL)
visibility = STV_HIDDEN;
auto priority = [&](u8 visibility) {
switch (visibility) {
case STV_HIDDEN:
return 1;
case STV_PROTECTED:
return 2;
case STV_DEFAULT:
return 3;
}
Fatal(ctx) << *this << ": unknown symbol visibility: " << sym;
};
u8 val = sym.visibility;
while (priority(visibility) < priority(val))
if (sym.visibility.compare_exchange_strong(val, visibility))
break;
}
template <typename E>
void ObjectFile<E>::resolve_lazy_symbols(Context<E> &ctx) {
ASSERT(is_in_lib);
for (i64 i = first_global; i < this->symbols.size(); i++) {
Symbol<E> &sym = *this->symbols[i];
const ElfSym<E> &esym = elf_syms[i];
if (esym.is_undef() || esym.is_common())
continue;
std::lock_guard lock(sym.mu);
if (get_rank(this, esym, true) < get_rank(sym)) {
sym.file = this;
sym.sym_idx = i;
sym.is_lazy = true;
sym.is_weak = false;
if (sym.traced)
SyncOut(ctx) << "trace-symbol: " << *this
<< ": lazy definition of " << sym;
}
}
}
template <typename E>
void ObjectFile<E>::resolve_regular_symbols(Context<E> &ctx) {
ASSERT(!is_in_lib);
for (i64 i = first_global; i < this->symbols.size(); i++) {
Symbol<E> &sym = *this->symbols[i];
const ElfSym<E> &esym = elf_syms[i];
if (esym.is_undef() || esym.is_common())
continue;
std::lock_guard lock(sym.mu);
if (get_rank(this, esym, false) < get_rank(sym))
override_symbol(ctx, sym, esym, i);
}
}
template <typename E>
void
ObjectFile<E>::mark_live_objects(Context<E> &ctx,
std::function<void(ObjectFile<E> *)> feeder) {
ASSERT(this->is_alive);
for (i64 i = first_global; i < this->symbols.size(); i++) {
const ElfSym<E> &esym = elf_syms[i];
Symbol<E> &sym = *this->symbols[i];
merge_visibility(ctx, sym, exclude_libs ? STV_HIDDEN : esym.st_visibility);
if (sym.traced) {
if (esym.is_defined())
SyncOut(ctx) << "trace-symbol: " << *this << ": definition of " << sym;
else if (esym.is_weak())
SyncOut(ctx) << "trace-symbol: " << *this << ": weak reference to " << sym;
else
SyncOut(ctx) << "trace-symbol: " << *this << ": reference to " << sym;
}
std::lock_guard lock(sym.mu);
if (esym.is_undef() || esym.is_common()) {
if (!esym.is_weak() && sym.file && !sym.file->is_alive.exchange(true)) {
feeder((ObjectFile<E> *)sym.file);
if (sym.traced)
SyncOut(ctx) << "trace-symbol: " << *this << " keeps " << *sym.file
<< " for " << sym;
}
continue;
}
if (get_rank(this, esym, false) < get_rank(sym))
override_symbol(ctx, sym, esym, i);
}
}
template <typename E>
void ObjectFile<E>::resolve_common_symbols(Context<E> &ctx) {
if (!has_common_symbol)
return;
for (i64 i = first_global; i < this->symbols.size(); i++) {
const ElfSym<E> &esym = elf_syms[i];
if (!esym.is_common())
continue;
Symbol<E> &sym = *this->symbols[i];
std::lock_guard lock(sym.mu);
if (get_rank(this, esym, false) < get_rank(sym)) {
sym.file = this;
sym.input_section = nullptr;
sym.value = esym.st_value;
sym.sym_idx = i;
sym.ver_idx = ctx.arg.default_version;
sym.is_lazy = false;
sym.is_weak = false;
sym.is_imported = false;
sym.is_exported = false;
if (sym.traced)
SyncOut(ctx) << "trace-symbol: " << *this
<< ": common definition of " << sym;
}
}
}
template <typename E>
void ObjectFile<E>::resolve_comdat_groups() {
for (auto &pair : comdat_groups) {
ComdatGroup *group = pair.first;
u32 cur = group->owner;
while (cur == -1 || cur > this->priority)
if (group->owner.compare_exchange_weak(cur, this->priority))
break;
}
}
template <typename E>
void ObjectFile<E>::eliminate_duplicate_comdat_groups() {
for (auto &pair : comdat_groups) {
ComdatGroup *group = pair.first;
if (group->owner == this->priority)
continue;
std::span<u32> entries = pair.second;
for (u32 i : entries)
if (sections[i])
sections[i]->kill();
}
}
template <typename E>
void ObjectFile<E>::claim_unresolved_symbols(Context<E> &ctx) {
if (!this->is_alive)
return;
bool claim_all = ctx.arg.shared && !ctx.arg.z_defs;
for (i64 i = first_global; i < this->symbols.size(); i++) {
const ElfSym<E> &esym = elf_syms[i];
Symbol<E> &sym = *this->symbols[i];
if (!esym.is_undef())
continue;
std::lock_guard lock(sym.mu);
if (!sym.file ||
(sym.esym().is_undef() && sym.file->priority < this->priority)) {
if (claim_all || esym.is_weak()) {
// Convert remaining undefined symbols to dynamic symbols.
sym.file = this;
sym.input_section = nullptr;
sym.value = 0;
sym.sym_idx = i;
sym.ver_idx = ctx.arg.default_version;
sym.is_lazy = false;
sym.is_weak = false;
sym.is_imported = !ctx.arg.is_static;
sym.is_exported = false;
if (sym.traced)
SyncOut(ctx) << "trace-symbol: " << *this << ": unresolved"
<< (esym.is_weak() ? " weak" : "")
<< " symbol " << sym;
} else if (ctx.arg.unresolved_symbols != UnresolvedKind::ERROR) {
// Convert remaining undefined symbols to absolute symbols with
// value 0.
sym.file = this;
sym.input_section = nullptr;
sym.value = 0;
sym.sym_idx = i;
sym.ver_idx = ctx.arg.default_version;