-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_c_variants.sh
141 lines (86 loc) · 3.62 KB
/
02_c_variants.sh
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
132
133
134
135
136
137
138
139
140
141
#! /bin/bash
# small / 02_c_variants.sh version 20230922
# apadted from https://web.archive.org/web/20160514233312/http://www.diku.dk/hjemmesider/studerende/firefly/emspace-html/node4.html
set -o errexit
et (){ log ; trace "$@" ;} # ------------------------------ et
log (){ 1>&2 echo "$@" ;} # ----------------------------- log
trace (){ log "+ $*" ; "$@" ;} # --------------------------- trace
example_2 () { # -------------------------------------------- example_2
trace dd of='30_example_2.c' status='none' <<EOF
#include <unistd.h>
char hello[] = "Hello, C write dynamic!\n";
int _start () {
write ( 1, hello, sizeof(hello)-1 );
_exit ( 0 ); }
EOF
trace gcc 30_example_2.c -o 31_example_2_dynamic \
-m32 -no-pie -nostartfiles -ffreestanding
trace cp 31_example_2_dynamic 32_example_2_dynamic_stripped
trace strip --strip-all 32_example_2_dynamic_stripped
return ; }
example_4 () { # -------------------------------------------- example_4
# 20230921 I believe this syscall implementation is for 32-bit x86 only.
trace dd of='40_example_4.c' status='none' <<EOF
#include <syscall.h>
void syscall_1 ( int num, int arg1 ) {
asm (
"int\t\$0x80\n\t" :
/* output */ :
/* input */ "a"(num), "b"(arg1)
/* clobbered */ ); }
void syscall_3 ( int num, int arg1, int arg2, int arg3 ) {
asm (
"int\t\$0x80\n\t" :
/* output */ :
/* input */ "a"(num), "b"(arg1), "c"(arg2), "d"(arg3)
/* clobbered */ ); }
char hello[] = "Hello, C asm-syscall static!\n";
int _start () {
syscall_3 ( SYS_write, 1, (int) hello, sizeof(hello)-1);
syscall_1 ( SYS_exit, 0 ); }
EOF
trace gcc 40_example_4.c -o 41_example_4_static \
-m32 -no-pie -nostartfiles -nostdlib -ffreestanding
trace cp 41_example_4_static 42_example_4_static_stripped
trace strip --strip-all 42_example_4_static_stripped
return ; }
example_5 () { # -------------------------------------------- example_5
# example 5 is example 2, modified to allow static compilation.
trace dd of='50_example_5.c' status='none' <<EOF
#include <unistd.h>
char hello[] = "Hello, C write static!\n";
int main () {
write ( 1, hello, sizeof(hello)-1 );
return 0; }
EOF
# 20230921 -ffreestanding has no effect? may be gratuitous?
trace gcc 50_example_5.c -o 51_example_5_static \
-m32 -no-pie -ffreestanding -static
trace cp 51_example_5_static 52_example_5_static_stripped
trace strip --strip-all 52_example_5_static_stripped
if test -x /usr/bin/musl-gcc
then trace musl-gcc 50_example_5.c -o 53_example_5_musl \
-no-pie -ffreestanding -static
trace cp 53_example_5_musl 54_example_5_musl_stripped
trace strip --strip-all 54_example_5_musl_stripped
fi
return ; }
main () { # ------------------------------------------------------ main
et mkdir -p /tmp/small
trace cd /tmp/small
et example_2
et example_4
et example_5
et /tmp/small/31_example_2_dynamic
et /tmp/small/32_example_2_dynamic_stripped
et /tmp/small/41_example_4_static
et /tmp/small/42_example_4_static_stripped
et /tmp/small/51_example_5_static
et /tmp/small/52_example_5_static_stripped
test -x /tmp/small/53_example_5_musl &&
et /tmp/small/53_example_5_musl
test -x /tmp/small/54_example_5_musl_stripped &&
et /tmp/small/54_example_5_musl_stripped
et ls -n -B --color=auto
return ; }
main