diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -672,13 +672,25 @@ if (isec->shouldOmitFromOutput()) continue; + bool skipNext = false; for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) { lld::macho::Reloc &r = *it; + + // Canonicalize the referent so that later accesses in Writer won't + // have to worry about it. Perhaps we should do this for Defined::isec + // too... + if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) + r.referent = referentIsec->canonical(); + + if (skipNext) { + skipNext = false; + continue; + } if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { // Skip over the following UNSIGNED relocation -- it's just there as the // minuend, and doesn't have the usual UNSIGNED semantics. We don't want // to emit rebase opcodes for it. - it++; + skipNext = true; continue; } if (auto *sym = r.referent.dyn_cast<Symbol *>()) { @@ -688,11 +700,6 @@ if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r)) prepareSymbolRelocation(sym, isec, r); } else { - // Canonicalize the referent so that later accesses in Writer won't - // have to worry about it. Perhaps we should do this for Defined::isec - // too... - auto *referentIsec = r.referent.get<InputSection *>(); - r.referent = referentIsec->canonical(); if (!r.pcrel) { if (config->emitChainedFixups) in.chainedFixups->addRebase(isec, r.offset);