LLVM 24.0.0git
MipsAsmBackend.cpp
Go to the documentation of this file.
1//===-- MipsAsmBackend.cpp - Mips Asm Backend ----------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the MipsAsmBackend class.
10//
11//===----------------------------------------------------------------------===//
12//
13
20#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCSymbolELF.h"
27#include "llvm/MC/MCValue.h"
32
33using namespace llvm;
34
35// Prepare value for the target space for it
36static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
37 MCContext &Ctx) {
38
39 unsigned Kind = Fixup.getKind();
40
41 // Add/subtract and shift
42 switch (Kind) {
43 default:
44 return 0;
45 case FK_Data_2:
62 Value &= 0xffff;
63 break;
66 Ctx.reportError(Fixup.getLoc(),
67 "fixup value out of range [-32768, 65535]");
68 break;
74 case FK_Data_4:
75 case FK_Data_8:
78 break;
80 // The displacement is then divided by 4 to give us an 18 bit
81 // address range. Forcing a signed division because Value can be negative.
82 Value = (int64_t)Value / 4;
83 // We now check if Value can be encoded as a 16-bit signed immediate.
84 if (!isInt<16>(Value)) {
85 Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup");
86 return 0;
87 }
88 break;
91 // Forcing a signed division because Value can be negative.
92 Value = (int64_t)Value / 4;
93 // We now check if Value can be encoded as a 19-bit signed immediate.
94 if (!isInt<19>(Value)) {
95 Ctx.reportError(Fixup.getLoc(), "out of range PC19 fixup");
96 return 0;
97 }
98 break;
100 // So far we are only using this type for jumps.
101 // The displacement is then divided by 4 to give us an 28 bit
102 // address range.
103 Value >>= 2;
104 break;
112 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
113 Value = ((Value + 0x8000) >> 16) & 0xffff;
114 break;
117 // Get the 3rd 16-bits.
118 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
119 break;
122 // Get the 4th 16-bits.
123 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
124 break;
126 Value >>= 1;
127 break;
129 Value -= 4;
130 // Forcing a signed division because Value can be negative.
131 Value = (int64_t) Value / 2;
132 // We now check if Value can be encoded as a 7-bit signed immediate.
133 if (!isInt<7>(Value)) {
134 Ctx.reportError(Fixup.getLoc(), "out of range PC7 fixup");
135 return 0;
136 }
137 break;
139 Value -= 2;
140 // Forcing a signed division because Value can be negative.
141 Value = (int64_t) Value / 2;
142 // We now check if Value can be encoded as a 10-bit signed immediate.
143 if (!isInt<10>(Value)) {
144 Ctx.reportError(Fixup.getLoc(), "out of range PC10 fixup");
145 return 0;
146 }
147 break;
149 Value -= 4;
150 // Forcing a signed division because Value can be negative.
151 Value = (int64_t)Value / 2;
152 // We now check if Value can be encoded as a 16-bit signed immediate.
153 if (!isInt<16>(Value)) {
154 Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup");
155 return 0;
156 }
157 break;
159 // Forcing a signed division because Value can be negative.
160 Value = (int64_t)Value / 8;
161 // We now check if Value can be encoded as a 18-bit signed immediate.
162 if (!isInt<18>(Value)) {
163 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
164 return 0;
165 }
166 break;
168 // Check alignment.
169 if ((Value & 7)) {
170 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
171 }
172 // Forcing a signed division because Value can be negative.
173 Value = (int64_t)Value / 8;
174 // We now check if Value can be encoded as a 18-bit signed immediate.
175 if (!isInt<18>(Value)) {
176 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
177 return 0;
178 }
179 break;
181 // Forcing a signed division because Value can be negative.
182 Value = (int64_t) Value / 4;
183 // We now check if Value can be encoded as a 21-bit signed immediate.
184 if (!isInt<21>(Value)) {
185 Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup");
186 return 0;
187 }
188 break;
190 // Forcing a signed division because Value can be negative.
191 Value = (int64_t) Value / 4;
192 // We now check if Value can be encoded as a 26-bit signed immediate.
193 if (!isInt<26>(Value)) {
194 Ctx.reportError(Fixup.getLoc(), "out of range PC26 fixup");
195 return 0;
196 }
197 break;
199 // Forcing a signed division because Value can be negative.
200 Value = (int64_t)Value / 2;
201 // We now check if Value can be encoded as a 26-bit signed immediate.
202 if (!isInt<26>(Value)) {
203 Ctx.reportError(Fixup.getLoc(), "out of range PC26 fixup");
204 return 0;
205 }
206 break;
208 // Forcing a signed division because Value can be negative.
209 Value = (int64_t)Value / 2;
210 // We now check if Value can be encoded as a 21-bit signed immediate.
211 if (!isInt<21>(Value)) {
212 Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup");
213 return 0;
214 }
215 break;
216 }
217
218 return Value;
219}
220
221std::unique_ptr<MCObjectTargetWriter>
223 return createMipsELFObjectWriter(TheTriple, IsN32);
224}
225
226// Little-endian fixup data byte ordering:
227// mips32r2: a | b | x | x
228// microMIPS: x | x | a | b
229
230static bool needsMMLEByteOrder(unsigned Kind) {
231 return Kind != Mips::fixup_MICROMIPS_PC10_S1 &&
234}
235
236// Calculate index for microMIPS specific little endian byte order
237static unsigned calculateMMLEIndex(unsigned i) {
238 assert(i <= 3 && "Index out of range!");
239
240 return (1 - i / 2) * 2 + i % 2;
241}
242
243static bool shouldForceRelocation(const MCFixup &Fixup) {
244 const unsigned FixupKind = Fixup.getKind();
245 switch (FixupKind) {
246 default:
247 return false;
248 // All these relocations require special processing
249 // at linking time. Delegate this work to a linker.
280 return true;
281 }
282}
283
284/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
285/// data fragment, at the offset specified by the fixup and following the
286/// fixup kind as appropriate.
288 const MCValue &Target, uint8_t *Data,
289 uint64_t Value, bool IsResolved) {
290 if (shouldForceRelocation(Fixup))
291 IsResolved = false;
292 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
293 MCFixupKind Kind = Fixup.getKind();
294 MCContext &Ctx = getContext();
295 Value = adjustFixupValue(Fixup, Value, Ctx);
296
297 if (!Value)
298 return; // Doesn't change encoding.
299
300 // Where do we start in the object
301 // Number of bytes we need to fixup
302 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
303 // Used to point to big endian bytes
304 unsigned FullSize;
305
306 switch ((unsigned)Kind) {
307 case FK_Data_2:
310 FullSize = 2;
311 break;
312 case FK_Data_8:
314 FullSize = 8;
315 break;
316 case FK_Data_4:
317 default:
318 FullSize = 4;
319 break;
320 }
321
322 // Grab current value, if any, from bits.
323 uint64_t CurVal = 0;
324
325 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
326
327 for (unsigned i = 0; i != NumBytes; ++i) {
328 unsigned Idx = Endian == llvm::endianness::little
329 ? (microMipsLEByteOrder ? calculateMMLEIndex(i) : i)
330 : (FullSize - 1 - i);
331 CurVal |= (uint64_t)((uint8_t)Data[Idx]) << (i * 8);
332 }
333
334 uint64_t Mask = ((uint64_t)(-1) >>
335 (64 - getFixupKindInfo(Kind).TargetSize));
336 CurVal |= Value & Mask;
337
338 // Write out the fixed up bytes back to the code/data bits.
339 for (unsigned i = 0; i != NumBytes; ++i) {
340 unsigned Idx = Endian == llvm::endianness::little
341 ? (microMipsLEByteOrder ? calculateMMLEIndex(i) : i)
342 : (FullSize - 1 - i);
343 Data[Idx] = (uint8_t)((CurVal >> (i * 8)) & 0xff);
344 }
345}
346
347std::optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
348 unsigned Type = llvm::StringSwitch<unsigned>(Name)
349 .Case("BFD_RELOC_NONE", ELF::R_MIPS_NONE)
350 .Case("BFD_RELOC_16", ELF::R_MIPS_16)
351 .Case("BFD_RELOC_32", ELF::R_MIPS_32)
352 .Case("BFD_RELOC_64", ELF::R_MIPS_64)
353 .Default(-1u);
354 if (Type != -1u)
355 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
356
358 .Case("R_MIPS_NONE", FK_NONE)
359 .Case("R_MIPS_32", FK_Data_4)
360 .Case("R_MIPS_CALL_HI16", Mips::fixup_Mips_CALL_HI16)
361 .Case("R_MIPS_CALL_LO16", Mips::fixup_Mips_CALL_LO16)
362 .Case("R_MIPS_CALL16", Mips::fixup_Mips_CALL16)
363 .Case("R_MIPS_GOT16", Mips::fixup_Mips_GOT)
364 .Case("R_MIPS_GOT_PAGE", Mips::fixup_Mips_GOT_PAGE)
365 .Case("R_MIPS_GOT_OFST", Mips::fixup_Mips_GOT_OFST)
366 .Case("R_MIPS_GOT_DISP", Mips::fixup_Mips_GOT_DISP)
367 .Case("R_MIPS_GOT_HI16", Mips::fixup_Mips_GOT_HI16)
368 .Case("R_MIPS_GOT_LO16", Mips::fixup_Mips_GOT_LO16)
369 .Case("R_MIPS_TLS_GOTTPREL", Mips::fixup_Mips_GOTTPREL)
370 .Case("R_MIPS_TLS_DTPREL_HI16", Mips::fixup_Mips_DTPREL_HI)
371 .Case("R_MIPS_TLS_DTPREL_LO16", Mips::fixup_Mips_DTPREL_LO)
372 .Case("R_MIPS_TLS_GD", Mips::fixup_Mips_TLSGD)
373 .Case("R_MIPS_TLS_LDM", Mips::fixup_Mips_TLSLDM)
374 .Case("R_MIPS_TLS_TPREL_HI16", Mips::fixup_Mips_TPREL_HI)
375 .Case("R_MIPS_TLS_TPREL_LO16", Mips::fixup_Mips_TPREL_LO)
376 .Case("R_MICROMIPS_CALL16", Mips::fixup_MICROMIPS_CALL16)
377 .Case("R_MICROMIPS_GOT_DISP", Mips::fixup_MICROMIPS_GOT_DISP)
378 .Case("R_MICROMIPS_GOT_PAGE", Mips::fixup_MICROMIPS_GOT_PAGE)
379 .Case("R_MICROMIPS_GOT_OFST", Mips::fixup_MICROMIPS_GOT_OFST)
380 .Case("R_MICROMIPS_GOT16", Mips::fixup_MICROMIPS_GOT16)
381 .Case("R_MICROMIPS_TLS_GOTTPREL", Mips::fixup_MICROMIPS_GOTTPREL)
382 .Case("R_MICROMIPS_TLS_DTPREL_HI16",
384 .Case("R_MICROMIPS_TLS_DTPREL_LO16",
386 .Case("R_MICROMIPS_TLS_GD", Mips::fixup_MICROMIPS_TLS_GD)
387 .Case("R_MICROMIPS_TLS_LDM", Mips::fixup_MICROMIPS_TLS_LDM)
388 .Case("R_MICROMIPS_TLS_TPREL_HI16", Mips::fixup_MICROMIPS_TLS_TPREL_HI16)
389 .Case("R_MICROMIPS_TLS_TPREL_LO16", Mips::fixup_MICROMIPS_TLS_TPREL_LO16)
390 .Case("R_MIPS_JALR", Mips::fixup_Mips_JALR)
391 .Case("R_MICROMIPS_JALR", Mips::fixup_MICROMIPS_JALR)
393}
394
396 const static MCFixupKindInfo LittleEndianInfos[] = {
397 // This table *must* be in same the order of fixup_* kinds in
398 // MipsFixupKinds.h.
399 //
400 // name offset bits flags
401 // clang-format off
402 { "fixup_Mips_16", 0, 16, 0 },
403 { "fixup_Mips_32", 0, 32, 0 },
404 { "fixup_Mips_REL32", 0, 32, 0 },
405 { "fixup_Mips_GPREL32", 0, 32, 0 },
406 { "fixup_Mips_DTPREL32", 0, 32, 0 },
407 { "fixup_Mips_DTPREL64", 0, 64, 0 },
408 { "fixup_Mips_TPREL32", 0, 32, 0 },
409 { "fixup_Mips_TPREL64", 0, 64, 0 },
410 { "fixup_Mips_26", 0, 26, 0 },
411 { "fixup_Mips_HI16", 0, 16, 0 },
412 { "fixup_Mips_LO16", 0, 16, 0 },
413 { "fixup_Mips_AnyImm16", 0, 16, 0 },
414 { "fixup_Mips_GPREL16", 0, 16, 0 },
415 { "fixup_Mips_LITERAL", 0, 16, 0 },
416 { "fixup_Mips_GOT", 0, 16, 0 },
417 { "fixup_Mips_PC16", 0, 16, 0 },
418 { "fixup_Mips_CALL16", 0, 16, 0 },
419 { "fixup_Mips_SHIFT5", 6, 5, 0 },
420 { "fixup_Mips_SHIFT6", 6, 5, 0 },
421 { "fixup_Mips_64", 0, 64, 0 },
422 { "fixup_Mips_TLSGD", 0, 16, 0 },
423 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
424 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
425 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
426 { "fixup_Mips_TLSLDM", 0, 16, 0 },
427 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
428 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
429 { "fixup_Mips_Branch_PCRel", 0, 16, 0 },
430 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
431 { "fixup_MICROMIPS_GPOFF_HI",0, 16, 0 },
432 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
433 { "fixup_MICROMIPS_GPOFF_LO",0, 16, 0 },
434 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
435 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
436 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
437 { "fixup_Mips_HIGHER", 0, 16, 0 },
438 { "fixup_MICROMIPS_HIGHER", 0, 16, 0 },
439 { "fixup_Mips_HIGHEST", 0, 16, 0 },
440 { "fixup_MICROMIPS_HIGHEST", 0, 16, 0 },
441 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
442 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
443 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
444 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
445 { "fixup_Mips_PC18_S3", 0, 18, 0 },
446 { "fixup_MIPS_PC19_S2", 0, 19, 0 },
447 { "fixup_MIPS_PC21_S2", 0, 21, 0 },
448 { "fixup_MIPS_PC26_S2", 0, 26, 0 },
449 { "fixup_MIPS_PCHI16", 0, 16, 0 },
450 { "fixup_MIPS_PCLO16", 0, 16, 0 },
451 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
452 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
453 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
454 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
455 { "fixup_MICROMIPS_PC7_S1", 0, 7, 0 },
456 { "fixup_MICROMIPS_PC10_S1", 0, 10, 0 },
457 { "fixup_MICROMIPS_PC16_S1", 0, 16, 0 },
458 { "fixup_MICROMIPS_PC26_S1", 0, 26, 0 },
459 { "fixup_MICROMIPS_PC19_S2", 0, 19, 0 },
460 { "fixup_MICROMIPS_PC18_S3", 0, 18, 0 },
461 { "fixup_MICROMIPS_PC21_S1", 0, 21, 0 },
462 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
463 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
464 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
465 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
466 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
467 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
468 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
469 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
470 { "fixup_MICROMIPS_GOTTPREL", 0, 16, 0 },
471 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
472 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 },
473 { "fixup_Mips_SUB", 0, 64, 0 },
474 { "fixup_MICROMIPS_SUB", 0, 64, 0 },
475 { "fixup_Mips_JALR", 0, 32, 0 },
476 { "fixup_MICROMIPS_JALR", 0, 32, 0 },
477 // clang-format on
478 };
479 static_assert(std::size(LittleEndianInfos) == Mips::NumTargetFixupKinds,
480 "Not all MIPS little endian fixup kinds added!");
481
482 const static MCFixupKindInfo BigEndianInfos[] = {
483 // This table *must* be in same the order of fixup_* kinds in
484 // MipsFixupKinds.h.
485 //
486 // name offset bits flags
487 // clang-format off
488 { "fixup_Mips_16", 16, 16, 0 },
489 { "fixup_Mips_32", 0, 32, 0 },
490 { "fixup_Mips_REL32", 0, 32, 0 },
491 { "fixup_Mips_GPREL32", 0, 32, 0 },
492 { "fixup_Mips_DTPREL32", 0, 32, 0 },
493 { "fixup_Mips_DTPREL64", 0, 64, 0 },
494 { "fixup_Mips_TPREL32", 0, 32, 0 },
495 { "fixup_Mips_TPREL64", 0, 64, 0 },
496 { "fixup_Mips_26", 6, 26, 0 },
497 { "fixup_Mips_HI16", 16, 16, 0 },
498 { "fixup_Mips_LO16", 16, 16, 0 },
499 { "fixup_Mips_AnyImm16", 16, 16, 0 },
500 { "fixup_Mips_GPREL16", 16, 16, 0 },
501 { "fixup_Mips_LITERAL", 16, 16, 0 },
502 { "fixup_Mips_GOT", 16, 16, 0 },
503 { "fixup_Mips_PC16", 16, 16, 0 },
504 { "fixup_Mips_CALL16", 16, 16, 0 },
505 { "fixup_Mips_SHIFT5", 21, 5, 0 },
506 { "fixup_Mips_SHIFT6", 21, 5, 0 },
507 { "fixup_Mips_64", 0, 64, 0 },
508 { "fixup_Mips_TLSGD", 16, 16, 0 },
509 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
510 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
511 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
512 { "fixup_Mips_TLSLDM", 16, 16, 0 },
513 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
514 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
515 { "fixup_Mips_Branch_PCRel",16, 16, 0 },
516 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
517 { "fixup_MICROMIPS_GPOFF_HI", 16, 16, 0 },
518 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
519 { "fixup_MICROMIPS_GPOFF_LO", 16, 16, 0 },
520 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
521 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
522 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
523 { "fixup_Mips_HIGHER", 16, 16, 0 },
524 { "fixup_MICROMIPS_HIGHER", 16, 16, 0 },
525 { "fixup_Mips_HIGHEST", 16, 16, 0 },
526 { "fixup_MICROMIPS_HIGHEST",16, 16, 0 },
527 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
528 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
529 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
530 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
531 { "fixup_Mips_PC18_S3", 14, 18, 0 },
532 { "fixup_MIPS_PC19_S2", 13, 19, 0 },
533 { "fixup_MIPS_PC21_S2", 11, 21, 0 },
534 { "fixup_MIPS_PC26_S2", 6, 26, 0 },
535 { "fixup_MIPS_PCHI16", 16, 16, 0 },
536 { "fixup_MIPS_PCLO16", 16, 16, 0 },
537 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
538 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
539 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
540 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
541 { "fixup_MICROMIPS_PC7_S1", 9, 7, 0 },
542 { "fixup_MICROMIPS_PC10_S1", 6, 10, 0 },
543 { "fixup_MICROMIPS_PC16_S1",16, 16, 0 },
544 { "fixup_MICROMIPS_PC26_S1", 6, 26, 0 },
545 { "fixup_MICROMIPS_PC19_S2",13, 19, 0 },
546 { "fixup_MICROMIPS_PC18_S3",14, 18, 0 },
547 { "fixup_MICROMIPS_PC21_S1",11, 21, 0 },
548 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
549 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
550 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
551 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
552 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
553 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
554 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
555 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
556 { "fixup_MICROMIPS_GOTTPREL", 16, 16, 0 },
557 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
558 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 },
559 { "fixup_Mips_SUB", 0, 64, 0 },
560 { "fixup_MICROMIPS_SUB", 0, 64, 0 },
561 { "fixup_Mips_JALR", 0, 32, 0 },
562 { "fixup_MICROMIPS_JALR", 0, 32, 0 },
563 // clang-format on
564 };
565 static_assert(std::size(BigEndianInfos) == Mips::NumTargetFixupKinds,
566 "Not all MIPS big endian fixup kinds added!");
567
568 if (mc::isRelocation(Kind))
569 return {};
570 if (Kind < FirstTargetFixupKind)
572
574 "Invalid kind!");
575
577 return LittleEndianInfos[Kind - FirstTargetFixupKind];
578 return BigEndianInfos[Kind - FirstTargetFixupKind];
579}
580
583 const MCSubtargetInfo &STI) const {
584 return Opcode == Mips::PseudoGOTLoadGlobal;
585}
586
587// PseudoGOTLoadGlobal is emitted for a symbol that was not defined when the
588// instruction was parsed. Once the whole file has been parsed we know whether
589// the symbol is local, in which case it must use the local GOT sequence
590// rather than a global GOT entry or %call16. This mirrors the IsLocalSym
591// check in MipsAsmParser::loadAndAddSymbolAddress.
593 const MCFixup &,
594 const MCValue &Target,
595 uint64_t, bool) const {
596 const auto *Sym = static_cast<const MCSymbolELF *>(Target.getAddSym());
597 return Sym && Sym->isInSection() && Sym->getBinding() == ELF::STB_LOCAL;
598}
599
601 const MCSubtargetInfo &STI) const {
602 assert(Inst.getOpcode() == Mips::PseudoGOTLoadGlobal &&
603 "unexpected instruction to relax");
604 Inst.setOpcode(Mips::PseudoGOTLoadLocal);
605}
606
607/// WriteNopData - Write an (optimal) nop sequence of Count bytes
608/// to the given output. If the target cannot generate such a sequence,
609/// it should return an error.
610///
611/// \return - True on success.
613 const MCSubtargetInfo *STI) const {
614 // We mostly follow binutils' convention here: align to an even boundary with
615 // zero-fill padding, then emit up to one 2-byte NOP. Use move $zero, $zero
616 // for microMIPS, or zero-fill for standard MIPS. Pad the remainder with
617 // 4-byte NOPs.
618
619 // An odd byte is data padding. Align to an instruction boundary first.
620 if (Count % 2) {
621 OS.write_zeros(1);
622 Count -= 1;
623 }
624
625 if (Count % 4 == 2) {
626 // Use move $zero, $zero for a microMIPS 16-bit NOP. For standard MIPS,
627 // this halfword is data padding.
628 uint16_t Nop = STI && STI->hasFeature(Mips::FeatureMicroMips) ? 0x0c00 : 0;
630 Count -= 2;
631 }
632
633 // Zero encodes a 32-bit NOP in both standard MIPS and microMIPS.
634 OS.write_zeros(Count);
635 return true;
636}
637
638namespace {
639
640class WindowsMipsAsmBackend : public MipsAsmBackend {
641public:
642 WindowsMipsAsmBackend(const Target &T, const MCRegisterInfo &MRI,
643 const MCSubtargetInfo &STI)
644 : MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(), false) {}
645
646 std::unique_ptr<MCObjectTargetWriter>
647 createObjectTargetWriter() const override {
649 }
650};
651
652} // end anonymous namespace
653
655 const MCSubtargetInfo &STI,
656 const MCRegisterInfo &MRI,
657 const MCTargetOptions &Options) {
658 const Triple &TheTriple = STI.getTargetTriple();
659 if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF())
660 return new WindowsMipsAsmBackend(T, MRI, STI);
661
663 Options.getABIName());
664 return new MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(),
665 ABI.IsN32());
666}
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
static bool shouldForceRelocation(const MCFixup &Fixup)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define T
static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value, MCContext &Ctx)
static unsigned calculateMMLEIndex(unsigned i)
static bool needsMMLEByteOrder(unsigned Kind)
MachineRegisterInfo & MRI
SI Fold Operands
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Generic interface to target specific assembler backends.
const llvm::endianness Endian
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCContext & getContext() const
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &, uint64_t &Value, bool IsResolved)
Context object for machine code objects.
Definition MCContext.h:84
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
void setOpcode(unsigned Op)
Definition MCInst.h:201
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
StringRef getCPU() const
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition MCSymbol.h:237
static MipsABIInfo computeTargetABI(const Triple &TT, StringRef ABIName)
void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const override
Relax the instruction in the given fragment to the next wider instruction.
bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override
WriteNopData - Write an (optimal) nop sequence of Count bytes to the given output.
bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &, const MCValue &, uint64_t, bool) const override
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const override
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
std::optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved) override
ApplyFixup - Apply the Value for given Fixup into the provided data fragment, at the offset specified...
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition Triple.h:872
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:780
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
@ STB_LOCAL
Definition ELF.h:1260
@ fixup_MICROMIPS_TLS_TPREL_LO16
@ fixup_MICROMIPS_TLS_TPREL_HI16
@ fixup_MICROMIPS_TLS_DTPREL_HI16
@ fixup_MICROMIPS_TLS_DTPREL_LO16
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createMipsELFObjectWriter(const Triple &TT, bool IsN32)
Construct a Mips ELF object writer.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
static StringRef getCPU(StringRef CPU)
Processes a CPU name.
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
std::unique_ptr< MCObjectTargetWriter > createMipsWinCOFFObjectWriter()
Construct a Mips Win COFF object writer.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
static Lanai::Fixups FixupKind(const MCExpr *Expr)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:37
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32
MCAsmBackend * createMipsAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target independent information on a fixup kind.
uint8_t TargetSize
The number of bits written by this fixup.