Skip to content

Instantly share code, notes, and snippets.

@jgarvin
Created March 21, 2024 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarvin/10e74e17abb7df149d8a5de53efb5039 to your computer and use it in GitHub Desktop.
Save jgarvin/10e74e17abb7df149d8a5de53efb5039 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
typedef void (*test_function_t)();
// Why is `__attribute__((aligned(16)))` necessary? With the default
// alignment of 8, we print a large negative value for limit and then
// don't print any `a` fields.
struct __attribute__((aligned(16))) foo
{
const char* a;
uint64_t b;
test_function_t c;
};
void bar() {}
__attribute__((__section__("mysection")))
static const struct foo data_1 = {
.a = "hello",
.b = 1,
.c = bar
};
__attribute__((__section__("mysection")))
static const struct foo data_2 = {
.a = "world",
.b = 2,
.c = bar
};
int main()
{
extern struct foo __start_mysection;
extern struct foo __stop_mysection;
struct foo* foos = &__start_mysection;
ptrdiff_t limit = &__stop_mysection - &__start_mysection;
fprintf(stdout, "limit=%td\n", limit);
for(int i = 0; i < limit; ++i)
{
fprintf(stdout, "%s\n", foos[i].a);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment