[llvm-commits] [llvm] r118793 - in /llvm/trunk: lib/MC/ELFObjectWriter.cpp test/MC/ELF/weakref-reloc.s

Rafael Espindola rafael.espindola at gmail.com
Thu Nov 11 08:48:11 PST 2010


Author: rafael
Date: Thu Nov 11 10:48:11 2010
New Revision: 118793

URL: http://llvm.org/viewvc/llvm-project?rev=118793&view=rev
Log:
Fix the symbol index of weak references. Also make RecordRelocation a bit
easier to read by having const references to the symbol, aliased symbol and
renamed symbol.

Added:
    llvm/trunk/test/MC/ELF/weakref-reloc.s
Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=118793&r1=118792&r2=118793&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Thu Nov 11 10:48:11 2010
@@ -669,16 +669,16 @@
   int64_t Addend = 0;
   int Index = 0;
   int64_t Value = Target.getConstant();
-  const MCSymbol *Symbol = 0;
-  const MCSymbol *Renamed = 0;
+  const MCSymbol &Symbol = Target.getSymA()->getSymbol();
+  const MCSymbol &ASymbol = AliasedSymbol(Symbol);
+  const MCSymbol *RenamedP = Renames.lookup(&Symbol);
+  if (!RenamedP)
+    RenamedP = &ASymbol;
+  const MCSymbol &Renamed = *RenamedP;
 
   bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
   if (!Target.isAbsolute()) {
-    Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
-    Renamed = Renames.lookup(Symbol);
-    if (!Renamed)
-      Renamed = &Target.getSymA()->getSymbol();
-    MCSymbolData &SD = Asm.getSymbolData(*Symbol);
+    MCSymbolData &SD = Asm.getSymbolData(Symbol);
     MCFragment *F = SD.getFragment();
 
     if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
@@ -695,15 +695,6 @@
       Value += b - a;
     }
 
-    // Check that this case has already been fully resolved before we get
-    // here.
-    if (Symbol->isDefined() && !SD.isExternal() &&
-        IsPCRel &&
-        &Fragment->getParent()->getSection() == &Symbol->getSection()) {
-      llvm_unreachable("We don't need a relocation in this case.");
-      return;
-    }
-
     bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
     if (!RelocOnSymbol) {
       Index = F->getParent()->getOrdinal();
@@ -712,11 +703,10 @@
       // Offset of the symbol in the section
       Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
     } else {
-      UsedInReloc.insert(Renamed);
-      MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
-      if (RenamedSD.getFlags() & ELF_Other_Weakref) {
-        WeakrefUsedInReloc.insert(Symbol);
-      }
+      if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
+        WeakrefUsedInReloc.insert(&Renamed);
+      else
+        UsedInReloc.insert(&Renamed);
       Index = -1;
     }
     Addend = Value;
@@ -864,7 +854,7 @@
 
   ERE.Index = Index;
   ERE.Type = Type;
-  ERE.Symbol = Renamed;
+  ERE.Symbol = &Renamed;
 
   ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
 

Added: llvm/trunk/test/MC/ELF/weakref-reloc.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/weakref-reloc.s?rev=118793&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/weakref-reloc.s (added)
+++ llvm/trunk/test/MC/ELF/weakref-reloc.s Thu Nov 11 10:48:11 2010
@@ -0,0 +1,49 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump  | FileCheck %s
+
+// Test that the relocations point to the correct symbols. We used to get the
+// symbol index wrong for weakrefs when creating _GLOBAL_OFFSET_TABLE_.
+
+	.weakref	bar,foo
+        call    zed at PLT
+	call	bar
+
+// CHECK:      # Symbol 0x00000004
+// CHECK-NEXT: (('st_name', 0x00000009) # '_GLOBAL_OFFSET_TABLE_'
+// CHECK-NEXT:  ('st_bind', 0x00000001)
+// CHECK-NEXT:  ('st_type', 0x00000000)
+// CHECK-NEXT:  ('st_other', 0x00000000)
+// CHECK-NEXT:  ('st_shndx', 0x00000000)
+// CHECK-NEXT:  ('st_value', 0x00000000)
+// CHECK-NEXT:  ('st_size', 0x00000000)
+// CHECK-NEXT: ),
+// CHECK-NEXT: # Symbol 0x00000005
+// CHECK-NEXT: (('st_name', 0x00000001) # 'foo'
+// CHECK-NEXT:  ('st_bind', 0x00000002)
+// CHECK-NEXT:  ('st_type', 0x00000000)
+// CHECK-NEXT:  ('st_other', 0x00000000)
+// CHECK-NEXT:  ('st_shndx', 0x00000000)
+// CHECK-NEXT:  ('st_value', 0x00000000)
+// CHECK-NEXT:  ('st_size', 0x00000000)
+// CHECK-NEXT: ),
+// CHECK-NEXT: # Symbol 0x00000006
+// CHECK-NEXT: (('st_name', 0x00000005) # 'zed'
+// CHECK-NEXT:  ('st_bind', 0x00000001)
+// CHECK-NEXT:  ('st_type', 0x00000000)
+// CHECK-NEXT:  ('st_other', 0x00000000)
+// CHECK-NEXT:  ('st_shndx', 0x00000000)
+// CHECK-NEXT:  ('st_value', 0x00000000)
+// CHECK-NEXT:  ('st_size', 0x00000000)
+// CHECK-NEXT: ),
+
+// CHECK:      # Relocation 0x00000000
+// CHECK-NEXT: (('r_offset', 0x00000001)
+// CHECK-NEXT:  ('r_sym', 0x00000006)
+// CHECK-NEXT:  ('r_type', 0x00000004)
+// CHECK-NEXT:  ('r_addend', 0xfffffffc)
+// CHECK-NEXT: ),
+// CHECK-NEXT: # Relocation 0x00000001
+// CHECK-NEXT: (('r_offset', 0x00000006)
+// CHECK-NEXT:  ('r_sym', 0x00000005)
+// CHECK-NEXT:  ('r_type', 0x00000002)
+// CHECK-NEXT:  ('r_addend', 0xfffffffc)
+// CHECK-NEXT: ),





More information about the llvm-commits mailing list