Skip to content

Commit 6071313

Browse files
committed
Initial commit of echo
0 parents  commit 6071313

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2020 Brian Callahan <[email protected]>
2+
3+
Permission to use, copy, modify, and distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# echo Makefile
2+
3+
CC = cc
4+
CFLAGS = -Oz -nostdinc -fomit-frame-pointer
5+
CFLAGS += -fno-PIE -fno-PIC -fno-ret-protector
6+
CFLAGS += -fno-stack-protector -mno-retpoline
7+
CFLAGS += -fno-asynchronous-unwind-tables
8+
CFLAGS += -Wno-int-to-void-pointer-cast
9+
10+
PROG = echo
11+
OBJS = _start.o _syscall.o crt.o echo.o
12+
13+
all: ${OBJS}
14+
/usr/bin/ld -nopie -o ${PROG} ${OBJS}
15+
/usr/bin/strip ${PROG}
16+
/usr/bin/strip -R .comment ${PROG}
17+
18+
clean:
19+
rm -rf ${PROG} ${OBJS} ${PROG}.core ${PROG}~

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
echo
2+
====
3+
echo is a reimplementation of the echo(1) utility without libc.
4+
It is used to teach how argc and argv are passed in Unix.
5+
6+
Why?
7+
----
8+
Read my
9+
[blog post](https://briancallahan.net/blog/20200808.html)
10+
about this codebase.
11+
12+
Building
13+
--------
14+
Just run `make`.
15+
This will probably only run on
16+
[OpenBSD](https://www.openbsd.org)/amd64
17+
as is.
18+
Feel free to port to your Unix of choice.
19+
20+
License
21+
-------
22+
ISC License.
23+
See `LICENSE` for details.

_start.s

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2020 Brian Callahan <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
.text
18+
.p2align 2
19+
.globl _start
20+
_start:
21+
popq %rdi
22+
movq %rsp, %rsi
23+
movq 8(%rsp, %rdi, 8), %rdx
24+
callq main
25+
movl %eax, %edi
26+
movl $1, %eax
27+
syscall
28+
.size _start,.-_start

_syscall.s

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2020 Brian Callahan <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
.text
18+
.p2align 2
19+
.globl _syscall
20+
_syscall:
21+
movq %rdi, %rax
22+
movq %rsi, %rdi
23+
movq %rdx, %rsi
24+
movq %rcx, %rdx
25+
movq %r8, %rcx
26+
movq %r9, %r8
27+
syscall
28+
retq
29+
.size _syscall,.-_syscall

crt.s

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 Brian Callahan <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
.section ".note.openbsd.ident", "a"
18+
.p2align 2
19+
.long 0x8
20+
.long 0x4
21+
.long 0x1
22+
.asciz "OpenBSD"
23+
.long 0x0
24+
.previous

echo.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2020 Brian Callahan <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
/*
18+
* echo -- print out argv
19+
*/
20+
21+
extern void *_syscall(void *n, void *a, void *b, void *c, void *d, void *e);
22+
23+
static void
24+
write(int d, const void *buf, unsigned long nbytes)
25+
{
26+
27+
_syscall((void *) 4, (void *) d, (void *) buf, (void *) nbytes, (void *) 0, (void *) 0);
28+
}
29+
30+
static unsigned long
31+
strlen(const char *s)
32+
{
33+
char *t;
34+
35+
t = (char *) s;
36+
while (*t != '\0')
37+
t++;
38+
39+
return t - s;
40+
}
41+
42+
int
43+
main(int argc, char *argv[])
44+
{
45+
int i;
46+
47+
for (i = 1; i < argc; i++) {
48+
write(1, argv[i], strlen(argv[i]));
49+
if (i + 1 != argc)
50+
write(1, " ", 1);
51+
}
52+
write(1, "\n", 1);
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)