[dmd-internals] Building the runtime as a dynamic library on Linux
Johannes Pfau
johannespfau at googlemail.com
Sun Jul 4 05:18:47 PDT 2010
On 30.06.2010 22:16, Jacob wrote:
> I've been working on building the runtime (actually the Tango runtime, but that's almost the same as druntime) as a dynamic library on Linux. After getting around some problems I've come as far as this person has: http://www.curoles.com/j/dso/dso.html
>
> Now I have the question: is this segmentation fault (shown in the link above) a bug in the compiler or in the runtime?
>
I spent some more time looking into this, and I don't think the runtime could cause this.
The problem seems to be in the assembler code generated for main:
http://www.dsource.org/projects/druntime/browser/trunk/src/rt/dmain2.d :
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.
But there's another problem:
--------------------------------------
(gdb) x/1x 0xb7f16ff4+0x2b4
0xb7f172a8: 0x00000001 ---> should be 0xb7f88142?
--------------------------------------
Using gdb to look at the correct GOT entry address, it's content is
0x00000001. But as far as I know it should point to the push instruction
in the PLT entry(here: 0xb7f88142) or to _STI_monitor_staticctor.
Something is wrong here as well.
Walter, any chance you could please look into this?
--
Johannes Pfau
More information about the dmd-internals
mailing list