Thread overview
[Issue 9760] New: PIC code uses variable and thus needs a stack frame
Mar 19, 2013
Martin Nowak
Mar 20, 2013
Walter Bright
Mar 20, 2013
Martin Nowak
Mar 20, 2013
Walter Bright
[Issue 9760] asm docs should say PIC code uses variable and thus needs a stack frame
Mar 20, 2013
Don
Mar 20, 2013
Martin Nowak
March 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760

           Summary: PIC code uses variable and thus needs a stack frame
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: code@dawg.eu


--- Comment #0 from Martin Nowak <code@dawg.eu> 2013-03-19 13:47:58 PDT ---
cat > bug.d << CODE
void foo() {}

void bug()
{
    asm { naked; }
    foo();
    asm { ret; }
}

CODE

--------------------
dmd -c -m32 -fPIC bug.d
--------------------
_D3bug3bugFZv PROC NEAR
        call    ?_003                                   ; 0000 _ E8, 00000000

?_003   LABEL NEAR
        pop     eax                                     ; 0005 _ 58
        add     eax, offset _GLOBAL_OFFSET_TABLE_-$+1H  ; 0006 _ 05,
00000002(GOT r)
        mov     dword ptr [ebp-4H], eax                 ; 000B _ 89. 45, FC
        mov     ebx, dword ptr [ebp-4H]                 ; 000E _ 8B. 5D, FC
        call    _D3bug3fooFZv                           ; 0011 _ E8,
FFFFFFFC(PLT r)
        ret                                             ; 0016 _ C3
_D3bug3bugFZv ENDP

EBP isn't set up so storing the localgot will overwrite some other memory.
Not sure if we want to fix this because one might argue that non-asm is invalid
in naked asm blocks.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |INVALID


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2013-03-19 17:08:47 PDT ---
You're pretty much on your own with naked asm, that's the point of it!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760



--- Comment #2 from Martin Nowak <code@dawg.eu> 2013-03-19 23:17:05 PDT ---
The GOT loading code sequence doesn't work because I have no detailed control about the emitted relocations. This uses a R_386_GOT32 relocation instead of the needed R_386_GOTPC one.

----
extern(C) __gshared extern void* _GLOBAL_OFFSET_TABLE_;

void loadGOT()
{
    asm
    {
        naked;
        call Lgot;
    Lgot: pop EBX;
        add EBX, offsetof _GLOBAL_OFFSET_TABLE_ + 3;
    }
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760



--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2013-03-19 23:46:44 PDT ---
The inline assembler doesn't give access to the complete set of relocation types. For those, it's best not to use naked and let the compiler set it up for you.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |
            Summary|PIC code uses variable and  |asm docs should say PIC
                   |thus needs a stack frame    |code uses variable and thus
                   |                            |needs a stack frame


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2013-03-20 04:40:43 PDT ---
Reopening this as a spec bug. I think the docs for 'naked' should mention this, since it's not at all obvious that any variables are being used in the example code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9760



--- Comment #5 from Martin Nowak <code@dawg.eu> 2013-03-20 13:19:46 PDT ---
(In reply to comment #3)
> The inline assembler doesn't give access to the complete set of relocation types. For those, it's best not to use naked and let the compiler set it up for you.

BTW, this means I can't fix _trace_epi_n which gets called without saving
registers that belong to the callee.
Naked asm doesn't work because of the mentioned memory corruption and
the inability to load the GOT otherwise.
Normal asm doesn't work either, because the compiler trashes EAX when loading
the GOT.
I think the best solution would be to let the compiler do the regsave as it
does now for _c_trace_pro.

Also note that D doesn't have a possibility to mark a function local, i.e. C++'s static, which wouldn't require a GOT entry to call in the first place.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------