[Ping] Port of VTV for Cygwin and MinGW

Patrick Wollgast patrick.wollgast@rub.de
Thu Oct 16 10:23:00 GMT 2014


On 09.10.2014 16:42, Kai Tietz wrote:
>> Not approved:
>> * gcc/cp/vtable-class-hierarchy.c
> 
> Index: gcc/cp/vtable-class-hierarchy.c
> ===================================================================
> --- gcc/cp/vtable-class-hierarchy.c    (Revision 214408)
> +++ gcc/cp/vtable-class-hierarchy.c    (Arbeitskopie)
> @@ -1182,7 +1182,7 @@ vtv_generate_init_routine (void)
>        TREE_STATIC (vtv_fndecl) = 1;
>        TREE_USED (vtv_fndecl) = 1;
>        DECL_PRESERVE_P (vtv_fndecl) = 1;
> -      if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
> +      if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
> 
> You need to check that TARGET_PECOFF is defined.  Otherwise you break
> compilation for none i386 targets.
> 
>          DECL_STATIC_CONSTRUCTOR (vtv_fndecl) = 0;
> 
>        gimplify_function_tree (vtv_fndecl);
> @@ -1190,7 +1190,7 @@ vtv_generate_init_routine (void)
> 
>        cgraph_process_new_functions ();
> 
> -      if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
> +      if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
> 
> See above. Likewise
>          assemble_vtv_preinit_initializer (vtv_fndecl);
> 
>      }
> 
> 
>> * gcc/varasm.c
> 
> Index: gcc/varasm.c
> ===================================================================
> --- gcc/varasm.c    (Revision 214408)
> +++ gcc/varasm.c    (Arbeitskopie)
> @@ -2165,6 +2165,33 @@ assemble_variable (tree decl, int top_le
>                               DECL_NAME (decl));
>            in_section = sect;
>  #else
> +          /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
> +             Therefore the following check is used.
> +             In case a the target is PE or COFF a comdat group section
> +             is created, e.g. .vtable_map_vars$foo. The linker places
> +             everything in .vtable_map_vars at the end.
> +
> +             A fix could be made in
> +             gcc/config/i386/winnt.c: i386_pe_unique_section. */
> +          if (TARGET_PECOFF)
> 
> You need to test, if TARGET_PECOFF is defined!
> 
> +          {
> +            char *name;
> +
> +            if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
> +              name = ACONCAT ((sect->named.name, "$",
> +               IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
> +            else
> +              name = ACONCAT ((sect->named.name, "$",
> +               IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
> +               NULL));
> +
> +            targetm.asm_out.named_section (name,
> +             sect->named.common.flags
> +                   | SECTION_LINKONCE,
> 
> Here it seems to me that you have some whitespace issues,
> 
> +                       DECL_NAME (decl));
> +            in_section = sect;
> +        }
> +        else
>            switch_to_section (sect);
>  #endif
> 

This has been changed in the attached patch.

> 
>> * libgcc/Makefile.in
> 
> Looks ok to me.
> 
>> * libgcc/config.host
> 
> Looks fine to me, too.
> 
>> * libiberty/obstack.c
> 
> Why you use instead of C-runtime exit/abort-functions the
> platform-functions to terminate the process.  This looks to me like
> useless change.  For cygwin this might be even wrong in some aspects.
> What is the reasoning for this change?
> 

I haven't encountered crashes in obstack.c itself, but there were
problems in vtv_rts.cc with abort() on MinGW 32bit. The following stack
traces were taken at MinGW 32bit. Most of the time the process had to be
stopped in the process manager because a wrong process handle was passed
to NtTerminateProcess. This was tested and occurred on Windows 7 64bit
and Windows 8.1 64bit. To be sure to avoid this issue the calls have
also been exchanged in obstack.c.

With abort(), correct process handle, postmortem debugger triggert.

ffffffff 00000003 0028fe98 ntdll!NtTerminateProcess+0xc
00000003 77e8f3b0 ffffffff ntdll!RtlExitUserProcess+0x6d
00000003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
00000003 11e9bfd9 00409000 msvcrt!exit+0x32
00000003 00000001 00000000 msvcrt!flushall+0x2e9
00000003 00010001 00000065 msvcrt!exit+0x11
6efcf294 00000080 0028ffcc msvcrt!abort+0xf3
00560f70 0000000d 00000001 libvtv_0!Z14__fortify_failPKc+0x18
7ffde000 0028ffdc 77568f8b test_std+0x13de
7ffde000 138a1dee 00000000 KERNEL32!BaseThreadInitThunk+0xe
ffffffff 7755dad3 00000000 ntdll!__RtlUserThreadStart+0x20
004014e0 7ffde000 00000000 ntdll!_RtlUserThreadStart+0x1b

With abort(), wrong process handle, NtTerminateProcess returns instead
of ending the own process. This case happens most of the time.

00000000 00000003 0028fe98 ntdll!NtTerminateProcess+0x5
00000003 77e8f3b0 ffffffff ntdll!RtlExitUserProcess+0x35
00000003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
00000003 9f9f5ea3 00409000 msvcrt!exit+0x32
00000003 00000001 00000000 msvcrt!flushall+0x2e9
00000003 00010001 00000040 msvcrt!exit+0x11
6efcf294 00000080 0028ffcc msvcrt!abort+0xf3
00701060 0000001e 00000001 libvtv_0!Z14__fortify_failPKc+0x18
7ffde000 0028ffdc 77568f8b image00400000+0x13de
7ffde000 9defd39c 00000000 KERNEL32!BaseThreadInitThunk+0xe
ffffffff 7755dac5 00000000 ntdll!__RtlUserThreadStart+0x20
004014e0 7ffde000 00000000 ntdll!_RtlUserThreadStart+0x1b

TerminateProcess. Everything's fine on MinGW x86, x86-64 (both gcc 5.0),
Cygwin x86-64 (gcc 4.9.0).

ffffffff 0000022f 0028feb8 ntdll!NtTerminateProcess+0x5
ffffffff 0000022f 00409000 KERNELBASE!TerminateProcess+0x27
00731060 0000001e 00000001 libvtv_0!Z14__fortify_failPKc+0x2a
7ffde000 0028ffdc 77568f8b image00400000+0x13de
7ffde000 e2645ec6 00000000 KERNEL32!BaseThreadInitThunk+0xe
ffffffff 7755dae7 00000000 ntdll!__RtlUserThreadStart+0x20
004014e0 7ffde000 00000000 ntdll!_RtlUserThreadStart+0x1b

> Another note I have about re-implementation of mprotect in ---
> libvtv/vtv_malloc.cc.  Why you need that?  it is already part of
> libgcc for mingw.  And for cygwin this function is part of cygwin's
> library itself.  So why re-implementing it here?
> 

It is already part of libgcc for MinGW, but it can neither be found in
the exports of the dll, nor can the function prototype be found in any
header files. Therefore I get unknown reference errors if I don't
re-implement it. I checked the exports of several compilations for this.

* self compiled MinGW
* MinGW installed from the Arch Linux repositories
*
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_x86_64-linux_20131228.tar.bz2/download

If I'm missing something here let me know.

-------------- next part --------------
* gcc/config/i386/cygwin.h (STARTFILE_SPEC): Add vtv_start.o,
    if -fvtable-verify=preinit/std is used.
* gcc/config/i386/mingw-w64.h (STARTFILE_SPEC): Likewise.
* gcc/config/i386/mingw32.h (STARTFILE_SPEC): Likewise.
* gcc/config/i386/cygwin.h (ENDFILE_SPEC): Add vtv_end.o,
    if -fvtable-verify=preinit/std is used.
* gcc/config/i386/mingw32.h (ENDFILE_SPEC): Likewise.
* gcc/config/i386/cygwin.h (LIB_SPEC): Pass -lvtv and -lpsapi,
    if -fvtable-verify=preinit/std is used.
* gcc/config/i386/mingw-w64.h (LIB_SPEC): Likewise.
* gcc/config/i386/mingw32.h (LIB_SPEC): Likewise.

* gcc/cp/vtable-class-hierarchy.c (vtv_generate_init_routine): Add
    check for not TARGET_PECOFF at the VTV_PREINIT_PRIORITY checks.

* gcc/varasm.c (assemble_variable): Add code to properly set the comdat
    section and name for the .vtable_map_vars section in case the
    target is PE or COFF.


* libgcc/Makefile.in: Move rules to build vtv_*.o out of the check
    for CUSTOM_CRTSTUFF.
* libgcc/config.host (i[34567]86-*-cygwin*, x86_64-*-cygwin*, i[34567]86-*-mingw*)
    (x86_64-*-mingw*): Only add vtv_*.o to extra_parts if enable_vtable_verify.


* libiberty/obstack.c: Include windows.h on Cygwin and MinGW.
    Exchange abort with TerminateProcess on Cygwin and MinGW.
    Exchange exit with ExitProcess on Cygwin and MinGW.


* libstdc++-v3/acinclude.m4: Define VTV_CYGMIN.
* libstdc++-v3/configure: Regenerate.

* libstdc++-v3/libsupc++/Makefile.am: Add vtv_sources only to
    libsupc___la_SOURCES and libsupc__convenience_la_SOURCES if VTV_CYGMIN is
    not set.
* libstdc++-v3/libsupc++/Makefile.in: Regenerated.
* libstdc++-v3/libsupc++/vtv_stubs.cc: Add none weak declaration of every
    function for Cygwin and MinGW.

* libstdc++-v3/src/Makefile.am: Add libvtv.la to toolexeclib_LTLIBRARIES,
    if VTV_CYGMIN is set. Define libvtv_la_SOURCES, libvtv_la_LDFLAGS,
    libvtv_la_AM_CXXFLAGS and libvtv_la_LINK if VTV_CYGMIN is set.
* libstdc++-v3/src/Makefile.in: Regenerate.


* libvtv/Makefile.am : Add libvtv.la to toolexeclib_LTLIBRARIES, if VTV_CYGMIN
    is set. Define libvtv_la_LIBADD, libvtv_la_LDFLAGS, libvtv_stubs_la_LDFLAGS
    and libvtv_stubs_la_SOURCES if VTV_CYGMIN is set. Add obstac.c to
    libvtv_la_SOURCES if VTV_CYGMIN is set.
* libvtv/Makefile.in : Regenerate.
* libvtv/aclocal.m4 : Regenerate.
* libvtv/configure : Regenerate.
* libvtv/configure.ac : Add ACX_LT_HOST_FLAGS. Define VTV_CYGMIN.
* libvtv/configure.tgt : (x86_64-*-cygwin*, i?86-*-cygwin*, x86_64-*-mingw*)
    (i?86-*-mingw*): Add to supported targets.
* libvtv/vtv_fail.cc : Skip inclusion of execinfo.h on Cygwin and MinGW.
(log_error_message): Skip calls to backtrace and backtrace_symbols_fd on Cygwin
    and MinGW.
* libvtv/vtv_malloc.cc : Include windows.h and skip sys/mman.h inclusion on
    Cygwin and MinGW. Add sysconf and mprotect port on Cygwin and MinGW.
(obstack_chunk_alloc): Exchange call to mmap with call to VirtualAlloc on Cygwin
    and MinGW.
(__vtv_malloc_init): Exchange call to sysconf with call to port of sysconf on
    Cygwin and MinGW.
* libvtv/vtv_malloc.h : Declare mprotect and define PROT_READ and PROT_WRITE on
    Cygwin and MinGW.
* libvtv/map.h : Include stdint.h on MinGW.
* libvtv/rts.cc : Include windows.h, winternl.h and psapi.h, skip include of
    execinfo.h, sys/mman.h and link.h on Cygwin and MinGW.
    Add port of __fortify_fail on Cygwin and MinGW.
    Change ElfW (Addr) to uintptr_t on Cygwin and MinGW.
(read_section_offset_and_length): Add port for Cygwin and MinGW
(iterate_modules): New function.
(vtv_unprotect_vtable_vars): Use iterate_modules instead of dl_iterate_phdr on
    Cygwin and MinGW.
(vtv_protect_vtable_vars): Likewise.
(count_all_pages): Likewise.
(dl_iterate_phdr_count_pages): Don't build on Cygwin and MinGW.
* libvtv/utils.cc : Include windows.h and skip execinfo.h inclusion on
    Cygwin and MinGW.
(__vtv_open_log): Exchange call to getuid and getpid with GetCurrentProcessId and
    adjust call to snprintf accordingly on Cygwin and MinGW.
    Adjust calls to mkdir on MinGW.
    Adjust call to open on Cygwin and MinGW.
(__vtv_add_to_log): Adjust call to snprintf on Cygwin and MinGW.
(__vtv_log_verification_failure): Don't generate a backtrace on Cygwin and MinGW.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vtv_cygmin.patch
Type: text/x-patch
Size: 39179 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20141016/79f064df/attachment.bin>


More information about the Gcc-patches mailing list