diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h --- a/llvm/include/llvm/BinaryFormat/ELF.h +++ b/llvm/include/llvm/BinaryFormat/ELF.h @@ -564,6 +564,15 @@ EF_MIPS_ARCH = 0xf0000000 // Mask for applying EF_MIPS_ARCH_ variant }; +// MIPS-specific section indexes +enum { + SHN_MIPS_ACOMMON = 0xff00, // Common symbols which are defined and allocated + SHN_MIPS_TEXT = 0xff01, // Not ABI compliant + SHN_MIPS_DATA = 0xff02, // Not ABI compliant + SHN_MIPS_SCOMMON = 0xff03, // Common symbols for global data area + SHN_MIPS_SUNDEFINED = 0xff04 // Undefined symbols for global data area +}; + // ELF Relocation types for Mips enum { #include "ELFRelocs/Mips.def" diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.h b/llvm/lib/ObjCopy/ELF/ELFObject.h --- a/llvm/lib/ObjCopy/ELF/ELFObject.h +++ b/llvm/lib/ObjCopy/ELF/ELFObject.h @@ -621,6 +621,11 @@ SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, + SYMBOL_MIPS_ACOMMON = ELF::SHN_MIPS_ACOMMON, + SYMBOL_MIPS_TEXT = ELF::SHN_MIPS_TEXT, + SYMBOL_MIPS_DATA = ELF::SHN_MIPS_DATA, + SYMBOL_MIPS_SCOMMON = ELF::SHN_MIPS_SCOMMON, + SYMBOL_MIPS_SUNDEFINED = ELF::SHN_MIPS_SUNDEFINED, SYMBOL_HIPROC = ELF::SHN_HIPROC, SYMBOL_LOOS = ELF::SHN_LOOS, SYMBOL_HIOS = ELF::SHN_HIOS, diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp --- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp @@ -655,6 +655,15 @@ return Index == SHN_AMDGPU_LDS; } + if (Machine == EM_MIPS) { + switch (Index) { + case SHN_MIPS_ACOMMON: + case SHN_MIPS_SCOMMON: + case SHN_MIPS_SUNDEFINED: + return true; + } + } + if (Machine == EM_HEXAGON) { switch (Index) { case SHN_HEXAGON_SCOMMON: diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -755,6 +755,8 @@ void ScalarEnumerationTraits::enumeration( IO &IO, ELFYAML::ELF_SHN &Value) { + const auto *Object = static_cast(IO.getContext()); + assert(Object && "The IO context is not initialized"); #define ECase(X) IO.enumCase(Value, #X, ELF::X) ECase(SHN_UNDEF); ECase(SHN_LORESERVE); @@ -767,6 +769,15 @@ ECase(SHN_XINDEX); ECase(SHN_HIRESERVE); ECase(SHN_AMDGPU_LDS); + + if (!IO.outputting() || Object->getMachine() == ELF::EM_MIPS) { + ECase(SHN_MIPS_ACOMMON); + ECase(SHN_MIPS_TEXT); + ECase(SHN_MIPS_DATA); + ECase(SHN_MIPS_SCOMMON); + ECase(SHN_MIPS_SUNDEFINED); + } + ECase(SHN_HEXAGON_SCOMMON); ECase(SHN_HEXAGON_SCOMMON_1); ECase(SHN_HEXAGON_SCOMMON_2); diff --git a/llvm/test/tools/llvm-objcopy/ELF/hexagon-unsupported-on-x86.test b/llvm/test/tools/llvm-objcopy/ELF/hexagon-unsupported-on-x86.test deleted file mode 100644 --- a/llvm/test/tools/llvm-objcopy/ELF/hexagon-unsupported-on-x86.test +++ /dev/null @@ -1,15 +0,0 @@ -# RUN: yaml2obj %s -o %t -# RUN: not llvm-objcopy %t %t2 2>&1 >/dev/null | FileCheck %s - -!ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_X86_64 -Symbols: - - Name: test - Index: SHN_HEXAGON_SCOMMON - Binding: STB_GLOBAL - -# CHECK: symbol 'test' has unsupported value greater than or equal to SHN_LORESERVE: 65280 diff --git a/llvm/test/tools/llvm-objcopy/ELF/mips-symbol.test b/llvm/test/tools/llvm-objcopy/ELF/mips-symbol.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/mips-symbol.test @@ -0,0 +1,62 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj --symbols %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_MIPS +Symbols: + - Name: test_mips_acommon + Index: SHN_MIPS_ACOMMON + Value: 0x1234 + Binding: STB_GLOBAL + - Name: test_mips_scommon + Index: SHN_MIPS_SCOMMON + Value: 0x1237 + Binding: STB_GLOBAL + - Name: test_mips_sundefined + Index: SHN_MIPS_SUNDEFINED + Value: 0x1238 + Binding: STB_GLOBAL + +# CHECK: Symbols [ +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: +# CHECK-NEXT: Value: 0x0 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Binding: Local (0x0) +# CHECK-NEXT: Type: None (0x0) +# CHECK-NEXT: Other: 0 +# CHECK-NEXT: Section: Undefined (0x0) +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: test_mips_acommon +# CHECK-NEXT: Value: 0x1234 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Binding: Global (0x1) +# CHECK-NEXT: Type: None (0x0) +# CHECK-NEXT: Other: 0 +# CHECK-NEXT: Section: Processor Specific (0xFF00) +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: test_mips_scommon +# CHECK-NEXT: Value: 0x1237 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Binding: Global (0x1) +# CHECK-NEXT: Type: None (0x0) +# CHECK-NEXT: Other: 0 +# CHECK-NEXT: Section: Processor Specific (0xFF03) +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: test_mips_sundefined +# CHECK-NEXT: Value: 0x1238 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Binding: Global (0x1) +# CHECK-NEXT: Type: None (0x0) +# CHECK-NEXT: Other: 0 +# CHECK-NEXT: Section: Processor Specific (0xFF04) +# CHECK-NEXT: } +# CHECK-NEXT:] diff --git a/llvm/test/tools/llvm-objcopy/ELF/unsupported-machine-specific-shndx.test b/llvm/test/tools/llvm-objcopy/ELF/unsupported-machine-specific-shndx.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/unsupported-machine-specific-shndx.test @@ -0,0 +1,17 @@ +# RUN: yaml2obj %s -o %t -DMACHINE=HEXAGON +# RUN: not llvm-objcopy %t %t2 2>&1 >/dev/null | FileCheck %s -DINDEX=65280 +# RUN: yaml2obj %s -o %t -DMACHINE=MIPS +# RUN: not llvm-objcopy %t %t2 2>&1 >/dev/null | FileCheck %s -DINDEX=65283 + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Symbols: + - Name: test + Index: SHN_[[MACHINE]]_SCOMMON + Binding: STB_GLOBAL + +# CHECK: symbol 'test' has unsupported value greater than or equal to SHN_LORESERVE: [[INDEX]] diff --git a/llvm/test/tools/obj2yaml/ELF/special-symbol-indices.yaml b/llvm/test/tools/obj2yaml/ELF/special-symbol-indices.yaml --- a/llvm/test/tools/obj2yaml/ELF/special-symbol-indices.yaml +++ b/llvm/test/tools/obj2yaml/ELF/special-symbol-indices.yaml @@ -1,5 +1,7 @@ -# RUN: yaml2obj %s -o %t -# RUN: obj2yaml %t | FileCheck %s +# RUN: yaml2obj %s -o %t -DMACHINE=HEXAGON +# RUN: obj2yaml %t | FileCheck %s -DMACHINE=HEXAGON -DNAME=SCOMMON_1 +# RUN: yaml2obj %s -o %t -DMACHINE=MIPS +# RUN: obj2yaml %t | FileCheck %s -DMACHINE=MIPS -DNAME=TEXT ## Test checks that we are able to handle symbols with special/reserved indices. @@ -15,7 +17,7 @@ # CHECK-NEXT: Section: .text # CHECK-NEXT: Binding: STB_GLOBAL # CHECK-NEXT: - Name: processor_specific_index -# CHECK-NEXT: Index: SHN_HEXAGON_SCOMMON_1 +# CHECK-NEXT: Index: SHN_[[MACHINE]]_[[NAME]] # CHECK-NEXT: Binding: STB_GLOBAL # CHECK-NEXT: - Name: unknown_index # CHECK-NEXT: Index: 0xFFFE @@ -26,7 +28,7 @@ Class: ELFCLASS64 Data: ELFDATA2LSB Type: ET_EXEC - Machine: EM_HEXAGON + Machine: EM_[[MACHINE]] Sections: - Name: .text Type: SHT_PROGBITS @@ -42,7 +44,7 @@ Index: 0x1 Binding: STB_GLOBAL - Name: processor_specific_index - Index: SHN_HEXAGON_SCOMMON_1 + Index: 0xff01 Binding: STB_GLOBAL - Name: unknown_index Index: 0xfffe