authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-03 10:03:03+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:12:26+01:00
log33a0a72e87bd01ce6dbd10fa327c74168a0166db
treeef016abc30e70792ab37cb872e2bf4d1dba79a9a
parent875beb25b5493ee3b0aedd730f831e4ea5d124cc

elf: align ar_hdr to at least 2 bytes


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

src/link/Elf.zig+17-4
...@@ -1580,6 +1580,13 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void...@@ -1580,6 +1580,13 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
1580 files.putAssumeCapacityNoClobber(zig_object.index, .{ off, 0, 0 });1580 files.putAssumeCapacityNoClobber(zig_object.index, .{ off, 0, 0 });
1581 }1581 }
15821582
1583 // Align to even byte boundary
1584 {
1585 const end = ar_strtab.items.len;
1586 const aligned = mem.alignForward(usize, end, 2);
1587 try ar_strtab.writer().writeByteNTimes(0, aligned - end);
1588 }
1589
1583 // Encode ar symtab in 64bit format.1590 // Encode ar symtab in 64bit format.
1584 var ar_symtab = std.ArrayList(u8).init(gpa);1591 var ar_symtab = std.ArrayList(u8).init(gpa);
1585 defer ar_symtab.deinit();1592 defer ar_symtab.deinit();
...@@ -1599,7 +1606,7 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void...@@ -1599,7 +1606,7 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
1599 try ar_symtab.writer().print("{s}\x00", .{name});1606 try ar_symtab.writer().print("{s}\x00", .{name});
1600 }1607 }
16011608
1602 // Align to 8bytes if required1609 // Align to 8 bytes if required
1603 {1610 {
1604 const end = ar_symtab.items.len;1611 const end = ar_symtab.items.len;
1605 const aligned = mem.alignForward(usize, end, 8);1612 const aligned = mem.alignForward(usize, end, 8);
...@@ -1617,8 +1624,6 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void...@@ -1617,8 +1624,6 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
1617 file_off += @sizeOf(Archive.ar_hdr) + @as(u64, @intCast(ar_symtab.items.len));1624 file_off += @sizeOf(Archive.ar_hdr) + @as(u64, @intCast(ar_symtab.items.len));
1618 // Strtab1625 // Strtab
1619 file_off += @sizeOf(Archive.ar_hdr) + @as(u64, @intCast(ar_strtab.items.len));1626 file_off += @sizeOf(Archive.ar_hdr) + @as(u64, @intCast(ar_strtab.items.len));
1620 // And because we are nice, we will align to 8 bytes.
1621 file_off = mem.alignForward(u64, file_off, 8);
16221627
1623 const files_ptr = files.getPtr(zig_object.index).?;1628 const files_ptr = files.getPtr(zig_object.index).?;
1624 files_ptr[1] = file_off;1629 files_ptr[1] = file_off;
...@@ -1661,7 +1666,7 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void...@@ -1661,7 +1666,7 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
1661 const hdr = setArHdr(.{1666 const hdr = setArHdr(.{
1662 .kind = .strtab,1667 .kind = .strtab,
1663 .name_off = 0,1668 .name_off = 0,
1664 .size = @intCast(mem.alignForward(usize, ar_strtab.items.len, 8)),1669 .size = @intCast(ar_strtab.items.len),
1665 });1670 });
1666 try self.base.file.?.pwriteAll(mem.asBytes(&hdr), pos);1671 try self.base.file.?.pwriteAll(mem.asBytes(&hdr), pos);
1667 pos += @sizeOf(Archive.ar_hdr);1672 pos += @sizeOf(Archive.ar_hdr);
...@@ -1674,8 +1679,16 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void...@@ -1674,8 +1679,16 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
1674 const entry = files.get(zig_object.index).?;1679 const entry = files.get(zig_object.index).?;
1675 const hdr = setArHdr(.{ .kind = .object, .name_off = entry[0], .size = @intCast(entry[2]) });1680 const hdr = setArHdr(.{ .kind = .object, .name_off = entry[0], .size = @intCast(entry[2]) });
1676 try self.base.file.?.pwriteAll(mem.asBytes(&hdr), entry[1]);1681 try self.base.file.?.pwriteAll(mem.asBytes(&hdr), entry[1]);
1682 pos += @sizeOf(Archive.ar_hdr) + entry[2];
1677 }1683 }
16781684
1685 if (pos % 2 != 0) {
1686 pos += 1;
1687 try self.base.file.?.pwriteAll(&[1]u8{0}, pos);
1688 }
1689
1690 assert(mem.isAligned(pos, 2));
1691
1679 // TODO parsed positionals1692 // TODO parsed positionals
16801693
1681 // Magic bytes.1694 // Magic bytes.
src/link/Elf/Archive.zig+1-1
...@@ -88,7 +88,7 @@ pub fn parse(self: *Archive, elf_file: *Elf) !void {...@@ -88,7 +88,7 @@ pub fn parse(self: *Archive, elf_file: *Elf) !void {
88 if (stream.pos % 2 != 0) {88 if (stream.pos % 2 != 0) {
89 stream.pos += 1;89 stream.pos += 1;
90 }90 }
9191 // TODO flag an error if stream.pos > self.data.len after alignment
92 const hdr = reader.readStruct(ar_hdr) catch break;92 const hdr = reader.readStruct(ar_hdr) catch break;
9393
94 if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) {94 if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) {