-1

I want to compile C code like it is 32-bit app (so sizeof(void*) would be 4), but run it inside 64-bit app.

I can guarantee this code will not receive pointers to >4 GB address space, etc.

I just need a toolchain to do so.

StaceyGirl
  • 7,242
  • 12
  • 35
  • 64
  • Ok so if I understand this correctly, it's ok if you're in 64bit mode, but the program should (as much as possible) pretend that isn't? So basically, no use of 64bit registers (except when making external calls). That's a lot more possible, though I don't know of any compiler that will actually do it. – harold Sep 15 '14 at 20:05
  • Yes. I need to forbit generated to use 64bit registers and overall code should behave like it is 32-bit C code (sizeof operator should return same values as at 32-bit platform) – StaceyGirl Sep 15 '14 at 20:09
  • 2
    Perhaps it would be easier to answer this question if you tell us what problem you are *really* trying to solve. – Anders Hansson Sep 15 '14 at 20:12
  • trying to run C code written for 32-bit ARM microcontroller natively on x86-64 – StaceyGirl Sep 15 '14 at 20:14
  • If you are not going to access external OS libs, etc, you might get away with using GCC together with the -m32 flag. – Anders Hansson Sep 15 '14 at 20:15
  • @Ivan: You are compiling from source, or trying to run ARM machine code? The latter definitely can't work, your x86_64 CPU knows nothing about ARM instructions. – Ben Voigt Sep 15 '14 at 21:20
  • I want to compile C code written for ARM microcontroller, so it assumes sizeof(void*) is 4. But I want to run it in 64 bit environment. – StaceyGirl Sep 15 '14 at 21:29
  • The OS is free to load your program wherever it wants, so you can't guarantee every pointer will be within the first 4GB of memory. Without that guarantee you can't assume 4 byte pointers will work. – Mark Ransom Sep 15 '14 at 22:01
  • I can force OS to load everything I need to run 32 bit in the first 4 GB. Other parts will use 64 bit address space freely. – StaceyGirl Sep 15 '14 at 22:17
  • gcc has an option "use 32 bit pointers in a 64 bit OS" which may be useful (the program itself is 64 bit) FWIW http://stackoverflow.com/a/10084046/32453 – rogerdpack Nov 12 '16 at 05:07

2 Answers2

3

There is an x32 memory model available for Linux, which is 64-bit code with 32-bit pointers. This seems more suitable for "don't break code that stores pointers in unsigned int, please!" usage. But the whole application should be built using that target.

However combining x32 and x86_64 code in the same process is much less difficult than x86 and x86_64, since it doesn't require the CPU to switch modes.

hauptmech
  • 343
  • 2
  • 11
Ben Voigt
  • 269,602
  • 39
  • 394
  • 697
  • Actually I'm seeing mixed names, is it x64_32 or x86_32? Anyway, it seems to not have caught on very well. – Ben Voigt Sep 15 '14 at 21:19
  • Neither. It's simply "x32". –  Sep 15 '14 at 22:09
  • One of the major reasons for x32's existence is to reduce the cache footprint / memory-bandwidth consumption of code with lots of pointer-heavy data structures, while getting the speedups from long mode (including SSE2 as a baseline) and a better ABI. (`long` is also 32 bit, but of course `int64_t` is still efficient and atomic since it runs in long mode with 64-bit registers.) Interesting point that it could be useful for not-very-portable code. – Peter Cordes Aug 13 '16 at 03:06
1

How can I compile C code as 32 bit code and run in in 64-bit app??

On both Windows and Linux, you cannot execute 32 bit code in a 64 bit process. Or indeed vice versa.

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
  • I am not searching for "valid" way to do so. OSes forbid this because 32bit code will not be able to work with 64bit pointers. But I would like to hack it in some way. My C code will not access OS api etc. – StaceyGirl Sep 15 '14 at 19:38
  • *OSes forbid this because 32bit code will not be able to work with 64bit pointers.* That is just one reason of the multitude of reasons. Anyway, what I state is accurate. – David Heffernan Sep 15 '14 at 19:42
  • The point is I do not need OS support, I just want to generate 32bit code and run it inside 64 bit app. Writing my own compiler to do this is a bad idea, so I am looking for alternatives – StaceyGirl Sep 15 '14 at 19:44
  • I am asking if there is toolchain to do so...I think I will corrent the question though – StaceyGirl Sep 15 '14 at 19:49
  • OS works in 64 bit mode and starts 64 bit app, CPU can execute 32bit instructions here – StaceyGirl Sep 15 '14 at 21:28
  • So what's stopping you? – David Heffernan Sep 15 '14 at 21:30
  • I need compiler to generate and link together 32 bit and 64 bit code. I think I can try using Clang/LLVM to perform C->LLIR translation for x86 arch, then compiler resulting IR into x86_64 assembly. I will post results later – StaceyGirl Sep 15 '14 at 21:35