[feat] Support runtime linking of riscv64 relocatable files.#28
Open
lxyhit wants to merge 1 commit intoweizhiao:mainfrom
Open
[feat] Support runtime linking of riscv64 relocatable files.#28lxyhit wants to merge 1 commit intoweizhiao:mainfrom
lxyhit wants to merge 1 commit intoweizhiao:mainfrom
Conversation
weizhiao
reviewed
Feb 12, 2026
| let lo12 = final_off & 0xfff; | ||
|
|
||
| let ptr = segments.get_mut_ptr::<u32>(offset); | ||
| let auipc = unsafe { ptr.read_unaligned() }; |
Owner
There was a problem hiding this comment.
指令是满足一定的对齐的,可能不用使用read_unaligned和write_unaligned?
Contributor
Author
There was a problem hiding this comment.
riscv的c扩展会导致有的指令不满足对齐,为了性能优化可以用feature指定开不开c扩展
Owner
There was a problem hiding this comment.
好吧,那就使用read_unaligned/write_unaligned吧。
weizhiao
reviewed
Feb 12, 2026
src/arch/riscv64.rs
Outdated
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) |
d3ddf4e to
38f88b8
Compare
weizhiao
reviewed
Feb 20, 2026
src/relocation/static.rs
Outdated
|
|
||
| pub(crate) struct StaticRelocation { | ||
| relocation: Box<[&'static [ElfRelType]]>, | ||
| relocator: StaticRelocator, |
Owner
There was a problem hiding this comment.
这个不要放在StaticRelocation里面,在relocate_impl方法里再进行构造。目的是用到的时侯再创建,避免让RawObject一直带着
deb68b6 to
d360ec2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue: #21.
Initial prototype code to support runtime linking of riscv64 relocatable files, this will be modified.