[llvm] [yaml2obj][obj2yaml][objdump] Handle MIPS COFF files (PR #112591)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 11:12:48 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-objectyaml

Author: Hervé Poussineau (hpoussin)

<details>
<summary>Changes</summary>

- 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
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!.

``````````

</details>


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


More information about the llvm-commits mailing list