I would like to be able to associate some stringy, easily-extractible key-value pairs with my ELF executables.
I've noticed gcc-compiled ELFs have a .comment section
$ readelf -p .comment a.out
So I tried to replicate that:
main.c:
#include <stdio.h>
int main(){ puts("Hello world"); return 0; }
const char str[] __attribute__((section(".meta"))) = "hello meta world";
Test:
$ readelf -p .meta a.out
String dump of section '.meta':
[ 0] hello meta world
or
$ readelf -p .meta a.out | sed -n 's/^[^]]*] //p'
hello meta world
This works.
Is there a better way to attach such stringly key-value pairs to an ELF file (without breaking it down to multiple files)? Is there a namespace (e.g. .user.meta) for user sections that would prevent me from breaking something?
According to the spec, all dot-prefixed section names are reserved and user sections without the dot prefix are safe to use:
Section names with a dot ( . ) prefix are reserved for the system, although applications may use these sections if their existing meanings are satisfactory. Applications may use names without the prefix to avoid conflicts with system sections. The object file format lets one define sections not in the list above. An object file may have more than one section with the same name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With