mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
8cba6b1df8
This is f5fb720a5399ee98e45f36337b2f68a4d23a783c plus ehaas's nonnull attribute pull request currently at 4b26cb3ac610a0a070fc43e43da8b4cdf0e9101b with zig patches intact.
74 lines
1.8 KiB
Zig
Vendored
74 lines
1.8 KiB
Zig
Vendored
const std = @import("std");
|
|
const Allocator = std.mem.Allocator;
|
|
const Elf = @import("Object/Elf.zig");
|
|
|
|
const Object = @This();
|
|
|
|
format: std.Target.ObjectFormat,
|
|
target: std.Target,
|
|
|
|
pub fn create(gpa: Allocator, target: std.Target) !*Object {
|
|
switch (target.ofmt) {
|
|
.elf => return Elf.create(gpa, target),
|
|
else => unreachable,
|
|
}
|
|
}
|
|
|
|
pub fn deinit(obj: *Object) void {
|
|
switch (obj.format) {
|
|
.elf => @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).deinit(),
|
|
else => unreachable,
|
|
}
|
|
}
|
|
|
|
pub const Section = union(enum) {
|
|
undefined,
|
|
data,
|
|
read_only_data,
|
|
func,
|
|
strings,
|
|
custom: []const u8,
|
|
};
|
|
|
|
pub fn getSection(obj: *Object, section: Section) !*std.ArrayList(u8) {
|
|
switch (obj.format) {
|
|
.elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).getSection(section),
|
|
else => unreachable,
|
|
}
|
|
}
|
|
|
|
pub const SymbolType = enum {
|
|
func,
|
|
variable,
|
|
external,
|
|
};
|
|
|
|
pub fn declareSymbol(
|
|
obj: *Object,
|
|
section: Section,
|
|
name: ?[]const u8,
|
|
linkage: std.builtin.GlobalLinkage,
|
|
@"type": SymbolType,
|
|
offset: u64,
|
|
size: u64,
|
|
) ![]const u8 {
|
|
switch (obj.format) {
|
|
.elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).declareSymbol(section, name, linkage, @"type", offset, size),
|
|
else => unreachable,
|
|
}
|
|
}
|
|
|
|
pub fn addRelocation(obj: *Object, name: []const u8, section: Section, address: u64, addend: i64) !void {
|
|
switch (obj.format) {
|
|
.elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).addRelocation(name, section, address, addend),
|
|
else => unreachable,
|
|
}
|
|
}
|
|
|
|
pub fn finish(obj: *Object, w: *std.Io.Writer) !void {
|
|
switch (obj.format) {
|
|
.elf => return @as(*Elf, @alignCast(@fieldParentPtr("obj", obj))).finish(w),
|
|
else => unreachable,
|
|
}
|
|
}
|