elf_loader 0.16.0

A no_std-friendly ELF loader and runtime linker for Rust.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
use super::ObjectSections;
use crate::{
    AlignedBytes, MmapError, ParseShdrError, Result,
    arch::object::{PLT_ENTRY, PLT_ENTRY_SIZE},
    elf::{
        ElfLayout, ElfRelEntry, ElfRelType, ElfSectionFlags, ElfSectionId, ElfSectionType, ElfShdr,
        ElfWord,
    },
    entity::{EntityRef, PrimaryMap},
    input::{ElfReader, ElfReaderExt},
    memory::{ImageMemory, RegionAccess, VmAddr, VmOffset, rounddown, roundup},
    observer::{LoadObserver, SectionLayoutEvent},
    os::{MapFlags, Mmap, ProtFlags},
    relocation::{ObjectArch, RelocationArch},
    segment::{ElfSegment, ElfSegments, FileMapInfo, MemoryProtection, SegmentBuilder},
    sync::Arc,
};
use alloc::vec::Vec;
use core::{
    mem::{MaybeUninit, size_of},
    ptr::NonNull,
};
use hashbrown::{HashMap, HashSet, hash_map::Entry};

/// Convert section flags to memory protection flags
pub(crate) fn section_prot(sh_flags: ElfSectionFlags) -> ProtFlags {
    let mut prot = ProtFlags::PROT_READ;
    if sh_flags.contains(ElfSectionFlags::WRITE) {
        prot |= ProtFlags::PROT_WRITE;
    }
    if sh_flags.contains(ElfSectionFlags::EXECINSTR) {
        prot |= ProtFlags::PROT_EXEC;
    }
    prot
}

/// Manages segments created from ELF section headers
pub(crate) struct SectionSegments<Arch: ObjectArch = crate::arch::NativeArch> {
    core: SectionSegmentSet,
    init: SectionSegmentSet,
    pltgot: Option<PltGotSection>,
    _arch: core::marker::PhantomData<Arch>,
}

pub(crate) struct ObjectSegments<R: RegionAccess> {
    core: ElfSegments<R>,
    init: Option<ElfSegments<R>>,
}

impl<R: RegionAccess> ObjectSegments<R> {
    #[inline]
    pub(crate) const fn new(core: ElfSegments<R>, init: Option<ElfSegments<R>>) -> Self {
        Self { core, init }
    }

    #[inline]
    pub(crate) fn init(&self) -> Option<&ElfSegments<R>> {
        self.init.as_ref()
    }

    #[inline]
    pub(crate) fn view(&self) -> ObjectSegmentView<'_, R> {
        ObjectSegmentView::new(&self.core, self.init.as_ref())
    }

    #[inline]
    pub(crate) fn base_for(&self, lifetime: SectionLifetime) -> VmAddr {
        match lifetime {
            SectionLifetime::Core => self.core.base(),
            SectionLifetime::Init => self
                .init()
                .map(ElfSegments::base)
                .unwrap_or_else(|| self.core.base()),
        }
    }

    #[inline]
    pub(crate) fn into_parts(self) -> (ElfSegments<R>, Option<ElfSegments<R>>) {
        (self.core, self.init)
    }
}

/// Borrowed view over core and init-only object mappings.
pub struct ObjectSegmentView<'segments, R: RegionAccess> {
    core: &'segments ElfSegments<R>,
    init: Option<&'segments ElfSegments<R>>,
}

// Keep these impls manual so copying the borrowed view does not require R: Clone or R: Copy.
impl<R: RegionAccess> Clone for ObjectSegmentView<'_, R> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<R: RegionAccess> Copy for ObjectSegmentView<'_, R> {}

impl<'segments, R: RegionAccess> ObjectSegmentView<'segments, R> {
    #[inline]
    pub(crate) const fn new(
        core: &'segments ElfSegments<R>,
        init: Option<&'segments ElfSegments<R>>,
    ) -> Self {
        Self { core, init }
    }

    /// Returns the persistent core segment mapping for the object.
    #[inline]
    pub const fn core(&self) -> &'segments ElfSegments<R> {
        self.core
    }

    /// Returns the optional init-only segment mapping for the object.
    #[inline]
    pub const fn init(&self) -> Option<&'segments ElfSegments<R>> {
        self.init
    }

    #[inline]
    fn segment_for_addr(&self, addr: VmAddr) -> Result<&'segments ElfSegments<R>> {
        if self.core.contains_addr(addr) {
            return Ok(self.core);
        }
        if let Some(init) = self.init
            && init.contains_addr(addr)
        {
            return Ok(init);
        }
        Err(MmapError::InvalidMappedRegionRange.into())
    }
}

impl<R: RegionAccess> ImageMemory for ObjectSegmentView<'_, R> {
    #[inline]
    fn base(&self) -> VmAddr {
        self.core.base()
    }

    #[inline]
    fn host_ptr(&self, addr: VmAddr) -> Option<NonNull<u8>> {
        self.host_ptr_range(addr, 1)
    }

    #[inline]
    fn host_ptr_range(&self, addr: VmAddr, len: usize) -> Option<NonNull<u8>> {
        if self.core.contains_range(addr, len) {
            return self.core.host_ptr_range(addr, len);
        }
        if let Some(init) = self.init
            && init.contains_range(addr, len)
        {
            return init.host_ptr_range(addr, len);
        }
        None
    }

    #[inline]
    fn read_bytes(&self, addr: VmAddr, dst: &mut [u8]) -> Result<()> {
        self.segment_for_addr(addr)?.read_bytes(addr, dst)
    }

    #[inline]
    fn write_bytes(&self, addr: VmAddr, src: &[u8]) -> Result<()> {
        self.segment_for_addr(addr)?.write_bytes(addr, src)
    }
}

/// Handle to a bucket used to place object sections during layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SectionGroup(usize);

impl SectionGroup {
    /// Returns the raw group identifier.
    #[inline]
    pub const fn index(self) -> usize {
        self.0
    }
}

impl EntityRef for SectionGroup {
    #[inline]
    fn new(index: usize) -> Self {
        Self(index)
    }

    #[inline]
    fn index(self) -> usize {
        self.0
    }
}

const READ_SECTION_GROUP: SectionGroup = SectionGroup(0);
const WRITE_SECTION_GROUP: SectionGroup = SectionGroup(1);
const EXEC_SECTION_GROUP: SectionGroup = SectionGroup(2);
const WRITE_EXEC_SECTION_GROUP: SectionGroup = SectionGroup(3);
/// Loader-managed init-only group used for non-allocated staging metadata.
pub(crate) const STAGING_SECTION_GROUP: SectionGroup = SectionGroup(4);

/// Lifetime class for sections placed in a runtime layout group.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SectionLifetime {
    /// Section memory is retained for the object lifetime.
    Core,
    /// Section memory may be released after initialization completes.
    Init,
}

#[derive(Clone, Copy)]
pub(crate) struct SectionGroupDef {
    init_prot: ProtFlags,
    final_prot: ProtFlags,
    order: usize,
    lifetime: SectionLifetime,
}

impl SectionGroupDef {
    #[inline]
    pub(crate) const fn new(
        init_prot: ProtFlags,
        final_prot: ProtFlags,
        order: usize,
        lifetime: SectionLifetime,
    ) -> Self {
        Self {
            init_prot,
            final_prot,
            order,
            lifetime,
        }
    }
}

/// Configurable section layout groups used while loading relocatable objects.
#[derive(Clone)]
pub struct SectionGroups {
    defs: PrimaryMap<SectionGroup, SectionGroupDef>,
}

impl Default for SectionGroups {
    #[inline]
    fn default() -> Self {
        let mut groups = Self {
            defs: PrimaryMap::default(),
        };
        debug_assert_eq!(
            groups.define(
                ProtFlags::PROT_READ,
                ProtFlags::PROT_READ,
                0,
                SectionLifetime::Core,
            ),
            READ_SECTION_GROUP
        );
        debug_assert_eq!(
            groups.define(
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                1,
                SectionLifetime::Core,
            ),
            WRITE_SECTION_GROUP
        );
        debug_assert_eq!(
            groups.define(
                ProtFlags::PROT_READ | ProtFlags::PROT_EXEC,
                ProtFlags::PROT_READ | ProtFlags::PROT_EXEC,
                2,
                SectionLifetime::Core,
            ),
            EXEC_SECTION_GROUP
        );
        debug_assert_eq!(
            groups.define(
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC,
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC,
                3,
                SectionLifetime::Core,
            ),
            WRITE_EXEC_SECTION_GROUP
        );
        debug_assert_eq!(
            groups.define(
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                usize::MAX,
                SectionLifetime::Init,
            ),
            STAGING_SECTION_GROUP
        );
        groups
    }
}

impl SectionGroups {
    /// Defines one object section layout group and returns its handle.
    #[inline]
    pub fn define(
        &mut self,
        init_prot: ProtFlags,
        final_prot: ProtFlags,
        order: usize,
        lifetime: SectionLifetime,
    ) -> SectionGroup {
        self.defs
            .push(SectionGroupDef::new(init_prot, final_prot, order, lifetime))
    }

    pub(crate) fn sorted_defs(&self) -> Vec<(SectionGroup, SectionGroupDef)> {
        let mut defs: Vec<_> = self.defs.iter().map(|(group, def)| (group, *def)).collect();
        defs.sort_by_key(|(group, def)| {
            (
                def.order,
                matches!(def.lifetime, SectionLifetime::Init),
                group.index(),
            )
        });
        defs
    }

    #[inline]
    pub(crate) fn def(&self, group: SectionGroup) -> Option<SectionGroupDef> {
        self.defs.get(group).copied()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum SectionPlacement {
    Place(SectionGroup),
    Skip,
}

/// Final object section layout choices collected from [`SectionLayoutEvent`].
pub(crate) struct SectionLayoutPlan {
    groups: Arc<SectionGroups>,
    placements: Vec<SectionPlacement>,
}

impl SectionLayoutPlan {
    #[inline]
    pub(crate) fn placement(&self, id: ElfSectionId) -> SectionPlacement {
        self.placements[id.index()]
    }

    #[inline]
    fn placement_group_def(
        &self,
        id: ElfSectionId,
    ) -> Result<Option<(SectionGroup, SectionGroupDef)>> {
        match self.placement(id) {
            SectionPlacement::Place(group) => self
                .groups
                .def(group)
                .map(|def| (group, def))
                .map(Some)
                .ok_or_else(|| {
                    ParseShdrError::malformed("section layout group is not defined").into()
                }),
            SectionPlacement::Skip => Ok(None),
        }
    }
}

fn default_section_group(flags: ElfSectionFlags) -> SectionGroup {
    let prot = section_prot(flags);
    let index = usize::from(prot.contains(ProtFlags::PROT_WRITE))
        | (usize::from(prot.contains(ProtFlags::PROT_EXEC)) << 1);
    [
        READ_SECTION_GROUP,
        WRITE_SECTION_GROUP,
        EXEC_SECTION_GROUP,
        WRITE_EXEC_SECTION_GROUP,
    ][index]
}

fn create_section_plan<L: ElfLayout>(
    sections: &ObjectSections<L>,
    groups: Arc<SectionGroups>,
    placement_overrides: Vec<Option<SectionPlacement>>,
) -> SectionLayoutPlan {
    debug_assert_eq!(sections.headers().len(), placement_overrides.len());

    let placements = sections
        .headers()
        .iter()
        .enumerate()
        .map(|(index, shdr)| {
            placement_overrides[index].unwrap_or_else(|| {
                if shdr.flags().contains(ElfSectionFlags::ALLOC) {
                    SectionPlacement::Place(default_section_group(shdr.flags()))
                } else {
                    SectionPlacement::Place(STAGING_SECTION_GROUP)
                }
            })
        })
        .collect();
    SectionLayoutPlan { groups, placements }
}

#[derive(Clone, Copy)]
struct PltGotShdrs {
    got: Option<ElfSectionId>,
    got_plt: Option<ElfSectionId>,
    plt: Option<ElfSectionId>,
}

impl PltGotShdrs {
    #[inline]
    fn addr<L: ElfLayout>(id: Option<ElfSectionId>, sections: &ObjectSections<L>) -> VmAddr {
        id.map(|id| VmAddr::new(sections.section(id).sh_addr()))
            .unwrap_or_else(VmAddr::null)
    }

    #[inline]
    fn got_addr<L: ElfLayout>(&self, sections: &ObjectSections<L>) -> VmAddr {
        Self::addr(self.got, sections)
    }

    #[inline]
    fn got_plt_addr<L: ElfLayout>(&self, sections: &ObjectSections<L>) -> VmAddr {
        Self::addr(self.got_plt, sections)
    }

    #[inline]
    fn plt_addr<L: ElfLayout>(&self, sections: &ObjectSections<L>) -> VmAddr {
        Self::addr(self.plt, sections)
    }
}

fn prepare_pltgot_shdrs<L: ElfLayout>(
    sections: &mut ObjectSections<L>,
    got_cnt: usize,
    plt_cnt: usize,
) -> PltGotShdrs {
    let mut got = None;
    let mut got_plt = None;
    let mut plt = None;
    for index in 0..sections.headers().len() {
        let id = ElfSectionId::new(index);
        match sections.section_name(id).to_bytes() {
            b".got" => got = Some(id),
            b".got.plt" => got_plt = Some(id),
            b".plt" => plt = Some(id),
            _ => {}
        }
    }

    if let Some(id) = got {
        configure_pltgot_shdr(
            &mut sections.headers_mut()[id.index()],
            ElfSectionFlags::ALLOC | ElfSectionFlags::WRITE,
            got_cnt,
            size_of::<usize>(),
        );
    } else if got_cnt != 0 {
        got = Some(sections.push_section(".got", PltGotSection::create_got_shdr(got_cnt)));
    }
    if let Some(id) = got_plt {
        configure_pltgot_shdr(
            &mut sections.headers_mut()[id.index()],
            ElfSectionFlags::ALLOC | ElfSectionFlags::WRITE,
            plt_cnt,
            size_of::<usize>(),
        );
    } else if plt_cnt != 0 {
        got_plt =
            Some(sections.push_section(".got.plt", PltGotSection::create_got_plt_shdr(plt_cnt)));
    }
    if let Some(id) = plt {
        configure_pltgot_shdr(
            &mut sections.headers_mut()[id.index()],
            ElfSectionFlags::ALLOC | ElfSectionFlags::EXECINSTR,
            plt_cnt,
            PLT_ENTRY_SIZE,
        );
        sections.headers_mut()[id.index()].set_sh_addralign(size_of::<usize>());
    } else if plt_cnt != 0 {
        plt = Some(sections.push_section(".plt", PltGotSection::create_plt_shdr(plt_cnt)));
    }

    PltGotShdrs { got, got_plt, plt }
}

fn configure_pltgot_shdr<L: ElfLayout>(
    shdr: &mut ElfShdr<L>,
    flags: ElfSectionFlags,
    elem_cnt: usize,
    ent_size: usize,
) {
    shdr.set_section_type(ElfSectionType::NOBITS);
    shdr.set_flags(flags);
    shdr.set_sh_size(elem_cnt * ent_size);
    shdr.set_sh_addralign(16);
    shdr.set_sh_entsize(ent_size);
}

fn create_pltgot_shdr<L: ElfLayout>(
    flags: ElfSectionFlags,
    elem_cnt: usize,
    ent_size: usize,
) -> ElfShdr<L> {
    ElfShdr::new(
        0,
        ElfSectionType::NOBITS,
        flags,
        0,
        0,
        elem_cnt * ent_size,
        0,
        0,
        16,
        ent_size,
    )
}

#[derive(Default)]
struct SectionSegmentSet {
    segments: Vec<ElfSegment>,
    final_prots: Vec<ProtFlags>,
    total_size: usize,
}

impl SectionSegmentSet {
    #[inline]
    fn is_empty(&self) -> bool {
        self.total_size == 0
    }

    #[inline]
    fn push(&mut self, segment: ElfSegment, final_prot: ProtFlags) {
        self.segments.push(segment);
        self.final_prots.push(final_prot);
    }

    fn mprotect_final<R>(&self, segments: &ElfSegments<R>) -> Result<()>
    where
        R: RegionAccess,
    {
        for (segment, final_prot) in self.segments.iter().zip(self.final_prots.iter().copied()) {
            if segment.prot.bits() == final_prot.bits() {
                continue;
            }
            MemoryProtection::new(
                segments.base() + segment.offset,
                segment.len,
                segment.page_size,
                final_prot,
            )
            .apply(segments)?;
        }
        Ok(())
    }
}

impl SegmentBuilder for SectionSegmentSet {
    fn create_space<M>(&mut self, mapper: &M) -> Result<ElfSegments<M::Region>>
    where
        M: Mmap + ?Sized,
    {
        let len = self.total_size;
        if len == 0 {
            let region = unsafe {
                mapper.create_space(None, 1, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, false)
            }?;
            let base = region.addr();
            return Ok(ElfSegments::from_ranges(region, base, Vec::new()));
        }

        let region = unsafe {
            mapper.create_space(
                None,
                len,
                ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
                false,
            )
        }?;
        let base = region.addr();
        Ok(ElfSegments::new(region, base, VmOffset::new(0)))
    }

    fn create_segments(&mut self) -> Result<()> {
        Ok(())
    }

    fn segments_mut(&mut self) -> &mut [ElfSegment] {
        &mut self.segments
    }

    fn segments(&self) -> &[ElfSegment] {
        &self.segments
    }
}

impl<Arch: ObjectArch> SectionSegments<Arch> {
    pub(crate) fn load<D, Obs, M>(
        sections: &mut ObjectSections<Arch::Layout>,
        object: &impl ElfReader,
        page_size: usize,
        groups: Arc<SectionGroups>,
        observer: &mut Obs,
        mapper: &M,
    ) -> Result<(Self, ObjectSegments<M::Region>)>
    where
        D: 'static,
        Obs: LoadObserver<D, Arch>,
        M: Mmap + ?Sized,
    {
        let (got_cnt, plt_cnt) =
            PltGotSection::count_needed_entries::<Arch>(sections.headers(), object)?;

        let pltgot_shdrs = prepare_pltgot_shdrs(sections, got_cnt, plt_cnt);
        let mut event = SectionLayoutEvent::new(sections);
        observer.on_section_layout(&mut event)?;
        let placement_overrides = event.into_placements();
        let plan = create_section_plan(sections, groups, placement_overrides);
        let mut units = create_section_units::<Arch::Layout>(&plan);

        for (index, shdr) in sections.headers_mut().iter_mut().enumerate() {
            let id = ElfSectionId::new(index);
            if let Some((group, _)) = plan.placement_group_def(id)? {
                add_section_to_units(&mut units, group, shdr)?
            }
        }
        let mut core = SectionSegmentSet::default();
        let mut init = SectionSegmentSet::default();
        let mut core_offset = 0;
        let mut init_offset = 0;
        for (_, unit) in &mut units {
            let (offset, segment_set) = match unit.lifetime {
                SectionLifetime::Core => (&mut core_offset, &mut core),
                SectionLifetime::Init => (&mut init_offset, &mut init),
            };
            if let Some(segment) = unit.create_segment(offset, page_size) {
                let final_prot = unit.final_prot;
                *offset = roundup(*offset, page_size);
                segment_set.push(segment, final_prot);
            }
        }
        drop(units);
        core.total_size = core_offset;
        init.total_size = init_offset;

        let mut section_segments = Self {
            core,
            init,
            pltgot: None,
            _arch: core::marker::PhantomData,
        };
        let segments = section_segments.load_segments(mapper, object)?;
        section_segments.rebase_loaded_sections(sections.headers_mut(), &plan, &segments)?;
        section_segments.pltgot = Some(PltGotSection::new(
            pltgot_shdrs.got_addr(sections),
            pltgot_shdrs.got_plt_addr(sections),
            pltgot_shdrs.plt_addr(sections),
        ));
        sections.set_layout_metadata(&plan.placements);
        Ok((section_segments, segments))
    }

    pub(crate) fn take_pltgot(&mut self) -> PltGotSection {
        self.pltgot.take().expect("PLTGOT already taken")
    }

    fn load_segments<M>(
        &mut self,
        mapper: &M,
        object: &impl ElfReader,
    ) -> Result<ObjectSegments<M::Region>>
    where
        M: Mmap + ?Sized,
    {
        let core = self.core.load_segments(mapper, object)?;
        let init = if self.init.is_empty() {
            None
        } else {
            Some(self.init.load_segments(mapper, object)?)
        };
        Ok(ObjectSegments::new(core, init))
    }

    pub(crate) fn mprotect<R>(&self, segments: &ObjectSegmentView<'_, R>) -> Result<()>
    where
        R: RegionAccess,
    {
        self.core.mprotect(segments.core())?;
        if let Some(init) = segments.init() {
            self.init.mprotect(init)?;
        }
        Ok(())
    }

    pub(crate) fn mprotect_final<R>(&self, segments: &ObjectSegmentView<'_, R>) -> Result<()>
    where
        R: RegionAccess,
    {
        self.core.mprotect_final(segments.core())?;
        if let Some(init) = segments.init() {
            self.init.mprotect_final(init)?;
        }
        Ok(())
    }

    fn rebase_loaded_sections<R>(
        &mut self,
        shdrs: &mut [ElfShdr<Arch::Layout>],
        plan: &SectionLayoutPlan,
        segments: &ObjectSegments<R>,
    ) -> Result<()>
    where
        R: RegionAccess,
    {
        for (index, shdr) in shdrs.iter_mut().enumerate() {
            let Some((_, group_def)) = plan.placement_group_def(ElfSectionId::new(index))? else {
                continue;
            };
            let base = segments.base_for(group_def.lifetime);
            shdr.set_sh_addr((base + VmOffset::new(shdr.sh_addr())).get());
        }

        Ok(())
    }
}

/// Manages PLT (Procedure Linkage Table) and GOT (Global Offset Table) sections
pub(crate) struct PltGotSection {
    got_base: VmAddr,
    got_plt_base: VmAddr,
    plt_base: VmAddr,
    got_idx: usize,
    got_plt_idx: usize,
    plt_idx: usize,
    got_map: HashMap<ObjectRelocKey, usize>,
    plt_map: HashMap<ObjectRelocKey, usize>,
}

pub(crate) struct UsizeEntry<'entry>(&'entry mut usize);

impl UsizeEntry<'_> {
    pub(crate) fn update(&mut self, value: VmAddr) {
        *self.0 = value.get();
    }

    pub(crate) fn get_addr(&self) -> VmAddr {
        VmAddr::from_ptr(self.0 as *const _)
    }
}

pub(crate) enum GotEntry<'got> {
    Occupied(VmAddr),
    Vacant(UsizeEntry<'got>),
}

pub(crate) enum PltEntry<'plt> {
    Occupied(VmAddr),
    Vacant {
        plt: &'plt mut [u8],
        got: UsizeEntry<'plt>,
    },
}

/// Relocation identity used to deduplicate object GOT/PLT entries.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct ObjectRelocKey {
    r_type: crate::elf::ElfRelocationType,
    r_sym: usize,
    addend: isize,
}

impl ObjectRelocKey {
    #[inline]
    pub(crate) fn new<Arch: RelocationArch>(rel: &ElfRelType<Arch>, addend: isize) -> Self {
        Self {
            r_type: rel.r_type(),
            r_sym: rel.r_symbol(),
            addend,
        }
    }
}

#[inline]
fn object_relocation_key_addend<Arch>(
    object: &impl ElfReader,
    target: &ElfShdr<Arch::Layout>,
    rel: &ElfRelType<Arch>,
) -> Result<isize>
where
    Arch: RelocationArch,
{
    if rel.as_rel().is_some() {
        let offset = rel.r_offset().get();
        let len = size_of::<<Arch::Layout as ElfLayout>::Word>();
        let Some(end) = offset.checked_add(len) else {
            return Err(ParseShdrError::malformed("REL relocation addend range overflows").into());
        };
        if end > target.sh_size() {
            return Err(
                ParseShdrError::malformed("REL relocation addend exceeds target section").into(),
            );
        }
        if target.section_type() == ElfSectionType::NOBITS {
            return Ok(0);
        }

        let file_offset = target.sh_offset().checked_add(offset).ok_or_else(|| {
            ParseShdrError::malformed("REL relocation addend file offset overflows")
        })?;
        let mut word = MaybeUninit::<<Arch::Layout as ElfLayout>::Word>::uninit();
        let bytes = unsafe { core::slice::from_raw_parts_mut(word.as_mut_ptr().cast::<u8>(), len) };
        object.read(bytes, file_offset)?;
        return Ok(unsafe { word.assume_init() }.to_usize() as isize);
    }

    rel.as_rela()
        .map(|rel| rel.r_addend())
        .ok_or_else(|| ParseShdrError::malformed("RELA relocation key addend is missing").into())
}

impl PltGotSection {
    fn count_needed_entries<Arch: ObjectArch>(
        shdrs: &[ElfShdr<Arch::Layout>],
        object: &impl ElfReader,
    ) -> Result<(usize, usize)> {
        let mut got_set = HashSet::new();
        let mut got_plt_set = HashSet::new();
        let mut scratch = AlignedBytes::default();

        for shdr in shdrs
            .iter()
            .filter(|s| matches!(s.section_type(), ElfSectionType::REL | ElfSectionType::RELA))
        {
            let entry_size = size_of::<ElfRelType<Arch>>();
            let size = shdr.sh_size() / entry_size * entry_size;
            if size == 0 {
                continue;
            }
            let target = shdrs.get(shdr.sh_info() as usize).ok_or_else(|| {
                ParseShdrError::malformed("relocation target section is out of range")
            })?;

            object.with_bytes::<ElfRelType<Arch>, _, _>(
                shdr.sh_offset(),
                size,
                &mut scratch,
                |relocations| {
                    for rel_entry in relocations {
                        let r_type = rel_entry.r_type();
                        let needs_got = Arch::needs_got(r_type);
                        let needs_plt = Arch::needs_plt(r_type);
                        if !needs_got && !needs_plt {
                            continue;
                        }
                        let addend =
                            object_relocation_key_addend::<Arch>(object, target, rel_entry)?;
                        let key = ObjectRelocKey::new::<Arch>(rel_entry, addend);

                        if needs_got {
                            got_set.insert(key);
                        }
                        if needs_plt {
                            got_plt_set.insert(key);
                        }
                    }
                    Ok(())
                },
            )?;
        }

        Ok((got_set.len(), got_plt_set.len()))
    }

    fn create_got_shdr<L: ElfLayout>(elem_cnt: usize) -> ElfShdr<L> {
        create_pltgot_shdr(
            ElfSectionFlags::ALLOC | ElfSectionFlags::WRITE,
            elem_cnt,
            size_of::<usize>(),
        )
    }

    fn create_got_plt_shdr<L: ElfLayout>(elem_cnt: usize) -> ElfShdr<L> {
        create_pltgot_shdr(
            ElfSectionFlags::ALLOC | ElfSectionFlags::WRITE,
            elem_cnt,
            size_of::<usize>(),
        )
    }

    fn create_plt_shdr<L: ElfLayout>(elem_cnt: usize) -> ElfShdr<L> {
        let mut shdr = create_pltgot_shdr(
            ElfSectionFlags::ALLOC | ElfSectionFlags::EXECINSTR,
            elem_cnt,
            PLT_ENTRY_SIZE,
        );
        shdr.set_sh_addralign(size_of::<usize>());
        shdr
    }

    fn new(got_base: VmAddr, got_plt_base: VmAddr, plt_base: VmAddr) -> Self {
        Self {
            got_idx: 0,
            got_plt_idx: 0,
            plt_idx: 0,
            got_map: HashMap::new(),
            plt_map: HashMap::new(),
            got_base,
            got_plt_base,
            plt_base,
        }
    }

    pub(crate) fn add_got_entry(&mut self, key: ObjectRelocKey) -> GotEntry<'_> {
        let base = self.got_base;
        let ent_size = size_of::<usize>();
        match self.got_map.entry(key) {
            Entry::Occupied(mut entry) => {
                GotEntry::Occupied(base + VmOffset::new(*entry.get_mut() * ent_size))
            }
            Entry::Vacant(entry) => {
                let idx = *entry.insert(self.got_idx);
                self.got_idx += 1;
                GotEntry::Vacant(unsafe {
                    UsizeEntry(&mut *(base + VmOffset::new(idx * ent_size)).as_mut_ptr())
                })
            }
        }
    }

    pub(crate) fn add_plt_entry(&mut self, key: ObjectRelocKey) -> PltEntry<'_> {
        let plt_base = self.plt_base;
        let got_plt_base = self.got_plt_base;
        let plt_ent_size = PLT_ENTRY_SIZE;
        let got_ent_size = size_of::<usize>();
        match self.plt_map.entry(key) {
            Entry::Occupied(mut entry) => {
                PltEntry::Occupied(plt_base + VmOffset::new(*entry.get_mut() * plt_ent_size))
            }
            Entry::Vacant(entry) => {
                let plt_idx = *entry.insert(self.plt_idx);
                self.plt_idx += 1;

                let got_idx = self.got_plt_idx;
                self.got_plt_idx += 1;

                let plt = unsafe {
                    core::slice::from_raw_parts_mut(
                        (plt_base + VmOffset::new(plt_idx * plt_ent_size)).as_mut_ptr(),
                        plt_ent_size,
                    )
                };

                plt.copy_from_slice(&PLT_ENTRY);

                PltEntry::Vacant {
                    plt,
                    got: unsafe {
                        UsizeEntry(
                            &mut *(got_plt_base + VmOffset::new(got_idx * got_ent_size))
                                .as_mut_ptr(),
                        )
                    },
                }
            }
        }
    }
}

struct SectionUnit<'shdr, L: ElfLayout> {
    init_prot: ProtFlags,
    final_prot: ProtFlags,
    lifetime: SectionLifetime,
    content_sections: Vec<&'shdr mut ElfShdr<L>>,
    zero_sections: Vec<&'shdr mut ElfShdr<L>>,
}

impl<'shdr, L: ElfLayout> SectionUnit<'shdr, L> {
    fn new(init_prot: ProtFlags, final_prot: ProtFlags, lifetime: SectionLifetime) -> Self {
        Self {
            init_prot,
            final_prot,
            lifetime,
            content_sections: Vec::new(),
            zero_sections: Vec::new(),
        }
    }

    fn add_section(&mut self, shdr: &'shdr mut ElfShdr<L>) {
        if shdr.section_type() == ElfSectionType::NOBITS {
            self.zero_sections.push(shdr);
        } else {
            self.content_sections.push(shdr);
        }
    }

    fn create_segment(&mut self, base_offset: &mut usize, page_size: usize) -> Option<ElfSegment> {
        self.content_sections
            .first()
            .or(self.zero_sections.first())?;

        let segment_start = *base_offset;

        let mut current_offset = segment_start;
        let mut map_info = Vec::new();
        for shdr in &mut self.content_sections {
            if shdr.sh_size() == 0 {
                continue;
            }
            current_offset = roundup(current_offset, shdr.sh_addralign());
            shdr.set_sh_addr(current_offset);
            map_info.push(FileMapInfo {
                filesz: shdr.sh_size(),
                offset: shdr.sh_offset(),
                start: current_offset - segment_start,
            });
            current_offset += shdr.sh_size();
        }

        if map_info.len() == 1 {
            let info = &mut map_info[0];
            let file_offset = rounddown(info.offset, page_size);
            let align_len = info.offset - file_offset;

            let shdr = self
                .content_sections
                .iter_mut()
                .find(|shdr| shdr.sh_offset() == info.offset)
                .unwrap();

            shdr.add_sh_addr(align_len);
            info.filesz += align_len;
            info.offset = file_offset;
            current_offset += align_len;
        }

        let content_size = current_offset - segment_start;

        for shdr in &mut self.zero_sections {
            current_offset = roundup(current_offset, shdr.sh_addralign());
            shdr.set_sh_addr(current_offset);
            current_offset += shdr.sh_size();
        }

        let unaligned_total_size = current_offset - segment_start;
        let total_size = roundup(unaligned_total_size, page_size);

        if total_size == 0 {
            return None;
        }

        *base_offset += total_size;
        Some(ElfSegment {
            offset: VmOffset::new(segment_start),
            prot: self.init_prot,
            len: total_size,
            page_size,
            content_size,
            zero_size: unaligned_total_size - content_size,
            need_copy: false,
            flags: MapFlags::MAP_PRIVATE | MapFlags::MAP_FIXED,
            map_info,
            from_relocatable: true,
        })
    }
}

fn create_section_units<L: ElfLayout>(
    plan: &SectionLayoutPlan,
) -> Vec<(SectionGroup, SectionUnit<'_, L>)> {
    plan.groups
        .sorted_defs()
        .into_iter()
        .map(|(group, def)| {
            (
                group,
                SectionUnit::new(def.init_prot, def.final_prot, def.lifetime),
            )
        })
        .collect()
}

fn add_section_to_units<'shdr, L: ElfLayout>(
    units: &mut [(SectionGroup, SectionUnit<'shdr, L>)],
    group: SectionGroup,
    shdr: &'shdr mut ElfShdr<L>,
) -> Result<()> {
    let Some((_, unit)) = units
        .iter_mut()
        .find(|(unit_group, _)| *unit_group == group)
    else {
        return Err(ParseShdrError::malformed("section layout group is not defined").into());
    };
    unit.add_section(shdr);
    Ok(())
}