Skip to content

[llvm-dlltool] Handle MIPS R4000 architecture #114621

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

Merged
merged 3 commits into from
Dec 26, 2024

Conversation

hpoussin
Copy link
Contributor

@hpoussin hpoussin commented Nov 1, 2024

No description provided.

@hpoussin
Copy link
Contributor Author

hpoussin commented Nov 1, 2024

This is an extract of PR #107744

@hpoussin hpoussin marked this pull request as draft November 2, 2024 20:04
@hpoussin hpoussin marked this pull request as ready for review December 22, 2024 20:57
@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Hervé Poussineau (hpoussin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/114621.diff

3 Files Affected:

  • (modified) llvm/lib/Object/COFFImportFile.cpp (+2)
  • (modified) llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp (+2-1)
  • (modified) llvm/test/tools/llvm-dlltool/machine-opt.def (+3)
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index ff3dcf9e13ffaf..595533ff947252 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -133,6 +133,8 @@ static uint16_t getImgRelRelocation(MachineTypes Machine) {
     return IMAGE_REL_ARM64_ADDR32NB;
   case IMAGE_FILE_MACHINE_I386:
     return IMAGE_REL_I386_DIR32NB;
+  case IMAGE_FILE_MACHINE_R4000:
+    return IMAGE_REL_MIPS_REFWORDNB;
   }
 }
 
diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index 58ff720516f384..fdd6f3ec4be54b 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -79,6 +79,7 @@ MachineTypes getEmulation(StringRef S) {
       .Case("arm", IMAGE_FILE_MACHINE_ARMNT)
       .Case("arm64", IMAGE_FILE_MACHINE_ARM64)
       .Case("arm64ec", IMAGE_FILE_MACHINE_ARM64EC)
+      .Case("mips", IMAGE_FILE_MACHINE_R4000)
       .Default(IMAGE_FILE_MACHINE_UNKNOWN);
 }
 
@@ -173,7 +174,7 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
       (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l))) {
     Table.printHelp(outs(), "llvm-dlltool [options] file...", "llvm-dlltool",
                     false);
-    llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64, arm64ec\n";
+    llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64, arm64ec, mips\n";
     return 1;
   }
 
diff --git a/llvm/test/tools/llvm-dlltool/machine-opt.def b/llvm/test/tools/llvm-dlltool/machine-opt.def
index 6dce8255a43db3..37eccec8831fc6 100644
--- a/llvm/test/tools/llvm-dlltool/machine-opt.def
+++ b/llvm/test/tools/llvm-dlltool/machine-opt.def
@@ -6,6 +6,8 @@
 ; RUN: llvm-readobj %t.a | FileCheck --check-prefix=ARM %s
 ; RUN: llvm-dlltool -m arm64 -d %s -l %t.a
 ; RUN: llvm-readobj %t.a | FileCheck --check-prefix=ARM64 %s
+; RUN: llvm-dlltool -m mips -d %s -l %t.a
+; RUN: llvm-readobj %t.a | FileCheck --check-prefix=MIPS %s
 
 LIBRARY test.dll
 EXPORTS
@@ -15,3 +17,4 @@ TestFunction
 ; X86_64: Format: COFF-x86-64
 ; ARM: Format: COFF-ARM{{$}}
 ; ARM64: Format: COFF-ARM64
+; MIPS: Format: COFF-MIPS

@@ -79,6 +79,7 @@ MachineTypes getEmulation(StringRef S) {
.Case("arm", IMAGE_FILE_MACHINE_ARMNT)
.Case("arm64", IMAGE_FILE_MACHINE_ARM64)
.Case("arm64ec", IMAGE_FILE_MACHINE_ARM64EC)
.Case("mips", IMAGE_FILE_MACHINE_R4000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we should cross-check this with binutils - if it should be r4000 or mips as there was a MIPS16 and a MIPSv2 machine type that PE/COFF has supported over the years.

@hpoussin
Copy link
Contributor Author

binutils dlltool only supports i386, i386:x86-64, arm, thumb, arm64, arm-wince, arm_internetwork, mcore-be, mcore-le, mcore-elf-be and mcore-elf-le.
It also supported once ppc (removed), arm-epoc (removed), mcore (renamed to mcore-be) and mcore-elf (renamed to mcore-elf-be)

llvm-binutils currently supports i386, i386:x86-64, arm, arm64 and arm64ec.

mips name may fit in this list, with mips16 and mipsv2 later, or mips-wince for the Windows CE variant.

@compnerd
Copy link
Member

What do you think of -m r4000 as the parameter instead of -m mips?

@hpoussin
Copy link
Contributor Author

I don't care.

Copy link

github-actions bot commented Dec 23, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@wzssyqa
Copy link
Contributor

wzssyqa commented Dec 26, 2024

What do you think of -m r4000 as the parameter instead of -m mips?

Why r4000 is better than mips here?

@wzssyqa
Copy link
Contributor

wzssyqa commented Dec 26, 2024

Oh, I see: https://learn.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants

IMAGE_FILE_MACHINE_R3000
0x0162
MIPS little-endian, 0x160 big-endian
IMAGE_FILE_MACHINE_R4000
0x0166
MIPS little-endian
IMAGE_FILE_MACHINE_R10000
0x0168
MIPS little-endian
IMAGE_FILE_MACHINE_WCEMIPSV2
0x0169
MIPS little-endian WCE v2

@wzssyqa wzssyqa changed the title [llvm-dlltool] Handle MIPS architecture [llvm-dlltool] Handle MIPS R4000 architecture Dec 26, 2024
@wzssyqa wzssyqa merged commit a72bfc5 into llvm:main Dec 26, 2024
8 checks passed
@hpoussin hpoussin deleted the clang-mips-coff-6 branch January 21, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants