lib/tldr/src/formats/elf/sections.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const record = @import("record.zig");
 3 
 4 const Allocator = std.mem.Allocator;
 5 const Symbol = record.Symbol;
 6 
 7 pub const SectionRef = struct {
 8     object_index: usize,
 9     section_index: usize,
10 };
11 
12 pub fn ensureDiscardedSections(allocator: Allocator, object: anytype) Allocator.Error![]bool {
13     if (object.discarded_sections.len != 0) return object.discarded_sections;
14     object.discarded_sections = try allocator.alloc(bool, object.sections.len);
15     @memset(object.discarded_sections, false);
16     return object.discarded_sections;
17 }
18 
19 pub fn ensureFoldedSections(allocator: Allocator, object: anytype) Allocator.Error![]?SectionRef {
20     if (object.folded_sections.len != 0) return object.folded_sections;
21     object.folded_sections = try allocator.alloc(?SectionRef, object.sections.len);
22     @memset(object.folded_sections, null);
23     return object.folded_sections;
24 }
25 
26 pub inline fn sectionDiscarded(object: anytype, section_index: usize) bool {
27     if (section_index >= object.discarded_sections.len) return false;
28     return object.discarded_sections[section_index];
29 }
30 
31 pub inline fn foldedSection(object: anytype, section_index: usize) ?SectionRef {
32     if (section_index >= object.folded_sections.len) return null;
33     return object.folded_sections[section_index];
34 }
35 
36 pub fn symbolSectionDiscarded(object: anytype, symbol: Symbol) bool {
37     if (symbol.isUndefined() or symbol.isCommon() or symbol.isAbsolute()) return false;
38     return sectionDiscarded(object, symbol.section_index);
39 }