tyfkda / xcc

Standalone C compiler/assembler/linker/libc for x86-64/aarch64/riscv64/wasm
https://tyfkda.github.io/xcc/
MIT License
193 stars 14 forks source link

Fail to build on Ubuntu-18-04 #162

Closed mingodad closed 1 month ago

mingodad commented 1 month ago

When trying to build on Ubuntu-18.04 I'm getting several errors due to *_RISCV_* not defined, with the changes show bellow I managed to build it but probably the final solution should be something different.

diff --git a/src/as/emit_elf.c b/src/as/emit_elf.c
index 41dace50..0455848f 100644
--- a/src/as/emit_elf.c
+++ b/src/as/emit_elf.c
@@ -201,6 +201,7 @@ static void construct_relas(Vector *unresolved, Symtab *symtab, Table *label_tab
       break;
     case UNRES_RISCV_BRANCH:
     case UNRES_RISCV_RVC_BRANCH:
+#ifdef R_RISCV_RVC_BRANCH
       {
         Elf64_Sym *sym = symtab_add(symtab, u->label);
         size_t index = sym - symtab->buf;
@@ -210,8 +211,8 @@ static void construct_relas(Vector *unresolved, Symtab *symtab, Table *label_tab
                                                                              : R_RISCV_BRANCH);
         rela->r_addend = u->add;
       }
+#endif
       break;
-
     case UNRES_CALL:
       {
         int symidx = symtab_find(symtab, u->label);
@@ -270,9 +271,9 @@ static void construct_relas(Vector *unresolved, Symtab *symtab, Table *label_tab
     case UNRES_X64_GOT_LOAD:
       assert(!"TODO");
       break;
-
     case UNRES_RISCV_JAL:
     case UNRES_RISCV_RVC_JUMP:
+#ifdef R_RISCV_JAL
       {
         int symidx = symtab_find(symtab, u->label);
         assert(symidx >= 0);
@@ -281,13 +282,16 @@ static void construct_relas(Vector *unresolved, Symtab *symtab, Table *label_tab
         rela->r_info = ELF64_R_INFO(symidx, u->kind == UNRES_RISCV_JAL ? R_RISCV_JAL : R_RISCV_RVC_JUMP);
         rela->r_addend = u->add;
       }
+#endif
       break;
     case UNRES_RISCV_RELAX:
+#ifdef R_RISCV_RELAX
       {
         rela->r_offset = u->offset;
         rela->r_info = ELF64_R_INFO(0, R_RISCV_RELAX);
         rela->r_addend = u->add;
       }
+#endif
       break;
     }
   }
diff --git a/src/ld/ld.c b/src/ld/ld.c
index 032a865d..c56c130d 100644
--- a/src/ld/ld.c
+++ b/src/ld/ld.c
@@ -303,7 +303,7 @@ static void resolve_rela_elfobj(LinkEditor *ld, ElfObj *elfobj) {
       case R_AARCH64_LD64_GOT_LO12_NC:
         assert(!"TODO: Implement");
         break;
-
+#ifdef R_RISCV_CALL
       case R_RISCV_CALL:
         {
           int64_t offset = address - pc;
@@ -399,6 +399,7 @@ static void resolve_rela_elfobj(LinkEditor *ld, ElfObj *elfobj) {
           }
         }
         break;
+#endif

       default: assert(false); break;
       }
mingodad commented 1 month ago

Also when trying the playground I'm getting this errors on the console:

Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'interest-cohort'.
main.js:1 Alpine Expression Error: undefined

Expression: "runCode()"

 <span class=​"run-btn busy" :class=​"{busy:​busy, loading:​!loaded}​" @click=​"runCode()​" x-text=​"`${runMode}​ (⌘+⏎)​`">​Run (⌘+⏎)​</span>​
oe @ main.js:1
(anonymous) @ main.js:1
Promise.catch (async)
(anonymous) @ main.js:1
se @ main.js:1
(anonymous) @ main.js:1
s @ main.js:1
(anonymous) @ main.js:1
(anonymous) @ main.js:1
main.js:1 Uncaught String {"Error: ENOENT: no such file or directory, open '/usr/bin/cc'", el: span.run-btn.busy, expression: 'runCode()'}
(anonymous) @ main.js:1
setTimeout (async)
oe @ main.js:1
(anonymous) @ main.js:1
Promise.catch (async)
(anonymous) @ main.js:1
se @ main.js:1
(anonymous) @ main.js:1
s @ main.js:1
(anonymous) @ main.js:1
(anonymous) @ main.js:1
tyfkda.github.io/:1 Uncaught (in promise) Error: ENOENT: no such file or directory, open '/usr/bin/cc'
Promise.then (async)
pe @ main.js:1
(anonymous) @ main.js:1
se @ main.js:1
(anonymous) @ main.js:1
s @ main.js:1
(anonymous) @ main.js:1
(anonymous) @ main.js:1
tyfkda commented 1 month ago

@mingodad Thank you to report the compile error.

Could you use conditional compilation with XCC_TARGET_ARCH ?

    switch (u->kind) {
    // Common cases
    ...

#if XCC_TARGET_ARCH == XCC_ARCH_X64
    // x64 specific cases
#elif XCC_TARGET_ARCH == XCC_ARCH_AARCH64
    // aarch64 specific cases
#elif XCC_TARGET_ARCH == XCC_ARCH_RISCV64
    case UNRES_RISCV_BRANCH:
    case UNRES_RISCV_RVC_BRANCH:
      ...
    case UNRES_CALL:
      ...
tyfkda commented 1 month ago

Also when trying the playground I'm getting this errors on the console:

I've fixed the error. It is caused because wccfiles.zip is broken (gulp5 encodes binary file as UTF-8.)

mingodad commented 1 month ago

Could you use conditional compilation with XCC_TARGET_ARCH ?

Not the way you've showed because the compiler would complain about cases not managed in switch statement.