list.c
lines 1 to 42 of 42
1
// A linked list on the heap: built, reversed, summed. `list_done`
2
// finished list, the place for a tracer to look at it.
3
#include
<stdio.h>
4
#include
<stdlib.h>
5
6
struct
node
{
7
long
value
;
8
struct
node
*
next
;
9
}
;
10
11
__attribute__
(
(
noinline
)
)
static
struct
node
*
push
(
struct
node
*
he
…
12
struct
node
*
n
=
malloc
(
sizeof
*
n
)
;
13
n
-
>
value
=
value
;
14
n
-
>
next
=
head
;
15
return
n
;
16
}
17
18
__attribute__
(
(
noinline
)
)
static
struct
node
*
reverse
(
struct
node
…
19
struct
node
*
prev
=
NULL
;
20
while
(
head
)
{
21
struct
node
*
next
=
head
-
>
next
;
22
head
-
>
next
=
prev
;
23
prev
=
head
;
24
head
=
next
;
25
}
26
return
prev
;
27
}
28
29
__attribute__
(
(
noinline
)
)
long
list_done
(
struct
node
*
head
)
{
30
long
sum
=
0
;
31
for
(
;
head
;
head
=
head
-
>
next
)
sum
+
=
head
-
>
value
;
32
return
sum
;
33
}
34
35
int
main
(
int
argc
,
char
*
*
argv
)
{
36
int
n
=
argc
>
1
?
atoi
(
argv
[
1
]
)
:
6
;
37
struct
node
*
head
=
NULL
;
38
for
(
int
i
=
n
;
i
>
=
1
;
i
-
-
)
head
=
push
(
head
,
i
*
i
)
;
39
head
=
reverse
(
head
)
;
40
printf
(
"sum = %ld\n"
,
list_done
(
head
)
)
;
41
return
0
;
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
disassembly
reverse
1001
8a8
39
mov
rdi, rax
main
1001
8ab
call
1001910
1001
8b0
40
mov
rdi, rax
1001
8b3
call
1001830
1001
8b8
lea
rdi, [rip - 0x12cb]
1001
8bf
mov
rsi, rax
1001
8c2
xor
eax, eax
1001
8c4
call
1001990
printf
1001
8c9
41
xor
eax, eax
1001
8cb
add
rsp, 8
1001
8cf
pop
rbx
1001
8d0
pop
rbp
1001
8d1
ret
1001
8d2
nop
word ptr cs:[rax + rax]
1001
8e0
11
push
rbp
push
1001
8e1
mov
rbp, rsp
1001
8e4
push
r14
1001
8e6
push
rbx
1001
8e7
mov
rbx, rsi
1001
8ea
mov
r14, rdi
1001
8ed
12
mov
edi, 0x10
1001
8f2
call
10019a0
malloc
1001
8f7
13
mov
qword ptr [rax], rbx
1001
8fa
14
mov
qword ptr [rax + 8], r14
1001
8fe
15
pop
rbx
1001
8ff
pop
r14
1001
901
pop
rbp
1001
902
ret
1001
903
nop
word ptr cs:[rax + rax]
1001
910
20
test
rdi, rdi
1001
913
je
1001935
1001
915
push
rbp
1001
916
mov
rbp, rsp
1001
919
xor
eax, eax
1001
91b
nop
dword ptr [rax + rax]
1001
920
mov
rcx, rax
1001
923
mov
rax, rdi
1001
926
21
mov
rdi, qword ptr [rdi + 8]
1004098
1004070
9
1001
92a
22
mov
qword ptr [rax + 8], rcx
1001
92e
20
test
rdi, rdi
1001
931
jne
1001920
1001
933
pop
rbp
1001
934
26
ret
1001
935
xor
eax, eax
1001
937
ret
1001
938
endbr64
_init
1001
93c
sub
rsp, 8
1001
940
mov
rax, qword ptr [rip + 0x1231]
1001
947
test
rax, rax
1001
94a
je
100194e
1001
94c
call
rax
1001
94e
add
rsp, 8
1001
952
ret
1001
954
endbr64
_fini
1001
958
sub
rsp, 8
1001
95c
add
rsp, 8
1001
960
ret
1001
970
push
qword ptr [rip + 0x1222]
strtol@plt-0x10
1001
976
jmp
qword ptr [rip + 0x1224]
1001
97c
nop
dword ptr [rax]
1001
980
jmp
qword ptr [rip + 0x1222]
strtol@plt
1001
986
push
0
1001
98b
jmp
1001970
1001
990
jmp
qword ptr [rip + 0x121a]
printf@plt
1001
996
push
1
1001
99b
jmp
1001970
1001
9a0
jmp
qword ptr [rip + 0x1212]
malloc@plt
1001
9a6
push
2
1001
9ab
jmp
1001970
registers
stack
heap
code
data
rwx
read-only
rax
1004090
4
rbx
0
rcx
10040b0
1
rdx
0
rsi
10040c0
0
rdi
1004090
4
rbp
7fffffffece0
7fffffffed00
7fffffffedb0
7fffffffee10
0
rsp
7fffffffece0
7fffffffed00
7fffffffedb0
7fffffffee10
0
r8
7ffff7e19ac0
r9
0x20
32
r10
0x10
16
r11
7ffff7e19ac8
r12
7fffffffee38
7fffffffefea
"./list"
r13
1
r14
7ffff7ffd000
7ffff7ffe3c0
0
r15
10029c0
1
rip
1001926
reverse+22
cf
PF
af
zf
sf
IF
df
of
backtrace
newest on top
#0
reverse+22
list.c:21
#1
main+80
list.c:40
#2
libc+27781
#3
__libc_start_main+137
#4
_start+37
memory
the heap, round what the step reads
10040
00
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
10
24
00
00
00
00
00
00
00
36
00
00
00
00
00
00
00
00
NULL
10040
20
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
30
19
00
00
00
00
00
00
00
25
10
40
00
01
00
00
00
00
1004010
10040
40
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
50
10
00
00
00
00
00
00
00
16
30
40
00
01
00
00
00
00
1004030
10040
60
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
70
09
00
00
00
00
00
00
00
9
50
40
00
01
00
00
00
00
1004050
10040
80
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
90
04
00
00
00
00
00
00
00
4
70
40
00
01
00
00
00
00
1004070
10040
a0
00
00
00
00
00
00
00
00
21
00
00
00
00
00
00
00
malloc's: a chunk of 32
10040
b0
01
00
00
00
00
00
00
00
1
00
00
00
00
00
00
00
00
NULL
10040
c0
00
00
00
00
00
00
00
00
41
0f
02
00
00
00
00
00
malloc's: the top chunk
19 rows
stack
69 words from rsp · the frames they belong to
rbp rsp
+0
saved rbp
7fffffffe
ce0
7fffffffed00
+8
7fffffffe
ce8
10018b0
main+80
+10
7fffffffe
cf0
7ffff7e20de8
+18
saved rbx
7fffffffe
cf8
0
+20
saved rbp
7fffffffe
d00
7fffffffedb0
+28
7fffffffe
d08
7ffff7c27781
libc+27781
+30
7fffffffe
d10
7ffff7fe0c60
ld.so+22c60
+38
7fffffffe
d18
7fffffffee38
7fffffffefea
".…
+40
7fffffffe
d20
0x1f7fbe000
+48
7fffffffe
d28
1001860
main+0
+50
7fffffffe
d30
7fffffffed70
+58
7fffffffe
d38
+60
7fffffffe
d40
0
+68
7fffffffe
d48
0x1fe335fc95a5781c
+70
7fffffffe
d50
7fffffffee38
7fffffffefea
".…
+78
7fffffffe
d58
1
+80
7fffffffe
d60
7ffff7ffd000
+88
7fffffffe
d68
10029c0
+90
7fffffffe
d70
0x1fe335fc94e5781c
+98
7fffffffe
d78
0x1fe32587a0af781c
+a0
7fffffffe
d80
0x7fff00000000
+a8
7fffffffe
d88
0
+b0
7fffffffe
d90
0
+b8
7fffffffe
d98
0
+c0
7fffffffe
da0
7fffffffee38
7fffffffefea
".…
+c8
7fffffffe
da8
0x15f6f7c6b8dafb00
+d0
saved rbp
7fffffffe
db0
7fffffffee10
0
+d8
7fffffffe
db8
+e0
7fffffffe
dc0
7fffffffee48
0
+e8
7fffffffe
dc8
10029c0
+f0
7fffffffe
dd0
1001860
main+0
+f8
7fffffffe
dd8
7fffffffee48
0
+100
7fffffffe
de0
0
+108
7fffffffe
de8
0
+110
7fffffffe
df0
1001730
_start+0
+118
7fffffffe
df8
7fffffffee30
1
+120
7fffffffe
e00
0
+128
7fffffffe
e08
0
+130
saved rbp
7fffffffe
e10
0
+138
7fffffffe
e18
1001755
_start+37
+140
7fffffffe
e20
7fffffffee28
0x38
56
+148
7fffffffe
e28
0x38
56
+150
argc
7fffffffe
e30
1
+158
argv[0]
7fffffffe
e38
7fffffffefea
"./list"
+160
argv end
7fffffffe
e40
0
+168
envp end
7fffffffe
e48
0
+170
auxv
7fffffffe
e50
0x21
33
+178
7fffffffe
e58
7ffff7fbc000
vdso+0
+180
7fffffffe
e60
0x33
51
+188
7fffffffe
e68
0xe30
3632
+190
7fffffffe
e70
0x10
16
+198
7fffffffe
e78
0xbfebfbff
+1a0
7fffffffe
e80
6
+1a8
7fffffffe
e88
0x1000
4096
+1b0
7fffffffe
e90
0x11
17
+1b8
7fffffffe
e98
0x64
100
+1c0
7fffffffe
ea0
3
+1c8
7fffffffe
ea8
1000040
+1d0
7fffffffe
eb0
4
+1d8
7fffffffe
eb8
0x38
56
+1e0
7fffffffe
ec0
5
+1e8
7fffffffe
ec8
0xb
11
+1f0
7fffffffe
ed0
7
+1f8
7fffffffe
ed8
0x7ffff7fbe000
+200
7fffffffe
ee0
8
+208
7fffffffe
ee8
0
+210
7fffffffe
ef0
9
+218
7fffffffe
ef8
1001730
_start+0
+220
7fffffffe
f00
0xb
11
reverse
main
libc+27781
__libc_start_main
_start
moment 2 of 4
Stopped in reverse, about to load head->next.
rdi points at a node; the word after its value is the next node.