zig
zig copied to clipboard
compiler_rt and mcmodel medium on freestanding RISCV build
Zig Version
0.12.0-dev.1785+72568c131
Steps to Reproduce and Observed Behavior
While playing around with a bare-metal riscv code, I'm having a relocation linking issue related to the formatting of float values:
$ zig build-exe -mcmodel=medium zmain.zig init.s -target riscv64-freestanding-none --script memory.ld
error: ld.lld: /home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a(/home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a.o):(function __floatuntidf: .text.__floatuntidf+0x10e):
relocation R_RISCV_HI20 out of range: 524932 is not in [-524288, 524287]; references '.LCPI149_0'
note: referenced by float_from_int.zig:0 (/home/melko/zig-linux-x86_64-0.12.0-dev.1785+72568c131/lib/compiler_rt/float_from_int.zig:0)
note: defined in /home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a(/home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a.o)
error: ld.lld: /home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a(/home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a.o):(function __floatuntidf: .text.__floatuntidf+0x116):
relocation R_RISCV_HI20 out of range: 524833 is not in [-524288, 524287]; references '.LCPI149_1'
note: referenced by float_from_int.zig:0 (/home/melko/zig-linux-x86_64-0.12.0-dev.1785+72568c131/lib/compiler_rt/float_from_int.zig:0)
note: defined in /home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a(/home/melko/.cache/zig/o/df77a783dc22f5323e965e5fb77e6c22/libcompiler_rt.a.o)
As far as I understand, the mcmodel=medium flag should prevent these relocation issues, but somehow seems the flag isn't correctly forwarded to compiler_rt compilation.
If I manually build and link compiler_rt, everything works fine:
$ zig build-obj -mcmodel=medium ~/zig-linux-x86_64-0.12.0-dev.1785+72568c131/lib/compiler_rt.zig -target riscv64-freestanding-none
$ zig build-exe -mcmodel=medium zmain.zig init.s compiler_rt.o -target riscv64-freestanding-none --script memory.ld
Not sure if related to #16957
For the reference, here's the content of zmain.zig:
const std = @import("std");
const uart_addr: *volatile u32 = @ptrFromInt(0x60010004);
const uart_full: *volatile u32 = @ptrFromInt(0x60010008);
const SerialError = error{};
fn write_bytes(_: void, str: []const u8) SerialError!usize {
print(str);
return str.len;
}
const Serial_Writer = std.io.Writer(void, SerialError, write_bytes);
const serial_writer = @as(Serial_Writer, .{ .context = {} });
pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
@setCold(true);
println("-------- PANIC --------");
println(message);
while (true) {}
}
pub export fn main() noreturn {
var dd: f32 = 3;
const ffp: *volatile f32 = ⅆ
std.log.err("Hello {}\n\r", .{ffp.*});
while (true) {}
}
fn putc(c: u8) void {
while ((uart_full.* & 0b1000) != 0) {}
uart_addr.* = c;
}
fn print(str: []const u8) void {
for (str) |c| {
putc(c);
}
}
fn println(str: []const u8) void {
print(str);
print("\n\r");
}
pub const std_options = struct {
pub const log_level = .info;
pub const logFn = myLogFn;
};
pub fn myLogFn(comptime level: std.log.Level, comptime scope: @TypeOf(.enum_literal), comptime format: []const u8, args: anytype) void {
_ = level;
_ = scope;
std.fmt.format(serial_writer, format, args) catch @panic("error formatting string: " ++ format);
}
init.s:
# Assembly for booting
.section .text.entry
# Declare _start used in linker.ld script
.global _start
_start:
/* enable fpu */
li t0, 0x6000
csrr t1, mstatus
or t1, t1, t0
csrw mstatus, t1
# Setup Stack
la sp, boot_stack_top
mv tp, a0
call main
.section .bss.stack
.global boot_stack
boot_stack:
# 1GB
.space 1073741824
.global boot_stack_top
boot_stack_top:
and memory.ld:
/* Linker file */
/* Apply memory mapping to adapt to riscv OpenSBI */
/* Architecture */
OUTPUT_ARCH(riscv)
/* Entry Point */
ENTRY(_start)
/* OpenSBI transfer control to this address */
BASE_ADDRESS = 0x80200000;
SECTIONS
{
. = BASE_ADDRESS;
kernel_start = .;
. = ALIGN(4K); /* alignment for page size 4k */
text_start = .;
.text : {
*(.text.entry)
/* All text section off object file */
*(.text .text.*)
}
. = ALIGN(4K); /* alignment for page size 4k */
rodata_start = .;
.rodata : {
*(.rodata .rodata.*)
__debug_info_start = .;
KEEP(*(.debug_info))
__debug_info_end = .;
__debug_abbrev_start = .;
KEEP(*(.debug_abbrev))
__debug_abbrev_end = .;
__debug_str_start = .;
KEEP(*(.debug_str))
__debug_str_end = .;
__debug_line_start = .;
KEEP(*(.debug_line))
__debug_line_end = .;
__debug_ranges_start = .;
KEEP(*(.debug_ranges))
__debug_ranges_end = .;
}
. = ALIGN(4K); /* alignment for page size 4k */
data_start = .;
.data : {
*(.data .data.*)
*(.sdata .sdata.*)
}
. = ALIGN(4K); /* alignment for page size 4k */
.stack : {
*(.bss.stack)
}
. = ALIGN(4K); /* alignment for page size 4k */
bss_start = .;
.bss : {
*(.bss .bss.*)
*(.sbss .sbss.*)
}
. = ALIGN(4K); /* alignment for page size 4k */
PROVIDE(kernel_end = .);
}
Expected Behavior
Should compile and link without errors.
Yeah, I've ran into the same problem when using floating-point types. Even if I add in floating point features like f
, I get the error.