xref: /freebsd/contrib/llvm-project/lld/ELF/ARMErrataFix.cpp (revision 85868e8a1daeaae7a0e48effb2ea2310ae3b02c6)
1*85868e8aSDimitry Andric //===- ARMErrataFix.cpp ---------------------------------------------------===//
2*85868e8aSDimitry Andric //
3*85868e8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*85868e8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*85868e8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*85868e8aSDimitry Andric //
7*85868e8aSDimitry Andric //===----------------------------------------------------------------------===//
8*85868e8aSDimitry Andric // This file implements Section Patching for the purpose of working around the
9*85868e8aSDimitry Andric // Cortex-a8 erratum 657417 "A 32bit branch instruction that spans 2 4K regions
10*85868e8aSDimitry Andric // can result in an incorrect instruction fetch or processor deadlock." The
11*85868e8aSDimitry Andric // erratum affects all but r1p7, r2p5, r2p6, r3p1 and r3p2 revisions of the
12*85868e8aSDimitry Andric // Cortex-A8. A high level description of the patching technique is given in
13*85868e8aSDimitry Andric // the opening comment of AArch64ErrataFix.cpp.
14*85868e8aSDimitry Andric //===----------------------------------------------------------------------===//
15*85868e8aSDimitry Andric 
16*85868e8aSDimitry Andric #include "ARMErrataFix.h"
17*85868e8aSDimitry Andric 
18*85868e8aSDimitry Andric #include "Config.h"
19*85868e8aSDimitry Andric #include "LinkerScript.h"
20*85868e8aSDimitry Andric #include "OutputSections.h"
21*85868e8aSDimitry Andric #include "Relocations.h"
22*85868e8aSDimitry Andric #include "Symbols.h"
23*85868e8aSDimitry Andric #include "SyntheticSections.h"
24*85868e8aSDimitry Andric #include "Target.h"
25*85868e8aSDimitry Andric #include "lld/Common/Memory.h"
26*85868e8aSDimitry Andric #include "lld/Common/Strings.h"
27*85868e8aSDimitry Andric #include "llvm/Support/Endian.h"
28*85868e8aSDimitry Andric #include "llvm/Support/raw_ostream.h"
29*85868e8aSDimitry Andric #include <algorithm>
30*85868e8aSDimitry Andric 
31*85868e8aSDimitry Andric using namespace llvm;
32*85868e8aSDimitry Andric using namespace llvm::ELF;
33*85868e8aSDimitry Andric using namespace llvm::object;
34*85868e8aSDimitry Andric using namespace llvm::support;
35*85868e8aSDimitry Andric using namespace llvm::support::endian;
36*85868e8aSDimitry Andric 
37*85868e8aSDimitry Andric namespace lld {
38*85868e8aSDimitry Andric namespace elf {
39*85868e8aSDimitry Andric 
40*85868e8aSDimitry Andric // The documented title for Erratum 657417 is:
41*85868e8aSDimitry Andric // "A 32bit branch instruction that spans two 4K regions can result in an
42*85868e8aSDimitry Andric // incorrect instruction fetch or processor deadlock". Graphically using a
43*85868e8aSDimitry Andric // 32-bit B.w instruction encoded as a pair of halfwords 0xf7fe 0xbfff
44*85868e8aSDimitry Andric // xxxxxx000 // Memory region 1 start
45*85868e8aSDimitry Andric // target:
46*85868e8aSDimitry Andric // ...
47*85868e8aSDimitry Andric // xxxxxxffe f7fe // First halfword of branch to target:
48*85868e8aSDimitry Andric // xxxxxx000 // Memory region 2 start
49*85868e8aSDimitry Andric // xxxxxx002 bfff // Second halfword of branch to target:
50*85868e8aSDimitry Andric //
51*85868e8aSDimitry Andric // The specific trigger conditions that can be detected at link time are:
52*85868e8aSDimitry Andric // - There is a 32-bit Thumb-2 branch instruction with an address of the form
53*85868e8aSDimitry Andric //   xxxxxxFFE. The first 2 bytes of the instruction are in 4KiB region 1, the
54*85868e8aSDimitry Andric //   second 2 bytes are in region 2.
55*85868e8aSDimitry Andric // - The branch instruction is one of BLX, BL, B.w BCC.w
56*85868e8aSDimitry Andric // - The instruction preceding the branch is a 32-bit non-branch instruction.
57*85868e8aSDimitry Andric // - The target of the branch is in region 1.
58*85868e8aSDimitry Andric //
59*85868e8aSDimitry Andric // The linker mitigation for the fix is to redirect any branch that meets the
60*85868e8aSDimitry Andric // erratum conditions to a patch section containing a branch to the target.
61*85868e8aSDimitry Andric //
62*85868e8aSDimitry Andric // As adding patch sections may move branches onto region boundaries the patch
63*85868e8aSDimitry Andric // must iterate until no more patches are added.
64*85868e8aSDimitry Andric //
65*85868e8aSDimitry Andric // Example, before:
66*85868e8aSDimitry Andric // 00000FFA func: NOP.w      // 32-bit Thumb function
67*85868e8aSDimitry Andric // 00000FFE       B.W func   // 32-bit branch spanning 2 regions, dest in 1st.
68*85868e8aSDimitry Andric // Example, after:
69*85868e8aSDimitry Andric // 00000FFA func: NOP.w      // 32-bit Thumb function
70*85868e8aSDimitry Andric // 00000FFE       B.w __CortexA8657417_00000FFE
71*85868e8aSDimitry Andric // 00001002       2 - bytes padding
72*85868e8aSDimitry Andric // 00001004 __CortexA8657417_00000FFE: B.w func
73*85868e8aSDimitry Andric 
74*85868e8aSDimitry Andric class Patch657417Section : public SyntheticSection {
75*85868e8aSDimitry Andric public:
76*85868e8aSDimitry Andric   Patch657417Section(InputSection *p, uint64_t off, uint32_t instr, bool isARM);
77*85868e8aSDimitry Andric 
78*85868e8aSDimitry Andric   void writeTo(uint8_t *buf) override;
79*85868e8aSDimitry Andric 
80*85868e8aSDimitry Andric   size_t getSize() const override { return 4; }
81*85868e8aSDimitry Andric 
82*85868e8aSDimitry Andric   // Get the virtual address of the branch instruction at patcheeOffset.
83*85868e8aSDimitry Andric   uint64_t getBranchAddr() const;
84*85868e8aSDimitry Andric 
85*85868e8aSDimitry Andric   // The Section we are patching.
86*85868e8aSDimitry Andric   const InputSection *patchee;
87*85868e8aSDimitry Andric   // The offset of the instruction in the Patchee section we are patching.
88*85868e8aSDimitry Andric   uint64_t patcheeOffset;
89*85868e8aSDimitry Andric   // A label for the start of the Patch that we can use as a relocation target.
90*85868e8aSDimitry Andric   Symbol *patchSym;
91*85868e8aSDimitry Andric   // A decoding of the branch instruction at patcheeOffset.
92*85868e8aSDimitry Andric   uint32_t instr;
93*85868e8aSDimitry Andric   // True If the patch is to be written in ARM state, otherwise the patch will
94*85868e8aSDimitry Andric   // be written in Thumb state.
95*85868e8aSDimitry Andric   bool isARM;
96*85868e8aSDimitry Andric };
97*85868e8aSDimitry Andric 
98*85868e8aSDimitry Andric // Return true if the half-word, when taken as the first of a pair of halfwords
99*85868e8aSDimitry Andric // is the first half of a 32-bit instruction.
100*85868e8aSDimitry Andric // Reference from ARM Architecure Reference Manual ARMv7-A and ARMv7-R edition
101*85868e8aSDimitry Andric // section A6.3: 32-bit Thumb instruction encoding
102*85868e8aSDimitry Andric // |             HW1                   |               HW2                |
103*85868e8aSDimitry Andric // | 1 1 1 | op1 (2) | op2 (7) | x (4) |op|           x (15)              |
104*85868e8aSDimitry Andric // With op1 == 0b00, a 16-bit instruction is encoded.
105*85868e8aSDimitry Andric //
106*85868e8aSDimitry Andric // We test only the first halfword, looking for op != 0b00.
107*85868e8aSDimitry Andric static bool is32bitInstruction(uint16_t hw) {
108*85868e8aSDimitry Andric   return (hw & 0xe000) == 0xe000 && (hw & 0x1800) != 0x0000;
109*85868e8aSDimitry Andric }
110*85868e8aSDimitry Andric 
111*85868e8aSDimitry Andric // Reference from ARM Architecure Reference Manual ARMv7-A and ARMv7-R edition
112*85868e8aSDimitry Andric // section A6.3.4 Branches and miscellaneous control.
113*85868e8aSDimitry Andric // |             HW1              |               HW2                |
114*85868e8aSDimitry Andric // | 1 1 1 | 1 0 | op (7) | x (4) | 1 | op1 (3) | op2 (4) | imm8 (8) |
115*85868e8aSDimitry Andric // op1 == 0x0 op != x111xxx | Conditional branch (Bcc.W)
116*85868e8aSDimitry Andric // op1 == 0x1               | Branch (B.W)
117*85868e8aSDimitry Andric // op1 == 1x0               | Branch with Link and Exchange (BLX.w)
118*85868e8aSDimitry Andric // op1 == 1x1               | Branch with Link (BL.W)
119*85868e8aSDimitry Andric 
120*85868e8aSDimitry Andric static bool isBcc(uint32_t instr) {
121*85868e8aSDimitry Andric   return (instr & 0xf800d000) == 0xf0008000 &&
122*85868e8aSDimitry Andric          (instr & 0x03800000) != 0x03800000;
123*85868e8aSDimitry Andric }
124*85868e8aSDimitry Andric 
125*85868e8aSDimitry Andric static bool isB(uint32_t instr) { return (instr & 0xf800d000) == 0xf0009000; }
126*85868e8aSDimitry Andric 
127*85868e8aSDimitry Andric static bool isBLX(uint32_t instr) { return (instr & 0xf800d000) == 0xf000c000; }
128*85868e8aSDimitry Andric 
129*85868e8aSDimitry Andric static bool isBL(uint32_t instr) { return (instr & 0xf800d000) == 0xf000d000; }
130*85868e8aSDimitry Andric 
131*85868e8aSDimitry Andric static bool is32bitBranch(uint32_t instr) {
132*85868e8aSDimitry Andric   return isBcc(instr) || isB(instr) || isBL(instr) || isBLX(instr);
133*85868e8aSDimitry Andric }
134*85868e8aSDimitry Andric 
135*85868e8aSDimitry Andric Patch657417Section::Patch657417Section(InputSection *p, uint64_t off,
136*85868e8aSDimitry Andric                                        uint32_t instr, bool isARM)
137*85868e8aSDimitry Andric     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 4,
138*85868e8aSDimitry Andric                        ".text.patch"),
139*85868e8aSDimitry Andric       patchee(p), patcheeOffset(off), instr(instr), isARM(isARM) {
140*85868e8aSDimitry Andric   parent = p->getParent();
141*85868e8aSDimitry Andric   patchSym = addSyntheticLocal(
142*85868e8aSDimitry Andric       saver.save("__CortexA8657417_" + utohexstr(getBranchAddr())), STT_FUNC,
143*85868e8aSDimitry Andric       isARM ? 0 : 1, getSize(), *this);
144*85868e8aSDimitry Andric   addSyntheticLocal(saver.save(isARM ? "$a" : "$t"), STT_NOTYPE, 0, 0, *this);
145*85868e8aSDimitry Andric }
146*85868e8aSDimitry Andric 
147*85868e8aSDimitry Andric uint64_t Patch657417Section::getBranchAddr() const {
148*85868e8aSDimitry Andric   return patchee->getVA(patcheeOffset);
149*85868e8aSDimitry Andric }
150*85868e8aSDimitry Andric 
151*85868e8aSDimitry Andric // Given a branch instruction instr at sourceAddr work out its destination
152*85868e8aSDimitry Andric // address. This is only used when the branch instruction has no relocation.
153*85868e8aSDimitry Andric static uint64_t getThumbDestAddr(uint64_t sourceAddr, uint32_t instr) {
154*85868e8aSDimitry Andric   uint8_t buf[4];
155*85868e8aSDimitry Andric   write16le(buf, instr >> 16);
156*85868e8aSDimitry Andric   write16le(buf + 2, instr & 0x0000ffff);
157*85868e8aSDimitry Andric   int64_t offset;
158*85868e8aSDimitry Andric   if (isBcc(instr))
159*85868e8aSDimitry Andric     offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP19);
160*85868e8aSDimitry Andric   else if (isB(instr))
161*85868e8aSDimitry Andric     offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP24);
162*85868e8aSDimitry Andric   else
163*85868e8aSDimitry Andric     offset = target->getImplicitAddend(buf, R_ARM_THM_CALL);
164*85868e8aSDimitry Andric   return sourceAddr + offset + 4;
165*85868e8aSDimitry Andric }
166*85868e8aSDimitry Andric 
167*85868e8aSDimitry Andric void Patch657417Section::writeTo(uint8_t *buf) {
168*85868e8aSDimitry Andric   // The base instruction of the patch is always a 32-bit unconditional branch.
169*85868e8aSDimitry Andric   if (isARM)
170*85868e8aSDimitry Andric     write32le(buf, 0xea000000);
171*85868e8aSDimitry Andric   else
172*85868e8aSDimitry Andric     write32le(buf, 0x9000f000);
173*85868e8aSDimitry Andric   // If we have a relocation then apply it. For a SyntheticSection buf already
174*85868e8aSDimitry Andric   // has outSecOff added, but relocateAlloc also adds outSecOff so we need to
175*85868e8aSDimitry Andric   // subtract to avoid double counting.
176*85868e8aSDimitry Andric   if (!relocations.empty()) {
177*85868e8aSDimitry Andric     relocateAlloc(buf - outSecOff, buf - outSecOff + getSize());
178*85868e8aSDimitry Andric     return;
179*85868e8aSDimitry Andric   }
180*85868e8aSDimitry Andric 
181*85868e8aSDimitry Andric   // If we don't have a relocation then we must calculate and write the offset
182*85868e8aSDimitry Andric   // ourselves.
183*85868e8aSDimitry Andric   // Get the destination offset from the addend in the branch instruction.
184*85868e8aSDimitry Andric   // We cannot use the instruction in the patchee section as this will have
185*85868e8aSDimitry Andric   // been altered to point to us!
186*85868e8aSDimitry Andric   uint64_t s = getThumbDestAddr(getBranchAddr(), instr);
187*85868e8aSDimitry Andric   uint64_t p = getVA(4);
188*85868e8aSDimitry Andric   target->relocateOne(buf, isARM ? R_ARM_JUMP24 : R_ARM_THM_JUMP24, s - p);
189*85868e8aSDimitry Andric }
190*85868e8aSDimitry Andric 
191*85868e8aSDimitry Andric // Given a branch instruction spanning two 4KiB regions, at offset off from the
192*85868e8aSDimitry Andric // start of isec, return true if the destination of the branch is within the
193*85868e8aSDimitry Andric // first of the two 4Kib regions.
194*85868e8aSDimitry Andric static bool branchDestInFirstRegion(const InputSection *isec, uint64_t off,
195*85868e8aSDimitry Andric                                     uint32_t instr, const Relocation *r) {
196*85868e8aSDimitry Andric   uint64_t sourceAddr = isec->getVA(0) + off;
197*85868e8aSDimitry Andric   assert((sourceAddr & 0xfff) == 0xffe);
198*85868e8aSDimitry Andric   uint64_t destAddr = sourceAddr;
199*85868e8aSDimitry Andric   // If there is a branch relocation at the same offset we must use this to
200*85868e8aSDimitry Andric   // find the destination address as the branch could be indirected via a thunk
201*85868e8aSDimitry Andric   // or the PLT.
202*85868e8aSDimitry Andric   if (r) {
203*85868e8aSDimitry Andric     uint64_t dst = (r->expr == R_PLT_PC) ? r->sym->getPltVA() : r->sym->getVA();
204*85868e8aSDimitry Andric     // Account for Thumb PC bias, usually cancelled to 0 by addend of -4.
205*85868e8aSDimitry Andric     destAddr = dst + r->addend + 4;
206*85868e8aSDimitry Andric   } else {
207*85868e8aSDimitry Andric     // If there is no relocation, we must have an intra-section branch
208*85868e8aSDimitry Andric     // We must extract the offset from the addend manually.
209*85868e8aSDimitry Andric     destAddr = getThumbDestAddr(sourceAddr, instr);
210*85868e8aSDimitry Andric   }
211*85868e8aSDimitry Andric 
212*85868e8aSDimitry Andric   return (destAddr & 0xfffff000) == (sourceAddr & 0xfffff000);
213*85868e8aSDimitry Andric }
214*85868e8aSDimitry Andric 
215*85868e8aSDimitry Andric // Return true if a branch can reach a patch section placed after isec.
216*85868e8aSDimitry Andric // The Bcc.w instruction has a range of 1 MiB, all others have 16 MiB.
217*85868e8aSDimitry Andric static bool patchInRange(const InputSection *isec, uint64_t off,
218*85868e8aSDimitry Andric                          uint32_t instr) {
219*85868e8aSDimitry Andric 
220*85868e8aSDimitry Andric   // We need the branch at source to reach a patch section placed immediately
221*85868e8aSDimitry Andric   // after isec. As there can be more than one patch in the patch section we
222*85868e8aSDimitry Andric   // add 0x100 as contingency to account for worst case of 1 branch every 4KiB
223*85868e8aSDimitry Andric   // for a 1 MiB range.
224*85868e8aSDimitry Andric   return target->inBranchRange(
225*85868e8aSDimitry Andric       isBcc(instr) ? R_ARM_THM_JUMP19 : R_ARM_THM_JUMP24, isec->getVA(off),
226*85868e8aSDimitry Andric       isec->getVA() + isec->getSize() + 0x100);
227*85868e8aSDimitry Andric }
228*85868e8aSDimitry Andric 
229*85868e8aSDimitry Andric struct ScanResult {
230*85868e8aSDimitry Andric   // Offset of branch within its InputSection.
231*85868e8aSDimitry Andric   uint64_t off;
232*85868e8aSDimitry Andric   // Cached decoding of the branch instruction.
233*85868e8aSDimitry Andric   uint32_t instr;
234*85868e8aSDimitry Andric   // Branch relocation at off. Will be nullptr if no relocation exists.
235*85868e8aSDimitry Andric   Relocation *rel;
236*85868e8aSDimitry Andric };
237*85868e8aSDimitry Andric 
238*85868e8aSDimitry Andric // Detect the erratum sequence, returning the offset of the branch instruction
239*85868e8aSDimitry Andric // and a decoding of the branch. If the erratum sequence is not found then
240*85868e8aSDimitry Andric // return an offset of 0 for the branch. 0 is a safe value to use for no patch
241*85868e8aSDimitry Andric // as there must be at least one 32-bit non-branch instruction before the
242*85868e8aSDimitry Andric // branch so the minimum offset for a patch is 4.
243*85868e8aSDimitry Andric static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
244*85868e8aSDimitry Andric                                            uint64_t limit) {
245*85868e8aSDimitry Andric   uint64_t isecAddr = isec->getVA(0);
246*85868e8aSDimitry Andric   // Advance Off so that (isecAddr + off) modulo 0x1000 is at least 0xffa. We
247*85868e8aSDimitry Andric   // need to check for a 32-bit instruction immediately before a 32-bit branch
248*85868e8aSDimitry Andric   // at 0xffe modulo 0x1000.
249*85868e8aSDimitry Andric   off = alignTo(isecAddr + off, 0x1000, 0xffa) - isecAddr;
250*85868e8aSDimitry Andric   if (off >= limit || limit - off < 8) {
251*85868e8aSDimitry Andric     // Need at least 2 4-byte sized instructions to trigger erratum.
252*85868e8aSDimitry Andric     off = limit;
253*85868e8aSDimitry Andric     return {0, 0, nullptr};
254*85868e8aSDimitry Andric   }
255*85868e8aSDimitry Andric 
256*85868e8aSDimitry Andric   ScanResult scanRes = {0, 0, nullptr};
257*85868e8aSDimitry Andric   const uint8_t *buf = isec->data().begin();
258*85868e8aSDimitry Andric   // ARMv7-A Thumb 32-bit instructions are encoded 2 consecutive
259*85868e8aSDimitry Andric   // little-endian halfwords.
260*85868e8aSDimitry Andric   const ulittle16_t *instBuf = reinterpret_cast<const ulittle16_t *>(buf + off);
261*85868e8aSDimitry Andric   uint16_t hw11 = *instBuf++;
262*85868e8aSDimitry Andric   uint16_t hw12 = *instBuf++;
263*85868e8aSDimitry Andric   uint16_t hw21 = *instBuf++;
264*85868e8aSDimitry Andric   uint16_t hw22 = *instBuf++;
265*85868e8aSDimitry Andric   if (is32bitInstruction(hw11) && is32bitInstruction(hw21)) {
266*85868e8aSDimitry Andric     uint32_t instr1 = (hw11 << 16) | hw12;
267*85868e8aSDimitry Andric     uint32_t instr2 = (hw21 << 16) | hw22;
268*85868e8aSDimitry Andric     if (!is32bitBranch(instr1) && is32bitBranch(instr2)) {
269*85868e8aSDimitry Andric       // Find a relocation for the branch if it exists. This will be used
270*85868e8aSDimitry Andric       // to determine the target.
271*85868e8aSDimitry Andric       uint64_t branchOff = off + 4;
272*85868e8aSDimitry Andric       auto relIt = llvm::find_if(isec->relocations, [=](const Relocation &r) {
273*85868e8aSDimitry Andric         return r.offset == branchOff &&
274*85868e8aSDimitry Andric                (r.type == R_ARM_THM_JUMP19 || r.type == R_ARM_THM_JUMP24 ||
275*85868e8aSDimitry Andric                 r.type == R_ARM_THM_CALL);
276*85868e8aSDimitry Andric       });
277*85868e8aSDimitry Andric       if (relIt != isec->relocations.end())
278*85868e8aSDimitry Andric         scanRes.rel = &(*relIt);
279*85868e8aSDimitry Andric       if (branchDestInFirstRegion(isec, branchOff, instr2, scanRes.rel)) {
280*85868e8aSDimitry Andric         if (patchInRange(isec, branchOff, instr2)) {
281*85868e8aSDimitry Andric           scanRes.off = branchOff;
282*85868e8aSDimitry Andric           scanRes.instr = instr2;
283*85868e8aSDimitry Andric         } else {
284*85868e8aSDimitry Andric           warn(toString(isec->file) +
285*85868e8aSDimitry Andric                ": skipping cortex-a8 657417 erratum sequence, section " +
286*85868e8aSDimitry Andric                isec->name + " is too large to patch");
287*85868e8aSDimitry Andric         }
288*85868e8aSDimitry Andric       }
289*85868e8aSDimitry Andric     }
290*85868e8aSDimitry Andric   }
291*85868e8aSDimitry Andric   off += 0x1000;
292*85868e8aSDimitry Andric   return scanRes;
293*85868e8aSDimitry Andric }
294*85868e8aSDimitry Andric 
295*85868e8aSDimitry Andric void ARMErr657417Patcher::init() {
296*85868e8aSDimitry Andric   // The Arm ABI permits a mix of ARM, Thumb and Data in the same
297*85868e8aSDimitry Andric   // InputSection. We must only scan Thumb instructions to avoid false
298*85868e8aSDimitry Andric   // matches. We use the mapping symbols in the InputObjects to identify this
299*85868e8aSDimitry Andric   // data, caching the results in sectionMap so we don't have to recalculate
300*85868e8aSDimitry Andric   // it each pass.
301*85868e8aSDimitry Andric 
302*85868e8aSDimitry Andric   // The ABI Section 4.5.5 Mapping symbols; defines local symbols that describe
303*85868e8aSDimitry Andric   // half open intervals [Symbol Value, Next Symbol Value) of code and data
304*85868e8aSDimitry Andric   // within sections. If there is no next symbol then the half open interval is
305*85868e8aSDimitry Andric   // [Symbol Value, End of section). The type, code or data, is determined by
306*85868e8aSDimitry Andric   // the mapping symbol name, $a for Arm code, $t for Thumb code, $d for data.
307*85868e8aSDimitry Andric   auto isArmMapSymbol = [](const Symbol *s) {
308*85868e8aSDimitry Andric     return s->getName() == "$a" || s->getName().startswith("$a.");
309*85868e8aSDimitry Andric   };
310*85868e8aSDimitry Andric   auto isThumbMapSymbol = [](const Symbol *s) {
311*85868e8aSDimitry Andric     return s->getName() == "$t" || s->getName().startswith("$t.");
312*85868e8aSDimitry Andric   };
313*85868e8aSDimitry Andric   auto isDataMapSymbol = [](const Symbol *s) {
314*85868e8aSDimitry Andric     return s->getName() == "$d" || s->getName().startswith("$d.");
315*85868e8aSDimitry Andric   };
316*85868e8aSDimitry Andric 
317*85868e8aSDimitry Andric   // Collect mapping symbols for every executable InputSection.
318*85868e8aSDimitry Andric   for (InputFile *file : objectFiles) {
319*85868e8aSDimitry Andric     auto *f = cast<ObjFile<ELF32LE>>(file);
320*85868e8aSDimitry Andric     for (Symbol *s : f->getLocalSymbols()) {
321*85868e8aSDimitry Andric       auto *def = dyn_cast<Defined>(s);
322*85868e8aSDimitry Andric       if (!def)
323*85868e8aSDimitry Andric         continue;
324*85868e8aSDimitry Andric       if (!isArmMapSymbol(def) && !isThumbMapSymbol(def) &&
325*85868e8aSDimitry Andric           !isDataMapSymbol(def))
326*85868e8aSDimitry Andric         continue;
327*85868e8aSDimitry Andric       if (auto *sec = dyn_cast_or_null<InputSection>(def->section))
328*85868e8aSDimitry Andric         if (sec->flags & SHF_EXECINSTR)
329*85868e8aSDimitry Andric           sectionMap[sec].push_back(def);
330*85868e8aSDimitry Andric     }
331*85868e8aSDimitry Andric   }
332*85868e8aSDimitry Andric   // For each InputSection make sure the mapping symbols are in sorted in
333*85868e8aSDimitry Andric   // ascending order and are in alternating Thumb, non-Thumb order.
334*85868e8aSDimitry Andric   for (auto &kv : sectionMap) {
335*85868e8aSDimitry Andric     std::vector<const Defined *> &mapSyms = kv.second;
336*85868e8aSDimitry Andric     llvm::stable_sort(mapSyms, [](const Defined *a, const Defined *b) {
337*85868e8aSDimitry Andric       return a->value < b->value;
338*85868e8aSDimitry Andric     });
339*85868e8aSDimitry Andric     mapSyms.erase(std::unique(mapSyms.begin(), mapSyms.end(),
340*85868e8aSDimitry Andric                               [=](const Defined *a, const Defined *b) {
341*85868e8aSDimitry Andric                                 return (isThumbMapSymbol(a) ==
342*85868e8aSDimitry Andric                                         isThumbMapSymbol(b));
343*85868e8aSDimitry Andric                               }),
344*85868e8aSDimitry Andric                   mapSyms.end());
345*85868e8aSDimitry Andric     // Always start with a Thumb Mapping Symbol
346*85868e8aSDimitry Andric     if (!mapSyms.empty() && !isThumbMapSymbol(mapSyms.front()))
347*85868e8aSDimitry Andric       mapSyms.erase(mapSyms.begin());
348*85868e8aSDimitry Andric   }
349*85868e8aSDimitry Andric   initialized = true;
350*85868e8aSDimitry Andric }
351*85868e8aSDimitry Andric 
352*85868e8aSDimitry Andric void ARMErr657417Patcher::insertPatches(
353*85868e8aSDimitry Andric     InputSectionDescription &isd, std::vector<Patch657417Section *> &patches) {
354*85868e8aSDimitry Andric   uint64_t spacing = 0x100000 - 0x7500;
355*85868e8aSDimitry Andric   uint64_t isecLimit;
356*85868e8aSDimitry Andric   uint64_t prevIsecLimit = isd.sections.front()->outSecOff;
357*85868e8aSDimitry Andric   uint64_t patchUpperBound = prevIsecLimit + spacing;
358*85868e8aSDimitry Andric   uint64_t outSecAddr = isd.sections.front()->getParent()->addr;
359*85868e8aSDimitry Andric 
360*85868e8aSDimitry Andric   // Set the outSecOff of patches to the place where we want to insert them.
361*85868e8aSDimitry Andric   // We use a similar strategy to initial thunk placement, using 1 MiB as the
362*85868e8aSDimitry Andric   // range of the Thumb-2 conditional branch with a contingency accounting for
363*85868e8aSDimitry Andric   // thunk generation.
364*85868e8aSDimitry Andric   auto patchIt = patches.begin();
365*85868e8aSDimitry Andric   auto patchEnd = patches.end();
366*85868e8aSDimitry Andric   for (const InputSection *isec : isd.sections) {
367*85868e8aSDimitry Andric     isecLimit = isec->outSecOff + isec->getSize();
368*85868e8aSDimitry Andric     if (isecLimit > patchUpperBound) {
369*85868e8aSDimitry Andric       for (; patchIt != patchEnd; ++patchIt) {
370*85868e8aSDimitry Andric         if ((*patchIt)->getBranchAddr() - outSecAddr >= prevIsecLimit)
371*85868e8aSDimitry Andric           break;
372*85868e8aSDimitry Andric         (*patchIt)->outSecOff = prevIsecLimit;
373*85868e8aSDimitry Andric       }
374*85868e8aSDimitry Andric       patchUpperBound = prevIsecLimit + spacing;
375*85868e8aSDimitry Andric     }
376*85868e8aSDimitry Andric     prevIsecLimit = isecLimit;
377*85868e8aSDimitry Andric   }
378*85868e8aSDimitry Andric   for (; patchIt != patchEnd; ++patchIt)
379*85868e8aSDimitry Andric     (*patchIt)->outSecOff = isecLimit;
380*85868e8aSDimitry Andric 
381*85868e8aSDimitry Andric   // Merge all patch sections. We use the outSecOff assigned above to
382*85868e8aSDimitry Andric   // determine the insertion point. This is ok as we only merge into an
383*85868e8aSDimitry Andric   // InputSectionDescription once per pass, and at the end of the pass
384*85868e8aSDimitry Andric   // assignAddresses() will recalculate all the outSecOff values.
385*85868e8aSDimitry Andric   std::vector<InputSection *> tmp;
386*85868e8aSDimitry Andric   tmp.reserve(isd.sections.size() + patches.size());
387*85868e8aSDimitry Andric   auto mergeCmp = [](const InputSection *a, const InputSection *b) {
388*85868e8aSDimitry Andric     if (a->outSecOff != b->outSecOff)
389*85868e8aSDimitry Andric       return a->outSecOff < b->outSecOff;
390*85868e8aSDimitry Andric     return isa<Patch657417Section>(a) && !isa<Patch657417Section>(b);
391*85868e8aSDimitry Andric   };
392*85868e8aSDimitry Andric   std::merge(isd.sections.begin(), isd.sections.end(), patches.begin(),
393*85868e8aSDimitry Andric              patches.end(), std::back_inserter(tmp), mergeCmp);
394*85868e8aSDimitry Andric   isd.sections = std::move(tmp);
395*85868e8aSDimitry Andric }
396*85868e8aSDimitry Andric 
397*85868e8aSDimitry Andric // Given a branch instruction described by ScanRes redirect it to a patch
398*85868e8aSDimitry Andric // section containing an unconditional branch instruction to the target.
399*85868e8aSDimitry Andric // Ensure that this patch section is 4-byte aligned so that the branch cannot
400*85868e8aSDimitry Andric // span two 4 KiB regions. Place the patch section so that it is always after
401*85868e8aSDimitry Andric // isec so the branch we are patching always goes forwards.
402*85868e8aSDimitry Andric static void implementPatch(ScanResult sr, InputSection *isec,
403*85868e8aSDimitry Andric                            std::vector<Patch657417Section *> &patches) {
404*85868e8aSDimitry Andric 
405*85868e8aSDimitry Andric   log("detected cortex-a8-657419 erratum sequence starting at " +
406*85868e8aSDimitry Andric       utohexstr(isec->getVA(sr.off)) + " in unpatched output.");
407*85868e8aSDimitry Andric   Patch657417Section *psec;
408*85868e8aSDimitry Andric   // We have two cases to deal with.
409*85868e8aSDimitry Andric   // Case 1. There is a relocation at patcheeOffset to a symbol. The
410*85868e8aSDimitry Andric   // unconditional branch in the patch must have a relocation so that any
411*85868e8aSDimitry Andric   // further redirection via the PLT or a Thunk happens as normal. At
412*85868e8aSDimitry Andric   // patcheeOffset we redirect the existing relocation to a Symbol defined at
413*85868e8aSDimitry Andric   // the start of the patch section.
414*85868e8aSDimitry Andric   //
415*85868e8aSDimitry Andric   // Case 2. There is no relocation at patcheeOffset. We are unlikely to have
416*85868e8aSDimitry Andric   // a symbol that we can use as a target for a relocation in the patch section.
417*85868e8aSDimitry Andric   // Luckily we know that the destination cannot be indirected via the PLT or
418*85868e8aSDimitry Andric   // a Thunk so we can just write the destination directly.
419*85868e8aSDimitry Andric   if (sr.rel) {
420*85868e8aSDimitry Andric     // Case 1. We have an existing relocation to redirect to patch and a
421*85868e8aSDimitry Andric     // Symbol target.
422*85868e8aSDimitry Andric 
423*85868e8aSDimitry Andric     // Create a branch relocation for the unconditional branch in the patch.
424*85868e8aSDimitry Andric     // This can be redirected via the PLT or Thunks.
425*85868e8aSDimitry Andric     RelType patchRelType = R_ARM_THM_JUMP24;
426*85868e8aSDimitry Andric     int64_t patchRelAddend = sr.rel->addend;
427*85868e8aSDimitry Andric     bool destIsARM = false;
428*85868e8aSDimitry Andric     if (isBL(sr.instr) || isBLX(sr.instr)) {
429*85868e8aSDimitry Andric       // The final target of the branch may be ARM or Thumb, if the target
430*85868e8aSDimitry Andric       // is ARM then we write the patch in ARM state to avoid a state change
431*85868e8aSDimitry Andric       // Thunk from the patch to the target.
432*85868e8aSDimitry Andric       uint64_t dstSymAddr = (sr.rel->expr == R_PLT_PC) ? sr.rel->sym->getPltVA()
433*85868e8aSDimitry Andric                                                        : sr.rel->sym->getVA();
434*85868e8aSDimitry Andric       destIsARM = (dstSymAddr & 1) == 0;
435*85868e8aSDimitry Andric     }
436*85868e8aSDimitry Andric     psec = make<Patch657417Section>(isec, sr.off, sr.instr, destIsARM);
437*85868e8aSDimitry Andric     if (destIsARM) {
438*85868e8aSDimitry Andric       // The patch will be in ARM state. Use an ARM relocation and account for
439*85868e8aSDimitry Andric       // the larger ARM PC-bias of 8 rather than Thumb's 4.
440*85868e8aSDimitry Andric       patchRelType = R_ARM_JUMP24;
441*85868e8aSDimitry Andric       patchRelAddend -= 4;
442*85868e8aSDimitry Andric     }
443*85868e8aSDimitry Andric     psec->relocations.push_back(
444*85868e8aSDimitry Andric         Relocation{sr.rel->expr, patchRelType, 0, patchRelAddend, sr.rel->sym});
445*85868e8aSDimitry Andric     // Redirect the existing branch relocation to the patch.
446*85868e8aSDimitry Andric     sr.rel->expr = R_PC;
447*85868e8aSDimitry Andric     sr.rel->addend = -4;
448*85868e8aSDimitry Andric     sr.rel->sym = psec->patchSym;
449*85868e8aSDimitry Andric   } else {
450*85868e8aSDimitry Andric     // Case 2. We do not have a relocation to the patch. Add a relocation of the
451*85868e8aSDimitry Andric     // appropriate type to the patch at patcheeOffset.
452*85868e8aSDimitry Andric 
453*85868e8aSDimitry Andric     // The destination is ARM if we have a BLX.
454*85868e8aSDimitry Andric     psec = make<Patch657417Section>(isec, sr.off, sr.instr, isBLX(sr.instr));
455*85868e8aSDimitry Andric     RelType type;
456*85868e8aSDimitry Andric     if (isBcc(sr.instr))
457*85868e8aSDimitry Andric       type = R_ARM_THM_JUMP19;
458*85868e8aSDimitry Andric     else if (isB(sr.instr))
459*85868e8aSDimitry Andric       type = R_ARM_THM_JUMP24;
460*85868e8aSDimitry Andric     else
461*85868e8aSDimitry Andric       type = R_ARM_THM_CALL;
462*85868e8aSDimitry Andric     isec->relocations.push_back(
463*85868e8aSDimitry Andric         Relocation{R_PC, type, sr.off, -4, psec->patchSym});
464*85868e8aSDimitry Andric   }
465*85868e8aSDimitry Andric   patches.push_back(psec);
466*85868e8aSDimitry Andric }
467*85868e8aSDimitry Andric 
468*85868e8aSDimitry Andric // Scan all the instructions in InputSectionDescription, for each instance of
469*85868e8aSDimitry Andric // the erratum sequence create a Patch657417Section. We return the list of
470*85868e8aSDimitry Andric // Patch657417Sections that need to be applied to the InputSectionDescription.
471*85868e8aSDimitry Andric std::vector<Patch657417Section *>
472*85868e8aSDimitry Andric ARMErr657417Patcher::patchInputSectionDescription(
473*85868e8aSDimitry Andric     InputSectionDescription &isd) {
474*85868e8aSDimitry Andric   std::vector<Patch657417Section *> patches;
475*85868e8aSDimitry Andric   for (InputSection *isec : isd.sections) {
476*85868e8aSDimitry Andric     // LLD doesn't use the erratum sequence in SyntheticSections.
477*85868e8aSDimitry Andric     if (isa<SyntheticSection>(isec))
478*85868e8aSDimitry Andric       continue;
479*85868e8aSDimitry Andric     // Use sectionMap to make sure we only scan Thumb code and not Arm or inline
480*85868e8aSDimitry Andric     // data. We have already sorted mapSyms in ascending order and removed
481*85868e8aSDimitry Andric     // consecutive mapping symbols of the same type. Our range of executable
482*85868e8aSDimitry Andric     // instructions to scan is therefore [thumbSym->value, nonThumbSym->value)
483*85868e8aSDimitry Andric     // or [thumbSym->value, section size).
484*85868e8aSDimitry Andric     std::vector<const Defined *> &mapSyms = sectionMap[isec];
485*85868e8aSDimitry Andric 
486*85868e8aSDimitry Andric     auto thumbSym = mapSyms.begin();
487*85868e8aSDimitry Andric     while (thumbSym != mapSyms.end()) {
488*85868e8aSDimitry Andric       auto nonThumbSym = std::next(thumbSym);
489*85868e8aSDimitry Andric       uint64_t off = (*thumbSym)->value;
490*85868e8aSDimitry Andric       uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->data().size()
491*85868e8aSDimitry Andric                                                       : (*nonThumbSym)->value;
492*85868e8aSDimitry Andric 
493*85868e8aSDimitry Andric       while (off < limit) {
494*85868e8aSDimitry Andric         ScanResult sr = scanCortexA8Errata657417(isec, off, limit);
495*85868e8aSDimitry Andric         if (sr.off)
496*85868e8aSDimitry Andric           implementPatch(sr, isec, patches);
497*85868e8aSDimitry Andric       }
498*85868e8aSDimitry Andric       if (nonThumbSym == mapSyms.end())
499*85868e8aSDimitry Andric         break;
500*85868e8aSDimitry Andric       thumbSym = std::next(nonThumbSym);
501*85868e8aSDimitry Andric     }
502*85868e8aSDimitry Andric   }
503*85868e8aSDimitry Andric   return patches;
504*85868e8aSDimitry Andric }
505*85868e8aSDimitry Andric 
506*85868e8aSDimitry Andric bool ARMErr657417Patcher::createFixes() {
507*85868e8aSDimitry Andric   if (!initialized)
508*85868e8aSDimitry Andric     init();
509*85868e8aSDimitry Andric 
510*85868e8aSDimitry Andric   bool addressesChanged = false;
511*85868e8aSDimitry Andric   for (OutputSection *os : outputSections) {
512*85868e8aSDimitry Andric     if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))
513*85868e8aSDimitry Andric       continue;
514*85868e8aSDimitry Andric     for (BaseCommand *bc : os->sectionCommands)
515*85868e8aSDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {
516*85868e8aSDimitry Andric         std::vector<Patch657417Section *> patches =
517*85868e8aSDimitry Andric             patchInputSectionDescription(*isd);
518*85868e8aSDimitry Andric         if (!patches.empty()) {
519*85868e8aSDimitry Andric           insertPatches(*isd, patches);
520*85868e8aSDimitry Andric           addressesChanged = true;
521*85868e8aSDimitry Andric         }
522*85868e8aSDimitry Andric       }
523*85868e8aSDimitry Andric   }
524*85868e8aSDimitry Andric   return addressesChanged;
525*85868e8aSDimitry Andric }
526*85868e8aSDimitry Andric 
527*85868e8aSDimitry Andric } // namespace elf
528*85868e8aSDimitry Andric } // namespace lld
529