]> granicus.if.org Git - llvm/commitdiff
[MC] Accept a numeric value as an ELF section header's type
authorSimon Atanasyan <simon@atanasyan.com>
Fri, 10 Mar 2017 08:22:13 +0000 (08:22 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Fri, 10 Mar 2017 08:22:13 +0000 (08:22 +0000)
GAS supports specification of section header's type using a numeric
value [1]. This patch brings the same functionality to LLVM. That allows
to setup some target-specific section types belong to the SHT_LOPROC -
SHT_HIPROC range. If we attempt to print unknown section type, MCSectionELF
class shows an error message. It's better than print sole '@' sign
without any section type name.

In case of MIPS, example of such section's type is SHT_MIPS_DWARF.
Without the patch we will have to implement some workarounds
in probably not-MIPS-specific part of code base to convert SHT_MIPS_DWARF
to the @progbits while printing assembly and to assign SHT_MIPS_DWARF for
@progbits sections named .debug_* if we encounter such section in
an input assembly.

[1] https://sourceware.org/binutils/docs/as/Section.html

Differential Revision: https://reviews.llvm.org/D29719

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297446 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/MCParser/ELFAsmParser.cpp
lib/MC/MCSectionELF.cpp
test/MC/ELF/section-numeric-invalid-type.s [new file with mode: 0644]
test/MC/ELF/section-numeric-type.s [new file with mode: 0644]

index fff081f0445594f21b4519474c6fa693c405d741..8838f16d60ac888fdfa07fcf3d32724e2012a991 100644 (file)
@@ -395,7 +395,10 @@ bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
     return TokError("expected '@<type>', '%<type>' or \"<type>\"");
   if (!L.is(AsmToken::String))
     Lex();
-  if (getParser().parseIdentifier(TypeName))
+  if (L.is(AsmToken::Integer)) {
+    TypeName = getTok().getString();
+    Lex();
+  } else if (getParser().parseIdentifier(TypeName))
     return TokError("expected identifier in directive");
   return false;
 }
@@ -580,7 +583,7 @@ EndStmt:
       Type = ELF::SHT_NOTE;
     else if (TypeName == "unwind")
       Type = ELF::SHT_X86_64_UNWIND;
-    else
+    else if (TypeName.getAsInteger(0, Type))
       return TokError("unknown section type");
   }
 
index fdd9239a0f1a302e6acdbf1cd40152628aba7ab4..c73fafe151b09034a4d55a1ac631f01be2ca382f 100644 (file)
@@ -12,6 +12,7 @@
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/Support/ELF.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cassert>
 
@@ -140,6 +141,9 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
     OS << "progbits";
   else if (Type == ELF::SHT_X86_64_UNWIND)
     OS << "unwind";
+  else
+    report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
+                       " for section " + getSectionName());
 
   if (EntrySize) {
     assert(Flags & ELF::SHF_MERGE);
diff --git a/test/MC/ELF/section-numeric-invalid-type.s b/test/MC/ELF/section-numeric-invalid-type.s
new file mode 100644 (file)
index 0000000..3ae071b
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux-gnu %s -o - \
+// RUN:   | llvm-readobj -s -t | FileCheck --check-prefix=OBJ %s
+
+// RUN: not llvm-mc -filetype=asm -triple=x86_64-pc-linux-gnu %s -o - 2>&1 \
+// RUN:   | FileCheck --check-prefix=ASM %s
+
+  .section .sec,"a",@0x7fffffff
+
+// OBJ:      Section {
+// OBJ:        Name: .sec
+// OBJ-NEXT:   Type: (0x7FFFFFFF)
+// OBJ:      }
+
+// ASM: unsupported type 0x7fffffff for section .sec
diff --git a/test/MC/ELF/section-numeric-type.s b/test/MC/ELF/section-numeric-type.s
new file mode 100644 (file)
index 0000000..2e51bd4
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux-gnu %s -o - \
+// RUN:   | llvm-readobj -s -t | FileCheck --check-prefix=OBJ %s
+
+// RUN: llvm-mc -filetype=asm -triple=x86_64-pc-linux-gnu %s -o - \
+// RUN:   | FileCheck --check-prefix=ASM %s
+
+  .section .sec1,"a",@0x70000001
+  .section .sec2,"a",@1879048193
+
+// OBJ:      Section {
+// OBJ:        Name: .sec1
+// OBJ-NEXT:   Type: SHT_X86_64_UNWIND (0x70000001)
+// OBJ:      }
+// OBJ:      Section {
+// OBJ:        Name: .sec2
+// OBJ-NEXT:   Type: SHT_X86_64_UNWIND (0x70000001)
+// OBJ:      }
+
+// ASM: .section  .sec1,"a",@unwind
+// ASM: .section  .sec2,"a",@unwind