113

The final images produced by compliers contain both bin file and extended loader format ELf file ,what is the difference between the two , especially the utility of ELF file.

2
  • This is what NASM has to say. Not ARM specific, but likely to be the same concept. E.g., if you compile a file containing just NOP without -f (or -fbin), it compiles to a single byte 0x90, instead of a 400 byte ELF container with -felf32. So just the raw code, no container metadata. NASM says it is mostly used for MS-DOS .COM and .SYS files. section directives are mostly ignored and only generate alignment. Commented Apr 29, 2015 at 9:54
  • This is one way in which bin files can be useful: to make a boot sector to deploy operating systems: stackoverflow.com/a/32483545/895245 Commented Sep 17, 2015 at 17:57

6 Answers 6

114

A Bin file is a pure binary file with no memory fix-ups or relocations, more than likely it has explicit instructions to be loaded at a specific memory address. Whereas....

ELF files are Executable Linkable Format which consists of a symbol look-ups and relocatable table, that is, it can be loaded at any memory address by the kernel and automatically, all symbols used, are adjusted to the offset from that memory address where it was loaded into. Usually ELF files have a number of sections, such as 'data', 'text', 'bss', to name but a few...it is within those sections where the run-time can calculate where to adjust the symbol's memory references dynamically at run-time.

7
  • "more than likely it has explicit instructions to be loaded at a specific memory address": does this mean the bin file generation process adds additional code for loading data to specific address? Commented May 28, 2014 at 14:44
  • 2
    As far I has learned is the bin file is like running the program from offset 0 and the data segment is embedded within. If this is wrong please correct me. Commented Apr 9, 2015 at 1:26
  • 1
    @MartinKersten correct, bin files start from offset 0.
    – t0mm13b
    Commented Jun 1, 2015 at 0:37
  • 2
    @t0mm13b So .elf files can be burned onto a micro-controller just like a regular .hex file but it takes more flash memory, and every time the micro is reset, the sections addresses changes?
    – Aelgawad
    Commented Jan 16, 2016 at 20:17
  • 1
    @Aelgawad gdb will burn only the binary portions of the .elf onto a microcontroller with the 'load' command. It takes the same amount of flash, but with debug information for gdb on the host. The elf metadata also says where in the microcontroller's memory to load each section. Elf does not have to be relocatable, on a micro each section will be loaded at a fixed location.
    – joeforker
    Commented Jul 28, 2017 at 19:32
49

A bin file is just the bits and bytes that go into the rom or a particular address from which you will run the program. You can take this data and load it directly as is, you need to know what the base address is though as that is normally not in there.

An elf file contains the bin information but it is surrounded by lots of other information, possible debug info, symbols, can distinguish code from data within the binary. Allows for more than one chunk of binary data (when you dump one of these to a bin you get one big bin file with fill data to pad it to the next block). Tells you how much binary you have and how much bss data is there that wants to be initialised to zeros (gnu tools have problems creating bin files correctly).

The elf file format is a standard, arm publishes its enhancements/variations on the standard. I recommend everyone writes an elf parsing program to understand what is in there, dont bother with a library, it is quite simple to just use the information and structures in the spec. Helps to overcome gnu problems in general creating .bin files as well as debugging linker scripts and other things that can help to mess up your bin or elf output.

10
  • 1
    0x7C00 sounds like a bootloader thing which doesnt use elf necessarily. this is a generic question. an operating system would have rules for the (virtual) address spaces, the toolchain would need to be targetted at that operating systems rules, then the file format would indicate loadable items with addresses plus an entry point once loaded, plus other things. elf is just a container, like a box, you have to pack it right for the targetted use case.
    – old_timer
    Commented Jun 15, 2020 at 16:51
  • 1
    if you want to print some ascii to the vga, you write a program to do that which has some data or mathmatically generates the data on the fly or some combination, then you load that program into the operating system defined code space, and then run it. you dont generally shove data right into a physical peripheral, and its the rare operating system that would let you do that anyway, or allow its loader to do that.
    – old_timer
    Commented Jun 15, 2020 at 16:51
  • 1
    for bare metal, esp if this elf file is the bootloader and/or first program run, then the entry point and _start are not relevant as you use the elf file as a stepping stone to either a tool that programs the flash (like openocd over jtag) or through whatever-whatever-objcopy -O binary file.elf file.bin and then that file is somehow loaded into the flash. Not gone and tried a bootloader on x86 but assume that the bios cant parse elf files so it would need to be a memory image as well. so a -O binary type bin file
    – old_timer
    Commented Jun 16, 2020 at 4:06
  • 1
    the separate entity is the hardware/logic or other design. for operating systems then the operating system makes the rules, for a microcontroller the chip/processor design makes the rules. for example if there is a vector table and then the vectors point to the handlers you have to roll all of that into your linker script, etc so that the loadable data is destined for the flash that the thing boots off of.
    – old_timer
    Commented Jun 16, 2020 at 4:08
  • 1
    To broaden that your target has rules be it an operating system or a processor or a multi-stage bootloader, etc. And you need to build your "binary" based on those rules, the bootstrap and linkerscript being most important. Then it is very broad as to each target and how you apply that binary and what file formats are supported. Assuming gnu on a number of host development platforms the elf file format is the default output and then you use tools as needed (if the target specific utilities/loaders) to extract or convert from elf to something else.
    – old_timer
    Commented Jun 16, 2020 at 4:17
32

some resources:

  1. ELF for the ARM architecture
    http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
  2. ELF from wiki
    http://en.wikipedia.org/wiki/Executable_and_Linkable_Format

ELF format is generally the default output of compiling. if you use GNU tool chains, you can translate it to binary format by using objcopy, such as:

  arm-elf-objcopy -O binary [elf-input-file] [binary-output-file]

or using fromELF utility(built in most IDEs such as ADS though):

 fromelf -bin -o [binary-output-file] [elf-input-file]
2
  • 6
    This was added after the bin file detail was answered, and does add-on a practically useful technique. +1 for that.
    – erbdex
    Commented May 31, 2015 at 23:48
  • Does anyone maybee know if using arm-none-eabi-objcopy with parameter --strip-all is this the same as using it with -O binary? I think it is not. First one still produces an ELF file (stripped) while second produces binary with binary segments already mapped to appropprioate places.
    – 71GA
    Commented Oct 26, 2023 at 20:27
0

Pure binary is a sequence of executable opcodes to be executed from his 0 offset, ** but not only **.

Pure binaries are generally used on microcontrollers, bare metal programming and initial boots, where there is no "laoder" except the ROM loader.

Pure binaries includes .bss and .data, that from binaries that are read from flash memory as in a microcontroller, have to be relocated somewhere in ram. A binary file can include a vector table, symbol tables for "rel" or "rela" relocation (see -fPIC), got table and so on. All this additional data are obtained from the ELF->binary conversion, since the addresses of this specific tables are obtained from the ELF.

Another important point for the binary production are "linker scripts", they produce section offsets, and different addresses used in the absolute jumps depending of the declared sections.

Final note: pure binaries code (.text) is also often relocated to ram, since cpu internal sram is generally not too big. For this, such relocation tables comes into use.

0

I will ask another question,

What is the difference between cheese and rocks?

Well, there are many different types of cheese, some of which are hard as a rock.

TL;DR - the name 'binary' and 'elf' have lots of flexibility. Toolsets are also capable of producing 'SREC', ihex, etc. There is a continuum of features from 'raw binary' to the full elf specification.

The final images produced by compilers contain both bin file and extended loader format ELf file, what is the difference between the two , especially the utility of ELF file.

For many embedded systems, they do not have an ELF loader. In these cases, you must target a binary (and possibly amend it with loading code and/or structures).

For a hosted OS, it is common to want an ELF format. The ELF format can specify stack, bss and data needs and locations. It can also add facilities like C++ static constructors, debugging, tracing, exceptions and other features expected from modern OS's and toolsets.


A binary has no externally visible format (to the toolset). It often has a fixed start address which is often the first word. Often there is some ''hidden'' format. For instance, many embedded CPUs may have crypto-graphic offsets with-in a binary that are used to find digest, file sizes, etc. Another instance is some nRF chipsets with ROM code that does RF base band and application code that is loaded.

This brings up two related topics.

  1. Linking
  2. Loading

The difference between these tasks is when they occur; when building or at run-time. For instance, many binaries have 'init data' sections. This is static variables that have a 'boot' value. They are copied from a ROM/Flash section to the live RAM location. This is in essence a small loader.

An SREC and iHex are in-between an ELF and a binary. Data is tagged with address and you can have 'gaps' in data. This can be a benefit if code/data are separate and/or you have different memory targets. These concepts will seem foreign to anyone who doesn not do 'embedded' or 'firmware' programming.

The ELF file has a rich format for relocation and other notations (such as debug, frame layout, etc). These tables maybe needed at runtime. For instance, you can/should take '.exidx' and '.extab' data from an ELF into a binary, if you want to perform stack tracing at run time (in a binary image). You must arrange for your run time to use this information to trace a stack. In fact, most toolsets will allow you to put any/all ELF information in a binary.

Some of the elf information/sections does not make sense when loading. When different object files are processed (partial/full link), there are many 'references' that are resolved. If you have an OS, then it may resolve some of the references at load time (by a loader like ld.so) or use them for other purposes. As the compile phase often produces ELF objects, for later processing, many OS's will also have a loader that knows and understand the ELF format.

People with an ELF loader might produce a binary to compress or encrypt. This binary will then be included in an ELF file and a small 'loader' section will perform the decompress or decrypt step.

The benefit of ELF is, it is a documented format. So, with some work a new toolset can work with an OS that supports ELF. You have more interoperability and tool choices.

For a binary, it can support anything that ELF does, but you have to make all the infrastructure yourself and most people don't like re-inventing the wheel.

1
  • Also, stackoverflow.com/questions/25172834/… For an OS, you may want to have shared libraries with various features. The easiest is to use an ELF like format. The question gives some layout/ideas for statically linked shared libraries. Ie, address at build time, but used by multiple callers (maybe in a different context). Commented Apr 16 at 18:25
-1

I just want to correct a point here. ELF file is produced by the Linker, not the compiler.

The Compiler mission ends after producing the object files (*.o) out of the source code files. Linker links all .o files together and produces the ELF.

4
  • 3
    Downvoted because it's not answering the question nor necessarily correct. Broadly defined, compilation includes linking. Quoted from the ld documentation: Usually the last step in compiling a program is to run ld.
    – bzeaman
    Commented May 12, 2020 at 21:38
  • 2
    Downvoted because wrong info was given. .o are also ELF files, of type relocatable. There are other two types: executable and shared object. So yes, the assembler produces ELF files, as well as the linker does. Commented Apr 18, 2023 at 5:42
  • I wolud also like to add that also newer GCC compilers are able to link as well!
    – 71GA
    Commented Oct 26, 2023 at 20:24
  • Compilers have different phases. The 'link' phase, whether called directly from the compiler driver, or separately is completely different. A compiler front-end is different than the back-end (but both are part of a compiler binary). So, people have down-voted because of the ambiguity of human language. The posters point is correct; this tasks are decided by the link phase, no matter what your fingers type. Also noteworthy is a 'loader'; which is the run time variant of a linker. A loader maybe include in a binary (linux init code, uboot, etc). With LTO, the linker becomes a compiler. Commented Apr 9 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.