2

I developed bootstrap software to start my game. I did this with Go. It was especially important for me to be cross-platform. Also, I didn't want to divide the download links into two as x86 / x64. I wanted to handle everything in one output. That's why I had to compile to x86. When I do this, I cannot properly detect that the operating system is x86 or x64.

In a software compiled as x86, how can i properly detect operating system x86 or x64 (in Go).

This code is not correct when compiled as x86.

const is64Bit = uint64(^uintptr(0)) == ^uint64(0)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
fladon
  • 65
  • 6
  • 4
    You may need to actually ask the OS, in which case it will of course depend what OS you're using. – Peter Cordes Feb 18 '21 at 06:44
  • I don't want information for a specific operating system. Compiled program will be cross-platform, it should be able to work on all operating systems. – fladon Feb 18 '21 at 07:31
  • Different OSes use different executable formats; it's not like you could have a single binary that you could run on a GNU/Linux system, or a Mac, or Windows. It's certainly possible that you could use the same portable detection code to choose between two other executables both built for the current OS (e.g. some library that handles OS differences), but you should be prepared in case that turns out not to be possible. – Peter Cordes Feb 18 '21 at 07:36
  • 1
    AFAIK, in x86 asm [there's no way to tell](https://stackoverflow.com/questions/32668558/how-can-an-x86-application-detect-that-its-running-in-amd64-compatibility-mode) (by just running user-space asm instructions, no syscalls) whether you're in 32-bit protected mode (under a "legacy mode" 32-bit OS) or in 32-bit compat mode (sub-mode of long mode under a 64-bit OS: https://en.wikipedia.org/wiki/X86-64#Operating_modes). – Peter Cordes Feb 18 '21 at 07:39
  • @PeterCordes Then should I solve this problem by putting two separate download links as x64 x32 bit on the website? For example, how did discord solve this problem? Discord has only one download link. https://discord.com/download – fladon Feb 18 '21 at 08:38
  • It has 4 different download links, one each for iOS, Android, Mac, and Windows. But yes, there's only one download for each OS. For the Windows download for example, they either built a dumb 32-bit only executable, or they probably used Windows-specific ways to detect bitness. (e.g. as part of the Windows installer to detect once at install time, or they could in theory do what you're suggesting and write a "launcher" that has to run every time.) – Peter Cordes Feb 18 '21 at 08:45
  • I understand everything better, thank you. – fladon Feb 18 '21 at 09:45

1 Answers1

1

On Windows you can call IsWow64Process to determine whether you are a 32-bit process running on a 64-bit OS. Note that it returns false if you are a 64-bit process running on a 64-bit OS, but if you have a 64-bit process running, then you know the OS is 64-bit or it wouldn't run.

Also note that 32-bit Windows is considered obsolete. Microsoft is already phasing out support for 32-bit Windows - they no longer want it to be installed on new computers.

On Linux you can call uname and look at the machine field. Here's a list of possible values. Note that most of them won't be compatible with your program, only i386, i686 and x86_64.

user253751
  • 57,427
  • 7
  • 48
  • 90