in reply to Re: (OT) How can I do unless(caller) { main; } in C using GCC in Windows?
in thread (OT) How can I do unless(caller) { main; } in C using GCC in Windows?

That's a fair question.

Perlmonks is most likely to understand what unless(caller) { main; } does; that's been the largest barrier to communication so far. I basically want to replicate this snippet in as many programming languages as possible, because I prefer my software to work well in shell script environments.

Strawberry Perl's GCC compiler happens to be the locus of the problem, because it does not recognize the same macros that GCC does in an ordinary Unix environment. Any Perl monks familiar with Strawberry Perl's internals could help with this issue.

  • Comment on Re^2: (OT) How can I do unless(caller) { main; } in C using GCC in Windows?
  • Download Code

Replies are listed 'Best First'.
Re^3: (OT) How can I do unless(caller) { main; } in C using GCC in Windows?
by Anonymous Monk on Sep 12, 2012 at 22:57 UTC
    So you're talking about Weak symbol?

    win32 has "entry points", default is main, then WinMain, but if gcc weak symbol thing is hiding it as entry point, they you have to specify via some gcc switches what the entry point is

    what gcc is probably doing is renaming the symbol to something not main ( or _main )

    Find out what it is, then use  gcc ... -e _the_new_main

    To find out , compile the .c file to .o, I don't remember what switch to use, -O/-c, something like that. Then use objdump -x ...o to see the symbols

    Strawberry Perl's GCC compiler happens to be the locus of the problem, because it does not recognize the same macros that GCC does in an ordinary Unix environment. Any Perl monks familiar with Strawberry Perl's internals could help with this issue.

    There are no strawberry perl internals, they're the same as regular perl

      Thanks for your help! I'm able to create the .o objects and objdump their contents, but I don't know how to look in the dumps to find entry points.

      C:\> gcc -c scriptedmain.c scriptedmain.h C:\> gcc -c test.c scriptedmain.c scriptedmain.h C:\> objdump -x scriptedmain.o scriptedmain.o: file format pe-i386 scriptedmain.o architecture: i386, flags 0x00000039: HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS start address 0x00000000 Characteristics 0x104 line numbers stripped 32 bit words Time/Date Wed Dec 31 19:00:00 1969 Magic 0000 MajorLinkerVersion 0 MinorLinkerVersion 0 SizeOfCode 00000000 SizeOfInitializedData 00000000 SizeOfUninitializedData 00000000 AddressOfEntryPoint 00000000 BaseOfCode 00000000 BaseOfData 00000000 ImageBase 00000000 SectionAlignment 00000000 FileAlignment 00000000 MajorOSystemVersion 0 MinorOSystemVersion 0 MajorImageVersion 0 MinorImageVersion 0 MajorSubsystemVersion 0 MinorSubsystemVersion 0 Win32Version 00000000 SizeOfImage 00000000 SizeOfHeaders 00000000 CheckSum 00000000 Subsystem 00000000 (unspecified) DllCharacteristics 00000000 SizeOfStackReserve 00000000 SizeOfStackCommit 00000000 SizeOfHeapReserve 00000000 SizeOfHeapCommit 00000000 LoaderFlags 00000000 NumberOfRvaAndSizes 00000000 The Data Directory Entry 0 00000000 00000000 Export Directory [.edata (or where ever we f +ound it)] Entry 1 00000000 00000000 Import Directory [parts of .idata] Entry 2 00000000 00000000 Resource Directory [.rsrc] Entry 3 00000000 00000000 Exception Directory [.pdata] Entry 4 00000000 00000000 Security Directory Entry 5 00000000 00000000 Base Relocation Directory [.reloc] Entry 6 00000000 00000000 Debug Directory Entry 7 00000000 00000000 Description Directory Entry 8 00000000 00000000 Special Directory Entry 9 00000000 00000000 Thread Storage Directory [.tls] Entry a 00000000 00000000 Load Configuration Directory Entry b 00000000 00000000 Bound Import Directory Entry c 00000000 00000000 Import Address Table Directory Entry d 00000000 00000000 Delay Import Directory Entry e 00000000 00000000 CLR Runtime Header Entry f 00000000 00000000 Reserved Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000034 00000000 00000000 000000b4 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .rdata 00000024 00000000 00000000 000000e8 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 scriptedmain +.c File [ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x00000000 _meaning_of_ +life AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0 [ 4](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text AUX scnlen 0x34 nreloc 3 nlnno 0 [ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 8](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 10](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .rdata AUX scnlen 0x21 nreloc 0 nlnno 0 [ 12](sec 1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x0000000a .weak._main. +_meaning_of_life [ 13](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 ___main [ 14](sec 0)(fl 0x00)(ty 20)(scl 105) (nx 1) 0x00000000 _main AUX lnno 1 size 0x0 tagndx 12 [ 16](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _printf RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 00000014 DISP32 ___main 00000024 dir32 .rdata 00000029 DISP32 _printf C:\> objdump -x test.o test.o: file format pe-i386 test.o architecture: i386, flags 0x00000039: HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS start address 0x00000000 Characteristics 0x104 line numbers stripped 32 bit words Time/Date Wed Dec 31 19:00:00 1969 Magic 0000 MajorLinkerVersion 0 MinorLinkerVersion 0 SizeOfCode 00000000 SizeOfInitializedData 00000000 SizeOfUninitializedData 00000000 AddressOfEntryPoint 00000000 BaseOfCode 00000000 BaseOfData 00000000 ImageBase 00000000 SectionAlignment 00000000 FileAlignment 00000000 MajorOSystemVersion 0 MinorOSystemVersion 0 MajorImageVersion 0 MinorImageVersion 0 MajorSubsystemVersion 0 MinorSubsystemVersion 0 Win32Version 00000000 SizeOfImage 00000000 SizeOfHeaders 00000000 CheckSum 00000000 Subsystem 00000000 (unspecified) DllCharacteristics 00000000 SizeOfStackReserve 00000000 SizeOfStackCommit 00000000 SizeOfHeapReserve 00000000 SizeOfHeapCommit 00000000 LoaderFlags 00000000 NumberOfRvaAndSizes 00000000 The Data Directory Entry 0 00000000 00000000 Export Directory [.edata (or where ever we f +ound it)] Entry 1 00000000 00000000 Import Directory [parts of .idata] Entry 2 00000000 00000000 Resource Directory [.rsrc] Entry 3 00000000 00000000 Exception Directory [.pdata] Entry 4 00000000 00000000 Security Directory Entry 5 00000000 00000000 Base Relocation Directory [.reloc] Entry 6 00000000 00000000 Debug Directory Entry 7 00000000 00000000 Description Directory Entry 8 00000000 00000000 Special Directory Entry 9 00000000 00000000 Thread Storage Directory [.tls] Entry a 00000000 00000000 Load Configuration Directory Entry b 00000000 00000000 Bound Import Directory Entry c 00000000 00000000 Import Address Table Directory Entry d 00000000 00000000 Delay Import Directory Entry e 00000000 00000000 CLR Runtime Header Entry f 00000000 00000000 Reserved Sections: Idx Name Size VMA LMA File off Algn 0 .text 0000002c 00000000 00000000 000000b4 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .rdata 00000024 00000000 00000000 000000e0 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 test.c File [ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _main [ 3](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text AUX scnlen 0x2a nreloc 4 nlnno 0 [ 5](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 7](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 9](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .rdata AUX scnlen 0x21 nreloc 0 nlnno 0 [ 11](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x00000000 ___main AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0 [ 13](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _meaning_of_ +life [ 14](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 _printf RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 0000000a DISP32 ___main 0000000f DISP32 _meaning_of_life 0000001a dir32 .rdata 0000001f DISP32 _printf

      If I'm reading these correctly, both seem to contain the entry points _main and __main. At least the offsets differ, but compiling scriptedmain by referring to the entry point offset doesn't work.

      C:\Users\andrew\Desktop\src\scriptedmain\c>gcc -o scriptedmain -e00000 +014 scriptedmain.c scriptedmain.h c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.4.3/../../../../i686 +-w64-mingw32/lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): In functi +on `main': /opt/W64_156151-src.32/build-crt/../mingw-w64-crt/crt/crt0_c.c:18: und +efined reference to `WinMain@16' collect2: ld returned 1 exit status

        Ok, so whatever that is, its a bug in libmingw32

        Report bugs to mingw folks :)

        I tried

        gcc -o scriptedmain scriptedmain.c -s -Wl,-subsystem,console -nostdl +ib

        Which avoid libmingw32 but this fails with

        undefined reference to `___main' undefined reference to `_printf' undefined reference to `_printf'

        Naturally -e ___main doesn't help anything

        $ gcc -o scriptedmain scriptedmain.c -s -Wl,-subsystem,console -nost +dlib -e ___main c:/mingw/bin/../lib/gcc/mingw32/4.7.0/../../../../mingw32/bin/ld.exe: +warning: cannot find entry symbol ___main; defaul ing to 00401000

        So however you're supposed to work it, ask the mingw folks

      I found a solution that works in Windows and Unix, using simple preprocessor instructions.

      http://stackoverflow.com/a/12397886/350106

        :p you didn't know about that? I thought you wanted to exploit the compilers built-in mechanisms