|
| 1 | +// Copyright (c) Alexandre Mutel. All rights reserved. |
| 2 | +// This file is licensed under the BSD-Clause 2 license. |
| 3 | +// See the license.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace LibObjectFile.MachO; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// A relocation entry, telling the linker how to fix up one place in a section. |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// Two forms share the same eight bytes. The ordinary form names a symbol or a section by number. |
| 14 | +/// The scattered form carries the target address outright, for when the fixed-up value does not |
| 15 | +/// land inside the thing it refers to and a section number would be ambiguous; it has no room |
| 16 | +/// left for an external flag, so it is always local. |
| 17 | +/// </remarks> |
| 18 | +public sealed class MachORelocation |
| 19 | +{ |
| 20 | + /// <summary>Bit of the first word marking an entry as scattered (<c>R_SCATTERED</c>).</summary> |
| 21 | + public const uint ScatteredMask = 0x80000000; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets the offset of the fixed-up field from the start of its section. |
| 25 | + /// </summary> |
| 26 | + public int Address { get; set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Gets or sets the symbol index when <see cref="IsExternal"/> is true, and the one-based |
| 30 | + /// section number otherwise. Always zero for a scattered entry. |
| 31 | + /// </summary> |
| 32 | + public uint SymbolOrSectionNumber { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the address of the target, for a scattered entry only. |
| 36 | + /// </summary> |
| 37 | + public int Value { get; set; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets or sets whether the fixed-up field holds a displacement from the program counter |
| 41 | + /// rather than an address. |
| 42 | + /// </summary> |
| 43 | + public bool IsPcRelative { get; set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets the width of the fixed-up field as a power of two, so 2 means four bytes. |
| 47 | + /// </summary> |
| 48 | + public byte LengthLog2 { get; set; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets or sets whether <see cref="SymbolOrSectionNumber"/> is a symbol index. Never true |
| 52 | + /// for a scattered entry, which has no room for the flag. |
| 53 | + /// </summary> |
| 54 | + public bool IsExternal { get; set; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets or sets whether this entry carries its target address rather than a symbol or |
| 58 | + /// section number. |
| 59 | + /// </summary> |
| 60 | + public bool IsScattered { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets or sets the type, whose meaning depends on the image's architecture. Interpret it as |
| 64 | + /// <see cref="MachOX86_64RelocationType"/>, <see cref="MachOArm64RelocationType"/> or |
| 65 | + /// <see cref="MachOGenericRelocationType"/> accordingly. |
| 66 | + /// </summary> |
| 67 | + public byte RawType { get; set; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the width of the fixed-up field in bytes. |
| 71 | + /// </summary> |
| 72 | + public int LengthInBytes => 1 << LengthLog2; |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Decodes an entry from the two words it occupies. |
| 76 | + /// </summary> |
| 77 | + /// <param name="word0">The first word, which is the address unless the top bit marks it scattered.</param> |
| 78 | + /// <param name="word1">The second word.</param> |
| 79 | + /// <returns>The decoded entry.</returns> |
| 80 | + public static MachORelocation Decode(uint word0, uint word1) |
| 81 | + { |
| 82 | + if ((word0 & ScatteredMask) != 0) |
| 83 | + { |
| 84 | + return new MachORelocation |
| 85 | + { |
| 86 | + IsScattered = true, |
| 87 | + Address = (int)(word0 & 0x00ffffff), |
| 88 | + RawType = (byte)((word0 >> 24) & 0xf), |
| 89 | + LengthLog2 = (byte)((word0 >> 28) & 0x3), |
| 90 | + IsPcRelative = ((word0 >> 30) & 1) != 0, |
| 91 | + Value = (int)word1, |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + return new MachORelocation |
| 96 | + { |
| 97 | + Address = (int)word0, |
| 98 | + SymbolOrSectionNumber = word1 & 0x00ffffff, |
| 99 | + IsPcRelative = ((word1 >> 24) & 1) != 0, |
| 100 | + LengthLog2 = (byte)((word1 >> 25) & 0x3), |
| 101 | + IsExternal = ((word1 >> 27) & 1) != 0, |
| 102 | + RawType = (byte)((word1 >> 28) & 0xf), |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Encodes this entry back into the two words it occupies. |
| 108 | + /// </summary> |
| 109 | + /// <returns>The first and second words.</returns> |
| 110 | + public (uint Word0, uint Word1) Encode() |
| 111 | + { |
| 112 | + if (IsScattered) |
| 113 | + { |
| 114 | + var packed = ScatteredMask |
| 115 | + | ((uint)Address & 0x00ffffff) |
| 116 | + | ((uint)(RawType & 0xf) << 24) |
| 117 | + | ((uint)(LengthLog2 & 0x3) << 28) |
| 118 | + | (IsPcRelative ? 1u << 30 : 0u); |
| 119 | + return (packed, (uint)Value); |
| 120 | + } |
| 121 | + |
| 122 | + var info = (SymbolOrSectionNumber & 0x00ffffff) |
| 123 | + | (IsPcRelative ? 1u << 24 : 0u) |
| 124 | + | ((uint)(LengthLog2 & 0x3) << 25) |
| 125 | + | (IsExternal ? 1u << 27 : 0u) |
| 126 | + | ((uint)(RawType & 0xf) << 28); |
| 127 | + return ((uint)Address, info); |
| 128 | + } |
| 129 | + |
| 130 | + /// <inheritdoc /> |
| 131 | + public override string ToString() |
| 132 | + => $"{nameof(MachORelocation)} {{ Address = 0x{Address:X}, Type = {RawType}, {LengthInBytes} bytes{(IsPcRelative ? ", pcrel" : string.Empty)}{(IsExternal ? ", external" : string.Empty)}{(IsScattered ? ", scattered" : string.Empty)} }}"; |
| 133 | +} |
0 commit comments