| ... | @@ -79,7 +79,9 @@ data_decl_table: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, []u8) = .{}, | ... | @@ -79,7 +79,9 @@ data_decl_table: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, []u8) = .{}, |
| 79 | /// with `Decl` `main`, and lives as long as that `Decl`. | 79 | /// with `Decl` `main`, and lives as long as that `Decl`. |
| 80 | unnamed_const_atoms: UnnamedConstTable = .{}, | 80 | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 81 | | 81 | |
| 82 | relocs: std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Reloc)) = .{}, | 82 | lazy_syms: LazySymbolTable = .{}, |
| | 83 | |
| | 84 | relocs: std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Reloc)) = .{}, |
| 83 | hdr: aout.ExecHdr = undefined, | 85 | hdr: aout.ExecHdr = undefined, |
| 84 | | 86 | |
| 85 | // relocs: std. | 87 | // relocs: std. |
| ... | @@ -94,13 +96,14 @@ got_index_free_list: std.ArrayListUnmanaged(usize) = .{}, | ... | @@ -94,13 +96,14 @@ got_index_free_list: std.ArrayListUnmanaged(usize) = .{}, |
| 94 | | 96 | |
| 95 | syms_index_free_list: std.ArrayListUnmanaged(usize) = .{}, | 97 | syms_index_free_list: std.ArrayListUnmanaged(usize) = .{}, |
| 96 | | 98 | |
| 97 | decl_blocks: std.ArrayListUnmanaged(DeclBlock) = .{}, | 99 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 98 | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, | 100 | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 99 | | 101 | |
| 100 | const Reloc = struct { | 102 | const Reloc = struct { |
| 101 | target: Module.Decl.Index, | 103 | target: Atom.Index, |
| 102 | offset: u64, | 104 | offset: u64, |
| 103 | addend: u32, | 105 | addend: u32, |
| | 106 | pcrel: bool = false, |
| 104 | }; | 107 | }; |
| 105 | | 108 | |
| 106 | const Bases = struct { | 109 | const Bases = struct { |
| ... | @@ -109,11 +112,28 @@ const Bases = struct { | ... | @@ -109,11 +112,28 @@ const Bases = struct { |
| 109 | data: u64, | 112 | data: u64, |
| 110 | }; | 113 | }; |
| 111 | | 114 | |
| 112 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(struct { info: DeclBlock, code: []const u8 })); | 115 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index)); |
| | 116 | |
| | 117 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); |
| | 118 | |
| | 119 | const LazySymbolMetadata = struct { |
| | 120 | const State = enum { unused, pending_flush, flushed }; |
| | 121 | text_atom: Atom.Index = undefined, |
| | 122 | rodata_atom: Atom.Index = undefined, |
| | 123 | text_state: State = .unused, |
| | 124 | rodata_state: State = .unused, |
| | 125 | |
| | 126 | fn numberOfAtoms(self: LazySymbolMetadata) u32 { |
| | 127 | var n: u32 = 0; |
| | 128 | if (self.text_state != .unused) n += 1; |
| | 129 | if (self.rodata_state != .unused) n += 1; |
| | 130 | return n; |
| | 131 | } |
| | 132 | }; |
| 113 | | 133 | |
| 114 | pub const PtrWidth = enum { p32, p64 }; | 134 | pub const PtrWidth = enum { p32, p64 }; |
| 115 | | 135 | |
| 116 | pub const DeclBlock = struct { | 136 | pub const Atom = struct { |
| 117 | type: aout.Sym.Type, | 137 | type: aout.Sym.Type, |
| 118 | /// offset in the text or data sects | 138 | /// offset in the text or data sects |
| 119 | offset: ?u64, | 139 | offset: ?u64, |
| ... | @@ -121,12 +141,60 @@ pub const DeclBlock = struct { | ... | @@ -121,12 +141,60 @@ pub const DeclBlock = struct { |
| 121 | sym_index: ?usize, | 141 | sym_index: ?usize, |
| 122 | /// offset into got | 142 | /// offset into got |
| 123 | got_index: ?usize, | 143 | got_index: ?usize, |
| | 144 | /// We include the code here to be use in relocs |
| | 145 | /// In the case of unnamed_const_atoms and lazy_syms, this atom owns the code. |
| | 146 | /// But, in the case of function and data decls, they own the code and this field |
| | 147 | /// is just a pointer for convience. |
| | 148 | code: CodePtr, |
| | 149 | |
| | 150 | const CodePtr = struct { |
| | 151 | code_ptr: ?[*]u8, |
| | 152 | other: union { |
| | 153 | code_len: usize, |
| | 154 | decl_index: Module.Decl.Index, |
| | 155 | }, |
| | 156 | fn getCode(self: CodePtr, plan9: *const Plan9) []u8 { |
| | 157 | const mod = plan9.base.options.module.?; |
| | 158 | return if (self.code_ptr) |p| p[0..self.other.code_len] else blk: { |
| | 159 | const decl_index = self.other.decl_index; |
| | 160 | const decl = mod.declPtr(decl_index); |
| | 161 | if (decl.ty.zigTypeTag(mod) == .Fn) { |
| | 162 | const table = plan9.fn_decl_table.get(decl.getFileScope(mod)).?.functions; |
| | 163 | const output = table.get(decl_index).?; |
| | 164 | break :blk output.code; |
| | 165 | } else { |
| | 166 | break :blk plan9.data_decl_table.get(decl_index).?; |
| | 167 | } |
| | 168 | }; |
| | 169 | } |
| | 170 | fn getOwnedCode(self: CodePtr) ?[]u8 { |
| | 171 | return if (self.code_ptr) |p| p[0..self.other.code_len] else null; |
| | 172 | } |
| | 173 | }; |
| 124 | | 174 | |
| 125 | pub const Index = u32; | 175 | pub const Index = u32; |
| | 176 | |
| | 177 | pub fn getOrCreateOffsetTableEntry(self: *Atom, plan9: *Plan9) usize { |
| | 178 | if (self.got_index == null) self.got_index = plan9.allocateGotIndex(); |
| | 179 | return self.got_index.?; |
| | 180 | } |
| | 181 | |
| | 182 | pub fn getOrCreateSymbolTableEntry(self: *Atom, plan9: *Plan9) !usize { |
| | 183 | if (self.sym_index == null) self.sym_index = try plan9.allocateSymbolIndex(); |
| | 184 | return self.sym_index.?; |
| | 185 | } |
| | 186 | |
| | 187 | // asserts that self.got_index != null |
| | 188 | pub fn getOffsetTableAddress(self: Atom, plan9: *Plan9) u64 { |
| | 189 | const ptr_bytes = @divExact(plan9.base.options.target.ptrBitWidth(), 8); |
| | 190 | const got_addr = plan9.bases.data; |
| | 191 | const got_index = self.got_index.?; |
| | 192 | return got_addr + got_index * ptr_bytes; |
| | 193 | } |
| 126 | }; | 194 | }; |
| 127 | | 195 | |
| 128 | const DeclMetadata = struct { | 196 | const DeclMetadata = struct { |
| 129 | index: DeclBlock.Index, | 197 | index: Atom.Index, |
| 130 | exports: std.ArrayListUnmanaged(usize) = .{}, | 198 | exports: std.ArrayListUnmanaged(usize) = .{}, |
| 131 | | 199 | |
| 132 | fn getExport(m: DeclMetadata, p9: *const Plan9, name: []const u8) ?usize { | 200 | fn getExport(m: DeclMetadata, p9: *const Plan9, name: []const u8) ?usize { |
| ... | @@ -286,7 +354,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: Module.Fn.Index, air: | ... | @@ -286,7 +354,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: Module.Fn.Index, air: |
| 286 | const decl = mod.declPtr(decl_index); | 354 | const decl = mod.declPtr(decl_index); |
| 287 | self.freeUnnamedConsts(decl_index); | 355 | self.freeUnnamedConsts(decl_index); |
| 288 | | 356 | |
| 289 | _ = try self.seeDecl(decl_index); | 357 | const atom_idx = try self.seeDecl(decl_index); |
| 290 | | 358 | |
| 291 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); | 359 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 292 | defer code_buffer.deinit(); | 360 | defer code_buffer.deinit(); |
| ... | @@ -320,6 +388,10 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: Module.Fn.Index, air: | ... | @@ -320,6 +388,10 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: Module.Fn.Index, air: |
| 320 | return; | 388 | return; |
| 321 | }, | 389 | }, |
| 322 | }; | 390 | }; |
| | 391 | self.getAtomPtr(atom_idx).code = .{ |
| | 392 | .code_ptr = null, |
| | 393 | .other = .{ .decl_index = decl_index }, |
| | 394 | }; |
| 323 | const out: FnDeclOutput = .{ | 395 | const out: FnDeclOutput = .{ |
| 324 | .code = code, | 396 | .code = code, |
| 325 | .lineinfo = try dbg_line_buffer.toOwnedSlice(), | 397 | .lineinfo = try dbg_line_buffer.toOwnedSlice(), |
| ... | @@ -351,12 +423,13 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I | ... | @@ -351,12 +423,13 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 351 | const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index }); | 423 | const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 352 | | 424 | |
| 353 | const sym_index = try self.allocateSymbolIndex(); | 425 | const sym_index = try self.allocateSymbolIndex(); |
| 354 | | 426 | const new_atom_idx = try self.createAtom(); |
| 355 | const info: DeclBlock = .{ | 427 | var info: Atom = .{ |
| 356 | .type = .d, | 428 | .type = .d, |
| 357 | .offset = null, | 429 | .offset = null, |
| 358 | .sym_index = sym_index, | 430 | .sym_index = sym_index, |
| 359 | .got_index = self.allocateGotIndex(), | 431 | .got_index = self.allocateGotIndex(), |
| | 432 | .code = undefined, // filled in later |
| 360 | }; | 433 | }; |
| 361 | const sym: aout.Sym = .{ | 434 | const sym: aout.Sym = .{ |
| 362 | .value = undefined, | 435 | .value = undefined, |
| ... | @@ -368,7 +441,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I | ... | @@ -368,7 +441,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 368 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), tv, &code_buffer, .{ | 441 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), tv, &code_buffer, .{ |
| 369 | .none = {}, | 442 | .none = {}, |
| 370 | }, .{ | 443 | }, .{ |
| 371 | .parent_atom_index = @enumToInt(decl_index), | 444 | .parent_atom_index = new_atom_idx, |
| 372 | }); | 445 | }); |
| 373 | const code = switch (res) { | 446 | const code = switch (res) { |
| 374 | .ok => code_buffer.items, | 447 | .ok => code_buffer.items, |
| ... | @@ -382,9 +455,12 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I | ... | @@ -382,9 +455,12 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 382 | // duped_code is freed when the unnamed const is freed | 455 | // duped_code is freed when the unnamed const is freed |
| 383 | var duped_code = try self.base.allocator.dupe(u8, code); | 456 | var duped_code = try self.base.allocator.dupe(u8, code); |
| 384 | errdefer self.base.allocator.free(duped_code); | 457 | errdefer self.base.allocator.free(duped_code); |
| 385 | try unnamed_consts.append(self.base.allocator, .{ .info = info, .code = duped_code }); | 458 | const new_atom = self.getAtomPtr(new_atom_idx); |
| 386 | // we return the got_index to codegen so that it can reference to the place of the data in the got | 459 | new_atom.* = info; |
| 387 | return @intCast(u32, info.got_index.?); | 460 | new_atom.code = .{ .code_ptr = duped_code.ptr, .other = .{ .code_len = duped_code.len } }; |
| | 461 | try unnamed_consts.append(self.base.allocator, new_atom_idx); |
| | 462 | // we return the new_atom_idx to codegen |
| | 463 | return new_atom_idx; |
| 388 | } | 464 | } |
| 389 | | 465 | |
| 390 | pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void { | 466 | pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void { |
| ... | @@ -399,7 +475,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo | ... | @@ -399,7 +475,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo |
| 399 | } | 475 | } |
| 400 | } | 476 | } |
| 401 | | 477 | |
| 402 | _ = try self.seeDecl(decl_index); | 478 | const atom_idx = try self.seeDecl(decl_index); |
| 403 | | 479 | |
| 404 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); | 480 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 405 | defer code_buffer.deinit(); | 481 | defer code_buffer.deinit(); |
| ... | @@ -409,7 +485,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo | ... | @@ -409,7 +485,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo |
| 409 | .ty = decl.ty, | 485 | .ty = decl.ty, |
| 410 | .val = decl_val, | 486 | .val = decl_val, |
| 411 | }, &code_buffer, .{ .none = {} }, .{ | 487 | }, &code_buffer, .{ .none = {} }, .{ |
| 412 | .parent_atom_index = @enumToInt(decl_index), | 488 | .parent_atom_index = @intCast(Atom.Index, atom_idx), |
| 413 | }); | 489 | }); |
| 414 | const code = switch (res) { | 490 | const code = switch (res) { |
| 415 | .ok => code_buffer.items, | 491 | .ok => code_buffer.items, |
| ... | @@ -421,6 +497,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo | ... | @@ -421,6 +497,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo |
| 421 | }; | 497 | }; |
| 422 | try self.data_decl_table.ensureUnusedCapacity(self.base.allocator, 1); | 498 | try self.data_decl_table.ensureUnusedCapacity(self.base.allocator, 1); |
| 423 | const duped_code = try self.base.allocator.dupe(u8, code); | 499 | const duped_code = try self.base.allocator.dupe(u8, code); |
| | 500 | self.getAtomPtr(self.decls.get(decl_index).?.index).code = .{ .code_ptr = null, .other = .{ .decl_index = decl_index } }; |
| 424 | if (self.data_decl_table.fetchPutAssumeCapacity(decl_index, duped_code)) |old_entry| { | 501 | if (self.data_decl_table.fetchPutAssumeCapacity(decl_index, duped_code)) |old_entry| { |
| 425 | self.base.allocator.free(old_entry.value); | 502 | self.base.allocator.free(old_entry.value); |
| 426 | } | 503 | } |
| ... | @@ -433,22 +510,22 @@ fn updateFinish(self: *Plan9, decl_index: Module.Decl.Index) !void { | ... | @@ -433,22 +510,22 @@ fn updateFinish(self: *Plan9, decl_index: Module.Decl.Index) !void { |
| 433 | const is_fn = (decl.ty.zigTypeTag(mod) == .Fn); | 510 | const is_fn = (decl.ty.zigTypeTag(mod) == .Fn); |
| 434 | const sym_t: aout.Sym.Type = if (is_fn) .t else .d; | 511 | const sym_t: aout.Sym.Type = if (is_fn) .t else .d; |
| 435 | | 512 | |
| 436 | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); | 513 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 437 | // write the internal linker metadata | 514 | // write the internal linker metadata |
| 438 | decl_block.type = sym_t; | 515 | atom.type = sym_t; |
| 439 | // write the symbol | 516 | // write the symbol |
| 440 | // we already have the got index | 517 | // we already have the got index |
| 441 | const sym: aout.Sym = .{ | 518 | const sym: aout.Sym = .{ |
| 442 | .value = undefined, // the value of stuff gets filled in in flushModule | 519 | .value = undefined, // the value of stuff gets filled in in flushModule |
| 443 | .type = decl_block.type, | 520 | .type = atom.type, |
| 444 | .name = try self.base.allocator.dupe(u8, mod.intern_pool.stringToSlice(decl.name)), | 521 | .name = try self.base.allocator.dupe(u8, mod.intern_pool.stringToSlice(decl.name)), |
| 445 | }; | 522 | }; |
| 446 | | 523 | |
| 447 | if (decl_block.sym_index) |s| { | 524 | if (atom.sym_index) |s| { |
| 448 | self.syms.items[s] = sym; | 525 | self.syms.items[s] = sym; |
| 449 | } else { | 526 | } else { |
| 450 | const s = try self.allocateSymbolIndex(); | 527 | const s = try self.allocateSymbolIndex(); |
| 451 | decl_block.sym_index = s; | 528 | atom.sym_index = s; |
| 452 | self.syms.items[s] = sym; | 529 | self.syms.items[s] = sym; |
| 453 | } | 530 | } |
| 454 | } | 531 | } |
| ... | @@ -461,6 +538,7 @@ fn allocateSymbolIndex(self: *Plan9) !usize { | ... | @@ -461,6 +538,7 @@ fn allocateSymbolIndex(self: *Plan9) !usize { |
| 461 | return self.syms.items.len - 1; | 538 | return self.syms.items.len - 1; |
| 462 | } | 539 | } |
| 463 | } | 540 | } |
| | 541 | |
| 464 | fn allocateGotIndex(self: *Plan9) usize { | 542 | fn allocateGotIndex(self: *Plan9) usize { |
| 465 | if (self.got_index_free_list.popOrNull()) |i| { | 543 | if (self.got_index_free_list.popOrNull()) |i| { |
| 466 | return i; | 544 | return i; |
| ... | @@ -495,7 +573,7 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { | ... | @@ -495,7 +573,7 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { |
| 495 | } | 573 | } |
| 496 | } | 574 | } |
| 497 | | 575 | |
| 498 | // counts decls and unnamed consts | 576 | // counts decls, unnamed consts, and lazy syms |
| 499 | fn atomCount(self: *Plan9) usize { | 577 | fn atomCount(self: *Plan9) usize { |
| 500 | var fn_decl_count: usize = 0; | 578 | var fn_decl_count: usize = 0; |
| 501 | var itf_files = self.fn_decl_table.iterator(); | 579 | var itf_files = self.fn_decl_table.iterator(); |
| ... | @@ -510,7 +588,12 @@ fn atomCount(self: *Plan9) usize { | ... | @@ -510,7 +588,12 @@ fn atomCount(self: *Plan9) usize { |
| 510 | while (it_unc.next()) |unnamed_consts| { | 588 | while (it_unc.next()) |unnamed_consts| { |
| 511 | unnamed_const_count += unnamed_consts.value_ptr.items.len; | 589 | unnamed_const_count += unnamed_consts.value_ptr.items.len; |
| 512 | } | 590 | } |
| 513 | return data_decl_count + fn_decl_count + unnamed_const_count; | 591 | var lazy_atom_count: usize = 0; |
| | 592 | var it_lazy = self.lazy_syms.iterator(); |
| | 593 | while (it_lazy.next()) |kv| { |
| | 594 | lazy_atom_count += kv.value_ptr.numberOfAtoms(); |
| | 595 | } |
| | 596 | return data_decl_count + fn_decl_count + unnamed_const_count + lazy_atom_count; |
| 514 | } | 597 | } |
| 515 | | 598 | |
| 516 | pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.Node) link.File.FlushError!void { | 599 | pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.Node) link.File.FlushError!void { |
| ... | @@ -532,7 +615,32 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -532,7 +615,32 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 532 | | 615 | |
| 533 | const mod = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; | 616 | const mod = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 534 | | 617 | |
| 535 | assert(self.got_len == self.atomCount() + self.got_index_free_list.items.len); | 618 | // finish up the lazy syms |
| | 619 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| | 620 | // Most lazy symbols can be updated on first use, but |
| | 621 | // anyerror needs to wait for everything to be flushed. |
| | 622 | if (metadata.text_state != .unused) self.updateLazySymbolAtom( |
| | 623 | File.LazySymbol.initDecl(.code, null, mod), |
| | 624 | metadata.text_atom, |
| | 625 | ) catch |err| return switch (err) { |
| | 626 | error.CodegenFail => error.FlushFailure, |
| | 627 | else => |e| e, |
| | 628 | }; |
| | 629 | if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( |
| | 630 | File.LazySymbol.initDecl(.const_data, null, mod), |
| | 631 | metadata.rodata_atom, |
| | 632 | ) catch |err| return switch (err) { |
| | 633 | error.CodegenFail => error.FlushFailure, |
| | 634 | else => |e| e, |
| | 635 | }; |
| | 636 | } |
| | 637 | for (self.lazy_syms.values()) |*metadata| { |
| | 638 | if (metadata.text_state != .unused) metadata.text_state = .flushed; |
| | 639 | if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed; |
| | 640 | } |
| | 641 | // make sure the got table is good |
| | 642 | const atom_count = self.atomCount(); |
| | 643 | assert(self.got_len == atom_count + self.got_index_free_list.items.len); |
| 536 | const got_size = self.got_len * if (!self.sixtyfour_bit) @as(u32, 4) else 8; | 644 | const got_size = self.got_len * if (!self.sixtyfour_bit) @as(u32, 4) else 8; |
| 537 | var got_table = try self.base.allocator.alloc(u8, got_size); | 645 | var got_table = try self.base.allocator.alloc(u8, got_size); |
| 538 | defer self.base.allocator.free(got_table); | 646 | defer self.base.allocator.free(got_table); |
| ... | @@ -562,7 +670,8 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -562,7 +670,8 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 562 | var it = fentry.value_ptr.functions.iterator(); | 670 | var it = fentry.value_ptr.functions.iterator(); |
| 563 | while (it.next()) |entry| { | 671 | while (it.next()) |entry| { |
| 564 | const decl_index = entry.key_ptr.*; | 672 | const decl_index = entry.key_ptr.*; |
| 565 | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); | 673 | const decl = mod.declPtr(decl_index); |
| | 674 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 566 | const out = entry.value_ptr.*; | 675 | const out = entry.value_ptr.*; |
| 567 | { | 676 | { |
| 568 | // connect the previous decl to the next | 677 | // connect the previous decl to the next |
| ... | @@ -580,14 +689,14 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -580,14 +689,14 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 580 | iovecs_i += 1; | 689 | iovecs_i += 1; |
| 581 | const off = self.getAddr(text_i, .t); | 690 | const off = self.getAddr(text_i, .t); |
| 582 | text_i += out.code.len; | 691 | text_i += out.code.len; |
| 583 | decl_block.offset = off; | 692 | atom.offset = off; |
| | 693 | log.debug("write text decl {*} ({}), lines {d} to {d}.;__GOT+0x{x} vaddr: 0x{x}", .{ decl, decl.name.fmt(&mod.intern_pool), out.start_line + 1, out.end_line, atom.got_index.? * 8, off }); |
| 584 | if (!self.sixtyfour_bit) { | 694 | if (!self.sixtyfour_bit) { |
| 585 | mem.writeIntNative(u32, got_table[decl_block.got_index.? * 4 ..][0..4], @intCast(u32, off)); | 695 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 586 | mem.writeInt(u32, got_table[decl_block.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); | | |
| 587 | } else { | 696 | } else { |
| 588 | mem.writeInt(u64, got_table[decl_block.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); | 697 | mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 589 | } | 698 | } |
| 590 | self.syms.items[decl_block.sym_index.?].value = off; | 699 | self.syms.items[atom.sym_index.?].value = off; |
| 591 | if (mod.decl_exports.get(decl_index)) |exports| { | 700 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 592 | try self.addDeclExports(mod, decl_index, exports.items); | 701 | try self.addDeclExports(mod, decl_index, exports.items); |
| 593 | } | 702 | } |
| ... | @@ -597,9 +706,30 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -597,9 +706,30 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 597 | // just a nop to make it even, the plan9 linker does this | 706 | // just a nop to make it even, the plan9 linker does this |
| 598 | try linecountinfo.append(129); | 707 | try linecountinfo.append(129); |
| 599 | } | 708 | } |
| 600 | // etext symbol | | |
| 601 | self.syms.items[2].value = self.getAddr(text_i, .t); | | |
| 602 | } | 709 | } |
| | 710 | // the text lazy symbols |
| | 711 | { |
| | 712 | var it = self.lazy_syms.iterator(); |
| | 713 | while (it.next()) |kv| { |
| | 714 | const meta = kv.value_ptr; |
| | 715 | const text_atom = if (meta.text_state != .unused) self.getAtomPtr(meta.text_atom) else continue; |
| | 716 | const code = text_atom.code.getOwnedCode().?; |
| | 717 | foff += code.len; |
| | 718 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| | 719 | iovecs_i += 1; |
| | 720 | const off = self.getAddr(text_i, .t); |
| | 721 | text_i += code.len; |
| | 722 | text_atom.offset = off; |
| | 723 | if (!self.sixtyfour_bit) { |
| | 724 | mem.writeInt(u32, got_table[text_atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| | 725 | } else { |
| | 726 | mem.writeInt(u64, got_table[text_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| | 727 | } |
| | 728 | self.syms.items[text_atom.sym_index.?].value = off; |
| | 729 | } |
| | 730 | } |
| | 731 | // etext symbol |
| | 732 | self.syms.items[2].value = self.getAddr(text_i, .t); |
| 603 | // global offset table is in data | 733 | // global offset table is in data |
| 604 | iovecs[iovecs_i] = .{ .iov_base = got_table.ptr, .iov_len = got_table.len }; | 734 | iovecs[iovecs_i] = .{ .iov_base = got_table.ptr, .iov_len = got_table.len }; |
| 605 | iovecs_i += 1; | 735 | iovecs_i += 1; |
| ... | @@ -609,7 +739,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -609,7 +739,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 609 | var it = self.data_decl_table.iterator(); | 739 | var it = self.data_decl_table.iterator(); |
| 610 | while (it.next()) |entry| { | 740 | while (it.next()) |entry| { |
| 611 | const decl_index = entry.key_ptr.*; | 741 | const decl_index = entry.key_ptr.*; |
| 612 | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); | 742 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 613 | const code = entry.value_ptr.*; | 743 | const code = entry.value_ptr.*; |
| 614 | | 744 | |
| 615 | foff += code.len; | 745 | foff += code.len; |
| ... | @@ -617,13 +747,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -617,13 +747,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 617 | iovecs_i += 1; | 747 | iovecs_i += 1; |
| 618 | const off = self.getAddr(data_i, .d); | 748 | const off = self.getAddr(data_i, .d); |
| 619 | data_i += code.len; | 749 | data_i += code.len; |
| 620 | decl_block.offset = off; | 750 | atom.offset = off; |
| 621 | if (!self.sixtyfour_bit) { | 751 | if (!self.sixtyfour_bit) { |
| 622 | mem.writeInt(u32, got_table[decl_block.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); | 752 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 623 | } else { | 753 | } else { |
| 624 | mem.writeInt(u64, got_table[decl_block.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); | 754 | mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 625 | } | 755 | } |
| 626 | self.syms.items[decl_block.sym_index.?].value = off; | 756 | self.syms.items[atom.sym_index.?].value = off; |
| 627 | if (mod.decl_exports.get(decl_index)) |exports| { | 757 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 628 | try self.addDeclExports(mod, decl_index, exports.items); | 758 | try self.addDeclExports(mod, decl_index, exports.items); |
| 629 | } | 759 | } |
| ... | @@ -631,28 +761,48 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -631,28 +761,48 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 631 | // write the unnamed constants after the other data decls | 761 | // write the unnamed constants after the other data decls |
| 632 | var it_unc = self.unnamed_const_atoms.iterator(); | 762 | var it_unc = self.unnamed_const_atoms.iterator(); |
| 633 | while (it_unc.next()) |unnamed_consts| { | 763 | while (it_unc.next()) |unnamed_consts| { |
| 634 | for (unnamed_consts.value_ptr.items) |*unnamed_const| { | 764 | for (unnamed_consts.value_ptr.items) |atom_idx| { |
| 635 | const code = unnamed_const.code; | 765 | const atom = self.getAtomPtr(atom_idx); |
| 636 | log.debug("write unnamed const: ({s})", .{self.syms.items[unnamed_const.info.sym_index.?].name}); | 766 | const code = atom.code.getOwnedCode().?; // unnamed consts must own their code |
| | 767 | log.debug("write unnamed const: ({s})", .{self.syms.items[atom.sym_index.?].name}); |
| 637 | foff += code.len; | 768 | foff += code.len; |
| 638 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; | 769 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| 639 | iovecs_i += 1; | 770 | iovecs_i += 1; |
| 640 | const off = self.getAddr(data_i, .d); | 771 | const off = self.getAddr(data_i, .d); |
| 641 | data_i += code.len; | 772 | data_i += code.len; |
| 642 | unnamed_const.info.offset = off; | 773 | atom.offset = off; |
| 643 | if (!self.sixtyfour_bit) { | 774 | if (!self.sixtyfour_bit) { |
| 644 | mem.writeInt(u32, got_table[unnamed_const.info.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); | 775 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 645 | } else { | 776 | } else { |
| 646 | mem.writeInt(u64, got_table[unnamed_const.info.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); | 777 | mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 647 | } | 778 | } |
| 648 | self.syms.items[unnamed_const.info.sym_index.?].value = off; | 779 | self.syms.items[atom.sym_index.?].value = off; |
| | 780 | } |
| | 781 | } |
| | 782 | // the lazy data symbols |
| | 783 | var it_lazy = self.lazy_syms.iterator(); |
| | 784 | while (it_lazy.next()) |kv| { |
| | 785 | const meta = kv.value_ptr; |
| | 786 | const data_atom = if (meta.rodata_state != .unused) self.getAtomPtr(meta.rodata_atom) else continue; |
| | 787 | const code = data_atom.code.getOwnedCode().?; // lazy symbols must own their code |
| | 788 | foff += code.len; |
| | 789 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| | 790 | iovecs_i += 1; |
| | 791 | const off = self.getAddr(data_i, .d); |
| | 792 | data_i += code.len; |
| | 793 | data_atom.offset = off; |
| | 794 | if (!self.sixtyfour_bit) { |
| | 795 | mem.writeInt(u32, got_table[data_atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| | 796 | } else { |
| | 797 | mem.writeInt(u64, got_table[data_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 649 | } | 798 | } |
| | 799 | self.syms.items[data_atom.sym_index.?].value = off; |
| 650 | } | 800 | } |
| 651 | // edata symbol | 801 | // edata symbol |
| 652 | self.syms.items[0].value = self.getAddr(data_i, .b); | 802 | self.syms.items[0].value = self.getAddr(data_i, .b); |
| | 803 | // end |
| | 804 | self.syms.items[1].value = self.getAddr(data_i, .b); |
| 653 | } | 805 | } |
| 654 | // edata | | |
| 655 | self.syms.items[1].value = self.getAddr(0x0, .b); | | |
| 656 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); | 806 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); |
| 657 | try self.writeSyms(&sym_buf); | 807 | try self.writeSyms(&sym_buf); |
| 658 | const syms = try sym_buf.toOwnedSlice(); | 808 | const syms = try sym_buf.toOwnedSlice(); |
| ... | @@ -682,33 +832,31 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -682,33 +832,31 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 682 | { | 832 | { |
| 683 | var it = self.relocs.iterator(); | 833 | var it = self.relocs.iterator(); |
| 684 | while (it.next()) |kv| { | 834 | while (it.next()) |kv| { |
| 685 | const source_decl_index = kv.key_ptr.*; | 835 | const source_atom_index = kv.key_ptr.*; |
| 686 | const source_decl = mod.declPtr(source_decl_index); | 836 | const source_atom = self.getAtom(source_atom_index); |
| | 837 | const source_atom_symbol = self.syms.items[source_atom.sym_index.?]; |
| 687 | for (kv.value_ptr.items) |reloc| { | 838 | for (kv.value_ptr.items) |reloc| { |
| 688 | const target_decl_index = reloc.target; | 839 | const target_atom_index = reloc.target; |
| 689 | const target_decl_block = self.getDeclBlock(self.decls.get(target_decl_index).?.index); | 840 | const target_atom = self.getAtomPtr(target_atom_index); |
| 690 | const target_decl_offset = target_decl_block.offset.?; | 841 | const target_symbol = self.syms.items[target_atom.sym_index.?]; |
| | 842 | const target_offset = target_atom.offset.?; |
| 691 | | 843 | |
| 692 | const offset = reloc.offset; | 844 | const offset = reloc.offset; |
| 693 | const addend = reloc.addend; | 845 | const addend = reloc.addend; |
| 694 | | 846 | |
| 695 | const code = blk: { | 847 | const code = source_atom.code.getCode(self); |
| 696 | const is_fn = source_decl.ty.zigTypeTag(mod) == .Fn; | | |
| 697 | if (is_fn) { | | |
| 698 | const table = self.fn_decl_table.get(source_decl.getFileScope(mod)).?.functions; | | |
| 699 | const output = table.get(source_decl_index).?; | | |
| 700 | break :blk output.code; | | |
| 701 | } else { | | |
| 702 | const code = self.data_decl_table.get(source_decl_index).?; | | |
| 703 | break :blk code; | | |
| 704 | } | | |
| 705 | }; | | |
| 706 | | 848 | |
| 707 | if (!self.sixtyfour_bit) { | 849 | if (reloc.pcrel) { |
| 708 | mem.writeInt(u32, code[@intCast(usize, offset)..][0..4], @intCast(u32, target_decl_offset + addend), self.base.options.target.cpu.arch.endian()); | 850 | const disp = @intCast(i32, target_offset) - @intCast(i32, source_atom.offset.?) - 4 - @intCast(i32, offset); |
| | 851 | mem.writeInt(i32, code[@intCast(usize, offset)..][0..4], @intCast(i32, disp), self.base.options.target.cpu.arch.endian()); |
| 709 | } else { | 852 | } else { |
| 710 | mem.writeInt(u64, code[@intCast(usize, offset)..][0..8], target_decl_offset + addend, self.base.options.target.cpu.arch.endian()); | 853 | if (!self.sixtyfour_bit) { |
| | 854 | mem.writeInt(u32, code[@intCast(usize, offset)..][0..4], @intCast(u32, target_offset + addend), self.base.options.target.cpu.arch.endian()); |
| | 855 | } else { |
| | 856 | mem.writeInt(u64, code[@intCast(usize, offset)..][0..8], target_offset + addend, self.base.options.target.cpu.arch.endian()); |
| | 857 | } |
| 711 | } | 858 | } |
| | 859 | log.debug("relocating the address of '{s}' + {d} into '{s}' + {d} (({s}[{d}] = 0x{x} + 0x{x})", .{ target_symbol.name, addend, source_atom_symbol.name, offset, source_atom_symbol.name, offset, target_offset, addend }); |
| 712 | } | 860 | } |
| 713 | } | 861 | } |
| 714 | } | 862 | } |
| ... | @@ -722,7 +870,7 @@ fn addDeclExports( | ... | @@ -722,7 +870,7 @@ fn addDeclExports( |
| 722 | exports: []const *Module.Export, | 870 | exports: []const *Module.Export, |
| 723 | ) !void { | 871 | ) !void { |
| 724 | const metadata = self.decls.getPtr(decl_index).?; | 872 | const metadata = self.decls.getPtr(decl_index).?; |
| 725 | const decl_block = self.getDeclBlock(metadata.index); | 873 | const atom = self.getAtom(metadata.index); |
| 726 | | 874 | |
| 727 | for (exports) |exp| { | 875 | for (exports) |exp| { |
| 728 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); | 876 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| ... | @@ -739,8 +887,8 @@ fn addDeclExports( | ... | @@ -739,8 +887,8 @@ fn addDeclExports( |
| 739 | } | 887 | } |
| 740 | } | 888 | } |
| 741 | const sym = .{ | 889 | const sym = .{ |
| 742 | .value = decl_block.offset.?, | 890 | .value = atom.offset.?, |
| 743 | .type = decl_block.type.toGlobal(), | 891 | .type = atom.type.toGlobal(), |
| 744 | .name = try self.base.allocator.dupe(u8, exp_name), | 892 | .name = try self.base.allocator.dupe(u8, exp_name), |
| 745 | }; | 893 | }; |
| 746 | | 894 | |
| ... | @@ -780,12 +928,12 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void { | ... | @@ -780,12 +928,12 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 780 | } | 928 | } |
| 781 | if (self.decls.fetchRemove(decl_index)) |const_kv| { | 929 | if (self.decls.fetchRemove(decl_index)) |const_kv| { |
| 782 | var kv = const_kv; | 930 | var kv = const_kv; |
| 783 | const decl_block = self.getDeclBlock(kv.value.index); | 931 | const atom = self.getAtom(kv.value.index); |
| 784 | if (decl_block.got_index) |i| { | 932 | if (atom.got_index) |i| { |
| 785 | // TODO: if this catch {} is triggered, an assertion in flushModule will be triggered, because got_index_free_list will have the wrong length | 933 | // TODO: if this catch {} is triggered, an assertion in flushModule will be triggered, because got_index_free_list will have the wrong length |
| 786 | self.got_index_free_list.append(self.base.allocator, i) catch {}; | 934 | self.got_index_free_list.append(self.base.allocator, i) catch {}; |
| 787 | } | 935 | } |
| 788 | if (decl_block.sym_index) |i| { | 936 | if (atom.sym_index) |i| { |
| 789 | self.syms_index_free_list.append(self.base.allocator, i) catch {}; | 937 | self.syms_index_free_list.append(self.base.allocator, i) catch {}; |
| 790 | self.syms.items[i] = aout.Sym.undefined_symbol; | 938 | self.syms.items[i] = aout.Sym.undefined_symbol; |
| 791 | } | 939 | } |
| ... | @@ -793,40 +941,42 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void { | ... | @@ -793,40 +941,42 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 793 | } | 941 | } |
| 794 | self.freeUnnamedConsts(decl_index); | 942 | self.freeUnnamedConsts(decl_index); |
| 795 | { | 943 | { |
| 796 | const relocs = self.relocs.getPtr(decl_index) orelse return; | 944 | const atom_index = self.decls.get(decl_index).?.index; |
| | 945 | const relocs = self.relocs.getPtr(atom_index) orelse return; |
| 797 | relocs.clearAndFree(self.base.allocator); | 946 | relocs.clearAndFree(self.base.allocator); |
| 798 | assert(self.relocs.remove(decl_index)); | 947 | assert(self.relocs.remove(atom_index)); |
| 799 | } | 948 | } |
| 800 | } | 949 | } |
| 801 | fn freeUnnamedConsts(self: *Plan9, decl_index: Module.Decl.Index) void { | 950 | fn freeUnnamedConsts(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 802 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; | 951 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 803 | for (unnamed_consts.items) |c| { | 952 | for (unnamed_consts.items) |atom_idx| { |
| 804 | self.base.allocator.free(self.syms.items[c.info.sym_index.?].name); | 953 | const atom = self.getAtom(atom_idx); |
| 805 | self.base.allocator.free(c.code); | 954 | self.base.allocator.free(self.syms.items[atom.sym_index.?].name); |
| 806 | self.syms.items[c.info.sym_index.?] = aout.Sym.undefined_symbol; | 955 | self.syms.items[atom.sym_index.?] = aout.Sym.undefined_symbol; |
| 807 | self.syms_index_free_list.append(self.base.allocator, c.info.sym_index.?) catch {}; | 956 | self.syms_index_free_list.append(self.base.allocator, atom.sym_index.?) catch {}; |
| 808 | } | 957 | } |
| 809 | unnamed_consts.clearAndFree(self.base.allocator); | 958 | unnamed_consts.clearAndFree(self.base.allocator); |
| 810 | } | 959 | } |
| 811 | | 960 | |
| 812 | fn createDeclBlock(self: *Plan9) !DeclBlock.Index { | 961 | fn createAtom(self: *Plan9) !Atom.Index { |
| 813 | const gpa = self.base.allocator; | 962 | const gpa = self.base.allocator; |
| 814 | const index = @intCast(DeclBlock.Index, self.decl_blocks.items.len); | 963 | const index = @intCast(Atom.Index, self.atoms.items.len); |
| 815 | const decl_block = try self.decl_blocks.addOne(gpa); | 964 | const atom = try self.atoms.addOne(gpa); |
| 816 | decl_block.* = .{ | 965 | atom.* = .{ |
| 817 | .type = .t, | 966 | .type = .t, |
| 818 | .offset = null, | 967 | .offset = null, |
| 819 | .sym_index = null, | 968 | .sym_index = null, |
| 820 | .got_index = null, | 969 | .got_index = null, |
| | 970 | .code = undefined, |
| 821 | }; | 971 | }; |
| 822 | return index; | 972 | return index; |
| 823 | } | 973 | } |
| 824 | | 974 | |
| 825 | pub fn seeDecl(self: *Plan9, decl_index: Module.Decl.Index) !DeclBlock.Index { | 975 | pub fn seeDecl(self: *Plan9, decl_index: Module.Decl.Index) !Atom.Index { |
| 826 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); | 976 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 827 | if (!gop.found_existing) { | 977 | if (!gop.found_existing) { |
| 828 | const index = try self.createDeclBlock(); | 978 | const index = try self.createAtom(); |
| 829 | self.getDeclBlockPtr(index).got_index = self.allocateGotIndex(); | 979 | self.getAtomPtr(index).got_index = self.allocateGotIndex(); |
| 830 | gop.value_ptr.* = .{ | 980 | gop.value_ptr.* = .{ |
| 831 | .index = index, | 981 | .index = index, |
| 832 | .exports = .{}, | 982 | .exports = .{}, |
| ... | @@ -846,6 +996,88 @@ pub fn updateDeclExports( | ... | @@ -846,6 +996,88 @@ pub fn updateDeclExports( |
| 846 | _ = module; | 996 | _ = module; |
| 847 | _ = exports; | 997 | _ = exports; |
| 848 | } | 998 | } |
| | 999 | |
| | 1000 | pub fn getOrCreateAtomForLazySymbol(self: *Plan9, sym: File.LazySymbol) !Atom.Index { |
| | 1001 | const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(self.base.options.module.?)); |
| | 1002 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| | 1003 | |
| | 1004 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| | 1005 | |
| | 1006 | const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) { |
| | 1007 | .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state }, |
| | 1008 | .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state }, |
| | 1009 | }; |
| | 1010 | switch (metadata.state.*) { |
| | 1011 | .unused => metadata.atom.* = try self.createAtom(), |
| | 1012 | .pending_flush => return metadata.atom.*, |
| | 1013 | .flushed => {}, |
| | 1014 | } |
| | 1015 | metadata.state.* = .pending_flush; |
| | 1016 | const atom = metadata.atom.*; |
| | 1017 | _ = try self.getAtomPtr(atom).getOrCreateSymbolTableEntry(self); |
| | 1018 | _ = self.getAtomPtr(atom).getOrCreateOffsetTableEntry(self); |
| | 1019 | // anyerror needs to be deferred until flushModule |
| | 1020 | if (sym.getDecl(self.base.options.module.?) != .none) { |
| | 1021 | try self.updateLazySymbolAtom(sym, atom); |
| | 1022 | } |
| | 1023 | return atom; |
| | 1024 | } |
| | 1025 | |
| | 1026 | fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Index) !void { |
| | 1027 | const gpa = self.base.allocator; |
| | 1028 | const mod = self.base.options.module.?; |
| | 1029 | |
| | 1030 | var required_alignment: u32 = undefined; |
| | 1031 | var code_buffer = std.ArrayList(u8).init(gpa); |
| | 1032 | defer code_buffer.deinit(); |
| | 1033 | |
| | 1034 | // create the symbol for the name |
| | 1035 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| | 1036 | @tagName(sym.kind), |
| | 1037 | sym.ty.fmt(mod), |
| | 1038 | }); |
| | 1039 | |
| | 1040 | const symbol: aout.Sym = .{ |
| | 1041 | .value = undefined, |
| | 1042 | .type = if (sym.kind == .code) .t else .d, |
| | 1043 | .name = name, |
| | 1044 | }; |
| | 1045 | self.syms.items[self.getAtomPtr(atom_index).sym_index.?] = symbol; |
| | 1046 | |
| | 1047 | // generate the code |
| | 1048 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| | 1049 | mod.declPtr(owner_decl).srcLoc(mod) |
| | 1050 | else |
| | 1051 | Module.SrcLoc{ |
| | 1052 | .file_scope = undefined, |
| | 1053 | .parent_decl_node = undefined, |
| | 1054 | .lazy = .unneeded, |
| | 1055 | }; |
| | 1056 | const res = try codegen.generateLazySymbol( |
| | 1057 | &self.base, |
| | 1058 | src, |
| | 1059 | sym, |
| | 1060 | &required_alignment, |
| | 1061 | &code_buffer, |
| | 1062 | .none, |
| | 1063 | .{ .parent_atom_index = @intCast(Atom.Index, atom_index) }, |
| | 1064 | ); |
| | 1065 | const code = switch (res) { |
| | 1066 | .ok => code_buffer.items, |
| | 1067 | .fail => |em| { |
| | 1068 | log.err("{s}", .{em.msg}); |
| | 1069 | return error.CodegenFail; |
| | 1070 | }, |
| | 1071 | }; |
| | 1072 | // duped_code is freed when the atom is freed |
| | 1073 | var duped_code = try self.base.allocator.dupe(u8, code); |
| | 1074 | errdefer self.base.allocator.free(duped_code); |
| | 1075 | self.getAtomPtr(atom_index).code = .{ |
| | 1076 | .code_ptr = duped_code.ptr, |
| | 1077 | .other = .{ .code_len = duped_code.len }, |
| | 1078 | }; |
| | 1079 | } |
| | 1080 | |
| 849 | pub fn deinit(self: *Plan9) void { | 1081 | pub fn deinit(self: *Plan9) void { |
| 850 | const gpa = self.base.allocator; | 1082 | const gpa = self.base.allocator; |
| 851 | { | 1083 | { |
| ... | @@ -861,6 +1093,14 @@ pub fn deinit(self: *Plan9) void { | ... | @@ -861,6 +1093,14 @@ pub fn deinit(self: *Plan9) void { |
| 861 | self.freeUnnamedConsts(kv.key_ptr.*); | 1093 | self.freeUnnamedConsts(kv.key_ptr.*); |
| 862 | } | 1094 | } |
| 863 | self.unnamed_const_atoms.deinit(gpa); | 1095 | self.unnamed_const_atoms.deinit(gpa); |
| | 1096 | var it_lzc = self.lazy_syms.iterator(); |
| | 1097 | while (it_lzc.next()) |kv| { |
| | 1098 | if (kv.value_ptr.text_state != .unused) |
| | 1099 | gpa.free(self.syms.items[self.getAtom(kv.value_ptr.text_atom).sym_index.?].name); |
| | 1100 | if (kv.value_ptr.rodata_state != .unused) |
| | 1101 | gpa.free(self.syms.items[self.getAtom(kv.value_ptr.rodata_atom).sym_index.?].name); |
| | 1102 | } |
| | 1103 | self.lazy_syms.deinit(gpa); |
| 864 | var itf_files = self.fn_decl_table.iterator(); | 1104 | var itf_files = self.fn_decl_table.iterator(); |
| 865 | while (itf_files.next()) |ent| { | 1105 | while (itf_files.next()) |ent| { |
| 866 | // get the submap | 1106 | // get the submap |
| ... | @@ -883,7 +1123,12 @@ pub fn deinit(self: *Plan9) void { | ... | @@ -883,7 +1123,12 @@ pub fn deinit(self: *Plan9) void { |
| 883 | self.syms_index_free_list.deinit(gpa); | 1123 | self.syms_index_free_list.deinit(gpa); |
| 884 | self.file_segments.deinit(gpa); | 1124 | self.file_segments.deinit(gpa); |
| 885 | self.path_arena.deinit(); | 1125 | self.path_arena.deinit(); |
| 886 | self.decl_blocks.deinit(gpa); | 1126 | for (self.atoms.items) |a| { |
| | 1127 | if (a.code.getOwnedCode()) |c| { |
| | 1128 | gpa.free(c); |
| | 1129 | } |
| | 1130 | } |
| | 1131 | self.atoms.deinit(gpa); |
| 887 | | 1132 | |
| 888 | { | 1133 | { |
| 889 | var it = self.decls.iterator(); | 1134 | var it = self.decls.iterator(); |
| ... | @@ -911,7 +1156,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option | ... | @@ -911,7 +1156,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 911 | | 1156 | |
| 912 | self.bases = defaultBaseAddrs(options.target.cpu.arch); | 1157 | self.bases = defaultBaseAddrs(options.target.cpu.arch); |
| 913 | | 1158 | |
| 914 | // first 3 symbols in our table are edata, end, etext | 1159 | // first 4 symbols in our table are edata, end, etext, and got |
| 915 | try self.syms.appendSlice(self.base.allocator, &.{ | 1160 | try self.syms.appendSlice(self.base.allocator, &.{ |
| 916 | .{ | 1161 | .{ |
| 917 | .value = 0xcafebabe, | 1162 | .value = 0xcafebabe, |
| ... | @@ -928,13 +1173,19 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option | ... | @@ -928,13 +1173,19 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 928 | .type = .T, | 1173 | .type = .T, |
| 929 | .name = "etext", | 1174 | .name = "etext", |
| 930 | }, | 1175 | }, |
| | 1176 | // we include the global offset table to make it easier for debugging |
| | 1177 | .{ |
| | 1178 | .value = self.getAddr(0, .d), // the global offset table starts at 0 |
| | 1179 | .type = .d, |
| | 1180 | .name = "__GOT", |
| | 1181 | }, |
| 931 | }); | 1182 | }); |
| 932 | | 1183 | |
| 933 | return self; | 1184 | return self; |
| 934 | } | 1185 | } |
| 935 | | 1186 | |
| 936 | pub fn writeSym(self: *Plan9, w: anytype, sym: aout.Sym) !void { | 1187 | pub fn writeSym(self: *Plan9, w: anytype, sym: aout.Sym) !void { |
| 937 | log.debug("write sym{{name: {s}, value: {x}}}", .{ sym.name, sym.value }); | 1188 | // log.debug("write sym{{name: {s}, value: {x}}}", .{ sym.name, sym.value }); |
| 938 | if (sym.type == .bad) return; // we don't want to write free'd symbols | 1189 | if (sym.type == .bad) return; // we don't want to write free'd symbols |
| 939 | if (!self.sixtyfour_bit) { | 1190 | if (!self.sixtyfour_bit) { |
| 940 | try w.writeIntBig(u32, @intCast(u32, sym.value)); | 1191 | try w.writeIntBig(u32, @intCast(u32, sym.value)); |
| ... | @@ -950,6 +1201,11 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { | ... | @@ -950,6 +1201,11 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 950 | const mod = self.base.options.module.?; | 1201 | const mod = self.base.options.module.?; |
| 951 | const ip = &mod.intern_pool; | 1202 | const ip = &mod.intern_pool; |
| 952 | const writer = buf.writer(); | 1203 | const writer = buf.writer(); |
| | 1204 | // write the first four symbols (edata, etext, end, __GOT) |
| | 1205 | try self.writeSym(writer, self.syms.items[0]); |
| | 1206 | try self.writeSym(writer, self.syms.items[1]); |
| | 1207 | try self.writeSym(writer, self.syms.items[2]); |
| | 1208 | try self.writeSym(writer, self.syms.items[3]); |
| 953 | // write the f symbols | 1209 | // write the f symbols |
| 954 | { | 1210 | { |
| 955 | var it = self.file_segments.iterator(); | 1211 | var it = self.file_segments.iterator(); |
| ... | @@ -968,8 +1224,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { | ... | @@ -968,8 +1224,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 968 | while (it.next()) |entry| { | 1224 | while (it.next()) |entry| { |
| 969 | const decl_index = entry.key_ptr.*; | 1225 | const decl_index = entry.key_ptr.*; |
| 970 | const decl_metadata = self.decls.get(decl_index).?; | 1226 | const decl_metadata = self.decls.get(decl_index).?; |
| 971 | const decl_block = self.getDeclBlock(decl_metadata.index); | 1227 | const atom = self.getAtom(decl_metadata.index); |
| 972 | const sym = self.syms.items[decl_block.sym_index.?]; | 1228 | const sym = self.syms.items[atom.sym_index.?]; |
| 973 | try self.writeSym(writer, sym); | 1229 | try self.writeSym(writer, sym); |
| 974 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { | 1230 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 975 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { | 1231 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { |
| ... | @@ -978,6 +1234,27 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { | ... | @@ -978,6 +1234,27 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 978 | } | 1234 | } |
| 979 | } | 1235 | } |
| 980 | } | 1236 | } |
| | 1237 | // the data lazy symbols |
| | 1238 | { |
| | 1239 | var it = self.lazy_syms.iterator(); |
| | 1240 | while (it.next()) |kv| { |
| | 1241 | const meta = kv.value_ptr; |
| | 1242 | const data_atom = if (meta.rodata_state != .unused) self.getAtomPtr(meta.rodata_atom) else continue; |
| | 1243 | const sym = self.syms.items[data_atom.sym_index.?]; |
| | 1244 | try self.writeSym(writer, sym); |
| | 1245 | } |
| | 1246 | } |
| | 1247 | // unnamed consts |
| | 1248 | { |
| | 1249 | var it = self.unnamed_const_atoms.iterator(); |
| | 1250 | while (it.next()) |kv| { |
| | 1251 | const consts = kv.value_ptr; |
| | 1252 | for (consts.items) |atom_index| { |
| | 1253 | const sym = self.syms.items[self.getAtom(atom_index).sym_index.?]; |
| | 1254 | try self.writeSym(writer, sym); |
| | 1255 | } |
| | 1256 | } |
| | 1257 | } |
| 981 | // text symbols are the hardest: | 1258 | // text symbols are the hardest: |
| 982 | // the file of a text symbol is the .z symbol before it | 1259 | // the file of a text symbol is the .z symbol before it |
| 983 | // so we have to write everything in the right order | 1260 | // so we have to write everything in the right order |
| ... | @@ -994,8 +1271,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { | ... | @@ -994,8 +1271,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 994 | while (submap_it.next()) |entry| { | 1271 | while (submap_it.next()) |entry| { |
| 995 | const decl_index = entry.key_ptr.*; | 1272 | const decl_index = entry.key_ptr.*; |
| 996 | const decl_metadata = self.decls.get(decl_index).?; | 1273 | const decl_metadata = self.decls.get(decl_index).?; |
| 997 | const decl_block = self.getDeclBlock(decl_metadata.index); | 1274 | const atom = self.getAtom(decl_metadata.index); |
| 998 | const sym = self.syms.items[decl_block.sym_index.?]; | 1275 | const sym = self.syms.items[atom.sym_index.?]; |
| 999 | try self.writeSym(writer, sym); | 1276 | try self.writeSym(writer, sym); |
| 1000 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { | 1277 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 1001 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { | 1278 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { |
| ... | @@ -1007,6 +1284,16 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { | ... | @@ -1007,6 +1284,16 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 1007 | } | 1284 | } |
| 1008 | } | 1285 | } |
| 1009 | } | 1286 | } |
| | 1287 | // the text lazy symbols |
| | 1288 | { |
| | 1289 | var it = self.lazy_syms.iterator(); |
| | 1290 | while (it.next()) |kv| { |
| | 1291 | const meta = kv.value_ptr; |
| | 1292 | const text_atom = if (meta.text_state != .unused) self.getAtomPtr(meta.text_atom) else continue; |
| | 1293 | const sym = self.syms.items[text_atom.sym_index.?]; |
| | 1294 | try self.writeSym(writer, sym); |
| | 1295 | } |
| | 1296 | } |
| 1010 | } | 1297 | } |
| 1011 | } | 1298 | } |
| 1012 | | 1299 | |
| ... | @@ -1024,6 +1311,7 @@ pub fn getDeclVAddr( | ... | @@ -1024,6 +1311,7 @@ pub fn getDeclVAddr( |
| 1024 | ) !u64 { | 1311 | ) !u64 { |
| 1025 | const mod = self.base.options.module.?; | 1312 | const mod = self.base.options.module.?; |
| 1026 | const decl = mod.declPtr(decl_index); | 1313 | const decl = mod.declPtr(decl_index); |
| | 1314 | // we might already know the vaddr |
| 1027 | if (decl.ty.zigTypeTag(mod) == .Fn) { | 1315 | if (decl.ty.zigTypeTag(mod) == .Fn) { |
| 1028 | var start = self.bases.text; | 1316 | var start = self.bases.text; |
| 1029 | var it_file = self.fn_decl_table.iterator(); | 1317 | var it_file = self.fn_decl_table.iterator(); |
| ... | @@ -1043,23 +1331,28 @@ pub fn getDeclVAddr( | ... | @@ -1043,23 +1331,28 @@ pub fn getDeclVAddr( |
| 1043 | start += kv.value_ptr.len; | 1331 | start += kv.value_ptr.len; |
| 1044 | } | 1332 | } |
| 1045 | } | 1333 | } |
| | 1334 | const atom_index = try self.seeDecl(decl_index); |
| 1046 | // the parent_atom_index in this case is just the decl_index of the parent | 1335 | // the parent_atom_index in this case is just the decl_index of the parent |
| 1047 | const gop = try self.relocs.getOrPut(self.base.allocator, @intToEnum(Module.Decl.Index, reloc_info.parent_atom_index)); | 1336 | try self.addReloc(reloc_info.parent_atom_index, .{ |
| 1048 | if (!gop.found_existing) { | 1337 | .target = atom_index, |
| 1049 | gop.value_ptr.* = .{}; | | |
| 1050 | } | | |
| 1051 | try gop.value_ptr.append(self.base.allocator, .{ | | |
| 1052 | .target = decl_index, | | |
| 1053 | .offset = reloc_info.offset, | 1338 | .offset = reloc_info.offset, |
| 1054 | .addend = reloc_info.addend, | 1339 | .addend = reloc_info.addend, |
| 1055 | }); | 1340 | }); |
| 1056 | return 0; | 1341 | return 0xcafebabe; |
| | 1342 | } |
| | 1343 | |
| | 1344 | pub fn addReloc(self: *Plan9, parent_index: Atom.Index, reloc: Reloc) !void { |
| | 1345 | const gop = try self.relocs.getOrPut(self.base.allocator, parent_index); |
| | 1346 | if (!gop.found_existing) { |
| | 1347 | gop.value_ptr.* = .{}; |
| | 1348 | } |
| | 1349 | try gop.value_ptr.append(self.base.allocator, reloc); |
| 1057 | } | 1350 | } |
| 1058 | | 1351 | |
| 1059 | pub fn getDeclBlock(self: *const Plan9, index: DeclBlock.Index) DeclBlock { | 1352 | pub fn getAtom(self: *const Plan9, index: Atom.Index) Atom { |
| 1060 | return self.decl_blocks.items[index]; | 1353 | return self.atoms.items[index]; |
| 1061 | } | 1354 | } |
| 1062 | | 1355 | |
| 1063 | fn getDeclBlockPtr(self: *Plan9, index: DeclBlock.Index) *DeclBlock { | 1356 | fn getAtomPtr(self: *Plan9, index: Atom.Index) *Atom { |
| 1064 | return &self.decl_blocks.items[index]; | 1357 | return &self.atoms.items[index]; |
| 1065 | } | 1358 | } |