llvm.org GIT mirror llvm / cf0db29 include / llvm / Object / RelocVisitor.h
cf0db29

Tree @cf0db29 (Download .tar.gz)

RelocVisitor.h @cf0db29raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//===-- RelocVisitor.h - Visitor for object file relocations -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides a wrapper around all the different types of relocations
// in different file formats, such that a client can handle them in a unified
// manner by only implementing a minimal number of functions.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_OBJECT_RELOCVISITOR_H
#define LLVM_OBJECT_RELOCVISITOR_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ELF.h"
#include "llvm/Support/MachO.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {
namespace object {

struct RelocToApply {
  // The computed value after applying the relevant relocations.
  int64_t Value;

  // The width of the value; how many bytes to touch when applying the
  // relocation.
  char Width;
  RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
  RelocToApply() : Value(0), Width(0) {}
};

/// @brief Base class for object file relocation visitors.
class RelocVisitor {
public:
  explicit RelocVisitor(const ObjectFile &Obj)
    : ObjToVisit(Obj), HasError(false) {}

  // TODO: Should handle multiple applied relocations via either passing in the
  // previously computed value or just count paired relocations as a single
  // visit.
  RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) {
    if (isa<ELFObjectFileBase>(ObjToVisit))
      return visitELF(RelocType, R, Value);
    if (isa<COFFObjectFile>(ObjToVisit))
      return visitCOFF(RelocType, R, Value);
    if (isa<MachOObjectFile>(ObjToVisit))
      return visitMachO(RelocType, R, Value);

    HasError = true;
    return RelocToApply();
  }

  bool error() { return HasError; }

private:
  const ObjectFile &ObjToVisit;
  bool HasError;

  RelocToApply visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
    if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file
      switch (ObjToVisit.getArch()) {
      case Triple::x86_64:
        switch (RelocType) {
        case llvm::ELF::R_X86_64_NONE:
          return visitELF_X86_64_NONE(R);
        case llvm::ELF::R_X86_64_64:
          return visitELF_X86_64_64(R, Value);
        case llvm::ELF::R_X86_64_PC32:
          return visitELF_X86_64_PC32(R, Value);
        case llvm::ELF::R_X86_64_32:
          return visitELF_X86_64_32(R, Value);
        case llvm::ELF::R_X86_64_32S:
          return visitELF_X86_64_32S(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::aarch64:
        switch (RelocType) {
        case llvm::ELF::R_AARCH64_ABS32:
          return visitELF_AARCH64_ABS32(R, Value);
        case llvm::ELF::R_AARCH64_ABS64:
          return visitELF_AARCH64_ABS64(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::mips64el:
      case Triple::mips64:
        switch (RelocType) {
        case llvm::ELF::R_MIPS_32:
          return visitELF_MIPS_32(R, Value);
        case llvm::ELF::R_MIPS_64:
          return visitELF_MIPS_64(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::ppc64le:
      case Triple::ppc64:
        switch (RelocType) {
        case llvm::ELF::R_PPC64_ADDR32:
          return visitELF_PPC64_ADDR32(R, Value);
        case llvm::ELF::R_PPC64_ADDR64:
          return visitELF_PPC64_ADDR64(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::systemz:
        switch (RelocType) {
        case llvm::ELF::R_390_32:
          return visitELF_390_32(R, Value);
        case llvm::ELF::R_390_64:
          return visitELF_390_64(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::sparcv9:
        switch (RelocType) {
        case llvm::ELF::R_SPARC_32:
        case llvm::ELF::R_SPARC_UA32:
          return visitELF_SPARCV9_32(R, Value);
        case llvm::ELF::R_SPARC_64:
        case llvm::ELF::R_SPARC_UA64:
          return visitELF_SPARCV9_64(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      default:
        HasError = true;
        return RelocToApply();
      }
    } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file
      switch (ObjToVisit.getArch()) {
      case Triple::x86:
        switch (RelocType) {
        case llvm::ELF::R_386_NONE:
          return visitELF_386_NONE(R);
        case llvm::ELF::R_386_32:
          return visitELF_386_32(R, Value);
        case llvm::ELF::R_386_PC32:
          return visitELF_386_PC32(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::ppc:
        switch (RelocType) {
        case llvm::ELF::R_PPC_ADDR32:
          return visitELF_PPC_ADDR32(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::arm:
      case Triple::armeb:
        switch (RelocType) {
        default:
          HasError = true;
          return RelocToApply();
        case llvm::ELF::R_ARM_ABS32:
          return visitELF_ARM_ABS32(R, Value);
        }
      case Triple::mipsel:
      case Triple::mips:
        switch (RelocType) {
        case llvm::ELF::R_MIPS_32:
          return visitELF_MIPS_32(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      case Triple::sparc:
        switch (RelocType) {
        case llvm::ELF::R_SPARC_32:
        case llvm::ELF::R_SPARC_UA32:
          return visitELF_SPARC_32(R, Value);
        default:
          HasError = true;
          return RelocToApply();
        }
      default:
        HasError = true;
        return RelocToApply();
      }
    } else {
      report_fatal_error("Invalid word size in object file");
    }
  }

  RelocToApply visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) {
    switch (ObjToVisit.getArch()) {
    case Triple::x86:
      switch (RelocType) {
      case COFF::IMAGE_REL_I386_SECREL:
        return visitCOFF_I386_SECREL(R, Value);
      case COFF::IMAGE_REL_I386_DIR32:
        return visitCOFF_I386_DIR32(R, Value);
      }
      break;
    case Triple::x86_64:
      switch (RelocType) {
      case COFF::IMAGE_REL_AMD64_SECREL:
        return visitCOFF_AMD64_SECREL(R, Value);
      case COFF::IMAGE_REL_AMD64_ADDR64:
        return visitCOFF_AMD64_ADDR64(R, Value);
      }
      break;
    }
    HasError = true;
    return RelocToApply();
  }

  RelocToApply visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) {
    switch (ObjToVisit.getArch()) {
    default: break;
    case Triple::x86_64:
      switch (RelocType) {
        default: break;
        case MachO::X86_64_RELOC_UNSIGNED:
          return visitMACHO_X86_64_UNSIGNED(R, Value);
      }
    }
    HasError = true;
    return RelocToApply();
  }

  int64_t getELFAddend32LE(RelocationRef R) {
    const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
    DataRefImpl DRI = R.getRawDataRefImpl();
    int64_t Addend;
    Obj->getRelocationAddend(DRI, Addend);
    return Addend;
  }

  int64_t getELFAddend64LE(RelocationRef R) {
    const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
    DataRefImpl DRI = R.getRawDataRefImpl();
    int64_t Addend;
    Obj->getRelocationAddend(DRI, Addend);
    return Addend;
  }

  int64_t getELFAddend32BE(RelocationRef R) {
    const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
    DataRefImpl DRI = R.getRawDataRefImpl();
    int64_t Addend;
    Obj->getRelocationAddend(DRI, Addend);
    return Addend;
  }

  int64_t getELFAddend64BE(RelocationRef R) {
    const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
    DataRefImpl DRI = R.getRawDataRefImpl();
    int64_t Addend;
    Obj->getRelocationAddend(DRI, Addend);
    return Addend;
  }

  uint8_t getLengthMachO64(RelocationRef R) {
    const MachOObjectFile *Obj = cast<MachOObjectFile>(R.getObjectFile());
    return Obj->getRelocationLength(R.getRawDataRefImpl());
  }

  /// Operations

  /// 386-ELF
  RelocToApply visitELF_386_NONE(RelocationRef R) {
    return RelocToApply(0, 0);
  }

  // Ideally the Addend here will be the addend in the data for
  // the relocation. It's not actually the case for Rel relocations.
  RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend32LE(R);
    return RelocToApply(Value + Addend, 4);
  }

  RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend32LE(R);
    uint64_t Address;
    R.getOffset(Address);
    return RelocToApply(Value + Addend - Address, 4);
  }

  /// X86-64 ELF
  RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
    return RelocToApply(0, 0);
  }
  RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64LE(R);
    return RelocToApply(Value + Addend, 8);
  }
  RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64LE(R);
    uint64_t Address;
    R.getOffset(Address);
    return RelocToApply(Value + Addend - Address, 4);
  }
  RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64LE(R);
    uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
    return RelocToApply(Res, 4);
  }
  RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64LE(R);
    int32_t Res = (Value + Addend) & 0xFFFFFFFF;
    return RelocToApply(Res, 4);
  }

  /// PPC64 ELF
  RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
    return RelocToApply(Res, 4);
  }
  RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    return RelocToApply(Value + Addend, 8);
  }

  /// PPC32 ELF
  RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend32BE(R);
    uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
    return RelocToApply(Res, 4);
  }

  /// MIPS ELF
  RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
    return RelocToApply(Res, 4);
  }

  RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    uint64_t Res = (Value + Addend);
    return RelocToApply(Res, 8);
  }

  // AArch64 ELF
  RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    int64_t Res =  Value + Addend;

    // Overflow check allows for both signed and unsigned interpretation.
    if (Res < INT32_MIN || Res > UINT32_MAX)
      HasError = true;

    return RelocToApply(static_cast<uint32_t>(Res), 4);
  }

  RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    return RelocToApply(Value + Addend, 8);
  }

  // SystemZ ELF
  RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64BE(R);
    int64_t Res = Value + Addend;

    // Overflow check allows for both signed and unsigned interpretation.
    if (Res < INT32_MIN || Res > UINT32_MAX)
      HasError = true;

    return RelocToApply(static_cast<uint32_t>(Res), 4);
  }

  RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64BE(R);
    return RelocToApply(Value + Addend, 8);
  }

  RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
    int32_t Addend = getELFAddend32BE(R);
    return RelocToApply(Value + Addend, 4);
  }

  RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
    int32_t Addend = getELFAddend64BE(R);
    return RelocToApply(Value + Addend, 4);
  }

  RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
    int64_t Addend = getELFAddend64BE(R);
    return RelocToApply(Value + Addend, 8);
  }

  RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
    int64_t Addend;
    getELFRelocationAddend(R, Addend);
    int64_t Res = Value + Addend;

    // Overflow check allows for both signed and unsigned interpretation.
    if (Res < INT32_MIN || Res > UINT32_MAX)
      HasError = true;

    return RelocToApply(static_cast<uint32_t>(Res), 4);
  }

  /// I386 COFF
  RelocToApply visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) {
    return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
  }

  RelocToApply visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) {
    return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
  }

  /// AMD64 COFF
  RelocToApply visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) {
    return RelocToApply(static_cast<uint32_t>(Value), /*Width=*/4);
  }

  RelocToApply visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) {
    return RelocToApply(Value, /*Width=*/8);
  }

  // X86_64 MachO
  RelocToApply visitMACHO_X86_64_UNSIGNED(RelocationRef R, uint64_t Value) {
    uint8_t Length = getLengthMachO64(R);
    Length = 1<<Length;
    return RelocToApply(Value, Length);
  }
};

} // namespace object
} // namespace llvm
#endif