diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-04-29 19:31:34 -0400 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2023-05-01 19:22:52 -0400 |
| commit | f37ca3fa7370c501c630c53b370fecdeb313e3be (patch) | |
| tree | 5025974bf1873d55e6281a15f31fae016a15a922 /src | |
| parent | 10a4c2269d110d636e7817677fb50c6f418bff34 (diff) | |
| download | zig-f37ca3fa7370c501c630c53b370fecdeb313e3be.tar.gz zig-f37ca3fa7370c501c630c53b370fecdeb313e3be.zip | |
link: cleanup lazy alignment
This gets the alignment from the code that creates a lazy symbol instead
of guessing it at every use.
Diffstat (limited to 'src')
| -rw-r--r-- | src/arch/x86_64/CodeGen.zig | 6 | ||||
| -rw-r--r-- | src/codegen.zig | 8 | ||||
| -rw-r--r-- | src/link/Coff.zig | 18 | ||||
| -rw-r--r-- | src/link/Elf.zig | 12 | ||||
| -rw-r--r-- | src/link/MachO.zig | 14 |
5 files changed, 18 insertions, 40 deletions
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 7f0d07cf9b..eee89e9ded 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -6416,7 +6416,6 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { if (self.bin_file.cast(link.File.Elf)) |elf_file| { const atom_index = try elf_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const atom = elf_file.getAtom(atom_index); _ = try atom.getOrCreateOffsetTableEntry(elf_file); @@ -6429,14 +6428,12 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { const atom_index = try coff_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?; try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index }); } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { const atom_index = try macho_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?; try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index }); @@ -8504,7 +8501,6 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { if (self.bin_file.cast(link.File.Elf)) |elf_file| { const atom_index = try elf_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const atom = elf_file.getAtom(atom_index); _ = try atom.getOrCreateOffsetTableEntry(elf_file); @@ -8517,14 +8513,12 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { const atom_index = try coff_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?; try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index }); } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { const atom_index = try macho_file.getOrCreateAtomForLazySymbol( .{ .kind = .const_data, .ty = Type.anyerror }, - 4, // dword alignment ); const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?; try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index }); diff --git a/src/codegen.zig b/src/codegen.zig index f967566034..690e96d25c 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -104,7 +104,7 @@ pub fn generateLazySymbol( code: *std.ArrayList(u8), debug_output: DebugInfoOutput, reloc_info: RelocInfo, -) CodeGenError!Result { +) CodeGenError!struct { res: Result, alignment: u32 } { _ = debug_output; _ = reloc_info; @@ -133,13 +133,13 @@ pub fn generateLazySymbol( code.appendAssumeCapacity(0); } mem.writeInt(u32, code.items[offset..][0..4], @intCast(u32, code.items.len), endian); - return Result.ok; - } else return .{ .fail = try ErrorMsg.create( + return .{ .res = Result.ok, .alignment = 4 }; + } else return .{ .res = .{ .fail = try ErrorMsg.create( bin_file.allocator, src_loc, "TODO implement generateLazySymbol for {s} {}", .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(mod) }, - ) }; + ) }, .alignment = undefined }; } pub fn generateSymbol( diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 0af681bb5e..d20d17f2b1 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -145,7 +145,6 @@ const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, const LazySymbolMetadata = struct { text_atom: ?Atom.Index = null, rdata_atom: ?Atom.Index = null, - alignment: u32, }; const DeclMetadata = struct { @@ -1195,13 +1194,11 @@ fn updateLazySymbol(self: *Coff, decl: Module.Decl.OptionalIndex, metadata: Lazy link.File.LazySymbol.initDecl(.code, decl, mod), atom, self.text_section_index.?, - metadata.alignment, ); if (metadata.rdata_atom) |atom| try self.updateLazySymbolAtom( link.File.LazySymbol.initDecl(.const_data, decl, mod), atom, self.rdata_section_index.?, - metadata.alignment, ); } @@ -1210,7 +1207,6 @@ fn updateLazySymbolAtom( sym: link.File.LazySymbol, atom_index: Atom.Index, section_index: u16, - required_alignment: u32, ) !void { const gpa = self.base.allocator; const mod = self.base.options.module.?; @@ -1238,7 +1234,7 @@ fn updateLazySymbolAtom( const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{ .parent_atom_index = local_sym_index, }); - const code = switch (res) { + const code = switch (res.res) { .ok => code_buffer.items, .fail => |em| { log.err("{s}", .{em.msg}); @@ -1252,11 +1248,11 @@ fn updateLazySymbolAtom( symbol.section_number = @intToEnum(coff.SectionNumber, section_index + 1); symbol.type = .{ .complex_type = .NULL, .base_type = .NULL }; - const vaddr = try self.allocateAtom(atom_index, code_len, required_alignment); + const vaddr = try self.allocateAtom(atom_index, code_len, res.alignment); errdefer self.freeAtom(atom_index); log.debug("allocated atom for {s} at 0x{x}", .{ name, vaddr }); - log.debug(" (required alignment 0x{x})", .{required_alignment}); + log.debug(" (required alignment 0x{x})", .{res.alignment}); atom.size = code_len; symbol.value = vaddr; @@ -1265,14 +1261,10 @@ fn updateLazySymbolAtom( try self.writeAtom(atom_index, code); } -pub fn getOrCreateAtomForLazySymbol( - self: *Coff, - sym: link.File.LazySymbol, - alignment: u32, -) !Atom.Index { +pub fn getOrCreateAtomForLazySymbol(self: *Coff, sym: link.File.LazySymbol) !Atom.Index { const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl()); errdefer _ = self.lazy_syms.pop(); - if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment }; + if (!gop.found_existing) gop.value_ptr.* = .{}; const atom = switch (sym.kind) { .code => &gop.value_ptr.text_atom, .const_data => &gop.value_ptr.rdata_atom, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 48d952b6cc..b9c113f834 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -67,7 +67,6 @@ const Section = struct { const LazySymbolMetadata = struct { text_atom: ?Atom.Index = null, rodata_atom: ?Atom.Index = null, - alignment: u32, }; const DeclMetadata = struct { @@ -2377,10 +2376,10 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void { } } -pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol, alignment: u32) !Atom.Index { +pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Index { const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl()); errdefer _ = self.lazy_syms.pop(); - if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment }; + if (!gop.found_existing) gop.value_ptr.* = .{}; const atom = switch (sym.kind) { .code => &gop.value_ptr.text_atom, .const_data => &gop.value_ptr.rodata_atom, @@ -2663,13 +2662,11 @@ fn updateLazySymbol(self: *Elf, decl: Module.Decl.OptionalIndex, metadata: LazyS File.LazySymbol.initDecl(.code, decl, mod), atom, self.text_section_index.?, - metadata.alignment, ); if (metadata.rodata_atom) |atom| try self.updateLazySymbolAtom( File.LazySymbol.initDecl(.const_data, decl, mod), atom, self.rodata_section_index.?, - metadata.alignment, ); } @@ -2678,7 +2675,6 @@ fn updateLazySymbolAtom( sym: File.LazySymbol, atom_index: Atom.Index, shdr_index: u16, - required_alignment: u32, ) !void { const gpa = self.base.allocator; const mod = self.base.options.module.?; @@ -2710,7 +2706,7 @@ fn updateLazySymbolAtom( const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{ .parent_atom_index = local_sym_index, }); - const code = switch (res) { + const code = switch (res.res) { .ok => code_buffer.items, .fail => |em| { log.err("{s}", .{em.msg}); @@ -2728,7 +2724,7 @@ fn updateLazySymbolAtom( .st_value = 0, .st_size = 0, }; - const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); + const vaddr = try self.allocateAtom(atom_index, code.len, res.alignment); errdefer self.freeAtom(atom_index); log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr }); diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 21633dea64..a57742507d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -238,7 +238,6 @@ const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, const LazySymbolMetadata = struct { text_atom: ?Atom.Index = null, data_const_atom: ?Atom.Index = null, - alignment: u32, }; const TlvSymbolTable = std.AutoArrayHashMapUnmanaged(SymbolWithLoc, Atom.Index); @@ -2043,13 +2042,11 @@ fn updateLazySymbol(self: *MachO, decl: Module.Decl.OptionalIndex, metadata: Laz File.LazySymbol.initDecl(.code, decl, mod), atom, self.text_section_index.?, - metadata.alignment, ); if (metadata.data_const_atom) |atom| try self.updateLazySymbolAtom( File.LazySymbol.initDecl(.const_data, decl, mod), atom, self.data_const_section_index.?, - metadata.alignment, ); } @@ -2058,7 +2055,6 @@ fn updateLazySymbolAtom( sym: File.LazySymbol, atom_index: Atom.Index, section_index: u8, - required_alignment: u32, ) !void { const gpa = self.base.allocator; const mod = self.base.options.module.?; @@ -2090,7 +2086,7 @@ fn updateLazySymbolAtom( const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{ .parent_atom_index = local_sym_index, }); - const code = switch (res) { + const code = switch (res.res) { .ok => code_buffer.items, .fail => |em| { log.err("{s}", .{em.msg}); @@ -2104,11 +2100,11 @@ fn updateLazySymbolAtom( symbol.n_sect = section_index + 1; symbol.n_desc = 0; - const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); + const vaddr = try self.allocateAtom(atom_index, code.len, res.alignment); errdefer self.freeAtom(atom_index); log.debug("allocated atom for {s} at 0x{x}", .{ name, vaddr }); - log.debug(" (required alignment 0x{x}", .{required_alignment}); + log.debug(" (required alignment 0x{x}", .{res.alignment}); atom.size = code.len; symbol.n_value = vaddr; @@ -2117,10 +2113,10 @@ fn updateLazySymbolAtom( try self.writeAtom(atom_index, code); } -pub fn getOrCreateAtomForLazySymbol(self: *MachO, sym: File.LazySymbol, alignment: u32) !Atom.Index { +pub fn getOrCreateAtomForLazySymbol(self: *MachO, sym: File.LazySymbol) !Atom.Index { const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl()); errdefer _ = self.lazy_syms.pop(); - if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment }; + if (!gop.found_existing) gop.value_ptr.* = .{}; const atom = switch (sym.kind) { .code => &gop.value_ptr.text_atom, .const_data => &gop.value_ptr.data_const_atom, |
