如何在64位Linux上使用Gas(“as”)组装32位二进制文件?

5

我如何在64位Linux上使用Gas('as')将源代码汇编为32位二进制文件?

这是为了遵循32位教程而不必更改所有指针和大量指令为四重字的麻烦。

谢谢,

Chris。

P.S. 我可以轻松地在C中完成这个问题...

chris@chris-linux-desktop:~$ cat test.c
#include "stdio.h"

int main() {
    printf("hello world");
    return 0;
}

chris@chris-linux-desktop:~$ gcc test.c -o test64
chris@chris-linux-desktop:~$ gcc -m32 test.c -o test32
chris@chris-linux-desktop:~$ file test32
test32: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
chris@chris-linux-desktop:~$ file test64
test64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

请使用-m32命令在gcc中添加32位编译选项。 - user786653
2个回答

6

使用选项"--32"和as,例如:

as --32 source.s -o objectfile

或者你可以直接使用gcc来汇编和链接一个汇编源文件。gcc通过文件扩展名识别它。

gcc -m32 source.s -o executable


谢谢,我太糊涂了 - 它在手册页中... 目标i386选项: [--32 | --64] [-n] [-march = CPU [+ EXTENSION ...]] [-mtune = CPU] - fadedbee

2

您可能还需要使用链接器的-m选项来为不同目标架构设置仿真链接文件。 ld --help提供了可能的仿真值列表。

ld -m elf_i386 -o file file.o file2.o ...etc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接