Hello!
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.
asm__functions.h:
#define ASM_CONST_1 0x80
#define ASM_CONST_2 0xaf
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
test__asm.c:
#include "asm_foo.h"
int main() {
uint8_t res = asm_foo( 1, 2, 3);
return 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.
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 to
PCI_FUNCTION_ID'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0xa): undefined reference to
READ_CONFIG_BYTE'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0x18): undefined reference to
PCI_BIOS_FUNCTION_INT'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0x1b): undefined reference to
BAD_REGISTER_NUMBER'
/tmp/ccY0SmMN.o: In function _pci_bios_read_word':
(.text+0x30): undefined reference to
PCI_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.
-
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.
TURBOxSPOOL : Yes, I considered it, but I am writing device level code which I wanted to create assembly routines directed. I thought in the end it would be cleaner than a ton of asm() calls.TURBOxSPOOL : Heh, in my actual assembly file and header file they are indeed defined with underscores: uint8_t _pci_bios_read_word( int, int int );Zifre : No, you shouldn't define it with underscores in the header file (that will give you double underscores). You only need the underscores in the assembly file. -
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) :
#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.
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:
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.
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.TURBOxSPOOL : I am still having a problem with the preprocessor it seems. In my header file I have a bunch of #defines. When I try to compile everything from source with a single command I get a bunch of undefined references for the #defines in the header file.TURBOxSPOOL : But I thought that was the point? I created the .h file as if it were going to be linked with C code. But I just implemented the functions in assembly...I took the #include out of the assembly file, but I obviously still need it in the C program. So in the end I want the C program to include the header which has prototypes/constants for the assembly functions. Thanks!TURBOxSPOOL : Hmm, after adding the .globl I am still having the errors. I should have posted my code directly but I was leaning toward brevity...pts : I've just updated, extended and fixed my answer. Please have a look again.pts : Please rename pci_config.s to pci_config.S . This should fix your problem.TURBOxSPOOL : Thanks! Excellent post!
0 comments:
Post a Comment