Commit f37ca3fa73

Jacob Young <jacobly0@users.noreply.github.com>
2023-04-30 01:31:34
link: cleanup lazy alignment
This gets the alignment from the code that creates a lazy symbol instead of guessing it at every use.
1 parent 10a4c22
Changed files (5)
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 });
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,
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 });
 
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,
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(