0

`There are two files in the following: program.s and functions.c

I build it with following command:

gcc -c -std=c99 functions.c

gcc -o program program.s functions.o

run it: ./program

but got segmentation fault error

if remove "pushq %rbx" "popq %rbx", segmentation fault disappear.

please help me analysis it ?

//program.s

    .globl main
    .align 16    
main:
    pushq %rbp
    movq %rsp, %rbp
    pushq %rbx
    call    read_int@PLT
    popq %rbx
    popq %rbp
    retq 

//functions.c


#include <stdio.h>
#include <inttypes.h>

int64_t read_int() {
    int64_t i;
    scanf("%" SCNd64, &i);
    return i;
}

look at the above, if remove "pushq %rbx" "popq %rbx", segmentation fault disappear.

`

phuclv
  • 37,963
  • 15
  • 156
  • 475
capsule
  • 1
  • 1
  • 1
    Hi Hongyong, this is a somewhat frequently asked question, so I've marked it as a duplicate of a previous one. Welcome to the site! – Nate Eldredge Jun 16 '23 at 01:55
  • thanks, I got answer from https://stackoverflow.com/questions/51070716/glibc-scanf-segmentation-faults-when-called-from-a-function-that-doesnt-align-r – capsule Jun 16 '23 at 02:07

0 Answers0