authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-04 17:52:10+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-05 10:15:01+01:00
log83d89a05b7670c5c25d4f0f717ac5168a9cb6557
tree66e0874d751a6c8773d8b07cf7ac960f0d3f9fff
parent205421c3117dbcd819dc636a7485e3cfeec0be20

coff: compile and link simple exit program on arm64

* make image base target dependent * fix relocs to imports

5 files changed, 163 insertions(+), 112 deletions(-)

lib/std/io.zig+3-3
......@@ -36,7 +36,7 @@ pub const default_mode: ModeOverride = if (is_async) Mode.evented else .blocking
3636
3737fn getStdOutHandle() os.fd_t {
3838 if (builtin.os.tag == .windows) {
39 if (builtin.zig_backend == .stage2_x86_64) {
39 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
4040 // TODO: this is just a temporary workaround until we advance x86 backend further along.
4141 return os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
4242 }
......@@ -62,7 +62,7 @@ pub fn getStdOut() File {
6262
6363fn getStdErrHandle() os.fd_t {
6464 if (builtin.os.tag == .windows) {
65 if (builtin.zig_backend == .stage2_x86_64) {
65 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
6666 // TODO: this is just a temporary workaround until we advance x86 backend further along.
6767 return os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
6868 }
......@@ -88,7 +88,7 @@ pub fn getStdErr() File {
8888
8989fn getStdInHandle() os.fd_t {
9090 if (builtin.os.tag == .windows) {
91 if (builtin.zig_backend == .stage2_x86_64) {
91 if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_aarch64) {
9292 // TODO: this is just a temporary workaround until we advance x86 backend further along.
9393 return os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE) catch os.windows.INVALID_HANDLE_VALUE;
9494 }
src/arch/aarch64/CodeGen.zig+26-13
......@@ -142,7 +142,8 @@ const MCValue = union(enum) {
142142 /// The value is in memory but requires a linker relocation fixup:
143143 /// * got - the value is referenced indirectly via GOT entry index (the linker emits a got-type reloc)
144144 /// * direct - the value is referenced directly via symbol index index (the linker emits a displacement reloc)
145 linker_load: struct { @"type": enum { got, direct }, sym_index: u32 },
145 /// * import - the value is referenced indirectly via import entry index (the linker emits an import-type reloc)
146 linker_load: struct { @"type": enum { got, direct, import }, sym_index: u32 },
146147 /// The value is one of the stack variables.
147148 ///
148149 /// If the type is a pointer, it means the pointer address is in
......@@ -3717,6 +3718,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
37173718 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
37183719 .got => .load_memory_ptr_got,
37193720 .direct => .load_memory_ptr_direct,
3721 .import => unreachable,
37203722 };
37213723 const mod = self.bin_file.options.module.?;
37223724 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
......@@ -4086,16 +4088,17 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
40864088 });
40874089 }
40884090 const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4089
4090 _ = try self.addInst(.{
4091 .tag = .call_extern,
4092 .data = .{
4093 .relocation = .{
4094 .atom_index = mod.declPtr(self.mod_fn.owner_decl).link.coff.sym_index,
4095 .sym_index = sym_index,
4096 },
4091 try self.genSetReg(Type.initTag(.u64), .x30, .{
4092 .linker_load = .{
4093 .@"type" = .import,
4094 .sym_index = sym_index,
40974095 },
40984096 });
4097 // blr x30
4098 _ = try self.addInst(.{
4099 .tag = .blr,
4100 .data = .{ .reg = .x30 },
4101 });
40994102 } else {
41004103 return self.fail("TODO implement calling bitcasted functions", .{});
41014104 }
......@@ -4119,8 +4122,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
41194122 } else {
41204123 return self.fail("TODO implement calling bitcasted functions", .{});
41214124 }
4122 } else if (self.bin_file.cast(link.File.Coff)) |_| {
4123 return self.fail("TODO implement calling in COFF for {}", .{self.target.cpu.arch});
41244125 } else unreachable;
41254126 } else {
41264127 assert(ty.zigTypeTag() == .Pointer);
......@@ -5203,6 +5204,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
52035204 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
52045205 .got => .load_memory_ptr_got,
52055206 .direct => .load_memory_ptr_direct,
5207 .import => unreachable,
52065208 };
52075209 const mod = self.bin_file.options.module.?;
52085210 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
......@@ -5316,6 +5318,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
53165318 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
53175319 .got => .load_memory_got,
53185320 .direct => .load_memory_direct,
5321 .import => .load_memory_import,
53195322 };
53205323 const mod = self.bin_file.options.module.?;
53215324 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
......@@ -5509,6 +5512,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
55095512 const tag: Mir.Inst.Tag = switch (load_struct.@"type") {
55105513 .got => .load_memory_ptr_got,
55115514 .direct => .load_memory_ptr_direct,
5515 .import => unreachable,
55125516 };
55135517 const mod = self.bin_file.options.module.?;
55145518 const owner_decl = mod.declPtr(self.mod_fn.owner_decl);
......@@ -5835,7 +5839,13 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
58355839 .sym_index = decl.link.macho.sym_index,
58365840 } };
58375841 } else if (self.bin_file.cast(link.File.Coff)) |_| {
5838 return self.fail("TODO codegen COFF const Decl pointer", .{});
5842 // Because COFF is PIE-always-on, we defer memory address resolution until
5843 // the linker has enough info to perform relocations.
5844 assert(decl.link.coff.sym_index != 0);
5845 return MCValue{ .linker_load = .{
5846 .@"type" = .got,
5847 .sym_index = decl.link.coff.sym_index,
5848 } };
58395849 } else if (self.bin_file.cast(link.File.Plan9)) |p9| {
58405850 try p9.seeDecl(decl_index);
58415851 const got_addr = p9.bases.data + decl.link.plan9.got_index.? * ptr_bytes;
......@@ -5859,7 +5869,10 @@ fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
58595869 .sym_index = local_sym_index,
58605870 } };
58615871 } else if (self.bin_file.cast(link.File.Coff)) |_| {
5862 return self.fail("TODO lower unnamed const in COFF", .{});
5872 return MCValue{ .linker_load = .{
5873 .@"type" = .direct,
5874 .sym_index = local_sym_index,
5875 } };
58635876 } else if (self.bin_file.cast(link.File.Plan9)) |_| {
58645877 return self.fail("TODO lower unnamed const in Plan9", .{});
58655878 } else {
src/arch/aarch64/Emit.zig+19-15
......@@ -145,6 +145,7 @@ pub fn emitMir(
145145
146146 .load_memory_got => try emit.mirLoadMemoryPie(inst),
147147 .load_memory_direct => try emit.mirLoadMemoryPie(inst),
148 .load_memory_import => try emit.mirLoadMemoryPie(inst),
148149 .load_memory_ptr_got => try emit.mirLoadMemoryPie(inst),
149150 .load_memory_ptr_direct => try emit.mirLoadMemoryPie(inst),
150151
......@@ -693,18 +694,8 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
693694 .pcrel = true,
694695 .length = 2,
695696 });
696 } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
697 // Add relocation to the decl.
698 const atom = coff_file.getAtomForSymbol(.{ .sym_index = relocation.atom_index, .file = null }).?;
699 const target = coff_file.getGlobalByIndex(relocation.sym_index);
700 try atom.addRelocation(coff_file, .{
701 .@"type" = .branch_26,
702 .target = target,
703 .offset = offset,
704 .addend = 0,
705 .pcrel = true,
706 .length = 2,
707 });
697 } else if (emit.bin_file.cast(link.File.Coff)) |_| {
698 unreachable; // Calling imports is handled via `.load_memory_import`
708699 } else {
709700 return emit.fail("Implement call_extern for linking backends != {{ COFF, MachO }}", .{});
710701 }
......@@ -868,7 +859,9 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
868859 try emit.writeInstruction(Instruction.adrp(reg.toX(), 0));
869860
870861 switch (tag) {
871 .load_memory_got => {
862 .load_memory_got,
863 .load_memory_import,
864 => {
872865 // ldr reg, reg, offset
873866 try emit.writeInstruction(Instruction.ldr(
874867 reg,
......@@ -941,8 +934,17 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
941934 });
942935 } else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
943936 const atom = coff_file.getAtomForSymbol(.{ .sym_index = data.atom_index, .file = null }).?;
937 const target = switch (tag) {
938 .load_memory_got,
939 .load_memory_ptr_got,
940 .load_memory_direct,
941 .load_memory_ptr_direct,
942 => link.File.Coff.SymbolWithLoc{ .sym_index = data.sym_index, .file = null },
943 .load_memory_import => coff_file.getGlobalByIndex(data.sym_index),
944 else => unreachable,
945 };
944946 try atom.addRelocation(coff_file, .{
945 .target = .{ .sym_index = data.sym_index, .file = null },
947 .target = target,
946948 .offset = offset,
947949 .addend = 0,
948950 .pcrel = true,
......@@ -954,11 +956,12 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
954956 .load_memory_direct,
955957 .load_memory_ptr_direct,
956958 => .page,
959 .load_memory_import => .import_page,
957960 else => unreachable,
958961 },
959962 });
960963 try atom.addRelocation(coff_file, .{
961 .target = .{ .sym_index = data.sym_index, .file = null },
964 .target = target,
962965 .offset = offset + 4,
963966 .addend = 0,
964967 .pcrel = false,
......@@ -970,6 +973,7 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
970973 .load_memory_direct,
971974 .load_memory_ptr_direct,
972975 => .pageoff,
976 .load_memory_import => .import_pageoff,
973977 else => unreachable,
974978 },
975979 });
src/arch/aarch64/Mir.zig+4
......@@ -84,6 +84,10 @@ pub const Inst = struct {
8484 ///
8585 /// Payload is `LoadMemoryPie`
8686 load_memory_direct,
87 /// Loads the contents into a register
88 ///
89 /// Payload is `LoadMemoryPie`
90 load_memory_import,
8791 /// Loads the address into a register
8892 ///
8993 /// Payload is `LoadMemoryPie`
src/link/Coff.zig+111-81
......@@ -128,18 +128,28 @@ const Entry = struct {
128128pub const Reloc = struct {
129129 @"type": enum {
130130 // x86, x86_64
131 /// RIP-relative displacement to a GOT pointer
131132 got,
133 /// RIP-relative displacement to an import pointer
134 import,
132135
133136 // aarch64
134 branch_26,
137 /// PC-relative distance to target page in GOT section
135138 got_page,
139 /// Offset to a GOT pointer relative to the start of a page in GOT section
136140 got_pageoff,
141 /// PC-relative distance to target page in a section (e.g., .rdata)
137142 page,
143 /// Offset to a pointer relative to the start of a page in a section (e.g., .rdata)
138144 pageoff,
145 /// PC-relative distance to target page in a import section
146 import_page,
147 /// Offset to a pointer relative to the start of a page in an import section (e.g., .rdata)
148 import_pageoff,
139149
140150 // common
141 import,
142 direct, // as unsigned, TODO split into signed for x86
151 /// Absolute pointer value
152 direct,
143153 },
144154 target: SymbolWithLoc,
145155 offset: u32,
......@@ -157,12 +167,14 @@ pub const Reloc = struct {
157167 => return coff_file.getGotAtomForSymbol(self.target),
158168
159169 .direct,
160 .branch_26,
161170 .page,
162171 .pageoff,
163172 => return coff_file.getAtomForSymbol(self.target),
164173
165 .import => return coff_file.getImportAtomForSymbol(self.target),
174 .import,
175 .import_page,
176 .import_pageoff,
177 => return coff_file.getImportAtomForSymbol(self.target),
166178 }
167179 }
168180};
......@@ -172,8 +184,6 @@ const BaseRelocationTable = std.AutoHashMapUnmanaged(*Atom, std.ArrayListUnmanag
172184const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(*Atom));
173185
174186const default_file_alignment: u16 = 0x200;
175const default_image_base_dll: u64 = 0x10000000;
176const default_image_base_exe: u64 = 0x400000;
177187const default_size_of_stack_reserve: u32 = 0x1000000;
178188const default_size_of_stack_commit: u32 = 0x1000;
179189const default_size_of_heap_reserve: u32 = 0x100000;
......@@ -891,6 +901,7 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
891901 const target_atom = reloc.getTargetAtom(self) orelse continue;
892902 const target_vaddr = target_atom.getSymbol(self).value;
893903 const target_vaddr_with_addend = target_vaddr + reloc.addend;
904 const image_base = self.getImageBase();
894905
895906 log.debug(" ({x}: [() => 0x{x} ({s})) ({s}) (in file at 0x{x})", .{
896907 source_vaddr,
......@@ -902,31 +913,23 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
902913
903914 reloc.dirty = false;
904915
905 var buffer: [@sizeOf(u32)]u8 = undefined;
906 switch (reloc.@"type") {
907 .branch_26,
908 .got_page,
909 .got_pageoff,
910 .page,
911 .pageoff,
912 => {
913 const amt = try self.base.file.?.preadAll(&buffer, file_offset + reloc.offset);
914 if (amt != buffer.len) return error.InputOutput;
916 switch (self.base.options.target.cpu.arch) {
917 .aarch64 => {
918 var buffer: [@sizeOf(u64)]u8 = undefined;
919 switch (reloc.length) {
920 2 => {
921 const amt = try self.base.file.?.preadAll(buffer[0..4], file_offset + reloc.offset);
922 if (amt != 4) return error.InputOutput;
923 },
924 3 => {
925 const amt = try self.base.file.?.preadAll(&buffer, file_offset + reloc.offset);
926 if (amt != 8) return error.InputOutput;
927 },
928 else => unreachable,
929 }
915930
916931 switch (reloc.@"type") {
917 .branch_26 => {
918 const displacement = math.cast(i28, @intCast(i64, target_vaddr_with_addend) - @intCast(i64, source_vaddr)) orelse
919 unreachable; // TODO generate thunks
920 var inst = aarch64.Instruction{
921 .unconditional_branch_immediate = mem.bytesToValue(meta.TagPayload(
922 aarch64.Instruction,
923 aarch64.Instruction.unconditional_branch_immediate,
924 ), &buffer),
925 };
926 inst.unconditional_branch_immediate.imm26 = @truncate(u26, @bitCast(u28, displacement >> 2));
927 mem.writeIntLittle(u32, &buffer, inst.toU32());
928 },
929 .got_page, .page => {
932 .got_page, .import_page, .page => {
930933 const source_page = @intCast(i32, source_vaddr >> 12);
931934 const target_page = @intCast(i32, target_vaddr_with_addend >> 12);
932935 const pages = @bitCast(u21, @intCast(i21, target_page - source_page));
......@@ -934,31 +937,31 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
934937 .pc_relative_address = mem.bytesToValue(meta.TagPayload(
935938 aarch64.Instruction,
936939 aarch64.Instruction.pc_relative_address,
937 ), &buffer),
940 ), buffer[0..4]),
938941 };
939942 inst.pc_relative_address.immhi = @truncate(u19, pages >> 2);
940943 inst.pc_relative_address.immlo = @truncate(u2, pages);
941 mem.writeIntLittle(u32, &buffer, inst.toU32());
944 mem.writeIntLittle(u32, buffer[0..4], inst.toU32());
942945 },
943 .got_pageoff, .pageoff => {
946 .got_pageoff, .import_pageoff, .pageoff => {
944947 assert(!reloc.pcrel);
945948
946949 const narrowed = @truncate(u12, @intCast(u64, target_vaddr_with_addend));
947 if (isArithmeticOp(&buffer)) {
950 if (isArithmeticOp(buffer[0..4])) {
948951 var inst = aarch64.Instruction{
949952 .add_subtract_immediate = mem.bytesToValue(meta.TagPayload(
950953 aarch64.Instruction,
951954 aarch64.Instruction.add_subtract_immediate,
952 ), &buffer),
955 ), buffer[0..4]),
953956 };
954957 inst.add_subtract_immediate.imm12 = narrowed;
955 mem.writeIntLittle(u32, &buffer, inst.toU32());
958 mem.writeIntLittle(u32, buffer[0..4], inst.toU32());
956959 } else {
957960 var inst = aarch64.Instruction{
958961 .load_store_register = mem.bytesToValue(meta.TagPayload(
959962 aarch64.Instruction,
960963 aarch64.Instruction.load_store_register,
961 ), &buffer),
964 ), buffer[0..4]),
962965 };
963966 const offset: u12 = blk: {
964967 if (inst.load_store_register.size == 0) {
......@@ -974,55 +977,73 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
974977 }
975978 };
976979 inst.load_store_register.offset = offset;
977 mem.writeIntLittle(u32, &buffer, inst.toU32());
980 mem.writeIntLittle(u32, buffer[0..4], inst.toU32());
981 }
982 },
983 .direct => {
984 assert(!reloc.pcrel);
985 switch (reloc.length) {
986 2 => mem.writeIntLittle(
987 u32,
988 buffer[0..4],
989 @truncate(u32, target_vaddr_with_addend + image_base),
990 ),
991 3 => mem.writeIntLittle(u64, &buffer, target_vaddr_with_addend + image_base),
992 else => unreachable,
978993 }
979994 },
980995
981 else => unreachable,
996 .got => unreachable,
997 .import => unreachable,
982998 }
983999
984 try self.base.file.?.pwriteAll(&buffer, file_offset + reloc.offset);
985
986 return;
1000 switch (reloc.length) {
1001 2 => try self.base.file.?.pwriteAll(buffer[0..4], file_offset + reloc.offset),
1002 3 => try self.base.file.?.pwriteAll(&buffer, file_offset + reloc.offset),
1003 else => unreachable,
1004 }
9871005 },
9881006
989 else => {},
990 }
991
992 switch (reloc.@"type") {
993 .branch_26 => unreachable,
994 .got_page => unreachable,
995 .got_pageoff => unreachable,
996 .page => unreachable,
997 .pageoff => unreachable,
998
999 .got, .import => {
1000 assert(reloc.pcrel);
1001 const disp = @intCast(i32, target_vaddr_with_addend) - @intCast(i32, source_vaddr) - 4;
1002 try self.base.file.?.pwriteAll(mem.asBytes(&disp), file_offset + reloc.offset);
1003 },
1004 .direct => {
1005 if (reloc.pcrel) {
1006 const disp = @intCast(i32, target_vaddr_with_addend) - @intCast(i32, source_vaddr) - 4;
1007 try self.base.file.?.pwriteAll(mem.asBytes(&disp), file_offset + reloc.offset);
1008 } else switch (self.ptr_width) {
1009 .p32 => try self.base.file.?.pwriteAll(
1010 mem.asBytes(&@intCast(u32, target_vaddr_with_addend + default_image_base_exe)),
1011 file_offset + reloc.offset,
1012 ),
1013 .p64 => switch (reloc.length) {
1014 2 => try self.base.file.?.pwriteAll(
1015 mem.asBytes(&@truncate(u32, target_vaddr_with_addend + default_image_base_exe)),
1016 file_offset + reloc.offset,
1017 ),
1018 3 => try self.base.file.?.pwriteAll(
1019 mem.asBytes(&(target_vaddr_with_addend + default_image_base_exe)),
1020 file_offset + reloc.offset,
1021 ),
1022 else => unreachable,
1007 .x86_64, .i386 => {
1008 switch (reloc.@"type") {
1009 .got_page => unreachable,
1010 .got_pageoff => unreachable,
1011 .page => unreachable,
1012 .pageoff => unreachable,
1013 .import_page => unreachable,
1014 .import_pageoff => unreachable,
1015
1016 .got, .import => {
1017 assert(reloc.pcrel);
1018 const disp = @intCast(i32, target_vaddr_with_addend) - @intCast(i32, source_vaddr) - 4;
1019 try self.base.file.?.pwriteAll(mem.asBytes(&disp), file_offset + reloc.offset);
1020 },
1021 .direct => {
1022 if (reloc.pcrel) {
1023 const disp = @intCast(i32, target_vaddr_with_addend) - @intCast(i32, source_vaddr) - 4;
1024 try self.base.file.?.pwriteAll(mem.asBytes(&disp), file_offset + reloc.offset);
1025 } else switch (self.ptr_width) {
1026 .p32 => try self.base.file.?.pwriteAll(
1027 mem.asBytes(&@intCast(u32, target_vaddr_with_addend + image_base)),
1028 file_offset + reloc.offset,
1029 ),
1030 .p64 => switch (reloc.length) {
1031 2 => try self.base.file.?.pwriteAll(
1032 mem.asBytes(&@truncate(u32, target_vaddr_with_addend + image_base)),
1033 file_offset + reloc.offset,
1034 ),
1035 3 => try self.base.file.?.pwriteAll(
1036 mem.asBytes(&(target_vaddr_with_addend + image_base)),
1037 file_offset + reloc.offset,
1038 ),
1039 else => unreachable,
1040 },
1041 }
10231042 },
10241043 }
10251044 },
1045
1046 else => unreachable, // unhandled target architecture
10261047 }
10271048 }
10281049}
......@@ -1950,11 +1971,7 @@ fn writeHeader(self: *Coff) !void {
19501971 const subsystem: coff.Subsystem = .WINDOWS_CUI;
19511972 const size_of_image: u32 = self.getSizeOfImage();
19521973 const size_of_headers: u32 = mem.alignForwardGeneric(u32, self.getSizeOfHeaders(), default_file_alignment);
1953 const image_base = self.base.options.image_base_override orelse switch (self.base.options.output_mode) {
1954 .Exe => default_image_base_exe,
1955 .Lib => default_image_base_dll,
1956 else => unreachable,
1957 };
1974 const image_base = self.getImageBase();
19581975
19591976 const base_of_code = self.sections.get(self.text_section_index.?).header.virtual_address;
19601977 const base_of_data = self.sections.get(self.data_section_index.?).header.virtual_address;
......@@ -2161,6 +2178,19 @@ pub fn getEntryPoint(self: Coff) ?SymbolWithLoc {
21612178 return self.globals.items[global_index];
21622179}
21632180
2181pub fn getImageBase(self: Coff) u64 {
2182 const image_base: u64 = self.base.options.image_base_override orelse switch (self.base.options.output_mode) {
2183 .Exe => switch (self.base.options.target.cpu.arch) {
2184 .aarch64 => 0x140000000,
2185 .x86_64, .i386 => 0x400000,
2186 else => unreachable, // unsupported target architecture
2187 },
2188 .Lib => 0x10000000,
2189 else => unreachable,
2190 };
2191 return image_base;
2192}
2193
21642194/// Returns pointer-to-symbol described by `sym_loc` descriptor.
21652195pub fn getSymbolPtr(self: *Coff, sym_loc: SymbolWithLoc) *coff.Symbol {
21662196 assert(sym_loc.file == null); // TODO linking object files