blob: 8ace2895d6813358288cf7bd16f821e4525bca27 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
Rafael Espindola1dc45d82011-12-22 03:03:17 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MCTargetDesc/MipsFixupKinds.h"
11#include "MCTargetDesc/MipsMCTargetDesc.h"
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000012#include "llvm/ADT/STLExtras.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/ELF.h"
Simon Atanasyanb243d8d2018-12-29 10:09:55 +000014#include "llvm/MC/MCContext.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000015#include "llvm/MC/MCELFObjectWriter.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000016#include "llvm/MC/MCFixup.h"
Lang Hames60fbc7c2017-10-10 16:28:07 +000017#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000018#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
Daniel Sandersa463d312016-05-06 13:49:25 +000021#include "llvm/Support/Debug.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000022#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000023#include "llvm/Support/MathExtras.h"
24#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
26#include <cassert>
27#include <cstdint>
28#include <iterator>
29#include <list>
30#include <utility>
Rafael Espindola1dc45d82011-12-22 03:03:17 +000031
Daniel Sandersa463d312016-05-06 13:49:25 +000032#define DEBUG_TYPE "mips-elf-object-writer"
33
Rafael Espindola1dc45d82011-12-22 03:03:17 +000034using namespace llvm;
35
36namespace {
Eugene Zelenkodde94e42017-01-30 23:21:32 +000037
Daniel Sandersa463d312016-05-06 13:49:25 +000038/// Holds additional information needed by the relocation ordering algorithm.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000039struct MipsRelocationEntry {
Daniel Sandersa463d312016-05-06 13:49:25 +000040 const ELFRelocationEntry R; ///< The relocation.
Eugene Zelenkodde94e42017-01-30 23:21:32 +000041 bool Matched = false; ///< Is this relocation part of a match.
Daniel Sandersa463d312016-05-06 13:49:25 +000042
Eugene Zelenkodde94e42017-01-30 23:21:32 +000043 MipsRelocationEntry(const ELFRelocationEntry &R) : R(R) {}
Daniel Sandersa463d312016-05-06 13:49:25 +000044
45 void print(raw_ostream &Out) const {
46 R.print(Out);
47 Out << ", Matched=" << Matched;
48 }
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000049};
50
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000051#ifndef NDEBUG
Daniel Sandersa463d312016-05-06 13:49:25 +000052raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
53 RHS.print(OS);
54 return OS;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000055}
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000056#endif
Rafael Espindola1dc45d82011-12-22 03:03:17 +000057
Daniel Sandersa463d312016-05-06 13:49:25 +000058class MipsELFObjectWriter : public MCELFObjectTargetWriter {
59public:
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000060 MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64);
Daniel Sandersa463d312016-05-06 13:49:25 +000061
Eugene Zelenkodde94e42017-01-30 23:21:32 +000062 ~MipsELFObjectWriter() override = default;
Daniel Sandersa463d312016-05-06 13:49:25 +000063
64 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
65 const MCFixup &Fixup, bool IsPCRel) const override;
66 bool needsRelocateWithSymbol(const MCSymbol &Sym,
67 unsigned Type) const override;
Eugene Zelenkodde94e42017-01-30 23:21:32 +000068 void sortRelocs(const MCAssembler &Asm,
69 std::vector<ELFRelocationEntry> &Relocs) override;
Daniel Sandersa463d312016-05-06 13:49:25 +000070};
71
Eugene Zelenkodde94e42017-01-30 23:21:32 +000072/// The possible results of the Predicate function used by find_best.
73enum FindBestPredicateResult {
74 FindBest_NoMatch = 0, ///< The current element is not a match.
75 FindBest_Match, ///< The current element is a match but better ones are
76 /// possible.
77 FindBest_PerfectMatch, ///< The current element is an unbeatable match.
78};
79
80} // end anonymous namespace
81
Daniel Sandersa463d312016-05-06 13:49:25 +000082/// Copy elements in the range [First, Last) to d1 when the predicate is true or
83/// d2 when the predicate is false. This is essentially both std::copy_if and
84/// std::remove_copy_if combined into a single pass.
85template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
Eugene Zelenkodde94e42017-01-30 23:21:32 +000086static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
87 OutputIt1 d1, OutputIt2 d2,
88 UnaryPredicate Predicate) {
Daniel Sandersa463d312016-05-06 13:49:25 +000089 for (InputIt I = First; I != Last; ++I) {
90 if (Predicate(*I)) {
91 *d1 = *I;
92 d1++;
93 } else {
94 *d2 = *I;
95 d2++;
96 }
97 }
98
99 return std::make_pair(d1, d2);
100}
101
Daniel Sandersa463d312016-05-06 13:49:25 +0000102/// Find the best match in the range [First, Last).
103///
104/// An element matches when Predicate(X) returns FindBest_Match or
105/// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
106/// the search. BetterThan(A, B) is a comparator that returns true when A is a
107/// better match than B. The return value is the position of the best match.
108///
109/// This is similar to std::find_if but finds the best of multiple possible
110/// matches.
111template <class InputIt, class UnaryPredicate, class Comparator>
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000112static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
113 Comparator BetterThan) {
Daniel Sandersa463d312016-05-06 13:49:25 +0000114 InputIt Best = Last;
115
116 for (InputIt I = First; I != Last; ++I) {
117 unsigned Matched = Predicate(*I);
118 if (Matched != FindBest_NoMatch) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000119 LLVM_DEBUG(dbgs() << std::distance(First, I) << " is a match (";
120 I->print(dbgs()); dbgs() << ")\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000121 if (Best == Last || BetterThan(*I, *Best)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000122 LLVM_DEBUG(dbgs() << ".. and it beats the last one\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000123 Best = I;
124 }
125 }
126 if (Matched == FindBest_PerfectMatch) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000127 LLVM_DEBUG(dbgs() << ".. and it is unbeatable\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000128 break;
129 }
130 }
131
132 return Best;
133}
134
135/// Determine the low relocation that matches the given relocation.
136/// If the relocation does not need a low relocation then the return value
137/// is ELF::R_MIPS_NONE.
138///
139/// The relocations that need a matching low part are
140/// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and
141/// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.
142static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
143 unsigned Type = Reloc.Type;
144 if (Type == ELF::R_MIPS_HI16)
145 return ELF::R_MIPS_LO16;
146 if (Type == ELF::R_MICROMIPS_HI16)
147 return ELF::R_MICROMIPS_LO16;
148 if (Type == ELF::R_MIPS16_HI16)
149 return ELF::R_MIPS16_LO16;
150
Simon Atanasyan3a44bcf2018-06-01 16:37:42 +0000151 if (Reloc.OriginalSymbol &&
152 Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)
Daniel Sandersa463d312016-05-06 13:49:25 +0000153 return ELF::R_MIPS_NONE;
154
155 if (Type == ELF::R_MIPS_GOT16)
156 return ELF::R_MIPS_LO16;
157 if (Type == ELF::R_MICROMIPS_GOT16)
158 return ELF::R_MICROMIPS_LO16;
159 if (Type == ELF::R_MIPS16_GOT16)
160 return ELF::R_MIPS16_LO16;
161
162 return ELF::R_MIPS_NONE;
163}
164
165/// Determine whether a relocation (X) matches the one given in R.
166///
167/// A relocation matches if:
168/// - It's type matches that of a corresponding low part. This is provided in
169/// MatchingType for efficiency.
170/// - It's based on the same symbol.
171/// - It's offset of greater or equal to that of the one given in R.
172/// It should be noted that this rule assumes the programmer does not use
173/// offsets that exceed the alignment of the symbol. The carry-bit will be
174/// incorrect if this is not true.
175///
176/// A matching relocation is unbeatable if:
177/// - It is not already involved in a match.
178/// - It's offset is exactly that of the one given in R.
Daniel Sanders108823b2016-05-09 15:50:15 +0000179static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
180 const ELFRelocationEntry &R,
181 unsigned MatchingType) {
Daniel Sandersa463d312016-05-06 13:49:25 +0000182 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
183 if (!X.Matched &&
184 X.R.OriginalAddend == R.OriginalAddend)
185 return FindBest_PerfectMatch;
186 else if (X.R.OriginalAddend >= R.OriginalAddend)
187 return FindBest_Match;
188 }
189 return FindBest_NoMatch;
190}
191
192/// Determine whether Candidate or PreviousBest is the better match.
193/// The return value is true if Candidate is the better match.
194///
195/// A matching relocation is a better match if:
196/// - It has a smaller addend.
197/// - It is not already involved in a match.
198static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
199 const MipsRelocationEntry &PreviousBest) {
200 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
201 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
202 return PreviousBest.Matched && !Candidate.Matched;
203}
204
205#ifndef NDEBUG
206/// Print all the relocations.
207template <class Container>
208static void dumpRelocs(const char *Prefix, const Container &Relocs) {
209 for (const auto &R : Relocs)
210 dbgs() << Prefix << R << "\n";
211}
212#endif
213
Simon Atanasyan6d795862017-09-07 12:54:26 +0000214MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000215 bool HasRelocationAddend, bool Is64)
Simon Atanasyan9f676a72017-09-21 14:04:47 +0000216 : MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {}
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000217
Rafael Espindola8340f942016-01-13 22:56:57 +0000218unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
219 const MCValue &Target,
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000220 const MCFixup &Fixup,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000221 bool IsPCRel) const {
Daniel Sanders58405d82015-06-16 13:46:26 +0000222 // Determine the type of the relocation.
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000223 unsigned Kind = (unsigned)Fixup.getKind();
224
225 switch (Kind) {
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000226 case Mips::fixup_Mips_NONE:
227 return ELF::R_MIPS_NONE;
Stefan Maksimovic5481c212018-01-11 10:07:47 +0000228 case FK_Data_1:
Simon Atanasyanb243d8d2018-12-29 10:09:55 +0000229 Ctx.reportError(Fixup.getLoc(),
230 "MIPS does not support one byte relocations");
231 return ELF::R_MIPS_NONE;
Daniel Sanders58405d82015-06-16 13:46:26 +0000232 case Mips::fixup_Mips_16:
233 case FK_Data_2:
234 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000235 case Mips::fixup_Mips_32:
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000236 case FK_Data_4:
Rafael Espindola7f7caf92015-06-01 15:10:51 +0000237 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
Daniel Sanders58405d82015-06-16 13:46:26 +0000238 }
239
240 if (IsPCRel) {
241 switch (Kind) {
Simon Atanasyana6424e72018-12-29 10:10:02 +0000242 case FK_Data_8:
243 Ctx.reportError(Fixup.getLoc(),
244 "MIPS does not support 64-bit PC-relative relocations");
245 return ELF::R_MIPS_NONE;
Daniel Sanders58405d82015-06-16 13:46:26 +0000246 case Mips::fixup_Mips_Branch_PCRel:
247 case Mips::fixup_Mips_PC16:
248 return ELF::R_MIPS_PC16;
249 case Mips::fixup_MICROMIPS_PC7_S1:
250 return ELF::R_MICROMIPS_PC7_S1;
251 case Mips::fixup_MICROMIPS_PC10_S1:
252 return ELF::R_MICROMIPS_PC10_S1;
253 case Mips::fixup_MICROMIPS_PC16_S1:
254 return ELF::R_MICROMIPS_PC16_S1;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000255 case Mips::fixup_MICROMIPS_PC26_S1:
256 return ELF::R_MICROMIPS_PC26_S1;
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000257 case Mips::fixup_MICROMIPS_PC19_S2:
258 return ELF::R_MICROMIPS_PC19_S2;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000259 case Mips::fixup_MICROMIPS_PC18_S3:
260 return ELF::R_MICROMIPS_PC18_S3;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000261 case Mips::fixup_MICROMIPS_PC21_S1:
262 return ELF::R_MICROMIPS_PC21_S1;
Daniel Sanders58405d82015-06-16 13:46:26 +0000263 case Mips::fixup_MIPS_PC19_S2:
264 return ELF::R_MIPS_PC19_S2;
265 case Mips::fixup_MIPS_PC18_S3:
266 return ELF::R_MIPS_PC18_S3;
267 case Mips::fixup_MIPS_PC21_S2:
268 return ELF::R_MIPS_PC21_S2;
269 case Mips::fixup_MIPS_PC26_S2:
270 return ELF::R_MIPS_PC26_S2;
271 case Mips::fixup_MIPS_PCHI16:
272 return ELF::R_MIPS_PCHI16;
273 case Mips::fixup_MIPS_PCLO16:
274 return ELF::R_MIPS_PCLO16;
275 }
276
277 llvm_unreachable("invalid PC-relative fixup kind!");
278 }
279
280 switch (Kind) {
Daniel Sanders60f1db02015-03-13 12:45:09 +0000281 case Mips::fixup_Mips_64:
Jack Carter4c583812012-08-07 00:01:14 +0000282 case FK_Data_8:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000283 return ELF::R_MIPS_64;
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000284 case FK_DTPRel_4:
285 return ELF::R_MIPS_TLS_DTPREL32;
286 case FK_DTPRel_8:
287 return ELF::R_MIPS_TLS_DTPREL64;
288 case FK_TPRel_4:
289 return ELF::R_MIPS_TLS_TPREL32;
290 case FK_TPRel_8:
291 return ELF::R_MIPS_TLS_TPREL64;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000292 case FK_GPRel_4:
Simon Atanasyan9f676a72017-09-21 14:04:47 +0000293 if (is64Bit()) {
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000294 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterf2385102013-01-15 01:08:02 +0000295 Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
296 Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
297 Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000298 return Type;
Jack Carterf2385102013-01-15 01:08:02 +0000299 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000300 return ELF::R_MIPS_GPREL32;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000301 case Mips::fixup_Mips_GPREL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000302 return ELF::R_MIPS_GPREL16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000303 case Mips::fixup_Mips_26:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000304 return ELF::R_MIPS_26;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000305 case Mips::fixup_Mips_CALL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000306 return ELF::R_MIPS_CALL16;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000307 case Mips::fixup_Mips_GOT:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000308 return ELF::R_MIPS_GOT16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000309 case Mips::fixup_Mips_HI16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000310 return ELF::R_MIPS_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000311 case Mips::fixup_Mips_LO16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000312 return ELF::R_MIPS_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000313 case Mips::fixup_Mips_TLSGD:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000314 return ELF::R_MIPS_TLS_GD;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000315 case Mips::fixup_Mips_GOTTPREL:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000316 return ELF::R_MIPS_TLS_GOTTPREL;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000317 case Mips::fixup_Mips_TPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000318 return ELF::R_MIPS_TLS_TPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000319 case Mips::fixup_Mips_TPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000320 return ELF::R_MIPS_TLS_TPREL_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000321 case Mips::fixup_Mips_TLSLDM:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000322 return ELF::R_MIPS_TLS_LDM;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000323 case Mips::fixup_Mips_DTPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000324 return ELF::R_MIPS_TLS_DTPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000325 case Mips::fixup_Mips_DTPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000326 return ELF::R_MIPS_TLS_DTPREL_LO16;
Jack Carterb9f9de92012-06-27 22:48:25 +0000327 case Mips::fixup_Mips_GOT_PAGE:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000328 return ELF::R_MIPS_GOT_PAGE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000329 case Mips::fixup_Mips_GOT_OFST:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000330 return ELF::R_MIPS_GOT_OFST;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000331 case Mips::fixup_Mips_GOT_DISP:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000332 return ELF::R_MIPS_GOT_DISP;
333 case Mips::fixup_Mips_GPOFF_HI: {
334 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000335 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
336 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
337 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000338 return Type;
339 }
Simon Atanasyana1d69f92018-05-29 11:33:54 +0000340 case Mips::fixup_MICROMIPS_GPOFF_HI: {
341 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
342 Type = setRType((unsigned)ELF::R_MICROMIPS_GPREL16, Type);
343 Type = setRType2((unsigned)ELF::R_MICROMIPS_SUB, Type);
344 Type = setRType3((unsigned)ELF::R_MICROMIPS_HI16, Type);
345 return Type;
346 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000347 case Mips::fixup_Mips_GPOFF_LO: {
348 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000349 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
350 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
351 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000352 return Type;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000353 }
Simon Atanasyana1d69f92018-05-29 11:33:54 +0000354 case Mips::fixup_MICROMIPS_GPOFF_LO: {
355 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
356 Type = setRType((unsigned)ELF::R_MICROMIPS_GPREL16, Type);
357 Type = setRType2((unsigned)ELF::R_MICROMIPS_SUB, Type);
358 Type = setRType3((unsigned)ELF::R_MICROMIPS_LO16, Type);
359 return Type;
360 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000361 case Mips::fixup_Mips_HIGHER:
362 return ELF::R_MIPS_HIGHER;
363 case Mips::fixup_Mips_HIGHEST:
364 return ELF::R_MIPS_HIGHEST;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000365 case Mips::fixup_Mips_SUB:
366 return ELF::R_MIPS_SUB;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000367 case Mips::fixup_Mips_GOT_HI16:
368 return ELF::R_MIPS_GOT_HI16;
369 case Mips::fixup_Mips_GOT_LO16:
370 return ELF::R_MIPS_GOT_LO16;
371 case Mips::fixup_Mips_CALL_HI16:
372 return ELF::R_MIPS_CALL_HI16;
373 case Mips::fixup_Mips_CALL_LO16:
374 return ELF::R_MIPS_CALL_LO16;
375 case Mips::fixup_MICROMIPS_26_S1:
376 return ELF::R_MICROMIPS_26_S1;
377 case Mips::fixup_MICROMIPS_HI16:
378 return ELF::R_MICROMIPS_HI16;
379 case Mips::fixup_MICROMIPS_LO16:
380 return ELF::R_MICROMIPS_LO16;
381 case Mips::fixup_MICROMIPS_GOT16:
382 return ELF::R_MICROMIPS_GOT16;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000383 case Mips::fixup_MICROMIPS_CALL16:
384 return ELF::R_MICROMIPS_CALL16;
385 case Mips::fixup_MICROMIPS_GOT_DISP:
386 return ELF::R_MICROMIPS_GOT_DISP;
387 case Mips::fixup_MICROMIPS_GOT_PAGE:
388 return ELF::R_MICROMIPS_GOT_PAGE;
389 case Mips::fixup_MICROMIPS_GOT_OFST:
390 return ELF::R_MICROMIPS_GOT_OFST;
391 case Mips::fixup_MICROMIPS_TLS_GD:
392 return ELF::R_MICROMIPS_TLS_GD;
393 case Mips::fixup_MICROMIPS_TLS_LDM:
394 return ELF::R_MICROMIPS_TLS_LDM;
395 case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
396 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
397 case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
398 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
Simon Atanasyan3979f432017-04-30 04:27:23 +0000399 case Mips::fixup_MICROMIPS_GOTTPREL:
400 return ELF::R_MICROMIPS_TLS_GOTTPREL;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000401 case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
402 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
403 case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
404 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000405 case Mips::fixup_MICROMIPS_SUB:
406 return ELF::R_MICROMIPS_SUB;
Simon Atanasyan6be87bc2018-05-29 10:27:44 +0000407 case Mips::fixup_MICROMIPS_HIGHER:
408 return ELF::R_MICROMIPS_HIGHER;
409 case Mips::fixup_MICROMIPS_HIGHEST:
410 return ELF::R_MICROMIPS_HIGHEST;
Vladimir Stefanovic64ad1cf2018-11-21 16:38:34 +0000411 case Mips::fixup_Mips_JALR:
412 return ELF::R_MIPS_JALR;
413 case Mips::fixup_MICROMIPS_JALR:
414 return ELF::R_MICROMIPS_JALR;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000415 }
Daniel Sanders58405d82015-06-16 13:46:26 +0000416
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000417 llvm_unreachable("invalid fixup kind!");
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000418}
419
Daniel Sandersa463d312016-05-06 13:49:25 +0000420/// Sort relocation table entries by offset except where another order is
421/// required by the MIPS ABI.
422///
423/// MIPS has a few relocations that have an AHL component in the expression used
424/// to evaluate them. This AHL component is an addend with the same number of
425/// bits as a symbol value but not all of our ABI's are able to supply a
426/// sufficiently sized addend in a single relocation.
427///
428/// The O32 ABI for example, uses REL relocations which store the addend in the
429/// section data. All the relocations with AHL components affect 16-bit fields
430/// so the addend for a single relocation is limited to 16-bit. This ABI
431/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
432/// R_MIPS_LO16) and distributing the addend between the linked relocations. The
433/// ABI mandates that such relocations must be next to each other in a
434/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
435/// matching R_MIPS_LO16) but the rule is less strict in practice.
436///
437/// The de facto standard is lenient in the following ways:
438/// - 'Immediately following' does not refer to the next relocation entry but
439/// the next matching relocation.
440/// - There may be multiple high parts relocations for one low part relocation.
441/// - There may be multiple low part relocations for one high part relocation.
442/// - The AHL addend in each part does not have to be exactly equal as long as
443/// the difference does not affect the carry bit from bit 15 into 16. This is
444/// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
445/// both halves of a long long.
446///
447/// See getMatchingLoType() for a description of which high part relocations
448/// match which low part relocations. One particular thing to note is that
449/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
450/// symbols.
451///
452/// It should also be noted that this function is not affected by whether
453/// the symbol was kept or rewritten into a section-relative equivalent. We
454/// always match using the expressions from the source.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000455void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
456 std::vector<ELFRelocationEntry> &Relocs) {
Simon Dardis8fe36cd2016-12-05 12:55:19 +0000457 // We do not need to sort the relocation table for RELA relocations which
458 // N32/N64 uses as the relocation addend contains the value we require,
459 // rather than it being split across a pair of relocations.
460 if (hasRelocationAddend())
461 return;
462
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000463 if (Relocs.size() < 2)
464 return;
465
Daniel Sandersa463d312016-05-06 13:49:25 +0000466 // Sort relocations by the address they are applied to.
Fangrui Song0cac7262018-09-27 02:13:45 +0000467 llvm::sort(Relocs,
Mandeep Singh Grang10d8b852018-03-29 19:05:26 +0000468 [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
469 return A.Offset < B.Offset;
470 });
Rafael Espindolaf44db242015-12-17 16:22:06 +0000471
Daniel Sandersa463d312016-05-06 13:49:25 +0000472 std::list<MipsRelocationEntry> Sorted;
473 std::list<ELFRelocationEntry> Remainder;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000474
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000475 LLVM_DEBUG(dumpRelocs("R: ", Relocs));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000476
Daniel Sandersa463d312016-05-06 13:49:25 +0000477 // Separate the movable relocations (AHL relocations using the high bits) from
478 // the immobile relocations (everything else). This does not preserve high/low
479 // matches that already existed in the input.
480 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
481 std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
482 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
483 });
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000484
Daniel Sandersa463d312016-05-06 13:49:25 +0000485 for (auto &R : Remainder) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000486 LLVM_DEBUG(dbgs() << "Matching: " << R << "\n");
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000487
Daniel Sandersa463d312016-05-06 13:49:25 +0000488 unsigned MatchingType = getMatchingLoType(R);
489 assert(MatchingType != ELF::R_MIPS_NONE &&
490 "Wrong list for reloc that doesn't need a match");
491
492 // Find the best matching relocation for the current high part.
493 // See isMatchingReloc for a description of a matching relocation and
494 // compareMatchingRelocs for a description of what 'best' means.
495 auto InsertionPoint =
496 find_best(Sorted.begin(), Sorted.end(),
497 [&R, &MatchingType](const MipsRelocationEntry &X) {
498 return isMatchingReloc(X, R, MatchingType);
499 },
500 compareMatchingRelocs);
501
502 // If we matched then insert the high part in front of the match and mark
503 // both relocations as being involved in a match. We only mark the high
504 // part for cosmetic reasons in the debug output.
505 //
506 // If we failed to find a match then the high part is orphaned. This is not
507 // permitted since the relocation cannot be evaluated without knowing the
508 // carry-in. We can sometimes handle this using a matching low part that is
509 // already used in a match but we already cover that case in
510 // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
511 // should insert the high part at the end of the list. This will cause the
512 // linker to fail but the alternative is to cause the linker to bind the
513 // high part to a semi-matching low part and silently calculate the wrong
514 // value. Unfortunately we have no means to warn the user that we did this
515 // so leave it up to the linker to complain about it.
516 if (InsertionPoint != Sorted.end())
517 InsertionPoint->Matched = true;
518 Sorted.insert(InsertionPoint, R)->Matched = true;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000519 }
520
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000521 LLVM_DEBUG(dumpRelocs("S: ", Sorted));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000522
Daniel Sandersa463d312016-05-06 13:49:25 +0000523 assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
524
525 // Overwrite the original vector with the sorted elements. The caller expects
526 // them in reverse order.
527 unsigned CopyTo = 0;
528 for (const auto &R : reverse(Sorted))
529 Relocs[CopyTo++] = R.R;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000530}
531
Rafael Espindolaece40ca2015-05-29 18:26:09 +0000532bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
533 unsigned Type) const {
Daniel Sanders55d38332016-05-12 10:55:00 +0000534 // If it's a compound relocation for N64 then we need the relocation if any
535 // sub-relocation needs it.
Daniel Sanders2225d942016-05-10 12:17:04 +0000536 if (!isUInt<8>(Type))
Daniel Sanders55d38332016-05-12 10:55:00 +0000537 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
538 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
539 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
Daniel Sanders2225d942016-05-10 12:17:04 +0000540
Rafael Espindola5904e122014-03-29 06:26:49 +0000541 switch (Type) {
542 default:
Daniel Sanders2225d942016-05-10 12:17:04 +0000543 errs() << Type << "\n";
544 llvm_unreachable("Unexpected relocation");
Rafael Espindola5904e122014-03-29 06:26:49 +0000545 return true;
546
Daniel Sanders2225d942016-05-10 12:17:04 +0000547 // This relocation doesn't affect the section data.
548 case ELF::R_MIPS_NONE:
549 return false;
550
Daniel Sanders3d000562016-05-09 10:21:14 +0000551 // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
552 // by the static linker by matching the symbol and offset.
553 // We only see one relocation at a time but it's still safe to relocate with
554 // the section so long as both relocations make the same decision.
555 //
556 // Some older linkers may require the symbol for particular cases. Such cases
557 // are not supported yet but can be added as required.
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000558 case ELF::R_MIPS_GOT16:
559 case ELF::R_MIPS16_GOT16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000560 case ELF::R_MICROMIPS_GOT16:
Simon Dardisca74dd72017-01-27 11:36:52 +0000561 case ELF::R_MIPS_HIGHER:
562 case ELF::R_MIPS_HIGHEST:
Rafael Espindola5904e122014-03-29 06:26:49 +0000563 case ELF::R_MIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000564 case ELF::R_MIPS16_HI16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000565 case ELF::R_MICROMIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000566 case ELF::R_MIPS_LO16:
567 case ELF::R_MIPS16_LO16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000568 case ELF::R_MICROMIPS_LO16:
Daniel Sanders45533b42016-05-11 15:44:23 +0000569 // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
570 // we neglect to handle the adjustment to the LSB of the addend that
571 // it causes in applyFixup() and similar.
572 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
573 return true;
Daniel Sanders3d000562016-05-09 10:21:14 +0000574 return false;
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000575
Daniel Sanders38784122016-07-19 10:58:06 +0000576 case ELF::R_MIPS_GOT_PAGE:
577 case ELF::R_MICROMIPS_GOT_PAGE:
578 case ELF::R_MIPS_GOT_OFST:
579 case ELF::R_MICROMIPS_GOT_OFST:
Scott Egertond1aeb052016-02-15 16:11:51 +0000580 case ELF::R_MIPS_16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000581 case ELF::R_MIPS_32:
Daniel Sanders5fb391c2016-05-12 13:39:13 +0000582 case ELF::R_MIPS_GPREL32:
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000583 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
Zoran Jovanovic10646912014-12-30 22:04:16 +0000584 return true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000585 LLVM_FALLTHROUGH;
Zoran Jovanovic10646912014-12-30 22:04:16 +0000586 case ELF::R_MIPS_26:
Rafael Espindola5904e122014-03-29 06:26:49 +0000587 case ELF::R_MIPS_64:
588 case ELF::R_MIPS_GPREL16:
Daniel Sanders3d000562016-05-09 10:21:14 +0000589 case ELF::R_MIPS_PC16:
Daniel Sanders55d38332016-05-12 10:55:00 +0000590 case ELF::R_MIPS_SUB:
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000591 return false;
Daniel Sanders2225d942016-05-10 12:17:04 +0000592
593 // FIXME: Many of these relocations should probably return false but this
594 // hasn't been confirmed to be safe yet.
595 case ELF::R_MIPS_REL32:
596 case ELF::R_MIPS_LITERAL:
597 case ELF::R_MIPS_CALL16:
598 case ELF::R_MIPS_SHIFT5:
599 case ELF::R_MIPS_SHIFT6:
600 case ELF::R_MIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000601 case ELF::R_MIPS_GOT_HI16:
602 case ELF::R_MIPS_GOT_LO16:
Daniel Sanders2225d942016-05-10 12:17:04 +0000603 case ELF::R_MIPS_INSERT_A:
604 case ELF::R_MIPS_INSERT_B:
605 case ELF::R_MIPS_DELETE:
Daniel Sanders2225d942016-05-10 12:17:04 +0000606 case ELF::R_MIPS_CALL_HI16:
607 case ELF::R_MIPS_CALL_LO16:
608 case ELF::R_MIPS_SCN_DISP:
609 case ELF::R_MIPS_REL16:
610 case ELF::R_MIPS_ADD_IMMEDIATE:
611 case ELF::R_MIPS_PJUMP:
612 case ELF::R_MIPS_RELGOT:
613 case ELF::R_MIPS_JALR:
614 case ELF::R_MIPS_TLS_DTPMOD32:
615 case ELF::R_MIPS_TLS_DTPREL32:
616 case ELF::R_MIPS_TLS_DTPMOD64:
617 case ELF::R_MIPS_TLS_DTPREL64:
618 case ELF::R_MIPS_TLS_GD:
619 case ELF::R_MIPS_TLS_LDM:
620 case ELF::R_MIPS_TLS_DTPREL_HI16:
621 case ELF::R_MIPS_TLS_DTPREL_LO16:
622 case ELF::R_MIPS_TLS_GOTTPREL:
623 case ELF::R_MIPS_TLS_TPREL32:
624 case ELF::R_MIPS_TLS_TPREL64:
625 case ELF::R_MIPS_TLS_TPREL_HI16:
626 case ELF::R_MIPS_TLS_TPREL_LO16:
627 case ELF::R_MIPS_GLOB_DAT:
628 case ELF::R_MIPS_PC21_S2:
629 case ELF::R_MIPS_PC26_S2:
630 case ELF::R_MIPS_PC18_S3:
631 case ELF::R_MIPS_PC19_S2:
632 case ELF::R_MIPS_PCHI16:
633 case ELF::R_MIPS_PCLO16:
634 case ELF::R_MIPS_COPY:
635 case ELF::R_MIPS_JUMP_SLOT:
636 case ELF::R_MIPS_NUM:
637 case ELF::R_MIPS_PC32:
638 case ELF::R_MIPS_EH:
639 case ELF::R_MICROMIPS_26_S1:
640 case ELF::R_MICROMIPS_GPREL16:
641 case ELF::R_MICROMIPS_LITERAL:
642 case ELF::R_MICROMIPS_PC7_S1:
643 case ELF::R_MICROMIPS_PC10_S1:
644 case ELF::R_MICROMIPS_PC16_S1:
645 case ELF::R_MICROMIPS_CALL16:
646 case ELF::R_MICROMIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000647 case ELF::R_MICROMIPS_GOT_HI16:
648 case ELF::R_MICROMIPS_GOT_LO16:
649 case ELF::R_MICROMIPS_SUB:
650 case ELF::R_MICROMIPS_HIGHER:
651 case ELF::R_MICROMIPS_HIGHEST:
652 case ELF::R_MICROMIPS_CALL_HI16:
653 case ELF::R_MICROMIPS_CALL_LO16:
654 case ELF::R_MICROMIPS_SCN_DISP:
655 case ELF::R_MICROMIPS_JALR:
656 case ELF::R_MICROMIPS_HI0_LO16:
657 case ELF::R_MICROMIPS_TLS_GD:
658 case ELF::R_MICROMIPS_TLS_LDM:
659 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
660 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
661 case ELF::R_MICROMIPS_TLS_GOTTPREL:
662 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
663 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
664 case ELF::R_MICROMIPS_GPREL7_S2:
665 case ELF::R_MICROMIPS_PC23_S2:
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000666 case ELF::R_MICROMIPS_PC21_S1:
Daniel Sanders2225d942016-05-10 12:17:04 +0000667 case ELF::R_MICROMIPS_PC26_S1:
668 case ELF::R_MICROMIPS_PC18_S3:
669 case ELF::R_MICROMIPS_PC19_S2:
670 return true;
671
672 // FIXME: Many of these should probably return false but MIPS16 isn't
673 // supported by the integrated assembler.
674 case ELF::R_MIPS16_26:
675 case ELF::R_MIPS16_GPREL:
676 case ELF::R_MIPS16_CALL16:
677 case ELF::R_MIPS16_TLS_GD:
678 case ELF::R_MIPS16_TLS_LDM:
679 case ELF::R_MIPS16_TLS_DTPREL_HI16:
680 case ELF::R_MIPS16_TLS_DTPREL_LO16:
681 case ELF::R_MIPS16_TLS_GOTTPREL:
682 case ELF::R_MIPS16_TLS_TPREL_HI16:
683 case ELF::R_MIPS16_TLS_TPREL_LO16:
684 llvm_unreachable("Unsupported MIPS16 relocation");
685 return true;
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000686 }
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000687}
688
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000689std::unique_ptr<MCObjectTargetWriter>
690llvm::createMipsELFObjectWriter(const Triple &TT, bool IsN32) {
Simon Atanasyan6d795862017-09-07 12:54:26 +0000691 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Simon Atanasyan117665582017-09-21 10:44:26 +0000692 bool IsN64 = TT.isArch64Bit() && !IsN32;
693 bool HasRelocationAddend = TT.isArch64Bit();
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000694 return llvm::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
695 IsN64);
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000696}