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); |
| 342 | uint64_t Val = O->getAnyRelocationAddress(RE); |
| 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(); |
| 350 | advanceTo(SI, Val); |
| 351 | SI->getName(S); |
| 352 | } |
| 353 | |
| 354 | fmt << S; |
| 355 | } |
| 356 | |
| 357 | static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) { |
| 358 | return RE.Word0; |
| 359 | } |
| 360 | |
| 361 | static unsigned |
| 362 | getScatteredRelocationAddress(const macho::RelocationEntry &RE) { |
| 363 | return RE.Word0 & 0xffffff; |
| 364 | } |
| 365 | |
| 366 | static bool getPlainRelocationPCRel(const MachOObjectFile *O, |
| 367 | const macho::RelocationEntry &RE) { |
| 368 | if (O->isLittleEndian()) |
| 369 | return (RE.Word1 >> 24) & 1; |
| 370 | return (RE.Word1 >> 7) & 1; |
| 371 | } |
| 372 | |
| 373 | static bool |
| 374 | getScatteredRelocationPCRel(const MachOObjectFile *O, |
| 375 | const macho::RelocationEntry &RE) { |
| 376 | return (RE.Word0 >> 30) & 1; |
| 377 | } |
| 378 | |
| 379 | static unsigned getPlainRelocationLength(const MachOObjectFile *O, |
| 380 | const macho::RelocationEntry &RE) { |
| 381 | if (O->isLittleEndian()) |
| 382 | return (RE.Word1 >> 25) & 3; |
| 383 | return (RE.Word1 >> 5) & 3; |
| 384 | } |
| 385 | |
| 386 | static unsigned |
| 387 | getScatteredRelocationLength(const macho::RelocationEntry &RE) { |
| 388 | return (RE.Word0 >> 28) & 3; |
| 389 | } |
| 390 | |
| 391 | static unsigned getPlainRelocationType(const MachOObjectFile *O, |
| 392 | const macho::RelocationEntry &RE) { |
| 393 | if (O->isLittleEndian()) |
| 394 | return RE.Word1 >> 28; |
| 395 | return RE.Word1 & 0xf; |
| 396 | } |
| 397 | |
| 398 | static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) { |
| 399 | return (RE.Word0 >> 24) & 0xf; |
| 400 | } |
| 401 | |
| 402 | static uint32_t getSectionFlags(const MachOObjectFile *O, |
| 403 | DataRefImpl Sec) { |
| 404 | if (O->is64Bit()) { |
| 405 | macho::Section64 Sect = O->getSection64(Sec); |
| 406 | return Sect.Flags; |
| 407 | } |
| 408 | macho::Section Sect = O->getSection(Sec); |
| 409 | return Sect.Flags; |
| 410 | } |
| 411 | |
| 412 | MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, |
| 413 | bool IsLittleEndian, bool Is64bits, |
| 414 | error_code &ec) |
| 415 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object), |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 416 | SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 417 | uint32_t LoadCommandCount = this->getHeader().NumLoadCommands; |
| 418 | macho::LoadCommandType SegmentLoadType = is64Bit() ? |
| 419 | macho::LCT_Segment64 : macho::LCT_Segment; |
| 420 | |
| 421 | MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo(); |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 422 | for (unsigned I = 0; ; ++I) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 423 | if (Load.C.Type == macho::LCT_Symtab) { |
| 424 | assert(!SymtabLoadCmd && "Multiple symbol tables"); |
| 425 | SymtabLoadCmd = Load.Ptr; |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 426 | } else if (Load.C.Type == macho::LCT_Dysymtab) { |
| 427 | assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables"); |
| 428 | DysymtabLoadCmd = Load.Ptr; |
| 429 | } else if (Load.C.Type == SegmentLoadType) { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 430 | uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load); |
| 431 | for (unsigned J = 0; J < NumSections; ++J) { |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 432 | const char *Sec = getSectionPtr(this, Load, J); |
| 433 | Sections.push_back(Sec); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Rafael Espindola | db5f927 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 436 | |
| 437 | if (I == LoadCommandCount - 1) |
| 438 | break; |
| 439 | else |
| 440 | Load = getNextLoadCommandInfo(Load); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb, |
| 445 | SymbolRef &Res) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 446 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 447 | sizeof(macho::Symbol64TableEntry) : |
| 448 | sizeof(macho::SymbolTableEntry); |
| 449 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 450 | Res = SymbolRef(Symb, this); |
| 451 | return object_error::success; |
| 452 | } |
| 453 | |
| 454 | error_code MachOObjectFile::getSymbolName(DataRefImpl Symb, |
| 455 | StringRef &Res) const { |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 456 | StringRef StringTable = getStringTableData(); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 457 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 458 | const char *Start = &StringTable.data()[Entry.StringIndex]; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 459 | Res = StringRef(Start); |
| 460 | return object_error::success; |
| 461 | } |
| 462 | |
| 463 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 464 | uint64_t &Res) const { |
| 465 | if (is64Bit()) { |
| 466 | macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb); |
| 467 | Res = Entry.Value; |
| 468 | } else { |
| 469 | macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb); |
| 470 | Res = Entry.Value; |
| 471 | } |
| 472 | return object_error::success; |
| 473 | } |
| 474 | |
| 475 | error_code |
| 476 | MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb, |
| 477 | uint64_t &Res) const { |
| 478 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 479 | getSymbolAddress(Symb, Res); |
| 480 | if (Entry.SectionIndex) { |
| 481 | uint64_t Delta; |
| 482 | DataRefImpl SecRel; |
| 483 | SecRel.d.a = Entry.SectionIndex-1; |
| 484 | if (is64Bit()) { |
| 485 | macho::Section64 Sec = getSection64(SecRel); |
| 486 | Delta = Sec.Offset - Sec.Address; |
| 487 | } else { |
| 488 | macho::Section Sec = getSection(SecRel); |
| 489 | Delta = Sec.Offset - Sec.Address; |
| 490 | } |
| 491 | |
| 492 | Res += Delta; |
| 493 | } |
| 494 | |
| 495 | return object_error::success; |
| 496 | } |
| 497 | |
| 498 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 499 | uint64_t &Result) const { |
| 500 | uint64_t BeginOffset; |
| 501 | uint64_t EndOffset = 0; |
| 502 | uint8_t SectionIndex; |
| 503 | |
| 504 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); |
| 505 | uint64_t Value; |
| 506 | getSymbolAddress(DRI, Value); |
| 507 | |
| 508 | BeginOffset = Value; |
| 509 | |
| 510 | SectionIndex = Entry.SectionIndex; |
| 511 | if (!SectionIndex) { |
| 512 | uint32_t flags = SymbolRef::SF_None; |
| 513 | this->getSymbolFlags(DRI, flags); |
| 514 | if (flags & SymbolRef::SF_Common) |
| 515 | Result = Value; |
| 516 | else |
| 517 | Result = UnknownAddressOrSize; |
| 518 | return object_error::success; |
| 519 | } |
| 520 | // Unfortunately symbols are unsorted so we need to touch all |
| 521 | // symbols from load command |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 522 | error_code ec; |
| 523 | for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; |
| 524 | I.increment(ec)) { |
| 525 | DataRefImpl DRI = I->getRawDataRefImpl(); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 526 | Entry = getSymbolTableEntryBase(this, DRI); |
| 527 | getSymbolAddress(DRI, Value); |
| 528 | if (Entry.SectionIndex == SectionIndex && Value > BeginOffset) |
| 529 | if (!EndOffset || Value < EndOffset) |
| 530 | EndOffset = Value; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 531 | } |
| 532 | if (!EndOffset) { |
| 533 | uint64_t Size; |
| 534 | DataRefImpl Sec; |
| 535 | Sec.d.a = SectionIndex-1; |
| 536 | getSectionSize(Sec, Size); |
| 537 | getSectionAddress(Sec, EndOffset); |
| 538 | EndOffset += Size; |
| 539 | } |
| 540 | Result = EndOffset - BeginOffset; |
| 541 | return object_error::success; |
| 542 | } |
| 543 | |
| 544 | error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, |
| 545 | SymbolRef::Type &Res) const { |
| 546 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 547 | uint8_t n_type = Entry.Type; |
| 548 | |
| 549 | Res = SymbolRef::ST_Other; |
| 550 | |
| 551 | // If this is a STAB debugging symbol, we can do nothing more. |
| 552 | if (n_type & MachO::NlistMaskStab) { |
| 553 | Res = SymbolRef::ST_Debug; |
| 554 | return object_error::success; |
| 555 | } |
| 556 | |
| 557 | switch (n_type & MachO::NlistMaskType) { |
| 558 | case MachO::NListTypeUndefined : |
| 559 | Res = SymbolRef::ST_Unknown; |
| 560 | break; |
| 561 | case MachO::NListTypeSection : |
| 562 | Res = SymbolRef::ST_Function; |
| 563 | break; |
| 564 | } |
| 565 | return object_error::success; |
| 566 | } |
| 567 | |
| 568 | error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, |
| 569 | char &Res) const { |
| 570 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 571 | uint8_t Type = Entry.Type; |
| 572 | uint16_t Flags = Entry.Flags; |
| 573 | |
| 574 | char Char; |
| 575 | switch (Type & macho::STF_TypeMask) { |
| 576 | case macho::STT_Undefined: |
| 577 | Char = 'u'; |
| 578 | break; |
| 579 | case macho::STT_Absolute: |
| 580 | case macho::STT_Section: |
| 581 | Char = 's'; |
| 582 | break; |
| 583 | default: |
| 584 | Char = '?'; |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | if (Flags & (macho::STF_External | macho::STF_PrivateExtern)) |
| 589 | Char = toupper(static_cast<unsigned char>(Char)); |
| 590 | Res = Char; |
| 591 | return object_error::success; |
| 592 | } |
| 593 | |
| 594 | error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI, |
| 595 | uint32_t &Result) const { |
| 596 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI); |
| 597 | |
| 598 | uint8_t MachOType = Entry.Type; |
| 599 | uint16_t MachOFlags = Entry.Flags; |
| 600 | |
| 601 | // TODO: Correctly set SF_ThreadLocal |
| 602 | Result = SymbolRef::SF_None; |
| 603 | |
| 604 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) |
| 605 | Result |= SymbolRef::SF_Undefined; |
| 606 | |
| 607 | if (MachOFlags & macho::STF_StabsEntryMask) |
| 608 | Result |= SymbolRef::SF_FormatSpecific; |
| 609 | |
| 610 | if (MachOType & MachO::NlistMaskExternal) { |
| 611 | Result |= SymbolRef::SF_Global; |
| 612 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) |
| 613 | Result |= SymbolRef::SF_Common; |
| 614 | } |
| 615 | |
| 616 | if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef)) |
| 617 | Result |= SymbolRef::SF_Weak; |
| 618 | |
| 619 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute) |
| 620 | Result |= SymbolRef::SF_Absolute; |
| 621 | |
| 622 | return object_error::success; |
| 623 | } |
| 624 | |
| 625 | error_code |
| 626 | MachOObjectFile::getSymbolSection(DataRefImpl Symb, |
| 627 | section_iterator &Res) const { |
| 628 | SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb); |
| 629 | uint8_t index = Entry.SectionIndex; |
| 630 | |
| 631 | if (index == 0) { |
| 632 | Res = end_sections(); |
| 633 | } else { |
| 634 | DataRefImpl DRI; |
| 635 | DRI.d.a = index - 1; |
| 636 | Res = section_iterator(SectionRef(DRI, this)); |
| 637 | } |
| 638 | |
| 639 | return object_error::success; |
| 640 | } |
| 641 | |
| 642 | error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 643 | uint64_t &Val) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 644 | report_fatal_error("getSymbolValue unimplemented in MachOObjectFile"); |
| 645 | } |
| 646 | |
| 647 | error_code MachOObjectFile::getSectionNext(DataRefImpl Sec, |
| 648 | SectionRef &Res) const { |
| 649 | Sec.d.a++; |
| 650 | Res = SectionRef(Sec, this); |
| 651 | return object_error::success; |
| 652 | } |
| 653 | |
| 654 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 655 | MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 656 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 657 | Result = parseSegmentOrSectionName(Raw.data()); |
| 658 | return object_error::success; |
| 659 | } |
| 660 | |
| 661 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 662 | MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 663 | if (is64Bit()) { |
| 664 | macho::Section64 Sect = getSection64(Sec); |
| 665 | Res = Sect.Address; |
| 666 | } else { |
| 667 | macho::Section Sect = getSection(Sec); |
| 668 | Res = Sect.Address; |
| 669 | } |
| 670 | return object_error::success; |
| 671 | } |
| 672 | |
| 673 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 674 | MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 675 | if (is64Bit()) { |
| 676 | macho::Section64 Sect = getSection64(Sec); |
| 677 | Res = Sect.Size; |
| 678 | } else { |
| 679 | macho::Section Sect = getSection(Sec); |
| 680 | Res = Sect.Size; |
| 681 | } |
| 682 | |
| 683 | return object_error::success; |
| 684 | } |
| 685 | |
| 686 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 687 | MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 688 | uint32_t Offset; |
| 689 | uint64_t Size; |
| 690 | |
| 691 | if (is64Bit()) { |
| 692 | macho::Section64 Sect = getSection64(Sec); |
| 693 | Offset = Sect.Offset; |
| 694 | Size = Sect.Size; |
| 695 | } else { |
| 696 | macho::Section Sect =getSection(Sec); |
| 697 | Offset = Sect.Offset; |
| 698 | Size = Sect.Size; |
| 699 | } |
| 700 | |
| 701 | Res = this->getData().substr(Offset, Size); |
| 702 | return object_error::success; |
| 703 | } |
| 704 | |
| 705 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 706 | MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 707 | uint32_t Align; |
| 708 | if (is64Bit()) { |
| 709 | macho::Section64 Sect = getSection64(Sec); |
| 710 | Align = Sect.Align; |
| 711 | } else { |
| 712 | macho::Section Sect = getSection(Sec); |
| 713 | Align = Sect.Align; |
| 714 | } |
| 715 | |
| 716 | Res = uint64_t(1) << Align; |
| 717 | return object_error::success; |
| 718 | } |
| 719 | |
| 720 | error_code |
| 721 | MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { |
| 722 | uint32_t Flags = getSectionFlags(this, Sec); |
| 723 | Res = Flags & macho::SF_PureInstructions; |
| 724 | return object_error::success; |
| 725 | } |
| 726 | |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 727 | error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const { |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 728 | // FIXME: Unimplemented. |
| 729 | Result = false; |
| 730 | return object_error::success; |
| 731 | } |
| 732 | |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 733 | error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const { |
Andrew Kaylor | 30b20eb | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 734 | // FIXME: Unimplemented. |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 735 | Result = false; |
| 736 | return object_error::success; |
| 737 | } |
| 738 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 739 | error_code |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 740 | MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 741 | bool &Result) const { |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 742 | // FIXME: Unimplemented. |
| 743 | Result = true; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 744 | return object_error::success; |
| 745 | } |
| 746 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 747 | error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 748 | bool &Result) const { |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 749 | // FIXME: Unimplemented. |
| 750 | Result = false; |
| 751 | return object_error::success; |
| 752 | } |
| 753 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 754 | error_code |
| 755 | MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { |
| 756 | uint32_t Flags = getSectionFlags(this, Sec); |
| 757 | unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType; |
| 758 | Res = SectionType == MachO::SectionTypeZeroFill || |
| 759 | SectionType == MachO::SectionTypeZeroFillLarge; |
| 760 | return object_error::success; |
| 761 | } |
| 762 | |
| 763 | error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 764 | bool &Result) const { |
Andrew Kaylor | 3a129c8 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 765 | // Consider using the code from isSectionText to look for __const sections. |
| 766 | // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS |
| 767 | // to use section attributes to distinguish code from data. |
| 768 | |
| 769 | // FIXME: Unimplemented. |
| 770 | Result = false; |
| 771 | return object_error::success; |
| 772 | } |
| 773 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 774 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 775 | MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 776 | bool &Result) const { |
| 777 | SymbolRef::Type ST; |
| 778 | this->getSymbolType(Symb, ST); |
| 779 | if (ST == SymbolRef::ST_Unknown) { |
| 780 | Result = false; |
| 781 | return object_error::success; |
| 782 | } |
| 783 | |
| 784 | uint64_t SectBegin, SectEnd; |
| 785 | getSectionAddress(Sec, SectBegin); |
| 786 | getSectionSize(Sec, SectEnd); |
| 787 | SectEnd += SectBegin; |
| 788 | |
| 789 | uint64_t SymAddr; |
| 790 | getSymbolAddress(Symb, SymAddr); |
| 791 | Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); |
| 792 | |
| 793 | return object_error::success; |
| 794 | } |
| 795 | |
| 796 | relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 797 | uint32_t Offset; |
| 798 | if (is64Bit()) { |
| 799 | macho::Section64 Sect = getSection64(Sec); |
| 800 | Offset = Sect.RelocationTableOffset; |
| 801 | } else { |
| 802 | macho::Section Sect = getSection(Sec); |
| 803 | Offset = Sect.RelocationTableOffset; |
| 804 | } |
| 805 | |
| 806 | DataRefImpl Ret; |
| 807 | Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 808 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 809 | } |
Rafael Espindola | 335f1d4 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 810 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 811 | relocation_iterator |
| 812 | MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 813 | uint32_t Offset; |
| 814 | uint32_t Num; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 815 | if (is64Bit()) { |
| 816 | macho::Section64 Sect = getSection64(Sec); |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 817 | Offset = Sect.RelocationTableOffset; |
| 818 | Num = Sect.NumRelocationTableEntries; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 819 | } else { |
| 820 | macho::Section Sect = getSection(Sec); |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 821 | Offset = Sect.RelocationTableOffset; |
| 822 | Num = Sect.NumRelocationTableEntries; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 823 | } |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 824 | |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 825 | const macho::RelocationEntry *P = |
| 826 | reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset)); |
| 827 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 828 | DataRefImpl Ret; |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 829 | Ret.p = reinterpret_cast<uintptr_t>(P + Num); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 830 | return relocation_iterator(RelocationRef(Ret, this)); |
| 831 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 832 | |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 833 | error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel, |
| 834 | RelocationRef &Res) const { |
| 835 | const macho::RelocationEntry *P = |
| 836 | reinterpret_cast<const macho::RelocationEntry *>(Rel.p); |
| 837 | Rel.p = reinterpret_cast<uintptr_t>(P + 1); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 838 | Res = RelocationRef(Rel, this); |
| 839 | return object_error::success; |
| 840 | } |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 841 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 842 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 843 | MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { |
Rafael Espindola | 956ca72 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 844 | report_fatal_error("getRelocationAddress not implemented in MachOObjectFile"); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 847 | error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 848 | uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 849 | macho::RelocationEntry RE = getRelocation(Rel); |
| 850 | Res = getAnyRelocationAddress(RE); |
| 851 | return object_error::success; |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 854 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 855 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 856 | macho::RelocationEntry RE = getRelocation(Rel); |
| 857 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 858 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 859 | if (!isExtern) { |
| 860 | Res = *end_symbols(); |
| 861 | return object_error::success; |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 862 | } |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 863 | |
| 864 | macho::SymtabLoadCommand S = getSymtabLoadCommand(); |
| 865 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 866 | sizeof(macho::Symbol64TableEntry) : |
| 867 | sizeof(macho::SymbolTableEntry); |
| 868 | uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize; |
| 869 | DataRefImpl Sym; |
| 870 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 871 | Res = SymbolRef(Sym, this); |
| 872 | return object_error::success; |
| 873 | } |
| 874 | |
| 875 | error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 876 | uint64_t &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 877 | macho::RelocationEntry RE = getRelocation(Rel); |
| 878 | Res = getAnyRelocationType(RE); |
| 879 | return object_error::success; |
| 880 | } |
| 881 | |
| 882 | error_code |
| 883 | MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 884 | SmallVectorImpl<char> &Result) const { |
| 885 | StringRef res; |
| 886 | uint64_t RType; |
| 887 | getRelocationType(Rel, RType); |
| 888 | |
| 889 | unsigned Arch = this->getArch(); |
| 890 | |
| 891 | switch (Arch) { |
| 892 | case Triple::x86: { |
| 893 | static const char *const Table[] = { |
| 894 | "GENERIC_RELOC_VANILLA", |
| 895 | "GENERIC_RELOC_PAIR", |
| 896 | "GENERIC_RELOC_SECTDIFF", |
| 897 | "GENERIC_RELOC_PB_LA_PTR", |
| 898 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 899 | "GENERIC_RELOC_TLV" }; |
| 900 | |
| 901 | if (RType > 6) |
| 902 | res = "Unknown"; |
| 903 | else |
| 904 | res = Table[RType]; |
| 905 | break; |
| 906 | } |
| 907 | case Triple::x86_64: { |
| 908 | static const char *const Table[] = { |
| 909 | "X86_64_RELOC_UNSIGNED", |
| 910 | "X86_64_RELOC_SIGNED", |
| 911 | "X86_64_RELOC_BRANCH", |
| 912 | "X86_64_RELOC_GOT_LOAD", |
| 913 | "X86_64_RELOC_GOT", |
| 914 | "X86_64_RELOC_SUBTRACTOR", |
| 915 | "X86_64_RELOC_SIGNED_1", |
| 916 | "X86_64_RELOC_SIGNED_2", |
| 917 | "X86_64_RELOC_SIGNED_4", |
| 918 | "X86_64_RELOC_TLV" }; |
| 919 | |
| 920 | if (RType > 9) |
| 921 | res = "Unknown"; |
| 922 | else |
| 923 | res = Table[RType]; |
| 924 | break; |
| 925 | } |
| 926 | case Triple::arm: { |
| 927 | static const char *const Table[] = { |
| 928 | "ARM_RELOC_VANILLA", |
| 929 | "ARM_RELOC_PAIR", |
| 930 | "ARM_RELOC_SECTDIFF", |
| 931 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 932 | "ARM_RELOC_PB_LA_PTR", |
| 933 | "ARM_RELOC_BR24", |
| 934 | "ARM_THUMB_RELOC_BR22", |
| 935 | "ARM_THUMB_32BIT_BRANCH", |
| 936 | "ARM_RELOC_HALF", |
| 937 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 938 | |
| 939 | if (RType > 9) |
| 940 | res = "Unknown"; |
| 941 | else |
| 942 | res = Table[RType]; |
| 943 | break; |
| 944 | } |
| 945 | case Triple::ppc: { |
| 946 | static const char *const Table[] = { |
| 947 | "PPC_RELOC_VANILLA", |
| 948 | "PPC_RELOC_PAIR", |
| 949 | "PPC_RELOC_BR14", |
| 950 | "PPC_RELOC_BR24", |
| 951 | "PPC_RELOC_HI16", |
| 952 | "PPC_RELOC_LO16", |
| 953 | "PPC_RELOC_HA16", |
| 954 | "PPC_RELOC_LO14", |
| 955 | "PPC_RELOC_SECTDIFF", |
| 956 | "PPC_RELOC_PB_LA_PTR", |
| 957 | "PPC_RELOC_HI16_SECTDIFF", |
| 958 | "PPC_RELOC_LO16_SECTDIFF", |
| 959 | "PPC_RELOC_HA16_SECTDIFF", |
| 960 | "PPC_RELOC_JBSR", |
| 961 | "PPC_RELOC_LO14_SECTDIFF", |
| 962 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 963 | |
| 964 | res = Table[RType]; |
| 965 | break; |
| 966 | } |
| 967 | case Triple::UnknownArch: |
| 968 | res = "Unknown"; |
| 969 | break; |
| 970 | } |
| 971 | Result.append(res.begin(), res.end()); |
| 972 | return object_error::success; |
| 973 | } |
| 974 | |
| 975 | error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 976 | int64_t &Res) const { |
Rafael Espindola | 8bf8006 | 2013-04-11 02:21:31 +0000 | [diff] [blame] | 977 | Res = 0; |
| 978 | return object_error::success; |
| 979 | } |
| 980 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 981 | error_code |
| 982 | MachOObjectFile::getRelocationValueString(DataRefImpl Rel, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 983 | SmallVectorImpl<char> &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 984 | macho::RelocationEntry RE = getRelocation(Rel); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 985 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 986 | unsigned Arch = this->getArch(); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 987 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 988 | std::string fmtbuf; |
| 989 | raw_string_ostream fmt(fmtbuf); |
| 990 | unsigned Type = this->getAnyRelocationType(RE); |
| 991 | bool IsPCRel = this->getAnyRelocationPCRel(RE); |
| 992 | |
| 993 | // Determine any addends that should be displayed with the relocation. |
| 994 | // These require decoding the relocation type, which is triple-specific. |
| 995 | |
| 996 | // X86_64 has entirely custom relocation types. |
| 997 | if (Arch == Triple::x86_64) { |
| 998 | bool isPCRel = getAnyRelocationPCRel(RE); |
| 999 | |
| 1000 | switch (Type) { |
| 1001 | case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD |
| 1002 | case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT |
| 1003 | printRelocationTargetName(this, RE, fmt); |
| 1004 | fmt << "@GOT"; |
| 1005 | if (isPCRel) fmt << "PCREL"; |
| 1006 | break; |
| 1007 | } |
| 1008 | case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR |
| 1009 | DataRefImpl RelNext = Rel; |
| 1010 | RelNext.d.a++; |
| 1011 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1012 | |
| 1013 | // X86_64_SUBTRACTOR must be followed by a relocation of type |
| 1014 | // X86_64_RELOC_UNSIGNED. |
| 1015 | // NOTE: Scattered relocations don't exist on x86_64. |
| 1016 | unsigned RType = getAnyRelocationType(RENext); |
| 1017 | if (RType != 0) |
| 1018 | report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " |
| 1019 | "X86_64_RELOC_SUBTRACTOR."); |
| 1020 | |
| 1021 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol, |
| 1022 | // X86_64_SUBTRACTOR contains to the subtrahend. |
| 1023 | printRelocationTargetName(this, RENext, fmt); |
| 1024 | fmt << "-"; |
| 1025 | printRelocationTargetName(this, RE, fmt); |
| 1026 | break; |
| 1027 | } |
| 1028 | case macho::RIT_X86_64_TLV: |
| 1029 | printRelocationTargetName(this, RE, fmt); |
| 1030 | fmt << "@TLV"; |
| 1031 | if (isPCRel) fmt << "P"; |
| 1032 | break; |
| 1033 | case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1 |
| 1034 | printRelocationTargetName(this, RE, fmt); |
| 1035 | fmt << "-1"; |
| 1036 | break; |
| 1037 | case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2 |
| 1038 | printRelocationTargetName(this, RE, fmt); |
| 1039 | fmt << "-2"; |
| 1040 | break; |
| 1041 | case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4 |
| 1042 | printRelocationTargetName(this, RE, fmt); |
| 1043 | fmt << "-4"; |
| 1044 | break; |
| 1045 | default: |
| 1046 | printRelocationTargetName(this, RE, fmt); |
| 1047 | break; |
| 1048 | } |
| 1049 | // X86 and ARM share some relocation types in common. |
| 1050 | } else if (Arch == Triple::x86 || Arch == Triple::arm) { |
| 1051 | // Generic relocation types... |
| 1052 | switch (Type) { |
| 1053 | case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info |
| 1054 | return object_error::success; |
| 1055 | case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF |
| 1056 | DataRefImpl RelNext = Rel; |
| 1057 | RelNext.d.a++; |
| 1058 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1059 | |
| 1060 | // X86 sect diff's must be followed by a relocation of type |
| 1061 | // GENERIC_RELOC_PAIR. |
| 1062 | unsigned RType = getAnyRelocationType(RENext); |
| 1063 | |
| 1064 | if (RType != 1) |
| 1065 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1066 | "GENERIC_RELOC_SECTDIFF."); |
| 1067 | |
| 1068 | printRelocationTargetName(this, RE, fmt); |
| 1069 | fmt << "-"; |
| 1070 | printRelocationTargetName(this, RENext, fmt); |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | if (Arch == Triple::x86) { |
| 1076 | // All X86 relocations that need special printing were already |
| 1077 | // handled in the generic code. |
| 1078 | switch (Type) { |
| 1079 | case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF |
| 1080 | DataRefImpl RelNext = Rel; |
| 1081 | RelNext.d.a++; |
| 1082 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1083 | |
| 1084 | // X86 sect diff's must be followed by a relocation of type |
| 1085 | // GENERIC_RELOC_PAIR. |
| 1086 | unsigned RType = getAnyRelocationType(RENext); |
| 1087 | if (RType != 1) |
| 1088 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1089 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
| 1090 | |
| 1091 | printRelocationTargetName(this, RE, fmt); |
| 1092 | fmt << "-"; |
| 1093 | printRelocationTargetName(this, RENext, fmt); |
| 1094 | break; |
| 1095 | } |
| 1096 | case macho::RIT_Generic_TLV: { |
| 1097 | printRelocationTargetName(this, RE, fmt); |
| 1098 | fmt << "@TLV"; |
| 1099 | if (IsPCRel) fmt << "P"; |
| 1100 | break; |
| 1101 | } |
| 1102 | default: |
| 1103 | printRelocationTargetName(this, RE, fmt); |
| 1104 | } |
| 1105 | } else { // ARM-specific relocations |
| 1106 | switch (Type) { |
| 1107 | case macho::RIT_ARM_Half: // ARM_RELOC_HALF |
| 1108 | case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF |
| 1109 | // Half relocations steal a bit from the length field to encode |
| 1110 | // whether this is an upper16 or a lower16 relocation. |
| 1111 | bool isUpper = getAnyRelocationLength(RE) >> 1; |
| 1112 | |
| 1113 | if (isUpper) |
| 1114 | fmt << ":upper16:("; |
| 1115 | else |
| 1116 | fmt << ":lower16:("; |
| 1117 | printRelocationTargetName(this, RE, fmt); |
| 1118 | |
| 1119 | DataRefImpl RelNext = Rel; |
| 1120 | RelNext.d.a++; |
| 1121 | macho::RelocationEntry RENext = getRelocation(RelNext); |
| 1122 | |
| 1123 | // ARM half relocs must be followed by a relocation of type |
| 1124 | // ARM_RELOC_PAIR. |
| 1125 | unsigned RType = getAnyRelocationType(RENext); |
| 1126 | if (RType != 1) |
| 1127 | report_fatal_error("Expected ARM_RELOC_PAIR after " |
| 1128 | "GENERIC_RELOC_HALF"); |
| 1129 | |
| 1130 | // NOTE: The half of the target virtual address is stashed in the |
| 1131 | // address field of the secondary relocation, but we can't reverse |
| 1132 | // engineer the constant offset from it without decoding the movw/movt |
| 1133 | // instruction to find the other half in its immediate field. |
| 1134 | |
| 1135 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 1136 | // symbol/section pointer of the follow-on relocation. |
| 1137 | if (Type == macho::RIT_ARM_HalfDifference) { |
| 1138 | fmt << "-"; |
| 1139 | printRelocationTargetName(this, RENext, fmt); |
| 1140 | } |
| 1141 | |
| 1142 | fmt << ")"; |
| 1143 | break; |
| 1144 | } |
| 1145 | default: { |
| 1146 | printRelocationTargetName(this, RE, fmt); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | } else |
| 1151 | printRelocationTargetName(this, RE, fmt); |
| 1152 | |
| 1153 | fmt.flush(); |
| 1154 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 1155 | return object_error::success; |
| 1156 | } |
| 1157 | |
| 1158 | error_code |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1159 | MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1160 | unsigned Arch = getArch(); |
| 1161 | uint64_t Type; |
| 1162 | getRelocationType(Rel, Type); |
| 1163 | |
| 1164 | Result = false; |
| 1165 | |
| 1166 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 1167 | // is always hidden. |
| 1168 | if (Arch == Triple::x86 || Arch == Triple::arm) { |
| 1169 | if (Type == macho::RIT_Pair) Result = true; |
| 1170 | } else if (Arch == Triple::x86_64) { |
| 1171 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
| 1172 | // an X864_64_RELOC_SUBTRACTOR. |
| 1173 | if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) { |
| 1174 | DataRefImpl RelPrev = Rel; |
| 1175 | RelPrev.d.a--; |
| 1176 | uint64_t PrevType; |
| 1177 | getRelocationType(RelPrev, PrevType); |
| 1178 | if (PrevType == macho::RIT_X86_64_Subtractor) |
| 1179 | Result = true; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | return object_error::success; |
| 1184 | } |
| 1185 | |
| 1186 | error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1187 | LibraryRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1188 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1189 | } |
| 1190 | |
| 1191 | error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, |
Rafael Espindola | f69a81f | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1192 | StringRef &Res) const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1193 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1194 | } |
| 1195 | |
| 1196 | symbol_iterator MachOObjectFile::begin_symbols() const { |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1197 | DataRefImpl DRI; |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1198 | if (!SymtabLoadCmd) |
| 1199 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1200 | |
| 1201 | macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); |
| 1202 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1203 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1204 | } |
| 1205 | |
| 1206 | symbol_iterator MachOObjectFile::end_symbols() const { |
| 1207 | DataRefImpl DRI; |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1208 | if (!SymtabLoadCmd) |
| 1209 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1210 | |
| 1211 | macho::SymtabLoadCommand Symtab = getSymtabLoadCommand(); |
| 1212 | unsigned SymbolTableEntrySize = is64Bit() ? |
| 1213 | sizeof(macho::Symbol64TableEntry) : |
| 1214 | sizeof(macho::SymbolTableEntry); |
| 1215 | unsigned Offset = Symtab.SymbolTableOffset + |
| 1216 | Symtab.NumSymbolTableEntries * SymbolTableEntrySize; |
| 1217 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1218 | return symbol_iterator(SymbolRef(DRI, this)); |
| 1219 | } |
| 1220 | |
| 1221 | symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { |
| 1222 | // TODO: implement |
| 1223 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 1224 | } |
| 1225 | |
| 1226 | symbol_iterator MachOObjectFile::end_dynamic_symbols() const { |
| 1227 | // TODO: implement |
| 1228 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 1229 | } |
| 1230 | |
| 1231 | section_iterator MachOObjectFile::begin_sections() const { |
| 1232 | DataRefImpl DRI; |
| 1233 | return section_iterator(SectionRef(DRI, this)); |
| 1234 | } |
| 1235 | |
| 1236 | section_iterator MachOObjectFile::end_sections() const { |
| 1237 | DataRefImpl DRI; |
| 1238 | DRI.d.a = Sections.size(); |
| 1239 | return section_iterator(SectionRef(DRI, this)); |
| 1240 | } |
| 1241 | |
| 1242 | library_iterator MachOObjectFile::begin_libraries_needed() const { |
| 1243 | // TODO: implement |
| 1244 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1245 | } |
| 1246 | |
| 1247 | library_iterator MachOObjectFile::end_libraries_needed() const { |
| 1248 | // TODO: implement |
| 1249 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1250 | } |
| 1251 | |
| 1252 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 1253 | return is64Bit() ? 8 : 4; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1256 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1257 | unsigned CPUType = getCPUType(this); |
| 1258 | if (!is64Bit()) { |
| 1259 | switch (CPUType) { |
| 1260 | case llvm::MachO::CPUTypeI386: |
| 1261 | return "Mach-O 32-bit i386"; |
| 1262 | case llvm::MachO::CPUTypeARM: |
| 1263 | return "Mach-O arm"; |
| 1264 | case llvm::MachO::CPUTypePowerPC: |
| 1265 | return "Mach-O 32-bit ppc"; |
| 1266 | default: |
| 1267 | assert((CPUType & llvm::MachO::CPUArchABI64) == 0 && |
| 1268 | "64-bit object file when we're not 64-bit?"); |
| 1269 | return "Mach-O 32-bit unknown"; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | // Make sure the cpu type has the correct mask. |
| 1274 | assert((CPUType & llvm::MachO::CPUArchABI64) |
| 1275 | == llvm::MachO::CPUArchABI64 && |
| 1276 | "32-bit object file when we're 64-bit?"); |
| 1277 | |
| 1278 | switch (CPUType) { |
| 1279 | case llvm::MachO::CPUTypeX86_64: |
| 1280 | return "Mach-O 64-bit x86-64"; |
| 1281 | case llvm::MachO::CPUTypePowerPC64: |
| 1282 | return "Mach-O 64-bit ppc64"; |
| 1283 | default: |
| 1284 | return "Mach-O 64-bit unknown"; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | unsigned MachOObjectFile::getArch() const { |
| 1289 | switch (getCPUType(this)) { |
| 1290 | case llvm::MachO::CPUTypeI386: |
| 1291 | return Triple::x86; |
| 1292 | case llvm::MachO::CPUTypeX86_64: |
| 1293 | return Triple::x86_64; |
| 1294 | case llvm::MachO::CPUTypeARM: |
| 1295 | return Triple::arm; |
| 1296 | case llvm::MachO::CPUTypePowerPC: |
| 1297 | return Triple::ppc; |
| 1298 | case llvm::MachO::CPUTypePowerPC64: |
| 1299 | return Triple::ppc64; |
| 1300 | default: |
| 1301 | return Triple::UnknownArch; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | StringRef MachOObjectFile::getLoadName() const { |
| 1306 | // TODO: Implement |
| 1307 | report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); |
| 1308 | } |
| 1309 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1310 | relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const { |
| 1311 | DataRefImpl DRI; |
| 1312 | DRI.d.a = Index; |
| 1313 | return getSectionRelBegin(DRI); |
| 1314 | } |
| 1315 | |
| 1316 | relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const { |
| 1317 | DataRefImpl DRI; |
| 1318 | DRI.d.a = Index; |
| 1319 | return getSectionRelEnd(DRI); |
| 1320 | } |
| 1321 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1322 | StringRef |
| 1323 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 1324 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 1325 | return parseSegmentOrSectionName(Raw.data()); |
| 1326 | } |
| 1327 | |
| 1328 | ArrayRef<char> |
| 1329 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
| 1330 | const SectionBase *Base = |
| 1331 | reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); |
| 1332 | return ArrayRef<char>(Base->Name); |
| 1333 | } |
| 1334 | |
| 1335 | ArrayRef<char> |
| 1336 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
| 1337 | const SectionBase *Base = |
| 1338 | reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]); |
| 1339 | return ArrayRef<char>(Base->SegmentName); |
| 1340 | } |
| 1341 | |
| 1342 | bool |
| 1343 | MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE) |
| 1344 | const { |
| 1345 | if (getCPUType(this) == llvm::MachO::CPUTypeX86_64) |
| 1346 | return false; |
| 1347 | return getPlainRelocationAddress(RE) & macho::RF_Scattered; |
| 1348 | } |
| 1349 | |
| 1350 | unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const { |
| 1351 | if (isLittleEndian()) |
| 1352 | return RE.Word1 & 0xffffff; |
| 1353 | return RE.Word1 >> 8; |
| 1354 | } |
| 1355 | |
| 1356 | bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const { |
| 1357 | if (isLittleEndian()) |
| 1358 | return (RE.Word1 >> 27) & 1; |
| 1359 | return (RE.Word1 >> 4) & 1; |
| 1360 | } |
| 1361 | |
| 1362 | bool |
| 1363 | MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const { |
| 1364 | return RE.Word0 >> 31; |
| 1365 | } |
| 1366 | |
| 1367 | uint32_t |
| 1368 | MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const { |
| 1369 | return RE.Word1; |
| 1370 | } |
| 1371 | |
| 1372 | unsigned |
| 1373 | MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const { |
| 1374 | if (isRelocationScattered(RE)) |
| 1375 | return getScatteredRelocationAddress(RE); |
| 1376 | return getPlainRelocationAddress(RE); |
| 1377 | } |
| 1378 | |
| 1379 | unsigned |
| 1380 | MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const { |
| 1381 | if (isRelocationScattered(RE)) |
| 1382 | return getScatteredRelocationPCRel(this, RE); |
| 1383 | return getPlainRelocationPCRel(this, RE); |
| 1384 | } |
| 1385 | |
| 1386 | unsigned |
| 1387 | MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const { |
| 1388 | if (isRelocationScattered(RE)) |
| 1389 | return getScatteredRelocationLength(RE); |
| 1390 | return getPlainRelocationLength(this, RE); |
| 1391 | } |
| 1392 | |
| 1393 | unsigned |
| 1394 | MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const { |
| 1395 | if (isRelocationScattered(RE)) |
| 1396 | return getScatteredRelocationType(RE); |
| 1397 | return getPlainRelocationType(this, RE); |
| 1398 | } |
| 1399 | |
| 1400 | MachOObjectFile::LoadCommandInfo |
| 1401 | MachOObjectFile::getFirstLoadCommandInfo() const { |
| 1402 | MachOObjectFile::LoadCommandInfo Load; |
| 1403 | |
| 1404 | unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size; |
| 1405 | Load.Ptr = getPtr(this, HeaderSize); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1406 | Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1407 | return Load; |
| 1408 | } |
| 1409 | |
| 1410 | MachOObjectFile::LoadCommandInfo |
| 1411 | MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { |
| 1412 | MachOObjectFile::LoadCommandInfo Next; |
| 1413 | Next.Ptr = L.Ptr + L.C.Size; |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1414 | Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1415 | return Next; |
| 1416 | } |
| 1417 | |
| 1418 | macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1419 | return getStruct<macho::Section>(this, Sections[DRI.d.a]); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1423 | return getStruct<macho::Section64>(this, Sections[DRI.d.a]); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1426 | macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L, |
| 1427 | unsigned Index) const { |
| 1428 | const char *Sec = getSectionPtr(this, L, Index); |
| 1429 | return getStruct<macho::Section>(this, Sec); |
| 1430 | } |
| 1431 | |
| 1432 | macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 1433 | unsigned Index) const { |
| 1434 | const char *Sec = getSectionPtr(this, L, Index); |
| 1435 | return getStruct<macho::Section64>(this, Sec); |
| 1436 | } |
| 1437 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1438 | macho::SymbolTableEntry |
| 1439 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1440 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1441 | return getStruct<macho::SymbolTableEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | macho::Symbol64TableEntry |
| 1445 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 802fe93 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1446 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1447 | return getStruct<macho::Symbol64TableEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | macho::LinkeditDataLoadCommand |
| 1451 | MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1452 | return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1455 | macho::SegmentLoadCommand |
| 1456 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
| 1457 | return getStruct<macho::SegmentLoadCommand>(this, L.Ptr); |
| 1458 | } |
| 1459 | |
| 1460 | macho::Segment64LoadCommand |
| 1461 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
| 1462 | return getStruct<macho::Segment64LoadCommand>(this, L.Ptr); |
| 1463 | } |
| 1464 | |
| 1465 | macho::LinkerOptionsLoadCommand |
| 1466 | MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { |
| 1467 | return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr); |
| 1468 | } |
| 1469 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1470 | macho::RelocationEntry |
| 1471 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | e5330f7 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1472 | const char *P = reinterpret_cast<const char *>(Rel.p); |
| 1473 | return getStruct<macho::RelocationEntry>(this, P); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | macho::Header MachOObjectFile::getHeader() const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1477 | return getStruct<macho::Header>(this, getPtr(this, 0)); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1480 | macho::Header64Ext MachOObjectFile::getHeader64Ext() const { |
| 1481 | return |
| 1482 | getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header))); |
| 1483 | } |
| 1484 | |
| 1485 | macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry( |
| 1486 | const macho::DysymtabLoadCommand &DLC, |
| 1487 | unsigned Index) const { |
| 1488 | uint64_t Offset = DLC.IndirectSymbolTableOffset + |
| 1489 | Index * sizeof(macho::IndirectSymbolTableEntry); |
| 1490 | return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset)); |
| 1491 | } |
| 1492 | |
| 1493 | macho::DataInCodeTableEntry |
| 1494 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 1495 | unsigned Index) const { |
| 1496 | uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry); |
| 1497 | return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset)); |
| 1498 | } |
| 1499 | |
| 1500 | macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const { |
Rafael Espindola | 143d223 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 1501 | return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd); |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Rafael Espindola | 2173e18 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1504 | macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const { |
| 1505 | return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd); |
| 1506 | } |
| 1507 | |
| 1508 | StringRef MachOObjectFile::getStringTableData() const { |
| 1509 | macho::SymtabLoadCommand S = getSymtabLoadCommand(); |
| 1510 | return getData().substr(S.StringTableOffset, S.StringTableSize); |
| 1511 | } |
| 1512 | |
Rafael Espindola | fd7aa38 | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1513 | bool MachOObjectFile::is64Bit() const { |
| 1514 | return getType() == getMachOType(false, true) || |
| 1515 | getType() == getMachOType(true, true); |
| 1516 | } |
| 1517 | |
| 1518 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 1519 | SmallVectorImpl<uint64_t> &Out) const { |
| 1520 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 1521 | |
| 1522 | uint32_t offset = Index; |
| 1523 | uint64_t data = 0; |
| 1524 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 1525 | data += delta; |
| 1526 | Out.push_back(data); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
| 1531 | StringRef Magic = Buffer->getBuffer().slice(0, 4); |
| 1532 | error_code ec; |
| 1533 | ObjectFile *Ret; |
| 1534 | if (Magic == "\xFE\xED\xFA\xCE") |
| 1535 | Ret = new MachOObjectFile(Buffer, false, false, ec); |
| 1536 | else if (Magic == "\xCE\xFA\xED\xFE") |
| 1537 | Ret = new MachOObjectFile(Buffer, true, false, ec); |
| 1538 | else if (Magic == "\xFE\xED\xFA\xCF") |
| 1539 | Ret = new MachOObjectFile(Buffer, false, true, ec); |
| 1540 | else if (Magic == "\xCF\xFA\xED\xFE") |
| 1541 | Ret = new MachOObjectFile(Buffer, true, true, ec); |
| 1542 | else |
| 1543 | return NULL; |
| 1544 | |
| 1545 | if (ec) |
| 1546 | return NULL; |
| 1547 | return Ret; |
| 1548 | } |
| 1549 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 1550 | } // end namespace object |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1551 | } // end namespace llvm |