kirbyfan64sos
kirbyfan64sos

Reputation: 10727

Segfault in /system/bin/linker with binary compiled for Android

I'm trying to build Kona for Android and have been running into some issues. The patches I made to Kona are available here.

For one thing, I'm trying to avoid using ndk-build and therefore have edited the makefile to support Android. The details of that aren't particularly useful to this question (or fun), so I'll skip them.

Basically, the source files are getting compiled like this:

arm-linux-androideabi-clang -g -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -mtune=xscale -msoft-float -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3    -c -o src/ks.o src/ks.c
...
arm-linux-androideabi-clang -g -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -mtune=xscale -msoft-float -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3  src/0.o src/c.o src/getline.o src/getline_android.o src/mt.o src/p.o src/r.o src/k.o src/kc.o src/kx.o src/kg.o src/km.o src/kn.o src/ko.o src/ks.o src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o src/main.o -o k -Wl,--gc-sections -Wl,-z,nocopyreloc -lgcc -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb -lc -lm -ldl

I based these flags off of what I saw ndk-build using. I then ran the following:

ryan@DevPC-LX:~/stuff/kdroid$ adb remount
remount succeeded
ryan@DevPC-LX:~/stuff/kdroid$ adb push ./k /system/bin/k
3201 KB/s (724928 bytes in 0.221s)
ryan@DevPC-LX:~/stuff/kdroid$

Now, trying to run the binary just segfaults:

ryan@DevPC-LX:~/stuff/kdroid$ adb shell
# k
[1] + Stopped (signal)        k
# 
[1]   Segmentation fault      k
# 

This is where is starts getting weird. If I try to use gdb, I get this:

ryan@DevPC-LX:~/stuff/kdroid$ adb shell
# gdbserver :5039 /system/bin/k
Process /system/bin/k created; pid = 297
Listening on port 5039

In another shell window:

ryan@DevPC-LX:~/stuff/kdroid$ arm-linux-androideabi-gdb
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-linux-gnu --target=arm-linux-android".
For bug reporting instructions, please see:
<http://source.android.com/source/report-bugs.html>.
/home/ryan/.gdbinit:1: Error in sourced command file:
No symbol table is loaded.  Use the "file" command.
(gdb) symbol-file k
Reading symbols from /media/ryan/stuff/kdroid/k...done.
(gdb) target remote :5039
Remote debugging using :5039
0xb0001000 in ?? ()
(gdb) continue
Continuing.
Cannot access memory at address 0x0

Program received signal SIGSEGV, Segmentation fault.
0xb0004d36 in ?? ()
(gdb) bt
#0  0xb0004d36 in ?? ()
#1  0xb0005278 in ?? ()
#2  0xb0005278 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) 

ndk-stack gives me this:

********** Crash dump: **********
Build fingerprint: 'generic/sdk/generic:2.3.3/GRI34/101070:eng/test-keys'
pid: 297, tid: 297  >>> /system/bin/k <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0003cabc
Stack frame #00  pc b0004d36  /system/bin/linker: Unable to open symbol file k/linker. Error (20): Not a directory

So...the linker seems to be crashing. If I pull /system/bin/linker into the current directory, then I get this:

********** Crash dump: **********
Build fingerprint: 'generic/sdk/generic:2.3.3/GRI34/101070:eng/test-keys'
pid: 291, tid: 291  >>> k <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0003cabc
Stack frame #00  pc b0004d36  /system/bin/linker: Routine BFD:  ./linker: warning: sh_link not set for section `.ARM.exidx'
??
??:0
Crash dump is completed

Which is all very confusing.

I have absolutely no clue what the heck could be wrong at this point. I mean, I feel like it's something wrong with my compiler commands, but I don't know what.

EDIT: It's definitely something wrong with my compiler commands. If I build this program:

int main() { return 0; }

with ndk-build and with the command-line flags I posted before, the version built without ndk-build segfaults.

Upvotes: 4

Views: 2561

Answers (2)

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

Figured it out! It was Clang! For some reason, it was causing the segfault. I still have no clue why...but the problem is still mostly solved.

Upvotes: 1

tavmem
tavmem

Reputation: 19

There is a separate version of Kona at https://github.com/tavmem/konaStk

It provides stack reporting on the Kona execution process, printing out a mass of details concerning just about every step made by the Kona executable.

I use it as a tool for debugging, and/or adding new features to Kona. It might help show where the Stop and the Segmentation Fault is occurring.

It might be easier to use if you try it out in your Linux environment first (to get a feel for it), then try it in the Android environment.

Upvotes: 0

Related Questions