forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmold.h
2890 lines (2420 loc) · 84 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
#include "elf.h"
#include "../common/common.h"
#include <atomic>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_unordered_map.h>
#include <tbb/concurrent_vector.h>
#include <tbb/enumerable_thread_specific.h>
#include <tbb/spin_mutex.h>
#include <tbb/task_group.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>
#ifndef _WIN32
# include <unistd.h>
#endif
namespace mold::elf {
template <typename E> class InputFile;
template <typename E> class InputSection;
template <typename E> class MergedSection;
template <typename E> class ObjectFile;
template <typename E> class Chunk;
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 RelocSection;
template <typename E>
std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym);
//
// Mergeable section fragments
//
template <typename E>
struct SectionFragment {
SectionFragment(MergedSection<E> *sec, bool is_alive)
: output_section(*sec), is_alive(is_alive) {}
u64 get_addr(Context<E> &ctx) const;
MergedSection<E> &output_section;
u32 offset = -1;
Atomic<u8> p2align = 0;
Atomic<bool> is_alive = false;
};
// Additional 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.
template <typename E>
struct SymbolAux {
i32 got_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;
u32 djb_hash = 0;
};
template <>
struct SymbolAux<PPC64V1> : SymbolAux<X86_64> {
i32 opd_idx = -1;
};
//
// thunks.cc
//
template <typename E>
class Thunk {};
template <needs_thunk E>
class Thunk<E> {
public:
Thunk(OutputSection<E> &osec, i64 offset)
: output_section(osec), offset(offset) {}
i64 size() const { return E::thunk_hdr_size + symbols.size() * E::thunk_size; }
void copy_buf(Context<E> &ctx);
u64 get_addr(i64 idx) const {
return output_section.shdr.sh_addr + offset + E::thunk_hdr_size +
idx * E::thunk_size;
}
static constexpr i64 alignment = 16;
OutputSection<E> &output_section;
i64 offset;
std::mutex mu;
std::vector<Symbol<E> *> symbols;
};
struct ThunkRef {
i16 thunk_idx = -1;
i16 sym_idx = -1;
};
//
// input-sections.cc
//
// .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 CieRecord {
CieRecord(Context<E> &ctx, ObjectFile<E> &file, InputSection<E> &isec,
u32 input_offset, std::span<ElfRel<E>> rels, u32 rel_idx)
: file(file), input_section(isec), input_offset(input_offset),
rel_idx(rel_idx), rels(rels), contents(file.get_string(ctx, isec.shdr())) {}
i64 size() const {
return *(U32<E> *)(contents.data() + input_offset) + 4;
}
std::string_view get_contents() const {
return contents.substr(input_offset, size());
}
std::span<ElfRel<E>> get_rels() const {
i64 end = rel_idx;
while (end < rels.size() && rels[end].r_offset < input_offset + size())
end++;
return rels.subspan(rel_idx, end - rel_idx);
}
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;
};
template <typename E>
struct FdeRecord {
FdeRecord(u32 input_offset, u32 rel_idx)
: input_offset(input_offset), rel_idx(rel_idx) {}
i64 size(ObjectFile<E> &file) const;
std::string_view get_contents(ObjectFile<E> &file) const;
std::span<ElfRel<E>> get_rels(ObjectFile<E> &file) const;
u32 input_offset = -1;
u32 output_offset = -1;
u32 rel_idx = -1;
u16 cie_idx = -1;
Atomic<bool> is_alive = true;
};
// A struct to hold target-dependent input section members.
template <typename E>
struct InputSectionExtras {};
template <needs_thunk E>
struct InputSectionExtras<E> {
std::vector<ThunkRef> thunk_refs;
};
template <is_riscv E>
struct InputSectionExtras<E> {
std::vector<i32> r_deltas;
};
// InputSection represents a section in an input object file.
template <typename E>
class InputSection {
public:
InputSection(Context<E> &ctx, ObjectFile<E> &file, i64 shndx);
void uncompress(Context<E> &ctx);
void uncompress_to(Context<E> &ctx, u8 *buf);
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);
void kill();
std::string_view name() const;
i64 get_priority() const;
u64 get_addr() const;
const ElfShdr<E> &shdr() const;
std::span<ElfRel<E>> get_rels(Context<E> &ctx) const;
std::span<FdeRecord<E>> get_fdes() const;
std::string_view get_func_name(Context<E> &ctx, i64 offset) const;
bool is_relr_reloc(Context<E> &ctx, const ElfRel<E> &rel) const;
bool is_killed_by_icf() const;
bool record_undef_error(Context<E> &ctx, const ElfRel<E> &rel);
ObjectFile<E> &file;
OutputSection<E> *output_section = nullptr;
u64 sh_size = -1;
std::string_view contents;
[[no_unique_address]] InputSectionExtras<E> extra;
i32 fde_begin = -1;
i32 fde_end = -1;
u64 offset = -1;
u32 shndx = -1;
u32 relsec_idx = -1;
u32 reldyn_offset = 0;
bool uncompressed = false;
// For COMDAT de-duplication and garbage collection
std::atomic_bool is_alive = true;
u8 p2align = 0;
// For ICF
Atomic<bool> address_taken = false;
// For garbage collection
Atomic<bool> is_visited = false;
// For ICF
//
// `leader` is the section that this section has been merged with.
// Three kind of values are possible:
// - `leader == nullptr`: This section was not eligible for ICF.
// - `leader == this`: This section was retained.
// - `leader != this`: This section was merged with another identical section.
InputSection<E> *leader = nullptr;
u32 icf_idx = -1;
bool icf_eligible = false;
bool icf_leaf = false;
private:
void scan_pcrel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel);
void scan_absrel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel);
void scan_dyn_absrel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel);
void scan_toc_rel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel);
void scan_tlsdesc(Context<E> &ctx, Symbol<E> &sym);
void check_tlsle(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel);
void apply_dyn_absrel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel,
u8 *loc, u64 S, i64 A, u64 P, ElfRel<E> **dynrel);
void apply_toc_rel(Context<E> &ctx, Symbol<E> &sym, const ElfRel<E> &rel,
u8 *loc, u64 S, i64 A, u64 P, ElfRel<E> **dynrel);
void copy_contents_riscv(Context<E> &ctx, u8 *buf);
std::pair<SectionFragment<E> *, i64>
get_fragment(Context<E> &ctx, const ElfRel<E> &rel);
u64 get_thunk_addr(i64 idx);
std::optional<u64> get_tombstone(Symbol<E> &sym, SectionFragment<E> *frag);
};
//
// tls.cc
//
template <typename E> u64 get_tls_begin(Context<E> &);
template <typename E> u64 get_tp_addr(Context<E> &);
template <typename E> u64 get_dtp_addr(Context<E> &);
//
// output-chunks.cc
//
template <typename E>
u64 get_eflags(Context<E> &ctx);
template <typename E>
i64 to_phdr_flags(Context<E> &ctx, Chunk<E> *chunk);
template <typename E>
std::string_view get_output_name(Context<E> &ctx, std::string_view name, u64 flags);
template <typename E>
void write_plt_header(Context<E> &ctx, u8 *buf);
template <typename E>
void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym);
template <typename E>
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym);
typedef enum { HEADER, OUTPUT_SECTION, SYNTHETIC } ChunkKind;
// Chunk represents a contiguous region in an output file.
template <typename E>
class Chunk {
public:
virtual ~Chunk() = default;
virtual ChunkKind kind() { return SYNTHETIC; }
virtual OutputSection<E> *to_osec() { return nullptr; }
virtual i64 get_reldyn_size(Context<E> &ctx) const { return 0; }
virtual void construct_relr(Context<E> &ctx) {}
virtual void copy_buf(Context<E> &ctx) {}
virtual void write_to(Context<E> &ctx, u8 *buf) { unreachable(); }
virtual void update_shdr(Context<E> &ctx) {}
std::string_view name;
ElfShdr<E> shdr = { .sh_addralign = 1 };
i64 shndx = 0;
bool is_relro = false;
// For --gdb-index
bool is_compressed = false;
std::vector<u8> uncompressed_data;
// Some synethetic sections add local symbols to the output.
// For example, range extension thunks adds function_name@thunk
// symbol for each thunk entry. The following members are used
// for such synthesizing symbols.
virtual void compute_symtab_size(Context<E> &ctx) {};
virtual void populate_symtab(Context<E> &ctx) {};
i64 local_symtab_idx = 0;
i64 num_local_symtab = 0;
i64 strtab_size = 0;
i64 strtab_offset = 0;
// Offset in .rel.dyn
i64 reldyn_offset = 0;
// For --section-order
i64 sect_order = 0;
// For --pack-dyn-relocs=relr
std::vector<u64> relr;
};
// ELF header
template <typename E>
class OutputEhdr : public Chunk<E> {
public:
OutputEhdr(u32 sh_flags) {
this->name = "EHDR";
this->shdr.sh_flags = sh_flags;
this->shdr.sh_size = sizeof(ElfEhdr<E>);
this->shdr.sh_addralign = sizeof(Word<E>);
}
ChunkKind kind() override { return HEADER; }
void copy_buf(Context<E> &ctx) override;
};
// Section header
template <typename E>
class OutputShdr : public Chunk<E> {
public:
OutputShdr() {
this->name = "SHDR";
this->shdr.sh_size = 1;
this->shdr.sh_addralign = sizeof(Word<E>);
}
ChunkKind kind() override { return HEADER; }
void copy_buf(Context<E> &ctx) override;
};
// Program header
template <typename E>
class OutputPhdr : public Chunk<E> {
public:
OutputPhdr(u32 sh_flags) {
this->name = "PHDR";
this->shdr.sh_flags = sh_flags;
this->shdr.sh_addralign = sizeof(Word<E>);
}
ChunkKind kind() override { return HEADER; }
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
std::vector<ElfPhdr<E>> phdrs;
};
template <typename E>
class InterpSection : public Chunk<E> {
public:
InterpSection() {
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 Chunk<E> {
public:
OutputSection(Context<E> &ctx, std::string_view name, u32 type, u64 flags);
ChunkKind kind() override { return OUTPUT_SECTION; }
OutputSection<E> *to_osec() override { return this; }
void construct_relr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
void write_to(Context<E> &ctx, u8 *buf) override;
void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;
void create_range_extension_thunks(Context<E> &ctx);
std::vector<InputSection<E> *> members;
std::vector<std::unique_ptr<Thunk<E>>> thunks;
std::unique_ptr<RelocSection<E>> reloc_sec;
};
template <typename E>
class GotSection : public Chunk<E> {
public:
GotSection() {
this->name = ".got";
this->is_relro = true;
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = sizeof(Word<E>);
// We always create a .got so that _GLOBAL_OFFSET_TABLE_ has
// something to point to. s390x psABI define GOT[1] as a
// reserved slot, so we allocate one more for them.
this->shdr.sh_size = (is_s390x<E> ? 2 : 1) * sizeof(Word<E>);
}
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;
bool has_tlsld(Context<E> &ctx) const { return tlsld_idx != -1; }
i64 get_reldyn_size(Context<E> &ctx) const override;
void copy_buf(Context<E> &ctx) override;
void construct_relr(Context<E> &ctx) override;
void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;
std::vector<Symbol<E> *> got_syms;
std::vector<Symbol<E> *> tlsgd_syms;
std::vector<Symbol<E> *> tlsdesc_syms;
std::vector<Symbol<E> *> gottp_syms;
u32 tlsld_idx = -1;
};
template <typename E>
class GotPltSection : public Chunk<E> {
public:
GotPltSection(Context<E> &ctx) {
this->name = ".got.plt";
this->is_relro = ctx.arg.z_now;
this->shdr.sh_type = is_ppc64<E> ? SHT_NOBITS : SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = sizeof(Word<E>);
this->shdr.sh_size = HDR_SIZE;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
static constexpr i64 HDR_SIZE = (is_ppc64v2<E> ? 2 : 3) * sizeof(Word<E>);
static constexpr i64 ENTRY_SIZE = (is_ppc64v1<E> ? 3 : 1) * sizeof(Word<E>);
};
template <typename E>
class PltSection : public Chunk<E> {
public:
PltSection() {
this->name = ".plt";
this->shdr.sh_type = SHT_PROGBITS;
if constexpr (is_sparc<E>) {
this->shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR | SHF_WRITE;
this->shdr.sh_addralign = 256;
} else {
this->shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
this->shdr.sh_addralign = 16;
}
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class PltGotSection : public Chunk<E> {
public:
PltGotSection() {
this->name = ".plt.got";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_EXECINSTR;
this->shdr.sh_addralign = 16;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void copy_buf(Context<E> &ctx) override;
void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class RelPltSection : public Chunk<E> {
public:
RelPltSection() {
this->name = E::is_rela ? ".rela.plt" : ".rel.plt";
this->shdr.sh_type = E::is_rela ? SHT_RELA : SHT_REL;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(ElfRel<E>);
this->shdr.sh_addralign = sizeof(Word<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class RelDynSection : public Chunk<E> {
public:
RelDynSection() {
this->name = E::is_rela ? ".rela.dyn" : ".rel.dyn";
this->shdr.sh_type = E::is_rela ? SHT_RELA : SHT_REL;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(ElfRel<E>);
this->shdr.sh_addralign = sizeof(Word<E>);
}
void update_shdr(Context<E> &ctx) override;
void sort(Context<E> &ctx);
};
template <typename E>
class RelrDynSection : public Chunk<E> {
public:
RelrDynSection() {
this->name = ".relr.dyn";
this->shdr.sh_type = SHT_RELR;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_entsize = sizeof(Word<E>);
this->shdr.sh_addralign = sizeof(Word<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class StrtabSection : public Chunk<E> {
public:
StrtabSection() {
this->name = ".strtab";
this->shdr.sh_type = SHT_STRTAB;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
// Offsets in .strtab for ARM32 mapping symbols
static constexpr i64 ARM = 1;
static constexpr i64 THUMB = 4;
static constexpr i64 DATA = 7;
};
template <typename E>
class ShstrtabSection : public Chunk<E> {
public:
ShstrtabSection() {
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 Chunk<E> {
public:
DynstrSection() {
this->name = ".dynstr";
this->shdr.sh_type = SHT_STRTAB;
this->shdr.sh_flags = SHF_ALLOC;
}
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 Chunk<E> {
public:
DynamicSection() {
this->name = ".dynamic";
this->is_relro = true;
this->shdr.sh_type = SHT_DYNAMIC;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
this->shdr.sh_addralign = sizeof(Word<E>);
this->shdr.sh_entsize = sizeof(ElfDyn<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
ElfSym<E> to_output_esym(Context<E> &ctx, Symbol<E> &sym, u32 st_name,
U32<E> *shndx);
template <typename E>
class SymtabSection : public Chunk<E> {
public:
SymtabSection() {
this->name = ".symtab";
this->shdr.sh_type = SHT_SYMTAB;
this->shdr.sh_entsize = sizeof(ElfSym<E>);
this->shdr.sh_addralign = sizeof(Word<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class SymtabShndxSection : public Chunk<E> {
public:
SymtabShndxSection() {
this->name = ".symtab_shndx";
this->shdr.sh_type = SHT_SYMTAB_SHNDX;
this->shdr.sh_entsize = 4;
this->shdr.sh_addralign = 4;
}
};
template <typename E>
class DynsymSection : public Chunk<E> {
public:
DynsymSection() {
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 = sizeof(Word<E>);
}
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;
bool finalized = false;
};
template <typename E>
class HashSection : public Chunk<E> {
public:
HashSection() {
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 Chunk<E> {
public:
GnuHashSection() {
this->name = ".gnu.hash";
this->shdr.sh_type = SHT_GNU_HASH;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = sizeof(Word<E>);
}
std::span<Symbol<E> *> get_exported_symbols(Context<E> &ctx);
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;
u32 num_buckets = -1;
u32 num_bloom = 1;
};
template <typename E>
class MergedSection : public Chunk<E> {
public:
static MergedSection<E> *
get_instance(Context<E> &ctx, std::string_view name, i64 type, i64 flags,
i64 entsize, i64 addralign);
SectionFragment<E> *insert(Context<E> &ctx, std::string_view data,
u64 hash, i64 p2align);
void assign_offsets(Context<E> &ctx);
void copy_buf(Context<E> &ctx) override;
void write_to(Context<E> &ctx, u8 *buf) override;
void print_stats(Context<E> &ctx);
HyperLogLog estimator;
private:
MergedSection(std::string_view name, i64 flags, i64 type, i64 entsize);
ConcurrentMap<SectionFragment<E>> map;
std::vector<i64> shard_offsets;
std::once_flag once_flag;
};
template <typename E>
class EhFrameSection : public Chunk<E> {
public:
EhFrameSection() {
this->name = ".eh_frame";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = sizeof(Word<E>);
}
void construct(Context<E> &ctx);
void apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel, u64 offset, u64 val);
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class EhFrameHdrSection : public Chunk<E> {
public:
EhFrameHdrSection() {
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 EhFrameRelocSection : public Chunk<E> {
public:
EhFrameRelocSection() {
this->name = E::is_rela ? ".rela.eh_frame" : ".rel.eh_frame";
this->shdr.sh_type = E::is_rela ? SHT_RELA : SHT_REL;
this->shdr.sh_flags = SHF_INFO_LINK;
this->shdr.sh_addralign = sizeof(Word<E>);
this->shdr.sh_entsize = sizeof(ElfRel<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class CopyrelSection : public Chunk<E> {
public:
CopyrelSection(bool is_relro) {
this->name = is_relro ? ".copyrel.rel.ro" : ".copyrel";
this->is_relro = is_relro;
this->shdr.sh_type = SHT_NOBITS;
this->shdr.sh_flags = SHF_ALLOC | SHF_WRITE;
}
void add_symbol(Context<E> &ctx, Symbol<E> *sym);
void update_shdr(Context<E> &ctx) override;
i64 get_reldyn_size(Context<E> &ctx) const override { return symbols.size(); }
void copy_buf(Context<E> &ctx) override;
std::vector<Symbol<E> *> symbols;
};
template <typename E>
class VersymSection : public Chunk<E> {
public:
VersymSection() {
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<E>> contents;
};
template <typename E>
class VerneedSection : public Chunk<E> {
public:
VerneedSection() {
this->name = ".gnu.version_r";
this->shdr.sh_type = SHT_GNU_VERNEED;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = sizeof(Word<E>);
}
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 Chunk<E> {
public:
VerdefSection() {
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 Chunk<E> {
public:
BuildIdSection() {
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 NotePackageSection : public Chunk<E> {
public:
NotePackageSection() {
this->name = ".note.package";
this->shdr.sh_type = SHT_NOTE;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = 4;
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
};
template <typename E>
class NotePropertySection : public Chunk<E> {
public:
NotePropertySection() {
this->name = ".note.gnu.property";
this->shdr.sh_type = SHT_NOTE;
this->shdr.sh_flags = SHF_ALLOC;
this->shdr.sh_addralign = sizeof(Word<E>);
}
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
private:
static constexpr i64 ENTRY_SIZE = E::is_64 ? 16 : 12;
std::map<u32, u32> properties;
};
template <typename E>
class GdbIndexSection : public Chunk<E> {
public:
GdbIndexSection() {
this->name = ".gdb_index";
this->shdr.sh_type = SHT_PROGBITS;
this->shdr.sh_addralign = 4;
}
};
template <typename E>
class CompressedSection : public Chunk<E> {
public:
CompressedSection(Context<E> &ctx, Chunk<E> &chunk);
void copy_buf(Context<E> &ctx) override;
private:
ElfChdr<E> chdr = {};
std::unique_ptr<Compressor> compressed;
};
template <typename E>
class RelocSection : public Chunk<E> {
public:
RelocSection(Context<E> &ctx, OutputSection<E> &osec);
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
private:
OutputSection<E> &output_section;
std::vector<i64> offsets;
};
// PT_GNU_RELRO works on page granularity. We want to align its end to
// a page boundary. We append this section at end of a segment so that
// the segment always ends at a page boundary.
template <typename E>
class RelroPaddingSection : public Chunk<E> {
public:
RelroPaddingSection() {
this->name = ".relro_padding";