1 | //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===// |
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 | #include "MCTargetDesc/MipsFixupKinds.h" |
10 | #include "MCTargetDesc/MipsMCAsmInfo.h" |
11 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
12 | #include "llvm/ADT/STLExtras.h" |
13 | #include "llvm/BinaryFormat/ELF.h" |
14 | #include "llvm/MC/MCContext.h" |
15 | #include "llvm/MC/MCELFObjectWriter.h" |
16 | #include "llvm/MC/MCFixup.h" |
17 | #include "llvm/MC/MCObjectWriter.h" |
18 | #include "llvm/MC/MCSymbolELF.h" |
19 | #include "llvm/MC/MCValue.h" |
20 | #include "llvm/Support/Casting.h" |
21 | #include "llvm/Support/Compiler.h" |
22 | #include "llvm/Support/Debug.h" |
23 | #include "llvm/Support/ErrorHandling.h" |
24 | #include "llvm/Support/MathExtras.h" |
25 | #include "llvm/Support/raw_ostream.h" |
26 | #include <cassert> |
27 | #include <cstdint> |
28 | #include <iterator> |
29 | #include <list> |
30 | #include <utility> |
31 | |
32 | #define DEBUG_TYPE "mips-elf-object-writer" |
33 | |
34 | using namespace llvm; |
35 | |
36 | namespace { |
37 | |
38 | /// Holds additional information needed by the relocation ordering algorithm. |
39 | struct MipsRelocationEntry { |
40 | const ELFRelocationEntry R; ///< The relocation. |
41 | bool Matched = false; ///< Is this relocation part of a match. |
42 | |
43 | MipsRelocationEntry(const ELFRelocationEntry &R) : R(R) {} |
44 | }; |
45 | |
46 | class MipsELFObjectWriter : public MCELFObjectTargetWriter { |
47 | public: |
48 | MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64); |
49 | |
50 | ~MipsELFObjectWriter() override = default; |
51 | |
52 | unsigned getRelocType(const MCFixup &, const MCValue &, |
53 | bool IsPCRel) const override; |
54 | bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override; |
55 | void sortRelocs(std::vector<ELFRelocationEntry> &Relocs) override; |
56 | }; |
57 | |
58 | /// The possible results of the Predicate function used by find_best. |
59 | enum FindBestPredicateResult { |
60 | FindBest_NoMatch = 0, ///< The current element is not a match. |
61 | FindBest_Match, ///< The current element is a match but better ones are |
62 | /// possible. |
63 | FindBest_PerfectMatch, ///< The current element is an unbeatable match. |
64 | }; |
65 | |
66 | } // end anonymous namespace |
67 | |
68 | /// Copy elements in the range [First, Last) to d1 when the predicate is true or |
69 | /// d2 when the predicate is false. This is essentially both std::copy_if and |
70 | /// std::remove_copy_if combined into a single pass. |
71 | template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate> |
72 | static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last, |
73 | OutputIt1 d1, OutputIt2 d2, |
74 | UnaryPredicate Predicate) { |
75 | for (InputIt I = First; I != Last; ++I) { |
76 | if (Predicate(*I)) { |
77 | *d1 = *I; |
78 | d1++; |
79 | } else { |
80 | *d2 = *I; |
81 | d2++; |
82 | } |
83 | } |
84 | |
85 | return std::make_pair(d1, d2); |
86 | } |
87 | |
88 | /// Find the best match in the range [First, Last). |
89 | /// |
90 | /// An element matches when Predicate(X) returns FindBest_Match or |
91 | /// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates |
92 | /// the search. BetterThan(A, B) is a comparator that returns true when A is a |
93 | /// better match than B. The return value is the position of the best match. |
94 | /// |
95 | /// This is similar to std::find_if but finds the best of multiple possible |
96 | /// matches. |
97 | template <class InputIt, class UnaryPredicate, class Comparator> |
98 | static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate, |
99 | Comparator BetterThan) { |
100 | InputIt Best = Last; |
101 | |
102 | for (InputIt I = First; I != Last; ++I) { |
103 | unsigned Matched = Predicate(*I); |
104 | if (Matched != FindBest_NoMatch) { |
105 | if (Best == Last || BetterThan(*I, *Best)) |
106 | Best = I; |
107 | } |
108 | if (Matched == FindBest_PerfectMatch) |
109 | break; |
110 | } |
111 | |
112 | return Best; |
113 | } |
114 | |
115 | /// Determine the low relocation that matches the given relocation. |
116 | /// If the relocation does not need a low relocation then the return value |
117 | /// is ELF::R_MIPS_NONE. |
118 | /// |
119 | /// The relocations that need a matching low part are |
120 | /// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and |
121 | /// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only. |
122 | static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) { |
123 | unsigned Type = Reloc.Type; |
124 | if (Type == ELF::R_MIPS_HI16) |
125 | return ELF::R_MIPS_LO16; |
126 | if (Type == ELF::R_MICROMIPS_HI16) |
127 | return ELF::R_MICROMIPS_LO16; |
128 | if (Type == ELF::R_MIPS16_HI16) |
129 | return ELF::R_MIPS16_LO16; |
130 | |
131 | if (Reloc.Symbol && Reloc.Symbol->getBinding() != ELF::STB_LOCAL) |
132 | return ELF::R_MIPS_NONE; |
133 | |
134 | if (Type == ELF::R_MIPS_GOT16) |
135 | return ELF::R_MIPS_LO16; |
136 | if (Type == ELF::R_MICROMIPS_GOT16) |
137 | return ELF::R_MICROMIPS_LO16; |
138 | if (Type == ELF::R_MIPS16_GOT16) |
139 | return ELF::R_MIPS16_LO16; |
140 | |
141 | return ELF::R_MIPS_NONE; |
142 | } |
143 | |
144 | // Determine whether a relocation X is a low-part and matches the high-part R |
145 | // perfectly by symbol and addend. |
146 | static bool isMatchingReloc(unsigned MatchingType, const ELFRelocationEntry &R, |
147 | const ELFRelocationEntry &X) { |
148 | return X.Type == MatchingType && X.Symbol == R.Symbol && X.Addend == R.Addend; |
149 | } |
150 | |
151 | MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI, |
152 | bool HasRelocationAddend, bool Is64) |
153 | : MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {} |
154 | |
155 | unsigned MipsELFObjectWriter::getRelocType(const MCFixup &Fixup, |
156 | const MCValue &Target, |
157 | bool IsPCRel) const { |
158 | // Determine the type of the relocation. |
159 | unsigned Kind = Fixup.getTargetKind(); |
160 | switch (Target.getSpecifier()) { |
161 | case Mips::S_DTPREL: |
162 | case Mips::S_DTPREL_HI: |
163 | case Mips::S_DTPREL_LO: |
164 | case Mips::S_TLSLDM: |
165 | case Mips::S_TLSGD: |
166 | case Mips::S_GOTTPREL: |
167 | case Mips::S_TPREL_HI: |
168 | case Mips::S_TPREL_LO: |
169 | if (auto *SA = Target.getAddSym()) |
170 | cast<MCSymbolELF>(Val: SA)->setType(ELF::STT_TLS); |
171 | break; |
172 | default: |
173 | break; |
174 | } |
175 | |
176 | switch (Kind) { |
177 | case FK_NONE: |
178 | return ELF::R_MIPS_NONE; |
179 | case FK_Data_1: |
180 | reportError(L: Fixup.getLoc(), Msg: "MIPS does not support one byte relocations" ); |
181 | return ELF::R_MIPS_NONE; |
182 | case Mips::fixup_Mips_16: |
183 | case FK_Data_2: |
184 | return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16; |
185 | case Mips::fixup_Mips_32: |
186 | case FK_Data_4: |
187 | return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32; |
188 | case Mips::fixup_Mips_64: |
189 | case FK_Data_8: |
190 | return IsPCRel |
191 | ? setRTypes(Value1: ELF::R_MIPS_PC32, Value2: ELF::R_MIPS_64, Value3: ELF::R_MIPS_NONE) |
192 | : (unsigned)ELF::R_MIPS_64; |
193 | } |
194 | |
195 | if (IsPCRel) { |
196 | switch (Kind) { |
197 | case Mips::fixup_Mips_Branch_PCRel: |
198 | case Mips::fixup_Mips_PC16: |
199 | return ELF::R_MIPS_PC16; |
200 | case Mips::fixup_MICROMIPS_PC7_S1: |
201 | return ELF::R_MICROMIPS_PC7_S1; |
202 | case Mips::fixup_MICROMIPS_PC10_S1: |
203 | return ELF::R_MICROMIPS_PC10_S1; |
204 | case Mips::fixup_MICROMIPS_PC16_S1: |
205 | return ELF::R_MICROMIPS_PC16_S1; |
206 | case Mips::fixup_MICROMIPS_PC26_S1: |
207 | return ELF::R_MICROMIPS_PC26_S1; |
208 | case Mips::fixup_MICROMIPS_PC19_S2: |
209 | return ELF::R_MICROMIPS_PC19_S2; |
210 | case Mips::fixup_MICROMIPS_PC18_S3: |
211 | return ELF::R_MICROMIPS_PC18_S3; |
212 | case Mips::fixup_MICROMIPS_PC21_S1: |
213 | return ELF::R_MICROMIPS_PC21_S1; |
214 | case Mips::fixup_MIPS_PC19_S2: |
215 | return ELF::R_MIPS_PC19_S2; |
216 | case Mips::fixup_MIPS_PC18_S3: |
217 | return ELF::R_MIPS_PC18_S3; |
218 | case Mips::fixup_MIPS_PC21_S2: |
219 | return ELF::R_MIPS_PC21_S2; |
220 | case Mips::fixup_MIPS_PC26_S2: |
221 | return ELF::R_MIPS_PC26_S2; |
222 | case Mips::fixup_MIPS_PCHI16: |
223 | return ELF::R_MIPS_PCHI16; |
224 | case Mips::fixup_MIPS_PCLO16: |
225 | return ELF::R_MIPS_PCLO16; |
226 | } |
227 | |
228 | llvm_unreachable("invalid PC-relative fixup kind!" ); |
229 | } |
230 | |
231 | switch (Kind) { |
232 | case Mips::fixup_Mips_DTPREL32: |
233 | return ELF::R_MIPS_TLS_DTPREL32; |
234 | case Mips::fixup_Mips_DTPREL64: |
235 | return ELF::R_MIPS_TLS_DTPREL64; |
236 | case Mips::fixup_Mips_TPREL32: |
237 | return ELF::R_MIPS_TLS_TPREL32; |
238 | case Mips::fixup_Mips_TPREL64: |
239 | return ELF::R_MIPS_TLS_TPREL64; |
240 | case Mips::fixup_Mips_GPREL32: |
241 | return setRTypes(Value1: ELF::R_MIPS_GPREL32, |
242 | Value2: is64Bit() ? ELF::R_MIPS_64 : ELF::R_MIPS_NONE, |
243 | Value3: ELF::R_MIPS_NONE); |
244 | case Mips::fixup_Mips_GPREL16: |
245 | return ELF::R_MIPS_GPREL16; |
246 | case Mips::fixup_Mips_26: |
247 | return ELF::R_MIPS_26; |
248 | case Mips::fixup_Mips_CALL16: |
249 | return ELF::R_MIPS_CALL16; |
250 | case Mips::fixup_Mips_GOT: |
251 | return ELF::R_MIPS_GOT16; |
252 | case Mips::fixup_Mips_HI16: |
253 | return ELF::R_MIPS_HI16; |
254 | case Mips::fixup_Mips_LO16: |
255 | return ELF::R_MIPS_LO16; |
256 | case Mips::fixup_Mips_TLSGD: |
257 | return ELF::R_MIPS_TLS_GD; |
258 | case Mips::fixup_Mips_GOTTPREL: |
259 | return ELF::R_MIPS_TLS_GOTTPREL; |
260 | case Mips::fixup_Mips_TPREL_HI: |
261 | return ELF::R_MIPS_TLS_TPREL_HI16; |
262 | case Mips::fixup_Mips_TPREL_LO: |
263 | return ELF::R_MIPS_TLS_TPREL_LO16; |
264 | case Mips::fixup_Mips_TLSLDM: |
265 | return ELF::R_MIPS_TLS_LDM; |
266 | case Mips::fixup_Mips_DTPREL_HI: |
267 | return ELF::R_MIPS_TLS_DTPREL_HI16; |
268 | case Mips::fixup_Mips_DTPREL_LO: |
269 | return ELF::R_MIPS_TLS_DTPREL_LO16; |
270 | case Mips::fixup_Mips_GOT_PAGE: |
271 | return ELF::R_MIPS_GOT_PAGE; |
272 | case Mips::fixup_Mips_GOT_OFST: |
273 | return ELF::R_MIPS_GOT_OFST; |
274 | case Mips::fixup_Mips_GOT_DISP: |
275 | return ELF::R_MIPS_GOT_DISP; |
276 | case Mips::fixup_Mips_GPOFF_HI: |
277 | return setRTypes(Value1: ELF::R_MIPS_GPREL16, Value2: ELF::R_MIPS_SUB, Value3: ELF::R_MIPS_HI16); |
278 | case Mips::fixup_MICROMIPS_GPOFF_HI: |
279 | return setRTypes(Value1: ELF::R_MICROMIPS_GPREL16, Value2: ELF::R_MICROMIPS_SUB, |
280 | Value3: ELF::R_MICROMIPS_HI16); |
281 | case Mips::fixup_Mips_GPOFF_LO: |
282 | return setRTypes(Value1: ELF::R_MIPS_GPREL16, Value2: ELF::R_MIPS_SUB, Value3: ELF::R_MIPS_LO16); |
283 | case Mips::fixup_MICROMIPS_GPOFF_LO: |
284 | return setRTypes(Value1: ELF::R_MICROMIPS_GPREL16, Value2: ELF::R_MICROMIPS_SUB, |
285 | Value3: ELF::R_MICROMIPS_LO16); |
286 | case Mips::fixup_Mips_HIGHER: |
287 | return ELF::R_MIPS_HIGHER; |
288 | case Mips::fixup_Mips_HIGHEST: |
289 | return ELF::R_MIPS_HIGHEST; |
290 | case Mips::fixup_Mips_SUB: |
291 | return ELF::R_MIPS_SUB; |
292 | case Mips::fixup_Mips_GOT_HI16: |
293 | return ELF::R_MIPS_GOT_HI16; |
294 | case Mips::fixup_Mips_GOT_LO16: |
295 | return ELF::R_MIPS_GOT_LO16; |
296 | case Mips::fixup_Mips_CALL_HI16: |
297 | return ELF::R_MIPS_CALL_HI16; |
298 | case Mips::fixup_Mips_CALL_LO16: |
299 | return ELF::R_MIPS_CALL_LO16; |
300 | case Mips::fixup_MICROMIPS_26_S1: |
301 | return ELF::R_MICROMIPS_26_S1; |
302 | case Mips::fixup_MICROMIPS_HI16: |
303 | return ELF::R_MICROMIPS_HI16; |
304 | case Mips::fixup_MICROMIPS_LO16: |
305 | return ELF::R_MICROMIPS_LO16; |
306 | case Mips::fixup_MICROMIPS_GOT16: |
307 | return ELF::R_MICROMIPS_GOT16; |
308 | case Mips::fixup_MICROMIPS_CALL16: |
309 | return ELF::R_MICROMIPS_CALL16; |
310 | case Mips::fixup_MICROMIPS_GOT_DISP: |
311 | return ELF::R_MICROMIPS_GOT_DISP; |
312 | case Mips::fixup_MICROMIPS_GOT_PAGE: |
313 | return ELF::R_MICROMIPS_GOT_PAGE; |
314 | case Mips::fixup_MICROMIPS_GOT_OFST: |
315 | return ELF::R_MICROMIPS_GOT_OFST; |
316 | case Mips::fixup_MICROMIPS_TLS_GD: |
317 | return ELF::R_MICROMIPS_TLS_GD; |
318 | case Mips::fixup_MICROMIPS_TLS_LDM: |
319 | return ELF::R_MICROMIPS_TLS_LDM; |
320 | case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16: |
321 | return ELF::R_MICROMIPS_TLS_DTPREL_HI16; |
322 | case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16: |
323 | return ELF::R_MICROMIPS_TLS_DTPREL_LO16; |
324 | case Mips::fixup_MICROMIPS_GOTTPREL: |
325 | return ELF::R_MICROMIPS_TLS_GOTTPREL; |
326 | case Mips::fixup_MICROMIPS_TLS_TPREL_HI16: |
327 | return ELF::R_MICROMIPS_TLS_TPREL_HI16; |
328 | case Mips::fixup_MICROMIPS_TLS_TPREL_LO16: |
329 | return ELF::R_MICROMIPS_TLS_TPREL_LO16; |
330 | case Mips::fixup_MICROMIPS_SUB: |
331 | return ELF::R_MICROMIPS_SUB; |
332 | case Mips::fixup_MICROMIPS_HIGHER: |
333 | return ELF::R_MICROMIPS_HIGHER; |
334 | case Mips::fixup_MICROMIPS_HIGHEST: |
335 | return ELF::R_MICROMIPS_HIGHEST; |
336 | case Mips::fixup_Mips_JALR: |
337 | return ELF::R_MIPS_JALR; |
338 | case Mips::fixup_MICROMIPS_JALR: |
339 | return ELF::R_MICROMIPS_JALR; |
340 | } |
341 | |
342 | reportError(L: Fixup.getLoc(), Msg: "unsupported relocation type" ); |
343 | return ELF::R_MIPS_NONE; |
344 | } |
345 | |
346 | /// Sort relocation table entries by offset except where another order is |
347 | /// required by the MIPS ABI. |
348 | /// |
349 | /// MIPS has a few relocations that have an AHL component in the expression used |
350 | /// to evaluate them. This AHL component is an addend with the same number of |
351 | /// bits as a symbol value but not all of our ABI's are able to supply a |
352 | /// sufficiently sized addend in a single relocation. |
353 | /// |
354 | /// The O32 ABI for example, uses REL relocations which store the addend in the |
355 | /// section data. All the relocations with AHL components affect 16-bit fields |
356 | /// so the addend for a single relocation is limited to 16-bit. This ABI |
357 | /// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and |
358 | /// R_MIPS_LO16) and distributing the addend between the linked relocations. The |
359 | /// ABI mandates that such relocations must be next to each other in a |
360 | /// particular order (e.g. R_MIPS_HI16 must be immediately followed by a |
361 | /// matching R_MIPS_LO16) but the rule is less strict in practice. |
362 | /// |
363 | /// The de facto standard is lenient in the following ways: |
364 | /// - 'Immediately following' does not refer to the next relocation entry but |
365 | /// the next matching relocation. |
366 | /// - There may be multiple high parts relocations for one low part relocation. |
367 | /// - There may be multiple low part relocations for one high part relocation. |
368 | /// - The AHL addend in each part does not have to be exactly equal as long as |
369 | /// the difference does not affect the carry bit from bit 15 into 16. This is |
370 | /// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading |
371 | /// both halves of a long long. |
372 | /// |
373 | /// See getMatchingLoType() for a description of which high part relocations |
374 | /// match which low part relocations. One particular thing to note is that |
375 | /// R_MIPS_GOT16 and similar only have AHL addends if they refer to local |
376 | /// symbols. |
377 | /// |
378 | /// It should also be noted that this function is not affected by whether |
379 | /// the symbol was kept or rewritten into a section-relative equivalent. We |
380 | /// always match using the expressions from the source. |
381 | void MipsELFObjectWriter::sortRelocs(std::vector<ELFRelocationEntry> &Relocs) { |
382 | // We do not need to sort the relocation table for RELA relocations which |
383 | // N32/N64 uses as the relocation addend contains the value we require, |
384 | // rather than it being split across a pair of relocations. |
385 | if (hasRelocationAddend()) |
386 | return; |
387 | |
388 | // Sort relocations by the address they are applied to. |
389 | llvm::sort(C&: Relocs, |
390 | Comp: [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) { |
391 | return A.Offset < B.Offset; |
392 | }); |
393 | |
394 | // Place relocations in a list for reorder convenience. Hi16 contains the |
395 | // iterators of high-part relocations. |
396 | std::list<MipsRelocationEntry> Sorted; |
397 | SmallVector<std::list<MipsRelocationEntry>::iterator, 0> Hi16; |
398 | for (auto &R : Relocs) { |
399 | Sorted.push_back(x: R); |
400 | if (getMatchingLoType(Reloc: R) != ELF::R_MIPS_NONE) |
401 | Hi16.push_back(Elt: std::prev(x: Sorted.end())); |
402 | } |
403 | |
404 | for (auto I : Hi16) { |
405 | auto &R = I->R; |
406 | unsigned MatchingType = getMatchingLoType(Reloc: R); |
407 | // If the next relocation is a perfect match, continue; |
408 | if (std::next(x: I) != Sorted.end() && |
409 | isMatchingReloc(MatchingType, R, X: std::next(x: I)->R)) |
410 | continue; |
411 | // Otherwise, find the best matching low-part relocation with the following |
412 | // criteria. It must have the same symbol and its addend is no lower than |
413 | // that of the current high-part. |
414 | // |
415 | // (1) %lo with a smaller offset is preferred. |
416 | // (2) %lo with the same offset that is unmatched is preferred. |
417 | // (3) later %lo is preferred. |
418 | auto Best = Sorted.end(); |
419 | for (auto J = Sorted.begin(); J != Sorted.end(); ++J) { |
420 | auto &R1 = J->R; |
421 | if (R1.Type == MatchingType && R.Symbol == R1.Symbol && |
422 | R.Addend <= R1.Addend && |
423 | (Best == Sorted.end() || R1.Addend < Best->R.Addend || |
424 | (!Best->Matched && R1.Addend == Best->R.Addend))) |
425 | Best = J; |
426 | } |
427 | if (Best != Sorted.end() && R.Addend == Best->R.Addend) |
428 | Best->Matched = true; |
429 | |
430 | // Move the high-part before the low-part, or if not found, the end of the |
431 | // list. The unmatched high-part will lead to a linker warning/error. |
432 | Sorted.splice(position: Best, x&: Sorted, i: I); |
433 | } |
434 | |
435 | assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed" ); |
436 | |
437 | // Overwrite the original vector with the sorted elements. |
438 | unsigned CopyTo = 0; |
439 | for (const auto &R : Sorted) |
440 | Relocs[CopyTo++] = R.R; |
441 | } |
442 | |
443 | bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V, |
444 | unsigned Type) const { |
445 | // If it's a compound relocation for N64 then we need the relocation if any |
446 | // sub-relocation needs it. |
447 | if (!isUInt<8>(x: Type)) |
448 | return needsRelocateWithSymbol(V, Type: Type & 0xff) || |
449 | needsRelocateWithSymbol(V, Type: (Type >> 8) & 0xff) || |
450 | needsRelocateWithSymbol(V, Type: (Type >> 16) & 0xff); |
451 | |
452 | switch (Type) { |
453 | default: |
454 | errs() << Type << "\n" ; |
455 | llvm_unreachable("Unexpected relocation" ); |
456 | return true; |
457 | |
458 | // This relocation doesn't affect the section data. |
459 | case ELF::R_MIPS_NONE: |
460 | return false; |
461 | |
462 | // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done |
463 | // by the static linker by matching the symbol and offset. |
464 | // We only see one relocation at a time but it's still safe to relocate with |
465 | // the section so long as both relocations make the same decision. |
466 | // |
467 | // Some older linkers may require the symbol for particular cases. Such cases |
468 | // are not supported yet but can be added as required. |
469 | case ELF::R_MIPS_GOT16: |
470 | case ELF::R_MIPS16_GOT16: |
471 | case ELF::R_MICROMIPS_GOT16: |
472 | case ELF::R_MIPS_HIGHER: |
473 | case ELF::R_MIPS_HIGHEST: |
474 | case ELF::R_MIPS_HI16: |
475 | case ELF::R_MIPS16_HI16: |
476 | case ELF::R_MICROMIPS_HI16: |
477 | case ELF::R_MIPS_LO16: |
478 | case ELF::R_MIPS16_LO16: |
479 | case ELF::R_MICROMIPS_LO16: |
480 | // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but |
481 | // we neglect to handle the adjustment to the LSB of the addend that |
482 | // it causes in applyFixup() and similar. |
483 | if (cast<MCSymbolELF>(Val: V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS) |
484 | return true; |
485 | return false; |
486 | |
487 | case ELF::R_MIPS_GOT_PAGE: |
488 | case ELF::R_MICROMIPS_GOT_PAGE: |
489 | case ELF::R_MIPS_GOT_OFST: |
490 | case ELF::R_MICROMIPS_GOT_OFST: |
491 | case ELF::R_MIPS_16: |
492 | case ELF::R_MIPS_32: |
493 | case ELF::R_MIPS_GPREL32: |
494 | if (cast<MCSymbolELF>(Val: V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS) |
495 | return true; |
496 | [[fallthrough]]; |
497 | case ELF::R_MIPS_26: |
498 | case ELF::R_MIPS_64: |
499 | case ELF::R_MIPS_GPREL16: |
500 | case ELF::R_MIPS_PC16: |
501 | case ELF::R_MIPS_SUB: |
502 | return false; |
503 | |
504 | // FIXME: Many of these relocations should probably return false but this |
505 | // hasn't been confirmed to be safe yet. |
506 | case ELF::R_MIPS_REL32: |
507 | case ELF::R_MIPS_LITERAL: |
508 | case ELF::R_MIPS_CALL16: |
509 | case ELF::R_MIPS_SHIFT5: |
510 | case ELF::R_MIPS_SHIFT6: |
511 | case ELF::R_MIPS_GOT_DISP: |
512 | case ELF::R_MIPS_GOT_HI16: |
513 | case ELF::R_MIPS_GOT_LO16: |
514 | case ELF::R_MIPS_INSERT_A: |
515 | case ELF::R_MIPS_INSERT_B: |
516 | case ELF::R_MIPS_DELETE: |
517 | case ELF::R_MIPS_CALL_HI16: |
518 | case ELF::R_MIPS_CALL_LO16: |
519 | case ELF::R_MIPS_SCN_DISP: |
520 | case ELF::R_MIPS_REL16: |
521 | case ELF::R_MIPS_ADD_IMMEDIATE: |
522 | case ELF::R_MIPS_PJUMP: |
523 | case ELF::R_MIPS_RELGOT: |
524 | case ELF::R_MIPS_JALR: |
525 | case ELF::R_MIPS_TLS_DTPMOD32: |
526 | case ELF::R_MIPS_TLS_DTPREL32: |
527 | case ELF::R_MIPS_TLS_DTPMOD64: |
528 | case ELF::R_MIPS_TLS_DTPREL64: |
529 | case ELF::R_MIPS_TLS_GD: |
530 | case ELF::R_MIPS_TLS_LDM: |
531 | case ELF::R_MIPS_TLS_DTPREL_HI16: |
532 | case ELF::R_MIPS_TLS_DTPREL_LO16: |
533 | case ELF::R_MIPS_TLS_GOTTPREL: |
534 | case ELF::R_MIPS_TLS_TPREL32: |
535 | case ELF::R_MIPS_TLS_TPREL64: |
536 | case ELF::R_MIPS_TLS_TPREL_HI16: |
537 | case ELF::R_MIPS_TLS_TPREL_LO16: |
538 | case ELF::R_MIPS_GLOB_DAT: |
539 | case ELF::R_MIPS_PC21_S2: |
540 | case ELF::R_MIPS_PC26_S2: |
541 | case ELF::R_MIPS_PC18_S3: |
542 | case ELF::R_MIPS_PC19_S2: |
543 | case ELF::R_MIPS_PCHI16: |
544 | case ELF::R_MIPS_PCLO16: |
545 | case ELF::R_MIPS_COPY: |
546 | case ELF::R_MIPS_JUMP_SLOT: |
547 | case ELF::R_MIPS_NUM: |
548 | case ELF::R_MIPS_PC32: |
549 | case ELF::R_MIPS_EH: |
550 | case ELF::R_MICROMIPS_26_S1: |
551 | case ELF::R_MICROMIPS_GPREL16: |
552 | case ELF::R_MICROMIPS_LITERAL: |
553 | case ELF::R_MICROMIPS_PC7_S1: |
554 | case ELF::R_MICROMIPS_PC10_S1: |
555 | case ELF::R_MICROMIPS_PC16_S1: |
556 | case ELF::R_MICROMIPS_CALL16: |
557 | case ELF::R_MICROMIPS_GOT_DISP: |
558 | case ELF::R_MICROMIPS_GOT_HI16: |
559 | case ELF::R_MICROMIPS_GOT_LO16: |
560 | case ELF::R_MICROMIPS_SUB: |
561 | case ELF::R_MICROMIPS_HIGHER: |
562 | case ELF::R_MICROMIPS_HIGHEST: |
563 | case ELF::R_MICROMIPS_CALL_HI16: |
564 | case ELF::R_MICROMIPS_CALL_LO16: |
565 | case ELF::R_MICROMIPS_SCN_DISP: |
566 | case ELF::R_MICROMIPS_JALR: |
567 | case ELF::R_MICROMIPS_HI0_LO16: |
568 | case ELF::R_MICROMIPS_TLS_GD: |
569 | case ELF::R_MICROMIPS_TLS_LDM: |
570 | case ELF::R_MICROMIPS_TLS_DTPREL_HI16: |
571 | case ELF::R_MICROMIPS_TLS_DTPREL_LO16: |
572 | case ELF::R_MICROMIPS_TLS_GOTTPREL: |
573 | case ELF::R_MICROMIPS_TLS_TPREL_HI16: |
574 | case ELF::R_MICROMIPS_TLS_TPREL_LO16: |
575 | case ELF::R_MICROMIPS_GPREL7_S2: |
576 | case ELF::R_MICROMIPS_PC23_S2: |
577 | case ELF::R_MICROMIPS_PC21_S1: |
578 | case ELF::R_MICROMIPS_PC26_S1: |
579 | case ELF::R_MICROMIPS_PC18_S3: |
580 | case ELF::R_MICROMIPS_PC19_S2: |
581 | return true; |
582 | |
583 | // FIXME: Many of these should probably return false but MIPS16 isn't |
584 | // supported by the integrated assembler. |
585 | case ELF::R_MIPS16_26: |
586 | case ELF::R_MIPS16_GPREL: |
587 | case ELF::R_MIPS16_CALL16: |
588 | case ELF::R_MIPS16_TLS_GD: |
589 | case ELF::R_MIPS16_TLS_LDM: |
590 | case ELF::R_MIPS16_TLS_DTPREL_HI16: |
591 | case ELF::R_MIPS16_TLS_DTPREL_LO16: |
592 | case ELF::R_MIPS16_TLS_GOTTPREL: |
593 | case ELF::R_MIPS16_TLS_TPREL_HI16: |
594 | case ELF::R_MIPS16_TLS_TPREL_LO16: |
595 | llvm_unreachable("Unsupported MIPS16 relocation" ); |
596 | return true; |
597 | } |
598 | } |
599 | |
600 | std::unique_ptr<MCObjectTargetWriter> |
601 | llvm::createMipsELFObjectWriter(const Triple &TT, bool IsN32) { |
602 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType: TT.getOS()); |
603 | bool IsN64 = TT.isArch64Bit() && !IsN32; |
604 | bool HasRelocationAddend = TT.isArch64Bit(); |
605 | return std::make_unique<MipsELFObjectWriter>(args&: OSABI, args&: HasRelocationAddend, |
606 | args&: IsN64); |
607 | } |
608 | |