authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-04 00:02:09+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-03-04 17:28:03-05:00
log3c924abb69db4d508165857ca1c54696a77645ce
tree7cca2c41173b27c163c151519d0bafebd467f3e5
parent8363b951788a666802d6bbd6364eb973e2dbc09a

std.zig.llvm.bitcode_writer: Fix word byte order on big endian systems.

The bitcode format always uses little endian words. Prior to this commit, a bitcode file produced on e.g. aarch64_be or s390x would fail to be loaded by LLVM.

1 files changed, 4 insertions(+), 4 deletions(-)

lib/std/zig/llvm/bitcode_writer.zig+4-4
...@@ -68,13 +68,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -68,13 +68,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {
68 in_bits -= n;68 in_bits -= n;
6969
70 if (self.bit_count != 0) return;70 if (self.bit_count != 0) return;
71 try self.buffer.append(self.bit_buffer);71 try self.buffer.append(std.mem.nativeToLittle(u32, self.bit_buffer));
72 self.bit_buffer = 0;72 self.bit_buffer = 0;
73 }73 }
7474
75 // Write 32-bit chunks of input bits75 // Write 32-bit chunks of input bits
76 while (in_bits >= 32) {76 while (in_bits >= 32) {
77 try self.buffer.append(@truncate(in_buffer));77 try self.buffer.append(std.mem.nativeToLittle(u32, @truncate(in_buffer)));
7878
79 in_buffer >>= 31;79 in_buffer >>= 31;
80 in_buffer >>= 1;80 in_buffer >>= 1;
...@@ -153,7 +153,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -153,7 +153,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
153 pub fn alignTo32(self: *BcWriter) Error!void {153 pub fn alignTo32(self: *BcWriter) Error!void {
154 if (self.bit_count == 0) return;154 if (self.bit_count == 0) return;
155155
156 try self.buffer.append(self.bit_buffer);156 try self.buffer.append(std.mem.nativeToLittle(u32, self.bit_buffer));
157 self.bit_buffer = 0;157 self.bit_buffer = 0;
158 self.bit_count = 0;158 self.bit_count = 0;
159 }159 }
...@@ -209,7 +209,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {...@@ -209,7 +209,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
209 try self.bitcode.alignTo32();209 try self.bitcode.alignTo32();
210210
211 // Set the number of words in the block at the start of the block211 // Set the number of words in the block at the start of the block
212 self.bitcode.buffer.items[self.start] = @truncate(self.bitcode.length() - self.start - 1);212 self.bitcode.buffer.items[self.start] = std.mem.nativeToLittle(u32, @truncate(self.bitcode.length() - self.start - 1));
213 }213 }
214214
215 pub fn writeUnabbrev(self: *Self, code: u32, values: []const u64) Error!void {215 pub fn writeUnabbrev(self: *Self, code: u32, values: []const u64) Error!void {