构建交叉编译器的必要性

huangapple go评论65阅读模式
英文:

Necessity of building a cross compiler

问题

我正在使用一台64位Linux机器
我曾经在进行一些操作系统开发,其中提到需要使用交叉编译器生成目标系统的代码。
Barebones教程(https://wiki.osdev.org/Bare_bones)指导使用主机操作系统的本地编译器(我认为是gcc 7.4.0)来构建一个i686-gcc编译器。

然而,使用gcc本身也可以为其他目标平台生成代码,例如:

gcc -ffreestanding -c -fno-pie -m32 source.c -o source.o        
ld source.o -o flatbinary.bin -m elf_i386 -Ttext 0x10000 --oformat binary

我认为第一条命令创建了一个32位机器的ELF对象(不使用标准库?我认为-ffreestanding表示这一点)。
第二条命令创建了一个平面二进制映像,其中绝对(虚拟?)地址偏移了0x10000。

这不就是交叉编译器的作用吗?为什么我需要一个单独的交叉编译器?

注意:我刚刚涉足编译器、目标文件等广阔领域,刚刚开始理解一些概念。我可能对一些概念有基本的错误理解。 构建交叉编译器的必要性

英文:

I am using a 64 bit linux machine
I was doing some operating system development, where it is said that you need to use a cross compiler to generate code for the target system.
The barebones tutorial (https://wiki.osdev.org/Bare_bones) instructs one to build a i686-gcc compiler using the native compiler of the host operating system (I believe that it is gcc 7.4.0).

However, one can generate code for other target platforms using gcc itself, eg.

gcc -ffreestanding -c -fno-pie -m32 source.c -o source.o        
ld source.o -o flatbinary.bin -m elf_i386 -Ttext 0x10000 --oformat binary

I believe that the first command creates an ELF object for a 32 bit machine. (Using no standard library? I think that -ffreestanding stands for that)
The second command creates a flat binary image where the absolute (virtual?) addresses are offset by 0x10000.

Isn't this what a cross compiler does? Why do I need a separate cross compiler??

Note: I have just ventured into this vast world of compilers, object files, etc. and am just beginning to get an understanding. I might have some concepts fundamentally incorrect. 构建交叉编译器的必要性

答案1

得分: 4

x86-64 ELF GCC(就像你在Linux上找到的那样)完全可以编译成32位x86/ELF。

如果你在某种其他ISA上,比如ARM,你需要一个交叉编译器来目标i386。

如果你在Windows上,你可能需要一个交叉编译器来创建32位x86 ELF对象,因为MinGW或Cygwin GCC会创建PE/COFF对象文件,而不是ELF。 正确的机器代码,错误的对象文件格式。

英文:

x86-64 ELF GCC (like you'll find on Linux) is perfectly capable of compiling for 32-bit x86/ELF.

If you were on some other ISA, like ARM, you would need a cross compiler to target i386.

If you were on Windows, you might need a cross-compiler to make 32-bit x86 ELF objects because MinGW or Cygwin GCC would be making PE/COFF object files, not ELF. Right machine code, wrong object-file format.

答案2

得分: 0

建议构建一个交叉编译器以避免在编译操作系统时出现错误,但这并非必需。

英文:

It's recommended to build a cross-compiler to avoid mistakes while compiling the operating system but it's not necessary.

huangapple
  • 本文由 发表于 2020年1月6日 02:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定