Skip to content

Commit 11dc7fc

Browse files
committedMay 23, 2018
ELF: Do not ICF two sections with different output sections.
Note that this doesn't do the right thing in the case where there is a linker script. We probably need to move output section assignment before ICF to get the correct behaviour here. Differential Revision: https://reviews.llvm.org/D47241 llvm-svn: 333052
1 parent 6de0ca4 commit 11dc7fc

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed
 

‎lld/ELF/ICF.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "SymbolTable.h"
7979
#include "Symbols.h"
8080
#include "SyntheticSections.h"
81+
#include "Writer.h"
8182
#include "lld/Common/Threads.h"
8283
#include "llvm/ADT/Hashing.h"
8384
#include "llvm/BinaryFormat/ELF.h"
@@ -302,6 +303,13 @@ bool ICF<ELFT>::equalsConstant(const InputSection *A, const InputSection *B) {
302303
A->getSize() != B->getSize() || A->Data != B->Data)
303304
return false;
304305

306+
// If two sections have different output sections, we cannot merge them.
307+
// FIXME: This doesn't do the right thing in the case where there is a linker
308+
// script. We probably need to move output section assignment before ICF to
309+
// get the correct behaviour here.
310+
if (getOutputSectionName(A) != getOutputSectionName(B))
311+
return false;
312+
305313
if (A->AreRelocsRela)
306314
return constantEq(A, A->template relas<ELFT>(), B,
307315
B->template relas<ELFT>());

‎lld/ELF/InputSection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ template <class ELFT> void InputSection::copyShtGroup(uint8_t *Buf) {
325325
*To++ = Sections[Idx]->getOutputSection()->SectionIndex;
326326
}
327327

328-
InputSectionBase *InputSection::getRelocatedSection() {
328+
InputSectionBase *InputSection::getRelocatedSection() const {
329329
if (!File || (Type != SHT_RELA && Type != SHT_REL))
330330
return nullptr;
331331
ArrayRef<InputSectionBase *> Sections = File->getSections();

‎lld/ELF/InputSection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class InputSection : public InputSectionBase {
317317

318318
static bool classof(const SectionBase *S);
319319

320-
InputSectionBase *getRelocatedSection();
320+
InputSectionBase *getRelocatedSection() const;
321321

322322
template <class ELFT, class RelTy>
323323
void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);

‎lld/ELF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static bool isSectionPrefix(StringRef Prefix, StringRef Name) {
9292
return Name.startswith(Prefix) || Name == Prefix.drop_back();
9393
}
9494

95-
StringRef elf::getOutputSectionName(InputSectionBase *S) {
95+
StringRef elf::getOutputSectionName(const InputSectionBase *S) {
9696
if (Config->Relocatable)
9797
return S->Name;
9898

‎lld/ELF/Writer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct PhdrEntry {
4848
};
4949

5050
void addReservedSymbols();
51-
llvm::StringRef getOutputSectionName(InputSectionBase *S);
51+
llvm::StringRef getOutputSectionName(const InputSectionBase *S);
5252

5353
template <class ELFT> uint32_t calcMipsEFlags();
5454

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
3+
# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | count 0
4+
5+
.section foo,"ax"
6+
.byte 42
7+
8+
.section bar,"ax"
9+
.byte 42

0 commit comments

Comments
 (0)
Please sign in to comment.