forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_file.cc
842 lines (694 loc) · 23.6 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
#include "mold.h"
#include <cstring>
#include <fcntl.h>
#include <regex>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
MemoryMappedFile *MemoryMappedFile::open(std::string path) {
struct stat st;
if (stat(path.c_str(), &st) == -1)
return nullptr;
u64 mtime = (u64)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;
return new MemoryMappedFile(path, nullptr, st.st_size, mtime);
}
MemoryMappedFile *MemoryMappedFile::must_open(std::string path) {
if (MemoryMappedFile *mb = MemoryMappedFile::open(path))
return mb;
error("cannot open " + path);
}
u8 *MemoryMappedFile::data() {
if (data_)
return data_;
std::lock_guard lock(mu);
if (data_)
return data_;
int fd = ::open(name.c_str(), O_RDONLY);
if (fd == -1)
error(name + ": cannot open: " + strerror(errno));
data_ = (u8 *)mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd, 0);
if (data_ == MAP_FAILED)
error(name + ": mmap failed: " + strerror(errno));
close(fd);
return data_;
}
MemoryMappedFile *MemoryMappedFile::slice(std::string name, u64 start, u64 size) {
MemoryMappedFile *mb = new MemoryMappedFile(name, data_ + start, size);
mb->parent = this;
return mb;
}
InputFile::InputFile(MemoryMappedFile *mb)
: mb(mb), name(mb->name), ehdr(*(ElfEhdr *)mb->data()), is_dso(ehdr.e_type == ET_DYN) {
if (mb->size() < sizeof(ElfEhdr))
error(to_string(this) + ": file too small");
if (memcmp(mb->data(), "\177ELF", 4))
error(to_string(this) + ": not an ELF file");
u8 *sh_begin = mb->data() + ehdr.e_shoff;
u8 *sh_end = sh_begin + ehdr.e_shnum * sizeof(ElfShdr);
if (mb->data() + mb->size() < sh_end)
error(to_string(this) + ": e_shoff or e_shnum corrupted: " +
std::to_string(mb->size()) + " " + std::to_string(ehdr.e_shnum));
elf_sections = {(ElfShdr *)sh_begin, (ElfShdr *)sh_end};
}
std::string_view InputFile::get_string(const ElfShdr &shdr) {
u8 *begin = mb->data() + shdr.sh_offset;
u8 *end = begin + shdr.sh_size;
if (mb->data() + mb->size() < end)
error(to_string(this) + ": shdr corrupted");
return {(char *)begin, (char *)end};
}
std::string_view InputFile::get_string(u32 idx) {
if (elf_sections.size() <= idx)
error(to_string(this) + ": invalid section index");
return get_string(elf_sections[idx]);
}
template<typename T>
std::span<T> InputFile::get_data(const ElfShdr &shdr) {
std::string_view view = get_string(shdr);
if (view.size() % sizeof(T))
error(to_string(this) + ": corrupted section");
return {(T *)view.data(), view.size() / sizeof(T)};
}
template<typename T>
std::span<T> InputFile::get_data(u32 idx) {
if (elf_sections.size() <= idx)
error(to_string(this) + ": invalid section index");
return get_data<T>(elf_sections[idx]);
}
ElfShdr *InputFile::find_section(u32 type) {
for (ElfShdr &sec : elf_sections)
if (sec.sh_type == type)
return &sec;
return nullptr;
}
ObjectFile::ObjectFile(MemoryMappedFile *mb, std::string archive_name)
: InputFile(mb), archive_name(archive_name),
is_in_archive(archive_name != "") {
is_alive = (archive_name == "");
}
void ObjectFile::initialize_sections() {
// Read sections
for (int i = 0; i < elf_sections.size(); i++) {
const ElfShdr &shdr = 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())
error(to_string(this) + ": invalid symbol index");
const ElfSym &sym = elf_syms[shdr.sh_info];
std::string_view signature = symbol_strtab.data() + sym.st_name;
// Get comdat group members.
std::span<u32> entries = get_data<u32>(shdr);
if (entries.empty())
error(to_string(this) + ": empty SHT_GROUP");
if (entries[0] == 0)
continue;
if (entries[0] != GRP_COMDAT)
error(to_string(this) + ": unsupported SHT_GROUP format");
static ConcurrentMap<ComdatGroup> map;
ComdatGroup *group = map.insert(signature, ComdatGroup(nullptr, 0));
comdat_groups.push_back({group, entries});
static Counter counter("comdats");
counter.inc();
break;
}
case SHT_SYMTAB_SHNDX:
error(to_string(this) + ": SHT_SYMTAB_SHNDX section is not supported");
break;
case SHT_SYMTAB:
case SHT_STRTAB:
case SHT_REL:
case SHT_RELA:
case SHT_NULL:
break;
default: {
static Counter counter("regular_sections");
counter.inc();
std::string_view shstrtab = get_string(ehdr.e_shstrndx);
std::string_view name = shstrtab.data() + shdr.sh_name;
this->sections[i] = new InputSection(this, shdr, name);
break;
}
}
}
// Attach relocation sections to their target sections.
for (const ElfShdr &shdr : elf_sections) {
if (shdr.sh_type != SHT_RELA)
continue;
if (shdr.sh_info >= sections.size())
error(to_string(this) + ": invalid relocated section index: " +
std::to_string((u32)shdr.sh_info));
InputSection *target = sections[shdr.sh_info];
if (target) {
target->rels = get_data<ElfRela>(shdr);
target->rel_types.resize(target->rels.size());
target->has_rel_piece.resize(target->rels.size());
if (target->shdr.sh_flags & SHF_ALLOC) {
static Counter counter("relocs_alloc");
counter.inc(target->rels.size());
}
}
}
// Set is_comdat_member bits.
for (auto &pair : comdat_groups) {
std::span<u32> entries = pair.second;
for (u32 i : entries)
if (this->sections[i])
this->sections[i]->is_comdat_member = true;
}
}
static bool should_write_symtab(const ElfSym &esym, std::string_view name) {
if (config.discard_all || config.strip_all)
return false;
if (esym.st_type == STT_SECTION)
return false;
if (config.discard_locals && name.starts_with(".L"))
return false;
return true;
}
void ObjectFile::initialize_symbols() {
if (!symtab_sec)
return;
static Counter counter("all_syms");
counter.inc(elf_syms.size());
symbols.reserve(elf_syms.size());
local_symbols.reserve(first_global);
sym_pieces.resize(elf_syms.size() - first_global);
// First symbol entry is always null
local_symbols.emplace_back("");
symbols.push_back(&local_symbols.back());
// Initialize local symbols
for (int i = 1; i < first_global; i++) {
const ElfSym &esym = elf_syms[i];
std::string_view name = symbol_strtab.data() + esym.st_name;
local_symbols.emplace_back(name);
Symbol &sym = local_symbols.back();
sym.file = this;
sym.type = esym.st_type;
sym.value = esym.st_value;
sym.esym = &esym;
sym.write_symtab = should_write_symtab(esym, name);
if (!esym.is_abs()) {
if (esym.is_common())
error("common local symbol?");
sym.input_section = sections[esym.st_shndx];
}
symbols.push_back(&local_symbols.back());
if (sym.write_symtab) {
strtab_size += name.size() + 1;
local_symtab_size += sizeof(ElfSym);
}
}
// Initialize global symbols
for (int i = first_global; i < elf_syms.size(); i++) {
const ElfSym &esym = elf_syms[i];
std::string_view name = symbol_strtab.data() + esym.st_name;
int pos = name.find('@');
if (pos != std::string_view::npos)
name = name.substr(0, pos);
symbols.push_back(Symbol::intern(name));
if (esym.is_common())
has_common_symbol = true;
}
}
static const StringPieceRef *
binary_search(std::span<StringPieceRef> pieces, u32 offset) {
if (offset < pieces[0].input_offset)
return nullptr;
while (pieces.size() > 1) {
u32 mid = pieces.size() / 2;
const StringPieceRef &ref = pieces[mid];
if (offset < ref.input_offset)
pieces = pieces.subspan(0, mid);
else
pieces = pieces.subspan(mid);
}
return &pieces[0];
}
static bool is_mergeable(const ElfShdr &shdr) {
return (shdr.sh_flags & SHF_MERGE) &&
(shdr.sh_flags & SHF_STRINGS) &&
shdr.sh_entsize == 1;
}
void ObjectFile::initialize_mergeable_sections() {
mergeable_sections.resize(sections.size());
for (int i = 0; i < sections.size(); i++) {
InputSection *isec = sections[i];
if (isec && is_mergeable(isec->shdr)) {
mergeable_sections[i] = new MergeableSection(isec, get_string(isec->shdr));
sections[i] = nullptr;
}
}
// Initialize rel_pieces
for (InputSection *isec : sections) {
if (!isec || isec->rels.empty())
continue;
for (int i = 0; i < isec->rels.size(); i++) {
const ElfRela &rel = isec->rels[i];
switch (rel.r_type) {
case R_X86_64_64:
case R_X86_64_PC32:
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_16:
case R_X86_64_PC16:
case R_X86_64_8:
case R_X86_64_PC8:
if (rel.r_sym >= this->first_global)
continue;
const ElfSym &esym = elf_syms[rel.r_sym];
if (esym.st_type != STT_SECTION)
continue;
MergeableSection *m = mergeable_sections[esym.st_shndx];
if (!m)
continue;
u32 offset = esym.st_value + rel.r_addend;
const StringPieceRef *ref = binary_search(m->pieces, offset);
if (!ref)
error(to_string(this) + ": bad relocation at " + std::to_string(rel.r_sym));
isec->rel_pieces.push_back(
{.piece = ref->piece, .addend = (i32)(offset - ref->input_offset)});
isec->has_rel_piece[i] = true;
}
}
}
// Initialize sym_pieces
for (int i = 0; i < elf_syms.size(); i++) {
const ElfSym &esym = elf_syms[i];
if (esym.is_abs() || esym.is_common())
continue;
MergeableSection *m = mergeable_sections[esym.st_shndx];
if (!m)
continue;
const StringPieceRef *ref = binary_search(m->pieces, esym.st_value);
if (!ref)
error(to_string(this) + ": bad symbol value");
if (i < first_global) {
local_symbols[i].piece_ref = *ref;
} else {
sym_pieces[i - first_global].piece = ref->piece;
sym_pieces[i - first_global].addend = esym.st_value - ref->input_offset;
}
}
erase(mergeable_sections, [](MergeableSection *m) { return !m; });
}
void ObjectFile::parse() {
sections.resize(elf_sections.size());
symtab_sec = find_section(SHT_SYMTAB);
if (symtab_sec) {
first_global = symtab_sec->sh_info;
elf_syms = get_data<ElfSym>(*symtab_sec);
symbol_strtab = get_string(symtab_sec->sh_link);
}
initialize_sections();
initialize_symbols();
initialize_mergeable_sections();
}
// 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. Defined symbol in an archive member
// 4. Unclaimed (nonexistent) symbol
//
// Ties are broken by file priority.
static u64 get_rank(InputFile *file, const ElfSym &esym, InputSection *isec) {
if (isec && isec->is_comdat_member)
return file->priority;
if (esym.is_undef()) {
assert(esym.st_bind == STB_WEAK);
return ((u64)2 << 32) + file->priority;
}
if (esym.st_bind == STB_WEAK)
return ((u64)1 << 32) + file->priority;
return file->priority;
}
static u64 get_rank(const Symbol &sym) {
if (!sym.file)
return (u64)4 << 32;
if (sym.is_placeholder)
return ((u64)3 << 32) + sym.file->priority;
return get_rank(sym.file, *sym.esym, sym.input_section);
}
void ObjectFile::maybe_override_symbol(Symbol &sym, int symidx) {
InputSection *isec = nullptr;
const ElfSym &esym = elf_syms[symidx];
if (!esym.is_abs() && !esym.is_common())
isec = sections[esym.st_shndx];
std::lock_guard lock(sym.mu);
u64 new_rank = get_rank(this, esym, isec);
u64 existing_rank = get_rank(sym);
if (new_rank < existing_rank) {
sym.file = this;
sym.input_section = isec;
sym.piece_ref = sym_pieces[symidx - first_global];
sym.value = esym.st_value;
sym.ver_idx = 0;
sym.type = esym.st_type;
sym.esym = &esym;
sym.is_placeholder = false;
sym.is_weak = (esym.st_bind == STB_WEAK);
sym.is_imported = false;
if (UNLIKELY(sym.traced))
message("trace: " + to_string(sym.file) +
(sym.is_weak ? ": weak definition of " : ": definition of ") +
std::string(sym.name));
}
}
void ObjectFile::resolve_symbols() {
for (int i = first_global; i < symbols.size(); i++) {
const ElfSym &esym = elf_syms[i];
if (!esym.is_defined())
continue;
Symbol &sym = *symbols[i];
if (is_in_archive) {
std::lock_guard lock(sym.mu);
bool is_new = !sym.file;
bool tie_but_higher_priority =
sym.is_placeholder && this->priority < sym.file->priority;
if (is_new || tie_but_higher_priority) {
sym.file = this;
sym.is_placeholder = true;
if (UNLIKELY(sym.traced))
message("trace: " + to_string(sym.file) + ": lazy definition of " +
std::string(sym.name));
}
} else {
maybe_override_symbol(sym, i);
}
}
}
std::vector<ObjectFile *> ObjectFile::mark_live_objects() {
std::vector<ObjectFile *> vec;
assert(is_alive);
for (int i = first_global; i < symbols.size(); i++) {
const ElfSym &esym = elf_syms[i];
Symbol &sym = *symbols[i];
if (esym.is_defined()) {
if (is_in_archive)
maybe_override_symbol(sym, i);
continue;
}
if (UNLIKELY(sym.traced))
message("trace: " + to_string(this) + ": reference to " + std::string(sym.name));
if (esym.st_bind != STB_WEAK && sym.file &&
!sym.file->is_alive.exchange(true)) {
if (!sym.file->is_dso)
vec.push_back((ObjectFile *)sym.file);
if (UNLIKELY(sym.traced))
message("trace: " + to_string(this) + " keeps " + to_string(sym.file) +
" for " + std::string(sym.name));
}
}
return vec;
}
void ObjectFile::handle_undefined_weak_symbols() {
if (!is_alive)
return;
for (int i = first_global; i < symbols.size(); i++) {
const ElfSym &esym = elf_syms[i];
Symbol &sym = *symbols[i];
if (esym.is_undef() && esym.st_bind == STB_WEAK) {
std::lock_guard lock(sym.mu);
bool is_new = !sym.file || sym.is_placeholder;
bool tie_but_higher_priority =
!is_new && sym.is_undef_weak && this->priority < sym.file->priority;
if (is_new || tie_but_higher_priority) {
sym.file = this;
sym.input_section = nullptr;
sym.value = 0;
sym.esym = &esym;
sym.is_placeholder = false;
sym.is_undef_weak = true;
sym.is_imported = false;
if (UNLIKELY(sym.traced))
message("trace: " + to_string(this) + ": unresolved weak symbol " +
std::string(sym.name));
}
}
}
}
void ObjectFile::resolve_comdat_groups() {
if (!is_alive)
return;
for (auto &pair : comdat_groups) {
ComdatGroup *group = pair.first;
ObjectFile *cur = group->file;
while (!cur || cur->priority > this->priority)
if (group->file.compare_exchange_weak(cur, this))
break;
}
}
void ObjectFile::eliminate_duplicate_comdat_groups() {
if (!is_alive)
return;
for (auto &pair : comdat_groups) {
ComdatGroup *group = pair.first;
if (group->file == this)
continue;
std::span<u32> entries = pair.second;
for (u32 i : entries) {
if (sections[i])
sections[i]->is_alive = false;
sections[i] = nullptr;
}
static Counter counter("removed_comdat_mem");
counter.inc(entries.size());
}
}
void ObjectFile::convert_common_symbols() {
if (!has_common_symbol)
return;
static OutputSection *bss =
OutputSection::get_instance(".bss", SHT_NOBITS, SHF_WRITE | SHF_ALLOC);
for (int i = first_global; i < elf_syms.size(); i++) {
if (!elf_syms[i].is_common())
continue;
Symbol *sym = symbols[i];
if (sym->file != this)
continue;
auto *shdr = new ElfShdr;
memset(shdr, 0, sizeof(*shdr));
shdr->sh_flags = SHF_ALLOC;
shdr->sh_type = SHT_NOBITS;
shdr->sh_size = elf_syms[i].st_size;
shdr->sh_addralign = 1;
auto *isec = new InputSection(this, *shdr, ".bss");
isec->output_section = bss;
sections.push_back(isec);
sym->input_section = isec;
sym->value = 0;
}
}
static bool should_write_global_symtab(Symbol &sym) {
return !config.strip_all && sym.esym->st_type != STT_SECTION;
}
void ObjectFile::compute_symtab() {
for (int i = first_global; i < elf_syms.size(); i++) {
const ElfSym &esym = elf_syms[i];
Symbol &sym = *symbols[i];
if (sym.file == this && should_write_global_symtab(sym)) {
global_symtab_size += sizeof(ElfSym);
strtab_size += sym.name.size() + 1;
}
}
}
void ObjectFile::write_symtab() {
u8 *symtab_base = out::buf + out::symtab->shdr.sh_offset;
u8 *strtab_base = out::buf + out::strtab->shdr.sh_offset;
u32 symtab_off;
u32 strtab_off = strtab_offset;
auto write_sym = [&](u32 i) {
Symbol &sym = *symbols[i];
ElfSym &esym = *(ElfSym *)(symtab_base + symtab_off);
symtab_off += sizeof(ElfSym);
esym = elf_syms[i];
esym.st_name = strtab_off;
if (sym.type == STT_TLS)
esym.st_value = sym.get_addr() - sym.input_section->output_section->shdr.sh_addr;
else
esym.st_value = sym.get_addr();
if (sym.input_section)
esym.st_shndx = sym.input_section->output_section->shndx;
else if (sym.shndx)
esym.st_shndx = sym.shndx;
else
esym.st_shndx = SHN_ABS;
write_string(strtab_base + strtab_off, sym.name);
strtab_off += sym.name.size() + 1;
};
symtab_off = local_symtab_offset;
for (int i = 1; i < first_global; i++)
if (symbols[i]->write_symtab)
write_sym(i);
symtab_off = global_symtab_offset;
for (int i = first_global; i < elf_syms.size(); i++)
if (symbols[i]->file == this && should_write_global_symtab(*symbols[i]))
write_sym(i);
}
bool is_c_identifier(std::string_view name) {
static std::regex re("[a-zA-Z_][a-zA-Z0-9_]*");
return std::regex_match(name.begin(), name.end(), re);
}
ObjectFile *ObjectFile::create_internal_file() {
// Create a dummy object file.
constexpr int bufsz = 256;
u8 *buf = (u8 *)calloc(1, bufsz);
memcpy(buf, "\177ELF", 4);
MemoryMappedFile *mb = new MemoryMappedFile("<internal>", buf, bufsz);
auto *obj = new ObjectFile(mb, "");
// Create linker-synthesized symbols.
auto *elf_syms = new std::vector<ElfSym>(1);
obj->symbols.push_back(new Symbol(""));
obj->first_global = 1;
obj->is_alive = true;
auto add = [&](std::string_view name, u8 visibility = STV_DEFAULT) {
ElfSym esym = {};
esym.st_type = STT_NOTYPE;
esym.st_shndx = SHN_ABS;
esym.st_bind = STB_GLOBAL;
esym.st_visibility = visibility;
elf_syms->push_back(esym);
Symbol *sym = Symbol::intern(name);
obj->symbols.push_back(sym);
return sym;
};
out::__ehdr_start = add("__ehdr_start", STV_HIDDEN);
out::__rela_iplt_start = add("__rela_iplt_start", STV_HIDDEN);
out::__rela_iplt_end = add("__rela_iplt_end", STV_HIDDEN);
out::__init_array_start = add("__init_array_start", STV_HIDDEN);
out::__init_array_end = add("__init_array_end", STV_HIDDEN);
out::__fini_array_start = add("__fini_array_start", STV_HIDDEN);
out::__fini_array_end = add("__fini_array_end", STV_HIDDEN);
out::__preinit_array_start = add("__preinit_array_start", STV_HIDDEN);
out::__preinit_array_end = add("__preinit_array_end", STV_HIDDEN);
out::_DYNAMIC = add("_DYNAMIC", STV_HIDDEN);
out::_GLOBAL_OFFSET_TABLE_ = add("_GLOBAL_OFFSET_TABLE_", STV_HIDDEN);
out::__bss_start = add("__bss_start", STV_HIDDEN);
out::_end = add("_end", STV_HIDDEN);
out::_etext = add("_etext", STV_HIDDEN);
out::_edata = add("_edata", STV_HIDDEN);
for (OutputChunk *chunk : out::chunks) {
if (!is_c_identifier(chunk->name))
continue;
auto *start = new std::string("__start_" + std::string(chunk->name));
auto *stop = new std::string("__stop_" + std::string(chunk->name));
add(*start, STV_HIDDEN);
add(*stop, STV_HIDDEN);
}
obj->elf_syms = *elf_syms;
obj->sym_pieces.resize(elf_syms->size() - obj->first_global);
return obj;
}
std::string to_string(const InputFile *file) {
if (file->is_dso)
return file->name;
ObjectFile *obj = (ObjectFile *)file;
if (obj->archive_name == "")
return obj->name;
return std::string(obj->archive_name) + ":(" + std::string(obj->name) + ")";
}
std::string_view SharedFile::get_soname() {
if (ElfShdr *sec = find_section(SHT_DYNAMIC))
for (ElfDyn &dyn : get_data<ElfDyn>(*sec))
if (dyn.d_tag == DT_SONAME)
return std::string_view(symbol_strtab.data() + dyn.d_val);
return name;
}
void SharedFile::parse() {
symtab_sec = find_section(SHT_DYNSYM);
if (!symtab_sec)
return;
symbol_strtab = get_string(symtab_sec->sh_link);
soname = get_soname();
version_strings = read_verdef();
// Read a symbol table.
int first_global = symtab_sec->sh_info;
std::span<ElfSym> esyms = get_data<ElfSym>(*symtab_sec);
std::span<u16> vers;
if (ElfShdr *sec = find_section(SHT_GNU_VERSYM))
vers = get_data<u16>(*sec);
std::vector<std::pair<const ElfSym *, u16>> pairs;
for (int i = first_global; i < esyms.size(); i++) {
if (!esyms[i].is_defined())
continue;
if (!vers.empty() && (vers[i] >> 15) == 1)
continue;
if (vers.empty())
pairs.push_back({&esyms[i], 1});
else
pairs.push_back({&esyms[i], vers[i]});
}
// Sort symbols by value for find_aliases(), as find_aliases() does
// binary search on symbols.
sort(pairs, [](const std::pair<const ElfSym *, u16> &a,
const std::pair<const ElfSym *, u16> &b) {
return a.first->st_value < b.first->st_value;
});
elf_syms.reserve(pairs.size());
versyms.reserve(pairs.size());
symbols.reserve(pairs.size());
for (std::pair<const ElfSym *, u16> &x : pairs) {
elf_syms.push_back(x.first);
versyms.push_back(x.second);
std::string_view name = symbol_strtab.data() + x.first->st_name;
symbols.push_back(Symbol::intern(name));
}
static Counter counter("dso_syms");
counter.inc(elf_syms.size());
}
std::vector<std::string_view> SharedFile::read_verdef() {
ElfShdr *verdef_sec = find_section(SHT_GNU_VERDEF);
if (!verdef_sec)
return {};
std::string_view verdef = get_string(*verdef_sec);
std::string_view strtab = get_string(verdef_sec->sh_link);
std::vector<std::string_view> ret(2);
auto *ver = (ElfVerdef *)verdef.data();
for (;;) {
if (ret.size() <= ver->vd_ndx)
ret.resize(ver->vd_ndx + 1);
ElfVerdaux *aux = (ElfVerdaux *)((u8 *)ver + ver->vd_aux);
ret[ver->vd_ndx] = strtab.data() + aux->vda_name;
if (!ver->vd_next)
break;
ver = (ElfVerdef *)((u8 *)ver + ver->vd_next);
}
return ret;
}
void SharedFile::resolve_symbols() {
for (int i = 0; i < symbols.size(); i++) {
Symbol &sym = *symbols[i];
const ElfSym &esym = *elf_syms[i];
std::lock_guard lock(sym.mu);
u64 new_rank = get_rank(this, esym, nullptr);
u64 existing_rank = get_rank(sym);
if (new_rank < existing_rank) {
sym.file = this;
sym.input_section = nullptr;
sym.piece_ref = {};
sym.value = esym.st_value;
sym.ver_idx = versyms[i];
sym.type = (esym.st_type == STT_GNU_IFUNC) ? STT_FUNC : esym.st_type;
sym.esym = &esym;
sym.is_placeholder = false;
sym.is_weak = (esym.st_bind == STB_WEAK);
sym.is_imported = true;
if (UNLIKELY(sym.traced))
message("trace: " + to_string(sym.file) +
(sym.is_weak ? ": weak definition of " : ": definition of ") +
std::string(sym.name));
}
}
}
std::span<Symbol *> SharedFile::find_aliases(Symbol *sym) {
assert(sym->file == this);
auto [begin, end] = std::equal_range(
symbols.begin(), symbols.end(), sym,
[&](Symbol *a, Symbol *b) { return a->value < b->value; });
return {begin, end};
}