1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/style.css">
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1">
<meta content="#ffffff" name="theme-color">
<meta name="HandheldFriendly" content="true">
<meta property="og:title" content="nerdypepper">
<meta property="og:type" content="website">
<meta property="og:description" content="a static site {for, by, about} me ">
<meta property="og:url" content="https://peppe.rs">
<link rel="icon" type="image/x-icon" href="/favicon.png">
<title>Call To ARMs · peppe.rs</title>
<body>
<div class="posts">
<div class="post">
<a href="/" class="post-end-link">⟵ Back</a>
<a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/call_to_ARMs.md
">View Raw</a>
<div class="separator"></div>
<div class="date">
08/02 — 2020
<div class="stats">
<span class="stats-number">
33.57
</span>
<span class="stats-unit">cm</span>
 
<span class="stats-number">
2.2
</span>
<span class="stats-unit">min</span>
</div>
</div>
<span class="post-title">
Call To ARMs
</span>
<div class="post-text">
<p>My 4th semester involves ARM programming. And proprietary
tooling (Keil C). But we don't do that here.</p>
<h3 id="Building">Building</h3>
<p>Assembling and linking ARM binaries on non-ARM architecture
devices is fairly trivial. I went along with the GNU cross
bare metal toolchain binutils, which provides <code>arm-as</code> and
<code>arm-ld</code> (among a bunch of other utils that I don't care
about for now). </p>
<p>Assemble <code>.s</code> files with:</p>
<pre><code class="language-shell">arm-none-eabi-as main.s -g -march=armv8.1-a -o main.out
</code></pre>
<p>The <code>-g</code> flag generates extra debugging information that
<code>gdb</code> picks up. The <code>-march</code> option establishes target
architecture.</p>
<p>Link <code>.o</code> files with:</p>
<pre><code class="language-shell">arm-none-eabi-ld main.out -o main
</code></pre>
<h3 id="Running%20(and%20Debugging)">Running (and Debugging)</h3>
<p>Things get interesting here. <code>gdb</code> on your x86 machine
cannot read nor execute binaries compiled for ARM. So, we
simulate an ARM processor using <code>qemu</code>. Now qemu allows you
to run <code>gdbserver</code> on startup. Connecting our local <code>gdb</code>
instance to <code>gdbserver</code> gives us a view into the program’s
execution. Easy!</p>
<p>Run <code>qemu</code>, with <code>gdbserver</code> on port <code>1234</code>, with our ARM
binary, <code>main</code>:</p>
<pre><code class="language-shell">qemu-arm -singlestep -g 1234 main
</code></pre>
<p>Start up <code>gdb</code> on your machine, and connect to <code>qemu</code>’s
<code>gdbserver</code>:</p>
<pre><code>(gdb) set architecture armv8-a
(gdb) target remote localhost:1234
(gdb) file main
Reading symbols from main... # yay!
</code></pre>
<h3 id="GDB%20Enhanced">GDB Enhanced</h3>
<p><code>gdb</code> is cool, but it's not nearly as comfortable as well
fleshed out emulators/IDEs like Keil. Watching registers,
CPSR and memory chunks update <em>is</em> pretty fun. </p>
<p>I came across <code>gdb</code>'s TUI mode (hit <code>C-x C-a</code> or type <code>tui
enable</code> at the prompt). TUI mode is a godsend. It highlights
the current line of execution, shows you disassembly
outputs, updated registers, active breakpoints and more.</p>
<p><em>But</em>, it is an absolute eyesore.</p>
<p>Say hello to <a href="https://github.com/hugsy/gef">GEF</a>! “GDB
Enhanced Features” teaches our old dog some cool new tricks.
Here are some additions that made my ARM debugging
experience loads better:</p>
<ul>
<li>Memory watches</li>
<li>Register watches, with up to 7 levels of deref (overkill,
I agree)</li>
<li>Stack tracing</li>
</ul>
<p>And it's pretty! See for yourself:</p>
<p><a href="https://u.peppe.rs/wq.png"><img src="https://u.peppe.rs/wq.png" alt="gef.png" /></a></p>
<h3 id="Editing">Editing</h3>
<p>Vim, with <code>syntax off</code> because it
dosen't handle GNU ARM syntax too well.</p>
</div>
<div class="separator"></div>
<a href="/" class="post-end-link">⟵ Back</a>
<a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/call_to_ARMs.md
">View Raw</a>
</div>
</div>
</body>
</html>
|