arm

tags: arm

遠端除錯:

qemu-aarch -g [port] [debug_file]
gdb-multiarch [debug_file]
(gdb) target remote localhost:1234

using on command line

  1. ddd eval-command="target remote localhost:1234"
  2. put target remote localhost:1234 into a file and use command to execute it
  3. put target remote localhost:1234 into your .gdbinit

ddd

ddd debugger gdb-multiarch gdb [name of your ARM binary]

compile:

若要在無linux環境下執行(bare)
-nostdlib:不使用system call
-static:或是包成static
arm-linux-gnueabi-gcc -o test test.c

或是直接打包

aarch64-linux-gnu-gcc -c test.s
aarch64-linux-gnu-gcc -static -g -o test_xxx test.o

執行:

qemu-arm -L /usr/arm-linux-gnueabi/ test

qemu-arm /lib/ld-linux-armhf.so.3: No such file or directory

解決方法:

rpath

arm-linux-gnueabihf-gcc -Xlinker -rpath=/usr/arm-linux-gnueabihf/lib hello.c

此執行檔會優先使用arm-linux-gnueabihf

rpath

rpath 是 runtime 時期的library search path,這些字串會完整的被複製到runtime linker (也就是ld.so)用來尋找共享函式庫;如果rpath 沒有設定,則linker 會使用LD_RUN_PATH 的值。

我們可以使用兩種方式在連結時加入rpath設定rpath

arm-linux-gnueabihf-gcc -Xlinker -rpath=/usr/arm-linux-gnueabihf/lib hello.c
arm-linux-gnueabihf-gcc -Wl,-rpath=/usr/arm-linux-gnueabihf/lib hello.c

或是直接設定LD_RUN_PATH

export LD_RUN_PATH=/usr/arm-linux/gnueabihf/lib
arm-linux-gnueabihf-gcc hello.c

以上兩種都可以透過readelf讀出

readelf -d hello
0x0000000f (RPATH) 函式庫路徑:[/usr/arm-linux-gnueabihf/lib]

另外還有一種是 -L,它會設定連接時搜尋共享函式庫的目錄,這裡只給一個最粗淺的例子:
arm-linux-gnueabihf-gcc -c hello.c -o hello.o
arm-linux-gnueabihf-ld hell.o -o hello
會發生undefined reference to puts 的錯誤,因為我們沒有連接所需要的 c library,另外我們也沒有指定程式的進入點為何,要能連結通過至少要:
arm-linux-gnueabihf-ld hell.o -o -lc -L/usr/arm-linux-gnueabihf/lib hello –entry main

當然這樣不代表可以執行,試著執行會發現dynamic linker 並沒有正確設定,除此之外還有各種runtime 的library 需要連結進去才會動;要看到可運作的呼叫方式,可以用gcc -v (-verbose) 來觀察。

-rpath-link 則只指定link 時搜尋shared library的路徑,這個路徑不會包含到executable 裡面

使用-rpath-link 須知它也會蓋掉原本的搜尋路徑,因此用-rpath-link有個危害是:link time linker(ld)跟runtime linker (ld.so) 可能會使用不同的shared library,因為後者並沒有設定這個路徑,而是去預設的路徑尋找。

set LD_LIBRARY_PATH

因為LD_LIBRARY_PATH的優先順序高過ldconfig因此可以被qemu連結

export LD_LIBRARY_PATH=/usr/arm-linux-gnueabihf/lib/

利用qemu的-L flag

要先確定沒有設定LD_LIBRARY_PATH,因為他的優先順序比LD_LIBRARY_PATH要低

qemu-arm -L /usr/arm-linux-gnueabihf hello

Appendix

mini arm os

https://paper.dropbox.com/doc/Week7-Lab42-ti7vbuiHPSdKZYkbpQxQ4
https://paper.dropbox.com/doc/Week3-Lab39-SxyMP3KWtO5OtyHiObQcP

arm 架構演進

https://drive.google.com/open?id=0B5GW0aIORHIBUjVvUXJ2NVhPckU

arm big little

https://drive.google.com/open?id=0B5GW0aIORHIBLW0ycldfZHhieHc

arm32

http://hackfoldr.org/arm/

ubuntu 18.04

  • ubuntu下的gcc預設會開啟pie關閉後才可以single step debug
  • 不然用arm-none版本

warning: Unable to find dynamic linker breakpoint function

https://www.ptt.cc/bbs/LinuxDev/M.1270355520.A.468.html

solib-search-path

set solib-search-path "\usr\lib"         # Specify search directory
set solib-search-path "/lib":"/My Lib"   # Specify two search directories(Unix)

arm real-time

exception return

  1. Nested interrupt 立即返回
  2. 節省回傳時間

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →