Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C library call from assembly generated by gcc from some c code

Status
Not open for further replies.

achaleus

Member level 5
Joined
Dec 21, 2012
Messages
85
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,288
Location
Bangalore
Activity points
1,866
I am experimenting on modifying assembly by calling c library from assembly, I ran into segmentation fault
exp1.c contains

Code:
#include <stdio.h>

double *a,*b,*c;
int main()
{
double a_d = 1.1;
double b_d = 2.1;
double c_d;
c = &c_d;
a = &a_d;
b = &b_d;
*c = (*a + *b);
printf("\n%lf",*c);
}

add.c contains

double add(double a, double b){
double c = a*b;
return c;
}
I have created library add.c with name libtemp.a
the corresponding assembly code for exp1.c is

.file "exp1.c"
.comm a,8,8
.comm b,8,8
.comm c,8,8
.section .rodata
.LC2:
.string "\n%lf"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $48, %rsp
movabsq $4607632778762754458, %rax
movq %rax, -24(%rbp)
movabsq $4611911198408756429, %rax
movq %rax, -16(%rbp)
leaq -8(%rbp), %rax
movq %rax, c(%rip)
leaq -24(%rbp), %rax
movq %rax, a(%rip)
leaq -16(%rbp), %rax
movq %rax, b(%rip)
movq c(%rip), %rax
movq a(%rip), %rdx
movsd (%rdx), %xmm1
movq b(%rip), %rdx
movsd (%rdx), %xmm0
#addsd %xmm1, %xmm0            [B]# commented actual addition[/B]
call add                                     [B] # added my library call here[/B]
movsd %xmm0, (%rax)
movq c(%rip), %rax
movq (%rax), %rax
movq %rax, -40(%rbp)
movsd -40(%rbp), %xmm0
movl $.LC2, %edi
movl $1, %eax
call printf
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
.section .note.GNU-stack,"",@progbits

---------------------------------------------------------------------
Any ideas why I got segmentation fault and how to make this working. I should only able to touch assembly
 
Last edited by a moderator:

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
49,399
Helped
14,438
Reputation
29,140
Reaction score
13,232
Trophy points
1,393
Location
Bochum, Germany
Activity points
284,301
Re: c library call from assembly generated by gcc from some c code

Most likely a stack fault, e.g due to incorrect argument passing in function call. How did you check your function call is complying will calling conventions in effect? Also are you sure that you have set up the C run time environment as far as required by the library function?

The usual way to handle similar problems is to single step the code in GNU debugger.
 

achaleus

Member level 5
Joined
Dec 21, 2012
Messages
85
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,288
Location
Bangalore
Activity points
1,866
The assembly of add.c is given below, where it is operating on %xmm0 and %xmm1, and in actual assembly this add call should actually work .
Code:
gcc -c add.c
ar -crv libtemp.a add.o

gcc -S exp1.s -ltemp L. exp1.o
gcc exp1.o -o exp1 -ltemp L.
./exp1 -- segmentation fault

Code:
.file   "add.c"
        .text
        .globl  add
        .type   add, @function
add:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movsd   %xmm0, -24(%rbp)
        movsd   %xmm1, -32(%rbp)
        movsd   -24(%rbp), %xmm0
        mulsd   -32(%rbp), %xmm0
        movsd   %xmm0, -8(%rbp)
        movq    -8(%rbp), %rax
        movq    %rax, -40(%rbp)
        movsd   -40(%rbp), %xmm0
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   add, .-add
        .ident  "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
        .section        .note.GNU-stack,"",@progbits
 
Last edited:

achaleus

Member level 5
Joined
Dec 21, 2012
Messages
85
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,288
Location
Bangalore
Activity points
1,866
Re: c library call from assembly generated by gcc from some c code

I have done single step gdb debugging,

Code:
#addsd %xmm1, %xmm0            # commented actual addition
call add                                      # added my library call here
movsd %xmm0, (%rax)              # segmentation fault here
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
49,399
Helped
14,438
Reputation
29,140
Reaction score
13,232
Trophy points
1,393
Location
Bochum, Germany
Activity points
284,301
You're on the way. Learn to look on the debug results more closely.

Does the segmentation fault happen during return instruction (usually due to unpaired stack operations) or during movsd because (rax) isn't a valid destination address. I guess the latter, rax has been modified in the called function and must be setup anew.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top