www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4583] New: PIC code not working: EBX register set incorrectly

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583

           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
        AssignedTo: nobody puremagic.com
        ReportedBy: johannespfau gmail.com


--- Comment #0 from Johannes Pfau <johannespfau gmail.com> 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: http://www.curoles.com/j/dso/dso.html (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:
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #1 from Walter Bright <bugzilla digitalmars.com> 2010-08-05
14:20:51 PDT ---
I don't think EBX is required to pass between functions. Each function reloads
it as necessary.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 05 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #2 from Johannes Pfau <johannespfau gmail.com> 2010-08-06 02:22:55
PDT ---
Yes I know, but the problem occurs even before the called function is executed:
The PLT is a table containing executable code. If you do an position
independent function call, you call into this PLT code, not directly into your
target function. And these PLT instructions require EBX to be set to the GOT
address.

I strogly recommend reading
http://www.skyfree.org/linux/references/ELF_Format.pdf especially the section
about PLT, page 48 and page 49, I think the explanation there is very good.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 06 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #3 from Johannes Pfau <johannespfau gmail.com> 2010-08-06 03:18:32
PDT ---
Btw, that's different on x86_64 which uses "Instruction pointer relative data
access".

http://www.x86-64.org/documentation/abi.pdf
(I guess you already know this document, as you're implementing 64 bit support,
but just in case...)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 06 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |clugdbug yahoo.com.au
           Severity|major                       |critical


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 20 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #4 from Trass3r <mrmocool gmx.de> 2011-11-30 11:32:10 PST ---
Created an attachment (id=1047)
my Makefile adjustments

I tried it on x64:

$ make MODEL=64 -f posix.mak -j2
cc -c -m64 -O -fPIC src/core/stdc/errno.c -oobj/errno_c.o
cc -Wa,-noexecstack -c -m64 -O -fPIC src/core/threadasm.S -oobj/threadasm.o
cc -c -m64 -O -fPIC src/rt/complex.c -oobj/complex.o
...
dmd -c -oflib/ofdrt.o -m64 -O -fPIC -release -inline -nofloat -w -d -Isrc
-Iimport src/object_.d [......]
gcc -shared -Wl,-export-dynamic,-soname,lib/libdruntime.so.1 -o
lib/libdruntime.so.1.0.1 lib/ofdrt.o obj/errno_c.o obj/threadasm.o
obj/complex.o
/usr/bin/ld: lib/ofdrt.o: relocation R_X86_64_PC32 against symbol
`_Dmodule_ref' can not be used when making a shared object; recompile with
-fPIC
/usr/bin/ld: final link failed: Bad value

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 30 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2011-11-30
14:31:30 PST ---
(In reply to comment #2)
 Yes I know, but the problem occurs even before the called function is executed:
 The PLT is a table containing executable code. If you do an position
 independent function call, you call into this PLT code, not directly into your
 target function. And these PLT instructions require EBX to be set to the GOT
 address.
You're right. DMD doesn't do this at the moment. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 30 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #6 from Walter Bright <bugzilla digitalmars.com> 2011-12-01
10:53:36 PST ---
https://github.com/D-Programming-Language/dmd/commit/887dda0ba2439ca4dbeec38b0434377ba831cf40

https://github.com/D-Programming-Language/dmd/commit/70904844f652f53d80b3f379060638fe91811721

This addresses setting EBX before the function call, not any other issues.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 01 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #7 from Walter Bright <bugzilla digitalmars.com> 2012-04-27
23:34:02 PDT ---
(In reply to comment #6)
 This addresses setting EBX before the function call, not any other issues.
Still, a couple of the runtime compiler helper functions pass arguments in EBX. This still needs fixing. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #8 from github-bugzilla puremagic.com 2012-05-04 23:00:44 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/2a5385345c17a65f8280efab1674c23bde3df68e
fix Issue 4583 - PIC code not working: EBX register set incorrectly

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #9 from github-bugzilla puremagic.com 2012-05-04 23:01:16 PDT ---
Commit pushed to phobos-1.x at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/df21e384a4207e6f888b5abed0f7b3298a2d0320
fix Issue 4583 - PIC code not working: EBX register set incorrectly

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #10 from github-bugzilla puremagic.com 2012-05-04 23:02:33 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/39c0a6ec5e2d1ae412d5d60834feb8ab610b090e
fix Issue 4583 - PIC code not working: EBX register set incorrectly

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583



--- Comment #11 from github-bugzilla puremagic.com 2012-05-04 23:02:48 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/ca53f96c09be934c51b2ea1d91c277639181cfec
fix Issue 4583 - PIC code not working: EBX register set incorrectly

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4583


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


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