lib/tldr/src/formats/elf/address/size.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const root = @import("../../../root.zig");
3 const elf = @import("../root.zig");
4 const boundary = @import("boundary.zig");
5
6 const model = root.model;
7 const layout = elf.layout;
8 const section_state = elf.section_state;
9
10 const GlobalSymbol = layout.GlobalSymbol;
11 const ObjectFile = elf.parser.ObjectFile;
12 const SymbolRef = layout.SymbolRef;
13 const symbolSectionDiscarded = section_state.symbolSectionDiscarded;
14
15 pub fn symbol(
16 objects: []const ObjectFile,
17 globals: *const std.StringHashMapUnmanaged(GlobalSymbol),
18 object_index: usize,
19 symbol_index: usize,
20 ) model.Error!u64 {
21 if (symbol_index >= objects[object_index].symbols.len) return error.UndefinedSymbol;
22 const symbol_record = objects[object_index].symbols[symbol_index];
23 if (symbol_record.isUndefined()) {
24 const global = globals.get(symbol_record.name) orelse {
25 if (symbol_record.isWeakUndefined()) return 0;
26 if (boundary.forSymbol(symbol_record.name) != null) return 0;
27 return error.UndefinedSymbol;
28 };
29 return symbol(objects, globals, global.ref.object_index, global.ref.symbol_index);
30 }
31 if (symbolSectionDiscarded(objects[object_index], symbol_record) and symbol_record.name.len != 0) {
32 const global = globals.get(symbol_record.name) orelse return error.MissingSection;
33 const current_ref = SymbolRef{ .object_index = object_index, .symbol_index = symbol_index };
34 if (global.ref.eql(current_ref)) return error.MissingSection;
35 return symbol(objects, globals, global.ref.object_index, global.ref.symbol_index);
36 }
37 if (symbol_record.isCommon() and symbol_record.name.len != 0) {
38 const global = globals.get(symbol_record.name) orelse return error.UndefinedSymbol;
39 const current_ref = SymbolRef{ .object_index = object_index, .symbol_index = symbol_index };
40 if (!global.ref.eql(current_ref)) return symbol(objects, globals, global.ref.object_index, global.ref.symbol_index);
41 }
42 return symbol_record.size;
43 }