xref: /freebsd/contrib/llvm-project/lld/ELF/AArch64ErrataFix.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- AArch64ErrataFix.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // This file implements Section Patching for the purpose of working around
985868e8aSDimitry Andric // the AArch64 Cortex-53 errata 843419 that affects r0p0, r0p1, r0p2 and r0p4
1085868e8aSDimitry Andric // versions of the core.
1185868e8aSDimitry Andric //
1285868e8aSDimitry Andric // The general principle is that an erratum sequence of one or
130b57cec5SDimitry Andric // more instructions is detected in the instruction stream, one of the
140b57cec5SDimitry Andric // instructions in the sequence is replaced with a branch to a patch sequence
150b57cec5SDimitry Andric // of replacement instructions. At the end of the replacement sequence the
160b57cec5SDimitry Andric // patch branches back to the instruction stream.
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric // This technique is only suitable for fixing an erratum when:
190b57cec5SDimitry Andric // - There is a set of necessary conditions required to trigger the erratum that
200b57cec5SDimitry Andric // can be detected at static link time.
210b57cec5SDimitry Andric // - There is a set of replacement instructions that can be used to remove at
220b57cec5SDimitry Andric // least one of the necessary conditions that trigger the erratum.
230b57cec5SDimitry Andric // - We can overwrite an instruction in the erratum sequence with a branch to
240b57cec5SDimitry Andric // the replacement sequence.
250b57cec5SDimitry Andric // - We can place the replacement sequence within range of the branch.
260b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #include "AArch64ErrataFix.h"
2981ad6265SDimitry Andric #include "InputFiles.h"
300b57cec5SDimitry Andric #include "LinkerScript.h"
310b57cec5SDimitry Andric #include "OutputSections.h"
320b57cec5SDimitry Andric #include "Relocations.h"
330b57cec5SDimitry Andric #include "Symbols.h"
340b57cec5SDimitry Andric #include "SyntheticSections.h"
350b57cec5SDimitry Andric #include "Target.h"
3604eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
370b57cec5SDimitry Andric #include "lld/Common/Strings.h"
38*06c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
390b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
400b57cec5SDimitry Andric #include <algorithm>
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric using namespace llvm;
430b57cec5SDimitry Andric using namespace llvm::ELF;
440b57cec5SDimitry Andric using namespace llvm::object;
450b57cec5SDimitry Andric using namespace llvm::support;
460b57cec5SDimitry Andric using namespace llvm::support::endian;
475ffd83dbSDimitry Andric using namespace lld;
485ffd83dbSDimitry Andric using namespace lld::elf;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // Helper functions to identify instructions and conditions needed to trigger
510b57cec5SDimitry Andric // the Cortex-A53-843419 erratum.
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric // ADRP
540b57cec5SDimitry Andric // | 1 | immlo (2) | 1 | 0 0 0 0 | immhi (19) | Rd (5) |
isADRP(uint32_t instr)550b57cec5SDimitry Andric static bool isADRP(uint32_t instr) {
560b57cec5SDimitry Andric   return (instr & 0x9f000000) == 0x90000000;
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric 
59bdd1243dSDimitry Andric // Load and store bit patterns from ARMv8-A.
600b57cec5SDimitry Andric // Instructions appear in order of appearance starting from table in
610b57cec5SDimitry Andric // C4.1.3 Loads and Stores.
620b57cec5SDimitry Andric 
63480093f4SDimitry Andric // All loads and stores have 1 (at bit position 27), (0 at bit position 25).
640b57cec5SDimitry Andric // | op0 x op1 (2) | 1 op2 0 op3 (2) | x | op4 (5) | xxxx | op5 (2) | x (10) |
isLoadStoreClass(uint32_t instr)650b57cec5SDimitry Andric static bool isLoadStoreClass(uint32_t instr) {
660b57cec5SDimitry Andric   return (instr & 0x0a000000) == 0x08000000;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric // LDN/STN multiple no offset
700b57cec5SDimitry Andric // | 0 Q 00 | 1100 | 0 L 00 | 0000 | opcode (4) | size (2) | Rn (5) | Rt (5) |
710b57cec5SDimitry Andric // LDN/STN multiple post-indexed
720b57cec5SDimitry Andric // | 0 Q 00 | 1100 | 1 L 0 | Rm (5)| opcode (4) | size (2) | Rn (5) | Rt (5) |
730b57cec5SDimitry Andric // L == 0 for stores.
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric // Utility routine to decode opcode field of LDN/STN multiple structure
760b57cec5SDimitry Andric // instructions to find the ST1 instructions.
770b57cec5SDimitry Andric // opcode == 0010 ST1 4 registers.
780b57cec5SDimitry Andric // opcode == 0110 ST1 3 registers.
790b57cec5SDimitry Andric // opcode == 0111 ST1 1 register.
800b57cec5SDimitry Andric // opcode == 1010 ST1 2 registers.
isST1MultipleOpcode(uint32_t instr)810b57cec5SDimitry Andric static bool isST1MultipleOpcode(uint32_t instr) {
820b57cec5SDimitry Andric   return (instr & 0x0000f000) == 0x00002000 ||
830b57cec5SDimitry Andric          (instr & 0x0000f000) == 0x00006000 ||
840b57cec5SDimitry Andric          (instr & 0x0000f000) == 0x00007000 ||
850b57cec5SDimitry Andric          (instr & 0x0000f000) == 0x0000a000;
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
isST1Multiple(uint32_t instr)880b57cec5SDimitry Andric static bool isST1Multiple(uint32_t instr) {
890b57cec5SDimitry Andric   return (instr & 0xbfff0000) == 0x0c000000 && isST1MultipleOpcode(instr);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric // Writes to Rn (writeback).
isST1MultiplePost(uint32_t instr)930b57cec5SDimitry Andric static bool isST1MultiplePost(uint32_t instr) {
940b57cec5SDimitry Andric   return (instr & 0xbfe00000) == 0x0c800000 && isST1MultipleOpcode(instr);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric // LDN/STN single no offset
980b57cec5SDimitry Andric // | 0 Q 00 | 1101 | 0 L R 0 | 0000 | opc (3) S | size (2) | Rn (5) | Rt (5)|
990b57cec5SDimitry Andric // LDN/STN single post-indexed
1000b57cec5SDimitry Andric // | 0 Q 00 | 1101 | 1 L R | Rm (5) | opc (3) S | size (2) | Rn (5) | Rt (5)|
1010b57cec5SDimitry Andric // L == 0 for stores
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric // Utility routine to decode opcode field of LDN/STN single structure
1040b57cec5SDimitry Andric // instructions to find the ST1 instructions.
1050b57cec5SDimitry Andric // R == 0 for ST1 and ST3, R == 1 for ST2 and ST4.
1060b57cec5SDimitry Andric // opcode == 000 ST1 8-bit.
1070b57cec5SDimitry Andric // opcode == 010 ST1 16-bit.
1080b57cec5SDimitry Andric // opcode == 100 ST1 32 or 64-bit (Size determines which).
isST1SingleOpcode(uint32_t instr)1090b57cec5SDimitry Andric static bool isST1SingleOpcode(uint32_t instr) {
1100b57cec5SDimitry Andric   return (instr & 0x0040e000) == 0x00000000 ||
1110b57cec5SDimitry Andric          (instr & 0x0040e000) == 0x00004000 ||
1120b57cec5SDimitry Andric          (instr & 0x0040e000) == 0x00008000;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
isST1Single(uint32_t instr)1150b57cec5SDimitry Andric static bool isST1Single(uint32_t instr) {
1160b57cec5SDimitry Andric   return (instr & 0xbfff0000) == 0x0d000000 && isST1SingleOpcode(instr);
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric // Writes to Rn (writeback).
isST1SinglePost(uint32_t instr)1200b57cec5SDimitry Andric static bool isST1SinglePost(uint32_t instr) {
1210b57cec5SDimitry Andric   return (instr & 0xbfe00000) == 0x0d800000 && isST1SingleOpcode(instr);
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
isST1(uint32_t instr)1240b57cec5SDimitry Andric static bool isST1(uint32_t instr) {
1250b57cec5SDimitry Andric   return isST1Multiple(instr) || isST1MultiplePost(instr) ||
1260b57cec5SDimitry Andric          isST1Single(instr) || isST1SinglePost(instr);
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric // Load/store exclusive
1300b57cec5SDimitry Andric // | size (2) 00 | 1000 | o2 L o1 | Rs (5) | o0 | Rt2 (5) | Rn (5) | Rt (5) |
1310b57cec5SDimitry Andric // L == 0 for Stores.
isLoadStoreExclusive(uint32_t instr)1320b57cec5SDimitry Andric static bool isLoadStoreExclusive(uint32_t instr) {
1330b57cec5SDimitry Andric   return (instr & 0x3f000000) == 0x08000000;
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
isLoadExclusive(uint32_t instr)1360b57cec5SDimitry Andric static bool isLoadExclusive(uint32_t instr) {
1370b57cec5SDimitry Andric   return (instr & 0x3f400000) == 0x08400000;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric // Load register literal
1410b57cec5SDimitry Andric // | opc (2) 01 | 1 V 00 | imm19 | Rt (5) |
isLoadLiteral(uint32_t instr)1420b57cec5SDimitry Andric static bool isLoadLiteral(uint32_t instr) {
1430b57cec5SDimitry Andric   return (instr & 0x3b000000) == 0x18000000;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric // Load/store no-allocate pair
1470b57cec5SDimitry Andric // (offset)
1480b57cec5SDimitry Andric // | opc (2) 10 | 1 V 00 | 0 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |
1490b57cec5SDimitry Andric // L == 0 for stores.
1500b57cec5SDimitry Andric // Never writes to register
isSTNP(uint32_t instr)1510b57cec5SDimitry Andric static bool isSTNP(uint32_t instr) {
1520b57cec5SDimitry Andric   return (instr & 0x3bc00000) == 0x28000000;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric // Load/store register pair
1560b57cec5SDimitry Andric // (post-indexed)
1570b57cec5SDimitry Andric // | opc (2) 10 | 1 V 00 | 1 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |
1580b57cec5SDimitry Andric // L == 0 for stores, V == 0 for Scalar, V == 1 for Simd/FP
1590b57cec5SDimitry Andric // Writes to Rn.
isSTPPost(uint32_t instr)1600b57cec5SDimitry Andric static bool isSTPPost(uint32_t instr) {
1610b57cec5SDimitry Andric   return (instr & 0x3bc00000) == 0x28800000;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric // (offset)
1650b57cec5SDimitry Andric // | opc (2) 10 | 1 V 01 | 0 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |
isSTPOffset(uint32_t instr)1660b57cec5SDimitry Andric static bool isSTPOffset(uint32_t instr) {
1670b57cec5SDimitry Andric   return (instr & 0x3bc00000) == 0x29000000;
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric // (pre-index)
1710b57cec5SDimitry Andric // | opc (2) 10 | 1 V 01 | 1 L | imm7 | Rt2 (5) | Rn (5) | Rt (5) |
1720b57cec5SDimitry Andric // Writes to Rn.
isSTPPre(uint32_t instr)1730b57cec5SDimitry Andric static bool isSTPPre(uint32_t instr) {
1740b57cec5SDimitry Andric   return (instr & 0x3bc00000) == 0x29800000;
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
isSTP(uint32_t instr)1770b57cec5SDimitry Andric static bool isSTP(uint32_t instr) {
1780b57cec5SDimitry Andric   return isSTPPost(instr) || isSTPOffset(instr) || isSTPPre(instr);
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric // Load/store register (unscaled immediate)
1820b57cec5SDimitry Andric // | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 00 | Rn (5) | Rt (5) |
1830b57cec5SDimitry Andric // V == 0 for Scalar, V == 1 for Simd/FP.
isLoadStoreUnscaled(uint32_t instr)1840b57cec5SDimitry Andric static bool isLoadStoreUnscaled(uint32_t instr) {
1850b57cec5SDimitry Andric   return (instr & 0x3b000c00) == 0x38000000;
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric // Load/store register (immediate post-indexed)
1890b57cec5SDimitry Andric // | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 01 | Rn (5) | Rt (5) |
isLoadStoreImmediatePost(uint32_t instr)1900b57cec5SDimitry Andric static bool isLoadStoreImmediatePost(uint32_t instr) {
1910b57cec5SDimitry Andric   return (instr & 0x3b200c00) == 0x38000400;
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric // Load/store register (unprivileged)
1950b57cec5SDimitry Andric // | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 10 | Rn (5) | Rt (5) |
isLoadStoreUnpriv(uint32_t instr)1960b57cec5SDimitry Andric static bool isLoadStoreUnpriv(uint32_t instr) {
1970b57cec5SDimitry Andric   return (instr & 0x3b200c00) == 0x38000800;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric // Load/store register (immediate pre-indexed)
2010b57cec5SDimitry Andric // | size (2) 11 | 1 V 00 | opc (2) 0 | imm9 | 11 | Rn (5) | Rt (5) |
isLoadStoreImmediatePre(uint32_t instr)2020b57cec5SDimitry Andric static bool isLoadStoreImmediatePre(uint32_t instr) {
2030b57cec5SDimitry Andric   return (instr & 0x3b200c00) == 0x38000c00;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric // Load/store register (register offset)
2070b57cec5SDimitry Andric // | size (2) 11 | 1 V 00 | opc (2) 1 | Rm (5) | option (3) S | 10 | Rn | Rt |
isLoadStoreRegisterOff(uint32_t instr)2080b57cec5SDimitry Andric static bool isLoadStoreRegisterOff(uint32_t instr) {
2090b57cec5SDimitry Andric   return (instr & 0x3b200c00) == 0x38200800;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric // Load/store register (unsigned immediate)
2130b57cec5SDimitry Andric // | size (2) 11 | 1 V 01 | opc (2) | imm12 | Rn (5) | Rt (5) |
isLoadStoreRegisterUnsigned(uint32_t instr)2140b57cec5SDimitry Andric static bool isLoadStoreRegisterUnsigned(uint32_t instr) {
2150b57cec5SDimitry Andric   return (instr & 0x3b000000) == 0x39000000;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric // Rt is always in bit position 0 - 4.
getRt(uint32_t instr)2190b57cec5SDimitry Andric static uint32_t getRt(uint32_t instr) { return (instr & 0x1f); }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric // Rn is always in bit position 5 - 9.
getRn(uint32_t instr)2220b57cec5SDimitry Andric static uint32_t getRn(uint32_t instr) { return (instr >> 5) & 0x1f; }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric // C4.1.2 Branches, Exception Generating and System instructions
2250b57cec5SDimitry Andric // | op0 (3) 1 | 01 op1 (4) | x (22) |
2260b57cec5SDimitry Andric // op0 == 010 101 op1 == 0xxx Conditional Branch.
2270b57cec5SDimitry Andric // op0 == 110 101 op1 == 1xxx Unconditional Branch Register.
2280b57cec5SDimitry Andric // op0 == x00 101 op1 == xxxx Unconditional Branch immediate.
2290b57cec5SDimitry Andric // op0 == x01 101 op1 == 0xxx Compare and branch immediate.
2300b57cec5SDimitry Andric // op0 == x01 101 op1 == 1xxx Test and branch immediate.
isBranch(uint32_t instr)2310b57cec5SDimitry Andric static bool isBranch(uint32_t instr) {
2320b57cec5SDimitry Andric   return ((instr & 0xfe000000) == 0xd6000000) || // Cond branch.
2330b57cec5SDimitry Andric          ((instr & 0xfe000000) == 0x54000000) || // Uncond branch reg.
2340b57cec5SDimitry Andric          ((instr & 0x7c000000) == 0x14000000) || // Uncond branch imm.
2350b57cec5SDimitry Andric          ((instr & 0x7c000000) == 0x34000000);   // Compare and test branch.
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
isV8SingleRegisterNonStructureLoadStore(uint32_t instr)2380b57cec5SDimitry Andric static bool isV8SingleRegisterNonStructureLoadStore(uint32_t instr) {
2390b57cec5SDimitry Andric   return isLoadStoreUnscaled(instr) || isLoadStoreImmediatePost(instr) ||
2400b57cec5SDimitry Andric          isLoadStoreUnpriv(instr) || isLoadStoreImmediatePre(instr) ||
2410b57cec5SDimitry Andric          isLoadStoreRegisterOff(instr) || isLoadStoreRegisterUnsigned(instr);
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric // Note that this function refers to v8.0 only and does not include the
2450b57cec5SDimitry Andric // additional load and store instructions added for in later revisions of
2460b57cec5SDimitry Andric // the architecture such as the Atomic memory operations introduced
2470b57cec5SDimitry Andric // in v8.1.
isV8NonStructureLoad(uint32_t instr)2480b57cec5SDimitry Andric static bool isV8NonStructureLoad(uint32_t instr) {
2490b57cec5SDimitry Andric   if (isLoadExclusive(instr))
2500b57cec5SDimitry Andric     return true;
2510b57cec5SDimitry Andric   if (isLoadLiteral(instr))
2520b57cec5SDimitry Andric     return true;
2530b57cec5SDimitry Andric   else if (isV8SingleRegisterNonStructureLoadStore(instr)) {
2540b57cec5SDimitry Andric     // For Load and Store single register, Loads are derived from a
2550b57cec5SDimitry Andric     // combination of the Size, V and Opc fields.
2560b57cec5SDimitry Andric     uint32_t size = (instr >> 30) & 0xff;
2570b57cec5SDimitry Andric     uint32_t v = (instr >> 26) & 0x1;
2580b57cec5SDimitry Andric     uint32_t opc = (instr >> 22) & 0x3;
2590b57cec5SDimitry Andric     // For the load and store instructions that we are decoding.
2600b57cec5SDimitry Andric     // Opc == 0 are all stores.
2610b57cec5SDimitry Andric     // Opc == 1 with a couple of exceptions are loads. The exceptions are:
2620b57cec5SDimitry Andric     // Size == 00 (0), V == 1, Opc == 10 (2) which is a store and
2630b57cec5SDimitry Andric     // Size == 11 (3), V == 0, Opc == 10 (2) which is a prefetch.
2640b57cec5SDimitry Andric     return opc != 0 && !(size == 0 && v == 1 && opc == 2) &&
2650b57cec5SDimitry Andric            !(size == 3 && v == 0 && opc == 2);
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric   return false;
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric // The following decode instructions are only complete up to the instructions
2710b57cec5SDimitry Andric // needed for errata 843419.
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric // Instruction with writeback updates the index register after the load/store.
hasWriteback(uint32_t instr)2740b57cec5SDimitry Andric static bool hasWriteback(uint32_t instr) {
2750b57cec5SDimitry Andric   return isLoadStoreImmediatePre(instr) || isLoadStoreImmediatePost(instr) ||
2760b57cec5SDimitry Andric          isSTPPre(instr) || isSTPPost(instr) || isST1SinglePost(instr) ||
2770b57cec5SDimitry Andric          isST1MultiplePost(instr);
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric // For the load and store class of instructions, a load can write to the
2810b57cec5SDimitry Andric // destination register, a load and a store can write to the base register when
2820b57cec5SDimitry Andric // the instruction has writeback.
doesLoadStoreWriteToReg(uint32_t instr,uint32_t reg)2830b57cec5SDimitry Andric static bool doesLoadStoreWriteToReg(uint32_t instr, uint32_t reg) {
2840b57cec5SDimitry Andric   return (isV8NonStructureLoad(instr) && getRt(instr) == reg) ||
2850b57cec5SDimitry Andric          (hasWriteback(instr) && getRn(instr) == reg);
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric // Scanner for Cortex-A53 errata 843419
2890b57cec5SDimitry Andric // Full details are available in the Cortex A53 MPCore revision 0 Software
2900b57cec5SDimitry Andric // Developers Errata Notice (ARM-EPM-048406).
2910b57cec5SDimitry Andric //
2920b57cec5SDimitry Andric // The instruction sequence that triggers the erratum is common in compiled
2930b57cec5SDimitry Andric // AArch64 code, however it is sensitive to the offset of the sequence within
2940b57cec5SDimitry Andric // a 4k page. This means that by scanning and fixing the patch after we have
2950b57cec5SDimitry Andric // assigned addresses we only need to disassemble and fix instances of the
2960b57cec5SDimitry Andric // sequence in the range of affected offsets.
2970b57cec5SDimitry Andric //
2980b57cec5SDimitry Andric // In summary the erratum conditions are a series of 4 instructions:
2990b57cec5SDimitry Andric // 1.) An ADRP instruction that writes to register Rn with low 12 bits of
3000b57cec5SDimitry Andric //     address of instruction either 0xff8 or 0xffc.
3010b57cec5SDimitry Andric // 2.) A load or store instruction that can be:
3020b57cec5SDimitry Andric // - A single register load or store, of either integer or vector registers.
3030b57cec5SDimitry Andric // - An STP or STNP, of either integer or vector registers.
3040b57cec5SDimitry Andric // - An Advanced SIMD ST1 store instruction.
3050b57cec5SDimitry Andric // - Must not write to Rn, but may optionally read from it.
3060b57cec5SDimitry Andric // 3.) An optional instruction that is not a branch and does not write to Rn.
3070b57cec5SDimitry Andric // 4.) A load or store from the  Load/store register (unsigned immediate) class
3080b57cec5SDimitry Andric //     that uses Rn as the base address register.
3090b57cec5SDimitry Andric //
3100b57cec5SDimitry Andric // Note that we do not attempt to scan for Sequence 2 as described in the
3110b57cec5SDimitry Andric // Software Developers Errata Notice as this has been assessed to be extremely
3120b57cec5SDimitry Andric // unlikely to occur in compiled code. This matches gold and ld.bfd behavior.
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric // Return true if the Instruction sequence Adrp, Instr2, and Instr4 match
3150b57cec5SDimitry Andric // the erratum sequence. The Adrp, Instr2 and Instr4 correspond to 1.), 2.),
3160b57cec5SDimitry Andric // and 4.) in the Scanner for Cortex-A53 errata comment above.
is843419ErratumSequence(uint32_t instr1,uint32_t instr2,uint32_t instr4)3170b57cec5SDimitry Andric static bool is843419ErratumSequence(uint32_t instr1, uint32_t instr2,
3180b57cec5SDimitry Andric                                     uint32_t instr4) {
3190b57cec5SDimitry Andric   if (!isADRP(instr1))
3200b57cec5SDimitry Andric     return false;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   uint32_t rn = getRt(instr1);
3230b57cec5SDimitry Andric   return isLoadStoreClass(instr2) &&
3240b57cec5SDimitry Andric          (isLoadStoreExclusive(instr2) || isLoadLiteral(instr2) ||
3250b57cec5SDimitry Andric           isV8SingleRegisterNonStructureLoadStore(instr2) || isSTP(instr2) ||
3260b57cec5SDimitry Andric           isSTNP(instr2) || isST1(instr2)) &&
3270b57cec5SDimitry Andric          !doesLoadStoreWriteToReg(instr2, rn) &&
3280b57cec5SDimitry Andric          isLoadStoreRegisterUnsigned(instr4) && getRn(instr4) == rn;
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric // Scan the instruction sequence starting at Offset Off from the base of
33285868e8aSDimitry Andric // InputSection isec. We update Off in this function rather than in the caller
33385868e8aSDimitry Andric // as we can skip ahead much further into the section when we know how many
3340b57cec5SDimitry Andric // instructions we've scanned.
33585868e8aSDimitry Andric // Return the offset of the load or store instruction in isec that we want to
3360b57cec5SDimitry Andric // patch or 0 if no patch required.
scanCortexA53Errata843419(InputSection * isec,uint64_t & off,uint64_t limit)3370b57cec5SDimitry Andric static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
3380b57cec5SDimitry Andric                                           uint64_t limit) {
3390b57cec5SDimitry Andric   uint64_t isecAddr = isec->getVA(0);
3400b57cec5SDimitry Andric 
34185868e8aSDimitry Andric   // Advance Off so that (isecAddr + Off) modulo 0x1000 is at least 0xff8.
3420b57cec5SDimitry Andric   uint64_t initialPageOff = (isecAddr + off) & 0xfff;
3430b57cec5SDimitry Andric   if (initialPageOff < 0xff8)
3440b57cec5SDimitry Andric     off += 0xff8 - initialPageOff;
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   bool optionalAllowed = limit - off > 12;
3470b57cec5SDimitry Andric   if (off >= limit || limit - off < 12) {
3480b57cec5SDimitry Andric     // Need at least 3 4-byte sized instructions to trigger erratum.
3490b57cec5SDimitry Andric     off = limit;
3500b57cec5SDimitry Andric     return 0;
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   uint64_t patchOff = 0;
354bdd1243dSDimitry Andric   const uint8_t *buf = isec->content().begin();
3550b57cec5SDimitry Andric   const ulittle32_t *instBuf = reinterpret_cast<const ulittle32_t *>(buf + off);
3560b57cec5SDimitry Andric   uint32_t instr1 = *instBuf++;
3570b57cec5SDimitry Andric   uint32_t instr2 = *instBuf++;
3580b57cec5SDimitry Andric   uint32_t instr3 = *instBuf++;
3590b57cec5SDimitry Andric   if (is843419ErratumSequence(instr1, instr2, instr3)) {
3600b57cec5SDimitry Andric     patchOff = off + 8;
3610b57cec5SDimitry Andric   } else if (optionalAllowed && !isBranch(instr3)) {
3620b57cec5SDimitry Andric     uint32_t instr4 = *instBuf++;
3630b57cec5SDimitry Andric     if (is843419ErratumSequence(instr1, instr2, instr4))
3640b57cec5SDimitry Andric       patchOff = off + 12;
3650b57cec5SDimitry Andric   }
3660b57cec5SDimitry Andric   if (((isecAddr + off) & 0xfff) == 0xff8)
3670b57cec5SDimitry Andric     off += 4;
3680b57cec5SDimitry Andric   else
3690b57cec5SDimitry Andric     off += 0xffc;
3700b57cec5SDimitry Andric   return patchOff;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
373bdd1243dSDimitry Andric class elf::Patch843419Section final : public SyntheticSection {
3740b57cec5SDimitry Andric public:
3750b57cec5SDimitry Andric   Patch843419Section(InputSection *p, uint64_t off);
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
3780b57cec5SDimitry Andric 
getSize() const3790b57cec5SDimitry Andric   size_t getSize() const override { return 8; }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   uint64_t getLDSTAddr() const;
3820b57cec5SDimitry Andric 
classof(const SectionBase * d)383480093f4SDimitry Andric   static bool classof(const SectionBase *d) {
384480093f4SDimitry Andric     return d->kind() == InputSectionBase::Synthetic && d->name == ".text.patch";
385480093f4SDimitry Andric   }
386480093f4SDimitry Andric 
3870b57cec5SDimitry Andric   // The Section we are patching.
3880b57cec5SDimitry Andric   const InputSection *patchee;
38985868e8aSDimitry Andric   // The offset of the instruction in the patchee section we are patching.
3900b57cec5SDimitry Andric   uint64_t patcheeOffset;
3910b57cec5SDimitry Andric   // A label for the start of the Patch that we can use as a relocation target.
3920b57cec5SDimitry Andric   Symbol *patchSym;
3930b57cec5SDimitry Andric };
3940b57cec5SDimitry Andric 
Patch843419Section(InputSection * p,uint64_t off)39585868e8aSDimitry Andric Patch843419Section::Patch843419Section(InputSection *p, uint64_t off)
3960b57cec5SDimitry Andric     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 4,
3970b57cec5SDimitry Andric                        ".text.patch"),
3980b57cec5SDimitry Andric       patchee(p), patcheeOffset(off) {
3990b57cec5SDimitry Andric   this->parent = p->getParent();
4000b57cec5SDimitry Andric   patchSym = addSyntheticLocal(
40104eeddc0SDimitry Andric       saver().save("__CortexA53843419_" + utohexstr(getLDSTAddr())), STT_FUNC,
40204eeddc0SDimitry Andric       0, getSize(), *this);
40304eeddc0SDimitry Andric   addSyntheticLocal(saver().save("$x"), STT_NOTYPE, 0, 0, *this);
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric 
getLDSTAddr() const40685868e8aSDimitry Andric uint64_t Patch843419Section::getLDSTAddr() const {
4070b57cec5SDimitry Andric   return patchee->getVA(patcheeOffset);
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric 
writeTo(uint8_t * buf)41085868e8aSDimitry Andric void Patch843419Section::writeTo(uint8_t *buf) {
4110b57cec5SDimitry Andric   // Copy the instruction that we will be replacing with a branch in the
41285868e8aSDimitry Andric   // patchee Section.
413bdd1243dSDimitry Andric   write32le(buf, read32le(patchee->content().begin() + patcheeOffset));
4140b57cec5SDimitry Andric 
41585868e8aSDimitry Andric   // Apply any relocation transferred from the original patchee section.
416bdd1243dSDimitry Andric   target->relocateAlloc(*this, buf);
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   // Return address is the next instruction after the one we have just copied.
4190b57cec5SDimitry Andric   uint64_t s = getLDSTAddr() + 4;
4200b57cec5SDimitry Andric   uint64_t p = patchSym->getVA() + 4;
4215ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_AARCH64_JUMP26, s - p);
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric 
init()4240b57cec5SDimitry Andric void AArch64Err843419Patcher::init() {
4250b57cec5SDimitry Andric   // The AArch64 ABI permits data in executable sections. We must avoid scanning
4260b57cec5SDimitry Andric   // this data as if it were instructions to avoid false matches. We use the
4270b57cec5SDimitry Andric   // mapping symbols in the InputObjects to identify this data, caching the
4280b57cec5SDimitry Andric   // results in sectionMap so we don't have to recalculate it each pass.
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   // The ABI Section 4.5.4 Mapping symbols; defines local symbols that describe
4310b57cec5SDimitry Andric   // half open intervals [Symbol Value, Next Symbol Value) of code and data
4320b57cec5SDimitry Andric   // within sections. If there is no next symbol then the half open interval is
4330b57cec5SDimitry Andric   // [Symbol Value, End of section). The type, code or data, is determined by
4340b57cec5SDimitry Andric   // the mapping symbol name, $x for code, $d for data.
4350b57cec5SDimitry Andric   auto isCodeMapSymbol = [](const Symbol *b) {
436*06c3fb27SDimitry Andric     return b->getName() == "$x" || b->getName().starts_with("$x.");
4370b57cec5SDimitry Andric   };
4380b57cec5SDimitry Andric   auto isDataMapSymbol = [](const Symbol *b) {
439*06c3fb27SDimitry Andric     return b->getName() == "$d" || b->getName().starts_with("$d.");
4400b57cec5SDimitry Andric   };
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   // Collect mapping symbols for every executable InputSection.
443bdd1243dSDimitry Andric   for (ELFFileBase *file : ctx.objectFiles) {
4440eae32dcSDimitry Andric     for (Symbol *b : file->getLocalSymbols()) {
4450b57cec5SDimitry Andric       auto *def = dyn_cast<Defined>(b);
4460b57cec5SDimitry Andric       if (!def)
4470b57cec5SDimitry Andric         continue;
4480b57cec5SDimitry Andric       if (!isCodeMapSymbol(def) && !isDataMapSymbol(def))
4490b57cec5SDimitry Andric         continue;
4500b57cec5SDimitry Andric       if (auto *sec = dyn_cast_or_null<InputSection>(def->section))
4510b57cec5SDimitry Andric         if (sec->flags & SHF_EXECINSTR)
4520b57cec5SDimitry Andric           sectionMap[sec].push_back(def);
4530b57cec5SDimitry Andric     }
4540b57cec5SDimitry Andric   }
4550b57cec5SDimitry Andric   // For each InputSection make sure the mapping symbols are in sorted in
4560b57cec5SDimitry Andric   // ascending order and free from consecutive runs of mapping symbols with
4570b57cec5SDimitry Andric   // the same type. For example we must remove the redundant $d.1 from $x.0
4580b57cec5SDimitry Andric   // $d.0 $d.1 $x.1.
4590b57cec5SDimitry Andric   for (auto &kv : sectionMap) {
4600b57cec5SDimitry Andric     std::vector<const Defined *> &mapSyms = kv.second;
4610b57cec5SDimitry Andric     llvm::stable_sort(mapSyms, [](const Defined *a, const Defined *b) {
4620b57cec5SDimitry Andric       return a->value < b->value;
4630b57cec5SDimitry Andric     });
4640b57cec5SDimitry Andric     mapSyms.erase(
4650b57cec5SDimitry Andric         std::unique(mapSyms.begin(), mapSyms.end(),
4660b57cec5SDimitry Andric                     [=](const Defined *a, const Defined *b) {
46785868e8aSDimitry Andric                       return isCodeMapSymbol(a) == isCodeMapSymbol(b);
4680b57cec5SDimitry Andric                     }),
4690b57cec5SDimitry Andric         mapSyms.end());
47085868e8aSDimitry Andric     // Always start with a Code Mapping Symbol.
47185868e8aSDimitry Andric     if (!mapSyms.empty() && !isCodeMapSymbol(mapSyms.front()))
47285868e8aSDimitry Andric       mapSyms.erase(mapSyms.begin());
4730b57cec5SDimitry Andric   }
4740b57cec5SDimitry Andric   initialized = true;
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric // Insert the PatchSections we have created back into the
4780b57cec5SDimitry Andric // InputSectionDescription. As inserting patches alters the addresses of
4790b57cec5SDimitry Andric // InputSections that follow them, we try and place the patches after all the
4800b57cec5SDimitry Andric // executable sections, although we may need to insert them earlier if the
4810b57cec5SDimitry Andric // InputSectionDescription is larger than the maximum branch range.
insertPatches(InputSectionDescription & isd,std::vector<Patch843419Section * > & patches)4820b57cec5SDimitry Andric void AArch64Err843419Patcher::insertPatches(
4830b57cec5SDimitry Andric     InputSectionDescription &isd, std::vector<Patch843419Section *> &patches) {
4840b57cec5SDimitry Andric   uint64_t isecLimit;
4850b57cec5SDimitry Andric   uint64_t prevIsecLimit = isd.sections.front()->outSecOff;
4860b57cec5SDimitry Andric   uint64_t patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();
4870b57cec5SDimitry Andric   uint64_t outSecAddr = isd.sections.front()->getParent()->addr;
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   // Set the outSecOff of patches to the place where we want to insert them.
4900b57cec5SDimitry Andric   // We use a similar strategy to Thunk placement. Place patches roughly
4910b57cec5SDimitry Andric   // every multiple of maximum branch range.
4920b57cec5SDimitry Andric   auto patchIt = patches.begin();
4930b57cec5SDimitry Andric   auto patchEnd = patches.end();
4940b57cec5SDimitry Andric   for (const InputSection *isec : isd.sections) {
4950b57cec5SDimitry Andric     isecLimit = isec->outSecOff + isec->getSize();
4960b57cec5SDimitry Andric     if (isecLimit > patchUpperBound) {
4970b57cec5SDimitry Andric       while (patchIt != patchEnd) {
4980b57cec5SDimitry Andric         if ((*patchIt)->getLDSTAddr() - outSecAddr >= prevIsecLimit)
4990b57cec5SDimitry Andric           break;
5000b57cec5SDimitry Andric         (*patchIt)->outSecOff = prevIsecLimit;
5010b57cec5SDimitry Andric         ++patchIt;
5020b57cec5SDimitry Andric       }
5030b57cec5SDimitry Andric       patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();
5040b57cec5SDimitry Andric     }
5050b57cec5SDimitry Andric     prevIsecLimit = isecLimit;
5060b57cec5SDimitry Andric   }
5070b57cec5SDimitry Andric   for (; patchIt != patchEnd; ++patchIt) {
5080b57cec5SDimitry Andric     (*patchIt)->outSecOff = isecLimit;
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric 
51185868e8aSDimitry Andric   // Merge all patch sections. We use the outSecOff assigned above to
5120b57cec5SDimitry Andric   // determine the insertion point. This is ok as we only merge into an
5130b57cec5SDimitry Andric   // InputSectionDescription once per pass, and at the end of the pass
5140b57cec5SDimitry Andric   // assignAddresses() will recalculate all the outSecOff values.
51504eeddc0SDimitry Andric   SmallVector<InputSection *, 0> tmp;
5160b57cec5SDimitry Andric   tmp.reserve(isd.sections.size() + patches.size());
5170b57cec5SDimitry Andric   auto mergeCmp = [](const InputSection *a, const InputSection *b) {
51885868e8aSDimitry Andric     if (a->outSecOff != b->outSecOff)
51985868e8aSDimitry Andric       return a->outSecOff < b->outSecOff;
52085868e8aSDimitry Andric     return isa<Patch843419Section>(a) && !isa<Patch843419Section>(b);
5210b57cec5SDimitry Andric   };
5220b57cec5SDimitry Andric   std::merge(isd.sections.begin(), isd.sections.end(), patches.begin(),
5230b57cec5SDimitry Andric              patches.end(), std::back_inserter(tmp), mergeCmp);
5240b57cec5SDimitry Andric   isd.sections = std::move(tmp);
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric // Given an erratum sequence that starts at address adrpAddr, with an
5280b57cec5SDimitry Andric // instruction that we need to patch at patcheeOffset from the start of
52985868e8aSDimitry Andric // InputSection isec, create a Patch843419 Section and add it to the
5300b57cec5SDimitry Andric // Patches that we need to insert.
implementPatch(uint64_t adrpAddr,uint64_t patcheeOffset,InputSection * isec,std::vector<Patch843419Section * > & patches)5310b57cec5SDimitry Andric static void implementPatch(uint64_t adrpAddr, uint64_t patcheeOffset,
5320b57cec5SDimitry Andric                            InputSection *isec,
5330b57cec5SDimitry Andric                            std::vector<Patch843419Section *> &patches) {
5340b57cec5SDimitry Andric   // There may be a relocation at the same offset that we are patching. There
5350b57cec5SDimitry Andric   // are four cases that we need to consider.
5360b57cec5SDimitry Andric   // Case 1: R_AARCH64_JUMP26 branch relocation. We have already patched this
5370b57cec5SDimitry Andric   // instance of the erratum on a previous patch and altered the relocation. We
5380b57cec5SDimitry Andric   // have nothing more to do.
5390b57cec5SDimitry Andric   // Case 2: A TLS Relaxation R_RELAX_TLS_IE_TO_LE. In this case the ADRP that
5400b57cec5SDimitry Andric   // we read will be transformed into a MOVZ later so we actually don't match
5410b57cec5SDimitry Andric   // the sequence and have nothing more to do.
5420b57cec5SDimitry Andric   // Case 3: A load/store register (unsigned immediate) class relocation. There
5430b57cec5SDimitry Andric   // are two of these R_AARCH_LD64_ABS_LO12_NC and R_AARCH_LD64_GOT_LO12_NC and
5440b57cec5SDimitry Andric   // they are both absolute. We need to add the same relocation to the patch,
5450b57cec5SDimitry Andric   // and replace the relocation with a R_AARCH_JUMP26 branch relocation.
5460b57cec5SDimitry Andric   // Case 4: No relocation. We must create a new R_AARCH64_JUMP26 branch
5470b57cec5SDimitry Andric   // relocation at the offset.
548bdd1243dSDimitry Andric   auto relIt = llvm::find_if(isec->relocs(), [=](const Relocation &r) {
5490b57cec5SDimitry Andric     return r.offset == patcheeOffset;
5500b57cec5SDimitry Andric   });
551bdd1243dSDimitry Andric   if (relIt != isec->relocs().end() &&
5520b57cec5SDimitry Andric       (relIt->type == R_AARCH64_JUMP26 || relIt->expr == R_RELAX_TLS_IE_TO_LE))
5530b57cec5SDimitry Andric     return;
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric   log("detected cortex-a53-843419 erratum sequence starting at " +
5560b57cec5SDimitry Andric       utohexstr(adrpAddr) + " in unpatched output.");
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   auto *ps = make<Patch843419Section>(isec, patcheeOffset);
5590b57cec5SDimitry Andric   patches.push_back(ps);
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   auto makeRelToPatch = [](uint64_t offset, Symbol *patchSym) {
5620b57cec5SDimitry Andric     return Relocation{R_PC, R_AARCH64_JUMP26, offset, 0, patchSym};
5630b57cec5SDimitry Andric   };
5640b57cec5SDimitry Andric 
565bdd1243dSDimitry Andric   if (relIt != isec->relocs().end()) {
566bdd1243dSDimitry Andric     ps->addReloc({relIt->expr, relIt->type, 0, relIt->addend, relIt->sym});
5670b57cec5SDimitry Andric     *relIt = makeRelToPatch(patcheeOffset, ps->patchSym);
5680b57cec5SDimitry Andric   } else
569bdd1243dSDimitry Andric     isec->addReloc(makeRelToPatch(patcheeOffset, ps->patchSym));
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric // Scan all the instructions in InputSectionDescription, for each instance of
5730b57cec5SDimitry Andric // the erratum sequence create a Patch843419Section. We return the list of
57485868e8aSDimitry Andric // Patch843419Sections that need to be applied to the InputSectionDescription.
5750b57cec5SDimitry Andric std::vector<Patch843419Section *>
patchInputSectionDescription(InputSectionDescription & isd)5760b57cec5SDimitry Andric AArch64Err843419Patcher::patchInputSectionDescription(
5770b57cec5SDimitry Andric     InputSectionDescription &isd) {
5780b57cec5SDimitry Andric   std::vector<Patch843419Section *> patches;
5790b57cec5SDimitry Andric   for (InputSection *isec : isd.sections) {
5800b57cec5SDimitry Andric     //  LLD doesn't use the erratum sequence in SyntheticSections.
5810b57cec5SDimitry Andric     if (isa<SyntheticSection>(isec))
5820b57cec5SDimitry Andric       continue;
5830b57cec5SDimitry Andric     // Use sectionMap to make sure we only scan code and not inline data.
5840b57cec5SDimitry Andric     // We have already sorted MapSyms in ascending order and removed consecutive
5850b57cec5SDimitry Andric     // mapping symbols of the same type. Our range of executable instructions to
5860b57cec5SDimitry Andric     // scan is therefore [codeSym->value, dataSym->value) or [codeSym->value,
5870b57cec5SDimitry Andric     // section size).
5880b57cec5SDimitry Andric     std::vector<const Defined *> &mapSyms = sectionMap[isec];
5890b57cec5SDimitry Andric 
59085868e8aSDimitry Andric     auto codeSym = mapSyms.begin();
5910b57cec5SDimitry Andric     while (codeSym != mapSyms.end()) {
5920b57cec5SDimitry Andric       auto dataSym = std::next(codeSym);
5930b57cec5SDimitry Andric       uint64_t off = (*codeSym)->value;
594bdd1243dSDimitry Andric       uint64_t limit = (dataSym == mapSyms.end()) ? isec->content().size()
595bdd1243dSDimitry Andric                                                   : (*dataSym)->value;
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric       while (off < limit) {
5980b57cec5SDimitry Andric         uint64_t startAddr = isec->getVA(off);
59985868e8aSDimitry Andric         if (uint64_t patcheeOffset =
60085868e8aSDimitry Andric                 scanCortexA53Errata843419(isec, off, limit))
6010b57cec5SDimitry Andric           implementPatch(startAddr, patcheeOffset, isec, patches);
6020b57cec5SDimitry Andric       }
6030b57cec5SDimitry Andric       if (dataSym == mapSyms.end())
6040b57cec5SDimitry Andric         break;
6050b57cec5SDimitry Andric       codeSym = std::next(dataSym);
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric   return patches;
6090b57cec5SDimitry Andric }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric // For each InputSectionDescription make one pass over the executable sections
6120b57cec5SDimitry Andric // looking for the erratum sequence; creating a synthetic Patch843419Section
6130b57cec5SDimitry Andric // for each instance found. We insert these synthetic patch sections after the
6140b57cec5SDimitry Andric // executable code in each InputSectionDescription.
6150b57cec5SDimitry Andric //
6160b57cec5SDimitry Andric // PreConditions:
6170b57cec5SDimitry Andric // The Output and Input Sections have had their final addresses assigned.
6180b57cec5SDimitry Andric //
6190b57cec5SDimitry Andric // PostConditions:
6200b57cec5SDimitry Andric // Returns true if at least one patch was added. The addresses of the
621480093f4SDimitry Andric // Output and Input Sections may have been changed.
6220b57cec5SDimitry Andric // Returns false if no patches were required and no changes were made.
createFixes()6230b57cec5SDimitry Andric bool AArch64Err843419Patcher::createFixes() {
62485868e8aSDimitry Andric   if (!initialized)
6250b57cec5SDimitry Andric     init();
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   bool addressesChanged = false;
6280b57cec5SDimitry Andric   for (OutputSection *os : outputSections) {
6290b57cec5SDimitry Andric     if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))
6300b57cec5SDimitry Andric       continue;
6314824e7fdSDimitry Andric     for (SectionCommand *cmd : os->commands)
6324824e7fdSDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
6330b57cec5SDimitry Andric         std::vector<Patch843419Section *> patches =
6340b57cec5SDimitry Andric             patchInputSectionDescription(*isd);
6350b57cec5SDimitry Andric         if (!patches.empty()) {
6360b57cec5SDimitry Andric           insertPatches(*isd, patches);
6370b57cec5SDimitry Andric           addressesChanged = true;
6380b57cec5SDimitry Andric         }
6390b57cec5SDimitry Andric       }
6400b57cec5SDimitry Andric   }
6410b57cec5SDimitry Andric   return addressesChanged;
6420b57cec5SDimitry Andric }
643