| ... | ... | @@ -79,6 +79,8 @@ data_decl_table: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, []u8) = .{}, |
| 79 | 79 | /// with `Decl` `main`, and lives as long as that `Decl`. |
| 80 | 80 | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 81 | 81 | |
| 82 | lazy_syms: LazySymbolTable = .{}, |
| 83 | |
| 82 | 84 | relocs: std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Reloc)) = .{}, |
| 83 | 85 | hdr: aout.ExecHdr = undefined, |
| 84 | 86 | |
| ... | ... | @@ -94,7 +96,7 @@ got_index_free_list: std.ArrayListUnmanaged(usize) = .{}, |
| 94 | 96 | |
| 95 | 97 | syms_index_free_list: std.ArrayListUnmanaged(usize) = .{}, |
| 96 | 98 | |
| 97 | | decl_blocks: std.ArrayListUnmanaged(DeclBlock) = .{}, |
| 99 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 98 | 100 | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 99 | 101 | |
| 100 | 102 | const Reloc = struct { |
| ... | ... | @@ -109,11 +111,28 @@ const Bases = struct { |
| 109 | 111 | data: u64, |
| 110 | 112 | }; |
| 111 | 113 | |
| 112 | | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(struct { info: DeclBlock, code: []const u8 })); |
| 114 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(struct { info: Atom, code: []const u8 })); |
| 115 | |
| 116 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); |
| 117 | |
| 118 | const LazySymbolMetadata = struct { |
| 119 | const State = enum { unused, pending_flush, flushed }; |
| 120 | text_atom: Atom.Index = undefined, |
| 121 | rodata_atom: Atom.Index = undefined, |
| 122 | text_state: State = .unused, |
| 123 | rodata_state: State = .unused, |
| 124 | |
| 125 | fn numberOfAtoms(self: LazySymbolMetadata) u32 { |
| 126 | var n: u32 = 0; |
| 127 | if (self.text_state != .unused) n += 1; |
| 128 | if (self.rodata_state != .unused) n += 1; |
| 129 | return n; |
| 130 | } |
| 131 | }; |
| 113 | 132 | |
| 114 | 133 | pub const PtrWidth = enum { p32, p64 }; |
| 115 | 134 | |
| 116 | | pub const DeclBlock = struct { |
| 135 | pub const Atom = struct { |
| 117 | 136 | type: aout.Sym.Type, |
| 118 | 137 | /// offset in the text or data sects |
| 119 | 138 | offset: ?u64, |
| ... | ... | @@ -121,12 +140,36 @@ pub const DeclBlock = struct { |
| 121 | 140 | sym_index: ?usize, |
| 122 | 141 | /// offset into got |
| 123 | 142 | got_index: ?usize, |
| 143 | /// We can optionally store code with the atom |
| 144 | /// It is still owned by whatever created it |
| 145 | /// This can be useful so that we don't need |
| 146 | /// to setup so much infrastructure just to store code |
| 147 | /// for stuff like LazySymbols. |
| 148 | code: ?[]const u8 = null, |
| 124 | 149 | |
| 125 | 150 | pub const Index = u32; |
| 151 | |
| 152 | pub fn getOrCreateOffsetTableEntry(self: *Atom, plan9: *Plan9) usize { |
| 153 | if (self.got_index == null) self.got_index = plan9.allocateGotIndex(); |
| 154 | return self.got_index.?; |
| 155 | } |
| 156 | |
| 157 | pub fn getOrCreateSymbolTableEntry(self: *Atom, plan9: *Plan9) !usize { |
| 158 | if (self.sym_index == null) self.sym_index = try plan9.allocateSymbolIndex(); |
| 159 | return self.sym_index.?; |
| 160 | } |
| 161 | |
| 162 | // asserts that self.got_index != null |
| 163 | pub fn getOffsetTableAddress(self: Atom, plan9: *Plan9) u64 { |
| 164 | const ptr_bytes = @divExact(plan9.base.options.target.ptrBitWidth(), 8); |
| 165 | const got_addr = plan9.bases.data; |
| 166 | const got_index = self.got_index.?; |
| 167 | return got_addr + got_index * ptr_bytes; |
| 168 | } |
| 126 | 169 | }; |
| 127 | 170 | |
| 128 | 171 | const DeclMetadata = struct { |
| 129 | | index: DeclBlock.Index, |
| 172 | index: Atom.Index, |
| 130 | 173 | exports: std.ArrayListUnmanaged(usize) = .{}, |
| 131 | 174 | |
| 132 | 175 | fn getExport(m: DeclMetadata, p9: *const Plan9, name: []const u8) ?usize { |
| ... | ... | @@ -352,7 +395,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 352 | 395 | |
| 353 | 396 | const sym_index = try self.allocateSymbolIndex(); |
| 354 | 397 | |
| 355 | | const info: DeclBlock = .{ |
| 398 | const info: Atom = .{ |
| 356 | 399 | .type = .d, |
| 357 | 400 | .offset = null, |
| 358 | 401 | .sym_index = sym_index, |
| ... | ... | @@ -433,22 +476,22 @@ fn updateFinish(self: *Plan9, decl_index: Module.Decl.Index) !void { |
| 433 | 476 | const is_fn = (decl.ty.zigTypeTag(mod) == .Fn); |
| 434 | 477 | const sym_t: aout.Sym.Type = if (is_fn) .t else .d; |
| 435 | 478 | |
| 436 | | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); |
| 479 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 437 | 480 | // write the internal linker metadata |
| 438 | | decl_block.type = sym_t; |
| 481 | atom.type = sym_t; |
| 439 | 482 | // write the symbol |
| 440 | 483 | // we already have the got index |
| 441 | 484 | const sym: aout.Sym = .{ |
| 442 | 485 | .value = undefined, // the value of stuff gets filled in in flushModule |
| 443 | | .type = decl_block.type, |
| 486 | .type = atom.type, |
| 444 | 487 | .name = try self.base.allocator.dupe(u8, mod.intern_pool.stringToSlice(decl.name)), |
| 445 | 488 | }; |
| 446 | 489 | |
| 447 | | if (decl_block.sym_index) |s| { |
| 490 | if (atom.sym_index) |s| { |
| 448 | 491 | self.syms.items[s] = sym; |
| 449 | 492 | } else { |
| 450 | 493 | const s = try self.allocateSymbolIndex(); |
| 451 | | decl_block.sym_index = s; |
| 494 | atom.sym_index = s; |
| 452 | 495 | self.syms.items[s] = sym; |
| 453 | 496 | } |
| 454 | 497 | } |
| ... | ... | @@ -461,6 +504,7 @@ fn allocateSymbolIndex(self: *Plan9) !usize { |
| 461 | 504 | return self.syms.items.len - 1; |
| 462 | 505 | } |
| 463 | 506 | } |
| 507 | |
| 464 | 508 | fn allocateGotIndex(self: *Plan9) usize { |
| 465 | 509 | if (self.got_index_free_list.popOrNull()) |i| { |
| 466 | 510 | return i; |
| ... | ... | @@ -495,7 +539,7 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { |
| 495 | 539 | } |
| 496 | 540 | } |
| 497 | 541 | |
| 498 | | // counts decls and unnamed consts |
| 542 | // counts decls, unnamed consts, and lazy syms |
| 499 | 543 | fn atomCount(self: *Plan9) usize { |
| 500 | 544 | var fn_decl_count: usize = 0; |
| 501 | 545 | var itf_files = self.fn_decl_table.iterator(); |
| ... | ... | @@ -510,7 +554,12 @@ fn atomCount(self: *Plan9) usize { |
| 510 | 554 | while (it_unc.next()) |unnamed_consts| { |
| 511 | 555 | unnamed_const_count += unnamed_consts.value_ptr.items.len; |
| 512 | 556 | } |
| 513 | | return data_decl_count + fn_decl_count + unnamed_const_count; |
| 557 | var lazy_atom_count: usize = 0; |
| 558 | var it_lazy = self.lazy_syms.iterator(); |
| 559 | while (it_lazy.next()) |kv| { |
| 560 | lazy_atom_count += kv.value_ptr.numberOfAtoms(); |
| 561 | } |
| 562 | return data_decl_count + fn_decl_count + unnamed_const_count + lazy_atom_count; |
| 514 | 563 | } |
| 515 | 564 | |
| 516 | 565 | pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.Node) link.File.FlushError!void { |
| ... | ... | @@ -532,7 +581,32 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 532 | 581 | |
| 533 | 582 | const mod = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 534 | 583 | |
| 535 | | assert(self.got_len == self.atomCount() + self.got_index_free_list.items.len); |
| 584 | // finish up the lazy syms |
| 585 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 586 | // Most lazy symbols can be updated on first use, but |
| 587 | // anyerror needs to wait for everything to be flushed. |
| 588 | if (metadata.text_state != .unused) self.updateLazySymbolAtom( |
| 589 | File.LazySymbol.initDecl(.code, null, mod), |
| 590 | metadata.text_atom, |
| 591 | ) catch |err| return switch (err) { |
| 592 | error.CodegenFail => error.FlushFailure, |
| 593 | else => |e| e, |
| 594 | }; |
| 595 | if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( |
| 596 | File.LazySymbol.initDecl(.const_data, null, mod), |
| 597 | metadata.rodata_atom, |
| 598 | ) catch |err| return switch (err) { |
| 599 | error.CodegenFail => error.FlushFailure, |
| 600 | else => |e| e, |
| 601 | }; |
| 602 | } |
| 603 | for (self.lazy_syms.values()) |*metadata| { |
| 604 | if (metadata.text_state != .unused) metadata.text_state = .flushed; |
| 605 | if (metadata.rodata_state != .unused) metadata.rodata_state = .flushed; |
| 606 | } |
| 607 | // make sure the got table is good |
| 608 | const atom_count = self.atomCount(); |
| 609 | assert(self.got_len == atom_count + self.got_index_free_list.items.len); |
| 536 | 610 | const got_size = self.got_len * if (!self.sixtyfour_bit) @as(u32, 4) else 8; |
| 537 | 611 | var got_table = try self.base.allocator.alloc(u8, got_size); |
| 538 | 612 | defer self.base.allocator.free(got_table); |
| ... | ... | @@ -562,7 +636,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 562 | 636 | var it = fentry.value_ptr.functions.iterator(); |
| 563 | 637 | while (it.next()) |entry| { |
| 564 | 638 | const decl_index = entry.key_ptr.*; |
| 565 | | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); |
| 639 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 566 | 640 | const out = entry.value_ptr.*; |
| 567 | 641 | { |
| 568 | 642 | // connect the previous decl to the next |
| ... | ... | @@ -580,14 +654,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 580 | 654 | iovecs_i += 1; |
| 581 | 655 | const off = self.getAddr(text_i, .t); |
| 582 | 656 | text_i += out.code.len; |
| 583 | | decl_block.offset = off; |
| 657 | atom.offset = off; |
| 584 | 658 | if (!self.sixtyfour_bit) { |
| 585 | | mem.writeIntNative(u32, got_table[decl_block.got_index.? * 4 ..][0..4], @intCast(u32, off)); |
| 586 | | mem.writeInt(u32, got_table[decl_block.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 659 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 587 | 660 | } else { |
| 588 | | mem.writeInt(u64, got_table[decl_block.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 661 | mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 589 | 662 | } |
| 590 | | self.syms.items[decl_block.sym_index.?].value = off; |
| 663 | self.syms.items[atom.sym_index.?].value = off; |
| 591 | 664 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 592 | 665 | try self.addDeclExports(mod, decl_index, exports.items); |
| 593 | 666 | } |
| ... | ... | @@ -597,9 +670,30 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 597 | 670 | // just a nop to make it even, the plan9 linker does this |
| 598 | 671 | try linecountinfo.append(129); |
| 599 | 672 | } |
| 600 | | // etext symbol |
| 601 | | self.syms.items[2].value = self.getAddr(text_i, .t); |
| 602 | 673 | } |
| 674 | // the text lazy symbols |
| 675 | { |
| 676 | var it = self.lazy_syms.iterator(); |
| 677 | while (it.next()) |kv| { |
| 678 | const meta = kv.value_ptr; |
| 679 | const text_atom = if (meta.text_state != .unused) self.getAtomPtr(meta.text_atom) else continue; |
| 680 | const code = text_atom.code.?; |
| 681 | foff += code.len; |
| 682 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| 683 | iovecs_i += 1; |
| 684 | const off = self.getAddr(text_i, .t); |
| 685 | text_i += code.len; |
| 686 | text_atom.offset = off; |
| 687 | if (!self.sixtyfour_bit) { |
| 688 | mem.writeInt(u32, got_table[text_atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 689 | } else { |
| 690 | mem.writeInt(u64, got_table[text_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 691 | } |
| 692 | self.syms.items[text_atom.sym_index.?].value = off; |
| 693 | } |
| 694 | } |
| 695 | // etext symbol |
| 696 | self.syms.items[2].value = self.getAddr(text_i, .t); |
| 603 | 697 | // global offset table is in data |
| 604 | 698 | iovecs[iovecs_i] = .{ .iov_base = got_table.ptr, .iov_len = got_table.len }; |
| 605 | 699 | iovecs_i += 1; |
| ... | ... | @@ -609,7 +703,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 609 | 703 | var it = self.data_decl_table.iterator(); |
| 610 | 704 | while (it.next()) |entry| { |
| 611 | 705 | const decl_index = entry.key_ptr.*; |
| 612 | | const decl_block = self.getDeclBlockPtr(self.decls.get(decl_index).?.index); |
| 706 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 613 | 707 | const code = entry.value_ptr.*; |
| 614 | 708 | |
| 615 | 709 | foff += code.len; |
| ... | ... | @@ -617,13 +711,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 617 | 711 | iovecs_i += 1; |
| 618 | 712 | const off = self.getAddr(data_i, .d); |
| 619 | 713 | data_i += code.len; |
| 620 | | decl_block.offset = off; |
| 714 | atom.offset = off; |
| 621 | 715 | 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()); |
| 716 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 623 | 717 | } else { |
| 624 | | mem.writeInt(u64, got_table[decl_block.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 718 | mem.writeInt(u64, got_table[atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 625 | 719 | } |
| 626 | | self.syms.items[decl_block.sym_index.?].value = off; |
| 720 | self.syms.items[atom.sym_index.?].value = off; |
| 627 | 721 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 628 | 722 | try self.addDeclExports(mod, decl_index, exports.items); |
| 629 | 723 | } |
| ... | ... | @@ -648,11 +742,30 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 648 | 742 | self.syms.items[unnamed_const.info.sym_index.?].value = off; |
| 649 | 743 | } |
| 650 | 744 | } |
| 745 | // the lazy data symbols |
| 746 | var it_lazy = self.lazy_syms.iterator(); |
| 747 | while (it_lazy.next()) |kv| { |
| 748 | const meta = kv.value_ptr; |
| 749 | const data_atom = if (meta.rodata_state != .unused) self.getAtomPtr(meta.rodata_atom) else continue; |
| 750 | const code = data_atom.code.?; |
| 751 | foff += code.len; |
| 752 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| 753 | iovecs_i += 1; |
| 754 | const off = self.getAddr(data_i, .d); |
| 755 | data_i += code.len; |
| 756 | data_atom.offset = off; |
| 757 | if (!self.sixtyfour_bit) { |
| 758 | mem.writeInt(u32, got_table[data_atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 759 | } else { |
| 760 | mem.writeInt(u64, got_table[data_atom.got_index.? * 8 ..][0..8], off, self.base.options.target.cpu.arch.endian()); |
| 761 | } |
| 762 | self.syms.items[data_atom.sym_index.?].value = off; |
| 763 | } |
| 651 | 764 | // edata symbol |
| 652 | 765 | self.syms.items[0].value = self.getAddr(data_i, .b); |
| 766 | // end |
| 767 | self.syms.items[1].value = self.getAddr(data_i, .b); |
| 653 | 768 | } |
| 654 | | // edata |
| 655 | | self.syms.items[1].value = self.getAddr(0x0, .b); |
| 656 | 769 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); |
| 657 | 770 | try self.writeSyms(&sym_buf); |
| 658 | 771 | const syms = try sym_buf.toOwnedSlice(); |
| ... | ... | @@ -686,8 +799,10 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 686 | 799 | const source_decl = mod.declPtr(source_decl_index); |
| 687 | 800 | for (kv.value_ptr.items) |reloc| { |
| 688 | 801 | const target_decl_index = reloc.target; |
| 689 | | const target_decl_block = self.getDeclBlock(self.decls.get(target_decl_index).?.index); |
| 690 | | const target_decl_offset = target_decl_block.offset.?; |
| 802 | const target_decl = mod.declPtr(target_decl_index); |
| 803 | _ = target_decl; |
| 804 | const target_atom = self.getAtom(self.decls.get(target_decl_index).?.index); |
| 805 | const target_decl_offset = target_atom.offset.?; |
| 691 | 806 | |
| 692 | 807 | const offset = reloc.offset; |
| 693 | 808 | const addend = reloc.addend; |
| ... | ... | @@ -722,7 +837,7 @@ fn addDeclExports( |
| 722 | 837 | exports: []const *Module.Export, |
| 723 | 838 | ) !void { |
| 724 | 839 | const metadata = self.decls.getPtr(decl_index).?; |
| 725 | | const decl_block = self.getDeclBlock(metadata.index); |
| 840 | const atom = self.getAtom(metadata.index); |
| 726 | 841 | |
| 727 | 842 | for (exports) |exp| { |
| 728 | 843 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| ... | ... | @@ -739,8 +854,8 @@ fn addDeclExports( |
| 739 | 854 | } |
| 740 | 855 | } |
| 741 | 856 | const sym = .{ |
| 742 | | .value = decl_block.offset.?, |
| 743 | | .type = decl_block.type.toGlobal(), |
| 857 | .value = atom.offset.?, |
| 858 | .type = atom.type.toGlobal(), |
| 744 | 859 | .name = try self.base.allocator.dupe(u8, exp_name), |
| 745 | 860 | }; |
| 746 | 861 | |
| ... | ... | @@ -780,12 +895,12 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 780 | 895 | } |
| 781 | 896 | if (self.decls.fetchRemove(decl_index)) |const_kv| { |
| 782 | 897 | var kv = const_kv; |
| 783 | | const decl_block = self.getDeclBlock(kv.value.index); |
| 784 | | if (decl_block.got_index) |i| { |
| 898 | const atom = self.getAtom(kv.value.index); |
| 899 | if (atom.got_index) |i| { |
| 785 | 900 | // TODO: if this catch {} is triggered, an assertion in flushModule will be triggered, because got_index_free_list will have the wrong length |
| 786 | 901 | self.got_index_free_list.append(self.base.allocator, i) catch {}; |
| 787 | 902 | } |
| 788 | | if (decl_block.sym_index) |i| { |
| 903 | if (atom.sym_index) |i| { |
| 789 | 904 | self.syms_index_free_list.append(self.base.allocator, i) catch {}; |
| 790 | 905 | self.syms.items[i] = aout.Sym.undefined_symbol; |
| 791 | 906 | } |
| ... | ... | @@ -809,11 +924,11 @@ fn freeUnnamedConsts(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 809 | 924 | unnamed_consts.clearAndFree(self.base.allocator); |
| 810 | 925 | } |
| 811 | 926 | |
| 812 | | fn createDeclBlock(self: *Plan9) !DeclBlock.Index { |
| 927 | fn createAtom(self: *Plan9) !Atom.Index { |
| 813 | 928 | const gpa = self.base.allocator; |
| 814 | | const index = @intCast(DeclBlock.Index, self.decl_blocks.items.len); |
| 815 | | const decl_block = try self.decl_blocks.addOne(gpa); |
| 816 | | decl_block.* = .{ |
| 929 | const index = @intCast(Atom.Index, self.atoms.items.len); |
| 930 | const atom = try self.atoms.addOne(gpa); |
| 931 | atom.* = .{ |
| 817 | 932 | .type = .t, |
| 818 | 933 | .offset = null, |
| 819 | 934 | .sym_index = null, |
| ... | ... | @@ -822,11 +937,11 @@ fn createDeclBlock(self: *Plan9) !DeclBlock.Index { |
| 822 | 937 | return index; |
| 823 | 938 | } |
| 824 | 939 | |
| 825 | | pub fn seeDecl(self: *Plan9, decl_index: Module.Decl.Index) !DeclBlock.Index { |
| 940 | pub fn seeDecl(self: *Plan9, decl_index: Module.Decl.Index) !Atom.Index { |
| 826 | 941 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 827 | 942 | if (!gop.found_existing) { |
| 828 | | const index = try self.createDeclBlock(); |
| 829 | | self.getDeclBlockPtr(index).got_index = self.allocateGotIndex(); |
| 943 | const index = try self.createAtom(); |
| 944 | self.getAtomPtr(index).got_index = self.allocateGotIndex(); |
| 830 | 945 | gop.value_ptr.* = .{ |
| 831 | 946 | .index = index, |
| 832 | 947 | .exports = .{}, |
| ... | ... | @@ -846,6 +961,89 @@ pub fn updateDeclExports( |
| 846 | 961 | _ = module; |
| 847 | 962 | _ = exports; |
| 848 | 963 | } |
| 964 | |
| 965 | pub fn getOrCreateAtomForLazySymbol(self: *Plan9, sym: File.LazySymbol) !Atom.Index { |
| 966 | const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(self.base.options.module.?)); |
| 967 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 968 | |
| 969 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 970 | |
| 971 | const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) { |
| 972 | .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state }, |
| 973 | .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state }, |
| 974 | }; |
| 975 | switch (metadata.state.*) { |
| 976 | .unused => metadata.atom.* = try self.createAtom(), |
| 977 | .pending_flush => return metadata.atom.*, |
| 978 | .flushed => {}, |
| 979 | } |
| 980 | metadata.state.* = .pending_flush; |
| 981 | const atom = metadata.atom.*; |
| 982 | _ = try self.getAtomPtr(atom).getOrCreateSymbolTableEntry(self); |
| 983 | _ = self.getAtomPtr(atom).getOrCreateOffsetTableEntry(self); |
| 984 | // anyerror needs to be deferred until flushModule |
| 985 | if (sym.getDecl(self.base.options.module.?) != .none) { |
| 986 | try self.updateLazySymbolAtom(sym, atom); |
| 987 | } |
| 988 | return atom; |
| 989 | } |
| 990 | |
| 991 | fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Index) !void { |
| 992 | const gpa = self.base.allocator; |
| 993 | const mod = self.base.options.module.?; |
| 994 | |
| 995 | const atom = self.getAtomPtr(atom_index); |
| 996 | const local_sym_index = atom.sym_index.?; |
| 997 | |
| 998 | var required_alignment: u32 = undefined; |
| 999 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1000 | defer code_buffer.deinit(); |
| 1001 | |
| 1002 | // create the symbol for the name |
| 1003 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1004 | @tagName(sym.kind), |
| 1005 | sym.ty.fmt(mod), |
| 1006 | }); |
| 1007 | |
| 1008 | const symbol: aout.Sym = .{ |
| 1009 | .value = undefined, |
| 1010 | .type = if (sym.kind == .code) .t else .d, |
| 1011 | .name = name, |
| 1012 | }; |
| 1013 | self.syms.items[atom.sym_index.?] = symbol; |
| 1014 | |
| 1015 | // generate the code |
| 1016 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| 1017 | mod.declPtr(owner_decl).srcLoc(mod) |
| 1018 | else |
| 1019 | Module.SrcLoc{ |
| 1020 | .file_scope = undefined, |
| 1021 | .parent_decl_node = undefined, |
| 1022 | .lazy = .unneeded, |
| 1023 | }; |
| 1024 | const res = try codegen.generateLazySymbol( |
| 1025 | &self.base, |
| 1026 | src, |
| 1027 | sym, |
| 1028 | &required_alignment, |
| 1029 | &code_buffer, |
| 1030 | .none, |
| 1031 | .{ .parent_atom_index = @intCast(u32, local_sym_index) }, |
| 1032 | ); |
| 1033 | const code = switch (res) { |
| 1034 | .ok => code_buffer.items, |
| 1035 | .fail => |em| { |
| 1036 | log.err("{s}", .{em.msg}); |
| 1037 | return error.CodegenFail; |
| 1038 | }, |
| 1039 | }; |
| 1040 | // duped_code is freed when the atom is freed |
| 1041 | var duped_code = try self.base.allocator.dupe(u8, code); |
| 1042 | errdefer self.base.allocator.free(duped_code); |
| 1043 | |
| 1044 | atom.code = duped_code; |
| 1045 | } |
| 1046 | |
| 849 | 1047 | pub fn deinit(self: *Plan9) void { |
| 850 | 1048 | const gpa = self.base.allocator; |
| 851 | 1049 | { |
| ... | ... | @@ -861,6 +1059,14 @@ pub fn deinit(self: *Plan9) void { |
| 861 | 1059 | self.freeUnnamedConsts(kv.key_ptr.*); |
| 862 | 1060 | } |
| 863 | 1061 | self.unnamed_const_atoms.deinit(gpa); |
| 1062 | var it_lzc = self.lazy_syms.iterator(); |
| 1063 | while (it_lzc.next()) |kv| { |
| 1064 | if (kv.value_ptr.text_state != .unused) |
| 1065 | gpa.free(self.syms.items[self.getAtom(kv.value_ptr.text_atom).sym_index.?].name); |
| 1066 | if (kv.value_ptr.rodata_state != .unused) |
| 1067 | gpa.free(self.syms.items[self.getAtom(kv.value_ptr.rodata_atom).sym_index.?].name); |
| 1068 | } |
| 1069 | self.lazy_syms.deinit(gpa); |
| 864 | 1070 | var itf_files = self.fn_decl_table.iterator(); |
| 865 | 1071 | while (itf_files.next()) |ent| { |
| 866 | 1072 | // get the submap |
| ... | ... | @@ -883,7 +1089,12 @@ pub fn deinit(self: *Plan9) void { |
| 883 | 1089 | self.syms_index_free_list.deinit(gpa); |
| 884 | 1090 | self.file_segments.deinit(gpa); |
| 885 | 1091 | self.path_arena.deinit(); |
| 886 | | self.decl_blocks.deinit(gpa); |
| 1092 | for (self.atoms.items) |atom| { |
| 1093 | if (atom.code) |c| { |
| 1094 | gpa.free(c); |
| 1095 | } |
| 1096 | } |
| 1097 | self.atoms.deinit(gpa); |
| 887 | 1098 | |
| 888 | 1099 | { |
| 889 | 1100 | var it = self.decls.iterator(); |
| ... | ... | @@ -911,7 +1122,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 911 | 1122 | |
| 912 | 1123 | self.bases = defaultBaseAddrs(options.target.cpu.arch); |
| 913 | 1124 | |
| 914 | | // first 3 symbols in our table are edata, end, etext |
| 1125 | // first 4 symbols in our table are edata, end, etext, and got |
| 915 | 1126 | try self.syms.appendSlice(self.base.allocator, &.{ |
| 916 | 1127 | .{ |
| 917 | 1128 | .value = 0xcafebabe, |
| ... | ... | @@ -928,6 +1139,12 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 928 | 1139 | .type = .T, |
| 929 | 1140 | .name = "etext", |
| 930 | 1141 | }, |
| 1142 | // we include the global offset table to make it easier for debugging |
| 1143 | .{ |
| 1144 | .value = self.getAddr(0, .d), // the global offset table starts at 0 |
| 1145 | .type = .d, |
| 1146 | .name = "__GOT", |
| 1147 | }, |
| 931 | 1148 | }); |
| 932 | 1149 | |
| 933 | 1150 | return self; |
| ... | ... | @@ -950,6 +1167,11 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 950 | 1167 | const mod = self.base.options.module.?; |
| 951 | 1168 | const ip = &mod.intern_pool; |
| 952 | 1169 | const writer = buf.writer(); |
| 1170 | // write the first four symbols (edata, etext, end, __GOT) |
| 1171 | try self.writeSym(writer, self.syms.items[0]); |
| 1172 | try self.writeSym(writer, self.syms.items[1]); |
| 1173 | try self.writeSym(writer, self.syms.items[2]); |
| 1174 | try self.writeSym(writer, self.syms.items[3]); |
| 953 | 1175 | // write the f symbols |
| 954 | 1176 | { |
| 955 | 1177 | var it = self.file_segments.iterator(); |
| ... | ... | @@ -968,8 +1190,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 968 | 1190 | while (it.next()) |entry| { |
| 969 | 1191 | const decl_index = entry.key_ptr.*; |
| 970 | 1192 | const decl_metadata = self.decls.get(decl_index).?; |
| 971 | | const decl_block = self.getDeclBlock(decl_metadata.index); |
| 972 | | const sym = self.syms.items[decl_block.sym_index.?]; |
| 1193 | const atom = self.getAtom(decl_metadata.index); |
| 1194 | const sym = self.syms.items[atom.sym_index.?]; |
| 973 | 1195 | try self.writeSym(writer, sym); |
| 974 | 1196 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 975 | 1197 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { |
| ... | ... | @@ -978,6 +1200,16 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 978 | 1200 | } |
| 979 | 1201 | } |
| 980 | 1202 | } |
| 1203 | // the data lazy symbols |
| 1204 | { |
| 1205 | var it = self.lazy_syms.iterator(); |
| 1206 | while (it.next()) |kv| { |
| 1207 | const meta = kv.value_ptr; |
| 1208 | const data_atom = if (meta.rodata_state != .unused) self.getAtomPtr(meta.rodata_atom) else continue; |
| 1209 | const sym = self.syms.items[data_atom.sym_index.?]; |
| 1210 | try self.writeSym(writer, sym); |
| 1211 | } |
| 1212 | } |
| 981 | 1213 | // text symbols are the hardest: |
| 982 | 1214 | // the file of a text symbol is the .z symbol before it |
| 983 | 1215 | // so we have to write everything in the right order |
| ... | ... | @@ -994,8 +1226,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 994 | 1226 | while (submap_it.next()) |entry| { |
| 995 | 1227 | const decl_index = entry.key_ptr.*; |
| 996 | 1228 | const decl_metadata = self.decls.get(decl_index).?; |
| 997 | | const decl_block = self.getDeclBlock(decl_metadata.index); |
| 998 | | const sym = self.syms.items[decl_block.sym_index.?]; |
| 1229 | const atom = self.getAtom(decl_metadata.index); |
| 1230 | const sym = self.syms.items[atom.sym_index.?]; |
| 999 | 1231 | try self.writeSym(writer, sym); |
| 1000 | 1232 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 1001 | 1233 | for (exports.items) |e| if (decl_metadata.getExport(self, ip.stringToSlice(e.opts.name))) |exp_i| { |
| ... | ... | @@ -1007,6 +1239,16 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 1007 | 1239 | } |
| 1008 | 1240 | } |
| 1009 | 1241 | } |
| 1242 | // the text lazy symbols |
| 1243 | { |
| 1244 | var it = self.lazy_syms.iterator(); |
| 1245 | while (it.next()) |kv| { |
| 1246 | const meta = kv.value_ptr; |
| 1247 | const text_atom = if (meta.text_state != .unused) self.getAtomPtr(meta.text_atom) else continue; |
| 1248 | const sym = self.syms.items[text_atom.sym_index.?]; |
| 1249 | try self.writeSym(writer, sym); |
| 1250 | } |
| 1251 | } |
| 1010 | 1252 | } |
| 1011 | 1253 | } |
| 1012 | 1254 | |
| ... | ... | @@ -1056,10 +1298,10 @@ pub fn getDeclVAddr( |
| 1056 | 1298 | return 0; |
| 1057 | 1299 | } |
| 1058 | 1300 | |
| 1059 | | pub fn getDeclBlock(self: *const Plan9, index: DeclBlock.Index) DeclBlock { |
| 1060 | | return self.decl_blocks.items[index]; |
| 1301 | pub fn getAtom(self: *const Plan9, index: Atom.Index) Atom { |
| 1302 | return self.atoms.items[index]; |
| 1061 | 1303 | } |
| 1062 | 1304 | |
| 1063 | | fn getDeclBlockPtr(self: *Plan9, index: DeclBlock.Index) *DeclBlock { |
| 1064 | | return &self.decl_blocks.items[index]; |
| 1305 | fn getAtomPtr(self: *Plan9, index: Atom.Index) *Atom { |
| 1306 | return &self.atoms.items[index]; |
| 1065 | 1307 | } |