[llvm] r294589 - Make it possible to set SHF_LINK_ORDER explicitly.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 9 06:59:21 PST 2017


Author: rafael
Date: Thu Feb  9 08:59:20 2017
New Revision: 294589

URL: http://llvm.org/viewvc/llvm-project?rev=294589&view=rev
Log:
Make it possible to set SHF_LINK_ORDER explicitly.

This will make it possible to add support for gcing user metadata
(asan for example).

Added:
    llvm/trunk/test/MC/ELF/section-metadata-err1.s
    llvm/trunk/test/MC/ELF/section-metadata-err2.s
    llvm/trunk/test/MC/ELF/section-metadata-err3.s
    llvm/trunk/test/MC/ELF/section-metadata-err4.s
Modified:
    llvm/trunk/docs/Extensions.rst
    llvm/trunk/include/llvm/MC/MCContext.h
    llvm/trunk/lib/MC/ELFObjectWriter.cpp
    llvm/trunk/lib/MC/MCContext.cpp
    llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp
    llvm/trunk/test/MC/ELF/section.s

Modified: llvm/trunk/docs/Extensions.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/Extensions.rst?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/docs/Extensions.rst (original)
+++ llvm/trunk/docs/Extensions.rst Thu Feb  9 08:59:20 2017
@@ -204,6 +204,24 @@ For example, the following code creates
 The unique number is not present in the resulting object at all. It is just used
 in the assembler to differentiate the sections.
 
+The 'm' flag is mapped to SHF_LINK_ORDER. If it is present, a symbol
+must be given that identifies the section to be placed is the
+.sh_link.
+
+.. code-block:: gas
+
+        .section .foo,"a", at progbits
+        .Ltmp:
+        .section .bar,"am", at progbits,.Ltmp
+
+which is equivalent to just
+
+.. code-block:: gas
+
+        .section .foo,"a", at progbits
+        .section .bar,"am", at progbits,.foo
+
+
 Target Specific Behaviour
 =========================
 

Modified: llvm/trunk/include/llvm/MC/MCContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCContext.h?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCContext.h (original)
+++ llvm/trunk/include/llvm/MC/MCContext.h Thu Feb  9 08:59:20 2017
@@ -357,7 +357,15 @@ namespace llvm {
 
     MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
                                 unsigned Flags, unsigned EntrySize,
-                                const Twine &Group, unsigned UniqueID);
+                                const Twine &Group, unsigned UniqueID) {
+      return getELFSection(Section, Type, Flags, EntrySize, Group, UniqueID,
+                           nullptr);
+    }
+
+    MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
+                                unsigned Flags, unsigned EntrySize,
+                                const Twine &Group, unsigned UniqueID,
+                                const MCSectionELF *Associated);
 
     MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
                                 unsigned Flags, unsigned EntrySize,

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Thu Feb  9 08:59:20 2017
@@ -1157,8 +1157,7 @@ void ELFObjectWriter::writeSection(const
     break;
   }
 
-  if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
-      Section.getType() == ELF::SHT_ARM_EXIDX)
+  if (Section.getFlags() & ELF::SHF_LINK_ORDER)
     sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
 
   WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),

Modified: llvm/trunk/lib/MC/MCContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCContext.cpp?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCContext.cpp (original)
+++ llvm/trunk/lib/MC/MCContext.cpp Thu Feb  9 08:59:20 2017
@@ -358,13 +358,14 @@ MCSectionELF *MCContext::getELFNamedSect
 
 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
                                        unsigned Flags, unsigned EntrySize,
-                                       const Twine &Group, unsigned UniqueID) {
+                                       const Twine &Group, unsigned UniqueID,
+                                       const MCSectionELF *Associated) {
   MCSymbolELF *GroupSym = nullptr;
   if (!Group.isTriviallyEmpty() && !Group.str().empty())
     GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
 
   return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
-                       nullptr);
+                       Associated);
 }
 
 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,

Modified: llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp Thu Feb  9 08:59:20 2017
@@ -157,6 +157,7 @@ private:
   bool maybeParseSectionType(StringRef &TypeName);
   bool parseMergeSize(int64_t &Size);
   bool parseGroup(StringRef &GroupName);
+  bool parseMetadataSym(MCSectionELF *&Associated);
   bool maybeParseUniqueID(int64_t &UniqueID);
 };
 
@@ -297,6 +298,9 @@ static unsigned parseSectionFlags(String
     case 'w':
       flags |= ELF::SHF_WRITE;
       break;
+    case 'm':
+      flags |= ELF::SHF_LINK_ORDER;
+      break;
     case 'M':
       flags |= ELF::SHF_MERGE;
       break;
@@ -425,6 +429,21 @@ bool ELFAsmParser::parseGroup(StringRef
   return false;
 }
 
+bool ELFAsmParser::parseMetadataSym(MCSectionELF *&Associated) {
+  MCAsmLexer &L = getLexer();
+  if (L.isNot(AsmToken::Comma))
+    return TokError("expected metadata symbol");
+  Lex();
+  StringRef Name;
+  if (getParser().parseIdentifier(Name))
+    return true;
+  MCSymbol *Sym = getContext().lookupSymbol(Name);
+  if (!Sym || !Sym->isInSection())
+    return TokError("symbol is not in a section: " + Name);
+  Associated = cast<MCSectionELF>(&Sym->getSection());
+  return false;
+}
+
 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
   MCAsmLexer &L = getLexer();
   if (L.isNot(AsmToken::Comma))
@@ -460,6 +479,7 @@ bool ELFAsmParser::ParseSectionArguments
   const MCExpr *Subsection = nullptr;
   bool UseLastGroup = false;
   StringRef UniqueStr;
+  MCSectionELF *Associated = nullptr;
   int64_t UniqueID = ~0;
 
   // Set the defaults first.
@@ -522,6 +542,9 @@ bool ELFAsmParser::ParseSectionArguments
     if (Group)
       if (parseGroup(GroupName))
         return true;
+    if (Flags & ELF::SHF_LINK_ORDER)
+      if (parseMetadataSym(Associated))
+        return true;
     if (maybeParseUniqueID(UniqueID))
       return true;
   }
@@ -571,8 +594,8 @@ EndStmt:
       }
   }
 
-  MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
-                                                     Size, GroupName, UniqueID);
+  MCSection *ELFSection = getContext().getELFSection(
+      SectionName, Type, Flags, Size, GroupName, UniqueID, Associated);
   getStreamer().SwitchSection(ELFSection, Subsection);
 
   if (getContext().getGenDwarfForAssembly()) {

Added: llvm/trunk/test/MC/ELF/section-metadata-err1.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/section-metadata-err1.s?rev=294589&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/section-metadata-err1.s (added)
+++ llvm/trunk/test/MC/ELF/section-metadata-err1.s Thu Feb  9 08:59:20 2017
@@ -0,0 +1,5 @@
+// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s
+
+// CHECK: error: symbol is not in a section: foo
+
+        .section .shf_metadata,"am", at progbits,foo

Added: llvm/trunk/test/MC/ELF/section-metadata-err2.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/section-metadata-err2.s?rev=294589&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/section-metadata-err2.s (added)
+++ llvm/trunk/test/MC/ELF/section-metadata-err2.s Thu Feb  9 08:59:20 2017
@@ -0,0 +1,6 @@
+// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s
+
+// CHECK: error: symbol is not in a section: foo
+
+        .quad foo
+        .section .shf_metadata,"am", at progbits,foo

Added: llvm/trunk/test/MC/ELF/section-metadata-err3.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/section-metadata-err3.s?rev=294589&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/section-metadata-err3.s (added)
+++ llvm/trunk/test/MC/ELF/section-metadata-err3.s Thu Feb  9 08:59:20 2017
@@ -0,0 +1,6 @@
+// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s
+
+// CHECK: error: symbol is not in a section: foo
+
+        foo = 42
+        .section .shf_metadata,"am", at progbits,foo

Added: llvm/trunk/test/MC/ELF/section-metadata-err4.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/section-metadata-err4.s?rev=294589&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/section-metadata-err4.s (added)
+++ llvm/trunk/test/MC/ELF/section-metadata-err4.s Thu Feb  9 08:59:20 2017
@@ -0,0 +1,5 @@
+// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s
+
+// CHECK: error: expected metadata symbol
+
+        .section .shf_metadata,"am", at progbits

Modified: llvm/trunk/test/MC/ELF/section.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/section.s?rev=294589&r1=294588&r2=294589&view=diff
==============================================================================
--- llvm/trunk/test/MC/ELF/section.s (original)
+++ llvm/trunk/test/MC/ELF/section.s Thu Feb  9 08:59:20 2017
@@ -149,3 +149,69 @@ bar:
 // CHECK:          Name: bar-"foo"
 // CHECK:        Section {
 // CHECK:          Name: foo
+
+// Test SHF_LINK_ORDER
+
+.section .shf_metadata_target1, "a"
+        .quad 0
+.section .shf_metadata_target2, "a", @progbits, unique, 1
+.Lshf_metadata_target2_1:
+        .quad 0
+.section .shf_metadata_target2, "a", @progbits, unique, 2
+.Lshf_metadata_target2_2:
+        .quad 0
+
+.section .shf_metadata1,"am", at progbits,.Lshf_metadata_target2_1
+.section .shf_metadata2,"am", at progbits,.Lshf_metadata_target2_2
+.section .shf_metadata3,"am", at progbits,.shf_metadata_target1
+
+// CHECK:      Section {
+// CHECK:        Index: 22
+// CHECK-NEXT:   Name: .shf_metadata_target1
+
+// CHECK:      Section {
+// CHECK:        Index: 23
+// CHECK-NEXT:   Name: .shf_metadata_target2
+
+// CHECK:      Section {
+// CHECK:        Index: 24
+// CHECK-NEXT:   Name: .shf_metadata_target2
+
+// CHECK:      Section {
+// CHECK:        Name: .shf_metadata1
+// CHECK-NEXT:   Type: SHT_PROGBITS
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:     SHF_ALLOC
+// CHECK-NEXT:     SHF_LINK_ORDER
+// CHECK-NEXT:   ]
+// CHECK-NEXT:   Address:
+// CHECK-NEXT:   Offset:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Link:    23
+// CHECK-NEXT:   Info:    0
+
+// CHECK:      Section {
+// CHECK:        Name: .shf_metadata2
+// CHECK-NEXT:   Type: SHT_PROGBITS
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:     SHF_ALLOC
+// CHECK-NEXT:     SHF_LINK_ORDER
+// CHECK-NEXT:   ]
+// CHECK-NEXT:   Address:
+// CHECK-NEXT:   Offset:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Link:    24
+// CHECK-NEXT:   Info:    0
+
+// CHECK:      Section {
+// CHECK:        Name: .shf_metadata3
+// CHECK-NEXT:   Type: SHT_PROGBITS
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:     SHF_ALLOC
+// CHECK-NEXT:     SHF_LINK_ORDER
+// CHECK-NEXT:   ]
+// CHECK-NEXT:   Address:
+// CHECK-NEXT:   Offset:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Link:    22
+// CHECK-NEXT:   Info:    0




More information about the llvm-commits mailing list