如何编译用于C程序(GNU汇编程序)的程序集例程?

时间:2022-03-23 03:15:59

I have a set of assembly function which I want to use in C programs by creating a header file. For instance, if I have asm_functions.s which defines the actual assembly routines and asm_functions.h which has prototypes for the functions as well as some standard #define's I needed. My goal is to use a C program, say test_asm.c to call the assembly functions.

我有一组汇编函数,我想通过创建头文件在C程序中使用它。例如,如果我有定义实际程序集例程的asm_functions.s和asm_functions.h,它们具有函数原型以及我需要的一些标准#define。我的目标是使用C程序,比如test_asm.c来调用程序集函数。

asm__functions.h:


 #define ASM_CONST_1    0x80
 #define ASM_CONST_2    0xaf

uint8_t asm_foo( int, int, int );

uint8_t asm_foo(int,int,int);

asm__functions.s:


 /* dont need this: #include "asm_functions.h" */

.section .text .type asm_foo, @function asm__foo: /* asm code with proper stack manipulation for C calling conventions */ ret

.section .text .type asm_foo,@ function asm__foo:/ * asm代码,具有适当的C调用约定的堆栈操作* / ret

test__asm.c:


 #include "asm_foo.h"

int main() { uint8_t res = asm_foo( 1, 2, 3); return 0; }

int main(){uint8_t res = asm_foo(1,2,3);返回0; }

In a situation like this what would be the proper way to compile a link the program? I was trying something like this:

在这样的情况下,编译链接程序的正确方法是什么?我正在尝试这样的事情:


gas -o asm_foo.o asm_foo.s
gcc -o test_asm test_asm.c

But I still get a linker error from GCC saying that my assembly routine is undefined. I hope this contrived example is good enough to explain the situation.

但我仍然从GCC得到一个链接器错误,说我的程序集例程未定义。我希望这个人为的例子足以解释这种情况。

Thanks!

EDIT:

Here is a snippet of output when I compile with a single command:

使用单个命令编译时,这是一个输出片段:


tja@tja-desktop:~/RIT/SP2/latest$ gcc -o test_pci pci_config.s test_pci.c
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte': (.text+0x8): undefined reference toPCI_FUNCTION_ID'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte': (.text+0xa): undefined reference toREAD_CONFIG_BYTE'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte': (.text+0x18): undefined reference toPCI_BIOS_FUNCTION_INT'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte': (.text+0x1b): undefined reference toBAD_REGISTER_NUMBER'
/tmp/ccY0SmMN.o: In function _pci_bios_read_word': (.text+0x30): undefined reference toPCI_FUNCTION_ID'
...

All of those, such as PCI_FUNCTION_ID, are defined in my header file, which is included by the C program. When I compile the assembly code by itself there are no errors.

所有这些,例如PCI_FUNCTION_ID,都在我的头文件中定义,该文件包含在C程序中。当我自己编译汇编代码时没有错误。

2 个解决方案

#1


Based on the files in your question, I managed to compile it. I've changed both the file names and the file contents.

根据你问题中的文件,我设法编译它。我已经更改了文件名和文件内容。

asm_const.h :

#define ASM_CONST_1    0x80
#define ASM_CONST_2    0xaf

asm_functions.h :

#include "asm_const.h"
unsigned char asm_foo( int, int, int );

asm_functions.S (the trailing S must be capital! #include needs it) :

asm_functions.S(尾随S必须是大写!#include需要它):

#include "asm_const.h"
.section .text
.globl asm_foo
.type asm_foo, @function
asm_foo:
  mov $ASM_CONST_1, %eax
  /* asm code with proper stack manipulation for C calling conventions */
  ret

test_asm.c :

#include "asm_functions.h"
int main() {
  return asm_foo( 1, 2, 3);
}

Please note that you need the the assembly file extension .S with capital S. With .s, the .s file wouldn't be run through the preprocessor, thus #include wouldn't work, and you wouldn't be able to use ASM_CONST_1 in the .s file.

请注意,您需要使用大写字母S的汇编文件扩展名.S。.s,.s文件不会通过预处理器运行,因此#include不起作用,您将无法使用.s文件中的ASM_CONST_1。

Compile with a single command:

使用单个命令编译:

gcc -o test_asm asm_functions.S test_asm.c

Or, as an alternative, compile with multiple commands, creating .o files:

或者,作为替代方案,使用多个命令进行编译,创建.o文件:

gcc -c asm_functions.S
gcc -c test_asm.c
gcc -o test_asm asm_functions.o test_asm.o

The single-command gcc takes care of compiling the .S file using gas, the .c file with GCC's C compiler, and linking the resulting temporary .o files together using ld. gcc runs all those commands with the appropriate flags by default.

单命令gcc负责使用gas编译.S文件,使用GCC的C编译器编译.c文件,并使用ld将生成的临时.o文件链接在一起。默认情况下,gcc使用适当的标志运行所有这些命令。

On some systems (but not on Linux with the default GCC installation) you have to prepend an underscore to the names of exported functions in the .S file (but not in the .c or .h files). So all instances of asm_foo would become _asm_foo only in the .S file.

在某些系统上(但不是在安装了默认GCC的Linux上),您必须在.S文件中的导出函数名称前加下划线(但不包括.c或.h文件中)。因此,asm_foo的所有实例仅在.S文件中变为_asm_foo。

#2


Have you considered using inline assembly? It would be much easier than using an assembler file.

您是否考虑过使用内联汇编?它比使用汇编程序文件容易得多。

Edit: Also, the reason you are getting linker errors is probably because the C compiler adds a leading underscore on identifiers for the actual symbols. Try adding an underscore in your assembly file.

编辑:此外,您获得链接器错误的原因可能是因为C编译器在实际符号的标识符上添加了前导下划线。尝试在程序集文件中添加下划线。

#1


Based on the files in your question, I managed to compile it. I've changed both the file names and the file contents.

根据你问题中的文件,我设法编译它。我已经更改了文件名和文件内容。

asm_const.h :

#define ASM_CONST_1    0x80
#define ASM_CONST_2    0xaf

asm_functions.h :

#include "asm_const.h"
unsigned char asm_foo( int, int, int );

asm_functions.S (the trailing S must be capital! #include needs it) :

asm_functions.S(尾随S必须是大写!#include需要它):

#include "asm_const.h"
.section .text
.globl asm_foo
.type asm_foo, @function
asm_foo:
  mov $ASM_CONST_1, %eax
  /* asm code with proper stack manipulation for C calling conventions */
  ret

test_asm.c :

#include "asm_functions.h"
int main() {
  return asm_foo( 1, 2, 3);
}

Please note that you need the the assembly file extension .S with capital S. With .s, the .s file wouldn't be run through the preprocessor, thus #include wouldn't work, and you wouldn't be able to use ASM_CONST_1 in the .s file.

请注意,您需要使用大写字母S的汇编文件扩展名.S。.s,.s文件不会通过预处理器运行,因此#include不起作用,您将无法使用.s文件中的ASM_CONST_1。

Compile with a single command:

使用单个命令编译:

gcc -o test_asm asm_functions.S test_asm.c

Or, as an alternative, compile with multiple commands, creating .o files:

或者,作为替代方案,使用多个命令进行编译,创建.o文件:

gcc -c asm_functions.S
gcc -c test_asm.c
gcc -o test_asm asm_functions.o test_asm.o

The single-command gcc takes care of compiling the .S file using gas, the .c file with GCC's C compiler, and linking the resulting temporary .o files together using ld. gcc runs all those commands with the appropriate flags by default.

单命令gcc负责使用gas编译.S文件,使用GCC的C编译器编译.c文件,并使用ld将生成的临时.o文件链接在一起。默认情况下,gcc使用适当的标志运行所有这些命令。

On some systems (but not on Linux with the default GCC installation) you have to prepend an underscore to the names of exported functions in the .S file (but not in the .c or .h files). So all instances of asm_foo would become _asm_foo only in the .S file.

在某些系统上(但不是在安装了默认GCC的Linux上),您必须在.S文件中的导出函数名称前加下划线(但不包括.c或.h文件中)。因此,asm_foo的所有实例仅在.S文件中变为_asm_foo。

#2


Have you considered using inline assembly? It would be much easier than using an assembler file.

您是否考虑过使用内联汇编?它比使用汇编程序文件容易得多。

Edit: Also, the reason you are getting linker errors is probably because the C compiler adds a leading underscore on identifiers for the actual symbols. Try adding an underscore in your assembly file.

编辑:此外,您获得链接器错误的原因可能是因为C编译器在实际符号的标识符上添加了前导下划线。尝试在程序集文件中添加下划线。