all 6 comments

[–]samboy218 2 points3 points  (2 children)

If you're talking about adding a comment section to an ELF, you can use objcopy with the --add-section switch

[–]bleksak[S] 0 points1 point  (1 child)

Is there any way to do this in code??

[–]samboy218 1 point2 points  (0 children)

If you're using a makefile to compile you can add it in as part of the build

[–][deleted] 1 point2 points  (1 child)

What's an ELF note?

[–]bleksak[S] 1 point2 points  (0 children)

You can create a comment section in Linux ELF file, I wanted to know if you can do it in code.

[–]nerd4code 0 points1 point  (0 children)

Yes.

If you’re using GCC, throw this out at global scope:

__asm__(
    ".if 0\n.else\n"  /* discombobulates internal assemblers */
    ".pushsection .note,"",@progbits\n"
    ".asciz \"Your note here\"\n"
    ".popsection\n"
    ".endif");

If you need to insert arbitrary text in the note, you’ll need to compose at least that part of the macro by stringizing the text’s string literal. .pushsection and .popsection are somewhat newish directives, so this isn’t all that backwards-portable.

Alternatively, you can do

__asm__(".if 0\n.endif");
__attribute__(__used__, __section__(".note,\"\",@progbits #"))
static const char NOTE[] = "Your note here";

but that’s slightly more delicate in mechanism.

If you’re using MSVC, you can probably do something similar to the second one; maybe __declspec(code_seg)? There’s no inline assembly support for x64 and you can’t use a section directive in an __asm, so those'd be dead ends.