authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-04-29 19:31:34-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-01 19:22:52-04:00
logf37ca3fa7370c501c630c53b370fecdeb313e3be
tree5025974bf1873d55e6281a15f31fae016a15a922
parent10a4c2269d110d636e7817677fb50c6f418bff34

link: cleanup lazy alignment

This gets the alignment from the code that creates a lazy symbol instead of guessing it at every use.

5 files changed, 18 insertions(+), 40 deletions(-)

src/arch/x86_64/CodeGen.zig-6
......@@ -6416,7 +6416,6 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void {
64166416 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
64176417 const atom_index = try elf_file.getOrCreateAtomForLazySymbol(
64186418 .{ .kind = .const_data, .ty = Type.anyerror },
6419 4, // dword alignment
64206419 );
64216420 const atom = elf_file.getAtom(atom_index);
64226421 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
......@@ -6429,14 +6428,12 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void {
64296428 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
64306429 const atom_index = try coff_file.getOrCreateAtomForLazySymbol(
64316430 .{ .kind = .const_data, .ty = Type.anyerror },
6432 4, // dword alignment
64336431 );
64346432 const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?;
64356433 try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index });
64366434 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
64376435 const atom_index = try macho_file.getOrCreateAtomForLazySymbol(
64386436 .{ .kind = .const_data, .ty = Type.anyerror },
6439 4, // dword alignment
64406437 );
64416438 const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
64426439 try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index });
......@@ -8504,7 +8501,6 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
85048501 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
85058502 const atom_index = try elf_file.getOrCreateAtomForLazySymbol(
85068503 .{ .kind = .const_data, .ty = Type.anyerror },
8507 4, // dword alignment
85088504 );
85098505 const atom = elf_file.getAtom(atom_index);
85108506 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
......@@ -8517,14 +8513,12 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
85178513 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
85188514 const atom_index = try coff_file.getOrCreateAtomForLazySymbol(
85198515 .{ .kind = .const_data, .ty = Type.anyerror },
8520 4, // dword alignment
85218516 );
85228517 const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?;
85238518 try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index });
85248519 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
85258520 const atom_index = try macho_file.getOrCreateAtomForLazySymbol(
85268521 .{ .kind = .const_data, .ty = Type.anyerror },
8527 4, // dword alignment
85288522 );
85298523 const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
85308524 try self.genSetReg(addr_reg, Type.usize, .{ .lea_got = sym_index });
src/codegen.zig+4-4
......@@ -104,7 +104,7 @@ pub fn generateLazySymbol(
104104 code: *std.ArrayList(u8),
105105 debug_output: DebugInfoOutput,
106106 reloc_info: RelocInfo,
107) CodeGenError!Result {
107) CodeGenError!struct { res: Result, alignment: u32 } {
108108 _ = debug_output;
109109 _ = reloc_info;
110110
......@@ -133,13 +133,13 @@ pub fn generateLazySymbol(
133133 code.appendAssumeCapacity(0);
134134 }
135135 mem.writeInt(u32, code.items[offset..][0..4], @intCast(u32, code.items.len), endian);
136 return Result.ok;
137 } else return .{ .fail = try ErrorMsg.create(
136 return .{ .res = Result.ok, .alignment = 4 };
137 } else return .{ .res = .{ .fail = try ErrorMsg.create(
138138 bin_file.allocator,
139139 src_loc,
140140 "TODO implement generateLazySymbol for {s} {}",
141141 .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(mod) },
142 ) };
142 ) }, .alignment = undefined };
143143}
144144
145145pub fn generateSymbol(
src/link/Coff.zig+5-13
......@@ -145,7 +145,6 @@ const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex,
145145const LazySymbolMetadata = struct {
146146 text_atom: ?Atom.Index = null,
147147 rdata_atom: ?Atom.Index = null,
148 alignment: u32,
149148};
150149
151150const DeclMetadata = struct {
......@@ -1195,13 +1194,11 @@ fn updateLazySymbol(self: *Coff, decl: Module.Decl.OptionalIndex, metadata: Lazy
11951194 link.File.LazySymbol.initDecl(.code, decl, mod),
11961195 atom,
11971196 self.text_section_index.?,
1198 metadata.alignment,
11991197 );
12001198 if (metadata.rdata_atom) |atom| try self.updateLazySymbolAtom(
12011199 link.File.LazySymbol.initDecl(.const_data, decl, mod),
12021200 atom,
12031201 self.rdata_section_index.?,
1204 metadata.alignment,
12051202 );
12061203}
12071204
......@@ -1210,7 +1207,6 @@ fn updateLazySymbolAtom(
12101207 sym: link.File.LazySymbol,
12111208 atom_index: Atom.Index,
12121209 section_index: u16,
1213 required_alignment: u32,
12141210) !void {
12151211 const gpa = self.base.allocator;
12161212 const mod = self.base.options.module.?;
......@@ -1238,7 +1234,7 @@ fn updateLazySymbolAtom(
12381234 const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{
12391235 .parent_atom_index = local_sym_index,
12401236 });
1241 const code = switch (res) {
1237 const code = switch (res.res) {
12421238 .ok => code_buffer.items,
12431239 .fail => |em| {
12441240 log.err("{s}", .{em.msg});
......@@ -1252,11 +1248,11 @@ fn updateLazySymbolAtom(
12521248 symbol.section_number = @intToEnum(coff.SectionNumber, section_index + 1);
12531249 symbol.type = .{ .complex_type = .NULL, .base_type = .NULL };
12541250
1255 const vaddr = try self.allocateAtom(atom_index, code_len, required_alignment);
1251 const vaddr = try self.allocateAtom(atom_index, code_len, res.alignment);
12561252 errdefer self.freeAtom(atom_index);
12571253
12581254 log.debug("allocated atom for {s} at 0x{x}", .{ name, vaddr });
1259 log.debug(" (required alignment 0x{x})", .{required_alignment});
1255 log.debug(" (required alignment 0x{x})", .{res.alignment});
12601256
12611257 atom.size = code_len;
12621258 symbol.value = vaddr;
......@@ -1265,14 +1261,10 @@ fn updateLazySymbolAtom(
12651261 try self.writeAtom(atom_index, code);
12661262}
12671263
1268pub fn getOrCreateAtomForLazySymbol(
1269 self: *Coff,
1270 sym: link.File.LazySymbol,
1271 alignment: u32,
1272) !Atom.Index {
1264pub fn getOrCreateAtomForLazySymbol(self: *Coff, sym: link.File.LazySymbol) !Atom.Index {
12731265 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl());
12741266 errdefer _ = self.lazy_syms.pop();
1275 if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment };
1267 if (!gop.found_existing) gop.value_ptr.* = .{};
12761268 const atom = switch (sym.kind) {
12771269 .code => &gop.value_ptr.text_atom,
12781270 .const_data => &gop.value_ptr.rdata_atom,
src/link/Elf.zig+4-8
......@@ -67,7 +67,6 @@ const Section = struct {
6767const LazySymbolMetadata = struct {
6868 text_atom: ?Atom.Index = null,
6969 rodata_atom: ?Atom.Index = null,
70 alignment: u32,
7170};
7271
7372const DeclMetadata = struct {
......@@ -2377,10 +2376,10 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void {
23772376 }
23782377}
23792378
2380pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol, alignment: u32) !Atom.Index {
2379pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Index {
23812380 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl());
23822381 errdefer _ = self.lazy_syms.pop();
2383 if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment };
2382 if (!gop.found_existing) gop.value_ptr.* = .{};
23842383 const atom = switch (sym.kind) {
23852384 .code => &gop.value_ptr.text_atom,
23862385 .const_data => &gop.value_ptr.rodata_atom,
......@@ -2663,13 +2662,11 @@ fn updateLazySymbol(self: *Elf, decl: Module.Decl.OptionalIndex, metadata: LazyS
26632662 File.LazySymbol.initDecl(.code, decl, mod),
26642663 atom,
26652664 self.text_section_index.?,
2666 metadata.alignment,
26672665 );
26682666 if (metadata.rodata_atom) |atom| try self.updateLazySymbolAtom(
26692667 File.LazySymbol.initDecl(.const_data, decl, mod),
26702668 atom,
26712669 self.rodata_section_index.?,
2672 metadata.alignment,
26732670 );
26742671}
26752672
......@@ -2678,7 +2675,6 @@ fn updateLazySymbolAtom(
26782675 sym: File.LazySymbol,
26792676 atom_index: Atom.Index,
26802677 shdr_index: u16,
2681 required_alignment: u32,
26822678) !void {
26832679 const gpa = self.base.allocator;
26842680 const mod = self.base.options.module.?;
......@@ -2710,7 +2706,7 @@ fn updateLazySymbolAtom(
27102706 const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{
27112707 .parent_atom_index = local_sym_index,
27122708 });
2713 const code = switch (res) {
2709 const code = switch (res.res) {
27142710 .ok => code_buffer.items,
27152711 .fail => |em| {
27162712 log.err("{s}", .{em.msg});
......@@ -2728,7 +2724,7 @@ fn updateLazySymbolAtom(
27282724 .st_value = 0,
27292725 .st_size = 0,
27302726 };
2731 const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment);
2727 const vaddr = try self.allocateAtom(atom_index, code.len, res.alignment);
27322728 errdefer self.freeAtom(atom_index);
27332729 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });
27342730
src/link/MachO.zig+5-9
......@@ -238,7 +238,6 @@ const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex,
238238const LazySymbolMetadata = struct {
239239 text_atom: ?Atom.Index = null,
240240 data_const_atom: ?Atom.Index = null,
241 alignment: u32,
242241};
243242
244243const TlvSymbolTable = std.AutoArrayHashMapUnmanaged(SymbolWithLoc, Atom.Index);
......@@ -2043,13 +2042,11 @@ fn updateLazySymbol(self: *MachO, decl: Module.Decl.OptionalIndex, metadata: Laz
20432042 File.LazySymbol.initDecl(.code, decl, mod),
20442043 atom,
20452044 self.text_section_index.?,
2046 metadata.alignment,
20472045 );
20482046 if (metadata.data_const_atom) |atom| try self.updateLazySymbolAtom(
20492047 File.LazySymbol.initDecl(.const_data, decl, mod),
20502048 atom,
20512049 self.data_const_section_index.?,
2052 metadata.alignment,
20532050 );
20542051}
20552052
......@@ -2058,7 +2055,6 @@ fn updateLazySymbolAtom(
20582055 sym: File.LazySymbol,
20592056 atom_index: Atom.Index,
20602057 section_index: u8,
2061 required_alignment: u32,
20622058) !void {
20632059 const gpa = self.base.allocator;
20642060 const mod = self.base.options.module.?;
......@@ -2090,7 +2086,7 @@ fn updateLazySymbolAtom(
20902086 const res = try codegen.generateLazySymbol(&self.base, src, sym, &code_buffer, .none, .{
20912087 .parent_atom_index = local_sym_index,
20922088 });
2093 const code = switch (res) {
2089 const code = switch (res.res) {
20942090 .ok => code_buffer.items,
20952091 .fail => |em| {
20962092 log.err("{s}", .{em.msg});
......@@ -2104,11 +2100,11 @@ fn updateLazySymbolAtom(
21042100 symbol.n_sect = section_index + 1;
21052101 symbol.n_desc = 0;
21062102
2107 const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment);
2103 const vaddr = try self.allocateAtom(atom_index, code.len, res.alignment);
21082104 errdefer self.freeAtom(atom_index);
21092105
21102106 log.debug("allocated atom for {s} at 0x{x}", .{ name, vaddr });
2111 log.debug(" (required alignment 0x{x}", .{required_alignment});
2107 log.debug(" (required alignment 0x{x}", .{res.alignment});
21122108
21132109 atom.size = code.len;
21142110 symbol.n_value = vaddr;
......@@ -2117,10 +2113,10 @@ fn updateLazySymbolAtom(
21172113 try self.writeAtom(atom_index, code);
21182114}
21192115
2120pub fn getOrCreateAtomForLazySymbol(self: *MachO, sym: File.LazySymbol, alignment: u32) !Atom.Index {
2116pub fn getOrCreateAtomForLazySymbol(self: *MachO, sym: File.LazySymbol) !Atom.Index {
21212117 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl());
21222118 errdefer _ = self.lazy_syms.pop();
2123 if (!gop.found_existing) gop.value_ptr.* = .{ .alignment = alignment };
2119 if (!gop.found_existing) gop.value_ptr.* = .{};
21242120 const atom = switch (sym.kind) {
21252121 .code => &gop.value_ptr.text_atom,
21262122 .const_data => &gop.value_ptr.data_const_atom,