什么是应用程序二进制接口(ABI)? [英] What is an application binary interface (ABI)?

查看:125
本文介绍了什么是应用程序二进制接口(ABI)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有清楚地了解什么是ABI.请不要将我引至Wikipedia文章.如果我能理解的话,我就不会在这里发表如此冗长的文章.

I never clearly understood what an ABI is. Please don't point me to a Wikipedia article. If I could understand it, I wouldn't be here posting such a lengthy post.

这是我对不同界面的看法:

This is my mindset about different interfaces:

电视遥控器是用户和电视之间的接口.它是一个现有的实体,但是它本身是无用的(不提供任何功能).遥控器上所有这些按钮的所有功能都在电视机中实现.

A TV remote is an interface between the user and the TV. It is an existing entity, but useless (doesn't provide any functionality) by itself. All the functionality for each of those buttons on the remote is implemented in the television set.

接口:它是 该功能的functionalityconsumer.界面本身 什么也没做它只是调用了隐藏的功能.

Interface: It is an "existing entity" layer between the functionality and consumer of that functionality. An interface by itself doesn't do anything. It just invokes the functionality lying behind.

现在,取决于用户的身份,有不同类型的界面.

Now depending on who the user is there are different type of interfaces.

命令行界面(CLI)命令是现有的实体, 消费者是用户,而功能却在后面.

Command Line Interface (CLI) commands are the existing entities, the consumer is the user and functionality lies behind.

functionality:我的软件功能解决了一些问题 我们描述此接口的目的.

functionality: my software functionality which solves some purpose to which we are describing this interface.

existing entities:命令

consumer:用户

图形用户界面(GUI)窗口,按钮等是现有的 实体,而消费者又是用户,功能就在后面.

Graphical User Interface(GUI) window, buttons, etc. are the existing entities, and again the consumer is the user and functionality lies behind.

functionality:我的软件功能可以解决我们在描述此界面时遇到的一些问题.

functionality: my software functionality which solves some problem to which we are describing this interface.

existing entities:窗口,按钮等.

consumer:用户

应用程序编程接口(API)功能(或将 (更正确)接口(在基于接口的编程中)是 现有的实体,这里的消费者是另一个程序而不是用户, 功能位于该层的后面.

Application Programming Interface(API) functions (or to be more correct) interfaces (in interfaced based programming) are the existing entities, consumer here is another program not a user, and again functionality lies behind this layer.

functionality:我的软件功能解决了一些问题 我们正在描述此接口的问题.

functionality: my software functionality which solves some problem to which we are describing this interface.

existing entities:函数,接口(函数数组).

existing entities: functions, Interfaces (array of functions).

consumer:另一个程序/应用程序.

consumer: another program/application.

应用程序二进制接口(ABI),这是我的问题所在.

Application Binary Interface (ABI) Here is where my problem starts.

functionality: ???

existing entities: ???

consumer: ???

  • 我用不同的语言编写了软件,并提供了不同种类的界面(CLI,GUI和API),但是我不确定是否提供过任何ABI.
  • 维基百科说:

    ABI涵盖了诸如

    ABIs cover details such as

    • 数据类型,大小和对齐方式;
    • 调用约定,它控制函数参数的方式 传递的值和返回的值;
    • 系统调用号以及应用程序应如何进行系统调用 到操作系统;
    • data type, size, and alignment;
    • the calling convention, which controls how functions' arguments are passed and return values retrieved;
    • the system call numbers and how an application should make system calls to the operating system;

    其他ABI标准化了详细信息,例如

    Other ABIs standardize details such as

    • C ++名称修饰,
    • 异常传播,和
    • 在同一平台上的编译器之间的
    • 调用约定,但是 不需要跨平台兼容性.
    • the C++ name mangling,
    • exception propagation, and
    • calling convention between compilers on the same platform, but do not require cross-platform compatibility.

    • 谁需要这些详细信息?请不要说操作系统.我知道汇编编程.我知道如何链接&正在加载作品.我确切地知道里面发生了什么.

      • Who needs these details? Please don't say the OS. I know assembly programming. I know how linking & loading works. I know exactly what happens inside.

        为什么要进行C ++名称修改?我以为我们在二进制级别上进行讨论.语言为什么会出现?

        Why did C++ name mangling come in? I thought we are talking at the binary level. Why do languages come in?

        无论如何,我已经下载了 [PDF]系统V应用程序二进制接口版本4.1(1997-03-18) 来查看其中包含的内容.好吧,大多数都没有任何意义.

        Anyway, I've downloaded the [PDF] System V Application Binary Interface Edition 4.1 (1997-03-18) to see what exactly it contains. Well, most of it didn't make any sense.

        • 为什么包含两章(第4和第5章)来描述 ELF 文件格式?实际上,这是该规范中仅有的两个重要章节.其余各章是特定于处理器"的.无论如何,尽管我是一个完全不同的话题.请不要说ELF文件格式规范 是ABI.根据定义,它没有资格成为接口.

        • Why does it contain two chapters (4th & 5th) to describe the ELF file format? In fact, these are the only two significant chapters of that specification. The rest of the chapters are "processor specific". Anyway, I though that it is a completely different topic. Please don't say that ELF file format specifications are the ABI. It doesn't qualify to be an interface according to the definition.

        我知道,由于我们的讨论水平很低,因此必须非常具体.但是我不确定指令集体系结构(ISA)"具体如何?

        I know, since we are talking at such a low level it must be very specific. But I'm not sure how is it "instruction set architecture (ISA)" specific?

        在哪里可以找到Microsoft Windows的ABI?

        Where can I find Microsoft Windows' ABI?

        因此,这些是困扰我的主要查询.

        So, these are the major queries that are bugging me.

        推荐答案

        了解"ABI"的一种简单方法是将其与"API"进行比较.

        One easy way to understand "ABI" is to compare it to "API".

        您已经熟悉API的概念.如果要使用某些库或操作系统的功能,则将根据API进行编程.该API包含数据类型/结构,常量,函数等,您可以在代码中使用它们来访问该外部组件的功能.

        You are already familiar with the concept of an API. If you want to use the features of, say, some library or your OS, you will program against an API. The API consists of data types/structures, constants, functions, etc that you can use in your code to access the functionality of that external component.

        ABI非常相似.可以将其视为API的编译版本(或机器语言级别的API).编写源代码时,可以通过API访问该库.编译代码后,您的应用程序将通过ABI访问库中的二进制数据. ABI仅在较低级别上定义了已编译的应用程序将用来访问外部库的结构和方法(就像API一样).您的API定义了将参数传递给函数的顺序.您的ABI定义了如何传递这些参数(寄存器,堆栈等)的机制.您的API定义了哪些函数是您的库的一部分.您的ABI定义了代码如何存储在库文件中,以便使用库的任何程序都可以找到所需的函数并执行它.

        An ABI is very similar. Think of it as the compiled version of an API (or as an API on the machine-language level). When you write source code, you access the library through an API. Once the code is compiled, your application accesses the binary data in the library through the ABI. The ABI defines the structures and methods that your compiled application will use to access the external library (just like the API did), only on a lower level. Your API defines the order in which you pass arguments to a function. Your ABI defines the mechanics of how these arguments are passed (registers, stack, etc.). Your API defines which functions are part of your library. Your ABI defines how your code is stored inside the library file, so that any program using your library can locate the desired function and execute it.

        ABI非常重要.库中充满了代码和其他资源,但是您的程序必须知道如何在库文件中找到所需的内容.您的ABI定义了如何将库的内容存储在文件中,并且您的程序使用ABI来搜索文件并找到它所需要的内容.如果系统中的所有内容都遵循相同的ABI,则任何程序都可以使用任何库文件,无论是谁创建的. Linux和Windows使用不同的ABI,因此Windows程序将不知道如何访问为Linux编译的库.

        ABIs are important when it comes to applications that use external libraries. Libraries are full of code and other resources, but your program has to know how to locate what it needs inside the library file. Your ABI defines how the contents of a library are stored inside the file, and your program uses the ABI to search through the file and find what it needs. If everything in your system conforms to the same ABI, then any program is able to work with any library file, no matter who created them. Linux and Windows use different ABIs, so a Windows program won't know how to access a library compiled for Linux.

        有时候,ABI的变化是不可避免的.发生这种情况时,使用该库的所有程序都将无法工作,除非将它们重新编译为使用该库的新版本.如果ABI更改但API不变,则旧库和新库版本有时称为源兼容".这意味着尽管为一个库版本编译的程序不能与另一个库一起使用,但是为一个库编写的源代码如果重新编译就可以为另一个库使用.

        Sometimes, ABI changes are unavoidable. When this happens, any programs that use that library will not work unless they are re-compiled to use the new version of the library. If the ABI changes but the API does not, then the old and new library versions are sometimes called "source compatible". This implies that while a program compiled for one library version will not work with the other, source code written for one will work for the other if re-compiled.

        由于这个原因,开发人员倾向于尝试保持其ABI稳定(以最大程度地减少中断).保持ABI稳定意味着不更改函数接口(返回类型和数量,类型和参数顺序),数据类型或数据结构的定义,定义的常量等.可以添加新的函数和数据类型,但必须保留现有的函数和数据类型相同.例如,如果您的库使用32位整数表示函数的偏移量而您切换到64位整数,则使用该库的已编译代码将无法正确访问该字段(或后面的任何字段) .访问数据结构的成员在编译期间会转换为内存地址和偏移量,如果数据结构发生更改,则这些偏移量将不会指向代码期望它们指向的内容,并且结果充其量是不可预测的.

        For this reason, developers tend to try to keep their ABI stable (to minimize disruption). Keeping an ABI stable means not changing function interfaces (return type and number, types, and order of arguments), definitions of data types or data structures, defined constants, etc. New functions and data types can be added, but existing ones must stay the same. If, for instance, your library uses 32-bit integers to indicate the offset of a function and you switch to 64-bit integers, then already-compiled code that uses that library will not be accessing that field (or any following it) correctly. Accessing data structure members gets converted into memory addresses and offsets during compilation and if the data structure changes, then these offsets will not point to what the code is expecting them to point to and the results are unpredictable at best.

        除非您正在进行非常底层的系统设计工作,否则ABI不一定是您将明确提供的东西.这也不是特定于语言的,因为(例如)C应用程序和Pascal应用程序在编译后可以使用相同的ABI.

        An ABI isn't necessarily something you will explicitly provide unless you are doing very low-level systems design work. It isn't language-specific either, since (for example) a C application and a Pascal application can use the same ABI after they are compiled.

        关于SysV ABI文档中有关ELF文件格式的章节的问题:之所以包含此信息,是因为ELF格式定义了操作系统和应用程序之间的接口.当您告诉OS运行程序时,它期望程序以某种方式进行格式化,并且(例如)期望二进制文件的第一部分是一个ELF标头,其中包含位于特定内存偏移处的某些信息.这是应用程序将有关其自身的重要信息传达给操作系统的方式.如果以非ELF二进制格式(例如a.out或PE)构建程序,则要求ELF格式的应用程序的操作系统将无法解释该二进制文件或运行该应用程序.这是Windows应用程序不能直接在Linux机器上运行(反之亦然)的一个重要原因,而无需重新编译或无法在可以将一种二进制格式转换为另一种二进制格式的某种类型的仿真层中运行.

        Regarding your question about the chapters regarding the ELF file format in the SysV ABI docs: The reason this information is included is because the ELF format defines the interface between operating system and application. When you tell the OS to run a program, it expects the program to be formatted in a certain way and (for example) expects the first section of the binary to be an ELF header containing certain information at specific memory offsets. This is how the application communicates important information about itself to the operating system. If you build a program in a non-ELF binary format (such as a.out or PE), then an OS that expects ELF-formatted applications will not be able to interpret the binary file or run the application. This is one big reason why Windows apps cannot be run directly on a Linux machine (or vice versa) without being either re-compiled or run inside some type of emulation layer that can translate from one binary format to another.

        IIRC,Windows当前使用便携式可执行文件(或PE)格式.该Wikipedia页面的外部链接"部分中包含一些链接,其中包含有关PE格式的更多信息.

        IIRC, Windows currently uses the Portable Executable (or, PE) format. There are links in the "external links" section of that Wikipedia page with more information about the PE format.

        此外,关于您有关C ++名称修改的注意事项:在库文件中查找函数时,通常会按名称查找该函数. C ++允许您重载函数名称,因此仅使用名称不足以标识函数. C ++编译器在内部有自己的处理方式,称为名称修改. ABI可以定义对函数名称进行编码的标准方法,以便使用其他语言或编译器构建的程序可以找到所需的内容.在C ++程序中使用 extern "c" 时,您正在指示编译器使用标准化的方式来记录其他软件可以理解的名称.

        Also, regarding your note about C++ name mangling: When locating a function in a library file, the function is typically looked up by name. C++ allows you to overload function names, so name alone is not sufficient to identify a function. C++ compilers have their own ways of dealing with this internally, called name mangling. An ABI can define a standard way of encoding the name of a function so that programs built with a different language or compiler can locate what they need. When you use extern "c" in a C++ program, you're instructing the compiler to use a standardized way of recording names that's understandable by other software.

        这篇关于什么是应用程序二进制接口(ABI)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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