Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ELF sections for attaching stringy metadata

Tags:

linux

gcc

elf

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?

like image 673
PSkocik Avatar asked Aug 10 '26 07:08

PSkocik


1 Answers

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.

like image 128
PSkocik Avatar answered Aug 13 '26 07:08

PSkocik