From 09247f8955f3cf868cfee2cc54b2b1b9e8f270b3 Mon Sep 17 00:00:00 2001 From: Sean Silva Date: Thu, 4 Feb 2016 21:41:07 +0000 Subject: [PATCH] Don't push relocation sections onto InputSection::Discarded.RelocSections Summary: LLVM3.3 (and earlier) would fail to include a relocation section in the group that the section it was relocating is in. Object files affected by this issue have been encountered in the wild when using LLD. This would result in a siutation like: Section { Index: 5 Name: .text._Z3fooIiEvv (6) Type: SHT_PROGBITS (0x1) Flags [ (0x206) SHF_ALLOC (0x2) SHF_EXECINSTR (0x4) SHF_GROUP (0x200) ] Address: 0x0 Offset: 0x48 Size: 5 Link: 0 Info: 0 AddressAlignment: 1 EntrySize: 0 } Section { Index: 6 Name: .rela.text._Z3fooIiEvv (1) Type: SHT_RELA (0x4) Flags [ (0x0) ] Address: 0x0 Offset: 0x3F0 Size: 24 Link: 8 Info: 5 AddressAlignment: 8 EntrySize: 24 } In LLD, during symbol resolution, we discard the section containing the weak symbol, but this amounts to replacing it with InputSection::Discarded. When we later saw the corresponding relocation section, we would then end up pusing onto InputSection::Discarded.RelocSections, which is bogus. Reviewers: ruiu, rafael Subscribers: llvm-commits, Bigcheese Differential Revision: http://reviews.llvm.org/D16898 llvm-svn: 259831 --- lld/ELF/InputFiles.cpp | 5 +++++ lld/test/ELF/Inputs/llvm33-rela-outside-group.o | Bin 0 -> 1032 bytes lld/test/ELF/llvm33-rela-outside-group.s | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 lld/test/ELF/Inputs/llvm33-rela-outside-group.o create mode 100644 lld/test/ELF/llvm33-rela-outside-group.s diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index cee398bd8a9c..21b385a8683d 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -214,6 +214,11 @@ void ObjectFile::initializeSections(DenseSet &ComdatGroups) { fatal("Invalid relocated section index"); InputSectionBase *RelocatedSection = Sections[RelocatedSectionIndex]; + // Strictly speaking, a relocation section must be included in the + // group of the section it relocates. However, LLVM 3.3 and earlier + // would fail to do so, so we gracefully handle that case. + if (RelocatedSection == &InputSection::Discarded) + continue; if (!RelocatedSection) fatal("Unsupported relocation reference"); if (auto *S = dyn_cast>(RelocatedSection)) { diff --git a/lld/test/ELF/Inputs/llvm33-rela-outside-group.o b/lld/test/ELF/Inputs/llvm33-rela-outside-group.o new file mode 100644 index 0000000000000000000000000000000000000000..2e2f2dc99ea3de3bd0c036976f945c0fc92c32e6 GIT binary patch literal 1032 zcmbtTJ5B>Z4E1J#02Oqp5E325G*BY61wnw|0yJn=gpUMTv^zvlaSe{cF*pJx2S5Tp zuP1UwQy{REvHkpc9?#}we0n%2ijq_c+NUBqM!y=n61$>R8cOaW`yYb2c5>%km^=k{ zalUiaG{?8&>68Q^S9cw`>$Z7(AlKb=p$)!fE9&Qa@h^Sw((GTy7FVxlI#h}+8@D(9k7A=*B4j{jZ3^xH`J zwS88V*~!3{V+5YtB=GbVdb(0xZk`&Mo0)vfy*8iPd$n)L{s%QD-V?hW^6%@rmmk?^ BFwFn} literal 0 HcmV?d00001 diff --git a/lld/test/ELF/llvm33-rela-outside-group.s b/lld/test/ELF/llvm33-rela-outside-group.s new file mode 100644 index 000000000000..612753706f8a --- /dev/null +++ b/lld/test/ELF/llvm33-rela-outside-group.s @@ -0,0 +1,11 @@ +// Input file generated with: +// llvm33/llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %S/Inputs/llvm33-rela-outside-group.o +// +// RUN: ld.lld -shared %S/Inputs/llvm33-rela-outside-group.o %S/Inputs/llvm33-rela-outside-group.o + + .global bar + .weak _Z3fooIiEvv + + .section .text._Z3fooIiEvv,"axG",@progbits,_Z3fooIiEvv,comdat +_Z3fooIiEvv: + callq bar