Custom Executable Sections in C

It is possible to define custom executable sections in C with pre-defined names. This is particularly useful for low-level coding and application development.

The data can then be read at runtime and used as needed, for example, as an embedded executable payload (x86/x64 instructions), bytecode for a custom language (i.e., MRuby) or just data.

An example of such a C program is below.

// custom_section.c
#include <stdio.h>

// Use section name WITHOUT leading dot (matches your successful "foo" example)
__attribute__((section("mycust")))
const char custom_data[] = "This is data embedded in a custom section!\n";

__attribute__((section("mycust")))
const int custom_value = 12345;

// Linker-generated symbols (no __asm needed on your setup)
extern char __start_mycust;
extern char __stop_mycust;

int main() {
    printf("Custom value: %d\n", custom_value);
    printf("%s", custom_data);

    printf("Section contents (as chars):\n");
    for (char* p = &__start_mycust; p < &__stop_mycust; ++p) {
        if (*p >= 32 && *p <= 126) {
            putchar(*p);
        }
    }
    printf("\n");

    return 0;
}

It can be compiled with the following.

gcc custom_section.c -o cust_section.exe -Wall -g

We can then check the sections exist.

objdump -h cust_section.exe

This can also be replicated on Windows – very easily using Mingw – and checked with a suitable PE file viewer, such as PEView.