segfault when using asm in c

I have an issue when trying to execute a c program using assembly code in it. The issue is only occuring with parrot, the program perfectly works when using an other operating system. When trying to use an extern asm function in my C code, i’m immediately getting a segfault for an unknown reason. So i’m wondering if the issue comes from me and my setup or is it a common issue with parrot? And if possible, how could I fix this.

Here is the code:

  • progC.c
#include <stdio.h>
#include <inttypes.h>

extern int64_t max2(int64_t a, int64_t b);

int main() {
    int64_t i =10;
    int64_t j =17;

    printf("%ld\n",max2(i,j));
    return(0);
}
  • max2.s
.data

.global max2

max2:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
movq 16(%rbp), %rax
movq 24(%rbp), %rbx
cmpq %rax, %rbx
jl fin_max2
movq %rbx, %rax
fin_max2:
popq %rbx
movq %rbp, %rsp
popq %rbp
ret
  • Makefile
CC = gcc

build_myprintf:
	@echo "Building executable a.out"
	${CC} -no-pie -g progC.c max2.s -o a.out
	@echo "Executable a.out was built"

run_myprintf:
	./a.out

Very interesting. I think the reason is the version of GCC that Parrot is using.
From GDB, program crashes when it goes to first line of function max2 (i edited to max in code)
Trying disas the function gave me error 'max' has unknown type; cast it to its declared type
It would be nice if any C / ASM guy can help you about this.

Yes, i already tried to see something using gdb but it instantly send the segfault signal.

After googling and testing, here is the code that should work
The important part is .type fuma, @function that registers fumax (which should be max2, i edited function name again to make sure there is no same prebuilt function name) to be a C function


Trying to print function fumax which wasn’t detected as function but label in your code with radare2

The error that has no function register, or wrong syntax
image
Stackoverflow link about this topic: https://stackoverflow.com/a/29390271

I figured out where was the problem using your code: the problem wasn’t because of he .type <name>, @function but because of the presence of the .data. I removed it and know it’s working. If you want to use a .data you should then specify the .text for your function etc…
The only strange things is that this error only occured with parrot for me, i tried on ubuntu and mint and it works.
Thanks you all for the help!

Oh nice! Good to know that. This is very first time i do this and i was just removing things and adding things.

It is very strange to me as well. In theory same binary (ofc if compiler generates same binaries from source code) must have same behaviors and same results.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.