6

我正在尝试生成 16 位 DOS 可执行文件,但使用 gcc 编译器。所以我使用的是古老的 gcc-4.3 ia16 端口。我为我的构建制作了一个 Docker 映像:https ://registry.hub.docker.com/u/ysangkok/ia16-gcc-rask

这是我正在尝试的:

host $ mkdir results
host $ docker run -v $PWD/results:/results -it ysangkok/ia16-gcc-rask
container $ cd results

我不包含标头,因为 gcc 不能使用 OpenWatcom 的 libc 标头。

container $ echo 'main() { printf("lol"); }' > test.c

我没有链接,因为我没有可用的 16 位 binutils。如果我构建一个目标文件,它没有正确标记为 16 位。

container $ /trunk/build-ia16-master/prefix/bin/ia16-unknown-elf-gcc -S test.c

现在我有这个程序集文件:

    .arch i8086,jumps
    .code16
    .att_syntax prefix
#NO_APP
    .section    .rodata
.LC0:
    .string "lol"
    .text
    .p2align    1
    .global main
    .type   main, @function
main:
    pushw   %bp
    movw    %sp,    %bp
    subw    $4, %sp
    call    __main
    movw    $.LC0,  %ax
    pushw   %ax
    call    printf
    addw    $2, %sp
    movw    %bp,    %sp
    popw    %bp
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.3.0 20070829 (experimental)"

在容器之外,在主机中,我尝试用 yasm 组装它:

 % yasm -m x86 -p gas -f elf -o test.o test.s  
test.s:1: warning: directive `.arch' not recognized
test.s:3: error: junk at end of line, first unrecognized character is `p'

yasm看不懂,我把语法行注释掉,再试一次,这次成功了。

我测试重定位符号:

 % objdump -r test.o

test.o:     file format elf32-i386

RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE 
00000007 R_386_PC16        __main
0000000a R_386_16          .rodata
0000000e R_386_PC16        printf

可悲的是,它们是 32 位的。当我尝试在容器中进行链接时,它不起作用:

root@1341f35c4590:/# cd ow/binl/
root@1341f35c4590:/ow/binl# WATCOM=/ow /ow/binl/wlink 
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
Press CTRL/D to finish
WLINK>system dos
WLINK>file /results/test.o
[ comment: i press control-d on the next line ]
WLINK>loading object files
Warning! W1080: file /results/test.o is a 32-bit object file
Error! E2015: file /results/test.o(test.s): bad relocation type specified
Error! E2015: file /results/test.o(test.s): bad relocation type specified
Error! E2015: file /results/test.o(test.s): bad relocation type specified

如果我尝试制作 COFF 而不是 ELF,yasm 甚至无法组装:

root@1341f35c4590:/# cd ow/binl/
root@1341f35c4590:/ow/binl# WATCOM=/ow /ow/binl/wlink 
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
Press CTRL/D to finish
WLINK>system dos
WLINK>file /results/test.o
WLINK>loading object files
Warning! W1080: file /results/test.o is a 32-bit object file
Error! E2015: file /results/test.o(test.s): bad relocation type specified
Error! E2015: file /results/test.o(test.s): bad relocation type specified
Error! E2015: file /results/test.o(test.s): bad relocation type specified

我知道 yasm 不支持 16 位,但也许有解决方法?有兼容 GAS 的 16 位汇编器吗?GAS-to-Intel 转换器不工作。

4

1 回答 1

2

不是专家,但 AFAIK 没有 16 位 GAS 兼容的汇编程序。

此外,gcc 从未打算生成 8086 16 位代码。在默认情况下操作数大小为 16 位的意义上,Rask 端口生成 16 位代码。所以像这样的指令mov ax, 1234h被发出b8 34h 12h而不是66 b8 34h 12h被解释为mov eax, xxxx1234h实模式(如果你在 80386+ 上运行)

地址模式也是如此。

问题是这只是代码,目标文件格式仍然是 32 位的,所以它们最终会被 32 位工具用于 v86 环境。例如,ELF 不支持 16 位重定位,COFF 也不支持(根据 nasm)。

因此,即使 GCC 和 GAS 生成 16 位代码,它们也只能输出相对较新的对象格式。给定目标文件的每个工具都创建了 MZ 或 COM 可执行文件,这些工具都是在这些格式之前创建的,并且不支持它们。由于 DOS 很久以前就停止使用,因此没有花任何精力来增加对新格式的支持。


很长的解决方法(不打算使用)

我只能想象两种非常非常难的方式来使用 gcc 作为编译器。

  1. 尝试移植到 NASM。NASM 支持的输出文件格式比 YASM 多得多(同样旧的 16 位格式已被删除)。

用 flag 组装源文件-masm=intel以获得 Intel 语法。然后,您需要一个工具来将 GAS 点指令转换为 NASM 指令。 这必须手动编码。它们中的大多数都是像.globalXXX这样的简单替换,GLOBAL XXX但您需要转换有效地址并添加EXTERN XXX未定义的函数。

  1. 自己搬迁。(需要熟练掌握IA16架构和DOS)

您不得使用任何外部符号并生成 PIC 代码(-fPIC标志)和原始二进制文件(即只是代码)。定义一个函数指针结构,一个用于您需要使用的每个外部函数,例如

结构上下文_t
{
    int (*printf)(char* 格式, ...);
    ...
};
然后声明一个指针context_t,比如说context_t* ctx;如果您需要使用类似printfuse的功能ctx->printf。编译代码。

现在创建一个 C 源代码,称为 loader,它定义一个类型变量context_t并初始化它的指针。然后加载器必须读取二进制文件,定位为ctx指针分配的空间并将其设置为context_t变量的地址,然后将二进制文件加载到内存中(在段边界处)并通过远调用执行它。

您需要在文件中找到指针的位置,可以使用 GCC 生成的映射文件(-Xlinker -Map=output.map开关)或使用旧 BIOS PCI 32 位服务($PCI 签名)之类的签名并进行扫描。请注意,由 GCC 生成的代码可能会施加其他约束,但 PIC 开关应将其最小化。您可以在加载程序之后附加二进制文件(请注意,如果您使用 MZ 格式并注意对齐)并简化事情。

于 2015-05-21T16:55:11.173 回答