On Sat, Jan 21, 2023 at 07:03:52AM +0100, tccm wrote:
> I will test your patch and report here.
Hello Brian,
Many thanks for your code, it helped a lot.
I did not use the upstream build scripts/tools (including configure and
Makefile), they are not applicable on older platforms. IOW I did not
test this part of your patch.
The attached variant of elf2aout.c and a corresponding patch to other
files have been successfully tested on Minix-2.i386 and Minix-vmd.
After a bootstrap the compiler becomes self-hosted.
(The C standard library is rebuilt with Tiny CC to be available as elf)
This code produces stripped "common I" executables (symtab conversion
is skipped) with a 1MB stack+memory limit which afterwards can be adjusted
with Minix chmem utility.
I think that the changes as in the patch are very light and could be added
to upstream (suitably ifdef-conditioned) without adding any remarkable
weight to tcc. Using current configure to activate them can not work on
Minix-2 (unfortunately) but this does not make them unusable.
elf2aout.c contains platform-specific details. It works for Minix-2
and Minix-vmd but would need some ifdefs for other platforms. Would it
harm to add it to upstream, as a reference for possible future needs
and developers?
Improvements are of course possible, not least supporting separate I
or handling symbol tables, but this was beyond my current needs and is
left for a possible later exercise.
Thanks again, it is very useful to have a fast and compact C99-toolchain
on these lightweight kernels / OS.
Cheers
/tccm
diff -u -r a/tinycc-20a1ebf/i386-link.c b/tinycc-20a1ebf/i386-link.c
--- a/tinycc-20a1ebf/i386-link.c 2022-08-18 09:34:36.0 +
+++ b/tinycc-20a1ebf/i386-link.c 2023-02-03 13:07:51.488566020 +
@@ -12,7 +12,7 @@
#define R_NUM R_386_NUM
-#define ELF_START_ADDR 0x08048000
+#define ELF_START_ADDR 0
#define ELF_PAGE_SIZE 0x1000
#define PCRELATIVE_DLLPLT 0
Only in b/tinycc-20a1ebf: i386-link.c.
Only in b/tinycc-20a1ebf: i386-link.c_
diff -u -r a/tinycc-20a1ebf/libtcc.c b/tinycc-20a1ebf/libtcc.c
--- a/tinycc-20a1ebf/libtcc.c 2022-08-18 09:34:36.0 +
+++ b/tinycc-20a1ebf/libtcc.c 2023-02-03 13:07:51.468566019 +
@@ -818,6 +818,8 @@
s->ppfp = stdout;
/* might be used in error() before preprocess_start() */
s->include_stack_ptr = s->include_stack;
+/* do not try dynamic linking on this platform */
+s->static_link = 1;
tccelf_new(s);
diff -u -r a/tinycc-20a1ebf/tcc.c b/tinycc-20a1ebf/tcc.c
--- a/tinycc-20a1ebf/tcc.c 2022-08-18 09:34:36.0 +
+++ b/tinycc-20a1ebf/tcc.c 2023-02-03 13:07:51.456566019 +
@@ -397,6 +397,15 @@
if (done && 0 == t && 0 == ret && s->do_bench)
tcc_print_stats(s, end_time - start_time);
+#ifdef TCC_GENERATE_AOUT
+{ int e2a;
+if (s->output_type == TCC_OUTPUT_EXE) {
+if ((e2a = elf2aout(s->outfile)) != 0)
+return e2a;
+}
+}
+#endif
+
tcc_delete(s);
if (!done)
goto redo; /* compile more files with -c */
diff -u -r a/tinycc-20a1ebf/tccgen.c b/tinycc-20a1ebf/tccgen.c
--- a/tinycc-20a1ebf/tccgen.c 2022-08-18 09:34:36.0 +
+++ b/tinycc-20a1ebf/tccgen.c 2023-02-03 13:07:51.476566020 +
@@ -1731,7 +1731,7 @@
(vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
/* CPUs usually cannot use float constants, so we store them
generically in data segment */
-init_params p = { rodata_section };
+init_params p = { data_section };
unsigned long offset;
size = type_size(>type, );
if (NODATA_WANTED)
@@ -5344,7 +5344,7 @@
mk_pointer();
type.t |= VT_ARRAY;
type.ref->c = len;
-sec = rodata_section;
+sec = data_section;
vpush_ref(, sec, sec->data_offset, len);
if (!NODATA_WANTED)
memcpy(section_ptr_add(sec, len), funcname, len);
@@ -5368,7 +5368,7 @@
mk_pointer();
type.t |= VT_ARRAY;
memset(, 0, sizeof(AttributeDef));
-ad.section = rodata_section;
+ad.section = data_section;
decl_initializer_alloc(, , VT_CONST, 2, 0, 0);
break;
case '(':
@@ -7944,7 +7944,7 @@
while ((tp->t & (VT_BTYPE|VT_ARRAY)) == (VT_PTR|VT_ARRAY))
tp = >ref->type;
if (tp->t & VT_CONSTANT) {
- sec = rodata_section;
+ sec = data_section;
} else if (has_init) {
sec = data_section;
/*if (tcc_state->g_debug & 4)
diff -u -r a/tinycc-20a1ebf/tcc.h b/tinycc-20a1ebf/tcc.h
--- a/tinycc-20a1ebf/tcc.h 2022-08-18 09:34:36.0 +
+++ b/tinycc-20a1ebf/tcc.h 2023-02-03 13:07:51.436566019 +
@@ -1815,6 +1815,12 @@
#define dwarf_str_section s1->dwarf_str_section
#define dwarf_line_str_section s1->dwarf_line_str_section
+/* elf2aout.c */
+#ifdef