call c function from assembly in 64 bit mode
by dogpatch from LinuxQuestions.org on (#53P8V)
Am having a problem with static calls from assembly code to a C function. I suppose the problem is with the different calling conventions between 32 bit and 64 bit compilers. In my case, my home computer is a 32 bit Linux, where everything is working fine. The server where i compile the same program is 64 bit BSD. Am using gcc to compile both the C and assembly code, on both machines.
My principal code is a C program with a number of functions that may be called from a variety of points, sometimes recursively. It also calls a couple of assembly language routines contained in their own source code files, compiled and linked statically.
At a couple of points, i need to call one of the C functions from within the assembly code. Weirdly, the call works fine most of the time, say around 70 or 80 percent of the time, but sometimes fails at the call with a Seg violation or signal 10 (ten), which tech support says is a bus error. I cannot use gdb or other interactive debugging tools, just have to drop a value into a global variable which i can then look at after catching the signal.
The code is far too complex to show here. In simplified form, it goes something like this: The C code in my_program.c:Code:int c_function(int);
int main (int argc, char *argv[]) {
int var1 var2, ret_c, ret_a;
ret_c = c_function(var1);
.
.
ret_a = asm_func(var2);
.
.
}
int c_function(int my_var) {
do something here
}The assembly module (asm_func.S):Code:.text
.globl asm_func
.type asm_func, @function
asm_func:
.
.
'prologue' and other assembly stuff
.
.
pushq %rdi
movq a_var,%rdi
call c_function /* SOMEtimes crashes right here */
popq %rdi
.
.
'epilogue' stuff
retThe compile instructions in makefile:Code:asm_func.o : asm_func.S ;
gcc -c -Wall asm_func.S -o asm_func.o
my_program : my_program.c asm_func.o ;
gcc -s my_program.c asm_func.o -o my_programI may try to write a simple routine (like the above) to try to make the problem occur in such a way as to give better details. Meanwhile, anybody that has experience in doing this sort of thing in 64 bit archtecture, i'd welcome a tip or two. Thanks


My principal code is a C program with a number of functions that may be called from a variety of points, sometimes recursively. It also calls a couple of assembly language routines contained in their own source code files, compiled and linked statically.
At a couple of points, i need to call one of the C functions from within the assembly code. Weirdly, the call works fine most of the time, say around 70 or 80 percent of the time, but sometimes fails at the call with a Seg violation or signal 10 (ten), which tech support says is a bus error. I cannot use gdb or other interactive debugging tools, just have to drop a value into a global variable which i can then look at after catching the signal.
The code is far too complex to show here. In simplified form, it goes something like this: The C code in my_program.c:Code:int c_function(int);
int main (int argc, char *argv[]) {
int var1 var2, ret_c, ret_a;
ret_c = c_function(var1);
.
.
ret_a = asm_func(var2);
.
.
}
int c_function(int my_var) {
do something here
}The assembly module (asm_func.S):Code:.text
.globl asm_func
.type asm_func, @function
asm_func:
.
.
'prologue' and other assembly stuff
.
.
pushq %rdi
movq a_var,%rdi
call c_function /* SOMEtimes crashes right here */
popq %rdi
.
.
'epilogue' stuff
retThe compile instructions in makefile:Code:asm_func.o : asm_func.S ;
gcc -c -Wall asm_func.S -o asm_func.o
my_program : my_program.c asm_func.o ;
gcc -s my_program.c asm_func.o -o my_programI may try to write a simple routine (like the above) to try to make the problem occur in such a way as to give better details. Meanwhile, anybody that has experience in doing this sort of thing in 64 bit archtecture, i'd welcome a tip or two. Thanks