blob: 23a17f510499fcf7e3f7e1e265df55b568e9a227 [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"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000014#include "llvm/MC/MCELFObjectWriter.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000015#include "llvm/MC/MCFixup.h"
Lang Hames60fbc7c2017-10-10 16:28:07 +000016#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000017#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000018#include "llvm/Support/Casting.h"
19#include "llvm/Support/Compiler.h"
Daniel Sandersa463d312016-05-06 13:49:25 +000020#include "llvm/Support/Debug.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000021#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkodde94e42017-01-30 23:21:32 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
25#include <cassert>
26#include <cstdint>
27#include <iterator>
28#include <list>
29#include <utility>
Rafael Espindola1dc45d82011-12-22 03:03:17 +000030
Daniel Sandersa463d312016-05-06 13:49:25 +000031#define DEBUG_TYPE "mips-elf-object-writer"
32
Rafael Espindola1dc45d82011-12-22 03:03:17 +000033using namespace llvm;
34
35namespace {
Eugene Zelenkodde94e42017-01-30 23:21:32 +000036
Daniel Sandersa463d312016-05-06 13:49:25 +000037/// Holds additional information needed by the relocation ordering algorithm.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000038struct MipsRelocationEntry {
Daniel Sandersa463d312016-05-06 13:49:25 +000039 const ELFRelocationEntry R; ///< The relocation.
Eugene Zelenkodde94e42017-01-30 23:21:32 +000040 bool Matched = false; ///< Is this relocation part of a match.
Daniel Sandersa463d312016-05-06 13:49:25 +000041
Eugene Zelenkodde94e42017-01-30 23:21:32 +000042 MipsRelocationEntry(const ELFRelocationEntry &R) : R(R) {}
Daniel Sandersa463d312016-05-06 13:49:25 +000043
44 void print(raw_ostream &Out) const {
45 R.print(Out);
46 Out << ", Matched=" << Matched;
47 }
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000048};
49
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000050#ifndef NDEBUG
Daniel Sandersa463d312016-05-06 13:49:25 +000051raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
52 RHS.print(OS);
53 return OS;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000054}
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000055#endif
Rafael Espindola1dc45d82011-12-22 03:03:17 +000056
Daniel Sandersa463d312016-05-06 13:49:25 +000057class MipsELFObjectWriter : public MCELFObjectTargetWriter {
58public:
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000059 MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64);
Daniel Sandersa463d312016-05-06 13:49:25 +000060
Eugene Zelenkodde94e42017-01-30 23:21:32 +000061 ~MipsELFObjectWriter() override = default;
Daniel Sandersa463d312016-05-06 13:49:25 +000062
63 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
64 const MCFixup &Fixup, bool IsPCRel) const override;
65 bool needsRelocateWithSymbol(const MCSymbol &Sym,
66 unsigned Type) const override;
Eugene Zelenkodde94e42017-01-30 23:21:32 +000067 void sortRelocs(const MCAssembler &Asm,
68 std::vector<ELFRelocationEntry> &Relocs) override;
Daniel Sandersa463d312016-05-06 13:49:25 +000069};
70
Eugene Zelenkodde94e42017-01-30 23:21:32 +000071/// The possible results of the Predicate function used by find_best.
72enum FindBestPredicateResult {
73 FindBest_NoMatch = 0, ///< The current element is not a match.
74 FindBest_Match, ///< The current element is a match but better ones are
75 /// possible.
76 FindBest_PerfectMatch, ///< The current element is an unbeatable match.
77};
78
79} // end anonymous namespace
80
Daniel Sandersa463d312016-05-06 13:49:25 +000081/// Copy elements in the range [First, Last) to d1 when the predicate is true or
82/// d2 when the predicate is false. This is essentially both std::copy_if and
83/// std::remove_copy_if combined into a single pass.
84template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
Eugene Zelenkodde94e42017-01-30 23:21:32 +000085static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
86 OutputIt1 d1, OutputIt2 d2,
87 UnaryPredicate Predicate) {
Daniel Sandersa463d312016-05-06 13:49:25 +000088 for (InputIt I = First; I != Last; ++I) {
89 if (Predicate(*I)) {
90 *d1 = *I;
91 d1++;
92 } else {
93 *d2 = *I;
94 d2++;
95 }
96 }
97
98 return std::make_pair(d1, d2);
99}
100
Daniel Sandersa463d312016-05-06 13:49:25 +0000101/// Find the best match in the range [First, Last).
102///
103/// An element matches when Predicate(X) returns FindBest_Match or
104/// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
105/// the search. BetterThan(A, B) is a comparator that returns true when A is a
106/// better match than B. The return value is the position of the best match.
107///
108/// This is similar to std::find_if but finds the best of multiple possible
109/// matches.
110template <class InputIt, class UnaryPredicate, class Comparator>
Eugene Zelenkodde94e42017-01-30 23:21:32 +0000111static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
112 Comparator BetterThan) {
Daniel Sandersa463d312016-05-06 13:49:25 +0000113 InputIt Best = Last;
114
115 for (InputIt I = First; I != Last; ++I) {
116 unsigned Matched = Predicate(*I);
117 if (Matched != FindBest_NoMatch) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000118 LLVM_DEBUG(dbgs() << std::distance(First, I) << " is a match (";
119 I->print(dbgs()); dbgs() << ")\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000120 if (Best == Last || BetterThan(*I, *Best)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000121 LLVM_DEBUG(dbgs() << ".. and it beats the last one\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000122 Best = I;
123 }
124 }
125 if (Matched == FindBest_PerfectMatch) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000126 LLVM_DEBUG(dbgs() << ".. and it is unbeatable\n");
Daniel Sandersa463d312016-05-06 13:49:25 +0000127 break;
128 }
129 }
130
131 return Best;
132}
133
134/// Determine the low relocation that matches the given relocation.
135/// If the relocation does not need a low relocation then the return value
136/// is ELF::R_MIPS_NONE.
137///
138/// The relocations that need a matching low part are
139/// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and
140/// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.
141static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
142 unsigned Type = Reloc.Type;
143 if (Type == ELF::R_MIPS_HI16)
144 return ELF::R_MIPS_LO16;
145 if (Type == ELF::R_MICROMIPS_HI16)
146 return ELF::R_MICROMIPS_LO16;
147 if (Type == ELF::R_MIPS16_HI16)
148 return ELF::R_MIPS16_LO16;
149
150 if (Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)
151 return ELF::R_MIPS_NONE;
152
153 if (Type == ELF::R_MIPS_GOT16)
154 return ELF::R_MIPS_LO16;
155 if (Type == ELF::R_MICROMIPS_GOT16)
156 return ELF::R_MICROMIPS_LO16;
157 if (Type == ELF::R_MIPS16_GOT16)
158 return ELF::R_MIPS16_LO16;
159
160 return ELF::R_MIPS_NONE;
161}
162
163/// Determine whether a relocation (X) matches the one given in R.
164///
165/// A relocation matches if:
166/// - It's type matches that of a corresponding low part. This is provided in
167/// MatchingType for efficiency.
168/// - It's based on the same symbol.
169/// - It's offset of greater or equal to that of the one given in R.
170/// It should be noted that this rule assumes the programmer does not use
171/// offsets that exceed the alignment of the symbol. The carry-bit will be
172/// incorrect if this is not true.
173///
174/// A matching relocation is unbeatable if:
175/// - It is not already involved in a match.
176/// - It's offset is exactly that of the one given in R.
Daniel Sanders108823b2016-05-09 15:50:15 +0000177static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
178 const ELFRelocationEntry &R,
179 unsigned MatchingType) {
Daniel Sandersa463d312016-05-06 13:49:25 +0000180 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
181 if (!X.Matched &&
182 X.R.OriginalAddend == R.OriginalAddend)
183 return FindBest_PerfectMatch;
184 else if (X.R.OriginalAddend >= R.OriginalAddend)
185 return FindBest_Match;
186 }
187 return FindBest_NoMatch;
188}
189
190/// Determine whether Candidate or PreviousBest is the better match.
191/// The return value is true if Candidate is the better match.
192///
193/// A matching relocation is a better match if:
194/// - It has a smaller addend.
195/// - It is not already involved in a match.
196static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
197 const MipsRelocationEntry &PreviousBest) {
198 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
199 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
200 return PreviousBest.Matched && !Candidate.Matched;
201}
202
203#ifndef NDEBUG
204/// Print all the relocations.
205template <class Container>
206static void dumpRelocs(const char *Prefix, const Container &Relocs) {
207 for (const auto &R : Relocs)
208 dbgs() << Prefix << R << "\n";
209}
210#endif
211
Simon Atanasyan6d795862017-09-07 12:54:26 +0000212MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000213 bool HasRelocationAddend, bool Is64)
Simon Atanasyan9f676a72017-09-21 14:04:47 +0000214 : MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {}
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000215
Rafael Espindola8340f942016-01-13 22:56:57 +0000216unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
217 const MCValue &Target,
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000218 const MCFixup &Fixup,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000219 bool IsPCRel) const {
Daniel Sanders58405d82015-06-16 13:46:26 +0000220 // Determine the type of the relocation.
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000221 unsigned Kind = (unsigned)Fixup.getKind();
222
223 switch (Kind) {
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000224 case Mips::fixup_Mips_NONE:
225 return ELF::R_MIPS_NONE;
Stefan Maksimovic5481c212018-01-11 10:07:47 +0000226 case FK_Data_1:
227 report_fatal_error("MIPS does not support one byte relocations");
Daniel Sanders58405d82015-06-16 13:46:26 +0000228 case Mips::fixup_Mips_16:
229 case FK_Data_2:
230 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000231 case Mips::fixup_Mips_32:
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000232 case FK_Data_4:
Rafael Espindola7f7caf92015-06-01 15:10:51 +0000233 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
Daniel Sanders58405d82015-06-16 13:46:26 +0000234 }
235
236 if (IsPCRel) {
237 switch (Kind) {
238 case Mips::fixup_Mips_Branch_PCRel:
239 case Mips::fixup_Mips_PC16:
240 return ELF::R_MIPS_PC16;
241 case Mips::fixup_MICROMIPS_PC7_S1:
242 return ELF::R_MICROMIPS_PC7_S1;
243 case Mips::fixup_MICROMIPS_PC10_S1:
244 return ELF::R_MICROMIPS_PC10_S1;
245 case Mips::fixup_MICROMIPS_PC16_S1:
246 return ELF::R_MICROMIPS_PC16_S1;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000247 case Mips::fixup_MICROMIPS_PC26_S1:
248 return ELF::R_MICROMIPS_PC26_S1;
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000249 case Mips::fixup_MICROMIPS_PC19_S2:
250 return ELF::R_MICROMIPS_PC19_S2;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000251 case Mips::fixup_MICROMIPS_PC18_S3:
252 return ELF::R_MICROMIPS_PC18_S3;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000253 case Mips::fixup_MICROMIPS_PC21_S1:
254 return ELF::R_MICROMIPS_PC21_S1;
Daniel Sanders58405d82015-06-16 13:46:26 +0000255 case Mips::fixup_MIPS_PC19_S2:
256 return ELF::R_MIPS_PC19_S2;
257 case Mips::fixup_MIPS_PC18_S3:
258 return ELF::R_MIPS_PC18_S3;
259 case Mips::fixup_MIPS_PC21_S2:
260 return ELF::R_MIPS_PC21_S2;
261 case Mips::fixup_MIPS_PC26_S2:
262 return ELF::R_MIPS_PC26_S2;
263 case Mips::fixup_MIPS_PCHI16:
264 return ELF::R_MIPS_PCHI16;
265 case Mips::fixup_MIPS_PCLO16:
266 return ELF::R_MIPS_PCLO16;
267 }
268
269 llvm_unreachable("invalid PC-relative fixup kind!");
270 }
271
272 switch (Kind) {
Daniel Sanders60f1db02015-03-13 12:45:09 +0000273 case Mips::fixup_Mips_64:
Jack Carter4c583812012-08-07 00:01:14 +0000274 case FK_Data_8:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000275 return ELF::R_MIPS_64;
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000276 case FK_DTPRel_4:
277 return ELF::R_MIPS_TLS_DTPREL32;
278 case FK_DTPRel_8:
279 return ELF::R_MIPS_TLS_DTPREL64;
280 case FK_TPRel_4:
281 return ELF::R_MIPS_TLS_TPREL32;
282 case FK_TPRel_8:
283 return ELF::R_MIPS_TLS_TPREL64;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000284 case FK_GPRel_4:
Simon Atanasyan9f676a72017-09-21 14:04:47 +0000285 if (is64Bit()) {
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000286 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterf2385102013-01-15 01:08:02 +0000287 Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
288 Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
289 Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000290 return Type;
Jack Carterf2385102013-01-15 01:08:02 +0000291 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000292 return ELF::R_MIPS_GPREL32;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000293 case Mips::fixup_Mips_GPREL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000294 return ELF::R_MIPS_GPREL16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000295 case Mips::fixup_Mips_26:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000296 return ELF::R_MIPS_26;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000297 case Mips::fixup_Mips_CALL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000298 return ELF::R_MIPS_CALL16;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000299 case Mips::fixup_Mips_GOT:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000300 return ELF::R_MIPS_GOT16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000301 case Mips::fixup_Mips_HI16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000302 return ELF::R_MIPS_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000303 case Mips::fixup_Mips_LO16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000304 return ELF::R_MIPS_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000305 case Mips::fixup_Mips_TLSGD:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000306 return ELF::R_MIPS_TLS_GD;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000307 case Mips::fixup_Mips_GOTTPREL:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000308 return ELF::R_MIPS_TLS_GOTTPREL;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000309 case Mips::fixup_Mips_TPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000310 return ELF::R_MIPS_TLS_TPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000311 case Mips::fixup_Mips_TPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000312 return ELF::R_MIPS_TLS_TPREL_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000313 case Mips::fixup_Mips_TLSLDM:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000314 return ELF::R_MIPS_TLS_LDM;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000315 case Mips::fixup_Mips_DTPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000316 return ELF::R_MIPS_TLS_DTPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000317 case Mips::fixup_Mips_DTPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000318 return ELF::R_MIPS_TLS_DTPREL_LO16;
Jack Carterb9f9de92012-06-27 22:48:25 +0000319 case Mips::fixup_Mips_GOT_PAGE:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000320 return ELF::R_MIPS_GOT_PAGE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000321 case Mips::fixup_Mips_GOT_OFST:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000322 return ELF::R_MIPS_GOT_OFST;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000323 case Mips::fixup_Mips_GOT_DISP:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000324 return ELF::R_MIPS_GOT_DISP;
325 case Mips::fixup_Mips_GPOFF_HI: {
326 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000327 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
328 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
329 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000330 return Type;
331 }
332 case Mips::fixup_Mips_GPOFF_LO: {
333 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000334 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
335 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
336 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000337 return Type;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000338 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000339 case Mips::fixup_Mips_HIGHER:
340 return ELF::R_MIPS_HIGHER;
341 case Mips::fixup_Mips_HIGHEST:
342 return ELF::R_MIPS_HIGHEST;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000343 case Mips::fixup_Mips_SUB:
344 return ELF::R_MIPS_SUB;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000345 case Mips::fixup_Mips_GOT_HI16:
346 return ELF::R_MIPS_GOT_HI16;
347 case Mips::fixup_Mips_GOT_LO16:
348 return ELF::R_MIPS_GOT_LO16;
349 case Mips::fixup_Mips_CALL_HI16:
350 return ELF::R_MIPS_CALL_HI16;
351 case Mips::fixup_Mips_CALL_LO16:
352 return ELF::R_MIPS_CALL_LO16;
353 case Mips::fixup_MICROMIPS_26_S1:
354 return ELF::R_MICROMIPS_26_S1;
355 case Mips::fixup_MICROMIPS_HI16:
356 return ELF::R_MICROMIPS_HI16;
357 case Mips::fixup_MICROMIPS_LO16:
358 return ELF::R_MICROMIPS_LO16;
359 case Mips::fixup_MICROMIPS_GOT16:
360 return ELF::R_MICROMIPS_GOT16;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000361 case Mips::fixup_MICROMIPS_CALL16:
362 return ELF::R_MICROMIPS_CALL16;
363 case Mips::fixup_MICROMIPS_GOT_DISP:
364 return ELF::R_MICROMIPS_GOT_DISP;
365 case Mips::fixup_MICROMIPS_GOT_PAGE:
366 return ELF::R_MICROMIPS_GOT_PAGE;
367 case Mips::fixup_MICROMIPS_GOT_OFST:
368 return ELF::R_MICROMIPS_GOT_OFST;
369 case Mips::fixup_MICROMIPS_TLS_GD:
370 return ELF::R_MICROMIPS_TLS_GD;
371 case Mips::fixup_MICROMIPS_TLS_LDM:
372 return ELF::R_MICROMIPS_TLS_LDM;
373 case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
374 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
375 case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
376 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
Simon Atanasyan3979f432017-04-30 04:27:23 +0000377 case Mips::fixup_MICROMIPS_GOTTPREL:
378 return ELF::R_MICROMIPS_TLS_GOTTPREL;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000379 case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
380 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
381 case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
382 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000383 case Mips::fixup_MICROMIPS_SUB:
384 return ELF::R_MICROMIPS_SUB;
Simon Atanasyan6be87bc2018-05-29 10:27:44 +0000385 case Mips::fixup_MICROMIPS_HIGHER:
386 return ELF::R_MICROMIPS_HIGHER;
387 case Mips::fixup_MICROMIPS_HIGHEST:
388 return ELF::R_MICROMIPS_HIGHEST;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000389 }
Daniel Sanders58405d82015-06-16 13:46:26 +0000390
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000391 llvm_unreachable("invalid fixup kind!");
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000392}
393
Daniel Sandersa463d312016-05-06 13:49:25 +0000394/// Sort relocation table entries by offset except where another order is
395/// required by the MIPS ABI.
396///
397/// MIPS has a few relocations that have an AHL component in the expression used
398/// to evaluate them. This AHL component is an addend with the same number of
399/// bits as a symbol value but not all of our ABI's are able to supply a
400/// sufficiently sized addend in a single relocation.
401///
402/// The O32 ABI for example, uses REL relocations which store the addend in the
403/// section data. All the relocations with AHL components affect 16-bit fields
404/// so the addend for a single relocation is limited to 16-bit. This ABI
405/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
406/// R_MIPS_LO16) and distributing the addend between the linked relocations. The
407/// ABI mandates that such relocations must be next to each other in a
408/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
409/// matching R_MIPS_LO16) but the rule is less strict in practice.
410///
411/// The de facto standard is lenient in the following ways:
412/// - 'Immediately following' does not refer to the next relocation entry but
413/// the next matching relocation.
414/// - There may be multiple high parts relocations for one low part relocation.
415/// - There may be multiple low part relocations for one high part relocation.
416/// - The AHL addend in each part does not have to be exactly equal as long as
417/// the difference does not affect the carry bit from bit 15 into 16. This is
418/// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
419/// both halves of a long long.
420///
421/// See getMatchingLoType() for a description of which high part relocations
422/// match which low part relocations. One particular thing to note is that
423/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
424/// symbols.
425///
426/// It should also be noted that this function is not affected by whether
427/// the symbol was kept or rewritten into a section-relative equivalent. We
428/// always match using the expressions from the source.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000429void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
430 std::vector<ELFRelocationEntry> &Relocs) {
Simon Dardis8fe36cd2016-12-05 12:55:19 +0000431 // We do not need to sort the relocation table for RELA relocations which
432 // N32/N64 uses as the relocation addend contains the value we require,
433 // rather than it being split across a pair of relocations.
434 if (hasRelocationAddend())
435 return;
436
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000437 if (Relocs.size() < 2)
438 return;
439
Daniel Sandersa463d312016-05-06 13:49:25 +0000440 // Sort relocations by the address they are applied to.
Mandeep Singh Grang10d8b852018-03-29 19:05:26 +0000441 llvm::sort(Relocs.begin(), Relocs.end(),
442 [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
443 return A.Offset < B.Offset;
444 });
Rafael Espindolaf44db242015-12-17 16:22:06 +0000445
Daniel Sandersa463d312016-05-06 13:49:25 +0000446 std::list<MipsRelocationEntry> Sorted;
447 std::list<ELFRelocationEntry> Remainder;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000448
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000449 LLVM_DEBUG(dumpRelocs("R: ", Relocs));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000450
Daniel Sandersa463d312016-05-06 13:49:25 +0000451 // Separate the movable relocations (AHL relocations using the high bits) from
452 // the immobile relocations (everything else). This does not preserve high/low
453 // matches that already existed in the input.
454 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
455 std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
456 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
457 });
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000458
Daniel Sandersa463d312016-05-06 13:49:25 +0000459 for (auto &R : Remainder) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000460 LLVM_DEBUG(dbgs() << "Matching: " << R << "\n");
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000461
Daniel Sandersa463d312016-05-06 13:49:25 +0000462 unsigned MatchingType = getMatchingLoType(R);
463 assert(MatchingType != ELF::R_MIPS_NONE &&
464 "Wrong list for reloc that doesn't need a match");
465
466 // Find the best matching relocation for the current high part.
467 // See isMatchingReloc for a description of a matching relocation and
468 // compareMatchingRelocs for a description of what 'best' means.
469 auto InsertionPoint =
470 find_best(Sorted.begin(), Sorted.end(),
471 [&R, &MatchingType](const MipsRelocationEntry &X) {
472 return isMatchingReloc(X, R, MatchingType);
473 },
474 compareMatchingRelocs);
475
476 // If we matched then insert the high part in front of the match and mark
477 // both relocations as being involved in a match. We only mark the high
478 // part for cosmetic reasons in the debug output.
479 //
480 // If we failed to find a match then the high part is orphaned. This is not
481 // permitted since the relocation cannot be evaluated without knowing the
482 // carry-in. We can sometimes handle this using a matching low part that is
483 // already used in a match but we already cover that case in
484 // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
485 // should insert the high part at the end of the list. This will cause the
486 // linker to fail but the alternative is to cause the linker to bind the
487 // high part to a semi-matching low part and silently calculate the wrong
488 // value. Unfortunately we have no means to warn the user that we did this
489 // so leave it up to the linker to complain about it.
490 if (InsertionPoint != Sorted.end())
491 InsertionPoint->Matched = true;
492 Sorted.insert(InsertionPoint, R)->Matched = true;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000493 }
494
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000495 LLVM_DEBUG(dumpRelocs("S: ", Sorted));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000496
Daniel Sandersa463d312016-05-06 13:49:25 +0000497 assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
498
499 // Overwrite the original vector with the sorted elements. The caller expects
500 // them in reverse order.
501 unsigned CopyTo = 0;
502 for (const auto &R : reverse(Sorted))
503 Relocs[CopyTo++] = R.R;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000504}
505
Rafael Espindolaece40ca2015-05-29 18:26:09 +0000506bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
507 unsigned Type) const {
Daniel Sanders55d38332016-05-12 10:55:00 +0000508 // If it's a compound relocation for N64 then we need the relocation if any
509 // sub-relocation needs it.
Daniel Sanders2225d942016-05-10 12:17:04 +0000510 if (!isUInt<8>(Type))
Daniel Sanders55d38332016-05-12 10:55:00 +0000511 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
512 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
513 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
Daniel Sanders2225d942016-05-10 12:17:04 +0000514
Rafael Espindola5904e122014-03-29 06:26:49 +0000515 switch (Type) {
516 default:
Daniel Sanders2225d942016-05-10 12:17:04 +0000517 errs() << Type << "\n";
518 llvm_unreachable("Unexpected relocation");
Rafael Espindola5904e122014-03-29 06:26:49 +0000519 return true;
520
Daniel Sanders2225d942016-05-10 12:17:04 +0000521 // This relocation doesn't affect the section data.
522 case ELF::R_MIPS_NONE:
523 return false;
524
Daniel Sanders3d000562016-05-09 10:21:14 +0000525 // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
526 // by the static linker by matching the symbol and offset.
527 // We only see one relocation at a time but it's still safe to relocate with
528 // the section so long as both relocations make the same decision.
529 //
530 // Some older linkers may require the symbol for particular cases. Such cases
531 // are not supported yet but can be added as required.
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000532 case ELF::R_MIPS_GOT16:
533 case ELF::R_MIPS16_GOT16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000534 case ELF::R_MICROMIPS_GOT16:
Simon Dardisca74dd72017-01-27 11:36:52 +0000535 case ELF::R_MIPS_HIGHER:
536 case ELF::R_MIPS_HIGHEST:
Rafael Espindola5904e122014-03-29 06:26:49 +0000537 case ELF::R_MIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000538 case ELF::R_MIPS16_HI16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000539 case ELF::R_MICROMIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000540 case ELF::R_MIPS_LO16:
541 case ELF::R_MIPS16_LO16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000542 case ELF::R_MICROMIPS_LO16:
Daniel Sanders45533b42016-05-11 15:44:23 +0000543 // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
544 // we neglect to handle the adjustment to the LSB of the addend that
545 // it causes in applyFixup() and similar.
546 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
547 return true;
Daniel Sanders3d000562016-05-09 10:21:14 +0000548 return false;
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000549
Daniel Sanders38784122016-07-19 10:58:06 +0000550 case ELF::R_MIPS_GOT_PAGE:
551 case ELF::R_MICROMIPS_GOT_PAGE:
552 case ELF::R_MIPS_GOT_OFST:
553 case ELF::R_MICROMIPS_GOT_OFST:
Scott Egertond1aeb052016-02-15 16:11:51 +0000554 case ELF::R_MIPS_16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000555 case ELF::R_MIPS_32:
Daniel Sanders5fb391c2016-05-12 13:39:13 +0000556 case ELF::R_MIPS_GPREL32:
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000557 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
Zoran Jovanovic10646912014-12-30 22:04:16 +0000558 return true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000559 LLVM_FALLTHROUGH;
Zoran Jovanovic10646912014-12-30 22:04:16 +0000560 case ELF::R_MIPS_26:
Rafael Espindola5904e122014-03-29 06:26:49 +0000561 case ELF::R_MIPS_64:
562 case ELF::R_MIPS_GPREL16:
Daniel Sanders3d000562016-05-09 10:21:14 +0000563 case ELF::R_MIPS_PC16:
Daniel Sanders55d38332016-05-12 10:55:00 +0000564 case ELF::R_MIPS_SUB:
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000565 return false;
Daniel Sanders2225d942016-05-10 12:17:04 +0000566
567 // FIXME: Many of these relocations should probably return false but this
568 // hasn't been confirmed to be safe yet.
569 case ELF::R_MIPS_REL32:
570 case ELF::R_MIPS_LITERAL:
571 case ELF::R_MIPS_CALL16:
572 case ELF::R_MIPS_SHIFT5:
573 case ELF::R_MIPS_SHIFT6:
574 case ELF::R_MIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000575 case ELF::R_MIPS_GOT_HI16:
576 case ELF::R_MIPS_GOT_LO16:
Daniel Sanders2225d942016-05-10 12:17:04 +0000577 case ELF::R_MIPS_INSERT_A:
578 case ELF::R_MIPS_INSERT_B:
579 case ELF::R_MIPS_DELETE:
Daniel Sanders2225d942016-05-10 12:17:04 +0000580 case ELF::R_MIPS_CALL_HI16:
581 case ELF::R_MIPS_CALL_LO16:
582 case ELF::R_MIPS_SCN_DISP:
583 case ELF::R_MIPS_REL16:
584 case ELF::R_MIPS_ADD_IMMEDIATE:
585 case ELF::R_MIPS_PJUMP:
586 case ELF::R_MIPS_RELGOT:
587 case ELF::R_MIPS_JALR:
588 case ELF::R_MIPS_TLS_DTPMOD32:
589 case ELF::R_MIPS_TLS_DTPREL32:
590 case ELF::R_MIPS_TLS_DTPMOD64:
591 case ELF::R_MIPS_TLS_DTPREL64:
592 case ELF::R_MIPS_TLS_GD:
593 case ELF::R_MIPS_TLS_LDM:
594 case ELF::R_MIPS_TLS_DTPREL_HI16:
595 case ELF::R_MIPS_TLS_DTPREL_LO16:
596 case ELF::R_MIPS_TLS_GOTTPREL:
597 case ELF::R_MIPS_TLS_TPREL32:
598 case ELF::R_MIPS_TLS_TPREL64:
599 case ELF::R_MIPS_TLS_TPREL_HI16:
600 case ELF::R_MIPS_TLS_TPREL_LO16:
601 case ELF::R_MIPS_GLOB_DAT:
602 case ELF::R_MIPS_PC21_S2:
603 case ELF::R_MIPS_PC26_S2:
604 case ELF::R_MIPS_PC18_S3:
605 case ELF::R_MIPS_PC19_S2:
606 case ELF::R_MIPS_PCHI16:
607 case ELF::R_MIPS_PCLO16:
608 case ELF::R_MIPS_COPY:
609 case ELF::R_MIPS_JUMP_SLOT:
610 case ELF::R_MIPS_NUM:
611 case ELF::R_MIPS_PC32:
612 case ELF::R_MIPS_EH:
613 case ELF::R_MICROMIPS_26_S1:
614 case ELF::R_MICROMIPS_GPREL16:
615 case ELF::R_MICROMIPS_LITERAL:
616 case ELF::R_MICROMIPS_PC7_S1:
617 case ELF::R_MICROMIPS_PC10_S1:
618 case ELF::R_MICROMIPS_PC16_S1:
619 case ELF::R_MICROMIPS_CALL16:
620 case ELF::R_MICROMIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000621 case ELF::R_MICROMIPS_GOT_HI16:
622 case ELF::R_MICROMIPS_GOT_LO16:
623 case ELF::R_MICROMIPS_SUB:
624 case ELF::R_MICROMIPS_HIGHER:
625 case ELF::R_MICROMIPS_HIGHEST:
626 case ELF::R_MICROMIPS_CALL_HI16:
627 case ELF::R_MICROMIPS_CALL_LO16:
628 case ELF::R_MICROMIPS_SCN_DISP:
629 case ELF::R_MICROMIPS_JALR:
630 case ELF::R_MICROMIPS_HI0_LO16:
631 case ELF::R_MICROMIPS_TLS_GD:
632 case ELF::R_MICROMIPS_TLS_LDM:
633 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
634 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
635 case ELF::R_MICROMIPS_TLS_GOTTPREL:
636 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
637 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
638 case ELF::R_MICROMIPS_GPREL7_S2:
639 case ELF::R_MICROMIPS_PC23_S2:
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000640 case ELF::R_MICROMIPS_PC21_S1:
Daniel Sanders2225d942016-05-10 12:17:04 +0000641 case ELF::R_MICROMIPS_PC26_S1:
642 case ELF::R_MICROMIPS_PC18_S3:
643 case ELF::R_MICROMIPS_PC19_S2:
644 return true;
645
646 // FIXME: Many of these should probably return false but MIPS16 isn't
647 // supported by the integrated assembler.
648 case ELF::R_MIPS16_26:
649 case ELF::R_MIPS16_GPREL:
650 case ELF::R_MIPS16_CALL16:
651 case ELF::R_MIPS16_TLS_GD:
652 case ELF::R_MIPS16_TLS_LDM:
653 case ELF::R_MIPS16_TLS_DTPREL_HI16:
654 case ELF::R_MIPS16_TLS_DTPREL_LO16:
655 case ELF::R_MIPS16_TLS_GOTTPREL:
656 case ELF::R_MIPS16_TLS_TPREL_HI16:
657 case ELF::R_MIPS16_TLS_TPREL_LO16:
658 llvm_unreachable("Unsupported MIPS16 relocation");
659 return true;
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000660 }
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000661}
662
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000663std::unique_ptr<MCObjectTargetWriter>
664llvm::createMipsELFObjectWriter(const Triple &TT, bool IsN32) {
Simon Atanasyan6d795862017-09-07 12:54:26 +0000665 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Simon Atanasyan117665582017-09-21 10:44:26 +0000666 bool IsN64 = TT.isArch64Bit() && !IsN32;
667 bool HasRelocationAddend = TT.isArch64Bit();
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000668 return llvm::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
669 IsN64);
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000670}