Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -45,6 +45,13 @@ StringRef Name; + // This pointer points to the "real" instance of this instance. + // Usually Repl == this. However, if ICF merges two sections, + // Repl pointer of one section points to another section. So, + // if you need to get a pointer to this instance, do not use + // this but instead this->Repl. + SectionBase *Repl; + unsigned SectionKind : 3; // The next two bit fields are only used by InputSectionBase, but we @@ -77,9 +84,9 @@ SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags, uint64_t Entsize, uint64_t Alignment, uint32_t Type, uint32_t Info, uint32_t Link) - : Name(Name), SectionKind(SectionKind), Live(false), Bss(false), - Alignment(Alignment), Flags(Flags), Entsize(Entsize), Type(Type), - Link(Link), Info(Info) {} + : Name(Name), Repl(this), SectionKind(SectionKind), Live(false), + Bss(false), Alignment(Alignment), Flags(Flags), Entsize(Entsize), + Type(Type), Link(Link), Info(Info) {} }; // This corresponds to a section of an input file. @@ -146,13 +153,6 @@ NumRelocations); } - // This pointer points to the "real" instance of this instance. - // Usually Repl == this. However, if ICF merges two sections, - // Repl pointer of one section points to another section. So, - // if you need to get a pointer to this instance, do not use - // this but instead this->Repl. - InputSectionBase *Repl; - // InputSections that are dependent on us (reverse dependency for GC) llvm::TinyPtrVector DependentSections; Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -87,7 +87,7 @@ StringRef Name, Kind SectionKind) : SectionBase(SectionKind, Name, Flags, Entsize, Alignment, Type, Info, Link), - File(File), Data(Data), Repl(this) { + File(File), Data(Data) { NumRelocations = 0; AreRelocsRela = false; Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -45,8 +45,6 @@ case Symbol::DefinedKind: { auto &D = cast(Sym); SectionBase *IS = D.Section; - if (auto *ISB = dyn_cast_or_null(IS)) - IS = ISB->Repl; // According to the ELF spec reference to a local symbol from outside // the group are not allowed. Unfortunately .eh_frame breaks that rule @@ -59,6 +57,7 @@ if (!IS) return D.Value; + IS = IS->Repl; uint64_t Offset = D.Value; // An object in an SHF_MERGE section might be referenced via a @@ -161,11 +160,8 @@ OutputSection *Symbol::getOutputSection() const { if (auto *S = dyn_cast(this)) { - if (auto *Sec = S->Section) { - if (auto *IS = dyn_cast(Sec)) - Sec = IS->Repl; - return Sec->getOutputSection(); - } + if (auto *Sec = S->Section) + return Sec->Repl->getOutputSection(); return nullptr; } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -516,13 +516,10 @@ SectionBase *Sec = D->Section; if (!Sec) return true; - if (auto *IS = dyn_cast(Sec)) { - Sec = IS->Repl; - IS = cast(Sec); - // Exclude symbols pointing to garbage-collected sections. - if (!IS->Live) - return false; - } + Sec = Sec->Repl; + // Exclude symbols pointing to garbage-collected sections. + if (isa(Sec) && !Sec->Live) + return false; if (auto *S = dyn_cast(Sec)) if (!S->getSectionPiece(D->Value)->Live) return false;