Last active
December 11, 2019 18:37
-
-
Save garettbass/16297b715ee2fb72c59db3181d4ec37a to your computer and use it in GitHub Desktop.
Enumerating section symbols with MSVC (https://rextester.com/TOUNK25484)
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
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 | |
#include <stdio.h> | |
#define CONCAT(A, B) _CONCAT(A, B) | |
#define _CONCAT(A, B) A##B | |
#define TOSTRING(...) _TOSTRING(__VA_ARGS__) | |
#define _TOSTRING(...) #__VA_ARGS__ | |
#define subsection_name(SECTION,SUBSECTION)\ | |
TOSTRING(CONCAT(CONCAT(SECTION,$),SUBSECTION)) | |
#ifdef SECTION_DEFINITIONS | |
#define _section_declaration_definition(SECTION) section_definition(SECTION) | |
#else | |
#define _section_declaration_definition(SECTION) | |
#endif | |
#define section_declaration(SECTION)\ | |
__pragma(section(subsection_name(SECTION,a),read,write))\ | |
__pragma(section(subsection_name(SECTION,f),read,write))\ | |
__pragma(section(subsection_name(SECTION,z),read,write))\ | |
void* CONCAT(SECTION,_begin)(void);\ | |
void* CONCAT(SECTION,_end)(void);\ | |
_section_declaration_definition(SECTION)\ | |
/**/ | |
#define section_definition(SECTION)\ | |
void* CONCAT(SECTION,_begin)(void) {\ | |
__declspec(allocate(subsection_name(SECTION,a)))\ | |
static void* CONCAT(__start_,SECTION) = TOSTRING(CONCAT(__start_,SECTION));\ | |
return &CONCAT(__start_,SECTION) + 1;\ | |
}\ | |
void* CONCAT(SECTION,_end)(void) {\ | |
__declspec(allocate(subsection_name(SECTION,z)))\ | |
static void* CONCAT(__stop_,SECTION) = TOSTRING(CONCAT(__stop_,SECTION));\ | |
return &CONCAT(__stop_,SECTION);\ | |
}\ | |
/**/ | |
section_declaration(mysection) // declaration may appear more than once | |
section_definition(mysection) // definition must appear only once, defines endpoints | |
#define section_entry(SECTION)\ | |
__declspec(allocate(subsection_name(SECTION,f))) | |
void print_mysection_entries(void) | |
{ | |
const void** const end = mysection_end(); | |
for (const void** itr = mysection_begin(); itr < end; ++itr) | |
{ | |
const void* str = *itr; | |
if (str) | |
printf("%p : \"%s\"\n", itr, (char*)str); | |
else | |
printf("%p : null\n", itr); | |
} | |
} | |
int main(void) | |
{ | |
section_entry(mysection) | |
static const char* mysection_0 = "mysection_0"; | |
section_entry(mysection) | |
static const char* mysection_1 = "mysection_1"; | |
section_entry(mysection) | |
static const char* mysection_2 = "mysection_2"; | |
print_mysection_entries(); | |
puts("DONE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment