Skip to content

[yaml2obj][obj2yaml][objdump] Handle MIPS COFF files #112591

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
Nov 1, 2024

Conversation

hpoussin
Copy link
Contributor

  • handle IMAGE_FILE_MACHINE_R4000 machine type
  • handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.

@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2024

@llvm/pr-subscribers-objectyaml

Author: Hervé Poussineau (hpoussin)

Changes
  • handle IMAGE_FILE_MACHINE_R4000 machine type
  • handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.


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

8 Files Affected:

  • (modified) llvm/include/llvm/Object/WindowsMachineFlag.h (+2)
  • (modified) llvm/include/llvm/ObjectYAML/COFFYAML.h (+4)
  • (modified) llvm/lib/Object/COFFObjectFile.cpp (+2)
  • (modified) llvm/lib/ObjectYAML/COFFYAML.cpp (+23)
  • (added) llvm/test/Object/Inputs/COFF/mips.yaml (+76)
  • (added) llvm/test/Object/Inputs/trivial-object-test.coff-mips ()
  • (modified) llvm/test/Object/obj2yaml.test (+92)
  • (modified) llvm/test/Object/objdump-section-content.test (+10)
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/Object/Inputs/COFF/mips.yaml b/llvm/test/Object/Inputs/COFF/mips.yaml
new file mode 100644
index 00000000000000..53de16c5d2a2c9
--- /dev/null
+++ b/llvm/test/Object/Inputs/COFF/mips.yaml
@@ -0,0 +1,76 @@
+!COFF
+header: !Header
+  Machine: IMAGE_FILE_MACHINE_R4000 # (0x166)
+  Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
+
+sections:
+  - !Section
+    Name: .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment: 4
+    SectionData:  E8FFBD271400BFAF0000013C0000000C000024240000000C00000000000002241400BF8F0800E0031800BD27
+
+    Relocations:
+      - !Relocation
+        VirtualAddress: 8
+        SymbolName: str
+        Type: IMAGE_REL_MIPS_REFHI
+
+      - !Relocation
+        VirtualAddress: 8
+        SymbolName: .text
+        Type: IMAGE_REL_MIPS_PAIR
+
+      - !Relocation
+        VirtualAddress: 12
+        SymbolName: puts
+        Type: IMAGE_REL_MIPS_JMPADDR
+
+      - !Relocation
+        VirtualAddress: 16
+        SymbolName: str
+        Type: IMAGE_REL_MIPS_REFLO
+
+      - !Relocation
+        VirtualAddress: 20
+        SymbolName: SomeOtherFunction
+        Type: IMAGE_REL_MIPS_JMPADDR
+
+  - !Section
+    Name: .rdata
+    Alignment: 1
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] # 0xc0100040
+    SectionData:  !hex "48656C6C6F20576F726C642100" # |Hello World!.|
+
+symbols:
+  - !Symbol
+    Name: main
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: str
+    Value: 0
+    SectionNumber: 1
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: puts
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: SomeOtherFunction
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
diff --git a/llvm/test/Object/Inputs/trivial-object-test.coff-mips b/llvm/test/Object/Inputs/trivial-object-test.coff-mips
new file mode 100644
index 00000000000000..084998c763fa31
Binary files /dev/null and b/llvm/test/Object/Inputs/trivial-object-test.coff-mips differ
diff --git a/llvm/test/Object/obj2yaml.test b/llvm/test/Object/obj2yaml.test
index dea9cf41eab549..7f98f02e0ed1b2 100644
--- a/llvm/test/Object/obj2yaml.test
+++ b/llvm/test/Object/obj2yaml.test
@@ -343,6 +343,98 @@
 # COFF-ARM64-NEXT:     ComplexType: IMAGE_SYM_DTYPE_NULL
 # COFF-ARM64-NEXT:     StorageClass: IMAGE_SYM_CLASS_EXTERNAL
 
+# RUN: obj2yaml %p/Inputs/trivial-object-test.coff-mips | FileCheck %s --check-prefix COFF-MIPS
+
+# COFF-MIPS: header:
+# COFF-MIPS-NEXT:  Machine: IMAGE_FILE_MACHINE_R4000
+
+# COFF-MIPS: sections:
+# COFF-MIPS-NEXT:  - Name: .text
+# COFF-MIPS-NEXT:    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+# COFF-MIPS-NEXT:    Alignment: 4
+# COFF-MIPS-NEXT:    SectionData:  E8FFBD271400BFAF0000013C0000000C000024240000000C00000000000002241400BF8F0800E0031800BD27
+
+# COFF-MIPS:    Relocations:
+# COFF-MIPS-NEXT:      - VirtualAddress: 8
+# COFF-MIPS-NEXT:        SymbolName: str
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_REFHI
+
+# COFF-MIPS:           - VirtualAddress: 8
+# COFF-MIPS-NEXT:        SymbolName: .text
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_PAIR
+
+# COFF-MIPS:           - VirtualAddress: 12
+# COFF-MIPS-NEXT:        SymbolName: puts
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_JMPADDR
+
+# COFF-MIPS:           - VirtualAddress: 16
+# COFF-MIPS-NEXT:        SymbolName: str
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_REFLO
+
+# COFF-MIPS:           - VirtualAddress: 20
+# COFF-MIPS-NEXT:        SymbolName: SomeOtherFunction
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_JMPADDR
+
+# COFF-MIPS:       - Name: .rdata
+# COFF-MIPS-NEXT:    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+# COFF-MIPS-NEXT:    Alignment: 4
+# COFF-MIPS-NEXT:    SectionData: 48656C6C6F20576F726C642100
+
+# COFF-MIPS: symbols:
+# COFF-MIPS-NEXT:  - Name: .text
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 1
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_STATIC
+# COFF-MIPS-NEXT:    SectionDefinition:
+# COFF-MIPS-NEXT:      Length:          44
+# COFF-MIPS-NEXT:      NumberOfRelocations: 5
+# COFF-MIPS-NEXT:      NumberOfLinenumbers: 0
+# COFF-MIPS-NEXT:      CheckSum:        3518661474
+# COFF-MIPS-NEXT:      Number:          1
+
+# COFF-MIPS:       - Name: .rdata
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 4
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_STATIC
+# COFF-MIPS-NEXT:    SectionDefinition:
+# COFF-MIPS-NEXT:      Length:          13
+# COFF-MIPS-NEXT:      NumberOfRelocations: 0
+# COFF-MIPS-NEXT:      NumberOfLinenumbers: 0
+# COFF-MIPS-NEXT:      CheckSum:        2461364801
+# COFF-MIPS-NEXT:      Number:          4
+
+# COFF-MIPS:       - Name: main
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 1
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: str
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 4
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: puts
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 0
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: SomeOtherFunction
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 0
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
 # RUN: obj2yaml %p/Inputs/trivial-object-test.elf-mipsel | FileCheck %s --check-prefix ELF-MIPSEL
 
 # ELF-MIPSEL:      FileHeader:
diff --git a/llvm/test/Object/objdump-section-content.test b/llvm/test/Object/objdump-section-content.test
index d4c2cd8190b7c1..3e2a022f9669ef 100644
--- a/llvm/test/Object/objdump-section-content.test
+++ b/llvm/test/Object/objdump-section-content.test
@@ -39,3 +39,13 @@ Sections:
 
 # BSS: Contents of section .bss:
 # BSS-NEXT: <skipping contents of bss section at [12c8, 12cc)>
+
+# RUN: yaml2obj %p/Inputs/COFF/mips.yaml | llvm-objdump -s - | FileCheck %s --check-prefix=COFF-MIPS
+
+# COFF-MIPS: <stdin>:     file format coff-mips
+# COFF-MIPS: Contents of section .text:
+# COFF-MIPS:  0000 e8ffbd27 1400bfaf 0000013c 0000000c  ...'.......<....
+# COFF-MIPS:  0010 00002424 0000000c 00000000 00000224  ..$$...........$
+# COFF-MIPS:  0020 1400bf8f 0800e003 1800bd27           ...........'
+# COFF-MIPS: Contents of section .rdata:
+# COFF-MIPS:  0000 48656c6c 6f20576f 726c6421 00        Hello World!.

@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2024

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

Author: Hervé Poussineau (hpoussin)

Changes
  • handle IMAGE_FILE_MACHINE_R4000 machine type
  • handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.


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

8 Files Affected:

  • (modified) llvm/include/llvm/Object/WindowsMachineFlag.h (+2)
  • (modified) llvm/include/llvm/ObjectYAML/COFFYAML.h (+4)
  • (modified) llvm/lib/Object/COFFObjectFile.cpp (+2)
  • (modified) llvm/lib/ObjectYAML/COFFYAML.cpp (+23)
  • (added) llvm/test/Object/Inputs/COFF/mips.yaml (+76)
  • (added) llvm/test/Object/Inputs/trivial-object-test.coff-mips ()
  • (modified) llvm/test/Object/obj2yaml.test (+92)
  • (modified) llvm/test/Object/objdump-section-content.test (+10)
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/Object/Inputs/COFF/mips.yaml b/llvm/test/Object/Inputs/COFF/mips.yaml
new file mode 100644
index 00000000000000..53de16c5d2a2c9
--- /dev/null
+++ b/llvm/test/Object/Inputs/COFF/mips.yaml
@@ -0,0 +1,76 @@
+!COFF
+header: !Header
+  Machine: IMAGE_FILE_MACHINE_R4000 # (0x166)
+  Characteristics: [ IMAGE_FILE_DEBUG_STRIPPED ]
+
+sections:
+  - !Section
+    Name: .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment: 4
+    SectionData:  E8FFBD271400BFAF0000013C0000000C000024240000000C00000000000002241400BF8F0800E0031800BD27
+
+    Relocations:
+      - !Relocation
+        VirtualAddress: 8
+        SymbolName: str
+        Type: IMAGE_REL_MIPS_REFHI
+
+      - !Relocation
+        VirtualAddress: 8
+        SymbolName: .text
+        Type: IMAGE_REL_MIPS_PAIR
+
+      - !Relocation
+        VirtualAddress: 12
+        SymbolName: puts
+        Type: IMAGE_REL_MIPS_JMPADDR
+
+      - !Relocation
+        VirtualAddress: 16
+        SymbolName: str
+        Type: IMAGE_REL_MIPS_REFLO
+
+      - !Relocation
+        VirtualAddress: 20
+        SymbolName: SomeOtherFunction
+        Type: IMAGE_REL_MIPS_JMPADDR
+
+  - !Section
+    Name: .rdata
+    Alignment: 1
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] # 0xc0100040
+    SectionData:  !hex "48656C6C6F20576F726C642100" # |Hello World!.|
+
+symbols:
+  - !Symbol
+    Name: main
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: str
+    Value: 0
+    SectionNumber: 1
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: puts
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+  - !Symbol
+    Name: SomeOtherFunction
+    Value: 0
+    SectionNumber: 0
+    SimpleType: IMAGE_SYM_TYPE_NULL
+    ComplexType: IMAGE_SYM_DTYPE_NULL
+    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
diff --git a/llvm/test/Object/Inputs/trivial-object-test.coff-mips b/llvm/test/Object/Inputs/trivial-object-test.coff-mips
new file mode 100644
index 00000000000000..084998c763fa31
Binary files /dev/null and b/llvm/test/Object/Inputs/trivial-object-test.coff-mips differ
diff --git a/llvm/test/Object/obj2yaml.test b/llvm/test/Object/obj2yaml.test
index dea9cf41eab549..7f98f02e0ed1b2 100644
--- a/llvm/test/Object/obj2yaml.test
+++ b/llvm/test/Object/obj2yaml.test
@@ -343,6 +343,98 @@
 # COFF-ARM64-NEXT:     ComplexType: IMAGE_SYM_DTYPE_NULL
 # COFF-ARM64-NEXT:     StorageClass: IMAGE_SYM_CLASS_EXTERNAL
 
+# RUN: obj2yaml %p/Inputs/trivial-object-test.coff-mips | FileCheck %s --check-prefix COFF-MIPS
+
+# COFF-MIPS: header:
+# COFF-MIPS-NEXT:  Machine: IMAGE_FILE_MACHINE_R4000
+
+# COFF-MIPS: sections:
+# COFF-MIPS-NEXT:  - Name: .text
+# COFF-MIPS-NEXT:    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+# COFF-MIPS-NEXT:    Alignment: 4
+# COFF-MIPS-NEXT:    SectionData:  E8FFBD271400BFAF0000013C0000000C000024240000000C00000000000002241400BF8F0800E0031800BD27
+
+# COFF-MIPS:    Relocations:
+# COFF-MIPS-NEXT:      - VirtualAddress: 8
+# COFF-MIPS-NEXT:        SymbolName: str
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_REFHI
+
+# COFF-MIPS:           - VirtualAddress: 8
+# COFF-MIPS-NEXT:        SymbolName: .text
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_PAIR
+
+# COFF-MIPS:           - VirtualAddress: 12
+# COFF-MIPS-NEXT:        SymbolName: puts
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_JMPADDR
+
+# COFF-MIPS:           - VirtualAddress: 16
+# COFF-MIPS-NEXT:        SymbolName: str
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_REFLO
+
+# COFF-MIPS:           - VirtualAddress: 20
+# COFF-MIPS-NEXT:        SymbolName: SomeOtherFunction
+# COFF-MIPS-NEXT:        Type: IMAGE_REL_MIPS_JMPADDR
+
+# COFF-MIPS:       - Name: .rdata
+# COFF-MIPS-NEXT:    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+# COFF-MIPS-NEXT:    Alignment: 4
+# COFF-MIPS-NEXT:    SectionData: 48656C6C6F20576F726C642100
+
+# COFF-MIPS: symbols:
+# COFF-MIPS-NEXT:  - Name: .text
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 1
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_STATIC
+# COFF-MIPS-NEXT:    SectionDefinition:
+# COFF-MIPS-NEXT:      Length:          44
+# COFF-MIPS-NEXT:      NumberOfRelocations: 5
+# COFF-MIPS-NEXT:      NumberOfLinenumbers: 0
+# COFF-MIPS-NEXT:      CheckSum:        3518661474
+# COFF-MIPS-NEXT:      Number:          1
+
+# COFF-MIPS:       - Name: .rdata
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 4
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_STATIC
+# COFF-MIPS-NEXT:    SectionDefinition:
+# COFF-MIPS-NEXT:      Length:          13
+# COFF-MIPS-NEXT:      NumberOfRelocations: 0
+# COFF-MIPS-NEXT:      NumberOfLinenumbers: 0
+# COFF-MIPS-NEXT:      CheckSum:        2461364801
+# COFF-MIPS-NEXT:      Number:          4
+
+# COFF-MIPS:       - Name: main
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 1
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: str
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 4
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: puts
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 0
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
+# COFF-MIPS:       - Name: SomeOtherFunction
+# COFF-MIPS-NEXT:    Value: 0
+# COFF-MIPS-NEXT:    SectionNumber: 0
+# COFF-MIPS-NEXT:    SimpleType: IMAGE_SYM_TYPE_NULL
+# COFF-MIPS-NEXT:    ComplexType: IMAGE_SYM_DTYPE_NULL
+# COFF-MIPS-NEXT:    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+
 # RUN: obj2yaml %p/Inputs/trivial-object-test.elf-mipsel | FileCheck %s --check-prefix ELF-MIPSEL
 
 # ELF-MIPSEL:      FileHeader:
diff --git a/llvm/test/Object/objdump-section-content.test b/llvm/test/Object/objdump-section-content.test
index d4c2cd8190b7c1..3e2a022f9669ef 100644
--- a/llvm/test/Object/objdump-section-content.test
+++ b/llvm/test/Object/objdump-section-content.test
@@ -39,3 +39,13 @@ Sections:
 
 # BSS: Contents of section .bss:
 # BSS-NEXT: <skipping contents of bss section at [12c8, 12cc)>
+
+# RUN: yaml2obj %p/Inputs/COFF/mips.yaml | llvm-objdump -s - | FileCheck %s --check-prefix=COFF-MIPS
+
+# COFF-MIPS: <stdin>:     file format coff-mips
+# COFF-MIPS: Contents of section .text:
+# COFF-MIPS:  0000 e8ffbd27 1400bfaf 0000013c 0000000c  ...'.......<....
+# COFF-MIPS:  0010 00002424 0000000c 00000000 00000224  ..$$...........$
+# COFF-MIPS:  0020 1400bf8f 0800e003 1800bd27           ...........'
+# COFF-MIPS: Contents of section .rdata:
+# COFF-MIPS:  0000 48656c6c 6f20576f 726c6421 00        Hello World!.

@hpoussin
Copy link
Contributor Author

This is an extract of PR #107744

@@ -343,6 +343,98 @@
# COFF-ARM64-NEXT: ComplexType: IMAGE_SYM_DTYPE_NULL
# COFF-ARM64-NEXT: StorageClass: IMAGE_SYM_CLASS_EXTERNAL

# RUN: obj2yaml %p/Inputs/trivial-object-test.coff-mips | FileCheck %s --check-prefix COFF-MIPS
Copy link
Collaborator

Choose a reason for hiding this comment

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

We try to avoid pre-canned binaries in new tests, due to a) their opaqueness, making it hard/impossible to properly review and regenerate in the future, b) security concerns (it could do anything and be "accidentally" executed during build time - see https://discourse.llvm.org/t/ideas-for-removing-test-binaries-from-the-git-repo/81480 for more discussion), and c) the impact they have on the .git repo size.

With obj2yaml/yaml2obj, it's common for us to just have a round-trip test, i.e. start with a YAML doc, convert it to an object using yaml2obj, then convert it back using obj2yaml. If we have llvm-readobj support for a feature already, we'll add some checking with that of the object created by yaml2obj too. Presumably there's nothing particularly special about MIPS COFF beyond the machine and relocation types?

Finally, I'd consider putting this testing in its own file in the newer obj2yaml testsuite at tests/tools/obj2yaml, rather than adding to this outdated test file (that's in the wrong place and is a bit of an unfocused "soup" of a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'm adding test/tools/yaml2obj/COFF/basic-mips.yaml using the model basic-arm64.yaml.
I'm not adding it in the directory test/tools/obj2yaml/COFF, as tests here are not architecture-specific.

And indeed, nothing is special about MIPS COFF, except machine and relocation types.

@@ -0,0 +1,76 @@
!COFF
header: !Header
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's minimise this input file and inline it into the test that needs it. I think the only significant parts of it are the machine type and relocations. Most of the rest can be reduced/removed. It doesn't need to be a fully runnable binary, for instance, for the purposes of this feature.

@@ -39,3 +39,13 @@ Sections:

# BSS: Contents of section .bss:
# BSS-NEXT: <skipping contents of bss section at [12c8, 12cc)>

# RUN: yaml2obj %p/Inputs/COFF/mips.yaml | llvm-objdump -s - | FileCheck %s --check-prefix=COFF-MIPS
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's special about the MIPS behaviour here that requires an additional test of dumping section contents?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing special here, I was only adding the test here because other architectures had it.
I'm removing this test.

- handle IMAGE_FILE_MACHINE_R4000 machine type
- handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

Please could you avoid force pushes when adding additional changes to a PR under active revision, because it trashes the review history, rendering things like "view changes since last review" non-functional. Instead, you can simply add additional commits on top of the original commit, and if rebasing on main is needed for the review to make sense, use a merge commit from main.

@hpoussin hpoussin requested a review from jh7370 October 18, 2024 21:45
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

LGTM!

@hpoussin
Copy link
Contributor Author

hpoussin commented Nov 1, 2024

LGTM!

Ping. Is it possible to merge this PR?

@jh7370
Copy link
Collaborator

jh7370 commented Nov 1, 2024

LGTM!

Ping. Is it possible to merge this PR?

The LLVM expectation is that people merge their PRs (if they have commit access), or specifically ask for their PRs to be merged (if they don't have commit access), as the working assumption is that most contributors have commit access.

Regardless, this isn't ready to be merged, as the basic-mips.yaml test is failing. Please investigate and fix.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

Requesting changes to clear my approval, given the test failure.

@hpoussin hpoussin requested a review from jh7370 November 1, 2024 15:20
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

LGTM again. I'll get this merged now.

@jh7370 jh7370 merged commit 19f2f67 into llvm:main Nov 1, 2024
8 checks passed
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
- handle IMAGE_FILE_MACHINE_R4000 machine type
- handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
- handle IMAGE_FILE_MACHINE_R4000 machine type
- handle MIPS COFF relocations

llvm-objdump can now parse MIPS COFF files.
@hpoussin hpoussin deleted the clang-mips-coff-3 branch January 21, 2025 17:50
leecheechen added a commit to leecheechen/llvm-project that referenced this pull request Apr 30, 2025
- handle IMAGE_FILE_MACHINE_LOONGARCH64 machine type
- handle LOONGARCH COFF relocations

This patch is inspired by Mips's pr llvm#112591.
leecheechen added a commit to leecheechen/llvm-project that referenced this pull request May 6, 2025
- handle IMAGE_FILE_MACHINE_LOONGARCH64 machine type
- handle LOONGARCH COFF relocations

This patch is inspired by Mips's pr llvm#112591.
leecheechen added a commit to leecheechen/llvm-project that referenced this pull request May 7, 2025
- handle IMAGE_FILE_MACHINE_LOONGARCH64 machine type
- handle LOONGARCH COFF relocations

This patch is inspired by Mips's pr llvm#112591.
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.

3 participants