├── src ├── false.nim ├── true.nim ├── panicoverride.nim ├── nim.cfg ├── env.nim ├── cat.nim ├── echo.nim └── framework.nim ├── .gitignore ├── tools ├── x86 │ ├── script.ld │ ├── start.s │ └── elf.s └── x86_64 │ ├── script.ld │ ├── start.s │ └── elf.s ├── readme.md └── nakefile.nim /src/false.nim: -------------------------------------------------------------------------------- 1 | proc main: int = 1 2 | -------------------------------------------------------------------------------- /src/true.nim: -------------------------------------------------------------------------------- 1 | proc main: int = 0 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !*/ 3 | !*.* 4 | nimcache/ 5 | *.swp 6 | *.c 7 | *.bin 8 | *.o 9 | -------------------------------------------------------------------------------- /src/panicoverride.nim: -------------------------------------------------------------------------------- 1 | proc rawoutput(s: string) = discard 2 | proc panic(s: string) = discard 3 | -------------------------------------------------------------------------------- /src/nim.cfg: -------------------------------------------------------------------------------- 1 | os = standalone 2 | noMain 3 | passL = "-nostdlib" 4 | passC = "-fno-stack-protector" 5 | -------------------------------------------------------------------------------- /tools/x86/script.ld: -------------------------------------------------------------------------------- 1 | SECTIONS { 2 | . = 0x400054; 3 | 4 | combined . : AT(0x400054) ALIGN(1) SUBALIGN(1) { 5 | *(.text*) 6 | *(.data*) 7 | *(.rodata*) 8 | *(.bss*) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tools/x86_64/script.ld: -------------------------------------------------------------------------------- 1 | SECTIONS { 2 | . = 0x400078; 3 | 4 | combined . : AT(0x400078) ALIGN(1) SUBALIGN(1) { 5 | *(.text*) 6 | *(.data*) 7 | *(.rodata*) 8 | *(.bss*) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/env.nim: -------------------------------------------------------------------------------- 1 | proc main(argc: int, argv, envp: cstringArray): int = 2 | var buf = CharBuf[4096](fd: stdout) 3 | 4 | for i in 0 .. int.high: 5 | if envp[i] == nil: 6 | break 7 | buf.add envp[i] 8 | buf.add '\l' 9 | buf.flush() 10 | -------------------------------------------------------------------------------- /src/cat.nim: -------------------------------------------------------------------------------- 1 | proc main(argc: int, argv: cstringArray): int = 2 | var 3 | buf = CharBuf[4096](fd: stdout) 4 | start = 1 5 | file = stdin 6 | stdinRead = false 7 | 8 | if argc > 1 and argv[start][0] == '-': 9 | inc start 10 | 11 | for i in start .. 1 and strcmp(argv[start], "-n") == 0: 8 | inc start 9 | newLine = false 10 | 11 | block outer: 12 | for i in start .. b[i]: 53 | return 1 54 | 55 | # Helpers 56 | 57 | type CharBuf[S: static[int]] = object 58 | buf: array[S, char] 59 | pos: int 60 | fd: cint 61 | 62 | proc flush[S](s: var CharBuf[S]) = 63 | write s.fd, s.buf, s.pos 64 | s.pos = 0 65 | 66 | proc add[S](s: var CharBuf[S], x: char) = 67 | s.buf[s.pos] = x 68 | inc s.pos 69 | if s.pos >= S: 70 | s.flush() 71 | 72 | proc add[S](s: var CharBuf[S], xs: varargs[cstring]) = 73 | for x in xs: 74 | for i in 0 .. int.high: 75 | if x[i] == '\0': return 76 | s.add x[i] 77 | 78 | macro includeIt: stmt = 79 | const file = staticExec("echo $file") 80 | result = parseStmt("include " & file) 81 | includeIt() 82 | 83 | when compiles(main()): 84 | proc stdmain {.exportc: "_start".} = 85 | exit main().cint 86 | else: 87 | when defined(x86_64): 88 | {.compile: "tools/x86_64/start.s".} 89 | elif defined(x86): 90 | {.compile: "tools/x86/start.s".} 91 | else: 92 | {.error: "No _start defined for this architecture".} 93 | 94 | proc stdmain(argc: cint, argv: cstringArray) {.exportc.} = 95 | when compiles(main(argc.int, argv)): 96 | exit main(argc.int, argv).cint 97 | else: 98 | let envp = cast[cstringArray](addr argv[argc + 1]) 99 | exit main(argc.int, argv, envp).cint 100 | --------------------------------------------------------------------------------