[lld] [ELF] Support DW_EH_PE_sdata8 encoding in .eh_frame_hdr (PR #179089)
Farid Zakaria via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 3 09:00:00 PST 2026
================
@@ -602,53 +565,31 @@ void EhFrameSection::writeTo(uint8_t *buf) {
if (!hdr || !hdr->getParent())
return;
- // Write the .eh_frame_hdr section, which contains a binary search table of
- // pointers to FDEs. This must be written after .eh_frame relocation since
- // the content depends on relocated initial_location fields in FDEs.
- using FdeData = EhFrameSection::FdeData;
- SmallVector<FdeData, 0> fdes;
- uint64_t va = hdr->getVA();
- for (CieRecord *rec : cieRecords) {
- uint8_t enc = getFdeEncoding(rec->cie);
- for (EhSectionPiece *fde : rec->fdes) {
- uint64_t pc = getFdePc(buf, fde->outputOff, enc);
- uint64_t fdeVA = getParent()->addr + fde->outputOff;
- if (!isInt<32>(pc - va)) {
- Err(ctx) << fde->sec << ": PC offset is too large: 0x"
- << Twine::utohexstr(pc - va);
- continue;
- }
- fdes.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});
- }
- }
-
- // 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.
- llvm::stable_sort(fdes, [](const FdeData &a, const FdeData &b) {
- return a.pcRel < b.pcRel;
- });
- fdes.erase(
- llvm::unique(fdes, [](auto &a, auto &b) { return a.pcRel == b.pcRel; }),
- fdes.end());
+ // Write the .eh_frame_hdr section using cached FDE data from updateAllocSize.
+ bool large = hdr->large;
+ int64_t ehFramePtr = getParent()->addr - hdr->getVA() - 4;
+ auto writeField = [&](uint8_t *buf, uint64_t val) {
+ large ? write64(ctx, buf, val) : write32(ctx, buf, val);
+ };
- // Write header.
uint8_t *hdrBuf = ctx.bufferStart + hdr->getParent()->offset + hdr->outSecOff;
- hdrBuf[0] = 1; // version
- hdrBuf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; // eh_frame_ptr_enc
- hdrBuf[2] = DW_EH_PE_udata4; // fde_count_enc
- hdrBuf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; // table_enc
- write32(ctx, hdrBuf + 4,
- getParent()->addr - hdr->getVA() - 4); // eh_frame_ptr
- write32(ctx, hdrBuf + 8, fdes.size()); // fde_count
- hdrBuf += 12;
-
- // Write binary search table. Each entry describes the starting PC and the FDE
- // address.
- for (FdeData &fde : fdes) {
- write32(ctx, hdrBuf, fde.pcRel);
- write32(ctx, hdrBuf + 4, fde.fdeVARel);
- hdrBuf += 8;
+ // version
+ hdrBuf[0] = 1;
+ // eh_frame_ptr_enc
+ hdrBuf[1] = DW_EH_PE_pcrel | (large ? DW_EH_PE_sdata8 : DW_EH_PE_sdata4);
+ // fde_count_enc
+ hdrBuf[2] = DW_EH_PE_udata4;
+ // table_enc
+ hdrBuf[3] = DW_EH_PE_datarel | (large ? DW_EH_PE_sdata8 : DW_EH_PE_sdata4);
+ hdrBuf += 4;
+ writeField(hdrBuf, ehFramePtr);
+ hdrBuf += large ? 8 : 4;
+ write32(ctx, hdrBuf, hdr->fdes.size());
----------------
fzakaria wrote:
How does LLVM handle these auto down-casting?
While I agree it's not likely here to exceed 4 billion entries.
Should there be a lint here or a guard?
(I see this pattern so it's probably for my edification)
https://github.com/llvm/llvm-project/pull/179089
More information about the llvm-commits
mailing list