forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmold.h
2259 lines (1868 loc) · 60.8 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 <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <regex>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_vector.h>
#include <tbb/enumerable_thread_specific.h>
#include <tbb/spin_mutex.h>
#include <tbb/task_group.h>
#include <unistd.h>
#include <unordered_set>
#include <vector>
#include <xxh3.h>
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;
static constexpr i32 SECTOR_SIZE = 512;
static constexpr i32 PAGE_SIZE = 4096;
static constexpr i32 SHA256_SIZE = 32;
template <typename E> class InputFile;
template <typename E> class InputSection;
template <typename E> class MemoryMappedFile;
template <typename E> class MergedSection;
template <typename E> class ObjectFile;
template <typename E> class OutputChunk;
template <typename E> class OutputSection;
template <typename E> class SharedFile;
template <typename E> class Symbol;
template <typename E> struct CieRecord;
template <typename E> struct Context;
template <typename E> struct FdeRecord;
template <typename E> class ROutputChunk;
template <typename E> class ROutputEhdr;
template <typename E> class ROutputShdr;
template <typename E> class RStrtabSection;
template <typename E> class RSymtabSection;
class ZlibCompressor;
class GzipCompressor;
class TarFile;
template <typename E> void cleanup();
template <typename E>
std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);
void assertion_failure(std::string file, i64 line, std::string func,
std::string expr);
#define ASSERT(expr) \
do { \
if (!(expr)) \
assertion_failure(__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
} while (0)
//
// Mergeable section fragments
//
template <typename E>
struct SectionFragment {
SectionFragment(MergedSection<E> *sec, std::string_view data)
: output_section(*sec), data(data) {}
SectionFragment(const SectionFragment &other)
: output_section(other.output_section), data(other.data),
offset(other.offset), alignment(other.alignment.load()),
is_alive(other.is_alive.load()) {}
inline u64 get_addr(Context<E> &ctx) const;
MergedSection<E> &output_section;
std::string_view data;
u32 offset = -1;
std::atomic_uint16_t alignment = 1;
std::atomic_bool is_alive = false;
};
template <typename E>
struct SectionFragmentRef {
SectionFragment<E> *frag = nullptr;
i32 idx = 0;
i32 addend = 0;
};
// Additinal class members for dynamic symbols. Because most symbols
// don't need them and we allocate tens of millions of symbol objects
// for large programs, we separate them from `Symbol` class to save
// memory.
struct SymbolAux {
i32 got_idx = -1;
i32 gotplt_idx = -1;
i32 gottp_idx = -1;
i32 tlsgd_idx = -1;
i32 tlsdesc_idx = -1;
i32 plt_idx = -1;
i32 pltgot_idx = -1;
i32 dynsym_idx = -1;
};
//
// Interned string
//
inline u64 hash_string(std::string_view str) {
return XXH3_64bits(str.data(), str.size());
}
template<> struct tbb::tbb_hash_compare<std::string_view> {
static size_t hash(const std::string_view &k) {
return hash_string(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;
};
//
// input_sections.cc
//
enum {
R_DYN = 1,
R_BASEREL,
};
// .eh_frame section contains CIE and FDE records to teach the runtime
// how to handle exceptions. Usually, a .eh_frame contains one CIE
// followed by as many FDEs as the number of functions defined by the
// file. CIE contains common information for FDEs (it is actually
// short for Common Information Entry). FDE contains the start address
// of a function and its length as well as how to handle exceptions
// for that function.
//
// Unlike other sections, the linker has to parse .eh_frame for optimal
// output for the following reasons:
//
// - Compilers tend to emit the same CIE as long as the programming
// language is the same, so CIEs in input object files are almost
// always identical. We want to merge them to make a resulting
// .eh_frame smaller.
//
// - If we eliminate a function (e.g. when we see two object files
// containing the duplicate definition of an inlined function), we
// want to also eliminate a corresponding FDE so that a resulting
// .eh_frame doesn't contain a dead FDE entry.
//
// - If we need to compare two function definitions for equality for
// ICF, we need to compare not only the function body but also its
// exception handlers.
//
// Note that we assume that the first relocation entry for an FDE
// always points to the function that the FDE is associated to.
template <typename E>
struct FdeRecord {
FdeRecord(u32 input_offset, u32 rel_idx)
: input_offset(input_offset), rel_idx(rel_idx) {}
FdeRecord(const FdeRecord &other)
: cie(other.cie), input_offset(other.input_offset),
output_offset(other.output_offset), rel_idx(other.rel_idx),
is_alive(other.is_alive.load()) {}
FdeRecord &operator=(const FdeRecord<E> &other) {
cie = other.cie;
input_offset = other.input_offset;
output_offset = other.output_offset;
rel_idx = other.rel_idx;
is_alive = other.is_alive.load();
return *this;
}
i64 size() const;
std::string_view get_contents() const;
std::span<ElfRel<E>> get_rels() const;
union {
CieRecord<E> *cie = nullptr;
u32 cie_idx;
};
u32 input_offset = -1;
u32 output_offset = -1;
u32 rel_idx = -1;
std::atomic_bool is_alive = true;
};
template <typename E>
struct CieRecord {
CieRecord(Context<E> &ctx, ObjectFile<E> &file,
InputSection<E> &isec, u32 input_offset, u32 rel_idx)
: file(file), input_section(isec), input_offset(input_offset),
rel_idx(rel_idx), rels(isec.get_rels(ctx)),
contents(file.get_string(ctx, isec.shdr)) {}
i64 size() const;
std::string_view get_contents() const;
std::span<ElfRel<E>> get_rels() const;
bool equals(const CieRecord &other) const;
ObjectFile<E> &file;
InputSection<E> &input_section;
u32 input_offset = -1;
u32 output_offset = -1;
u32 rel_idx = -1;
u32 icf_idx = -1;
bool is_leader = false;
std::span<ElfRel<E>> rels;
std::string_view contents;
};
// InputSection represents a section in an input object file.
template <typename E>
class InputSection {
public:
InputSection(Context<E> &ctx, ObjectFile<E> &file, const ElfShdr<E> &shdr,
std::string_view name, std::string_view contents,
i64 section_idx);
void scan_relocations(Context<E> &ctx);
void write_to(Context<E> &ctx, u8 *buf);
void apply_reloc_alloc(Context<E> &ctx, u8 *base);
void apply_reloc_nonalloc(Context<E> &ctx, u8 *base);
inline void kill();
inline std::string_view name() const {
return {nameptr, (size_t)namelen};
}
inline i64 get_priority() const;
inline u64 get_addr() const;
inline i64 get_addend(const ElfRel<E> &rel) const;
inline std::span<ElfRel<E>> get_rels(Context<E> &ctx) const;
inline std::span<FdeRecord<E>> get_fdes() const;
ObjectFile<E> &file;
const ElfShdr<E> &shdr;
OutputSection<E> *output_section = nullptr;
std::string_view contents;
std::unique_ptr<SectionFragmentRef<E>[]> rel_fragments;
std::unique_ptr<u8[]> rel_exprs;
i32 fde_begin = -1;
i32 fde_end = -1;
const char *nameptr = nullptr;
i32 namelen = 0;
u32 offset = -1;
u32 section_idx = -1;
u32 relsec_idx = -1;
u32 reldyn_offset = 0;
// For COMDAT de-duplication and garbage collection
std::atomic_bool is_alive = true;
// For garbage collection
std::atomic_bool is_visited = false;
// For ICF
InputSection *leader = nullptr;
u32 icf_idx = -1;
bool icf_eligible = false;
bool icf_leaf = false;
bool is_ehframe = false;
private:
typedef enum { NONE, ERROR, COPYREL, PLT, DYNREL, BASEREL } Action;
void uncompress_old_style(Context<E> &ctx);
void uncompress_new_style(Context<E> &ctx);
void do_uncompress(Context<E> &ctx, std::string_view data, u64 size);
void dispatch(Context<E> &ctx, Action table[3][4], i64 i);
void report_undef(Context<E> &ctx, Symbol<E> &sym);
};
//
// output_chunks.cc
//
template <typename E>
bool is_relro(Context<E> &ctx, OutputChunk<E> *chunk);
// OutputChunk represents a contiguous region in an output file.
template <typename E>
class OutputChunk {
public:
// There are three types of OutputChunks:
// - HEADER: the ELF, section or segment headers
// - REGULAR: output sections containing input sections
// - SYNTHETIC: linker-synthesized sections such as .got or .plt
enum Kind : u8 { HEADER, REGULAR, SYNTHETIC };
virtual ~OutputChunk() = default;
virtual void copy_buf(Context<E> &ctx) {}
virtual void write_to(Context<E> &ctx, u8 *buf);
virtual void update_shdr(Context<E> &ctx) {}
std::string_view name;
i64 shndx = 0;
Kind kind;
bool new_page = false;
ElfShdr<E> shdr = {};
protected:
OutputChunk(Kind kind) : kind(kind) {
shdr.sh_addralign = 1;
}
};
// ELF header
template <typename E>
class OutputEhdr : public OutputChunk<E> {
public:
OutputEhdr() : OutputChunk<E>(this->HEADER) {
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_size = sizeof(ElfEhdr<E>);
this->shdr.sh_addralign = E::wordsize;
}
void copy_buf(Context<E> &ctx) override;
};
// Section header
template <typename E>
class OutputShdr : public OutputChunk<E> {
public:
OutputShdr() : OutputChunk<E>(this->HEADER) {
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
// Program header
template <typename E>
class OutputPhdr : public OutputChunk<E> {
public:
OutputPhdr() : OutputChunk<E>(this->HEADER) {
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class InterpSection : public OutputChunk<E> {
public:
InterpSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".interp";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
// Sections
template <typename E>
class OutputSection : public OutputChunk<E> {
public:
static OutputSection *
get_instance(Context<E> &ctx, std::string_view name, u64 type, u64 flags);
void copy_buf(Context<E> &ctx) override;
void write_to(Context<E> &ctx, u8 *buf) override;
std::vector<InputSection<E> *> members;
u32 idx;
private:
OutputSection(std::string_view name, u32 type, u64 flags, u32 idx);
};
template <typename E>
class GotSection : public OutputChunk<E> {
public:
GotSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".got";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = E::wordsize;
}
void add_got_symbol(Context<E> &ctx, Symbol<E> *sym);
void add_gottp_symbol(Context<E> &ctx, Symbol<E> *sym);
void add_tlsgd_symbol(Context<E> &ctx, Symbol<E> *sym);
void add_tlsdesc_symbol(Context<E> &ctx, Symbol<E> *sym);
void add_tlsld(Context<E> &ctx);
u64 get_tlsld_addr(Context<E> &ctx) const;
i64 get_reldyn_size(Context<E> &ctx) const;
void copy_buf(Context<E> &ctx) override;
std::vector<Symbol<E> *> got_syms;
std::vector<Symbol<E> *> gottp_syms;
std::vector<Symbol<E> *> tlsgd_syms;
std::vector<Symbol<E> *> tlsdesc_syms;
u32 tlsld_idx = -1;
};
template <typename E>
class GotPltSection : public OutputChunk<E> {
public:
GotPltSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".got.plt";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = E::wordsize;
}
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class PltSection : public OutputChunk<E> {
public:
PltSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".plt";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
this->shdr.sh_addralign = E::plt_hdr_size;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void copy_buf(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class PltGotSection : public OutputChunk<E> {
public:
PltGotSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".plt.got";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
this->shdr.sh_addralign = E::pltgot_size;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void copy_buf(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class RelPltSection : public OutputChunk<E> {
public:
RelPltSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = E::is_rel ? ".rel.plt" : ".rela.plt";
this->shdr.sh_type = E::is_rel ? SHT_REL : SHT_RELA;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(ElfRel<E>);
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class RelDynSection : public OutputChunk<E> {
public:
RelDynSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = E::is_rel ? ".rel.dyn" : ".rela.dyn";
this->shdr.sh_type = E::is_rel ? SHT_REL : SHT_RELA;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(ElfRel<E>);
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void sort(Context<E> &ctx);
i64 relcount = 0;
};
template <typename E>
class StrtabSection : public OutputChunk<E> {
public:
StrtabSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".strtab";
this->shdr.sh_type = SHT_STRTAB;
this->shdr.sh_size = 1;
}
void update_shdr(Context<E> &ctx) override;
};
template <typename E>
class ShstrtabSection : public OutputChunk<E> {
public:
ShstrtabSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".shstrtab";
this->shdr.sh_type = SHT_STRTAB;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class DynstrSection : public OutputChunk<E> {
public:
DynstrSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".dynstr";
this->shdr.sh_type = SHT_STRTAB;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_size = 1;
}
i64 add_string(std::string_view str);
i64 find_string(std::string_view str);
void copy_buf(Context<E> &ctx) override;
i64 dynsym_offset = -1;
private:
std::unordered_map<std::string_view, i64> strings;
};
template <typename E>
class DynamicSection : public OutputChunk<E> {
public:
DynamicSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".dynamic";
this->shdr.sh_type = SHT_DYNAMIC;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = E::wordsize;
this->shdr.sh_entsize = sizeof(ElfDyn<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class SymtabSection : public OutputChunk<E> {
public:
SymtabSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".symtab";
this->shdr.sh_type = SHT_SYMTAB;
this->shdr.sh_entsize = sizeof(ElfSym<E>);
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class DynsymSection : public OutputChunk<E> {
public:
DynsymSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".dynsym";
this->shdr.sh_type = SHT_DYNSYM;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(ElfSym<E>);
this->shdr.sh_addralign = E::wordsize;
this->shdr.sh_info = 1;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void finalize(Context<E> &ctx);
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols{1};
};
template <typename E>
class HashSection : public OutputChunk<E> {
public:
HashSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".hash";
this->shdr.sh_type = SHT_HASH;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = 4;
this->shdr.sh_addralign = 4;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class GnuHashSection : public OutputChunk<E> {
public:
GnuHashSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".gnu.hash";
this->shdr.sh_type = SHT_GNU_HASH;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
static constexpr i64 LOAD_FACTOR = 8;
static constexpr i64 HEADER_SIZE = 16;
static constexpr i64 BLOOM_SHIFT = 26;
static constexpr i64 ELFCLASS_BITS = E::wordsize * 8;
u32 num_buckets = -1;
u32 symoffset = -1;
u32 num_bloom = 1;
};
template <typename E>
class MergedSection : public OutputChunk<E> {
public:
static MergedSection<E> *
get_instance(Context<E> &ctx, std::string_view name, u64 type, u64 flags);
SectionFragment<E> *insert(std::string_view data, i64 alignment);
void assign_offsets();
void copy_buf(Context<E> &ctx) override;
void write_to(Context<E> &ctx, u8 *buf) override;
private:
using MapTy =
tbb::concurrent_hash_map<std::string_view, SectionFragment<E>>;
static constexpr i64 NUM_SHARDS = 64;
MergedSection(std::string_view name, u64 flags, u32 type)
: OutputChunk<E>(this->SYNTHETIC) {
this->name = name;
this->shdr.sh_flags = flags;
this->shdr.sh_type = type;
}
MapTy maps[NUM_SHARDS];
i64 shard_offsets[NUM_SHARDS + 1] = {};
tbb::enumerable_thread_specific<i64> max_alignments;
};
template <typename E>
class EhFrameSection : public OutputChunk<E> {
public:
EhFrameSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".eh_frame";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = E::wordsize;
}
void construct(Context<E> &ctx);
void apply_reloc(Context<E> &ctx, ElfRel<E> &rel, u64 loc, u64 val);
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class EhFrameHdrSection : public OutputChunk<E> {
public:
EhFrameHdrSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".eh_frame_hdr";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = 4;
this->shdr.sh_size = HEADER_SIZE;
}
static constexpr i64 HEADER_SIZE = 12;
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
u32 num_fdes = 0;
};
template <typename E>
class DynbssSection : public OutputChunk<E> {
public:
DynbssSection(bool is_relro) : OutputChunk<E>(this->SYNTHETIC) {
this->name = is_relro ? ".dynbss.rel.ro" : ".dynbss";
this->shdr.sh_type = SHT_NOBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = 64;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class VersymSection : public OutputChunk<E> {
public:
VersymSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".gnu.version";
this->shdr.sh_type = SHT_GNU_VERSYM;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = 2;
this->shdr.sh_addralign = 2;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
std::vector<u16> contents;
};
template <typename E>
class VerneedSection : public OutputChunk<E> {
public:
VerneedSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".gnu.version_r";
this->shdr.sh_type = SHT_GNU_VERNEED;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = E::wordsize;
}
void construct(Context<E> &ctx);
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
std::vector<u8> contents;
};
template <typename E>
class VerdefSection : public OutputChunk<E> {
public:
VerdefSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".gnu.version_d";
this->shdr.sh_type = SHT_GNU_VERDEF;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = 8;
}
void construct(Context<E> &ctx);
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
std::vector<u8> contents;
};
template <typename E>
class BuildIdSection : public OutputChunk<E> {
public:
BuildIdSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".note.gnu.build-id";
this->shdr.sh_type = SHT_NOTE;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = 4;
this->shdr.sh_size = 1;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
void write_buildid(Context<E> &ctx);
static constexpr i64 HEADER_SIZE = 16;
};
template <typename E>
class NotePropertySection : public OutputChunk<E> {
public:
NotePropertySection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".note.gnu.property";
this->shdr.sh_type = SHT_NOTE;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = E::wordsize;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
u32 features = 0;
};
template <typename E>
class GabiCompressedSection : public OutputChunk<E> {
public:
GabiCompressedSection(Context<E> &ctx, OutputChunk<E> &chunk);
void copy_buf(Context<E> &ctx) override;
private:
ElfChdr<E> chdr = {};
std::unique_ptr<ZlibCompressor> contents;
};
template <typename E>
class GnuCompressedSection : public OutputChunk<E> {
public:
GnuCompressedSection(Context<E> &ctx, OutputChunk<E> &chunk);
void copy_buf(Context<E> &ctx) override;
private:
static constexpr i64 HEADER_SIZE = 12;
i64 original_size = 0;
std::unique_ptr<ZlibCompressor> contents;
};
template <typename E>
class ReproSection : public OutputChunk<E> {
public:
ReproSection() : OutputChunk<E>(this->SYNTHETIC) {
this->name = ".repro";
this->shdr.sh_type = SHT_PROGBITS;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
private:
std::unique_ptr<GzipCompressor> contents;
};
bool is_c_identifier(std::string_view name);
template <typename E>
std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx);
//
// object_file.cc
//
// A comdat section typically represents an inline function,
// which are de-duplicated by the linker.
//
// For each inline function, there's one comdat section, which
// contains section indices of the function code and its data such as
// string literals, if any.
//
// Comdat sections are identified by its signature. If two comdat
// sections have the same signature, the linker picks up one and
// discards the other by eliminating all sections that the other
// comdat section refers to.
struct ComdatGroup {
ComdatGroup() = default;
ComdatGroup(const ComdatGroup &other)
: owner(other.owner.load()) {}
// The file priority of the owner file of this comdat section.
std::atomic_uint32_t owner = -1;
};
// InputFile is the base class of ObjectFile and SharedFile.
template <typename E>
class InputFile {
public:
InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb);
InputFile() : filename("<internal>") {}
template<typename T> std::span<T>
inline get_data(Context<E> &ctx, const ElfShdr<E> &shdr);
template<typename T> std::span<T>
inline get_data(Context<E> &ctx, i64 idx);
inline std::string_view get_string(Context<E> &ctx, const ElfShdr<E> &shdr);
inline std::string_view get_string(Context<E> &ctx, i64 idx);
ElfShdr<E> *find_section(i64 type);
MemoryMappedFile<E> *mb;
std::span<ElfShdr<E>> elf_sections;
std::vector<Symbol<E> *> symbols;
std::string filename;
bool is_dso = false;
u32 priority;
std::atomic_bool is_alive = false;
std::string_view shstrtab;
protected:
std::unique_ptr<Symbol<E>[]> local_syms;
};
// ObjectFile represents an input .o file.
template <typename E>
class ObjectFile : public InputFile<E> {
public:
static ObjectFile<E> *create(Context<E> &ctx, MemoryMappedFile<E> *mb,
std::string archive_name, bool is_in_lib);
static ObjectFile<E> *create_internal_file(Context<E> &ctx);
void parse(Context<E> &ctx);
void resolve_lazy_symbols(Context<E> &ctx);
void resolve_regular_symbols(Context<E> &ctx);
void mark_live_objects(Context<E> &ctx,
std::function<void(ObjectFile<E> *)> feeder);
void resolve_common_symbols(Context<E> &ctx);
void convert_undefined_weak_symbols(Context<E> &ctx);
void resolve_comdat_groups();
void eliminate_duplicate_comdat_groups();
void claim_unresolved_symbols(Context<E> &ctx);
void scan_relocations(Context<E> &ctx);
void convert_common_symbols(Context<E> &ctx);
void compute_symtab(Context<E> &ctx);
void write_symtab(Context<E> &ctx);
inline i64 get_shndx(const ElfSym<E> &esym);
inline InputSection<E> *get_section(const ElfSym<E> &esym);
inline std::span<Symbol<E> *> get_global_syms();
std::string archive_name;
std::vector<std::unique_ptr<InputSection<E>>> sections;
std::span<ElfSym<E>> elf_syms;
i64 first_global = 0;
const bool is_in_lib = false;
std::vector<CieRecord<E>> cies;
std::vector<FdeRecord<E>> fdes;
std::vector<const char *> symvers;
std::vector<SectionFragment<E> *> fragments;
std::vector<SectionFragmentRef<E>> sym_fragments;
std::vector<std::pair<ComdatGroup *, std::span<u32>>> comdat_groups;
bool exclude_libs = false;
u32 features = 0;
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;
u64 fde_idx = 0;
u64 fde_offset = 0;
u64 fde_size = 0;
private:
ObjectFile();
ObjectFile(Context<E> &ctx, MemoryMappedFile<E> *mb,
std::string archive_name, bool is_in_lib);
void initialize_sections(Context<E> &ctx);
void initialize_symbols(Context<E> &ctx);
void initialize_mergeable_sections(Context<E> &ctx);
void initialize_ehframe_sections(Context<E> &ctx);
u32 read_note_gnu_property(Context<E> &ctx, const ElfShdr<E> &shdr);
void read_ehframe(Context<E> &ctx, InputSection<E> &isec);
void override_symbol(Context<E> &ctx, Symbol<E> &sym,
const ElfSym<E> &esym, i64 symidx);
void merge_visibility(Context<E> &ctx, Symbol<E> &sym, u8 visibility);
std::pair<std::string_view, const ElfShdr<E> *>
uncompress_contents(Context<E> &ctx, const ElfShdr<E> &shdr,
std::string_view name);
bool has_common_symbol;
std::string_view symbol_strtab;
const ElfShdr<E> *symtab_sec;
std::span<u32> symtab_shndx_sec;
};
// SharedFile represents an input .so file.