'gcc linker options "--format binary"?

What is the meaning/usage of '--format binary' options in below command ?.

ld -m elf_x86_64  --format binary  --oformat elf64-x86-64 -r stub -o stub-image.o


Solution 1:[1]

The -format binary option states that the input file, in this case named stub, is a raw binary blob of data.

The command you show takes this 'blob' and wraps it up in an elf file, similar to other objects created by the compiler, and suitable for linking into a program. This sort of trick is also useful if you have a ROM-programming tool, for example, that expects elf data rather than raw binaries.

The blob is placed in the .data section, and three symbols (a.k.a. variables) are created for you:

  • _binary_stub_start
  • _binary_stub_end
  • _binary_stub_size

If you link stub-image.o with a C program, in the usual way, then you can access the data like this (you can choose whatever pointer type is appropriate):

extern char *binary_stub_start;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ams