Summary: PIC code not working: EBX register set incorrectly
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD

--- Comment #0 from Johannes Pfau <> 2010-08-05 02:30:57 
PDT ---
Mostly copy and pasting from an old post in the newsgroup. I tried to build
druntime as a shared library and encountered this bug. I tried to reduce it to
a simpler testcase, but that didn't work.

To reproduce: Download the druntime shared library makefile (so.mak) from this
site: (at the bottom of the page) and
compile druntime with it.
Compile this very simple test program, as described at the top of that page.
void main(){ }

The problem seems to be in the assembler code generated for main: :
extern (C) int main(int argc, char **argv)
(gdb) disassemble 0xb7f9f36c
Dump of assembler code for function main: #ebx=0xb7f16ff4 ebp=0xbffff0a8
   0xb7f9f338 <+0>:    push   %ebp
   0xb7f9f339 <+1>:    mov    %esp,%ebp
   0xb7f9f33b <+3>:    sub    $0x3c,%esp
   0xb7f9f33e <+6>:    push   %ebx                #ebx=0xb7f16ff4
   0xb7f9f33f <+7>:    mov    0xc(%ebp),%ebx     
   0xb7f9f342 <+10>:    push   %esi                #ebx=0xbffff154
   0xb7f9f343 <+11>:    push   %edi
   0xb7f9f344 <+12>:    call   0xb7f9f349 <main+17>
   0xb7f9f349 <+17>:    pop    %eax
   0xb7f9f34a <+18>:    add    $0x15343,%eax
   0xb7f9f34f <+23>:    mov    %eax,-0x38(%ebp)
   0xb7f9f352 <+26>:    movl   $0x0,-0x34(%ebp)
   0xb7f9f359 <+33>:    movl   $0x0,-0x30(%ebp)
   0xb7f9f360 <+40>:    movl   $0x0,-0x2c(%ebp)
   0xb7f9f367 <+47>:    call   0xb7f8813c <_STI_monitor_staticctor at plt>
(gdb) disassemble '_STI_monitor_staticctor at plt'
Dump of assembler code for function _STI_monitor_staticctor at plt:
   0xb7f8813c <+0>:    jmp    *0x2b4(%ebx) -->Segfault here
   0xb7f88142 <+6>:    push   $0x550
   0xb7f88147 <+11>:    jmp    0xb7f8768c
The problem is the ebx register. If I understood elf files correctly,
the ebx register must hold the address of the GOT when calling a PLT
entry. I guess when the main function is called by libc, ebx should be
set correctly, in this case to 0xb7f16ff4. I also guess the "push %ebx"
instruction is meant to save the GOT adress to stack, because ebx is
used for other stuff. But the ebx register is not restored to the GOT
address before calling <_STI_monitor_staticctor at plt> and therefore "*jmp
0x2b4(%ebx) " crashes. So this seems to be a problem with dmds PIC
support / -fPIC switch.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to