@@ -370,7 +370,7 @@ static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
370370
371371class elf ::Patch843419Section final : public SyntheticSection {
372372public:
373- Patch843419Section (Ctx &, InputSection *p, uint64_t off);
373+ Patch843419Section (Ctx &, InputSection *p, uint64_t off, Symbol* patcheeCodeSym );
374374
375375 void writeTo (uint8_t *buf) override ;
376376
@@ -388,11 +388,9 @@ class elf::Patch843419Section final : public SyntheticSection {
388388 uint64_t patcheeOffset;
389389 // A label for the start of the Patch that we can use as a relocation target.
390390 Symbol *patchSym;
391- // A label for the return location.
392- Symbol *retSym;
393391};
394392
395- Patch843419Section::Patch843419Section (Ctx &ctx, InputSection *p, uint64_t off)
393+ Patch843419Section::Patch843419Section (Ctx &ctx, InputSection *p, uint64_t off, Symbol* patcheeCodeSym )
396394 : SyntheticSection(ctx, " .text.patch" , SHT_PROGBITS,
397395 SHF_ALLOC | SHF_EXECINSTR, 4 ),
398396 patchee(p), patcheeOffset(off) {
@@ -401,12 +399,8 @@ Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off)
401399 ctx, ctx.saver .save (" __CortexA53843419_" + utohexstr (getLDSTAddr ())),
402400 STT_FUNC, 0 , getSize (), *this );
403401 addSyntheticLocal (ctx, ctx.saver .save (" $x" ), STT_NOTYPE, 0 , 0 , *this );
404- retSym = addSyntheticLocal (
405- ctx, ctx.saver .save (" __CortexA53843419_" + utohexstr (getLDSTAddr ()) + " _ret" ),
406- STT_FUNC, off + 4 , 4 , *p);
407-
408- // Relocation must be created as soon as possible, so it'll be picked up.
409- addReloc ({R_PC, R_AARCH64_JUMP26, 4 , 0 , retSym});
402+ int64_t retToPatcheeSymOffset = (getLDSTAddr () - p->getVA (dyn_cast<Defined>(patcheeCodeSym)->value )) + 4 ;
403+ addReloc ({R_PC, R_AARCH64_JUMP26, 4 , retToPatcheeSymOffset, patcheeCodeSym});
410404}
411405
412406uint64_t Patch843419Section::getLDSTAddr () const {
@@ -418,10 +412,6 @@ void Patch843419Section::writeTo(uint8_t *buf) {
418412 // patchee Section.
419413 write32le (buf, read32le (patchee->content ().begin () + patcheeOffset));
420414
421- // Note: The jump back was configured in this classe's constructor, and
422- // will be filled by the relocation. Adding the relocation here would be
423- // too late.
424-
425415 // Apply relocations
426416 ctx.target ->relocateAlloc (*this , buf);
427417}
@@ -462,7 +452,7 @@ void AArch64Err843419Patcher::init() {
462452 // the same type. For example we must remove the redundant $d.1 from $x.0
463453 // $d.0 $d.1 $x.1.
464454 for (auto &kv : sectionMap) {
465- std::vector<const Defined *> &mapSyms = kv.second ;
455+ std::vector<Defined *> &mapSyms = kv.second ;
466456 llvm::stable_sort (mapSyms, [](const Defined *a, const Defined *b) {
467457 return a->value < b->value ;
468458 });
@@ -536,7 +526,8 @@ void AArch64Err843419Patcher::insertPatches(
536526// Patches that we need to insert.
537527static void implementPatch (Ctx &ctx, uint64_t adrpAddr, uint64_t patcheeOffset,
538528 InputSection *isec,
539- std::vector<Patch843419Section *> &patches) {
529+ std::vector<Patch843419Section *> &patches,
530+ Symbol* patcheeCodeSym) {
540531 // There may be a relocation at the same offset that we are patching. There
541532 // are four cases that we need to consider.
542533 // Case 1: R_AARCH64_JUMP26 branch relocation. We have already patched this
@@ -561,7 +552,7 @@ static void implementPatch(Ctx &ctx, uint64_t adrpAddr, uint64_t patcheeOffset,
561552 Log (ctx) << " detected cortex-a53-843419 erratum sequence starting at " <<
562553 utohexstr (adrpAddr) << " in unpatched output." ;
563554
564- auto *ps = make<Patch843419Section>(ctx, isec, patcheeOffset);
555+ auto *ps = make<Patch843419Section>(ctx, isec, patcheeOffset, patcheeCodeSym );
565556 patches.push_back (ps);
566557
567558 auto makeRelToPatch = [](uint64_t offset, Symbol *patchSym) {
@@ -591,7 +582,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
591582 // mapping symbols of the same type. Our range of executable instructions to
592583 // scan is therefore [codeSym->value, dataSym->value) or [codeSym->value,
593584 // section size).
594- std::vector<const Defined *> &mapSyms = sectionMap[isec];
585+ std::vector<Defined *> &mapSyms = sectionMap[isec];
595586
596587 auto codeSym = mapSyms.begin ();
597588 while (codeSym != mapSyms.end ()) {
@@ -604,7 +595,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
604595 uint64_t startAddr = isec->getVA (off);
605596 if (uint64_t patcheeOffset =
606597 scanCortexA53Errata843419 (isec, off, limit))
607- implementPatch (ctx, startAddr, patcheeOffset, isec, patches);
598+ implementPatch (ctx, startAddr, patcheeOffset, isec, patches, dyn_cast<Symbol>(*codeSym) );
608599 }
609600 if (dataSym == mapSyms.end ())
610601 break ;
0 commit comments