[llvm-commits] [llvm] r114382 - in /llvm/trunk: lib/MC/ELFObjectWriter.cpp lib/MC/MCELFStreamer.cpp lib/MC/MCParser/AsmParser.cpp test/MC/ELF/common.s

Rafael Espindola rafael.espindola at gmail.com
Mon Sep 20 17:24:38 PDT 2010


Author: rafael
Date: Mon Sep 20 19:24:38 2010
New Revision: 114382

URL: http://llvm.org/viewvc/llvm-project?rev=114382&view=rev
Log:
Implement support for .local and its "interesting" interactions with .comm.

Added:
    llvm/trunk/test/MC/ELF/common.s
Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp
    llvm/trunk/lib/MC/MCELFStreamer.cpp
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=114382&r1=114381&r2=114382&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Mon Sep 20 19:24:38 2010
@@ -386,6 +386,8 @@
   if (Data.isCommon() && Data.isExternal())
     Value = Data.getCommonAlignment();
 
+  assert(!(Data.isCommon() && !Data.isExternal()));
+
   if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
     if (MCFragment *FF = Data.getFragment())
       Value = Layout.getSymbolAddress(&Data) -
@@ -502,7 +504,10 @@
     if (Base) {
       if (F && (!Symbol->isInSection() || SD.isCommon()) && !SD.isExternal()) {
         Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
-        Value += Layout.getSymbolAddress(&SD);
+
+        MCSectionData *FSD = F->getParent();
+        // Offset of the symbol in the section
+        Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
       } else
         Index = getSymbolIndexInSymbolTable(Asm, Symbol);
       if (Base != &SD)
@@ -672,7 +677,10 @@
     MSD.SymbolData = it;
     MSD.StringIndex = Entry;
 
-    if (Symbol.isUndefined()) {
+    if (it->isCommon()) {
+      MSD.SectionIndex = ELF::SHN_COMMON;
+      ExternalSymbolData.push_back(MSD);
+    } else if (Symbol.isUndefined()) {
       MSD.SectionIndex = ELF::SHN_UNDEF;
       // XXX: for some reason we dont Emit* this
       it->setFlags(it->getFlags() | ELF_STB_Global);
@@ -680,9 +688,6 @@
     } else if (Symbol.isAbsolute()) {
       MSD.SectionIndex = ELF::SHN_ABS;
       ExternalSymbolData.push_back(MSD);
-    } else if (it->isCommon()) {
-      MSD.SectionIndex = ELF::SHN_COMMON;
-      ExternalSymbolData.push_back(MSD);
     } else {
       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
       assert(MSD.SectionIndex && "Invalid section index!");

Modified: llvm/trunk/lib/MC/MCELFStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCELFStreamer.cpp?rev=114382&r1=114381&r2=114382&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCELFStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCELFStreamer.cpp Mon Sep 20 19:24:38 2010
@@ -13,6 +13,7 @@
 
 #include "llvm/MC/MCStreamer.h"
 
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/MC/MCAssembler.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCCodeEmitter.h"
@@ -109,6 +110,8 @@
   virtual void EmitInstruction(const MCInst &Inst);
   virtual void Finish();
 
+private:
+  SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
   /// @}
   void SetSection(StringRef Section, unsigned Type, unsigned Flags,
                   SectionKind Kind) {
@@ -187,6 +190,13 @@
   SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
 }
 
+static unsigned GetBinding(const MCSymbolData &SD) {
+  uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
+  assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
+         Binding == ELF::STB_WEAK);
+  return Binding;
+}
+
 static void SetType(MCSymbolData &SD, unsigned Type) {
   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
@@ -246,15 +256,19 @@
   case MCSA_Global:
     SetBinding(SD, ELF::STB_GLOBAL);
     SD.setExternal(true);
+    BindingExplicitlySet.insert(Symbol);
     break;
 
   case MCSA_WeakReference:
   case MCSA_Weak:
     SetBinding(SD, ELF::STB_WEAK);
+    BindingExplicitlySet.insert(Symbol);
     break;
 
   case MCSA_Local:
     SetBinding(SD, ELF::STB_LOCAL);
+    SD.setExternal(false);
+    BindingExplicitlySet.insert(Symbol);
     break;
 
   case MCSA_ELF_TypeFunction:
@@ -295,7 +309,12 @@
                                        unsigned ByteAlignment) {
   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
 
-  if ((SD.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local) {
+  if (!BindingExplicitlySet.count(Symbol)) {
+    SetBinding(SD, ELF::STB_GLOBAL);
+    SD.setExternal(true);
+  }
+
+  if (GetBinding(SD) == ELF_STB_Local) {
     const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
                                                                     MCSectionELF::SHT_NOBITS,
                                                                     MCSectionELF::SHF_WRITE |
@@ -306,13 +325,11 @@
     MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
     SD.setFragment(F);
     Symbol->setSection(*Section);
-    SD.setSize(MCConstantExpr::Create(Size, getContext()));
+  } else {
+    SD.setCommon(Size, ByteAlignment);
   }
 
-  SetBinding(SD, ELF::STB_GLOBAL);
-  SD.setExternal(true);
-
-  SD.setCommon(Size, ByteAlignment);
+  SD.setSize(MCConstantExpr::Create(Size, getContext()));
 }
 
 void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=114382&r1=114381&r2=114382&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Mon Sep 20 19:24:38 2010
@@ -961,6 +961,9 @@
 
     if (IDVal == ".globl" || IDVal == ".global")
       return ParseDirectiveSymbolAttribute(MCSA_Global);
+    // ELF only? Should it be here?
+    if (IDVal == ".local")
+      return ParseDirectiveSymbolAttribute(MCSA_Local);
     if (IDVal == ".hidden")
       return ParseDirectiveSymbolAttribute(MCSA_Hidden);
     if (IDVal == ".indirect_symbol")

Added: llvm/trunk/test/MC/ELF/common.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/common.s?rev=114382&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/common.s (added)
+++ llvm/trunk/test/MC/ELF/common.s Mon Sep 20 19:24:38 2010
@@ -0,0 +1,63 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump  --dump-section-data | FileCheck %s
+
+
+	.text
+
+// Test that this produces a regular local symbol.
+	.type	common1, at object
+	.local	common1
+	.comm	common1,1,1
+
+// CHECK: ('st_name', 1) # 'common1'
+// CHECK-NEXT: ('st_bind', 0)
+// CHECK-NEXT: ('st_type', 1)
+// CHECK-NEXT: ('st_other', 0)
+// CHECK-NEXT: ('st_shndx',
+// CHECK-NEXT: ('st_value', 0)
+// CHECK-NEXT: ('st_size', 1)
+
+
+// Same as common1, but with directives in a different order.
+	.local	common2
+	.type	common2, at object
+	.comm	common2,1,1
+
+// CHECK: ('st_name', 9) # 'common2'
+// CHECK-NEXT: ('st_bind', 0)
+// CHECK-NEXT: ('st_type', 1)
+// CHECK-NEXT: ('st_other', 0)
+// CHECK-NEXT: ('st_shndx',
+// CHECK-NEXT: ('st_value', 1)
+// CHECK-NEXT: ('st_size', 1)
+
+// Test that without an explicit .local we produce a global.
+	.type	common3, at object
+	.comm	common3,4,4
+
+// CHECK: ('st_name', 17) # 'common3'
+// CHECK-NEXT: ('st_bind', 1)
+// CHECK-NEXT: ('st_type', 1)
+// CHECK-NEXT: ('st_other', 0)
+// CHECK-NEXT: ('st_shndx', 65522)
+// CHECK-NEXT: ('st_value', 4)
+// CHECK-NEXT: ('st_size', 4)
+
+
+// Test that without an explicit .local we produce a global, even if the first
+// occurrence is not in a directive.
+	.globl	foo
+	.type	foo, at function
+foo:
+	movsbl	common4+3(%rip), %eax
+
+
+	.type	common4, at object
+	.comm	common4,40,16
+
+// CHECK: ('st_name', 29) # 'common4'
+// CHECK-NEXT: ('st_bind', 1)
+// CHECK-NEXT: ('st_type', 1)
+// CHECK-NEXT: ('st_other', 0)
+// CHECK-NEXT: ('st_shndx', 65522)
+// CHECK-NEXT: ('st_value', 16)
+// CHECK-NEXT: ('st_size', 40)





More information about the llvm-commits mailing list