Skip to content

Commit

Permalink
backup v8 before I blow everything up
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Jul 16, 2018
1 parent 4ad2fda commit 1b328d0
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 85 deletions.
17 changes: 8 additions & 9 deletions Makefile
@@ -1,9 +1,11 @@
.POSIX:

ARCH = arm
CC = $(PREFIX_PATH)-gcc
# no-pie: https://stackoverflow.com/questions/51310756/how-to-gdb-step-debug-a-dynamically-linked-executable-in-qemu-user-mode
CFLAGS = -fno-pie -ggdb3 -march=$(MARCH) -marm -pedantic -no-pie -std=c99 -Wall -Wextra #-mthumb
CFLAGS = -fno-pie -ggdb3 -march=$(MARCH) -pedantic -no-pie -std=c99 -Wall -Wextra $(CFLAGS_EXTRA)
CTNG =
DEFAULT_SYSROOT = /usr/$(PREFIX)
DRIVER_BASENAME = main
DRIVER_OBJ = $(DRIVER_BASENAME)$(OBJ_EXT)
IN_EXT = .S
Expand All @@ -13,14 +15,14 @@ OBJDUMP_EXT = .objdump
OBJ_EXT = .o
OUT_EXT = .out
PREFIX = arm-linux-gnueabihf
QEMU_EXE = qemu-arm
QEMU_EXE = qemu-$(ARCH)
RUN_CMD = $(QEMU_EXE) -L $(SYSROOT)
GDB_PORT = 1234
TEST = test

ifeq ($(CTNG),)
PREFIX_PATH = $(PREFIX)
SYSROOT = /usr/arm-linux-gnueabihf
SYSROOT = $(DEFAULT_SYSROOT)
else
PREFIX_PATH = $(CTNG)/$(PREFIX)/bin/$(PREFIX)
SYSROOT = $(CTNG)/$(PREFIX)/$(PREFIX)/sysroot
Expand All @@ -35,13 +37,10 @@ OBJDUMPS := $(addsuffix $(OBJDUMP_EXT), $(INS_NOEXT))
.PHONY: all clean objdump test
.PRECIOUS: %$(OBJ_EXT)

all: $(OUTS) hello_c$(OUT_EXT)
all: $(OUTS)

objdump: $(OBJDUMPS)

hello_c$(OUT_EXT): hello_c.c
$(CC) $(CFLAGS) -o '$@' '$<'

%$(OUT_EXT): %$(OBJ_EXT) $(DRIVER_OBJ)
$(CC) $(CFLAGS) -o '$@' '$<' $(DRIVER_OBJ)

Expand All @@ -62,11 +61,11 @@ gdb-%: %$(OUT_EXT)
gdb-multiarch -q \
-nh \
-ex 'set confirm off' \
-ex 'set architecture arm' \
-ex 'set architecture $(ARCH)' \
-ex 'set sysroot $(SYSROOT)' \
-ex 'file $<' \
-ex 'target remote localhost:$(GDB_PORT)' \
-ex 'break asm_main' \
-ex 'break asm_main_end' \
-ex 'continue' \
-ex 'layout split' \
;
Expand Down
8 changes: 7 additions & 1 deletion README.adoc
Expand Up @@ -93,6 +93,10 @@ Shortcut:
./t -g add
....

This leaves us right at the end of `asm_main`, and at the start of the assembly code in the `.S` file.

TODO: possible to restart the running program from GDB as in `gdbserver --multi`? Convenient as you can reuse breakpoints easily.

Bibliography: https://stackoverflow.com/questions/20590155/how-to-single-step-arm-assembler-in-gdb-on-qemu/51310791#51310791

=== Disassemble
Expand Down Expand Up @@ -164,8 +168,8 @@ Non OS portable examples will be clearly labeled with their OS.
These examples show how our tooling works:

. link:fail.S[]
. link:hello_c.c[]
. link:hello_driver.S[]
. link:hello_common.S[]

=== C driver

Expand Down Expand Up @@ -335,6 +339,8 @@ Unlike most our other examples, which use the C standard library for portability

== ARMv8

TODO I'm setting this up now, but it is segfaulting. Almost there I think.

In this repository we will document only points where ARMv8 differs from ARMv7 behaviour: so you should likely learn ARMv7 first.

ARMv8 is the 64 bit version of the ARM architecture.
Expand Down
7 changes: 7 additions & 0 deletions add.S
Expand Up @@ -4,6 +4,13 @@ ENTRY

/* 1 + 2 = 3, immediates. */
mov r0, #1
/* r1 = r0 + 2 */
add r1, r0, #2
cmp r1, #3
FAIL_IF(bne)

/* If src == dest, we can ommit one of them. */
mov r0, #1
add r0, #2
cmp r0, #3
FAIL_IF(bne)
Expand Down
47 changes: 2 additions & 45 deletions common.h
@@ -1,51 +1,8 @@
#ifndef COMMON_H
#define COMMON_H

.extern exit, printf, puts

/* Store all callee saved registers, and LR in case we make further BL calls.
*
* Also save the input arguments r0-r3 on the stack, so we can access them later on,
* despite those registers being overwritten.
*/
#define ENTRY \
.text; \
.global asm_main; \
asm_main: \
stmdb sp!, { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, lr }; \
;
#include "common_arch.h"

/* Branching to "fail" makes tests fail with exit status 1.
*
* If EXIT is reached, the program ends successfully.
*
* Meant to be called at the end of ENTRY.
*
* Restore LR and jump to it to return from asm_main.
*
* * r0: line of failure
*/
#define EXIT \
mov r0, #0; \
mov r1, #0; \
b pass; \
fail: \
ldr r1, [sp]; \
str r0, [r1]; \
mov r0, #1; \
pass: \
add sp, #16; \
ldmia sp!, { r4, r5, r6, r7, r8, r9, r10, r11, lr }; \
bx lr; \
;

#define FAIL_IF(condition) \
condition 1f; \
b 2f; \
1: \
ldr r0, =__LINE__; \
b fail; \
2: \
;
.extern exit, printf, puts

#endif
48 changes: 48 additions & 0 deletions common_arch.h
@@ -0,0 +1,48 @@
#ifndef COMMON_ARCH_H
#define COMMON_ARCH_H

/* Store all callee saved registers, and LR in case we make further BL calls.
*
* Also save the input arguments r0-r3 on the stack, so we can access them later on,
* despite those registers being overwritten.
*/
#define ENTRY \
.text; \
.global asm_main; \
asm_main: \
stmdb sp!, { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, lr }; \
asm_main_end: \
;

/* Meant to be called at the end of ENTRY.*
*
* Branching to "fail" makes tests fail with exit status 1.
*
* If EXIT is reached, the program ends successfully.
*
* Restore LR and bx jump to it to return from asm_main.
*/
#define EXIT \
mov r0, #0; \
mov r1, #0; \
b pass; \
fail: \
ldr r1, [sp]; \
str r0, [r1]; \
mov r0, #1; \
pass: \
add sp, #16; \
ldmia sp!, { r4, r5, r6, r7, r8, r9, r10, r11, lr }; \
bx lr; \
;

#define FAIL_IF(condition) \
condition 1f; \
b 2f; \
1: \
ldr r0, =__LINE__; \
b fail; \
2: \
;

#endif
13 changes: 0 additions & 13 deletions hello_c.c

This file was deleted.

6 changes: 6 additions & 0 deletions hello_common.S
@@ -0,0 +1,6 @@
/* Minimal example using common.h. */

#include "common.h"

ENTRY
EXIT
2 changes: 0 additions & 2 deletions hello_driver.S
Expand Up @@ -3,8 +3,6 @@
* Controls the exit status of the program.
*/

#include "common.h"

.text
.global asm_main
asm_main:
Expand Down
2 changes: 1 addition & 1 deletion main.c
@@ -1,7 +1,7 @@
#include "stdio.h"
#include "stdint.h"

int asm_main(uint32_t *line) __attribute__((target("arm")));
int asm_main(uint32_t *line);

int main(void) {
uint32_t ret, line;
Expand Down
2 changes: 2 additions & 0 deletions params.mk
@@ -0,0 +1,2 @@
# The opposite of -mthumb.
CFLAGS_EXTRA = -marm
1 change: 1 addition & 0 deletions v8/common.h
41 changes: 41 additions & 0 deletions v8/common_arch.h
@@ -0,0 +1,41 @@
#ifndef COMMON_ARCH_H
#define COMMON_ARCH_H

#define ENTRY \
.text; \
.global asm_main; \
asm_main: \
sub sp, sp, #0xA0; \
stp x29, x30, [sp, #0x0]; \
stp x27, x28, [sp, #0x10]; \
stp x25, x26, [sp, #0x20]; \
stp x23, x24, [sp, #0x30]; \
stp x21, x22, [sp, #0x40]; \
stp x19, x20, [sp, #0x50]; \
stp x6, x7, [sp, #0x60]; \
stp x4, x5, [sp, #0x80]; \
stp x2, x3, [sp, #0x90]; \
stp x0, x1, [sp, #0xA0]; \
asm_main_end: \
;

#define EXIT \
mov w0, #0; \
mov w1, #0; \
b pass; \
fail: \
ldr w1, [sp]; \
str w0, [x1]; \
mov w0, #1; \
pass: \
ldp x19, x20, [sp, #0x50]; \
ldp x21, x22, [sp, #0x40]; \
ldp x23, x24, [sp, #0x30]; \
ldp x25, x26, [sp, #0x20]; \
ldp x27, x28, [sp, #0x10]; \
ldp x29, x30, [sp]; \
add sp, sp, #0xA0; \
ret; \
;

#endif
1 change: 1 addition & 0 deletions v8/hello_common.S
5 changes: 5 additions & 0 deletions v8/hello_driver.S
@@ -0,0 +1,5 @@
.text
.global asm_main
asm_main:
mov w0, #0
ret
28 changes: 14 additions & 14 deletions v8/mov.S
Expand Up @@ -7,19 +7,19 @@

ENTRY

/* Immediate. */
mov r0, #0
cmp r0, #0
FAIL_IF(bne)
mov r0, #1
cmp r0, #1
FAIL_IF(bne)
#/* Immediate. */
#mov r0, #0
#cmp r0, #0
#FAIL_IF(bne)
#mov r0, #1
#cmp r0, #1
#FAIL_IF(bne)

/* Register. */
mov r0, #0
mov r1, #1
mov r1, r0
cmp r1, #0
FAIL_IF(bne)
#/* Register. */
#mov r0, #0
#mov r1, #1
#mov r1, r0
#cmp r1, #0
#FAIL_IF(bne)

EXIT
EXIT
2 changes: 2 additions & 0 deletions v8/params.mk
@@ -1,2 +1,4 @@
ARCH = aarch64
DEFAULT_SYSROOT = /usr/aarch64-linux-gnu
MARCH = armv8-a
PREFIX = aarch64-linux-gnu

0 comments on commit 1b328d0

Please sign in to comment.