Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -427,6 +427,17 @@ return Cie; } +template +static InputSectionBase *getRelocTarget(InputSectionBase *Sec, RelTy &Rel) { + SymbolBody &B = Sec->template getFile()->getRelocTargetSym(Rel); + auto *D = dyn_cast(&B); + if (!D || !D->Section) + return nullptr; + auto *Target = + cast(cast(D->Section)->Repl); + return Target; +} + // There is one FDE per function. Returns true if a given FDE // points to a live function. template @@ -444,13 +455,7 @@ if (FirstRelI == (unsigned)-1) return false; - const RelTy &Rel = Rels[FirstRelI]; - SymbolBody &B = Sec->template getFile()->getRelocTargetSym(Rel); - auto *D = dyn_cast(&B); - if (!D || !D->Section) - return false; - auto *Target = - cast(cast(D->Section)->Repl); + InputSectionBase *Target = getRelocTarget(Sec, Rels[FirstRelI]); return Target && Target->Live; } @@ -485,7 +490,6 @@ if (!isFdeLive(Piece, Rels)) continue; Cie->FdePieces.push_back(&Piece); - NumFdes++; } } @@ -533,15 +537,36 @@ if (this->Size) return; // Already finalized. + llvm::DenseSet> UniqueFdeMap; size_t Off = 0; for (CieRecord *Cie : Cies) { Cie->Piece->OutputOff = Off; Off += alignTo(Cie->Piece->size(), Config->Wordsize); - for (EhSectionPiece *Fde : Cie->FdePieces) { - Fde->OutputOff = Off; - Off += alignTo(Fde->size(), Config->Wordsize); + // DIE entries are uniquified by address this FDE applies. We uniquify pairs + // of sections where FDE function resides in and appropriate relocation + // addends. That allows to filter out excessive duplicate FDE entries. + for (EhSectionPiece *&Fde : Cie->FdePieces) { + InputSectionBase *Sec = Fde->ID; + std::pair P; + if (Sec->AreRelocsRela) { + auto &Rel = Sec->template relas()[Fde->FirstRelocation]; + P = {getRelocTarget(Sec, Rel), getAddend(Rel)}; + } else { + auto &Rel = Sec->template rels()[Fde->FirstRelocation]; + P = {getRelocTarget(Sec, Rel), getAddend(Rel)}; + } + + if (UniqueFdeMap.insert(P).second) { + Fde->OutputOff = Off; + Off += alignTo(Fde->size(), Config->Wordsize); + continue; + } + Fde = nullptr; } + + llvm::erase_if(Cie->FdePieces, [](EhSectionPiece *P) { return !P; }); + NumFdes += Cie->FdePieces.size(); } // The LSB standard does not allow a .eh_frame section with zero @@ -1929,13 +1954,13 @@ template void EhFrameHeader::writeTo(uint8_t *Buf) { const endianness E = ELFT::TargetEndianness; - // Sort the FDE list by their PC and uniqueify. Usually there is only - // one FDE for a PC (i.e. function), but if ICF merges two functions - // into one, there can be more than one FDEs pointing to the address. + // Sort the FDE list by their PC. auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; std::stable_sort(Fdes.begin(), Fdes.end(), Less); - auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; - Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); + assert(std::unique(Fdes.begin(), Fdes.end(), + [](const FdeData &A, const FdeData &B) { + return A.Pc == B.Pc; + }) == Fdes.end()); Buf[0] = 1; Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; Index: test/ELF/eh-frame-hdr-icf-fde.s =================================================================== --- test/ELF/eh-frame-hdr-icf-fde.s +++ test/ELF/eh-frame-hdr-icf-fde.s @@ -0,0 +1,95 @@ +# REQUIRES: x86 + +## Testcase checks that we correctly deduplicate FDEs when ICF +## merges two sections. + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: ld.lld %t -o %t2 --icf=all --eh-frame-hdr +# RUN: llvm-readobj -r %t | FileCheck %s --check-prefix=OBJ +# RUN: llvm-readobj -s -section-data %t2 | FileCheck %s + +# OBJ: Relocations [ +# OBJ-NEXT: Section {{.*}} .rela.eh_frame { +# OBJ-NEXT: 0x20 R_X86_64_PC32 .text.f1 0x0 +# OBJ-NEXT: 0x34 R_X86_64_PC32 .text.f1 0x2 +# OBJ-NEXT: 0x48 R_X86_64_PC32 .text.f2 0x0 +# OBJ-NEXT: 0x5C R_X86_64_PC32 .text.f2 0x2 +# OBJ-NEXT: } +# OBJ-NEXT: ] + +# CHECK: Section { +# CHECK: Index: 1 +# CHECK: Name: .eh_frame_hdr +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x200158 +# CHECK-NEXT: Offset: 0x158 +# CHECK-NEXT: Size: 28 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: SectionData ( +# CHECK-NEXT: 0000: 011B033B 1C000000 02000000 A80E0000 +## ^ ^-- FDE(1) PC +## ^-- Number of FDEs +# CHECK-NEXT: 0010: 38000000 AA0E0000 50000000 +## ^-- FDE(2) PC +# CHECK-NEXT: ) +# CHECK-NEXT: } +## FDE(1) == 0x201000 - .eh_frame_hdr(0x200158) = 0xEA8 +## FDE(2) == 0x201000 - .eh_frame_hdr(0x200158) + 2(relocation addend) = 0xEAA + +## Check .eh_frame contains CIE and two FDEs remaining after ICF. +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 2 +# CHECK-NEXT: Name: .eh_frame +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x200178 +# CHECK-NEXT: Offset: 0x178 +# CHECK-NEXT: Size: 72 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 8 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: SectionData ( +# CHECK-NEXT: 0000: 14000000 00000000 017A5200 01781001 +# CHECK-NEXT: 0010: 1B0C0708 90010000 14000000 1C000000 +# CHECK-NEXT: 0020: 680E0000 01000000 00000000 00000000 +# CHECK-NEXT: 0030: 14000000 34000000 520E0000 01000000 +# CHECK-NEXT: 0040: 00000000 00000000 +# CHECK-NEXT: ) +# CHECK-NEXT: } + +# CHECK: Section { +# CHECK: Index: +# CHECK: Name: .text +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: SHF_EXECINSTR +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x201000 + +.section .text.f1, "ax" +.cfi_startproc +nop +.cfi_endproc +nop +.cfi_startproc +ret +.cfi_endproc + +.section .text.f2, "ax" +.cfi_startproc +nop +.cfi_endproc +nop +.cfi_startproc +ret +.cfi_endproc Index: test/ELF/eh-frame-hdr-icf.s =================================================================== --- test/ELF/eh-frame-hdr-icf.s +++ test/ELF/eh-frame-hdr-icf.s @@ -2,13 +2,15 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t # RUN: ld.lld %t -o %t2 --icf=all --eh-frame-hdr -# RUN: llvm-objdump -s %t2 | FileCheck %s +# RUN: llvm-objdump -s -section-headers %t2 | FileCheck %s + +## Check there is a only one FDE in .eh_frame_hdr and no any dummy data. +## Name Size +# CHECK: .eh_frame_hdr 00000014 # CHECK: Contents of section .eh_frame_hdr: -# CHECK-NEXT: 200158 011b033b 1c000000 01000000 a80e0000 +# CHECK-NEXT: 200158 011b033b 14000000 01000000 # ^ FDE count -# CHECK-NEXT: 200168 38000000 00000000 00000000 -# ^ FDE for f2 .globl _start, f1, f2 _start: