Object.zig (1871B) - Raw
1 const std = @import("std"); 2 const Allocator = std.mem.Allocator; 3 const Elf = @import("Object/Elf.zig"); 4 5 const Object = @This(); 6 7 format: std.Target.ObjectFormat, 8 target: std.Target, 9 10 pub fn create(gpa: Allocator, target: std.Target) !*Object { 11 switch (target.ofmt) { 12 .elf => return Elf.create(gpa, target), 13 else => unreachable, 14 } 15 } 16 17 pub fn deinit(obj: *Object) void { 18 switch (obj.format) { 19 .elf => @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).deinit(), 20 else => unreachable, 21 } 22 } 23 24 pub const Section = union(enum) { 25 undefined, 26 data, 27 read_only_data, 28 func, 29 strings, 30 custom: []const u8, 31 }; 32 33 pub fn getSection(obj: *Object, section: Section) !*std.array_list.Managed(u8) { 34 switch (obj.format) { 35 .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).getSection(section), 36 else => unreachable, 37 } 38 } 39 40 pub const SymbolType = enum { 41 func, 42 variable, 43 external, 44 }; 45 46 pub fn declareSymbol( 47 obj: *Object, 48 section: Section, 49 name: ?[]const u8, 50 linkage: std.builtin.GlobalLinkage, 51 @"type": SymbolType, 52 offset: u64, 53 size: u64, 54 ) ![]const u8 { 55 switch (obj.format) { 56 .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).declareSymbol(section, name, linkage, @"type", offset, size), 57 else => unreachable, 58 } 59 } 60 61 pub fn addRelocation(obj: *Object, name: []const u8, section: Section, address: u64, addend: i64) !void { 62 switch (obj.format) { 63 .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).addRelocation(name, section, address, addend), 64 else => unreachable, 65 } 66 } 67 68 pub fn finish(obj: *Object, file: std.fs.File) !void { 69 switch (obj.format) { 70 .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).finish(file), 71 else => unreachable, 72 } 73 }