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
//! The COFF `x86_64` relocation table: each `IMAGE_REL_AMD64_*` type mapped to
//! a [`Spec`].
//!
//! COFF `x86_64` shares the instruction encoding with ELF and Mach-O but uses
//! its own relocation numbering. The standard address and PC-relative types
//! map directly onto the portable [`RelExpr`] arithmetic, so the scan and
//! apply drivers are shared with the other formats. The type fixes the write
//! width; COFF relocations are REL-encoded (the addend lives in the target
//! bytes), so the addend is extracted at the driver-layer normalisation step,
//! not by the spec.
//!
//! # Addend discipline for REL32 / `REL32_1..5`
//!
//! A COFF PC-relative fixup stores the displacement from the end of the
//! instruction, so the implicit addend is `-4` for `REL32`. The `_1` through
//! `_5` variants carry an extra `-1`..`-5` byte correction (for the trailing
//! opcode bytes after the 4-byte displacement, as in `MOV`/`CALL` encodings
//! with extra immediates). The total addend (`-4`, `-5`, ..., `-9`) is applied
//! when the COFF reloc is normalised into a [`crate::reloc::Reloc`] record at
//! the driver layer; the declarative table only fixes the expression (`S + A -
//! P`) and the write kind (`W32S`), exactly as the Mach-O table handles
//! `SIGNED`/`SIGNED_1/2/4`. An undefined symbol referenced through an
//! `__imp_` alias resolves to the IAT slot; for a statically resolved symbol
//! the value is `S + A - P`.
//!
//! # Section-relative and RVA types
//!
//! `ADDR32NB` (an RVA, `S + A - ImageBase`), `SECTION` (section index),
//! `SECREL` (offset within the section) and `SECREL7` (the low 7 bits of that
//! offset) need information the portable [`Resolver`] does not supply (the
//! image base or a section base). They are routed through [`RelExpr::Escape`]:
//! the COFF writer, which owns the image base and the laid-out section bases,
//! resolves them. The same seam the `AArch64` split-immediate and the Mach-O
//! `SUBTRACTOR` relocations use.
//!
//! Note: the Microsoft PE/COFF spec defines no `GOTPCREL32` or `GOTREL32` for
//! AMD64 (those are ELF constructs; `R_X86_64_GOTPCREL`). Windows resolves
//! imported symbols through the IAT and `__imp_` aliases instead of a GOT, so
//! the standard `REL32`/`ADDR32` types cover those references.
use bytes_of as of;
use crate::;
// --- raw relocation type numbers (Microsoft PE/COFF, AMD64) ----------------
/// The relocation is ignored (padding / sentinel).
pub const IMAGE_REL_AMD64_ABSOLUTE: u32 = 0x0000;
/// 64-bit absolute address: `S + A`.
pub const IMAGE_REL_AMD64_ADDR64: u32 = 0x0001;
/// 32-bit absolute address: `S + A`.
pub const IMAGE_REL_AMD64_ADDR32: u32 = 0x0002;
/// 32-bit RVA (address relative to the image base): `S + A - ImageBase`.
pub const IMAGE_REL_AMD64_ADDR32NB: u32 = 0x0003;
/// 32-bit PC-relative: `S + A - P` (addend `-4`).
pub const IMAGE_REL_AMD64_REL32: u32 = 0x0004;
/// 32-bit PC-relative, extra `-1` byte adjustment (addend `-5`).
pub const IMAGE_REL_AMD64_REL32_1: u32 = 0x0005;
/// 32-bit PC-relative, extra `-2` byte adjustment (addend `-6`).
pub const IMAGE_REL_AMD64_REL32_2: u32 = 0x0006;
/// 32-bit PC-relative, extra `-3` byte adjustment (addend `-7`).
pub const IMAGE_REL_AMD64_REL32_3: u32 = 0x0007;
/// 32-bit PC-relative, extra `-4` byte adjustment (addend `-8`).
pub const IMAGE_REL_AMD64_REL32_4: u32 = 0x0008;
/// 32-bit PC-relative, extra `-5` byte adjustment (addend `-9`).
pub const IMAGE_REL_AMD64_REL32_5: u32 = 0x0009;
/// 16-bit section index of the symbol. Section-relative (Escape).
pub const IMAGE_REL_AMD64_SECTION: u32 = 0x000A;
/// 32-bit offset of the symbol within its section. Section-relative (Escape).
pub const IMAGE_REL_AMD64_SECREL: u32 = 0x000B;
/// 7-bit offset of the symbol within its section. Section-relative (Escape).
pub const IMAGE_REL_AMD64_SECREL7: u32 = 0x000C;
/// CLR metadata token (Escape).
pub const IMAGE_REL_AMD64_TOKEN: u32 = 0x000D;
/// 32-bit signed span-dependent value, paired with PAIR (Escape).
pub const IMAGE_REL_AMD64_SREL32: u32 = 0x000E;
/// High half of an SREL32 pair (Escape).
pub const IMAGE_REL_AMD64_PAIR: u32 = 0x000F;
/// 32-bit signed span-dependent value (Escape).
pub const IMAGE_REL_AMD64_SSPAN32: u32 = 0x0010;
/// The COFF `x86_64` architecture, for use as the `A` type parameter of the
/// drivers.
;