ELF: Do not create copy relocations for references in writable sections.
They are unnecessary, as the dynamic loader can apply the original relocations
directly. This was also resulting in the creation of copy relocations in PIEs.
Differential Revision: http://reviews.llvm.org/D19089
llvm-svn: 266273
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 6bfe75d..45add5b 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -427,7 +427,9 @@
template <class ELFT>
template <class RelTy>
void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
- bool IsAlloc = C.getSectionHdr()->sh_flags & SHF_ALLOC;
+ uintX_t Flags = C.getSectionHdr()->sh_flags;
+ bool IsAlloc = Flags & SHF_ALLOC;
+ bool IsWrite = Flags & SHF_WRITE;
auto AddDyn = [=](const DynamicReloc<ELFT> &Reloc) {
if (IsAlloc)
@@ -480,10 +482,11 @@
AddDyn({Target->RelativeRel, C.OutSec, Offset, true, &Body,
getAddend<ELFT>(RI)});
- // If a symbol in a DSO is referenced directly instead of through GOT,
- // we need to create a copy relocation for the symbol.
+ // If a symbol in a DSO is referenced directly instead of through GOT
+ // in a read-only section, we need to create a copy relocation for the
+ // symbol.
if (auto *B = dyn_cast<SharedSymbol<ELFT>>(&Body)) {
- if (IsAlloc && Target->needsCopyRel<ELFT>(Type, *B)) {
+ if (IsAlloc && !IsWrite && Target->needsCopyRel<ELFT>(Type, *B)) {
if (!B->needsCopy())
addCopyRelSymbol(B);
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});