什么是C和汇编实际上编译? [英] What do C and Assembler actually compile to?

查看:164
本文介绍了什么是C和汇编实际上编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

于是我发现,C(++)程序其实并不编译成纯二进制(我可能错在这里得到一些东西,在这种情况下,我很抱歉:D),但一系列的事情(符号表,操作系统相关的东西,...),但...

So I found out that C(++) programs actually don't compile to plain "binary" (I may have gotten some things wrong here, in that case I'm sorry :D) but to a range of things (symbol table, os-related stuff,...) but...


  • 是否汇编编译以纯二进制?这意味着,除了像predefined字符串等资源,没有多余的东西。

  • Does assembler "compile" to pure binary? That means no extra stuff besides resources like predefined strings, etc.

如果是C编译的东西比普通的二进制别人,怎么能小汇编引导程序刚刚从硬盘驱动器的说明复制到内存中并执行他们?我的意思是,如果操作系统内核,这可能是用C语言编写,编译成东西比纯二进制不同 - ?如何引导装载程序处理它

If C compiles to something else than plain binary, how can that small assembler bootloader just copy the instructions from the HDD to memory and execute them? I mean if the OS kernel, which is probably written in C, compiles to something different than plain binary - how does the bootloader handle it?

编辑:我知道,汇编器不编译,因为它只有你的机器的指令集 - 我没有找到什么汇编组装到一个好词。如果你有一个,离开这里的评论,我会改变它。

edit: I know that assembler doesn't "compile" because it only has your machine's instruction set - I didn't find a good word for what assembler "assembles" to. If you have one, leave it here as comment and I'll change it.

推荐答案

ç通常编译成汇编,只因为那让生活变得简单编译器作家差。

C typically compiles to assembler, just because that makes life easy for the poor compiler writer.

大会code始终汇编(不是编译)来的重定位目标code 的。你可以认为这是二进制机器code和二进制数据,但有很多的装饰和元数据。关键的部分是:

Assembly code always assembles (not "compiles") to relocatable object code. You can think of this as binary machine code and binary data, but with lots of decoration and metadata. The key parts are:


  • code和数据出现在一个名为乐段。

  • Code and data appear in named "sections".

重定位目标文件可能包含的的定义标签的,是指位置的部分中。

Relocatable object files may include definitions of labels, which refer to locations within the sections.

重新定位的对象文件可以包括要被填充有标签的其他地方定义的值洞。对于这样一个孔的正式名称是的重定位项

Relocatable object files may include "holes" that are to be filled with the values of labels defined elsewhere. The official name for such a hole is a relocation entry.

例如,如果编译和组装(但不链接)这个节目

For example, if you compile and assemble (but don't link) this program

int main () { printf("Hello, world\n"); }

你很可能与


  • 含有机code为

    A 文本部分

对于A标签定义指向文本部分的开头

A label definition for main which points to the beginning of the text section

A rodata (只读数据),其中包含字符串中的字节部分你好,世界\\ n

A rodata (read-only data) section containing the bytes of the string literal "Hello, world\n"

一个重定位项依赖于的printf 和指向一个洞,在调用指令文本节中段。

A relocation entry that depends on printf and that points to a "hole" in a call instruction in the middle of a text section.

如果你是一个Unix系统中的重定位目标文件通常被称为一个.o文件,如 hello.o 上,您可以探索标签定义和用途有一个叫做简单的工具纳米,你可以从一个名为较为复杂的工具来获得更详细的信息。 objdump的

If you are on a Unix system a relocatable object file is generally called a .o file, as in hello.o, and you can explore the label definitions and uses with a simple tool called nm, and you can get more detailed information from a somewhat more complicated tool called objdump.

我教涵盖这些主题类的,我让学生写一个汇编程序和连接,这需要几个星期,但是当他们做,他们大多重新定位的对象上的pretty手感好code。这可不是一件容易的事情。

I teach a class that covers these topics, and I have students write an assembler and linker, which takes a couple of weeks, but when they've done that most of them have a pretty good handle on relocatable object code. It's not such an easy thing.

这篇关于什么是C和汇编实际上编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆