简体   繁体   English

什么用于“--oformat = elf32-i386”?

[英]What the usage of “--oformat=elf32-i386”?

I have some confusion with two command line options of GNU linker --- ld. 我对GNU链接器的两个命令行选项--- ld感到困惑。 But before my question, I will show you some backgrounds. 但在我提出问题之前,我会告诉你一些背景知识。

I am reading Richard Blum's Professional Assembly Language these days, and cpuid.s is an sample of assembly code from this book. 我这些天正在阅读Richard Blum的专业汇编语言 ,而cpuid.s是本书中的汇编代码示例。 This book introduces 32-bit assembly programming, but mine is a 64-bit OS. 本书介绍了32位汇编编程,但我的是64位操作系统。

In order to generate the 32-bit output, just in accord with the book's sample. 为了生成32位输出,只需按照本书的样本。 I uses the following two command to assembly and link the code: 我使用以下两个命令来汇编和链接代码:

as --32 -o cpuid.o cpuid.s
ld --oformat=elf32-i386 -o cpuid cpuid.o

however, at the second step, ld fails with "ld: i386 architecture of input file `cpuid2.o' is incompatible with i386:x86-64 output". 然而,在第二步, ld失败,“输入文件`cpuid2.o'的ld:i386架构与i386:x86-64输出不兼容”。

After some google, I find that I need to use -melf_i386 option: 经过一些谷歌,我发现我需要使用-melf_i386选项:

ld -melf_i386 -o cpuid cpuid.o

Yeah, this time the link is success, but I don't know why, in the GNU's official documentation, it says: 是的,这次链接是成功的,但我不知道为什么,在GNU的官方文档中,它说:

you can use the `--oformat' option to specify the binary format for the output object 您可以使用`--oformat'选项指定输出对象的二进制格式

I uses an 32-bit object file cpuid.o , and tell ld to generate 32-bit output explicitly through --oformat=elf32-i386 optoin, I think it should be no trouble. 我使用32位目标文件cpuid.o ,并告诉ld通过--oformat=elf32-i386 optoin显式生成32位输出,我认为应该没问题。 But why must I use the -melf_i386 option? 但为什么我必须使用-melf_i386选项? So what the reasons of existing --oformat ? 那么现有的原因--oformat

I tried to googled the anwser, but failed, I found two most relevant links below, but not answers my question: 我试图谷歌搜索anwser,但失败了,我发现下面有两个最相关的链接,但没有回答我的问题:

http://www.linuxquestions.org/questions/linux-software-2/relocatable-linking-on-x86-64-for-i386-872812/ http://www.linuxquestions.org/questions/linux-software-2/relocatable-linking-on-x86-64-for-i386-872812/

Building a 32-bit app in 64-bit Ubuntu 在64位Ubuntu中构建一个32位应用程序

Any help will be appreciated. 任何帮助将不胜感激。 Thanks... 谢谢...

An output format does not necessarily correspond to an architecture, even though that might be the case for elf32-i386. 输出格式不一定对应于体系结构,即使elf32-i386可能就是这种情况。 But for others like ihex or bin there is no way for ld to choose the appropriate architecture emulation. 但对于像ihex或bin这样的其他人来说,ld无法选择合适的架构仿真。 That's why it just defaults to the one it has been compiled for, which seems to be x86-64 in your case. 这就是为什么它只是默认为它编译的那个,在你的情况下似乎是x86-64。

as --32

means you are targeting a 32-bit arch machine but 意味着你的目标是32位拱形机器但是

ld --oformat=elf32-i386

means you are asking ld to make exe file format as elf in 32-bit for i386 platform, whereas -melf_i386 is asking linker to generate/emulate object file for i386 machine. 意味着你要求ld将ec文件格式设置为32位ilf86平台的elf,而-melf_i386则要求链接器为i386机器生成/模拟目标文件。 So --oformat is asking linker to generate object as per target arch whereas -m is asking linker to emulate the target arch. 所以--oformat要求链接器根据目标arch生成对象,而-m要求链接器模拟目标arch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM