[ELF] Do not merge sections in case of relocatable object generation
Do not merge sections if generating a relocatable object. It makes
the code simpler because we do not need to update relocations addends
to reflect changes introduced by merging. Instead of that we write
such "merge" sections into separate OutputSections and keep SHF_MERGE
/ SHF_STRINGS flags and sh_entsize value to be able to perform merging
later during a final linking.
Differential Revision: http://reviews.llvm.org/D25066
llvm-svn: 283300
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 0cbee53..0a6c873 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -894,6 +894,10 @@
Sections.push_back(S);
S->OutSec = this;
this->updateAlignment(S->Alignment);
+ // Keep sh_entsize value of the input section to be able to perform merging
+ // later during a final linking using the generated relocatable object.
+ if (Config->Relocatable && (S->getSectionHdr()->sh_flags & SHF_MERGE))
+ this->Header.sh_entsize = S->getSectionHdr()->sh_entsize;
}
// This function is called after we sort input sections
@@ -1836,8 +1840,12 @@
// For SHF_MERGE we create different output sections for each alignment.
// This makes each output section simple and keeps a single level mapping from
// input to output.
+ // In case of relocatable object generation we do not try to perform merging
+ // and treat SHF_MERGE sections as regular ones, but also create different
+ // output sections for them to allow merging at final linking stage.
uintX_t Alignment = 0;
- if (isa<MergeInputSection<ELFT>>(C))
+ if (isa<MergeInputSection<ELFT>>(C) ||
+ (Config->Relocatable && (H->sh_flags & SHF_MERGE)))
Alignment = std::max(H->sh_addralign, H->sh_entsize);
uint32_t Type = H->sh_type;