0

I am running Ubuntu 20.04 32-bit server on a Raspberry Pi4 (armv7l architecture with Cortex-A72). I have a simple program, return.s as follows:

        .section .text
        .global _start
_start: mov r0, #1
        mov r7, #1
        swi 0

I can assemble, run, and debug the program locally if libc is excluded:

as -g return.s -o return.o
ld return.o -o return
./return; echo $?  # result is "1"
gdb return
start              # breaks at first line

But if I include libc as a dynamic-linked library, then the debugging hangs:

ld return.o -o return -lc-2.31 \
    -dynamic-linker=/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3
./return; echo $?  # result is "1"
gdb return
start              # hangs

If I interrupt the debugger with Ctrl-C, then I see the following backtrace:

#0  0xb6fe12fe in ?? () from /usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3
#1  0xb6fd81e4 in ?? () from /usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Any idea what is happening? Is it possible to debug programs with linked libraries? If not, is there a static library available?

James Foster
  • 2,070
  • 10
  • 15
  • You'll need to recompile the linked library with debug symbols – 0x777C Nov 04 '20 at 23:45
  • Dynamic module loading is done by the default `_start` function, so if you are replacing it, then none of your dynamic libraries will be loaded and thus there's no reason to link them. I don't think that explains your issue though. – Colonel Thirty Two Nov 04 '20 at 23:47
  • @ColonelThirtyTwo, The examples I'm copying have `_start`. Can you point me to a trivial assembly program that uses a C library? Should I use `main` instead? – James Foster Nov 05 '20 at 00:04
  • @ColonelThirtyTwo, when I change `_start` to `main` I get the following: `ld: warning: cannot find entry symbol _start; defaulting to 0001016c`. – James Foster Nov 05 '20 at 00:12
  • 1
    `_start` is usually provided by the startup libs that your C compiler automatically adds to the program. If you're compiling with `as` then you have no such luxury. I think you can "just" compile assembly with `gcc` using `main` and it will bring in the appropriate libraries. – Colonel Thirty Two Nov 05 '20 at 00:16
  • @ColonelThirtyTwo, When I replace `_start` with `main` and assemble with `gcc` the program runs but the debugger hangs in the same way. – James Foster Nov 05 '20 at 00:54
  • So, I compiled `glibc` from sources following [these](https://stackoverflow.com/questions/10412684/how-to-compile-my-own-glibc-c-standard-library-from-source-and-use-it) instructions and when I used `ld-2.28.so` instead of `ld-2.31.so` then the problem went away. – James Foster Nov 05 '20 at 04:55

0 Answers0