0

I am trying to write a OS and make it total with something new, take ideas from my mind and just dont copy from the internet i have done it everywhere i could as memory management process creation, multitasking and more, its all code and engineering by me.

But im running into a problem, when im running tasks the program does not know where it is and use addresses as low as the program, for example if i declare a variable and it is stored in 0x800, when i run the program the cpu is going to search for it in that location, but there is a offset, the program offset, so i need to add the program offset to that 0x800 to get the real value, the next code will give you an idea

int a = 10;
if(a == 10)
{
    //more code
}

this does not work becouse the variable a is on a wrong address

I know there is paging but the way the os is already created with multitasking and process management i would need to recreate those functions

Is there a way to give the kernel or the cpu that program address offset so it would run with the correct addresses

Rabyt
  • 61
  • 6
  • x86-64 supports RIP-relative addressing, making position-independent code easy. Otherwise it's inconvenient, e.g. look at GCC or clang output for `-m32 -fPIE` for accessing static / global variables in C, where it uses `call` / `pop` to copy EIP to a register, and uses offsets from that to read pointers from the global-offset-table (GOT). But normally you don't need to do that unless you want ASLR or relocatable shared libraries without runtime fixups (text relocations): with paging, every process has its own address-space so can have their globals at the same fixed address. – Peter Cordes Jul 25 '23 at 21:18
  • Also, you've shown C code that uses `if ()` at the same scope as the declaration, so that's a local variable. Normal compilers implement the "automatic" storage class (local vars) using stack space, with addressing modes like `[rsp + 4]`, when they can't keep them in registers. (BTW, I intentionally didn't mention x86 segment registers as a way to have treat the same address a different way. If you don't care about 64-bit mode, you could maybe use non-zero a non-zero base address for DS/ES/SS if you're trying to do multiprocessing without paging.) – Peter Cordes Jul 25 '23 at 21:21
  • but if the variable is global it does use the stack to store them. See the next print https://prnt.sc/efOlZXBMtz17 the work "RUNNING" is in the binary of the file im going to execute and the pc thinks it is in the address is 0x304d, i need a way to make the computer add the program address that is somewhere in the memory (i save it when i create the task) – Rabyt Jul 25 '23 at 22:06
  • That's a `char *` to a string literal. The string data goes in static storage regardless of where the pointer is stored. Unlike your example where you have a local(?) `int`. Anyway, see my first comment re: position-independent static storage. See [Why does this MOVSS instruction use RIP-relative addressing?](https://stackoverflow.com/q/44967075) for more details and examples of x86-64 RIP-relative addressing, and [How to load address of function or label into register](https://stackoverflow.com/q/57212012) – Peter Cordes Jul 25 '23 at 22:21
  • i cant see how this would help me to get the address right, on your links i read a lot about the rip instruction, and i dont get how it would help me to get an address of a variable like a char * or s global int – Rabyt Jul 26 '23 at 02:45
  • `mov eax, [rip + 0x4000]` or `lea rdi, [rip + 0x4000]` or a similar-sized numerical offset Just Work if you put the static data a fixed distance away from the code. (Normally you wouldn't write the rel32 numerically, you'd have the assembler or linker calculate the distance using symbols and relocation entries, if you're using standard tools and object-file formats like ELF with GNU Binutls and maybe NASM). You can relocate the whole program or library anywhere in virtual address space and it still addresses its data, as long as the relative distance between code and data is constant. – Peter Cordes Jul 26 '23 at 02:50
  • (Syntax like `mov eax, [rip + 0x4000]` is pseudo-code; if you actually wrote that in GAS, it would try to use RIP-relative addressing to access absolute address `0x4000`. [How to load address of function or label into register](https://stackoverflow.com/q/57212012)) – Peter Cordes Jul 26 '23 at 02:51

0 Answers0