Target Triplet

From OSDev Wiki
Jump to navigation Jump to search

Target Triplets describe a platform on which code runs and are a core concept in the GNU build system. They contain three fields: the name of the CPU family/model, the vendor, and the operating system name. You can view the unambiguous target triplet for your current system by running:

gcc -dumpmachine

Structure

Target triplets have this simple structure:

machine-vendor-operatingsystem

For instance, a FreeBSD system could be written as:

x86_64-unknown-freebsd

Notice how the vendor field is mostly irrelevant and is usually 'pc' for 32-bit x86 systems or 'unknown' or 'none' for other systems. The simple three-field target triplet we have seen so far is unambiguous and easy to parse. However, since the vendor field is mostly unused, the GNU build system allows you to leave out the vendor field; the build system will automatically insert a default vendor part when it disambiguates your target triplet. For instance, this allows you to type:

x86_64-freebsd

The build system will then automatically deduce that the vendor is the default (unknown) if it wishes to know the unambiguous target triplet. Note that parsing target triplets are a bit more tricky, as sometimes the operating system field can be two fields:

x86_64-unknown-linux-gnu

This gets a bit worse since the vendor field can be left out:

x86_64-linux-gnu

This is most definitely ambiguous. Most autoconf-based packages ship with a huge shell script called config.sub whose function is to disambiguate target triplet using a long list of known CPUs and known operating systems.

Rationale

Target triplets are intended to be systematic unambiguous platform names (well, after disambiguation). They lets build systems understand exactly which system the code will run on and allows enabling platform-specific features automatically. In any compilation setting, there are usually three platforms involved (which might be the same three ones):

  • Build Platform: This is the platform on which the compilation tools are executed.
  • Host Platform: This is the platform on which the code will eventually run.
  • Target Platform: If this is a compiler, this is the platform that the compiler will generate code for.

This means that up to three differently-targeted compilers might be in play (if you are building a GCC on platform A, which will run on platform B, which produces executables for platform C). This problem is solved by simply prefixing the compilation tools with the target triplet. When you build a cross-compiler, the installed executables will be prefixed with the specified target triplet:

i686-elf-gcc

This prevents the wrong compiler from being used (and prevents things from the build machine to leak onto the target machine) if build systems are carefully to prefix all compilation tools with the target prefix.

Target Triplets for Operating Systems Development

For instance, if you develop your own operating system and modify GCC to add a new target triplet, yours could be:

x86_64-myos

However, when starting out you simply wish to use the generic targets that are well-suited for early OS development:

i686-elf
x86_64-elf
arm-none-eabi
aarch64-none-elf
riscv64-none-elf

These are bare targets meant for freestanding programs (bootloaders and kernels) that don't have a user-space.