【发布时间】:2016-04-30 14:21:25
【问题描述】:
我正在尝试将 GNU as 用作与 nasm 类似的通用汇编程序。我制作了这样的模板源:
.section .text
.globl _start
.intel_syntax noprefix
_start:
call 0xb77431c0 # the instruction I want to assemble
然后我像这样运行 assemble 命令:
as --32 -o test.o test.s
ld -m elf_i386 -Ttext 0xb77431d9 --oformat binary -o test.bin test.o
在 binutils 2.24 中一切正常。但似乎 binutils 2.22(Ubuntu Precise 中的那个)中的 as 将 .text 部分与 4 字节边界对齐,因此我得到了错误的结果,而不是预期的反汇编:
# expected and working in binutils 2.24
$ ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9 E8E2FFFFFF call dword 0xb77431c0
# actual in binutils 2.22
$ ndisasm -b 32 -o 0xb77431d9 test.bin
B77431D9 90 nop
B77431DA 90 nop
B77431DB 90 nop
B77431DC E8DFFFFFFF call dword 0xb77431c0
问题出在as 命令中(即不是ld):readelf -S 给了我以下as 2.22 的结果:
$ readelf -S test.o | grep ' \.text'
[ 1] .text PROGBITS 00000000 000034 000005 00 AX 0 0 4
对于 2.24,我有
$ readelf -S test.o | grep ' \.text'
[ 1] .text PROGBITS 00000000 000034 000005 00 AX 0 0 1
所以问题确实是.text 部分的对齐。我尝试将.align 0 和.align 1 放在源代码的各个位置,但它并没有改变输出。
现在我的问题:如何在 GNU 汇编器中显式指定 ELF 目标的部分对齐方式?
【问题讨论】:
-
我真的希望这可以直接在程序集中进行,而不需要链接器脚本。很伤心:-(
标签: assembly elf memory-alignment gnu-assembler