Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 1 | //===-- MipsAsmBackend.cpp - Mips Asm Backend ----------------------------===// |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 9 | // This file implements the MipsAsmBackend class. |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | // |
| 13 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/MipsAsmBackend.h" |
Simon Atanasyan | 9265dca | 2018-09-17 21:21:57 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsABIInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/MipsFixupKinds.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
Fangrui Song | 6717dc5 | 2024-07-21 11:17:19 -0700 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmBackend.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAssembler.h" |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCELFObjectWriter.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCFixupKindInfo.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCObjectWriter.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSubtargetInfo.h" |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCTargetOptions.h" |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCValue.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | 8a8cd2ba | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 31 | |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 34 | // Prepare value for the target space for it |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 35 | static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value, |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 36 | MCContext &Ctx) { |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 37 | |
| 38 | unsigned Kind = Fixup.getKind(); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 39 | |
| 40 | // Add/subtract and shift |
| 41 | switch (Kind) { |
| 42 | default: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 43 | return 0; |
Ed Maste | 2a710d0 | 2014-03-03 14:27:49 +0000 | [diff] [blame] | 44 | case FK_Data_2: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 45 | case Mips::fixup_Mips_LO16: |
Jack Carter | c3dd91c | 2013-01-08 19:01:28 +0000 | [diff] [blame] | 46 | case Mips::fixup_Mips_GPREL16: |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 47 | case Mips::fixup_Mips_GPOFF_HI: |
| 48 | case Mips::fixup_Mips_GPOFF_LO: |
| 49 | case Mips::fixup_Mips_GOT_PAGE: |
| 50 | case Mips::fixup_Mips_GOT_OFST: |
Jack Carter | 5ddcfda | 2012-07-13 19:15:47 +0000 | [diff] [blame] | 51 | case Mips::fixup_Mips_GOT_DISP: |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 52 | case Mips::fixup_Mips_GOT_LO16: |
| 53 | case Mips::fixup_Mips_CALL_LO16: |
Simon Atanasyan | a1d69f9 | 2018-05-29 11:33:54 +0000 | [diff] [blame] | 54 | case Mips::fixup_MICROMIPS_GPOFF_HI: |
| 55 | case Mips::fixup_MICROMIPS_GPOFF_LO: |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 56 | case Mips::fixup_MICROMIPS_LO16: |
| 57 | case Mips::fixup_MICROMIPS_GOT_PAGE: |
| 58 | case Mips::fixup_MICROMIPS_GOT_OFST: |
| 59 | case Mips::fixup_MICROMIPS_GOT_DISP: |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 60 | case Mips::fixup_MIPS_PCLO16: |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 61 | Value &= 0xffff; |
| 62 | break; |
Fangrui Song | b65e094 | 2025-03-01 10:26:49 -0800 | [diff] [blame] | 63 | case Mips::fixup_Mips_AnyImm16: |
| 64 | if (!isInt<16>(Value) && !isUInt<16>(Value)) |
| 65 | Ctx.reportError(Fixup.getLoc(), |
| 66 | "fixup value out of range [-32768, 65535]"); |
| 67 | break; |
Fangrui Song | 6048629 | 2025-03-02 14:59:20 -0800 | [diff] [blame] | 68 | case Mips::fixup_Mips_GPREL32: |
| 69 | case Mips::fixup_Mips_DTPREL32: |
| 70 | case Mips::fixup_Mips_DTPREL64: |
| 71 | case Mips::fixup_Mips_TPREL32: |
| 72 | case Mips::fixup_Mips_TPREL64: |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 73 | case FK_Data_4: |
| 74 | case FK_Data_8: |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 75 | case Mips::fixup_Mips_SUB: |
| 76 | case Mips::fixup_MICROMIPS_SUB: |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 77 | break; |
| 78 | case Mips::fixup_Mips_PC16: |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 79 | // The displacement is then divided by 4 to give us an 18 bit |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 80 | // address range. Forcing a signed division because Value can be negative. |
| 81 | Value = (int64_t)Value / 4; |
| 82 | // We now check if Value can be encoded as a 16-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 83 | if (!isInt<16>(Value)) { |
| 84 | Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 87 | break; |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 88 | case Mips::fixup_MIPS_PC19_S2: |
Zoran Jovanovic | 6764fa7 | 2016-04-21 14:09:35 +0000 | [diff] [blame] | 89 | case Mips::fixup_MICROMIPS_PC19_S2: |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 90 | // Forcing a signed division because Value can be negative. |
| 91 | Value = (int64_t)Value / 4; |
| 92 | // We now check if Value can be encoded as a 19-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 93 | if (!isInt<19>(Value)) { |
| 94 | Ctx.reportError(Fixup.getLoc(), "out of range PC19 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | } |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 97 | break; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 98 | case Mips::fixup_Mips_26: |
| 99 | // So far we are only using this type for jumps. |
| 100 | // The displacement is then divided by 4 to give us an 28 bit |
| 101 | // address range. |
| 102 | Value >>= 2; |
| 103 | break; |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 104 | case Mips::fixup_Mips_HI16: |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 105 | case Mips::fixup_Mips_GOT: |
Daniel Sanders | a2bde88 | 2016-05-16 09:33:59 +0000 | [diff] [blame] | 106 | case Mips::fixup_MICROMIPS_GOT16: |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 107 | case Mips::fixup_Mips_GOT_HI16: |
| 108 | case Mips::fixup_Mips_CALL_HI16: |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 109 | case Mips::fixup_MICROMIPS_HI16: |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 110 | case Mips::fixup_MIPS_PCHI16: |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 111 | // Get the 2nd 16-bits. Also add 1 if bit 15 is 1. |
Akira Hatanaka | da72819 | 2012-03-27 01:50:08 +0000 | [diff] [blame] | 112 | Value = ((Value + 0x8000) >> 16) & 0xffff; |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 113 | break; |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 114 | case Mips::fixup_Mips_HIGHER: |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 115 | case Mips::fixup_MICROMIPS_HIGHER: |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 116 | // Get the 3rd 16-bits. |
| 117 | Value = ((Value + 0x80008000LL) >> 32) & 0xffff; |
| 118 | break; |
| 119 | case Mips::fixup_Mips_HIGHEST: |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 120 | case Mips::fixup_MICROMIPS_HIGHEST: |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 121 | // Get the 4th 16-bits. |
| 122 | Value = ((Value + 0x800080008000LL) >> 48) & 0xffff; |
| 123 | break; |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 124 | case Mips::fixup_MICROMIPS_26_S1: |
| 125 | Value >>= 1; |
| 126 | break; |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 127 | case Mips::fixup_MICROMIPS_PC7_S1: |
| 128 | Value -= 4; |
| 129 | // Forcing a signed division because Value can be negative. |
| 130 | Value = (int64_t) Value / 2; |
| 131 | // We now check if Value can be encoded as a 7-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 132 | if (!isInt<7>(Value)) { |
| 133 | Ctx.reportError(Fixup.getLoc(), "out of range PC7 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 136 | break; |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 137 | case Mips::fixup_MICROMIPS_PC10_S1: |
| 138 | Value -= 2; |
| 139 | // Forcing a signed division because Value can be negative. |
| 140 | Value = (int64_t) Value / 2; |
| 141 | // We now check if Value can be encoded as a 10-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 142 | if (!isInt<10>(Value)) { |
| 143 | Ctx.reportError(Fixup.getLoc(), "out of range PC10 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 146 | break; |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 147 | case Mips::fixup_MICROMIPS_PC16_S1: |
| 148 | Value -= 4; |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 149 | // Forcing a signed division because Value can be negative. |
| 150 | Value = (int64_t)Value / 2; |
| 151 | // We now check if Value can be encoded as a 16-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 152 | if (!isInt<16>(Value)) { |
| 153 | Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 156 | break; |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 157 | case Mips::fixup_MIPS_PC18_S3: |
| 158 | // Forcing a signed division because Value can be negative. |
| 159 | Value = (int64_t)Value / 8; |
| 160 | // We now check if Value can be encoded as a 18-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 161 | if (!isInt<18>(Value)) { |
| 162 | Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 165 | break; |
Zoran Jovanovic | 8e36682 | 2016-04-22 10:15:12 +0000 | [diff] [blame] | 166 | case Mips::fixup_MICROMIPS_PC18_S3: |
| 167 | // Check alignment. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 168 | if ((Value & 7)) { |
| 169 | Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup"); |
Zoran Jovanovic | 8e36682 | 2016-04-22 10:15:12 +0000 | [diff] [blame] | 170 | } |
| 171 | // Forcing a signed division because Value can be negative. |
| 172 | Value = (int64_t)Value / 8; |
| 173 | // We now check if Value can be encoded as a 18-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 174 | if (!isInt<18>(Value)) { |
| 175 | Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup"); |
Zoran Jovanovic | 8e36682 | 2016-04-22 10:15:12 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | break; |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 179 | case Mips::fixup_MIPS_PC21_S2: |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 180 | // Forcing a signed division because Value can be negative. |
| 181 | Value = (int64_t) Value / 4; |
| 182 | // We now check if Value can be encoded as a 21-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 183 | if (!isInt<21>(Value)) { |
| 184 | Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 187 | break; |
| 188 | case Mips::fixup_MIPS_PC26_S2: |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 189 | // Forcing a signed division because Value can be negative. |
| 190 | Value = (int64_t) Value / 4; |
| 191 | // We now check if Value can be encoded as a 26-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 192 | if (!isInt<26>(Value)) { |
| 193 | Ctx.reportError(Fixup.getLoc(), "out of range PC26 fixup"); |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 196 | break; |
Zoran Jovanovic | 02b7003 | 2016-04-21 13:43:26 +0000 | [diff] [blame] | 197 | case Mips::fixup_MICROMIPS_PC26_S1: |
| 198 | // Forcing a signed division because Value can be negative. |
| 199 | Value = (int64_t)Value / 2; |
| 200 | // We now check if Value can be encoded as a 26-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 201 | if (!isInt<26>(Value)) { |
Fangrui Song | 349006b | 2022-01-15 00:37:24 -0800 | [diff] [blame] | 202 | Ctx.reportError(Fixup.getLoc(), "out of range PC26 fixup"); |
Zoran Jovanovic | 02b7003 | 2016-04-21 13:43:26 +0000 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | break; |
Zoran Jovanovic | 5f94ced | 2016-05-19 12:20:40 +0000 | [diff] [blame] | 206 | case Mips::fixup_MICROMIPS_PC21_S1: |
| 207 | // Forcing a signed division because Value can be negative. |
| 208 | Value = (int64_t)Value / 2; |
| 209 | // We now check if Value can be encoded as a 21-bit signed immediate. |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 210 | if (!isInt<21>(Value)) { |
| 211 | Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup"); |
Zoran Jovanovic | 5f94ced | 2016-05-19 12:20:40 +0000 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | break; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return Value; |
| 218 | } |
| 219 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 220 | std::unique_ptr<MCObjectTargetWriter> |
| 221 | MipsAsmBackend::createObjectTargetWriter() const { |
| 222 | return createMipsELFObjectWriter(TheTriple, IsN32); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 223 | } |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 224 | |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 225 | // Little-endian fixup data byte ordering: |
| 226 | // mips32r2: a | b | x | x |
| 227 | // microMIPS: x | x | a | b |
| 228 | |
| 229 | static bool needsMMLEByteOrder(unsigned Kind) { |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 230 | return Kind != Mips::fixup_MICROMIPS_PC10_S1 && |
| 231 | Kind >= Mips::fixup_MICROMIPS_26_S1 && |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 232 | Kind < Mips::LastTargetFixupKind; |
| 233 | } |
| 234 | |
| 235 | // Calculate index for microMIPS specific little endian byte order |
| 236 | static unsigned calculateMMLEIndex(unsigned i) { |
| 237 | assert(i <= 3 && "Index out of range!"); |
| 238 | |
| 239 | return (1 - i / 2) * 2 + i % 2; |
| 240 | } |
| 241 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 242 | /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided |
| 243 | /// data fragment, at the offset specified by the fixup and following the |
| 244 | /// fixup kind as appropriate. |
Rafael Espindola | 801b42d | 2017-06-23 22:52:36 +0000 | [diff] [blame] | 245 | void MipsAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
| 246 | const MCValue &Target, |
Rafael Espindola | 88d9e37 | 2017-06-21 23:06:53 +0000 | [diff] [blame] | 247 | MutableArrayRef<char> Data, uint64_t Value, |
Peter Smith | 57f661b | 2018-06-06 09:40:06 +0000 | [diff] [blame] | 248 | bool IsResolved, |
| 249 | const MCSubtargetInfo *STI) const { |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 250 | MCFixupKind Kind = Fixup.getKind(); |
Rafael Espindola | f3512921 | 2017-06-24 00:26:57 +0000 | [diff] [blame] | 251 | MCContext &Ctx = Asm.getContext(); |
Alex Bradbury | 866113c | 2017-04-05 10:16:14 +0000 | [diff] [blame] | 252 | Value = adjustFixupValue(Fixup, Value, Ctx); |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 253 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 254 | if (!Value) |
| 255 | return; // Doesn't change encoding. |
| 256 | |
| 257 | // Where do we start in the object |
| 258 | unsigned Offset = Fixup.getOffset(); |
| 259 | // Number of bytes we need to fixup |
| 260 | unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8; |
| 261 | // Used to point to big endian bytes |
| 262 | unsigned FullSize; |
| 263 | |
| 264 | switch ((unsigned)Kind) { |
| 265 | case FK_Data_2: |
| 266 | case Mips::fixup_Mips_16: |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 267 | case Mips::fixup_MICROMIPS_PC10_S1: |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 268 | FullSize = 2; |
| 269 | break; |
| 270 | case FK_Data_8: |
| 271 | case Mips::fixup_Mips_64: |
| 272 | FullSize = 8; |
| 273 | break; |
| 274 | case FK_Data_4: |
| 275 | default: |
| 276 | FullSize = 4; |
| 277 | break; |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 278 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 279 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 280 | // Grab current value, if any, from bits. |
| 281 | uint64_t CurVal = 0; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 282 | |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 283 | bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind); |
| 284 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 285 | for (unsigned i = 0; i != NumBytes; ++i) { |
Kazu Hirata | 4a0ccfa | 2023-10-12 21:21:44 -0700 | [diff] [blame] | 286 | unsigned Idx = Endian == llvm::endianness::little |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 287 | ? (microMipsLEByteOrder ? calculateMMLEIndex(i) : i) |
| 288 | : (FullSize - 1 - i); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 289 | CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8); |
Akira Hatanaka | 0137dfe | 2012-03-21 00:52:01 +0000 | [diff] [blame] | 290 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 291 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 292 | uint64_t Mask = ((uint64_t)(-1) >> |
| 293 | (64 - getFixupKindInfo(Kind).TargetSize)); |
| 294 | CurVal |= Value & Mask; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 295 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 296 | // Write out the fixed up bytes back to the code/data bits. |
| 297 | for (unsigned i = 0; i != NumBytes; ++i) { |
Kazu Hirata | 4a0ccfa | 2023-10-12 21:21:44 -0700 | [diff] [blame] | 298 | unsigned Idx = Endian == llvm::endianness::little |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 299 | ? (microMipsLEByteOrder ? calculateMMLEIndex(i) : i) |
| 300 | : (FullSize - 1 - i); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 301 | Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 302 | } |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 303 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 304 | |
Fangrui Song | f4c16c4 | 2022-12-04 21:36:08 +0000 | [diff] [blame] | 305 | std::optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const { |
Fangrui Song | dd58735 | 2022-06-30 16:39:23 -0700 | [diff] [blame] | 306 | unsigned Type = llvm::StringSwitch<unsigned>(Name) |
| 307 | .Case("BFD_RELOC_NONE", ELF::R_MIPS_NONE) |
| 308 | .Case("BFD_RELOC_16", ELF::R_MIPS_16) |
| 309 | .Case("BFD_RELOC_32", ELF::R_MIPS_32) |
| 310 | .Case("BFD_RELOC_64", ELF::R_MIPS_64) |
| 311 | .Default(-1u); |
| 312 | if (Type != -1u) |
| 313 | return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type); |
| 314 | |
Fangrui Song | f4c16c4 | 2022-12-04 21:36:08 +0000 | [diff] [blame] | 315 | return StringSwitch<std::optional<MCFixupKind>>(Name) |
Fangrui Song | 43ca0e9 | 2019-05-17 02:51:54 +0000 | [diff] [blame] | 316 | .Case("R_MIPS_NONE", FK_NONE) |
David Majnemer | ce10842 | 2016-01-19 23:05:27 +0000 | [diff] [blame] | 317 | .Case("R_MIPS_32", FK_Data_4) |
Simon Atanasyan | 3a44bcf | 2018-06-01 16:37:42 +0000 | [diff] [blame] | 318 | .Case("R_MIPS_CALL_HI16", (MCFixupKind)Mips::fixup_Mips_CALL_HI16) |
| 319 | .Case("R_MIPS_CALL_LO16", (MCFixupKind)Mips::fixup_Mips_CALL_LO16) |
| 320 | .Case("R_MIPS_CALL16", (MCFixupKind)Mips::fixup_Mips_CALL16) |
| 321 | .Case("R_MIPS_GOT16", (MCFixupKind)Mips::fixup_Mips_GOT) |
| 322 | .Case("R_MIPS_GOT_PAGE", (MCFixupKind)Mips::fixup_Mips_GOT_PAGE) |
| 323 | .Case("R_MIPS_GOT_OFST", (MCFixupKind)Mips::fixup_Mips_GOT_OFST) |
| 324 | .Case("R_MIPS_GOT_DISP", (MCFixupKind)Mips::fixup_Mips_GOT_DISP) |
| 325 | .Case("R_MIPS_GOT_HI16", (MCFixupKind)Mips::fixup_Mips_GOT_HI16) |
| 326 | .Case("R_MIPS_GOT_LO16", (MCFixupKind)Mips::fixup_Mips_GOT_LO16) |
| 327 | .Case("R_MIPS_TLS_GOTTPREL", (MCFixupKind)Mips::fixup_Mips_GOTTPREL) |
| 328 | .Case("R_MIPS_TLS_DTPREL_HI16", (MCFixupKind)Mips::fixup_Mips_DTPREL_HI) |
| 329 | .Case("R_MIPS_TLS_DTPREL_LO16", (MCFixupKind)Mips::fixup_Mips_DTPREL_LO) |
| 330 | .Case("R_MIPS_TLS_GD", (MCFixupKind)Mips::fixup_Mips_TLSGD) |
| 331 | .Case("R_MIPS_TLS_LDM", (MCFixupKind)Mips::fixup_Mips_TLSLDM) |
| 332 | .Case("R_MIPS_TLS_TPREL_HI16", (MCFixupKind)Mips::fixup_Mips_TPREL_HI) |
| 333 | .Case("R_MIPS_TLS_TPREL_LO16", (MCFixupKind)Mips::fixup_Mips_TPREL_LO) |
| 334 | .Case("R_MICROMIPS_CALL16", (MCFixupKind)Mips::fixup_MICROMIPS_CALL16) |
| 335 | .Case("R_MICROMIPS_GOT_DISP", (MCFixupKind)Mips::fixup_MICROMIPS_GOT_DISP) |
| 336 | .Case("R_MICROMIPS_GOT_PAGE", (MCFixupKind)Mips::fixup_MICROMIPS_GOT_PAGE) |
| 337 | .Case("R_MICROMIPS_GOT_OFST", (MCFixupKind)Mips::fixup_MICROMIPS_GOT_OFST) |
| 338 | .Case("R_MICROMIPS_GOT16", (MCFixupKind)Mips::fixup_MICROMIPS_GOT16) |
| 339 | .Case("R_MICROMIPS_TLS_GOTTPREL", |
| 340 | (MCFixupKind)Mips::fixup_MICROMIPS_GOTTPREL) |
| 341 | .Case("R_MICROMIPS_TLS_DTPREL_HI16", |
| 342 | (MCFixupKind)Mips::fixup_MICROMIPS_TLS_DTPREL_HI16) |
| 343 | .Case("R_MICROMIPS_TLS_DTPREL_LO16", |
| 344 | (MCFixupKind)Mips::fixup_MICROMIPS_TLS_DTPREL_LO16) |
| 345 | .Case("R_MICROMIPS_TLS_GD", (MCFixupKind)Mips::fixup_MICROMIPS_TLS_GD) |
| 346 | .Case("R_MICROMIPS_TLS_LDM", (MCFixupKind)Mips::fixup_MICROMIPS_TLS_LDM) |
| 347 | .Case("R_MICROMIPS_TLS_TPREL_HI16", |
| 348 | (MCFixupKind)Mips::fixup_MICROMIPS_TLS_TPREL_HI16) |
| 349 | .Case("R_MICROMIPS_TLS_TPREL_LO16", |
| 350 | (MCFixupKind)Mips::fixup_MICROMIPS_TLS_TPREL_LO16) |
Vladimir Stefanovic | 64ad1cf | 2018-11-21 16:38:34 +0000 | [diff] [blame] | 351 | .Case("R_MIPS_JALR", (MCFixupKind)Mips::fixup_Mips_JALR) |
| 352 | .Case("R_MICROMIPS_JALR", (MCFixupKind)Mips::fixup_MICROMIPS_JALR) |
David Majnemer | ce10842 | 2016-01-19 23:05:27 +0000 | [diff] [blame] | 353 | .Default(MCAsmBackend::getFixupKind(Name)); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 356 | const MCFixupKindInfo &MipsAsmBackend:: |
| 357 | getFixupKindInfo(MCFixupKind Kind) const { |
Alexander Richardson | e8059b1 | 2018-03-27 10:08:12 +0000 | [diff] [blame] | 358 | const static MCFixupKindInfo LittleEndianInfos[] = { |
Fangrui Song | b356a30 | 2025-03-01 10:16:02 -0800 | [diff] [blame] | 359 | // This table *must* be in same the order of fixup_* kinds in |
| 360 | // MipsFixupKinds.h. |
| 361 | // |
| 362 | // name offset bits flags |
| 363 | // clang-format off |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 364 | { "fixup_Mips_16", 0, 16, 0 }, |
| 365 | { "fixup_Mips_32", 0, 32, 0 }, |
| 366 | { "fixup_Mips_REL32", 0, 32, 0 }, |
Fangrui Song | 6048629 | 2025-03-02 14:59:20 -0800 | [diff] [blame] | 367 | { "fixup_Mips_GPREL32", 0, 32, 0 }, |
| 368 | { "fixup_Mips_DTPREL32", 0, 32, 0 }, |
| 369 | { "fixup_Mips_DTPREL64", 0, 64, 0 }, |
| 370 | { "fixup_Mips_TPREL32", 0, 32, 0 }, |
| 371 | { "fixup_Mips_TPREL64", 0, 64, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 372 | { "fixup_Mips_26", 0, 26, 0 }, |
| 373 | { "fixup_Mips_HI16", 0, 16, 0 }, |
| 374 | { "fixup_Mips_LO16", 0, 16, 0 }, |
Fangrui Song | b65e094 | 2025-03-01 10:26:49 -0800 | [diff] [blame] | 375 | { "fixup_Mips_AnyImm16", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 376 | { "fixup_Mips_GPREL16", 0, 16, 0 }, |
| 377 | { "fixup_Mips_LITERAL", 0, 16, 0 }, |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 378 | { "fixup_Mips_GOT", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 379 | { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 380 | { "fixup_Mips_CALL16", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 381 | { "fixup_Mips_SHIFT5", 6, 5, 0 }, |
| 382 | { "fixup_Mips_SHIFT6", 6, 5, 0 }, |
| 383 | { "fixup_Mips_64", 0, 64, 0 }, |
| 384 | { "fixup_Mips_TLSGD", 0, 16, 0 }, |
| 385 | { "fixup_Mips_GOTTPREL", 0, 16, 0 }, |
| 386 | { "fixup_Mips_TPREL_HI", 0, 16, 0 }, |
| 387 | { "fixup_Mips_TPREL_LO", 0, 16, 0 }, |
| 388 | { "fixup_Mips_TLSLDM", 0, 16, 0 }, |
| 389 | { "fixup_Mips_DTPREL_HI", 0, 16, 0 }, |
| 390 | { "fixup_Mips_DTPREL_LO", 0, 16, 0 }, |
| 391 | { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 392 | { "fixup_Mips_GPOFF_HI", 0, 16, 0 }, |
Simon Atanasyan | a1d69f9 | 2018-05-29 11:33:54 +0000 | [diff] [blame] | 393 | { "fixup_MICROMIPS_GPOFF_HI",0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 394 | { "fixup_Mips_GPOFF_LO", 0, 16, 0 }, |
Simon Atanasyan | a1d69f9 | 2018-05-29 11:33:54 +0000 | [diff] [blame] | 395 | { "fixup_MICROMIPS_GPOFF_LO",0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 396 | { "fixup_Mips_GOT_PAGE", 0, 16, 0 }, |
| 397 | { "fixup_Mips_GOT_OFST", 0, 16, 0 }, |
| 398 | { "fixup_Mips_GOT_DISP", 0, 16, 0 }, |
| 399 | { "fixup_Mips_HIGHER", 0, 16, 0 }, |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 400 | { "fixup_MICROMIPS_HIGHER", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 401 | { "fixup_Mips_HIGHEST", 0, 16, 0 }, |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 402 | { "fixup_MICROMIPS_HIGHEST", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 403 | { "fixup_Mips_GOT_HI16", 0, 16, 0 }, |
| 404 | { "fixup_Mips_GOT_LO16", 0, 16, 0 }, |
| 405 | { "fixup_Mips_CALL_HI16", 0, 16, 0 }, |
| 406 | { "fixup_Mips_CALL_LO16", 0, 16, 0 }, |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 407 | { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 408 | { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 409 | { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel }, |
| 410 | { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 411 | { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 412 | { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 413 | { "fixup_MICROMIPS_26_S1", 0, 26, 0 }, |
| 414 | { "fixup_MICROMIPS_HI16", 0, 16, 0 }, |
| 415 | { "fixup_MICROMIPS_LO16", 0, 16, 0 }, |
| 416 | { "fixup_MICROMIPS_GOT16", 0, 16, 0 }, |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 417 | { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel }, |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 418 | { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 419 | { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 02b7003 | 2016-04-21 13:43:26 +0000 | [diff] [blame] | 420 | { "fixup_MICROMIPS_PC26_S1", 0, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 6764fa7 | 2016-04-21 14:09:35 +0000 | [diff] [blame] | 421 | { "fixup_MICROMIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 8e36682 | 2016-04-22 10:15:12 +0000 | [diff] [blame] | 422 | { "fixup_MICROMIPS_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 5f94ced | 2016-05-19 12:20:40 +0000 | [diff] [blame] | 423 | { "fixup_MICROMIPS_PC21_S1", 0, 21, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 424 | { "fixup_MICROMIPS_CALL16", 0, 16, 0 }, |
| 425 | { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 }, |
| 426 | { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 }, |
| 427 | { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 }, |
| 428 | { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 }, |
| 429 | { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 }, |
| 430 | { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 }, |
| 431 | { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 }, |
Simon Atanasyan | 3979f43 | 2017-04-30 04:27:23 +0000 | [diff] [blame] | 432 | { "fixup_MICROMIPS_GOTTPREL", 0, 16, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 433 | { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 }, |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 434 | { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }, |
| 435 | { "fixup_Mips_SUB", 0, 64, 0 }, |
Vladimir Stefanovic | 64ad1cf | 2018-11-21 16:38:34 +0000 | [diff] [blame] | 436 | { "fixup_MICROMIPS_SUB", 0, 64, 0 }, |
| 437 | { "fixup_Mips_JALR", 0, 32, 0 }, |
Fangrui Song | b356a30 | 2025-03-01 10:16:02 -0800 | [diff] [blame] | 438 | { "fixup_MICROMIPS_JALR", 0, 32, 0 }, |
| 439 | // clang-format on |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 440 | }; |
Joe Loser | 5e96cea | 2022-09-06 18:06:58 -0600 | [diff] [blame] | 441 | static_assert(std::size(LittleEndianInfos) == Mips::NumTargetFixupKinds, |
Alexander Richardson | e8059b1 | 2018-03-27 10:08:12 +0000 | [diff] [blame] | 442 | "Not all MIPS little endian fixup kinds added!"); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 443 | |
Alexander Richardson | e8059b1 | 2018-03-27 10:08:12 +0000 | [diff] [blame] | 444 | const static MCFixupKindInfo BigEndianInfos[] = { |
Fangrui Song | b356a30 | 2025-03-01 10:16:02 -0800 | [diff] [blame] | 445 | // This table *must* be in same the order of fixup_* kinds in |
| 446 | // MipsFixupKinds.h. |
| 447 | // |
| 448 | // name offset bits flags |
| 449 | // clang-format off |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 450 | { "fixup_Mips_16", 16, 16, 0 }, |
| 451 | { "fixup_Mips_32", 0, 32, 0 }, |
| 452 | { "fixup_Mips_REL32", 0, 32, 0 }, |
Fangrui Song | 6048629 | 2025-03-02 14:59:20 -0800 | [diff] [blame] | 453 | { "fixup_Mips_GPREL32", 0, 32, 0 }, |
| 454 | { "fixup_Mips_DTPREL32", 0, 32, 0 }, |
| 455 | { "fixup_Mips_DTPREL64", 0, 64, 0 }, |
| 456 | { "fixup_Mips_TPREL32", 0, 32, 0 }, |
| 457 | { "fixup_Mips_TPREL64", 0, 64, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 458 | { "fixup_Mips_26", 6, 26, 0 }, |
| 459 | { "fixup_Mips_HI16", 16, 16, 0 }, |
| 460 | { "fixup_Mips_LO16", 16, 16, 0 }, |
Fangrui Song | b65e094 | 2025-03-01 10:26:49 -0800 | [diff] [blame] | 461 | { "fixup_Mips_AnyImm16", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 462 | { "fixup_Mips_GPREL16", 16, 16, 0 }, |
| 463 | { "fixup_Mips_LITERAL", 16, 16, 0 }, |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 464 | { "fixup_Mips_GOT", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 465 | { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 466 | { "fixup_Mips_CALL16", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 467 | { "fixup_Mips_SHIFT5", 21, 5, 0 }, |
| 468 | { "fixup_Mips_SHIFT6", 21, 5, 0 }, |
| 469 | { "fixup_Mips_64", 0, 64, 0 }, |
| 470 | { "fixup_Mips_TLSGD", 16, 16, 0 }, |
| 471 | { "fixup_Mips_GOTTPREL", 16, 16, 0 }, |
| 472 | { "fixup_Mips_TPREL_HI", 16, 16, 0 }, |
| 473 | { "fixup_Mips_TPREL_LO", 16, 16, 0 }, |
| 474 | { "fixup_Mips_TLSLDM", 16, 16, 0 }, |
| 475 | { "fixup_Mips_DTPREL_HI", 16, 16, 0 }, |
| 476 | { "fixup_Mips_DTPREL_LO", 16, 16, 0 }, |
| 477 | { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 478 | { "fixup_Mips_GPOFF_HI", 16, 16, 0 }, |
Simon Atanasyan | a1d69f9 | 2018-05-29 11:33:54 +0000 | [diff] [blame] | 479 | { "fixup_MICROMIPS_GPOFF_HI", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 480 | { "fixup_Mips_GPOFF_LO", 16, 16, 0 }, |
Simon Atanasyan | a1d69f9 | 2018-05-29 11:33:54 +0000 | [diff] [blame] | 481 | { "fixup_MICROMIPS_GPOFF_LO", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 482 | { "fixup_Mips_GOT_PAGE", 16, 16, 0 }, |
| 483 | { "fixup_Mips_GOT_OFST", 16, 16, 0 }, |
| 484 | { "fixup_Mips_GOT_DISP", 16, 16, 0 }, |
| 485 | { "fixup_Mips_HIGHER", 16, 16, 0 }, |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 486 | { "fixup_MICROMIPS_HIGHER", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 487 | { "fixup_Mips_HIGHEST", 16, 16, 0 }, |
Simon Atanasyan | 6be87bc | 2018-05-29 10:27:44 +0000 | [diff] [blame] | 488 | { "fixup_MICROMIPS_HIGHEST",16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 489 | { "fixup_Mips_GOT_HI16", 16, 16, 0 }, |
| 490 | { "fixup_Mips_GOT_LO16", 16, 16, 0 }, |
| 491 | { "fixup_Mips_CALL_HI16", 16, 16, 0 }, |
| 492 | { "fixup_Mips_CALL_LO16", 16, 16, 0 }, |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 493 | { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 494 | { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 495 | { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel }, |
| 496 | { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 497 | { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 498 | { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 499 | { "fixup_MICROMIPS_26_S1", 6, 26, 0 }, |
| 500 | { "fixup_MICROMIPS_HI16", 16, 16, 0 }, |
| 501 | { "fixup_MICROMIPS_LO16", 16, 16, 0 }, |
| 502 | { "fixup_MICROMIPS_GOT16", 16, 16, 0 }, |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 503 | { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel }, |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 504 | { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 505 | { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 02b7003 | 2016-04-21 13:43:26 +0000 | [diff] [blame] | 506 | { "fixup_MICROMIPS_PC26_S1", 6, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 6764fa7 | 2016-04-21 14:09:35 +0000 | [diff] [blame] | 507 | { "fixup_MICROMIPS_PC19_S2",13, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 8e36682 | 2016-04-22 10:15:12 +0000 | [diff] [blame] | 508 | { "fixup_MICROMIPS_PC18_S3",14, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 5f94ced | 2016-05-19 12:20:40 +0000 | [diff] [blame] | 509 | { "fixup_MICROMIPS_PC21_S1",11, 21, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 510 | { "fixup_MICROMIPS_CALL16", 16, 16, 0 }, |
| 511 | { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 }, |
| 512 | { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 }, |
| 513 | { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 }, |
| 514 | { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 }, |
| 515 | { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 }, |
| 516 | { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 }, |
| 517 | { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 }, |
Simon Atanasyan | 3979f43 | 2017-04-30 04:27:23 +0000 | [diff] [blame] | 518 | { "fixup_MICROMIPS_GOTTPREL", 16, 16, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 519 | { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 }, |
Daniel Sanders | 3feeb9c | 2016-08-08 11:50:25 +0000 | [diff] [blame] | 520 | { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 }, |
| 521 | { "fixup_Mips_SUB", 0, 64, 0 }, |
Vladimir Stefanovic | 64ad1cf | 2018-11-21 16:38:34 +0000 | [diff] [blame] | 522 | { "fixup_MICROMIPS_SUB", 0, 64, 0 }, |
| 523 | { "fixup_Mips_JALR", 0, 32, 0 }, |
Fangrui Song | b356a30 | 2025-03-01 10:16:02 -0800 | [diff] [blame] | 524 | { "fixup_MICROMIPS_JALR", 0, 32, 0 }, |
| 525 | // clang-format on |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 526 | }; |
Joe Loser | 5e96cea | 2022-09-06 18:06:58 -0600 | [diff] [blame] | 527 | static_assert(std::size(BigEndianInfos) == Mips::NumTargetFixupKinds, |
Alexander Richardson | e8059b1 | 2018-03-27 10:08:12 +0000 | [diff] [blame] | 528 | "Not all MIPS big endian fixup kinds added!"); |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 529 | |
Fangrui Song | f9bd89b | 2025-04-18 18:18:30 -0700 | [diff] [blame^] | 530 | if (mc::isRelocation(Kind)) |
Fangrui Song | dd58735 | 2022-06-30 16:39:23 -0700 | [diff] [blame] | 531 | return MCAsmBackend::getFixupKindInfo(FK_NONE); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 532 | if (Kind < FirstTargetFixupKind) |
| 533 | return MCAsmBackend::getFixupKindInfo(Kind); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 534 | |
Fangrui Song | 2c35cb6 | 2025-03-16 23:50:46 -0700 | [diff] [blame] | 535 | assert(unsigned(Kind - FirstTargetFixupKind) < Mips::NumTargetFixupKinds && |
| 536 | "Invalid kind!"); |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 537 | |
Kazu Hirata | 4a0ccfa | 2023-10-12 21:21:44 -0700 | [diff] [blame] | 538 | if (Endian == llvm::endianness::little) |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 539 | return LittleEndianInfos[Kind - FirstTargetFixupKind]; |
| 540 | return BigEndianInfos[Kind - FirstTargetFixupKind]; |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 541 | } |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 542 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 543 | /// WriteNopData - Write an (optimal) nop sequence of Count bytes |
| 544 | /// to the given output. If the target cannot generate such a sequence, |
| 545 | /// it should return an error. |
| 546 | /// |
| 547 | /// \return - True on success. |
Peter Smith | e63455d | 2021-08-09 11:40:22 +0100 | [diff] [blame] | 548 | bool MipsAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, |
| 549 | const MCSubtargetInfo *STI) const { |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 550 | // Check for a less than instruction size number of bytes |
| 551 | // FIXME: 16 bit instructions are not handled yet here. |
| 552 | // We shouldn't be using a hard coded number for instruction size. |
Joerg Sonnenberger | f148a6d | 2014-10-02 13:41:42 +0000 | [diff] [blame] | 553 | |
| 554 | // If the count is not 4-byte aligned, we must be writing data into the text |
| 555 | // section (otherwise we have unaligned instructions, and thus have far |
| 556 | // bigger problems), so just write zeros instead. |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 557 | OS.write_zeros(Count); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 558 | return true; |
| 559 | } |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 560 | |
Simon Atanasyan | 3a44bcf | 2018-06-01 16:37:42 +0000 | [diff] [blame] | 561 | bool MipsAsmBackend::shouldForceRelocation(const MCAssembler &Asm, |
| 562 | const MCFixup &Fixup, |
Craig Topper | e87f33d | 2023-12-07 13:17:58 -0800 | [diff] [blame] | 563 | const MCValue &Target, |
| 564 | const MCSubtargetInfo *STI) { |
Simon Atanasyan | 3a44bcf | 2018-06-01 16:37:42 +0000 | [diff] [blame] | 565 | const unsigned FixupKind = Fixup.getKind(); |
| 566 | switch (FixupKind) { |
| 567 | default: |
| 568 | return false; |
| 569 | // All these relocations require special processing |
| 570 | // at linking time. Delegate this work to a linker. |
| 571 | case Mips::fixup_Mips_CALL_HI16: |
| 572 | case Mips::fixup_Mips_CALL_LO16: |
| 573 | case Mips::fixup_Mips_CALL16: |
| 574 | case Mips::fixup_Mips_GOT: |
| 575 | case Mips::fixup_Mips_GOT_PAGE: |
| 576 | case Mips::fixup_Mips_GOT_OFST: |
| 577 | case Mips::fixup_Mips_GOT_DISP: |
| 578 | case Mips::fixup_Mips_GOT_HI16: |
| 579 | case Mips::fixup_Mips_GOT_LO16: |
| 580 | case Mips::fixup_Mips_GOTTPREL: |
| 581 | case Mips::fixup_Mips_DTPREL_HI: |
| 582 | case Mips::fixup_Mips_DTPREL_LO: |
| 583 | case Mips::fixup_Mips_TLSGD: |
| 584 | case Mips::fixup_Mips_TLSLDM: |
| 585 | case Mips::fixup_Mips_TPREL_HI: |
| 586 | case Mips::fixup_Mips_TPREL_LO: |
Vladimir Stefanovic | 64ad1cf | 2018-11-21 16:38:34 +0000 | [diff] [blame] | 587 | case Mips::fixup_Mips_JALR: |
Simon Atanasyan | 3a44bcf | 2018-06-01 16:37:42 +0000 | [diff] [blame] | 588 | case Mips::fixup_MICROMIPS_CALL16: |
| 589 | case Mips::fixup_MICROMIPS_GOT_DISP: |
| 590 | case Mips::fixup_MICROMIPS_GOT_PAGE: |
| 591 | case Mips::fixup_MICROMIPS_GOT_OFST: |
| 592 | case Mips::fixup_MICROMIPS_GOT16: |
| 593 | case Mips::fixup_MICROMIPS_GOTTPREL: |
| 594 | case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16: |
| 595 | case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16: |
| 596 | case Mips::fixup_MICROMIPS_TLS_GD: |
| 597 | case Mips::fixup_MICROMIPS_TLS_LDM: |
| 598 | case Mips::fixup_MICROMIPS_TLS_TPREL_HI16: |
| 599 | case Mips::fixup_MICROMIPS_TLS_TPREL_LO16: |
Vladimir Stefanovic | 64ad1cf | 2018-11-21 16:38:34 +0000 | [diff] [blame] | 600 | case Mips::fixup_MICROMIPS_JALR: |
Simon Atanasyan | 3a44bcf | 2018-06-01 16:37:42 +0000 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | |
Hervé Poussineau | d8a5fae | 2024-12-20 10:31:38 +0100 | [diff] [blame] | 605 | namespace { |
| 606 | |
| 607 | class WindowsMipsAsmBackend : public MipsAsmBackend { |
| 608 | public: |
| 609 | WindowsMipsAsmBackend(const Target &T, const MCRegisterInfo &MRI, |
| 610 | const MCSubtargetInfo &STI) |
| 611 | : MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(), false) {} |
| 612 | |
| 613 | std::unique_ptr<MCObjectTargetWriter> |
| 614 | createObjectTargetWriter() const override { |
| 615 | return createMipsWinCOFFObjectWriter(); |
| 616 | } |
| 617 | }; |
| 618 | |
| 619 | } // end anonymous namespace |
| 620 | |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 621 | MCAsmBackend *llvm::createMipsAsmBackend(const Target &T, |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 622 | const MCSubtargetInfo &STI, |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 623 | const MCRegisterInfo &MRI, |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 624 | const MCTargetOptions &Options) { |
Hervé Poussineau | d8a5fae | 2024-12-20 10:31:38 +0100 | [diff] [blame] | 625 | const Triple &TheTriple = STI.getTargetTriple(); |
| 626 | if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF()) |
| 627 | return new WindowsMipsAsmBackend(T, MRI, STI); |
| 628 | |
Simon Atanasyan | 0bd82a9 | 2019-11-02 16:33:10 +0300 | [diff] [blame] | 629 | MipsABIInfo ABI = MipsABIInfo::computeTargetABI(STI.getTargetTriple(), |
| 630 | STI.getCPU(), Options); |
| 631 | return new MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(), |
| 632 | ABI.IsN32()); |
Simon Atanasyan | 11766558 | 2017-09-21 10:44:26 +0000 | [diff] [blame] | 633 | } |