blob: 0185c7cbe018a7e0adb08d72dec23e765c751200 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//=== MachORelocation.h - Mach-O Relocation Info ----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the MachORelocation class.
10//
11//===----------------------------------------------------------------------===//
12
13
14#ifndef LLVM_CODEGEN_MACHORELOCATION_H
15#define LLVM_CODEGEN_MACHORELOCATION_H
16
17#include "llvm/Support/DataTypes.h"
18
19namespace llvm {
20
21 /// MachORelocation - This struct contains information about each relocation
22 /// that needs to be emitted to the file.
23 /// see <mach-o/reloc.h>
24 class MachORelocation {
25 uint32_t r_address; // offset in the section to what is being relocated
26 uint32_t r_symbolnum; // symbol index if r_extern == 1 else section index
27 bool r_pcrel; // was relocated pc-relative already
28 uint8_t r_length; // length = 2 ^ r_length
Andrew Scullcdfcccc2018-10-05 20:58:37 +010029 bool r_extern; //
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030 uint8_t r_type; // if not 0, machine-specific relocation type.
31 bool r_scattered; // 1 = scattered, 0 = non-scattered
32 int32_t r_value; // the value the item to be relocated is referring
33 // to.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010034 public:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035 uint32_t getPackedFields() const {
36 if (r_scattered)
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037 return (1 << 31) | (r_pcrel << 30) | ((r_length & 3) << 28) |
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 ((r_type & 15) << 24) | (r_address & 0x00FFFFFF);
39 else
40 return (r_symbolnum << 8) | (r_pcrel << 7) | ((r_length & 3) << 5) |
41 (r_extern << 4) | (r_type & 15);
42 }
43 uint32_t getAddress() const { return r_scattered ? r_value : r_address; }
44 uint32_t getRawAddress() const { return r_address; }
45
46 MachORelocation(uint32_t addr, uint32_t index, bool pcrel, uint8_t len,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 bool ext, uint8_t type, bool scattered = false,
48 int32_t value = 0) :
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049 r_address(addr), r_symbolnum(index), r_pcrel(pcrel), r_length(len),
50 r_extern(ext), r_type(type), r_scattered(scattered), r_value(value) {}
51 };
52
53} // end llvm namespace
54
55#endif // LLVM_CODEGEN_MACHORELOCATION_H