Skip to content

Commit 3c924ab

Browse files
alexrpandrewrk
authored andcommitted
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 parent 8363b95 commit 3c924ab

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/std/zig/llvm/bitcode_writer.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ pub fn BitcodeWriter(comptime types: []const type) type {
6868
in_bits -= n;
6969

7070
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));
7272
self.bit_buffer = 0;
7373
}
7474

7575
// Write 32-bit chunks of input bits
7676
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

7979
in_buffer >>= 31;
8080
in_buffer >>= 1;
@@ -153,7 +153,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
153153
pub fn alignTo32(self: *BcWriter) Error!void {
154154
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));
157157
self.bit_buffer = 0;
158158
self.bit_count = 0;
159159
}
@@ -209,7 +209,7 @@ pub fn BitcodeWriter(comptime types: []const type) type {
209209
try self.bitcode.alignTo32();
210210

211211
// 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));
213213
}
214214

215215
pub fn writeUnabbrev(self: *Self, code: u32, values: []const u64) Error!void {

0 commit comments

Comments
 (0)