Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1 | //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the MachOObjectFile class, which binds the MachOObject |
| 11 | // class to the generic ObjectFile wrapper. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 15 | #include "llvm/Object/MachO.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 17 | #include "llvm/Object/MachOFormat.h" |
Rafael Espindola | 8764c89 | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DataExtractor.h" |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Host.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 22 | #include <cctype> |
| 23 | #include <cstring> |
| 24 | #include <limits> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace object; |
| 28 | |
| 29 | namespace llvm { |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 30 | namespace object { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 32 | struct SymbolTableEntryBase { |
| 33 | uint32_t StringIndex; |
| 34 | uint8_t Type; |
| 35 | uint8_t SectionIndex; |
| 36 | uint16_t Flags; |
| 37 | }; |
| 38 | |
| 39 | struct SectionBase { |
| 40 | char Name[16]; |
| 41 | char SegmentName[16]; |
| 42 | }; |
| 43 | |
| 44 | template<typename T> |
| 45 | static void SwapValue(T &Value) { |
| 46 | Value = sys::SwapByteOrder(Value); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 49 | template<typename T> |
| 50 | static void SwapStruct(T &Value); |
| 51 | |
| 52 | template<> |
| 53 | void SwapStruct(macho::RelocationEntry &H) { |
| 54 | SwapValue(H.Word0); |
| 55 | SwapValue(H.Word1); |
Rafael Espindola | 3eff318 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 58 | template<> |
| 59 | void SwapStruct(macho::LoadCommand &L) { |
| 60 | SwapValue(L.Type); |
| 61 | SwapValue(L.Size); |
| 62 | } |
Rafael Espindola | 8764c89 | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 64 | template<> |
| 65 | void SwapStruct(SymbolTableEntryBase &S) { |
| 66 | SwapValue(S.StringIndex); |
| 67 | SwapValue(S.Flags); |
| 68 | } |
| 69 | |
| 70 | template<> |
| 71 | void SwapStruct(macho::Section &S) { |
| 72 | SwapValue(S.Address); |
| 73 | SwapValue(S.Size); |
| 74 | SwapValue(S.Offset); |
| 75 | SwapValue(S.Align); |
| 76 | SwapValue(S.RelocationTableOffset); |
| 77 | SwapValue(S.NumRelocationTableEntries); |
| 78 | SwapValue(S.Flags); |
| 79 | SwapValue(S.Reserved1); |
| 80 | SwapValue(S.Reserved2); |
| 81 | } |
| 82 | |
| 83 | template<> |
| 84 | void SwapStruct(macho::Section64 &S) { |
| 85 | SwapValue(S.Address); |
| 86 | SwapValue(S.Size); |
| 87 | SwapValue(S.Offset); |
| 88 | SwapValue(S.Align); |
| 89 | SwapValue(S.RelocationTableOffset); |
| 90 | SwapValue(S.NumRelocationTableEntries); |
| 91 | SwapValue(S.Flags); |
| 92 | SwapValue(S.Reserved1); |
| 93 | SwapValue(S.Reserved2); |
| 94 | SwapValue(S.Reserved3); |
| 95 | } |
| 96 | |
| 97 | template<> |
| 98 | void SwapStruct(macho::SymbolTableEntry &S) { |
| 99 | SwapValue(S.StringIndex); |
| 100 | SwapValue(S.Flags); |
| 101 | SwapValue(S.Value); |
| 102 | } |
| 103 | |
| 104 | template<> |
| 105 | void SwapStruct(macho::Symbol64TableEntry &S) { |
| 106 | SwapValue(S.StringIndex); |
| 107 | SwapValue(S.Flags); |
| 108 | SwapValue(S.Value); |
| 109 | } |
| 110 | |
| 111 | template<> |
| 112 | void SwapStruct(macho::Header &H) { |
| 113 | SwapValue(H.Magic); |
| 114 | SwapValue(H.CPUType); |
| 115 | SwapValue(H.CPUSubtype); |
| 116 | SwapValue(H.FileType); |
| 117 | SwapValue(H.NumLoadCommands); |
| 118 | SwapValue(H.SizeOfLoadCommands); |
| 119 | SwapValue(H.Flags); |
| 120 | } |
| 121 | |
| 122 | template<> |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 123 | void SwapStruct(macho::Header64Ext &E) { |
| 124 | SwapValue(E.Reserved); |
| 125 | } |
| 126 | |
| 127 | template<> |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 128 | void SwapStruct(macho::SymtabLoadCommand &C) { |
| 129 | SwapValue(C.Type); |
| 130 | SwapValue(C.Size); |
| 131 | SwapValue(C.SymbolTableOffset); |
| 132 | SwapValue(C.NumSymbolTableEntries); |
| 133 | SwapValue(C.StringTableOffset); |
| 134 | SwapValue(C.StringTableSize); |
| 135 | } |
| 136 | |
| 137 | template<> |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 138 | void SwapStruct(macho::DysymtabLoadCommand &C) { |
| 139 | SwapValue(C.Type); |
| 140 | SwapValue(C.Size); |
| 141 | SwapValue(C.LocalSymbolsIndex); |
| 142 | SwapValue(C.NumLocalSymbols); |
| 143 | SwapValue(C.ExternalSymbolsIndex); |
| 144 | SwapValue(C.NumExternalSymbols); |
| 145 | SwapValue(C.UndefinedSymbolsIndex); |
| 146 | SwapValue(C.NumUndefinedSymbols); |
| 147 | SwapValue(C.TOCOffset); |
| 148 | SwapValue(C.NumTOCEntries); |
| 149 | SwapValue(C.ModuleTableOffset); |
| 150 | SwapValue(C.NumModuleTableEntries); |
| 151 | SwapValue(C.ReferenceSymbolTableOffset); |
| 152 | SwapValue(C.NumReferencedSymbolTableEntries); |
| 153 | SwapValue(C.IndirectSymbolTableOffset); |
| 154 | SwapValue(C.NumIndirectSymbolTableEntries); |
| 155 | SwapValue(C.ExternalRelocationTableOffset); |
| 156 | SwapValue(C.NumExternalRelocationTableEntries); |
| 157 | SwapValue(C.LocalRelocationTableOffset); |
| 158 | SwapValue(C.NumLocalRelocationTableEntries); |
| 159 | } |
| 160 | |
| 161 | template<> |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 162 | void SwapStruct(macho::LinkeditDataLoadCommand &C) { |
| 163 | SwapValue(C.Type); |
| 164 | SwapValue(C.Size); |
| 165 | SwapValue(C.DataOffset); |
| 166 | SwapValue(C.DataSize); |
| 167 | } |
| 168 | |
| 169 | template<> |
| 170 | void SwapStruct(macho::SegmentLoadCommand &C) { |
| 171 | SwapValue(C.Type); |
| 172 | SwapValue(C.Size); |
| 173 | SwapValue(C.VMAddress); |
| 174 | SwapValue(C.VMSize); |
| 175 | SwapValue(C.FileOffset); |
| 176 | SwapValue(C.FileSize); |
| 177 | SwapValue(C.MaxVMProtection); |
| 178 | SwapValue(C.InitialVMProtection); |
| 179 | SwapValue(C.NumSections); |
| 180 | SwapValue(C.Flags); |
| 181 | } |
| 182 | |
| 183 | template<> |
| 184 | void SwapStruct(macho::Segment64LoadCommand &C) { |
| 185 | SwapValue(C.Type); |
| 186 | SwapValue(C.Size); |
| 187 | SwapValue(C.VMAddress); |
| 188 | SwapValue(C.VMSize); |
| 189 | SwapValue(C.FileOffset); |
| 190 | SwapValue(C.FileSize); |
| 191 | SwapValue(C.MaxVMProtection); |
| 192 | SwapValue(C.InitialVMProtection); |
| 193 | SwapValue(C.NumSections); |
| 194 | SwapValue(C.Flags); |
| 195 | } |
| 196 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 197 | template<> |
| 198 | void SwapStruct(macho::IndirectSymbolTableEntry &C) { |
| 199 | SwapValue(C.Index); |
| 200 | } |
| 201 | |
| 202 | template<> |
| 203 | void SwapStruct(macho::LinkerOptionsLoadCommand &C) { |
| 204 | SwapValue(C.Type); |
| 205 | SwapValue(C.Size); |
| 206 | SwapValue(C.Count); |
| 207 | } |
| 208 | |
| 209 | template<> |
| 210 | void SwapStruct(macho::DataInCodeTableEntry &C) { |
| 211 | SwapValue(C.Offset); |
| 212 | SwapValue(C.Length); |
| 213 | SwapValue(C.Kind); |
| 214 | } |
| 215 | |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 216 | template<typename T> |
| 217 | T getStruct(const MachOObjectFile *O, const char *P) { |
| 218 | T Cmd; |
| 219 | memcpy(&Cmd, P, sizeof(T)); |
| 220 | if (O->isLittleEndian() != sys::IsLittleEndianHost) |
| 221 | SwapStruct(Cmd); |
| 222 | return Cmd; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 225 | static uint32_t |
| 226 | getSegmentLoadCommandNumSections(const MachOObjectFile *O, |
| 227 | const MachOObjectFile::LoadCommandInfo &L) { |
| 228 | if (O->is64Bit()) { |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 229 | macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 230 | return S.NumSections; |
Rafael Espindola | 8764c89 | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 231 | } |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 232 | macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 233 | return S.NumSections; |
Rafael Espindola | 3eff318 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 236 | static const char * |
| 237 | getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L, |
| 238 | unsigned Sec) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 239 | uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); |
| 240 | |
| 241 | bool Is64 = O->is64Bit(); |
| 242 | unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) : |
| 243 | sizeof(macho::SegmentLoadCommand); |
| 244 | unsigned SectionSize = Is64 ? sizeof(macho::Section64) : |
| 245 | sizeof(macho::Section); |
| 246 | |
| 247 | uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 248 | return reinterpret_cast<const char*>(SectionAddr); |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 251 | static const char *getPtr(const MachOObjectFile *O, size_t Offset) { |
| 252 | return O->getData().substr(Offset, 1).data(); |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 255 | static SymbolTableEntryBase |
| 256 | getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 257 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 258 | return getStruct<SymbolTableEntryBase>(O, P); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 261 | static StringRef parseSegmentOrSectionName(const char *P) { |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 262 | if (P[15] == 0) |
| 263 | // Null terminated. |
| 264 | return P; |
| 265 | // Not null terminated, so this is a 16 char string. |
| 266 | return StringRef(P, 16); |
| 267 | } |
| 268 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 269 | // Helper to advance a section or symbol iterator multiple increments at a time. |
| 270 | template<class T> |
| 271 | static error_code advance(T &it, size_t Val) { |
| 272 | error_code ec; |
| 273 | while (Val--) { |
| 274 | it.increment(ec); |
| 275 | } |
| 276 | return ec; |
| 277 | } |
| 278 | |
| 279 | template<class T> |
| 280 | static void advanceTo(T &it, size_t Val) { |
| 281 | if (error_code ec = advance(it, Val)) |
| 282 | report_fatal_error(ec.message()); |
| 283 | } |
| 284 | |
| 285 | static unsigned getCPUType(const MachOObjectFile *O) { |
| 286 | return O->getHeader().CPUType; |
| 287 | } |
| 288 | |
| 289 | static void printRelocationTargetName(const MachOObjectFile *O, |
| 290 | const macho::RelocationEntry &RE, |
| 291 | raw_string_ostream &fmt) { |
| 292 | bool IsScattered = O->isRelocationScattered(RE); |
| 293 | |
| 294 | // Target of a scattered relocation is an address. In the interest of |
| 295 | // generating pretty output, scan through the symbol table looking for a |
| 296 | // symbol that aligns with that address. If we find one, print it. |
| 297 | // Otherwise, we just print the hex address of the target. |
| 298 | if (IsScattered) { |
| 299 | uint32_t Val = O->getPlainRelocationSymbolNum(RE); |
| 300 | |
| 301 | error_code ec; |
| 302 | for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols(); |
| 303 | SI != SE; SI.increment(ec)) { |
| 304 | if (ec) report_fatal_error(ec.message()); |
| 305 | |
| 306 | uint64_t Addr; |
| 307 | StringRef Name; |
| 308 | |
| 309 | if ((ec = SI->getAddress(Addr))) |
| 310 | report_fatal_error(ec.message()); |
| 311 | if (Addr != Val) continue; |
| 312 | if ((ec = SI->getName(Name))) |
| 313 | report_fatal_error(ec.message()); |
| 314 | fmt << Name; |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | // If we couldn't find a symbol that this relocation refers to, try |
| 319 | // to find a section beginning instead. |
| 320 | for (section_iterator SI = O->begin_sections(), SE = O->end_sections(); |
| 321 | SI != SE; SI.increment(ec)) { |
| 322 | if (ec) report_fatal_error(ec.message()); |
| 323 | |
| 324 | uint64_t Addr; |
| 325 | StringRef Name; |
| 326 | |
| 327 | if ((ec = SI->getAddress(Addr))) |
| 328 | report_fatal_error(ec.message()); |
| 329 | if (Addr != Val) continue; |
| 330 | if ((ec = SI->getName(Name))) |
| 331 | report_fatal_error(ec.message()); |
| 332 | fmt << Name; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | fmt << format("0x%x", Val); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | StringRef S; |
| 341 | bool isExtern = O->getPlainRelocationExternal(RE); |
Ahmed Bougacha | ebb9f17 | 2013-05-14 22:41:29 +0000 | [diff] [blame] | 342 | uint64_t Val = O->getPlainRelocationSymbolNum(RE); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 343 | |
| 344 | if (isExtern) { |
| 345 | symbol_iterator SI = O->begin_symbols(); |
| 346 | advanceTo(SI, Val); |
| 347 | SI->getName(S); |
| 348 | } else { |
| 349 | section_iterator SI = O->begin_sections(); |
Ahmed Bougacha | ebb9f17 | 2013-05-14 22:41:29 +0000 | [diff] [blame] | 350 | // Adjust for the fact that sections are 1-indexed. |
| 351 | advanceTo(SI, Val - 1); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 352 | SI->getName(S); |
| 353 | } |
| 354 | |
| 355 | fmt << S; |
| 356 | } |
| 357 | |
| 358 | static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) { |
| 359 | return RE.Word0; |
| 360 | } |
| 361 | |
| 362 | static unsigned |
| 363 | getScatteredRelocationAddress(const macho::RelocationEntry &RE) { |
| 364 | return RE.Word0 & 0xffffff; |
| 365 | } |
| 366 | |
| 367 | static bool getPlainRelocationPCRel(const MachOObjectFile *O, |
| 368 | const macho::RelocationEntry &RE) { |
| 369 | if (O->isLittleEndian()) |
| 370 | return (RE.Word1 >> 24) & 1; |
| 371 | return (RE.Word1 >> 7) & 1; |
| 372 | } |
| 373 | |
| 374 | static bool |
| 375 | getScatteredRelocationPCRel(const MachOObjectFile *O, |
| 376 | const macho::RelocationEntry &RE) { |
| 377 | return (RE.Word0 >> 30) & 1; |
| 378 | } |
| 379 | |
| 380 | static unsigned getPlainRelocationLength(const MachOObjectFile *O, |
| 381 | const macho::RelocationEntry &RE) { |
| 382 | if (O->isLittleEndian()) |
| 383 | return (RE.Word1 >> 25) & 3; |
| 384 | return (RE.Word1 >> 5) & 3; |
| 385 | } |
| 386 | |
| 387 | static unsigned |
| 388 | getScatteredRelocationLength(const macho::RelocationEntry &RE) { |
| 389 | return (RE.Word0 >> 28) & 3; |
| 390 | } |
| 391 | |
| 392 | static unsigned getPlainRelocationType(const MachOObjectFile *O, |
| 393 | const macho::RelocationEntry &RE) { |
| 394 | if (O->isLittleEndian()) |
| 395 | return RE.Word1 >> 28; |
| 396 | return RE.Word1 & 0xf; |
| 397 | } |
| 398 | |
| 399 | static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) { |
| 400 | return (RE.Word0 >> 24) & 0xf; |
| 401 | } |
| 402 | |
| 403 | static uint32_t getSectionFlags(const MachOObjectFile *O, |
| 404 | DataRefImpl Sec) { |
| 405 | if (O->is64Bit()) { |
| 406 | macho::Section64 Sect = O->getSection64(Sec); |
| 407 | return Sect.Flags; |
| 408 | } |
| 409 | macho::Section Sect = O->getSection(Sec); |
| 410 | return Sect.Flags; |
| 411 | } |
| 412 | |
| 413 | MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, |
| 414 | bool IsLittleEndian, bool Is64bits, |
| 415 | error_code &ec) |
| 416 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object), |
Kevin Enderby | 54154f3 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 417 | SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 418 | uint32_t LoadCommandCount = this->getHeader().NumLoadCommands; |
| 419 | macho::LoadCommandType SegmentLoadType = is64Bit() ? |
| 420 | macho::LCT_Segment64 : macho::LCT_Segment; |
| 421 | |
| 422 | MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo(); |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 423 | for (unsigned I = 0; ; ++I) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 424 | if (Load.C.Type == macho::LCT_Symtab) { |
| 425 | assert(!SymtabLoadCmd && "Multiple symbol tables"); |
| 426 | SymtabLoadCmd = Load.Ptr; |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 427 | } else if (Load.C.Type == macho::LCT_Dysymtab) { |
| 428 | assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables"); |
| 429 | DysymtabLoadCmd = Load.Ptr; |
Kevin Enderby | 54154f3 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 430 | } else if (Load.C.Type == macho::LCT_DataInCode) { |
| 431 | assert(!DataInCodeLoadCmd && "Multiple data in code tables"); |
| 432 | DataInCodeLoadCmd = Load.Ptr; |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 433 | } else if (Load.C.Type == SegmentLoadType) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 434 | uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load); |
| 435 | for (unsigned J = 0; J < NumSections; ++J) { |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 436 | const char *Sec = getSectionPtr(this, Load, J); |
| 437 | Sections.push_back(Sec); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 440 | |
| 441 | if (I == LoadCommandCount - 1) |
| 442 | break; |
| 443 | else |
| 444 | Load = getNextLoadCommandInfo(Load); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb, |
| 449 | SymbolRef &Res) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 450 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 451 | sizeof(macho::Symbol64TableEntry) : |
| 452 | sizeof(macho::SymbolTableEntry); |
| 453 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 454 | Res = SymbolRef(Symb, this); |
| 455 | return object_error::success; |
| 456 | } |
| 457 | |
| 458 | error_code MachOObjectFile::getSymbolName(DataRefImpl Symb, |
| 459 | StringRef &Res) const { |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 460 | StringRef StringTable = getStringTableData(); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 461 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 462 | const char *Start = &StringTable.data()[Entry.StringIndex]; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 463 | Res = StringRef(Start); |
| 464 | return object_error::success; |
| 465 | } |
| 466 | |
| 467 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 468 | uint64_t &Res) const { |
| 469 | if (is64Bit()) { |
| 470 | macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb); |
| 471 | Res = Entry.Value; |
| 472 | } else { |
| 473 | macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb); |
| 474 | Res = Entry.Value; |
| 475 | } |
| 476 | return object_error::success; |
| 477 | } |
| 478 | |
| 479 | error_code |
| 480 | MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb, |
| 481 | uint64_t &Res) const { |
| 482 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 483 | getSymbolAddress(Symb, Res); |
| 484 | if (Entry.SectionIndex) { |
| 485 | uint64_t Delta; |
| 486 | DataRefImpl SecRel; |
| 487 | SecRel.d.a = Entry.SectionIndex-1; |
| 488 | if (is64Bit()) { |
| 489 | macho::Section64 Sec = getSection64(SecRel); |
| 490 | Delta = Sec.Offset - Sec.Address; |
| 491 | } else { |
| 492 | macho::Section Sec = getSection(SecRel); |
| 493 | Delta = Sec.Offset - Sec.Address; |
| 494 | } |
| 495 | |
| 496 | Res += Delta; |
| 497 | } |
| 498 | |
| 499 | return object_error::success; |
| 500 | } |
| 501 | |
Rafael Espindola | 59a0e79 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 502 | error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI, |
| 503 | uint32_t &Result) const { |
| 504 | uint32_t flags; |
| 505 | this->getSymbolFlags(DRI, flags); |
| 506 | if (flags & SymbolRef::SF_Common) { |
| 507 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); |
| 508 | Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags); |
| 509 | } else { |
| 510 | Result = 0; |
| 511 | } |
| 512 | return object_error::success; |
| 513 | } |
| 514 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 515 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 516 | uint64_t &Result) const { |
| 517 | uint64_t BeginOffset; |
| 518 | uint64_t EndOffset = 0; |
| 519 | uint8_t SectionIndex; |
| 520 | |
| 521 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); |
| 522 | uint64_t Value; |
| 523 | getSymbolAddress(DRI, Value); |
| 524 | |
| 525 | BeginOffset = Value; |
| 526 | |
| 527 | SectionIndex = Entry.SectionIndex; |
| 528 | if (!SectionIndex) { |
| 529 | uint32_t flags = SymbolRef::SF_None; |
| 530 | this->getSymbolFlags(DRI, flags); |
| 531 | if (flags & SymbolRef::SF_Common) |
| 532 | Result = Value; |
| 533 | else |
| 534 | Result = UnknownAddressOrSize; |
| 535 | return object_error::success; |
| 536 | } |
| 537 | // Unfortunately symbols are unsorted so we need to touch all |
| 538 | // symbols from load command |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 539 | error_code ec; |
| 540 | for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; |
| 541 | I.increment(ec)) { |
| 542 | DataRefImpl DRI = I->getRawDataRefImpl(); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 543 | Entry = getSymbolTableEntryBase(this, DRI); |
| 544 | getSymbolAddress(DRI, Value); |
| 545 | if (Entry.SectionIndex == SectionIndex && Value > BeginOffset) |
| 546 | if (!EndOffset || Value < EndOffset) |
| 547 | EndOffset = Value; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 548 | } |
| 549 | if (!EndOffset) { |
| 550 | uint64_t Size; |
| 551 | DataRefImpl Sec; |
| 552 | Sec.d.a = SectionIndex-1; |
| 553 | getSectionSize(Sec, Size); |
| 554 | getSectionAddress(Sec, EndOffset); |
| 555 | EndOffset += Size; |
| 556 | } |
| 557 | Result = EndOffset - BeginOffset; |
| 558 | return object_error::success; |
| 559 | } |
| 560 | |
| 561 | error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, |
| 562 | SymbolRef::Type &Res) const { |
| 563 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 564 | uint8_t n_type = Entry.Type; |
| 565 | |
| 566 | Res = SymbolRef::ST_Other; |
| 567 | |
| 568 | // If this is a STAB debugging symbol, we can do nothing more. |
| 569 | if (n_type & MachO::NlistMaskStab) { |
| 570 | Res = SymbolRef::ST_Debug; |
| 571 | return object_error::success; |
| 572 | } |
| 573 | |
| 574 | switch (n_type & MachO::NlistMaskType) { |
| 575 | case MachO::NListTypeUndefined : |
| 576 | Res = SymbolRef::ST_Unknown; |
| 577 | break; |
| 578 | case MachO::NListTypeSection : |
| 579 | Res = SymbolRef::ST_Function; |
| 580 | break; |
| 581 | } |
| 582 | return object_error::success; |
| 583 | } |
| 584 | |
| 585 | error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, |
| 586 | char &Res) const { |
| 587 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 588 | uint8_t Type = Entry.Type; |
| 589 | uint16_t Flags = Entry.Flags; |
| 590 | |
| 591 | char Char; |
| 592 | switch (Type & macho::STF_TypeMask) { |
| 593 | case macho::STT_Undefined: |
| 594 | Char = 'u'; |
| 595 | break; |
| 596 | case macho::STT_Absolute: |
| 597 | case macho::STT_Section: |
| 598 | Char = 's'; |
| 599 | break; |
| 600 | default: |
| 601 | Char = '?'; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | if (Flags & (macho::STF_External | macho::STF_PrivateExtern)) |
| 606 | Char = toupper(static_cast<unsigned char>(Char)); |
| 607 | Res = Char; |
| 608 | return object_error::success; |
| 609 | } |
| 610 | |
| 611 | error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI, |
| 612 | uint32_t &Result) const { |
| 613 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); |
| 614 | |
| 615 | uint8_t MachOType = Entry.Type; |
| 616 | uint16_t MachOFlags = Entry.Flags; |
| 617 | |
| 618 | // TODO: Correctly set SF_ThreadLocal |
| 619 | Result = SymbolRef::SF_None; |
| 620 | |
| 621 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) |
| 622 | Result |= SymbolRef::SF_Undefined; |
| 623 | |
| 624 | if (MachOFlags & macho::STF_StabsEntryMask) |
| 625 | Result |= SymbolRef::SF_FormatSpecific; |
| 626 | |
| 627 | if (MachOType & MachO::NlistMaskExternal) { |
| 628 | Result |= SymbolRef::SF_Global; |
Rafael Espindola | 59a0e79 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 629 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) { |
| 630 | uint64_t Value; |
| 631 | getSymbolAddress(DRI, Value); |
| 632 | if (Value) |
| 633 | Result |= SymbolRef::SF_Common; |
| 634 | } |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef)) |
| 638 | Result |= SymbolRef::SF_Weak; |
| 639 | |
| 640 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute) |
| 641 | Result |= SymbolRef::SF_Absolute; |
| 642 | |
| 643 | return object_error::success; |
| 644 | } |
| 645 | |
| 646 | error_code |
| 647 | MachOObjectFile::getSymbolSection(DataRefImpl Symb, |
| 648 | section_iterator &Res) const { |
| 649 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 650 | uint8_t index = Entry.SectionIndex; |
| 651 | |
| 652 | if (index == 0) { |
| 653 | Res = end_sections(); |
| 654 | } else { |
| 655 | DataRefImpl DRI; |
| 656 | DRI.d.a = index - 1; |
| 657 | Res = section_iterator(SectionRef(DRI, this)); |
| 658 | } |
| 659 | |
| 660 | return object_error::success; |
| 661 | } |
| 662 | |
| 663 | error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 664 | uint64_t &Val) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 665 | report_fatal_error("getSymbolValue unimplemented in MachOObjectFile"); |
| 666 | } |
| 667 | |
| 668 | error_code MachOObjectFile::getSectionNext(DataRefImpl Sec, |
| 669 | SectionRef &Res) const { |
| 670 | Sec.d.a++; |
| 671 | Res = SectionRef(Sec, this); |
| 672 | return object_error::success; |
| 673 | } |
| 674 | |
| 675 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 676 | MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 677 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 678 | Result = parseSegmentOrSectionName(Raw.data()); |
| 679 | return object_error::success; |
| 680 | } |
| 681 | |
| 682 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 683 | MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 684 | if (is64Bit()) { |
| 685 | macho::Section64 Sect = getSection64(Sec); |
| 686 | Res = Sect.Address; |
| 687 | } else { |
| 688 | macho::Section Sect = getSection(Sec); |
| 689 | Res = Sect.Address; |
| 690 | } |
| 691 | return object_error::success; |
| 692 | } |
| 693 | |
| 694 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 695 | MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 696 | if (is64Bit()) { |
| 697 | macho::Section64 Sect = getSection64(Sec); |
| 698 | Res = Sect.Size; |
| 699 | } else { |
| 700 | macho::Section Sect = getSection(Sec); |
| 701 | Res = Sect.Size; |
| 702 | } |
| 703 | |
| 704 | return object_error::success; |
| 705 | } |
| 706 | |
| 707 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 708 | MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 709 | uint32_t Offset; |
| 710 | uint64_t Size; |
| 711 | |
| 712 | if (is64Bit()) { |
| 713 | macho::Section64 Sect = getSection64(Sec); |
| 714 | Offset = Sect.Offset; |
| 715 | Size = Sect.Size; |
| 716 | } else { |
| 717 | macho::Section Sect =getSection(Sec); |
| 718 | Offset = Sect.Offset; |
| 719 | Size = Sect.Size; |
| 720 | } |
| 721 | |
| 722 | Res = this->getData().substr(Offset, Size); |
| 723 | return object_error::success; |
| 724 | } |
| 725 | |
| 726 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 727 | MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 728 | uint32_t Align; |
| 729 | if (is64Bit()) { |
| 730 | macho::Section64 Sect = getSection64(Sec); |
| 731 | Align = Sect.Align; |
| 732 | } else { |
| 733 | macho::Section Sect = getSection(Sec); |
| 734 | Align = Sect.Align; |
| 735 | } |
| 736 | |
| 737 | Res = uint64_t(1) << Align; |
| 738 | return object_error::success; |
| 739 | } |
| 740 | |
| 741 | error_code |
| 742 | MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { |
| 743 | uint32_t Flags = getSectionFlags(this, Sec); |
| 744 | Res = Flags & macho::SF_PureInstructions; |
| 745 | return object_error::success; |
| 746 | } |
| 747 | |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 748 | error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const { |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 749 | // FIXME: Unimplemented. |
| 750 | Result = false; |
| 751 | return object_error::success; |
| 752 | } |
| 753 | |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 754 | error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const { |
Andrew Kaylor | 30b20eb | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 755 | // FIXME: Unimplemented. |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 756 | Result = false; |
| 757 | return object_error::success; |
| 758 | } |
| 759 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 760 | error_code |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 761 | MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 762 | bool &Result) const { |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 763 | // FIXME: Unimplemented. |
| 764 | Result = true; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 765 | return object_error::success; |
| 766 | } |
| 767 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 768 | error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 769 | bool &Result) const { |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 770 | // FIXME: Unimplemented. |
| 771 | Result = false; |
| 772 | return object_error::success; |
| 773 | } |
| 774 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 775 | error_code |
| 776 | MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { |
| 777 | uint32_t Flags = getSectionFlags(this, Sec); |
| 778 | unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType; |
| 779 | Res = SectionType == MachO::SectionTypeZeroFill || |
| 780 | SectionType == MachO::SectionTypeZeroFillLarge; |
| 781 | return object_error::success; |
| 782 | } |
| 783 | |
| 784 | error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 785 | bool &Result) const { |
Andrew Kaylor | 3a129c8 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 786 | // Consider using the code from isSectionText to look for __const sections. |
| 787 | // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS |
| 788 | // to use section attributes to distinguish code from data. |
| 789 | |
| 790 | // FIXME: Unimplemented. |
| 791 | Result = false; |
| 792 | return object_error::success; |
| 793 | } |
| 794 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 795 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 796 | MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 797 | bool &Result) const { |
| 798 | SymbolRef::Type ST; |
| 799 | this->getSymbolType(Symb, ST); |
| 800 | if (ST == SymbolRef::ST_Unknown) { |
| 801 | Result = false; |
| 802 | return object_error::success; |
| 803 | } |
| 804 | |
| 805 | uint64_t SectBegin, SectEnd; |
| 806 | getSectionAddress(Sec, SectBegin); |
| 807 | getSectionSize(Sec, SectEnd); |
| 808 | SectEnd += SectBegin; |
| 809 | |
| 810 | uint64_t SymAddr; |
| 811 | getSymbolAddress(Symb, SymAddr); |
| 812 | Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); |
| 813 | |
| 814 | return object_error::success; |
| 815 | } |
| 816 | |
| 817 | relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 818 | uint32_t Offset; |
| 819 | if (is64Bit()) { |
| 820 | macho::Section64 Sect = getSection64(Sec); |
| 821 | Offset = Sect.RelocationTableOffset; |
| 822 | } else { |
| 823 | macho::Section Sect = getSection(Sec); |
| 824 | Offset = Sect.RelocationTableOffset; |
| 825 | } |
| 826 | |
| 827 | DataRefImpl Ret; |
| 828 | Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 829 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 830 | } |
Rafael Espindola | 335f1d4 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 831 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 832 | relocation_iterator |
| 833 | MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 834 | uint32_t Offset; |
| 835 | uint32_t Num; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 836 | if (is64Bit()) { |
| 837 | macho::Section64 Sect = getSection64(Sec); |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 838 | Offset = Sect.RelocationTableOffset; |
| 839 | Num = Sect.NumRelocationTableEntries; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 840 | } else { |
| 841 | macho::Section Sect = getSection(Sec); |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 842 | Offset = Sect.RelocationTableOffset; |
| 843 | Num = Sect.NumRelocationTableEntries; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 844 | } |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 845 | |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 846 | const macho::RelocationEntry *P = |
| 847 | reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset)); |
| 848 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 849 | DataRefImpl Ret; |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 850 | Ret.p = reinterpret_cast<uintptr_t>(P + Num); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 851 | return relocation_iterator(RelocationRef(Ret, this)); |
| 852 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 853 | |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 854 | error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel, |
| 855 | RelocationRef &Res) const { |
| 856 | const macho::RelocationEntry *P = |
| 857 | reinterpret_cast<const macho::RelocationEntry *>(Rel.p); |
| 858 | Rel.p = reinterpret_cast<uintptr_t>(P + 1); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 859 | Res = RelocationRef(Rel, this); |
| 860 | return object_error::success; |
| 861 | } |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 862 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 863 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 864 | MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { |
Rafael Espindola | 956ca72 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 865 | report_fatal_error("getRelocationAddress not implemented in MachOObjectFile"); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 868 | error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 869 | uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 870 | macho::RelocationEntry RE = getRelocation(Rel); |
| 871 | Res = getAnyRelocationAddress(RE); |
| 872 | return object_error::success; |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 875 | symbol_iterator |
| 876 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 877 | macho::RelocationEntry RE = getRelocation(Rel); |
| 878 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 879 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 880 | if (!isExtern) |
| 881 | return end_symbols(); |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 882 | |
| 883 | macho::SymtabLoadCommand S = getSymtabLoadCommand(); |
| 884 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 885 | sizeof(macho::Symbol64TableEntry) : |
| 886 | sizeof(macho::SymbolTableEntry); |
| 887 | uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize; |
| 888 | DataRefImpl Sym; |
| 889 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 890 | return symbol_iterator(SymbolRef(Sym, this)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 894 | uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 895 | macho::RelocationEntry RE = getRelocation(Rel); |
| 896 | Res = getAnyRelocationType(RE); |
| 897 | return object_error::success; |
| 898 | } |
| 899 | |
| 900 | error_code |
| 901 | MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 902 | SmallVectorImpl<char> &Result) const { |
| 903 | StringRef res; |
| 904 | uint64_t RType; |
| 905 | getRelocationType(Rel, RType); |
| 906 | |
| 907 | unsigned Arch = this->getArch(); |
| 908 | |
| 909 | switch (Arch) { |
| 910 | case Triple::x86: { |
| 911 | static const char *const Table[] = { |
| 912 | "GENERIC_RELOC_VANILLA", |
| 913 | "GENERIC_RELOC_PAIR", |
| 914 | "GENERIC_RELOC_SECTDIFF", |
| 915 | "GENERIC_RELOC_PB_LA_PTR", |
| 916 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 917 | "GENERIC_RELOC_TLV" }; |
| 918 | |
| 919 | if (RType > 6) |
| 920 | res = "Unknown"; |
| 921 | else |
| 922 | res = Table[RType]; |
| 923 | break; |
| 924 | } |
| 925 | case Triple::x86_64: { |
| 926 | static const char *const Table[] = { |
| 927 | "X86_64_RELOC_UNSIGNED", |
| 928 | "X86_64_RELOC_SIGNED", |
| 929 | "X86_64_RELOC_BRANCH", |
| 930 | "X86_64_RELOC_GOT_LOAD", |
| 931 | "X86_64_RELOC_GOT", |
| 932 | "X86_64_RELOC_SUBTRACTOR", |
| 933 | "X86_64_RELOC_SIGNED_1", |
| 934 | "X86_64_RELOC_SIGNED_2", |
| 935 | "X86_64_RELOC_SIGNED_4", |
| 936 | "X86_64_RELOC_TLV" }; |
| 937 | |
| 938 | if (RType > 9) |
| 939 | res = "Unknown"; |
| 940 | else |
| 941 | res = Table[RType]; |
| 942 | break; |
| 943 | } |
| 944 | case Triple::arm: { |
| 945 | static const char *const Table[] = { |
| 946 | "ARM_RELOC_VANILLA", |
| 947 | "ARM_RELOC_PAIR", |
| 948 | "ARM_RELOC_SECTDIFF", |
| 949 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 950 | "ARM_RELOC_PB_LA_PTR", |
| 951 | "ARM_RELOC_BR24", |
| 952 | "ARM_THUMB_RELOC_BR22", |
| 953 | "ARM_THUMB_32BIT_BRANCH", |
| 954 | "ARM_RELOC_HALF", |
| 955 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 956 | |
| 957 | if (RType > 9) |
| 958 | res = "Unknown"; |
| 959 | else |
| 960 | res = Table[RType]; |
| 961 | break; |
| 962 | } |
| 963 | case Triple::ppc: { |
| 964 | static const char *const Table[] = { |
| 965 | "PPC_RELOC_VANILLA", |
| 966 | "PPC_RELOC_PAIR", |
| 967 | "PPC_RELOC_BR14", |
| 968 | "PPC_RELOC_BR24", |
| 969 | "PPC_RELOC_HI16", |
| 970 | "PPC_RELOC_LO16", |
| 971 | "PPC_RELOC_HA16", |
| 972 | "PPC_RELOC_LO14", |
| 973 | "PPC_RELOC_SECTDIFF", |
| 974 | "PPC_RELOC_PB_LA_PTR", |
| 975 | "PPC_RELOC_HI16_SECTDIFF", |
| 976 | "PPC_RELOC_LO16_SECTDIFF", |
| 977 | "PPC_RELOC_HA16_SECTDIFF", |
| 978 | "PPC_RELOC_JBSR", |
| 979 | "PPC_RELOC_LO14_SECTDIFF", |
| 980 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 981 | |
| 982 | res = Table[RType]; |
| 983 | break; |
| 984 | } |
| 985 | case Triple::UnknownArch: |
| 986 | res = "Unknown"; |
| 987 | break; |
| 988 | } |
| 989 | Result.append(res.begin(), res.end()); |
| 990 | return object_error::success; |
| 991 | } |
| 992 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 993 | error_code |
| 994 | MachOObjectFile::getRelocationValueString(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 995 | SmallVectorImpl<char> &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 996 | macho::RelocationEntry RE = getRelocation(Rel); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 997 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 998 | unsigned Arch = this->getArch(); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 999 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1000 | std::string fmtbuf; |
| 1001 | raw_string_ostream fmt(fmtbuf); |
| 1002 | unsigned Type = this->getAnyRelocationType(RE); |
| 1003 | bool IsPCRel = this->getAnyRelocationPCRel(RE); |
| 1004 | |
| 1005 | // Determine any addends that should be displayed with the relocation. |
| 1006 | // These require decoding the relocation type, which is triple-specific. |
| 1007 | |
| 1008 | // X86_64 has entirely custom relocation types. |
| 1009 | if (Arch == Triple::x86_64) { |
| 1010 | bool isPCRel = getAnyRelocationPCRel(RE); |
| 1011 | |
| 1012 | switch (Type) { |
| 1013 | case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD |
| 1014 | case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT |
| 1015 | printRelocationTargetName(this, RE, fmt); |
| 1016 | fmt << "@GOT"; |
| 1017 | if (isPCRel) fmt << "PCREL"; |
| 1018 | break; |
| 1019 | } |
| 1020 | case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR |
| 1021 | DataRefImpl RelNext = Rel; |
| 1022 | RelNext.d.a++; |
| 1023 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1024 | |
| 1025 | // X86_64_SUBTRACTOR must be followed by a relocation of type |
| 1026 | // X86_64_RELOC_UNSIGNED. |
| 1027 | // NOTE: Scattered relocations don't exist on x86_64. |
| 1028 | unsigned RType = getAnyRelocationType(RENext); |
| 1029 | if (RType != 0) |
| 1030 | report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " |
| 1031 | "X86_64_RELOC_SUBTRACTOR."); |
| 1032 | |
| 1033 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol, |
| 1034 | // X86_64_SUBTRACTOR contains to the subtrahend. |
| 1035 | printRelocationTargetName(this, RENext, fmt); |
| 1036 | fmt << "-"; |
| 1037 | printRelocationTargetName(this, RE, fmt); |
| 1038 | break; |
| 1039 | } |
| 1040 | case macho::RIT_X86_64_TLV: |
| 1041 | printRelocationTargetName(this, RE, fmt); |
| 1042 | fmt << "@TLV"; |
| 1043 | if (isPCRel) fmt << "P"; |
| 1044 | break; |
| 1045 | case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1 |
| 1046 | printRelocationTargetName(this, RE, fmt); |
| 1047 | fmt << "-1"; |
| 1048 | break; |
| 1049 | case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2 |
| 1050 | printRelocationTargetName(this, RE, fmt); |
| 1051 | fmt << "-2"; |
| 1052 | break; |
| 1053 | case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4 |
| 1054 | printRelocationTargetName(this, RE, fmt); |
| 1055 | fmt << "-4"; |
| 1056 | break; |
| 1057 | default: |
| 1058 | printRelocationTargetName(this, RE, fmt); |
| 1059 | break; |
| 1060 | } |
| 1061 | // X86 and ARM share some relocation types in common. |
| 1062 | } else if (Arch == Triple::x86 || Arch == Triple::arm) { |
| 1063 | // Generic relocation types... |
| 1064 | switch (Type) { |
| 1065 | case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info |
| 1066 | return object_error::success; |
| 1067 | case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF |
| 1068 | DataRefImpl RelNext = Rel; |
| 1069 | RelNext.d.a++; |
| 1070 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1071 | |
| 1072 | // X86 sect diff's must be followed by a relocation of type |
| 1073 | // GENERIC_RELOC_PAIR. |
| 1074 | unsigned RType = getAnyRelocationType(RENext); |
| 1075 | |
| 1076 | if (RType != 1) |
| 1077 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1078 | "GENERIC_RELOC_SECTDIFF."); |
| 1079 | |
| 1080 | printRelocationTargetName(this, RE, fmt); |
| 1081 | fmt << "-"; |
| 1082 | printRelocationTargetName(this, RENext, fmt); |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | if (Arch == Triple::x86) { |
| 1088 | // All X86 relocations that need special printing were already |
| 1089 | // handled in the generic code. |
| 1090 | switch (Type) { |
| 1091 | case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF |
| 1092 | DataRefImpl RelNext = Rel; |
| 1093 | RelNext.d.a++; |
| 1094 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1095 | |
| 1096 | // X86 sect diff's must be followed by a relocation of type |
| 1097 | // GENERIC_RELOC_PAIR. |
| 1098 | unsigned RType = getAnyRelocationType(RENext); |
| 1099 | if (RType != 1) |
| 1100 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1101 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
| 1102 | |
| 1103 | printRelocationTargetName(this, RE, fmt); |
| 1104 | fmt << "-"; |
| 1105 | printRelocationTargetName(this, RENext, fmt); |
| 1106 | break; |
| 1107 | } |
| 1108 | case macho::RIT_Generic_TLV: { |
| 1109 | printRelocationTargetName(this, RE, fmt); |
| 1110 | fmt << "@TLV"; |
| 1111 | if (IsPCRel) fmt << "P"; |
| 1112 | break; |
| 1113 | } |
| 1114 | default: |
| 1115 | printRelocationTargetName(this, RE, fmt); |
| 1116 | } |
| 1117 | } else { // ARM-specific relocations |
| 1118 | switch (Type) { |
| 1119 | case macho::RIT_ARM_Half: // ARM_RELOC_HALF |
| 1120 | case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF |
| 1121 | // Half relocations steal a bit from the length field to encode |
| 1122 | // whether this is an upper16 or a lower16 relocation. |
| 1123 | bool isUpper = getAnyRelocationLength(RE) >> 1; |
| 1124 | |
| 1125 | if (isUpper) |
| 1126 | fmt << ":upper16:("; |
| 1127 | else |
| 1128 | fmt << ":lower16:("; |
| 1129 | printRelocationTargetName(this, RE, fmt); |
| 1130 | |
| 1131 | DataRefImpl RelNext = Rel; |
| 1132 | RelNext.d.a++; |
| 1133 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1134 | |
| 1135 | // ARM half relocs must be followed by a relocation of type |
| 1136 | // ARM_RELOC_PAIR. |
| 1137 | unsigned RType = getAnyRelocationType(RENext); |
| 1138 | if (RType != 1) |
| 1139 | report_fatal_error("Expected ARM_RELOC_PAIR after " |
| 1140 | "GENERIC_RELOC_HALF"); |
| 1141 | |
| 1142 | // NOTE: The half of the target virtual address is stashed in the |
| 1143 | // address field of the secondary relocation, but we can't reverse |
| 1144 | // engineer the constant offset from it without decoding the movw/movt |
| 1145 | // instruction to find the other half in its immediate field. |
| 1146 | |
| 1147 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 1148 | // symbol/section pointer of the follow-on relocation. |
| 1149 | if (Type == macho::RIT_ARM_HalfDifference) { |
| 1150 | fmt << "-"; |
| 1151 | printRelocationTargetName(this, RENext, fmt); |
| 1152 | } |
| 1153 | |
| 1154 | fmt << ")"; |
| 1155 | break; |
| 1156 | } |
| 1157 | default: { |
| 1158 | printRelocationTargetName(this, RE, fmt); |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } else |
| 1163 | printRelocationTargetName(this, RE, fmt); |
| 1164 | |
| 1165 | fmt.flush(); |
| 1166 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 1167 | return object_error::success; |
| 1168 | } |
| 1169 | |
| 1170 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1171 | MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1172 | unsigned Arch = getArch(); |
| 1173 | uint64_t Type; |
| 1174 | getRelocationType(Rel, Type); |
| 1175 | |
| 1176 | Result = false; |
| 1177 | |
| 1178 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 1179 | // is always hidden. |
| 1180 | if (Arch == Triple::x86 || Arch == Triple::arm) { |
| 1181 | if (Type == macho::RIT_Pair) Result = true; |
| 1182 | } else if (Arch == Triple::x86_64) { |
| 1183 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
| 1184 | // an X864_64_RELOC_SUBTRACTOR. |
| 1185 | if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) { |
| 1186 | DataRefImpl RelPrev = Rel; |
| 1187 | RelPrev.d.a--; |
| 1188 | uint64_t PrevType; |
| 1189 | getRelocationType(RelPrev, PrevType); |
| 1190 | if (PrevType == macho::RIT_X86_64_Subtractor) |
| 1191 | Result = true; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | return object_error::success; |
| 1196 | } |
| 1197 | |
| 1198 | error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1199 | LibraryRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1200 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1201 | } |
| 1202 | |
| 1203 | error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1204 | StringRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1205 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1206 | } |
| 1207 | |
| 1208 | symbol_iterator MachOObjectFile::begin_symbols() const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1209 | DataRefImpl DRI; |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1210 | if (!SymtabLoadCmd) |
| 1211 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1212 | |
| 1213 | macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); |
| 1214 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1215 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1216 | } |
| 1217 | |
| 1218 | symbol_iterator MachOObjectFile::end_symbols() const { |
| 1219 | DataRefImpl DRI; |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1220 | if (!SymtabLoadCmd) |
| 1221 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1222 | |
| 1223 | macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); |
| 1224 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 1225 | sizeof(macho::Symbol64TableEntry) : |
| 1226 | sizeof(macho::SymbolTableEntry); |
| 1227 | unsigned Offset = Symtab.SymbolTableOffset + |
| 1228 | Symtab.NumSymbolTableEntries * SymbolTableEntrySize; |
| 1229 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1230 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1231 | } |
| 1232 | |
| 1233 | symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { |
| 1234 | // TODO: implement |
| 1235 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 1236 | } |
| 1237 | |
| 1238 | symbol_iterator MachOObjectFile::end_dynamic_symbols() const { |
| 1239 | // TODO: implement |
| 1240 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 1241 | } |
| 1242 | |
| 1243 | section_iterator MachOObjectFile::begin_sections() const { |
| 1244 | DataRefImpl DRI; |
| 1245 | return section_iterator(SectionRef(DRI, this)); |
| 1246 | } |
| 1247 | |
| 1248 | section_iterator MachOObjectFile::end_sections() const { |
| 1249 | DataRefImpl DRI; |
| 1250 | DRI.d.a = Sections.size(); |
| 1251 | return section_iterator(SectionRef(DRI, this)); |
| 1252 | } |
| 1253 | |
| 1254 | library_iterator MachOObjectFile::begin_libraries_needed() const { |
| 1255 | // TODO: implement |
| 1256 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1257 | } |
| 1258 | |
| 1259 | library_iterator MachOObjectFile::end_libraries_needed() const { |
| 1260 | // TODO: implement |
| 1261 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1262 | } |
| 1263 | |
| 1264 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 1265 | return is64Bit() ? 8 : 4; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1268 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1269 | unsigned CPUType = getCPUType(this); |
| 1270 | if (!is64Bit()) { |
| 1271 | switch (CPUType) { |
| 1272 | case llvm::MachO::CPUTypeI386: |
| 1273 | return "Mach-O 32-bit i386"; |
| 1274 | case llvm::MachO::CPUTypeARM: |
| 1275 | return "Mach-O arm"; |
| 1276 | case llvm::MachO::CPUTypePowerPC: |
| 1277 | return "Mach-O 32-bit ppc"; |
| 1278 | default: |
| 1279 | assert((CPUType & llvm::MachO::CPUArchABI64) == 0 && |
| 1280 | "64-bit object file when we're not 64-bit?"); |
| 1281 | return "Mach-O 32-bit unknown"; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | // Make sure the cpu type has the correct mask. |
| 1286 | assert((CPUType & llvm::MachO::CPUArchABI64) |
| 1287 | == llvm::MachO::CPUArchABI64 && |
| 1288 | "32-bit object file when we're 64-bit?"); |
| 1289 | |
| 1290 | switch (CPUType) { |
| 1291 | case llvm::MachO::CPUTypeX86_64: |
| 1292 | return "Mach-O 64-bit x86-64"; |
| 1293 | case llvm::MachO::CPUTypePowerPC64: |
| 1294 | return "Mach-O 64-bit ppc64"; |
| 1295 | default: |
| 1296 | return "Mach-O 64-bit unknown"; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | unsigned MachOObjectFile::getArch() const { |
| 1301 | switch (getCPUType(this)) { |
| 1302 | case llvm::MachO::CPUTypeI386: |
| 1303 | return Triple::x86; |
| 1304 | case llvm::MachO::CPUTypeX86_64: |
| 1305 | return Triple::x86_64; |
| 1306 | case llvm::MachO::CPUTypeARM: |
| 1307 | return Triple::arm; |
| 1308 | case llvm::MachO::CPUTypePowerPC: |
| 1309 | return Triple::ppc; |
| 1310 | case llvm::MachO::CPUTypePowerPC64: |
| 1311 | return Triple::ppc64; |
| 1312 | default: |
| 1313 | return Triple::UnknownArch; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | StringRef MachOObjectFile::getLoadName() const { |
| 1318 | // TODO: Implement |
| 1319 | report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); |
| 1320 | } |
| 1321 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1322 | relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const { |
| 1323 | DataRefImpl DRI; |
| 1324 | DRI.d.a = Index; |
| 1325 | return getSectionRelBegin(DRI); |
| 1326 | } |
| 1327 | |
| 1328 | relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const { |
| 1329 | DataRefImpl DRI; |
| 1330 | DRI.d.a = Index; |
| 1331 | return getSectionRelEnd(DRI); |
| 1332 | } |
| 1333 | |
Kevin Enderby | 54154f3 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1334 | dice_iterator MachOObjectFile::begin_dices() const { |
| 1335 | DataRefImpl DRI; |
| 1336 | if (!DataInCodeLoadCmd) |
| 1337 | return dice_iterator(DiceRef(DRI, this)); |
| 1338 | |
| 1339 | macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand(); |
| 1340 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.DataOffset)); |
| 1341 | return dice_iterator(DiceRef(DRI, this)); |
| 1342 | } |
| 1343 | |
| 1344 | dice_iterator MachOObjectFile::end_dices() const { |
| 1345 | DataRefImpl DRI; |
| 1346 | if (!DataInCodeLoadCmd) |
| 1347 | return dice_iterator(DiceRef(DRI, this)); |
| 1348 | |
| 1349 | macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand(); |
| 1350 | unsigned Offset = DicLC.DataOffset + DicLC.DataSize; |
| 1351 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 1352 | return dice_iterator(DiceRef(DRI, this)); |
| 1353 | } |
| 1354 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1355 | StringRef |
| 1356 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 1357 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 1358 | return parseSegmentOrSectionName(Raw.data()); |
| 1359 | } |
| 1360 | |
| 1361 | ArrayRef<char> |
| 1362 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
| 1363 | const SectionBase *Base = |
| 1364 | reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); |
| 1365 | return ArrayRef<char>(Base->Name); |
| 1366 | } |
| 1367 | |
| 1368 | ArrayRef<char> |
| 1369 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
| 1370 | const SectionBase *Base = |
| 1371 | reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); |
| 1372 | return ArrayRef<char>(Base->SegmentName); |
| 1373 | } |
| 1374 | |
| 1375 | bool |
| 1376 | MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE) |
| 1377 | const { |
| 1378 | if (getCPUType(this) == llvm::MachO::CPUTypeX86_64) |
| 1379 | return false; |
| 1380 | return getPlainRelocationAddress(RE) & macho::RF_Scattered; |
| 1381 | } |
| 1382 | |
| 1383 | unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const { |
| 1384 | if (isLittleEndian()) |
| 1385 | return RE.Word1 & 0xffffff; |
| 1386 | return RE.Word1 >> 8; |
| 1387 | } |
| 1388 | |
| 1389 | bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const { |
| 1390 | if (isLittleEndian()) |
| 1391 | return (RE.Word1 >> 27) & 1; |
| 1392 | return (RE.Word1 >> 4) & 1; |
| 1393 | } |
| 1394 | |
| 1395 | bool |
| 1396 | MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const { |
| 1397 | return RE.Word0 >> 31; |
| 1398 | } |
| 1399 | |
| 1400 | uint32_t |
| 1401 | MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const { |
| 1402 | return RE.Word1; |
| 1403 | } |
| 1404 | |
| 1405 | unsigned |
| 1406 | MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const { |
| 1407 | if (isRelocationScattered(RE)) |
| 1408 | return getScatteredRelocationAddress(RE); |
| 1409 | return getPlainRelocationAddress(RE); |
| 1410 | } |
| 1411 | |
| 1412 | unsigned |
| 1413 | MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const { |
| 1414 | if (isRelocationScattered(RE)) |
| 1415 | return getScatteredRelocationPCRel(this, RE); |
| 1416 | return getPlainRelocationPCRel(this, RE); |
| 1417 | } |
| 1418 | |
| 1419 | unsigned |
| 1420 | MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const { |
| 1421 | if (isRelocationScattered(RE)) |
| 1422 | return getScatteredRelocationLength(RE); |
| 1423 | return getPlainRelocationLength(this, RE); |
| 1424 | } |
| 1425 | |
| 1426 | unsigned |
| 1427 | MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const { |
| 1428 | if (isRelocationScattered(RE)) |
| 1429 | return getScatteredRelocationType(RE); |
| 1430 | return getPlainRelocationType(this, RE); |
| 1431 | } |
| 1432 | |
Rafael Espindola | e87dadc | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 1433 | SectionRef |
| 1434 | MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const { |
| 1435 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
| 1436 | return *end_sections(); |
| 1437 | unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1; |
| 1438 | DataRefImpl DRI; |
| 1439 | DRI.d.a = SecNum; |
| 1440 | return SectionRef(DRI, this); |
| 1441 | } |
| 1442 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1443 | MachOObjectFile::LoadCommandInfo |
| 1444 | MachOObjectFile::getFirstLoadCommandInfo() const { |
| 1445 | MachOObjectFile::LoadCommandInfo Load; |
| 1446 | |
| 1447 | unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; |
| 1448 | Load.Ptr = getPtr(this, HeaderSize); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1449 | Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1450 | return Load; |
| 1451 | } |
| 1452 | |
| 1453 | MachOObjectFile::LoadCommandInfo |
| 1454 | MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { |
| 1455 | MachOObjectFile::LoadCommandInfo Next; |
| 1456 | Next.Ptr = L.Ptr + L.C.Size; |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1457 | Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1458 | return Next; |
| 1459 | } |
| 1460 | |
| 1461 | macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1462 | return getStruct<macho::Section>(this, Sections[DRI.d.a]); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1466 | return getStruct<macho::Section64>(this, Sections[DRI.d.a]); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1469 | macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L, |
| 1470 | unsigned Index) const { |
| 1471 | const char *Sec = getSectionPtr(this, L, Index); |
| 1472 | return getStruct<macho::Section>(this, Sec); |
| 1473 | } |
| 1474 | |
| 1475 | macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 1476 | unsigned Index) const { |
| 1477 | const char *Sec = getSectionPtr(this, L, Index); |
| 1478 | return getStruct<macho::Section64>(this, Sec); |
| 1479 | } |
| 1480 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1481 | macho::SymbolTableEntry |
| 1482 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1483 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1484 | return getStruct<macho::SymbolTableEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | macho::Symbol64TableEntry |
| 1488 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1489 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1490 | return getStruct<macho::Symbol64TableEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | macho::LinkeditDataLoadCommand |
| 1494 | MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1495 | return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1498 | macho::SegmentLoadCommand |
| 1499 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
| 1500 | return getStruct<macho::SegmentLoadCommand>(this, L.Ptr); |
| 1501 | } |
| 1502 | |
| 1503 | macho::Segment64LoadCommand |
| 1504 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
| 1505 | return getStruct<macho::Segment64LoadCommand>(this, L.Ptr); |
| 1506 | } |
| 1507 | |
| 1508 | macho::LinkerOptionsLoadCommand |
| 1509 | MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { |
| 1510 | return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr); |
| 1511 | } |
| 1512 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1513 | macho::RelocationEntry |
| 1514 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1515 | const char *P = reinterpret_cast<const char *>(Rel.p); |
| 1516 | return getStruct<macho::RelocationEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Kevin Enderby | 54154f3 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1519 | macho::DataInCodeTableEntry |
| 1520 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 1521 | const char *P = reinterpret_cast<const char *>(Rel.p); |
| 1522 | return getStruct<macho::DataInCodeTableEntry>(this, P); |
| 1523 | } |
| 1524 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1525 | macho::Header MachOObjectFile::getHeader() const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1526 | return getStruct<macho::Header>(this, getPtr(this, 0)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1529 | macho::Header64Ext MachOObjectFile::getHeader64Ext() const { |
| 1530 | return |
| 1531 | getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header))); |
| 1532 | } |
| 1533 | |
| 1534 | macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry( |
| 1535 | const macho::DysymtabLoadCommand &DLC, |
| 1536 | unsigned Index) const { |
| 1537 | uint64_t Offset = DLC.IndirectSymbolTableOffset + |
| 1538 | Index * sizeof(macho::IndirectSymbolTableEntry); |
| 1539 | return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset)); |
| 1540 | } |
| 1541 | |
| 1542 | macho::DataInCodeTableEntry |
| 1543 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 1544 | unsigned Index) const { |
| 1545 | uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry); |
| 1546 | return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset)); |
| 1547 | } |
| 1548 | |
| 1549 | macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1550 | return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1553 | macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const { |
| 1554 | return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd); |
| 1555 | } |
| 1556 | |
Kevin Enderby | 54154f3 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1557 | macho::LinkeditDataLoadCommand |
| 1558 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 1559 | if (DataInCodeLoadCmd) |
| 1560 | return getStruct<macho::LinkeditDataLoadCommand>(this, DataInCodeLoadCmd); |
| 1561 | |
| 1562 | // If there is no DataInCodeLoadCmd return a load command with zero'ed fields. |
| 1563 | macho::LinkeditDataLoadCommand Cmd; |
| 1564 | Cmd.Type = macho::LCT_DataInCode; |
| 1565 | Cmd.Size = macho::LinkeditLoadCommandSize; |
| 1566 | Cmd.DataOffset = 0; |
| 1567 | Cmd.DataSize = 0; |
| 1568 | return Cmd; |
| 1569 | } |
| 1570 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1571 | StringRef MachOObjectFile::getStringTableData() const { |
| 1572 | macho::SymtabLoadCommand S = getSymtabLoadCommand(); |
| 1573 | return getData().substr(S.StringTableOffset, S.StringTableSize); |
| 1574 | } |
| 1575 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1576 | bool MachOObjectFile::is64Bit() const { |
| 1577 | return getType() == getMachOType(false, true) || |
| 1578 | getType() == getMachOType(true, true); |
| 1579 | } |
| 1580 | |
| 1581 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 1582 | SmallVectorImpl<uint64_t> &Out) const { |
| 1583 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 1584 | |
| 1585 | uint32_t offset = Index; |
| 1586 | uint64_t data = 0; |
| 1587 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 1588 | data += delta; |
| 1589 | Out.push_back(data); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
| 1594 | StringRef Magic = Buffer->getBuffer().slice(0, 4); |
| 1595 | error_code ec; |
| 1596 | ObjectFile *Ret; |
| 1597 | if (Magic == "\xFE\xED\xFA\xCE") |
| 1598 | Ret = new MachOObjectFile(Buffer, false, false, ec); |
| 1599 | else if (Magic == "\xCE\xFA\xED\xFE") |
| 1600 | Ret = new MachOObjectFile(Buffer, true, false, ec); |
| 1601 | else if (Magic == "\xFE\xED\xFA\xCF") |
| 1602 | Ret = new MachOObjectFile(Buffer, false, true, ec); |
| 1603 | else if (Magic == "\xCF\xFA\xED\xFE") |
| 1604 | Ret = new MachOObjectFile(Buffer, true, true, ec); |
| 1605 | else |
| 1606 | return NULL; |
| 1607 | |
| 1608 | if (ec) |
| 1609 | return NULL; |
| 1610 | return Ret; |
| 1611 | } |
| 1612 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 1613 | } // end namespace object |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1614 | } // end namespace llvm |