如何组装GAS组件并将其与Open Watcom C库链接?

Jan*_*sen 7 c assembly coff gnu-assembler watcom

我正在尝试生成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
Run Code Online (Sandbox Code Playgroud)

我没有包含头文件,导致gcc无法使用OpenWatcom的libc头文件.

container $ echo 'main() { printf("lol"); }' > test.c
Run Code Online (Sandbox Code Playgroud)

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

container $ /trunk/build-ia16-master/prefix/bin/ia16-unknown-elf-gcc -S test.c
Run Code Online (Sandbox Code Playgroud)

现在我有这个汇编文件:

    .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)"
Run Code Online (Sandbox Code Playgroud)

在容器外面,在主机中,我尝试用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'
Run Code Online (Sandbox Code Playgroud)

我注释掉了语法行,因为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
Run Code Online (Sandbox Code Playgroud)

可悲的是,他们是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
Run Code Online (Sandbox Code Playgroud)

如果我尝试制作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
Run Code Online (Sandbox Code Playgroud)

我知道yasm不支持16位,但也许有一个解决方法?是否有兼容GAS的16位汇编程序?GAS-to-Intel转换器无法正常工作.

小智 2

不是专家,但据我所知,没有 16 位 GAS 兼容的汇编器。

此外,gcc 从未打算生成 8086 16 位代码。Rask 端口生成 16 位代码,因为操作数大小默认为 16 位。因此,类似的指令mov ax, 1234h会被发出为 asb8 34h 12h而不是 as 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 位格式已被删除)。

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

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

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

结构体context_t
{
    int (*printf)(char* 格式, ...);
    ...
};
然后声明一个指向 的指针context_t,例如context_t* ctxprintf如果您需要使用类似use 的函数ctx->printf来代替。编译代码。

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

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