Using gcc and ld to Compile Other Binary Formats

Using gcc and ld to Compile Other Binary Formats

Compiling different binary formats is an annoyingly undocumented task. Here's a list of useful options for compiling special binaries. Some of these may be found in man pages, and some seem to be impossible to find.

Options for GCC

  • -nostdinc: "No standard includes". This means not to search the standard include path, which is usually "/usr/include". You'll need this and the -I option (to add directories to the include path) if you're compiling using your own set of standard include files.

Options for LD

  • -nostdlib: "No standard libraries". This means not to attempt to link with the standard libraries. Similarly, you'll need this and the -L option (to add directories to the library path) if you're compiling using your own set of standard libraries.
  • -Ttext 0x100000: Use load address 0x100000. This specifies that the text segment is to be loaded at 0x100000. The other segments will automatically be placed immediately following the text segment unless their locations are also explicitly specified. If linking to an ELF, the headers will give the entry point and the load address of each segment. The loader should use those, as there could be space left in between the segments. If linking to a raw, the load address is also 0x100000 and all the segments are smashed together.
  • --oformat binary: Output a raw binary. Input modules are still in the host system's native format (usually ELF). Intermediate modules obviously cannot be raw binaries because they need symbol and relocation information. (--iformat specifies the input format, but that is seldom useful.)
  • -static: If you use a set of standard libraries, they must be statically linked unless your loader supports dynamic linking.

Code directives

The assembler can compile 16-bit assembly code to be run in a 16-bit segment, 32-bit assembly code to be run in a 32-bit segment, or 32-bit assembly code to be run in a 16-bit segment (by using overrides on each instruction). As far as we know, GCC always generates 32-bit assembly code. Several directives can be placed in the assembly output by using the asm() directive in the C code.
  • .code16: This specifies that the code being assembled is 16-bit code to be run in a 16-bit segment.
  • .code32: This specifies that the code being assembled is 32-bit code to be run in a 32-bit segment. This is the default.
  • .code16gcc: This specifies that the code being assembled was generated by GCC and therefore is 32-bit assembly code that will run in a 16-bit segment. GAS will add the necessary overrides to each instruction to indicate 32-bit addresses, registers, data, etc. This is the most useful, since this directive allows us to write C code to be run in a 16-bit segment (either real mode or protected mode). In terms of applying this to C code, this means adding asm(".code16gcc/n"); at the top of each C module. Notice that you can mix functions to be run in 16-bit segments and functions to be run in 32-bit segments in the same module, but if you do, be careful about switching segments and make sure the load addresses are valid for every part of the code.

Compiling DOS COM Files and Boot Loaders

The DOS COM file format is just a raw binary loaded into a 16-bit real mode segment at address 0x100. To compile a COM with gcc, you need to add asm(".code16gcc/n"); at the top of each C module (including all the standard libraries) and link with the options "-static -Ttext 0x100 --oformat binary". Although you will probably have a lot of in-line assembly for system calls, it will probably still be easier to write in C.

To make system calls easier, we wrote a macro that expands to an asm() that takes arguments for the values of eax, ebx, ecx, edx, esi, and edi. We wrote more macros for getting the return values out of the registers and for testing the values of the bits in the flags register after returning from a system call. This makes it trivial to write some wrapper functions for most file and terminal I/O in DOS.

Compiling a boot loader would work the same way, except the load address is 0x7C00 instead of 0x100 and the size of your raw binary is limited to 446 bytes for a master boot record and 512 bytes for a boot sector. Good luck fitting a useful C program into 446 bytes, though.

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: GCC LD手册中文版是指GNU Compiler Collection(GCC)和GNU链接器(LD)的开发者文档的中文翻译版本。GCC是一套自由软件编译器,支持多种编程语言,如C、C++、Objective-C、Fortran、Ada和D等。LD是一款用于将可重定位目标文件链接成可执行文件或共享库的工具。这些工具是开源的,并且广泛用于Linux、Unix和其他操作系统中。 GCC LD手册中文版包含了GCCLD的详细介绍、使用方法、命令语法、选项和示例等。这些文档提供了极其详细和全面的指导,适合开发人员使用。无论是刚入门编程的新手,还是有丰富编程经验的专业人士,都可以从中获得优质的指导和帮助。 GCC LD手册中文版对于在Linux或Unix平台开发软件的开发人员来说是非常有用的。它可以帮助彻底理解GCCLD的特点、功能和用法,并能够提高工作效率和代码质量。此外,为了更好的使用这些工具,学习英文也是很有必要的。 总之,GCC LD手册中文版是一份非常有用的文档。它是一款开源软件开发中的必备工具,有助于开发人员更加深入地理解这些工具的使用。 ### 回答2: GCCLD均是常见的编译工具,其中GCC是编译器,LD是链接器。而gcc ld手册中文版,即GCCLD的中文使用手册。 该手册中包含GCCLD的基础语法、参数选项、使用方法等内容,是GCCLD的使用参考书。对于初学者来说,该手册能够帮助他们更好地学习和使用GCCLD。 手册主要内容包括GCCLD的编译链接流程和用法,GCCLD还支持一些选项和参数,如优化选项、调试选项、链接器脚本等等。同时,手册中也介绍了GCCLD在不同操作系统上的使用方式。 总之,gcc ld手册中文版对于使用GCCLD的人来说是非常重要的,它帮助用户更好地理解和使用这两个工具,提高编译和链接的效率,减少错误发生的概率。因此,对于程序员来说,掌握gcc ld手册中文版是非常必要的。 ### 回答3: GCCLD都是在Linux系统上最常用的编译器和链接器。它们被广泛用于开发各种软件应用。它们的手册是帮助开发人员正确地使用这些工具的关键资源。 GCC手册介绍了GCC编译器的功能和使用方法。它提供了丰富的信息,包括语法、选项和参数等,以便开发人员正确地编译和构建代码。此外,GCC手册还描述了所支持的平台和版本、调试技术以及其他编译器相关的主题。这本手册对于想要使用GCC编译器的人来说是一个重要的参考资料。 另一方面,LD手册介绍了LD链接器的功能和使用方法。它详细描述了链接器如何将代码、数据和库文件组合成可执行文件,以便运行程序。LD手册解释了链接器所需的选项和参数,以及常见问题的解决方案。此外,这本手册还包括链接器支持的平台和版本,以及与链接器相关的其他主题。这本手册对于想要深入了解链接器的开发人员来说是非常有用的。 总而言之, GCCLD手册中文版 提供了编译器和链接器的完整指南,以帮助开发人员正确地使用它们来创建高效、可靠的应用程序。这些手册对于程序员学习、理解和使用这些工具是非常重要的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HashCodeWithJava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值