Detect if a 32-bit process is running in a 64-bit environment under Linux

I am distributing a 32 bit assembly of software. I want to detect at runtime if the user realm it is running in is 64-bit and capable of executing 64-bit executable binaries (ELF 64-bit x86-64).

This is not the same problem as at compile time, whether the architecture is 32-bit or 64-bit.

Ideally I would like to do this in the most portable way, so I would rather avoid things like file $(which init) | grep x86-64

that depend on

I don't think reading the cpuid is also a solution - maybe the 32-bit OS is running on a 64-bit processor.

Detecting a 64-bit processor running in long mode is also not a solution, nor does it determine if the kernel itself is 64-bit, as there is a possibility that the OS is a 32-bit user area with 64-bit kernel (as is possible with some Debian configurations ).

I'm not primarily interested in detecting bitness other than 32 and 64.

One possible robust solution I can think of is to actually enable and try to call the 64-bit ELF binary and see if it works or not, but this is not a very efficient way to do it. Is there a linux function or something in stl or boost that can help me find reliably?

Ironically, the equivalent problem is very easy to solve on Windows this time around .

+3


source to share


1 answer


You can check availability /lib64/ld-linux-x86-64.so.2

. In theory, this doesn't always work because the Linux system might put the dynamic linker somewhere else, but this particular path is by far the most common, plus the path to the dynamic linker is hardcoded into ELF binaries, so this works when at least the same as actually merging the 64-bit library with your software (assuming there is a corresponding libc anyway).



+3


source







All Articles