| ... | ... | @@ -79,7 +79,9 @@ 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 | | relocs: std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Reloc)) = .{}, |
| 82 | lazy_syms: LazySymbolTable = .{}, |
| 83 | |
| 84 | relocs: std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Reloc)) = .{}, |
| 83 | 85 | hdr: aout.ExecHdr = undefined, |
| 84 | 86 | |
| 85 | 87 | // relocs: std. |
| ... | ... | @@ -94,13 +96,14 @@ 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 { |
| 101 | | target: Module.Decl.Index, |
| 103 | target: Atom.Index, |
| 102 | 104 | offset: u64, |
| 103 | 105 | addend: u32, |
| 106 | pcrel: bool = false, |
| 104 | 107 | }; |
| 105 | 108 | |
| 106 | 109 | const Bases = struct { |
| ... | ... | @@ -109,11 +112,28 @@ const Bases = struct { |
| 109 | 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 | 134 | pub const PtrWidth = enum { p32, p64 }; |
| 115 | 135 | |
| 116 | | pub const DeclBlock = struct { |
| 136 | pub const Atom = struct { |
| 117 | 137 | type: aout.Sym.Type, |
| 118 | 138 | /// offset in the text or data sects |
| 119 | 139 | offset: ?u64, |
| ... | ... | @@ -121,12 +141,60 @@ pub const DeclBlock = struct { |
| 121 | 141 | sym_index: ?usize, |
| 122 | 142 | /// offset into got |
| 123 | 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 | 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 | 196 | const DeclMetadata = struct { |
| 129 | | index: DeclBlock.Index, |
| 197 | index: Atom.Index, |
| 130 | 198 | exports: std.ArrayListUnmanaged(usize) = .{}, |
| 131 | 199 | |
| 132 | 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 | 354 | const decl = mod.declPtr(decl_index); |
| 287 | 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 | 359 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 292 | 360 | defer code_buffer.deinit(); |
| ... | ... | @@ -320,6 +388,10 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: Module.Fn.Index, air: |
| 320 | 388 | return; |
| 321 | 389 | }, |
| 322 | 390 | }; |
| 391 | self.getAtomPtr(atom_idx).code = .{ |
| 392 | .code_ptr = null, |
| 393 | .other = .{ .decl_index = decl_index }, |
| 394 | }; |
| 323 | 395 | const out: FnDeclOutput = .{ |
| 324 | 396 | .code = code, |
| 325 | 397 | .lineinfo = try dbg_line_buffer.toOwnedSlice(), |
| ... | ... | @@ -351,12 +423,13 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 351 | 423 | const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index }); |
| 352 | 424 | |
| 353 | 425 | const sym_index = try self.allocateSymbolIndex(); |
| 354 | | |
| 355 | | const info: DeclBlock = .{ |
| 426 | const new_atom_idx = try self.createAtom(); |
| 427 | var info: Atom = .{ |
| 356 | 428 | .type = .d, |
| 357 | 429 | .offset = null, |
| 358 | 430 | .sym_index = sym_index, |
| 359 | 431 | .got_index = self.allocateGotIndex(), |
| 432 | .code = undefined, // filled in later |
| 360 | 433 | }; |
| 361 | 434 | const sym: aout.Sym = .{ |
| 362 | 435 | .value = undefined, |
| ... | ... | @@ -368,7 +441,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 368 | 441 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), tv, &code_buffer, .{ |
| 369 | 442 | .none = {}, |
| 370 | 443 | }, .{ |
| 371 | | .parent_atom_index = @enumToInt(decl_index), |
| 444 | .parent_atom_index = new_atom_idx, |
| 372 | 445 | }); |
| 373 | 446 | const code = switch (res) { |
| 374 | 447 | .ok => code_buffer.items, |
| ... | ... | @@ -382,9 +455,12 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I |
| 382 | 455 | // duped_code is freed when the unnamed const is freed |
| 383 | 456 | var duped_code = try self.base.allocator.dupe(u8, code); |
| 384 | 457 | errdefer self.base.allocator.free(duped_code); |
| 385 | | try unnamed_consts.append(self.base.allocator, .{ .info = info, .code = duped_code }); |
| 386 | | // we return the got_index to codegen so that it can reference to the place of the data in the got |
| 387 | | return @intCast(u32, info.got_index.?); |
| 458 | const new_atom = self.getAtomPtr(new_atom_idx); |
| 459 | new_atom.* = info; |
| 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 | 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 | 475 | } |
| 400 | 476 | } |
| 401 | 477 | |
| 402 | | _ = try self.seeDecl(decl_index); |
| 478 | const atom_idx = try self.seeDecl(decl_index); |
| 403 | 479 | |
| 404 | 480 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 405 | 481 | defer code_buffer.deinit(); |
| ... | ... | @@ -409,7 +485,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo |
| 409 | 485 | .ty = decl.ty, |
| 410 | 486 | .val = decl_val, |
| 411 | 487 | }, &code_buffer, .{ .none = {} }, .{ |
| 412 | | .parent_atom_index = @enumToInt(decl_index), |
| 488 | .parent_atom_index = @intCast(Atom.Index, atom_idx), |
| 413 | 489 | }); |
| 414 | 490 | const code = switch (res) { |
| 415 | 491 | .ok => code_buffer.items, |
| ... | ... | @@ -421,6 +497,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo |
| 421 | 497 | }; |
| 422 | 498 | try self.data_decl_table.ensureUnusedCapacity(self.base.allocator, 1); |
| 423 | 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 | 501 | if (self.data_decl_table.fetchPutAssumeCapacity(decl_index, duped_code)) |old_entry| { |
| 425 | 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 | 510 | const is_fn = (decl.ty.zigTypeTag(mod) == .Fn); |
| 434 | 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 | 514 | // write the internal linker metadata |
| 438 | | decl_block.type = sym_t; |
| 515 | atom.type = sym_t; |
| 439 | 516 | // write the symbol |
| 440 | 517 | // we already have the got index |
| 441 | 518 | const sym: aout.Sym = .{ |
| 442 | 519 | .value = undefined, // the value of stuff gets filled in in flushModule |
| 443 | | .type = decl_block.type, |
| 520 | .type = atom.type, |
| 444 | 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 | 525 | self.syms.items[s] = sym; |
| 449 | 526 | } else { |
| 450 | 527 | const s = try self.allocateSymbolIndex(); |
| 451 | | decl_block.sym_index = s; |
| 528 | atom.sym_index = s; |
| 452 | 529 | self.syms.items[s] = sym; |
| 453 | 530 | } |
| 454 | 531 | } |
| ... | ... | @@ -461,6 +538,7 @@ fn allocateSymbolIndex(self: *Plan9) !usize { |
| 461 | 538 | return self.syms.items.len - 1; |
| 462 | 539 | } |
| 463 | 540 | } |
| 541 | |
| 464 | 542 | fn allocateGotIndex(self: *Plan9) usize { |
| 465 | 543 | if (self.got_index_free_list.popOrNull()) |i| { |
| 466 | 544 | return i; |
| ... | ... | @@ -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 | 577 | fn atomCount(self: *Plan9) usize { |
| 500 | 578 | var fn_decl_count: usize = 0; |
| 501 | 579 | var itf_files = self.fn_decl_table.iterator(); |
| ... | ... | @@ -510,7 +588,12 @@ fn atomCount(self: *Plan9) usize { |
| 510 | 588 | while (it_unc.next()) |unnamed_consts| { |
| 511 | 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 | 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 | 615 | |
| 533 | 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 | 644 | const got_size = self.got_len * if (!self.sixtyfour_bit) @as(u32, 4) else 8; |
| 537 | 645 | var got_table = try self.base.allocator.alloc(u8, got_size); |
| 538 | 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 | 670 | var it = fentry.value_ptr.functions.iterator(); |
| 563 | 671 | while (it.next()) |entry| { |
| 564 | 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 | 675 | const out = entry.value_ptr.*; |
| 567 | 676 | { |
| 568 | 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 | 689 | iovecs_i += 1; |
| 581 | 690 | const off = self.getAddr(text_i, .t); |
| 582 | 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 | 694 | 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()); |
| 695 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @intCast(u32, off), self.base.options.target.cpu.arch.endian()); |
| 587 | 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 | 700 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 592 | 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 | 706 | // just a nop to make it even, the plan9 linker does this |
| 598 | 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 | 733 | // global offset table is in data |
| 604 | 734 | iovecs[iovecs_i] = .{ .iov_base = got_table.ptr, .iov_len = got_table.len }; |
| 605 | 735 | iovecs_i += 1; |
| ... | ... | @@ -609,7 +739,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 609 | 739 | var it = self.data_decl_table.iterator(); |
| 610 | 740 | while (it.next()) |entry| { |
| 611 | 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 | 743 | const code = entry.value_ptr.*; |
| 614 | 744 | |
| 615 | 745 | foff += code.len; |
| ... | ... | @@ -617,13 +747,13 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 617 | 747 | iovecs_i += 1; |
| 618 | 748 | const off = self.getAddr(data_i, .d); |
| 619 | 749 | data_i += code.len; |
| 620 | | decl_block.offset = off; |
| 750 | atom.offset = off; |
| 621 | 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 | 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 | 757 | if (mod.decl_exports.get(decl_index)) |exports| { |
| 628 | 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 | 761 | // write the unnamed constants after the other data decls |
| 632 | 762 | var it_unc = self.unnamed_const_atoms.iterator(); |
| 633 | 763 | while (it_unc.next()) |unnamed_consts| { |
| 634 | | for (unnamed_consts.value_ptr.items) |*unnamed_const| { |
| 635 | | const code = unnamed_const.code; |
| 636 | | log.debug("write unnamed const: ({s})", .{self.syms.items[unnamed_const.info.sym_index.?].name}); |
| 764 | for (unnamed_consts.value_ptr.items) |atom_idx| { |
| 765 | const atom = self.getAtomPtr(atom_idx); |
| 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 | 768 | foff += code.len; |
| 638 | 769 | iovecs[iovecs_i] = .{ .iov_base = code.ptr, .iov_len = code.len }; |
| 639 | 770 | iovecs_i += 1; |
| 640 | 771 | const off = self.getAddr(data_i, .d); |
| 641 | 772 | data_i += code.len; |
| 642 | | unnamed_const.info.offset = off; |
| 773 | atom.offset = off; |
| 643 | 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 | 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 | 801 | // edata symbol |
| 652 | 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 | 806 | var sym_buf = std.ArrayList(u8).init(self.base.allocator); |
| 657 | 807 | try self.writeSyms(&sym_buf); |
| 658 | 808 | const syms = try sym_buf.toOwnedSlice(); |
| ... | ... | @@ -682,33 +832,31 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 682 | 832 | { |
| 683 | 833 | var it = self.relocs.iterator(); |
| 684 | 834 | while (it.next()) |kv| { |
| 685 | | const source_decl_index = kv.key_ptr.*; |
| 686 | | const source_decl = mod.declPtr(source_decl_index); |
| 835 | const source_atom_index = kv.key_ptr.*; |
| 836 | const source_atom = self.getAtom(source_atom_index); |
| 837 | const source_atom_symbol = self.syms.items[source_atom.sym_index.?]; |
| 687 | 838 | for (kv.value_ptr.items) |reloc| { |
| 688 | | 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.?; |
| 839 | const target_atom_index = reloc.target; |
| 840 | const target_atom = self.getAtomPtr(target_atom_index); |
| 841 | const target_symbol = self.syms.items[target_atom.sym_index.?]; |
| 842 | const target_offset = target_atom.offset.?; |
| 691 | 843 | |
| 692 | 844 | const offset = reloc.offset; |
| 693 | 845 | const addend = reloc.addend; |
| 694 | 846 | |
| 695 | | const code = blk: { |
| 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 | | }; |
| 847 | const code = source_atom.code.getCode(self); |
| 706 | 848 | |
| 707 | | if (!self.sixtyfour_bit) { |
| 708 | | mem.writeInt(u32, code[@intCast(usize, offset)..][0..4], @intCast(u32, target_decl_offset + addend), self.base.options.target.cpu.arch.endian()); |
| 849 | if (reloc.pcrel) { |
| 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 | 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 | 870 | exports: []const *Module.Export, |
| 723 | 871 | ) !void { |
| 724 | 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 | 875 | for (exports) |exp| { |
| 728 | 876 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| ... | ... | @@ -739,8 +887,8 @@ fn addDeclExports( |
| 739 | 887 | } |
| 740 | 888 | } |
| 741 | 889 | const sym = .{ |
| 742 | | .value = decl_block.offset.?, |
| 743 | | .type = decl_block.type.toGlobal(), |
| 890 | .value = atom.offset.?, |
| 891 | .type = atom.type.toGlobal(), |
| 744 | 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 | 928 | } |
| 781 | 929 | if (self.decls.fetchRemove(decl_index)) |const_kv| { |
| 782 | 930 | var kv = const_kv; |
| 783 | | const decl_block = self.getDeclBlock(kv.value.index); |
| 784 | | if (decl_block.got_index) |i| { |
| 931 | const atom = self.getAtom(kv.value.index); |
| 932 | if (atom.got_index) |i| { |
| 785 | 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 | 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 | 937 | self.syms_index_free_list.append(self.base.allocator, i) catch {}; |
| 790 | 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 | 941 | } |
| 794 | 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 | 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 | 950 | fn freeUnnamedConsts(self: *Plan9, decl_index: Module.Decl.Index) void { |
| 802 | 951 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 803 | | for (unnamed_consts.items) |c| { |
| 804 | | self.base.allocator.free(self.syms.items[c.info.sym_index.?].name); |
| 805 | | self.base.allocator.free(c.code); |
| 806 | | self.syms.items[c.info.sym_index.?] = aout.Sym.undefined_symbol; |
| 807 | | self.syms_index_free_list.append(self.base.allocator, c.info.sym_index.?) catch {}; |
| 952 | for (unnamed_consts.items) |atom_idx| { |
| 953 | const atom = self.getAtom(atom_idx); |
| 954 | self.base.allocator.free(self.syms.items[atom.sym_index.?].name); |
| 955 | self.syms.items[atom.sym_index.?] = aout.Sym.undefined_symbol; |
| 956 | self.syms_index_free_list.append(self.base.allocator, atom.sym_index.?) catch {}; |
| 808 | 957 | } |
| 809 | 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 | 962 | 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.* = .{ |
| 963 | const index = @intCast(Atom.Index, self.atoms.items.len); |
| 964 | const atom = try self.atoms.addOne(gpa); |
| 965 | atom.* = .{ |
| 817 | 966 | .type = .t, |
| 818 | 967 | .offset = null, |
| 819 | 968 | .sym_index = null, |
| 820 | 969 | .got_index = null, |
| 970 | .code = undefined, |
| 821 | 971 | }; |
| 822 | 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 | 976 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 827 | 977 | if (!gop.found_existing) { |
| 828 | | const index = try self.createDeclBlock(); |
| 829 | | self.getDeclBlockPtr(index).got_index = self.allocateGotIndex(); |
| 978 | const index = try self.createAtom(); |
| 979 | self.getAtomPtr(index).got_index = self.allocateGotIndex(); |
| 830 | 980 | gop.value_ptr.* = .{ |
| 831 | 981 | .index = index, |
| 832 | 982 | .exports = .{}, |
| ... | ... | @@ -846,6 +996,88 @@ pub fn updateDeclExports( |
| 846 | 996 | _ = module; |
| 847 | 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 | 1081 | pub fn deinit(self: *Plan9) void { |
| 850 | 1082 | const gpa = self.base.allocator; |
| 851 | 1083 | { |
| ... | ... | @@ -861,6 +1093,14 @@ pub fn deinit(self: *Plan9) void { |
| 861 | 1093 | self.freeUnnamedConsts(kv.key_ptr.*); |
| 862 | 1094 | } |
| 863 | 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 | 1104 | var itf_files = self.fn_decl_table.iterator(); |
| 865 | 1105 | while (itf_files.next()) |ent| { |
| 866 | 1106 | // get the submap |
| ... | ... | @@ -883,7 +1123,12 @@ pub fn deinit(self: *Plan9) void { |
| 883 | 1123 | self.syms_index_free_list.deinit(gpa); |
| 884 | 1124 | self.file_segments.deinit(gpa); |
| 885 | 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 | 1134 | var it = self.decls.iterator(); |
| ... | ... | @@ -911,7 +1156,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 911 | 1156 | |
| 912 | 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 | 1160 | try self.syms.appendSlice(self.base.allocator, &.{ |
| 916 | 1161 | .{ |
| 917 | 1162 | .value = 0xcafebabe, |
| ... | ... | @@ -928,13 +1173,19 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 928 | 1173 | .type = .T, |
| 929 | 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 | 1184 | return self; |
| 934 | 1185 | } |
| 935 | 1186 | |
| 936 | 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 | 1189 | if (sym.type == .bad) return; // we don't want to write free'd symbols |
| 939 | 1190 | if (!self.sixtyfour_bit) { |
| 940 | 1191 | try w.writeIntBig(u32, @intCast(u32, sym.value)); |
| ... | ... | @@ -950,6 +1201,11 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 950 | 1201 | const mod = self.base.options.module.?; |
| 951 | 1202 | const ip = &mod.intern_pool; |
| 952 | 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 | 1209 | // write the f symbols |
| 954 | 1210 | { |
| 955 | 1211 | var it = self.file_segments.iterator(); |
| ... | ... | @@ -968,8 +1224,8 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 968 | 1224 | while (it.next()) |entry| { |
| 969 | 1225 | const decl_index = entry.key_ptr.*; |
| 970 | 1226 | 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.?]; |
| 1227 | const atom = self.getAtom(decl_metadata.index); |
| 1228 | const sym = self.syms.items[atom.sym_index.?]; |
| 973 | 1229 | try self.writeSym(writer, sym); |
| 974 | 1230 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 975 | 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 | 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 | 1258 | // text symbols are the hardest: |
| 982 | 1259 | // the file of a text symbol is the .z symbol before it |
| 983 | 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 | 1271 | while (submap_it.next()) |entry| { |
| 995 | 1272 | const decl_index = entry.key_ptr.*; |
| 996 | 1273 | 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.?]; |
| 1274 | const atom = self.getAtom(decl_metadata.index); |
| 1275 | const sym = self.syms.items[atom.sym_index.?]; |
| 999 | 1276 | try self.writeSym(writer, sym); |
| 1000 | 1277 | if (self.base.options.module.?.decl_exports.get(decl_index)) |exports| { |
| 1001 | 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 | 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 | 1311 | ) !u64 { |
| 1025 | 1312 | const mod = self.base.options.module.?; |
| 1026 | 1313 | const decl = mod.declPtr(decl_index); |
| 1314 | // we might already know the vaddr |
| 1027 | 1315 | if (decl.ty.zigTypeTag(mod) == .Fn) { |
| 1028 | 1316 | var start = self.bases.text; |
| 1029 | 1317 | var it_file = self.fn_decl_table.iterator(); |
| ... | ... | @@ -1043,23 +1331,28 @@ pub fn getDeclVAddr( |
| 1043 | 1331 | start += kv.value_ptr.len; |
| 1044 | 1332 | } |
| 1045 | 1333 | } |
| 1334 | const atom_index = try self.seeDecl(decl_index); |
| 1046 | 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)); |
| 1048 | | if (!gop.found_existing) { |
| 1049 | | gop.value_ptr.* = .{}; |
| 1050 | | } |
| 1051 | | try gop.value_ptr.append(self.base.allocator, .{ |
| 1052 | | .target = decl_index, |
| 1336 | try self.addReloc(reloc_info.parent_atom_index, .{ |
| 1337 | .target = atom_index, |
| 1053 | 1338 | .offset = reloc_info.offset, |
| 1054 | 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 { |
| 1060 | | return self.decl_blocks.items[index]; |
| 1352 | pub fn getAtom(self: *const Plan9, index: Atom.Index) Atom { |
| 1353 | return self.atoms.items[index]; |
| 1061 | 1354 | } |
| 1062 | 1355 | |
| 1063 | | fn getDeclBlockPtr(self: *Plan9, index: DeclBlock.Index) *DeclBlock { |
| 1064 | | return &self.decl_blocks.items[index]; |
| 1356 | fn getAtomPtr(self: *Plan9, index: Atom.Index) *Atom { |
| 1357 | return &self.atoms.items[index]; |
| 1065 | 1358 | } |