forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mold.h
1388 lines (1130 loc) · 30 KB
/
mold.h
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
#pragma once
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "elf.h"
#include <atomic>
#include <cassert>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <set>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <tbb/concurrent_hash_map.h>
#include <tbb/enumerable_thread_specific.h>
#include <tbb/spin_mutex.h>
#include <vector>
static constexpr i64 SECTOR_SIZE = 512;
static constexpr i64 PAGE_SIZE = 4096;
static constexpr i64 GOT_SIZE = 8;
static constexpr i64 PLT_SIZE = 16;
static constexpr i64 PLT_GOT_SIZE = 8;
static constexpr i64 SHA256_SIZE = 32;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
class InputChunk;
class InputFile;
class InputSection;
class MergeableSection;
class MergedSection;
class ObjectFile;
class OutputChunk;
class OutputSection;
class SharedFile;
class Symbol;
enum class BuildIdKind : u8 { NONE, MD5, SHA1, SHA256, UUID };
struct Config {
BuildIdKind build_id = BuildIdKind::NONE;
bool allow_multiple_definition = false;
bool discard_all = false;
bool discard_locals = false;
bool eh_frame_hdr = true;
bool export_dynamic = false;
bool fork = true;
bool gc_sections = false;
bool hash_style_gnu = false;
bool hash_style_sysv = true;
bool icf = false;
bool is_static = false;
bool perf = false;
bool pic = false;
bool pie = false;
bool preload = false;
bool print_gc_sections = false;
bool print_icf_sections = false;
bool print_map = false;
bool quick_exit = true;
bool relax = true;
bool shared = false;
bool stats = false;
bool strip_all = false;
bool trace = false;
bool z_now = false;
i64 filler = -1;
i64 thread_count = -1;
std::string dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
std::string entry = "_start";
std::string output;
std::string rpaths;
std::string sysroot;
std::vector<std::string> globals;
std::vector<std::string_view> library_paths;
std::vector<std::string_view> trace_symbol;
std::vector<std::string_view> undefined;
std::vector<std::string_view> version_script;
u64 image_base = 0x200000;
};
inline Config config;
void cleanup();
class SyncOut {
public:
SyncOut(std::ostream &out = std::cout) : out(out) {}
~SyncOut() {
static std::mutex mu;
std::lock_guard lock(mu);
out << ss.str() << "\n";
}
template <class T> SyncOut &operator<<(T &&val) {
ss << std::forward<T>(val);
return *this;
}
private:
std::ostream &out;
std::stringstream ss;
};
class Error {
public:
Error() {
has_error = true;
}
template <class T> Error &operator<<(T &&val) {
out << std::forward<T>(val);
return *this;
}
static void checkpoint() {
if (!has_error)
return;
cleanup();
_exit(1);
}
private:
static inline std::atomic_bool has_error = false;
SyncOut out{std::cerr};
};
class Fatal {
public:
[[noreturn]] ~Fatal() {
out.~SyncOut();
cleanup();
_exit(1);
}
template <class T> Fatal &operator<<(T &&val) {
out << std::forward<T>(val);
return *this;
}
private:
SyncOut out{std::cerr};
};
#define unreachable() \
do { Fatal() << "internal error at " << __FILE__ << ":" << __LINE__; } while (0)
std::ostream &operator<<(std::ostream &out, const InputFile &file);
//
// Interned string
//
namespace tbb {
template<> struct tbb_hash_compare<std::string_view> {
static size_t hash(const std::string_view &k) {
return std::hash<std::string_view>()(k);
}
static bool equal(const std::string_view &k1, const std::string_view &k2) {
return k1 == k2;
}
};
}
template<typename ValueT> class ConcurrentMap {
public:
ValueT *insert(std::string_view key, const ValueT &val) {
typename decltype(map)::const_accessor acc;
map.insert(acc, std::make_pair(key, val));
return const_cast<ValueT *>(&acc->second);
}
tbb::concurrent_hash_map<std::string_view, ValueT> map;
};
//
// Symbol
//
struct SectionFragment {
SectionFragment(std::string_view data) : data(data) {}
SectionFragment(const SectionFragment &other)
: isec(other.isec.load()), data(other.data), offset(other.offset) {}
inline u64 get_addr() const;
std::atomic<MergeableSection *> isec = nullptr;
std::string_view data;
u32 offset = -1;
u16 alignment = 1;
std::atomic_bool is_alive = !config.gc_sections;
};
struct SectionFragmentRef {
SectionFragment *frag = nullptr;
i32 addend = 0;
};
struct SectionFragmentKey {
std::string_view data;
u32 alignment;
};
namespace tbb {
template<> struct tbb_hash_compare<SectionFragmentKey> {
static size_t hash(const SectionFragmentKey &k) {
return std::hash<std::string_view>()(k.data) ^ std::hash<u32>()(k.alignment);
}
static bool equal(const SectionFragmentKey &k1, const SectionFragmentKey &k2) {
return k1.data == k2.data && k1.alignment == k2.alignment;
}
};
}
enum {
NEEDS_GOT = 1 << 0,
NEEDS_PLT = 1 << 1,
NEEDS_GOTTPOFF = 1 << 2,
NEEDS_TLSGD = 1 << 3,
NEEDS_TLSLD = 1 << 4,
NEEDS_COPYREL = 1 << 5,
NEEDS_DYNSYM = 1 << 6,
};
class Symbol {
public:
Symbol() = default;
Symbol(std::string_view name) : name(name) {}
Symbol(const Symbol &other) : name(other.name) {}
static Symbol *intern(std::string_view name) {
static ConcurrentMap<Symbol> map;
return map.insert(name, {name});
}
inline u64 get_addr() const;
inline u64 get_got_addr() const;
inline u64 get_gotplt_addr() const;
inline u64 get_gottpoff_addr() const;
inline u64 get_tlsgd_addr() const;
inline u64 get_plt_addr() const;
inline bool is_alive() const;
inline bool is_absolute() const;
inline bool is_relative() const { return !is_absolute(); }
inline bool is_imported() const;
inline bool is_undef() const;
inline bool is_undef_weak() const;
std::string_view name;
InputFile *file = nullptr;
const ElfSym *esym = nullptr;
InputSection *input_section = nullptr;
SectionFragment *frag = nullptr;
u64 value = -1;
u32 got_idx = -1;
u32 gotplt_idx = -1;
u32 gottpoff_idx = -1;
u32 tlsgd_idx = -1;
u32 plt_idx = -1;
u32 dynsym_idx = -1;
u16 shndx = 0;
u16 ver_idx = 0;
std::atomic_uint8_t flags = 0;
u8 st_type = STT_NOTYPE;
tbb::spin_mutex mu;
u8 is_placeholder : 1 = false;
u8 write_symtab : 1 = false;
u8 traced : 1 = false;
u8 has_relplt : 1 = false;
u8 has_copyrel : 1 = false;
};
//
// input_sections.cc
//
class InputChunk {
public:
virtual void copy_buf() {}
inline u64 get_addr() const;
std::string_view get_contents() const;
ObjectFile *file;
const ElfShdr &shdr;
OutputSection *output_section = nullptr;
std::string_view name;
u32 offset = -1;
protected:
InputChunk(ObjectFile *file, const ElfShdr &shdr, std::string_view name);
};
enum RelType : u8 {
R_NONE = 1,
R_ABS,
R_DYN,
R_BASEREL,
R_PC,
R_GOT,
R_GOTPC,
R_GOTPCREL,
R_TLSGD,
R_TLSGD_RELAX_LE,
R_TLSLD,
R_TLSLD_RELAX_LE,
R_DTPOFF,
R_TPOFF,
R_GOTTPOFF,
};
struct EhReloc {
Symbol &sym;
u32 type;
u32 offset;
i64 addend;
};
inline bool operator==(const EhReloc &a, const EhReloc &b) {
return std::tuple(&a.sym, a.type, a.offset, a.addend) ==
std::tuple(&b.sym, b.type, b.offset, b.addend);
}
struct CieRecord;
struct FdeRecord {
FdeRecord(std::string_view contents, std::vector<EhReloc> &&rels,
u32 cie_idx)
: contents(contents), rels(std::move(rels)), cie_idx(cie_idx) {}
FdeRecord(const FdeRecord &&other)
: contents(other.contents), rels(std::move(other.rels)),
cie_idx(other.cie_idx), offset(other.offset),
is_alive(other.is_alive.load()) {}
std::string_view contents;
std::vector<EhReloc> rels;
u32 cie_idx = -1;
u32 offset = -1;
std::atomic_bool is_alive = true;
};
struct CieRecord {
bool should_merge(const CieRecord &other) const;
std::string_view contents;
std::vector<EhReloc> rels;
std::vector<FdeRecord> fdes;
// For .eh_frame
u32 offset = -1;
u32 leader_offset = -1;
u32 fde_size = -1;
// For .eh_frame_hdr
u32 num_fdes = 0;
u32 fde_idx = -1;
// For ICF
u32 icf_idx = -1;
};
class InputSection : public InputChunk {
public:
InputSection(ObjectFile *file, const ElfShdr &shdr, std::string_view name,
i64 section_idx)
: InputChunk(file, shdr, name), section_idx(section_idx) {}
void copy_buf() override;
void scan_relocations();
void report_undefined_symbols();
void apply_reloc_alloc(u8 *base);
void apply_reloc_nonalloc(u8 *base);
void kill();
inline i64 get_priority() const;
std::span<ElfRela> rels;
std::vector<bool> has_fragments;
std::vector<SectionFragmentRef> rel_fragments;
std::vector<RelType> rel_types;
std::span<FdeRecord> fdes;
u64 reldyn_offset = 0;
u32 section_idx = -1;
bool is_comdat_member = false;
bool is_ehframe = false;
// For COMDAT de-duplication and garbage collection
bool is_alive = true;
// For garbage collection
std::atomic_bool is_visited = false;
// For ICF
bool icf_eligible = false;
bool icf_leaf = false;
InputSection *leader = nullptr;
u32 icf_idx = -1;
};
class MergeableSection : public InputChunk {
public:
MergeableSection(InputSection *isec);
MergedSection &parent;
std::vector<SectionFragment *> fragments;
std::vector<u32> frag_offsets;
u32 size = 0;
u32 padding = 0;
};
//
// output_chunks.cc
//
class OutputChunk {
public:
enum Kind : u8 { HEADER, REGULAR, SYNTHETIC };
virtual void copy_buf() {}
virtual void update_shdr() {}
std::string_view name;
i64 shndx = 0;
Kind kind;
bool starts_new_ptload = false;
ElfShdr shdr = { .sh_addralign = 1 };
protected:
OutputChunk(Kind kind) : kind(kind) {}
};
// ELF header
class OutputEhdr : public OutputChunk {
public:
OutputEhdr() : OutputChunk(HEADER) {
shdr.sh_flags = SHF_ALLOC;
shdr.sh_size = sizeof(ElfEhdr);
}
void copy_buf() override;
};
// Section header
class OutputShdr : public OutputChunk {
public:
OutputShdr() : OutputChunk(HEADER) {
shdr.sh_flags = SHF_ALLOC;
}
void update_shdr() override;
void copy_buf() override;
};
// Program header
class OutputPhdr : public OutputChunk {
public:
OutputPhdr() : OutputChunk(HEADER) {
shdr.sh_flags = SHF_ALLOC;
}
void update_shdr() override;
void copy_buf() override;
};
class InterpSection : public OutputChunk {
public:
InterpSection() : OutputChunk(SYNTHETIC) {
name = ".interp";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_size = config.dynamic_linker.size() + 1;
}
void copy_buf() override;
};
// Sections
class OutputSection : public OutputChunk {
public:
static OutputSection *get_instance(std::string_view name, u64 type, u64 flags);
OutputSection(std::string_view name, u32 type, u64 flags)
: OutputChunk(REGULAR) {
this->name = name;
shdr.sh_type = type;
shdr.sh_flags = flags;
idx = instances.size();
instances.push_back(this);
}
void copy_buf() override;
static inline std::vector<OutputSection *> instances;
std::vector<InputSection *> members;
u32 idx;
};
class GotSection : public OutputChunk {
public:
GotSection() : OutputChunk(SYNTHETIC) {
name = ".got";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
shdr.sh_addralign = GOT_SIZE;
}
void add_got_symbol(Symbol *sym);
void add_gottpoff_symbol(Symbol *sym);
void add_tlsgd_symbol(Symbol *sym);
void add_tlsld();
u64 get_tlsld_addr() const {
assert(tlsld_idx != -1);
return shdr.sh_addr + tlsld_idx * GOT_SIZE;
}
void copy_buf() override;
std::vector<Symbol *> got_syms;
std::vector<Symbol *> gottpoff_syms;
std::vector<Symbol *> tlsgd_syms;
u32 tlsld_idx = -1;
};
class GotPltSection : public OutputChunk {
public:
GotPltSection() : OutputChunk(SYNTHETIC) {
name = ".got.plt";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
shdr.sh_addralign = GOT_SIZE;
shdr.sh_size = GOT_SIZE * 3;
}
void copy_buf() override;
};
class PltSection : public OutputChunk {
public:
PltSection() : OutputChunk(SYNTHETIC) {
name = ".plt";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
shdr.sh_addralign = 8;
shdr.sh_size = PLT_SIZE;
}
void add_symbol(Symbol *sym);
void copy_buf() override;
std::vector<Symbol *> symbols;
};
class PltGotSection : public OutputChunk {
public:
PltGotSection() : OutputChunk(SYNTHETIC) {
name = ".plt.got";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
shdr.sh_addralign = 8;
}
void add_symbol(Symbol *sym);
void copy_buf() override;
std::vector<Symbol *> symbols;
};
class RelPltSection : public OutputChunk {
public:
RelPltSection() : OutputChunk(SYNTHETIC) {
name = ".rela.plt";
shdr.sh_type = SHT_RELA;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_entsize = sizeof(ElfRela);
shdr.sh_addralign = 8;
}
void update_shdr() override;
void copy_buf() override;
};
class RelDynSection : public OutputChunk {
public:
RelDynSection() : OutputChunk(SYNTHETIC) {
name = ".rela.dyn";
shdr.sh_type = SHT_RELA;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_entsize = sizeof(ElfRela);
shdr.sh_addralign = 8;
}
void update_shdr() override;
void copy_buf() override;
};
class StrtabSection : public OutputChunk {
public:
StrtabSection() : OutputChunk(SYNTHETIC) {
name = ".strtab";
shdr.sh_type = SHT_STRTAB;
shdr.sh_size = 1;
}
void update_shdr() override;
};
class ShstrtabSection : public OutputChunk {
public:
ShstrtabSection() : OutputChunk(SYNTHETIC) {
name = ".shstrtab";
shdr.sh_type = SHT_STRTAB;
}
void update_shdr() override;
void copy_buf() override;
};
class DynstrSection : public OutputChunk {
public:
DynstrSection() : OutputChunk(SYNTHETIC) {
name = ".dynstr";
shdr.sh_type = SHT_STRTAB;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_size = 1;
}
i64 add_string(std::string_view str);
i64 find_string(std::string_view str);
void copy_buf() override;
private:
std::unordered_map<std::string_view, i64> strings;
};
class DynamicSection : public OutputChunk {
public:
DynamicSection() : OutputChunk(SYNTHETIC) {
name = ".dynamic";
shdr.sh_type = SHT_DYNAMIC;
shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
shdr.sh_addralign = 8;
shdr.sh_entsize = sizeof(ElfDyn);
}
void update_shdr() override;
void copy_buf() override;
};
class SymtabSection : public OutputChunk {
public:
SymtabSection() : OutputChunk(SYNTHETIC) {
name = ".symtab";
shdr.sh_type = SHT_SYMTAB;
shdr.sh_entsize = sizeof(ElfSym);
shdr.sh_addralign = 8;
shdr.sh_size = sizeof(ElfSym);
}
void update_shdr() override;
void copy_buf() override;
};
class DynsymSection : public OutputChunk {
public:
DynsymSection() : OutputChunk(SYNTHETIC) {
name = ".dynsym";
shdr.sh_type = SHT_DYNSYM;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_entsize = sizeof(ElfSym);
shdr.sh_addralign = 8;
shdr.sh_size = sizeof(ElfSym);
}
void add_symbol(Symbol *sym);
void sort_symbols();
void update_shdr() override;
void copy_buf() override;
std::vector<Symbol *> symbols = {nullptr};
std::vector<u32> name_indices = {(u32)-1};
};
class HashSection : public OutputChunk {
public:
HashSection() : OutputChunk(SYNTHETIC) {
name = ".hash";
shdr.sh_type = SHT_HASH;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_entsize = 4;
shdr.sh_addralign = 4;
}
void update_shdr() override;
void copy_buf() override;
};
class GnuHashSection : public OutputChunk {
public:
GnuHashSection() : OutputChunk(SYNTHETIC) {
name = ".gnu.hash";
shdr.sh_type = SHT_GNU_HASH;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 8;
}
void update_shdr() override;
void copy_buf() override;
static constexpr i64 LOAD_FACTOR = 8;
static constexpr i64 HEADER_SIZE = 16;
static constexpr i64 BLOOM_SHIFT = 26;
static constexpr i64 ELFCLASS_BITS = 64;
u32 num_buckets = -1;
u32 symoffset = -1;
u32 num_bloom = 1;
};
class MergedSection : public OutputChunk {
public:
static MergedSection *get_instance(std::string_view name, u64 type, u64 flags);
static inline std::vector<MergedSection *> instances;
SectionFragment *insert(std::string_view data, u32 alignment) {
typename decltype(map)::const_accessor acc;
map.insert(acc, std::pair(SectionFragmentKey{data, alignment},
SectionFragment(data)));
return const_cast<SectionFragment *>(&acc->second);
}
void copy_buf() override;
private:
MergedSection(std::string_view name, u64 flags, u32 type)
: OutputChunk(SYNTHETIC) {
this->name = name;
shdr.sh_flags = flags;
shdr.sh_type = type;
}
tbb::concurrent_hash_map<SectionFragmentKey, SectionFragment> map;
};
class EhFrameSection : public OutputChunk {
public:
EhFrameSection() : OutputChunk(SYNTHETIC) {
name = ".eh_frame";
shdr.sh_type = SHT_X86_64_UNWIND;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 8;
}
void construct();
void copy_buf() override;
u64 get_addr(const Symbol &sym);
std::vector<CieRecord *> cies;
u32 num_fdes = 0;
};
class EhFrameHdrSection : public OutputChunk {
public:
EhFrameHdrSection() : OutputChunk(SYNTHETIC) {
name = ".eh_frame_hdr";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 4;
shdr.sh_size = HEADER_SIZE;
}
static constexpr i64 HEADER_SIZE = 12;
};
class CopyrelSection : public OutputChunk {
public:
CopyrelSection() : OutputChunk(SYNTHETIC) {
name = ".bss";
shdr.sh_type = SHT_NOBITS;
shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
shdr.sh_addralign = 32;
}
void add_symbol(Symbol *sym);
std::vector<Symbol *> symbols;
};
class VersymSection : public OutputChunk {
public:
VersymSection() : OutputChunk(SYNTHETIC) {
name = ".gnu.version";
shdr.sh_type = SHT_GNU_VERSYM;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_entsize = 2;
shdr.sh_addralign = 2;
}
void update_shdr() override;
void copy_buf() override;
std::vector<u16> contents;
};
class VerneedSection : public OutputChunk {
public:
VerneedSection() : OutputChunk(SYNTHETIC) {
name = ".gnu.version_r";
shdr.sh_type = SHT_GNU_VERNEED;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 8;
}
void update_shdr() override;
void copy_buf() override;
std::vector<u8> contents;
};
class BuildIdSection : public OutputChunk {
public:
BuildIdSection() : OutputChunk(SYNTHETIC) {
name = ".note.gnu.build-id";
shdr.sh_type = SHT_NOTE;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 4;
shdr.sh_size = 1;
}
void update_shdr() override;
void copy_buf() override;
void write_buildid(i64 filesize);
static constexpr i64 HEADER_SIZE = 16;
};
bool is_c_identifier(std::string_view name);
std::vector<ElfPhdr> create_phdr();
//
// object_file.cc
//
struct ComdatGroup {
ComdatGroup() = default;
ComdatGroup(const ComdatGroup &other)
: owner(other.owner.load()), section_idx(other.section_idx) {}
std::atomic<ObjectFile *> owner = nullptr;
u32 section_idx = -1;
};
class MemoryMappedFile {
public:
static MemoryMappedFile *open(std::string path);
static MemoryMappedFile *must_open(std::string path);
MemoryMappedFile(std::string name, u8 *data, u64 size, u64 mtime = 0)
: name(name), data_(data), size_(size), mtime(mtime) {}
MemoryMappedFile() = delete;
MemoryMappedFile *slice(std::string name, u64 start, u64 size);
u8 *data();
i64 size() const { return size_; }
std::string_view get_contents() {
return std::string_view((char *)data(), size());
}
std::string name;
i64 mtime = 0;
private:
std::mutex mu;
MemoryMappedFile *parent;
std::atomic<u8 *> data_;
i64 size_ = 0;
};
class InputFile {
public:
InputFile(MemoryMappedFile *mb);
InputFile() : name("<internal>") {}
MemoryMappedFile *mb;
std::span<ElfShdr> elf_sections;
std::vector<Symbol *> symbols;
std::string name;
bool is_dso = false;
u32 priority;
std::atomic_bool is_alive = false;
std::string_view get_string(const ElfShdr &shdr);
std::string_view get_string(i64 idx);
protected:
template<typename T> std::span<T> get_data(const ElfShdr &shdr);
template<typename T> std::span<T> get_data(i64 idx);
ElfShdr *find_section(i64 type);
std::string_view shstrtab;
};
class ObjectFile : public InputFile {
public:
ObjectFile(MemoryMappedFile *mb, std::string archive_name, bool is_in_lib);
ObjectFile();
void parse();
void resolve_symbols();
void mark_live_objects(std::function<void(ObjectFile *)> feeder);
void handle_undefined_weak_symbols();
void resolve_comdat_groups();
void eliminate_duplicate_comdat_groups();
void claim_unresolved_symbols();
void scan_relocations();
void convert_common_symbols();
void compute_symtab();
void write_symtab();
static ObjectFile *create_internal_obj();
std::string archive_name;
std::vector<InputSection *> sections;
std::span<ElfSym> elf_syms;
i64 first_global = 0;
const bool is_in_lib = false;
std::vector<CieRecord> cies;
u64 num_dynrel = 0;
u64 reldyn_offset = 0;
u64 local_symtab_offset = 0;
u64 global_symtab_offset = 0;
u64 num_local_symtab = 0;
u64 num_global_symtab = 0;
u64 strtab_offset = 0;
u64 strtab_size = 0;
std::vector<MergeableSection *> mergeable_sections;
private:
void initialize_sections();
void initialize_symbols();
void initialize_mergeable_sections();
void initialize_ehframe_sections();
void read_ehframe(InputSection &isec);
void maybe_override_symbol(Symbol &sym, i64 symidx);
std::vector<std::pair<ComdatGroup *, std::span<u32>>> comdat_groups;
std::vector<SectionFragmentRef> sym_fragments;
bool has_common_symbol;
std::string_view symbol_strtab;
const ElfShdr *symtab_sec;
};
class SharedFile : public InputFile {
public:
SharedFile(MemoryMappedFile *mb, bool as_needed) : InputFile(mb) {
is_alive = !as_needed;
}
void parse();
void resolve_symbols();
std::vector<Symbol *> find_aliases(Symbol *sym);
std::string_view soname;
std::vector<std::string_view> version_strings;
std::vector<Symbol *> undefs;