Skip to content

Commit 2efbdc0

Browse files
fix(llvm): R_X86_64_32S addresses did not fit, swapping to PIC code (#130)
1 parent 31e1e4b commit 2efbdc0

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

diffsl/src/execution/llvm/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl LlvmModule {
110110
cpu.as_str(),
111111
features.as_str(),
112112
inkwell::OptimizationLevel::Aggressive,
113-
inkwell::targets::RelocMode::Default,
113+
inkwell::targets::RelocMode::PIC,
114114
inkwell::targets::CodeModel::Default,
115115
)
116116
.unwrap();

diffsl/src/execution/relocations.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,24 @@ fn handle_relocation_generic_x86(rela: &Relocation, s: *const u8, p: *mut u8) ->
149149
}
150150
};
151151
match size {
152-
16 => unsafe { (p as *mut i16).write_unaligned(i16::try_from(val).unwrap()) },
153-
32 => unsafe { (p as *mut i32).write_unaligned(i32::try_from(val).unwrap()) },
152+
16 => unsafe {
153+
(p as *mut i16).write_unaligned(i16::try_from(val).map_err(|_| {
154+
anyhow!(
155+
"x86 relocation overflow for {:?} {:?}-bit relocation: value {val:#x} does not fit in i16",
156+
rela.kind(),
157+
size
158+
)
159+
})?)
160+
},
161+
32 => unsafe {
162+
(p as *mut i32).write_unaligned(i32::try_from(val).map_err(|_| {
163+
anyhow!(
164+
"x86 relocation overflow for {:?} {:?}-bit relocation: value {val:#x} does not fit in i32",
165+
rela.kind(),
166+
size
167+
)
168+
})?)
169+
},
154170
64 => unsafe { (p as *mut i64).write_unaligned(val) },
155171
_ => return Err(anyhow!("Unsupported relocation size {:?}", size)),
156172
}

diffsl/tests/roundtrips.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
))]
99
fn model_code() -> &'static str {
1010
r#"
11+
c_i { 1.0, 1.0 }
1112
u { y = 1 }
12-
F { -y }
13+
F { -y * c_i[N] }
1314
out { y }
1415
"#
1516
}

0 commit comments

Comments
 (0)