llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.59k stars 11.82k forks source link

[MIPS I] STB_LOCAL symbol found at index >= .symtab's sh_info #47406

Open llvmbot opened 3 years ago

llvmbot commented 3 years ago
Bugzilla Link 48062
Version unspecified
OS other
Reporter LLVM Bugzilla Contributor
CC @atanasyan,@smithp35

Extended Description

I am using llvm-project commit 8fc424f26bf1ea1471bd770a1b4eee4545c2bc96 with two additional patches for preliminary MIPS I support (see https://github.com/llvm/llvm-project/compare/release/11.x...XaviDCR92:release/11.x) and zig (https://github.com/ziglang/zig) commit b30a765b95cf666409c1e5324718a7c8c60af4af for an application targeted to the FPU-less MIPS R3000A featured on the original Sony PlayStation. The application uses mipsel-unknown-elf-as for the assembly files, which are later linked by lld when calling zig. The application used to build on llvm-10.x (plus the experimental patches) and older versions of zig, but does no longer build on llvm-11.x and latest zig. After some discussion on the #zig channel, I was encouraged to report this issue here based on the "lld:error: " messages shown below.

As an alternative, I have tried to assemble the assembly files directly from zig, but unfortunately the assembler does not recognize the "rfe" (return from exception) MIPS I instruction for some unknown reason:

This is the output when building everything from zig:

$ make zig build-exe src/main.zig -Ddprintf=printf -L. -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys /home/xavier/psxsdk-src/libpsx/src/atexit.c /home/xavier/psxsdk-src/libpsx/src/cdrom.c /home/xavier/psxsdk-src/libpsx/src/cop.c /home/xavier/psxsdk-src/libpsx/src/exception.c /home/xavier/psxsdk-src/libpsx/src/gpu.c /home/xavier/psxsdk-src/libpsx/src/libc.c /home/xavier/psxsdk-src/libpsx/src/memcard.c /home/xavier/psxsdk-src/libpsx/src/memory.c /home/xavier/psxsdk-src/libpsx/src/pad.c /home/xavier/psxsdk-src/libpsx/src/psxsdk.c /home/xavier/psxsdk-src/libpsx/src/setup.c /home/xavier/psxsdk-src/libpsx/src/sio.c /home/xavier/psxsdk-src/libpsx/src/spu.c /home/xavier/psxsdk-src/libpsx/src/util.c /home/xavier/psxsdk-src/libpsx/src/libc/error.c /home/xavier/psxsdk-src/libpsx/src/libc/misc.c /home/xavier/psxsdk-src/libpsx/src/libc/printf.c /home/xavier/psxsdk-src/libpsx/src/libc/qsort.c /home/xavier/psxsdk-src/libpsx/src/libc/scanf.c /home/xavier/psxsdk-src/libpsx/src/libc/stat.c /home/xavier/psxsdk-src/libpsx/src/libc/string.c /home/xavier/psxsdk-src/libpsx/src/libc/strings.c /home/xavier/psxsdk-src/libpsx/src/libc/unistd.c /home/xavier/psxsdk-src/libpsx/src/cdromh.s /home/xavier/psxsdk-src/libpsx/src/exc1.s /home/xavier/psxsdk-src/libpsx/src/syscalls.s /home/xavier/psxsdk-src/libpsx/src/start/start.s /home/xavier/psxsdk-src/libpsx/src/runexe/stage2.s -target mipsel-freestanding-none --single-threaded -mcpu mips1+soft_float -Tpsx.ld error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-D dprintf=printf' [-Wunused-command-line-argument] /home/xavier/psxsdk-src/libpsx/src/exc1.s:95:2: error: unknown instruction, did you mean: rem? rfe ^

/home/xavier/psxsdk-src/libpsx/src/exc1.s:1:1: error: unable to build C object: clang exited with code 1 /home/xavier/psxsdk/bin/elf2exe main cdimg/main.exe -mark_eur mipsel-unknown-elf-objcopy: 'main': No such file Could not open objcopy output at main.bin. Check your permissions. Aborting. make: *** [Makefile:32: cdimg/main.exe] Error 255

And this is the output when assembling with mipsel-unknown-elf-as and compiling and linking from zig:

$ make mipsel-unknown-elf-as /home/xavier/psxsdk-src/libpsx/src/cdromh.s -o obj/cdromh.o -msoft-float -march=mips1 -mabi=32 mipsel-unknown-elf-as /home/xavier/psxsdk-src/libpsx/src/exc1.s -o obj/exc1.o -msoft-float -march=mips1 -mabi=32 mipsel-unknown-elf-as /home/xavier/psxsdk-src/libpsx/src/syscalls.s -o obj/syscalls.o -msoft-float -march=mips1 -mabi=32 mipsel-unknown-elf-as /home/xavier/psxsdk-src/libpsx/src/start/start.s -o obj/start.o -msoft-float -march=mips1 -mabi=32 mipsel-unknown-elf-as /home/xavier/psxsdk-src/libpsx/src/runexe/stage2.s -o obj/stage2.o -msoft-float -march=mips1 -mabi=32 zig build-exe src/main.zig -Ddprintf=printf -L. -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys /home/xavier/psxsdk-src/libpsx/src/atexit.c /home/xavier/psxsdk-src/libpsx/src/cdrom.c /home/xavier/psxsdk-src/libpsx/src/cop.c /home/xavier/psxsdk-src/libpsx/src/exception.c /home/xavier/psxsdk-src/libpsx/src/gpu.c /home/xavier/psxsdk-src/libpsx/src/libc.c /home/xavier/psxsdk-src/libpsx/src/memcard.c /home/xavier/psxsdk-src/libpsx/src/memory.c /home/xavier/psxsdk-src/libpsx/src/pad.c /home/xavier/psxsdk-src/libpsx/src/psxsdk.c /home/xavier/psxsdk-src/libpsx/src/setup.c /home/xavier/psxsdk-src/libpsx/src/sio.c /home/xavier/psxsdk-src/libpsx/src/spu.c /home/xavier/psxsdk-src/libpsx/src/util.c /home/xavier/psxsdk-src/libpsx/src/libc/error.c /home/xavier/psxsdk-src/libpsx/src/libc/misc.c /home/xavier/psxsdk-src/libpsx/src/libc/printf.c /home/xavier/psxsdk-src/libpsx/src/libc/qsort.c /home/xavier/psxsdk-src/libpsx/src/libc/scanf.c /home/xavier/psxsdk-src/libpsx/src/libc/stat.c /home/xavier/psxsdk-src/libpsx/src/libc/string.c /home/xavier/psxsdk-src/libpsx/src/libc/strings.c /home/xavier/psxsdk-src/libpsx/src/libc/unistd.c obj/cdromh.o obj/exc1.o obj/syscalls.o obj/start.o obj/stage2.o -target mipsel-freestanding-none --single-threaded -mcpu mips1+soft_float -ffunction-sections -Tpsx.ld warning: MIPS-I support is experimental lld: error: obj/cdromh.o: STB_LOCAL symbol (9) found at index >= .symtab's sh_info (8) lld: error: obj/cdromh.o: STB_LOCAL symbol (10) found at index >= .symtab's sh_info (8) lld: error: obj/cdromh.o: STB_LOCAL symbol (12) found at index >= .symtab's sh_info (8) lld: error: obj/cdromh.o: STB_LOCAL symbol (14) found at index >= .symtab's sh_info (8) lld: error: obj/syscalls.o: STB_LOCAL symbol (13) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (24) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (25) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (27) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (28) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (29) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (30) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (31) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (32) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (33) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (34) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (35) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (36) found at index >= .symtab's sh_info (8) lld: error: obj/start.o: STB_LOCAL symbol (37) found at index >= .symtab's sh_info (8) lld: error: obj/stage2.o: STB_LOCAL symbol (10) found at index >= .symtab's sh_info (8) lld: error: obj/stage2.o: STB_LOCAL symbol (11) found at index >= .symtab's sh_info (8) lld: error: obj/stage2.o: STB_LOCAL symbol (12) found at index >= .symtab's sh_info (8) attempt to unwrap error: LLDReportedFailure /home/xavier/zig/src/link/Elf.zig:716:9: 0x5621554252e6 in link.Elf.flush (zig1) return self.linkWithLLD(comp); ^ /home/xavier/zig/src/link.zig:354:21: 0x5621553e1d37 in link.File.flush (zig1) .elf => return @​fieldParentPtr(Elf, "base", base).flush(comp), ^ /home/xavier/zig/src/Compilation.zig:1100:5: 0x5621553dac4a in Compilation.update (zig1) try self.bin_file.flush(self); ^ /home/xavier/zig/src/main.zig:1843:5: 0x562155392314 in main.updateModule (zig1) try comp.update(); ^ /home/xavier/zig/src/main.zig:1710:5: 0x5621552e99a2 in main.buildOutputType (zig1) try updateModule(gpa, comp, zir_out_path, hook); ^ /home/xavier/zig/src/main.zig:159:9: 0x5621552c30e5 in main.mainArgs (zig1) return buildOutputType(gpa, arena, args, .{ .build = .Exe }); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5621554cf809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5621554cf809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5621552bf2f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5621554cf809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/src/link/Elf.zig:1652:9: 0x562155435c4a in link.Elf.linkWithLLD (zig1) return error.LLDReportedFailure; ^ ???:?:?: 0x5621552c4305 in ??? (???) /home/xavier/zig/src/stage1.zig:45:43: 0x5621552c2c2d in main (zig1) stage2.mainArgs(gpa, arena, args) catch unreachable; ^ ???:?:?: 0x7f9a89b2c0b2 in ??? (???) make: *** [Makefile:35: main] Aborted (core dumped)

atanasyan commented 3 years ago

Any updates on that?

atanasyan commented 3 years ago

/home/xavier/zig/build/zig clang -fno-caret-diagnostics -target mipsel-unknown-unknown-unknown -ffreestanding -Ddprintf=printf -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys -c -o src/zig-cache/tmp/653e2ffd3973934e-exc1.o /home/xavier/psxsdk-src/libpsx/src/exc1.s

By default in case of 32-bit architecture Clang generates code for MIPS32 R2. You need to pass -mips1 option to enable MIPS1 support.

llvmbot commented 3 years ago

error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-D dprintf=printf' [-Wunused-command-line-argument] /home/xavier/psxsdk-src/libpsx/src/exc1.s:95:2: error: instruction requires a CPU feature not currently enabled rfe ^

Is it possible to see a list of arguments used to invoke Clang?

Adding --verbose-cc to zig prints the following command:

/home/xavier/zig/build/zig clang -fno-caret-diagnostics -target mipsel-unknown-unknown-unknown -ffreestanding -Ddprintf=printf -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys -c -o src/zig-cache/tmp/653e2ffd3973934e-exc1.o /home/xavier/psxsdk-src/libpsx/src/exc1.s

Which returns the same result as above:

$ /home/xavier/zig/build/zig clang -fno-caret-diagnostics -target mipsel-unknown-unknown-unknown -ffreestanding -Ddprintf=printf -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys -c -o src/zig-cache/tmp/653e2ffd3973934e-exc1.o /home/xavier/psxsdk-src/libpsx/src/exc1.s zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-D dprintf=printf' [-Wunused-command-line-argument] /home/xavier/psxsdk-src/libpsx/src/exc1.s:95:2: error: instruction requires a CPU feature not currently enabled rfe ^

atanasyan commented 3 years ago

error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-D dprintf=printf' [-Wunused-command-line-argument] /home/xavier/psxsdk-src/libpsx/src/exc1.s:95:2: error: instruction requires a CPU feature not currently enabled rfe ^

Is it possible to see a list of arguments used to invoke Clang?

llvmbot commented 3 years ago

Could you try the rfe.patch? It should bring support for the rfe instruction.

As to the incorrect sh_info field of .symtab section I know that there is a commit [1] which adds to GNU readelf a warning if symbol index is greater or equal to sh_info. But this warning is not shown for MIPS and Solaris because their binaries sometimes violates that rule. But I'm sure that the latest version of GNU binutils generates a correct MIPS binaries.

[1] https://sourceware.org/git/?p=binutils-gdb.git;a=commit;f=binutils/readelf.c; h=10ca4b042d15cd0727300cf3f5a9f11ac8d6abff

I have applied the patch and rebuilt the binaries, but now it returns a different error:

$ make zig build-exe src/main.zig -Ddprintf=printf -L. -I/home/xavier/psxsdk-src/libpsx/include -I/home/xavier/psxsdk-src/libpsx/include/sys /home/xavier/psxsdk-src/libpsx/src/atexit.c /home/xavier/psxsdk-src/libpsx/src/cdrom.c /home/xavier/psxsdk-src/libpsx/src/cop.c /home/xavier/psxsdk-src/libpsx/src/exception.c /home/xavier/psxsdk-src/libpsx/src/gpu.c /home/xavier/psxsdk-src/libpsx/src/libc.c /home/xavier/psxsdk-src/libpsx/src/memcard.c /home/xavier/psxsdk-src/libpsx/src/memory.c /home/xavier/psxsdk-src/libpsx/src/pad.c /home/xavier/psxsdk-src/libpsx/src/psxsdk.c /home/xavier/psxsdk-src/libpsx/src/setup.c /home/xavier/psxsdk-src/libpsx/src/sio.c /home/xavier/psxsdk-src/libpsx/src/spu.c /home/xavier/psxsdk-src/libpsx/src/util.c /home/xavier/psxsdk-src/libpsx/src/libc/error.c /home/xavier/psxsdk-src/libpsx/src/libc/misc.c /home/xavier/psxsdk-src/libpsx/src/libc/printf.c /home/xavier/psxsdk-src/libpsx/src/libc/qsort.c /home/xavier/psxsdk-src/libpsx/src/libc/scanf.c /home/xavier/psxsdk-src/libpsx/src/libc/stat.c /home/xavier/psxsdk-src/libpsx/src/libc/string.c /home/xavier/psxsdk-src/libpsx/src/libc/strings.c /home/xavier/psxsdk-src/libpsx/src/libc/unistd.c /home/xavier/psxsdk-src/libpsx/src/cdromh.s /home/xavier/psxsdk-src/libpsx/src/exc1.s /home/xavier/psxsdk-src/libpsx/src/syscalls.s /home/xavier/psxsdk-src/libpsx/src/start/start.s /home/xavier/psxsdk-src/libpsx/src/runexe/stage2.s -target mipsel-freestanding-none --single-threaded -mcpu mips1+soft_float -T/home/xavier/psxsdk/mipsel-unknown-elf/lib/ldscripts/playstation.x error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument] zig: warning: argument unused during compilation: '-D dprintf=printf' [-Wunused-command-line-argument] /home/xavier/psxsdk-src/libpsx/src/exc1.s:95:2: error: instruction requires a CPU feature not currently enabled rfe ^

atanasyan commented 3 years ago

Could you try the rfe.patch? It should bring support for the rfe instruction.

As to the incorrect sh_info field of .symtab section I know that there is a commit [1] which adds to GNU readelf a warning if symbol index is greater or equal to sh_info. But this warning is not shown for MIPS and Solaris because their binaries sometimes violates that rule. But I'm sure that the latest version of GNU binutils generates a correct MIPS binaries.

[1] https://sourceware.org/git/?p=binutils-gdb.git;a=commit;f=binutils/readelf.c;h=10ca4b042d15cd0727300cf3f5a9f11ac8d6abff

atanasyan commented 3 years ago

Support for MIPS rfe instruction

llvmbot commented 3 years ago

Maybe I support the rfe instruction in LLVM. But I cannot provide any time estimations.

That would be a nice alternative (and a nice addition to llvm, too). I had already thought of doing this myself, but i fear my inexperience with the inner workings of llvm might take me too long even for such an apparently trivial change. However, I might be able to tinker with this feature if you give me some pointers. Thanks for your help!

llvmbot commented 3 years ago

There are two problems. Unsupported rfe instruction and the linker's error message. As to the rfe instruction, unfortunately I cannot help. As to the error message, I think it's a bug in the mipsel-unknown-elf-as not in the LLVM code.

Take a look at the main.o .symtab section: % readelf -s main.o Num: Value Size Type Bind Vis Ndx Name ... 8: 00000000 0 NOTYPE LOCAL DEFAULT 1 main

and dump of the .symtab section header: % readelf -t main.o Section Headers: [Nr] Name Type Addr Off Size ES Lk Inf Al Flags ... [ 8] .symtab SYMTAB 00000000 000078 000090 10 9 8 4 [00000000]:

The main symbol has index 8 (starting from 0). The sh_info field of the .symtab section has value 8. But ELF file specification states that sh_info field of the .symtab section "... one greater than the symbol table index of the last local symbol (binding STB_LOCAL)". The correct value is 9. That's why LLD linker shows an error.

BTW the latest Codescape MIPS GNU Toolchain [1] generates a correct symbol table.

[1] https://codescape.mips.com/components/toolchain/2019.02-04/downloads.html

I was already aware there were two separate issues, but it was my intention to show the alternative path (building everything from llvm) hadn't work either. The examples I attached were built with GNU binutils 2.31, but the issue still occurs with the most recent version so far (2.35). Unfortunately, binutils-2019.02-04 ( the package you attached above) does not build for mipsel-unknown-elf (see error message below).

../../binutils-2019.02-04/gdb/remote-sim.c:1198:35: error: cannot convert ‘ptid_t’ to ‘thread_info*’

Do you know which files from GNU binutils source are involved with this issue? Should we report this bug to the GNU project?

atanasyan commented 3 years ago

Maybe I support the rfe instruction in LLVM. But I cannot provide any time estimations.

atanasyan commented 3 years ago

There are two problems. Unsupported rfe instruction and the linker's error message. As to the rfe instruction, unfortunately I cannot help. As to the error message, I think it's a bug in the mipsel-unknown-elf-as not in the LLVM code.

Take a look at the main.o .symtab section: % readelf -s main.o Num: Value Size Type Bind Vis Ndx Name ... 8: 00000000 0 NOTYPE LOCAL DEFAULT 1 main

and dump of the .symtab section header: % readelf -t main.o Section Headers: [Nr] Name Type Addr Off Size ES Lk Inf Al Flags ... [ 8] .symtab SYMTAB 00000000 000078 000090 10 9 8 4 [00000000]:

The main symbol has index 8 (starting from 0). The sh_info field of the .symtab section has value 8. But ELF file specification states that sh_info field of the .symtab section "... one greater than the symbol table index of the last local symbol (binding STB_LOCAL)". The correct value is 9. That's why LLD linker shows an error.

BTW the latest Codescape MIPS GNU Toolchain [1] generates a correct symbol table.

[1] https://codescape.mips.com/components/toolchain/2019.02-04/downloads.html

llvmbot commented 3 years ago

As an alternative, I have tried to assemble the assembly files directly from zig, but unfortunately the assembler does not recognize the "rfe" (return from exception) MIPS I instruction for some unknown reason:

There was and there is no plan to support MIPS I in LLVM. Features like load delay slots, MIPS I specific instructions etc. Recently, there is a growing interest from homebrew video game developers in modern programming languages using LLVM such as Rust or Zig, so MIPS I support is indeed very interesting. However, I guess most developers will be fine as long as load delay slots are supported via patches and assembly files are assembled with mipsel-unknown-elf-as.

lld: error: obj/cdromh.o: STB_LOCAL symbol (9) found at index >= .symtab's sh_info (8)

Could you provide the smallest .s file and corresponding .o file to reproduce the problem? I have attached two files, main.s and main.o, that already reproduce the two issues I described above:


$ zig build-exe main.s error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument] main.s:2:5: error: invalid instruction mnemonic 'rfe' rfe ^~~

main.s:1:1: error: unable to build C object: clang exited with code 1


$ zig build-exe main.o -target mipsel-freestanding-none --single-threaded -mcpu mips1+soft_float lld: error: main.o: STB_LOCAL symbol (8) found at index >= .symtab's sh_info (8) attempt to unwrap error: LLDReportedFailure /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5584e4ab3809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5584e4ab3809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/src/link/Elf.zig:1652:9: 0x5584e4a19c4a in link.Elf.linkWithLLD (zig1) return error.LLDReportedFailure; ^ /home/xavier/zig/src/link/Elf.zig:716:9: 0x5584e4a092e6 in link.Elf.flush (zig1) return self.linkWithLLD(comp); ^ /home/xavier/zig/src/link.zig:354:21: 0x5584e49c5d37 in link.File.flush (zig1) .elf => return @​fieldParentPtr(Elf, "base", base).flush(comp), ^ /home/xavier/zig/src/Compilation.zig:1100:5: 0x5584e49bec4a in Compilation.update (zig1) try self.bin_file.flush(self); ^ /home/xavier/zig/src/main.zig:1843:5: 0x5584e4976314 in main.updateModule (zig1) try comp.update(); ^ /home/xavier/zig/src/main.zig:1710:5: 0x5584e48cd9a2 in main.buildOutputType (zig1) try updateModule(gpa, comp, zir_out_path, hook); ^ /home/xavier/zig/src/main.zig:159:9: 0x5584e48a70e5 in main.mainArgs (zig1) return buildOutputType(gpa, arena, args, .{ .build = .Exe }); ^ /home/xavier/zig/lib/std/os.zig:2086:9: 0x5584e4930a7e in std.os.mkdirat (zig1) return mkdiratZ(dir_fd, &sub_dir_path_c, mode); ^ /home/xavier/zig/lib/std/fs.zig:923:9: 0x5584e4930956 in std.fs.Dir.makeDir (zig1) try os.mkdirat(self.fd, sub_path, default_new_dir_mode); ^ /home/xavier/zig/lib/std/os.zig:2125:19: 0x5584e4930bc8 in std.os.mkdiratZ (zig1) EEXIST => return error.PathAlreadyExists, ^ /home/xavier/zig/lib/std/os.zig:2086:9: 0x5584e4930a7e in std.os.mkdirat (zig1) return mkdiratZ(dir_fd, &sub_dir_path_c, mode); ^ /home/xavier/zig/lib/std/fs.zig:923:9: 0x5584e4930956 in std.fs.Dir.makeDir (zig1) try os.mkdirat(self.fd, sub_path, default_new_dir_mode); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5584e4ab3809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/os.zig:1196:23: 0x5584e4a833f1 in std.os.openatZ (zig1) ENOENT => return error.FileNotFound, ^ /home/xavier/zig/lib/std/fs.zig:754:13: 0x5584e492a59f in std.fs.Dir.openFileZ (zig1) try os.openatZ(self.fd, sub_path, os_flags, 0); ^ /home/xavier/zig/lib/std/fs.zig:687:9: 0x5584e49263a9 in std.fs.Dir.openFile (zig1) return self.openFileZ(&path_c, flags); ^ /home/xavier/zig/lib/std/fs.zig:1527:20: 0x5584e49556f7 in std.fs.Dir.readFileAllocOptions (zig1) var file = try self.openFile(file_path, .{}); ^ /home/xavier/zig/lib/std/fs.zig:1510:9: 0x5584e49525ea in std.fs.Dir.readFileAlloc (zig1) return self.readFileAllocOptions(allocator, file_path, max_bytes, null, @​alignOf(u8), null); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ /home/xavier/zig/lib/std/mem/Allocator.zig:319:16: 0x5584e4ab3809 in std.mem.Allocator.resize (zig1) const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @​returnAddress()); ^ /home/xavier/zig/lib/std/heap.zig:83:5: 0x5584e48a32f9 in std.heap.cResize (zig1) return error.OutOfMemory; ^ ???:?:?: 0x5584e48a8305 in ??? (???) /home/xavier/zig/src/stage1.zig:45:43: 0x5584e48a6c2d in main (zig1) stage2.mainArgs(gpa, arena, args) catch unreachable; ^ ???:?:?: 0x7effa5ea90b2 in ??? (???) Aborted (core dumped)

llvmbot commented 3 years ago

Minimal object file assembled from main.s using mipsel-unknown-elf-as

llvmbot commented 3 years ago

Minimal assembly file that reproduces the build issues with LLVM and Zig

atanasyan commented 3 years ago

As an alternative, I have tried to assemble the assembly files directly from zig, but unfortunately the assembler does not recognize the "rfe" (return from exception) MIPS I instruction for some unknown reason:

There was and there is no plan to support MIPS I in LLVM. Features like load delay slots, MIPS I specific instructions etc.

lld: error: obj/cdromh.o: STB_LOCAL symbol (9) found at index >= .symtab's sh_info (8)

Could you provide the smallest .s file and corresponding .o file to reproduce the problem?