How do executables use POSIX to keep compatibility between different UNIX systems?

In my understanding, POSIX only specifies a set of APIs that the OS needs to provide, but it doesn’t specify the implementation detail, specifically the assembly level compatibility. For example, on x86, you need to use a syscall to get the POSIX API:

1. set `eax` to the syscall number
2. set syscall arguments
3. call int 0x80

But this process can vary a lot depending on:

  1. UNIX OS: different OS can have a different mapping from syscall number to POSIX API
  2. architecture: X86/arm have different instructions to call int 0x80

So I think that POSIX does its job by keeping the API conversion for the POSIX library in different OS. e.g.

The POSIX API is provided as glibc symbols. Every time the executable calls the POSIX API, it finds the symbol in every OS’s and there is no need to call int 0x80 directly in the executable.

So my question is:

  1. is my understanding correct?
  2. besides, are there any other libraries which act like but in different UNIX OS?

POSIX doesn’t specify binary interfaces, so you can’t build a “POSIX” binary and run it on different POSIX-style operating systems. POSIX doesn’t even care about system calls; it defines functions, and it doesn’t matter where those functions end up implemented.

In practice, on Linux and other POSIX-style systems, the functions are implemented in a “C library”, as you mention. But there isn’t even a POSIX-defined way for binaries to call those functions; POSIX for C programs is mostly relevant when writing and compiling programs, and when considering the behaviour of system interfaces at runtime, but not as far as how those interfaces are accessed. The c99 command to run a compiler is specified, so being able to take C source and turn it into a binary is a requirement in POSIX, but the details of that binary are system-specific. (Binary interfaces on Linux follow the specification known as the System V ABI, with various architecture-specific extensions.)

There are other C libraries. Each platform provides its own, and some platforms have several. On Linux, there is the GNU C library, but also musl.

Note too that the C library isn’t only used for C programs; on POSIX-style systems, most compilers and interpreters end up using it. (Notable exceptions include Go binaries which implement their own interfaces to the kernel user-space API.)

Answered By: Stephen Kitt
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.