NASM 到 GAS:调用 equ'd 符号

发布于 2024-11-26 02:17:54 字数 1310 浏览 0 评论 0原文

我有一些 NASM 文件,其中包含一行:

%INCLUDE "bmdev.asm"

bmdev.asm 文件具有 equ 指令,例如:

b_print_newline         equ 0x0000000000100040

包含 bmdev.asm 的文件然后能够调用这些项目。即

call b_print_newline

有没有办法将其转换为 GAS?当我尝试进行直接翻译时,即

.set b_print_newline , 0x100040

call b_print_string

它似乎没有反汇编为正确的内容:

callq  *0x100040

NASM 调用反汇编为:

callq  0xfffffffffff00040

这里的目标是为 BareMetal OS 通过 GAS 而不是 NASM。

有效的二进制文件的完整反汇编:

$ 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   

无效的二进制文件的完整反汇编:

$ 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-12-03 02:17:54

让 AS 生成相对调用是如此困难,这似乎有点奇怪,也许更聪明的人可以加入。

无论如何,这样做:

.set b_print_newline, 0x100040
call b_print_newline

会导致(正如您所注意到的)AS 生成间接跳转。

您可以使用 . 自行伪造它。 (dot) 伪变量:

call b_print_newline - .

或者您可以使用链接器(根据需要使用命令行或脚本)来定义具有正确名称的符号,在这种情况下,代码将根据您的意愿进行编译。

$ 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   

可能有一种方法可以让它在不使用链接器定义符号的情况下工作,但我还没有找到一种方法。

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.

清风挽心 2024-12-03 02:17:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文