8 ms·
ELF files on Linux
- vectorEQ 8y agonice article but wish people would elaborate more on relocations instead of always skipping that. it's a very important part of understanding how ELF works when it's executed.
- cmrdporcupine 8y agoAbsolutely, I once wrote an ELF loader for the Atari ST (never finished, but almost), and the documentation on relocation was absolutely arcane.
- jcranmer 8y agoRelocations boil down to this: A relocation entry contains a location of the patch, the symbol to use in relocation, an optional addend, and a type. The type tells you how to compute the relocation and is completely defined by the processor-specific ABI. The simple relocations boil down to "add the addend to the address of the symbol, subtract the address of the relocation, and store it as signed N-bit number". There are more complex relocations that involve things such as symbol sizes, TLS relocations, or the GOT and the PLT.
- haberman 8y agoI wrote Bloaty (https://github.com/google/bloaty https://github.com/google/bloaty) which involved writing a totally custom ELF file parser. Here are some epiphanies I had about ELF while writing it. ELF (and Mach-O, PE, etc) are designed to optimize the creation of a process image. The runtime loader mainly just has to mmap() a bunch of file ranges into memory with various permissions. This is quite different than loading a .jar file, .pyc, etc. which involve building a runtime heap and loading objects into that heap. ELF has two file-level tables: sections and segments (the latter are also called program headers). Things clicked for me when I realized: sections are for the linker and segments are for the runtime loader. Sections are the atomic unit of data that linkers operate on: the linker will never rearrange data within a section (it may concatenate several input sections into a single output section though). The loader doesn't even look at the section table AFAIK, everything needed to load the binary is put into segments / program headers. Only some parts of the binary are actually read/loaded when the binary is executed. Debugging info may bloat the binary but it doesn't cost any RAM at runtime because it's never loaded unless you run a debugger. Bloaty makes this clear by showing both VM size and file size: https://github.com/google/bloaty#running-bloaty https://github.com/google/bloaty#running-bloaty
- monocasa 8y agoI just want to say thanks for bloaty. I've used it all the way from 100MB backend server programs, to deeply embedded, bare metal STM32F apps measured in KB.
- haberman 8y agoThat makes me really happy to hear. I'm glad Bloaty is useful for you!
- AceJohnny2 8y agoI've been wondering about qualitative differences between Mach-O and ELF, after hearing a Mach developer trash the ELF format, but I don't know enough about either to comment. Do you have any insight?
- monocasa 8y agoNot haberman, but I've written loaders for both Mach-O and ELF, and really prefer ELF. ELF is mainly structured like descriptive tables of how the relevant pieces look in memory; Mach-O is more structured like a script of commands that you run to load the binary. There's a couple places where the model breaks down for ELF, DWARF and GNU_STACK both feel more Mach-O, but if you're playing with binaries for non standard uses, ELF just feels a lot cleaner IMO. I'd love to hear the Mach developer's arguments though.
- jcranmer 8y ago
- zimbatm 8y agoIf you ever need to tweak or inspect an existing binary, https://github.com/NixOS/patchelf https://github.com/NixOS/patchelf is great.
- royragsdale 8y agolief - Library to Instrument Executable Formats https://lief.quarkslab.com/ https://lief.quarkslab.com/ is another great programmatic option
- mzs 8y ago/usr/ccs/bin/dump on solaris is great for shell scripts too.
- kccqzy 8y agoOne thing that's on my mind but haven't been able to spend time investigating is the fact that on my machine (Ubuntu 19.04), almost all distribution-installed executables are not ELF executables per se, but ELF shared objects. Running `file /bin/ls` shows that it's an ELF 64-bit LSB shared object. Running `readelf -h /bin/ls` also says that the type is DYN. Is the executable type basically deprecated now?
- jzwinck 8y agoPosition Independant Executable (PIE) files are detected as shared libraries because they use the same old identifier as position independent shared libraries. The ELF folks could have added a new type but did not, leading to some confusion like this: https://bugs.launchpad.net/ubuntu/+source/shared-mime-info/+bug/1639531 https://bugs.launchpad.net/ubuntu/+source/shared-mime-info/+... PIE is a security feature, which is why it has proliferated on newer systems. See https://access.redhat.com/blogs/766093/posts/1975793 https://access.redhat.com/blogs/766093/posts/1975793
- usr1106 8y agoHere is an example to try and see yourself: $ cat show-addr.c #include <stdio.h> int main(int argc, char **argv) { printf("main() is at %p\n", main); } $ gcc -o show-addr show-addr.c $ file show-addr show-addr: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=60c8b61a7040adccc90934bc79e24342eecae15a, not stripped $ ./show-addr main() is at 0x400526 $ ./show-addr main() is at 0x400526 $ gcc -pie -fPIC -o show-addr.pie show-addr.c $ file show-addr.pie show-addr.pie: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=73ae8065a65aad4b829567b7ce3464fb5d1e3fc3, not stripped $ ./show-addr.pie main() is at 0x55c722ae1750 $ ./show-addr.pie main() is at 0x5649bf97b750 $ (edit: incomplete copy-paste fixed)
- saagarjha 8y agoA somewhat lesser known but (IMO) fun fact is that if you compile your executable with -export-dynamic, you can dlopen it and dlsym for functions inside of it just like you would with any other shared library (note that if your goal is to just load the executable, such as if it already has a constructor defined, you don't even need this flag).
- saagarjha 8y ago> For those who love to read actual source code, have a look at a documented ELF structure header file from Apple. Surely the Linux kernel would be a better source?