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