I am trying to add metadata to an ELF executable by storing lines in a special section ( __dlog). The current approach uses (abuses?) The built-in assembly to store strings and almost works as desired.
#include <stdio.h>
#include <stdlib.h>
#define DLOG(proto...) \
__asm__(".pushsection __dlog, \"S\", @note\n\t" \
".asciz \"" __FILE__ ":function_name_here:" #proto "\"\n\t" \
".popsection\n\t" )
int
foo(int bar)
{
int baz = bar / 2;
DLOG(int baz);
return baz;
}
int
main(int argc, char *argv[])
{
foo(argc);
return EXIT_SUCCESS;
}
But ideally, the macro should automatically include the function name as part of the string using the identifier __func__. The end result should be a string
file.c:foo:int baz\0
in a section called __dlog. But since __func__it is not a string literal, gcc complains about this code
".asciz \"" __FILE__ ":" __func__ ":" #proto "\"\n\t"
Is it possible to add the contents of a string __func__to a string? Bonus points if the solution does not require custom assembly options or post-processing steps. The compiler is gcc 4.4 and 4.5.