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