diff options
Diffstat (limited to 'posts')
-rw-r--r-- | posts/call_to_ARMs.md | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/posts/call_to_ARMs.md b/posts/call_to_ARMs.md new file mode 100644 index 0000000..c4ce60a --- /dev/null +++ b/posts/call_to_ARMs.md | |||
@@ -0,0 +1,84 @@ | |||
1 | My 4th semester involves ARM programming. And proprietary | ||
2 | tooling (Keil C). But we don't do that here. | ||
3 | |||
4 | ### Building | ||
5 | |||
6 | Assembling and linking ARM binaries on non-ARM architecture | ||
7 | devices is fairly trivial. I went along with the GNU cross | ||
8 | bare metal toolchain binutils, which provides `arm-as` and | ||
9 | `arm-ld` (among a bunch of other utils that I don't care | ||
10 | about for now). | ||
11 | |||
12 | Assemble `.s` files with: | ||
13 | |||
14 | ```shell | ||
15 | arm-none-eabi-as main.s -g -march=armv8.1-a -o main.out | ||
16 | ``` | ||
17 | |||
18 | The `-g` flag generates extra debugging information that | ||
19 | `gdb` picks up. The `-march` option establishes target | ||
20 | architecture. | ||
21 | |||
22 | Link `.o` files with: | ||
23 | |||
24 | ```shell | ||
25 | arm-none-eabi-ld main.out -o main | ||
26 | ``` | ||
27 | |||
28 | ### Running (and Debugging) | ||
29 | |||
30 | Things get interesting here. `gdb` on your x86 machine | ||
31 | cannot read nor execute binaries compiled for ARM. So, we | ||
32 | simulate an ARM processor using `qemu`. Now qemu allows you | ||
33 | to run `gdbserver` on startup. Connecting our local `gdb` | ||
34 | instance to `gdbserver` gives us a view into the program's | ||
35 | execution. Easy! | ||
36 | |||
37 | Run `qemu`, with `gdbserver` on port `1234`, with our ARM | ||
38 | binary, `main`: | ||
39 | |||
40 | ```shell | ||
41 | qemu-arm -singlestep -g 1234 main | ||
42 | ``` | ||
43 | |||
44 | Start up `gdb` on your machine, and connect to `qemu`'s | ||
45 | `gdbserver`: | ||
46 | |||
47 | ``` | ||
48 | (gdb) set architecture armv8-a | ||
49 | (gdb) target remote localhost:1234 | ||
50 | (gdb) file main | ||
51 | Reading symbols from main... # yay! | ||
52 | ``` | ||
53 | |||
54 | ### GDB Enhanced | ||
55 | |||
56 | `gdb` is cool, but it's not nearly as comfortable as well | ||
57 | fleshed out emulators/IDEs like Keil. Watching registers, | ||
58 | CPSR and memory chunks update *is* pretty fun. | ||
59 | |||
60 | I came across `gdb`'s TUI mode (hit `C-x C-a` or type `tui | ||
61 | enable` at the prompt). TUI mode is a godsend. It highlights | ||
62 | the current line of execution, shows you disassembly | ||
63 | outputs, updated registers, active breakpoints and more. | ||
64 | |||
65 | *But*, it is an absolute eyesore. | ||
66 | |||
67 | Say hello to [GEF](https://github.com/hugsy/gef)! "GDB | ||
68 | Enhanced Features" teaches our old dog some cool new tricks. | ||
69 | Here are some additions that made my ARM debugging | ||
70 | experience loads better: | ||
71 | |||
72 | - Memory watches | ||
73 | - Register watches, with up to 7 levels of deref (overkill, | ||
74 | I agree) | ||
75 | - Stack tracing | ||
76 | |||
77 | And its pretty! See for yourself: | ||
78 | |||
79 | ![gef.png](https://u.peppe.rs/wq.png) | ||
80 | |||
81 | ### Editing | ||
82 | |||
83 | Vim, with `syntax off` because it | ||
84 | dosen't handle GNU ARM syntax too well. | ||