pe_parser/
relocation.rs

1use num_derive::FromPrimitive;
2
3/// Relocation type indicators for x64 and compatible processors.
4#[derive(FromPrimitive, Debug, PartialEq)]
5#[repr(u16)]
6pub enum X86RelocationType {
7    /// The relocation is ignored.
8    Absolute = 0x0000,
9    /// The 64-bit VA of the relocation target.
10    Addr64 = 0x0001,
11    /// The 32-bit VA of the relocation target.
12    Addr32 = 0x0002,
13    /// The 32-bit address without an image base (RVA).
14    Addr32Nb = 0x0003,
15    /// The 32-bit relative address from the byte following the relocation.
16    Rel32 = 0x0004,
17    /// The 32-bit address relative to byte distance 1 from the relocation.
18    Rel321 = 0x0005,
19    /// The 32-bit address relative to byte distance 2 from the relocation.
20    Rel322 = 0x0006,
21    /// The 32-bit address relative to byte distance 3 from the relocation.
22    Rel323 = 0x0007,
23    /// The 32-bit address relative to byte distance 4 from the relocation.
24    Rel324 = 0x0008,
25    /// The 32-bit address relative to byte distance 5 from the relocation.
26    Rel325 = 0x0009,
27    /// The 16-bit section index of the section that contains the target.
28    /// This is used to support debugging information.
29    Section = 0x000A,
30    /// The 32-bit offset of the target from the beginning of its section.
31    /// This is used to support debugging information and static thread local storage.
32    SecRel = 0x000B,
33    /// A 7-bit unsigned offset from the base of the section that contains the target.
34    SecRel7 = 0x000C,
35    /// CLR tokens.
36    Token = 0x000D,
37    /// A 32-bit signed span-dependent value emitted into the object.
38    SRel32 = 0x000E,
39    /// A pair that must immediately follow every span-dependent value.
40    Pair = 0x000F,
41    /// A 32-bit signed span-dependent value that is applied at link time.
42    SSpan32 = 0x0010,
43}
44
45/// Relocation type indicators for ARM processors.
46#[derive(FromPrimitive, Debug, PartialEq)]
47#[repr(u16)]
48pub enum ARMRelocationType {
49    /// The relocation is ignored.
50    Absolute = 0x0000,
51    /// The 32-bit VA of the target.
52    Addr32 = 0x0001,
53    /// The 32-bit RVA of the target.
54    Addr32Nb = 0x0002,
55    /// The 24-bit relative displacement to the target.
56    Branch24 = 0x0003,
57    /// The reference to a subroutine call.
58    /// The reference consists of two 16-bit instructions with 11-bit offsets.
59    Branch11 = 0x0004,
60    /// The 32-bit relative address from the byte following the relocation.
61    Rel32 = 0x000A,
62    /// The 16-bit section index of the section that contains the target.
63    /// This is used to support debugging information.
64    Section = 0x000E,
65    /// The 32-bit offset of the target from the beginning of its section.
66    /// This is used to support debugging information and static thread local storage.
67    SecRel = 0x000F,
68    /// The 32-bit VA of the target.
69    /// This relocation is applied using a MOVW instruction for the low 16 bits followed by a MOVT for the high 16 bits.
70    Mov32 = 0x0010,
71    /// The 32-bit VA of the target.
72    /// This relocation is applied using a MOVW instruction for the low 16 bits followed by a MOVT for the high 16 bits.
73    ImageRelThumbMOV32 = 0x0011,
74    /// The instruction is fixed up with the 21-bit relative displacement to the 2-byte aligned target.
75    /// The least significant bit of the displacement is always zero and is not stored.
76    /// This relocation corresponds to a Thumb-2 32-bit conditional B instruction.
77    ImageRelThumbBranch20 = 0x0012,
78    /// Unused relocation type.
79    Unused = 0x0013,
80    /// The instruction is fixed up with the 25-bit relative displacement to the 2-byte aligned target.
81    /// The least significant bit of the displacement is zero and is not stored.
82    /// This relocation corresponds to a Thumb-2 B instruction.
83    ImageRelThumbBranch24 = 0x0014,
84    /// The instruction is fixed up with the 25-bit relative displacement to the 4-byte aligned target.
85    /// The low 2 bits of the displacement are zero and are not stored.
86    /// This relocation corresponds to a Thumb-2 BLX instruction.
87    ImageRelThumbBLX23 = 0x0015,
88    /// The relocation is valid only when it immediately follows a ARM_REFHI or THUMB_REFHI.
89    /// Its SymbolTableIndex contains a displacement and not an index into the symbol table.
90    Pair = 0x0016,
91}
92
93/// Relocation type indicators for ARM64 processors.
94#[derive(FromPrimitive, Debug, PartialEq)]
95#[repr(u16)]
96pub enum ARM64RelocationType {
97    /// The relocation is ignored.
98    Absolute = 0x0000,
99    /// The 32-bit VA of the target.
100    Addr32 = 0x0001,
101    /// The 32-bit RVA of the target.
102    Addr32Nb = 0x0002,
103    /// The 26-bit relative displacement to the target, for B and BL instructions.
104    Branch26 = 0x0003,
105    /// The page base of the target, for ADRP instruction.
106    PageBaseRel21 = 0x0004,
107    /// The 12-bit relative displacement to the target, for instruction ADR.
108    Rel21 = 0x0005,
109    /// The 12-bit page offset of the target, for instructions ADD/ADDS (immediate) with zero shift.
110    PageOffset12A = 0x0006,
111    /// The 12-bit page offset of the target, for instruction LDR (indexed, unsigned immediate).
112    PageOffset12L = 0x0007,
113    /// The 32-bit offset of the target from the beginning of its section.
114    /// This is used to support debugging information and static thread local storage.
115    SecRel = 0x0008,
116    /// Bit 0:11 of section offset of the target, for instructions ADD/ADDS (immediate) with zero shift.
117    SecRelLow12A = 0x0009,
118    /// Bit 12:23 of section offset of the target, for instructions ADD/ADDS (immediate) with zero shift.
119    SecRelHigh12A = 0x000A,
120    /// Bit 0:11 of section offset of the target, for instruction LDR (indexed, unsigned immediate).
121    SecRelLow12L = 0x000B,
122    /// CLR token.
123    Token = 0x000C,
124    /// The 16-bit section index of the section that contains the target.
125    /// This is used to support debugging information.
126    Section = 0x000D,
127    /// The 64-bit VA of the relocation target.
128    Addr64 = 0x000E,
129    /// The 19-bit offset to the relocation target, for conditional B instruction.
130    Branch19 = 0x000F,
131    /// The 14-bit offset to the relocation target, for instructions TBZ and TBNZ.
132    Branch14 = 0x0010,
133    /// The 32-bit relative address from the byte following the relocation.
134    Rel32 = 0x0011,
135}
136
137/// Relocation type indicators for SH3 and SH4 processors.
138/// SH5-specific relocations are noted as SHM (SH Media).
139#[derive(FromPrimitive, Debug, PartialEq)]
140#[repr(u16)]
141pub enum SuperHRelocationType {
142    /// The relocation is ignored.
143    Absolute = 0x0000,
144    /// A reference to the 16-bit location that contains the VA of the target symbol.
145    Direct16 = 0x0001,
146    /// The 32-bit VA of the target symbol.
147    Direct32 = 0x0002,
148    /// A reference to the 8-bit location that contains the VA of the target symbol.
149    Direct8 = 0x0003,
150    /// A reference to the 8-bit instruction that contains the effective 16-bit VA of the target symbol.
151    Direct8Word = 0x0004,
152    /// A reference to the 8-bit instruction that contains the effective 32-bit VA of the target symbol.
153    Direct8Long = 0x0005,
154    /// A reference to the 8-bit location whose low 4 bits contain the VA of the target symbol.
155    Direct4 = 0x0006,
156    /// A reference to the 8-bit instruction whose low 4 bits contain the effective 16-bit VA of the target symbol.
157    Direct4Word = 0x0007,
158    /// A reference to the 8-bit instruction whose low 4 bits contain the effective 32-bit VA of the target symbol.
159    Direct4Long = 0x0008,
160    /// A reference to the 8-bit instruction that contains the effective 16-bit relative offset of the target symbol.
161    PCRel8Word = 0x0009,
162    /// A reference to the 8-bit instruction that contains the effective 32-bit relative offset of the target symbol.
163    PCRel8Long = 0x000A,
164    /// A reference to the 16-bit instruction whose low 12 bits contain the effective 16-bit relative offset of the target symbol.
165    PCRel12Word = 0x000B,
166    /// A reference to a 32-bit location that is the VA of the section that contains the target symbol.
167    StartOfSection = 0x000C,
168    /// A reference to the 32-bit location that is the size of the section that contains the target symbol.
169    SizeOfSection = 0x000D,
170    /// The 16-bit section index of the section that contains the target. This is used to support debugging information.
171    Section = 0x000E,
172    /// The 32-bit offset of the target from the beginning of its section.
173    /// This is used to support debugging information and static thread local storage.
174    SecRel = 0x000F,
175    /// The 32-bit RVA of the target symbol.
176    Direct32Nb = 0x0010,
177    /// GP relative.
178    GPRel4Long = 0x0011,
179    /// CLR token.
180    Token = 0x0012,
181    /// The offset from the current instruction in longwords.
182    /// If the NOMODE bit is not set, insert the inverse of the low bit at bit 32 to select PTA or PTB.
183    SHMPCRelPT = 0x0013,
184    /// The low 16 bits of the 32-bit address.
185    SHMRefLo = 0x0014,
186    /// The high 16 bits of the 32-bit address.
187    SHMRefHalf = 0x0015,
188    /// The low 16 bits of the relative address.
189    SHMRelLo = 0x0016,
190    /// The high 16 bits of the relative address.
191    SHMRelHalf = 0x0017,
192    /// The relocation is valid only when it immediately follows a REFHALF, RELHALF, or RELLO relocation.
193    /// The SymbolTableIndex field of the relocation contains a displacement and not an index into the symbol table.
194    SHMPair = 0x0018,
195    /// The relocation ignores section mode.
196    SHMNoMode = 0x8000,
197}
198
199/// Relocation type indicators for PowerPC processors.
200#[derive(FromPrimitive, Debug, PartialEq)]
201#[repr(u16)]
202pub enum PowerPCRelocationType {
203    /// The relocation is ignored.
204    Absolute = 0x0000,
205    /// The 64-bit VA of the target.
206    Addr64 = 0x0001,
207    /// The 32-bit VA of the target.
208    Addr32 = 0x0002,
209    /// The low 24 bits of the VA of the target.
210    /// This is valid only when the target symbol is absolute and can be sign-extended to its original value.
211    Addr24 = 0x0003,
212    /// The low 16 bits of the target's VA.
213    Addr16 = 0x0004,
214    /// The low 14 bits of the target's VA.
215    /// This is valid only when the target symbol is absolute and can be sign-extended to its original value.
216    Addr14 = 0x0005,
217    /// A 24-bit PC-relative offset to the symbol's location.
218    Rel24 = 0x0006,
219    /// A 14-bit PC-relative offset to the symbol's location.
220    Rel14 = 0x0007,
221    /// The 32-bit RVA of the target.
222    Addr32Nb = 0x000A,
223    /// The 32-bit offset of the target from the beginning of its section.
224    /// This is used to support debugging information and static thread local storage.
225    SecRel = 0x000B,
226    /// The 16-bit section index of the section that contains the target.
227    /// This is used to support debugging information.
228    Section = 0x000C,
229    /// The 16-bit offset of the target from the beginning of its section.
230    /// This is used to support debugging information and static thread local storage.
231    SecRel16 = 0x000F,
232    /// The high 16 bits of the target's 32-bit VA.
233    /// This is used for the first instruction in a two-instruction sequence that loads a full address.
234    /// This relocation must be immediately followed by a PAIR relocation whose SymbolTableIndex contains a signed
235    /// 16-bit displacement that is added to the upper 16 bits that was taken from the location that is being relocated.
236    RefHi = 0x0010,
237    /// The low 16 bits of the target's VA.
238    RefLo = 0x0011,
239    /// A relocation that is valid only when it immediately follows a REFHI or SECRELHI relocation.
240    /// Its SymbolTableIndex contains a displacement and not an index into the symbol table.
241    Pair = 0x0012,
242    /// The low 16 bits of the 32-bit offset of the target from the beginning of its section.
243    SecRelLo = 0x0013,
244    /// The 16-bit signed displacement of the target relative to the GP register.
245    GPRel = 0x0015,
246    /// The CLR token.
247    Token = 0x0016,
248}
249
250/// Relocation type indicators for Intel 386 processors.
251#[derive(FromPrimitive, Debug, PartialEq)]
252#[repr(u16)]
253pub enum I386RelocationType {
254    /// The relocation is ignored.
255    Absolute = 0x0000,
256    /// Not supported.
257    Dir16 = 0x0001,
258    /// Not supported.
259    Rel16 = 0x0002,
260    /// The target's 32-bit VA.
261    Dir32 = 0x0006,
262    /// The target's 32-bit RVA.
263    Dir32Nb = 0x0007,
264    /// Not supported.
265    Seg12 = 0x0009,
266    /// The 16-bit section index of the section that contains the target.
267    /// This is used to support debugging information.
268    Section = 0x000A,
269    /// The 32-bit offset of the target from the beginning of its section.
270    /// This is used to support debugging information and static thread local storage.
271    SecRel = 0x000B,
272    /// The CLR token.
273    Token = 0x000C,
274    /// A 7-bit offset from the base of the section that contains the target.
275    SecRel7 = 0x000D,
276    /// The 32-bit relative displacement to the target.
277    /// This supports the x86 relative branch and call instructions.
278    Rel32 = 0x0014,
279}
280
281/// Relocation type indicators for the Intel Itanium processor family and compatible processors.
282/// Note that relocations on instructions use the bundle's offset and slot number for the relocation offset.
283#[derive(FromPrimitive, Debug, PartialEq)]
284#[repr(u16)]
285pub enum IA64RelocationType {
286    /// The relocation is ignored.
287    Absolute = 0x0000,
288    /// The instruction relocation can be followed by an ADDEND relocation whose value is added
289    /// to the target address before it is inserted into the specified slot in the IMM14 bundle.
290    /// The relocation target must be absolute or the image must be fixed.
291    IMM14 = 0x0001,
292    /// The instruction relocation can be followed by an ADDEND relocation whose value is added
293    /// to the target address before it is inserted into the specified slot in the IMM22 bundle.
294    /// The relocation target must be absolute or the image must be fixed.
295    IMM32 = 0x0002,
296    /// The slot number of this relocation must be one (1).
297    /// The relocation can be followed by an ADDEND relocation whose value is added
298    /// to the target address before it is stored in all three slots of the IMM64 bundle.
299    IMM64 = 0x0003,
300    /// The target's 32-bit VA.
301    /// This is supported only for /LARGEADDRESSAWARE:NO images.
302    Dir32 = 0x0004,
303    /// The target's 64-bit VA.
304    Dir64 = 0x0005,
305    /// The instruction is fixed up with the 25-bit relative displacement to the 16-bit aligned target.
306    /// The low 4 bits of the displacement are zero and are not stored.
307    PCRel21B = 0x0006,
308    /// The instruction is fixed up with the 25-bit relative displacement to the 16-bit aligned target.
309    /// The low 4 bits of the displacement, which are zero, are not stored.
310    PCRel21M = 0x0007,
311    /// The LSBs of this relocation's offset must contain the slot number whereas the rest is the bundle address. The bundle is fixed up with the 25-bit relative displacement to the 16-bit aligned target.
312    /// The low 4 bits of the displacement are zero and are not stored.
313    PCRel21F = 0x0008,
314    /// The instruction relocation can be followed by an ADDEND relocation whose value is added
315    /// to the target address and then a 22-bit GP-relative offset that is calculated and applied to the GPREL22 bundle.
316    GPRel22 = 0x0009,
317    /// The instruction is fixed up with the 22-bit GP-relative offset to the target symbol's literal table entry.
318    /// The linker creates this literal table entry based on this relocation and the ADDEND relocation that might follow.
319    LTOff22 = 0x000A,
320    /// The 16-bit section index of the section contains the target.
321    /// This is used to support debugging information.
322    Section = 0x000B,
323    /// The instruction is fixed up with the 22-bit offset of the target from the beginning of its section.
324    /// This relocation can be followed immediately by an ADDEND relocation, whose Value field contains the 32-bit unsigned offset of the target from the beginning of the section.
325    SecRel22 = 0x000C,
326    /// The slot number for this relocation must be one (1). The instruction is fixed up with the 64-bit offset of the target from the beginning of its section.
327    /// This relocation can be followed immediately by an ADDEND relocation whose Value field contains the 32-bit unsigned offset of the target from the beginning of the section.
328    SecRel64I = 0x000D,
329    /// The address of data to be fixed up with the 32-bit offset of the target from the beginning of its section.
330    SecRel32 = 0x000E,
331    /// The target's 32-bit RVA.
332    Dir32Nb = 0x0010,
333    /// This is applied to a signed 14-bit immediate that contains the difference between two relocatable targets.
334    /// This is a declarative field for the linker that indicates that the compiler has already emitted this value.
335    SRel14 = 0x0011,
336    /// This is applied to a signed 22-bit immediate that contains the difference between two relocatable targets.
337    /// This is a declarative field for the linker that indicates that the compiler has already emitted this value.
338    SRel22 = 0x0012,
339    /// This is applied to a signed 32-bit immediate that contains the difference between two relocatable values.
340    /// This is a declarative field for the linker that indicates that the compiler has already emitted this value.
341    SRel32 = 0x0013,
342    /// This is applied to an unsigned 32-bit immediate that contains the difference between two relocatable values.
343    /// This is a declarative field for the linker that indicates that the compiler has already emitted this value.
344    URel32 = 0x0014,
345    /// A 60-bit PC-relative fixup that always stays as a BRL instruction of an MLX bundle.
346    PCRel60X = 0x0015,
347    /// A 60-bit PC-relative fixup.
348    /// If the target displacement fits in a signed 25-bit field, convert the entire bundle to an MBB bundle with NOP.B in slot 1 and a 25-bit BR instruction (with the 4 lowest bits all zero and dropped) in slot 2.
349    PCRel60B = 0x0016,
350    /// A 60-bit PC-relative fixup.
351    /// If the target displacement fits in a signed 25-bit field, convert the entire bundle to an MFB bundle with NOP.F in slot 1 and a 25-bit (4 lowest bits all zero and dropped) BR instruction in slot 2.
352    PCRel60F = 0x0017,
353    /// A 60-bit PC-relative fixup.
354    /// If the target displacement fits in a signed 25-bit field, convert the entire bundle to an MIB bundle with NOP.I in slot 1 and a 25-bit (4 lowest bits all zero and dropped) BR instruction in slot 2.
355    PCRel60I = 0x0018,
356    /// A 60-bit PC-relative fixup.
357    /// If the target displacement fits in a signed 25-bit field, convert the entire bundle to an MMB bundle with NOP.M in slot 1 and a 25-bit (4 lowest bits all zero and dropped) BR instruction in slot 2.
358    PCRel60M = 0x0019,
359    /// A 64-bit GP-relative fixup.
360    IMMGPRel64 = 0x001A,
361    /// A CLR token.
362    Token = 0x001B,
363    /// A 32-bit GP-relative fixup.
364    GPRel32 = 0x001C,
365    /// The relocation is valid only when it immediately follows one of the following relocations: IMM14, IMM22, IMM64, GPREL22, LTOFF22, LTOFF64, SECREL22, SECREL64I, or SECREL32.
366    /// Its value contains the addend to apply to instructions within a bundle, not for data.
367    AddEnd = 0x001F,
368}
369
370/// Relocation type indicators for MIPS processors.
371#[derive(FromPrimitive, Debug, PartialEq)]
372#[repr(u16)]
373pub enum MIPSRelocationType {
374    /// The relocation is ignored.
375    Absolute = 0x0000,
376    /// The high 16 bits of the target's 32-bit VA.
377    RefHalf = 0x0001,
378    /// The target's 32-bit VA.
379    RefWord = 0x0002,
380    /// The low 26 bits of the target's VA.
381    /// This supports the MIPS J and JAL instructions.
382    JMPAddr = 0x0003,
383    /// The high 16 bits of the target's 32-bit VA.
384    /// This is used for the first instruction in a two-instruction sequence that loads a full address.
385    /// This relocation must be immediately followed by a PAIR relocation whose SymbolTableIndex contains a signed 16-bit displacement that is added to the upper 16 bits that are taken from the location that is being relocated.
386    RefHi = 0x0004,
387    /// The low 16 bits of the target's VA.
388    RefLo = 0x0005,
389    /// A 16-bit signed displacement of the target relative to the GP register.
390    GPRel = 0x0006,
391    /// The same as `MIPSRelocationType::GPRel`.
392    Literal = 0x0007,
393    /// The 16-bit section index of the section contains the target.
394    /// This is used to support debugging information.
395    Section = 0x000A,
396    /// The 32-bit offset of the target from the beginning of its section.
397    /// This is used to support debugging information and static thread local storage.
398    SecRel = 0x000B,
399    /// The low 16 bits of the 32-bit offset of the target from the beginning of its section.
400    SecRelLo = 0x000C,
401    /// The high 16 bits of the 32-bit offset of the target from the beginning of its section.
402    /// A PAIR relocation must immediately follow this one.
403    /// The SymbolTableIndex of the PAIR relocation contains a signed 16-bit displacement that is added to the upper 16 bits that are taken from the location that is being relocated.
404    SecRelHi = 0x000D,
405    /// The low 26 bits of the target's VA.
406    /// This supports the MIPS16 JAL instruction.
407    JMPAddr16 = 0x0010,
408    /// The target's 32-bit RVA.
409    RefWordNb = 0x0022,
410    /// The relocation is valid only when it immediately follows a REFHI or SECRELHI relocation.
411    /// Its SymbolTableIndex contains a displacement and not an index into the symbol table.
412    Pair = 0x0025,
413}
414
415/// Relocation type indicators for Mitsubishi M32R processors.
416#[derive(FromPrimitive, Debug, PartialEq)]
417#[repr(u16)]
418pub enum M32RRelocationType {
419    /// The relocation is ignored.
420    Absolute = 0x0000,
421    /// The target's 32-bit VA.
422    Addr32 = 0x0001,
423    /// The target's 32-bit RVA.
424    Addr32Nb = 0x0002,
425    /// The target's 24-bit VA.
426    Addr24 = 0x0003,
427    /// The target's 16-bit offset from the GP register.
428    GPRel16 = 0x0004,
429    /// The target's 24-bit offset from the program counter (PC), shifted left by 2 bits and sign-extended.
430    PCRel24 = 0x0005,
431    /// The target's 16-bit offset from the PC, shifted left by 2 bits and sign-extended.
432    PCRel16 = 0x0006,
433    /// The target's 8-bit offset from the PC, shifted left by 2 bits and sign-extended.
434    PCRel8 = 0x0007,
435    /// The 16 MSBs of the target VA.
436    RefHalf = 0x0008,
437    /// The 16 MSBs of the target VA, adjusted for LSB sign extension.
438    /// This is used for the first instruction in a two-instruction sequence that loads a full 32-bit address.
439    /// This relocation must be immediately followed by a PAIR relocation whose SymbolTableIndex contains a signed 16-bit displacement that is added to the upper 16 bits that are taken from the location that is being relocated.
440    RefHi = 0x0009,
441    /// The 16 LSBs of the target VA.
442    RefLo = 0x000A,
443    /// The relocation must follow the REFHI relocation.
444    /// Its SymbolTableIndex contains a displacement and not an index into the symbol table.
445    Pair = 0x000B,
446    /// The 16-bit section index of the section that contains the target.
447    /// This is used to support debugging information.
448    Section = 0x000C,
449    /// The 32-bit offset of the target from the beginning of its section.
450    /// This is used to support debugging information and static thread local storage.
451    SecRel = 0x000D,
452    /// The CLR token.
453    Token = 0x000E,
454}