0

EDIT: The suggested commented solution (to use exit() not _exit) was unhelpful as there is no such system call number I can find. But a better solution is to explicitly ask Linux to flush any output in the C function: using fflush.

I have an issue where if I call my C function (foo) using 'b', it runs infinitely often, but if I call it with 'bl' it doesn't run at all! Does anyone know what I'm doing wrong? My files are below:


Makefile

TARGET      := APP
ENTRY       := MyMain
CC      := gcc
RM      := rm -f
INC     :=
ARM_FLAGS := -mlittle-endian -march=armv8-a+crc+simd+crypto+sb+predres -mtune=cortex-a72
LD_FLAGS    := -nostartfiles -e$(ENTRY)
GCC_FLAGS   := $(INC) -O2 -Wall -Wextra $(LD_FLAGS) $(ARM_FLAGS)
SRC = *.S *.c

.PHONY: all

all:
    @echo Building $(TARGET)..
    @$(CC) $(SRC) $(GCC_FLAGS) -o $(TARGET)
    @echo .. done

MyCFile.c

#include <stdio.h>

void foo(void)
{
    printf("ok");
    fflush(stout); // EDIT: adding this fixed the issue
}

Main.S

.global MyMain

.text

MyMain:
    bl foo      /* b: foo() is called infinitely. bl: foo() is never called [EDIT: fixed now with fflush ]*/
    MOV X0, 43 /* Return status of MyMain will be 43 */
    mov X8, 93 /* Syscall 93 = exit for this system */
    svc 0      /* Ask Linux to do its thing */

.data
Gregory Fenn
  • 460
  • 2
  • 13
  • I assume you didn't actually use a debugger to verify whether it was called or not, and your actual bug is using a raw `_exit` system call without flushing the line-buffer stdout IO buffer, since your string doesn't end with a newline and you didn't use puts. (And using `b`, you're tail-calling with whatever garbage was in the link register as a return address) – Peter Cordes Jul 25 '21 at 09:39
  • Correct, I don't know about these things, there isn't any use of debuggers or _exit calls in the tutorials I was following and couldn't find a clear answer here. – Gregory Fenn Jul 25 '21 at 09:41
  • Then find a tutorial on using GDB (or any other debugger) for asm. Trying to learn asm without a debugger is like trying to build a robot blindfold: a waste of your time when you could just take off your blindfold and see what's happening. Some of the stuff [at the bottom of the x86 tag wiki](https://stackoverflow.com/tags/x86/info) will apply to using GDB for asm on other ISAs, including AArch64. – Peter Cordes Jul 25 '21 at 09:42
  • 1
    `x8 = 93` / `svc 0` *is* a raw exit system call, documented in the `_exit(2)` man page. See [Syscall implementation of exit()](https://stackoverflow.com/q/46903180). re: using `b`, that tailcall is like trying to `ret` from `_start` (or having printf return to _start's return address), but `_start` doesn't have a valid return address anywhere. – Peter Cordes Jul 25 '21 at 09:45
  • 4
    `exit` is a function, not a syscall. Call it by doing `bl exit`. – fuz Jul 25 '21 at 10:18

0 Answers0