lib/tldr/src/formats/elf/address/contribution.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const root = @import("../../../root.zig");
 2 const elf = @import("../root.zig");
 3 
 4 const model = root.model;
 5 const layout = elf.layout;
 6 const merge = elf.merge;
 7 const section_state = elf.section_state;
 8 
 9 const ObjectFile = elf.parser.ObjectFile;
10 const ObjectLayout = layout.ObjectLayout;
11 const SectionContribution = layout.SectionContribution;
12 const contributionAt = layout.contributionAt;
13 const foldedSection = section_state.foldedSection;
14 
15 pub fn symbol(
16     objects: []const ObjectFile,
17     layouts: []const ObjectLayout,
18     object_index: usize,
19     symbol_index: usize,
20 ) model.Error!SectionContribution {
21     const object = objects[object_index];
22     const object_layout = layouts[object_index];
23     if (symbol_index >= object.symbols.len) return error.UndefinedSymbol;
24     const symbol_record = object.symbols[symbol_index];
25     if (symbol_record.isCommon()) {
26         if (symbol_index >= object_layout.common_symbols.len) return error.UnsupportedRelocation;
27         return (contributionAt(object_layout.common_symbols, symbol_index) orelse return error.MissingSection).*;
28     }
29     if (symbol_record.section_index >= object.sections.len) return error.UnsupportedRelocation;
30     if (merge.contribution(object_layout, object, symbol_record.section_index, symbol_record.value)) |symbol_contribution| {
31         return symbol_contribution;
32     }
33     if (foldedSection(object, symbol_record.section_index)) |canonical| {
34         return (contributionAt(layouts[canonical.object_index].sections, canonical.section_index) orelse return error.MissingSection).*;
35     }
36     return (contributionAt(object_layout.sections, symbol_record.section_index) orelse return error.MissingSection).*;
37 }