authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-30 21:24:49+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-30 21:24:49+02:00
log908ccce064a898d5db1d43dbdc4a3590fd84d4ba
tree528a13922ced3d06e01a9a236eb63746dcc5b4ac
parentee0c4457657523e218c1e211c447d3e196575ddc

coff: enable hot-code swapping on a compatible host only


1 files changed, 31 insertions(+), 20 deletions(-)

src/link/Coff.zig+31-20
......@@ -90,7 +90,12 @@ relocs: RelocTable = .{},
9090base_relocs: BaseRelocationTable = .{},
9191
9292/// Hot-code swapping state.
93hot_state: HotUpdateState = .{},
93hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{},
94
95const is_hot_update_compatible = switch (builtin.target.os.tag) {
96 .windows => true,
97 else => false,
98};
9499
95100const HotUpdateState = struct {
96101 /// Base address at which the process (image) got loaded.
......@@ -805,30 +810,32 @@ fn writeAtom(self: *Coff, atom_index: Atom.Index, code: []u8) !void {
805810 }
806811 }
807812
808 if (self.base.child_pid) |handle| {
809 const slide = @ptrToInt(self.hot_state.loaded_base_address.?);
813 if (is_hot_update_compatible) {
814 if (self.base.child_pid) |handle| {
815 const slide = @ptrToInt(self.hot_state.loaded_base_address.?);
810816
811 const mem_code = try gpa.dupe(u8, code);
812 defer gpa.free(mem_code);
813 self.resolveRelocs(atom_index, relocs.items, mem_code, slide);
817 const mem_code = try gpa.dupe(u8, code);
818 defer gpa.free(mem_code);
819 self.resolveRelocs(atom_index, relocs.items, mem_code, slide);
814820
815 const vaddr = sym.value + slide;
816 const pvaddr = @intToPtr(*anyopaque, vaddr);
821 const vaddr = sym.value + slide;
822 const pvaddr = @intToPtr(*anyopaque, vaddr);
817823
818 log.debug("writing to memory at address {x}", .{vaddr});
824 log.debug("writing to memory at address {x}", .{vaddr});
819825
820 if (build_options.enable_logging) {
821 try debugMem(gpa, handle, pvaddr, mem_code);
822 }
826 if (build_options.enable_logging) {
827 try debugMem(gpa, handle, pvaddr, mem_code);
828 }
823829
824 if (section.header.flags.MEM_WRITE == 0) {
825 writeMemProtected(handle, pvaddr, mem_code) catch |err| {
826 log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
827 };
828 } else {
829 writeMem(handle, pvaddr, mem_code) catch |err| {
830 log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
831 };
830 if (section.header.flags.MEM_WRITE == 0) {
831 writeMemProtected(handle, pvaddr, mem_code) catch |err| {
832 log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
833 };
834 } else {
835 writeMem(handle, pvaddr, mem_code) catch |err| {
836 log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)});
837 };
838 }
832839 }
833840 }
834841
......@@ -903,6 +910,8 @@ fn resolveRelocs(self: *Coff, atom_index: Atom.Index, relocs: []*const Relocatio
903910}
904911
905912pub fn ptraceAttach(self: *Coff, handle: std.ChildProcess.Id) !void {
913 if (!is_hot_update_compatible) return;
914
906915 log.debug("attaching to process with handle {*}", .{handle});
907916 self.hot_state.loaded_base_address = std.os.windows.ProcessBaseAddress(handle) catch |err| {
908917 log.warn("failed to get base address for the process with error: {s}", .{@errorName(err)});
......@@ -911,6 +920,8 @@ pub fn ptraceAttach(self: *Coff, handle: std.ChildProcess.Id) !void {
911920}
912921
913922pub fn ptraceDetach(self: *Coff, handle: std.ChildProcess.Id) void {
923 if (!is_hot_update_compatible) return;
924
914925 log.debug("detaching from process with handle {*}", .{handle});
915926 self.hot_state.loaded_base_address = null;
916927}