NASM to GAS: Calling equ'd symbols

一世执手 提交于 2019-12-10 18:16:46

问题


I have some NASM files which have a line:

%INCLUDE "bmdev.asm"

The bmdev.asm file has equ directives such as:

b_print_newline         equ 0x0000000000100040

The files which include bmdev.asm then are able to call those items. I.e.

call b_print_newline

Is there a way to convert this to GAS? When I try to do the direct translation, i.e.

.set b_print_newline , 0x100040

call b_print_string

it doesn't appear to disassemble to the right thing:

callq  *0x100040

The NASM call disassembles to:

callq  0xfffffffffff00040

The goal here is to generate binaries for BareMetal OS via GAS instead of NASM.

The full disassembly of the binary which works:

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

test-nl.app:     file format binary


Disassembly of section .data:

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

The full disassembly of the binary which doesn't work:

$ objdump -D -b binary -m i386:x86-64 test-nl-a.app 

test-nl-a.app:     file format binary


Disassembly of section .data:

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

I've posted a (hopefully) clearer version of this question. Closing this one.


回答1:


It seems kind of odd that it should be so difficult to get AS to generate a relative call, perhaps someone smarter can jump in.

Anyway, doing:

.set b_print_newline, 0x100040
call b_print_newline

results in (as you noticed) AS generating an indirect jump.

You can fake it on your own using the . (dot) pseudo-variable:

call b_print_newline - .

Or you can use the linker (command line or script as you wish) to define symbols with the correct names, in which case the code gets compiled as you wish.

$ cat test.S
.section .text
.globl _start
_start:
    call b_print_newline
    ret
$ as --64 -o test.o test.S
$ ld --defsym b_print_newline=0x100040 -Ttext 200000 --oformat binary -o test.bin test.o
$ objdump -D -b binary -m i386:x86-64 test.bin
test.bin:     file format binary

Disassembly of section .data:

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

There is probably a way to make it work without using the linker to define symbols, but I haven't been able to find a way.




回答2:


I brought this issue up on the binutils mailing list. It appears to be a bug. It was suggested that I file a bug report and I've done so.

A fix has been checked in to the binutils tree.



来源:https://stackoverflow.com/questions/6830427/nasm-to-gas-calling-equd-symbols

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!