Page 1 of 1
cast to pointer from integer of different size
Posted: Mon Jun 27, 2022 12:40 pm
by viruss33
Currently I'm working on physical memory manager and I have a code that assigns address to a pointer variable, and I got this warning which I don't know how to fix. I have a uint32_t* bitmap = (uint32_t*)address, where address is taken from grub structure and is uint64_t.
Similarly, I have a code
memory_region* memory_regions = (memory_region*) address; where address uint64_t.
Re: cast to pointer from integer of different size
Posted: Mon Jun 27, 2022 1:00 pm
by iansjack
Your code is 64-bit?
Re: cast to pointer from integer of different size
Posted: Mon Jun 27, 2022 1:04 pm
by Octocontrabass
The warning means exactly what it says. The physical addresses in the Multiboot structures are 64 bits. You're writing 32-bit code, so your virtual addresses (pointers) have 32 bits.
If you haven't enabled paging, or you've set up paging for identity mapping only, you need to check to make sure the address is below 4GB before assigning it to a pointer.
If you have enabled paging and you're not using identity mapping, you can't use a physical address as a pointer. You need to assign a virtual address to that physical address in your page tables and assign the virtual address to a pointer. If the physical address is above 4GB, you must use PAE to access it.
Re: cast to pointer from integer of different size
Posted: Mon Jun 27, 2022 2:00 pm
by viruss33
Thanks, checking for addresses <4gb and casting address to uint32_t fixes the problem. Paging is not yet enabled, that's what im working now. I'm wondering how the compiler knows that the code I'm writing is 32-bit. Is it the target architecture that we pass to it?
Re: cast to pointer from integer of different size
Posted: Mon Jun 27, 2022 2:19 pm
by eekee
viruss33 wrote:Thanks, checking for addresses <4gb and casting address to uint32_t fixes the problem. Paging is not yet enabled, that's what im working now. I'm wondering how the compiler knows that the code I'm writing is 32-bit. Is it the target architecture that we pass to it?
With gcc, the executable is different for each... what to call it? major architecture version, and for some other details about the target. For instance, the full name of gcc on my old Knoppix disk is "i686-linux-gnu-gcc-8". (It's the only gcc install I have handy.) There's a bunch of symlinks in /usr/bin:
gcc -> gcc-8
gcc-8 -> i686-linux-gnu-gcc-8
i586-linux-gnu-gcc -> gcc-8
i686-linux-gnu-gcc -> gcc-8
i686-linux-gnu-gcc-8