Home > OS >  Creating relative address references - Rust
Creating relative address references - Rust

Time:10-03

In code C we can have the following snippet to create a reference to a relative address in memory.

int *value = (int*)0x0061FF0C;

Since I haven't found a C-like way, I used inline assembly to get the value, but it's just the value, not the reference.

unsafe fn read_from_adress(adress:u32) -> i32 {
   let mut data = 0;
   asm!{
       "mov {}, [{}]", 
       out(reg) data,
       in(reg) adress, 
   };
   return data;
}


let var: i32 = read_from_adress(0x0061FF0C);

In rust I can't find an equivalent and simple way as in C.

I tried this snippet and to no avail.

let value = &mut 0x0061FF0C;

Is there a way equivalent to C ?

obs: Getting relative address values is being done with dll injection. In both C and Rust

CodePudding user response:

The equivalent Rust code of your C code is:

let value = unsafe { *(0x0061FF0C as *const i32) };

However, as noted in the comments, the address is not relative but absolute.

  • Related