master
 1const std = @import("std");
 2const Allocator = std.mem.Allocator;
 3const Elf = @import("Object/Elf.zig");
 4
 5const Object = @This();
 6
 7format: std.Target.ObjectFormat,
 8target: std.Target,
 9
10pub 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
17pub fn deinit(obj: *Object) void {
18    switch (obj.format) {
19        .elf => @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).deinit(),
20        else => unreachable,
21    }
22}
23
24pub const Section = union(enum) {
25    undefined,
26    data,
27    read_only_data,
28    func,
29    strings,
30    custom: []const u8,
31};
32
33pub fn getSection(obj: *Object, section: Section) !*std.ArrayList(u8) {
34    switch (obj.format) {
35        .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).getSection(section),
36        else => unreachable,
37    }
38}
39
40pub const SymbolType = enum {
41    func,
42    variable,
43    external,
44};
45
46pub 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
61pub 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
68pub fn finish(obj: *Object, w: *std.Io.Writer) !void {
69    switch (obj.format) {
70        .elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).finish(w),
71        else => unreachable,
72    }
73}