[lld] 315f8a5 - [ELF][PPC32] Don't report "relocation refers to a discarded section" for .got2

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 1 19:55:14 PST 2020


Author: Fangrui Song
Date: 2020-03-01T19:54:40-08:00
New Revision: 315f8a55f504f5b6073fe62b55a30ad5c21957a1

URL: https://github.com/llvm/llvm-project/commit/315f8a55f504f5b6073fe62b55a30ad5c21957a1
DIFF: https://github.com/llvm/llvm-project/commit/315f8a55f504f5b6073fe62b55a30ad5c21957a1.diff

LOG: [ELF][PPC32] Don't report "relocation refers to a discarded section" for .got2

Similar to D63182 [ELF][PPC64] Don't report "relocation refers to a discarded section" for .toc

Reviewed By: Bdragon28

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

Added: 
    lld/test/ELF/comdat-discarded-ppc32.s

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/Relocations.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 7aec542575a4..b3031a3723c2 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -438,11 +438,12 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
       // hopefully creates a frame that is ignored at runtime. Also, don't warn
       // on .gcc_except_table and debug sections.
       //
-      // See the comment in maybeReportUndefined for PPC64 .toc .
+      // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc
       auto *d = dyn_cast<Defined>(&sym);
       if (!d) {
         if (!isDebugSection(*sec) && sec->name != ".eh_frame" &&
-            sec->name != ".gcc_except_table" && sec->name != ".toc") {
+            sec->name != ".gcc_except_table" && sec->name != ".got2" &&
+            sec->name != ".toc") {
           uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx;
           Elf_Shdr_Impl<ELFT> sec =
               CHECK(file->getObj().sections(), file)[secIdx];

diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 60972e839ca3..280173c5420e 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -946,8 +946,12 @@ static bool maybeReportUndefined(Symbol &sym, InputSectionBase &sec,
   // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
   // spec says references from outside the group to a STB_LOCAL symbol are not
   // allowed. Work around the bug.
-  if (config->emachine == EM_PPC64 &&
-      cast<Undefined>(sym).discardedSecIdx != 0 && sec.name == ".toc")
+  //
+  // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible
+  // because .LC0-.LTOC is not representable if the two labels are in 
diff erent
+  // .got2
+  if (cast<Undefined>(sym).discardedSecIdx != 0 &&
+      (sec.name == ".got2" || sec.name == ".toc"))
     return false;
 
   bool isWarning =

diff  --git a/lld/test/ELF/comdat-discarded-ppc32.s b/lld/test/ELF/comdat-discarded-ppc32.s
new file mode 100644
index 000000000000..9b727846db5d
--- /dev/null
+++ b/lld/test/ELF/comdat-discarded-ppc32.s
@@ -0,0 +1,21 @@
+# REQUIRES: ppc
+# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
+# RUN: ld.lld %t.o %t.o -o /dev/null
+# RUN: ld.lld -r --fatal-warnings %t.o %t.o -o /dev/null
+
+## Similar to PPC64, clang/gcc PPC32 may emit a .rela.got2 which references a local symbol
+## defined in a discarded .rodata section. Unfortunately, .got2 cannot be placed in a comdat
+## because for lwz 3, .LC0-.LTOC(30), we cannot define .LC0 in a 
diff erent .got2 section.
+
+## Don't error "relocation refers to a discarded section".
+
+.section .text.foo,"axG", at progbits,foo,comdat
+.globl foo
+foo:
+ lwz 3, .LC0-.LTOC(30)
+.L0:
+
+.section .got2,"aw", at progbits
+.set .LTOC, .got2+32768
+.LC0:
+.long .L0


        


More information about the llvm-commits mailing list