[llvm] 19f2f67 - [yaml2obj][obj2yaml][objdump] Handle MIPS COFF files (#112591)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 08:28:27 PDT 2024


Author: Hervé Poussineau
Date: 2024-11-01T15:28:22Z
New Revision: 19f2f67928650c998202a8b6ae14fd1091f43bf5

URL: https://github.com/llvm/llvm-project/commit/19f2f67928650c998202a8b6ae14fd1091f43bf5
DIFF: https://github.com/llvm/llvm-project/commit/19f2f67928650c998202a8b6ae14fd1091f43bf5.diff

LOG: [yaml2obj][obj2yaml][objdump] Handle MIPS COFF files (#112591)

- handle IMAGE_FILE_MACHINE_R4000 machine type
- handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.

Added: 
    llvm/test/tools/yaml2obj/COFF/basic-mips.yaml

Modified: 
    llvm/include/llvm/Object/WindowsMachineFlag.h
    llvm/include/llvm/ObjectYAML/COFFYAML.h
    llvm/lib/Object/COFFObjectFile.cpp
    llvm/lib/ObjectYAML/COFFYAML.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/WindowsMachineFlag.h b/llvm/include/llvm/Object/WindowsMachineFlag.h
index 1cb408ed13d420..ce5b356f8bfeed 100644
--- a/llvm/include/llvm/Object/WindowsMachineFlag.h
+++ b/llvm/include/llvm/Object/WindowsMachineFlag.h
@@ -43,6 +43,8 @@ template <typename T> Triple::ArchType getMachineArchType(T machine) {
   case COFF::IMAGE_FILE_MACHINE_ARM64EC:
   case COFF::IMAGE_FILE_MACHINE_ARM64X:
     return llvm::Triple::ArchType::aarch64;
+  case COFF::IMAGE_FILE_MACHINE_R4000:
+    return llvm::Triple::ArchType::mipsel;
   default:
     return llvm::Triple::ArchType::UnknownArch;
   }

diff  --git a/llvm/include/llvm/ObjectYAML/COFFYAML.h b/llvm/include/llvm/ObjectYAML/COFFYAML.h
index 2f9a1aae0eb05a..c22d4e7f0b29d3 100644
--- a/llvm/include/llvm/ObjectYAML/COFFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/COFFYAML.h
@@ -179,6 +179,10 @@ struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
   static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
 };
 
+template <> struct ScalarEnumerationTraits<COFF::RelocationTypesMips> {
+  static void enumeration(IO &IO, COFF::RelocationTypesMips &Value);
+};
+
 template <>
 struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
   static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);

diff  --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp
index 7f21be25b68c84..242c123665f763 100644
--- a/llvm/lib/Object/COFFObjectFile.cpp
+++ b/llvm/lib/Object/COFFObjectFile.cpp
@@ -1132,6 +1132,8 @@ StringRef COFFObjectFile::getFileFormatName() const {
     return "COFF-ARM64EC";
   case COFF::IMAGE_FILE_MACHINE_ARM64X:
     return "COFF-ARM64X";
+  case COFF::IMAGE_FILE_MACHINE_R4000:
+    return "COFF-MIPS";
   default:
     return "COFF-<unknown arch>";
   }

diff  --git a/llvm/lib/ObjectYAML/COFFYAML.cpp b/llvm/lib/ObjectYAML/COFFYAML.cpp
index e14e1b5e467b41..53ea40a0354cea 100644
--- a/llvm/lib/ObjectYAML/COFFYAML.cpp
+++ b/llvm/lib/ObjectYAML/COFFYAML.cpp
@@ -183,6 +183,25 @@ void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration(
   ECase(IMAGE_REL_AMD64_SSPAN32);
 }
 
+void ScalarEnumerationTraits<COFF::RelocationTypesMips>::enumeration(
+    IO &IO, COFF::RelocationTypesMips &Value) {
+  ECase(IMAGE_REL_MIPS_ABSOLUTE);
+  ECase(IMAGE_REL_MIPS_REFHALF);
+  ECase(IMAGE_REL_MIPS_REFWORD);
+  ECase(IMAGE_REL_MIPS_JMPADDR);
+  ECase(IMAGE_REL_MIPS_REFHI);
+  ECase(IMAGE_REL_MIPS_REFLO);
+  ECase(IMAGE_REL_MIPS_GPREL);
+  ECase(IMAGE_REL_MIPS_LITERAL);
+  ECase(IMAGE_REL_MIPS_SECTION);
+  ECase(IMAGE_REL_MIPS_SECREL);
+  ECase(IMAGE_REL_MIPS_SECRELLO);
+  ECase(IMAGE_REL_MIPS_SECRELHI);
+  ECase(IMAGE_REL_MIPS_JMPADDR16);
+  ECase(IMAGE_REL_MIPS_REFWORDNB);
+  ECase(IMAGE_REL_MIPS_PAIR);
+}
+
 void ScalarEnumerationTraits<COFF::RelocationTypesARM>::enumeration(
     IO &IO, COFF::RelocationTypesARM &Value) {
   ECase(IMAGE_REL_ARM_ABSOLUTE);
@@ -427,6 +446,10 @@ void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
     MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
         IO, Rel.Type);
     IO.mapRequired("Type", NT->Type);
+  } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_R4000) {
+    MappingNormalization<NType<COFF::RelocationTypesMips>, uint16_t> NT(
+        IO, Rel.Type);
+    IO.mapRequired("Type", NT->Type);
   } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
     MappingNormalization<NType<COFF::RelocationTypesARM>, uint16_t> NT(
         IO, Rel.Type);

diff  --git a/llvm/test/tools/yaml2obj/COFF/basic-mips.yaml b/llvm/test/tools/yaml2obj/COFF/basic-mips.yaml
new file mode 100644
index 00000000000000..4c5afd500f7148
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/COFF/basic-mips.yaml
@@ -0,0 +1,39 @@
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readobj --file-headers --relocs %t | FileCheck %s
+# RUN: obj2yaml %t | FileCheck %s --check-prefix=ROUNDTRIP
+
+# CHECK: Format: COFF-MIPS
+# CHECK: Arch: mipsel
+# CHECK: Machine: IMAGE_FILE_MACHINE_R4000 (0x166)
+# CHECK: Relocations [
+# CHECK:  Section (1) .text {
+# CHECK:    0x4 IMAGE_REL_MIPS_JMPADDR main (0)
+# CHECK:  }
+# CHECK: ]
+
+# ROUNDTRIP:      Machine: IMAGE_FILE_MACHINE_R4000
+# ROUNDTRIP:      Relocations:
+# ROUNDTRIP-NEXT: - VirtualAddress: 4
+# ROUNDTRIP-NEXT:   SymbolName:     main
+# ROUNDTRIP-NEXT:   Type:           IMAGE_REL_MIPS_JMPADDR
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_R4000
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    VirtualSize:     12
+    SectionData:     000000000000000C00000000
+    Relocations:
+      - VirtualAddress:  4
+        SymbolName:      main
+        Type:            IMAGE_REL_MIPS_JMPADDR
+symbols:
+  - Name: main
+    Value: 0
+    SectionNumber: 1
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+...


        


More information about the llvm-commits mailing list