ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Incorrect ELF file for mips64el

Open jacob-carlborg opened this issue 1 year ago • 5 comments

If I compile for the mips64el architecture LDC creates an incorrect, 32 bit, ELF file:

$ ldc2 main.d -c --mtriple mips64-linux
$ file main.o                                                                                                                                                                                                                                                                                        
main.o: ELF 32-bit MSB relocatable, MIPS, N32 MIPS64 version 1 (GNU/Linux), not stripped
$ ldc2 --version
LDC - the LLVM D compiler (1.39.0):
  based on DMD v2.109.1 and LLVM 17.0.6
  built with LDC - the LLVM D compiler (1.39.0)
  Default target: arm64-apple-darwin24.0.0
  Host CPU: apple-m1
  http://dlang.org - http://wiki.dlang.org/LDC

Using Clang with the same version (more less) of LLVM results in this:

$ clang -target mips64el-linux main.c -o main.o -c
$ file main.o
main.o: ELF 64-bit LSB relocatable, MIPS, MIPS64 rel2 version 1 (SYSV), not stripped
$ clang --version                                                                                                                                                                                                         
clang version 17.0.1
Target: arm64-apple-darwin24.0.0
Thread model: posix

To me it looks like LDC outputs 64 bit MIPS code in a 32 bit ELF file.

jacob-carlborg avatar Aug 05 '24 08:08 jacob-carlborg

MIPS is a bit weird with its ABIs AFAICT; looks like our default is off: https://github.com/ldc-developers/ldc/blob/48c1dff0466f5231619356ae33ef1a181888a1ff/driver/targetmachine.cpp#L154-L156

$ ldc2 -c ../hello.d -mtriple=mips64el-linux -mabi=n64
$ file hello.o 
hello.o: ELF 64-bit LSB relocatable, MIPS, MIPS64 version 1 (GNU/Linux), not stripped

kinke avatar Aug 05 '24 10:08 kinke

I see. Is that intentional?

jacob-carlborg avatar Aug 05 '24 11:08 jacob-carlborg

I see. Is that intentional?

Don't see anything from history, looks like a typo.

JohanEngelen avatar Aug 05 '24 11:08 JohanEngelen

Compiling with the -mabi=n64 worked for me. Would it be appropriate to make this the default?

jacob-carlborg avatar Aug 07 '24 07:08 jacob-carlborg

Judging from the clang default, it seems so. Looks like n32 was intended for the 32-bit MIPS targets (which have no default ABI currently), i.e., llvm::Triple::mips[el].

kinke avatar Aug 07 '24 09:08 kinke