-
-
Save martijnthe/0adeb2bd71e592544dde63bb002df550 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#define OUTPUT_SOMEWHERE(...) \ | |
printf(__VA_ARGS__); | |
typedef struct | |
{ | |
size_t something; | |
size_t something_else; | |
size_t third_something; | |
} section_array_member; | |
extern section_array_member __start_struct_array[] __attribute__((weak)); | |
extern section_array_member __stop_struct_array[] __attribute__((weak)); | |
static const section_array_member x __attribute__((used)) __attribute__((section("struct_array"), aligned(1))) = | |
{ | |
.something = 0x0000000000000000, | |
.something_else = 0xffffffffffffffff, | |
.third_something = 0xffffffffffffffff | |
}; | |
//static int x_index = (&x - __start_struct_array); | |
static const section_array_member y __attribute__((used)) __attribute__((section("struct_array"), aligned(1))) = | |
{ | |
.something = 0xffffffffffffffff, | |
.something_else = 0xffffffffffffffff, | |
.third_something = 0xffffffffffffffff | |
}; | |
//static int y_index = (&y - __start_struct_array); | |
static void section_function1 (const char *name); | |
static void (*__static_global_section_function1) (const char *) | |
__attribute__((used)) __attribute__((section("function_pointer_array"))) = section_function1; | |
static void section_function1 (const char *name) {int x = 5; OUTPUT_SOMEWHERE("%d\n", x);} | |
static void section_function2 (const char *name); | |
static void (*__static_global_section_function2) (const char *) | |
__attribute__((used)) __attribute__((section("function_pointer_array"))) = section_function2; | |
static void section_function2 (const char *name) {OUTPUT_SOMEWHERE("xyz\n");} | |
typedef void (*section_function_pointer)(const char *); | |
extern section_array_member __start_struct_array[] __attribute__((weak)); | |
extern section_array_member __stop_struct_array[] __attribute__((weak)); | |
extern section_function_pointer __start_function_pointer_array[] __attribute__((weak)); | |
extern section_function_pointer __stop_function_pointer_array[] __attribute__((weak)); | |
static void dump_memory(char *prefix, unsigned char *ptr, size_t size) | |
{ | |
size_t index; | |
OUTPUT_SOMEWHERE("%s:\n", prefix); | |
for (index = 0; index < size; index++) { | |
OUTPUT_SOMEWHERE("%02x ", ptr[index]); | |
if (!((index + 1) % 8)) { | |
OUTPUT_SOMEWHERE("| "); | |
} | |
if (!((index + 1) % 32)) { | |
OUTPUT_SOMEWHERE("\n"); | |
} | |
} | |
OUTPUT_SOMEWHERE("\n"); | |
} | |
static int get_index(const section_array_member *member) { | |
return member - __start_struct_array; | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
OUTPUT_SOMEWHERE("*** section 'struct_array' ***\n"); | |
OUTPUT_SOMEWHERE("Local variables:\n"); | |
OUTPUT_SOMEWHERE("x_index: %d\n", get_index(&x)); | |
OUTPUT_SOMEWHERE("y_index: %d\n", get_index(&y)); | |
dump_memory("x", (unsigned char *)(&x), sizeof(section_array_member)); | |
dump_memory("y", (unsigned char *)(&y), sizeof(section_array_member)); | |
dump_memory("Section contents", (unsigned char *)__start_struct_array, | |
(size_t) (((unsigned char *)__stop_struct_array) - ((unsigned char *)__start_struct_array))); | |
OUTPUT_SOMEWHERE("*** section 'function_pointer_array' ***\n"); | |
OUTPUT_SOMEWHERE("Local varaiables:\n"); | |
dump_memory("section_function1", (unsigned char *)(§ion_function1), sizeof(section_function_pointer *)); | |
dump_memory("section_function2", (unsigned char *)(§ion_function2), sizeof(section_function_pointer *)); | |
dump_memory("Section contents", (unsigned char *)__start_function_pointer_array, | |
(size_t) (((unsigned char *)__stop_function_pointer_array) - ((unsigned char *)__start_function_pointer_array))); | |
OUTPUT_SOMEWHERE("Section contents as pointers:\n"); | |
for (int index = 0; &__start_function_pointer_array[index] < __stop_function_pointer_array; index++) | |
{ | |
dump_memory("function pointer", (unsigned char *)(&__start_function_pointer_array[index]), sizeof (section_function_pointer *)); | |
} | |
OUTPUT_SOMEWHERE("Result of calling the functions:\n"); | |
section_function1(NULL); | |
section_function2(NULL); | |
__start_function_pointer_array[0](NULL); | |
__start_function_pointer_array[1](NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment