[cfe-dev] Porting libcxxabi / Unwind to Windows / MingW 32 bit

Yaron Keren yaron.keren at gmail.com
Fri Nov 1 15:11:56 PDT 2013


Yes, it's nice when the OS does the work.

With MingW, I see a problem using our unwind library.

__EH_FRAME_BEGIN__ is a local symbol with no external visibility
outside the libgcc DLL. There is no API exposing it either.
Only code compiled inside libgcc can access it.

One solution is to (re)place our unwind library inside gcclib
itself so our unwind gets EH frame from initialization just
like gcc unwind does. Another would be to add API to libgcc exposing it.
Both are unlikely to happen.

The right solution would be to have a full replacement to libgcc based on
our
unwind code and use it instead. In addition to unwinding library, libgcc
also
supplies conversion and arithmetic functions so we'll need alternative
versions
of these too.

Yaron


2013/11/1 Nick Kledzik <kledzik at apple.com>

> On Oct 30, 2013, at 9:41 PM, Yaron Keren <yaron.keren at gmail.com> wrote:
>
> I see. It is different than the gcc runtime libgcc.
>
> libgcc always uses the set of FDE which was registered by
> __register_frame. This data is private to every process since unwind runs
> in user space as part of the process.
>
> Here, the OS first locates which set of FDE is relevant.
> Does this mean that in OS-X unwind runs in system space and holds FDE data
> for all processes?
>
> On OSX, to to find the FDE for a given pc, libunwind does the equivalent
> of dladdr() to find the base address of the DSO (or program) containing
> that pc.  Then it (calls a function) which walks the mach-o data structures
> from the base address to find the __eh_frame section.  There is no up front
> registration (__register_frame) of each __eh_frame section, it is done
> lazily.
>
> -Nick
>
>
> 2013/10/31 Nick Kledzik <kledzik at apple.com>
>
>>
>> On Oct 30, 2013, at 3:22 PM, Yaron Keren <yaron.keren at gmail.com> wrote:
>>
>> Hi,
>>
>> You are exactly correct, LocalAddressSpace::findUnwindSections does
>> nothing #if !APPLE so the output value is random.
>>
>> LocalAddressSpace::findOtherFDE and LocalAddressSpace::findFunctionName
>> also currently do nothing. Are these also required for regular exceptions?
>>
>> No.
>>
>> findOtherFDE() is only need if the OS has an alternate way to locate FDEs
>> such as those for JITed code.
>>
>> findFunctionNam() is used in debug builds to print out the name of
>> functions as the unwinding happens.
>>
>> -Nick
>>
>>
>>
>> 2013/10/31 Nick Kledzik <kledzik at apple.com>
>>
>>> Yaron,
>>>
>>> Each separately linked image (program, DLLs, etc) has its own set of
>>> FDEs (in its .eh_frame) section.  The method
>>> LocalAddressSpace::findUnwindSections() needs to be updated in an OS
>>> specific way to: 1) find the image for a given pc value, and 2) find the
>>> .eh_frame section bounds for that image.
>>>
>>> -Nick
>>>
>>>
>>> On Oct 30, 2013, at 3:03 PM, Yaron Keren <yaron.keren at gmail.com> wrote:
>>>
>>> Hi Nick,
>>>
>>> I looked at the disassembly, unw_getcontext() gets the correct IP into
>>> the context and later setInfoBasedOnIPRegister gets it right into "pc".
>>> This is OK.
>>>
>>> The trouble is, the whole address space is nowhere near the fde values.
>>> For example,
>>>
>>> p =          0x66179070
>>> <libunwind::DwarfFDECache<libunwind::LocalAddressSpace>::_initialBuffer>
>>> pc =         0x6604e7d9
>>> fde =         0x40801c0
>>> p->ip_start = 0x4080010
>>> p->ip_end =   0x408010c
>>>
>>> p is consistent with pc and local vars and addresses in unwind.dll
>>> (0x66YYYYYY)
>>> fde is consistent with the ip ranges and with the caller of unwind.dll
>>> (the throwing function) address space (0x40800YY)
>>>
>>> they live in different memory ranges, the fde addresses are in caller
>>> range whereas the unwind addresses are in the DLL range so when it tries to
>>> find its IP in the fde tables it can't find.
>>>
>>> It's as if the DLL is supposed to load in other memory range, some
>>> linker option required?
>>>
>>> Yaron
>>>
>>>
>>>
>>> 2013/10/29 Nick Kledzik <kledzik at apple.com>
>>>
>>>>
>>>> On Oct 29, 2013, at 12:11 AM, Yaron Keren <yaron.keren at gmail.com>
>>>> wrote:
>>>>
>>>> > Hello,
>>>> >
>>>> > I'm trying to make libcxxabi / Unwind work on Windows / MingW 32 bit.
>>>> Attached is a patch to make it compile without warnings and run without
>>>> crashing.
>>>> >
>>>> > However the functionality isn't working yet: upon exception, the IP
>>>> seems to be completely out of range the FDE tables. This may be related to
>>>> the memory model / compilation options???
>>>> Take a look at UnwindRegisterRestore.s.  When an exception is thrown,
>>>> unw_getcontext() is called which saves all registers and the return address
>>>> into a register struct.  Then the FDE info is used to step through each
>>>> frame and modify the register set.  At which step is the IP register out of
>>>> bounds?
>>>>
>>>> It might be that the C files need to be compiled with -fexceptions so
>>>> that they have unwind tables.
>>>>
>>>> -Nick
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131102/f274e282/attachment.html>


More information about the cfe-dev mailing list