简体   繁体   中英

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

I am distributing a 32bit build of a piece of software. I want to detect at runtime whether the userland it's running under is 64bit, and capable of running 64bit executable binaries (ELF 64-bit x86-64).

This isn't the same problem as detecting at compile-time whether an architecture is 32bit or 64bit.

I would ideally like to do this in the most portable way possible, so would prefer to avoid doing things like file $(which init) | grep x86-64 file $(which init) | grep x86-64 which depend on

I don't think reading cpuid is a solution either - it's possible to have a 32bit OS running on a 64bit CPU.

Detecting a 64bit cpu running in long mode doesn't seem to be a solution either, nor does detecting whether the kernel itself is 64bit, as there is the possibility that the OS is a 32bit userland with a 64bit kernel (as is possible with some Debian configurations ).

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

One possible robust solution I can think of is to actually include and attempt to call a 64bit ELF binary and see if it runs or not, but this doesn't feel like a very efficient way to do it. Is there a linux function or something in the stl or boost that could help me find out reliably?

Ironically, for once the equivalent problem is very easy to solve under Windows .

You could test for the presence of /lib64/ld-linux-x86-64.so.2 . Theoretically this doesn't always work because it's possible for a Linux system to 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 at least as well as actually bundling a 64-bit library with your software (provided there's a matching libc, anyway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM