185868e8aSDimitry Andric //===- ARMErrataFix.cpp ---------------------------------------------------===// 285868e8aSDimitry Andric // 385868e8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 485868e8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 585868e8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 685868e8aSDimitry Andric // 785868e8aSDimitry Andric //===----------------------------------------------------------------------===// 885868e8aSDimitry Andric // This file implements Section Patching for the purpose of working around the 985868e8aSDimitry Andric // Cortex-a8 erratum 657417 "A 32bit branch instruction that spans 2 4K regions 1085868e8aSDimitry Andric // can result in an incorrect instruction fetch or processor deadlock." The 1185868e8aSDimitry Andric // erratum affects all but r1p7, r2p5, r2p6, r3p1 and r3p2 revisions of the 1285868e8aSDimitry Andric // Cortex-A8. A high level description of the patching technique is given in 1385868e8aSDimitry Andric // the opening comment of AArch64ErrataFix.cpp. 1485868e8aSDimitry Andric //===----------------------------------------------------------------------===// 1585868e8aSDimitry Andric 1685868e8aSDimitry Andric #include "ARMErrataFix.h" 1785868e8aSDimitry Andric 1885868e8aSDimitry Andric #include "Config.h" 1985868e8aSDimitry Andric #include "LinkerScript.h" 2085868e8aSDimitry Andric #include "OutputSections.h" 2185868e8aSDimitry Andric #include "Relocations.h" 2285868e8aSDimitry Andric #include "Symbols.h" 2385868e8aSDimitry Andric #include "SyntheticSections.h" 2485868e8aSDimitry Andric #include "Target.h" 2585868e8aSDimitry Andric #include "lld/Common/Memory.h" 2685868e8aSDimitry Andric #include "lld/Common/Strings.h" 2785868e8aSDimitry Andric #include "llvm/Support/Endian.h" 2885868e8aSDimitry Andric #include "llvm/Support/raw_ostream.h" 2985868e8aSDimitry Andric #include <algorithm> 3085868e8aSDimitry Andric 3185868e8aSDimitry Andric using namespace llvm; 3285868e8aSDimitry Andric using namespace llvm::ELF; 3385868e8aSDimitry Andric using namespace llvm::object; 3485868e8aSDimitry Andric using namespace llvm::support; 3585868e8aSDimitry Andric using namespace llvm::support::endian; 36*5ffd83dbSDimitry Andric using namespace lld; 37*5ffd83dbSDimitry Andric using namespace lld::elf; 3885868e8aSDimitry Andric 3985868e8aSDimitry Andric // The documented title for Erratum 657417 is: 4085868e8aSDimitry Andric // "A 32bit branch instruction that spans two 4K regions can result in an 4185868e8aSDimitry Andric // incorrect instruction fetch or processor deadlock". Graphically using a 4285868e8aSDimitry Andric // 32-bit B.w instruction encoded as a pair of halfwords 0xf7fe 0xbfff 4385868e8aSDimitry Andric // xxxxxx000 // Memory region 1 start 4485868e8aSDimitry Andric // target: 4585868e8aSDimitry Andric // ... 4685868e8aSDimitry Andric // xxxxxxffe f7fe // First halfword of branch to target: 4785868e8aSDimitry Andric // xxxxxx000 // Memory region 2 start 4885868e8aSDimitry Andric // xxxxxx002 bfff // Second halfword of branch to target: 4985868e8aSDimitry Andric // 5085868e8aSDimitry Andric // The specific trigger conditions that can be detected at link time are: 5185868e8aSDimitry Andric // - There is a 32-bit Thumb-2 branch instruction with an address of the form 5285868e8aSDimitry Andric // xxxxxxFFE. The first 2 bytes of the instruction are in 4KiB region 1, the 5385868e8aSDimitry Andric // second 2 bytes are in region 2. 5485868e8aSDimitry Andric // - The branch instruction is one of BLX, BL, B.w BCC.w 5585868e8aSDimitry Andric // - The instruction preceding the branch is a 32-bit non-branch instruction. 5685868e8aSDimitry Andric // - The target of the branch is in region 1. 5785868e8aSDimitry Andric // 5885868e8aSDimitry Andric // The linker mitigation for the fix is to redirect any branch that meets the 5985868e8aSDimitry Andric // erratum conditions to a patch section containing a branch to the target. 6085868e8aSDimitry Andric // 6185868e8aSDimitry Andric // As adding patch sections may move branches onto region boundaries the patch 6285868e8aSDimitry Andric // must iterate until no more patches are added. 6385868e8aSDimitry Andric // 6485868e8aSDimitry Andric // Example, before: 6585868e8aSDimitry Andric // 00000FFA func: NOP.w // 32-bit Thumb function 6685868e8aSDimitry Andric // 00000FFE B.W func // 32-bit branch spanning 2 regions, dest in 1st. 6785868e8aSDimitry Andric // Example, after: 6885868e8aSDimitry Andric // 00000FFA func: NOP.w // 32-bit Thumb function 6985868e8aSDimitry Andric // 00000FFE B.w __CortexA8657417_00000FFE 7085868e8aSDimitry Andric // 00001002 2 - bytes padding 7185868e8aSDimitry Andric // 00001004 __CortexA8657417_00000FFE: B.w func 7285868e8aSDimitry Andric 73*5ffd83dbSDimitry Andric class elf::Patch657417Section : public SyntheticSection { 7485868e8aSDimitry Andric public: 7585868e8aSDimitry Andric Patch657417Section(InputSection *p, uint64_t off, uint32_t instr, bool isARM); 7685868e8aSDimitry Andric 7785868e8aSDimitry Andric void writeTo(uint8_t *buf) override; 7885868e8aSDimitry Andric 7985868e8aSDimitry Andric size_t getSize() const override { return 4; } 8085868e8aSDimitry Andric 8185868e8aSDimitry Andric // Get the virtual address of the branch instruction at patcheeOffset. 8285868e8aSDimitry Andric uint64_t getBranchAddr() const; 8385868e8aSDimitry Andric 84480093f4SDimitry Andric static bool classof(const SectionBase *d) { 85480093f4SDimitry Andric return d->kind() == InputSectionBase::Synthetic && d->name ==".text.patch"; 86480093f4SDimitry Andric } 87480093f4SDimitry Andric 8885868e8aSDimitry Andric // The Section we are patching. 8985868e8aSDimitry Andric const InputSection *patchee; 9085868e8aSDimitry Andric // The offset of the instruction in the Patchee section we are patching. 9185868e8aSDimitry Andric uint64_t patcheeOffset; 9285868e8aSDimitry Andric // A label for the start of the Patch that we can use as a relocation target. 9385868e8aSDimitry Andric Symbol *patchSym; 9485868e8aSDimitry Andric // A decoding of the branch instruction at patcheeOffset. 9585868e8aSDimitry Andric uint32_t instr; 9685868e8aSDimitry Andric // True If the patch is to be written in ARM state, otherwise the patch will 9785868e8aSDimitry Andric // be written in Thumb state. 9885868e8aSDimitry Andric bool isARM; 9985868e8aSDimitry Andric }; 10085868e8aSDimitry Andric 10185868e8aSDimitry Andric // Return true if the half-word, when taken as the first of a pair of halfwords 10285868e8aSDimitry Andric // is the first half of a 32-bit instruction. 103480093f4SDimitry Andric // Reference from ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition 10485868e8aSDimitry Andric // section A6.3: 32-bit Thumb instruction encoding 10585868e8aSDimitry Andric // | HW1 | HW2 | 10685868e8aSDimitry Andric // | 1 1 1 | op1 (2) | op2 (7) | x (4) |op| x (15) | 10785868e8aSDimitry Andric // With op1 == 0b00, a 16-bit instruction is encoded. 10885868e8aSDimitry Andric // 10985868e8aSDimitry Andric // We test only the first halfword, looking for op != 0b00. 11085868e8aSDimitry Andric static bool is32bitInstruction(uint16_t hw) { 11185868e8aSDimitry Andric return (hw & 0xe000) == 0xe000 && (hw & 0x1800) != 0x0000; 11285868e8aSDimitry Andric } 11385868e8aSDimitry Andric 114480093f4SDimitry Andric // Reference from ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition 11585868e8aSDimitry Andric // section A6.3.4 Branches and miscellaneous control. 11685868e8aSDimitry Andric // | HW1 | HW2 | 11785868e8aSDimitry Andric // | 1 1 1 | 1 0 | op (7) | x (4) | 1 | op1 (3) | op2 (4) | imm8 (8) | 11885868e8aSDimitry Andric // op1 == 0x0 op != x111xxx | Conditional branch (Bcc.W) 11985868e8aSDimitry Andric // op1 == 0x1 | Branch (B.W) 12085868e8aSDimitry Andric // op1 == 1x0 | Branch with Link and Exchange (BLX.w) 12185868e8aSDimitry Andric // op1 == 1x1 | Branch with Link (BL.W) 12285868e8aSDimitry Andric 12385868e8aSDimitry Andric static bool isBcc(uint32_t instr) { 12485868e8aSDimitry Andric return (instr & 0xf800d000) == 0xf0008000 && 12585868e8aSDimitry Andric (instr & 0x03800000) != 0x03800000; 12685868e8aSDimitry Andric } 12785868e8aSDimitry Andric 12885868e8aSDimitry Andric static bool isB(uint32_t instr) { return (instr & 0xf800d000) == 0xf0009000; } 12985868e8aSDimitry Andric 13085868e8aSDimitry Andric static bool isBLX(uint32_t instr) { return (instr & 0xf800d000) == 0xf000c000; } 13185868e8aSDimitry Andric 13285868e8aSDimitry Andric static bool isBL(uint32_t instr) { return (instr & 0xf800d000) == 0xf000d000; } 13385868e8aSDimitry Andric 13485868e8aSDimitry Andric static bool is32bitBranch(uint32_t instr) { 13585868e8aSDimitry Andric return isBcc(instr) || isB(instr) || isBL(instr) || isBLX(instr); 13685868e8aSDimitry Andric } 13785868e8aSDimitry Andric 13885868e8aSDimitry Andric Patch657417Section::Patch657417Section(InputSection *p, uint64_t off, 13985868e8aSDimitry Andric uint32_t instr, bool isARM) 14085868e8aSDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 4, 14185868e8aSDimitry Andric ".text.patch"), 14285868e8aSDimitry Andric patchee(p), patcheeOffset(off), instr(instr), isARM(isARM) { 14385868e8aSDimitry Andric parent = p->getParent(); 14485868e8aSDimitry Andric patchSym = addSyntheticLocal( 14585868e8aSDimitry Andric saver.save("__CortexA8657417_" + utohexstr(getBranchAddr())), STT_FUNC, 14685868e8aSDimitry Andric isARM ? 0 : 1, getSize(), *this); 14785868e8aSDimitry Andric addSyntheticLocal(saver.save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0, *this); 14885868e8aSDimitry Andric } 14985868e8aSDimitry Andric 15085868e8aSDimitry Andric uint64_t Patch657417Section::getBranchAddr() const { 15185868e8aSDimitry Andric return patchee->getVA(patcheeOffset); 15285868e8aSDimitry Andric } 15385868e8aSDimitry Andric 15485868e8aSDimitry Andric // Given a branch instruction instr at sourceAddr work out its destination 15585868e8aSDimitry Andric // address. This is only used when the branch instruction has no relocation. 15685868e8aSDimitry Andric static uint64_t getThumbDestAddr(uint64_t sourceAddr, uint32_t instr) { 15785868e8aSDimitry Andric uint8_t buf[4]; 15885868e8aSDimitry Andric write16le(buf, instr >> 16); 15985868e8aSDimitry Andric write16le(buf + 2, instr & 0x0000ffff); 16085868e8aSDimitry Andric int64_t offset; 16185868e8aSDimitry Andric if (isBcc(instr)) 16285868e8aSDimitry Andric offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP19); 16385868e8aSDimitry Andric else if (isB(instr)) 16485868e8aSDimitry Andric offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP24); 16585868e8aSDimitry Andric else 16685868e8aSDimitry Andric offset = target->getImplicitAddend(buf, R_ARM_THM_CALL); 16785868e8aSDimitry Andric return sourceAddr + offset + 4; 16885868e8aSDimitry Andric } 16985868e8aSDimitry Andric 17085868e8aSDimitry Andric void Patch657417Section::writeTo(uint8_t *buf) { 17185868e8aSDimitry Andric // The base instruction of the patch is always a 32-bit unconditional branch. 17285868e8aSDimitry Andric if (isARM) 17385868e8aSDimitry Andric write32le(buf, 0xea000000); 17485868e8aSDimitry Andric else 17585868e8aSDimitry Andric write32le(buf, 0x9000f000); 17685868e8aSDimitry Andric // If we have a relocation then apply it. For a SyntheticSection buf already 17785868e8aSDimitry Andric // has outSecOff added, but relocateAlloc also adds outSecOff so we need to 17885868e8aSDimitry Andric // subtract to avoid double counting. 17985868e8aSDimitry Andric if (!relocations.empty()) { 18085868e8aSDimitry Andric relocateAlloc(buf - outSecOff, buf - outSecOff + getSize()); 18185868e8aSDimitry Andric return; 18285868e8aSDimitry Andric } 18385868e8aSDimitry Andric 18485868e8aSDimitry Andric // If we don't have a relocation then we must calculate and write the offset 18585868e8aSDimitry Andric // ourselves. 18685868e8aSDimitry Andric // Get the destination offset from the addend in the branch instruction. 18785868e8aSDimitry Andric // We cannot use the instruction in the patchee section as this will have 18885868e8aSDimitry Andric // been altered to point to us! 18985868e8aSDimitry Andric uint64_t s = getThumbDestAddr(getBranchAddr(), instr); 19085868e8aSDimitry Andric uint64_t p = getVA(4); 191*5ffd83dbSDimitry Andric target->relocateNoSym(buf, isARM ? R_ARM_JUMP24 : R_ARM_THM_JUMP24, s - p); 19285868e8aSDimitry Andric } 19385868e8aSDimitry Andric 19485868e8aSDimitry Andric // Given a branch instruction spanning two 4KiB regions, at offset off from the 19585868e8aSDimitry Andric // start of isec, return true if the destination of the branch is within the 19685868e8aSDimitry Andric // first of the two 4Kib regions. 19785868e8aSDimitry Andric static bool branchDestInFirstRegion(const InputSection *isec, uint64_t off, 19885868e8aSDimitry Andric uint32_t instr, const Relocation *r) { 19985868e8aSDimitry Andric uint64_t sourceAddr = isec->getVA(0) + off; 20085868e8aSDimitry Andric assert((sourceAddr & 0xfff) == 0xffe); 20185868e8aSDimitry Andric uint64_t destAddr = sourceAddr; 20285868e8aSDimitry Andric // If there is a branch relocation at the same offset we must use this to 20385868e8aSDimitry Andric // find the destination address as the branch could be indirected via a thunk 20485868e8aSDimitry Andric // or the PLT. 20585868e8aSDimitry Andric if (r) { 20685868e8aSDimitry Andric uint64_t dst = (r->expr == R_PLT_PC) ? r->sym->getPltVA() : r->sym->getVA(); 20785868e8aSDimitry Andric // Account for Thumb PC bias, usually cancelled to 0 by addend of -4. 20885868e8aSDimitry Andric destAddr = dst + r->addend + 4; 20985868e8aSDimitry Andric } else { 21085868e8aSDimitry Andric // If there is no relocation, we must have an intra-section branch 21185868e8aSDimitry Andric // We must extract the offset from the addend manually. 21285868e8aSDimitry Andric destAddr = getThumbDestAddr(sourceAddr, instr); 21385868e8aSDimitry Andric } 21485868e8aSDimitry Andric 21585868e8aSDimitry Andric return (destAddr & 0xfffff000) == (sourceAddr & 0xfffff000); 21685868e8aSDimitry Andric } 21785868e8aSDimitry Andric 21885868e8aSDimitry Andric // Return true if a branch can reach a patch section placed after isec. 21985868e8aSDimitry Andric // The Bcc.w instruction has a range of 1 MiB, all others have 16 MiB. 22085868e8aSDimitry Andric static bool patchInRange(const InputSection *isec, uint64_t off, 22185868e8aSDimitry Andric uint32_t instr) { 22285868e8aSDimitry Andric 22385868e8aSDimitry Andric // We need the branch at source to reach a patch section placed immediately 22485868e8aSDimitry Andric // after isec. As there can be more than one patch in the patch section we 22585868e8aSDimitry Andric // add 0x100 as contingency to account for worst case of 1 branch every 4KiB 22685868e8aSDimitry Andric // for a 1 MiB range. 22785868e8aSDimitry Andric return target->inBranchRange( 22885868e8aSDimitry Andric isBcc(instr) ? R_ARM_THM_JUMP19 : R_ARM_THM_JUMP24, isec->getVA(off), 22985868e8aSDimitry Andric isec->getVA() + isec->getSize() + 0x100); 23085868e8aSDimitry Andric } 23185868e8aSDimitry Andric 23285868e8aSDimitry Andric struct ScanResult { 23385868e8aSDimitry Andric // Offset of branch within its InputSection. 23485868e8aSDimitry Andric uint64_t off; 23585868e8aSDimitry Andric // Cached decoding of the branch instruction. 23685868e8aSDimitry Andric uint32_t instr; 23785868e8aSDimitry Andric // Branch relocation at off. Will be nullptr if no relocation exists. 23885868e8aSDimitry Andric Relocation *rel; 23985868e8aSDimitry Andric }; 24085868e8aSDimitry Andric 24185868e8aSDimitry Andric // Detect the erratum sequence, returning the offset of the branch instruction 24285868e8aSDimitry Andric // and a decoding of the branch. If the erratum sequence is not found then 24385868e8aSDimitry Andric // return an offset of 0 for the branch. 0 is a safe value to use for no patch 24485868e8aSDimitry Andric // as there must be at least one 32-bit non-branch instruction before the 24585868e8aSDimitry Andric // branch so the minimum offset for a patch is 4. 24685868e8aSDimitry Andric static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off, 24785868e8aSDimitry Andric uint64_t limit) { 24885868e8aSDimitry Andric uint64_t isecAddr = isec->getVA(0); 24985868e8aSDimitry Andric // Advance Off so that (isecAddr + off) modulo 0x1000 is at least 0xffa. We 25085868e8aSDimitry Andric // need to check for a 32-bit instruction immediately before a 32-bit branch 25185868e8aSDimitry Andric // at 0xffe modulo 0x1000. 25285868e8aSDimitry Andric off = alignTo(isecAddr + off, 0x1000, 0xffa) - isecAddr; 25385868e8aSDimitry Andric if (off >= limit || limit - off < 8) { 25485868e8aSDimitry Andric // Need at least 2 4-byte sized instructions to trigger erratum. 25585868e8aSDimitry Andric off = limit; 25685868e8aSDimitry Andric return {0, 0, nullptr}; 25785868e8aSDimitry Andric } 25885868e8aSDimitry Andric 25985868e8aSDimitry Andric ScanResult scanRes = {0, 0, nullptr}; 26085868e8aSDimitry Andric const uint8_t *buf = isec->data().begin(); 26185868e8aSDimitry Andric // ARMv7-A Thumb 32-bit instructions are encoded 2 consecutive 26285868e8aSDimitry Andric // little-endian halfwords. 26385868e8aSDimitry Andric const ulittle16_t *instBuf = reinterpret_cast<const ulittle16_t *>(buf + off); 26485868e8aSDimitry Andric uint16_t hw11 = *instBuf++; 26585868e8aSDimitry Andric uint16_t hw12 = *instBuf++; 26685868e8aSDimitry Andric uint16_t hw21 = *instBuf++; 26785868e8aSDimitry Andric uint16_t hw22 = *instBuf++; 26885868e8aSDimitry Andric if (is32bitInstruction(hw11) && is32bitInstruction(hw21)) { 26985868e8aSDimitry Andric uint32_t instr1 = (hw11 << 16) | hw12; 27085868e8aSDimitry Andric uint32_t instr2 = (hw21 << 16) | hw22; 27185868e8aSDimitry Andric if (!is32bitBranch(instr1) && is32bitBranch(instr2)) { 27285868e8aSDimitry Andric // Find a relocation for the branch if it exists. This will be used 27385868e8aSDimitry Andric // to determine the target. 27485868e8aSDimitry Andric uint64_t branchOff = off + 4; 27585868e8aSDimitry Andric auto relIt = llvm::find_if(isec->relocations, [=](const Relocation &r) { 27685868e8aSDimitry Andric return r.offset == branchOff && 27785868e8aSDimitry Andric (r.type == R_ARM_THM_JUMP19 || r.type == R_ARM_THM_JUMP24 || 27885868e8aSDimitry Andric r.type == R_ARM_THM_CALL); 27985868e8aSDimitry Andric }); 28085868e8aSDimitry Andric if (relIt != isec->relocations.end()) 28185868e8aSDimitry Andric scanRes.rel = &(*relIt); 28285868e8aSDimitry Andric if (branchDestInFirstRegion(isec, branchOff, instr2, scanRes.rel)) { 28385868e8aSDimitry Andric if (patchInRange(isec, branchOff, instr2)) { 28485868e8aSDimitry Andric scanRes.off = branchOff; 28585868e8aSDimitry Andric scanRes.instr = instr2; 28685868e8aSDimitry Andric } else { 28785868e8aSDimitry Andric warn(toString(isec->file) + 28885868e8aSDimitry Andric ": skipping cortex-a8 657417 erratum sequence, section " + 28985868e8aSDimitry Andric isec->name + " is too large to patch"); 29085868e8aSDimitry Andric } 29185868e8aSDimitry Andric } 29285868e8aSDimitry Andric } 29385868e8aSDimitry Andric } 29485868e8aSDimitry Andric off += 0x1000; 29585868e8aSDimitry Andric return scanRes; 29685868e8aSDimitry Andric } 29785868e8aSDimitry Andric 29885868e8aSDimitry Andric void ARMErr657417Patcher::init() { 29985868e8aSDimitry Andric // The Arm ABI permits a mix of ARM, Thumb and Data in the same 30085868e8aSDimitry Andric // InputSection. We must only scan Thumb instructions to avoid false 30185868e8aSDimitry Andric // matches. We use the mapping symbols in the InputObjects to identify this 30285868e8aSDimitry Andric // data, caching the results in sectionMap so we don't have to recalculate 30385868e8aSDimitry Andric // it each pass. 30485868e8aSDimitry Andric 30585868e8aSDimitry Andric // The ABI Section 4.5.5 Mapping symbols; defines local symbols that describe 30685868e8aSDimitry Andric // half open intervals [Symbol Value, Next Symbol Value) of code and data 30785868e8aSDimitry Andric // within sections. If there is no next symbol then the half open interval is 30885868e8aSDimitry Andric // [Symbol Value, End of section). The type, code or data, is determined by 30985868e8aSDimitry Andric // the mapping symbol name, $a for Arm code, $t for Thumb code, $d for data. 31085868e8aSDimitry Andric auto isArmMapSymbol = [](const Symbol *s) { 31185868e8aSDimitry Andric return s->getName() == "$a" || s->getName().startswith("$a."); 31285868e8aSDimitry Andric }; 31385868e8aSDimitry Andric auto isThumbMapSymbol = [](const Symbol *s) { 31485868e8aSDimitry Andric return s->getName() == "$t" || s->getName().startswith("$t."); 31585868e8aSDimitry Andric }; 31685868e8aSDimitry Andric auto isDataMapSymbol = [](const Symbol *s) { 31785868e8aSDimitry Andric return s->getName() == "$d" || s->getName().startswith("$d."); 31885868e8aSDimitry Andric }; 31985868e8aSDimitry Andric 32085868e8aSDimitry Andric // Collect mapping symbols for every executable InputSection. 32185868e8aSDimitry Andric for (InputFile *file : objectFiles) { 32285868e8aSDimitry Andric auto *f = cast<ObjFile<ELF32LE>>(file); 32385868e8aSDimitry Andric for (Symbol *s : f->getLocalSymbols()) { 32485868e8aSDimitry Andric auto *def = dyn_cast<Defined>(s); 32585868e8aSDimitry Andric if (!def) 32685868e8aSDimitry Andric continue; 32785868e8aSDimitry Andric if (!isArmMapSymbol(def) && !isThumbMapSymbol(def) && 32885868e8aSDimitry Andric !isDataMapSymbol(def)) 32985868e8aSDimitry Andric continue; 33085868e8aSDimitry Andric if (auto *sec = dyn_cast_or_null<InputSection>(def->section)) 33185868e8aSDimitry Andric if (sec->flags & SHF_EXECINSTR) 33285868e8aSDimitry Andric sectionMap[sec].push_back(def); 33385868e8aSDimitry Andric } 33485868e8aSDimitry Andric } 33585868e8aSDimitry Andric // For each InputSection make sure the mapping symbols are in sorted in 33685868e8aSDimitry Andric // ascending order and are in alternating Thumb, non-Thumb order. 33785868e8aSDimitry Andric for (auto &kv : sectionMap) { 33885868e8aSDimitry Andric std::vector<const Defined *> &mapSyms = kv.second; 33985868e8aSDimitry Andric llvm::stable_sort(mapSyms, [](const Defined *a, const Defined *b) { 34085868e8aSDimitry Andric return a->value < b->value; 34185868e8aSDimitry Andric }); 34285868e8aSDimitry Andric mapSyms.erase(std::unique(mapSyms.begin(), mapSyms.end(), 34385868e8aSDimitry Andric [=](const Defined *a, const Defined *b) { 34485868e8aSDimitry Andric return (isThumbMapSymbol(a) == 34585868e8aSDimitry Andric isThumbMapSymbol(b)); 34685868e8aSDimitry Andric }), 34785868e8aSDimitry Andric mapSyms.end()); 34885868e8aSDimitry Andric // Always start with a Thumb Mapping Symbol 34985868e8aSDimitry Andric if (!mapSyms.empty() && !isThumbMapSymbol(mapSyms.front())) 35085868e8aSDimitry Andric mapSyms.erase(mapSyms.begin()); 35185868e8aSDimitry Andric } 35285868e8aSDimitry Andric initialized = true; 35385868e8aSDimitry Andric } 35485868e8aSDimitry Andric 35585868e8aSDimitry Andric void ARMErr657417Patcher::insertPatches( 35685868e8aSDimitry Andric InputSectionDescription &isd, std::vector<Patch657417Section *> &patches) { 35785868e8aSDimitry Andric uint64_t spacing = 0x100000 - 0x7500; 35885868e8aSDimitry Andric uint64_t isecLimit; 35985868e8aSDimitry Andric uint64_t prevIsecLimit = isd.sections.front()->outSecOff; 36085868e8aSDimitry Andric uint64_t patchUpperBound = prevIsecLimit + spacing; 36185868e8aSDimitry Andric uint64_t outSecAddr = isd.sections.front()->getParent()->addr; 36285868e8aSDimitry Andric 36385868e8aSDimitry Andric // Set the outSecOff of patches to the place where we want to insert them. 36485868e8aSDimitry Andric // We use a similar strategy to initial thunk placement, using 1 MiB as the 36585868e8aSDimitry Andric // range of the Thumb-2 conditional branch with a contingency accounting for 36685868e8aSDimitry Andric // thunk generation. 36785868e8aSDimitry Andric auto patchIt = patches.begin(); 36885868e8aSDimitry Andric auto patchEnd = patches.end(); 36985868e8aSDimitry Andric for (const InputSection *isec : isd.sections) { 37085868e8aSDimitry Andric isecLimit = isec->outSecOff + isec->getSize(); 37185868e8aSDimitry Andric if (isecLimit > patchUpperBound) { 37285868e8aSDimitry Andric for (; patchIt != patchEnd; ++patchIt) { 37385868e8aSDimitry Andric if ((*patchIt)->getBranchAddr() - outSecAddr >= prevIsecLimit) 37485868e8aSDimitry Andric break; 37585868e8aSDimitry Andric (*patchIt)->outSecOff = prevIsecLimit; 37685868e8aSDimitry Andric } 37785868e8aSDimitry Andric patchUpperBound = prevIsecLimit + spacing; 37885868e8aSDimitry Andric } 37985868e8aSDimitry Andric prevIsecLimit = isecLimit; 38085868e8aSDimitry Andric } 38185868e8aSDimitry Andric for (; patchIt != patchEnd; ++patchIt) 38285868e8aSDimitry Andric (*patchIt)->outSecOff = isecLimit; 38385868e8aSDimitry Andric 38485868e8aSDimitry Andric // Merge all patch sections. We use the outSecOff assigned above to 38585868e8aSDimitry Andric // determine the insertion point. This is ok as we only merge into an 38685868e8aSDimitry Andric // InputSectionDescription once per pass, and at the end of the pass 38785868e8aSDimitry Andric // assignAddresses() will recalculate all the outSecOff values. 38885868e8aSDimitry Andric std::vector<InputSection *> tmp; 38985868e8aSDimitry Andric tmp.reserve(isd.sections.size() + patches.size()); 39085868e8aSDimitry Andric auto mergeCmp = [](const InputSection *a, const InputSection *b) { 39185868e8aSDimitry Andric if (a->outSecOff != b->outSecOff) 39285868e8aSDimitry Andric return a->outSecOff < b->outSecOff; 39385868e8aSDimitry Andric return isa<Patch657417Section>(a) && !isa<Patch657417Section>(b); 39485868e8aSDimitry Andric }; 39585868e8aSDimitry Andric std::merge(isd.sections.begin(), isd.sections.end(), patches.begin(), 39685868e8aSDimitry Andric patches.end(), std::back_inserter(tmp), mergeCmp); 39785868e8aSDimitry Andric isd.sections = std::move(tmp); 39885868e8aSDimitry Andric } 39985868e8aSDimitry Andric 40085868e8aSDimitry Andric // Given a branch instruction described by ScanRes redirect it to a patch 40185868e8aSDimitry Andric // section containing an unconditional branch instruction to the target. 40285868e8aSDimitry Andric // Ensure that this patch section is 4-byte aligned so that the branch cannot 40385868e8aSDimitry Andric // span two 4 KiB regions. Place the patch section so that it is always after 40485868e8aSDimitry Andric // isec so the branch we are patching always goes forwards. 40585868e8aSDimitry Andric static void implementPatch(ScanResult sr, InputSection *isec, 40685868e8aSDimitry Andric std::vector<Patch657417Section *> &patches) { 40785868e8aSDimitry Andric 40885868e8aSDimitry Andric log("detected cortex-a8-657419 erratum sequence starting at " + 40985868e8aSDimitry Andric utohexstr(isec->getVA(sr.off)) + " in unpatched output."); 41085868e8aSDimitry Andric Patch657417Section *psec; 41185868e8aSDimitry Andric // We have two cases to deal with. 41285868e8aSDimitry Andric // Case 1. There is a relocation at patcheeOffset to a symbol. The 41385868e8aSDimitry Andric // unconditional branch in the patch must have a relocation so that any 41485868e8aSDimitry Andric // further redirection via the PLT or a Thunk happens as normal. At 41585868e8aSDimitry Andric // patcheeOffset we redirect the existing relocation to a Symbol defined at 41685868e8aSDimitry Andric // the start of the patch section. 41785868e8aSDimitry Andric // 41885868e8aSDimitry Andric // Case 2. There is no relocation at patcheeOffset. We are unlikely to have 41985868e8aSDimitry Andric // a symbol that we can use as a target for a relocation in the patch section. 42085868e8aSDimitry Andric // Luckily we know that the destination cannot be indirected via the PLT or 42185868e8aSDimitry Andric // a Thunk so we can just write the destination directly. 42285868e8aSDimitry Andric if (sr.rel) { 42385868e8aSDimitry Andric // Case 1. We have an existing relocation to redirect to patch and a 42485868e8aSDimitry Andric // Symbol target. 42585868e8aSDimitry Andric 42685868e8aSDimitry Andric // Create a branch relocation for the unconditional branch in the patch. 42785868e8aSDimitry Andric // This can be redirected via the PLT or Thunks. 42885868e8aSDimitry Andric RelType patchRelType = R_ARM_THM_JUMP24; 42985868e8aSDimitry Andric int64_t patchRelAddend = sr.rel->addend; 43085868e8aSDimitry Andric bool destIsARM = false; 43185868e8aSDimitry Andric if (isBL(sr.instr) || isBLX(sr.instr)) { 43285868e8aSDimitry Andric // The final target of the branch may be ARM or Thumb, if the target 43385868e8aSDimitry Andric // is ARM then we write the patch in ARM state to avoid a state change 43485868e8aSDimitry Andric // Thunk from the patch to the target. 43585868e8aSDimitry Andric uint64_t dstSymAddr = (sr.rel->expr == R_PLT_PC) ? sr.rel->sym->getPltVA() 43685868e8aSDimitry Andric : sr.rel->sym->getVA(); 43785868e8aSDimitry Andric destIsARM = (dstSymAddr & 1) == 0; 43885868e8aSDimitry Andric } 43985868e8aSDimitry Andric psec = make<Patch657417Section>(isec, sr.off, sr.instr, destIsARM); 44085868e8aSDimitry Andric if (destIsARM) { 44185868e8aSDimitry Andric // The patch will be in ARM state. Use an ARM relocation and account for 44285868e8aSDimitry Andric // the larger ARM PC-bias of 8 rather than Thumb's 4. 44385868e8aSDimitry Andric patchRelType = R_ARM_JUMP24; 44485868e8aSDimitry Andric patchRelAddend -= 4; 44585868e8aSDimitry Andric } 44685868e8aSDimitry Andric psec->relocations.push_back( 44785868e8aSDimitry Andric Relocation{sr.rel->expr, patchRelType, 0, patchRelAddend, sr.rel->sym}); 44885868e8aSDimitry Andric // Redirect the existing branch relocation to the patch. 44985868e8aSDimitry Andric sr.rel->expr = R_PC; 45085868e8aSDimitry Andric sr.rel->addend = -4; 45185868e8aSDimitry Andric sr.rel->sym = psec->patchSym; 45285868e8aSDimitry Andric } else { 45385868e8aSDimitry Andric // Case 2. We do not have a relocation to the patch. Add a relocation of the 45485868e8aSDimitry Andric // appropriate type to the patch at patcheeOffset. 45585868e8aSDimitry Andric 45685868e8aSDimitry Andric // The destination is ARM if we have a BLX. 45785868e8aSDimitry Andric psec = make<Patch657417Section>(isec, sr.off, sr.instr, isBLX(sr.instr)); 45885868e8aSDimitry Andric RelType type; 45985868e8aSDimitry Andric if (isBcc(sr.instr)) 46085868e8aSDimitry Andric type = R_ARM_THM_JUMP19; 46185868e8aSDimitry Andric else if (isB(sr.instr)) 46285868e8aSDimitry Andric type = R_ARM_THM_JUMP24; 46385868e8aSDimitry Andric else 46485868e8aSDimitry Andric type = R_ARM_THM_CALL; 46585868e8aSDimitry Andric isec->relocations.push_back( 46685868e8aSDimitry Andric Relocation{R_PC, type, sr.off, -4, psec->patchSym}); 46785868e8aSDimitry Andric } 46885868e8aSDimitry Andric patches.push_back(psec); 46985868e8aSDimitry Andric } 47085868e8aSDimitry Andric 47185868e8aSDimitry Andric // Scan all the instructions in InputSectionDescription, for each instance of 47285868e8aSDimitry Andric // the erratum sequence create a Patch657417Section. We return the list of 47385868e8aSDimitry Andric // Patch657417Sections that need to be applied to the InputSectionDescription. 47485868e8aSDimitry Andric std::vector<Patch657417Section *> 47585868e8aSDimitry Andric ARMErr657417Patcher::patchInputSectionDescription( 47685868e8aSDimitry Andric InputSectionDescription &isd) { 47785868e8aSDimitry Andric std::vector<Patch657417Section *> patches; 47885868e8aSDimitry Andric for (InputSection *isec : isd.sections) { 47985868e8aSDimitry Andric // LLD doesn't use the erratum sequence in SyntheticSections. 48085868e8aSDimitry Andric if (isa<SyntheticSection>(isec)) 48185868e8aSDimitry Andric continue; 48285868e8aSDimitry Andric // Use sectionMap to make sure we only scan Thumb code and not Arm or inline 48385868e8aSDimitry Andric // data. We have already sorted mapSyms in ascending order and removed 48485868e8aSDimitry Andric // consecutive mapping symbols of the same type. Our range of executable 48585868e8aSDimitry Andric // instructions to scan is therefore [thumbSym->value, nonThumbSym->value) 48685868e8aSDimitry Andric // or [thumbSym->value, section size). 48785868e8aSDimitry Andric std::vector<const Defined *> &mapSyms = sectionMap[isec]; 48885868e8aSDimitry Andric 48985868e8aSDimitry Andric auto thumbSym = mapSyms.begin(); 49085868e8aSDimitry Andric while (thumbSym != mapSyms.end()) { 49185868e8aSDimitry Andric auto nonThumbSym = std::next(thumbSym); 49285868e8aSDimitry Andric uint64_t off = (*thumbSym)->value; 49385868e8aSDimitry Andric uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->data().size() 49485868e8aSDimitry Andric : (*nonThumbSym)->value; 49585868e8aSDimitry Andric 49685868e8aSDimitry Andric while (off < limit) { 49785868e8aSDimitry Andric ScanResult sr = scanCortexA8Errata657417(isec, off, limit); 49885868e8aSDimitry Andric if (sr.off) 49985868e8aSDimitry Andric implementPatch(sr, isec, patches); 50085868e8aSDimitry Andric } 50185868e8aSDimitry Andric if (nonThumbSym == mapSyms.end()) 50285868e8aSDimitry Andric break; 50385868e8aSDimitry Andric thumbSym = std::next(nonThumbSym); 50485868e8aSDimitry Andric } 50585868e8aSDimitry Andric } 50685868e8aSDimitry Andric return patches; 50785868e8aSDimitry Andric } 50885868e8aSDimitry Andric 50985868e8aSDimitry Andric bool ARMErr657417Patcher::createFixes() { 51085868e8aSDimitry Andric if (!initialized) 51185868e8aSDimitry Andric init(); 51285868e8aSDimitry Andric 51385868e8aSDimitry Andric bool addressesChanged = false; 51485868e8aSDimitry Andric for (OutputSection *os : outputSections) { 51585868e8aSDimitry Andric if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR)) 51685868e8aSDimitry Andric continue; 51785868e8aSDimitry Andric for (BaseCommand *bc : os->sectionCommands) 51885868e8aSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(bc)) { 51985868e8aSDimitry Andric std::vector<Patch657417Section *> patches = 52085868e8aSDimitry Andric patchInputSectionDescription(*isd); 52185868e8aSDimitry Andric if (!patches.empty()) { 52285868e8aSDimitry Andric insertPatches(*isd, patches); 52385868e8aSDimitry Andric addressesChanged = true; 52485868e8aSDimitry Andric } 52585868e8aSDimitry Andric } 52685868e8aSDimitry Andric } 52785868e8aSDimitry Andric return addressesChanged; 52885868e8aSDimitry Andric } 529