Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full bootstrap? #1

Closed
Dawid33 opened this issue Jul 1, 2023 · 2 comments
Closed

Full bootstrap? #1

Dawid33 opened this issue Jul 1, 2023 · 2 comments

Comments

@Dawid33
Copy link
Contributor

Dawid33 commented Jul 1, 2023

I really like this project, really amazing job.

In the 05/README.md it says to you can compile tcc with the tcc0 binary which works but when I run that command with the -MD flag the resulting file says that it uses a lot of system level headers / libraries. I think the musl build is more self contained so I built that and attempted to build tcc using tcc0 with the -nostdinc and -nostdlib flags without much luck.

The contents of tcc1.d after adding -MD (./tcc0 -MD tcc.c -o tcc1)
tcc1: \
 /usr/lib/x86_64-linux-gnu/crt1.o \
 /usr/lib/x86_64-linux-gnu/crti.o \
 tcc.c \
 tcc.h \
 config.h \
 /usr/include/stdlib.h \
 /usr/include/bits/libc-header-start.h \
 /usr/include/features.h \
 /usr/include/features-time64.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/timesize.h \
 /usr/include/bits/wordsize.h \
 /usr/include/stdc-predef.h \
 /usr/include/sys/cdefs.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/long-double.h \
 /usr/include/gnu/stubs.h \
 /usr/include/gnu/stubs-64.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/waitflags.h \
 /usr/include/bits/waitstatus.h \
 /usr/include/bits/floatn.h \
 /usr/include/bits/floatn-common.h \
 /usr/include/bits/long-double.h \
 /usr/include/bits/types/locale_t.h \
 /usr/include/bits/types/__locale_t.h \
 /usr/include/sys/types.h \
 /usr/include/bits/types.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/timesize.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/typesizes.h \
 /usr/include/bits/time64.h \
 /usr/include/bits/types/clock_t.h \
 /usr/include/bits/types/clockid_t.h \
 /usr/include/bits/types/time_t.h \
 /usr/include/bits/types/timer_t.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/stdint-intn.h \
 /usr/include/endian.h \
 /usr/include/bits/endian.h \
 /usr/include/bits/endianness.h \
 /usr/include/bits/byteswap.h \
 /usr/include/bits/uintn-identity.h \
 /usr/include/sys/select.h \
 /usr/include/bits/select.h \
 /usr/include/bits/types/sigset_t.h \
 /usr/include/bits/types/__sigset_t.h \
 /usr/include/bits/types/struct_timeval.h \
 /usr/include/bits/types/struct_timespec.h \
 /usr/include/bits/pthreadtypes.h \
 /usr/include/bits/thread-shared-types.h \
 /usr/include/bits/pthreadtypes-arch.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/atomic_wide_counter.h \
 /usr/include/bits/struct_mutex.h \
 /usr/include/bits/struct_rwlock.h \
 /usr/include/alloca.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/stdlib-float.h \
 /usr/include/stdio.h \
 /usr/include/bits/libc-header-start.h \
 ../tcc-bootstrap/include/stddef.h \
 ../tcc-bootstrap/include/stdarg.h \
 /usr/include/bits/types/__fpos_t.h \
 /usr/include/bits/types/__mbstate_t.h \
 /usr/include/bits/types/__fpos64_t.h \
 /usr/include/bits/types/__FILE.h \
 /usr/include/bits/types/FILE.h \
 /usr/include/bits/types/struct_FILE.h \
 /usr/include/bits/types/cookie_io_functions_t.h \
 /usr/include/bits/stdio_lim.h \
 /usr/include/string.h \
 /usr/include/bits/libc-header-start.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/strings.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/errno.h \
 /usr/include/bits/errno.h \
 /usr/include/linux/errno.h \
 /usr/include/asm/errno.h \
 /usr/include/asm-generic/errno.h \
 /usr/include/asm-generic/errno-base.h \
 /usr/include/bits/types/error_t.h \
 /usr/include/math.h \
 /usr/include/bits/libc-header-start.h \
 /usr/include/bits/math-vector.h \
 /usr/include/bits/libm-simd-decl-stubs.h \
 /usr/include/bits/flt-eval-method.h \
 /usr/include/bits/fp-logb.h \
 /usr/include/bits/fp-fast.h \
 /usr/include/bits/mathcalls-helper-functions.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls-helper-functions.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls-helper-functions.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/mathcalls-narrow.h \
 /usr/include/bits/iscanonical.h \
 ../tcc-bootstrap/include/float.h \
 /usr/include/fcntl.h \
 /usr/include/bits/fcntl.h \
 /usr/include/bits/fcntl-linux.h \
 /usr/include/bits/types/struct_iovec.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/linux/falloc.h \
 /usr/include/bits/stat.h \
 /usr/include/bits/struct_stat.h \
 /usr/include/setjmp.h \
 /usr/include/bits/setjmp.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/types/struct___jmp_buf_tag.h \
 /usr/include/time.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/time.h \
 /usr/include/bits/timex.h \
 /usr/include/bits/types/struct_tm.h \
 /usr/include/bits/types/struct_itimerspec.h \
 /usr/include/unistd.h \
 /usr/include/bits/posix_opt.h \
 /usr/include/bits/environments.h \
 /usr/include/bits/wordsize.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/confname.h \
 /usr/include/bits/getopt_posix.h \
 /usr/include/bits/getopt_core.h \
 /usr/include/bits/unistd_ext.h \
 /usr/include/sys/time.h \
 libtcc.h \
 elf.h \
 stab.h \
 stab.def \
 x86_64-gen.c \
 x86_64-link.c \
 tcctok.h \
 libtcc.c \
 tccpp.c \
 tcctok.h \
 tccgen.c \
 tccelf.c \
 tccrun.c \
 /usr/include/sys/mman.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/mman.h \
 /usr/include/bits/mman-map-flags-generic.h \
 /usr/include/bits/mman-linux.h \
 /usr/include/bits/mman-shared.h \
 /usr/include/signal.h \
 /usr/include/bits/signum-generic.h \
 /usr/include/bits/signum-arch.h \
 /usr/include/bits/types/sig_atomic_t.h \
 /usr/include/bits/types/siginfo_t.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/types/__sigval_t.h \
 /usr/include/bits/siginfo-arch.h \
 /usr/include/bits/siginfo-consts.h \
 /usr/include/bits/siginfo-consts-arch.h \
 /usr/include/bits/types/sigval_t.h \
 /usr/include/bits/types/sigevent_t.h \
 /usr/include/bits/wordsize.h \
 /usr/include/bits/sigevent-consts.h \
 /usr/include/bits/sigaction.h \
 /usr/include/bits/sigcontext.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/bits/types/stack_t.h \
 ../tcc-bootstrap/include/stddef.h \
 /usr/include/sys/ucontext.h \
 /usr/include/bits/sigstack.h \
 /usr/include/bits/sigstksz.h \
 /usr/include/bits/ss_flags.h \
 /usr/include/bits/types/struct_sigstack.h \
 /usr/include/bits/sigthread.h \
 /usr/include/bits/signal_ext.h \
 x86_64-gen.c \
 /usr/include/assert.h \
 x86_64-link.c \
 i386-asm.c \
 x86_64-asm.h \
 x86_64-asm.h \
 tccasm.c \
 tcctools.c \
 /usr/lib/x86_64-linux-gnu/libc.so \
 /lib/x86_64-linux-gnu/libc.so.6 \
 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 \
 /usr/lib/x86_64-linux-gnu/libc_nonshared.a \
 /lib/x86_64-linux-gnu/libc.so.6 \
 /usr/lib/x86_64-linux-gnu/libc_nonshared.a \
 ../tcc-bootstrap/libtcc1.a \
 /usr/lib/x86_64-linux-gnu/crtn.o \

Here is what I did. I really just commented out one thing the config.h and added a few flags here. The rest of the changes are to make it slightly easier to compile. It fails with this error

The error.
cd tcc-0.9.27 && ./tcc0 -nostdinc -nostdlib -B ../tcc-boostrap -L../musl-bootstrap/lib -lc -I ../musl-bootstrap/include tcc.c -o tcc1
In file included from tcc.c:21:
In file included from tcc.h:293:
elf.h:43: error: ';' expected (got "Elf32_Half")
Error: Tried to longjmp from libtcc.c:548 with value 1
And the offending file.

if you revert those two lines then it works again but it links against the system libc files. I'm not very good with this stuff so I just wanted to ask if you tried something like this already or if I'm missing something obvious.

@pommicket
Copy link
Owner

pommicket commented Jul 3, 2023

You just need a few modifications to tcc's source to get this to work: see 3384d69

The build command

./tcc0 -nostdinc -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include tcc.c ../musl-bootstrap/lib/*.[oa] -o tcc1

works after those modifications. And adding -MD reveals that the build is entirely self-contained:

tcc1: \
 tcc.c \
 tcc.h \
 config.h \
 ../musl-bootstrap/include/stdlib.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/stdarg.h \
 ../musl-bootstrap/include/bits/wexitstatus.h \
 ../musl-bootstrap/include/stdio.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/string.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/errno.h \
 ../musl-bootstrap/include/bits/errno.h \
 ../musl-bootstrap/include/math.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/float.h \
 ../musl-bootstrap/include/bits/float.h \
 ../musl-bootstrap/include/fcntl.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/bits/fcntl.h \
 ../musl-bootstrap/include/setjmp.h \
 ../musl-bootstrap/include/bits/setjmp.h \
 ../musl-bootstrap/include/time.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/unistd.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/bits/posix.h \
 ../musl-bootstrap/include/sys/time.h \
 ../musl-bootstrap/include/sys/select.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 libtcc.h \
 elf.h \
 ../musl-bootstrap/include/inttypes.h \
 ../musl-bootstrap/include/stdint.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 ../musl-bootstrap/include/bits/stdint.h \
 ../musl-bootstrap/include/bits/alltypes.h \
 stab.h \
 stab.def \
 x86_64-gen.c \
 x86_64-link.c \
 tcctok.h \
 libtcc.c \
 tccpp.c \
 tcctok.h \
 tccgen.c \
 tccelf.c \
 tccrun.c \
 x86_64-gen.c \
 ../musl-bootstrap/include/assert.h \
 x86_64-link.c \
 i386-asm.c \
 x86_64-asm.h \
 x86_64-asm.h \
 tccasm.c \
 tcctools.c \
 ../musl-bootstrap/lib/libc.a \
 ../musl-bootstrap/lib/crt1.o \
 ../musl-bootstrap/lib/crti.o \
 ../musl-bootstrap/lib/crtn.o \

Though I think even with this building gcc or glibc in a fully self-contained way will be very difficult...

Anyways I'm glad you like this project! I had a lot of fun making it :)

@Dawid33
Copy link
Contributor Author

Dawid33 commented Jul 4, 2023

Thank you! My goal was to compile lua with that tcc + musl combination and I got it working so I'm happy with that. Building gcc or glibc would require a functional c++ compiler among other things so trying to bootstrap a gnu environment is very hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants