| ... | @@ -48,8 +48,8 @@ pub const DeclBlock = struct { | ... | @@ -48,8 +48,8 @@ pub const DeclBlock = struct { |
| 48 | }; | 48 | }; |
| 49 | }; | 49 | }; |
| 50 | | 50 | |
| 51 | // TODO change base addr based on target (right now it just works on amd64) | 51 | // TODO change base addr based on target (and section?) (right now it just works on amd64) |
| 52 | const default_base_addr = 0x00200000; | 52 | const default_base_addr = 0x00200028; |
| 53 | | 53 | |
| 54 | pub const CallReloc = struct { | 54 | pub const CallReloc = struct { |
| 55 | caller: *Module.Decl, | 55 | caller: *Module.Decl, |
| ... | @@ -157,11 +157,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { | ... | @@ -157,11 +157,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { |
| 157 | return; | 157 | return; |
| 158 | }, | 158 | }, |
| 159 | }; | 159 | }; |
| 160 | if (is_fn) | 160 | if (is_fn) { |
| 161 | try self.text_buf.appendSlice(self.base.allocator, code) | 161 | try self.text_buf.appendSlice(self.base.allocator, code); |
| 162 | else | 162 | try code_buffer.resize(0); |
| | 163 | } else { |
| 163 | try self.data_buf.appendSlice(self.base.allocator, code); | 164 | try self.data_buf.appendSlice(self.base.allocator, code); |
| 164 | code_buffer.items.len = 0; | 165 | try code_buffer.resize(0); |
| | 166 | } |
| 165 | } | 167 | } |
| 166 | } | 168 | } |
| 167 | | 169 | |
| ... | @@ -176,7 +178,6 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { | ... | @@ -176,7 +178,6 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { |
| 176 | const off = reloc.offset_in_caller + l.offset; | 178 | const off = reloc.offset_in_caller + l.offset; |
| 177 | std.mem.writeInt(u32, self.text_buf.items[off - 4 ..][0..4], callee_offset, endian); | 179 | std.mem.writeInt(u32, self.text_buf.items[off - 4 ..][0..4], callee_offset, endian); |
| 178 | } else { | 180 | } else { |
| 179 | // what we are writing | | |
| 180 | const callee_offset = reloc.callee.link.plan9.offset + default_base_addr; // TODO this is different if its data | 181 | const callee_offset = reloc.callee.link.plan9.offset + default_base_addr; // TODO this is different if its data |
| 181 | const off = reloc.offset_in_caller + l.offset; | 182 | const off = reloc.offset_in_caller + l.offset; |
| 182 | std.mem.writeInt(u64, self.text_buf.items[off - 8 ..][0..8], callee_offset, endian); | 183 | std.mem.writeInt(u64, self.text_buf.items[off - 8 ..][0..8], callee_offset, endian); |
| ... | @@ -184,6 +185,11 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { | ... | @@ -184,6 +185,11 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { |
| 184 | } | 185 | } |
| 185 | } | 186 | } |
| 186 | | 187 | |
| | 188 | // edata, end, etext |
| | 189 | self.syms.items[0].value = 0x200000; // TODO make this number other place, and what is it? |
| | 190 | self.syms.items[1].value = 0x200000; // TODO make this number other place, and what is it? |
| | 191 | self.syms.items[2].value = self.text_buf.items.len; |
| | 192 | |
| 187 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); | 193 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); |
| 188 | defer sym_buf.deinit(); | 194 | defer sym_buf.deinit(); |
| 189 | try self.writeSyms(&sym_buf); | 195 | try self.writeSyms(&sym_buf); |
| ... | @@ -202,10 +208,14 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { | ... | @@ -202,10 +208,14 @@ pub fn flushModule(self: *Plan9, comp: *Compilation) !void { |
| 202 | | 208 | |
| 203 | const file = self.base.file.?; | 209 | const file = self.base.file.?; |
| 204 | | 210 | |
| 205 | const hdr_buf = self.hdr.toU8s(); | 211 | var hdr_buf = self.hdr.toU8s(); |
| 206 | const hdr_slice: []const u8 = &hdr_buf; | 212 | const hdr_slice: []const u8 = &hdr_buf; |
| 207 | // account for the fat header | 213 | // account for the fat header |
| 208 | const hdr_size: u8 = if (self.ptr_width == .p32) 32 else 40; | 214 | const hdr_size: u8 = if (self.ptr_width == .p32) 32 else 40; |
| | 215 | // write the fat header for debug info |
| | 216 | if (self.ptr_width == .p64) { |
| | 217 | mem.writeIntSliceBig(u64, hdr_buf[32..40], self.hdr.entry); |
| | 218 | } |
| 209 | // write it all! | 219 | // write it all! |
| 210 | var vectors: [4]std.os.iovec_const = .{ | 220 | var vectors: [4]std.os.iovec_const = .{ |
| 211 | .{ .iov_base = hdr_slice.ptr, .iov_len = hdr_size }, | 221 | .{ .iov_base = hdr_slice.ptr, .iov_len = hdr_size }, |
| ... | @@ -290,6 +300,25 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio | ... | @@ -290,6 +300,25 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 290 | if (std.builtin.mode == .Debug or std.builtin.mode == .ReleaseSafe) | 300 | if (std.builtin.mode == .Debug or std.builtin.mode == .ReleaseSafe) |
| 291 | self.hdr.entry = 0x0; | 301 | self.hdr.entry = 0x0; |
| 292 | | 302 | |
| | 303 | // first 3 symbols in our table are edata, end, etext |
| | 304 | try self.syms.appendSlice(self.base.allocator, &.{ |
| | 305 | .{ |
| | 306 | .value = 0xcafebabe, |
| | 307 | .type = .B, |
| | 308 | .name = "edata", |
| | 309 | }, |
| | 310 | .{ |
| | 311 | .value = 0xcafebabe, |
| | 312 | .type = .B, |
| | 313 | .name = "end", |
| | 314 | }, |
| | 315 | .{ |
| | 316 | .value = 0xcafebabe, |
| | 317 | .type = .T, |
| | 318 | .name = "etext", |
| | 319 | }, |
| | 320 | }); |
| | 321 | |
| 293 | self.base.file = file; | 322 | self.base.file = file; |
| 294 | return self; | 323 | return self; |
| 295 | } | 324 | } |