Add Custom Section to Binary File on Linux

Add Custom Section to Binary File on Linux

When deploying compiled programs on Linux systems, embedding structured metadata directly into the executable can be useful for tracking build versions, release identifiers, or internal tags. Instead of storing such data externally, a dedicated section can be inserted into the executable itself. This approach allows version inspection without executing the program. This tutorial shows how to add custom section to binary file on Linux.

Most Linux executables follow the ELF (Executable and Linkable Format) structure. ELF files are divided into sections, each serving a specific purpose. Custom sections can be appended without affecting program execution when properly configured.

First, create a file containing the information that will be embedded. For example, to store an application version:

printf '1.0.0' > version.txt

The objcopy utility, included in the Binutils package, supports adding new sections to ELF binaries. To append a section named .version to a binary called myapp, run:

objcopy --add-section .version=version.txt --set-section-flags .version=noload,readonly myapp

The provided section flags ensures the section is marked as non-loadable and read-only, preventing it from being mapped into memory during execution. This modification embeds metadata without changing runtime behavior.

After modification, confirm that the .version section exists and contains the expected data:

readelf -p .version myapp

Output:

String dump of section '.version':
  [     0]  1.0.0

Leave a Comment

Cancel reply

Your email address will not be published.