lib/tldr/src/formats/elf/layout/section.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const elf = @import("../root.zig");
 3 
 4 const OutputSectionKind = elf.output_section.OutputSectionKind;
 5 const output_section_count = elf.output_section.output_section_count;
 6 
 7 pub const OutputSection = struct {
 8     kind: OutputSectionKind,
 9     file_size: u64 = 0,
10     memory_size: u64 = 0,
11     alignment: u64 = 1,
12     file_offset: u64 = 0,
13     address: u64 = 0,
14     reserved_size: u64 = 0,
15     section_index: u16 = 0,
16 
17     pub fn logicalSize(self: OutputSection) u64 {
18         if (self.kind.isAllocated()) return self.memory_size;
19         return self.file_size;
20     }
21 
22     pub fn fileLoadSize(self: OutputSection) u64 {
23         if (self.kind.isNoBits()) return 0;
24         return self.reserved_size;
25     }
26 };
27 
28 pub const SectionContribution = struct {
29     output_index_plus_one: u8 = 0,
30     offset: u64 = 0,
31     size: u64 = 0,
32     reserved_size: u64 = 0,
33     alignment: u64 = 0,
34 
35     pub fn init(output_index: usize, offset: u64, size: u64, reserved_size: u64, alignment: u64) SectionContribution {
36         std.debug.assert(output_index < output_section_count);
37         return .{
38             .output_index_plus_one = @intCast(output_index + 1),
39             .offset = offset,
40             .size = size,
41             .reserved_size = reserved_size,
42             .alignment = alignment,
43         };
44     }
45 
46     pub inline fn isPresent(self: SectionContribution) bool {
47         return self.output_index_plus_one != 0;
48     }
49 
50     pub inline fn outputIndex(self: SectionContribution) usize {
51         std.debug.assert(self.isPresent());
52         return @as(usize, self.output_index_plus_one) - 1;
53     }
54 };
55 
56 pub inline fn contributionAt(contributions: []const SectionContribution, index: usize) ?*const SectionContribution {
57     if (index >= contributions.len) return null;
58     const contribution = &contributions[index];
59     if (!contribution.isPresent()) return null;
60     return contribution;
61 }