Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
2
//
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"
12
#include "llvm/ADT/STLExtras.h"
13
#include "llvm/BinaryFormat/ELF.h"
14
#include "llvm/MC/MCELFObjectWriter.h"
15
#include "llvm/MC/MCFixup.h"
16
#include "llvm/MC/MCSymbolELF.h"
17
#include "llvm/Support/Casting.h"
18
#include "llvm/Support/Compiler.h"
19
#include "llvm/Support/Debug.h"
20
#include "llvm/Support/ErrorHandling.h"
21
#include "llvm/Support/MathExtras.h"
22
#include "llvm/Support/raw_ostream.h"
23
#include <algorithm>
24
#include <cassert>
25
#include <cstdint>
26
#include <iterator>
27
#include <list>
28
#include <utility>
29
30
#define DEBUG_TYPE "mips-elf-object-writer"
31
32
using namespace llvm;
33
34
namespace {
35
36
/// Holds additional information needed by the relocation ordering algorithm.
37
struct MipsRelocationEntry {
38
  const ELFRelocationEntry R; ///< The relocation.
39
  bool Matched = false;       ///< Is this relocation part of a match.
40
41
768
  MipsRelocationEntry(const ELFRelocationEntry &R) : R(R) {}
42
43
0
  void print(raw_ostream &Out) const {
44
0
    R.print(Out);
45
0
    Out << ", Matched=" << Matched;
46
0
  }
47
};
48
49
#ifndef NDEBUG
50
raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
51
  RHS.print(OS);
52
  return OS;
53
}
54
#endif
55
56
class MipsELFObjectWriter : public MCELFObjectTargetWriter {
57
public:
58
  MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64,
59
                      bool IsLittleEndian);
60
61
462
  ~MipsELFObjectWriter() override = default;
62
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;
67
  void sortRelocs(const MCAssembler &Asm,
68
                  std::vector<ELFRelocationEntry> &Relocs) override;
69
};
70
71
/// The possible results of the Predicate function used by find_best.
72
enum 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
81
/// 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.
84
template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
85
static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
86
                                                    OutputIt1 d1, OutputIt2 d2,
87
145
                                                    UnaryPredicate Predicate) {
88
913
  for (InputIt I = First; 
I != Last913
;
++I768
) {
89
768
    if (
Predicate(*I)768
) {
90
131
      *d1 = *I;
91
131
      d1++;
92
768
    } else {
93
637
      *d2 = *I;
94
637
      d2++;
95
637
    }
96
768
  }
97
145
98
145
  return std::make_pair(d1, d2);
99
145
}
100
101
/// 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.
110
template <class InputIt, class UnaryPredicate, class Comparator>
111
static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
112
131
                         Comparator BetterThan) {
113
131
  InputIt Best = Last;
114
131
115
484
  for (InputIt I = First; 
I != Last484
;
++I353
) {
116
465
    unsigned Matched = Predicate(*I);
117
465
    if (
Matched != FindBest_NoMatch465
) {
118
151
      DEBUG(dbgs() << std::distance(First, I) << " is a match (";
119
151
            I->print(dbgs()); dbgs() << ")\n");
120
151
      if (
Best == Last || 151
BetterThan(*I, *Best)23
) {
121
148
        DEBUG(dbgs() << ".. and it beats the last one\n");
122
148
        Best = I;
123
148
      }
124
151
    }
125
465
    if (
Matched == FindBest_PerfectMatch465
) {
126
112
      DEBUG(dbgs() << ".. and it is unbeatable\n");
127
112
      break;
128
112
    }
129
465
  }
130
131
131
131
  return Best;
132
131
}
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.
141
899
static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
142
899
  unsigned Type = Reloc.Type;
143
899
  if (Type == ELF::R_MIPS_HI16)
144
122
    return ELF::R_MIPS_LO16;
145
777
  
if (777
Type == ELF::R_MICROMIPS_HI16777
)
146
22
    return ELF::R_MICROMIPS_LO16;
147
755
  
if (755
Type == ELF::R_MIPS16_HI16755
)
148
0
    return ELF::R_MIPS16_LO16;
149
755
150
755
  
if (755
Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL755
)
151
363
    return ELF::R_MIPS_NONE;
152
392
153
392
  
if (392
Type == ELF::R_MIPS_GOT16392
)
154
96
    return ELF::R_MIPS_LO16;
155
296
  
if (296
Type == ELF::R_MICROMIPS_GOT16296
)
156
22
    return ELF::R_MICROMIPS_LO16;
157
274
  
if (274
Type == ELF::R_MIPS16_GOT16274
)
158
0
    return ELF::R_MIPS16_LO16;
159
274
160
274
  return ELF::R_MIPS_NONE;
161
274
}
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.
177
static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
178
                                               const ELFRelocationEntry &R,
179
465
                                               unsigned MatchingType) {
180
465
  if (
X.R.Type == MatchingType && 465
X.R.OriginalSymbol == R.OriginalSymbol259
) {
181
158
    if (!X.Matched &&
182
133
        X.R.OriginalAddend == R.OriginalAddend)
183
112
      return FindBest_PerfectMatch;
184
46
    else 
if (46
X.R.OriginalAddend >= R.OriginalAddend46
)
185
39
      return FindBest_Match;
186
314
  }
187
314
  return FindBest_NoMatch;
188
314
}
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.
196
static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
197
23
                                  const MipsRelocationEntry &PreviousBest) {
198
23
  if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
199
10
    return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
200
13
  
return PreviousBest.Matched && 13
!Candidate.Matched13
;
201
23
}
202
203
#ifndef NDEBUG
204
/// Print all the relocations.
205
template <class Container>
206
static void dumpRelocs(const char *Prefix, const Container &Relocs) {
207
  for (const auto &R : Relocs)
208
    dbgs() << Prefix << R << "\n";
209
}
210
#endif
211
212
MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
213
                                         bool HasRelocationAddend, bool Is64,
214
                                         bool IsLittleEndian)
215
462
    : MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {}
216
217
unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
218
                                           const MCValue &Target,
219
                                           const MCFixup &Fixup,
220
1.18k
                                           bool IsPCRel) const {
221
1.18k
  // Determine the type of the relocation.
222
1.18k
  unsigned Kind = (unsigned)Fixup.getKind();
223
1.18k
224
1.18k
  switch (Kind) {
225
9
  case Mips::fixup_Mips_NONE:
226
9
    return ELF::R_MIPS_NONE;
227
7
  case Mips::fixup_Mips_16:
228
7
  case FK_Data_2:
229
7
    return IsPCRel ? 
ELF::R_MIPS_PC162
:
ELF::R_MIPS_165
;
230
357
  case Mips::fixup_Mips_32:
231
357
  case FK_Data_4:
232
357
    return IsPCRel ? 
ELF::R_MIPS_PC3217
:
ELF::R_MIPS_32340
;
233
811
  }
234
811
235
811
  
if (811
IsPCRel811
) {
236
115
    switch (Kind) {
237
22
    case Mips::fixup_Mips_Branch_PCRel:
238
22
    case Mips::fixup_Mips_PC16:
239
22
      return ELF::R_MIPS_PC16;
240
4
    case Mips::fixup_MICROMIPS_PC7_S1:
241
4
      return ELF::R_MICROMIPS_PC7_S1;
242
3
    case Mips::fixup_MICROMIPS_PC10_S1:
243
3
      return ELF::R_MICROMIPS_PC10_S1;
244
11
    case Mips::fixup_MICROMIPS_PC16_S1:
245
11
      return ELF::R_MICROMIPS_PC16_S1;
246
4
    case Mips::fixup_MICROMIPS_PC26_S1:
247
4
      return ELF::R_MICROMIPS_PC26_S1;
248
6
    case Mips::fixup_MICROMIPS_PC19_S2:
249
6
      return ELF::R_MICROMIPS_PC19_S2;
250
1
    case Mips::fixup_MICROMIPS_PC18_S3:
251
1
      return ELF::R_MICROMIPS_PC18_S3;
252
4
    case Mips::fixup_MICROMIPS_PC21_S1:
253
4
      return ELF::R_MICROMIPS_PC21_S1;
254
15
    case Mips::fixup_MIPS_PC19_S2:
255
15
      return ELF::R_MIPS_PC19_S2;
256
5
    case Mips::fixup_MIPS_PC18_S3:
257
5
      return ELF::R_MIPS_PC18_S3;
258
11
    case Mips::fixup_MIPS_PC21_S2:
259
11
      return ELF::R_MIPS_PC21_S2;
260
11
    case Mips::fixup_MIPS_PC26_S2:
261
11
      return ELF::R_MIPS_PC26_S2;
262
9
    case Mips::fixup_MIPS_PCHI16:
263
9
      return ELF::R_MIPS_PCHI16;
264
9
    case Mips::fixup_MIPS_PCLO16:
265
9
      return ELF::R_MIPS_PCLO16;
266
0
    }
267
0
268
0
    
llvm_unreachable0
("invalid PC-relative fixup kind!");
269
0
  }
270
696
271
696
  switch (Kind) {
272
48
  case Mips::fixup_Mips_64:
273
48
  case FK_Data_8:
274
48
    return ELF::R_MIPS_64;
275
4
  case FK_DTPRel_4:
276
4
    return ELF::R_MIPS_TLS_DTPREL32;
277
4
  case FK_DTPRel_8:
278
4
    return ELF::R_MIPS_TLS_DTPREL64;
279
3
  case FK_TPRel_4:
280
3
    return ELF::R_MIPS_TLS_TPREL32;
281
3
  case FK_TPRel_8:
282
3
    return ELF::R_MIPS_TLS_TPREL64;
283
24
  case FK_GPRel_4:
284
24
    if (
is64Bit()24
) {
285
13
      unsigned Type = (unsigned)ELF::R_MIPS_NONE;
286
13
      Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
287
13
      Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
288
13
      Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
289
13
      return Type;
290
13
    }
291
11
    return ELF::R_MIPS_GPREL32;
292
8
  case Mips::fixup_Mips_GPREL16:
293
8
    return ELF::R_MIPS_GPREL16;
294
56
  case Mips::fixup_Mips_26:
295
56
    return ELF::R_MIPS_26;
296
30
  case Mips::fixup_Mips_CALL16:
297
30
    return ELF::R_MIPS_CALL16;
298
73
  case Mips::fixup_Mips_GOT:
299
73
    return ELF::R_MIPS_GOT16;
300
66
  case Mips::fixup_Mips_HI16:
301
66
    return ELF::R_MIPS_HI16;
302
118
  case Mips::fixup_Mips_LO16:
303
118
    return ELF::R_MIPS_LO16;
304
9
  case Mips::fixup_Mips_TLSGD:
305
9
    return ELF::R_MIPS_TLS_GD;
306
7
  case Mips::fixup_Mips_GOTTPREL:
307
7
    return ELF::R_MIPS_TLS_GOTTPREL;
308
3
  case Mips::fixup_Mips_TPREL_HI:
309
3
    return ELF::R_MIPS_TLS_TPREL_HI16;
310
3
  case Mips::fixup_Mips_TPREL_LO:
311
3
    return ELF::R_MIPS_TLS_TPREL_LO16;
312
6
  case Mips::fixup_Mips_TLSLDM:
313
6
    return ELF::R_MIPS_TLS_LDM;
314
4
  case Mips::fixup_Mips_DTPREL_HI:
315
4
    return ELF::R_MIPS_TLS_DTPREL_HI16;
316
5
  case Mips::fixup_Mips_DTPREL_LO:
317
5
    return ELF::R_MIPS_TLS_DTPREL_LO16;
318
20
  case Mips::fixup_Mips_GOT_PAGE:
319
20
    return ELF::R_MIPS_GOT_PAGE;
320
18
  case Mips::fixup_Mips_GOT_OFST:
321
18
    return ELF::R_MIPS_GOT_OFST;
322
23
  case Mips::fixup_Mips_GOT_DISP:
323
23
    return ELF::R_MIPS_GOT_DISP;
324
21
  case Mips::fixup_Mips_GPOFF_HI: {
325
21
    unsigned Type = (unsigned)ELF::R_MIPS_NONE;
326
21
    Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
327
21
    Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
328
21
    Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
329
21
    return Type;
330
11
  }
331
23
  case Mips::fixup_Mips_GPOFF_LO: {
332
23
    unsigned Type = (unsigned)ELF::R_MIPS_NONE;
333
23
    Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
334
23
    Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
335
23
    Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
336
23
    return Type;
337
11
  }
338
6
  case Mips::fixup_Mips_HIGHER:
339
6
    return ELF::R_MIPS_HIGHER;
340
6
  case Mips::fixup_Mips_HIGHEST:
341
6
    return ELF::R_MIPS_HIGHEST;
342
0
  case Mips::fixup_Mips_SUB:
343
0
    return ELF::R_MIPS_SUB;
344
8
  case Mips::fixup_Mips_GOT_HI16:
345
8
    return ELF::R_MIPS_GOT_HI16;
346
8
  case Mips::fixup_Mips_GOT_LO16:
347
8
    return ELF::R_MIPS_GOT_LO16;
348
6
  case Mips::fixup_Mips_CALL_HI16:
349
6
    return ELF::R_MIPS_CALL_HI16;
350
6
  case Mips::fixup_Mips_CALL_LO16:
351
6
    return ELF::R_MIPS_CALL_LO16;
352
7
  case Mips::fixup_MICROMIPS_26_S1:
353
7
    return ELF::R_MICROMIPS_26_S1;
354
11
  case Mips::fixup_MICROMIPS_HI16:
355
11
    return ELF::R_MICROMIPS_HI16;
356
22
  case Mips::fixup_MICROMIPS_LO16:
357
22
    return ELF::R_MICROMIPS_LO16;
358
15
  case Mips::fixup_MICROMIPS_GOT16:
359
15
    return ELF::R_MICROMIPS_GOT16;
360
5
  case Mips::fixup_MICROMIPS_CALL16:
361
5
    return ELF::R_MICROMIPS_CALL16;
362
1
  case Mips::fixup_MICROMIPS_GOT_DISP:
363
1
    return ELF::R_MICROMIPS_GOT_DISP;
364
4
  case Mips::fixup_MICROMIPS_GOT_PAGE:
365
4
    return ELF::R_MICROMIPS_GOT_PAGE;
366
4
  case Mips::fixup_MICROMIPS_GOT_OFST:
367
4
    return ELF::R_MICROMIPS_GOT_OFST;
368
1
  case Mips::fixup_MICROMIPS_TLS_GD:
369
1
    return ELF::R_MICROMIPS_TLS_GD;
370
1
  case Mips::fixup_MICROMIPS_TLS_LDM:
371
1
    return ELF::R_MICROMIPS_TLS_LDM;
372
1
  case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
373
1
    return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
374
1
  case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
375
1
    return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
376
2
  case Mips::fixup_MICROMIPS_GOTTPREL:
377
2
    return ELF::R_MICROMIPS_TLS_GOTTPREL;
378
1
  case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
379
1
    return ELF::R_MICROMIPS_TLS_TPREL_HI16;
380
1
  case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
381
1
    return ELF::R_MICROMIPS_TLS_TPREL_LO16;
382
0
  case Mips::fixup_MICROMIPS_SUB:
383
0
    return ELF::R_MICROMIPS_SUB;
384
0
  }
385
0
386
0
  
llvm_unreachable0
("invalid fixup kind!");
387
0
}
388
389
/// Sort relocation table entries by offset except where another order is
390
/// required by the MIPS ABI.
391
///
392
/// MIPS has a few relocations that have an AHL component in the expression used
393
/// to evaluate them. This AHL component is an addend with the same number of
394
/// bits as a symbol value but not all of our ABI's are able to supply a
395
/// sufficiently sized addend in a single relocation.
396
///
397
/// The O32 ABI for example, uses REL relocations which store the addend in the
398
/// section data. All the relocations with AHL components affect 16-bit fields
399
/// so the addend for a single relocation is limited to 16-bit. This ABI
400
/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
401
/// R_MIPS_LO16) and distributing the addend between the linked relocations. The
402
/// ABI mandates that such relocations must be next to each other in a
403
/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
404
/// matching R_MIPS_LO16) but the rule is less strict in practice.
405
///
406
/// The de facto standard is lenient in the following ways:
407
/// - 'Immediately following' does not refer to the next relocation entry but
408
///   the next matching relocation.
409
/// - There may be multiple high parts relocations for one low part relocation.
410
/// - There may be multiple low part relocations for one high part relocation.
411
/// - The AHL addend in each part does not have to be exactly equal as long as
412
///   the difference does not affect the carry bit from bit 15 into 16. This is
413
///   to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
414
///   both halves of a long long.
415
///
416
/// See getMatchingLoType() for a description of which high part relocations
417
/// match which low part relocations. One particular thing to note is that
418
/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
419
/// symbols.
420
///
421
/// It should also be noted that this function is not affected by whether
422
/// the symbol was kept or rewritten into a section-relative equivalent. We
423
/// always match using the expressions from the source.
424
void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
425
347
                                     std::vector<ELFRelocationEntry> &Relocs) {
426
347
  // We do not need to sort the relocation table for RELA relocations which
427
347
  // N32/N64 uses as the relocation addend contains the value we require,
428
347
  // rather than it being split across a pair of relocations.
429
347
  if (hasRelocationAddend())
430
118
    return;
431
229
432
229
  
if (229
Relocs.size() < 2229
)
433
84
    return;
434
145
435
145
  // Sort relocations by the address they are applied to.
436
145
  std::sort(Relocs.begin(), Relocs.end(),
437
2.05k
            [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
438
2.05k
              return A.Offset < B.Offset;
439
2.05k
            });
440
145
441
145
  std::list<MipsRelocationEntry> Sorted;
442
145
  std::list<ELFRelocationEntry> Remainder;
443
145
444
145
  DEBUG(dumpRelocs("R: ", Relocs));
445
145
446
145
  // Separate the movable relocations (AHL relocations using the high bits) from
447
145
  // the immobile relocations (everything else). This does not preserve high/low
448
145
  // matches that already existed in the input.
449
145
  copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
450
768
               std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
451
768
                 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
452
768
               });
453
145
454
131
  for (auto &R : Remainder) {
455
131
    DEBUG(dbgs() << "Matching: " << R << "\n");
456
131
457
131
    unsigned MatchingType = getMatchingLoType(R);
458
131
    assert(MatchingType != ELF::R_MIPS_NONE &&
459
131
           "Wrong list for reloc that doesn't need a match");
460
131
461
131
    // Find the best matching relocation for the current high part.
462
131
    // See isMatchingReloc for a description of a matching relocation and
463
131
    // compareMatchingRelocs for a description of what 'best' means.
464
131
    auto InsertionPoint =
465
131
        find_best(Sorted.begin(), Sorted.end(),
466
465
                  [&R, &MatchingType](const MipsRelocationEntry &X) {
467
465
                    return isMatchingReloc(X, R, MatchingType);
468
465
                  },
469
131
                  compareMatchingRelocs);
470
131
471
131
    // If we matched then insert the high part in front of the match and mark
472
131
    // both relocations as being involved in a match. We only mark the high
473
131
    // part for cosmetic reasons in the debug output.
474
131
    //
475
131
    // If we failed to find a match then the high part is orphaned. This is not
476
131
    // permitted since the relocation cannot be evaluated without knowing the
477
131
    // carry-in. We can sometimes handle this using a matching low part that is
478
131
    // already used in a match but we already cover that case in
479
131
    // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
480
131
    // should insert the high part at the end of the list. This will cause the
481
131
    // linker to fail but the alternative is to cause the linker to bind the
482
131
    // high part to a semi-matching low part and silently calculate the wrong
483
131
    // value. Unfortunately we have no means to warn the user that we did this
484
131
    // so leave it up to the linker to complain about it.
485
131
    if (InsertionPoint != Sorted.end())
486
128
      InsertionPoint->Matched = true;
487
131
    Sorted.insert(InsertionPoint, R)->Matched = true;
488
131
  }
489
145
490
145
  DEBUG(dumpRelocs("S: ", Sorted));
491
145
492
145
  assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
493
145
494
145
  // Overwrite the original vector with the sorted elements. The caller expects
495
145
  // them in reverse order.
496
145
  unsigned CopyTo = 0;
497
145
  for (const auto &R : reverse(Sorted))
498
768
    Relocs[CopyTo++] = R.R;
499
347
}
500
501
bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
502
498
                                                  unsigned Type) const {
503
498
  // If it's a compound relocation for N64 then we need the relocation if any
504
498
  // sub-relocation needs it.
505
498
  if (!isUInt<8>(Type))
506
17
    return needsRelocateWithSymbol(Sym, Type & 0xff) ||
507
17
           needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
508
17
           needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
509
481
510
481
  switch (Type) {
511
0
  default:
512
0
    errs() << Type << "\n";
513
0
    llvm_unreachable("Unexpected relocation");
514
0
    return true;
515
481
516
481
  // This relocation doesn't affect the section data.
517
19
  case ELF::R_MIPS_NONE:
518
19
    return false;
519
481
520
481
  // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
521
481
  // by the static linker by matching the symbol and offset.
522
481
  // We only see one relocation at a time but it's still safe to relocate with
523
481
  // the section so long as both relocations make the same decision.
524
481
  //
525
481
  // Some older linkers may require the symbol for particular cases. Such cases
526
481
  // are not supported yet but can be added as required.
527
134
  case ELF::R_MIPS_GOT16:
528
134
  case ELF::R_MIPS16_GOT16:
529
134
  case ELF::R_MICROMIPS_GOT16:
530
134
  case ELF::R_MIPS_HIGHER:
531
134
  case ELF::R_MIPS_HIGHEST:
532
134
  case ELF::R_MIPS_HI16:
533
134
  case ELF::R_MIPS16_HI16:
534
134
  case ELF::R_MICROMIPS_HI16:
535
134
  case ELF::R_MIPS_LO16:
536
134
  case ELF::R_MIPS16_LO16:
537
134
  case ELF::R_MICROMIPS_LO16:
538
134
    // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
539
134
    //        we neglect to handle the adjustment to the LSB of the addend that
540
134
    //        it causes in applyFixup() and similar.
541
134
    if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
542
10
      return true;
543
124
    return false;
544
124
545
220
  case ELF::R_MIPS_GOT_PAGE:
546
220
  case ELF::R_MICROMIPS_GOT_PAGE:
547
220
  case ELF::R_MIPS_GOT_OFST:
548
220
  case ELF::R_MICROMIPS_GOT_OFST:
549
220
  case ELF::R_MIPS_16:
550
220
  case ELF::R_MIPS_32:
551
220
  case ELF::R_MIPS_GPREL32:
552
220
    if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
553
6
      return true;
554
214
    
LLVM_FALLTHROUGH214
;
555
297
  case ELF::R_MIPS_26:
556
297
  case ELF::R_MIPS_64:
557
297
  case ELF::R_MIPS_GPREL16:
558
297
  case ELF::R_MIPS_PC16:
559
297
  case ELF::R_MIPS_SUB:
560
297
    return false;
561
297
562
297
  // FIXME: Many of these relocations should probably return false but this
563
297
  //        hasn't been confirmed to be safe yet.
564
25
  case ELF::R_MIPS_REL32:
565
25
  case ELF::R_MIPS_LITERAL:
566
25
  case ELF::R_MIPS_CALL16:
567
25
  case ELF::R_MIPS_SHIFT5:
568
25
  case ELF::R_MIPS_SHIFT6:
569
25
  case ELF::R_MIPS_GOT_DISP:
570
25
  case ELF::R_MIPS_GOT_HI16:
571
25
  case ELF::R_MIPS_GOT_LO16:
572
25
  case ELF::R_MIPS_INSERT_A:
573
25
  case ELF::R_MIPS_INSERT_B:
574
25
  case ELF::R_MIPS_DELETE:
575
25
  case ELF::R_MIPS_CALL_HI16:
576
25
  case ELF::R_MIPS_CALL_LO16:
577
25
  case ELF::R_MIPS_SCN_DISP:
578
25
  case ELF::R_MIPS_REL16:
579
25
  case ELF::R_MIPS_ADD_IMMEDIATE:
580
25
  case ELF::R_MIPS_PJUMP:
581
25
  case ELF::R_MIPS_RELGOT:
582
25
  case ELF::R_MIPS_JALR:
583
25
  case ELF::R_MIPS_TLS_DTPMOD32:
584
25
  case ELF::R_MIPS_TLS_DTPREL32:
585
25
  case ELF::R_MIPS_TLS_DTPMOD64:
586
25
  case ELF::R_MIPS_TLS_DTPREL64:
587
25
  case ELF::R_MIPS_TLS_GD:
588
25
  case ELF::R_MIPS_TLS_LDM:
589
25
  case ELF::R_MIPS_TLS_DTPREL_HI16:
590
25
  case ELF::R_MIPS_TLS_DTPREL_LO16:
591
25
  case ELF::R_MIPS_TLS_GOTTPREL:
592
25
  case ELF::R_MIPS_TLS_TPREL32:
593
25
  case ELF::R_MIPS_TLS_TPREL64:
594
25
  case ELF::R_MIPS_TLS_TPREL_HI16:
595
25
  case ELF::R_MIPS_TLS_TPREL_LO16:
596
25
  case ELF::R_MIPS_GLOB_DAT:
597
25
  case ELF::R_MIPS_PC21_S2:
598
25
  case ELF::R_MIPS_PC26_S2:
599
25
  case ELF::R_MIPS_PC18_S3:
600
25
  case ELF::R_MIPS_PC19_S2:
601
25
  case ELF::R_MIPS_PCHI16:
602
25
  case ELF::R_MIPS_PCLO16:
603
25
  case ELF::R_MIPS_COPY:
604
25
  case ELF::R_MIPS_JUMP_SLOT:
605
25
  case ELF::R_MIPS_NUM:
606
25
  case ELF::R_MIPS_PC32:
607
25
  case ELF::R_MIPS_EH:
608
25
  case ELF::R_MICROMIPS_26_S1:
609
25
  case ELF::R_MICROMIPS_GPREL16:
610
25
  case ELF::R_MICROMIPS_LITERAL:
611
25
  case ELF::R_MICROMIPS_PC7_S1:
612
25
  case ELF::R_MICROMIPS_PC10_S1:
613
25
  case ELF::R_MICROMIPS_PC16_S1:
614
25
  case ELF::R_MICROMIPS_CALL16:
615
25
  case ELF::R_MICROMIPS_GOT_DISP:
616
25
  case ELF::R_MICROMIPS_GOT_HI16:
617
25
  case ELF::R_MICROMIPS_GOT_LO16:
618
25
  case ELF::R_MICROMIPS_SUB:
619
25
  case ELF::R_MICROMIPS_HIGHER:
620
25
  case ELF::R_MICROMIPS_HIGHEST:
621
25
  case ELF::R_MICROMIPS_CALL_HI16:
622
25
  case ELF::R_MICROMIPS_CALL_LO16:
623
25
  case ELF::R_MICROMIPS_SCN_DISP:
624
25
  case ELF::R_MICROMIPS_JALR:
625
25
  case ELF::R_MICROMIPS_HI0_LO16:
626
25
  case ELF::R_MICROMIPS_TLS_GD:
627
25
  case ELF::R_MICROMIPS_TLS_LDM:
628
25
  case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
629
25
  case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
630
25
  case ELF::R_MICROMIPS_TLS_GOTTPREL:
631
25
  case ELF::R_MICROMIPS_TLS_TPREL_HI16:
632
25
  case ELF::R_MICROMIPS_TLS_TPREL_LO16:
633
25
  case ELF::R_MICROMIPS_GPREL7_S2:
634
25
  case ELF::R_MICROMIPS_PC23_S2:
635
25
  case ELF::R_MICROMIPS_PC21_S1:
636
25
  case ELF::R_MICROMIPS_PC26_S1:
637
25
  case ELF::R_MICROMIPS_PC18_S3:
638
25
  case ELF::R_MICROMIPS_PC19_S2:
639
25
    return true;
640
25
641
25
  // FIXME: Many of these should probably return false but MIPS16 isn't
642
25
  //        supported by the integrated assembler.
643
0
  case ELF::R_MIPS16_26:
644
0
  case ELF::R_MIPS16_GPREL:
645
0
  case ELF::R_MIPS16_CALL16:
646
0
  case ELF::R_MIPS16_TLS_GD:
647
0
  case ELF::R_MIPS16_TLS_LDM:
648
0
  case ELF::R_MIPS16_TLS_DTPREL_HI16:
649
0
  case ELF::R_MIPS16_TLS_DTPREL_LO16:
650
0
  case ELF::R_MIPS16_TLS_GOTTPREL:
651
0
  case ELF::R_MIPS16_TLS_TPREL_HI16:
652
0
  case ELF::R_MIPS16_TLS_TPREL_LO16:
653
0
    llvm_unreachable("Unsupported MIPS16 relocation");
654
0
    return true;
655
0
  }
656
0
}
657
658
MCObjectWriter *llvm::createMipsELFObjectWriter(raw_pwrite_stream &OS,
659
462
                                                const Triple &TT, bool IsN32) {
660
462
  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
661
158
  bool IsN64 = TT.isArch64Bit() && !IsN32;
662
462
  bool HasRelocationAddend = TT.isArch64Bit();
663
462
  auto *MOTW = new MipsELFObjectWriter(OSABI, HasRelocationAddend, IsN64,
664
462
                                       TT.isLittleEndian());
665
462
  return createELFObjectWriter(MOTW, OS, TT.isLittleEndian());
666
462
}