可执行文件实际上包含什么? [英] What does executable file actually contain?

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

问题描述

可执行文件实际上包含什么?.. 它是否包含操作码和操作数形式的处理器指令?如果是这样,为什么我们对不同的操作系统有不同的可执行文件?

What does executable actually contain ? .. Does it contain instructions to processor in the form of Opcode and Operands ? If so why we have different executables for different operating systems ?

推荐答案

处理器根据操作码理解程序 - 所以你对包含操作码的可执行文件的直觉是正确的,你猜对了,任何可执行文件都必须有操作码和操作数才能执行处理器上的程序.

Processors understand programs in terms of opcodes - so your intution about executables containing opcodes is correct, and you guessed correctly that any executable has to have opcodes and operands for executing the program on a processor.

但是,程序大多在操作系统的帮助下执行(您可以编写不使用操作系统执行的程序,但这将是很多不必要的工作)——它在程序的硬件之上提供抽象可以使用.操作系统负责为任何程序运行设置上下文",即为程序提供所需的内存,提供程序可用于执行常见操作(例如写入文件、打印到控制台等)的通用库.

However, programs mostly execute with the help of operating systems (you can write programs which do not use an OS to execute, but that would be a lot of unnecessary work) - which provide abstractions on top of the hardware which the programs can use. The OS is responsible for setting up a "context" for any program to run i.e. provide the program the memory it needs, provide general purpose libraries which the program can use for doing common stuff such as write to files, print to console etc.

但是,要为程序设置上下文(为其提供内存、加载其数据、为其设置堆栈),操作系统需要读取程序的可执行文件并需要了解有关程序的一些信息,例如作为程序期望使用的数据,该数据的大小,存储在该数据区域中的初始值,组成程序的操作码列表(也称为进程的文本区域),它们的大小等.这些数据和更多(调试信息、只读数据,如程序中的硬编码字符串、符号表等)存储在可执行文件中.每个操作系统都理解这个可执行文件的不同格式,因为他们希望所有这些信息以不同的方式存储在可执行文件中.查看 Groo 提供的链接.

However, to set up the context for the program (provide it memory, load its data, set up a stack for it), the OS needs to read a program's executable file and needs to know a few things about the program such as the data which the program expects to use, size of that data, the initial values stored in that data region, the list of opcodes that make up the program (also called the text region of a process), their size etc. All of this data and a lot more (debugging information, readonly data such as hardcoded strings in the program, symbol tables etc) is stored within the executable file. Each OS understands a different format of this executable file, since they expect all this info to be stored in the executable in different ways. Check out the links provided by Groo.

用于在可执行文件中存储信息的几种格式是 UNIX 系统上的 ELF 和 COFF 以及 Windows 上的 PE.

A couple of formats that have been used for storing information in an executable file are ELF and COFF on UNIX systems and PE on Windows.

附:- 并非所有程序都需要可执行格式.在 Google 上查找引导加载程序.这些是占用硬盘上可引导分区的第一个扇区的特殊程序,用于加载操作系统本身.

P.S. - Not all programs need executable formats. Look up bootloaders on Google. These are special programs which occupy the first sector of a bootable partition on the hard-disk and are used to load the OS itself.

这篇关于可执行文件实际上包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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