Calling equ'd symbols in GAS

2019-08-02 08:25发布

问题:

Here's a small NASM program:

        [BITS 64]
        [ORG 0x0000000000200000]

        b_print_newline equ 0x0000000000100040

start:
        call b_print_newline

        ret

Assemble it:

$ nasm -f bin pr-nl-a.asm -o pr-nl-a.app

Disassemble it:

$ objdump -D -b binary -m i386:x86-64 pr-nl-a.app 
pr-nl-a.app:     file format binary


Disassembly of section .data:

0000000000000000 <.data>:
   0:   e8 3b 00 f0 ff          callq  0xfffffffffff00040
   5:   c3                      retq

Here's a GAS version:

        .set b_print_newline , 0x0000000000100040

        .text

        .global _start

_start:

        call b_print_newline

        ret

Assemble and link it:

$ as -o pr-nl-b.o pr-nl-b.s
$ ld -Ttext 200000 --oformat binary -o pr-nl-b.app pr-nl-b.o

Disassemble it:

$ objdump -D -b binary -m i386:x86-64 pr-nl-b.app 
pr-nl-b.app:     file format binary


Disassembly of section .data:

0000000000000000 <.data>:
   0:   ff 14 25 40 00 10 00    callq  *0x100040
   7:   c3                      retq

As you can see, the disassembled code differs slightly. The code for call in NASM:

0:  e8 3b 00 f0 ff          callq  0xfffffffffff00040

vs GAS:

0:  ff 14 25 40 00 10 00    callq  *0x100040

Any suggestions for how to implement the GAS version properly?

Here's the program in FASM:

        b_print_newline equ 0x0000000000100040

        use64
        org 0x0000000000200000

start:  call b_print_newline
        ret

It does the right thing:

$ objdump -D -b binary -m i386:x86-64 pr-nl-c.app 

pr-nl-c.app:     file format binary


Disassembly of section .data:

0000000000000000 <.data>:
   0:   e8 3b 00 f0 ff          callq  0xfffffffffff00040
   5:   c3                      retq

回答1:

Add ".org 0x0000000000200000" to the GAS file.