Skip to content

Instantly share code, notes, and snippets.

@cmaruan

cmaruan/Makefile Secret

Last active October 26, 2022 19:49
Show Gist options
  • Save cmaruan/de60bf7f3cf8110ec7f332f42235f9fb to your computer and use it in GitHub Desktop.
Save cmaruan/de60bf7f3cf8110ec7f332f42235f9fb to your computer and use it in GitHub Desktop.
.global _start
_start:
ldr x30, =stack_top
mov sp, x30
bl kmain
b .
kernel.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <putchar>:
#include <stddef.h>
#include <stdint.h>
volatile uint8_t *uart = (uint8_t *)0x16000000;
void putchar(char c) { *uart = c; }
0: d10043ff sub sp, sp, #0x10
4: 39003fe0 strb w0, [sp, #15]
8: 90000000 adrp x0, 0 <putchar>
c: 91000000 add x0, x0, #0x0
10: f9400000 ldr x0, [x0]
14: 39403fe1 ldrb w1, [sp, #15]
18: 39000001 strb w1, [x0]
1c: d503201f nop
20: 910043ff add sp, sp, #0x10
24: d65f03c0 ret
0000000000000028 <print>:
void print(const char *s) {
28: a9be7bfd stp x29, x30, [sp, #-32]!
2c: 910003fd mov x29, sp
30: f9000fe0 str x0, [sp, #24]
while (*s != '\0') {
34: 14000007 b 50 <print+0x28>
putchar(*s);
38: f9400fe0 ldr x0, [sp, #24]
3c: 39400000 ldrb w0, [x0]
40: 94000000 bl 0 <putchar>
s++;
44: f9400fe0 ldr x0, [sp, #24]
48: 91000400 add x0, x0, #0x1
4c: f9000fe0 str x0, [sp, #24]
while (*s != '\0') {
50: f9400fe0 ldr x0, [sp, #24]
54: 39400000 ldrb w0, [x0]
58: 7100001f cmp w0, #0x0
5c: 54fffee1 b.ne 38 <print+0x10> // b.any
}
}
60: d503201f nop
64: d503201f nop
68: a8c27bfd ldp x29, x30, [sp], #32
6c: d65f03c0 ret
0000000000000070 <printf>:
int printf(const char *fmt, ...) {
70: a9af7bfd stp x29, x30, [sp, #-272]!
74: 910003fd mov x29, sp
78: f9000fe0 str x0, [sp, #24]
7c: f9006fe1 str x1, [sp, #216]
80: f90073e2 str x2, [sp, #224]
84: f90077e3 str x3, [sp, #232]
88: f9007be4 str x4, [sp, #240]
8c: f9007fe5 str x5, [sp, #248]
90: f90083e6 str x6, [sp, #256]
94: f90087e7 str x7, [sp, #264]
98: 3d8017e0 str q0, [sp, #80]
9c: 3d801be1 str q1, [sp, #96]
a0: 3d801fe2 str q2, [sp, #112]
a4: 3d8023e3 str q3, [sp, #128]
a8: 3d8027e4 str q4, [sp, #144]
ac: 3d802be5 str q5, [sp, #160]
b0: 3d802fe6 str q6, [sp, #176]
b4: 3d8033e7 str q7, [sp, #192]
va_list ap;
va_start(ap, fmt);
b8: 910443e0 add x0, sp, #0x110
bc: f90017e0 str x0, [sp, #40]
c0: 910443e0 add x0, sp, #0x110
c4: f9001be0 str x0, [sp, #48]
c8: 910343e0 add x0, sp, #0xd0
cc: f9001fe0 str x0, [sp, #56]
d0: 128006e0 mov w0, #0xffffffc8 // #-56
d4: b90043e0 str w0, [sp, #64]
d8: 12800fe0 mov w0, #0xffffff80 // #-128
dc: b90047e0 str w0, [sp, #68]
const char *text;
char c;
while (*fmt) {
e0: 1400002d b 194 <printf+0x124>
if ((c = *fmt++) != '%') {
e4: f9400fe0 ldr x0, [sp, #24]
e8: 91000401 add x1, x0, #0x1
ec: f9000fe1 str x1, [sp, #24]
f0: 39400000 ldrb w0, [x0]
f4: 39013fe0 strb w0, [sp, #79]
f8: 39413fe0 ldrb w0, [sp, #79]
fc: 7100941f cmp w0, #0x25
100: 54000080 b.eq 110 <printf+0xa0> // b.none
putchar(c);
104: 39413fe0 ldrb w0, [sp, #79]
108: 94000000 bl 0 <putchar>
continue;
10c: 14000022 b 194 <printf+0x124>
}
switch ((c = *fmt++)) {
110: f9400fe0 ldr x0, [sp, #24]
114: 91000401 add x1, x0, #0x1
118: f9000fe1 str x1, [sp, #24]
11c: 39400000 ldrb w0, [x0]
120: 39013fe0 strb w0, [sp, #79]
124: 39413fe0 ldrb w0, [sp, #79]
128: 7101cc1f cmp w0, #0x73
12c: 54000301 b.ne 18c <printf+0x11c> // b.any
case 's':
print(va_arg(ap, const char *));
130: b94043e1 ldr w1, [sp, #64]
134: f94017e0 ldr x0, [sp, #40]
138: 7100003f cmp w1, #0x0
13c: 540000ab b.lt 150 <printf+0xe0> // b.tstop
140: 91003c01 add x1, x0, #0xf
144: 927df021 and x1, x1, #0xfffffffffffffff8
148: f90017e1 str x1, [sp, #40]
14c: 1400000d b 180 <printf+0x110>
150: 11002022 add w2, w1, #0x8
154: b90043e2 str w2, [sp, #64]
158: b94043e2 ldr w2, [sp, #64]
15c: 7100005f cmp w2, #0x0
160: 540000ad b.le 174 <printf+0x104>
164: 91003c01 add x1, x0, #0xf
168: 927df021 and x1, x1, #0xfffffffffffffff8
16c: f90017e1 str x1, [sp, #40]
170: 14000004 b 180 <printf+0x110>
174: f9401be2 ldr x2, [sp, #48]
178: 93407c20 sxtw x0, w1
17c: 8b000040 add x0, x2, x0
180: f9400000 ldr x0, [x0]
184: 94000000 bl 28 <print>
break;
188: 14000003 b 194 <printf+0x124>
default:
putchar(c);
18c: 39413fe0 ldrb w0, [sp, #79]
190: 94000000 bl 0 <putchar>
while (*fmt) {
194: f9400fe0 ldr x0, [sp, #24]
198: 39400000 ldrb w0, [x0]
19c: 7100001f cmp w0, #0x0
1a0: 54fffa21 b.ne e4 <printf+0x74> // b.any
}
}
va_end(ap);
return 0;
1a4: 52800000 mov w0, #0x0 // #0
}
1a8: a8d17bfd ldp x29, x30, [sp], #272
1ac: d65f03c0 ret
00000000000001b0 <kmain>:
void kmain(void) {
1b0: a9bf7bfd stp x29, x30, [sp, #-16]!
1b4: 910003fd mov x29, sp
print("Result: ");
1b8: 90000000 adrp x0, 0 <putchar>
1bc: 91000000 add x0, x0, #0x0
1c0: 94000000 bl 28 <print>
printf("from %s", "printf");
1c4: 90000000 adrp x0, 0 <putchar>
1c8: 91000001 add x1, x0, #0x0
1cc: 90000000 adrp x0, 0 <putchar>
1d0: 91000000 add x0, x0, #0x0
1d4: 94000000 bl 70 <printf>
print("\n");
1d8: 90000000 adrp x0, 0 <putchar>
1dc: 91000000 add x0, x0, #0x0
1e0: 94000000 bl 28 <print>
1e4: d503201f nop
1e8: a8c17bfd ldp x29, x30, [sp], #16
1ec: d65f03c0 ret
kernel.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <putchar>:
#include <stddef.h>
#include <stdint.h>
volatile uint8_t *uart = (uint8_t *)0x16000000;
void putchar(char c) { *uart = c; }
0: 90000001 adrp x1, 0 <putchar>
4: 12001c00 and w0, w0, #0xff
8: f9400021 ldr x1, [x1]
c: 39000020 strb w0, [x1]
10: d65f03c0 ret
0000000000000014 <print>:
void print(const char *s) {
while (*s != '\0') {
14: 39400001 ldrb w1, [x0]
18: 34000101 cbz w1, 38 <print+0x24>
1c: 90000003 adrp x3, 0 <putchar>
20: 91000063 add x3, x3, #0x0
24: d503201f nop
void putchar(char c) { *uart = c; }
28: f9400062 ldr x2, [x3]
2c: 39000041 strb w1, [x2]
while (*s != '\0') {
30: 38401c01 ldrb w1, [x0, #1]!
34: 35ffffa1 cbnz w1, 28 <print+0x14>
putchar(*s);
s++;
}
}
38: d65f03c0 ret
3c: d503201f nop
0000000000000040 <printf>:
int printf(const char *fmt, ...) {
40: d10183ff sub sp, sp, #0x60
va_list ap;
va_start(ap, fmt);
44: 128006e8 mov w8, #0xffffffc8 // #-56
48: 910083e9 add x9, sp, #0x20
int printf(const char *fmt, ...) {
4c: a9028be1 stp x1, x2, [sp, #40]
va_start(ap, fmt);
50: 910183e2 add x2, sp, #0x60
const char *text;
char c;
while (*fmt) {
54: 39400001 ldrb w1, [x0]
va_start(ap, fmt);
58: a9000be2 stp x2, x2, [sp]
5c: f9000be9 str x9, [sp, #16]
60: 29037fe8 stp w8, wzr, [sp, #24]
int printf(const char *fmt, ...) {
64: a90393e3 stp x3, x4, [sp, #56]
68: a9049be5 stp x5, x6, [sp, #72]
6c: f9002fe7 str x7, [sp, #88]
while (*fmt) {
70: 34000241 cbz w1, b8 <printf+0x78>
void putchar(char c) { *uart = c; }
74: 90000004 adrp x4, 0 <putchar>
78: 14000006 b 90 <printf+0x50>
7c: f9400082 ldr x2, [x4]
if ((c = *fmt++) != '%') {
80: 91000400 add x0, x0, #0x1
void putchar(char c) { *uart = c; }
84: 39000041 strb w1, [x2]
while (*fmt) {
88: 39400001 ldrb w1, [x0]
8c: 34000161 cbz w1, b8 <printf+0x78>
if ((c = *fmt++) != '%') {
90: 7100943f cmp w1, #0x25
94: 54ffff41 b.ne 7c <printf+0x3c> // b.any
putchar(c);
continue;
}
switch ((c = *fmt++)) {
98: 39400401 ldrb w1, [x0, #1]
9c: 7101cc3f cmp w1, #0x73
a0: 54000120 b.eq c4 <printf+0x84> // b.none
void putchar(char c) { *uart = c; }
a4: f9400082 ldr x2, [x4]
a8: 39000041 strb w1, [x2]
switch ((c = *fmt++)) {
ac: 91000800 add x0, x0, #0x2
while (*fmt) {
b0: 39400001 ldrb w1, [x0]
b4: 35fffee1 cbnz w1, 90 <printf+0x50>
putchar(c);
}
}
va_end(ap);
return 0;
}
b8: 52800000 mov w0, #0x0 // #0
bc: 910183ff add sp, sp, #0x60
c0: d65f03c0 ret
print(va_arg(ap, const char *));
c4: b9401be2 ldr w2, [sp, #24]
c8: f94003e1 ldr x1, [sp]
cc: 37f801e2 tbnz w2, #31, 108 <printf+0xc8>
d0: 91003c22 add x2, x1, #0xf
d4: 927df042 and x2, x2, #0xfffffffffffffff8
d8: f90003e2 str x2, [sp]
dc: f9400022 ldr x2, [x1]
while (*s != '\0') {
e0: 39400041 ldrb w1, [x2]
e4: 34fffe41 cbz w1, ac <printf+0x6c>
e8: 90000005 adrp x5, 0 <putchar>
ec: 910000a5 add x5, x5, #0x0
void putchar(char c) { *uart = c; }
f0: f94000a3 ldr x3, [x5]
f4: 39000061 strb w1, [x3]
while (*s != '\0') {
f8: 38401c41 ldrb w1, [x2, #1]!
fc: 35ffffa1 cbnz w1, f0 <printf+0xb0>
switch ((c = *fmt++)) {
100: 91000800 add x0, x0, #0x2
104: 17ffffeb b b0 <printf+0x70>
print(va_arg(ap, const char *));
108: 11002043 add w3, w2, #0x8
10c: b9001be3 str w3, [sp, #24]
110: 7100007f cmp w3, #0x0
114: 54fffdec b.gt d0 <printf+0x90>
118: f94007e1 ldr x1, [sp, #8]
11c: 8b22c021 add x1, x1, w2, sxtw
120: 17ffffef b dc <printf+0x9c>
0000000000000124 <kmain>:
void kmain(void) {
124: 90000001 adrp x1, 0 <putchar>
128: 90000006 adrp x6, 0 <putchar>
12c: 91000021 add x1, x1, #0x0
130: 910000c3 add x3, x6, #0x0
134: a9bf7bfd stp x29, x30, [sp, #-16]!
while (*s != '\0') {
138: 52800a40 mov w0, #0x52 // #82
void kmain(void) {
13c: 910003fd mov x29, sp
void putchar(char c) { *uart = c; }
140: f9400062 ldr x2, [x3]
144: 39000040 strb w0, [x2]
while (*s != '\0') {
148: 38401c20 ldrb w0, [x1, #1]!
14c: 35ffffa0 cbnz w0, 140 <kmain+0x1c>
print("Result: ");
printf("from %s", "printf");
150: 90000001 adrp x1, 0 <putchar>
154: 90000000 adrp x0, 0 <putchar>
158: 91000021 add x1, x1, #0x0
15c: 91000000 add x0, x0, #0x0
160: 94000000 bl 40 <printf>
void putchar(char c) { *uart = c; }
164: f94000c0 ldr x0, [x6]
168: 52800141 mov w1, #0xa // #10
16c: 39000001 strb w1, [x0]
print("\n");
170: a8c17bfd ldp x29, x30, [sp], #16
174: d65f03c0 ret
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
volatile uint8_t *uart = (uint8_t *)0x09000000;
void putchar(char c) { *uart = c; }
void print(const char *s) {
while (*s != '\0') {
putchar(*s);
s++;
}
}
int printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *text;
char c;
while (*fmt) {
if ((c = *fmt++) != '%') {
putchar(c);
continue;
}
switch ((c = *fmt++)) {
case 's':
print(va_arg(ap, const char *));
break;
default:
putchar(c);
}
}
va_end(ap);
return 0;
}
void kmain(void) {
print("Result: ");
printf("from %s", "printf");
print("\n");
}
ENTRY(_start)
SECTIONS {
. = 0x40000000;
.startup . : { boot.o(.text) }
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss COMMON) }
. = ALIGN(0x10);
. += 0x10000;
stack_top = .;
}
all: kernel.elf
boot.o: boot.s
aarch64-elf-as boot.s -o boot.o
kernel.o: kernel.c
aarch64-elf-gcc -O0 -g -ffreestanding -c kernel.c -o kernel.o
kernel.elf: kernel.o boot.o
aarch64-elf-ld -nostdlib -Tlinker.ld boot.o kernel.o -o kernel.elf
clean:
rm -rf kernel.elf *.o
kernel.asm: kernel.o
aarch64-elf-objdump -S kernel.o > kernel.asm
qemu: kernel.elf
qemu-system-aarch64 -cpu cortex-a53 -kernel kernel.elf -d int -M virt -nographic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment