Eric Christopher | 7b015c7 | 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 | 27c579d | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 15 | #include "llvm/Object/MachO.h" |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Rafael Espindola | 421305a | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DataExtractor.h" |
Owen Anderson | bc14bd3 | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Host.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Jakub Staszak | 84a0ae7 | 2013-08-21 01:20:11 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 23 | #include <cctype> |
| 24 | #include <cstring> |
| 25 | #include <limits> |
| 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace object; |
| 29 | |
| 30 | namespace llvm { |
Owen Anderson | 27c579d | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 31 | namespace object { |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 32 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 33 | struct nlist_base { |
| 34 | uint32_t n_strx; |
| 35 | uint8_t n_type; |
| 36 | uint8_t n_sect; |
| 37 | uint16_t n_desc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 40 | struct section_base { |
| 41 | char sectname[16]; |
| 42 | char segname[16]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | template<typename T> |
| 46 | static void SwapValue(T &Value) { |
| 47 | Value = sys::SwapByteOrder(Value); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 50 | template<typename T> |
| 51 | static void SwapStruct(T &Value); |
| 52 | |
| 53 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 54 | void SwapStruct(MachO::any_relocation_info &H) { |
| 55 | SwapValue(H.r_word0); |
| 56 | SwapValue(H.r_word1); |
Rafael Espindola | 5ffc079 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 59 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 60 | void SwapStruct(MachO::load_command &L) { |
| 61 | SwapValue(L.cmd); |
| 62 | SwapValue(L.cmdsize); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 63 | } |
Rafael Espindola | 421305a | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 64 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 65 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 66 | void SwapStruct(nlist_base &S) { |
| 67 | SwapValue(S.n_strx); |
| 68 | SwapValue(S.n_desc); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 72 | void SwapStruct(MachO::section &S) { |
| 73 | SwapValue(S.addr); |
| 74 | SwapValue(S.size); |
| 75 | SwapValue(S.offset); |
| 76 | SwapValue(S.align); |
| 77 | SwapValue(S.reloff); |
| 78 | SwapValue(S.nreloc); |
| 79 | SwapValue(S.flags); |
| 80 | SwapValue(S.reserved1); |
| 81 | SwapValue(S.reserved2); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 85 | void SwapStruct(MachO::section_64 &S) { |
| 86 | SwapValue(S.addr); |
| 87 | SwapValue(S.size); |
| 88 | SwapValue(S.offset); |
| 89 | SwapValue(S.align); |
| 90 | SwapValue(S.reloff); |
| 91 | SwapValue(S.nreloc); |
| 92 | SwapValue(S.flags); |
| 93 | SwapValue(S.reserved1); |
| 94 | SwapValue(S.reserved2); |
| 95 | SwapValue(S.reserved3); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 99 | void SwapStruct(MachO::nlist &S) { |
| 100 | SwapValue(S.n_strx); |
| 101 | SwapValue(S.n_desc); |
| 102 | SwapValue(S.n_value); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 106 | void SwapStruct(MachO::nlist_64 &S) { |
| 107 | SwapValue(S.n_strx); |
| 108 | SwapValue(S.n_desc); |
| 109 | SwapValue(S.n_value); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 113 | void SwapStruct(MachO::mach_header &H) { |
| 114 | SwapValue(H.magic); |
| 115 | SwapValue(H.cputype); |
| 116 | SwapValue(H.cpusubtype); |
| 117 | SwapValue(H.filetype); |
| 118 | SwapValue(H.ncmds); |
| 119 | SwapValue(H.sizeofcmds); |
| 120 | SwapValue(H.flags); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 124 | void SwapStruct(MachO::mach_header_64 &H) { |
| 125 | SwapValue(H.magic); |
| 126 | SwapValue(H.cputype); |
| 127 | SwapValue(H.cpusubtype); |
| 128 | SwapValue(H.filetype); |
| 129 | SwapValue(H.ncmds); |
| 130 | SwapValue(H.sizeofcmds); |
| 131 | SwapValue(H.flags); |
| 132 | SwapValue(H.reserved); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 136 | void SwapStruct(MachO::symtab_command &C) { |
| 137 | SwapValue(C.cmd); |
| 138 | SwapValue(C.cmdsize); |
| 139 | SwapValue(C.symoff); |
| 140 | SwapValue(C.nsyms); |
| 141 | SwapValue(C.stroff); |
| 142 | SwapValue(C.strsize); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 146 | void SwapStruct(MachO::dysymtab_command &C) { |
| 147 | SwapValue(C.cmd); |
| 148 | SwapValue(C.cmdsize); |
| 149 | SwapValue(C.ilocalsym); |
| 150 | SwapValue(C.nlocalsym); |
| 151 | SwapValue(C.iextdefsym); |
| 152 | SwapValue(C.nextdefsym); |
| 153 | SwapValue(C.iundefsym); |
| 154 | SwapValue(C.nundefsym); |
| 155 | SwapValue(C.tocoff); |
| 156 | SwapValue(C.ntoc); |
| 157 | SwapValue(C.modtaboff); |
| 158 | SwapValue(C.nmodtab); |
| 159 | SwapValue(C.extrefsymoff); |
| 160 | SwapValue(C.nextrefsyms); |
| 161 | SwapValue(C.indirectsymoff); |
| 162 | SwapValue(C.nindirectsyms); |
| 163 | SwapValue(C.extreloff); |
| 164 | SwapValue(C.nextrel); |
| 165 | SwapValue(C.locreloff); |
| 166 | SwapValue(C.nlocrel); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 170 | void SwapStruct(MachO::linkedit_data_command &C) { |
| 171 | SwapValue(C.cmd); |
| 172 | SwapValue(C.cmdsize); |
| 173 | SwapValue(C.dataoff); |
| 174 | SwapValue(C.datasize); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 178 | void SwapStruct(MachO::segment_command &C) { |
| 179 | SwapValue(C.cmd); |
| 180 | SwapValue(C.cmdsize); |
| 181 | SwapValue(C.vmaddr); |
| 182 | SwapValue(C.vmsize); |
| 183 | SwapValue(C.fileoff); |
| 184 | SwapValue(C.filesize); |
| 185 | SwapValue(C.maxprot); |
| 186 | SwapValue(C.initprot); |
| 187 | SwapValue(C.nsects); |
| 188 | SwapValue(C.flags); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 192 | void SwapStruct(MachO::segment_command_64 &C) { |
| 193 | SwapValue(C.cmd); |
| 194 | SwapValue(C.cmdsize); |
| 195 | SwapValue(C.vmaddr); |
| 196 | SwapValue(C.vmsize); |
| 197 | SwapValue(C.fileoff); |
| 198 | SwapValue(C.filesize); |
| 199 | SwapValue(C.maxprot); |
| 200 | SwapValue(C.initprot); |
| 201 | SwapValue(C.nsects); |
| 202 | SwapValue(C.flags); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 205 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 206 | void SwapStruct(uint32_t &C) { |
| 207 | SwapValue(C); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 211 | void SwapStruct(MachO::linker_options_command &C) { |
| 212 | SwapValue(C.cmd); |
| 213 | SwapValue(C.cmdsize); |
| 214 | SwapValue(C.count); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | template<> |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 218 | void SwapStruct(MachO::version_min_command&C) { |
| 219 | SwapValue(C.cmd); |
| 220 | SwapValue(C.cmdsize); |
| 221 | SwapValue(C.version); |
| 222 | SwapValue(C.reserved); |
| 223 | } |
| 224 | |
| 225 | template<> |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 226 | void SwapStruct(MachO::dylib_command&C) { |
| 227 | SwapValue(C.cmd); |
| 228 | SwapValue(C.cmdsize); |
| 229 | SwapValue(C.dylib.name); |
| 230 | SwapValue(C.dylib.timestamp); |
| 231 | SwapValue(C.dylib.current_version); |
| 232 | SwapValue(C.dylib.compatibility_version); |
| 233 | } |
| 234 | |
| 235 | template<> |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 236 | void SwapStruct(MachO::data_in_code_entry &C) { |
| 237 | SwapValue(C.offset); |
| 238 | SwapValue(C.length); |
| 239 | SwapValue(C.kind); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Rafael Espindola | 3cdeb17 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 242 | template<typename T> |
| 243 | T getStruct(const MachOObjectFile *O, const char *P) { |
| 244 | T Cmd; |
| 245 | memcpy(&Cmd, P, sizeof(T)); |
| 246 | if (O->isLittleEndian() != sys::IsLittleEndianHost) |
| 247 | SwapStruct(Cmd); |
| 248 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 251 | static uint32_t |
| 252 | getSegmentLoadCommandNumSections(const MachOObjectFile *O, |
| 253 | const MachOObjectFile::LoadCommandInfo &L) { |
| 254 | if (O->is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 255 | MachO::segment_command_64 S = O->getSegment64LoadCommand(L); |
| 256 | return S.nsects; |
Rafael Espindola | 421305a | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 257 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 258 | MachO::segment_command S = O->getSegmentLoadCommand(L); |
| 259 | return S.nsects; |
Rafael Espindola | 5ffc079 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 262 | static const char * |
| 263 | getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L, |
| 264 | unsigned Sec) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 265 | uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); |
| 266 | |
| 267 | bool Is64 = O->is64Bit(); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 268 | unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) : |
| 269 | sizeof(MachO::segment_command); |
| 270 | unsigned SectionSize = Is64 ? sizeof(MachO::section_64) : |
| 271 | sizeof(MachO::section); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 272 | |
| 273 | uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 274 | return reinterpret_cast<const char*>(SectionAddr); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 277 | static const char *getPtr(const MachOObjectFile *O, size_t Offset) { |
| 278 | return O->getData().substr(Offset, 1).data(); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 281 | static nlist_base |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 282 | getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 283 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 284 | return getStruct<nlist_base>(O, P); |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 287 | static StringRef parseSegmentOrSectionName(const char *P) { |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 288 | if (P[15] == 0) |
| 289 | // Null terminated. |
| 290 | return P; |
| 291 | // Not null terminated, so this is a 16 char string. |
| 292 | return StringRef(P, 16); |
| 293 | } |
| 294 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 295 | // Helper to advance a section or symbol iterator multiple increments at a time. |
| 296 | template<class T> |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 297 | static void advance(T &it, size_t Val) { |
| 298 | while (Val--) |
| 299 | ++it; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static unsigned getCPUType(const MachOObjectFile *O) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 303 | return O->getHeader().cputype; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void printRelocationTargetName(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 307 | const MachO::any_relocation_info &RE, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 308 | raw_string_ostream &fmt) { |
| 309 | bool IsScattered = O->isRelocationScattered(RE); |
| 310 | |
| 311 | // Target of a scattered relocation is an address. In the interest of |
| 312 | // generating pretty output, scan through the symbol table looking for a |
| 313 | // symbol that aligns with that address. If we find one, print it. |
| 314 | // Otherwise, we just print the hex address of the target. |
| 315 | if (IsScattered) { |
| 316 | uint32_t Val = O->getPlainRelocationSymbolNum(RE); |
| 317 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 318 | for (const SymbolRef &Symbol : O->symbols()) { |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 319 | error_code ec; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 320 | uint64_t Addr; |
| 321 | StringRef Name; |
| 322 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 323 | if ((ec = Symbol.getAddress(Addr))) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 324 | report_fatal_error(ec.message()); |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 325 | if (Addr != Val) |
| 326 | continue; |
| 327 | if ((ec = Symbol.getName(Name))) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 328 | report_fatal_error(ec.message()); |
| 329 | fmt << Name; |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // If we couldn't find a symbol that this relocation refers to, try |
| 334 | // to find a section beginning instead. |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 335 | for (const SectionRef &Section : O->sections()) { |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 336 | error_code ec; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 337 | uint64_t Addr; |
| 338 | StringRef Name; |
| 339 | |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 340 | if ((ec = Section.getAddress(Addr))) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 341 | report_fatal_error(ec.message()); |
Alexey Samsonov | 063eb3f | 2014-03-13 13:52:54 +0000 | [diff] [blame] | 342 | if (Addr != Val) |
| 343 | continue; |
| 344 | if ((ec = Section.getName(Name))) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 345 | report_fatal_error(ec.message()); |
| 346 | fmt << Name; |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | fmt << format("0x%x", Val); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | StringRef S; |
| 355 | bool isExtern = O->getPlainRelocationExternal(RE); |
Ahmed Bougacha | 9dab0cc | 2013-05-14 22:41:29 +0000 | [diff] [blame] | 356 | uint64_t Val = O->getPlainRelocationSymbolNum(RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 357 | |
| 358 | if (isExtern) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 359 | symbol_iterator SI = O->symbol_begin(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 360 | advance(SI, Val); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 361 | SI->getName(S); |
| 362 | } else { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 363 | section_iterator SI = O->section_begin(); |
Ahmed Bougacha | 9dab0cc | 2013-05-14 22:41:29 +0000 | [diff] [blame] | 364 | // Adjust for the fact that sections are 1-indexed. |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 365 | advance(SI, Val - 1); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 366 | SI->getName(S); |
| 367 | } |
| 368 | |
| 369 | fmt << S; |
| 370 | } |
| 371 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 372 | static uint32_t |
| 373 | getPlainRelocationAddress(const MachO::any_relocation_info &RE) { |
| 374 | return RE.r_word0; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 378 | getScatteredRelocationAddress(const MachO::any_relocation_info &RE) { |
| 379 | return RE.r_word0 & 0xffffff; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static bool getPlainRelocationPCRel(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 383 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 384 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 385 | return (RE.r_word1 >> 24) & 1; |
| 386 | return (RE.r_word1 >> 7) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static bool |
| 390 | getScatteredRelocationPCRel(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 391 | const MachO::any_relocation_info &RE) { |
| 392 | return (RE.r_word0 >> 30) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static unsigned getPlainRelocationLength(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 396 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 397 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 398 | return (RE.r_word1 >> 25) & 3; |
| 399 | return (RE.r_word1 >> 5) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 403 | getScatteredRelocationLength(const MachO::any_relocation_info &RE) { |
| 404 | return (RE.r_word0 >> 28) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static unsigned getPlainRelocationType(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 408 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 409 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 410 | return RE.r_word1 >> 28; |
| 411 | return RE.r_word1 & 0xf; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 414 | static unsigned |
| 415 | getScatteredRelocationType(const MachO::any_relocation_info &RE) { |
| 416 | return (RE.r_word0 >> 24) & 0xf; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static uint32_t getSectionFlags(const MachOObjectFile *O, |
| 420 | DataRefImpl Sec) { |
| 421 | if (O->is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 422 | MachO::section_64 Sect = O->getSection64(Sec); |
| 423 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 424 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 425 | MachO::section Sect = O->getSection(Sec); |
| 426 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 429 | MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, |
| 430 | bool Is64bits, error_code &EC, |
| 431 | bool BufferOwned) |
| 432 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 433 | SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr), |
| 434 | DataInCodeLoadCmd(nullptr) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 435 | uint32_t LoadCommandCount = this->getHeader().ncmds; |
| 436 | MachO::LoadCommandType SegmentLoadType = is64Bit() ? |
| 437 | MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 438 | |
| 439 | MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo(); |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 440 | for (unsigned I = 0; ; ++I) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 441 | if (Load.C.cmd == MachO::LC_SYMTAB) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 442 | assert(!SymtabLoadCmd && "Multiple symbol tables"); |
| 443 | SymtabLoadCmd = Load.Ptr; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 444 | } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 445 | assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables"); |
| 446 | DysymtabLoadCmd = Load.Ptr; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 447 | } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 448 | assert(!DataInCodeLoadCmd && "Multiple data in code tables"); |
| 449 | DataInCodeLoadCmd = Load.Ptr; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 450 | } else if (Load.C.cmd == SegmentLoadType) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 451 | uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load); |
| 452 | for (unsigned J = 0; J < NumSections; ++J) { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 453 | const char *Sec = getSectionPtr(this, Load, J); |
| 454 | Sections.push_back(Sec); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 455 | } |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 456 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB || |
| 457 | Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || |
| 458 | Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || |
| 459 | Load.C.cmd == MachO::LC_REEXPORT_DYLIB || |
| 460 | Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
| 461 | Libraries.push_back(Load.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 462 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 463 | |
| 464 | if (I == LoadCommandCount - 1) |
| 465 | break; |
| 466 | else |
| 467 | Load = getNextLoadCommandInfo(Load); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 471 | void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 472 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 473 | sizeof(MachO::nlist_64) : |
| 474 | sizeof(MachO::nlist); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 475 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | error_code MachOObjectFile::getSymbolName(DataRefImpl Symb, |
| 479 | StringRef &Res) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 480 | StringRef StringTable = getStringTableData(); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 481 | nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
| 482 | const char *Start = &StringTable.data()[Entry.n_strx]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 483 | Res = StringRef(Start); |
| 484 | return object_error::success; |
| 485 | } |
| 486 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 487 | // getIndirectName() returns the name of the alias'ed symbol who's string table |
| 488 | // index is in the n_value field. |
| 489 | error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, |
| 490 | StringRef &Res) const { |
| 491 | StringRef StringTable = getStringTableData(); |
| 492 | uint64_t NValue; |
| 493 | if (is64Bit()) { |
| 494 | MachO::nlist_64 Entry = getSymbol64TableEntry(Symb); |
| 495 | NValue = Entry.n_value; |
| 496 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 497 | return object_error::parse_failed; |
| 498 | } else { |
| 499 | MachO::nlist Entry = getSymbolTableEntry(Symb); |
| 500 | NValue = Entry.n_value; |
| 501 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 502 | return object_error::parse_failed; |
| 503 | } |
| 504 | if (NValue >= StringTable.size()) |
| 505 | return object_error::parse_failed; |
| 506 | const char *Start = &StringTable.data()[NValue]; |
| 507 | Res = StringRef(Start); |
| 508 | return object_error::success; |
| 509 | } |
| 510 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 511 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 512 | uint64_t &Res) const { |
| 513 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 514 | MachO::nlist_64 Entry = getSymbol64TableEntry(Symb); |
Kevin Enderby | 1b985af | 2014-05-20 23:04:47 +0000 | [diff] [blame] | 515 | if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF && |
| 516 | Entry.n_value == 0) |
| 517 | Res = UnknownAddressOrSize; |
| 518 | else |
| 519 | Res = Entry.n_value; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 520 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 521 | MachO::nlist Entry = getSymbolTableEntry(Symb); |
Kevin Enderby | 1b985af | 2014-05-20 23:04:47 +0000 | [diff] [blame] | 522 | if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF && |
| 523 | Entry.n_value == 0) |
| 524 | Res = UnknownAddressOrSize; |
| 525 | else |
| 526 | Res = Entry.n_value; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 527 | } |
| 528 | return object_error::success; |
| 529 | } |
| 530 | |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 531 | error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI, |
| 532 | uint32_t &Result) const { |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 533 | uint32_t flags = getSymbolFlags(DRI); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 534 | if (flags & SymbolRef::SF_Common) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 535 | nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
| 536 | Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 537 | } else { |
| 538 | Result = 0; |
| 539 | } |
| 540 | return object_error::success; |
| 541 | } |
| 542 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 543 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 544 | uint64_t &Result) const { |
| 545 | uint64_t BeginOffset; |
| 546 | uint64_t EndOffset = 0; |
| 547 | uint8_t SectionIndex; |
| 548 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 549 | nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 550 | uint64_t Value; |
| 551 | getSymbolAddress(DRI, Value); |
Kevin Enderby | 1b985af | 2014-05-20 23:04:47 +0000 | [diff] [blame] | 552 | if (Value == UnknownAddressOrSize) { |
| 553 | Result = UnknownAddressOrSize; |
| 554 | return object_error::success; |
| 555 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 556 | |
| 557 | BeginOffset = Value; |
| 558 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 559 | SectionIndex = Entry.n_sect; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 560 | if (!SectionIndex) { |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 561 | uint32_t flags = getSymbolFlags(DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 562 | if (flags & SymbolRef::SF_Common) |
| 563 | Result = Value; |
| 564 | else |
| 565 | Result = UnknownAddressOrSize; |
| 566 | return object_error::success; |
| 567 | } |
| 568 | // Unfortunately symbols are unsorted so we need to touch all |
| 569 | // symbols from load command |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 570 | for (const SymbolRef &Symbol : symbols()) { |
| 571 | DataRefImpl DRI = Symbol.getRawDataRefImpl(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 572 | Entry = getSymbolTableEntryBase(this, DRI); |
| 573 | getSymbolAddress(DRI, Value); |
Kevin Enderby | 1b985af | 2014-05-20 23:04:47 +0000 | [diff] [blame] | 574 | if (Value == UnknownAddressOrSize) |
| 575 | continue; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 576 | if (Entry.n_sect == SectionIndex && Value > BeginOffset) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 577 | if (!EndOffset || Value < EndOffset) |
| 578 | EndOffset = Value; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 579 | } |
| 580 | if (!EndOffset) { |
| 581 | uint64_t Size; |
| 582 | DataRefImpl Sec; |
| 583 | Sec.d.a = SectionIndex-1; |
| 584 | getSectionSize(Sec, Size); |
| 585 | getSectionAddress(Sec, EndOffset); |
| 586 | EndOffset += Size; |
| 587 | } |
| 588 | Result = EndOffset - BeginOffset; |
| 589 | return object_error::success; |
| 590 | } |
| 591 | |
| 592 | error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, |
| 593 | SymbolRef::Type &Res) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 594 | nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
| 595 | uint8_t n_type = Entry.n_type; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 596 | |
| 597 | Res = SymbolRef::ST_Other; |
| 598 | |
| 599 | // If this is a STAB debugging symbol, we can do nothing more. |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 600 | if (n_type & MachO::N_STAB) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 601 | Res = SymbolRef::ST_Debug; |
| 602 | return object_error::success; |
| 603 | } |
| 604 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 605 | switch (n_type & MachO::N_TYPE) { |
| 606 | case MachO::N_UNDF : |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 607 | Res = SymbolRef::ST_Unknown; |
| 608 | break; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 609 | case MachO::N_SECT : |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 610 | Res = SymbolRef::ST_Function; |
| 611 | break; |
| 612 | } |
| 613 | return object_error::success; |
| 614 | } |
| 615 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 616 | uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 617 | nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 618 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 619 | uint8_t MachOType = Entry.n_type; |
| 620 | uint16_t MachOFlags = Entry.n_desc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 621 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 622 | uint32_t Result = SymbolRef::SF_None; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 623 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 624 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 625 | Result |= SymbolRef::SF_Undefined; |
| 626 | |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 627 | if ((MachOType & MachO::N_TYPE) == MachO::N_INDR) |
| 628 | Result |= SymbolRef::SF_Indirect; |
| 629 | |
Rafael Espindola | a135632 | 2013-11-02 05:03:24 +0000 | [diff] [blame] | 630 | if (MachOType & MachO::N_STAB) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 631 | Result |= SymbolRef::SF_FormatSpecific; |
| 632 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 633 | if (MachOType & MachO::N_EXT) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 634 | Result |= SymbolRef::SF_Global; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 635 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 636 | uint64_t Value; |
| 637 | getSymbolAddress(DRI, Value); |
Kevin Enderby | 1b985af | 2014-05-20 23:04:47 +0000 | [diff] [blame] | 638 | if (Value && Value != UnknownAddressOrSize) |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 639 | Result |= SymbolRef::SF_Common; |
| 640 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 643 | if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 644 | Result |= SymbolRef::SF_Weak; |
| 645 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 646 | if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 647 | Result |= SymbolRef::SF_Absolute; |
| 648 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 649 | return Result; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | error_code |
| 653 | MachOObjectFile::getSymbolSection(DataRefImpl Symb, |
| 654 | section_iterator &Res) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 655 | nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
| 656 | uint8_t index = Entry.n_sect; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 657 | |
| 658 | if (index == 0) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 659 | Res = section_end(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 660 | } else { |
| 661 | DataRefImpl DRI; |
| 662 | DRI.d.a = index - 1; |
| 663 | Res = section_iterator(SectionRef(DRI, this)); |
| 664 | } |
| 665 | |
| 666 | return object_error::success; |
| 667 | } |
| 668 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 669 | void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 670 | Sec.d.a++; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 674 | MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 675 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 676 | Result = parseSegmentOrSectionName(Raw.data()); |
| 677 | return object_error::success; |
| 678 | } |
| 679 | |
| 680 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 681 | MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 682 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 683 | MachO::section_64 Sect = getSection64(Sec); |
| 684 | Res = Sect.addr; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 685 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 686 | MachO::section Sect = getSection(Sec); |
| 687 | Res = Sect.addr; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 688 | } |
| 689 | return object_error::success; |
| 690 | } |
| 691 | |
| 692 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 693 | MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 694 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 695 | MachO::section_64 Sect = getSection64(Sec); |
| 696 | Res = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 697 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 698 | MachO::section Sect = getSection(Sec); |
| 699 | Res = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | return object_error::success; |
| 703 | } |
| 704 | |
| 705 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 706 | MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 707 | uint32_t Offset; |
| 708 | uint64_t Size; |
| 709 | |
| 710 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 711 | MachO::section_64 Sect = getSection64(Sec); |
| 712 | Offset = Sect.offset; |
| 713 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 714 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 715 | MachO::section Sect = getSection(Sec); |
| 716 | Offset = Sect.offset; |
| 717 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | Res = this->getData().substr(Offset, Size); |
| 721 | return object_error::success; |
| 722 | } |
| 723 | |
| 724 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 725 | MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 726 | uint32_t Align; |
| 727 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 728 | MachO::section_64 Sect = getSection64(Sec); |
| 729 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 730 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 731 | MachO::section Sect = getSection(Sec); |
| 732 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | Res = uint64_t(1) << Align; |
| 736 | return object_error::success; |
| 737 | } |
| 738 | |
| 739 | error_code |
| 740 | MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const { |
| 741 | uint32_t Flags = getSectionFlags(this, Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 742 | Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 743 | return object_error::success; |
| 744 | } |
| 745 | |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 746 | error_code MachOObjectFile::isSectionData(DataRefImpl Sec, bool &Result) const { |
| 747 | uint32_t Flags = getSectionFlags(this, Sec); |
| 748 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 749 | Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 750 | !(SectionType == MachO::S_ZEROFILL || |
| 751 | SectionType == MachO::S_GB_ZEROFILL); |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 752 | return object_error::success; |
| 753 | } |
| 754 | |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 755 | error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec, bool &Result) const { |
| 756 | uint32_t Flags = getSectionFlags(this, Sec); |
| 757 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 758 | Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 759 | (SectionType == MachO::S_ZEROFILL || |
| 760 | SectionType == MachO::S_GB_ZEROFILL); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 761 | return object_error::success; |
| 762 | } |
| 763 | |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 764 | error_code |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 765 | MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 766 | bool &Result) const { |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 767 | // FIXME: Unimplemented. |
| 768 | Result = true; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 769 | return object_error::success; |
| 770 | } |
| 771 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 772 | error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 773 | bool &Result) const { |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 774 | // FIXME: Unimplemented. |
| 775 | Result = false; |
| 776 | return object_error::success; |
| 777 | } |
| 778 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 779 | error_code |
| 780 | MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const { |
| 781 | uint32_t Flags = getSectionFlags(this, Sec); |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 782 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
| 783 | Res = SectionType == MachO::S_ZEROFILL || |
| 784 | SectionType == MachO::S_GB_ZEROFILL; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 785 | return object_error::success; |
| 786 | } |
| 787 | |
| 788 | error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 789 | bool &Result) const { |
Andrew Kaylor | 3f31fa0 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 790 | // Consider using the code from isSectionText to look for __const sections. |
| 791 | // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS |
| 792 | // to use section attributes to distinguish code from data. |
| 793 | |
| 794 | // FIXME: Unimplemented. |
| 795 | Result = false; |
| 796 | return object_error::success; |
| 797 | } |
| 798 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 799 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 800 | MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 801 | bool &Result) const { |
| 802 | SymbolRef::Type ST; |
| 803 | this->getSymbolType(Symb, ST); |
| 804 | if (ST == SymbolRef::ST_Unknown) { |
| 805 | Result = false; |
| 806 | return object_error::success; |
| 807 | } |
| 808 | |
| 809 | uint64_t SectBegin, SectEnd; |
| 810 | getSectionAddress(Sec, SectBegin); |
| 811 | getSectionSize(Sec, SectEnd); |
| 812 | SectEnd += SectBegin; |
| 813 | |
| 814 | uint64_t SymAddr; |
| 815 | getSymbolAddress(Symb, SymAddr); |
| 816 | Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); |
| 817 | |
| 818 | return object_error::success; |
| 819 | } |
| 820 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 821 | relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 822 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 823 | Ret.d.a = Sec.d.a; |
| 824 | Ret.d.b = 0; |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 825 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 826 | } |
Rafael Espindola | c0406e1 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 827 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 828 | relocation_iterator |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 829 | MachOObjectFile::section_rel_end(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 830 | uint32_t Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 831 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 832 | MachO::section_64 Sect = getSection64(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 833 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 834 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 835 | MachO::section Sect = getSection(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 836 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 837 | } |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 838 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 839 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 840 | Ret.d.a = Sec.d.a; |
| 841 | Ret.d.b = Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 842 | return relocation_iterator(RelocationRef(Ret, this)); |
| 843 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 844 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 845 | void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 846 | ++Rel.d.b; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 847 | } |
Owen Anderson | 171f485 | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 848 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 849 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 850 | MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const { |
Rafael Espindola | 7247546 | 2014-04-04 00:31:12 +0000 | [diff] [blame] | 851 | uint64_t Offset; |
| 852 | getRelocationOffset(Rel, Offset); |
Rafael Espindola | 7e91bc9 | 2014-04-03 23:54:35 +0000 | [diff] [blame] | 853 | |
| 854 | DataRefImpl Sec; |
| 855 | Sec.d.a = Rel.d.a; |
| 856 | uint64_t SecAddress; |
| 857 | getSectionAddress(Sec, SecAddress); |
| 858 | Res = SecAddress + Offset; |
| 859 | return object_error::success; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 862 | error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 863 | uint64_t &Res) const { |
Rafael Espindola | 7247546 | 2014-04-04 00:31:12 +0000 | [diff] [blame] | 864 | assert(getHeader().filetype == MachO::MH_OBJECT && |
| 865 | "Only implemented for MH_OBJECT"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 866 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 867 | Res = getAnyRelocationAddress(RE); |
| 868 | return object_error::success; |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 871 | symbol_iterator |
| 872 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 873 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 874 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 875 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 876 | if (!isExtern) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 877 | return symbol_end(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 878 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 879 | MachO::symtab_command S = getSymtabLoadCommand(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 880 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 881 | sizeof(MachO::nlist_64) : |
| 882 | sizeof(MachO::nlist); |
| 883 | uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 884 | DataRefImpl Sym; |
| 885 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 886 | return symbol_iterator(SymbolRef(Sym, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 890 | uint64_t &Res) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 891 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 892 | Res = getAnyRelocationType(RE); |
| 893 | return object_error::success; |
| 894 | } |
| 895 | |
| 896 | error_code |
| 897 | MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 898 | SmallVectorImpl<char> &Result) const { |
| 899 | StringRef res; |
| 900 | uint64_t RType; |
| 901 | getRelocationType(Rel, RType); |
| 902 | |
| 903 | unsigned Arch = this->getArch(); |
| 904 | |
| 905 | switch (Arch) { |
| 906 | case Triple::x86: { |
| 907 | static const char *const Table[] = { |
| 908 | "GENERIC_RELOC_VANILLA", |
| 909 | "GENERIC_RELOC_PAIR", |
| 910 | "GENERIC_RELOC_SECTDIFF", |
| 911 | "GENERIC_RELOC_PB_LA_PTR", |
| 912 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 913 | "GENERIC_RELOC_TLV" }; |
| 914 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 915 | if (RType > 5) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 916 | res = "Unknown"; |
| 917 | else |
| 918 | res = Table[RType]; |
| 919 | break; |
| 920 | } |
| 921 | case Triple::x86_64: { |
| 922 | static const char *const Table[] = { |
| 923 | "X86_64_RELOC_UNSIGNED", |
| 924 | "X86_64_RELOC_SIGNED", |
| 925 | "X86_64_RELOC_BRANCH", |
| 926 | "X86_64_RELOC_GOT_LOAD", |
| 927 | "X86_64_RELOC_GOT", |
| 928 | "X86_64_RELOC_SUBTRACTOR", |
| 929 | "X86_64_RELOC_SIGNED_1", |
| 930 | "X86_64_RELOC_SIGNED_2", |
| 931 | "X86_64_RELOC_SIGNED_4", |
| 932 | "X86_64_RELOC_TLV" }; |
| 933 | |
| 934 | if (RType > 9) |
| 935 | res = "Unknown"; |
| 936 | else |
| 937 | res = Table[RType]; |
| 938 | break; |
| 939 | } |
| 940 | case Triple::arm: { |
| 941 | static const char *const Table[] = { |
| 942 | "ARM_RELOC_VANILLA", |
| 943 | "ARM_RELOC_PAIR", |
| 944 | "ARM_RELOC_SECTDIFF", |
| 945 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 946 | "ARM_RELOC_PB_LA_PTR", |
| 947 | "ARM_RELOC_BR24", |
| 948 | "ARM_THUMB_RELOC_BR22", |
| 949 | "ARM_THUMB_32BIT_BRANCH", |
| 950 | "ARM_RELOC_HALF", |
| 951 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 952 | |
| 953 | if (RType > 9) |
| 954 | res = "Unknown"; |
| 955 | else |
| 956 | res = Table[RType]; |
| 957 | break; |
| 958 | } |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 959 | case Triple::arm64: |
| 960 | case Triple::aarch64: { |
| 961 | static const char *const Table[] = { |
| 962 | "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR", |
| 963 | "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", |
| 964 | "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21", |
| 965 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT", |
| 966 | "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", |
| 967 | "ARM64_RELOC_ADDEND" |
| 968 | }; |
| 969 | |
| 970 | if (RType >= array_lengthof(Table)) |
| 971 | res = "Unknown"; |
| 972 | else |
| 973 | res = Table[RType]; |
| 974 | break; |
| 975 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 976 | case Triple::ppc: { |
| 977 | static const char *const Table[] = { |
| 978 | "PPC_RELOC_VANILLA", |
| 979 | "PPC_RELOC_PAIR", |
| 980 | "PPC_RELOC_BR14", |
| 981 | "PPC_RELOC_BR24", |
| 982 | "PPC_RELOC_HI16", |
| 983 | "PPC_RELOC_LO16", |
| 984 | "PPC_RELOC_HA16", |
| 985 | "PPC_RELOC_LO14", |
| 986 | "PPC_RELOC_SECTDIFF", |
| 987 | "PPC_RELOC_PB_LA_PTR", |
| 988 | "PPC_RELOC_HI16_SECTDIFF", |
| 989 | "PPC_RELOC_LO16_SECTDIFF", |
| 990 | "PPC_RELOC_HA16_SECTDIFF", |
| 991 | "PPC_RELOC_JBSR", |
| 992 | "PPC_RELOC_LO14_SECTDIFF", |
| 993 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 994 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 995 | if (RType > 15) |
| 996 | res = "Unknown"; |
| 997 | else |
| 998 | res = Table[RType]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | case Triple::UnknownArch: |
| 1002 | res = "Unknown"; |
| 1003 | break; |
| 1004 | } |
| 1005 | Result.append(res.begin(), res.end()); |
| 1006 | return object_error::success; |
| 1007 | } |
| 1008 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1009 | error_code |
| 1010 | MachOObjectFile::getRelocationValueString(DataRefImpl Rel, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1011 | SmallVectorImpl<char> &Result) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1012 | MachO::any_relocation_info RE = getRelocation(Rel); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 1013 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1014 | unsigned Arch = this->getArch(); |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1015 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1016 | std::string fmtbuf; |
| 1017 | raw_string_ostream fmt(fmtbuf); |
| 1018 | unsigned Type = this->getAnyRelocationType(RE); |
| 1019 | bool IsPCRel = this->getAnyRelocationPCRel(RE); |
| 1020 | |
| 1021 | // Determine any addends that should be displayed with the relocation. |
| 1022 | // These require decoding the relocation type, which is triple-specific. |
| 1023 | |
| 1024 | // X86_64 has entirely custom relocation types. |
| 1025 | if (Arch == Triple::x86_64) { |
| 1026 | bool isPCRel = getAnyRelocationPCRel(RE); |
| 1027 | |
| 1028 | switch (Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1029 | case MachO::X86_64_RELOC_GOT_LOAD: |
| 1030 | case MachO::X86_64_RELOC_GOT: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1031 | printRelocationTargetName(this, RE, fmt); |
| 1032 | fmt << "@GOT"; |
| 1033 | if (isPCRel) fmt << "PCREL"; |
| 1034 | break; |
| 1035 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1036 | case MachO::X86_64_RELOC_SUBTRACTOR: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1037 | DataRefImpl RelNext = Rel; |
Rafael Espindola | 0cc9ba1 | 2014-04-03 23:20:02 +0000 | [diff] [blame] | 1038 | moveRelocationNext(RelNext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1039 | MachO::any_relocation_info RENext = getRelocation(RelNext); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1040 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1041 | // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1042 | // X86_64_RELOC_UNSIGNED. |
| 1043 | // NOTE: Scattered relocations don't exist on x86_64. |
| 1044 | unsigned RType = getAnyRelocationType(RENext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1045 | if (RType != MachO::X86_64_RELOC_UNSIGNED) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1046 | report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " |
| 1047 | "X86_64_RELOC_SUBTRACTOR."); |
| 1048 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1049 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol; |
| 1050 | // X86_64_RELOC_SUBTRACTOR contains the subtrahend. |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1051 | printRelocationTargetName(this, RENext, fmt); |
| 1052 | fmt << "-"; |
| 1053 | printRelocationTargetName(this, RE, fmt); |
| 1054 | break; |
| 1055 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1056 | case MachO::X86_64_RELOC_TLV: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1057 | printRelocationTargetName(this, RE, fmt); |
| 1058 | fmt << "@TLV"; |
| 1059 | if (isPCRel) fmt << "P"; |
| 1060 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1061 | case MachO::X86_64_RELOC_SIGNED_1: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1062 | printRelocationTargetName(this, RE, fmt); |
| 1063 | fmt << "-1"; |
| 1064 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1065 | case MachO::X86_64_RELOC_SIGNED_2: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1066 | printRelocationTargetName(this, RE, fmt); |
| 1067 | fmt << "-2"; |
| 1068 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1069 | case MachO::X86_64_RELOC_SIGNED_4: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1070 | printRelocationTargetName(this, RE, fmt); |
| 1071 | fmt << "-4"; |
| 1072 | break; |
| 1073 | default: |
| 1074 | printRelocationTargetName(this, RE, fmt); |
| 1075 | break; |
| 1076 | } |
| 1077 | // X86 and ARM share some relocation types in common. |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 1078 | } else if (Arch == Triple::x86 || Arch == Triple::arm || |
| 1079 | Arch == Triple::ppc) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1080 | // Generic relocation types... |
| 1081 | switch (Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1082 | case MachO::GENERIC_RELOC_PAIR: // prints no info |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1083 | return object_error::success; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1084 | case MachO::GENERIC_RELOC_SECTDIFF: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1085 | DataRefImpl RelNext = Rel; |
Rafael Espindola | 0cc9ba1 | 2014-04-03 23:20:02 +0000 | [diff] [blame] | 1086 | moveRelocationNext(RelNext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1087 | MachO::any_relocation_info RENext = getRelocation(RelNext); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1088 | |
| 1089 | // X86 sect diff's must be followed by a relocation of type |
| 1090 | // GENERIC_RELOC_PAIR. |
| 1091 | unsigned RType = getAnyRelocationType(RENext); |
| 1092 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1093 | if (RType != MachO::GENERIC_RELOC_PAIR) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1094 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1095 | "GENERIC_RELOC_SECTDIFF."); |
| 1096 | |
| 1097 | printRelocationTargetName(this, RE, fmt); |
| 1098 | fmt << "-"; |
| 1099 | printRelocationTargetName(this, RENext, fmt); |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 1104 | if (Arch == Triple::x86 || Arch == Triple::ppc) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1105 | switch (Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1106 | case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1107 | DataRefImpl RelNext = Rel; |
Rafael Espindola | 0cc9ba1 | 2014-04-03 23:20:02 +0000 | [diff] [blame] | 1108 | moveRelocationNext(RelNext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1109 | MachO::any_relocation_info RENext = getRelocation(RelNext); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1110 | |
| 1111 | // X86 sect diff's must be followed by a relocation of type |
| 1112 | // GENERIC_RELOC_PAIR. |
| 1113 | unsigned RType = getAnyRelocationType(RENext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1114 | if (RType != MachO::GENERIC_RELOC_PAIR) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1115 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1116 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
| 1117 | |
| 1118 | printRelocationTargetName(this, RE, fmt); |
| 1119 | fmt << "-"; |
| 1120 | printRelocationTargetName(this, RENext, fmt); |
| 1121 | break; |
| 1122 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1123 | case MachO::GENERIC_RELOC_TLV: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1124 | printRelocationTargetName(this, RE, fmt); |
| 1125 | fmt << "@TLV"; |
| 1126 | if (IsPCRel) fmt << "P"; |
| 1127 | break; |
| 1128 | } |
| 1129 | default: |
| 1130 | printRelocationTargetName(this, RE, fmt); |
| 1131 | } |
| 1132 | } else { // ARM-specific relocations |
| 1133 | switch (Type) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1134 | case MachO::ARM_RELOC_HALF: |
| 1135 | case MachO::ARM_RELOC_HALF_SECTDIFF: { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1136 | // Half relocations steal a bit from the length field to encode |
| 1137 | // whether this is an upper16 or a lower16 relocation. |
| 1138 | bool isUpper = getAnyRelocationLength(RE) >> 1; |
| 1139 | |
| 1140 | if (isUpper) |
| 1141 | fmt << ":upper16:("; |
| 1142 | else |
| 1143 | fmt << ":lower16:("; |
| 1144 | printRelocationTargetName(this, RE, fmt); |
| 1145 | |
| 1146 | DataRefImpl RelNext = Rel; |
Rafael Espindola | 0cc9ba1 | 2014-04-03 23:20:02 +0000 | [diff] [blame] | 1147 | moveRelocationNext(RelNext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1148 | MachO::any_relocation_info RENext = getRelocation(RelNext); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1149 | |
| 1150 | // ARM half relocs must be followed by a relocation of type |
| 1151 | // ARM_RELOC_PAIR. |
| 1152 | unsigned RType = getAnyRelocationType(RENext); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1153 | if (RType != MachO::ARM_RELOC_PAIR) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1154 | report_fatal_error("Expected ARM_RELOC_PAIR after " |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1155 | "ARM_RELOC_HALF"); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1156 | |
| 1157 | // NOTE: The half of the target virtual address is stashed in the |
| 1158 | // address field of the secondary relocation, but we can't reverse |
| 1159 | // engineer the constant offset from it without decoding the movw/movt |
| 1160 | // instruction to find the other half in its immediate field. |
| 1161 | |
| 1162 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 1163 | // symbol/section pointer of the follow-on relocation. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1164 | if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1165 | fmt << "-"; |
| 1166 | printRelocationTargetName(this, RENext, fmt); |
| 1167 | } |
| 1168 | |
| 1169 | fmt << ")"; |
| 1170 | break; |
| 1171 | } |
| 1172 | default: { |
| 1173 | printRelocationTargetName(this, RE, fmt); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } else |
| 1178 | printRelocationTargetName(this, RE, fmt); |
| 1179 | |
| 1180 | fmt.flush(); |
| 1181 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 1182 | return object_error::success; |
| 1183 | } |
| 1184 | |
| 1185 | error_code |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1186 | MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1187 | unsigned Arch = getArch(); |
| 1188 | uint64_t Type; |
| 1189 | getRelocationType(Rel, Type); |
| 1190 | |
| 1191 | Result = false; |
| 1192 | |
| 1193 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 1194 | // is always hidden. |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 1195 | if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1196 | if (Type == MachO::GENERIC_RELOC_PAIR) Result = true; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1197 | } else if (Arch == Triple::x86_64) { |
| 1198 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
Eric Christopher | 1ff26ab6 | 2013-07-22 22:25:09 +0000 | [diff] [blame] | 1199 | // an X86_64_RELOC_SUBTRACTOR. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1200 | if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1201 | DataRefImpl RelPrev = Rel; |
| 1202 | RelPrev.d.a--; |
| 1203 | uint64_t PrevType; |
| 1204 | getRelocationType(RelPrev, PrevType); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1205 | if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1206 | Result = true; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | return object_error::success; |
| 1211 | } |
| 1212 | |
| 1213 | error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1214 | LibraryRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1215 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1216 | } |
| 1217 | |
| 1218 | error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, |
Rafael Espindola | 137faa0 | 2013-04-24 15:14:22 +0000 | [diff] [blame] | 1219 | StringRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1220 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1221 | } |
| 1222 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1223 | // |
| 1224 | // guessLibraryShortName() is passed a name of a dynamic library and returns a |
| 1225 | // guess on what the short name is. Then name is returned as a substring of the |
| 1226 | // StringRef Name passed in. The name of the dynamic library is recognized as |
| 1227 | // a framework if it has one of the two following forms: |
| 1228 | // Foo.framework/Versions/A/Foo |
| 1229 | // Foo.framework/Foo |
| 1230 | // Where A and Foo can be any string. And may contain a trailing suffix |
| 1231 | // starting with an underbar. If the Name is recognized as a framework then |
| 1232 | // isFramework is set to true else it is set to false. If the Name has a |
| 1233 | // suffix then Suffix is set to the substring in Name that contains the suffix |
| 1234 | // else it is set to a NULL StringRef. |
| 1235 | // |
| 1236 | // The Name of the dynamic library is recognized as a library name if it has |
| 1237 | // one of the two following forms: |
| 1238 | // libFoo.A.dylib |
| 1239 | // libFoo.dylib |
| 1240 | // The library may have a suffix trailing the name Foo of the form: |
| 1241 | // libFoo_profile.A.dylib |
| 1242 | // libFoo_profile.dylib |
| 1243 | // |
| 1244 | // The Name of the dynamic library is also recognized as a library name if it |
| 1245 | // has the following form: |
| 1246 | // Foo.qtx |
| 1247 | // |
| 1248 | // If the Name of the dynamic library is none of the forms above then a NULL |
| 1249 | // StringRef is returned. |
| 1250 | // |
| 1251 | StringRef MachOObjectFile::guessLibraryShortName(StringRef Name, |
| 1252 | bool &isFramework, |
| 1253 | StringRef &Suffix) { |
| 1254 | StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx; |
| 1255 | size_t a, b, c, d, Idx; |
| 1256 | |
| 1257 | isFramework = false; |
| 1258 | Suffix = StringRef(); |
| 1259 | |
| 1260 | // Pull off the last component and make Foo point to it |
| 1261 | a = Name.rfind('/'); |
| 1262 | if (a == Name.npos || a == 0) |
| 1263 | goto guess_library; |
| 1264 | Foo = Name.slice(a+1, Name.npos); |
| 1265 | |
| 1266 | // Look for a suffix starting with a '_' |
| 1267 | Idx = Foo.rfind('_'); |
| 1268 | if (Idx != Foo.npos && Foo.size() >= 2) { |
| 1269 | Suffix = Foo.slice(Idx, Foo.npos); |
| 1270 | Foo = Foo.slice(0, Idx); |
| 1271 | } |
| 1272 | |
| 1273 | // First look for the form Foo.framework/Foo |
| 1274 | b = Name.rfind('/', a); |
| 1275 | if (b == Name.npos) |
| 1276 | Idx = 0; |
| 1277 | else |
| 1278 | Idx = b+1; |
| 1279 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1280 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1281 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1282 | if (F == Foo && DotFramework == ".framework/") { |
| 1283 | isFramework = true; |
| 1284 | return Foo; |
| 1285 | } |
| 1286 | |
| 1287 | // Next look for the form Foo.framework/Versions/A/Foo |
| 1288 | if (b == Name.npos) |
| 1289 | goto guess_library; |
| 1290 | c = Name.rfind('/', b); |
| 1291 | if (c == Name.npos || c == 0) |
| 1292 | goto guess_library; |
| 1293 | V = Name.slice(c+1, Name.npos); |
| 1294 | if (!V.startswith("Versions/")) |
| 1295 | goto guess_library; |
| 1296 | d = Name.rfind('/', c); |
| 1297 | if (d == Name.npos) |
| 1298 | Idx = 0; |
| 1299 | else |
| 1300 | Idx = d+1; |
| 1301 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1302 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1303 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1304 | if (F == Foo && DotFramework == ".framework/") { |
| 1305 | isFramework = true; |
| 1306 | return Foo; |
| 1307 | } |
| 1308 | |
| 1309 | guess_library: |
| 1310 | // pull off the suffix after the "." and make a point to it |
| 1311 | a = Name.rfind('.'); |
| 1312 | if (a == Name.npos || a == 0) |
| 1313 | return StringRef(); |
| 1314 | Dylib = Name.slice(a, Name.npos); |
| 1315 | if (Dylib != ".dylib") |
| 1316 | goto guess_qtx; |
| 1317 | |
| 1318 | // First pull off the version letter for the form Foo.A.dylib if any. |
| 1319 | if (a >= 3) { |
| 1320 | Dot = Name.slice(a-2, a-1); |
| 1321 | if (Dot == ".") |
| 1322 | a = a - 2; |
| 1323 | } |
| 1324 | |
| 1325 | b = Name.rfind('/', a); |
| 1326 | if (b == Name.npos) |
| 1327 | b = 0; |
| 1328 | else |
| 1329 | b = b+1; |
| 1330 | // ignore any suffix after an underbar like Foo_profile.A.dylib |
| 1331 | Idx = Name.find('_', b); |
| 1332 | if (Idx != Name.npos && Idx != b) { |
| 1333 | Lib = Name.slice(b, Idx); |
| 1334 | Suffix = Name.slice(Idx, a); |
| 1335 | } |
| 1336 | else |
| 1337 | Lib = Name.slice(b, a); |
| 1338 | // There are incorrect library names of the form: |
| 1339 | // libATS.A_profile.dylib so check for these. |
| 1340 | if (Lib.size() >= 3) { |
| 1341 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1342 | if (Dot == ".") |
| 1343 | Lib = Lib.slice(0, Lib.size()-2); |
| 1344 | } |
| 1345 | return Lib; |
| 1346 | |
| 1347 | guess_qtx: |
| 1348 | Qtx = Name.slice(a, Name.npos); |
| 1349 | if (Qtx != ".qtx") |
| 1350 | return StringRef(); |
| 1351 | b = Name.rfind('/', a); |
| 1352 | if (b == Name.npos) |
| 1353 | Lib = Name.slice(0, a); |
| 1354 | else |
| 1355 | Lib = Name.slice(b+1, a); |
| 1356 | // There are library names of the form: QT.A.qtx so check for these. |
| 1357 | if (Lib.size() >= 3) { |
| 1358 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1359 | if (Dot == ".") |
| 1360 | Lib = Lib.slice(0, Lib.size()-2); |
| 1361 | } |
| 1362 | return Lib; |
| 1363 | } |
| 1364 | |
| 1365 | // getLibraryShortNameByIndex() is used to get the short name of the library |
| 1366 | // for an undefined symbol in a linked Mach-O binary that was linked with the |
| 1367 | // normal two-level namespace default (that is MH_TWOLEVEL in the header). |
| 1368 | // It is passed the index (0 - based) of the library as translated from |
| 1369 | // GET_LIBRARY_ORDINAL (1 - based). |
| 1370 | error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index, |
| 1371 | StringRef &Res) { |
| 1372 | if (Index >= Libraries.size()) |
| 1373 | return object_error::parse_failed; |
| 1374 | |
| 1375 | MachO::dylib_command D = |
| 1376 | getStruct<MachO::dylib_command>(this, Libraries[Index]); |
| 1377 | if (D.dylib.name >= D.cmdsize) |
| 1378 | return object_error::parse_failed; |
| 1379 | |
| 1380 | // If the cache of LibrariesShortNames is not built up do that first for |
| 1381 | // all the Libraries. |
| 1382 | if (LibrariesShortNames.size() == 0) { |
| 1383 | for (unsigned i = 0; i < Libraries.size(); i++) { |
| 1384 | MachO::dylib_command D = |
| 1385 | getStruct<MachO::dylib_command>(this, Libraries[i]); |
| 1386 | if (D.dylib.name >= D.cmdsize) { |
| 1387 | LibrariesShortNames.push_back(StringRef()); |
| 1388 | continue; |
| 1389 | } |
| 1390 | char *P = (char *)(Libraries[i]) + D.dylib.name; |
| 1391 | StringRef Name = StringRef(P); |
| 1392 | StringRef Suffix; |
| 1393 | bool isFramework; |
| 1394 | StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix); |
| 1395 | if (shortName == StringRef()) |
| 1396 | LibrariesShortNames.push_back(Name); |
| 1397 | else |
| 1398 | LibrariesShortNames.push_back(shortName); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | Res = LibrariesShortNames[Index]; |
| 1403 | return object_error::success; |
| 1404 | } |
| 1405 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1406 | basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const { |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1407 | return getSymbolByIndex(0); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1410 | basic_symbol_iterator MachOObjectFile::symbol_end_impl() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1411 | DataRefImpl DRI; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1412 | if (!SymtabLoadCmd) |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1413 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1414 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1415 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1416 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1417 | sizeof(MachO::nlist_64) : |
| 1418 | sizeof(MachO::nlist); |
| 1419 | unsigned Offset = Symtab.symoff + |
| 1420 | Symtab.nsyms * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1421 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1422 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1425 | basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const { |
| 1426 | DataRefImpl DRI; |
| 1427 | if (!SymtabLoadCmd) |
| 1428 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1429 | |
| 1430 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1431 | assert(Index < Symtab.nsyms && "Requested symbol index is out of range."); |
| 1432 | unsigned SymbolTableEntrySize = |
| 1433 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 1434 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); |
| 1435 | DRI.p += Index * SymbolTableEntrySize; |
| 1436 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1437 | } |
| 1438 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1439 | section_iterator MachOObjectFile::section_begin() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1440 | DataRefImpl DRI; |
| 1441 | return section_iterator(SectionRef(DRI, this)); |
| 1442 | } |
| 1443 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1444 | section_iterator MachOObjectFile::section_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1445 | DataRefImpl DRI; |
| 1446 | DRI.d.a = Sections.size(); |
| 1447 | return section_iterator(SectionRef(DRI, this)); |
| 1448 | } |
| 1449 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1450 | library_iterator MachOObjectFile::needed_library_begin() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1451 | // TODO: implement |
| 1452 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1453 | } |
| 1454 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1455 | library_iterator MachOObjectFile::needed_library_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1456 | // TODO: implement |
| 1457 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1458 | } |
| 1459 | |
| 1460 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 1461 | return is64Bit() ? 8 : 4; |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1464 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1465 | unsigned CPUType = getCPUType(this); |
| 1466 | if (!is64Bit()) { |
| 1467 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1468 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1469 | return "Mach-O 32-bit i386"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1470 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1471 | return "Mach-O arm"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1472 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1473 | return "Mach-O 32-bit ppc"; |
| 1474 | default: |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1475 | assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 && |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1476 | "64-bit object file when we're not 64-bit?"); |
| 1477 | return "Mach-O 32-bit unknown"; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | // Make sure the cpu type has the correct mask. |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1482 | assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) |
| 1483 | == llvm::MachO::CPU_ARCH_ABI64 && |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1484 | "32-bit object file when we're 64-bit?"); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1485 | |
| 1486 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1487 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1488 | return "Mach-O 64-bit x86-64"; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1489 | case llvm::MachO::CPU_TYPE_ARM64: |
| 1490 | return "Mach-O arm64"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1491 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1492 | return "Mach-O 64-bit ppc64"; |
| 1493 | default: |
| 1494 | return "Mach-O 64-bit unknown"; |
| 1495 | } |
| 1496 | } |
| 1497 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1498 | Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) { |
| 1499 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1500 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1501 | return Triple::x86; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1502 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1503 | return Triple::x86_64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1504 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1505 | return Triple::arm; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1506 | case llvm::MachO::CPU_TYPE_ARM64: |
| 1507 | return Triple::arm64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1508 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1509 | return Triple::ppc; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1510 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1511 | return Triple::ppc64; |
| 1512 | default: |
| 1513 | return Triple::UnknownArch; |
| 1514 | } |
| 1515 | } |
| 1516 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1517 | unsigned MachOObjectFile::getArch() const { |
| 1518 | return getArch(getCPUType(this)); |
| 1519 | } |
| 1520 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1521 | StringRef MachOObjectFile::getLoadName() const { |
| 1522 | // TODO: Implement |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 1523 | report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1526 | relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1527 | DataRefImpl DRI; |
| 1528 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1529 | return section_rel_begin(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1532 | relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1533 | DataRefImpl DRI; |
| 1534 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1535 | return section_rel_end(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1538 | dice_iterator MachOObjectFile::begin_dices() const { |
| 1539 | DataRefImpl DRI; |
| 1540 | if (!DataInCodeLoadCmd) |
| 1541 | return dice_iterator(DiceRef(DRI, this)); |
| 1542 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1543 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 1544 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1545 | return dice_iterator(DiceRef(DRI, this)); |
| 1546 | } |
| 1547 | |
| 1548 | dice_iterator MachOObjectFile::end_dices() const { |
| 1549 | DataRefImpl DRI; |
| 1550 | if (!DataInCodeLoadCmd) |
| 1551 | return dice_iterator(DiceRef(DRI, this)); |
| 1552 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1553 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 1554 | unsigned Offset = DicLC.dataoff + DicLC.datasize; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1555 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 1556 | return dice_iterator(DiceRef(DRI, this)); |
| 1557 | } |
| 1558 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1559 | StringRef |
| 1560 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 1561 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 1562 | return parseSegmentOrSectionName(Raw.data()); |
| 1563 | } |
| 1564 | |
| 1565 | ArrayRef<char> |
| 1566 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1567 | const section_base *Base = |
| 1568 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
| 1569 | return ArrayRef<char>(Base->sectname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | ArrayRef<char> |
| 1573 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1574 | const section_base *Base = |
| 1575 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
| 1576 | return ArrayRef<char>(Base->segname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | bool |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1580 | MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1581 | const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1582 | if (getCPUType(this) == MachO::CPU_TYPE_X86_64) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1583 | return false; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1584 | return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1587 | unsigned MachOObjectFile::getPlainRelocationSymbolNum( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1588 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1589 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1590 | return RE.r_word1 & 0xffffff; |
| 1591 | return RE.r_word1 >> 8; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1594 | bool MachOObjectFile::getPlainRelocationExternal( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1595 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1596 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1597 | return (RE.r_word1 >> 27) & 1; |
| 1598 | return (RE.r_word1 >> 4) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1601 | bool MachOObjectFile::getScatteredRelocationScattered( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1602 | const MachO::any_relocation_info &RE) const { |
| 1603 | return RE.r_word0 >> 31; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1606 | uint32_t MachOObjectFile::getScatteredRelocationValue( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1607 | const MachO::any_relocation_info &RE) const { |
| 1608 | return RE.r_word1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1611 | unsigned MachOObjectFile::getAnyRelocationAddress( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1612 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1613 | if (isRelocationScattered(RE)) |
| 1614 | return getScatteredRelocationAddress(RE); |
| 1615 | return getPlainRelocationAddress(RE); |
| 1616 | } |
| 1617 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1618 | unsigned MachOObjectFile::getAnyRelocationPCRel( |
| 1619 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1620 | if (isRelocationScattered(RE)) |
| 1621 | return getScatteredRelocationPCRel(this, RE); |
| 1622 | return getPlainRelocationPCRel(this, RE); |
| 1623 | } |
| 1624 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 1625 | unsigned MachOObjectFile::getAnyRelocationLength( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1626 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1627 | if (isRelocationScattered(RE)) |
| 1628 | return getScatteredRelocationLength(RE); |
| 1629 | return getPlainRelocationLength(this, RE); |
| 1630 | } |
| 1631 | |
| 1632 | unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1633 | MachOObjectFile::getAnyRelocationType( |
| 1634 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1635 | if (isRelocationScattered(RE)) |
| 1636 | return getScatteredRelocationType(RE); |
| 1637 | return getPlainRelocationType(this, RE); |
| 1638 | } |
| 1639 | |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 1640 | SectionRef |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1641 | MachOObjectFile::getRelocationSection( |
| 1642 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 1643 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1644 | return *section_end(); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 1645 | unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1; |
| 1646 | DataRefImpl DRI; |
| 1647 | DRI.d.a = SecNum; |
| 1648 | return SectionRef(DRI, this); |
| 1649 | } |
| 1650 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1651 | MachOObjectFile::LoadCommandInfo |
| 1652 | MachOObjectFile::getFirstLoadCommandInfo() const { |
| 1653 | MachOObjectFile::LoadCommandInfo Load; |
| 1654 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1655 | unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) : |
| 1656 | sizeof(MachO::mach_header); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1657 | Load.Ptr = getPtr(this, HeaderSize); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1658 | Load.C = getStruct<MachO::load_command>(this, Load.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1659 | return Load; |
| 1660 | } |
| 1661 | |
| 1662 | MachOObjectFile::LoadCommandInfo |
| 1663 | MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const { |
| 1664 | MachOObjectFile::LoadCommandInfo Next; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1665 | Next.Ptr = L.Ptr + L.C.cmdsize; |
| 1666 | Next.C = getStruct<MachO::load_command>(this, Next.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1667 | return Next; |
| 1668 | } |
| 1669 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1670 | MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { |
| 1671 | return getStruct<MachO::section>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1674 | MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
| 1675 | return getStruct<MachO::section_64>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1678 | MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1679 | unsigned Index) const { |
| 1680 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1681 | return getStruct<MachO::section>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1684 | MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 1685 | unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1686 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1687 | return getStruct<MachO::section_64>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1690 | MachO::nlist |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1691 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1692 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1693 | return getStruct<MachO::nlist>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1696 | MachO::nlist_64 |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1697 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1698 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1699 | return getStruct<MachO::nlist_64>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1702 | MachO::linkedit_data_command |
| 1703 | MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { |
| 1704 | return getStruct<MachO::linkedit_data_command>(this, L.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1707 | MachO::segment_command |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1708 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1709 | return getStruct<MachO::segment_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1712 | MachO::segment_command_64 |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1713 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1714 | return getStruct<MachO::segment_command_64>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1717 | MachO::linker_options_command |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1718 | MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1719 | return getStruct<MachO::linker_options_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 1722 | MachO::version_min_command |
| 1723 | MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { |
| 1724 | return getStruct<MachO::version_min_command>(this, L.Ptr); |
| 1725 | } |
| 1726 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1727 | MachO::any_relocation_info |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1728 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1729 | DataRefImpl Sec; |
| 1730 | Sec.d.a = Rel.d.a; |
| 1731 | uint32_t Offset; |
| 1732 | if (is64Bit()) { |
| 1733 | MachO::section_64 Sect = getSection64(Sec); |
| 1734 | Offset = Sect.reloff; |
| 1735 | } else { |
| 1736 | MachO::section Sect = getSection(Sec); |
| 1737 | Offset = Sect.reloff; |
| 1738 | } |
| 1739 | |
| 1740 | auto P = reinterpret_cast<const MachO::any_relocation_info *>( |
| 1741 | getPtr(this, Offset)) + Rel.d.b; |
| 1742 | return getStruct<MachO::any_relocation_info>( |
| 1743 | this, reinterpret_cast<const char *>(P)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1746 | MachO::data_in_code_entry |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1747 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 1748 | const char *P = reinterpret_cast<const char *>(Rel.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1749 | return getStruct<MachO::data_in_code_entry>(this, P); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1752 | MachO::mach_header MachOObjectFile::getHeader() const { |
| 1753 | return getStruct<MachO::mach_header>(this, getPtr(this, 0)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1756 | MachO::mach_header_64 MachOObjectFile::getHeader64() const { |
| 1757 | return getStruct<MachO::mach_header_64>(this, getPtr(this, 0)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1760 | uint32_t MachOObjectFile::getIndirectSymbolTableEntry( |
| 1761 | const MachO::dysymtab_command &DLC, |
| 1762 | unsigned Index) const { |
| 1763 | uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); |
| 1764 | return getStruct<uint32_t>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1767 | MachO::data_in_code_entry |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1768 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 1769 | unsigned Index) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1770 | uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); |
| 1771 | return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1774 | MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { |
| 1775 | return getStruct<MachO::symtab_command>(this, SymtabLoadCmd); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1778 | MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { |
| 1779 | return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1782 | MachO::linkedit_data_command |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1783 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 1784 | if (DataInCodeLoadCmd) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1785 | return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1786 | |
| 1787 | // If there is no DataInCodeLoadCmd return a load command with zero'ed fields. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1788 | MachO::linkedit_data_command Cmd; |
| 1789 | Cmd.cmd = MachO::LC_DATA_IN_CODE; |
| 1790 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 1791 | Cmd.dataoff = 0; |
| 1792 | Cmd.datasize = 0; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1793 | return Cmd; |
| 1794 | } |
| 1795 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1796 | StringRef MachOObjectFile::getStringTableData() const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1797 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 1798 | return getData().substr(S.stroff, S.strsize); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1801 | bool MachOObjectFile::is64Bit() const { |
| 1802 | return getType() == getMachOType(false, true) || |
| 1803 | getType() == getMachOType(true, true); |
| 1804 | } |
| 1805 | |
| 1806 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 1807 | SmallVectorImpl<uint64_t> &Out) const { |
| 1808 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 1809 | |
| 1810 | uint32_t offset = Index; |
| 1811 | uint64_t data = 0; |
| 1812 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 1813 | data += delta; |
| 1814 | Out.push_back(data); |
| 1815 | } |
| 1816 | } |
| 1817 | |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 1818 | ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer, |
| 1819 | bool BufferOwned) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1820 | StringRef Magic = Buffer->getBuffer().slice(0, 4); |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 1821 | error_code EC; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1822 | std::unique_ptr<MachOObjectFile> Ret; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1823 | if (Magic == "\xFE\xED\xFA\xCE") |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 1824 | Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1825 | else if (Magic == "\xCE\xFA\xED\xFE") |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 1826 | Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1827 | else if (Magic == "\xFE\xED\xFA\xCF") |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 1828 | Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1829 | else if (Magic == "\xCF\xFA\xED\xFE") |
Rafael Espindola | afcc3df | 2014-01-24 21:32:21 +0000 | [diff] [blame] | 1830 | Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned)); |
Benjamin Kramer | 097e09a | 2013-08-03 22:16:37 +0000 | [diff] [blame] | 1831 | else { |
| 1832 | delete Buffer; |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 1833 | return object_error::parse_failed; |
Benjamin Kramer | 097e09a | 2013-08-03 22:16:37 +0000 | [diff] [blame] | 1834 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1835 | |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 1836 | if (EC) |
| 1837 | return EC; |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 1838 | return Ret.release(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Owen Anderson | 27c579d | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 1841 | } // end namespace object |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1842 | } // end namespace llvm |