| 1 | //===- MachOObjectFile.cpp - Mach-O object file binding -------------------===// |
| 2 | // |
| 3 | // 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the MachOObjectFile class, which binds the MachOObject |
| 10 | // class to the generic ObjectFile wrapper. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/ADT/bit.h" |
| 21 | #include "llvm/BinaryFormat/MachO.h" |
| 22 | #include "llvm/BinaryFormat/Swift.h" |
| 23 | #include "llvm/Object/Error.h" |
| 24 | #include "llvm/Object/MachO.h" |
| 25 | #include "llvm/Object/ObjectFile.h" |
| 26 | #include "llvm/Object/SymbolicFile.h" |
| 27 | #include "llvm/Support/DataExtractor.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/Errc.h" |
| 30 | #include "llvm/Support/Error.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/FileSystem.h" |
| 33 | #include "llvm/Support/Format.h" |
| 34 | #include "llvm/Support/LEB128.h" |
| 35 | #include "llvm/Support/MemoryBufferRef.h" |
| 36 | #include "llvm/Support/Path.h" |
| 37 | #include "llvm/Support/SwapByteOrder.h" |
| 38 | #include "llvm/Support/raw_ostream.h" |
| 39 | #include "llvm/TargetParser/Host.h" |
| 40 | #include "llvm/TargetParser/Triple.h" |
| 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <cstddef> |
| 44 | #include <cstdint> |
| 45 | #include <cstring> |
| 46 | #include <limits> |
| 47 | #include <list> |
| 48 | #include <memory> |
| 49 | #include <system_error> |
| 50 | |
| 51 | using namespace llvm; |
| 52 | using namespace object; |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | struct section_base { |
| 57 | char sectname[16]; |
| 58 | char segname[16]; |
| 59 | }; |
| 60 | |
| 61 | } // end anonymous namespace |
| 62 | |
| 63 | static Error malformedError(const Twine &Msg) { |
| 64 | return make_error<GenericBinaryError>(Args: "truncated or malformed object (" + |
| 65 | Msg + ")" , |
| 66 | Args: object_error::parse_failed); |
| 67 | } |
| 68 | |
| 69 | // FIXME: Replace all uses of this function with getStructOrErr. |
| 70 | template <typename T> |
| 71 | static T getStruct(const MachOObjectFile &O, const char *P) { |
| 72 | // Don't read before the beginning or past the end of the file |
| 73 | if (P < O.getData().begin() || P + sizeof(T) > O.getData().end()) |
| 74 | report_fatal_error(reason: "Malformed MachO file." ); |
| 75 | |
| 76 | T Cmd; |
| 77 | memcpy(&Cmd, P, sizeof(T)); |
| 78 | if (O.isLittleEndian() != sys::IsLittleEndianHost) |
| 79 | MachO::swapStruct(Cmd); |
| 80 | return Cmd; |
| 81 | } |
| 82 | |
| 83 | template <typename T> |
| 84 | static Expected<T> getStructOrErr(const MachOObjectFile &O, const char *P) { |
| 85 | // Don't read before the beginning or past the end of the file |
| 86 | if (P < O.getData().begin() || P + sizeof(T) > O.getData().end()) |
| 87 | return malformedError(Msg: "Structure read out-of-range" ); |
| 88 | |
| 89 | T Cmd; |
| 90 | memcpy(&Cmd, P, sizeof(T)); |
| 91 | if (O.isLittleEndian() != sys::IsLittleEndianHost) |
| 92 | MachO::swapStruct(Cmd); |
| 93 | return Cmd; |
| 94 | } |
| 95 | |
| 96 | static const char * |
| 97 | getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L, |
| 98 | unsigned Sec) { |
| 99 | uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); |
| 100 | |
| 101 | bool Is64 = O.is64Bit(); |
| 102 | unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) : |
| 103 | sizeof(MachO::segment_command); |
| 104 | unsigned SectionSize = Is64 ? sizeof(MachO::section_64) : |
| 105 | sizeof(MachO::section); |
| 106 | |
| 107 | uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; |
| 108 | return reinterpret_cast<const char*>(SectionAddr); |
| 109 | } |
| 110 | |
| 111 | static const char *getPtr(const MachOObjectFile &O, size_t Offset, |
| 112 | size_t MachOFilesetEntryOffset = 0) { |
| 113 | assert(Offset <= O.getData().size() && |
| 114 | MachOFilesetEntryOffset <= O.getData().size()); |
| 115 | return O.getData().data() + Offset + MachOFilesetEntryOffset; |
| 116 | } |
| 117 | |
| 118 | static MachO::nlist_base |
| 119 | getSymbolTableEntryBase(const MachOObjectFile &O, DataRefImpl DRI) { |
| 120 | const char *P = reinterpret_cast<const char *>(DRI.p); |
| 121 | return getStruct<MachO::nlist_base>(O, P); |
| 122 | } |
| 123 | |
| 124 | static StringRef parseSegmentOrSectionName(const char *P) { |
| 125 | if (P[15] == 0) |
| 126 | // Null terminated. |
| 127 | return P; |
| 128 | // Not null terminated, so this is a 16 char string. |
| 129 | return StringRef(P, 16); |
| 130 | } |
| 131 | |
| 132 | static unsigned getCPUType(const MachOObjectFile &O) { |
| 133 | return O.getHeader().cputype; |
| 134 | } |
| 135 | |
| 136 | static unsigned getCPUSubType(const MachOObjectFile &O) { |
| 137 | return O.getHeader().cpusubtype & ~MachO::CPU_SUBTYPE_MASK; |
| 138 | } |
| 139 | |
| 140 | static uint32_t |
| 141 | getPlainRelocationAddress(const MachO::any_relocation_info &RE) { |
| 142 | return RE.r_word0; |
| 143 | } |
| 144 | |
| 145 | static unsigned |
| 146 | getScatteredRelocationAddress(const MachO::any_relocation_info &RE) { |
| 147 | return RE.r_word0 & 0xffffff; |
| 148 | } |
| 149 | |
| 150 | static bool getPlainRelocationPCRel(const MachOObjectFile &O, |
| 151 | const MachO::any_relocation_info &RE) { |
| 152 | if (O.isLittleEndian()) |
| 153 | return (RE.r_word1 >> 24) & 1; |
| 154 | return (RE.r_word1 >> 7) & 1; |
| 155 | } |
| 156 | |
| 157 | static bool |
| 158 | getScatteredRelocationPCRel(const MachO::any_relocation_info &RE) { |
| 159 | return (RE.r_word0 >> 30) & 1; |
| 160 | } |
| 161 | |
| 162 | static unsigned getPlainRelocationLength(const MachOObjectFile &O, |
| 163 | const MachO::any_relocation_info &RE) { |
| 164 | if (O.isLittleEndian()) |
| 165 | return (RE.r_word1 >> 25) & 3; |
| 166 | return (RE.r_word1 >> 5) & 3; |
| 167 | } |
| 168 | |
| 169 | static unsigned |
| 170 | getScatteredRelocationLength(const MachO::any_relocation_info &RE) { |
| 171 | return (RE.r_word0 >> 28) & 3; |
| 172 | } |
| 173 | |
| 174 | static unsigned getPlainRelocationType(const MachOObjectFile &O, |
| 175 | const MachO::any_relocation_info &RE) { |
| 176 | if (O.isLittleEndian()) |
| 177 | return RE.r_word1 >> 28; |
| 178 | return RE.r_word1 & 0xf; |
| 179 | } |
| 180 | |
| 181 | static uint32_t getSectionFlags(const MachOObjectFile &O, |
| 182 | DataRefImpl Sec) { |
| 183 | if (O.is64Bit()) { |
| 184 | MachO::section_64 Sect = O.getSection64(DRI: Sec); |
| 185 | return Sect.flags; |
| 186 | } |
| 187 | MachO::section Sect = O.getSection(DRI: Sec); |
| 188 | return Sect.flags; |
| 189 | } |
| 190 | |
| 191 | static Expected<MachOObjectFile::LoadCommandInfo> |
| 192 | getLoadCommandInfo(const MachOObjectFile &Obj, const char *Ptr, |
| 193 | uint32_t LoadCommandIndex) { |
| 194 | if (auto CmdOrErr = getStructOrErr<MachO::load_command>(O: Obj, P: Ptr)) { |
| 195 | assert(Ptr <= Obj.getData().end() && "Start must be before end" ); |
| 196 | if (CmdOrErr->cmdsize > (uintptr_t)(Obj.getData().end() - Ptr)) |
| 197 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 198 | " extends past end of file" ); |
| 199 | if (CmdOrErr->cmdsize < 8) |
| 200 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 201 | " with size less than 8 bytes" ); |
| 202 | return MachOObjectFile::LoadCommandInfo({.Ptr: Ptr, .C: *CmdOrErr}); |
| 203 | } else |
| 204 | return CmdOrErr.takeError(); |
| 205 | } |
| 206 | |
| 207 | static Expected<MachOObjectFile::LoadCommandInfo> |
| 208 | getFirstLoadCommandInfo(const MachOObjectFile &Obj) { |
| 209 | unsigned = Obj.is64Bit() ? sizeof(MachO::mach_header_64) |
| 210 | : sizeof(MachO::mach_header); |
| 211 | if (sizeof(MachO::load_command) > Obj.getHeader().sizeofcmds) |
| 212 | return malformedError(Msg: "load command 0 extends past the end all load " |
| 213 | "commands in the file" ); |
| 214 | return getLoadCommandInfo( |
| 215 | Obj, Ptr: getPtr(O: Obj, Offset: HeaderSize, MachOFilesetEntryOffset: Obj.getMachOFilesetEntryOffset()), LoadCommandIndex: 0); |
| 216 | } |
| 217 | |
| 218 | static Expected<MachOObjectFile::LoadCommandInfo> |
| 219 | getNextLoadCommandInfo(const MachOObjectFile &Obj, uint32_t LoadCommandIndex, |
| 220 | const MachOObjectFile::LoadCommandInfo &L) { |
| 221 | unsigned = Obj.is64Bit() ? sizeof(MachO::mach_header_64) |
| 222 | : sizeof(MachO::mach_header); |
| 223 | if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) > |
| 224 | Obj.getData().data() + Obj.getMachOFilesetEntryOffset() + HeaderSize + |
| 225 | Obj.getHeader().sizeofcmds) |
| 226 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex + 1) + |
| 227 | " extends past the end all load commands in the file" ); |
| 228 | return getLoadCommandInfo(Obj, Ptr: L.Ptr + L.C.cmdsize, LoadCommandIndex: LoadCommandIndex + 1); |
| 229 | } |
| 230 | |
| 231 | template <typename T> |
| 232 | static void (const MachOObjectFile &Obj, T &, |
| 233 | Error &Err) { |
| 234 | if (sizeof(T) > Obj.getData().size()) { |
| 235 | Err = malformedError(Msg: "the mach header extends past the end of the " |
| 236 | "file" ); |
| 237 | return; |
| 238 | } |
| 239 | if (auto = getStructOrErr<T>( |
| 240 | Obj, getPtr(O: Obj, Offset: 0, MachOFilesetEntryOffset: Obj.getMachOFilesetEntryOffset()))) |
| 241 | Header = *HeaderOrErr; |
| 242 | else |
| 243 | Err = HeaderOrErr.takeError(); |
| 244 | } |
| 245 | |
| 246 | // This is used to check for overlapping of Mach-O elements. |
| 247 | struct MachOElement { |
| 248 | uint64_t Offset; |
| 249 | uint64_t Size; |
| 250 | const char *Name; |
| 251 | }; |
| 252 | |
| 253 | static Error checkOverlappingElement(std::list<MachOElement> &Elements, |
| 254 | uint64_t Offset, uint64_t Size, |
| 255 | const char *Name) { |
| 256 | if (Size == 0) |
| 257 | return Error::success(); |
| 258 | |
| 259 | for (auto it = Elements.begin(); it != Elements.end(); ++it) { |
| 260 | const auto &E = *it; |
| 261 | if ((Offset >= E.Offset && Offset < E.Offset + E.Size) || |
| 262 | (Offset + Size > E.Offset && Offset + Size < E.Offset + E.Size) || |
| 263 | (Offset <= E.Offset && Offset + Size >= E.Offset + E.Size)) |
| 264 | return malformedError(Msg: Twine(Name) + " at offset " + Twine(Offset) + |
| 265 | " with a size of " + Twine(Size) + ", overlaps " + |
| 266 | E.Name + " at offset " + Twine(E.Offset) + " with " |
| 267 | "a size of " + Twine(E.Size)); |
| 268 | auto nt = it; |
| 269 | nt++; |
| 270 | if (nt != Elements.end()) { |
| 271 | const auto &N = *nt; |
| 272 | if (Offset + Size <= N.Offset) { |
| 273 | Elements.insert(position: nt, x: {.Offset: Offset, .Size: Size, .Name: Name}); |
| 274 | return Error::success(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | Elements.push_back(x: {.Offset: Offset, .Size: Size, .Name: Name}); |
| 279 | return Error::success(); |
| 280 | } |
| 281 | |
| 282 | // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all |
| 283 | // sections to \param Sections, and optionally sets |
| 284 | // \param IsPageZeroSegment to true. |
| 285 | template <typename Segment, typename Section> |
| 286 | static Error parseSegmentLoadCommand( |
| 287 | const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, |
| 288 | SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment, |
| 289 | uint32_t LoadCommandIndex, const char *CmdName, uint64_t , |
| 290 | std::list<MachOElement> &Elements) { |
| 291 | const unsigned SegmentLoadSize = sizeof(Segment); |
| 292 | if (Load.C.cmdsize < SegmentLoadSize) |
| 293 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 294 | " " + CmdName + " cmdsize too small" ); |
| 295 | if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) { |
| 296 | Segment S = SegOrErr.get(); |
| 297 | const unsigned SectionSize = sizeof(Section); |
| 298 | uint64_t FileSize = Obj.getData().size(); |
| 299 | if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || |
| 300 | S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) |
| 301 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 302 | " inconsistent cmdsize in " + CmdName + |
| 303 | " for the number of sections" ); |
| 304 | for (unsigned J = 0; J < S.nsects; ++J) { |
| 305 | const char *Sec = getSectionPtr(O: Obj, L: Load, Sec: J); |
| 306 | Sections.push_back(Elt: Sec); |
| 307 | auto SectionOrErr = getStructOrErr<Section>(Obj, Sec); |
| 308 | if (!SectionOrErr) |
| 309 | return SectionOrErr.takeError(); |
| 310 | Section s = SectionOrErr.get(); |
| 311 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 312 | Obj.getHeader().filetype != MachO::MH_DSYM && |
| 313 | s.flags != MachO::S_ZEROFILL && |
| 314 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 315 | s.offset > FileSize) |
| 316 | return malformedError(Msg: "offset field of section " + Twine(J) + " in " + |
| 317 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 318 | " extends past the end of the file" ); |
| 319 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 320 | Obj.getHeader().filetype != MachO::MH_DSYM && |
| 321 | s.flags != MachO::S_ZEROFILL && |
| 322 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 && |
| 323 | s.offset < SizeOfHeaders && s.size != 0) |
| 324 | return malformedError(Msg: "offset field of section " + Twine(J) + " in " + |
| 325 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 326 | " not past the headers of the file" ); |
| 327 | uint64_t BigSize = s.offset; |
| 328 | BigSize += s.size; |
| 329 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 330 | Obj.getHeader().filetype != MachO::MH_DSYM && |
| 331 | s.flags != MachO::S_ZEROFILL && |
| 332 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 333 | BigSize > FileSize) |
| 334 | return malformedError(Msg: "offset field plus size field of section " + |
| 335 | Twine(J) + " in " + CmdName + " command " + |
| 336 | Twine(LoadCommandIndex) + |
| 337 | " extends past the end of the file" ); |
| 338 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 339 | Obj.getHeader().filetype != MachO::MH_DSYM && |
| 340 | s.flags != MachO::S_ZEROFILL && |
| 341 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 342 | s.size > S.filesize) |
| 343 | return malformedError(Msg: "size field of section " + |
| 344 | Twine(J) + " in " + CmdName + " command " + |
| 345 | Twine(LoadCommandIndex) + |
| 346 | " greater than the segment" ); |
| 347 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 348 | Obj.getHeader().filetype != MachO::MH_DSYM && s.size != 0 && |
| 349 | s.addr < S.vmaddr) |
| 350 | return malformedError(Msg: "addr field of section " + Twine(J) + " in " + |
| 351 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 352 | " less than the segment's vmaddr" ); |
| 353 | BigSize = s.addr; |
| 354 | BigSize += s.size; |
| 355 | uint64_t BigEnd = S.vmaddr; |
| 356 | BigEnd += S.vmsize; |
| 357 | if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd) |
| 358 | return malformedError(Msg: "addr field plus size of section " + Twine(J) + |
| 359 | " in " + CmdName + " command " + |
| 360 | Twine(LoadCommandIndex) + |
| 361 | " greater than than " |
| 362 | "the segment's vmaddr plus vmsize" ); |
| 363 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 364 | Obj.getHeader().filetype != MachO::MH_DSYM && |
| 365 | s.flags != MachO::S_ZEROFILL && |
| 366 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL) |
| 367 | if (Error Err = checkOverlappingElement(Elements, s.offset, s.size, |
| 368 | "section contents" )) |
| 369 | return Err; |
| 370 | if (s.reloff > FileSize) |
| 371 | return malformedError(Msg: "reloff field of section " + Twine(J) + " in " + |
| 372 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 373 | " extends past the end of the file" ); |
| 374 | BigSize = s.nreloc; |
| 375 | BigSize *= sizeof(struct MachO::relocation_info); |
| 376 | BigSize += s.reloff; |
| 377 | if (BigSize > FileSize) |
| 378 | return malformedError(Msg: "reloff field plus nreloc field times sizeof(" |
| 379 | "struct relocation_info) of section " + |
| 380 | Twine(J) + " in " + CmdName + " command " + |
| 381 | Twine(LoadCommandIndex) + |
| 382 | " extends past the end of the file" ); |
| 383 | if (Error Err = checkOverlappingElement(Elements, s.reloff, s.nreloc * |
| 384 | sizeof(struct |
| 385 | MachO::relocation_info), |
| 386 | "section relocation entries" )) |
| 387 | return Err; |
| 388 | } |
| 389 | if (S.fileoff > FileSize) |
| 390 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 391 | " fileoff field in " + CmdName + |
| 392 | " extends past the end of the file" ); |
| 393 | uint64_t BigSize = S.fileoff; |
| 394 | BigSize += S.filesize; |
| 395 | if (BigSize > FileSize) |
| 396 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 397 | " fileoff field plus filesize field in " + |
| 398 | CmdName + " extends past the end of the file" ); |
| 399 | if (S.vmsize != 0 && S.filesize > S.vmsize) |
| 400 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 401 | " filesize field in " + CmdName + |
| 402 | " greater than vmsize field" ); |
| 403 | IsPageZeroSegment |= StringRef("__PAGEZERO" ) == S.segname; |
| 404 | } else |
| 405 | return SegOrErr.takeError(); |
| 406 | |
| 407 | return Error::success(); |
| 408 | } |
| 409 | |
| 410 | static Error checkSymtabCommand(const MachOObjectFile &Obj, |
| 411 | const MachOObjectFile::LoadCommandInfo &Load, |
| 412 | uint32_t LoadCommandIndex, |
| 413 | const char **SymtabLoadCmd, |
| 414 | std::list<MachOElement> &Elements) { |
| 415 | if (Load.C.cmdsize < sizeof(MachO::symtab_command)) |
| 416 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 417 | " LC_SYMTAB cmdsize too small" ); |
| 418 | if (*SymtabLoadCmd != nullptr) |
| 419 | return malformedError(Msg: "more than one LC_SYMTAB command" ); |
| 420 | auto SymtabOrErr = getStructOrErr<MachO::symtab_command>(O: Obj, P: Load.Ptr); |
| 421 | if (!SymtabOrErr) |
| 422 | return SymtabOrErr.takeError(); |
| 423 | MachO::symtab_command Symtab = SymtabOrErr.get(); |
| 424 | if (Symtab.cmdsize != sizeof(MachO::symtab_command)) |
| 425 | return malformedError(Msg: "LC_SYMTAB command " + Twine(LoadCommandIndex) + |
| 426 | " has incorrect cmdsize" ); |
| 427 | uint64_t FileSize = Obj.getData().size(); |
| 428 | if (Symtab.symoff > FileSize) |
| 429 | return malformedError(Msg: "symoff field of LC_SYMTAB command " + |
| 430 | Twine(LoadCommandIndex) + " extends past the end " |
| 431 | "of the file" ); |
| 432 | uint64_t SymtabSize = Symtab.nsyms; |
| 433 | const char *struct_nlist_name; |
| 434 | if (Obj.is64Bit()) { |
| 435 | SymtabSize *= sizeof(MachO::nlist_64); |
| 436 | struct_nlist_name = "struct nlist_64" ; |
| 437 | } else { |
| 438 | SymtabSize *= sizeof(MachO::nlist); |
| 439 | struct_nlist_name = "struct nlist" ; |
| 440 | } |
| 441 | uint64_t BigSize = SymtabSize; |
| 442 | BigSize += Symtab.symoff; |
| 443 | if (BigSize > FileSize) |
| 444 | return malformedError(Msg: "symoff field plus nsyms field times sizeof(" + |
| 445 | Twine(struct_nlist_name) + ") of LC_SYMTAB command " + |
| 446 | Twine(LoadCommandIndex) + " extends past the end " |
| 447 | "of the file" ); |
| 448 | if (Error Err = checkOverlappingElement(Elements, Offset: Symtab.symoff, Size: SymtabSize, |
| 449 | Name: "symbol table" )) |
| 450 | return Err; |
| 451 | if (Symtab.stroff > FileSize) |
| 452 | return malformedError(Msg: "stroff field of LC_SYMTAB command " + |
| 453 | Twine(LoadCommandIndex) + " extends past the end " |
| 454 | "of the file" ); |
| 455 | BigSize = Symtab.stroff; |
| 456 | BigSize += Symtab.strsize; |
| 457 | if (BigSize > FileSize) |
| 458 | return malformedError(Msg: "stroff field plus strsize field of LC_SYMTAB " |
| 459 | "command " + Twine(LoadCommandIndex) + " extends " |
| 460 | "past the end of the file" ); |
| 461 | if (Error Err = checkOverlappingElement(Elements, Offset: Symtab.stroff, |
| 462 | Size: Symtab.strsize, Name: "string table" )) |
| 463 | return Err; |
| 464 | *SymtabLoadCmd = Load.Ptr; |
| 465 | return Error::success(); |
| 466 | } |
| 467 | |
| 468 | static Error checkDysymtabCommand(const MachOObjectFile &Obj, |
| 469 | const MachOObjectFile::LoadCommandInfo &Load, |
| 470 | uint32_t LoadCommandIndex, |
| 471 | const char **DysymtabLoadCmd, |
| 472 | std::list<MachOElement> &Elements) { |
| 473 | if (Load.C.cmdsize < sizeof(MachO::dysymtab_command)) |
| 474 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 475 | " LC_DYSYMTAB cmdsize too small" ); |
| 476 | if (*DysymtabLoadCmd != nullptr) |
| 477 | return malformedError(Msg: "more than one LC_DYSYMTAB command" ); |
| 478 | auto DysymtabOrErr = |
| 479 | getStructOrErr<MachO::dysymtab_command>(O: Obj, P: Load.Ptr); |
| 480 | if (!DysymtabOrErr) |
| 481 | return DysymtabOrErr.takeError(); |
| 482 | MachO::dysymtab_command Dysymtab = DysymtabOrErr.get(); |
| 483 | if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command)) |
| 484 | return malformedError(Msg: "LC_DYSYMTAB command " + Twine(LoadCommandIndex) + |
| 485 | " has incorrect cmdsize" ); |
| 486 | uint64_t FileSize = Obj.getData().size(); |
| 487 | if (Dysymtab.tocoff > FileSize) |
| 488 | return malformedError(Msg: "tocoff field of LC_DYSYMTAB command " + |
| 489 | Twine(LoadCommandIndex) + " extends past the end of " |
| 490 | "the file" ); |
| 491 | uint64_t BigSize = Dysymtab.ntoc; |
| 492 | BigSize *= sizeof(MachO::dylib_table_of_contents); |
| 493 | BigSize += Dysymtab.tocoff; |
| 494 | if (BigSize > FileSize) |
| 495 | return malformedError(Msg: "tocoff field plus ntoc field times sizeof(struct " |
| 496 | "dylib_table_of_contents) of LC_DYSYMTAB command " + |
| 497 | Twine(LoadCommandIndex) + " extends past the end of " |
| 498 | "the file" ); |
| 499 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.tocoff, |
| 500 | Size: Dysymtab.ntoc * sizeof(struct |
| 501 | MachO::dylib_table_of_contents), |
| 502 | Name: "table of contents" )) |
| 503 | return Err; |
| 504 | if (Dysymtab.modtaboff > FileSize) |
| 505 | return malformedError(Msg: "modtaboff field of LC_DYSYMTAB command " + |
| 506 | Twine(LoadCommandIndex) + " extends past the end of " |
| 507 | "the file" ); |
| 508 | BigSize = Dysymtab.nmodtab; |
| 509 | const char *struct_dylib_module_name; |
| 510 | uint64_t sizeof_modtab; |
| 511 | if (Obj.is64Bit()) { |
| 512 | sizeof_modtab = sizeof(MachO::dylib_module_64); |
| 513 | struct_dylib_module_name = "struct dylib_module_64" ; |
| 514 | } else { |
| 515 | sizeof_modtab = sizeof(MachO::dylib_module); |
| 516 | struct_dylib_module_name = "struct dylib_module" ; |
| 517 | } |
| 518 | BigSize *= sizeof_modtab; |
| 519 | BigSize += Dysymtab.modtaboff; |
| 520 | if (BigSize > FileSize) |
| 521 | return malformedError(Msg: "modtaboff field plus nmodtab field times sizeof(" + |
| 522 | Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB " |
| 523 | "command " + Twine(LoadCommandIndex) + " extends " |
| 524 | "past the end of the file" ); |
| 525 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.modtaboff, |
| 526 | Size: Dysymtab.nmodtab * sizeof_modtab, |
| 527 | Name: "module table" )) |
| 528 | return Err; |
| 529 | if (Dysymtab.extrefsymoff > FileSize) |
| 530 | return malformedError(Msg: "extrefsymoff field of LC_DYSYMTAB command " + |
| 531 | Twine(LoadCommandIndex) + " extends past the end of " |
| 532 | "the file" ); |
| 533 | BigSize = Dysymtab.nextrefsyms; |
| 534 | BigSize *= sizeof(MachO::dylib_reference); |
| 535 | BigSize += Dysymtab.extrefsymoff; |
| 536 | if (BigSize > FileSize) |
| 537 | return malformedError(Msg: "extrefsymoff field plus nextrefsyms field times " |
| 538 | "sizeof(struct dylib_reference) of LC_DYSYMTAB " |
| 539 | "command " + Twine(LoadCommandIndex) + " extends " |
| 540 | "past the end of the file" ); |
| 541 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.extrefsymoff, |
| 542 | Size: Dysymtab.nextrefsyms * |
| 543 | sizeof(MachO::dylib_reference), |
| 544 | Name: "reference table" )) |
| 545 | return Err; |
| 546 | if (Dysymtab.indirectsymoff > FileSize) |
| 547 | return malformedError(Msg: "indirectsymoff field of LC_DYSYMTAB command " + |
| 548 | Twine(LoadCommandIndex) + " extends past the end of " |
| 549 | "the file" ); |
| 550 | BigSize = Dysymtab.nindirectsyms; |
| 551 | BigSize *= sizeof(uint32_t); |
| 552 | BigSize += Dysymtab.indirectsymoff; |
| 553 | if (BigSize > FileSize) |
| 554 | return malformedError(Msg: "indirectsymoff field plus nindirectsyms field times " |
| 555 | "sizeof(uint32_t) of LC_DYSYMTAB command " + |
| 556 | Twine(LoadCommandIndex) + " extends past the end of " |
| 557 | "the file" ); |
| 558 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.indirectsymoff, |
| 559 | Size: Dysymtab.nindirectsyms * |
| 560 | sizeof(uint32_t), |
| 561 | Name: "indirect table" )) |
| 562 | return Err; |
| 563 | if (Dysymtab.extreloff > FileSize) |
| 564 | return malformedError(Msg: "extreloff field of LC_DYSYMTAB command " + |
| 565 | Twine(LoadCommandIndex) + " extends past the end of " |
| 566 | "the file" ); |
| 567 | BigSize = Dysymtab.nextrel; |
| 568 | BigSize *= sizeof(MachO::relocation_info); |
| 569 | BigSize += Dysymtab.extreloff; |
| 570 | if (BigSize > FileSize) |
| 571 | return malformedError(Msg: "extreloff field plus nextrel field times sizeof" |
| 572 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 573 | Twine(LoadCommandIndex) + " extends past the end of " |
| 574 | "the file" ); |
| 575 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.extreloff, |
| 576 | Size: Dysymtab.nextrel * |
| 577 | sizeof(MachO::relocation_info), |
| 578 | Name: "external relocation table" )) |
| 579 | return Err; |
| 580 | if (Dysymtab.locreloff > FileSize) |
| 581 | return malformedError(Msg: "locreloff field of LC_DYSYMTAB command " + |
| 582 | Twine(LoadCommandIndex) + " extends past the end of " |
| 583 | "the file" ); |
| 584 | BigSize = Dysymtab.nlocrel; |
| 585 | BigSize *= sizeof(MachO::relocation_info); |
| 586 | BigSize += Dysymtab.locreloff; |
| 587 | if (BigSize > FileSize) |
| 588 | return malformedError(Msg: "locreloff field plus nlocrel field times sizeof" |
| 589 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 590 | Twine(LoadCommandIndex) + " extends past the end of " |
| 591 | "the file" ); |
| 592 | if (Error Err = checkOverlappingElement(Elements, Offset: Dysymtab.locreloff, |
| 593 | Size: Dysymtab.nlocrel * |
| 594 | sizeof(MachO::relocation_info), |
| 595 | Name: "local relocation table" )) |
| 596 | return Err; |
| 597 | *DysymtabLoadCmd = Load.Ptr; |
| 598 | return Error::success(); |
| 599 | } |
| 600 | |
| 601 | static Error checkLinkeditDataCommand(const MachOObjectFile &Obj, |
| 602 | const MachOObjectFile::LoadCommandInfo &Load, |
| 603 | uint32_t LoadCommandIndex, |
| 604 | const char **LoadCmd, const char *CmdName, |
| 605 | std::list<MachOElement> &Elements, |
| 606 | const char *ElementName) { |
| 607 | if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command)) |
| 608 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 609 | CmdName + " cmdsize too small" ); |
| 610 | if (*LoadCmd != nullptr) |
| 611 | return malformedError(Msg: "more than one " + Twine(CmdName) + " command" ); |
| 612 | auto LinkDataOrError = |
| 613 | getStructOrErr<MachO::linkedit_data_command>(O: Obj, P: Load.Ptr); |
| 614 | if (!LinkDataOrError) |
| 615 | return LinkDataOrError.takeError(); |
| 616 | MachO::linkedit_data_command LinkData = LinkDataOrError.get(); |
| 617 | if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command)) |
| 618 | return malformedError(Msg: Twine(CmdName) + " command " + |
| 619 | Twine(LoadCommandIndex) + " has incorrect cmdsize" ); |
| 620 | uint64_t FileSize = Obj.getData().size(); |
| 621 | if (LinkData.dataoff > FileSize) |
| 622 | return malformedError(Msg: "dataoff field of " + Twine(CmdName) + " command " + |
| 623 | Twine(LoadCommandIndex) + " extends past the end of " |
| 624 | "the file" ); |
| 625 | uint64_t BigSize = LinkData.dataoff; |
| 626 | BigSize += LinkData.datasize; |
| 627 | if (BigSize > FileSize) |
| 628 | return malformedError(Msg: "dataoff field plus datasize field of " + |
| 629 | Twine(CmdName) + " command " + |
| 630 | Twine(LoadCommandIndex) + " extends past the end of " |
| 631 | "the file" ); |
| 632 | if (Error Err = checkOverlappingElement(Elements, Offset: LinkData.dataoff, |
| 633 | Size: LinkData.datasize, Name: ElementName)) |
| 634 | return Err; |
| 635 | *LoadCmd = Load.Ptr; |
| 636 | return Error::success(); |
| 637 | } |
| 638 | |
| 639 | static Error checkDyldInfoCommand(const MachOObjectFile &Obj, |
| 640 | const MachOObjectFile::LoadCommandInfo &Load, |
| 641 | uint32_t LoadCommandIndex, |
| 642 | const char **LoadCmd, const char *CmdName, |
| 643 | std::list<MachOElement> &Elements) { |
| 644 | if (Load.C.cmdsize < sizeof(MachO::dyld_info_command)) |
| 645 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 646 | CmdName + " cmdsize too small" ); |
| 647 | if (*LoadCmd != nullptr) |
| 648 | return malformedError(Msg: "more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY " |
| 649 | "command" ); |
| 650 | auto DyldInfoOrErr = |
| 651 | getStructOrErr<MachO::dyld_info_command>(O: Obj, P: Load.Ptr); |
| 652 | if (!DyldInfoOrErr) |
| 653 | return DyldInfoOrErr.takeError(); |
| 654 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 655 | if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command)) |
| 656 | return malformedError(Msg: Twine(CmdName) + " command " + |
| 657 | Twine(LoadCommandIndex) + " has incorrect cmdsize" ); |
| 658 | uint64_t FileSize = Obj.getData().size(); |
| 659 | if (DyldInfo.rebase_off > FileSize) |
| 660 | return malformedError(Msg: "rebase_off field of " + Twine(CmdName) + |
| 661 | " command " + Twine(LoadCommandIndex) + " extends " |
| 662 | "past the end of the file" ); |
| 663 | uint64_t BigSize = DyldInfo.rebase_off; |
| 664 | BigSize += DyldInfo.rebase_size; |
| 665 | if (BigSize > FileSize) |
| 666 | return malformedError(Msg: "rebase_off field plus rebase_size field of " + |
| 667 | Twine(CmdName) + " command " + |
| 668 | Twine(LoadCommandIndex) + " extends past the end of " |
| 669 | "the file" ); |
| 670 | if (Error Err = checkOverlappingElement(Elements, Offset: DyldInfo.rebase_off, |
| 671 | Size: DyldInfo.rebase_size, |
| 672 | Name: "dyld rebase info" )) |
| 673 | return Err; |
| 674 | if (DyldInfo.bind_off > FileSize) |
| 675 | return malformedError(Msg: "bind_off field of " + Twine(CmdName) + |
| 676 | " command " + Twine(LoadCommandIndex) + " extends " |
| 677 | "past the end of the file" ); |
| 678 | BigSize = DyldInfo.bind_off; |
| 679 | BigSize += DyldInfo.bind_size; |
| 680 | if (BigSize > FileSize) |
| 681 | return malformedError(Msg: "bind_off field plus bind_size field of " + |
| 682 | Twine(CmdName) + " command " + |
| 683 | Twine(LoadCommandIndex) + " extends past the end of " |
| 684 | "the file" ); |
| 685 | if (Error Err = checkOverlappingElement(Elements, Offset: DyldInfo.bind_off, |
| 686 | Size: DyldInfo.bind_size, |
| 687 | Name: "dyld bind info" )) |
| 688 | return Err; |
| 689 | if (DyldInfo.weak_bind_off > FileSize) |
| 690 | return malformedError(Msg: "weak_bind_off field of " + Twine(CmdName) + |
| 691 | " command " + Twine(LoadCommandIndex) + " extends " |
| 692 | "past the end of the file" ); |
| 693 | BigSize = DyldInfo.weak_bind_off; |
| 694 | BigSize += DyldInfo.weak_bind_size; |
| 695 | if (BigSize > FileSize) |
| 696 | return malformedError(Msg: "weak_bind_off field plus weak_bind_size field of " + |
| 697 | Twine(CmdName) + " command " + |
| 698 | Twine(LoadCommandIndex) + " extends past the end of " |
| 699 | "the file" ); |
| 700 | if (Error Err = checkOverlappingElement(Elements, Offset: DyldInfo.weak_bind_off, |
| 701 | Size: DyldInfo.weak_bind_size, |
| 702 | Name: "dyld weak bind info" )) |
| 703 | return Err; |
| 704 | if (DyldInfo.lazy_bind_off > FileSize) |
| 705 | return malformedError(Msg: "lazy_bind_off field of " + Twine(CmdName) + |
| 706 | " command " + Twine(LoadCommandIndex) + " extends " |
| 707 | "past the end of the file" ); |
| 708 | BigSize = DyldInfo.lazy_bind_off; |
| 709 | BigSize += DyldInfo.lazy_bind_size; |
| 710 | if (BigSize > FileSize) |
| 711 | return malformedError(Msg: "lazy_bind_off field plus lazy_bind_size field of " + |
| 712 | Twine(CmdName) + " command " + |
| 713 | Twine(LoadCommandIndex) + " extends past the end of " |
| 714 | "the file" ); |
| 715 | if (Error Err = checkOverlappingElement(Elements, Offset: DyldInfo.lazy_bind_off, |
| 716 | Size: DyldInfo.lazy_bind_size, |
| 717 | Name: "dyld lazy bind info" )) |
| 718 | return Err; |
| 719 | if (DyldInfo.export_off > FileSize) |
| 720 | return malformedError(Msg: "export_off field of " + Twine(CmdName) + |
| 721 | " command " + Twine(LoadCommandIndex) + " extends " |
| 722 | "past the end of the file" ); |
| 723 | BigSize = DyldInfo.export_off; |
| 724 | BigSize += DyldInfo.export_size; |
| 725 | if (BigSize > FileSize) |
| 726 | return malformedError(Msg: "export_off field plus export_size field of " + |
| 727 | Twine(CmdName) + " command " + |
| 728 | Twine(LoadCommandIndex) + " extends past the end of " |
| 729 | "the file" ); |
| 730 | if (Error Err = checkOverlappingElement(Elements, Offset: DyldInfo.export_off, |
| 731 | Size: DyldInfo.export_size, |
| 732 | Name: "dyld export info" )) |
| 733 | return Err; |
| 734 | *LoadCmd = Load.Ptr; |
| 735 | return Error::success(); |
| 736 | } |
| 737 | |
| 738 | static Error checkDylibCommand(const MachOObjectFile &Obj, |
| 739 | const MachOObjectFile::LoadCommandInfo &Load, |
| 740 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 741 | if (Load.C.cmdsize < sizeof(MachO::dylib_command)) |
| 742 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 743 | CmdName + " cmdsize too small" ); |
| 744 | auto CommandOrErr = getStructOrErr<MachO::dylib_command>(O: Obj, P: Load.Ptr); |
| 745 | if (!CommandOrErr) |
| 746 | return CommandOrErr.takeError(); |
| 747 | MachO::dylib_command D = CommandOrErr.get(); |
| 748 | if (D.dylib.name < sizeof(MachO::dylib_command)) |
| 749 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 750 | CmdName + " name.offset field too small, not past " |
| 751 | "the end of the dylib_command struct" ); |
| 752 | if (D.dylib.name >= D.cmdsize) |
| 753 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 754 | CmdName + " name.offset field extends past the end " |
| 755 | "of the load command" ); |
| 756 | // Make sure there is a null between the starting offset of the name and |
| 757 | // the end of the load command. |
| 758 | uint32_t i; |
| 759 | const char *P = (const char *)Load.Ptr; |
| 760 | for (i = D.dylib.name; i < D.cmdsize; i++) |
| 761 | if (P[i] == '\0') |
| 762 | break; |
| 763 | if (i >= D.cmdsize) |
| 764 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 765 | CmdName + " library name extends past the end of the " |
| 766 | "load command" ); |
| 767 | return Error::success(); |
| 768 | } |
| 769 | |
| 770 | static Error checkDylibIdCommand(const MachOObjectFile &Obj, |
| 771 | const MachOObjectFile::LoadCommandInfo &Load, |
| 772 | uint32_t LoadCommandIndex, |
| 773 | const char **LoadCmd) { |
| 774 | if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex, |
| 775 | CmdName: "LC_ID_DYLIB" )) |
| 776 | return Err; |
| 777 | if (*LoadCmd != nullptr) |
| 778 | return malformedError(Msg: "more than one LC_ID_DYLIB command" ); |
| 779 | if (Obj.getHeader().filetype != MachO::MH_DYLIB && |
| 780 | Obj.getHeader().filetype != MachO::MH_DYLIB_STUB) |
| 781 | return malformedError(Msg: "LC_ID_DYLIB load command in non-dynamic library " |
| 782 | "file type" ); |
| 783 | *LoadCmd = Load.Ptr; |
| 784 | return Error::success(); |
| 785 | } |
| 786 | |
| 787 | static Error checkDyldCommand(const MachOObjectFile &Obj, |
| 788 | const MachOObjectFile::LoadCommandInfo &Load, |
| 789 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 790 | if (Load.C.cmdsize < sizeof(MachO::dylinker_command)) |
| 791 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 792 | CmdName + " cmdsize too small" ); |
| 793 | auto CommandOrErr = getStructOrErr<MachO::dylinker_command>(O: Obj, P: Load.Ptr); |
| 794 | if (!CommandOrErr) |
| 795 | return CommandOrErr.takeError(); |
| 796 | MachO::dylinker_command D = CommandOrErr.get(); |
| 797 | if (D.name < sizeof(MachO::dylinker_command)) |
| 798 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 799 | CmdName + " name.offset field too small, not past " |
| 800 | "the end of the dylinker_command struct" ); |
| 801 | if (D.name >= D.cmdsize) |
| 802 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 803 | CmdName + " name.offset field extends past the end " |
| 804 | "of the load command" ); |
| 805 | // Make sure there is a null between the starting offset of the name and |
| 806 | // the end of the load command. |
| 807 | uint32_t i; |
| 808 | const char *P = (const char *)Load.Ptr; |
| 809 | for (i = D.name; i < D.cmdsize; i++) |
| 810 | if (P[i] == '\0') |
| 811 | break; |
| 812 | if (i >= D.cmdsize) |
| 813 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 814 | CmdName + " dyld name extends past the end of the " |
| 815 | "load command" ); |
| 816 | return Error::success(); |
| 817 | } |
| 818 | |
| 819 | static Error checkVersCommand(const MachOObjectFile &Obj, |
| 820 | const MachOObjectFile::LoadCommandInfo &Load, |
| 821 | uint32_t LoadCommandIndex, |
| 822 | const char **LoadCmd, const char *CmdName) { |
| 823 | if (Load.C.cmdsize != sizeof(MachO::version_min_command)) |
| 824 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 825 | CmdName + " has incorrect cmdsize" ); |
| 826 | if (*LoadCmd != nullptr) |
| 827 | return malformedError(Msg: "more than one LC_VERSION_MIN_MACOSX, " |
| 828 | "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or " |
| 829 | "LC_VERSION_MIN_WATCHOS command" ); |
| 830 | *LoadCmd = Load.Ptr; |
| 831 | return Error::success(); |
| 832 | } |
| 833 | |
| 834 | static Error checkNoteCommand(const MachOObjectFile &Obj, |
| 835 | const MachOObjectFile::LoadCommandInfo &Load, |
| 836 | uint32_t LoadCommandIndex, |
| 837 | std::list<MachOElement> &Elements) { |
| 838 | if (Load.C.cmdsize != sizeof(MachO::note_command)) |
| 839 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 840 | " LC_NOTE has incorrect cmdsize" ); |
| 841 | auto NoteCmdOrErr = getStructOrErr<MachO::note_command>(O: Obj, P: Load.Ptr); |
| 842 | if (!NoteCmdOrErr) |
| 843 | return NoteCmdOrErr.takeError(); |
| 844 | MachO::note_command Nt = NoteCmdOrErr.get(); |
| 845 | uint64_t FileSize = Obj.getData().size(); |
| 846 | if (Nt.offset > FileSize) |
| 847 | return malformedError(Msg: "offset field of LC_NOTE command " + |
| 848 | Twine(LoadCommandIndex) + " extends " |
| 849 | "past the end of the file" ); |
| 850 | uint64_t BigSize = Nt.offset; |
| 851 | BigSize += Nt.size; |
| 852 | if (BigSize > FileSize) |
| 853 | return malformedError(Msg: "size field plus offset field of LC_NOTE command " + |
| 854 | Twine(LoadCommandIndex) + " extends past the end of " |
| 855 | "the file" ); |
| 856 | if (Error Err = checkOverlappingElement(Elements, Offset: Nt.offset, Size: Nt.size, |
| 857 | Name: "LC_NOTE data" )) |
| 858 | return Err; |
| 859 | return Error::success(); |
| 860 | } |
| 861 | |
| 862 | static Error |
| 863 | parseBuildVersionCommand(const MachOObjectFile &Obj, |
| 864 | const MachOObjectFile::LoadCommandInfo &Load, |
| 865 | SmallVectorImpl<const char*> &BuildTools, |
| 866 | uint32_t LoadCommandIndex) { |
| 867 | auto BVCOrErr = |
| 868 | getStructOrErr<MachO::build_version_command>(O: Obj, P: Load.Ptr); |
| 869 | if (!BVCOrErr) |
| 870 | return BVCOrErr.takeError(); |
| 871 | MachO::build_version_command BVC = BVCOrErr.get(); |
| 872 | if (Load.C.cmdsize != |
| 873 | sizeof(MachO::build_version_command) + |
| 874 | BVC.ntools * sizeof(MachO::build_tool_version)) |
| 875 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 876 | " LC_BUILD_VERSION_COMMAND has incorrect cmdsize" ); |
| 877 | |
| 878 | auto Start = Load.Ptr + sizeof(MachO::build_version_command); |
| 879 | BuildTools.resize(N: BVC.ntools); |
| 880 | for (unsigned i = 0; i < BVC.ntools; ++i) |
| 881 | BuildTools[i] = Start + i * sizeof(MachO::build_tool_version); |
| 882 | |
| 883 | return Error::success(); |
| 884 | } |
| 885 | |
| 886 | static Error checkRpathCommand(const MachOObjectFile &Obj, |
| 887 | const MachOObjectFile::LoadCommandInfo &Load, |
| 888 | uint32_t LoadCommandIndex) { |
| 889 | if (Load.C.cmdsize < sizeof(MachO::rpath_command)) |
| 890 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 891 | " LC_RPATH cmdsize too small" ); |
| 892 | auto ROrErr = getStructOrErr<MachO::rpath_command>(O: Obj, P: Load.Ptr); |
| 893 | if (!ROrErr) |
| 894 | return ROrErr.takeError(); |
| 895 | MachO::rpath_command R = ROrErr.get(); |
| 896 | if (R.path < sizeof(MachO::rpath_command)) |
| 897 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 898 | " LC_RPATH path.offset field too small, not past " |
| 899 | "the end of the rpath_command struct" ); |
| 900 | if (R.path >= R.cmdsize) |
| 901 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 902 | " LC_RPATH path.offset field extends past the end " |
| 903 | "of the load command" ); |
| 904 | // Make sure there is a null between the starting offset of the path and |
| 905 | // the end of the load command. |
| 906 | uint32_t i; |
| 907 | const char *P = (const char *)Load.Ptr; |
| 908 | for (i = R.path; i < R.cmdsize; i++) |
| 909 | if (P[i] == '\0') |
| 910 | break; |
| 911 | if (i >= R.cmdsize) |
| 912 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 913 | " LC_RPATH library name extends past the end of the " |
| 914 | "load command" ); |
| 915 | return Error::success(); |
| 916 | } |
| 917 | |
| 918 | static Error checkEncryptCommand(const MachOObjectFile &Obj, |
| 919 | const MachOObjectFile::LoadCommandInfo &Load, |
| 920 | uint32_t LoadCommandIndex, |
| 921 | uint64_t cryptoff, uint64_t cryptsize, |
| 922 | const char **LoadCmd, const char *CmdName) { |
| 923 | if (*LoadCmd != nullptr) |
| 924 | return malformedError(Msg: "more than one LC_ENCRYPTION_INFO and or " |
| 925 | "LC_ENCRYPTION_INFO_64 command" ); |
| 926 | uint64_t FileSize = Obj.getData().size(); |
| 927 | if (cryptoff > FileSize) |
| 928 | return malformedError(Msg: "cryptoff field of " + Twine(CmdName) + |
| 929 | " command " + Twine(LoadCommandIndex) + " extends " |
| 930 | "past the end of the file" ); |
| 931 | uint64_t BigSize = cryptoff; |
| 932 | BigSize += cryptsize; |
| 933 | if (BigSize > FileSize) |
| 934 | return malformedError(Msg: "cryptoff field plus cryptsize field of " + |
| 935 | Twine(CmdName) + " command " + |
| 936 | Twine(LoadCommandIndex) + " extends past the end of " |
| 937 | "the file" ); |
| 938 | *LoadCmd = Load.Ptr; |
| 939 | return Error::success(); |
| 940 | } |
| 941 | |
| 942 | static Error checkLinkerOptCommand(const MachOObjectFile &Obj, |
| 943 | const MachOObjectFile::LoadCommandInfo &Load, |
| 944 | uint32_t LoadCommandIndex) { |
| 945 | if (Load.C.cmdsize < sizeof(MachO::linker_option_command)) |
| 946 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 947 | " LC_LINKER_OPTION cmdsize too small" ); |
| 948 | auto LinkOptionOrErr = |
| 949 | getStructOrErr<MachO::linker_option_command>(O: Obj, P: Load.Ptr); |
| 950 | if (!LinkOptionOrErr) |
| 951 | return LinkOptionOrErr.takeError(); |
| 952 | MachO::linker_option_command L = LinkOptionOrErr.get(); |
| 953 | // Make sure the count of strings is correct. |
| 954 | const char *string = (const char *)Load.Ptr + |
| 955 | sizeof(struct MachO::linker_option_command); |
| 956 | uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command); |
| 957 | uint32_t i = 0; |
| 958 | while (left > 0) { |
| 959 | while (*string == '\0' && left > 0) { |
| 960 | string++; |
| 961 | left--; |
| 962 | } |
| 963 | if (left > 0) { |
| 964 | i++; |
| 965 | uint32_t NullPos = StringRef(string, left).find(C: '\0'); |
| 966 | if (0xffffffff == NullPos) |
| 967 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 968 | " LC_LINKER_OPTION string #" + Twine(i) + |
| 969 | " is not NULL terminated" ); |
| 970 | uint32_t len = std::min(a: NullPos, b: left) + 1; |
| 971 | string += len; |
| 972 | left -= len; |
| 973 | } |
| 974 | } |
| 975 | if (L.count != i) |
| 976 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 977 | " LC_LINKER_OPTION string count " + Twine(L.count) + |
| 978 | " does not match number of strings" ); |
| 979 | return Error::success(); |
| 980 | } |
| 981 | |
| 982 | static Error checkSubCommand(const MachOObjectFile &Obj, |
| 983 | const MachOObjectFile::LoadCommandInfo &Load, |
| 984 | uint32_t LoadCommandIndex, const char *CmdName, |
| 985 | size_t SizeOfCmd, const char *CmdStructName, |
| 986 | uint32_t PathOffset, const char *PathFieldName) { |
| 987 | if (PathOffset < SizeOfCmd) |
| 988 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 989 | CmdName + " " + PathFieldName + ".offset field too " |
| 990 | "small, not past the end of the " + CmdStructName); |
| 991 | if (PathOffset >= Load.C.cmdsize) |
| 992 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 993 | CmdName + " " + PathFieldName + ".offset field " |
| 994 | "extends past the end of the load command" ); |
| 995 | // Make sure there is a null between the starting offset of the path and |
| 996 | // the end of the load command. |
| 997 | uint32_t i; |
| 998 | const char *P = (const char *)Load.Ptr; |
| 999 | for (i = PathOffset; i < Load.C.cmdsize; i++) |
| 1000 | if (P[i] == '\0') |
| 1001 | break; |
| 1002 | if (i >= Load.C.cmdsize) |
| 1003 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + " " + |
| 1004 | CmdName + " " + PathFieldName + " name extends past " |
| 1005 | "the end of the load command" ); |
| 1006 | return Error::success(); |
| 1007 | } |
| 1008 | |
| 1009 | static Error checkThreadCommand(const MachOObjectFile &Obj, |
| 1010 | const MachOObjectFile::LoadCommandInfo &Load, |
| 1011 | uint32_t LoadCommandIndex, |
| 1012 | const char *CmdName) { |
| 1013 | if (Load.C.cmdsize < sizeof(MachO::thread_command)) |
| 1014 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1015 | CmdName + " cmdsize too small" ); |
| 1016 | auto ThreadCommandOrErr = |
| 1017 | getStructOrErr<MachO::thread_command>(O: Obj, P: Load.Ptr); |
| 1018 | if (!ThreadCommandOrErr) |
| 1019 | return ThreadCommandOrErr.takeError(); |
| 1020 | MachO::thread_command T = ThreadCommandOrErr.get(); |
| 1021 | const char *state = Load.Ptr + sizeof(MachO::thread_command); |
| 1022 | const char *end = Load.Ptr + T.cmdsize; |
| 1023 | uint32_t nflavor = 0; |
| 1024 | uint32_t cputype = getCPUType(O: Obj); |
| 1025 | while (state < end) { |
| 1026 | if(state + sizeof(uint32_t) > end) |
| 1027 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1028 | "flavor in " + CmdName + " extends past end of " |
| 1029 | "command" ); |
| 1030 | uint32_t flavor; |
| 1031 | memcpy(dest: &flavor, src: state, n: sizeof(uint32_t)); |
| 1032 | if (Obj.isLittleEndian() != sys::IsLittleEndianHost) |
| 1033 | sys::swapByteOrder(Value&: flavor); |
| 1034 | state += sizeof(uint32_t); |
| 1035 | |
| 1036 | if(state + sizeof(uint32_t) > end) |
| 1037 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1038 | " count in " + CmdName + " extends past end of " |
| 1039 | "command" ); |
| 1040 | uint32_t count; |
| 1041 | memcpy(dest: &count, src: state, n: sizeof(uint32_t)); |
| 1042 | if (Obj.isLittleEndian() != sys::IsLittleEndianHost) |
| 1043 | sys::swapByteOrder(Value&: count); |
| 1044 | state += sizeof(uint32_t); |
| 1045 | |
| 1046 | if (cputype == MachO::CPU_TYPE_I386) { |
| 1047 | if (flavor == MachO::x86_THREAD_STATE32) { |
| 1048 | if (count != MachO::x86_THREAD_STATE32_COUNT) |
| 1049 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1050 | " count not x86_THREAD_STATE32_COUNT for " |
| 1051 | "flavor number " + Twine(nflavor) + " which is " |
| 1052 | "a x86_THREAD_STATE32 flavor in " + CmdName + |
| 1053 | " command" ); |
| 1054 | if (state + sizeof(MachO::x86_thread_state32_t) > end) |
| 1055 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1056 | " x86_THREAD_STATE32 extends past end of " |
| 1057 | "command in " + CmdName + " command" ); |
| 1058 | state += sizeof(MachO::x86_thread_state32_t); |
| 1059 | } else { |
| 1060 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1061 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1062 | "flavor number " + Twine(nflavor) + " in " + |
| 1063 | CmdName + " command" ); |
| 1064 | } |
| 1065 | } else if (cputype == MachO::CPU_TYPE_X86_64) { |
| 1066 | if (flavor == MachO::x86_THREAD_STATE) { |
| 1067 | if (count != MachO::x86_THREAD_STATE_COUNT) |
| 1068 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1069 | " count not x86_THREAD_STATE_COUNT for " |
| 1070 | "flavor number " + Twine(nflavor) + " which is " |
| 1071 | "a x86_THREAD_STATE flavor in " + CmdName + |
| 1072 | " command" ); |
| 1073 | if (state + sizeof(MachO::x86_thread_state_t) > end) |
| 1074 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1075 | " x86_THREAD_STATE extends past end of " |
| 1076 | "command in " + CmdName + " command" ); |
| 1077 | state += sizeof(MachO::x86_thread_state_t); |
| 1078 | } else if (flavor == MachO::x86_FLOAT_STATE) { |
| 1079 | if (count != MachO::x86_FLOAT_STATE_COUNT) |
| 1080 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1081 | " count not x86_FLOAT_STATE_COUNT for " |
| 1082 | "flavor number " + Twine(nflavor) + " which is " |
| 1083 | "a x86_FLOAT_STATE flavor in " + CmdName + |
| 1084 | " command" ); |
| 1085 | if (state + sizeof(MachO::x86_float_state_t) > end) |
| 1086 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1087 | " x86_FLOAT_STATE extends past end of " |
| 1088 | "command in " + CmdName + " command" ); |
| 1089 | state += sizeof(MachO::x86_float_state_t); |
| 1090 | } else if (flavor == MachO::x86_EXCEPTION_STATE) { |
| 1091 | if (count != MachO::x86_EXCEPTION_STATE_COUNT) |
| 1092 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1093 | " count not x86_EXCEPTION_STATE_COUNT for " |
| 1094 | "flavor number " + Twine(nflavor) + " which is " |
| 1095 | "a x86_EXCEPTION_STATE flavor in " + CmdName + |
| 1096 | " command" ); |
| 1097 | if (state + sizeof(MachO::x86_exception_state_t) > end) |
| 1098 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1099 | " x86_EXCEPTION_STATE extends past end of " |
| 1100 | "command in " + CmdName + " command" ); |
| 1101 | state += sizeof(MachO::x86_exception_state_t); |
| 1102 | } else if (flavor == MachO::x86_THREAD_STATE64) { |
| 1103 | if (count != MachO::x86_THREAD_STATE64_COUNT) |
| 1104 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1105 | " count not x86_THREAD_STATE64_COUNT for " |
| 1106 | "flavor number " + Twine(nflavor) + " which is " |
| 1107 | "a x86_THREAD_STATE64 flavor in " + CmdName + |
| 1108 | " command" ); |
| 1109 | if (state + sizeof(MachO::x86_thread_state64_t) > end) |
| 1110 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1111 | " x86_THREAD_STATE64 extends past end of " |
| 1112 | "command in " + CmdName + " command" ); |
| 1113 | state += sizeof(MachO::x86_thread_state64_t); |
| 1114 | } else if (flavor == MachO::x86_EXCEPTION_STATE64) { |
| 1115 | if (count != MachO::x86_EXCEPTION_STATE64_COUNT) |
| 1116 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1117 | " count not x86_EXCEPTION_STATE64_COUNT for " |
| 1118 | "flavor number " + Twine(nflavor) + " which is " |
| 1119 | "a x86_EXCEPTION_STATE64 flavor in " + CmdName + |
| 1120 | " command" ); |
| 1121 | if (state + sizeof(MachO::x86_exception_state64_t) > end) |
| 1122 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1123 | " x86_EXCEPTION_STATE64 extends past end of " |
| 1124 | "command in " + CmdName + " command" ); |
| 1125 | state += sizeof(MachO::x86_exception_state64_t); |
| 1126 | } else { |
| 1127 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1128 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1129 | "flavor number " + Twine(nflavor) + " in " + |
| 1130 | CmdName + " command" ); |
| 1131 | } |
| 1132 | } else if (cputype == MachO::CPU_TYPE_ARM) { |
| 1133 | if (flavor == MachO::ARM_THREAD_STATE) { |
| 1134 | if (count != MachO::ARM_THREAD_STATE_COUNT) |
| 1135 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1136 | " count not ARM_THREAD_STATE_COUNT for " |
| 1137 | "flavor number " + Twine(nflavor) + " which is " |
| 1138 | "a ARM_THREAD_STATE flavor in " + CmdName + |
| 1139 | " command" ); |
| 1140 | if (state + sizeof(MachO::arm_thread_state32_t) > end) |
| 1141 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1142 | " ARM_THREAD_STATE extends past end of " |
| 1143 | "command in " + CmdName + " command" ); |
| 1144 | state += sizeof(MachO::arm_thread_state32_t); |
| 1145 | } else { |
| 1146 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1147 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1148 | "flavor number " + Twine(nflavor) + " in " + |
| 1149 | CmdName + " command" ); |
| 1150 | } |
| 1151 | } else if (cputype == MachO::CPU_TYPE_ARM64 || |
| 1152 | cputype == MachO::CPU_TYPE_ARM64_32) { |
| 1153 | if (flavor == MachO::ARM_THREAD_STATE64) { |
| 1154 | if (count != MachO::ARM_THREAD_STATE64_COUNT) |
| 1155 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1156 | " count not ARM_THREAD_STATE64_COUNT for " |
| 1157 | "flavor number " + Twine(nflavor) + " which is " |
| 1158 | "a ARM_THREAD_STATE64 flavor in " + CmdName + |
| 1159 | " command" ); |
| 1160 | if (state + sizeof(MachO::arm_thread_state64_t) > end) |
| 1161 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1162 | " ARM_THREAD_STATE64 extends past end of " |
| 1163 | "command in " + CmdName + " command" ); |
| 1164 | state += sizeof(MachO::arm_thread_state64_t); |
| 1165 | } else { |
| 1166 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1167 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1168 | "flavor number " + Twine(nflavor) + " in " + |
| 1169 | CmdName + " command" ); |
| 1170 | } |
| 1171 | } else if (cputype == MachO::CPU_TYPE_POWERPC) { |
| 1172 | if (flavor == MachO::PPC_THREAD_STATE) { |
| 1173 | if (count != MachO::PPC_THREAD_STATE_COUNT) |
| 1174 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1175 | " count not PPC_THREAD_STATE_COUNT for " |
| 1176 | "flavor number " + Twine(nflavor) + " which is " |
| 1177 | "a PPC_THREAD_STATE flavor in " + CmdName + |
| 1178 | " command" ); |
| 1179 | if (state + sizeof(MachO::ppc_thread_state32_t) > end) |
| 1180 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1181 | " PPC_THREAD_STATE extends past end of " |
| 1182 | "command in " + CmdName + " command" ); |
| 1183 | state += sizeof(MachO::ppc_thread_state32_t); |
| 1184 | } else { |
| 1185 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1186 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1187 | "flavor number " + Twine(nflavor) + " in " + |
| 1188 | CmdName + " command" ); |
| 1189 | } |
| 1190 | } else { |
| 1191 | return malformedError(Msg: "unknown cputype (" + Twine(cputype) + ") load " |
| 1192 | "command " + Twine(LoadCommandIndex) + " for " + |
| 1193 | CmdName + " command can't be checked" ); |
| 1194 | } |
| 1195 | nflavor++; |
| 1196 | } |
| 1197 | return Error::success(); |
| 1198 | } |
| 1199 | |
| 1200 | static Error checkTwoLevelHintsCommand(const MachOObjectFile &Obj, |
| 1201 | const MachOObjectFile::LoadCommandInfo |
| 1202 | &Load, |
| 1203 | uint32_t LoadCommandIndex, |
| 1204 | const char **LoadCmd, |
| 1205 | std::list<MachOElement> &Elements) { |
| 1206 | if (Load.C.cmdsize != sizeof(MachO::twolevel_hints_command)) |
| 1207 | return malformedError(Msg: "load command " + Twine(LoadCommandIndex) + |
| 1208 | " LC_TWOLEVEL_HINTS has incorrect cmdsize" ); |
| 1209 | if (*LoadCmd != nullptr) |
| 1210 | return malformedError(Msg: "more than one LC_TWOLEVEL_HINTS command" ); |
| 1211 | auto HintsOrErr = getStructOrErr<MachO::twolevel_hints_command>(O: Obj, P: Load.Ptr); |
| 1212 | if(!HintsOrErr) |
| 1213 | return HintsOrErr.takeError(); |
| 1214 | MachO::twolevel_hints_command Hints = HintsOrErr.get(); |
| 1215 | uint64_t FileSize = Obj.getData().size(); |
| 1216 | if (Hints.offset > FileSize) |
| 1217 | return malformedError(Msg: "offset field of LC_TWOLEVEL_HINTS command " + |
| 1218 | Twine(LoadCommandIndex) + " extends past the end of " |
| 1219 | "the file" ); |
| 1220 | uint64_t BigSize = Hints.nhints; |
| 1221 | BigSize *= sizeof(MachO::twolevel_hint); |
| 1222 | BigSize += Hints.offset; |
| 1223 | if (BigSize > FileSize) |
| 1224 | return malformedError(Msg: "offset field plus nhints times sizeof(struct " |
| 1225 | "twolevel_hint) field of LC_TWOLEVEL_HINTS command " + |
| 1226 | Twine(LoadCommandIndex) + " extends past the end of " |
| 1227 | "the file" ); |
| 1228 | if (Error Err = checkOverlappingElement(Elements, Offset: Hints.offset, Size: Hints.nhints * |
| 1229 | sizeof(MachO::twolevel_hint), |
| 1230 | Name: "two level hints" )) |
| 1231 | return Err; |
| 1232 | *LoadCmd = Load.Ptr; |
| 1233 | return Error::success(); |
| 1234 | } |
| 1235 | |
| 1236 | // Returns true if the libObject code does not support the load command and its |
| 1237 | // contents. The cmd value it is treated as an unknown load command but with |
| 1238 | // an error message that says the cmd value is obsolete. |
| 1239 | static bool isLoadCommandObsolete(uint32_t cmd) { |
| 1240 | if (cmd == MachO::LC_SYMSEG || |
| 1241 | cmd == MachO::LC_LOADFVMLIB || |
| 1242 | cmd == MachO::LC_IDFVMLIB || |
| 1243 | cmd == MachO::LC_IDENT || |
| 1244 | cmd == MachO::LC_FVMFILE || |
| 1245 | cmd == MachO::LC_PREPAGE || |
| 1246 | cmd == MachO::LC_PREBOUND_DYLIB || |
| 1247 | cmd == MachO::LC_TWOLEVEL_HINTS || |
| 1248 | cmd == MachO::LC_PREBIND_CKSUM) |
| 1249 | return true; |
| 1250 | return false; |
| 1251 | } |
| 1252 | |
| 1253 | Expected<std::unique_ptr<MachOObjectFile>> |
| 1254 | MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian, |
| 1255 | bool Is64Bits, uint32_t UniversalCputype, |
| 1256 | uint32_t UniversalIndex, |
| 1257 | size_t MachOFilesetEntryOffset) { |
| 1258 | Error Err = Error::success(); |
| 1259 | std::unique_ptr<MachOObjectFile> Obj(new MachOObjectFile( |
| 1260 | std::move(Object), IsLittleEndian, Is64Bits, Err, UniversalCputype, |
| 1261 | UniversalIndex, MachOFilesetEntryOffset)); |
| 1262 | if (Err) |
| 1263 | return std::move(Err); |
| 1264 | return std::move(Obj); |
| 1265 | } |
| 1266 | |
| 1267 | MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, |
| 1268 | bool Is64bits, Error &Err, |
| 1269 | uint32_t UniversalCputype, |
| 1270 | uint32_t UniversalIndex, |
| 1271 | size_t MachOFilesetEntryOffset) |
| 1272 | : ObjectFile(getMachOType(isLE: IsLittleEndian, is64Bits: Is64bits), Object), |
| 1273 | MachOFilesetEntryOffset(MachOFilesetEntryOffset) { |
| 1274 | ErrorAsOutParameter ErrAsOutParam(Err); |
| 1275 | uint64_t ; |
| 1276 | uint32_t cputype; |
| 1277 | if (is64Bit()) { |
| 1278 | parseHeader(Obj: *this, Header&: Header64, Err); |
| 1279 | SizeOfHeaders = sizeof(MachO::mach_header_64); |
| 1280 | cputype = Header64.cputype; |
| 1281 | } else { |
| 1282 | parseHeader(Obj: *this, Header&: Header, Err); |
| 1283 | SizeOfHeaders = sizeof(MachO::mach_header); |
| 1284 | cputype = Header.cputype; |
| 1285 | } |
| 1286 | if (Err) |
| 1287 | return; |
| 1288 | SizeOfHeaders += getHeader().sizeofcmds; |
| 1289 | if (getData().data() + SizeOfHeaders > getData().end()) { |
| 1290 | Err = malformedError(Msg: "load commands extend past the end of the file" ); |
| 1291 | return; |
| 1292 | } |
| 1293 | if (UniversalCputype != 0 && cputype != UniversalCputype) { |
| 1294 | Err = malformedError(Msg: "universal header architecture: " + |
| 1295 | Twine(UniversalIndex) + "'s cputype does not match " |
| 1296 | "object file's mach header" ); |
| 1297 | return; |
| 1298 | } |
| 1299 | std::list<MachOElement> Elements; |
| 1300 | Elements.push_back(x: {.Offset: 0, .Size: SizeOfHeaders, .Name: "Mach-O headers" }); |
| 1301 | |
| 1302 | uint32_t LoadCommandCount = getHeader().ncmds; |
| 1303 | LoadCommandInfo Load; |
| 1304 | if (LoadCommandCount != 0) { |
| 1305 | if (auto LoadOrErr = getFirstLoadCommandInfo(Obj: *this)) |
| 1306 | Load = *LoadOrErr; |
| 1307 | else { |
| 1308 | Err = LoadOrErr.takeError(); |
| 1309 | return; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | const char *DyldIdLoadCmd = nullptr; |
| 1314 | const char *SplitInfoLoadCmd = nullptr; |
| 1315 | const char *CodeSignDrsLoadCmd = nullptr; |
| 1316 | const char *CodeSignLoadCmd = nullptr; |
| 1317 | const char *VersLoadCmd = nullptr; |
| 1318 | const char *SourceLoadCmd = nullptr; |
| 1319 | const char *EntryPointLoadCmd = nullptr; |
| 1320 | const char *EncryptLoadCmd = nullptr; |
| 1321 | const char *RoutinesLoadCmd = nullptr; |
| 1322 | const char *UnixThreadLoadCmd = nullptr; |
| 1323 | const char *TwoLevelHintsLoadCmd = nullptr; |
| 1324 | for (unsigned I = 0; I < LoadCommandCount; ++I) { |
| 1325 | if (is64Bit()) { |
| 1326 | if (Load.C.cmdsize % 8 != 0) { |
| 1327 | // We have a hack here to allow 64-bit Mach-O core files to have |
| 1328 | // LC_THREAD commands that are only a multiple of 4 and not 8 to be |
| 1329 | // allowed since the macOS kernel produces them. |
| 1330 | if (getHeader().filetype != MachO::MH_CORE || |
| 1331 | Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) { |
| 1332 | Err = malformedError(Msg: "load command " + Twine(I) + " cmdsize not a " |
| 1333 | "multiple of 8" ); |
| 1334 | return; |
| 1335 | } |
| 1336 | } |
| 1337 | } else { |
| 1338 | if (Load.C.cmdsize % 4 != 0) { |
| 1339 | Err = malformedError(Msg: "load command " + Twine(I) + " cmdsize not a " |
| 1340 | "multiple of 4" ); |
| 1341 | return; |
| 1342 | } |
| 1343 | } |
| 1344 | LoadCommands.push_back(Elt: Load); |
| 1345 | if (Load.C.cmd == MachO::LC_SYMTAB) { |
| 1346 | if ((Err = checkSymtabCommand(Obj: *this, Load, LoadCommandIndex: I, SymtabLoadCmd: &SymtabLoadCmd, Elements))) |
| 1347 | return; |
| 1348 | } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { |
| 1349 | if ((Err = checkDysymtabCommand(Obj: *this, Load, LoadCommandIndex: I, DysymtabLoadCmd: &DysymtabLoadCmd, |
| 1350 | Elements))) |
| 1351 | return; |
| 1352 | } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { |
| 1353 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DataInCodeLoadCmd, |
| 1354 | CmdName: "LC_DATA_IN_CODE" , Elements, |
| 1355 | ElementName: "data in code info" ))) |
| 1356 | return; |
| 1357 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
| 1358 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &LinkOptHintsLoadCmd, |
| 1359 | CmdName: "LC_LINKER_OPTIMIZATION_HINT" , |
| 1360 | Elements, ElementName: "linker optimization " |
| 1361 | "hints" ))) |
| 1362 | return; |
| 1363 | } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) { |
| 1364 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &FuncStartsLoadCmd, |
| 1365 | CmdName: "LC_FUNCTION_STARTS" , Elements, |
| 1366 | ElementName: "function starts data" ))) |
| 1367 | return; |
| 1368 | } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) { |
| 1369 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &SplitInfoLoadCmd, |
| 1370 | CmdName: "LC_SEGMENT_SPLIT_INFO" , Elements, |
| 1371 | ElementName: "split info data" ))) |
| 1372 | return; |
| 1373 | } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) { |
| 1374 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &CodeSignDrsLoadCmd, |
| 1375 | CmdName: "LC_DYLIB_CODE_SIGN_DRS" , Elements, |
| 1376 | ElementName: "code signing RDs data" ))) |
| 1377 | return; |
| 1378 | } else if (Load.C.cmd == MachO::LC_CODE_SIGNATURE) { |
| 1379 | if ((Err = checkLinkeditDataCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &CodeSignLoadCmd, |
| 1380 | CmdName: "LC_CODE_SIGNATURE" , Elements, |
| 1381 | ElementName: "code signature data" ))) |
| 1382 | return; |
| 1383 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO) { |
| 1384 | if ((Err = checkDyldInfoCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DyldInfoLoadCmd, |
| 1385 | CmdName: "LC_DYLD_INFO" , Elements))) |
| 1386 | return; |
| 1387 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
| 1388 | if ((Err = checkDyldInfoCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DyldInfoLoadCmd, |
| 1389 | CmdName: "LC_DYLD_INFO_ONLY" , Elements))) |
| 1390 | return; |
| 1391 | } else if (Load.C.cmd == MachO::LC_DYLD_CHAINED_FIXUPS) { |
| 1392 | if ((Err = checkLinkeditDataCommand( |
| 1393 | Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DyldChainedFixupsLoadCmd, |
| 1394 | CmdName: "LC_DYLD_CHAINED_FIXUPS" , Elements, ElementName: "chained fixups" ))) |
| 1395 | return; |
| 1396 | } else if (Load.C.cmd == MachO::LC_DYLD_EXPORTS_TRIE) { |
| 1397 | if ((Err = checkLinkeditDataCommand( |
| 1398 | Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DyldExportsTrieLoadCmd, CmdName: "LC_DYLD_EXPORTS_TRIE" , |
| 1399 | Elements, ElementName: "exports trie" ))) |
| 1400 | return; |
| 1401 | } else if (Load.C.cmd == MachO::LC_UUID) { |
| 1402 | if (Load.C.cmdsize != sizeof(MachO::uuid_command)) { |
| 1403 | Err = malformedError(Msg: "LC_UUID command " + Twine(I) + " has incorrect " |
| 1404 | "cmdsize" ); |
| 1405 | return; |
| 1406 | } |
| 1407 | if (UuidLoadCmd) { |
| 1408 | Err = malformedError(Msg: "more than one LC_UUID command" ); |
| 1409 | return; |
| 1410 | } |
| 1411 | UuidLoadCmd = Load.Ptr; |
| 1412 | } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 1413 | if ((Err = parseSegmentLoadCommand<MachO::segment_command_64, |
| 1414 | MachO::section_64>( |
| 1415 | Obj: *this, Load, Sections, IsPageZeroSegment&: HasPageZeroSegment, LoadCommandIndex: I, |
| 1416 | CmdName: "LC_SEGMENT_64" , SizeOfHeaders, Elements))) |
| 1417 | return; |
| 1418 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
| 1419 | if ((Err = parseSegmentLoadCommand<MachO::segment_command, |
| 1420 | MachO::section>( |
| 1421 | Obj: *this, Load, Sections, IsPageZeroSegment&: HasPageZeroSegment, LoadCommandIndex: I, |
| 1422 | CmdName: "LC_SEGMENT" , SizeOfHeaders, Elements))) |
| 1423 | return; |
| 1424 | } else if (Load.C.cmd == MachO::LC_ID_DYLIB) { |
| 1425 | if ((Err = checkDylibIdCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &DyldIdLoadCmd))) |
| 1426 | return; |
| 1427 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) { |
| 1428 | if ((Err = checkDylibCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_LOAD_DYLIB" ))) |
| 1429 | return; |
| 1430 | Libraries.push_back(Elt: Load.Ptr); |
| 1431 | } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) { |
| 1432 | if ((Err = checkDylibCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_LOAD_WEAK_DYLIB" ))) |
| 1433 | return; |
| 1434 | Libraries.push_back(Elt: Load.Ptr); |
| 1435 | } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) { |
| 1436 | if ((Err = checkDylibCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_LAZY_LOAD_DYLIB" ))) |
| 1437 | return; |
| 1438 | Libraries.push_back(Elt: Load.Ptr); |
| 1439 | } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) { |
| 1440 | if ((Err = checkDylibCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_REEXPORT_DYLIB" ))) |
| 1441 | return; |
| 1442 | Libraries.push_back(Elt: Load.Ptr); |
| 1443 | } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
| 1444 | if ((Err = checkDylibCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_LOAD_UPWARD_DYLIB" ))) |
| 1445 | return; |
| 1446 | Libraries.push_back(Elt: Load.Ptr); |
| 1447 | } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) { |
| 1448 | if ((Err = checkDyldCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_ID_DYLINKER" ))) |
| 1449 | return; |
| 1450 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) { |
| 1451 | if ((Err = checkDyldCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_LOAD_DYLINKER" ))) |
| 1452 | return; |
| 1453 | } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
| 1454 | if ((Err = checkDyldCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_DYLD_ENVIRONMENT" ))) |
| 1455 | return; |
| 1456 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) { |
| 1457 | if ((Err = checkVersCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &VersLoadCmd, |
| 1458 | CmdName: "LC_VERSION_MIN_MACOSX" ))) |
| 1459 | return; |
| 1460 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) { |
| 1461 | if ((Err = checkVersCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &VersLoadCmd, |
| 1462 | CmdName: "LC_VERSION_MIN_IPHONEOS" ))) |
| 1463 | return; |
| 1464 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) { |
| 1465 | if ((Err = checkVersCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &VersLoadCmd, |
| 1466 | CmdName: "LC_VERSION_MIN_TVOS" ))) |
| 1467 | return; |
| 1468 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { |
| 1469 | if ((Err = checkVersCommand(Obj: *this, Load, LoadCommandIndex: I, LoadCmd: &VersLoadCmd, |
| 1470 | CmdName: "LC_VERSION_MIN_WATCHOS" ))) |
| 1471 | return; |
| 1472 | } else if (Load.C.cmd == MachO::LC_NOTE) { |
| 1473 | if ((Err = checkNoteCommand(Obj: *this, Load, LoadCommandIndex: I, Elements))) |
| 1474 | return; |
| 1475 | } else if (Load.C.cmd == MachO::LC_BUILD_VERSION) { |
| 1476 | if ((Err = parseBuildVersionCommand(Obj: *this, Load, BuildTools, LoadCommandIndex: I))) |
| 1477 | return; |
| 1478 | } else if (Load.C.cmd == MachO::LC_RPATH) { |
| 1479 | if ((Err = checkRpathCommand(Obj: *this, Load, LoadCommandIndex: I))) |
| 1480 | return; |
| 1481 | } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) { |
| 1482 | if (Load.C.cmdsize != sizeof(MachO::source_version_command)) { |
| 1483 | Err = malformedError(Msg: "LC_SOURCE_VERSION command " + Twine(I) + |
| 1484 | " has incorrect cmdsize" ); |
| 1485 | return; |
| 1486 | } |
| 1487 | if (SourceLoadCmd) { |
| 1488 | Err = malformedError(Msg: "more than one LC_SOURCE_VERSION command" ); |
| 1489 | return; |
| 1490 | } |
| 1491 | SourceLoadCmd = Load.Ptr; |
| 1492 | } else if (Load.C.cmd == MachO::LC_MAIN) { |
| 1493 | if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) { |
| 1494 | Err = malformedError(Msg: "LC_MAIN command " + Twine(I) + |
| 1495 | " has incorrect cmdsize" ); |
| 1496 | return; |
| 1497 | } |
| 1498 | if (EntryPointLoadCmd) { |
| 1499 | Err = malformedError(Msg: "more than one LC_MAIN command" ); |
| 1500 | return; |
| 1501 | } |
| 1502 | EntryPointLoadCmd = Load.Ptr; |
| 1503 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) { |
| 1504 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) { |
| 1505 | Err = malformedError(Msg: "LC_ENCRYPTION_INFO command " + Twine(I) + |
| 1506 | " has incorrect cmdsize" ); |
| 1507 | return; |
| 1508 | } |
| 1509 | MachO::encryption_info_command E = |
| 1510 | getStruct<MachO::encryption_info_command>(O: *this, P: Load.Ptr); |
| 1511 | if ((Err = checkEncryptCommand(Obj: *this, Load, LoadCommandIndex: I, cryptoff: E.cryptoff, cryptsize: E.cryptsize, |
| 1512 | LoadCmd: &EncryptLoadCmd, CmdName: "LC_ENCRYPTION_INFO" ))) |
| 1513 | return; |
| 1514 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { |
| 1515 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) { |
| 1516 | Err = malformedError(Msg: "LC_ENCRYPTION_INFO_64 command " + Twine(I) + |
| 1517 | " has incorrect cmdsize" ); |
| 1518 | return; |
| 1519 | } |
| 1520 | MachO::encryption_info_command_64 E = |
| 1521 | getStruct<MachO::encryption_info_command_64>(O: *this, P: Load.Ptr); |
| 1522 | if ((Err = checkEncryptCommand(Obj: *this, Load, LoadCommandIndex: I, cryptoff: E.cryptoff, cryptsize: E.cryptsize, |
| 1523 | LoadCmd: &EncryptLoadCmd, CmdName: "LC_ENCRYPTION_INFO_64" ))) |
| 1524 | return; |
| 1525 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) { |
| 1526 | if ((Err = checkLinkerOptCommand(Obj: *this, Load, LoadCommandIndex: I))) |
| 1527 | return; |
| 1528 | } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) { |
| 1529 | if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) { |
| 1530 | Err = malformedError(Msg: "load command " + Twine(I) + |
| 1531 | " LC_SUB_FRAMEWORK cmdsize too small" ); |
| 1532 | return; |
| 1533 | } |
| 1534 | MachO::sub_framework_command S = |
| 1535 | getStruct<MachO::sub_framework_command>(O: *this, P: Load.Ptr); |
| 1536 | if ((Err = checkSubCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_SUB_FRAMEWORK" , |
| 1537 | SizeOfCmd: sizeof(MachO::sub_framework_command), |
| 1538 | CmdStructName: "sub_framework_command" , PathOffset: S.umbrella, |
| 1539 | PathFieldName: "umbrella" ))) |
| 1540 | return; |
| 1541 | } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) { |
| 1542 | if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) { |
| 1543 | Err = malformedError(Msg: "load command " + Twine(I) + |
| 1544 | " LC_SUB_UMBRELLA cmdsize too small" ); |
| 1545 | return; |
| 1546 | } |
| 1547 | MachO::sub_umbrella_command S = |
| 1548 | getStruct<MachO::sub_umbrella_command>(O: *this, P: Load.Ptr); |
| 1549 | if ((Err = checkSubCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_SUB_UMBRELLA" , |
| 1550 | SizeOfCmd: sizeof(MachO::sub_umbrella_command), |
| 1551 | CmdStructName: "sub_umbrella_command" , PathOffset: S.sub_umbrella, |
| 1552 | PathFieldName: "sub_umbrella" ))) |
| 1553 | return; |
| 1554 | } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) { |
| 1555 | if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) { |
| 1556 | Err = malformedError(Msg: "load command " + Twine(I) + |
| 1557 | " LC_SUB_LIBRARY cmdsize too small" ); |
| 1558 | return; |
| 1559 | } |
| 1560 | MachO::sub_library_command S = |
| 1561 | getStruct<MachO::sub_library_command>(O: *this, P: Load.Ptr); |
| 1562 | if ((Err = checkSubCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_SUB_LIBRARY" , |
| 1563 | SizeOfCmd: sizeof(MachO::sub_library_command), |
| 1564 | CmdStructName: "sub_library_command" , PathOffset: S.sub_library, |
| 1565 | PathFieldName: "sub_library" ))) |
| 1566 | return; |
| 1567 | } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) { |
| 1568 | if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) { |
| 1569 | Err = malformedError(Msg: "load command " + Twine(I) + |
| 1570 | " LC_SUB_CLIENT cmdsize too small" ); |
| 1571 | return; |
| 1572 | } |
| 1573 | MachO::sub_client_command S = |
| 1574 | getStruct<MachO::sub_client_command>(O: *this, P: Load.Ptr); |
| 1575 | if ((Err = checkSubCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_SUB_CLIENT" , |
| 1576 | SizeOfCmd: sizeof(MachO::sub_client_command), |
| 1577 | CmdStructName: "sub_client_command" , PathOffset: S.client, PathFieldName: "client" ))) |
| 1578 | return; |
| 1579 | } else if (Load.C.cmd == MachO::LC_ROUTINES) { |
| 1580 | if (Load.C.cmdsize != sizeof(MachO::routines_command)) { |
| 1581 | Err = malformedError(Msg: "LC_ROUTINES command " + Twine(I) + |
| 1582 | " has incorrect cmdsize" ); |
| 1583 | return; |
| 1584 | } |
| 1585 | if (RoutinesLoadCmd) { |
| 1586 | Err = malformedError(Msg: "more than one LC_ROUTINES and or LC_ROUTINES_64 " |
| 1587 | "command" ); |
| 1588 | return; |
| 1589 | } |
| 1590 | RoutinesLoadCmd = Load.Ptr; |
| 1591 | } else if (Load.C.cmd == MachO::LC_ROUTINES_64) { |
| 1592 | if (Load.C.cmdsize != sizeof(MachO::routines_command_64)) { |
| 1593 | Err = malformedError(Msg: "LC_ROUTINES_64 command " + Twine(I) + |
| 1594 | " has incorrect cmdsize" ); |
| 1595 | return; |
| 1596 | } |
| 1597 | if (RoutinesLoadCmd) { |
| 1598 | Err = malformedError(Msg: "more than one LC_ROUTINES_64 and or LC_ROUTINES " |
| 1599 | "command" ); |
| 1600 | return; |
| 1601 | } |
| 1602 | RoutinesLoadCmd = Load.Ptr; |
| 1603 | } else if (Load.C.cmd == MachO::LC_UNIXTHREAD) { |
| 1604 | if ((Err = checkThreadCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_UNIXTHREAD" ))) |
| 1605 | return; |
| 1606 | if (UnixThreadLoadCmd) { |
| 1607 | Err = malformedError(Msg: "more than one LC_UNIXTHREAD command" ); |
| 1608 | return; |
| 1609 | } |
| 1610 | UnixThreadLoadCmd = Load.Ptr; |
| 1611 | } else if (Load.C.cmd == MachO::LC_THREAD) { |
| 1612 | if ((Err = checkThreadCommand(Obj: *this, Load, LoadCommandIndex: I, CmdName: "LC_THREAD" ))) |
| 1613 | return; |
| 1614 | // Note: LC_TWOLEVEL_HINTS is really obsolete and is not supported. |
| 1615 | } else if (Load.C.cmd == MachO::LC_TWOLEVEL_HINTS) { |
| 1616 | if ((Err = checkTwoLevelHintsCommand(Obj: *this, Load, LoadCommandIndex: I, |
| 1617 | LoadCmd: &TwoLevelHintsLoadCmd, Elements))) |
| 1618 | return; |
| 1619 | } else if (Load.C.cmd == MachO::LC_IDENT) { |
| 1620 | // Note: LC_IDENT is ignored. |
| 1621 | continue; |
| 1622 | } else if (isLoadCommandObsolete(cmd: Load.C.cmd)) { |
| 1623 | Err = malformedError(Msg: "load command " + Twine(I) + " for cmd value of: " + |
| 1624 | Twine(Load.C.cmd) + " is obsolete and not " |
| 1625 | "supported" ); |
| 1626 | return; |
| 1627 | } |
| 1628 | // TODO: generate a error for unknown load commands by default. But still |
| 1629 | // need work out an approach to allow or not allow unknown values like this |
| 1630 | // as an option for some uses like lldb. |
| 1631 | if (I < LoadCommandCount - 1) { |
| 1632 | if (auto LoadOrErr = getNextLoadCommandInfo(Obj: *this, LoadCommandIndex: I, L: Load)) |
| 1633 | Load = *LoadOrErr; |
| 1634 | else { |
| 1635 | Err = LoadOrErr.takeError(); |
| 1636 | return; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | if (!SymtabLoadCmd) { |
| 1641 | if (DysymtabLoadCmd) { |
| 1642 | Err = malformedError(Msg: "contains LC_DYSYMTAB load command without a " |
| 1643 | "LC_SYMTAB load command" ); |
| 1644 | return; |
| 1645 | } |
| 1646 | } else if (DysymtabLoadCmd) { |
| 1647 | MachO::symtab_command Symtab = |
| 1648 | getStruct<MachO::symtab_command>(O: *this, P: SymtabLoadCmd); |
| 1649 | MachO::dysymtab_command Dysymtab = |
| 1650 | getStruct<MachO::dysymtab_command>(O: *this, P: DysymtabLoadCmd); |
| 1651 | if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
| 1652 | Err = malformedError(Msg: "ilocalsym in LC_DYSYMTAB load command " |
| 1653 | "extends past the end of the symbol table" ); |
| 1654 | return; |
| 1655 | } |
| 1656 | uint64_t BigSize = Dysymtab.ilocalsym; |
| 1657 | BigSize += Dysymtab.nlocalsym; |
| 1658 | if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { |
| 1659 | Err = malformedError(Msg: "ilocalsym plus nlocalsym in LC_DYSYMTAB load " |
| 1660 | "command extends past the end of the symbol table" ); |
| 1661 | return; |
| 1662 | } |
| 1663 | if (Dysymtab.nextdefsym != 0 && Dysymtab.iextdefsym > Symtab.nsyms) { |
| 1664 | Err = malformedError(Msg: "iextdefsym in LC_DYSYMTAB load command " |
| 1665 | "extends past the end of the symbol table" ); |
| 1666 | return; |
| 1667 | } |
| 1668 | BigSize = Dysymtab.iextdefsym; |
| 1669 | BigSize += Dysymtab.nextdefsym; |
| 1670 | if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { |
| 1671 | Err = malformedError(Msg: "iextdefsym plus nextdefsym in LC_DYSYMTAB " |
| 1672 | "load command extends past the end of the symbol " |
| 1673 | "table" ); |
| 1674 | return; |
| 1675 | } |
| 1676 | if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { |
| 1677 | Err = malformedError(Msg: "iundefsym in LC_DYSYMTAB load command " |
| 1678 | "extends past the end of the symbol table" ); |
| 1679 | return; |
| 1680 | } |
| 1681 | BigSize = Dysymtab.iundefsym; |
| 1682 | BigSize += Dysymtab.nundefsym; |
| 1683 | if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { |
| 1684 | Err = malformedError(Msg: "iundefsym plus nundefsym in LC_DYSYMTAB load " |
| 1685 | " command extends past the end of the symbol table" ); |
| 1686 | return; |
| 1687 | } |
| 1688 | } |
| 1689 | if ((getHeader().filetype == MachO::MH_DYLIB || |
| 1690 | getHeader().filetype == MachO::MH_DYLIB_STUB) && |
| 1691 | DyldIdLoadCmd == nullptr) { |
| 1692 | Err = malformedError(Msg: "no LC_ID_DYLIB load command in dynamic library " |
| 1693 | "filetype" ); |
| 1694 | return; |
| 1695 | } |
| 1696 | assert(LoadCommands.size() == LoadCommandCount); |
| 1697 | |
| 1698 | Err = Error::success(); |
| 1699 | } |
| 1700 | |
| 1701 | Error MachOObjectFile::checkSymbolTable() const { |
| 1702 | uint32_t Flags = 0; |
| 1703 | if (is64Bit()) { |
| 1704 | MachO::mach_header_64 H_64 = MachOObjectFile::getHeader64(); |
| 1705 | Flags = H_64.flags; |
| 1706 | } else { |
| 1707 | MachO::mach_header H = MachOObjectFile::getHeader(); |
| 1708 | Flags = H.flags; |
| 1709 | } |
| 1710 | uint8_t NType = 0; |
| 1711 | uint8_t NSect = 0; |
| 1712 | uint16_t NDesc = 0; |
| 1713 | uint32_t NStrx = 0; |
| 1714 | uint64_t NValue = 0; |
| 1715 | uint32_t SymbolIndex = 0; |
| 1716 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 1717 | for (const SymbolRef &Symbol : symbols()) { |
| 1718 | DataRefImpl SymDRI = Symbol.getRawDataRefImpl(); |
| 1719 | if (is64Bit()) { |
| 1720 | MachO::nlist_64 STE_64 = getSymbol64TableEntry(DRI: SymDRI); |
| 1721 | NType = STE_64.n_type; |
| 1722 | NSect = STE_64.n_sect; |
| 1723 | NDesc = STE_64.n_desc; |
| 1724 | NStrx = STE_64.n_strx; |
| 1725 | NValue = STE_64.n_value; |
| 1726 | } else { |
| 1727 | MachO::nlist STE = getSymbolTableEntry(DRI: SymDRI); |
| 1728 | NType = STE.n_type; |
| 1729 | NSect = STE.n_sect; |
| 1730 | NDesc = STE.n_desc; |
| 1731 | NStrx = STE.n_strx; |
| 1732 | NValue = STE.n_value; |
| 1733 | } |
| 1734 | if ((NType & MachO::N_STAB) == 0) { |
| 1735 | if ((NType & MachO::N_TYPE) == MachO::N_SECT) { |
| 1736 | if (NSect == 0 || NSect > Sections.size()) |
| 1737 | return malformedError(Msg: "bad section index: " + Twine((int)NSect) + |
| 1738 | " for symbol at index " + Twine(SymbolIndex)); |
| 1739 | } |
| 1740 | if ((NType & MachO::N_TYPE) == MachO::N_INDR) { |
| 1741 | if (NValue >= S.strsize) |
| 1742 | return malformedError(Msg: "bad n_value: " + Twine((int)NValue) + " past " |
| 1743 | "the end of string table, for N_INDR symbol at " |
| 1744 | "index " + Twine(SymbolIndex)); |
| 1745 | } |
| 1746 | if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL && |
| 1747 | (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) || |
| 1748 | (NType & MachO::N_TYPE) == MachO::N_PBUD)) { |
| 1749 | uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(n_desc: NDesc); |
| 1750 | if (LibraryOrdinal != 0 && |
| 1751 | LibraryOrdinal != MachO::EXECUTABLE_ORDINAL && |
| 1752 | LibraryOrdinal != MachO::DYNAMIC_LOOKUP_ORDINAL && |
| 1753 | LibraryOrdinal - 1 >= Libraries.size() ) { |
| 1754 | return malformedError(Msg: "bad library ordinal: " + Twine(LibraryOrdinal) + |
| 1755 | " for symbol at index " + Twine(SymbolIndex)); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | if (NStrx >= S.strsize) |
| 1760 | return malformedError(Msg: "bad string table index: " + Twine((int)NStrx) + |
| 1761 | " past the end of string table, for symbol at " |
| 1762 | "index " + Twine(SymbolIndex)); |
| 1763 | SymbolIndex++; |
| 1764 | } |
| 1765 | return Error::success(); |
| 1766 | } |
| 1767 | |
| 1768 | void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
| 1769 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 1770 | sizeof(MachO::nlist_64) : |
| 1771 | sizeof(MachO::nlist); |
| 1772 | Symb.p += SymbolTableEntrySize; |
| 1773 | } |
| 1774 | |
| 1775 | Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const { |
| 1776 | StringRef StringTable = getStringTableData(); |
| 1777 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI: Symb); |
| 1778 | if (Entry.n_strx == 0) |
| 1779 | // A n_strx value of 0 indicates that no name is associated with a |
| 1780 | // particular symbol table entry. |
| 1781 | return StringRef(); |
| 1782 | const char *Start = &StringTable.data()[Entry.n_strx]; |
| 1783 | if (Start < getData().begin() || Start >= getData().end()) { |
| 1784 | return malformedError(Msg: "bad string index: " + Twine(Entry.n_strx) + |
| 1785 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
| 1786 | } |
| 1787 | return StringRef(Start); |
| 1788 | } |
| 1789 | |
| 1790 | unsigned MachOObjectFile::getSectionType(SectionRef Sec) const { |
| 1791 | DataRefImpl DRI = Sec.getRawDataRefImpl(); |
| 1792 | uint32_t Flags = getSectionFlags(O: *this, Sec: DRI); |
| 1793 | return Flags & MachO::SECTION_TYPE; |
| 1794 | } |
| 1795 | |
| 1796 | uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const { |
| 1797 | if (is64Bit()) { |
| 1798 | MachO::nlist_64 Entry = getSymbol64TableEntry(DRI: Sym); |
| 1799 | return Entry.n_value; |
| 1800 | } |
| 1801 | MachO::nlist Entry = getSymbolTableEntry(DRI: Sym); |
| 1802 | return Entry.n_value; |
| 1803 | } |
| 1804 | |
| 1805 | // getIndirectName() returns the name of the alias'ed symbol who's string table |
| 1806 | // index is in the n_value field. |
| 1807 | std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, |
| 1808 | StringRef &Res) const { |
| 1809 | StringRef StringTable = getStringTableData(); |
| 1810 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI: Symb); |
| 1811 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 1812 | return object_error::parse_failed; |
| 1813 | uint64_t NValue = getNValue(Sym: Symb); |
| 1814 | if (NValue >= StringTable.size()) |
| 1815 | return object_error::parse_failed; |
| 1816 | const char *Start = &StringTable.data()[NValue]; |
| 1817 | Res = StringRef(Start); |
| 1818 | return std::error_code(); |
| 1819 | } |
| 1820 | |
| 1821 | uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const { |
| 1822 | return getNValue(Sym); |
| 1823 | } |
| 1824 | |
| 1825 | Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const { |
| 1826 | return getSymbolValue(Symb: Sym); |
| 1827 | } |
| 1828 | |
| 1829 | uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const { |
| 1830 | uint32_t Flags = cantFail(ValOrErr: getSymbolFlags(Symb: DRI)); |
| 1831 | if (Flags & SymbolRef::SF_Common) { |
| 1832 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI); |
| 1833 | return 1 << MachO::GET_COMM_ALIGN(n_desc: Entry.n_desc); |
| 1834 | } |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const { |
| 1839 | return getNValue(Sym: DRI); |
| 1840 | } |
| 1841 | |
| 1842 | Expected<SymbolRef::Type> |
| 1843 | MachOObjectFile::getSymbolType(DataRefImpl Symb) const { |
| 1844 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI: Symb); |
| 1845 | uint8_t n_type = Entry.n_type; |
| 1846 | |
| 1847 | // If this is a STAB debugging symbol, we can do nothing more. |
| 1848 | if (n_type & MachO::N_STAB) |
| 1849 | return SymbolRef::ST_Debug; |
| 1850 | |
| 1851 | switch (n_type & MachO::N_TYPE) { |
| 1852 | case MachO::N_UNDF : |
| 1853 | return SymbolRef::ST_Unknown; |
| 1854 | case MachO::N_SECT : |
| 1855 | Expected<section_iterator> SecOrError = getSymbolSection(Symb); |
| 1856 | if (!SecOrError) |
| 1857 | return SecOrError.takeError(); |
| 1858 | section_iterator Sec = *SecOrError; |
| 1859 | if (Sec == section_end()) |
| 1860 | return SymbolRef::ST_Other; |
| 1861 | if (Sec->isData() || Sec->isBSS()) |
| 1862 | return SymbolRef::ST_Data; |
| 1863 | return SymbolRef::ST_Function; |
| 1864 | } |
| 1865 | return SymbolRef::ST_Other; |
| 1866 | } |
| 1867 | |
| 1868 | Expected<uint32_t> MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { |
| 1869 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI); |
| 1870 | |
| 1871 | uint8_t MachOType = Entry.n_type; |
| 1872 | uint16_t MachOFlags = Entry.n_desc; |
| 1873 | |
| 1874 | uint32_t Result = SymbolRef::SF_None; |
| 1875 | |
| 1876 | if ((MachOType & MachO::N_TYPE) == MachO::N_INDR) |
| 1877 | Result |= SymbolRef::SF_Indirect; |
| 1878 | |
| 1879 | if (MachOType & MachO::N_STAB) |
| 1880 | Result |= SymbolRef::SF_FormatSpecific; |
| 1881 | |
| 1882 | if (MachOType & MachO::N_EXT) { |
| 1883 | Result |= SymbolRef::SF_Global; |
| 1884 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { |
| 1885 | if (getNValue(Sym: DRI)) |
| 1886 | Result |= SymbolRef::SF_Common; |
| 1887 | else |
| 1888 | Result |= SymbolRef::SF_Undefined; |
| 1889 | } |
| 1890 | |
| 1891 | if (MachOType & MachO::N_PEXT) |
| 1892 | Result |= SymbolRef::SF_Hidden; |
| 1893 | else |
| 1894 | Result |= SymbolRef::SF_Exported; |
| 1895 | |
| 1896 | } else if (MachOType & MachO::N_PEXT) |
| 1897 | Result |= SymbolRef::SF_Hidden; |
| 1898 | |
| 1899 | if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) |
| 1900 | Result |= SymbolRef::SF_Weak; |
| 1901 | |
| 1902 | if (MachOFlags & (MachO::N_ARM_THUMB_DEF)) |
| 1903 | Result |= SymbolRef::SF_Thumb; |
| 1904 | |
| 1905 | if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) |
| 1906 | Result |= SymbolRef::SF_Absolute; |
| 1907 | |
| 1908 | return Result; |
| 1909 | } |
| 1910 | |
| 1911 | Expected<section_iterator> |
| 1912 | MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { |
| 1913 | MachO::nlist_base Entry = getSymbolTableEntryBase(O: *this, DRI: Symb); |
| 1914 | uint8_t index = Entry.n_sect; |
| 1915 | |
| 1916 | if (index == 0) |
| 1917 | return section_end(); |
| 1918 | DataRefImpl DRI; |
| 1919 | DRI.d.a = index - 1; |
| 1920 | if (DRI.d.a >= Sections.size()){ |
| 1921 | return malformedError(Msg: "bad section index: " + Twine((int)index) + |
| 1922 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
| 1923 | } |
| 1924 | return section_iterator(SectionRef(DRI, this)); |
| 1925 | } |
| 1926 | |
| 1927 | unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const { |
| 1928 | MachO::nlist_base Entry = |
| 1929 | getSymbolTableEntryBase(O: *this, DRI: Sym.getRawDataRefImpl()); |
| 1930 | return Entry.n_sect - 1; |
| 1931 | } |
| 1932 | |
| 1933 | void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
| 1934 | Sec.d.a++; |
| 1935 | } |
| 1936 | |
| 1937 | Expected<StringRef> MachOObjectFile::getSectionName(DataRefImpl Sec) const { |
| 1938 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 1939 | return parseSegmentOrSectionName(P: Raw.data()); |
| 1940 | } |
| 1941 | |
| 1942 | uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const { |
| 1943 | if (is64Bit()) |
| 1944 | return getSection64(DRI: Sec).addr; |
| 1945 | return getSection(DRI: Sec).addr; |
| 1946 | } |
| 1947 | |
| 1948 | uint64_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { |
| 1949 | return Sec.d.a; |
| 1950 | } |
| 1951 | |
| 1952 | uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const { |
| 1953 | // In the case if a malformed Mach-O file where the section offset is past |
| 1954 | // the end of the file or some part of the section size is past the end of |
| 1955 | // the file return a size of zero or a size that covers the rest of the file |
| 1956 | // but does not extend past the end of the file. |
| 1957 | uint32_t SectOffset, SectType; |
| 1958 | uint64_t SectSize; |
| 1959 | |
| 1960 | if (is64Bit()) { |
| 1961 | MachO::section_64 Sect = getSection64(DRI: Sec); |
| 1962 | SectOffset = Sect.offset; |
| 1963 | SectSize = Sect.size; |
| 1964 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1965 | } else { |
| 1966 | MachO::section Sect = getSection(DRI: Sec); |
| 1967 | SectOffset = Sect.offset; |
| 1968 | SectSize = Sect.size; |
| 1969 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1970 | } |
| 1971 | if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL) |
| 1972 | return SectSize; |
| 1973 | uint64_t FileSize = getData().size(); |
| 1974 | if (SectOffset > FileSize) |
| 1975 | return 0; |
| 1976 | if (FileSize - SectOffset < SectSize) |
| 1977 | return FileSize - SectOffset; |
| 1978 | return SectSize; |
| 1979 | } |
| 1980 | |
| 1981 | ArrayRef<uint8_t> MachOObjectFile::getSectionContents(uint64_t Offset, |
| 1982 | uint64_t Size) const { |
| 1983 | return arrayRefFromStringRef(Input: getData().substr(Start: Offset, N: Size)); |
| 1984 | } |
| 1985 | |
| 1986 | Expected<ArrayRef<uint8_t>> |
| 1987 | MachOObjectFile::getSectionContents(DataRefImpl Sec) const { |
| 1988 | uint64_t Offset; |
| 1989 | uint64_t Size; |
| 1990 | |
| 1991 | if (is64Bit()) { |
| 1992 | MachO::section_64 Sect = getSection64(DRI: Sec); |
| 1993 | Offset = Sect.offset; |
| 1994 | Size = Sect.size; |
| 1995 | // Check for large mach-o files where the section contents might exceed |
| 1996 | // 4GB. MachO::section_64 objects only have 32 bit file offsets to the |
| 1997 | // section contents and can overflow in dSYM files. We can track this and |
| 1998 | // adjust the section offset to be 64 bit safe. If sections overflow then |
| 1999 | // section ordering is enforced. If sections are not ordered, then an error |
| 2000 | // will be returned stopping invalid section data from being returned. |
| 2001 | uint64_t PrevTrueOffset = 0; |
| 2002 | uint64_t SectOffsetAdjust = 0; |
| 2003 | for (uint32_t SectIdx = 0; SectIdx < Sec.d.a; ++SectIdx) { |
| 2004 | MachO::section_64 CurrSect = |
| 2005 | getStruct<MachO::section_64>(O: *this, P: Sections[SectIdx]); |
| 2006 | uint64_t CurrTrueOffset = (uint64_t)CurrSect.offset + SectOffsetAdjust; |
| 2007 | if ((SectOffsetAdjust > 0) && (PrevTrueOffset > CurrTrueOffset)) |
| 2008 | return malformedError(Msg: "section data exceeds 4GB and section file " |
| 2009 | "offsets are not ordered" ); |
| 2010 | const uint64_t EndSectFileOffset = |
| 2011 | (uint64_t)CurrSect.offset + CurrSect.size; |
| 2012 | if (EndSectFileOffset > UINT32_MAX) |
| 2013 | SectOffsetAdjust += EndSectFileOffset & 0xFFFFFFFF00000000ull; |
| 2014 | PrevTrueOffset = CurrTrueOffset; |
| 2015 | } |
| 2016 | Offset += SectOffsetAdjust; |
| 2017 | } else { |
| 2018 | MachO::section Sect = getSection(DRI: Sec); |
| 2019 | Offset = Sect.offset; |
| 2020 | Size = Sect.size; |
| 2021 | } |
| 2022 | |
| 2023 | return getSectionContents(Offset, Size); |
| 2024 | } |
| 2025 | |
| 2026 | uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
| 2027 | uint32_t Align; |
| 2028 | if (is64Bit()) { |
| 2029 | MachO::section_64 Sect = getSection64(DRI: Sec); |
| 2030 | Align = Sect.align; |
| 2031 | } else { |
| 2032 | MachO::section Sect = getSection(DRI: Sec); |
| 2033 | Align = Sect.align; |
| 2034 | } |
| 2035 | |
| 2036 | return uint64_t(1) << Align; |
| 2037 | } |
| 2038 | |
| 2039 | Expected<SectionRef> MachOObjectFile::getSection(unsigned SectionIndex) const { |
| 2040 | if (SectionIndex < 1 || SectionIndex > Sections.size()) |
| 2041 | return malformedError(Msg: "bad section index: " + Twine((int)SectionIndex)); |
| 2042 | |
| 2043 | DataRefImpl DRI; |
| 2044 | DRI.d.a = SectionIndex - 1; |
| 2045 | return SectionRef(DRI, this); |
| 2046 | } |
| 2047 | |
| 2048 | Expected<SectionRef> MachOObjectFile::getSection(StringRef SectionName) const { |
| 2049 | for (const SectionRef &Section : sections()) { |
| 2050 | auto NameOrErr = Section.getName(); |
| 2051 | if (!NameOrErr) |
| 2052 | return NameOrErr.takeError(); |
| 2053 | if (*NameOrErr == SectionName) |
| 2054 | return Section; |
| 2055 | } |
| 2056 | return errorCodeToError(EC: object_error::parse_failed); |
| 2057 | } |
| 2058 | |
| 2059 | bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
| 2060 | return false; |
| 2061 | } |
| 2062 | |
| 2063 | bool MachOObjectFile::isSectionText(DataRefImpl Sec) const { |
| 2064 | uint32_t Flags = getSectionFlags(O: *this, Sec); |
| 2065 | return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
| 2066 | } |
| 2067 | |
| 2068 | bool MachOObjectFile::isSectionData(DataRefImpl Sec) const { |
| 2069 | uint32_t Flags = getSectionFlags(O: *this, Sec); |
| 2070 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 2071 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 2072 | !(SectionType == MachO::S_ZEROFILL || |
| 2073 | SectionType == MachO::S_GB_ZEROFILL); |
| 2074 | } |
| 2075 | |
| 2076 | bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const { |
| 2077 | uint32_t Flags = getSectionFlags(O: *this, Sec); |
| 2078 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 2079 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 2080 | (SectionType == MachO::S_ZEROFILL || |
| 2081 | SectionType == MachO::S_GB_ZEROFILL); |
| 2082 | } |
| 2083 | |
| 2084 | bool MachOObjectFile::isDebugSection(DataRefImpl Sec) const { |
| 2085 | Expected<StringRef> SectionNameOrErr = getSectionName(Sec); |
| 2086 | if (!SectionNameOrErr) { |
| 2087 | // TODO: Report the error message properly. |
| 2088 | consumeError(Err: SectionNameOrErr.takeError()); |
| 2089 | return false; |
| 2090 | } |
| 2091 | StringRef SectionName = SectionNameOrErr.get(); |
| 2092 | return SectionName.starts_with(Prefix: "__debug" ) || |
| 2093 | SectionName.starts_with(Prefix: "__zdebug" ) || |
| 2094 | SectionName.starts_with(Prefix: "__apple" ) || SectionName == "__gdb_index" || |
| 2095 | SectionName == "__swift_ast" ; |
| 2096 | } |
| 2097 | |
| 2098 | namespace { |
| 2099 | template <typename LoadCommandType> |
| 2100 | ArrayRef<uint8_t> getSegmentContents(const MachOObjectFile &Obj, |
| 2101 | MachOObjectFile::LoadCommandInfo LoadCmd, |
| 2102 | StringRef SegmentName) { |
| 2103 | auto SegmentOrErr = getStructOrErr<LoadCommandType>(Obj, LoadCmd.Ptr); |
| 2104 | if (!SegmentOrErr) { |
| 2105 | consumeError(SegmentOrErr.takeError()); |
| 2106 | return {}; |
| 2107 | } |
| 2108 | auto &Segment = SegmentOrErr.get(); |
| 2109 | if (StringRef(Segment.segname, 16).starts_with(Prefix: SegmentName)) |
| 2110 | return arrayRefFromStringRef(Obj.getData().slice( |
| 2111 | Start: Segment.fileoff, End: Segment.fileoff + Segment.filesize)); |
| 2112 | return {}; |
| 2113 | } |
| 2114 | |
| 2115 | template <typename LoadCommandType> |
| 2116 | ArrayRef<uint8_t> getSegmentContents(const MachOObjectFile &Obj, |
| 2117 | MachOObjectFile::LoadCommandInfo LoadCmd) { |
| 2118 | auto SegmentOrErr = getStructOrErr<LoadCommandType>(Obj, LoadCmd.Ptr); |
| 2119 | if (!SegmentOrErr) { |
| 2120 | consumeError(SegmentOrErr.takeError()); |
| 2121 | return {}; |
| 2122 | } |
| 2123 | auto &Segment = SegmentOrErr.get(); |
| 2124 | return arrayRefFromStringRef( |
| 2125 | Obj.getData().substr(Start: Segment.fileoff, N: Segment.filesize)); |
| 2126 | } |
| 2127 | } // namespace |
| 2128 | |
| 2129 | ArrayRef<uint8_t> |
| 2130 | MachOObjectFile::getSegmentContents(StringRef SegmentName) const { |
| 2131 | for (auto LoadCmd : load_commands()) { |
| 2132 | ArrayRef<uint8_t> Contents; |
| 2133 | switch (LoadCmd.C.cmd) { |
| 2134 | case MachO::LC_SEGMENT: |
| 2135 | Contents = ::getSegmentContents<MachO::segment_command>(Obj: *this, LoadCmd, |
| 2136 | SegmentName); |
| 2137 | break; |
| 2138 | case MachO::LC_SEGMENT_64: |
| 2139 | Contents = ::getSegmentContents<MachO::segment_command_64>(Obj: *this, LoadCmd, |
| 2140 | SegmentName); |
| 2141 | break; |
| 2142 | default: |
| 2143 | continue; |
| 2144 | } |
| 2145 | if (!Contents.empty()) |
| 2146 | return Contents; |
| 2147 | } |
| 2148 | return {}; |
| 2149 | } |
| 2150 | |
| 2151 | ArrayRef<uint8_t> |
| 2152 | MachOObjectFile::getSegmentContents(size_t SegmentIndex) const { |
| 2153 | size_t Idx = 0; |
| 2154 | for (auto LoadCmd : load_commands()) { |
| 2155 | switch (LoadCmd.C.cmd) { |
| 2156 | case MachO::LC_SEGMENT: |
| 2157 | if (Idx == SegmentIndex) |
| 2158 | return ::getSegmentContents<MachO::segment_command>(Obj: *this, LoadCmd); |
| 2159 | ++Idx; |
| 2160 | break; |
| 2161 | case MachO::LC_SEGMENT_64: |
| 2162 | if (Idx == SegmentIndex) |
| 2163 | return ::getSegmentContents<MachO::segment_command_64>(Obj: *this, LoadCmd); |
| 2164 | ++Idx; |
| 2165 | break; |
| 2166 | default: |
| 2167 | continue; |
| 2168 | } |
| 2169 | } |
| 2170 | return {}; |
| 2171 | } |
| 2172 | |
| 2173 | unsigned MachOObjectFile::getSectionID(SectionRef Sec) const { |
| 2174 | return Sec.getRawDataRefImpl().d.a; |
| 2175 | } |
| 2176 | |
| 2177 | bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const { |
| 2178 | uint32_t Flags = getSectionFlags(O: *this, Sec); |
| 2179 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 2180 | return SectionType == MachO::S_ZEROFILL || |
| 2181 | SectionType == MachO::S_GB_ZEROFILL; |
| 2182 | } |
| 2183 | |
| 2184 | bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const { |
| 2185 | StringRef SegmentName = getSectionFinalSegmentName(Sec); |
| 2186 | if (Expected<StringRef> NameOrErr = getSectionName(Sec)) |
| 2187 | return (SegmentName == "__LLVM" && *NameOrErr == "__bitcode" ); |
| 2188 | return false; |
| 2189 | } |
| 2190 | |
| 2191 | bool MachOObjectFile::isSectionStripped(DataRefImpl Sec) const { |
| 2192 | if (is64Bit()) |
| 2193 | return getSection64(DRI: Sec).offset == 0; |
| 2194 | return getSection(DRI: Sec).offset == 0; |
| 2195 | } |
| 2196 | |
| 2197 | relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { |
| 2198 | DataRefImpl Ret; |
| 2199 | Ret.d.a = Sec.d.a; |
| 2200 | Ret.d.b = 0; |
| 2201 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2202 | } |
| 2203 | |
| 2204 | relocation_iterator |
| 2205 | MachOObjectFile::section_rel_end(DataRefImpl Sec) const { |
| 2206 | uint32_t Num; |
| 2207 | if (is64Bit()) { |
| 2208 | MachO::section_64 Sect = getSection64(DRI: Sec); |
| 2209 | Num = Sect.nreloc; |
| 2210 | } else { |
| 2211 | MachO::section Sect = getSection(DRI: Sec); |
| 2212 | Num = Sect.nreloc; |
| 2213 | } |
| 2214 | |
| 2215 | DataRefImpl Ret; |
| 2216 | Ret.d.a = Sec.d.a; |
| 2217 | Ret.d.b = Num; |
| 2218 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2219 | } |
| 2220 | |
| 2221 | relocation_iterator MachOObjectFile::extrel_begin() const { |
| 2222 | DataRefImpl Ret; |
| 2223 | // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations |
| 2224 | Ret.d.a = 0; // Would normally be a section index. |
| 2225 | Ret.d.b = 0; // Index into the external relocations |
| 2226 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2227 | } |
| 2228 | |
| 2229 | relocation_iterator MachOObjectFile::extrel_end() const { |
| 2230 | MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand(); |
| 2231 | DataRefImpl Ret; |
| 2232 | // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations |
| 2233 | Ret.d.a = 0; // Would normally be a section index. |
| 2234 | Ret.d.b = DysymtabLoadCmd.nextrel; // Index into the external relocations |
| 2235 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2236 | } |
| 2237 | |
| 2238 | relocation_iterator MachOObjectFile::locrel_begin() const { |
| 2239 | DataRefImpl Ret; |
| 2240 | // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations |
| 2241 | Ret.d.a = 1; // Would normally be a section index. |
| 2242 | Ret.d.b = 0; // Index into the local relocations |
| 2243 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2244 | } |
| 2245 | |
| 2246 | relocation_iterator MachOObjectFile::locrel_end() const { |
| 2247 | MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand(); |
| 2248 | DataRefImpl Ret; |
| 2249 | // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations |
| 2250 | Ret.d.a = 1; // Would normally be a section index. |
| 2251 | Ret.d.b = DysymtabLoadCmd.nlocrel; // Index into the local relocations |
| 2252 | return relocation_iterator(RelocationRef(Ret, this)); |
| 2253 | } |
| 2254 | |
| 2255 | void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
| 2256 | ++Rel.d.b; |
| 2257 | } |
| 2258 | |
| 2259 | uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const { |
| 2260 | assert((getHeader().filetype == MachO::MH_OBJECT || |
| 2261 | getHeader().filetype == MachO::MH_KEXT_BUNDLE) && |
| 2262 | "Only implemented for MH_OBJECT && MH_KEXT_BUNDLE" ); |
| 2263 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 2264 | return getAnyRelocationAddress(RE); |
| 2265 | } |
| 2266 | |
| 2267 | symbol_iterator |
| 2268 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
| 2269 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 2270 | if (isRelocationScattered(RE)) |
| 2271 | return symbol_end(); |
| 2272 | |
| 2273 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 2274 | bool isExtern = getPlainRelocationExternal(RE); |
| 2275 | if (!isExtern) |
| 2276 | return symbol_end(); |
| 2277 | |
| 2278 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 2279 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 2280 | sizeof(MachO::nlist_64) : |
| 2281 | sizeof(MachO::nlist); |
| 2282 | uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; |
| 2283 | DataRefImpl Sym; |
| 2284 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset)); |
| 2285 | return symbol_iterator(SymbolRef(Sym, this)); |
| 2286 | } |
| 2287 | |
| 2288 | section_iterator |
| 2289 | MachOObjectFile::getRelocationSection(DataRefImpl Rel) const { |
| 2290 | return section_iterator(getAnyRelocationSection(RE: getRelocation(Rel))); |
| 2291 | } |
| 2292 | |
| 2293 | uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const { |
| 2294 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 2295 | return getAnyRelocationType(RE); |
| 2296 | } |
| 2297 | |
| 2298 | void MachOObjectFile::getRelocationTypeName( |
| 2299 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
| 2300 | StringRef res; |
| 2301 | uint64_t RType = getRelocationType(Rel); |
| 2302 | |
| 2303 | unsigned Arch = this->getArch(); |
| 2304 | |
| 2305 | switch (Arch) { |
| 2306 | case Triple::x86: { |
| 2307 | static const char *const Table[] = { |
| 2308 | "GENERIC_RELOC_VANILLA" , |
| 2309 | "GENERIC_RELOC_PAIR" , |
| 2310 | "GENERIC_RELOC_SECTDIFF" , |
| 2311 | "GENERIC_RELOC_PB_LA_PTR" , |
| 2312 | "GENERIC_RELOC_LOCAL_SECTDIFF" , |
| 2313 | "GENERIC_RELOC_TLV" }; |
| 2314 | |
| 2315 | if (RType > 5) |
| 2316 | res = "Unknown" ; |
| 2317 | else |
| 2318 | res = Table[RType]; |
| 2319 | break; |
| 2320 | } |
| 2321 | case Triple::x86_64: { |
| 2322 | static const char *const Table[] = { |
| 2323 | "X86_64_RELOC_UNSIGNED" , |
| 2324 | "X86_64_RELOC_SIGNED" , |
| 2325 | "X86_64_RELOC_BRANCH" , |
| 2326 | "X86_64_RELOC_GOT_LOAD" , |
| 2327 | "X86_64_RELOC_GOT" , |
| 2328 | "X86_64_RELOC_SUBTRACTOR" , |
| 2329 | "X86_64_RELOC_SIGNED_1" , |
| 2330 | "X86_64_RELOC_SIGNED_2" , |
| 2331 | "X86_64_RELOC_SIGNED_4" , |
| 2332 | "X86_64_RELOC_TLV" }; |
| 2333 | |
| 2334 | if (RType > 9) |
| 2335 | res = "Unknown" ; |
| 2336 | else |
| 2337 | res = Table[RType]; |
| 2338 | break; |
| 2339 | } |
| 2340 | case Triple::arm: { |
| 2341 | static const char *const Table[] = { |
| 2342 | "ARM_RELOC_VANILLA" , |
| 2343 | "ARM_RELOC_PAIR" , |
| 2344 | "ARM_RELOC_SECTDIFF" , |
| 2345 | "ARM_RELOC_LOCAL_SECTDIFF" , |
| 2346 | "ARM_RELOC_PB_LA_PTR" , |
| 2347 | "ARM_RELOC_BR24" , |
| 2348 | "ARM_THUMB_RELOC_BR22" , |
| 2349 | "ARM_THUMB_32BIT_BRANCH" , |
| 2350 | "ARM_RELOC_HALF" , |
| 2351 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 2352 | |
| 2353 | if (RType > 9) |
| 2354 | res = "Unknown" ; |
| 2355 | else |
| 2356 | res = Table[RType]; |
| 2357 | break; |
| 2358 | } |
| 2359 | case Triple::aarch64: |
| 2360 | case Triple::aarch64_32: { |
| 2361 | static const char *const Table[] = { |
| 2362 | "ARM64_RELOC_UNSIGNED" , "ARM64_RELOC_SUBTRACTOR" , |
| 2363 | "ARM64_RELOC_BRANCH26" , "ARM64_RELOC_PAGE21" , |
| 2364 | "ARM64_RELOC_PAGEOFF12" , "ARM64_RELOC_GOT_LOAD_PAGE21" , |
| 2365 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12" , "ARM64_RELOC_POINTER_TO_GOT" , |
| 2366 | "ARM64_RELOC_TLVP_LOAD_PAGE21" , "ARM64_RELOC_TLVP_LOAD_PAGEOFF12" , |
| 2367 | "ARM64_RELOC_ADDEND" , "ARM64_RELOC_AUTHENTICATED_POINTER" |
| 2368 | }; |
| 2369 | |
| 2370 | if (RType >= std::size(Table)) |
| 2371 | res = "Unknown" ; |
| 2372 | else |
| 2373 | res = Table[RType]; |
| 2374 | break; |
| 2375 | } |
| 2376 | case Triple::ppc: { |
| 2377 | static const char *const Table[] = { |
| 2378 | "PPC_RELOC_VANILLA" , |
| 2379 | "PPC_RELOC_PAIR" , |
| 2380 | "PPC_RELOC_BR14" , |
| 2381 | "PPC_RELOC_BR24" , |
| 2382 | "PPC_RELOC_HI16" , |
| 2383 | "PPC_RELOC_LO16" , |
| 2384 | "PPC_RELOC_HA16" , |
| 2385 | "PPC_RELOC_LO14" , |
| 2386 | "PPC_RELOC_SECTDIFF" , |
| 2387 | "PPC_RELOC_PB_LA_PTR" , |
| 2388 | "PPC_RELOC_HI16_SECTDIFF" , |
| 2389 | "PPC_RELOC_LO16_SECTDIFF" , |
| 2390 | "PPC_RELOC_HA16_SECTDIFF" , |
| 2391 | "PPC_RELOC_JBSR" , |
| 2392 | "PPC_RELOC_LO14_SECTDIFF" , |
| 2393 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 2394 | |
| 2395 | if (RType > 15) |
| 2396 | res = "Unknown" ; |
| 2397 | else |
| 2398 | res = Table[RType]; |
| 2399 | break; |
| 2400 | } |
| 2401 | case Triple::riscv32: { |
| 2402 | static const char *const Table[] = { |
| 2403 | "RISCV_RELOC_UNSIGNED" , "RISCV_RELOC_SUBTRACTOR" , |
| 2404 | "RISCV_RELOC_BRANCH21" , "RISCV_RELOC_HI20" , |
| 2405 | "RISCV_RELOC_LO12" , "RISCV_RELOC_GOT_HI20" , |
| 2406 | "RISCV_RELOC_GOT_LO12" , "RISCV_RELOC_POINTER_TO_GOT" , |
| 2407 | "RISCV_RELOC_ADDEND" , |
| 2408 | }; |
| 2409 | |
| 2410 | if (RType >= std::size(Table)) |
| 2411 | res = "Unknown" ; |
| 2412 | else |
| 2413 | res = Table[RType]; |
| 2414 | Result.append(in_start: res.begin(), in_end: res.end()); |
| 2415 | if ((RType == MachO::RISCV_RELOC_HI20 || |
| 2416 | RType == MachO::RISCV_RELOC_GOT_HI20 || |
| 2417 | RType == MachO::RISCV_RELOC_LO12 || |
| 2418 | RType == MachO::RISCV_RELOC_GOT_LO12) && |
| 2419 | getAnyRelocationPCRel(RE: getRelocation(Rel))) { |
| 2420 | StringRef PCRel("(pcrel)" ); |
| 2421 | Result.append(in_start: PCRel.begin(), in_end: PCRel.end()); |
| 2422 | } |
| 2423 | return; |
| 2424 | } |
| 2425 | case Triple::UnknownArch: |
| 2426 | res = "Unknown" ; |
| 2427 | break; |
| 2428 | } |
| 2429 | Result.append(in_start: res.begin(), in_end: res.end()); |
| 2430 | } |
| 2431 | |
| 2432 | uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const { |
| 2433 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 2434 | return getAnyRelocationLength(RE); |
| 2435 | } |
| 2436 | |
| 2437 | // |
| 2438 | // guessLibraryShortName() is passed a name of a dynamic library and returns a |
| 2439 | // guess on what the short name is. Then name is returned as a substring of the |
| 2440 | // StringRef Name passed in. The name of the dynamic library is recognized as |
| 2441 | // a framework if it has one of the two following forms: |
| 2442 | // Foo.framework/Versions/A/Foo |
| 2443 | // Foo.framework/Foo |
| 2444 | // Where A and Foo can be any string. And may contain a trailing suffix |
| 2445 | // starting with an underbar. If the Name is recognized as a framework then |
| 2446 | // isFramework is set to true else it is set to false. If the Name has a |
| 2447 | // suffix then Suffix is set to the substring in Name that contains the suffix |
| 2448 | // else it is set to a NULL StringRef. |
| 2449 | // |
| 2450 | // The Name of the dynamic library is recognized as a library name if it has |
| 2451 | // one of the two following forms: |
| 2452 | // libFoo.A.dylib |
| 2453 | // libFoo.dylib |
| 2454 | // |
| 2455 | // The library may have a suffix trailing the name Foo of the form: |
| 2456 | // libFoo_profile.A.dylib |
| 2457 | // libFoo_profile.dylib |
| 2458 | // These dyld image suffixes are separated from the short name by a '_' |
| 2459 | // character. Because the '_' character is commonly used to separate words in |
| 2460 | // filenames guessLibraryShortName() cannot reliably separate a dylib's short |
| 2461 | // name from an arbitrary image suffix; imagine if both the short name and the |
| 2462 | // suffix contains an '_' character! To better deal with this ambiguity, |
| 2463 | // guessLibraryShortName() will recognize only "_debug" and "_profile" as valid |
| 2464 | // Suffix values. Calling code needs to be tolerant of guessLibraryShortName() |
| 2465 | // guessing incorrectly. |
| 2466 | // |
| 2467 | // The Name of the dynamic library is also recognized as a library name if it |
| 2468 | // has the following form: |
| 2469 | // Foo.qtx |
| 2470 | // |
| 2471 | // If the Name of the dynamic library is none of the forms above then a NULL |
| 2472 | // StringRef is returned. |
| 2473 | StringRef MachOObjectFile::guessLibraryShortName(StringRef Name, |
| 2474 | bool &isFramework, |
| 2475 | StringRef &Suffix) { |
| 2476 | StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx; |
| 2477 | size_t a, b, c, d, Idx; |
| 2478 | |
| 2479 | isFramework = false; |
| 2480 | Suffix = StringRef(); |
| 2481 | |
| 2482 | // Pull off the last component and make Foo point to it |
| 2483 | a = Name.rfind(C: '/'); |
| 2484 | if (a == Name.npos || a == 0) |
| 2485 | goto guess_library; |
| 2486 | Foo = Name.substr(Start: a + 1); |
| 2487 | |
| 2488 | // Look for a suffix starting with a '_' |
| 2489 | Idx = Foo.rfind(C: '_'); |
| 2490 | if (Idx != Foo.npos && Foo.size() >= 2) { |
| 2491 | Suffix = Foo.substr(Start: Idx); |
| 2492 | if (Suffix != "_debug" && Suffix != "_profile" ) |
| 2493 | Suffix = StringRef(); |
| 2494 | else |
| 2495 | Foo = Foo.slice(Start: 0, End: Idx); |
| 2496 | } |
| 2497 | |
| 2498 | // First look for the form Foo.framework/Foo |
| 2499 | b = Name.rfind(C: '/', From: a); |
| 2500 | if (b == Name.npos) |
| 2501 | Idx = 0; |
| 2502 | else |
| 2503 | Idx = b+1; |
| 2504 | F = Name.substr(Start: Idx, N: Foo.size()); |
| 2505 | DotFramework = Name.substr(Start: Idx + Foo.size(), N: sizeof(".framework/" ) - 1); |
| 2506 | if (F == Foo && DotFramework == ".framework/" ) { |
| 2507 | isFramework = true; |
| 2508 | return Foo; |
| 2509 | } |
| 2510 | |
| 2511 | // Next look for the form Foo.framework/Versions/A/Foo |
| 2512 | if (b == Name.npos) |
| 2513 | goto guess_library; |
| 2514 | c = Name.rfind(C: '/', From: b); |
| 2515 | if (c == Name.npos || c == 0) |
| 2516 | goto guess_library; |
| 2517 | V = Name.substr(Start: c + 1); |
| 2518 | if (!V.starts_with(Prefix: "Versions/" )) |
| 2519 | goto guess_library; |
| 2520 | d = Name.rfind(C: '/', From: c); |
| 2521 | if (d == Name.npos) |
| 2522 | Idx = 0; |
| 2523 | else |
| 2524 | Idx = d+1; |
| 2525 | F = Name.substr(Start: Idx, N: Foo.size()); |
| 2526 | DotFramework = Name.substr(Start: Idx + Foo.size(), N: sizeof(".framework/" ) - 1); |
| 2527 | if (F == Foo && DotFramework == ".framework/" ) { |
| 2528 | isFramework = true; |
| 2529 | return Foo; |
| 2530 | } |
| 2531 | |
| 2532 | guess_library: |
| 2533 | // pull off the suffix after the "." and make a point to it |
| 2534 | a = Name.rfind(C: '.'); |
| 2535 | if (a == Name.npos || a == 0) |
| 2536 | return StringRef(); |
| 2537 | Dylib = Name.substr(Start: a); |
| 2538 | if (Dylib != ".dylib" ) |
| 2539 | goto guess_qtx; |
| 2540 | |
| 2541 | // First pull off the version letter for the form Foo.A.dylib if any. |
| 2542 | if (a >= 3) { |
| 2543 | Dot = Name.substr(Start: a - 2, N: 1); |
| 2544 | if (Dot == "." ) |
| 2545 | a = a - 2; |
| 2546 | } |
| 2547 | |
| 2548 | b = Name.rfind(C: '/', From: a); |
| 2549 | if (b == Name.npos) |
| 2550 | b = 0; |
| 2551 | else |
| 2552 | b = b+1; |
| 2553 | // ignore any suffix after an underbar like Foo_profile.A.dylib |
| 2554 | Idx = Name.rfind(C: '_'); |
| 2555 | if (Idx != Name.npos && Idx != b) { |
| 2556 | Lib = Name.slice(Start: b, End: Idx); |
| 2557 | Suffix = Name.slice(Start: Idx, End: a); |
| 2558 | if (Suffix != "_debug" && Suffix != "_profile" ) { |
| 2559 | Suffix = StringRef(); |
| 2560 | Lib = Name.slice(Start: b, End: a); |
| 2561 | } |
| 2562 | } |
| 2563 | else |
| 2564 | Lib = Name.slice(Start: b, End: a); |
| 2565 | // There are incorrect library names of the form: |
| 2566 | // libATS.A_profile.dylib so check for these. |
| 2567 | if (Lib.size() >= 3) { |
| 2568 | Dot = Lib.substr(Start: Lib.size() - 2, N: 1); |
| 2569 | if (Dot == "." ) |
| 2570 | Lib = Lib.slice(Start: 0, End: Lib.size()-2); |
| 2571 | } |
| 2572 | return Lib; |
| 2573 | |
| 2574 | guess_qtx: |
| 2575 | Qtx = Name.substr(Start: a); |
| 2576 | if (Qtx != ".qtx" ) |
| 2577 | return StringRef(); |
| 2578 | b = Name.rfind(C: '/', From: a); |
| 2579 | if (b == Name.npos) |
| 2580 | Lib = Name.slice(Start: 0, End: a); |
| 2581 | else |
| 2582 | Lib = Name.slice(Start: b+1, End: a); |
| 2583 | // There are library names of the form: QT.A.qtx so check for these. |
| 2584 | if (Lib.size() >= 3) { |
| 2585 | Dot = Lib.substr(Start: Lib.size() - 2, N: 1); |
| 2586 | if (Dot == "." ) |
| 2587 | Lib = Lib.slice(Start: 0, End: Lib.size()-2); |
| 2588 | } |
| 2589 | return Lib; |
| 2590 | } |
| 2591 | |
| 2592 | // getLibraryShortNameByIndex() is used to get the short name of the library |
| 2593 | // for an undefined symbol in a linked Mach-O binary that was linked with the |
| 2594 | // normal two-level namespace default (that is MH_TWOLEVEL in the header). |
| 2595 | // It is passed the index (0 - based) of the library as translated from |
| 2596 | // GET_LIBRARY_ORDINAL (1 - based). |
| 2597 | std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index, |
| 2598 | StringRef &Res) const { |
| 2599 | if (Index >= Libraries.size()) |
| 2600 | return object_error::parse_failed; |
| 2601 | |
| 2602 | // If the cache of LibrariesShortNames is not built up do that first for |
| 2603 | // all the Libraries. |
| 2604 | if (LibrariesShortNames.size() == 0) { |
| 2605 | for (unsigned i = 0; i < Libraries.size(); i++) { |
| 2606 | auto CommandOrErr = |
| 2607 | getStructOrErr<MachO::dylib_command>(O: *this, P: Libraries[i]); |
| 2608 | if (!CommandOrErr) |
| 2609 | return object_error::parse_failed; |
| 2610 | MachO::dylib_command D = CommandOrErr.get(); |
| 2611 | if (D.dylib.name >= D.cmdsize) |
| 2612 | return object_error::parse_failed; |
| 2613 | const char *P = (const char *)(Libraries[i]) + D.dylib.name; |
| 2614 | StringRef Name = StringRef(P); |
| 2615 | if (D.dylib.name+Name.size() >= D.cmdsize) |
| 2616 | return object_error::parse_failed; |
| 2617 | StringRef Suffix; |
| 2618 | bool isFramework; |
| 2619 | StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix); |
| 2620 | if (shortName.empty()) |
| 2621 | LibrariesShortNames.push_back(Elt: Name); |
| 2622 | else |
| 2623 | LibrariesShortNames.push_back(Elt: shortName); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | Res = LibrariesShortNames[Index]; |
| 2628 | return std::error_code(); |
| 2629 | } |
| 2630 | |
| 2631 | uint32_t MachOObjectFile::getLibraryCount() const { |
| 2632 | return Libraries.size(); |
| 2633 | } |
| 2634 | |
| 2635 | section_iterator |
| 2636 | MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const { |
| 2637 | DataRefImpl Sec; |
| 2638 | Sec.d.a = Rel->getRawDataRefImpl().d.a; |
| 2639 | return section_iterator(SectionRef(Sec, this)); |
| 2640 | } |
| 2641 | |
| 2642 | basic_symbol_iterator MachOObjectFile::symbol_begin() const { |
| 2643 | DataRefImpl DRI; |
| 2644 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2645 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
| 2646 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2647 | |
| 2648 | return getSymbolByIndex(Index: 0); |
| 2649 | } |
| 2650 | |
| 2651 | basic_symbol_iterator MachOObjectFile::symbol_end() const { |
| 2652 | DataRefImpl DRI; |
| 2653 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2654 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
| 2655 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2656 | |
| 2657 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 2658 | sizeof(MachO::nlist_64) : |
| 2659 | sizeof(MachO::nlist); |
| 2660 | unsigned Offset = Symtab.symoff + |
| 2661 | Symtab.nsyms * SymbolTableEntrySize; |
| 2662 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset)); |
| 2663 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2664 | } |
| 2665 | |
| 2666 | symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const { |
| 2667 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2668 | if (!SymtabLoadCmd || Index >= Symtab.nsyms) |
| 2669 | report_fatal_error(reason: "Requested symbol index is out of range." ); |
| 2670 | unsigned SymbolTableEntrySize = |
| 2671 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 2672 | DataRefImpl DRI; |
| 2673 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset: Symtab.symoff)); |
| 2674 | DRI.p += Index * SymbolTableEntrySize; |
| 2675 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2676 | } |
| 2677 | |
| 2678 | uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const { |
| 2679 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2680 | if (!SymtabLoadCmd) |
| 2681 | report_fatal_error(reason: "getSymbolIndex() called with no symbol table symbol" ); |
| 2682 | unsigned SymbolTableEntrySize = |
| 2683 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 2684 | DataRefImpl DRIstart; |
| 2685 | DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset: Symtab.symoff)); |
| 2686 | uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize; |
| 2687 | return Index; |
| 2688 | } |
| 2689 | |
| 2690 | section_iterator MachOObjectFile::section_begin() const { |
| 2691 | DataRefImpl DRI; |
| 2692 | return section_iterator(SectionRef(DRI, this)); |
| 2693 | } |
| 2694 | |
| 2695 | section_iterator MachOObjectFile::section_end() const { |
| 2696 | DataRefImpl DRI; |
| 2697 | DRI.d.a = Sections.size(); |
| 2698 | return section_iterator(SectionRef(DRI, this)); |
| 2699 | } |
| 2700 | |
| 2701 | uint8_t MachOObjectFile::getBytesInAddress() const { |
| 2702 | return is64Bit() ? 8 : 4; |
| 2703 | } |
| 2704 | |
| 2705 | StringRef MachOObjectFile::getFileFormatName() const { |
| 2706 | unsigned CPUType = getCPUType(O: *this); |
| 2707 | if (!is64Bit()) { |
| 2708 | switch (CPUType) { |
| 2709 | case MachO::CPU_TYPE_I386: |
| 2710 | return "Mach-O 32-bit i386" ; |
| 2711 | case MachO::CPU_TYPE_ARM: |
| 2712 | return "Mach-O arm" ; |
| 2713 | case MachO::CPU_TYPE_ARM64_32: |
| 2714 | return "Mach-O arm64 (ILP32)" ; |
| 2715 | case MachO::CPU_TYPE_POWERPC: |
| 2716 | return "Mach-O 32-bit ppc" ; |
| 2717 | case MachO::CPU_TYPE_RISCV: |
| 2718 | return "Mach-O 32-bit RISC-V" ; |
| 2719 | default: |
| 2720 | return "Mach-O 32-bit unknown" ; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | switch (CPUType) { |
| 2725 | case MachO::CPU_TYPE_X86_64: |
| 2726 | return "Mach-O 64-bit x86-64" ; |
| 2727 | case MachO::CPU_TYPE_ARM64: |
| 2728 | return "Mach-O arm64" ; |
| 2729 | case MachO::CPU_TYPE_POWERPC64: |
| 2730 | return "Mach-O 64-bit ppc64" ; |
| 2731 | default: |
| 2732 | return "Mach-O 64-bit unknown" ; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType) { |
| 2737 | switch (CPUType) { |
| 2738 | case MachO::CPU_TYPE_I386: |
| 2739 | return Triple::x86; |
| 2740 | case MachO::CPU_TYPE_X86_64: |
| 2741 | return Triple::x86_64; |
| 2742 | case MachO::CPU_TYPE_ARM: |
| 2743 | return Triple::arm; |
| 2744 | case MachO::CPU_TYPE_ARM64: |
| 2745 | return Triple::aarch64; |
| 2746 | case MachO::CPU_TYPE_ARM64_32: |
| 2747 | return Triple::aarch64_32; |
| 2748 | case MachO::CPU_TYPE_POWERPC: |
| 2749 | return Triple::ppc; |
| 2750 | case MachO::CPU_TYPE_POWERPC64: |
| 2751 | return Triple::ppc64; |
| 2752 | case MachO::CPU_TYPE_RISCV: |
| 2753 | return Triple::riscv32; |
| 2754 | default: |
| 2755 | return Triple::UnknownArch; |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType, |
| 2760 | const char **McpuDefault, |
| 2761 | const char **ArchFlag) { |
| 2762 | if (McpuDefault) |
| 2763 | *McpuDefault = nullptr; |
| 2764 | if (ArchFlag) |
| 2765 | *ArchFlag = nullptr; |
| 2766 | |
| 2767 | switch (CPUType) { |
| 2768 | case MachO::CPU_TYPE_I386: |
| 2769 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2770 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 2771 | if (ArchFlag) |
| 2772 | *ArchFlag = "i386" ; |
| 2773 | return Triple("i386-apple-darwin" ); |
| 2774 | default: |
| 2775 | return Triple(); |
| 2776 | } |
| 2777 | case MachO::CPU_TYPE_X86_64: |
| 2778 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2779 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 2780 | if (ArchFlag) |
| 2781 | *ArchFlag = "x86_64" ; |
| 2782 | return Triple("x86_64-apple-darwin" ); |
| 2783 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 2784 | if (ArchFlag) |
| 2785 | *ArchFlag = "x86_64h" ; |
| 2786 | return Triple("x86_64h-apple-darwin" ); |
| 2787 | default: |
| 2788 | return Triple(); |
| 2789 | } |
| 2790 | case MachO::CPU_TYPE_ARM: |
| 2791 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2792 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 2793 | if (ArchFlag) |
| 2794 | *ArchFlag = "armv4t" ; |
| 2795 | return Triple("armv4t-apple-darwin" ); |
| 2796 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 2797 | if (ArchFlag) |
| 2798 | *ArchFlag = "armv5e" ; |
| 2799 | return Triple("armv5e-apple-darwin" ); |
| 2800 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 2801 | if (ArchFlag) |
| 2802 | *ArchFlag = "xscale" ; |
| 2803 | return Triple("xscale-apple-darwin" ); |
| 2804 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 2805 | if (ArchFlag) |
| 2806 | *ArchFlag = "armv6" ; |
| 2807 | return Triple("armv6-apple-darwin" ); |
| 2808 | case MachO::CPU_SUBTYPE_ARM_V6M: |
| 2809 | if (McpuDefault) |
| 2810 | *McpuDefault = "cortex-m0" ; |
| 2811 | if (ArchFlag) |
| 2812 | *ArchFlag = "armv6m" ; |
| 2813 | return Triple("armv6m-apple-darwin" ); |
| 2814 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 2815 | if (ArchFlag) |
| 2816 | *ArchFlag = "armv7" ; |
| 2817 | return Triple("armv7-apple-darwin" ); |
| 2818 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
| 2819 | if (McpuDefault) |
| 2820 | *McpuDefault = "cortex-m4" ; |
| 2821 | if (ArchFlag) |
| 2822 | *ArchFlag = "armv7em" ; |
| 2823 | return Triple("thumbv7em-apple-darwin" ); |
| 2824 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 2825 | if (McpuDefault) |
| 2826 | *McpuDefault = "cortex-a7" ; |
| 2827 | if (ArchFlag) |
| 2828 | *ArchFlag = "armv7k" ; |
| 2829 | return Triple("armv7k-apple-darwin" ); |
| 2830 | case MachO::CPU_SUBTYPE_ARM_V7M: |
| 2831 | if (McpuDefault) |
| 2832 | *McpuDefault = "cortex-m3" ; |
| 2833 | if (ArchFlag) |
| 2834 | *ArchFlag = "armv7m" ; |
| 2835 | return Triple("thumbv7m-apple-darwin" ); |
| 2836 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 2837 | if (McpuDefault) |
| 2838 | *McpuDefault = "cortex-a7" ; |
| 2839 | if (ArchFlag) |
| 2840 | *ArchFlag = "armv7s" ; |
| 2841 | return Triple("armv7s-apple-darwin" ); |
| 2842 | default: |
| 2843 | return Triple(); |
| 2844 | } |
| 2845 | case MachO::CPU_TYPE_ARM64: |
| 2846 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2847 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 2848 | if (McpuDefault) |
| 2849 | *McpuDefault = "cyclone" ; |
| 2850 | if (ArchFlag) |
| 2851 | *ArchFlag = "arm64" ; |
| 2852 | return Triple("arm64-apple-darwin" ); |
| 2853 | case MachO::CPU_SUBTYPE_ARM64E: |
| 2854 | if (McpuDefault) |
| 2855 | *McpuDefault = "apple-a12" ; |
| 2856 | if (ArchFlag) |
| 2857 | *ArchFlag = "arm64e" ; |
| 2858 | return Triple("arm64e-apple-darwin" ); |
| 2859 | default: |
| 2860 | return Triple(); |
| 2861 | } |
| 2862 | case MachO::CPU_TYPE_ARM64_32: |
| 2863 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2864 | case MachO::CPU_SUBTYPE_ARM64_32_V8: |
| 2865 | if (McpuDefault) |
| 2866 | *McpuDefault = "cyclone" ; |
| 2867 | if (ArchFlag) |
| 2868 | *ArchFlag = "arm64_32" ; |
| 2869 | return Triple("arm64_32-apple-darwin" ); |
| 2870 | default: |
| 2871 | return Triple(); |
| 2872 | } |
| 2873 | case MachO::CPU_TYPE_POWERPC: |
| 2874 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2875 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 2876 | if (ArchFlag) |
| 2877 | *ArchFlag = "ppc" ; |
| 2878 | return Triple("ppc-apple-darwin" ); |
| 2879 | default: |
| 2880 | return Triple(); |
| 2881 | } |
| 2882 | case MachO::CPU_TYPE_POWERPC64: |
| 2883 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2884 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 2885 | if (ArchFlag) |
| 2886 | *ArchFlag = "ppc64" ; |
| 2887 | return Triple("ppc64-apple-darwin" ); |
| 2888 | default: |
| 2889 | return Triple(); |
| 2890 | } |
| 2891 | case MachO::CPU_TYPE_RISCV: |
| 2892 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2893 | case MachO::CPU_SUBTYPE_RISCV_ALL: |
| 2894 | if (ArchFlag) |
| 2895 | *ArchFlag = "riscv32" ; |
| 2896 | return Triple("riscv32-apple-macho" ); |
| 2897 | default: |
| 2898 | return Triple(); |
| 2899 | } |
| 2900 | default: |
| 2901 | return Triple(); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | Triple MachOObjectFile::getHostArch() { |
| 2906 | return Triple(sys::getDefaultTargetTriple()); |
| 2907 | } |
| 2908 | |
| 2909 | bool MachOObjectFile::isValidArch(StringRef ArchFlag) { |
| 2910 | auto validArchs = getValidArchs(); |
| 2911 | return llvm::is_contained(Range&: validArchs, Element: ArchFlag); |
| 2912 | } |
| 2913 | |
| 2914 | ArrayRef<StringRef> MachOObjectFile::getValidArchs() { |
| 2915 | static const std::array<StringRef, 18> ValidArchs = {._M_elems: { |
| 2916 | "i386" , |
| 2917 | "x86_64" , |
| 2918 | "x86_64h" , |
| 2919 | "armv4t" , |
| 2920 | "arm" , |
| 2921 | "armv5e" , |
| 2922 | "armv6" , |
| 2923 | "armv6m" , |
| 2924 | "armv7" , |
| 2925 | "armv7em" , |
| 2926 | "armv7k" , |
| 2927 | "armv7m" , |
| 2928 | "armv7s" , |
| 2929 | "arm64" , |
| 2930 | "arm64e" , |
| 2931 | "arm64_32" , |
| 2932 | "ppc" , |
| 2933 | "ppc64" , |
| 2934 | }}; |
| 2935 | |
| 2936 | return ValidArchs; |
| 2937 | } |
| 2938 | |
| 2939 | Triple::ArchType MachOObjectFile::getArch() const { |
| 2940 | return getArch(CPUType: getCPUType(O: *this), CPUSubType: getCPUSubType(O: *this)); |
| 2941 | } |
| 2942 | |
| 2943 | Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const { |
| 2944 | return getArchTriple(CPUType: Header.cputype, CPUSubType: Header.cpusubtype, McpuDefault); |
| 2945 | } |
| 2946 | |
| 2947 | relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { |
| 2948 | DataRefImpl DRI; |
| 2949 | DRI.d.a = Index; |
| 2950 | return section_rel_begin(Sec: DRI); |
| 2951 | } |
| 2952 | |
| 2953 | relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { |
| 2954 | DataRefImpl DRI; |
| 2955 | DRI.d.a = Index; |
| 2956 | return section_rel_end(Sec: DRI); |
| 2957 | } |
| 2958 | |
| 2959 | dice_iterator MachOObjectFile::begin_dices() const { |
| 2960 | DataRefImpl DRI; |
| 2961 | if (!DataInCodeLoadCmd) |
| 2962 | return dice_iterator(DiceRef(DRI, this)); |
| 2963 | |
| 2964 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 2965 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset: DicLC.dataoff)); |
| 2966 | return dice_iterator(DiceRef(DRI, this)); |
| 2967 | } |
| 2968 | |
| 2969 | dice_iterator MachOObjectFile::end_dices() const { |
| 2970 | DataRefImpl DRI; |
| 2971 | if (!DataInCodeLoadCmd) |
| 2972 | return dice_iterator(DiceRef(DRI, this)); |
| 2973 | |
| 2974 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 2975 | unsigned Offset = DicLC.dataoff + DicLC.datasize; |
| 2976 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(O: *this, Offset)); |
| 2977 | return dice_iterator(DiceRef(DRI, this)); |
| 2978 | } |
| 2979 | |
| 2980 | ExportEntry::ExportEntry(Error *E, const MachOObjectFile *O, |
| 2981 | ArrayRef<uint8_t> T) : E(E), O(O), Trie(T) {} |
| 2982 | |
| 2983 | void ExportEntry::moveToFirst() { |
| 2984 | ErrorAsOutParameter ErrAsOutParam(E); |
| 2985 | pushNode(Offset: 0); |
| 2986 | if (*E) |
| 2987 | return; |
| 2988 | pushDownUntilBottom(); |
| 2989 | } |
| 2990 | |
| 2991 | void ExportEntry::moveToEnd() { |
| 2992 | Stack.clear(); |
| 2993 | Done = true; |
| 2994 | } |
| 2995 | |
| 2996 | bool ExportEntry::operator==(const ExportEntry &Other) const { |
| 2997 | // Common case, one at end, other iterating from begin. |
| 2998 | if (Done || Other.Done) |
| 2999 | return (Done == Other.Done); |
| 3000 | // Not equal if different stack sizes. |
| 3001 | if (Stack.size() != Other.Stack.size()) |
| 3002 | return false; |
| 3003 | // Not equal if different cumulative strings. |
| 3004 | if (!CumulativeString.equals(RHS: Other.CumulativeString)) |
| 3005 | return false; |
| 3006 | // Equal if all nodes in both stacks match. |
| 3007 | for (unsigned i=0; i < Stack.size(); ++i) { |
| 3008 | if (Stack[i].Start != Other.Stack[i].Start) |
| 3009 | return false; |
| 3010 | } |
| 3011 | return true; |
| 3012 | } |
| 3013 | |
| 3014 | uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr, const char **error) { |
| 3015 | unsigned Count; |
| 3016 | uint64_t Result = decodeULEB128(p: Ptr, n: &Count, end: Trie.end(), error); |
| 3017 | Ptr += Count; |
| 3018 | if (Ptr > Trie.end()) |
| 3019 | Ptr = Trie.end(); |
| 3020 | return Result; |
| 3021 | } |
| 3022 | |
| 3023 | StringRef ExportEntry::name() const { |
| 3024 | return CumulativeString; |
| 3025 | } |
| 3026 | |
| 3027 | uint64_t ExportEntry::flags() const { |
| 3028 | return Stack.back().Flags; |
| 3029 | } |
| 3030 | |
| 3031 | uint64_t ExportEntry::address() const { |
| 3032 | return Stack.back().Address; |
| 3033 | } |
| 3034 | |
| 3035 | uint64_t ExportEntry::other() const { |
| 3036 | return Stack.back().Other; |
| 3037 | } |
| 3038 | |
| 3039 | StringRef ExportEntry::otherName() const { |
| 3040 | const char* ImportName = Stack.back().ImportName; |
| 3041 | if (ImportName) |
| 3042 | return StringRef(ImportName); |
| 3043 | return StringRef(); |
| 3044 | } |
| 3045 | |
| 3046 | uint32_t ExportEntry::nodeOffset() const { |
| 3047 | return Stack.back().Start - Trie.begin(); |
| 3048 | } |
| 3049 | |
| 3050 | ExportEntry::NodeState::NodeState(const uint8_t *Ptr) |
| 3051 | : Start(Ptr), Current(Ptr) {} |
| 3052 | |
| 3053 | void ExportEntry::pushNode(uint64_t offset) { |
| 3054 | ErrorAsOutParameter ErrAsOutParam(E); |
| 3055 | const uint8_t *Ptr = Trie.begin() + offset; |
| 3056 | NodeState State(Ptr); |
| 3057 | const char *error = nullptr; |
| 3058 | uint64_t ExportInfoSize = readULEB128(Ptr&: State.Current, error: &error); |
| 3059 | if (error) { |
| 3060 | *E = malformedError(Msg: "export info size " + Twine(error) + |
| 3061 | " in export trie data at node: 0x" + |
| 3062 | Twine::utohexstr(Val: offset)); |
| 3063 | moveToEnd(); |
| 3064 | return; |
| 3065 | } |
| 3066 | State.IsExportNode = (ExportInfoSize != 0); |
| 3067 | const uint8_t* Children = State.Current + ExportInfoSize; |
| 3068 | if (Children > Trie.end()) { |
| 3069 | *E = malformedError( |
| 3070 | Msg: "export info size: 0x" + Twine::utohexstr(Val: ExportInfoSize) + |
| 3071 | " in export trie data at node: 0x" + Twine::utohexstr(Val: offset) + |
| 3072 | " too big and extends past end of trie data" ); |
| 3073 | moveToEnd(); |
| 3074 | return; |
| 3075 | } |
| 3076 | if (State.IsExportNode) { |
| 3077 | const uint8_t *ExportStart = State.Current; |
| 3078 | State.Flags = readULEB128(Ptr&: State.Current, error: &error); |
| 3079 | if (error) { |
| 3080 | *E = malformedError(Msg: "flags " + Twine(error) + |
| 3081 | " in export trie data at node: 0x" + |
| 3082 | Twine::utohexstr(Val: offset)); |
| 3083 | moveToEnd(); |
| 3084 | return; |
| 3085 | } |
| 3086 | uint64_t Kind = State.Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK; |
| 3087 | if (State.Flags != 0 && |
| 3088 | (Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_REGULAR && |
| 3089 | Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE && |
| 3090 | Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL)) { |
| 3091 | *E = malformedError( |
| 3092 | Msg: "unsupported exported symbol kind: " + Twine((int)Kind) + |
| 3093 | " in flags: 0x" + Twine::utohexstr(Val: State.Flags) + |
| 3094 | " in export trie data at node: 0x" + Twine::utohexstr(Val: offset)); |
| 3095 | moveToEnd(); |
| 3096 | return; |
| 3097 | } |
| 3098 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { |
| 3099 | State.Address = 0; |
| 3100 | State.Other = readULEB128(Ptr&: State.Current, error: &error); // dylib ordinal |
| 3101 | if (error) { |
| 3102 | *E = malformedError(Msg: "dylib ordinal of re-export " + Twine(error) + |
| 3103 | " in export trie data at node: 0x" + |
| 3104 | Twine::utohexstr(Val: offset)); |
| 3105 | moveToEnd(); |
| 3106 | return; |
| 3107 | } |
| 3108 | if (O != nullptr) { |
| 3109 | // Only positive numbers represent library ordinals. Zero and negative |
| 3110 | // numbers have special meaning (see BindSpecialDylib). |
| 3111 | if ((int64_t)State.Other > 0 && State.Other > O->getLibraryCount()) { |
| 3112 | *E = malformedError( |
| 3113 | Msg: "bad library ordinal: " + Twine((int)State.Other) + " (max " + |
| 3114 | Twine((int)O->getLibraryCount()) + |
| 3115 | ") in export trie data at node: 0x" + Twine::utohexstr(Val: offset)); |
| 3116 | moveToEnd(); |
| 3117 | return; |
| 3118 | } |
| 3119 | } |
| 3120 | State.ImportName = reinterpret_cast<const char*>(State.Current); |
| 3121 | if (*State.ImportName == '\0') { |
| 3122 | State.Current++; |
| 3123 | } else { |
| 3124 | const uint8_t *End = State.Current + 1; |
| 3125 | if (End >= Trie.end()) { |
| 3126 | *E = malformedError(Msg: "import name of re-export in export trie data at " |
| 3127 | "node: 0x" + |
| 3128 | Twine::utohexstr(Val: offset) + |
| 3129 | " starts past end of trie data" ); |
| 3130 | moveToEnd(); |
| 3131 | return; |
| 3132 | } |
| 3133 | while(*End != '\0' && End < Trie.end()) |
| 3134 | End++; |
| 3135 | if (*End != '\0') { |
| 3136 | *E = malformedError(Msg: "import name of re-export in export trie data at " |
| 3137 | "node: 0x" + |
| 3138 | Twine::utohexstr(Val: offset) + |
| 3139 | " extends past end of trie data" ); |
| 3140 | moveToEnd(); |
| 3141 | return; |
| 3142 | } |
| 3143 | State.Current = End + 1; |
| 3144 | } |
| 3145 | } else { |
| 3146 | State.Address = readULEB128(Ptr&: State.Current, error: &error); |
| 3147 | if (error) { |
| 3148 | *E = malformedError(Msg: "address " + Twine(error) + |
| 3149 | " in export trie data at node: 0x" + |
| 3150 | Twine::utohexstr(Val: offset)); |
| 3151 | moveToEnd(); |
| 3152 | return; |
| 3153 | } |
| 3154 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) { |
| 3155 | State.Other = readULEB128(Ptr&: State.Current, error: &error); |
| 3156 | if (error) { |
| 3157 | *E = malformedError(Msg: "resolver of stub and resolver " + Twine(error) + |
| 3158 | " in export trie data at node: 0x" + |
| 3159 | Twine::utohexstr(Val: offset)); |
| 3160 | moveToEnd(); |
| 3161 | return; |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | if (ExportStart + ExportInfoSize < State.Current) { |
| 3166 | *E = malformedError( |
| 3167 | Msg: "inconsistent export info size: 0x" + |
| 3168 | Twine::utohexstr(Val: ExportInfoSize) + " where actual size was: 0x" + |
| 3169 | Twine::utohexstr(Val: State.Current - ExportStart) + |
| 3170 | " in export trie data at node: 0x" + Twine::utohexstr(Val: offset)); |
| 3171 | moveToEnd(); |
| 3172 | return; |
| 3173 | } |
| 3174 | } |
| 3175 | State.ChildCount = *Children; |
| 3176 | if (State.ChildCount != 0 && Children + 1 >= Trie.end()) { |
| 3177 | *E = malformedError(Msg: "byte for count of children in export trie data at " |
| 3178 | "node: 0x" + |
| 3179 | Twine::utohexstr(Val: offset) + |
| 3180 | " extends past end of trie data" ); |
| 3181 | moveToEnd(); |
| 3182 | return; |
| 3183 | } |
| 3184 | State.Current = Children + 1; |
| 3185 | State.NextChildIndex = 0; |
| 3186 | State.ParentStringLength = CumulativeString.size(); |
| 3187 | Stack.push_back(Elt: State); |
| 3188 | } |
| 3189 | |
| 3190 | void ExportEntry::pushDownUntilBottom() { |
| 3191 | ErrorAsOutParameter ErrAsOutParam(E); |
| 3192 | const char *error = nullptr; |
| 3193 | while (Stack.back().NextChildIndex < Stack.back().ChildCount) { |
| 3194 | NodeState &Top = Stack.back(); |
| 3195 | CumulativeString.resize(N: Top.ParentStringLength); |
| 3196 | for (;*Top.Current != 0 && Top.Current < Trie.end(); Top.Current++) { |
| 3197 | char C = *Top.Current; |
| 3198 | CumulativeString.push_back(Elt: C); |
| 3199 | } |
| 3200 | if (Top.Current >= Trie.end()) { |
| 3201 | *E = malformedError(Msg: "edge sub-string in export trie data at node: 0x" + |
| 3202 | Twine::utohexstr(Val: Top.Start - Trie.begin()) + |
| 3203 | " for child #" + Twine((int)Top.NextChildIndex) + |
| 3204 | " extends past end of trie data" ); |
| 3205 | moveToEnd(); |
| 3206 | return; |
| 3207 | } |
| 3208 | Top.Current += 1; |
| 3209 | uint64_t childNodeIndex = readULEB128(Ptr&: Top.Current, error: &error); |
| 3210 | if (error) { |
| 3211 | *E = malformedError(Msg: "child node offset " + Twine(error) + |
| 3212 | " in export trie data at node: 0x" + |
| 3213 | Twine::utohexstr(Val: Top.Start - Trie.begin())); |
| 3214 | moveToEnd(); |
| 3215 | return; |
| 3216 | } |
| 3217 | for (const NodeState &node : nodes()) { |
| 3218 | if (node.Start == Trie.begin() + childNodeIndex){ |
| 3219 | *E = malformedError(Msg: "loop in children in export trie data at node: 0x" + |
| 3220 | Twine::utohexstr(Val: Top.Start - Trie.begin()) + |
| 3221 | " back to node: 0x" + |
| 3222 | Twine::utohexstr(Val: childNodeIndex)); |
| 3223 | moveToEnd(); |
| 3224 | return; |
| 3225 | } |
| 3226 | } |
| 3227 | Top.NextChildIndex += 1; |
| 3228 | pushNode(offset: childNodeIndex); |
| 3229 | if (*E) |
| 3230 | return; |
| 3231 | } |
| 3232 | if (!Stack.back().IsExportNode) { |
| 3233 | *E = malformedError(Msg: "node is not an export node in export trie data at " |
| 3234 | "node: 0x" + |
| 3235 | Twine::utohexstr(Val: Stack.back().Start - Trie.begin())); |
| 3236 | moveToEnd(); |
| 3237 | return; |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | // We have a trie data structure and need a way to walk it that is compatible |
| 3242 | // with the C++ iterator model. The solution is a non-recursive depth first |
| 3243 | // traversal where the iterator contains a stack of parent nodes along with a |
| 3244 | // string that is the accumulation of all edge strings along the parent chain |
| 3245 | // to this point. |
| 3246 | // |
| 3247 | // There is one "export" node for each exported symbol. But because some |
| 3248 | // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export |
| 3249 | // node may have child nodes too. |
| 3250 | // |
| 3251 | // The algorithm for moveNext() is to keep moving down the leftmost unvisited |
| 3252 | // child until hitting a node with no children (which is an export node or |
| 3253 | // else the trie is malformed). On the way down, each node is pushed on the |
| 3254 | // stack ivar. If there is no more ways down, it pops up one and tries to go |
| 3255 | // down a sibling path until a childless node is reached. |
| 3256 | void ExportEntry::moveNext() { |
| 3257 | assert(!Stack.empty() && "ExportEntry::moveNext() with empty node stack" ); |
| 3258 | if (!Stack.back().IsExportNode) { |
| 3259 | *E = malformedError(Msg: "node is not an export node in export trie data at " |
| 3260 | "node: 0x" + |
| 3261 | Twine::utohexstr(Val: Stack.back().Start - Trie.begin())); |
| 3262 | moveToEnd(); |
| 3263 | return; |
| 3264 | } |
| 3265 | |
| 3266 | Stack.pop_back(); |
| 3267 | while (!Stack.empty()) { |
| 3268 | NodeState &Top = Stack.back(); |
| 3269 | if (Top.NextChildIndex < Top.ChildCount) { |
| 3270 | pushDownUntilBottom(); |
| 3271 | // Now at the next export node. |
| 3272 | return; |
| 3273 | } else { |
| 3274 | if (Top.IsExportNode) { |
| 3275 | // This node has no children but is itself an export node. |
| 3276 | CumulativeString.resize(N: Top.ParentStringLength); |
| 3277 | return; |
| 3278 | } |
| 3279 | Stack.pop_back(); |
| 3280 | } |
| 3281 | } |
| 3282 | Done = true; |
| 3283 | } |
| 3284 | |
| 3285 | iterator_range<export_iterator> |
| 3286 | MachOObjectFile::exports(Error &E, ArrayRef<uint8_t> Trie, |
| 3287 | const MachOObjectFile *O) { |
| 3288 | ExportEntry Start(&E, O, Trie); |
| 3289 | if (Trie.empty()) |
| 3290 | Start.moveToEnd(); |
| 3291 | else |
| 3292 | Start.moveToFirst(); |
| 3293 | |
| 3294 | ExportEntry Finish(&E, O, Trie); |
| 3295 | Finish.moveToEnd(); |
| 3296 | |
| 3297 | return make_range(x: export_iterator(Start), y: export_iterator(Finish)); |
| 3298 | } |
| 3299 | |
| 3300 | iterator_range<export_iterator> MachOObjectFile::exports(Error &Err) const { |
| 3301 | ArrayRef<uint8_t> Trie; |
| 3302 | if (DyldInfoLoadCmd) |
| 3303 | Trie = getDyldInfoExportsTrie(); |
| 3304 | else if (DyldExportsTrieLoadCmd) |
| 3305 | Trie = getDyldExportsTrie(); |
| 3306 | |
| 3307 | return exports(E&: Err, Trie, O: this); |
| 3308 | } |
| 3309 | |
| 3310 | MachOAbstractFixupEntry::MachOAbstractFixupEntry(Error *E, |
| 3311 | const MachOObjectFile *O) |
| 3312 | : E(E), O(O) { |
| 3313 | // Cache the vmaddress of __TEXT |
| 3314 | for (const auto &Command : O->load_commands()) { |
| 3315 | if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 3316 | MachO::segment_command SLC = O->getSegmentLoadCommand(L: Command); |
| 3317 | if (StringRef(SLC.segname) == "__TEXT" ) { |
| 3318 | TextAddress = SLC.vmaddr; |
| 3319 | break; |
| 3320 | } |
| 3321 | } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { |
| 3322 | MachO::segment_command_64 SLC_64 = O->getSegment64LoadCommand(L: Command); |
| 3323 | if (StringRef(SLC_64.segname) == "__TEXT" ) { |
| 3324 | TextAddress = SLC_64.vmaddr; |
| 3325 | break; |
| 3326 | } |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | int32_t MachOAbstractFixupEntry::segmentIndex() const { return SegmentIndex; } |
| 3332 | |
| 3333 | uint64_t MachOAbstractFixupEntry::segmentOffset() const { |
| 3334 | return SegmentOffset; |
| 3335 | } |
| 3336 | |
| 3337 | uint64_t MachOAbstractFixupEntry::segmentAddress() const { |
| 3338 | return O->BindRebaseAddress(SegIndex: SegmentIndex, SegOffset: 0); |
| 3339 | } |
| 3340 | |
| 3341 | StringRef MachOAbstractFixupEntry::segmentName() const { |
| 3342 | return O->BindRebaseSegmentName(SegIndex: SegmentIndex); |
| 3343 | } |
| 3344 | |
| 3345 | StringRef MachOAbstractFixupEntry::sectionName() const { |
| 3346 | return O->BindRebaseSectionName(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 3347 | } |
| 3348 | |
| 3349 | uint64_t MachOAbstractFixupEntry::address() const { |
| 3350 | return O->BindRebaseAddress(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 3351 | } |
| 3352 | |
| 3353 | StringRef MachOAbstractFixupEntry::symbolName() const { return SymbolName; } |
| 3354 | |
| 3355 | int64_t MachOAbstractFixupEntry::addend() const { return Addend; } |
| 3356 | |
| 3357 | uint32_t MachOAbstractFixupEntry::flags() const { return Flags; } |
| 3358 | |
| 3359 | int MachOAbstractFixupEntry::ordinal() const { return Ordinal; } |
| 3360 | |
| 3361 | StringRef MachOAbstractFixupEntry::typeName() const { return "unknown" ; } |
| 3362 | |
| 3363 | void MachOAbstractFixupEntry::moveToFirst() { |
| 3364 | SegmentOffset = 0; |
| 3365 | SegmentIndex = -1; |
| 3366 | Ordinal = 0; |
| 3367 | Flags = 0; |
| 3368 | Addend = 0; |
| 3369 | Done = false; |
| 3370 | } |
| 3371 | |
| 3372 | void MachOAbstractFixupEntry::moveToEnd() { Done = true; } |
| 3373 | |
| 3374 | void MachOAbstractFixupEntry::moveNext() {} |
| 3375 | |
| 3376 | MachOChainedFixupEntry::MachOChainedFixupEntry(Error *E, |
| 3377 | const MachOObjectFile *O, |
| 3378 | bool Parse) |
| 3379 | : MachOAbstractFixupEntry(E, O) { |
| 3380 | ErrorAsOutParameter e(E); |
| 3381 | if (!Parse) |
| 3382 | return; |
| 3383 | |
| 3384 | if (auto FixupTargetsOrErr = O->getDyldChainedFixupTargets()) { |
| 3385 | FixupTargets = *FixupTargetsOrErr; |
| 3386 | } else { |
| 3387 | *E = FixupTargetsOrErr.takeError(); |
| 3388 | return; |
| 3389 | } |
| 3390 | |
| 3391 | if (auto SegmentsOrErr = O->getChainedFixupsSegments()) { |
| 3392 | Segments = std::move(SegmentsOrErr->second); |
| 3393 | } else { |
| 3394 | *E = SegmentsOrErr.takeError(); |
| 3395 | return; |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | void MachOChainedFixupEntry::findNextPageWithFixups() { |
| 3400 | auto FindInSegment = [this]() { |
| 3401 | const ChainedFixupsSegment &SegInfo = Segments[InfoSegIndex]; |
| 3402 | while (PageIndex < SegInfo.PageStarts.size() && |
| 3403 | SegInfo.PageStarts[PageIndex] == MachO::DYLD_CHAINED_PTR_START_NONE) |
| 3404 | ++PageIndex; |
| 3405 | return PageIndex < SegInfo.PageStarts.size(); |
| 3406 | }; |
| 3407 | |
| 3408 | while (InfoSegIndex < Segments.size()) { |
| 3409 | if (FindInSegment()) { |
| 3410 | PageOffset = Segments[InfoSegIndex].PageStarts[PageIndex]; |
| 3411 | SegmentData = O->getSegmentContents(SegmentIndex: Segments[InfoSegIndex].SegIdx); |
| 3412 | return; |
| 3413 | } |
| 3414 | |
| 3415 | InfoSegIndex++; |
| 3416 | PageIndex = 0; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | void MachOChainedFixupEntry::moveToFirst() { |
| 3421 | MachOAbstractFixupEntry::moveToFirst(); |
| 3422 | if (Segments.empty()) { |
| 3423 | Done = true; |
| 3424 | return; |
| 3425 | } |
| 3426 | |
| 3427 | InfoSegIndex = 0; |
| 3428 | PageIndex = 0; |
| 3429 | |
| 3430 | findNextPageWithFixups(); |
| 3431 | moveNext(); |
| 3432 | } |
| 3433 | |
| 3434 | void MachOChainedFixupEntry::moveToEnd() { |
| 3435 | MachOAbstractFixupEntry::moveToEnd(); |
| 3436 | } |
| 3437 | |
| 3438 | void MachOChainedFixupEntry::moveNext() { |
| 3439 | ErrorAsOutParameter ErrAsOutParam(E); |
| 3440 | |
| 3441 | if (InfoSegIndex == Segments.size()) { |
| 3442 | Done = true; |
| 3443 | return; |
| 3444 | } |
| 3445 | |
| 3446 | const ChainedFixupsSegment &SegInfo = Segments[InfoSegIndex]; |
| 3447 | SegmentIndex = SegInfo.SegIdx; |
| 3448 | SegmentOffset = SegInfo.Header.page_size * PageIndex + PageOffset; |
| 3449 | |
| 3450 | // FIXME: Handle other pointer formats. |
| 3451 | uint16_t PointerFormat = SegInfo.Header.pointer_format; |
| 3452 | if (PointerFormat != MachO::DYLD_CHAINED_PTR_64 && |
| 3453 | PointerFormat != MachO::DYLD_CHAINED_PTR_64_OFFSET) { |
| 3454 | *E = createError(Err: "segment " + Twine(SegmentIndex) + |
| 3455 | " has unsupported chained fixup pointer_format " + |
| 3456 | Twine(PointerFormat)); |
| 3457 | moveToEnd(); |
| 3458 | return; |
| 3459 | } |
| 3460 | |
| 3461 | Ordinal = 0; |
| 3462 | Flags = 0; |
| 3463 | Addend = 0; |
| 3464 | PointerValue = 0; |
| 3465 | SymbolName = {}; |
| 3466 | |
| 3467 | if (SegmentOffset + sizeof(RawValue) > SegmentData.size()) { |
| 3468 | *E = malformedError(Msg: "fixup in segment " + Twine(SegmentIndex) + |
| 3469 | " at offset " + Twine(SegmentOffset) + |
| 3470 | " extends past segment's end" ); |
| 3471 | moveToEnd(); |
| 3472 | return; |
| 3473 | } |
| 3474 | |
| 3475 | static_assert(sizeof(RawValue) == sizeof(MachO::dyld_chained_import_addend)); |
| 3476 | memcpy(dest: &RawValue, src: SegmentData.data() + SegmentOffset, n: sizeof(RawValue)); |
| 3477 | if (O->isLittleEndian() != sys::IsLittleEndianHost) |
| 3478 | sys::swapByteOrder(Value&: RawValue); |
| 3479 | |
| 3480 | // The bit extraction below assumes little-endian fixup entries. |
| 3481 | assert(O->isLittleEndian() && "big-endian object should have been rejected " |
| 3482 | "by getDyldChainedFixupTargets()" ); |
| 3483 | auto Field = [this](uint8_t Right, uint8_t Count) { |
| 3484 | return (RawValue >> Right) & ((1ULL << Count) - 1); |
| 3485 | }; |
| 3486 | |
| 3487 | // The `bind` field (most significant bit) of the encoded fixup determines |
| 3488 | // whether it is dyld_chained_ptr_64_bind or dyld_chained_ptr_64_rebase. |
| 3489 | bool IsBind = Field(63, 1); |
| 3490 | Kind = IsBind ? FixupKind::Bind : FixupKind::Rebase; |
| 3491 | uint32_t Next = Field(51, 12); |
| 3492 | if (IsBind) { |
| 3493 | uint32_t ImportOrdinal = Field(0, 24); |
| 3494 | uint8_t InlineAddend = Field(24, 8); |
| 3495 | |
| 3496 | if (ImportOrdinal >= FixupTargets.size()) { |
| 3497 | *E = malformedError(Msg: "fixup in segment " + Twine(SegmentIndex) + |
| 3498 | " at offset " + Twine(SegmentOffset) + |
| 3499 | " has out-of range import ordinal " + |
| 3500 | Twine(ImportOrdinal)); |
| 3501 | moveToEnd(); |
| 3502 | return; |
| 3503 | } |
| 3504 | |
| 3505 | ChainedFixupTarget &Target = FixupTargets[ImportOrdinal]; |
| 3506 | Ordinal = Target.libOrdinal(); |
| 3507 | Addend = InlineAddend ? InlineAddend : Target.addend(); |
| 3508 | Flags = Target.weakImport() ? MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT : 0; |
| 3509 | SymbolName = Target.symbolName(); |
| 3510 | } else { |
| 3511 | uint64_t Target = Field(0, 36); |
| 3512 | uint64_t High8 = Field(36, 8); |
| 3513 | |
| 3514 | PointerValue = Target | (High8 << 56); |
| 3515 | if (PointerFormat == MachO::DYLD_CHAINED_PTR_64_OFFSET) |
| 3516 | PointerValue += textAddress(); |
| 3517 | } |
| 3518 | |
| 3519 | // The stride is 4 bytes for DYLD_CHAINED_PTR_64(_OFFSET). |
| 3520 | if (Next != 0) { |
| 3521 | PageOffset += 4 * Next; |
| 3522 | } else { |
| 3523 | ++PageIndex; |
| 3524 | findNextPageWithFixups(); |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | bool MachOChainedFixupEntry::operator==( |
| 3529 | const MachOChainedFixupEntry &Other) const { |
| 3530 | if (Done && Other.Done) |
| 3531 | return true; |
| 3532 | if (Done != Other.Done) |
| 3533 | return false; |
| 3534 | return InfoSegIndex == Other.InfoSegIndex && PageIndex == Other.PageIndex && |
| 3535 | PageOffset == Other.PageOffset; |
| 3536 | } |
| 3537 | |
| 3538 | MachORebaseEntry::MachORebaseEntry(Error *E, const MachOObjectFile *O, |
| 3539 | ArrayRef<uint8_t> Bytes, bool is64Bit) |
| 3540 | : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()), |
| 3541 | PointerSize(is64Bit ? 8 : 4) {} |
| 3542 | |
| 3543 | void MachORebaseEntry::moveToFirst() { |
| 3544 | Ptr = Opcodes.begin(); |
| 3545 | moveNext(); |
| 3546 | } |
| 3547 | |
| 3548 | void MachORebaseEntry::moveToEnd() { |
| 3549 | Ptr = Opcodes.end(); |
| 3550 | RemainingLoopCount = 0; |
| 3551 | Done = true; |
| 3552 | } |
| 3553 | |
| 3554 | void MachORebaseEntry::moveNext() { |
| 3555 | ErrorAsOutParameter ErrAsOutParam(E); |
| 3556 | // If in the middle of some loop, move to next rebasing in loop. |
| 3557 | SegmentOffset += AdvanceAmount; |
| 3558 | if (RemainingLoopCount) { |
| 3559 | --RemainingLoopCount; |
| 3560 | return; |
| 3561 | } |
| 3562 | |
| 3563 | bool More = true; |
| 3564 | while (More) { |
| 3565 | // REBASE_OPCODE_DONE is only used for padding if we are not aligned to |
| 3566 | // pointer size. Therefore it is possible to reach the end without ever |
| 3567 | // having seen REBASE_OPCODE_DONE. |
| 3568 | if (Ptr == Opcodes.end()) { |
| 3569 | Done = true; |
| 3570 | return; |
| 3571 | } |
| 3572 | |
| 3573 | // Parse next opcode and set up next loop. |
| 3574 | const uint8_t *OpcodeStart = Ptr; |
| 3575 | uint8_t Byte = *Ptr++; |
| 3576 | uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK; |
| 3577 | uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK; |
| 3578 | uint64_t Count, Skip; |
| 3579 | const char *error = nullptr; |
| 3580 | switch (Opcode) { |
| 3581 | case MachO::REBASE_OPCODE_DONE: |
| 3582 | More = false; |
| 3583 | Done = true; |
| 3584 | moveToEnd(); |
| 3585 | DEBUG_WITH_TYPE("mach-o-rebase" , dbgs() << "REBASE_OPCODE_DONE\n" ); |
| 3586 | break; |
| 3587 | case MachO::REBASE_OPCODE_SET_TYPE_IMM: |
| 3588 | RebaseType = ImmValue; |
| 3589 | if (RebaseType > MachO::REBASE_TYPE_TEXT_PCREL32) { |
| 3590 | *E = malformedError(Msg: "for REBASE_OPCODE_SET_TYPE_IMM bad bind type: " + |
| 3591 | Twine((int)RebaseType) + " for opcode at: 0x" + |
| 3592 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3593 | moveToEnd(); |
| 3594 | return; |
| 3595 | } |
| 3596 | DEBUG_WITH_TYPE( |
| 3597 | "mach-o-rebase" , |
| 3598 | dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: " |
| 3599 | << "RebaseType=" << (int) RebaseType << "\n" ); |
| 3600 | break; |
| 3601 | case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 3602 | SegmentIndex = ImmValue; |
| 3603 | SegmentOffset = readULEB128(error: &error); |
| 3604 | if (error) { |
| 3605 | *E = malformedError(Msg: "for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 3606 | Twine(error) + " for opcode at: 0x" + |
| 3607 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3608 | moveToEnd(); |
| 3609 | return; |
| 3610 | } |
| 3611 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3612 | PointerSize); |
| 3613 | if (error) { |
| 3614 | *E = malformedError(Msg: "for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 3615 | Twine(error) + " for opcode at: 0x" + |
| 3616 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3617 | moveToEnd(); |
| 3618 | return; |
| 3619 | } |
| 3620 | DEBUG_WITH_TYPE( |
| 3621 | "mach-o-rebase" , |
| 3622 | dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 3623 | << "SegmentIndex=" << SegmentIndex << ", " |
| 3624 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 3625 | << "\n" ); |
| 3626 | break; |
| 3627 | case MachO::REBASE_OPCODE_ADD_ADDR_ULEB: |
| 3628 | SegmentOffset += readULEB128(error: &error); |
| 3629 | if (error) { |
| 3630 | *E = malformedError(Msg: "for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) + |
| 3631 | " for opcode at: 0x" + |
| 3632 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3633 | moveToEnd(); |
| 3634 | return; |
| 3635 | } |
| 3636 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3637 | PointerSize); |
| 3638 | if (error) { |
| 3639 | *E = malformedError(Msg: "for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) + |
| 3640 | " for opcode at: 0x" + |
| 3641 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3642 | moveToEnd(); |
| 3643 | return; |
| 3644 | } |
| 3645 | DEBUG_WITH_TYPE("mach-o-rebase" , |
| 3646 | dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: " |
| 3647 | << format("SegmentOffset=0x%06X" , |
| 3648 | SegmentOffset) << "\n" ); |
| 3649 | break; |
| 3650 | case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED: |
| 3651 | SegmentOffset += ImmValue * PointerSize; |
| 3652 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3653 | PointerSize); |
| 3654 | if (error) { |
| 3655 | *E = malformedError(Msg: "for REBASE_OPCODE_ADD_ADDR_IMM_SCALED " + |
| 3656 | Twine(error) + " for opcode at: 0x" + |
| 3657 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3658 | moveToEnd(); |
| 3659 | return; |
| 3660 | } |
| 3661 | DEBUG_WITH_TYPE("mach-o-rebase" , |
| 3662 | dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: " |
| 3663 | << format("SegmentOffset=0x%06X" , |
| 3664 | SegmentOffset) << "\n" ); |
| 3665 | break; |
| 3666 | case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES: |
| 3667 | AdvanceAmount = PointerSize; |
| 3668 | Skip = 0; |
| 3669 | Count = ImmValue; |
| 3670 | if (ImmValue != 0) |
| 3671 | RemainingLoopCount = ImmValue - 1; |
| 3672 | else |
| 3673 | RemainingLoopCount = 0; |
| 3674 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3675 | PointerSize, Count, Skip); |
| 3676 | if (error) { |
| 3677 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_IMM_TIMES " + |
| 3678 | Twine(error) + " for opcode at: 0x" + |
| 3679 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3680 | moveToEnd(); |
| 3681 | return; |
| 3682 | } |
| 3683 | DEBUG_WITH_TYPE( |
| 3684 | "mach-o-rebase" , |
| 3685 | dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: " |
| 3686 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 3687 | << ", AdvanceAmount=" << AdvanceAmount |
| 3688 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3689 | << "\n" ); |
| 3690 | return; |
| 3691 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES: |
| 3692 | AdvanceAmount = PointerSize; |
| 3693 | Skip = 0; |
| 3694 | Count = readULEB128(error: &error); |
| 3695 | if (error) { |
| 3696 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " + |
| 3697 | Twine(error) + " for opcode at: 0x" + |
| 3698 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3699 | moveToEnd(); |
| 3700 | return; |
| 3701 | } |
| 3702 | if (Count != 0) |
| 3703 | RemainingLoopCount = Count - 1; |
| 3704 | else |
| 3705 | RemainingLoopCount = 0; |
| 3706 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3707 | PointerSize, Count, Skip); |
| 3708 | if (error) { |
| 3709 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " + |
| 3710 | Twine(error) + " for opcode at: 0x" + |
| 3711 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3712 | moveToEnd(); |
| 3713 | return; |
| 3714 | } |
| 3715 | DEBUG_WITH_TYPE( |
| 3716 | "mach-o-rebase" , |
| 3717 | dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: " |
| 3718 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 3719 | << ", AdvanceAmount=" << AdvanceAmount |
| 3720 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3721 | << "\n" ); |
| 3722 | return; |
| 3723 | case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: |
| 3724 | Skip = readULEB128(error: &error); |
| 3725 | if (error) { |
| 3726 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " + |
| 3727 | Twine(error) + " for opcode at: 0x" + |
| 3728 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3729 | moveToEnd(); |
| 3730 | return; |
| 3731 | } |
| 3732 | AdvanceAmount = Skip + PointerSize; |
| 3733 | Count = 1; |
| 3734 | RemainingLoopCount = 0; |
| 3735 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3736 | PointerSize, Count, Skip); |
| 3737 | if (error) { |
| 3738 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " + |
| 3739 | Twine(error) + " for opcode at: 0x" + |
| 3740 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3741 | moveToEnd(); |
| 3742 | return; |
| 3743 | } |
| 3744 | DEBUG_WITH_TYPE( |
| 3745 | "mach-o-rebase" , |
| 3746 | dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: " |
| 3747 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 3748 | << ", AdvanceAmount=" << AdvanceAmount |
| 3749 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3750 | << "\n" ); |
| 3751 | return; |
| 3752 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: |
| 3753 | Count = readULEB128(error: &error); |
| 3754 | if (error) { |
| 3755 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3756 | "ULEB " + |
| 3757 | Twine(error) + " for opcode at: 0x" + |
| 3758 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3759 | moveToEnd(); |
| 3760 | return; |
| 3761 | } |
| 3762 | if (Count != 0) |
| 3763 | RemainingLoopCount = Count - 1; |
| 3764 | else |
| 3765 | RemainingLoopCount = 0; |
| 3766 | Skip = readULEB128(error: &error); |
| 3767 | if (error) { |
| 3768 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3769 | "ULEB " + |
| 3770 | Twine(error) + " for opcode at: 0x" + |
| 3771 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3772 | moveToEnd(); |
| 3773 | return; |
| 3774 | } |
| 3775 | AdvanceAmount = Skip + PointerSize; |
| 3776 | |
| 3777 | error = O->RebaseEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 3778 | PointerSize, Count, Skip); |
| 3779 | if (error) { |
| 3780 | *E = malformedError(Msg: "for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3781 | "ULEB " + |
| 3782 | Twine(error) + " for opcode at: 0x" + |
| 3783 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3784 | moveToEnd(); |
| 3785 | return; |
| 3786 | } |
| 3787 | DEBUG_WITH_TYPE( |
| 3788 | "mach-o-rebase" , |
| 3789 | dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: " |
| 3790 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 3791 | << ", AdvanceAmount=" << AdvanceAmount |
| 3792 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3793 | << "\n" ); |
| 3794 | return; |
| 3795 | default: |
| 3796 | *E = malformedError(Msg: "bad rebase info (bad opcode value 0x" + |
| 3797 | Twine::utohexstr(Val: Opcode) + " for opcode at: 0x" + |
| 3798 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3799 | moveToEnd(); |
| 3800 | return; |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | uint64_t MachORebaseEntry::readULEB128(const char **error) { |
| 3806 | unsigned Count; |
| 3807 | uint64_t Result = decodeULEB128(p: Ptr, n: &Count, end: Opcodes.end(), error); |
| 3808 | Ptr += Count; |
| 3809 | if (Ptr > Opcodes.end()) |
| 3810 | Ptr = Opcodes.end(); |
| 3811 | return Result; |
| 3812 | } |
| 3813 | |
| 3814 | int32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; } |
| 3815 | |
| 3816 | uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; } |
| 3817 | |
| 3818 | StringRef MachORebaseEntry::typeName() const { |
| 3819 | switch (RebaseType) { |
| 3820 | case MachO::REBASE_TYPE_POINTER: |
| 3821 | return "pointer" ; |
| 3822 | case MachO::REBASE_TYPE_TEXT_ABSOLUTE32: |
| 3823 | return "text abs32" ; |
| 3824 | case MachO::REBASE_TYPE_TEXT_PCREL32: |
| 3825 | return "text rel32" ; |
| 3826 | } |
| 3827 | return "unknown" ; |
| 3828 | } |
| 3829 | |
| 3830 | // For use with the SegIndex of a checked Mach-O Rebase entry |
| 3831 | // to get the segment name. |
| 3832 | StringRef MachORebaseEntry::segmentName() const { |
| 3833 | return O->BindRebaseSegmentName(SegIndex: SegmentIndex); |
| 3834 | } |
| 3835 | |
| 3836 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry |
| 3837 | // to get the section name. |
| 3838 | StringRef MachORebaseEntry::sectionName() const { |
| 3839 | return O->BindRebaseSectionName(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 3840 | } |
| 3841 | |
| 3842 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry |
| 3843 | // to get the address. |
| 3844 | uint64_t MachORebaseEntry::address() const { |
| 3845 | return O->BindRebaseAddress(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 3846 | } |
| 3847 | |
| 3848 | bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const { |
| 3849 | #ifdef EXPENSIVE_CHECKS |
| 3850 | assert(Opcodes == Other.Opcodes && "compare iterators of different files" ); |
| 3851 | #else |
| 3852 | assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files" ); |
| 3853 | #endif |
| 3854 | return (Ptr == Other.Ptr) && |
| 3855 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 3856 | (Done == Other.Done); |
| 3857 | } |
| 3858 | |
| 3859 | iterator_range<rebase_iterator> |
| 3860 | MachOObjectFile::rebaseTable(Error &Err, MachOObjectFile *O, |
| 3861 | ArrayRef<uint8_t> Opcodes, bool is64) { |
| 3862 | if (O->BindRebaseSectionTable == nullptr) |
| 3863 | O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(args&: O); |
| 3864 | MachORebaseEntry Start(&Err, O, Opcodes, is64); |
| 3865 | Start.moveToFirst(); |
| 3866 | |
| 3867 | MachORebaseEntry Finish(&Err, O, Opcodes, is64); |
| 3868 | Finish.moveToEnd(); |
| 3869 | |
| 3870 | return make_range(x: rebase_iterator(Start), y: rebase_iterator(Finish)); |
| 3871 | } |
| 3872 | |
| 3873 | iterator_range<rebase_iterator> MachOObjectFile::rebaseTable(Error &Err) { |
| 3874 | return rebaseTable(Err, O: this, Opcodes: getDyldInfoRebaseOpcodes(), is64: is64Bit()); |
| 3875 | } |
| 3876 | |
| 3877 | MachOBindEntry::MachOBindEntry(Error *E, const MachOObjectFile *O, |
| 3878 | ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK) |
| 3879 | : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()), |
| 3880 | PointerSize(is64Bit ? 8 : 4), TableKind(BK) {} |
| 3881 | |
| 3882 | void MachOBindEntry::moveToFirst() { |
| 3883 | Ptr = Opcodes.begin(); |
| 3884 | moveNext(); |
| 3885 | } |
| 3886 | |
| 3887 | void MachOBindEntry::moveToEnd() { |
| 3888 | Ptr = Opcodes.end(); |
| 3889 | RemainingLoopCount = 0; |
| 3890 | Done = true; |
| 3891 | } |
| 3892 | |
| 3893 | void MachOBindEntry::moveNext() { |
| 3894 | ErrorAsOutParameter ErrAsOutParam(E); |
| 3895 | // If in the middle of some loop, move to next binding in loop. |
| 3896 | SegmentOffset += AdvanceAmount; |
| 3897 | if (RemainingLoopCount) { |
| 3898 | --RemainingLoopCount; |
| 3899 | return; |
| 3900 | } |
| 3901 | |
| 3902 | bool More = true; |
| 3903 | while (More) { |
| 3904 | // BIND_OPCODE_DONE is only used for padding if we are not aligned to |
| 3905 | // pointer size. Therefore it is possible to reach the end without ever |
| 3906 | // having seen BIND_OPCODE_DONE. |
| 3907 | if (Ptr == Opcodes.end()) { |
| 3908 | Done = true; |
| 3909 | return; |
| 3910 | } |
| 3911 | |
| 3912 | // Parse next opcode and set up next loop. |
| 3913 | const uint8_t *OpcodeStart = Ptr; |
| 3914 | uint8_t Byte = *Ptr++; |
| 3915 | uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK; |
| 3916 | uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK; |
| 3917 | int8_t SignExtended; |
| 3918 | const uint8_t *SymStart; |
| 3919 | uint64_t Count, Skip; |
| 3920 | const char *error = nullptr; |
| 3921 | switch (Opcode) { |
| 3922 | case MachO::BIND_OPCODE_DONE: |
| 3923 | if (TableKind == Kind::Lazy) { |
| 3924 | // Lazying bindings have a DONE opcode between entries. Need to ignore |
| 3925 | // it to advance to next entry. But need not if this is last entry. |
| 3926 | bool NotLastEntry = false; |
| 3927 | for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) { |
| 3928 | if (*P) { |
| 3929 | NotLastEntry = true; |
| 3930 | } |
| 3931 | } |
| 3932 | if (NotLastEntry) |
| 3933 | break; |
| 3934 | } |
| 3935 | More = false; |
| 3936 | moveToEnd(); |
| 3937 | DEBUG_WITH_TYPE("mach-o-bind" , dbgs() << "BIND_OPCODE_DONE\n" ); |
| 3938 | break; |
| 3939 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: |
| 3940 | if (TableKind == Kind::Weak) { |
| 3941 | *E = malformedError(Msg: "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM not allowed in " |
| 3942 | "weak bind table for opcode at: 0x" + |
| 3943 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3944 | moveToEnd(); |
| 3945 | return; |
| 3946 | } |
| 3947 | Ordinal = ImmValue; |
| 3948 | LibraryOrdinalSet = true; |
| 3949 | if (ImmValue > O->getLibraryCount()) { |
| 3950 | *E = malformedError(Msg: "for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad " |
| 3951 | "library ordinal: " + |
| 3952 | Twine((int)ImmValue) + " (max " + |
| 3953 | Twine((int)O->getLibraryCount()) + |
| 3954 | ") for opcode at: 0x" + |
| 3955 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3956 | moveToEnd(); |
| 3957 | return; |
| 3958 | } |
| 3959 | DEBUG_WITH_TYPE( |
| 3960 | "mach-o-bind" , |
| 3961 | dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: " |
| 3962 | << "Ordinal=" << Ordinal << "\n" ); |
| 3963 | break; |
| 3964 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: |
| 3965 | if (TableKind == Kind::Weak) { |
| 3966 | *E = malformedError(Msg: "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB not allowed in " |
| 3967 | "weak bind table for opcode at: 0x" + |
| 3968 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3969 | moveToEnd(); |
| 3970 | return; |
| 3971 | } |
| 3972 | Ordinal = readULEB128(error: &error); |
| 3973 | LibraryOrdinalSet = true; |
| 3974 | if (error) { |
| 3975 | *E = malformedError(Msg: "for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB " + |
| 3976 | Twine(error) + " for opcode at: 0x" + |
| 3977 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3978 | moveToEnd(); |
| 3979 | return; |
| 3980 | } |
| 3981 | if (Ordinal > (int)O->getLibraryCount()) { |
| 3982 | *E = malformedError(Msg: "for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad " |
| 3983 | "library ordinal: " + |
| 3984 | Twine((int)Ordinal) + " (max " + |
| 3985 | Twine((int)O->getLibraryCount()) + |
| 3986 | ") for opcode at: 0x" + |
| 3987 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 3988 | moveToEnd(); |
| 3989 | return; |
| 3990 | } |
| 3991 | DEBUG_WITH_TYPE( |
| 3992 | "mach-o-bind" , |
| 3993 | dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: " |
| 3994 | << "Ordinal=" << Ordinal << "\n" ); |
| 3995 | break; |
| 3996 | case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: |
| 3997 | if (TableKind == Kind::Weak) { |
| 3998 | *E = malformedError(Msg: "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM not allowed in " |
| 3999 | "weak bind table for opcode at: 0x" + |
| 4000 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4001 | moveToEnd(); |
| 4002 | return; |
| 4003 | } |
| 4004 | if (ImmValue) { |
| 4005 | SignExtended = MachO::BIND_OPCODE_MASK | ImmValue; |
| 4006 | Ordinal = SignExtended; |
| 4007 | if (Ordinal < MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP) { |
| 4008 | *E = malformedError(Msg: "for BIND_OPCODE_SET_DYLIB_SPECIAL_IMM unknown " |
| 4009 | "special ordinal: " + |
| 4010 | Twine((int)Ordinal) + " for opcode at: 0x" + |
| 4011 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4012 | moveToEnd(); |
| 4013 | return; |
| 4014 | } |
| 4015 | } else |
| 4016 | Ordinal = 0; |
| 4017 | LibraryOrdinalSet = true; |
| 4018 | DEBUG_WITH_TYPE( |
| 4019 | "mach-o-bind" , |
| 4020 | dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: " |
| 4021 | << "Ordinal=" << Ordinal << "\n" ); |
| 4022 | break; |
| 4023 | case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: |
| 4024 | Flags = ImmValue; |
| 4025 | SymStart = Ptr; |
| 4026 | while (*Ptr && (Ptr < Opcodes.end())) { |
| 4027 | ++Ptr; |
| 4028 | } |
| 4029 | if (Ptr == Opcodes.end()) { |
| 4030 | *E = malformedError( |
| 4031 | Msg: "for BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM " |
| 4032 | "symbol name extends past opcodes for opcode at: 0x" + |
| 4033 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4034 | moveToEnd(); |
| 4035 | return; |
| 4036 | } |
| 4037 | SymbolName = StringRef(reinterpret_cast<const char*>(SymStart), |
| 4038 | Ptr-SymStart); |
| 4039 | ++Ptr; |
| 4040 | DEBUG_WITH_TYPE( |
| 4041 | "mach-o-bind" , |
| 4042 | dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: " |
| 4043 | << "SymbolName=" << SymbolName << "\n" ); |
| 4044 | if (TableKind == Kind::Weak) { |
| 4045 | if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) |
| 4046 | return; |
| 4047 | } |
| 4048 | break; |
| 4049 | case MachO::BIND_OPCODE_SET_TYPE_IMM: |
| 4050 | BindType = ImmValue; |
| 4051 | if (ImmValue > MachO::BIND_TYPE_TEXT_PCREL32) { |
| 4052 | *E = malformedError(Msg: "for BIND_OPCODE_SET_TYPE_IMM bad bind type: " + |
| 4053 | Twine((int)ImmValue) + " for opcode at: 0x" + |
| 4054 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4055 | moveToEnd(); |
| 4056 | return; |
| 4057 | } |
| 4058 | DEBUG_WITH_TYPE( |
| 4059 | "mach-o-bind" , |
| 4060 | dbgs() << "BIND_OPCODE_SET_TYPE_IMM: " |
| 4061 | << "BindType=" << (int)BindType << "\n" ); |
| 4062 | break; |
| 4063 | case MachO::BIND_OPCODE_SET_ADDEND_SLEB: |
| 4064 | Addend = readSLEB128(error: &error); |
| 4065 | if (error) { |
| 4066 | *E = malformedError(Msg: "for BIND_OPCODE_SET_ADDEND_SLEB " + Twine(error) + |
| 4067 | " for opcode at: 0x" + |
| 4068 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4069 | moveToEnd(); |
| 4070 | return; |
| 4071 | } |
| 4072 | DEBUG_WITH_TYPE( |
| 4073 | "mach-o-bind" , |
| 4074 | dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: " |
| 4075 | << "Addend=" << Addend << "\n" ); |
| 4076 | break; |
| 4077 | case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 4078 | SegmentIndex = ImmValue; |
| 4079 | SegmentOffset = readULEB128(error: &error); |
| 4080 | if (error) { |
| 4081 | *E = malformedError(Msg: "for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 4082 | Twine(error) + " for opcode at: 0x" + |
| 4083 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4084 | moveToEnd(); |
| 4085 | return; |
| 4086 | } |
| 4087 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 4088 | PointerSize); |
| 4089 | if (error) { |
| 4090 | *E = malformedError(Msg: "for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 4091 | Twine(error) + " for opcode at: 0x" + |
| 4092 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4093 | moveToEnd(); |
| 4094 | return; |
| 4095 | } |
| 4096 | DEBUG_WITH_TYPE( |
| 4097 | "mach-o-bind" , |
| 4098 | dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 4099 | << "SegmentIndex=" << SegmentIndex << ", " |
| 4100 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 4101 | << "\n" ); |
| 4102 | break; |
| 4103 | case MachO::BIND_OPCODE_ADD_ADDR_ULEB: |
| 4104 | SegmentOffset += readULEB128(error: &error); |
| 4105 | if (error) { |
| 4106 | *E = malformedError(Msg: "for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) + |
| 4107 | " for opcode at: 0x" + |
| 4108 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4109 | moveToEnd(); |
| 4110 | return; |
| 4111 | } |
| 4112 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 4113 | PointerSize); |
| 4114 | if (error) { |
| 4115 | *E = malformedError(Msg: "for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) + |
| 4116 | " for opcode at: 0x" + |
| 4117 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4118 | moveToEnd(); |
| 4119 | return; |
| 4120 | } |
| 4121 | DEBUG_WITH_TYPE("mach-o-bind" , |
| 4122 | dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: " |
| 4123 | << format("SegmentOffset=0x%06X" , |
| 4124 | SegmentOffset) << "\n" ); |
| 4125 | break; |
| 4126 | case MachO::BIND_OPCODE_DO_BIND: |
| 4127 | AdvanceAmount = PointerSize; |
| 4128 | RemainingLoopCount = 0; |
| 4129 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 4130 | PointerSize); |
| 4131 | if (error) { |
| 4132 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND " + Twine(error) + |
| 4133 | " for opcode at: 0x" + |
| 4134 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4135 | moveToEnd(); |
| 4136 | return; |
| 4137 | } |
| 4138 | if (SymbolName == StringRef()) { |
| 4139 | *E = malformedError( |
| 4140 | Msg: "for BIND_OPCODE_DO_BIND missing preceding " |
| 4141 | "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode at: 0x" + |
| 4142 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4143 | moveToEnd(); |
| 4144 | return; |
| 4145 | } |
| 4146 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 4147 | *E = |
| 4148 | malformedError(Msg: "for BIND_OPCODE_DO_BIND missing preceding " |
| 4149 | "BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" + |
| 4150 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4151 | moveToEnd(); |
| 4152 | return; |
| 4153 | } |
| 4154 | DEBUG_WITH_TYPE("mach-o-bind" , |
| 4155 | dbgs() << "BIND_OPCODE_DO_BIND: " |
| 4156 | << format("SegmentOffset=0x%06X" , |
| 4157 | SegmentOffset) << "\n" ); |
| 4158 | return; |
| 4159 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: |
| 4160 | if (TableKind == Kind::Lazy) { |
| 4161 | *E = malformedError(Msg: "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB not allowed in " |
| 4162 | "lazy bind table for opcode at: 0x" + |
| 4163 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4164 | moveToEnd(); |
| 4165 | return; |
| 4166 | } |
| 4167 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 4168 | PointerSize); |
| 4169 | if (error) { |
| 4170 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " + |
| 4171 | Twine(error) + " for opcode at: 0x" + |
| 4172 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4173 | moveToEnd(); |
| 4174 | return; |
| 4175 | } |
| 4176 | if (SymbolName == StringRef()) { |
| 4177 | *E = malformedError( |
| 4178 | Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing " |
| 4179 | "preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode " |
| 4180 | "at: 0x" + |
| 4181 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4182 | moveToEnd(); |
| 4183 | return; |
| 4184 | } |
| 4185 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 4186 | *E = malformedError( |
| 4187 | Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing " |
| 4188 | "preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" + |
| 4189 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4190 | moveToEnd(); |
| 4191 | return; |
| 4192 | } |
| 4193 | AdvanceAmount = readULEB128(error: &error) + PointerSize; |
| 4194 | if (error) { |
| 4195 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " + |
| 4196 | Twine(error) + " for opcode at: 0x" + |
| 4197 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4198 | moveToEnd(); |
| 4199 | return; |
| 4200 | } |
| 4201 | // Note, this is not really an error until the next bind but make no sense |
| 4202 | // for a BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB to not be followed by another |
| 4203 | // bind operation. |
| 4204 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset + |
| 4205 | AdvanceAmount, PointerSize); |
| 4206 | if (error) { |
| 4207 | *E = malformedError(Msg: "for BIND_OPCODE_ADD_ADDR_ULEB (after adding " |
| 4208 | "ULEB) " + |
| 4209 | Twine(error) + " for opcode at: 0x" + |
| 4210 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4211 | moveToEnd(); |
| 4212 | return; |
| 4213 | } |
| 4214 | RemainingLoopCount = 0; |
| 4215 | DEBUG_WITH_TYPE( |
| 4216 | "mach-o-bind" , |
| 4217 | dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: " |
| 4218 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 4219 | << ", AdvanceAmount=" << AdvanceAmount |
| 4220 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 4221 | << "\n" ); |
| 4222 | return; |
| 4223 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: |
| 4224 | if (TableKind == Kind::Lazy) { |
| 4225 | *E = malformedError(Msg: "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED not " |
| 4226 | "allowed in lazy bind table for opcode at: 0x" + |
| 4227 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4228 | moveToEnd(); |
| 4229 | return; |
| 4230 | } |
| 4231 | if (SymbolName == StringRef()) { |
| 4232 | *E = malformedError( |
| 4233 | Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " |
| 4234 | "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for " |
| 4235 | "opcode at: 0x" + |
| 4236 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4237 | moveToEnd(); |
| 4238 | return; |
| 4239 | } |
| 4240 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 4241 | *E = malformedError( |
| 4242 | Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " |
| 4243 | "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode " |
| 4244 | "at: 0x" + |
| 4245 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4246 | moveToEnd(); |
| 4247 | return; |
| 4248 | } |
| 4249 | AdvanceAmount = ImmValue * PointerSize + PointerSize; |
| 4250 | RemainingLoopCount = 0; |
| 4251 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset + |
| 4252 | AdvanceAmount, PointerSize); |
| 4253 | if (error) { |
| 4254 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " + |
| 4255 | Twine(error) + " for opcode at: 0x" + |
| 4256 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4257 | moveToEnd(); |
| 4258 | return; |
| 4259 | } |
| 4260 | DEBUG_WITH_TYPE("mach-o-bind" , |
| 4261 | dbgs() |
| 4262 | << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: " |
| 4263 | << format("SegmentOffset=0x%06X" , SegmentOffset) << "\n" ); |
| 4264 | return; |
| 4265 | case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: |
| 4266 | if (TableKind == Kind::Lazy) { |
| 4267 | *E = malformedError(Msg: "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB not " |
| 4268 | "allowed in lazy bind table for opcode at: 0x" + |
| 4269 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4270 | moveToEnd(); |
| 4271 | return; |
| 4272 | } |
| 4273 | Count = readULEB128(error: &error); |
| 4274 | if (Count != 0) |
| 4275 | RemainingLoopCount = Count - 1; |
| 4276 | else |
| 4277 | RemainingLoopCount = 0; |
| 4278 | if (error) { |
| 4279 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 4280 | " (count value) " + |
| 4281 | Twine(error) + " for opcode at: 0x" + |
| 4282 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4283 | moveToEnd(); |
| 4284 | return; |
| 4285 | } |
| 4286 | Skip = readULEB128(error: &error); |
| 4287 | AdvanceAmount = Skip + PointerSize; |
| 4288 | if (error) { |
| 4289 | *E = malformedError(Msg: "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 4290 | " (skip value) " + |
| 4291 | Twine(error) + " for opcode at: 0x" + |
| 4292 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4293 | moveToEnd(); |
| 4294 | return; |
| 4295 | } |
| 4296 | if (SymbolName == StringRef()) { |
| 4297 | *E = malformedError( |
| 4298 | Msg: "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 4299 | "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for " |
| 4300 | "opcode at: 0x" + |
| 4301 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4302 | moveToEnd(); |
| 4303 | return; |
| 4304 | } |
| 4305 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 4306 | *E = malformedError( |
| 4307 | Msg: "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 4308 | "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode " |
| 4309 | "at: 0x" + |
| 4310 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4311 | moveToEnd(); |
| 4312 | return; |
| 4313 | } |
| 4314 | error = O->BindEntryCheckSegAndOffsets(SegIndex: SegmentIndex, SegOffset: SegmentOffset, |
| 4315 | PointerSize, Count, Skip); |
| 4316 | if (error) { |
| 4317 | *E = |
| 4318 | malformedError(Msg: "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " + |
| 4319 | Twine(error) + " for opcode at: 0x" + |
| 4320 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4321 | moveToEnd(); |
| 4322 | return; |
| 4323 | } |
| 4324 | DEBUG_WITH_TYPE( |
| 4325 | "mach-o-bind" , |
| 4326 | dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: " |
| 4327 | << format("SegmentOffset=0x%06X" , SegmentOffset) |
| 4328 | << ", AdvanceAmount=" << AdvanceAmount |
| 4329 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 4330 | << "\n" ); |
| 4331 | return; |
| 4332 | default: |
| 4333 | *E = malformedError(Msg: "bad bind info (bad opcode value 0x" + |
| 4334 | Twine::utohexstr(Val: Opcode) + " for opcode at: 0x" + |
| 4335 | Twine::utohexstr(Val: OpcodeStart - Opcodes.begin())); |
| 4336 | moveToEnd(); |
| 4337 | return; |
| 4338 | } |
| 4339 | } |
| 4340 | } |
| 4341 | |
| 4342 | uint64_t MachOBindEntry::readULEB128(const char **error) { |
| 4343 | unsigned Count; |
| 4344 | uint64_t Result = decodeULEB128(p: Ptr, n: &Count, end: Opcodes.end(), error); |
| 4345 | Ptr += Count; |
| 4346 | if (Ptr > Opcodes.end()) |
| 4347 | Ptr = Opcodes.end(); |
| 4348 | return Result; |
| 4349 | } |
| 4350 | |
| 4351 | int64_t MachOBindEntry::readSLEB128(const char **error) { |
| 4352 | unsigned Count; |
| 4353 | int64_t Result = decodeSLEB128(p: Ptr, n: &Count, end: Opcodes.end(), error); |
| 4354 | Ptr += Count; |
| 4355 | if (Ptr > Opcodes.end()) |
| 4356 | Ptr = Opcodes.end(); |
| 4357 | return Result; |
| 4358 | } |
| 4359 | |
| 4360 | int32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; } |
| 4361 | |
| 4362 | uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; } |
| 4363 | |
| 4364 | StringRef MachOBindEntry::typeName() const { |
| 4365 | switch (BindType) { |
| 4366 | case MachO::BIND_TYPE_POINTER: |
| 4367 | return "pointer" ; |
| 4368 | case MachO::BIND_TYPE_TEXT_ABSOLUTE32: |
| 4369 | return "text abs32" ; |
| 4370 | case MachO::BIND_TYPE_TEXT_PCREL32: |
| 4371 | return "text rel32" ; |
| 4372 | } |
| 4373 | return "unknown" ; |
| 4374 | } |
| 4375 | |
| 4376 | StringRef MachOBindEntry::symbolName() const { return SymbolName; } |
| 4377 | |
| 4378 | int64_t MachOBindEntry::addend() const { return Addend; } |
| 4379 | |
| 4380 | uint32_t MachOBindEntry::flags() const { return Flags; } |
| 4381 | |
| 4382 | int MachOBindEntry::ordinal() const { return Ordinal; } |
| 4383 | |
| 4384 | // For use with the SegIndex of a checked Mach-O Bind entry |
| 4385 | // to get the segment name. |
| 4386 | StringRef MachOBindEntry::segmentName() const { |
| 4387 | return O->BindRebaseSegmentName(SegIndex: SegmentIndex); |
| 4388 | } |
| 4389 | |
| 4390 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry |
| 4391 | // to get the section name. |
| 4392 | StringRef MachOBindEntry::sectionName() const { |
| 4393 | return O->BindRebaseSectionName(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 4394 | } |
| 4395 | |
| 4396 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry |
| 4397 | // to get the address. |
| 4398 | uint64_t MachOBindEntry::address() const { |
| 4399 | return O->BindRebaseAddress(SegIndex: SegmentIndex, SegOffset: SegmentOffset); |
| 4400 | } |
| 4401 | |
| 4402 | bool MachOBindEntry::operator==(const MachOBindEntry &Other) const { |
| 4403 | #ifdef EXPENSIVE_CHECKS |
| 4404 | assert(Opcodes == Other.Opcodes && "compare iterators of different files" ); |
| 4405 | #else |
| 4406 | assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files" ); |
| 4407 | #endif |
| 4408 | return (Ptr == Other.Ptr) && |
| 4409 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 4410 | (Done == Other.Done); |
| 4411 | } |
| 4412 | |
| 4413 | // Build table of sections so SegIndex/SegOffset pairs can be translated. |
| 4414 | BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) { |
| 4415 | uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0; |
| 4416 | StringRef CurSegName; |
| 4417 | uint64_t CurSegAddress; |
| 4418 | for (const SectionRef &Section : Obj->sections()) { |
| 4419 | SectionInfo Info; |
| 4420 | Expected<StringRef> NameOrErr = Section.getName(); |
| 4421 | if (!NameOrErr) |
| 4422 | consumeError(Err: NameOrErr.takeError()); |
| 4423 | else |
| 4424 | Info.SectionName = *NameOrErr; |
| 4425 | Info.Address = Section.getAddress(); |
| 4426 | Info.Size = Section.getSize(); |
| 4427 | Info.SegmentName = |
| 4428 | Obj->getSectionFinalSegmentName(Sec: Section.getRawDataRefImpl()); |
| 4429 | if (Info.SegmentName != CurSegName) { |
| 4430 | ++CurSegIndex; |
| 4431 | CurSegName = Info.SegmentName; |
| 4432 | CurSegAddress = Info.Address; |
| 4433 | } |
| 4434 | Info.SegmentIndex = CurSegIndex - 1; |
| 4435 | Info.OffsetInSegment = Info.Address - CurSegAddress; |
| 4436 | Info.SegmentStartAddress = CurSegAddress; |
| 4437 | Sections.push_back(Elt: Info); |
| 4438 | } |
| 4439 | MaxSegIndex = CurSegIndex; |
| 4440 | } |
| 4441 | |
| 4442 | // For use with a SegIndex, SegOffset, and PointerSize triple in |
| 4443 | // MachOBindEntry::moveNext() to validate a MachOBindEntry or MachORebaseEntry. |
| 4444 | // |
| 4445 | // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists |
| 4446 | // that fully contains a pointer at that location. Multiple fixups in a bind |
| 4447 | // (such as with the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode) can |
| 4448 | // be tested via the Count and Skip parameters. |
| 4449 | const char *BindRebaseSegInfo::checkSegAndOffsets(int32_t SegIndex, |
| 4450 | uint64_t SegOffset, |
| 4451 | uint8_t PointerSize, |
| 4452 | uint64_t Count, |
| 4453 | uint64_t Skip) { |
| 4454 | if (SegIndex == -1) |
| 4455 | return "missing preceding *_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB" ; |
| 4456 | if (SegIndex >= MaxSegIndex) |
| 4457 | return "bad segIndex (too large)" ; |
| 4458 | for (uint64_t i = 0; i < Count; ++i) { |
| 4459 | uint64_t Start = SegOffset + i * (PointerSize + Skip); |
| 4460 | uint64_t End = Start + PointerSize; |
| 4461 | bool Found = false; |
| 4462 | for (const SectionInfo &SI : Sections) { |
| 4463 | if (SI.SegmentIndex != SegIndex) |
| 4464 | continue; |
| 4465 | if ((SI.OffsetInSegment<=Start) && (Start<(SI.OffsetInSegment+SI.Size))) { |
| 4466 | if (End <= SI.OffsetInSegment + SI.Size) { |
| 4467 | Found = true; |
| 4468 | break; |
| 4469 | } |
| 4470 | else |
| 4471 | return "bad offset, extends beyond section boundary" ; |
| 4472 | } |
| 4473 | } |
| 4474 | if (!Found) |
| 4475 | return "bad offset, not in section" ; |
| 4476 | } |
| 4477 | return nullptr; |
| 4478 | } |
| 4479 | |
| 4480 | // For use with the SegIndex of a checked Mach-O Bind or Rebase entry |
| 4481 | // to get the segment name. |
| 4482 | StringRef BindRebaseSegInfo::segmentName(int32_t SegIndex) { |
| 4483 | for (const SectionInfo &SI : Sections) { |
| 4484 | if (SI.SegmentIndex == SegIndex) |
| 4485 | return SI.SegmentName; |
| 4486 | } |
| 4487 | llvm_unreachable("invalid SegIndex" ); |
| 4488 | } |
| 4489 | |
| 4490 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 4491 | // to get the SectionInfo. |
| 4492 | const BindRebaseSegInfo::SectionInfo &BindRebaseSegInfo::findSection( |
| 4493 | int32_t SegIndex, uint64_t SegOffset) { |
| 4494 | for (const SectionInfo &SI : Sections) { |
| 4495 | if (SI.SegmentIndex != SegIndex) |
| 4496 | continue; |
| 4497 | if (SI.OffsetInSegment > SegOffset) |
| 4498 | continue; |
| 4499 | if (SegOffset >= (SI.OffsetInSegment + SI.Size)) |
| 4500 | continue; |
| 4501 | return SI; |
| 4502 | } |
| 4503 | llvm_unreachable("SegIndex and SegOffset not in any section" ); |
| 4504 | } |
| 4505 | |
| 4506 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 4507 | // entry to get the section name. |
| 4508 | StringRef BindRebaseSegInfo::sectionName(int32_t SegIndex, |
| 4509 | uint64_t SegOffset) { |
| 4510 | return findSection(SegIndex, SegOffset).SectionName; |
| 4511 | } |
| 4512 | |
| 4513 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 4514 | // entry to get the address. |
| 4515 | uint64_t BindRebaseSegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 4516 | const SectionInfo &SI = findSection(SegIndex, SegOffset: OffsetInSeg); |
| 4517 | return SI.SegmentStartAddress + OffsetInSeg; |
| 4518 | } |
| 4519 | |
| 4520 | iterator_range<bind_iterator> |
| 4521 | MachOObjectFile::bindTable(Error &Err, MachOObjectFile *O, |
| 4522 | ArrayRef<uint8_t> Opcodes, bool is64, |
| 4523 | MachOBindEntry::Kind BKind) { |
| 4524 | if (O->BindRebaseSectionTable == nullptr) |
| 4525 | O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(args&: O); |
| 4526 | MachOBindEntry Start(&Err, O, Opcodes, is64, BKind); |
| 4527 | Start.moveToFirst(); |
| 4528 | |
| 4529 | MachOBindEntry Finish(&Err, O, Opcodes, is64, BKind); |
| 4530 | Finish.moveToEnd(); |
| 4531 | |
| 4532 | return make_range(x: bind_iterator(Start), y: bind_iterator(Finish)); |
| 4533 | } |
| 4534 | |
| 4535 | iterator_range<bind_iterator> MachOObjectFile::bindTable(Error &Err) { |
| 4536 | return bindTable(Err, O: this, Opcodes: getDyldInfoBindOpcodes(), is64: is64Bit(), |
| 4537 | BKind: MachOBindEntry::Kind::Regular); |
| 4538 | } |
| 4539 | |
| 4540 | iterator_range<bind_iterator> MachOObjectFile::lazyBindTable(Error &Err) { |
| 4541 | return bindTable(Err, O: this, Opcodes: getDyldInfoLazyBindOpcodes(), is64: is64Bit(), |
| 4542 | BKind: MachOBindEntry::Kind::Lazy); |
| 4543 | } |
| 4544 | |
| 4545 | iterator_range<bind_iterator> MachOObjectFile::weakBindTable(Error &Err) { |
| 4546 | return bindTable(Err, O: this, Opcodes: getDyldInfoWeakBindOpcodes(), is64: is64Bit(), |
| 4547 | BKind: MachOBindEntry::Kind::Weak); |
| 4548 | } |
| 4549 | |
| 4550 | iterator_range<fixup_iterator> MachOObjectFile::fixupTable(Error &Err) { |
| 4551 | if (BindRebaseSectionTable == nullptr) |
| 4552 | BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(args: this); |
| 4553 | |
| 4554 | MachOChainedFixupEntry Start(&Err, this, true); |
| 4555 | Start.moveToFirst(); |
| 4556 | |
| 4557 | MachOChainedFixupEntry Finish(&Err, this, false); |
| 4558 | Finish.moveToEnd(); |
| 4559 | |
| 4560 | return make_range(x: fixup_iterator(Start), y: fixup_iterator(Finish)); |
| 4561 | } |
| 4562 | |
| 4563 | MachOObjectFile::load_command_iterator |
| 4564 | MachOObjectFile::begin_load_commands() const { |
| 4565 | return LoadCommands.begin(); |
| 4566 | } |
| 4567 | |
| 4568 | MachOObjectFile::load_command_iterator |
| 4569 | MachOObjectFile::end_load_commands() const { |
| 4570 | return LoadCommands.end(); |
| 4571 | } |
| 4572 | |
| 4573 | iterator_range<MachOObjectFile::load_command_iterator> |
| 4574 | MachOObjectFile::load_commands() const { |
| 4575 | return make_range(x: begin_load_commands(), y: end_load_commands()); |
| 4576 | } |
| 4577 | |
| 4578 | StringRef |
| 4579 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 4580 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 4581 | return parseSegmentOrSectionName(P: Raw.data()); |
| 4582 | } |
| 4583 | |
| 4584 | ArrayRef<char> |
| 4585 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
| 4586 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier" ); |
| 4587 | const section_base *Base = |
| 4588 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
| 4589 | return ArrayRef(Base->sectname); |
| 4590 | } |
| 4591 | |
| 4592 | ArrayRef<char> |
| 4593 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
| 4594 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier" ); |
| 4595 | const section_base *Base = |
| 4596 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
| 4597 | return ArrayRef(Base->segname); |
| 4598 | } |
| 4599 | |
| 4600 | bool |
| 4601 | MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) |
| 4602 | const { |
| 4603 | if (getCPUType(O: *this) == MachO::CPU_TYPE_X86_64) |
| 4604 | return false; |
| 4605 | return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; |
| 4606 | } |
| 4607 | |
| 4608 | unsigned MachOObjectFile::getPlainRelocationSymbolNum( |
| 4609 | const MachO::any_relocation_info &RE) const { |
| 4610 | if (isLittleEndian()) |
| 4611 | return RE.r_word1 & 0xffffff; |
| 4612 | return RE.r_word1 >> 8; |
| 4613 | } |
| 4614 | |
| 4615 | bool MachOObjectFile::getPlainRelocationExternal( |
| 4616 | const MachO::any_relocation_info &RE) const { |
| 4617 | if (isLittleEndian()) |
| 4618 | return (RE.r_word1 >> 27) & 1; |
| 4619 | return (RE.r_word1 >> 4) & 1; |
| 4620 | } |
| 4621 | |
| 4622 | bool MachOObjectFile::getScatteredRelocationScattered( |
| 4623 | const MachO::any_relocation_info &RE) const { |
| 4624 | return RE.r_word0 >> 31; |
| 4625 | } |
| 4626 | |
| 4627 | uint32_t MachOObjectFile::getScatteredRelocationValue( |
| 4628 | const MachO::any_relocation_info &RE) const { |
| 4629 | return RE.r_word1; |
| 4630 | } |
| 4631 | |
| 4632 | uint32_t MachOObjectFile::getScatteredRelocationType( |
| 4633 | const MachO::any_relocation_info &RE) const { |
| 4634 | return (RE.r_word0 >> 24) & 0xf; |
| 4635 | } |
| 4636 | |
| 4637 | unsigned MachOObjectFile::getAnyRelocationAddress( |
| 4638 | const MachO::any_relocation_info &RE) const { |
| 4639 | if (isRelocationScattered(RE)) |
| 4640 | return getScatteredRelocationAddress(RE); |
| 4641 | return getPlainRelocationAddress(RE); |
| 4642 | } |
| 4643 | |
| 4644 | unsigned MachOObjectFile::getAnyRelocationPCRel( |
| 4645 | const MachO::any_relocation_info &RE) const { |
| 4646 | if (isRelocationScattered(RE)) |
| 4647 | return getScatteredRelocationPCRel(RE); |
| 4648 | return getPlainRelocationPCRel(O: *this, RE); |
| 4649 | } |
| 4650 | |
| 4651 | unsigned MachOObjectFile::getAnyRelocationLength( |
| 4652 | const MachO::any_relocation_info &RE) const { |
| 4653 | if (isRelocationScattered(RE)) |
| 4654 | return getScatteredRelocationLength(RE); |
| 4655 | return getPlainRelocationLength(O: *this, RE); |
| 4656 | } |
| 4657 | |
| 4658 | unsigned |
| 4659 | MachOObjectFile::getAnyRelocationType( |
| 4660 | const MachO::any_relocation_info &RE) const { |
| 4661 | if (isRelocationScattered(RE)) |
| 4662 | return getScatteredRelocationType(RE); |
| 4663 | return getPlainRelocationType(O: *this, RE); |
| 4664 | } |
| 4665 | |
| 4666 | SectionRef |
| 4667 | MachOObjectFile::getAnyRelocationSection( |
| 4668 | const MachO::any_relocation_info &RE) const { |
| 4669 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
| 4670 | return *section_end(); |
| 4671 | unsigned SecNum = getPlainRelocationSymbolNum(RE); |
| 4672 | if (SecNum == MachO::R_ABS || SecNum > Sections.size()) |
| 4673 | return *section_end(); |
| 4674 | DataRefImpl DRI; |
| 4675 | DRI.d.a = SecNum - 1; |
| 4676 | return SectionRef(DRI, this); |
| 4677 | } |
| 4678 | |
| 4679 | MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { |
| 4680 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier" ); |
| 4681 | return getStruct<MachO::section>(O: *this, P: Sections[DRI.d.a]); |
| 4682 | } |
| 4683 | |
| 4684 | MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
| 4685 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier" ); |
| 4686 | return getStruct<MachO::section_64>(O: *this, P: Sections[DRI.d.a]); |
| 4687 | } |
| 4688 | |
| 4689 | MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, |
| 4690 | unsigned Index) const { |
| 4691 | const char *Sec = getSectionPtr(O: *this, L, Sec: Index); |
| 4692 | return getStruct<MachO::section>(O: *this, P: Sec); |
| 4693 | } |
| 4694 | |
| 4695 | MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 4696 | unsigned Index) const { |
| 4697 | const char *Sec = getSectionPtr(O: *this, L, Sec: Index); |
| 4698 | return getStruct<MachO::section_64>(O: *this, P: Sec); |
| 4699 | } |
| 4700 | |
| 4701 | MachO::nlist |
| 4702 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
| 4703 | const char *P = reinterpret_cast<const char *>(DRI.p); |
| 4704 | return getStruct<MachO::nlist>(O: *this, P); |
| 4705 | } |
| 4706 | |
| 4707 | MachO::nlist_64 |
| 4708 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
| 4709 | const char *P = reinterpret_cast<const char *>(DRI.p); |
| 4710 | return getStruct<MachO::nlist_64>(O: *this, P); |
| 4711 | } |
| 4712 | |
| 4713 | MachO::linkedit_data_command |
| 4714 | MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { |
| 4715 | return getStruct<MachO::linkedit_data_command>(O: *this, P: L.Ptr); |
| 4716 | } |
| 4717 | |
| 4718 | MachO::segment_command |
| 4719 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
| 4720 | return getStruct<MachO::segment_command>(O: *this, P: L.Ptr); |
| 4721 | } |
| 4722 | |
| 4723 | MachO::segment_command_64 |
| 4724 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
| 4725 | return getStruct<MachO::segment_command_64>(O: *this, P: L.Ptr); |
| 4726 | } |
| 4727 | |
| 4728 | MachO::linker_option_command |
| 4729 | MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const { |
| 4730 | return getStruct<MachO::linker_option_command>(O: *this, P: L.Ptr); |
| 4731 | } |
| 4732 | |
| 4733 | MachO::version_min_command |
| 4734 | MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { |
| 4735 | return getStruct<MachO::version_min_command>(O: *this, P: L.Ptr); |
| 4736 | } |
| 4737 | |
| 4738 | MachO::note_command |
| 4739 | MachOObjectFile::getNoteLoadCommand(const LoadCommandInfo &L) const { |
| 4740 | return getStruct<MachO::note_command>(O: *this, P: L.Ptr); |
| 4741 | } |
| 4742 | |
| 4743 | MachO::build_version_command |
| 4744 | MachOObjectFile::getBuildVersionLoadCommand(const LoadCommandInfo &L) const { |
| 4745 | return getStruct<MachO::build_version_command>(O: *this, P: L.Ptr); |
| 4746 | } |
| 4747 | |
| 4748 | MachO::build_tool_version |
| 4749 | MachOObjectFile::getBuildToolVersion(unsigned index) const { |
| 4750 | return getStruct<MachO::build_tool_version>(O: *this, P: BuildTools[index]); |
| 4751 | } |
| 4752 | |
| 4753 | MachO::dylib_command |
| 4754 | MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const { |
| 4755 | return getStruct<MachO::dylib_command>(O: *this, P: L.Ptr); |
| 4756 | } |
| 4757 | |
| 4758 | MachO::dyld_info_command |
| 4759 | MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const { |
| 4760 | return getStruct<MachO::dyld_info_command>(O: *this, P: L.Ptr); |
| 4761 | } |
| 4762 | |
| 4763 | MachO::dylinker_command |
| 4764 | MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const { |
| 4765 | return getStruct<MachO::dylinker_command>(O: *this, P: L.Ptr); |
| 4766 | } |
| 4767 | |
| 4768 | MachO::uuid_command |
| 4769 | MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const { |
| 4770 | return getStruct<MachO::uuid_command>(O: *this, P: L.Ptr); |
| 4771 | } |
| 4772 | |
| 4773 | MachO::rpath_command |
| 4774 | MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const { |
| 4775 | return getStruct<MachO::rpath_command>(O: *this, P: L.Ptr); |
| 4776 | } |
| 4777 | |
| 4778 | MachO::source_version_command |
| 4779 | MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const { |
| 4780 | return getStruct<MachO::source_version_command>(O: *this, P: L.Ptr); |
| 4781 | } |
| 4782 | |
| 4783 | MachO::entry_point_command |
| 4784 | MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const { |
| 4785 | return getStruct<MachO::entry_point_command>(O: *this, P: L.Ptr); |
| 4786 | } |
| 4787 | |
| 4788 | MachO::encryption_info_command |
| 4789 | MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const { |
| 4790 | return getStruct<MachO::encryption_info_command>(O: *this, P: L.Ptr); |
| 4791 | } |
| 4792 | |
| 4793 | MachO::encryption_info_command_64 |
| 4794 | MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const { |
| 4795 | return getStruct<MachO::encryption_info_command_64>(O: *this, P: L.Ptr); |
| 4796 | } |
| 4797 | |
| 4798 | MachO::sub_framework_command |
| 4799 | MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const { |
| 4800 | return getStruct<MachO::sub_framework_command>(O: *this, P: L.Ptr); |
| 4801 | } |
| 4802 | |
| 4803 | MachO::sub_umbrella_command |
| 4804 | MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const { |
| 4805 | return getStruct<MachO::sub_umbrella_command>(O: *this, P: L.Ptr); |
| 4806 | } |
| 4807 | |
| 4808 | MachO::sub_library_command |
| 4809 | MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const { |
| 4810 | return getStruct<MachO::sub_library_command>(O: *this, P: L.Ptr); |
| 4811 | } |
| 4812 | |
| 4813 | MachO::sub_client_command |
| 4814 | MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const { |
| 4815 | return getStruct<MachO::sub_client_command>(O: *this, P: L.Ptr); |
| 4816 | } |
| 4817 | |
| 4818 | MachO::routines_command |
| 4819 | MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const { |
| 4820 | return getStruct<MachO::routines_command>(O: *this, P: L.Ptr); |
| 4821 | } |
| 4822 | |
| 4823 | MachO::routines_command_64 |
| 4824 | MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const { |
| 4825 | return getStruct<MachO::routines_command_64>(O: *this, P: L.Ptr); |
| 4826 | } |
| 4827 | |
| 4828 | MachO::thread_command |
| 4829 | MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const { |
| 4830 | return getStruct<MachO::thread_command>(O: *this, P: L.Ptr); |
| 4831 | } |
| 4832 | |
| 4833 | MachO::fileset_entry_command |
| 4834 | MachOObjectFile::getFilesetEntryLoadCommand(const LoadCommandInfo &L) const { |
| 4835 | return getStruct<MachO::fileset_entry_command>(O: *this, P: L.Ptr); |
| 4836 | } |
| 4837 | |
| 4838 | MachO::any_relocation_info |
| 4839 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
| 4840 | uint32_t Offset; |
| 4841 | if (getHeader().filetype == MachO::MH_OBJECT) { |
| 4842 | DataRefImpl Sec; |
| 4843 | Sec.d.a = Rel.d.a; |
| 4844 | if (is64Bit()) { |
| 4845 | MachO::section_64 Sect = getSection64(DRI: Sec); |
| 4846 | Offset = Sect.reloff; |
| 4847 | } else { |
| 4848 | MachO::section Sect = getSection(DRI: Sec); |
| 4849 | Offset = Sect.reloff; |
| 4850 | } |
| 4851 | } else { |
| 4852 | MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand(); |
| 4853 | if (Rel.d.a == 0) |
| 4854 | Offset = DysymtabLoadCmd.extreloff; // Offset to the external relocations |
| 4855 | else |
| 4856 | Offset = DysymtabLoadCmd.locreloff; // Offset to the local relocations |
| 4857 | } |
| 4858 | |
| 4859 | auto P = reinterpret_cast<const MachO::any_relocation_info *>( |
| 4860 | getPtr(O: *this, Offset)) + Rel.d.b; |
| 4861 | return getStruct<MachO::any_relocation_info>( |
| 4862 | O: *this, P: reinterpret_cast<const char *>(P)); |
| 4863 | } |
| 4864 | |
| 4865 | MachO::data_in_code_entry |
| 4866 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 4867 | const char *P = reinterpret_cast<const char *>(Rel.p); |
| 4868 | return getStruct<MachO::data_in_code_entry>(O: *this, P); |
| 4869 | } |
| 4870 | |
| 4871 | const MachO::mach_header &MachOObjectFile::() const { |
| 4872 | return Header; |
| 4873 | } |
| 4874 | |
| 4875 | const MachO::mach_header_64 &MachOObjectFile::() const { |
| 4876 | assert(is64Bit()); |
| 4877 | return Header64; |
| 4878 | } |
| 4879 | |
| 4880 | uint32_t MachOObjectFile::getIndirectSymbolTableEntry( |
| 4881 | const MachO::dysymtab_command &DLC, |
| 4882 | unsigned Index) const { |
| 4883 | uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); |
| 4884 | return getStruct<uint32_t>(O: *this, P: getPtr(O: *this, Offset)); |
| 4885 | } |
| 4886 | |
| 4887 | MachO::data_in_code_entry |
| 4888 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 4889 | unsigned Index) const { |
| 4890 | uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); |
| 4891 | return getStruct<MachO::data_in_code_entry>(O: *this, P: getPtr(O: *this, Offset)); |
| 4892 | } |
| 4893 | |
| 4894 | MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { |
| 4895 | if (SymtabLoadCmd) |
| 4896 | return getStruct<MachO::symtab_command>(O: *this, P: SymtabLoadCmd); |
| 4897 | |
| 4898 | // If there is no SymtabLoadCmd return a load command with zero'ed fields. |
| 4899 | MachO::symtab_command Cmd; |
| 4900 | Cmd.cmd = MachO::LC_SYMTAB; |
| 4901 | Cmd.cmdsize = sizeof(MachO::symtab_command); |
| 4902 | Cmd.symoff = 0; |
| 4903 | Cmd.nsyms = 0; |
| 4904 | Cmd.stroff = 0; |
| 4905 | Cmd.strsize = 0; |
| 4906 | return Cmd; |
| 4907 | } |
| 4908 | |
| 4909 | MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { |
| 4910 | if (DysymtabLoadCmd) |
| 4911 | return getStruct<MachO::dysymtab_command>(O: *this, P: DysymtabLoadCmd); |
| 4912 | |
| 4913 | // If there is no DysymtabLoadCmd return a load command with zero'ed fields. |
| 4914 | MachO::dysymtab_command Cmd; |
| 4915 | Cmd.cmd = MachO::LC_DYSYMTAB; |
| 4916 | Cmd.cmdsize = sizeof(MachO::dysymtab_command); |
| 4917 | Cmd.ilocalsym = 0; |
| 4918 | Cmd.nlocalsym = 0; |
| 4919 | Cmd.iextdefsym = 0; |
| 4920 | Cmd.nextdefsym = 0; |
| 4921 | Cmd.iundefsym = 0; |
| 4922 | Cmd.nundefsym = 0; |
| 4923 | Cmd.tocoff = 0; |
| 4924 | Cmd.ntoc = 0; |
| 4925 | Cmd.modtaboff = 0; |
| 4926 | Cmd.nmodtab = 0; |
| 4927 | Cmd.extrefsymoff = 0; |
| 4928 | Cmd.nextrefsyms = 0; |
| 4929 | Cmd.indirectsymoff = 0; |
| 4930 | Cmd.nindirectsyms = 0; |
| 4931 | Cmd.extreloff = 0; |
| 4932 | Cmd.nextrel = 0; |
| 4933 | Cmd.locreloff = 0; |
| 4934 | Cmd.nlocrel = 0; |
| 4935 | return Cmd; |
| 4936 | } |
| 4937 | |
| 4938 | MachO::linkedit_data_command |
| 4939 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 4940 | if (DataInCodeLoadCmd) |
| 4941 | return getStruct<MachO::linkedit_data_command>(O: *this, P: DataInCodeLoadCmd); |
| 4942 | |
| 4943 | // If there is no DataInCodeLoadCmd return a load command with zero'ed fields. |
| 4944 | MachO::linkedit_data_command Cmd; |
| 4945 | Cmd.cmd = MachO::LC_DATA_IN_CODE; |
| 4946 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 4947 | Cmd.dataoff = 0; |
| 4948 | Cmd.datasize = 0; |
| 4949 | return Cmd; |
| 4950 | } |
| 4951 | |
| 4952 | MachO::linkedit_data_command |
| 4953 | MachOObjectFile::getLinkOptHintsLoadCommand() const { |
| 4954 | if (LinkOptHintsLoadCmd) |
| 4955 | return getStruct<MachO::linkedit_data_command>(O: *this, P: LinkOptHintsLoadCmd); |
| 4956 | |
| 4957 | // If there is no LinkOptHintsLoadCmd return a load command with zero'ed |
| 4958 | // fields. |
| 4959 | MachO::linkedit_data_command Cmd; |
| 4960 | Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT; |
| 4961 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 4962 | Cmd.dataoff = 0; |
| 4963 | Cmd.datasize = 0; |
| 4964 | return Cmd; |
| 4965 | } |
| 4966 | |
| 4967 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const { |
| 4968 | if (!DyldInfoLoadCmd) |
| 4969 | return {}; |
| 4970 | |
| 4971 | auto DyldInfoOrErr = |
| 4972 | getStructOrErr<MachO::dyld_info_command>(O: *this, P: DyldInfoLoadCmd); |
| 4973 | if (!DyldInfoOrErr) |
| 4974 | return {}; |
| 4975 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 4976 | const uint8_t *Ptr = |
| 4977 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldInfo.rebase_off)); |
| 4978 | return ArrayRef(Ptr, DyldInfo.rebase_size); |
| 4979 | } |
| 4980 | |
| 4981 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const { |
| 4982 | if (!DyldInfoLoadCmd) |
| 4983 | return {}; |
| 4984 | |
| 4985 | auto DyldInfoOrErr = |
| 4986 | getStructOrErr<MachO::dyld_info_command>(O: *this, P: DyldInfoLoadCmd); |
| 4987 | if (!DyldInfoOrErr) |
| 4988 | return {}; |
| 4989 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 4990 | const uint8_t *Ptr = |
| 4991 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldInfo.bind_off)); |
| 4992 | return ArrayRef(Ptr, DyldInfo.bind_size); |
| 4993 | } |
| 4994 | |
| 4995 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const { |
| 4996 | if (!DyldInfoLoadCmd) |
| 4997 | return {}; |
| 4998 | |
| 4999 | auto DyldInfoOrErr = |
| 5000 | getStructOrErr<MachO::dyld_info_command>(O: *this, P: DyldInfoLoadCmd); |
| 5001 | if (!DyldInfoOrErr) |
| 5002 | return {}; |
| 5003 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 5004 | const uint8_t *Ptr = |
| 5005 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldInfo.weak_bind_off)); |
| 5006 | return ArrayRef(Ptr, DyldInfo.weak_bind_size); |
| 5007 | } |
| 5008 | |
| 5009 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const { |
| 5010 | if (!DyldInfoLoadCmd) |
| 5011 | return {}; |
| 5012 | |
| 5013 | auto DyldInfoOrErr = |
| 5014 | getStructOrErr<MachO::dyld_info_command>(O: *this, P: DyldInfoLoadCmd); |
| 5015 | if (!DyldInfoOrErr) |
| 5016 | return {}; |
| 5017 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 5018 | const uint8_t *Ptr = |
| 5019 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldInfo.lazy_bind_off)); |
| 5020 | return ArrayRef(Ptr, DyldInfo.lazy_bind_size); |
| 5021 | } |
| 5022 | |
| 5023 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const { |
| 5024 | if (!DyldInfoLoadCmd) |
| 5025 | return {}; |
| 5026 | |
| 5027 | auto DyldInfoOrErr = |
| 5028 | getStructOrErr<MachO::dyld_info_command>(O: *this, P: DyldInfoLoadCmd); |
| 5029 | if (!DyldInfoOrErr) |
| 5030 | return {}; |
| 5031 | MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get(); |
| 5032 | const uint8_t *Ptr = |
| 5033 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldInfo.export_off)); |
| 5034 | return ArrayRef(Ptr, DyldInfo.export_size); |
| 5035 | } |
| 5036 | |
| 5037 | Expected<std::optional<MachO::linkedit_data_command>> |
| 5038 | MachOObjectFile::getChainedFixupsLoadCommand() const { |
| 5039 | // Load the dyld chained fixups load command. |
| 5040 | if (!DyldChainedFixupsLoadCmd) |
| 5041 | return std::nullopt; |
| 5042 | auto DyldChainedFixupsOrErr = getStructOrErr<MachO::linkedit_data_command>( |
| 5043 | O: *this, P: DyldChainedFixupsLoadCmd); |
| 5044 | if (!DyldChainedFixupsOrErr) |
| 5045 | return DyldChainedFixupsOrErr.takeError(); |
| 5046 | const MachO::linkedit_data_command &DyldChainedFixups = |
| 5047 | *DyldChainedFixupsOrErr; |
| 5048 | |
| 5049 | // If the load command is present but the data offset has been zeroed out, |
| 5050 | // as is the case for dylib stubs, return std::nullopt (no error). |
| 5051 | if (!DyldChainedFixups.dataoff) |
| 5052 | return std::nullopt; |
| 5053 | return DyldChainedFixups; |
| 5054 | } |
| 5055 | |
| 5056 | Expected<std::optional<MachO::dyld_chained_fixups_header>> |
| 5057 | MachOObjectFile::() const { |
| 5058 | auto CFOrErr = getChainedFixupsLoadCommand(); |
| 5059 | if (!CFOrErr) |
| 5060 | return CFOrErr.takeError(); |
| 5061 | if (!CFOrErr->has_value()) |
| 5062 | return std::nullopt; |
| 5063 | |
| 5064 | const MachO::linkedit_data_command &DyldChainedFixups = **CFOrErr; |
| 5065 | |
| 5066 | uint64_t = DyldChainedFixups.dataoff; |
| 5067 | uint64_t CFSize = DyldChainedFixups.datasize; |
| 5068 | |
| 5069 | // Load the dyld chained fixups header. |
| 5070 | const char * = getPtr(O: *this, Offset: CFHeaderOffset); |
| 5071 | auto = |
| 5072 | getStructOrErr<MachO::dyld_chained_fixups_header>(O: *this, P: CFHeaderPtr); |
| 5073 | if (!CFHeaderOrErr) |
| 5074 | return CFHeaderOrErr.takeError(); |
| 5075 | MachO::dyld_chained_fixups_header = CFHeaderOrErr.get(); |
| 5076 | |
| 5077 | // Reject unknown chained fixup formats. |
| 5078 | if (CFHeader.fixups_version != 0) |
| 5079 | return malformedError(Msg: Twine("bad chained fixups: unknown version: " ) + |
| 5080 | Twine(CFHeader.fixups_version)); |
| 5081 | if (CFHeader.imports_format < 1 || CFHeader.imports_format > 3) |
| 5082 | return malformedError( |
| 5083 | Msg: Twine("bad chained fixups: unknown imports format: " ) + |
| 5084 | Twine(CFHeader.imports_format)); |
| 5085 | |
| 5086 | // Validate the image format. |
| 5087 | // |
| 5088 | // Load the image starts. |
| 5089 | uint64_t CFImageStartsOffset = (CFHeaderOffset + CFHeader.starts_offset); |
| 5090 | if (CFHeader.starts_offset < sizeof(MachO::dyld_chained_fixups_header)) { |
| 5091 | return malformedError(Msg: Twine("bad chained fixups: image starts offset " ) + |
| 5092 | Twine(CFHeader.starts_offset) + |
| 5093 | " overlaps with chained fixups header" ); |
| 5094 | } |
| 5095 | uint32_t EndOffset = CFHeaderOffset + CFSize; |
| 5096 | if (CFImageStartsOffset + sizeof(MachO::dyld_chained_starts_in_image) > |
| 5097 | EndOffset) { |
| 5098 | return malformedError(Msg: Twine("bad chained fixups: image starts end " ) + |
| 5099 | Twine(CFImageStartsOffset + |
| 5100 | sizeof(MachO::dyld_chained_starts_in_image)) + |
| 5101 | " extends past end " + Twine(EndOffset)); |
| 5102 | } |
| 5103 | |
| 5104 | return CFHeader; |
| 5105 | } |
| 5106 | |
| 5107 | Expected<std::pair<size_t, std::vector<ChainedFixupsSegment>>> |
| 5108 | MachOObjectFile::getChainedFixupsSegments() const { |
| 5109 | auto CFOrErr = getChainedFixupsLoadCommand(); |
| 5110 | if (!CFOrErr) |
| 5111 | return CFOrErr.takeError(); |
| 5112 | |
| 5113 | std::vector<ChainedFixupsSegment> Segments; |
| 5114 | if (!CFOrErr->has_value()) |
| 5115 | return std::make_pair(x: 0, y&: Segments); |
| 5116 | |
| 5117 | const MachO::linkedit_data_command &DyldChainedFixups = **CFOrErr; |
| 5118 | |
| 5119 | auto = getChainedFixupsHeader(); |
| 5120 | if (!HeaderOrErr) |
| 5121 | return HeaderOrErr.takeError(); |
| 5122 | if (!HeaderOrErr->has_value()) |
| 5123 | return std::make_pair(x: 0, y&: Segments); |
| 5124 | const MachO::dyld_chained_fixups_header & = **HeaderOrErr; |
| 5125 | |
| 5126 | const char *Contents = getPtr(O: *this, Offset: DyldChainedFixups.dataoff); |
| 5127 | |
| 5128 | auto ImageStartsOrErr = getStructOrErr<MachO::dyld_chained_starts_in_image>( |
| 5129 | O: *this, P: Contents + Header.starts_offset); |
| 5130 | if (!ImageStartsOrErr) |
| 5131 | return ImageStartsOrErr.takeError(); |
| 5132 | const MachO::dyld_chained_starts_in_image &ImageStarts = *ImageStartsOrErr; |
| 5133 | |
| 5134 | const char *SegOffsPtr = |
| 5135 | Contents + Header.starts_offset + |
| 5136 | offsetof(MachO::dyld_chained_starts_in_image, seg_info_offset); |
| 5137 | const char *SegOffsEnd = |
| 5138 | SegOffsPtr + ImageStarts.seg_count * sizeof(uint32_t); |
| 5139 | if (SegOffsEnd > Contents + DyldChainedFixups.datasize) |
| 5140 | return malformedError( |
| 5141 | Msg: "bad chained fixups: seg_info_offset extends past end" ); |
| 5142 | |
| 5143 | const char *LastSegEnd = nullptr; |
| 5144 | for (size_t I = 0, N = ImageStarts.seg_count; I < N; ++I) { |
| 5145 | auto OffOrErr = |
| 5146 | getStructOrErr<uint32_t>(O: *this, P: SegOffsPtr + I * sizeof(uint32_t)); |
| 5147 | if (!OffOrErr) |
| 5148 | return OffOrErr.takeError(); |
| 5149 | // seg_info_offset == 0 means there is no associated starts_in_segment |
| 5150 | // entry. |
| 5151 | if (!*OffOrErr) |
| 5152 | continue; |
| 5153 | |
| 5154 | auto Fail = [&](Twine Message) { |
| 5155 | return malformedError(Msg: "bad chained fixups: segment info" + Twine(I) + |
| 5156 | " at offset " + Twine(*OffOrErr) + Message); |
| 5157 | }; |
| 5158 | |
| 5159 | const char *SegPtr = Contents + Header.starts_offset + *OffOrErr; |
| 5160 | if (LastSegEnd && SegPtr < LastSegEnd) |
| 5161 | return Fail(" overlaps with previous segment info" ); |
| 5162 | |
| 5163 | auto SegOrErr = |
| 5164 | getStructOrErr<MachO::dyld_chained_starts_in_segment>(O: *this, P: SegPtr); |
| 5165 | if (!SegOrErr) |
| 5166 | return SegOrErr.takeError(); |
| 5167 | const MachO::dyld_chained_starts_in_segment &Seg = *SegOrErr; |
| 5168 | |
| 5169 | LastSegEnd = SegPtr + Seg.size; |
| 5170 | if (Seg.pointer_format < 1 || Seg.pointer_format > 12) |
| 5171 | return Fail(" has unknown pointer format: " + Twine(Seg.pointer_format)); |
| 5172 | |
| 5173 | const char *PageStart = |
| 5174 | SegPtr + offsetof(MachO::dyld_chained_starts_in_segment, page_start); |
| 5175 | const char *PageEnd = PageStart + Seg.page_count * sizeof(uint16_t); |
| 5176 | if (PageEnd > SegPtr + Seg.size) |
| 5177 | return Fail(" : page_starts extend past seg_info size" ); |
| 5178 | |
| 5179 | // FIXME: This does not account for multiple offsets on a single page |
| 5180 | // (DYLD_CHAINED_PTR_START_MULTI; 32-bit only). |
| 5181 | std::vector<uint16_t> PageStarts; |
| 5182 | for (size_t PageIdx = 0; PageIdx < Seg.page_count; ++PageIdx) { |
| 5183 | uint16_t Start; |
| 5184 | memcpy(dest: &Start, src: PageStart + PageIdx * sizeof(uint16_t), n: sizeof(uint16_t)); |
| 5185 | if (isLittleEndian() != sys::IsLittleEndianHost) |
| 5186 | sys::swapByteOrder(Value&: Start); |
| 5187 | PageStarts.push_back(x: Start); |
| 5188 | } |
| 5189 | |
| 5190 | Segments.emplace_back(args&: I, args&: *OffOrErr, args: Seg, args: std::move(PageStarts)); |
| 5191 | } |
| 5192 | |
| 5193 | return std::make_pair(x: ImageStarts.seg_count, y&: Segments); |
| 5194 | } |
| 5195 | |
| 5196 | // The special library ordinals have a negative value, but they are encoded in |
| 5197 | // an unsigned bitfield, so we need to sign extend the value. |
| 5198 | template <typename T> static int getEncodedOrdinal(T Value) { |
| 5199 | if (Value == static_cast<T>(MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) || |
| 5200 | Value == static_cast<T>(MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP) || |
| 5201 | Value == static_cast<T>(MachO::BIND_SPECIAL_DYLIB_WEAK_LOOKUP)) |
| 5202 | return SignExtend32<sizeof(T) * CHAR_BIT>(Value); |
| 5203 | return Value; |
| 5204 | } |
| 5205 | |
| 5206 | template <typename T, unsigned N> |
| 5207 | static std::array<T, N> getArray(const MachOObjectFile &O, const void *Ptr) { |
| 5208 | std::array<T, N> RawValue; |
| 5209 | memcpy(RawValue.data(), Ptr, N * sizeof(T)); |
| 5210 | if (O.isLittleEndian() != sys::IsLittleEndianHost) |
| 5211 | for (auto &Element : RawValue) |
| 5212 | sys::swapByteOrder(Element); |
| 5213 | return RawValue; |
| 5214 | } |
| 5215 | |
| 5216 | Expected<std::vector<ChainedFixupTarget>> |
| 5217 | MachOObjectFile::getDyldChainedFixupTargets() const { |
| 5218 | auto CFOrErr = getChainedFixupsLoadCommand(); |
| 5219 | if (!CFOrErr) |
| 5220 | return CFOrErr.takeError(); |
| 5221 | |
| 5222 | std::vector<ChainedFixupTarget> Targets; |
| 5223 | if (!CFOrErr->has_value()) |
| 5224 | return Targets; |
| 5225 | |
| 5226 | const MachO::linkedit_data_command &DyldChainedFixups = **CFOrErr; |
| 5227 | |
| 5228 | auto = getChainedFixupsHeader(); |
| 5229 | if (!CFHeaderOrErr) |
| 5230 | return CFHeaderOrErr.takeError(); |
| 5231 | if (!(*CFHeaderOrErr)) |
| 5232 | return Targets; |
| 5233 | const MachO::dyld_chained_fixups_header & = **CFHeaderOrErr; |
| 5234 | |
| 5235 | size_t ImportSize = 0; |
| 5236 | if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT) |
| 5237 | ImportSize = sizeof(MachO::dyld_chained_import); |
| 5238 | else if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT_ADDEND) |
| 5239 | ImportSize = sizeof(MachO::dyld_chained_import_addend); |
| 5240 | else if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT_ADDEND64) |
| 5241 | ImportSize = sizeof(MachO::dyld_chained_import_addend64); |
| 5242 | else |
| 5243 | return malformedError(Msg: "bad chained fixups: unknown imports format: " + |
| 5244 | Twine(Header.imports_format)); |
| 5245 | |
| 5246 | const char *Contents = getPtr(O: *this, Offset: DyldChainedFixups.dataoff); |
| 5247 | const char *Imports = Contents + Header.imports_offset; |
| 5248 | size_t ImportsEndOffset = |
| 5249 | Header.imports_offset + ImportSize * Header.imports_count; |
| 5250 | const char *ImportsEnd = Contents + ImportsEndOffset; |
| 5251 | const char *Symbols = Contents + Header.symbols_offset; |
| 5252 | const char *SymbolsEnd = Contents + DyldChainedFixups.datasize; |
| 5253 | |
| 5254 | if (ImportsEnd > Symbols) |
| 5255 | return malformedError(Msg: "bad chained fixups: imports end " + |
| 5256 | Twine(ImportsEndOffset) + " overlaps with symbols" ); |
| 5257 | |
| 5258 | // We use bit manipulation to extract data from the bitfields. This is correct |
| 5259 | // for both LE and BE hosts, but we assume that the object is little-endian. |
| 5260 | if (!isLittleEndian()) |
| 5261 | return createError(Err: "parsing big-endian chained fixups is not implemented" ); |
| 5262 | for (const char *ImportPtr = Imports; ImportPtr < ImportsEnd; |
| 5263 | ImportPtr += ImportSize) { |
| 5264 | int LibOrdinal; |
| 5265 | bool WeakImport; |
| 5266 | uint32_t NameOffset; |
| 5267 | uint64_t Addend; |
| 5268 | if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT) { |
| 5269 | static_assert(sizeof(uint32_t) == sizeof(MachO::dyld_chained_import)); |
| 5270 | auto RawValue = getArray<uint32_t, 1>(O: *this, Ptr: ImportPtr); |
| 5271 | |
| 5272 | LibOrdinal = getEncodedOrdinal<uint8_t>(Value: RawValue[0] & 0xFF); |
| 5273 | WeakImport = (RawValue[0] >> 8) & 1; |
| 5274 | NameOffset = RawValue[0] >> 9; |
| 5275 | Addend = 0; |
| 5276 | } else if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT_ADDEND) { |
| 5277 | static_assert(sizeof(uint64_t) == |
| 5278 | sizeof(MachO::dyld_chained_import_addend)); |
| 5279 | auto RawValue = getArray<uint32_t, 2>(O: *this, Ptr: ImportPtr); |
| 5280 | |
| 5281 | LibOrdinal = getEncodedOrdinal<uint8_t>(Value: RawValue[0] & 0xFF); |
| 5282 | WeakImport = (RawValue[0] >> 8) & 1; |
| 5283 | NameOffset = RawValue[0] >> 9; |
| 5284 | Addend = bit_cast<int32_t>(from: RawValue[1]); |
| 5285 | } else if (Header.imports_format == MachO::DYLD_CHAINED_IMPORT_ADDEND64) { |
| 5286 | static_assert(2 * sizeof(uint64_t) == |
| 5287 | sizeof(MachO::dyld_chained_import_addend64)); |
| 5288 | auto RawValue = getArray<uint64_t, 2>(O: *this, Ptr: ImportPtr); |
| 5289 | |
| 5290 | LibOrdinal = getEncodedOrdinal<uint16_t>(Value: RawValue[0] & 0xFFFF); |
| 5291 | NameOffset = (RawValue[0] >> 16) & 1; |
| 5292 | WeakImport = RawValue[0] >> 17; |
| 5293 | Addend = RawValue[1]; |
| 5294 | } else { |
| 5295 | llvm_unreachable("Import format should have been checked" ); |
| 5296 | } |
| 5297 | |
| 5298 | const char *Str = Symbols + NameOffset; |
| 5299 | if (Str >= SymbolsEnd) |
| 5300 | return malformedError(Msg: "bad chained fixups: symbol offset " + |
| 5301 | Twine(NameOffset) + " extends past end " + |
| 5302 | Twine(DyldChainedFixups.datasize)); |
| 5303 | Targets.emplace_back(args&: LibOrdinal, args&: NameOffset, args&: Str, args&: Addend, args&: WeakImport); |
| 5304 | } |
| 5305 | |
| 5306 | return std::move(Targets); |
| 5307 | } |
| 5308 | |
| 5309 | ArrayRef<uint8_t> MachOObjectFile::getDyldExportsTrie() const { |
| 5310 | if (!DyldExportsTrieLoadCmd) |
| 5311 | return {}; |
| 5312 | |
| 5313 | auto DyldExportsTrieOrError = getStructOrErr<MachO::linkedit_data_command>( |
| 5314 | O: *this, P: DyldExportsTrieLoadCmd); |
| 5315 | if (!DyldExportsTrieOrError) |
| 5316 | return {}; |
| 5317 | MachO::linkedit_data_command DyldExportsTrie = DyldExportsTrieOrError.get(); |
| 5318 | const uint8_t *Ptr = |
| 5319 | reinterpret_cast<const uint8_t *>(getPtr(O: *this, Offset: DyldExportsTrie.dataoff)); |
| 5320 | return ArrayRef(Ptr, DyldExportsTrie.datasize); |
| 5321 | } |
| 5322 | |
| 5323 | SmallVector<uint64_t> MachOObjectFile::getFunctionStarts() const { |
| 5324 | if (!FuncStartsLoadCmd) |
| 5325 | return {}; |
| 5326 | |
| 5327 | auto InfoOrErr = |
| 5328 | getStructOrErr<MachO::linkedit_data_command>(O: *this, P: FuncStartsLoadCmd); |
| 5329 | if (!InfoOrErr) |
| 5330 | return {}; |
| 5331 | |
| 5332 | MachO::linkedit_data_command Info = InfoOrErr.get(); |
| 5333 | SmallVector<uint64_t, 8> FunctionStarts; |
| 5334 | this->ReadULEB128s(Index: Info.dataoff, Out&: FunctionStarts); |
| 5335 | return std::move(FunctionStarts); |
| 5336 | } |
| 5337 | |
| 5338 | ArrayRef<uint8_t> MachOObjectFile::getUuid() const { |
| 5339 | if (!UuidLoadCmd) |
| 5340 | return {}; |
| 5341 | // Returning a pointer is fine as uuid doesn't need endian swapping. |
| 5342 | const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid); |
| 5343 | return ArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16); |
| 5344 | } |
| 5345 | |
| 5346 | StringRef MachOObjectFile::getStringTableData() const { |
| 5347 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 5348 | return getData().substr(Start: S.stroff, N: S.strsize); |
| 5349 | } |
| 5350 | |
| 5351 | bool MachOObjectFile::is64Bit() const { |
| 5352 | return getType() == getMachOType(isLE: false, is64Bits: true) || |
| 5353 | getType() == getMachOType(isLE: true, is64Bits: true); |
| 5354 | } |
| 5355 | |
| 5356 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 5357 | SmallVectorImpl<uint64_t> &Out) const { |
| 5358 | DataExtractor (ObjectFile::getData(), true, 0); |
| 5359 | |
| 5360 | uint64_t offset = Index; |
| 5361 | uint64_t data = 0; |
| 5362 | while (uint64_t delta = extractor.getULEB128(offset_ptr: &offset)) { |
| 5363 | data += delta; |
| 5364 | Out.push_back(Elt: data); |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | bool MachOObjectFile::isRelocatableObject() const { |
| 5369 | return getHeader().filetype == MachO::MH_OBJECT; |
| 5370 | } |
| 5371 | |
| 5372 | /// Create a MachOObjectFile instance from a given buffer. |
| 5373 | /// |
| 5374 | /// \param Buffer Memory buffer containing the MachO binary data. |
| 5375 | /// \param UniversalCputype CPU type when the MachO part of a universal binary. |
| 5376 | /// \param UniversalIndex Index of the MachO within a universal binary. |
| 5377 | /// \param MachOFilesetEntryOffset Offset of the MachO entry in a fileset MachO. |
| 5378 | /// \returns A std::unique_ptr to a MachOObjectFile instance on success. |
| 5379 | Expected<std::unique_ptr<MachOObjectFile>> ObjectFile::createMachOObjectFile( |
| 5380 | MemoryBufferRef Buffer, uint32_t UniversalCputype, uint32_t UniversalIndex, |
| 5381 | size_t MachOFilesetEntryOffset) { |
| 5382 | StringRef Magic = Buffer.getBuffer().slice(Start: 0, End: 4); |
| 5383 | if (Magic == "\xFE\xED\xFA\xCE" ) |
| 5384 | return MachOObjectFile::create(Object: Buffer, IsLittleEndian: false, Is64Bits: false, UniversalCputype, |
| 5385 | UniversalIndex, MachOFilesetEntryOffset); |
| 5386 | if (Magic == "\xCE\xFA\xED\xFE" ) |
| 5387 | return MachOObjectFile::create(Object: Buffer, IsLittleEndian: true, Is64Bits: false, UniversalCputype, |
| 5388 | UniversalIndex, MachOFilesetEntryOffset); |
| 5389 | if (Magic == "\xFE\xED\xFA\xCF" ) |
| 5390 | return MachOObjectFile::create(Object: Buffer, IsLittleEndian: false, Is64Bits: true, UniversalCputype, |
| 5391 | UniversalIndex, MachOFilesetEntryOffset); |
| 5392 | if (Magic == "\xCF\xFA\xED\xFE" ) |
| 5393 | return MachOObjectFile::create(Object: Buffer, IsLittleEndian: true, Is64Bits: true, UniversalCputype, |
| 5394 | UniversalIndex, MachOFilesetEntryOffset); |
| 5395 | return make_error<GenericBinaryError>(Args: "Unrecognized MachO magic number" , |
| 5396 | Args: object_error::invalid_file_type); |
| 5397 | } |
| 5398 | |
| 5399 | StringRef MachOObjectFile::mapDebugSectionName(StringRef Name) const { |
| 5400 | return StringSwitch<StringRef>(Name) |
| 5401 | .Case(S: "debug_str_offs" , Value: "debug_str_offsets" ) |
| 5402 | .Default(Value: Name); |
| 5403 | } |
| 5404 | |
| 5405 | Expected<std::vector<std::string>> |
| 5406 | MachOObjectFile::findDsymObjectMembers(StringRef Path) { |
| 5407 | SmallString<256> BundlePath(Path); |
| 5408 | // Normalize input path. This is necessary to accept `bundle.dSYM/`. |
| 5409 | sys::path::remove_dots(path&: BundlePath); |
| 5410 | if (!sys::fs::is_directory(Path: BundlePath) || |
| 5411 | sys::path::extension(path: BundlePath) != ".dSYM" ) |
| 5412 | return std::vector<std::string>(); |
| 5413 | sys::path::append(path&: BundlePath, a: "Contents" , b: "Resources" , c: "DWARF" ); |
| 5414 | bool IsDir; |
| 5415 | auto EC = sys::fs::is_directory(path: BundlePath, result&: IsDir); |
| 5416 | if (EC == errc::no_such_file_or_directory || (!EC && !IsDir)) |
| 5417 | return createStringError( |
| 5418 | EC, Fmt: "%s: expected directory 'Contents/Resources/DWARF' in dSYM bundle" , |
| 5419 | Vals: Path.str().c_str()); |
| 5420 | if (EC) |
| 5421 | return createFileError(F: BundlePath, E: errorCodeToError(EC)); |
| 5422 | |
| 5423 | std::vector<std::string> ObjectPaths; |
| 5424 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 5425 | Dir != DirEnd && !EC; Dir.increment(ec&: EC)) { |
| 5426 | StringRef ObjectPath = Dir->path(); |
| 5427 | sys::fs::file_status Status; |
| 5428 | if (auto EC = sys::fs::status(path: ObjectPath, result&: Status)) |
| 5429 | return createFileError(F: ObjectPath, E: errorCodeToError(EC)); |
| 5430 | switch (Status.type()) { |
| 5431 | case sys::fs::file_type::regular_file: |
| 5432 | case sys::fs::file_type::symlink_file: |
| 5433 | case sys::fs::file_type::type_unknown: |
| 5434 | ObjectPaths.push_back(x: ObjectPath.str()); |
| 5435 | break; |
| 5436 | default: /*ignore*/; |
| 5437 | } |
| 5438 | } |
| 5439 | if (EC) |
| 5440 | return createFileError(F: BundlePath, E: errorCodeToError(EC)); |
| 5441 | if (ObjectPaths.empty()) |
| 5442 | return createStringError(EC: std::error_code(), |
| 5443 | Fmt: "%s: no objects found in dSYM bundle" , |
| 5444 | Vals: Path.str().c_str()); |
| 5445 | return ObjectPaths; |
| 5446 | } |
| 5447 | |
| 5448 | llvm::binaryformat::Swift5ReflectionSectionKind |
| 5449 | MachOObjectFile::mapReflectionSectionNameToEnumValue( |
| 5450 | StringRef SectionName) const { |
| 5451 | #define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \ |
| 5452 | .Case(MACHO, llvm::binaryformat::Swift5ReflectionSectionKind::KIND) |
| 5453 | return StringSwitch<llvm::binaryformat::Swift5ReflectionSectionKind>( |
| 5454 | SectionName) |
| 5455 | #include "llvm/BinaryFormat/Swift.def" |
| 5456 | .Default(Value: llvm::binaryformat::Swift5ReflectionSectionKind::unknown); |
| 5457 | #undef HANDLE_SWIFT_SECTION |
| 5458 | } |
| 5459 | |
| 5460 | bool MachOObjectFile::isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) { |
| 5461 | switch (Arch) { |
| 5462 | case Triple::x86: |
| 5463 | return RelocType == MachO::GENERIC_RELOC_SECTDIFF || |
| 5464 | RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF; |
| 5465 | case Triple::x86_64: |
| 5466 | return RelocType == MachO::X86_64_RELOC_SUBTRACTOR; |
| 5467 | case Triple::arm: |
| 5468 | case Triple::thumb: |
| 5469 | return RelocType == MachO::ARM_RELOC_SECTDIFF || |
| 5470 | RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF || |
| 5471 | RelocType == MachO::ARM_RELOC_HALF || |
| 5472 | RelocType == MachO::ARM_RELOC_HALF_SECTDIFF; |
| 5473 | case Triple::aarch64: |
| 5474 | return RelocType == MachO::ARM64_RELOC_SUBTRACTOR; |
| 5475 | default: |
| 5476 | return false; |
| 5477 | } |
| 5478 | } |
| 5479 | |