Path: blob/main/contrib/llvm-project/lld/ELF/AArch64ErrataFix.cpp
34879 views
//===- AArch64ErrataFix.cpp -----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This file implements Section Patching for the purpose of working around8// the AArch64 Cortex-53 errata 843419 that affects r0p0, r0p1, r0p2 and r0p49// versions of the core.10//11// The general principle is that an erratum sequence of one or12// more instructions is detected in the instruction stream, one of the13// instructions in the sequence is replaced with a branch to a patch sequence14// of replacement instructions. At the end of the replacement sequence the15// patch branches back to the instruction stream.1617// This technique is only suitable for fixing an erratum when:18// - There is a set of necessary conditions required to trigger the erratum that19// can be detected at static link time.20// - There is a set of replacement instructions that can be used to remove at21// least one of the necessary conditions that trigger the erratum.22// - We can overwrite an instruction in the erratum sequence with a branch to23// the replacement sequence.24// - We can place the replacement sequence within range of the branch.25//===----------------------------------------------------------------------===//2627#include "AArch64ErrataFix.h"28#include "InputFiles.h"29#include "LinkerScript.h"30#include "OutputSections.h"31#include "Relocations.h"32#include "Symbols.h"33#include "SyntheticSections.h"34#include "Target.h"35#include "lld/Common/CommonLinkerContext.h"36#include "lld/Common/Strings.h"37#include "llvm/ADT/StringExtras.h"38#include "llvm/Support/Endian.h"39#include <algorithm>4041using namespace llvm;42using namespace llvm::ELF;43using namespace llvm::object;44using namespace llvm::support;45using namespace llvm::support::endian;46using namespace lld;47using namespace lld::elf;4849// Helper functions to identify instructions and conditions needed to trigger50// the Cortex-A53-843419 erratum.5152// ADRP53// | 1 | immlo (2) | 1 | 0 0 0 0 | immhi (19) | Rd (5) |54static bool isADRP(uint32_t instr) {55return (instr & 0x9f000000) == 0x90000000;56}5758// Load and store bit patterns from ARMv8-A.59// Instructions appear in order of appearance starting from table in60// C4.1.3 Loads and Stores.6162// All loads and stores have 1 (at bit position 27), (0 at bit position 25).63// | op0 x op1 (2) | 1 op2 0 op3 (2) | x | op4 (5) | xxxx | op5 (2) | x (10) |64static bool isLoadStoreClass(uint32_t instr) {65return (instr & 0x0a000000) == 0x08000000;66}6768// LDN/STN multiple no offset69// | 0 Q 00 | 1100 | 0 L 00 | 0000 | opcode (4) | size (2) | Rn (5) | Rt (5) |70// LDN/STN multiple post-indexed71// | 0 Q 00 | 1100 | 1 L 0 | Rm (5)| opcode (4) | size (2) | Rn (5) | Rt (5) |72// L == 0 for stores.7374// Utility routine to decode opcode field of LDN/STN multiple structure75// instructions to find the ST1 instructions.76// opcode == 0010 ST1 4 registers.77// opcode == 0110 ST1 3 registers.78// opcode == 0111 ST1 1 register.79// opcode == 1010 ST1 2 registers.80static bool isST1MultipleOpcode(uint32_t instr) {81return (instr & 0x0000f000) == 0x00002000 ||82(instr & 0x0000f000) == 0x00006000 ||83(instr & 0x0000f000) == 0x00007000 ||84(instr & 0x0000f000) == 0x0000a000;85}8687static bool isST1Multiple(uint32_t instr) {88return (instr & 0xbfff0000) == 0x0c000000 && isST1MultipleOpcode(instr);89}9091// Writes to Rn (writeback).92static bool isST1MultiplePost(uint32_t instr) {93return (instr & 0xbfe00000) == 0x0c800000 && isST1MultipleOpcode(instr);94}9596// LDN/STN single no offset97// | 0 Q 00 | 1101 | 0 L R 0 | 0000 | opc (3) S | size (2) | Rn (5) | Rt (5)|98// LDN/STN single post-indexed99// | 0 Q 00 | 1101 | 1 L R | Rm (5) | opc (3) S | size (2) | Rn (5) | Rt (5)|100// L == 0 for stores101102// Utility routine to decode opcode field of LDN/STN single structure103// instructions to find the ST1 instructions.104// R == 0 for ST1 and ST3, R == 1 for ST2 and ST4.105// opcode == 000 ST1 8-bit.106// opcode == 010 ST1 16-bit.107// opcode == 100 ST1 32 or 64-bit (Size determines which).108static bool isST1SingleOpcode(uint32_t instr) {109return (instr & 0x0040e000) == 0x00000000 ||110(instr & 0x0040e000) == 0x00004000 ||111(instr & 0x0040e000) == 0x00008000;112}113114static bool isST1Single(uint32_t instr) {115return (instr & 0xbfff0000) == 0x0d000000 && isST1SingleOpcode(instr);116}117118// Writes to Rn (writeback).119static bool isST1SinglePost(uint32_t instr) {120return (instr & 0xbfe00000) == 0x0d800000 && isST1SingleOpcode(instr);121}122123static bool isST1(uint32_t instr) {124return isST1Multiple(instr) || isST1MultiplePost(instr) ||125isST1Single(instr) || isST1SinglePost(instr);126}127128// Load/store exclusive129// | size (2) 00 | 1000 | o2 L o1 | Rs (5) | o0 | Rt2 (5) | Rn (5) | Rt (5) |130// L == 0 for Stores.131static bool isLoadStoreExclusive(uint32_t instr) {132return (instr & 0x3f000000) == 0x08000000;133}134135static bool isLoadExclusive(uint32_t instr) {136return (instr & 0x3f400000) == 0x08400000;137}138139// Load register literal140// | opc (2) 01 | 1 V 00 | imm19 | Rt (5) |141static bool isLoadLiteral(uint32_t instr) {142return (instr & 0x3b000000) == 0x18000000;143}144145// Load/store no-allocate pair146// (offset)147// | opc (2) 10 | 1 V 00 | 0 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |148// L == 0 for stores.149// Never writes to register150static bool isSTNP(uint32_t instr) {151return (instr & 0x3bc00000) == 0x28000000;152}153154// Load/store register pair155// (post-indexed)156// | opc (2) 10 | 1 V 00 | 1 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |157// L == 0 for stores, V == 0 for Scalar, V == 1 for Simd/FP158// Writes to Rn.159static bool isSTPPost(uint32_t instr) {160return (instr & 0x3bc00000) == 0x28800000;161}162163// (offset)164// | opc (2) 10 | 1 V 01 | 0 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |165static bool isSTPOffset(uint32_t instr) {166return (instr & 0x3bc00000) == 0x29000000;167}168169// (pre-index)170// | opc (2) 10 | 1 V 01 | 1 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |171// Writes to Rn.172static bool isSTPPre(uint32_t instr) {173return (instr & 0x3bc00000) == 0x29800000;174}175176static bool isSTP(uint32_t instr) {177return isSTPPost(instr) || isSTPOffset(instr) || isSTPPre(instr);178}179180// Load/store register (unscaled immediate)181// | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 00 | Rn (5) | Rt (5) |182// V == 0 for Scalar, V == 1 for Simd/FP.183static bool isLoadStoreUnscaled(uint32_t instr) {184return (instr & 0x3b000c00) == 0x38000000;185}186187// Load/store register (immediate post-indexed)188// | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 01 | Rn (5) | Rt (5) |189static bool isLoadStoreImmediatePost(uint32_t instr) {190return (instr & 0x3b200c00) == 0x38000400;191}192193// Load/store register (unprivileged)194// | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 10 | Rn (5) | Rt (5) |195static bool isLoadStoreUnpriv(uint32_t instr) {196return (instr & 0x3b200c00) == 0x38000800;197}198199// Load/store register (immediate pre-indexed)200// | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 11 | Rn (5) | Rt (5) |201static bool isLoadStoreImmediatePre(uint32_t instr) {202return (instr & 0x3b200c00) == 0x38000c00;203}204205// Load/store register (register offset)206// | size (2) 11 | 1 V 00 | opc (2) 1 | Rm (5) | option (3) S | 10 | Rn | Rt |207static bool isLoadStoreRegisterOff(uint32_t instr) {208return (instr & 0x3b200c00) == 0x38200800;209}210211// Load/store register (unsigned immediate)212// | size (2) 11 | 1 V 01 | opc (2) | imm12 | Rn (5) | Rt (5) |213static bool isLoadStoreRegisterUnsigned(uint32_t instr) {214return (instr & 0x3b000000) == 0x39000000;215}216217// Rt is always in bit position 0 - 4.218static uint32_t getRt(uint32_t instr) { return (instr & 0x1f); }219220// Rn is always in bit position 5 - 9.221static uint32_t getRn(uint32_t instr) { return (instr >> 5) & 0x1f; }222223// C4.1.2 Branches, Exception Generating and System instructions224// | op0 (3) 1 | 01 op1 (4) | x (22) |225// op0 == 010 101 op1 == 0xxx Conditional Branch.226// op0 == 110 101 op1 == 1xxx Unconditional Branch Register.227// op0 == x00 101 op1 == xxxx Unconditional Branch immediate.228// op0 == x01 101 op1 == 0xxx Compare and branch immediate.229// op0 == x01 101 op1 == 1xxx Test and branch immediate.230static bool isBranch(uint32_t instr) {231return ((instr & 0xfe000000) == 0xd6000000) || // Cond branch.232((instr & 0xfe000000) == 0x54000000) || // Uncond branch reg.233((instr & 0x7c000000) == 0x14000000) || // Uncond branch imm.234((instr & 0x7c000000) == 0x34000000); // Compare and test branch.235}236237static bool isV8SingleRegisterNonStructureLoadStore(uint32_t instr) {238return isLoadStoreUnscaled(instr) || isLoadStoreImmediatePost(instr) ||239isLoadStoreUnpriv(instr) || isLoadStoreImmediatePre(instr) ||240isLoadStoreRegisterOff(instr) || isLoadStoreRegisterUnsigned(instr);241}242243// Note that this function refers to v8.0 only and does not include the244// additional load and store instructions added for in later revisions of245// the architecture such as the Atomic memory operations introduced246// in v8.1.247static bool isV8NonStructureLoad(uint32_t instr) {248if (isLoadExclusive(instr))249return true;250if (isLoadLiteral(instr))251return true;252else if (isV8SingleRegisterNonStructureLoadStore(instr)) {253// For Load and Store single register, Loads are derived from a254// combination of the Size, V and Opc fields.255uint32_t size = (instr >> 30) & 0xff;256uint32_t v = (instr >> 26) & 0x1;257uint32_t opc = (instr >> 22) & 0x3;258// For the load and store instructions that we are decoding.259// Opc == 0 are all stores.260// Opc == 1 with a couple of exceptions are loads. The exceptions are:261// Size == 00 (0), V == 1, Opc == 10 (2) which is a store and262// Size == 11 (3), V == 0, Opc == 10 (2) which is a prefetch.263return opc != 0 && !(size == 0 && v == 1 && opc == 2) &&264!(size == 3 && v == 0 && opc == 2);265}266return false;267}268269// The following decode instructions are only complete up to the instructions270// needed for errata 843419.271272// Instruction with writeback updates the index register after the load/store.273static bool hasWriteback(uint32_t instr) {274return isLoadStoreImmediatePre(instr) || isLoadStoreImmediatePost(instr) ||275isSTPPre(instr) || isSTPPost(instr) || isST1SinglePost(instr) ||276isST1MultiplePost(instr);277}278279// For the load and store class of instructions, a load can write to the280// destination register, a load and a store can write to the base register when281// the instruction has writeback.282static bool doesLoadStoreWriteToReg(uint32_t instr, uint32_t reg) {283return (isV8NonStructureLoad(instr) && getRt(instr) == reg) ||284(hasWriteback(instr) && getRn(instr) == reg);285}286287// Scanner for Cortex-A53 errata 843419288// Full details are available in the Cortex A53 MPCore revision 0 Software289// Developers Errata Notice (ARM-EPM-048406).290//291// The instruction sequence that triggers the erratum is common in compiled292// AArch64 code, however it is sensitive to the offset of the sequence within293// a 4k page. This means that by scanning and fixing the patch after we have294// assigned addresses we only need to disassemble and fix instances of the295// sequence in the range of affected offsets.296//297// In summary the erratum conditions are a series of 4 instructions:298// 1.) An ADRP instruction that writes to register Rn with low 12 bits of299// address of instruction either 0xff8 or 0xffc.300// 2.) A load or store instruction that can be:301// - A single register load or store, of either integer or vector registers.302// - An STP or STNP, of either integer or vector registers.303// - An Advanced SIMD ST1 store instruction.304// - Must not write to Rn, but may optionally read from it.305// 3.) An optional instruction that is not a branch and does not write to Rn.306// 4.) A load or store from the Load/store register (unsigned immediate) class307// that uses Rn as the base address register.308//309// Note that we do not attempt to scan for Sequence 2 as described in the310// Software Developers Errata Notice as this has been assessed to be extremely311// unlikely to occur in compiled code. This matches gold and ld.bfd behavior.312313// Return true if the Instruction sequence Adrp, Instr2, and Instr4 match314// the erratum sequence. The Adrp, Instr2 and Instr4 correspond to 1.), 2.),315// and 4.) in the Scanner for Cortex-A53 errata comment above.316static bool is843419ErratumSequence(uint32_t instr1, uint32_t instr2,317uint32_t instr4) {318if (!isADRP(instr1))319return false;320321uint32_t rn = getRt(instr1);322return isLoadStoreClass(instr2) &&323(isLoadStoreExclusive(instr2) || isLoadLiteral(instr2) ||324isV8SingleRegisterNonStructureLoadStore(instr2) || isSTP(instr2) ||325isSTNP(instr2) || isST1(instr2)) &&326!doesLoadStoreWriteToReg(instr2, rn) &&327isLoadStoreRegisterUnsigned(instr4) && getRn(instr4) == rn;328}329330// Scan the instruction sequence starting at Offset Off from the base of331// InputSection isec. We update Off in this function rather than in the caller332// as we can skip ahead much further into the section when we know how many333// instructions we've scanned.334// Return the offset of the load or store instruction in isec that we want to335// patch or 0 if no patch required.336static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,337uint64_t limit) {338uint64_t isecAddr = isec->getVA(0);339340// Advance Off so that (isecAddr + Off) modulo 0x1000 is at least 0xff8.341uint64_t initialPageOff = (isecAddr + off) & 0xfff;342if (initialPageOff < 0xff8)343off += 0xff8 - initialPageOff;344345bool optionalAllowed = limit - off > 12;346if (off >= limit || limit - off < 12) {347// Need at least 3 4-byte sized instructions to trigger erratum.348off = limit;349return 0;350}351352uint64_t patchOff = 0;353const uint8_t *buf = isec->content().begin();354const ulittle32_t *instBuf = reinterpret_cast<const ulittle32_t *>(buf + off);355uint32_t instr1 = *instBuf++;356uint32_t instr2 = *instBuf++;357uint32_t instr3 = *instBuf++;358if (is843419ErratumSequence(instr1, instr2, instr3)) {359patchOff = off + 8;360} else if (optionalAllowed && !isBranch(instr3)) {361uint32_t instr4 = *instBuf++;362if (is843419ErratumSequence(instr1, instr2, instr4))363patchOff = off + 12;364}365if (((isecAddr + off) & 0xfff) == 0xff8)366off += 4;367else368off += 0xffc;369return patchOff;370}371372class elf::Patch843419Section final : public SyntheticSection {373public:374Patch843419Section(InputSection *p, uint64_t off);375376void writeTo(uint8_t *buf) override;377378size_t getSize() const override { return 8; }379380uint64_t getLDSTAddr() const;381382static bool classof(const SectionBase *d) {383return d->kind() == InputSectionBase::Synthetic && d->name == ".text.patch";384}385386// The Section we are patching.387const InputSection *patchee;388// The offset of the instruction in the patchee section we are patching.389uint64_t patcheeOffset;390// A label for the start of the Patch that we can use as a relocation target.391Symbol *patchSym;392};393394Patch843419Section::Patch843419Section(InputSection *p, uint64_t off)395: SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 4,396".text.patch"),397patchee(p), patcheeOffset(off) {398this->parent = p->getParent();399patchSym = addSyntheticLocal(400saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())), STT_FUNC,4010, getSize(), *this);402addSyntheticLocal(saver().save("$x"), STT_NOTYPE, 0, 0, *this);403}404405uint64_t Patch843419Section::getLDSTAddr() const {406return patchee->getVA(patcheeOffset);407}408409void Patch843419Section::writeTo(uint8_t *buf) {410// Copy the instruction that we will be replacing with a branch in the411// patchee Section.412write32le(buf, read32le(patchee->content().begin() + patcheeOffset));413414// Apply any relocation transferred from the original patchee section.415target->relocateAlloc(*this, buf);416417// Return address is the next instruction after the one we have just copied.418uint64_t s = getLDSTAddr() + 4;419uint64_t p = patchSym->getVA() + 4;420target->relocateNoSym(buf + 4, R_AARCH64_JUMP26, s - p);421}422423void AArch64Err843419Patcher::init() {424// The AArch64 ABI permits data in executable sections. We must avoid scanning425// this data as if it were instructions to avoid false matches. We use the426// mapping symbols in the InputObjects to identify this data, caching the427// results in sectionMap so we don't have to recalculate it each pass.428429// The ABI Section 4.5.4 Mapping symbols; defines local symbols that describe430// half open intervals [Symbol Value, Next Symbol Value) of code and data431// within sections. If there is no next symbol then the half open interval is432// [Symbol Value, End of section). The type, code or data, is determined by433// the mapping symbol name, $x for code, $d for data.434auto isCodeMapSymbol = [](const Symbol *b) {435return b->getName() == "$x" || b->getName().starts_with("$x.");436};437auto isDataMapSymbol = [](const Symbol *b) {438return b->getName() == "$d" || b->getName().starts_with("$d.");439};440441// Collect mapping symbols for every executable InputSection.442for (ELFFileBase *file : ctx.objectFiles) {443for (Symbol *b : file->getLocalSymbols()) {444auto *def = dyn_cast<Defined>(b);445if (!def)446continue;447if (!isCodeMapSymbol(def) && !isDataMapSymbol(def))448continue;449if (auto *sec = dyn_cast_or_null<InputSection>(def->section))450if (sec->flags & SHF_EXECINSTR)451sectionMap[sec].push_back(def);452}453}454// For each InputSection make sure the mapping symbols are in sorted in455// ascending order and free from consecutive runs of mapping symbols with456// the same type. For example we must remove the redundant $d.1 from $x.0457// $d.0 $d.1 $x.1.458for (auto &kv : sectionMap) {459std::vector<const Defined *> &mapSyms = kv.second;460llvm::stable_sort(mapSyms, [](const Defined *a, const Defined *b) {461return a->value < b->value;462});463mapSyms.erase(464std::unique(mapSyms.begin(), mapSyms.end(),465[=](const Defined *a, const Defined *b) {466return isCodeMapSymbol(a) == isCodeMapSymbol(b);467}),468mapSyms.end());469// Always start with a Code Mapping Symbol.470if (!mapSyms.empty() && !isCodeMapSymbol(mapSyms.front()))471mapSyms.erase(mapSyms.begin());472}473initialized = true;474}475476// Insert the PatchSections we have created back into the477// InputSectionDescription. As inserting patches alters the addresses of478// InputSections that follow them, we try and place the patches after all the479// executable sections, although we may need to insert them earlier if the480// InputSectionDescription is larger than the maximum branch range.481void AArch64Err843419Patcher::insertPatches(482InputSectionDescription &isd, std::vector<Patch843419Section *> &patches) {483uint64_t isecLimit;484uint64_t prevIsecLimit = isd.sections.front()->outSecOff;485uint64_t patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();486uint64_t outSecAddr = isd.sections.front()->getParent()->addr;487488// Set the outSecOff of patches to the place where we want to insert them.489// We use a similar strategy to Thunk placement. Place patches roughly490// every multiple of maximum branch range.491auto patchIt = patches.begin();492auto patchEnd = patches.end();493for (const InputSection *isec : isd.sections) {494isecLimit = isec->outSecOff + isec->getSize();495if (isecLimit > patchUpperBound) {496while (patchIt != patchEnd) {497if ((*patchIt)->getLDSTAddr() - outSecAddr >= prevIsecLimit)498break;499(*patchIt)->outSecOff = prevIsecLimit;500++patchIt;501}502patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();503}504prevIsecLimit = isecLimit;505}506for (; patchIt != patchEnd; ++patchIt) {507(*patchIt)->outSecOff = isecLimit;508}509510// Merge all patch sections. We use the outSecOff assigned above to511// determine the insertion point. This is ok as we only merge into an512// InputSectionDescription once per pass, and at the end of the pass513// assignAddresses() will recalculate all the outSecOff values.514SmallVector<InputSection *, 0> tmp;515tmp.reserve(isd.sections.size() + patches.size());516auto mergeCmp = [](const InputSection *a, const InputSection *b) {517if (a->outSecOff != b->outSecOff)518return a->outSecOff < b->outSecOff;519return isa<Patch843419Section>(a) && !isa<Patch843419Section>(b);520};521std::merge(isd.sections.begin(), isd.sections.end(), patches.begin(),522patches.end(), std::back_inserter(tmp), mergeCmp);523isd.sections = std::move(tmp);524}525526// Given an erratum sequence that starts at address adrpAddr, with an527// instruction that we need to patch at patcheeOffset from the start of528// InputSection isec, create a Patch843419 Section and add it to the529// Patches that we need to insert.530static void implementPatch(uint64_t adrpAddr, uint64_t patcheeOffset,531InputSection *isec,532std::vector<Patch843419Section *> &patches) {533// There may be a relocation at the same offset that we are patching. There534// are four cases that we need to consider.535// Case 1: R_AARCH64_JUMP26 branch relocation. We have already patched this536// instance of the erratum on a previous patch and altered the relocation. We537// have nothing more to do.538// Case 2: A TLS Relaxation R_RELAX_TLS_IE_TO_LE. In this case the ADRP that539// we read will be transformed into a MOVZ later so we actually don't match540// the sequence and have nothing more to do.541// Case 3: A load/store register (unsigned immediate) class relocation. There542// are two of these R_AARCH_LD64_ABS_LO12_NC and R_AARCH_LD64_GOT_LO12_NC and543// they are both absolute. We need to add the same relocation to the patch,544// and replace the relocation with a R_AARCH_JUMP26 branch relocation.545// Case 4: No relocation. We must create a new R_AARCH64_JUMP26 branch546// relocation at the offset.547auto relIt = llvm::find_if(isec->relocs(), [=](const Relocation &r) {548return r.offset == patcheeOffset;549});550if (relIt != isec->relocs().end() &&551(relIt->type == R_AARCH64_JUMP26 || relIt->expr == R_RELAX_TLS_IE_TO_LE))552return;553554log("detected cortex-a53-843419 erratum sequence starting at " +555utohexstr(adrpAddr) + " in unpatched output.");556557auto *ps = make<Patch843419Section>(isec, patcheeOffset);558patches.push_back(ps);559560auto makeRelToPatch = [](uint64_t offset, Symbol *patchSym) {561return Relocation{R_PC, R_AARCH64_JUMP26, offset, 0, patchSym};562};563564if (relIt != isec->relocs().end()) {565ps->addReloc({relIt->expr, relIt->type, 0, relIt->addend, relIt->sym});566*relIt = makeRelToPatch(patcheeOffset, ps->patchSym);567} else568isec->addReloc(makeRelToPatch(patcheeOffset, ps->patchSym));569}570571// Scan all the instructions in InputSectionDescription, for each instance of572// the erratum sequence create a Patch843419Section. We return the list of573// Patch843419Sections that need to be applied to the InputSectionDescription.574std::vector<Patch843419Section *>575AArch64Err843419Patcher::patchInputSectionDescription(576InputSectionDescription &isd) {577std::vector<Patch843419Section *> patches;578for (InputSection *isec : isd.sections) {579// LLD doesn't use the erratum sequence in SyntheticSections.580if (isa<SyntheticSection>(isec))581continue;582// Use sectionMap to make sure we only scan code and not inline data.583// We have already sorted MapSyms in ascending order and removed consecutive584// mapping symbols of the same type. Our range of executable instructions to585// scan is therefore [codeSym->value, dataSym->value) or [codeSym->value,586// section size).587std::vector<const Defined *> &mapSyms = sectionMap[isec];588589auto codeSym = mapSyms.begin();590while (codeSym != mapSyms.end()) {591auto dataSym = std::next(codeSym);592uint64_t off = (*codeSym)->value;593uint64_t limit = (dataSym == mapSyms.end()) ? isec->content().size()594: (*dataSym)->value;595596while (off < limit) {597uint64_t startAddr = isec->getVA(off);598if (uint64_t patcheeOffset =599scanCortexA53Errata843419(isec, off, limit))600implementPatch(startAddr, patcheeOffset, isec, patches);601}602if (dataSym == mapSyms.end())603break;604codeSym = std::next(dataSym);605}606}607return patches;608}609610// For each InputSectionDescription make one pass over the executable sections611// looking for the erratum sequence; creating a synthetic Patch843419Section612// for each instance found. We insert these synthetic patch sections after the613// executable code in each InputSectionDescription.614//615// PreConditions:616// The Output and Input Sections have had their final addresses assigned.617//618// PostConditions:619// Returns true if at least one patch was added. The addresses of the620// Output and Input Sections may have been changed.621// Returns false if no patches were required and no changes were made.622bool AArch64Err843419Patcher::createFixes() {623if (!initialized)624init();625626bool addressesChanged = false;627for (OutputSection *os : outputSections) {628if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))629continue;630for (SectionCommand *cmd : os->commands)631if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {632std::vector<Patch843419Section *> patches =633patchInputSectionDescription(*isd);634if (!patches.empty()) {635insertPatches(*isd, patches);636addressesChanged = true;637}638}639}640return addressesChanged;641}642643644