This is an example for Linux ELF:
module test;
import std::io;
extern const String __START_MYSECTION @cname("__start_mysection");
extern const String __STOP_MYSECTION @cname("__stop_mysection");
fn void main() {
for (String* r = &__START_MYSECTION; r < &__STOP_MYSECTION; r++) {
io::printfn("Got %s", *r);
}
// The @nostrip doesn't do anything here
// Note that if you put these in global scope, they don't get stripped (only for this example though!)
const String STRING1 @section("mysection") @nostrip = "Hello";
const String STRING2 @section("mysection") @nostrip = "World";
// Uncomment this: Section exists, program links fine
// io::printfn("%s %s", STRING1, STRING2);
}
Run with c3c build --optsize=none && readelf -S build/test | grep mysection
Program linked to executable 'build/test'.
[29] mysection PROGBITS 0000000000047a50 00047a50
Run with c3c build --optsize=small
/usr/bin/ld: build/obj/linux-x64/test.o: in function `test.main':
/tmp/test/src/main.c3:8:(.text+0xb): undefined reference to `__start_mysection'
/usr/bin/ld: /tmp/test/src/main.c3:8:(.text+0x12): undefined reference to `__stop_mysection'
/usr/bin/ld: /tmp/test/src/main.c3:(.text+0x2a): undefined reference to `__start_mysection'
/usr/bin/ld: /tmp/test/src/main.c3:8:(.text+0x76): undefined reference to `__stop_mysection'
/usr/bin/ld: build/obj/linux-x64/test.o:(.debug_info+0x38): undefined reference to `__start_mysection'
/usr/bin/ld: build/obj/linux-x64/test.o:(.debug_info+0x99): undefined reference to `__stop_mysection'
collect2: error: ld returned 1 exit status
And readelf -S build/test | grep mysection does not show the section.
I need the constants to always be placed in the section in order to make some string interning optimizations possible. I'd be okay with having to add @nostrip, but maybe @section should already imply this, since adding @section is already quite intentional.
This is an example for Linux ELF:
Run with
c3c build --optsize=none && readelf -S build/test | grep mysectionRun with
c3c build --optsize=smallAnd
readelf -S build/test | grep mysectiondoes not show the section.I need the constants to always be placed in the section in order to make some string interning optimizations possible. I'd be okay with having to add
@nostrip, but maybe@sectionshould already imply this, since adding@sectionis already quite intentional.