[Nasm-bugs] [Bug 3392348] New: nasm 2.12.01 generates bad pc-relative relocations for macho32 target

no-reply at bugzilla-nasm.gorcunov.org no-reply at bugzilla-nasm.gorcunov.org
Fri Apr 15 11:05:03 PDT 2016


http://bugzilla.nasm.us/show_bug.cgi?id=3392348

            Bug ID: 3392348
           Summary: nasm 2.12.01 generates bad pc-relative relocations for
                    macho32 target
           Product: NASM
           Version: 2.12.xx
          Hardware: Macintosh
                OS: Mac OS
            Status: OPEN
          Severity: normal
          Priority: Medium
         Component: Assembler
          Assignee: nobody at nasm.us
          Reporter: zenith432 at users.sourceforge.net
                CC: gorcunov at gmail.com, hpa at zytor.com, nasm-bugs at nasm.us

Here's how pc-relative relocation are generated by clang

==== code (C)
#include <stdio.h>

extern int foo(void);

int main(int argc, char** argv)
{
    printf("%d\n", foo());
}
==== end code

complied with "clang -arch i386 -c -o main.o main.c"
main.o disassembled with 'otool -jtV main.o' result
==== disasm
main.o:
(__TEXT,__text) section
_main:
00000000    55                  pushl    %ebp
00000001    89e5                movl    %esp, %ebp
00000003    83ec18              subl    $0x18, %esp
00000006    e800000000          calll    0xb
0000000b    58                  popl    %eax
0000000c    8b4d0c              movl    0xc(%ebp), %ecx
0000000f    8b5508              movl    0x8(%ebp), %edx
00000012    8955fc              movl    %edx, -0x4(%ebp)
00000015    894df8              movl    %ecx, -0x8(%ebp)
00000018    8945f4              movl    %eax, -0xc(%ebp)
0000001b    e8e0ffffff          calll    _foo  <----- Note this!
00000020    8b4df4              movl    -0xc(%ebp), %ecx
00000023    8d9136000000        leal    65-11(%ecx), %edx
00000029    891424              movl    %edx, _main(%esp)
0000002c    89442404            movl    %eax, 0x4(%esp)
00000030    e8cbffffff          calll    _printf  <----- Note this!
00000035    31c9                xorl    %ecx, %ecx
00000037    8945f0              movl    %eax, -0x10(%ebp)
0000003a    89c8                movl    %ecx, %eax
0000003c    83c418              addl    $0x18, %esp
0000003f    5d                  popl    %ebp
00000040    c3                  retl
==== end disasm

The pc-relative relocations for the external symbols have the values -0x20 and
-0x35 plugged into the instruction - corresponding to their pc post-instruction
value of 0x20 and 0x35.

This is what nasm does...
==== nasm source

extern _foo

mov eax, 3
mov ebx, 4
add ebx, eax
call _foo
mov edx, 5
==== end nasm source

assembled with 'nasm -f macho32 -o bar.o bar.nasm', and then disassembled with
otool gives

==== disasm
bar.o:
(__TEXT,__text) section
00000000    b803000000          movl    $0x3, %eax
00000005    bb04000000          movl    $0x4, %ebx
0000000a    01c3                addl    %eax, %ebx
0000000c    e800000000          calll    _foo+17  <---- relocation wrong!
00000011    ba05000000          movl    $0x5, %edx
==== end disasm

nasm just plugs in zero at the pc-relative reloc, which ends up producing the
wrong offset by ld.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
You are watching all bug changes.


More information about the Nasm-bugs mailing list