Skip to content

Comments

[feat] Support runtime linking of riscv64 relocatable files.#28

Open
lxyhit wants to merge 1 commit intoweizhiao:mainfrom
lxyhit:riscv64-rel
Open

[feat] Support runtime linking of riscv64 relocatable files.#28
lxyhit wants to merge 1 commit intoweizhiao:mainfrom
lxyhit:riscv64-rel

Conversation

@lxyhit
Copy link
Contributor

@lxyhit lxyhit commented Feb 11, 2026

Related issue: #21.
Initial prototype code to support runtime linking of riscv64 relocatable files, this will be modified.

let lo12 = final_off & 0xfff;

let ptr = segments.get_mut_ptr::<u32>(offset);
let auipc = unsafe { ptr.read_unaligned() };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指令是满足一定的对齐的,可能不用使用read_unaligned和write_unaligned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

riscv的c扩展会导致有的指令不满足对齐,为了性能优化可以用feature指定开不开c扩展

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好吧,那就使用read_unaligned/write_unaligned吧。

Comment on lines 655 to 735
R_RISCV_ADD8 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u8>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_add((sym + addend).0 as u8);
unsafe { ptr.write_unaligned(new) };
}
// ADD16: *(u16*)P = *(u16*)P + (S + A)
R_RISCV_ADD16 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u16>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_add((sym + addend).0 as u16);
unsafe { ptr.write_unaligned(new) };
}
// ADD32: *(u32*)P = *(u32*)P + (S + A)
R_RISCV_ADD32 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u32>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_add((sym + addend).0 as u32);
unsafe { ptr.write_unaligned(new) };
}
// ADD64: *(u64*)P = *(u64*)P + (S + A)
R_RISCV_ADD64 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u64>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_add((sym + addend).0 as u64);
unsafe { ptr.write_unaligned(new) };
}

// SUB8: *(u8*)P = *(u8*)P - (S + A)
R_RISCV_SUB8 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u8>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_sub((sym + addend).0 as u8);
unsafe { ptr.write_unaligned(new) };
}
// SUB16: *(u16*)P = *(u16*)P - (S + A)
R_RISCV_SUB16 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u16>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_sub((sym + addend).0 as u16);
unsafe { ptr.write_unaligned(new) };
}
// SUB32: *(u32*)P = *(u32*)P - (S + A)
R_RISCV_SUB32 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u32>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_sub((sym + addend).0 as u32);
unsafe { ptr.write_unaligned(new) };
}
// SUB64: *(u64*)P = *(u64*)P - (S + A)
R_RISCV_SUB64 => {
let Some(sym) = helper.find_symbol(r_sym) else {
return Err(boxed_error());
};
let ptr = segments.get_mut_ptr::<u64>(offset);
let old = unsafe { ptr.read_unaligned() };
let new = old.wrapping_sub((sym + addend).0 as u64);
unsafe { ptr.write_unaligned(new) };
}
// SUB6: 6-bit subtraction (bits 5:0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些可以通过泛型来简化代码

@lxyhit lxyhit force-pushed the riscv64-rel branch 2 times, most recently from d3ddf4e to 38f88b8 Compare February 19, 2026 17:34

pub(crate) struct StaticRelocation {
relocation: Box<[&'static [ElfRelType]]>,
relocator: StaticRelocator,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不要放在StaticRelocation里面,在relocate_impl方法里再进行构造。目的是用到的时侯再创建,避免让RawObject一直带着

@lxyhit lxyhit requested a review from weizhiao February 20, 2026 15:53
@lxyhit lxyhit force-pushed the riscv64-rel branch 2 times, most recently from deb68b6 to d360ec2 Compare February 21, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants