From 8c7f9745de6fb7d548efa8e56019e132813be5f8 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 5 Oct 2010 23:57:26 +0000 Subject: [PATCH] Use a relocation against the symbol if it is a PLT and the symbol is in another section. Common because of linkonce sections. llvm-svn: 115718 --- lib/MC/ELFObjectWriter.cpp | 13 +++++++++++-- test/MC/ELF/merge.s | 24 ++++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index d79029106b0..9f9681ae3ef 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -529,7 +529,8 @@ void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F, } static bool ShouldRelocOnSymbol(const MCSymbolData &SD, - const MCValue &Target) { + const MCValue &Target, + const MCFragment &F) { const MCSymbol &Symbol = SD.getSymbol(); if (Symbol.isUndefined()) return true; @@ -543,6 +544,14 @@ static bool ShouldRelocOnSymbol(const MCSymbolData &SD, if (SD.isExternal()) return true; + const llvm::MCSymbolRefExpr& Ref = *Target.getSymA(); + const MCSectionELF &Sec2 = + static_cast(F.getParent()->getSection()); + + if (Ref.getKind() == MCSymbolRefExpr::VK_PLT && + &Sec2 != &Section) + return true; + return false; } @@ -587,7 +596,7 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm, return; } - bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target); + bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment); if (!RelocOnSymbol) { Index = F->getParent()->getOrdinal(); diff --git a/test/MC/ELF/merge.s b/test/MC/ELF/merge.s index 00ffd5ee9a8..227d4140e16 100644 --- a/test/MC/ELF/merge.s +++ b/test/MC/ELF/merge.s @@ -10,10 +10,14 @@ movsd .Lfoo(%rip), %xmm1 movl $.Lfoo, %edi movl $.Lfoo+2, %edi + jmp foo@PLT .section .sec1,"aM",@progbits,16 .Lfoo: + .section bar,"ax",@progbits +foo: + // Section 4 is "sec1" // CHECK: # Section 4 // CHECK-NEXT: (('sh_name', 18) # '.sec1' @@ -22,8 +26,12 @@ // CHECK: # Symbol 1 // CHECK-NEXT: (('st_name', 1) # '.Lfoo' -// Symbol number 5 is section 4 -// CHECK: # Symbol 5 +// Symbol number 2 is foo +// CHECK: # Symbol 2 +// CHECK-NEXT: (('st_name', 7) # 'foo' + +// Symbol number 6 is section 4 +// CHECK: # Symbol 6 // CHECK-NEXT: (('st_name', 0) # '' // CHECK-NEXT: ('st_bind', 0) // CHECK-NEXT: ('st_type', 3) @@ -39,10 +47,10 @@ // CHECK-NEXT: ('r_addend', // CHECK-NEXT: ), -// Relocation 1 refers to symbol 5 +// Relocation 1 refers to symbol 6 // CHECK-NEXT: # Relocation 1 // CHECK-NEXT: (('r_offset', -// CHECK-NEXT: ('r_sym', 5) +// CHECK-NEXT: ('r_sym', 6) // CHECK-NEXT: ('r_type', 10) // CHECK-NEXT: ('r_addend', // CHECK-NEXT: ), @@ -54,4 +62,12 @@ // CHECK-NEXT: ('r_type', 10 // CHECK-NEXT: ('r_addend', // CHECK-NEXT: ), + +// Relocation 3 refers to symbol 2 +// CHECK-NEXT: # Relocation 3 +// CHECK-NEXT: (('r_offset', +// CHECK-NEXT: ('r_sym', 2) +// CHECK-NEXT: ('r_type', 4 +// CHECK-NEXT: ('r_addend', +// CHECK-NEXT: ), // CHECK-NEXT: ])