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" |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Rafael Espindola | 421305a | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DataExtractor.h" |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Owen Anderson | bc14bd3 | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Host.h" |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LEB128.h" |
| 24 | #include "llvm/Support/MachO.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Jakub Staszak | 84a0ae7 | 2013-08-21 01:20:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 27 | #include <cctype> |
| 28 | #include <cstring> |
| 29 | #include <limits> |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 30 | #include <list> |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | using namespace object; |
| 34 | |
Artyom Skrobov | 7d602f7 | 2014-07-20 12:08:28 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | struct section_base { |
| 37 | char sectname[16]; |
| 38 | char segname[16]; |
| 39 | }; |
| 40 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 41 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 42 | static Error |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 43 | malformedError(Twine Msg) { |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 44 | std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")"; |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 45 | return make_error<GenericBinaryError>(std::move(StringMsg), |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 46 | object_error::parse_failed); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 49 | // FIXME: Replace all uses of this function with getStructOrErr. |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 50 | template <typename T> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 51 | static T getStruct(const MachOObjectFile &O, const char *P) { |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 52 | // Don't read before the beginning or past the end of the file |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 53 | if (P < O.getData().begin() || P + sizeof(T) > O.getData().end()) |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 54 | report_fatal_error("Malformed MachO file."); |
| 55 | |
Rafael Espindola | 3cdeb17 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 56 | T Cmd; |
| 57 | memcpy(&Cmd, P, sizeof(T)); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 58 | if (O.isLittleEndian() != sys::IsLittleEndianHost) |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 59 | MachO::swapStruct(Cmd); |
Rafael Espindola | 3cdeb17 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 60 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 63 | template <typename T> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 64 | static Expected<T> getStructOrErr(const MachOObjectFile &O, const char *P) { |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 65 | // Don't read before the beginning or past the end of the file |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 66 | if (P < O.getData().begin() || P + sizeof(T) > O.getData().end()) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 67 | return malformedError("Structure read out-of-range"); |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 68 | |
| 69 | T Cmd; |
| 70 | memcpy(&Cmd, P, sizeof(T)); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 71 | if (O.isLittleEndian() != sys::IsLittleEndianHost) |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 72 | MachO::swapStruct(Cmd); |
| 73 | return Cmd; |
| 74 | } |
| 75 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 76 | static const char * |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 77 | getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L, |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 78 | unsigned Sec) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 79 | uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); |
| 80 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 81 | bool Is64 = O.is64Bit(); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 82 | unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) : |
| 83 | sizeof(MachO::segment_command); |
| 84 | unsigned SectionSize = Is64 ? sizeof(MachO::section_64) : |
| 85 | sizeof(MachO::section); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 86 | |
| 87 | uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 88 | return reinterpret_cast<const char*>(SectionAddr); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 91 | static const char *getPtr(const MachOObjectFile &O, size_t Offset) { |
| 92 | return O.getData().substr(Offset, 1).data(); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 95 | static MachO::nlist_base |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 96 | getSymbolTableEntryBase(const MachOObjectFile &O, DataRefImpl DRI) { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 97 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 98 | return getStruct<MachO::nlist_base>(O, P); |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 101 | static StringRef parseSegmentOrSectionName(const char *P) { |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 102 | if (P[15] == 0) |
| 103 | // Null terminated. |
| 104 | return P; |
| 105 | // Not null terminated, so this is a 16 char string. |
| 106 | return StringRef(P, 16); |
| 107 | } |
| 108 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 109 | // Helper to advance a section or symbol iterator multiple increments at a time. |
| 110 | template<class T> |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 111 | static void advance(T &it, size_t Val) { |
| 112 | while (Val--) |
| 113 | ++it; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 116 | static unsigned getCPUType(const MachOObjectFile &O) { |
| 117 | return O.getHeader().cputype; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 120 | static uint32_t |
| 121 | getPlainRelocationAddress(const MachO::any_relocation_info &RE) { |
| 122 | return RE.r_word0; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 126 | getScatteredRelocationAddress(const MachO::any_relocation_info &RE) { |
| 127 | return RE.r_word0 & 0xffffff; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 130 | static bool getPlainRelocationPCRel(const MachOObjectFile &O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 131 | const MachO::any_relocation_info &RE) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 132 | if (O.isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 133 | return (RE.r_word1 >> 24) & 1; |
| 134 | return (RE.r_word1 >> 7) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static bool |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 138 | getScatteredRelocationPCRel(const MachO::any_relocation_info &RE) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 139 | return (RE.r_word0 >> 30) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 142 | static unsigned getPlainRelocationLength(const MachOObjectFile &O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 143 | const MachO::any_relocation_info &RE) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 144 | if (O.isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 145 | return (RE.r_word1 >> 25) & 3; |
| 146 | return (RE.r_word1 >> 5) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 150 | getScatteredRelocationLength(const MachO::any_relocation_info &RE) { |
| 151 | return (RE.r_word0 >> 28) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 154 | static unsigned getPlainRelocationType(const MachOObjectFile &O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 155 | const MachO::any_relocation_info &RE) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 156 | if (O.isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 157 | return RE.r_word1 >> 28; |
| 158 | return RE.r_word1 & 0xf; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 161 | static uint32_t getSectionFlags(const MachOObjectFile &O, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 162 | DataRefImpl Sec) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 163 | if (O.is64Bit()) { |
| 164 | MachO::section_64 Sect = O.getSection64(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 165 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 166 | } |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 167 | MachO::section Sect = O.getSection(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 168 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 171 | static Expected<MachOObjectFile::LoadCommandInfo> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 172 | getLoadCommandInfo(const MachOObjectFile &Obj, const char *Ptr, |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 173 | uint32_t LoadCommandIndex) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 174 | if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) { |
| 175 | if (CmdOrErr->cmdsize < 8) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 176 | return malformedError("load command " + Twine(LoadCommandIndex) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 177 | " with size less than 8 bytes"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 178 | return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr}); |
| 179 | } else |
| 180 | return CmdOrErr.takeError(); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 183 | static Expected<MachOObjectFile::LoadCommandInfo> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 184 | getFirstLoadCommandInfo(const MachOObjectFile &Obj) { |
| 185 | unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64) |
| 186 | : sizeof(MachO::mach_header); |
| 187 | if (sizeof(MachO::load_command) > Obj.getHeader().sizeofcmds) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 188 | return malformedError("load command 0 extends past the end all load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 189 | "commands in the file"); |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 190 | return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 193 | static Expected<MachOObjectFile::LoadCommandInfo> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 194 | getNextLoadCommandInfo(const MachOObjectFile &Obj, uint32_t LoadCommandIndex, |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 195 | const MachOObjectFile::LoadCommandInfo &L) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 196 | unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64) |
| 197 | : sizeof(MachO::mach_header); |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 198 | if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) > |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 199 | Obj.getData().data() + HeaderSize + Obj.getHeader().sizeofcmds) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 200 | return malformedError("load command " + Twine(LoadCommandIndex + 1) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 201 | " extends past the end all load commands in the file"); |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 202 | return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 205 | template <typename T> |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 206 | static void parseHeader(const MachOObjectFile &Obj, T &Header, |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 207 | Error &Err) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 208 | if (sizeof(T) > Obj.getData().size()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 209 | Err = malformedError("the mach header extends past the end of the " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 210 | "file"); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 213 | if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0))) |
| 214 | Header = *HeaderOrErr; |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 215 | else |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 216 | Err = HeaderOrErr.takeError(); |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 219 | // This is used to check for overlapping of Mach-O elements. |
| 220 | struct MachOElement { |
| 221 | uint64_t Offset; |
| 222 | uint64_t Size; |
| 223 | const char *Name; |
| 224 | }; |
| 225 | |
| 226 | static Error checkOverlappingElement(std::list<MachOElement> &Elements, |
| 227 | uint64_t Offset, uint64_t Size, |
| 228 | const char *Name) { |
| 229 | if (Size == 0) |
| 230 | return Error::success(); |
| 231 | |
| 232 | for (auto it=Elements.begin() ; it != Elements.end(); ++it) { |
| 233 | auto E = *it; |
| 234 | if ((Offset >= E.Offset && Offset < E.Offset + E.Size) || |
| 235 | (Offset + Size > E.Offset && Offset + Size < E.Offset + E.Size) || |
| 236 | (Offset <= E.Offset && Offset + Size >= E.Offset + E.Size)) |
| 237 | return malformedError(Twine(Name) + " at offset " + Twine(Offset) + |
| 238 | " with a size of " + Twine(Size) + ", overlaps " + |
| 239 | E.Name + " at offset " + Twine(E.Offset) + " with " |
| 240 | "a size of " + Twine(E.Size)); |
| 241 | auto nt = it; |
| 242 | nt++; |
| 243 | if (nt != Elements.end()) { |
| 244 | auto N = *nt; |
| 245 | if (Offset + Size <= N.Offset) { |
| 246 | Elements.insert(nt, {Offset, Size, Name}); |
| 247 | return Error::success(); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | Elements.push_back({Offset, Size, Name}); |
| 252 | return Error::success(); |
| 253 | } |
| 254 | |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 255 | // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all |
| 256 | // sections to \param Sections, and optionally sets |
| 257 | // \param IsPageZeroSegment to true. |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 258 | template <typename Segment, typename Section> |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 259 | static Error parseSegmentLoadCommand( |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 260 | const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 261 | SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 262 | uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders, |
| 263 | std::list<MachOElement> &Elements) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 264 | const unsigned SegmentLoadSize = sizeof(Segment); |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 265 | if (Load.C.cmdsize < SegmentLoadSize) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 266 | return malformedError("load command " + Twine(LoadCommandIndex) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 267 | " " + CmdName + " cmdsize too small"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 268 | if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) { |
| 269 | Segment S = SegOrErr.get(); |
| 270 | const unsigned SectionSize = sizeof(Section); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 271 | uint64_t FileSize = Obj.getData().size(); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 272 | if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || |
| 273 | S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 274 | return malformedError("load command " + Twine(LoadCommandIndex) + |
NAKAMURA Takumi | 9d0b531 | 2016-08-22 00:58:47 +0000 | [diff] [blame] | 275 | " inconsistent cmdsize in " + CmdName + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 276 | " for the number of sections"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 277 | for (unsigned J = 0; J < S.nsects; ++J) { |
| 278 | const char *Sec = getSectionPtr(Obj, Load, J); |
| 279 | Sections.push_back(Sec); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 280 | Section s = getStruct<Section>(Obj, Sec); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 281 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 282 | Obj.getHeader().filetype != MachO::MH_DSYM && |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 283 | s.flags != MachO::S_ZEROFILL && |
| 284 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 285 | s.offset > FileSize) |
| 286 | return malformedError("offset field of section " + Twine(J) + " in " + |
| 287 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 288 | " extends past the end of the file"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 289 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 290 | Obj.getHeader().filetype != MachO::MH_DSYM && |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 291 | s.flags != MachO::S_ZEROFILL && |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 292 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 && |
| 293 | s.offset < SizeOfHeaders && s.size != 0) |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 294 | return malformedError("offset field of section " + Twine(J) + " in " + |
| 295 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 296 | " not past the headers of the file"); |
| 297 | uint64_t BigSize = s.offset; |
| 298 | BigSize += s.size; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 299 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 300 | Obj.getHeader().filetype != MachO::MH_DSYM && |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 301 | s.flags != MachO::S_ZEROFILL && |
| 302 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 303 | BigSize > FileSize) |
| 304 | return malformedError("offset field plus size field of section " + |
| 305 | Twine(J) + " in " + CmdName + " command " + |
| 306 | Twine(LoadCommandIndex) + |
| 307 | " extends past the end of the file"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 308 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 309 | Obj.getHeader().filetype != MachO::MH_DSYM && |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 310 | s.flags != MachO::S_ZEROFILL && |
| 311 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 312 | s.size > S.filesize) |
| 313 | return malformedError("size field of section " + |
| 314 | Twine(J) + " in " + CmdName + " command " + |
| 315 | Twine(LoadCommandIndex) + |
| 316 | " greater than the segment"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 317 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 318 | Obj.getHeader().filetype != MachO::MH_DSYM && s.size != 0 && |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 319 | s.addr < S.vmaddr) |
| 320 | return malformedError("addr field of section " + Twine(J) + " in " + |
| 321 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 322 | " less than the segment's vmaddr"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 323 | BigSize = s.addr; |
| 324 | BigSize += s.size; |
| 325 | uint64_t BigEnd = S.vmaddr; |
| 326 | BigEnd += S.vmsize; |
| 327 | if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd) |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 328 | return malformedError("addr field plus size of section " + Twine(J) + |
| 329 | " in " + CmdName + " command " + |
| 330 | Twine(LoadCommandIndex) + |
| 331 | " greater than than " |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 332 | "the segment's vmaddr plus vmsize"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 333 | if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 334 | Obj.getHeader().filetype != MachO::MH_DSYM && |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 335 | s.flags != MachO::S_ZEROFILL && |
| 336 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL) |
| 337 | if (Error Err = checkOverlappingElement(Elements, s.offset, s.size, |
| 338 | "section contents")) |
| 339 | return Err; |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 340 | if (s.reloff > FileSize) |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 341 | return malformedError("reloff field of section " + Twine(J) + " in " + |
| 342 | CmdName + " command " + Twine(LoadCommandIndex) + |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 343 | " extends past the end of the file"); |
| 344 | BigSize = s.nreloc; |
| 345 | BigSize *= sizeof(struct MachO::relocation_info); |
| 346 | BigSize += s.reloff; |
| 347 | if (BigSize > FileSize) |
| 348 | return malformedError("reloff field plus nreloc field times sizeof(" |
| 349 | "struct relocation_info) of section " + |
| 350 | Twine(J) + " in " + CmdName + " command " + |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 351 | Twine(LoadCommandIndex) + |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 352 | " extends past the end of the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 353 | if (Error Err = checkOverlappingElement(Elements, s.reloff, s.nreloc * |
| 354 | sizeof(struct |
| 355 | MachO::relocation_info), |
| 356 | "section relocation entries")) |
| 357 | return Err; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 358 | } |
Kevin Enderby | 600fb3f | 2016-08-05 18:19:40 +0000 | [diff] [blame] | 359 | if (S.fileoff > FileSize) |
| 360 | return malformedError("load command " + Twine(LoadCommandIndex) + |
NAKAMURA Takumi | 9d0b531 | 2016-08-22 00:58:47 +0000 | [diff] [blame] | 361 | " fileoff field in " + CmdName + |
Kevin Enderby | 600fb3f | 2016-08-05 18:19:40 +0000 | [diff] [blame] | 362 | " extends past the end of the file"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 363 | uint64_t BigSize = S.fileoff; |
| 364 | BigSize += S.filesize; |
| 365 | if (BigSize > FileSize) |
| 366 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 367 | " fileoff field plus filesize field in " + |
| 368 | CmdName + " extends past the end of the file"); |
| 369 | if (S.vmsize != 0 && S.filesize > S.vmsize) |
| 370 | return malformedError("load command " + Twine(LoadCommandIndex) + |
Kevin Enderby | 86d8bd1 | 2017-02-07 21:20:44 +0000 | [diff] [blame] | 371 | " filesize field in " + CmdName + |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 372 | " greater than vmsize field"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 373 | IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname); |
| 374 | } else |
| 375 | return SegOrErr.takeError(); |
| 376 | |
| 377 | return Error::success(); |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 380 | static Error checkSymtabCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 381 | const MachOObjectFile::LoadCommandInfo &Load, |
| 382 | uint32_t LoadCommandIndex, |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 383 | const char **SymtabLoadCmd, |
| 384 | std::list<MachOElement> &Elements) { |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 385 | if (Load.C.cmdsize < sizeof(MachO::symtab_command)) |
| 386 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 387 | " LC_SYMTAB cmdsize too small"); |
| 388 | if (*SymtabLoadCmd != nullptr) |
| 389 | return malformedError("more than one LC_SYMTAB command"); |
| 390 | MachO::symtab_command Symtab = |
| 391 | getStruct<MachO::symtab_command>(Obj, Load.Ptr); |
| 392 | if (Symtab.cmdsize != sizeof(MachO::symtab_command)) |
| 393 | return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) + |
| 394 | " has incorrect cmdsize"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 395 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 396 | if (Symtab.symoff > FileSize) |
| 397 | return malformedError("symoff field of LC_SYMTAB command " + |
| 398 | Twine(LoadCommandIndex) + " extends past the end " |
| 399 | "of the file"); |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 400 | uint64_t SymtabSize = Symtab.nsyms; |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 401 | const char *struct_nlist_name; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 402 | if (Obj.is64Bit()) { |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 403 | SymtabSize *= sizeof(MachO::nlist_64); |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 404 | struct_nlist_name = "struct nlist_64"; |
| 405 | } else { |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 406 | SymtabSize *= sizeof(MachO::nlist); |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 407 | struct_nlist_name = "struct nlist"; |
| 408 | } |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 409 | uint64_t BigSize = SymtabSize; |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 410 | BigSize += Symtab.symoff; |
| 411 | if (BigSize > FileSize) |
| 412 | return malformedError("symoff field plus nsyms field times sizeof(" + |
| 413 | Twine(struct_nlist_name) + ") of LC_SYMTAB command " + |
| 414 | Twine(LoadCommandIndex) + " extends past the end " |
| 415 | "of the file"); |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 416 | if (Error Err = checkOverlappingElement(Elements, Symtab.symoff, SymtabSize, |
| 417 | "symbol table")) |
| 418 | return Err; |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 419 | if (Symtab.stroff > FileSize) |
| 420 | return malformedError("stroff field of LC_SYMTAB command " + |
| 421 | Twine(LoadCommandIndex) + " extends past the end " |
| 422 | "of the file"); |
| 423 | BigSize = Symtab.stroff; |
| 424 | BigSize += Symtab.strsize; |
| 425 | if (BigSize > FileSize) |
| 426 | return malformedError("stroff field plus strsize field of LC_SYMTAB " |
| 427 | "command " + Twine(LoadCommandIndex) + " extends " |
| 428 | "past the end of the file"); |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 429 | if (Error Err = checkOverlappingElement(Elements, Symtab.stroff, |
| 430 | Symtab.strsize, "string table")) |
| 431 | return Err; |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 432 | *SymtabLoadCmd = Load.Ptr; |
| 433 | return Error::success(); |
| 434 | } |
| 435 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 436 | static Error checkDysymtabCommand(const MachOObjectFile &Obj, |
| 437 | const MachOObjectFile::LoadCommandInfo &Load, |
| 438 | uint32_t LoadCommandIndex, |
| 439 | const char **DysymtabLoadCmd, |
| 440 | std::list<MachOElement> &Elements) { |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 441 | if (Load.C.cmdsize < sizeof(MachO::dysymtab_command)) |
| 442 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 443 | " LC_DYSYMTAB cmdsize too small"); |
| 444 | if (*DysymtabLoadCmd != nullptr) |
| 445 | return malformedError("more than one LC_DYSYMTAB command"); |
| 446 | MachO::dysymtab_command Dysymtab = |
| 447 | getStruct<MachO::dysymtab_command>(Obj, Load.Ptr); |
| 448 | if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command)) |
| 449 | return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) + |
| 450 | " has incorrect cmdsize"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 451 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 452 | if (Dysymtab.tocoff > FileSize) |
| 453 | return malformedError("tocoff field of LC_DYSYMTAB command " + |
| 454 | Twine(LoadCommandIndex) + " extends past the end of " |
| 455 | "the file"); |
| 456 | uint64_t BigSize = Dysymtab.ntoc; |
| 457 | BigSize *= sizeof(MachO::dylib_table_of_contents); |
| 458 | BigSize += Dysymtab.tocoff; |
| 459 | if (BigSize > FileSize) |
| 460 | return malformedError("tocoff field plus ntoc field times sizeof(struct " |
| 461 | "dylib_table_of_contents) of LC_DYSYMTAB command " + |
| 462 | Twine(LoadCommandIndex) + " extends past the end of " |
| 463 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 464 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.tocoff, |
| 465 | Dysymtab.ntoc * sizeof(struct |
| 466 | MachO::dylib_table_of_contents), |
| 467 | "table of contents")) |
| 468 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 469 | if (Dysymtab.modtaboff > FileSize) |
| 470 | return malformedError("modtaboff field of LC_DYSYMTAB command " + |
| 471 | Twine(LoadCommandIndex) + " extends past the end of " |
| 472 | "the file"); |
| 473 | BigSize = Dysymtab.nmodtab; |
| 474 | const char *struct_dylib_module_name; |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 475 | uint64_t sizeof_modtab; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 476 | if (Obj.is64Bit()) { |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 477 | sizeof_modtab = sizeof(MachO::dylib_module_64); |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 478 | struct_dylib_module_name = "struct dylib_module_64"; |
| 479 | } else { |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 480 | sizeof_modtab = sizeof(MachO::dylib_module); |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 481 | struct_dylib_module_name = "struct dylib_module"; |
| 482 | } |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 483 | BigSize *= sizeof_modtab; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 484 | BigSize += Dysymtab.modtaboff; |
| 485 | if (BigSize > FileSize) |
| 486 | return malformedError("modtaboff field plus nmodtab field times sizeof(" + |
| 487 | Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB " |
| 488 | "command " + Twine(LoadCommandIndex) + " extends " |
| 489 | "past the end of the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 490 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.modtaboff, |
| 491 | Dysymtab.nmodtab * sizeof_modtab, |
| 492 | "module table")) |
| 493 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 494 | if (Dysymtab.extrefsymoff > FileSize) |
| 495 | return malformedError("extrefsymoff field of LC_DYSYMTAB command " + |
| 496 | Twine(LoadCommandIndex) + " extends past the end of " |
| 497 | "the file"); |
| 498 | BigSize = Dysymtab.nextrefsyms; |
| 499 | BigSize *= sizeof(MachO::dylib_reference); |
| 500 | BigSize += Dysymtab.extrefsymoff; |
| 501 | if (BigSize > FileSize) |
| 502 | return malformedError("extrefsymoff field plus nextrefsyms field times " |
| 503 | "sizeof(struct dylib_reference) of LC_DYSYMTAB " |
| 504 | "command " + Twine(LoadCommandIndex) + " extends " |
| 505 | "past the end of the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 506 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.extrefsymoff, |
| 507 | Dysymtab.nextrefsyms * |
| 508 | sizeof(MachO::dylib_reference), |
| 509 | "reference table")) |
| 510 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 511 | if (Dysymtab.indirectsymoff > FileSize) |
| 512 | return malformedError("indirectsymoff field of LC_DYSYMTAB command " + |
| 513 | Twine(LoadCommandIndex) + " extends past the end of " |
| 514 | "the file"); |
| 515 | BigSize = Dysymtab.nindirectsyms; |
| 516 | BigSize *= sizeof(uint32_t); |
| 517 | BigSize += Dysymtab.indirectsymoff; |
| 518 | if (BigSize > FileSize) |
| 519 | return malformedError("indirectsymoff field plus nindirectsyms field times " |
| 520 | "sizeof(uint32_t) of LC_DYSYMTAB command " + |
| 521 | Twine(LoadCommandIndex) + " extends past the end of " |
| 522 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 523 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.indirectsymoff, |
| 524 | Dysymtab.nindirectsyms * |
| 525 | sizeof(uint32_t), |
| 526 | "indirect table")) |
| 527 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 528 | if (Dysymtab.extreloff > FileSize) |
| 529 | return malformedError("extreloff field of LC_DYSYMTAB command " + |
| 530 | Twine(LoadCommandIndex) + " extends past the end of " |
| 531 | "the file"); |
| 532 | BigSize = Dysymtab.nextrel; |
| 533 | BigSize *= sizeof(MachO::relocation_info); |
| 534 | BigSize += Dysymtab.extreloff; |
| 535 | if (BigSize > FileSize) |
| 536 | return malformedError("extreloff field plus nextrel field times sizeof" |
| 537 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 538 | Twine(LoadCommandIndex) + " extends past the end of " |
| 539 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 540 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.extreloff, |
| 541 | Dysymtab.nextrel * |
| 542 | sizeof(MachO::relocation_info), |
| 543 | "external relocation table")) |
| 544 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 545 | if (Dysymtab.locreloff > FileSize) |
| 546 | return malformedError("locreloff field of LC_DYSYMTAB command " + |
| 547 | Twine(LoadCommandIndex) + " extends past the end of " |
| 548 | "the file"); |
| 549 | BigSize = Dysymtab.nlocrel; |
| 550 | BigSize *= sizeof(MachO::relocation_info); |
| 551 | BigSize += Dysymtab.locreloff; |
| 552 | if (BigSize > FileSize) |
| 553 | return malformedError("locreloff field plus nlocrel field times sizeof" |
| 554 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 555 | Twine(LoadCommandIndex) + " extends past the end of " |
| 556 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 557 | if (Error Err = checkOverlappingElement(Elements, Dysymtab.locreloff, |
| 558 | Dysymtab.nlocrel * |
| 559 | sizeof(MachO::relocation_info), |
| 560 | "local relocation table")) |
| 561 | return Err; |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 562 | *DysymtabLoadCmd = Load.Ptr; |
| 563 | return Error::success(); |
| 564 | } |
| 565 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 566 | static Error checkLinkeditDataCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 567 | const MachOObjectFile::LoadCommandInfo &Load, |
| 568 | uint32_t LoadCommandIndex, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 569 | const char **LoadCmd, const char *CmdName, |
| 570 | std::list<MachOElement> &Elements, |
| 571 | const char *ElementName) { |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 572 | if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command)) |
| 573 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 574 | CmdName + " cmdsize too small"); |
| 575 | if (*LoadCmd != nullptr) |
| 576 | return malformedError("more than one " + Twine(CmdName) + " command"); |
| 577 | MachO::linkedit_data_command LinkData = |
| 578 | getStruct<MachO::linkedit_data_command>(Obj, Load.Ptr); |
| 579 | if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command)) |
| 580 | return malformedError(Twine(CmdName) + " command " + |
| 581 | Twine(LoadCommandIndex) + " has incorrect cmdsize"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 582 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 583 | if (LinkData.dataoff > FileSize) |
| 584 | return malformedError("dataoff field of " + Twine(CmdName) + " command " + |
| 585 | Twine(LoadCommandIndex) + " extends past the end of " |
| 586 | "the file"); |
| 587 | uint64_t BigSize = LinkData.dataoff; |
| 588 | BigSize += LinkData.datasize; |
| 589 | if (BigSize > FileSize) |
| 590 | return malformedError("dataoff field plus datasize field of " + |
| 591 | Twine(CmdName) + " command " + |
| 592 | Twine(LoadCommandIndex) + " extends past the end of " |
| 593 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 594 | if (Error Err = checkOverlappingElement(Elements, LinkData.dataoff, |
| 595 | LinkData.datasize, ElementName)) |
| 596 | return Err; |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 597 | *LoadCmd = Load.Ptr; |
| 598 | return Error::success(); |
| 599 | } |
| 600 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 601 | static Error checkDyldInfoCommand(const MachOObjectFile &Obj, |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 602 | const MachOObjectFile::LoadCommandInfo &Load, |
| 603 | uint32_t LoadCommandIndex, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 604 | const char **LoadCmd, const char *CmdName, |
| 605 | std::list<MachOElement> &Elements) { |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 606 | if (Load.C.cmdsize < sizeof(MachO::dyld_info_command)) |
| 607 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 608 | CmdName + " cmdsize too small"); |
| 609 | if (*LoadCmd != nullptr) |
| 610 | return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY " |
| 611 | "command"); |
| 612 | MachO::dyld_info_command DyldInfo = |
| 613 | getStruct<MachO::dyld_info_command>(Obj, Load.Ptr); |
| 614 | if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command)) |
| 615 | return malformedError(Twine(CmdName) + " command " + |
| 616 | Twine(LoadCommandIndex) + " has incorrect cmdsize"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 617 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 618 | if (DyldInfo.rebase_off > FileSize) |
| 619 | return malformedError("rebase_off field of " + Twine(CmdName) + |
| 620 | " command " + Twine(LoadCommandIndex) + " extends " |
| 621 | "past the end of the file"); |
| 622 | uint64_t BigSize = DyldInfo.rebase_off; |
| 623 | BigSize += DyldInfo.rebase_size; |
| 624 | if (BigSize > FileSize) |
| 625 | return malformedError("rebase_off field plus rebase_size field of " + |
| 626 | Twine(CmdName) + " command " + |
| 627 | Twine(LoadCommandIndex) + " extends past the end of " |
| 628 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 629 | if (Error Err = checkOverlappingElement(Elements, DyldInfo.rebase_off, |
| 630 | DyldInfo.rebase_size, |
| 631 | "dyld rebase info")) |
| 632 | return Err; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 633 | if (DyldInfo.bind_off > FileSize) |
| 634 | return malformedError("bind_off field of " + Twine(CmdName) + |
| 635 | " command " + Twine(LoadCommandIndex) + " extends " |
| 636 | "past the end of the file"); |
| 637 | BigSize = DyldInfo.bind_off; |
| 638 | BigSize += DyldInfo.bind_size; |
| 639 | if (BigSize > FileSize) |
| 640 | return malformedError("bind_off field plus bind_size field of " + |
| 641 | Twine(CmdName) + " command " + |
| 642 | Twine(LoadCommandIndex) + " extends past the end of " |
| 643 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 644 | if (Error Err = checkOverlappingElement(Elements, DyldInfo.bind_off, |
| 645 | DyldInfo.bind_size, |
| 646 | "dyld bind info")) |
| 647 | return Err; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 648 | if (DyldInfo.weak_bind_off > FileSize) |
| 649 | return malformedError("weak_bind_off field of " + Twine(CmdName) + |
| 650 | " command " + Twine(LoadCommandIndex) + " extends " |
| 651 | "past the end of the file"); |
| 652 | BigSize = DyldInfo.weak_bind_off; |
| 653 | BigSize += DyldInfo.weak_bind_size; |
| 654 | if (BigSize > FileSize) |
| 655 | return malformedError("weak_bind_off field plus weak_bind_size field of " + |
| 656 | Twine(CmdName) + " command " + |
| 657 | Twine(LoadCommandIndex) + " extends past the end of " |
| 658 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 659 | if (Error Err = checkOverlappingElement(Elements, DyldInfo.weak_bind_off, |
| 660 | DyldInfo.weak_bind_size, |
| 661 | "dyld weak bind info")) |
| 662 | return Err; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 663 | if (DyldInfo.lazy_bind_off > FileSize) |
| 664 | return malformedError("lazy_bind_off field of " + Twine(CmdName) + |
| 665 | " command " + Twine(LoadCommandIndex) + " extends " |
| 666 | "past the end of the file"); |
| 667 | BigSize = DyldInfo.lazy_bind_off; |
| 668 | BigSize += DyldInfo.lazy_bind_size; |
| 669 | if (BigSize > FileSize) |
| 670 | return malformedError("lazy_bind_off field plus lazy_bind_size field of " + |
| 671 | Twine(CmdName) + " command " + |
| 672 | Twine(LoadCommandIndex) + " extends past the end of " |
| 673 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 674 | if (Error Err = checkOverlappingElement(Elements, DyldInfo.lazy_bind_off, |
| 675 | DyldInfo.lazy_bind_size, |
| 676 | "dyld lazy bind info")) |
| 677 | return Err; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 678 | if (DyldInfo.export_off > FileSize) |
| 679 | return malformedError("export_off field of " + Twine(CmdName) + |
| 680 | " command " + Twine(LoadCommandIndex) + " extends " |
| 681 | "past the end of the file"); |
| 682 | BigSize = DyldInfo.export_off; |
| 683 | BigSize += DyldInfo.export_size; |
| 684 | if (BigSize > FileSize) |
| 685 | return malformedError("export_off field plus export_size field of " + |
| 686 | Twine(CmdName) + " command " + |
| 687 | Twine(LoadCommandIndex) + " extends past the end of " |
| 688 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 689 | if (Error Err = checkOverlappingElement(Elements, DyldInfo.export_off, |
| 690 | DyldInfo.export_size, |
| 691 | "dyld export info")) |
| 692 | return Err; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 693 | *LoadCmd = Load.Ptr; |
| 694 | return Error::success(); |
| 695 | } |
| 696 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 697 | static Error checkDylibCommand(const MachOObjectFile &Obj, |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 698 | const MachOObjectFile::LoadCommandInfo &Load, |
| 699 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 700 | if (Load.C.cmdsize < sizeof(MachO::dylib_command)) |
| 701 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 702 | CmdName + " cmdsize too small"); |
| 703 | MachO::dylib_command D = getStruct<MachO::dylib_command>(Obj, Load.Ptr); |
| 704 | if (D.dylib.name < sizeof(MachO::dylib_command)) |
| 705 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 706 | CmdName + " name.offset field too small, not past " |
| 707 | "the end of the dylib_command struct"); |
| 708 | if (D.dylib.name >= D.cmdsize) |
| 709 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 710 | CmdName + " name.offset field extends past the end " |
| 711 | "of the load command"); |
| 712 | // Make sure there is a null between the starting offset of the name and |
| 713 | // the end of the load command. |
| 714 | uint32_t i; |
| 715 | const char *P = (const char *)Load.Ptr; |
| 716 | for (i = D.dylib.name; i < D.cmdsize; i++) |
| 717 | if (P[i] == '\0') |
| 718 | break; |
| 719 | if (i >= D.cmdsize) |
| 720 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 721 | CmdName + " library name extends past the end of the " |
| 722 | "load command"); |
| 723 | return Error::success(); |
| 724 | } |
| 725 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 726 | static Error checkDylibIdCommand(const MachOObjectFile &Obj, |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 727 | const MachOObjectFile::LoadCommandInfo &Load, |
| 728 | uint32_t LoadCommandIndex, |
| 729 | const char **LoadCmd) { |
| 730 | if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex, |
| 731 | "LC_ID_DYLIB")) |
| 732 | return Err; |
| 733 | if (*LoadCmd != nullptr) |
| 734 | return malformedError("more than one LC_ID_DYLIB command"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 735 | if (Obj.getHeader().filetype != MachO::MH_DYLIB && |
| 736 | Obj.getHeader().filetype != MachO::MH_DYLIB_STUB) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 737 | return malformedError("LC_ID_DYLIB load command in non-dynamic library " |
| 738 | "file type"); |
| 739 | *LoadCmd = Load.Ptr; |
| 740 | return Error::success(); |
| 741 | } |
| 742 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 743 | static Error checkDyldCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 744 | const MachOObjectFile::LoadCommandInfo &Load, |
| 745 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 746 | if (Load.C.cmdsize < sizeof(MachO::dylinker_command)) |
| 747 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 748 | CmdName + " cmdsize too small"); |
| 749 | MachO::dylinker_command D = getStruct<MachO::dylinker_command>(Obj, Load.Ptr); |
| 750 | if (D.name < sizeof(MachO::dylinker_command)) |
| 751 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 752 | CmdName + " name.offset field too small, not past " |
| 753 | "the end of the dylinker_command struct"); |
| 754 | if (D.name >= D.cmdsize) |
| 755 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 756 | CmdName + " name.offset field extends past the end " |
| 757 | "of the load command"); |
| 758 | // Make sure there is a null between the starting offset of the name and |
| 759 | // the end of the load command. |
| 760 | uint32_t i; |
| 761 | const char *P = (const char *)Load.Ptr; |
| 762 | for (i = D.name; i < D.cmdsize; i++) |
| 763 | if (P[i] == '\0') |
| 764 | break; |
| 765 | if (i >= D.cmdsize) |
| 766 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 767 | CmdName + " dyld name extends past the end of the " |
| 768 | "load command"); |
| 769 | return Error::success(); |
| 770 | } |
| 771 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 772 | static Error checkVersCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 773 | const MachOObjectFile::LoadCommandInfo &Load, |
| 774 | uint32_t LoadCommandIndex, |
| 775 | const char **LoadCmd, const char *CmdName) { |
| 776 | if (Load.C.cmdsize != sizeof(MachO::version_min_command)) |
| 777 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 778 | CmdName + " has incorrect cmdsize"); |
| 779 | if (*LoadCmd != nullptr) |
| 780 | return malformedError("more than one LC_VERSION_MIN_MACOSX, " |
| 781 | "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or " |
| 782 | "LC_VERSION_MIN_WATCHOS command"); |
| 783 | *LoadCmd = Load.Ptr; |
| 784 | return Error::success(); |
| 785 | } |
| 786 | |
Kevin Enderby | a4579c4 | 2017-01-19 17:36:31 +0000 | [diff] [blame] | 787 | static Error checkNoteCommand(const MachOObjectFile &Obj, |
| 788 | const MachOObjectFile::LoadCommandInfo &Load, |
| 789 | uint32_t LoadCommandIndex, |
| 790 | std::list<MachOElement> &Elements) { |
| 791 | if (Load.C.cmdsize != sizeof(MachO::note_command)) |
| 792 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 793 | " LC_NOTE has incorrect cmdsize"); |
| 794 | MachO::note_command Nt = getStruct<MachO::note_command>(Obj, Load.Ptr); |
| 795 | uint64_t FileSize = Obj.getData().size(); |
| 796 | if (Nt.offset > FileSize) |
| 797 | return malformedError("offset field of LC_NOTE command " + |
| 798 | Twine(LoadCommandIndex) + " extends " |
| 799 | "past the end of the file"); |
| 800 | uint64_t BigSize = Nt.offset; |
| 801 | BigSize += Nt.size; |
| 802 | if (BigSize > FileSize) |
| 803 | return malformedError("size field plus offset field of LC_NOTE command " + |
| 804 | Twine(LoadCommandIndex) + " extends past the end of " |
| 805 | "the file"); |
| 806 | if (Error Err = checkOverlappingElement(Elements, Nt.offset, Nt.size, |
| 807 | "LC_NOTE data")) |
| 808 | return Err; |
| 809 | return Error::success(); |
| 810 | } |
| 811 | |
Steven Wu | 5b54a42 | 2017-01-23 20:07:55 +0000 | [diff] [blame] | 812 | static Error |
| 813 | parseBuildVersionCommand(const MachOObjectFile &Obj, |
| 814 | const MachOObjectFile::LoadCommandInfo &Load, |
| 815 | SmallVectorImpl<const char*> &BuildTools, |
| 816 | uint32_t LoadCommandIndex) { |
| 817 | MachO::build_version_command BVC = |
| 818 | getStruct<MachO::build_version_command>(Obj, Load.Ptr); |
| 819 | if (Load.C.cmdsize != |
| 820 | sizeof(MachO::build_version_command) + |
| 821 | BVC.ntools * sizeof(MachO::build_tool_version)) |
| 822 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 823 | " LC_BUILD_VERSION_COMMAND has incorrect cmdsize"); |
| 824 | |
| 825 | auto Start = Load.Ptr + sizeof(MachO::build_version_command); |
| 826 | BuildTools.resize(BVC.ntools); |
| 827 | for (unsigned i = 0; i < BVC.ntools; ++i) |
| 828 | BuildTools[i] = Start + i * sizeof(MachO::build_tool_version); |
| 829 | |
| 830 | return Error::success(); |
| 831 | } |
| 832 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 833 | static Error checkRpathCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 76966bf | 2016-09-28 23:16:01 +0000 | [diff] [blame] | 834 | const MachOObjectFile::LoadCommandInfo &Load, |
| 835 | uint32_t LoadCommandIndex) { |
| 836 | if (Load.C.cmdsize < sizeof(MachO::rpath_command)) |
| 837 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 838 | " LC_RPATH cmdsize too small"); |
| 839 | MachO::rpath_command R = getStruct<MachO::rpath_command>(Obj, Load.Ptr); |
| 840 | if (R.path < sizeof(MachO::rpath_command)) |
| 841 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 842 | " LC_RPATH path.offset field too small, not past " |
| 843 | "the end of the rpath_command struct"); |
| 844 | if (R.path >= R.cmdsize) |
| 845 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 846 | " LC_RPATH path.offset field extends past the end " |
| 847 | "of the load command"); |
| 848 | // Make sure there is a null between the starting offset of the path and |
| 849 | // the end of the load command. |
| 850 | uint32_t i; |
| 851 | const char *P = (const char *)Load.Ptr; |
| 852 | for (i = R.path; i < R.cmdsize; i++) |
| 853 | if (P[i] == '\0') |
| 854 | break; |
| 855 | if (i >= R.cmdsize) |
| 856 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 857 | " LC_RPATH library name extends past the end of the " |
| 858 | "load command"); |
| 859 | return Error::success(); |
| 860 | } |
| 861 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 862 | static Error checkEncryptCommand(const MachOObjectFile &Obj, |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 863 | const MachOObjectFile::LoadCommandInfo &Load, |
| 864 | uint32_t LoadCommandIndex, |
| 865 | uint64_t cryptoff, uint64_t cryptsize, |
| 866 | const char **LoadCmd, const char *CmdName) { |
| 867 | if (*LoadCmd != nullptr) |
| 868 | return malformedError("more than one LC_ENCRYPTION_INFO and or " |
| 869 | "LC_ENCRYPTION_INFO_64 command"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 870 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 871 | if (cryptoff > FileSize) |
| 872 | return malformedError("cryptoff field of " + Twine(CmdName) + |
| 873 | " command " + Twine(LoadCommandIndex) + " extends " |
| 874 | "past the end of the file"); |
| 875 | uint64_t BigSize = cryptoff; |
| 876 | BigSize += cryptsize; |
| 877 | if (BigSize > FileSize) |
| 878 | return malformedError("cryptoff field plus cryptsize field of " + |
| 879 | Twine(CmdName) + " command " + |
| 880 | Twine(LoadCommandIndex) + " extends past the end of " |
| 881 | "the file"); |
| 882 | *LoadCmd = Load.Ptr; |
| 883 | return Error::success(); |
| 884 | } |
| 885 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 886 | static Error checkLinkerOptCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 68fffa8 | 2016-10-11 21:04:39 +0000 | [diff] [blame] | 887 | const MachOObjectFile::LoadCommandInfo &Load, |
| 888 | uint32_t LoadCommandIndex) { |
| 889 | if (Load.C.cmdsize < sizeof(MachO::linker_option_command)) |
| 890 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 891 | " LC_LINKER_OPTION cmdsize too small"); |
| 892 | MachO::linker_option_command L = |
| 893 | getStruct<MachO::linker_option_command>(Obj, Load.Ptr); |
| 894 | // Make sure the count of strings is correct. |
| 895 | const char *string = (const char *)Load.Ptr + |
| 896 | sizeof(struct MachO::linker_option_command); |
| 897 | uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command); |
| 898 | uint32_t i = 0; |
| 899 | while (left > 0) { |
| 900 | while (*string == '\0' && left > 0) { |
| 901 | string++; |
| 902 | left--; |
| 903 | } |
| 904 | if (left > 0) { |
| 905 | i++; |
| 906 | uint32_t NullPos = StringRef(string, left).find('\0'); |
| 907 | uint32_t len = std::min(NullPos, left) + 1; |
| 908 | string += len; |
| 909 | left -= len; |
| 910 | } |
| 911 | } |
| 912 | if (L.count != i) |
| 913 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 914 | " LC_LINKER_OPTION string count " + Twine(L.count) + |
| 915 | " does not match number of strings"); |
| 916 | return Error::success(); |
| 917 | } |
| 918 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 919 | static Error checkSubCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 920 | const MachOObjectFile::LoadCommandInfo &Load, |
| 921 | uint32_t LoadCommandIndex, const char *CmdName, |
| 922 | size_t SizeOfCmd, const char *CmdStructName, |
| 923 | uint32_t PathOffset, const char *PathFieldName) { |
| 924 | if (PathOffset < SizeOfCmd) |
| 925 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 926 | CmdName + " " + PathFieldName + ".offset field too " |
| 927 | "small, not past the end of the " + CmdStructName); |
| 928 | if (PathOffset >= Load.C.cmdsize) |
| 929 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 930 | CmdName + " " + PathFieldName + ".offset field " |
| 931 | "extends past the end of the load command"); |
| 932 | // Make sure there is a null between the starting offset of the path and |
| 933 | // the end of the load command. |
| 934 | uint32_t i; |
| 935 | const char *P = (const char *)Load.Ptr; |
| 936 | for (i = PathOffset; i < Load.C.cmdsize; i++) |
| 937 | if (P[i] == '\0') |
| 938 | break; |
| 939 | if (i >= Load.C.cmdsize) |
| 940 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 941 | CmdName + " " + PathFieldName + " name extends past " |
| 942 | "the end of the load command"); |
| 943 | return Error::success(); |
| 944 | } |
| 945 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 946 | static Error checkThreadCommand(const MachOObjectFile &Obj, |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 947 | const MachOObjectFile::LoadCommandInfo &Load, |
| 948 | uint32_t LoadCommandIndex, |
| 949 | const char *CmdName) { |
| 950 | if (Load.C.cmdsize < sizeof(MachO::thread_command)) |
| 951 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 952 | CmdName + " cmdsize too small"); |
| 953 | MachO::thread_command T = |
| 954 | getStruct<MachO::thread_command>(Obj, Load.Ptr); |
| 955 | const char *state = Load.Ptr + sizeof(MachO::thread_command); |
| 956 | const char *end = Load.Ptr + T.cmdsize; |
| 957 | uint32_t nflavor = 0; |
| 958 | uint32_t cputype = getCPUType(Obj); |
| 959 | while (state < end) { |
| 960 | if(state + sizeof(uint32_t) > end) |
| 961 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 962 | "flavor in " + CmdName + " extends past end of " |
| 963 | "command"); |
| 964 | uint32_t flavor; |
| 965 | memcpy(&flavor, state, sizeof(uint32_t)); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 966 | if (Obj.isLittleEndian() != sys::IsLittleEndianHost) |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 967 | sys::swapByteOrder(flavor); |
| 968 | state += sizeof(uint32_t); |
| 969 | |
| 970 | if(state + sizeof(uint32_t) > end) |
| 971 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 972 | " count in " + CmdName + " extends past end of " |
| 973 | "command"); |
| 974 | uint32_t count; |
| 975 | memcpy(&count, state, sizeof(uint32_t)); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 976 | if (Obj.isLittleEndian() != sys::IsLittleEndianHost) |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 977 | sys::swapByteOrder(count); |
| 978 | state += sizeof(uint32_t); |
| 979 | |
Kevin Enderby | c3a035d | 2017-01-23 21:13:29 +0000 | [diff] [blame] | 980 | if (cputype == MachO::CPU_TYPE_I386) { |
| 981 | if (flavor == MachO::x86_THREAD_STATE32) { |
| 982 | if (count != MachO::x86_THREAD_STATE32_COUNT) |
| 983 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 984 | " count not x86_THREAD_STATE32_COUNT for " |
| 985 | "flavor number " + Twine(nflavor) + " which is " |
| 986 | "a x86_THREAD_STATE32 flavor in " + CmdName + |
| 987 | " command"); |
| 988 | if (state + sizeof(MachO::x86_thread_state32_t) > end) |
| 989 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 990 | " x86_THREAD_STATE32 extends past end of " |
| 991 | "command in " + CmdName + " command"); |
| 992 | state += sizeof(MachO::x86_thread_state32_t); |
| 993 | } else { |
| 994 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 995 | " unknown flavor (" + Twine(flavor) + ") for " |
| 996 | "flavor number " + Twine(nflavor) + " in " + |
| 997 | CmdName + " command"); |
| 998 | } |
| 999 | } else if (cputype == MachO::CPU_TYPE_X86_64) { |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1000 | if (flavor == MachO::x86_THREAD_STATE64) { |
| 1001 | if (count != MachO::x86_THREAD_STATE64_COUNT) |
| 1002 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1003 | " count not x86_THREAD_STATE64_COUNT for " |
| 1004 | "flavor number " + Twine(nflavor) + " which is " |
| 1005 | "a x86_THREAD_STATE64 flavor in " + CmdName + |
| 1006 | " command"); |
| 1007 | if (state + sizeof(MachO::x86_thread_state64_t) > end) |
| 1008 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1009 | " x86_THREAD_STATE64 extends past end of " |
| 1010 | "command in " + CmdName + " command"); |
| 1011 | state += sizeof(MachO::x86_thread_state64_t); |
| 1012 | } else { |
| 1013 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1014 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1015 | "flavor number " + Twine(nflavor) + " in " + |
| 1016 | CmdName + " command"); |
| 1017 | } |
| 1018 | } else if (cputype == MachO::CPU_TYPE_ARM) { |
| 1019 | if (flavor == MachO::ARM_THREAD_STATE) { |
| 1020 | if (count != MachO::ARM_THREAD_STATE_COUNT) |
| 1021 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1022 | " count not ARM_THREAD_STATE_COUNT for " |
| 1023 | "flavor number " + Twine(nflavor) + " which is " |
| 1024 | "a ARM_THREAD_STATE flavor in " + CmdName + |
| 1025 | " command"); |
| 1026 | if (state + sizeof(MachO::arm_thread_state32_t) > end) |
| 1027 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1028 | " ARM_THREAD_STATE extends past end of " |
| 1029 | "command in " + CmdName + " command"); |
| 1030 | state += sizeof(MachO::arm_thread_state32_t); |
| 1031 | } else { |
| 1032 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1033 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1034 | "flavor number " + Twine(nflavor) + " in " + |
| 1035 | CmdName + " command"); |
| 1036 | } |
Kevin Enderby | 7747cb5 | 2016-11-03 20:51:28 +0000 | [diff] [blame] | 1037 | } else if (cputype == MachO::CPU_TYPE_ARM64) { |
| 1038 | if (flavor == MachO::ARM_THREAD_STATE64) { |
| 1039 | if (count != MachO::ARM_THREAD_STATE64_COUNT) |
| 1040 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1041 | " count not ARM_THREAD_STATE64_COUNT for " |
| 1042 | "flavor number " + Twine(nflavor) + " which is " |
| 1043 | "a ARM_THREAD_STATE64 flavor in " + CmdName + |
| 1044 | " command"); |
| 1045 | if (state + sizeof(MachO::arm_thread_state64_t) > end) |
| 1046 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1047 | " ARM_THREAD_STATE64 extends past end of " |
| 1048 | "command in " + CmdName + " command"); |
| 1049 | state += sizeof(MachO::arm_thread_state64_t); |
| 1050 | } else { |
| 1051 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1052 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1053 | "flavor number " + Twine(nflavor) + " in " + |
| 1054 | CmdName + " command"); |
| 1055 | } |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1056 | } else if (cputype == MachO::CPU_TYPE_POWERPC) { |
| 1057 | if (flavor == MachO::PPC_THREAD_STATE) { |
| 1058 | if (count != MachO::PPC_THREAD_STATE_COUNT) |
| 1059 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1060 | " count not PPC_THREAD_STATE_COUNT for " |
| 1061 | "flavor number " + Twine(nflavor) + " which is " |
| 1062 | "a PPC_THREAD_STATE flavor in " + CmdName + |
| 1063 | " command"); |
| 1064 | if (state + sizeof(MachO::ppc_thread_state32_t) > end) |
| 1065 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1066 | " PPC_THREAD_STATE extends past end of " |
| 1067 | "command in " + CmdName + " command"); |
| 1068 | state += sizeof(MachO::ppc_thread_state32_t); |
| 1069 | } else { |
| 1070 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1071 | " unknown flavor (" + Twine(flavor) + ") for " |
| 1072 | "flavor number " + Twine(nflavor) + " in " + |
| 1073 | CmdName + " command"); |
| 1074 | } |
| 1075 | } else { |
| 1076 | return malformedError("unknown cputype (" + Twine(cputype) + ") load " |
| 1077 | "command " + Twine(LoadCommandIndex) + " for " + |
| 1078 | CmdName + " command can't be checked"); |
| 1079 | } |
| 1080 | nflavor++; |
| 1081 | } |
| 1082 | return Error::success(); |
| 1083 | } |
| 1084 | |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1085 | static Error checkTwoLevelHintsCommand(const MachOObjectFile &Obj, |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1086 | const MachOObjectFile::LoadCommandInfo |
| 1087 | &Load, |
| 1088 | uint32_t LoadCommandIndex, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1089 | const char **LoadCmd, |
| 1090 | std::list<MachOElement> &Elements) { |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1091 | if (Load.C.cmdsize != sizeof(MachO::twolevel_hints_command)) |
| 1092 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 1093 | " LC_TWOLEVEL_HINTS has incorrect cmdsize"); |
| 1094 | if (*LoadCmd != nullptr) |
| 1095 | return malformedError("more than one LC_TWOLEVEL_HINTS command"); |
| 1096 | MachO::twolevel_hints_command Hints = |
| 1097 | getStruct<MachO::twolevel_hints_command>(Obj, Load.Ptr); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1098 | uint64_t FileSize = Obj.getData().size(); |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1099 | if (Hints.offset > FileSize) |
| 1100 | return malformedError("offset field of LC_TWOLEVEL_HINTS command " + |
| 1101 | Twine(LoadCommandIndex) + " extends past the end of " |
| 1102 | "the file"); |
| 1103 | uint64_t BigSize = Hints.nhints; |
| 1104 | BigSize *= Hints.nhints * sizeof(MachO::twolevel_hint); |
| 1105 | BigSize += Hints.offset; |
| 1106 | if (BigSize > FileSize) |
| 1107 | return malformedError("offset field plus nhints times sizeof(struct " |
| 1108 | "twolevel_hint) field of LC_TWOLEVEL_HINTS command " + |
| 1109 | Twine(LoadCommandIndex) + " extends past the end of " |
| 1110 | "the file"); |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1111 | if (Error Err = checkOverlappingElement(Elements, Hints.offset, Hints.nhints * |
| 1112 | sizeof(MachO::twolevel_hint), |
| 1113 | "two level hints")) |
| 1114 | return Err; |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1115 | *LoadCmd = Load.Ptr; |
| 1116 | return Error::success(); |
| 1117 | } |
| 1118 | |
Kevin Enderby | bc5c29a | 2016-10-27 20:59:10 +0000 | [diff] [blame] | 1119 | // Returns true if the libObject code does not support the load command and its |
| 1120 | // contents. The cmd value it is treated as an unknown load command but with |
| 1121 | // an error message that says the cmd value is obsolete. |
| 1122 | static bool isLoadCommandObsolete(uint32_t cmd) { |
| 1123 | if (cmd == MachO::LC_SYMSEG || |
| 1124 | cmd == MachO::LC_LOADFVMLIB || |
| 1125 | cmd == MachO::LC_IDFVMLIB || |
| 1126 | cmd == MachO::LC_IDENT || |
| 1127 | cmd == MachO::LC_FVMFILE || |
| 1128 | cmd == MachO::LC_PREPAGE || |
| 1129 | cmd == MachO::LC_PREBOUND_DYLIB || |
| 1130 | cmd == MachO::LC_TWOLEVEL_HINTS || |
| 1131 | cmd == MachO::LC_PREBIND_CKSUM) |
| 1132 | return true; |
| 1133 | return false; |
| 1134 | } |
| 1135 | |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 1136 | Expected<std::unique_ptr<MachOObjectFile>> |
| 1137 | MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian, |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1138 | bool Is64Bits, uint32_t UniversalCputype, |
| 1139 | uint32_t UniversalIndex) { |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 1140 | Error Err = Error::success(); |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 1141 | std::unique_ptr<MachOObjectFile> Obj( |
| 1142 | new MachOObjectFile(std::move(Object), IsLittleEndian, |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1143 | Is64Bits, Err, UniversalCputype, |
| 1144 | UniversalIndex)); |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 1145 | if (Err) |
| 1146 | return std::move(Err); |
| 1147 | return std::move(Obj); |
| 1148 | } |
| 1149 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1150 | MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1151 | bool Is64bits, Error &Err, |
| 1152 | uint32_t UniversalCputype, |
| 1153 | uint32_t UniversalIndex) |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1154 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1155 | SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr), |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 1156 | DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr), |
| 1157 | DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr), |
| 1158 | HasPageZeroSegment(false) { |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 1159 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1160 | uint64_t SizeOfHeaders; |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1161 | uint32_t cputype; |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 1162 | if (is64Bit()) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1163 | parseHeader(*this, Header64, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1164 | SizeOfHeaders = sizeof(MachO::mach_header_64); |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1165 | cputype = Header64.cputype; |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 1166 | } else { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1167 | parseHeader(*this, Header, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1168 | SizeOfHeaders = sizeof(MachO::mach_header); |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1169 | cputype = Header.cputype; |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 1170 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1171 | if (Err) |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 1172 | return; |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1173 | SizeOfHeaders += getHeader().sizeofcmds; |
| 1174 | if (getData().data() + SizeOfHeaders > getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1175 | Err = malformedError("load commands extend past the end of the file"); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 1176 | return; |
| 1177 | } |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 1178 | if (UniversalCputype != 0 && cputype != UniversalCputype) { |
| 1179 | Err = malformedError("universal header architecture: " + |
| 1180 | Twine(UniversalIndex) + "'s cputype does not match " |
| 1181 | "object file's mach header"); |
| 1182 | return; |
| 1183 | } |
Kevin Enderby | d503940 | 2016-10-31 20:29:48 +0000 | [diff] [blame] | 1184 | std::list<MachOElement> Elements; |
| 1185 | Elements.push_back({0, SizeOfHeaders, "Mach-O headers"}); |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 1186 | |
| 1187 | uint32_t LoadCommandCount = getHeader().ncmds; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1188 | LoadCommandInfo Load; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1189 | if (LoadCommandCount != 0) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1190 | if (auto LoadOrErr = getFirstLoadCommandInfo(*this)) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1191 | Load = *LoadOrErr; |
| 1192 | else { |
| 1193 | Err = LoadOrErr.takeError(); |
| 1194 | return; |
| 1195 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1196 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1197 | |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1198 | const char *DyldIdLoadCmd = nullptr; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 1199 | const char *FuncStartsLoadCmd = nullptr; |
| 1200 | const char *SplitInfoLoadCmd = nullptr; |
| 1201 | const char *CodeSignDrsLoadCmd = nullptr; |
Kevin Enderby | 89baf99 | 2016-10-18 20:24:12 +0000 | [diff] [blame] | 1202 | const char *CodeSignLoadCmd = nullptr; |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1203 | const char *VersLoadCmd = nullptr; |
Kevin Enderby | 245be3e | 2016-09-29 17:45:23 +0000 | [diff] [blame] | 1204 | const char *SourceLoadCmd = nullptr; |
Kevin Enderby | 4f229d8 | 2016-09-29 21:07:29 +0000 | [diff] [blame] | 1205 | const char *EntryPointLoadCmd = nullptr; |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 1206 | const char *EncryptLoadCmd = nullptr; |
Kevin Enderby | 6f69582 | 2016-10-18 17:54:17 +0000 | [diff] [blame] | 1207 | const char *RoutinesLoadCmd = nullptr; |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1208 | const char *UnixThreadLoadCmd = nullptr; |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1209 | const char *TwoLevelHintsLoadCmd = nullptr; |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 1210 | for (unsigned I = 0; I < LoadCommandCount; ++I) { |
Kevin Enderby | 1851a82 | 2016-07-07 22:11:42 +0000 | [diff] [blame] | 1211 | if (is64Bit()) { |
| 1212 | if (Load.C.cmdsize % 8 != 0) { |
| 1213 | // We have a hack here to allow 64-bit Mach-O core files to have |
| 1214 | // LC_THREAD commands that are only a multiple of 4 and not 8 to be |
| 1215 | // allowed since the macOS kernel produces them. |
| 1216 | if (getHeader().filetype != MachO::MH_CORE || |
| 1217 | Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) { |
| 1218 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 1219 | "multiple of 8"); |
| 1220 | return; |
| 1221 | } |
| 1222 | } |
| 1223 | } else { |
| 1224 | if (Load.C.cmdsize % 4 != 0) { |
| 1225 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 1226 | "multiple of 4"); |
| 1227 | return; |
| 1228 | } |
| 1229 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 1230 | LoadCommands.push_back(Load); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1231 | if (Load.C.cmd == MachO::LC_SYMTAB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1232 | if ((Err = checkSymtabCommand(*this, Load, I, &SymtabLoadCmd, Elements))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1233 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1234 | } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1235 | if ((Err = checkDysymtabCommand(*this, Load, I, &DysymtabLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1236 | Elements))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1237 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1238 | } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1239 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &DataInCodeLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1240 | "LC_DATA_IN_CODE", Elements, |
| 1241 | "data in code info"))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1242 | return; |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 1243 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1244 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &LinkOptHintsLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1245 | "LC_LINKER_OPTIMIZATION_HINT", |
| 1246 | Elements, "linker optimization " |
| 1247 | "hints"))) |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 1248 | return; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 1249 | } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1250 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &FuncStartsLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1251 | "LC_FUNCTION_STARTS", Elements, |
| 1252 | "function starts data"))) |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 1253 | return; |
| 1254 | } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1255 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &SplitInfoLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1256 | "LC_SEGMENT_SPLIT_INFO", Elements, |
| 1257 | "split info data"))) |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1260 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignDrsLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1261 | "LC_DYLIB_CODE_SIGN_DRS", Elements, |
| 1262 | "code signing RDs data"))) |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 1263 | return; |
Kevin Enderby | 89baf99 | 2016-10-18 20:24:12 +0000 | [diff] [blame] | 1264 | } else if (Load.C.cmd == MachO::LC_CODE_SIGNATURE) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1265 | if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1266 | "LC_CODE_SIGNATURE", Elements, |
| 1267 | "code signature data"))) |
Kevin Enderby | 89baf99 | 2016-10-18 20:24:12 +0000 | [diff] [blame] | 1268 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 1269 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1270 | if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1271 | "LC_DYLD_INFO", Elements))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1272 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 1273 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1274 | if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1275 | "LC_DYLD_INFO_ONLY", Elements))) |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 1276 | return; |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 1277 | } else if (Load.C.cmd == MachO::LC_UUID) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 1278 | if (Load.C.cmdsize != sizeof(MachO::uuid_command)) { |
| 1279 | Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect " |
| 1280 | "cmdsize"); |
| 1281 | return; |
| 1282 | } |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1283 | if (UuidLoadCmd) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 1284 | Err = malformedError("more than one LC_UUID command"); |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 1285 | return; |
| 1286 | } |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 1287 | UuidLoadCmd = Load.Ptr; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 1288 | } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1289 | if ((Err = parseSegmentLoadCommand<MachO::segment_command_64, |
| 1290 | MachO::section_64>( |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1291 | *this, Load, Sections, HasPageZeroSegment, I, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1292 | "LC_SEGMENT_64", SizeOfHeaders, Elements))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 1293 | return; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 1294 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 1295 | if ((Err = parseSegmentLoadCommand<MachO::segment_command, |
| 1296 | MachO::section>( |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1297 | *this, Load, Sections, HasPageZeroSegment, I, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1298 | "LC_SEGMENT", SizeOfHeaders, Elements))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 1299 | return; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1300 | } else if (Load.C.cmd == MachO::LC_ID_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1301 | if ((Err = checkDylibIdCommand(*this, Load, I, &DyldIdLoadCmd))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1302 | return; |
| 1303 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1304 | if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_DYLIB"))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | Libraries.push_back(Load.Ptr); |
| 1307 | } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1308 | if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_WEAK_DYLIB"))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1309 | return; |
| 1310 | Libraries.push_back(Load.Ptr); |
| 1311 | } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1312 | if ((Err = checkDylibCommand(*this, Load, I, "LC_LAZY_LOAD_DYLIB"))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | Libraries.push_back(Load.Ptr); |
| 1315 | } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1316 | if ((Err = checkDylibCommand(*this, Load, I, "LC_REEXPORT_DYLIB"))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | Libraries.push_back(Load.Ptr); |
| 1319 | } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1320 | if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_UPWARD_DYLIB"))) |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1321 | return; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1322 | Libraries.push_back(Load.Ptr); |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 1323 | } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1324 | if ((Err = checkDyldCommand(*this, Load, I, "LC_ID_DYLINKER"))) |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1327 | if ((Err = checkDyldCommand(*this, Load, I, "LC_LOAD_DYLINKER"))) |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 1328 | return; |
| 1329 | } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1330 | if ((Err = checkDyldCommand(*this, Load, I, "LC_DYLD_ENVIRONMENT"))) |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 1331 | return; |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1332 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1333 | if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd, |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1334 | "LC_VERSION_MIN_MACOSX"))) |
| 1335 | return; |
| 1336 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1337 | if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd, |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1338 | "LC_VERSION_MIN_IPHONEOS"))) |
| 1339 | return; |
| 1340 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1341 | if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd, |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1342 | "LC_VERSION_MIN_TVOS"))) |
| 1343 | return; |
| 1344 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1345 | if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd, |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 1346 | "LC_VERSION_MIN_WATCHOS"))) |
| 1347 | return; |
Kevin Enderby | a4579c4 | 2017-01-19 17:36:31 +0000 | [diff] [blame] | 1348 | } else if (Load.C.cmd == MachO::LC_NOTE) { |
| 1349 | if ((Err = checkNoteCommand(*this, Load, I, Elements))) |
| 1350 | return; |
Steven Wu | 5b54a42 | 2017-01-23 20:07:55 +0000 | [diff] [blame] | 1351 | } else if (Load.C.cmd == MachO::LC_BUILD_VERSION) { |
| 1352 | if ((Err = parseBuildVersionCommand(*this, Load, BuildTools, I))) |
| 1353 | return; |
Kevin Enderby | 76966bf | 2016-09-28 23:16:01 +0000 | [diff] [blame] | 1354 | } else if (Load.C.cmd == MachO::LC_RPATH) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1355 | if ((Err = checkRpathCommand(*this, Load, I))) |
Kevin Enderby | 76966bf | 2016-09-28 23:16:01 +0000 | [diff] [blame] | 1356 | return; |
Kevin Enderby | 245be3e | 2016-09-29 17:45:23 +0000 | [diff] [blame] | 1357 | } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) { |
| 1358 | if (Load.C.cmdsize != sizeof(MachO::source_version_command)) { |
| 1359 | Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) + |
| 1360 | " has incorrect cmdsize"); |
| 1361 | return; |
| 1362 | } |
| 1363 | if (SourceLoadCmd) { |
| 1364 | Err = malformedError("more than one LC_SOURCE_VERSION command"); |
| 1365 | return; |
| 1366 | } |
| 1367 | SourceLoadCmd = Load.Ptr; |
Kevin Enderby | 4f229d8 | 2016-09-29 21:07:29 +0000 | [diff] [blame] | 1368 | } else if (Load.C.cmd == MachO::LC_MAIN) { |
| 1369 | if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) { |
| 1370 | Err = malformedError("LC_MAIN command " + Twine(I) + |
| 1371 | " has incorrect cmdsize"); |
| 1372 | return; |
| 1373 | } |
| 1374 | if (EntryPointLoadCmd) { |
| 1375 | Err = malformedError("more than one LC_MAIN command"); |
| 1376 | return; |
| 1377 | } |
| 1378 | EntryPointLoadCmd = Load.Ptr; |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 1379 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) { |
| 1380 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) { |
| 1381 | Err = malformedError("LC_ENCRYPTION_INFO command " + Twine(I) + |
| 1382 | " has incorrect cmdsize"); |
| 1383 | return; |
| 1384 | } |
| 1385 | MachO::encryption_info_command E = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1386 | getStruct<MachO::encryption_info_command>(*this, Load.Ptr); |
| 1387 | if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize, |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 1388 | &EncryptLoadCmd, "LC_ENCRYPTION_INFO"))) |
| 1389 | return; |
| 1390 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { |
| 1391 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) { |
| 1392 | Err = malformedError("LC_ENCRYPTION_INFO_64 command " + Twine(I) + |
| 1393 | " has incorrect cmdsize"); |
| 1394 | return; |
| 1395 | } |
| 1396 | MachO::encryption_info_command_64 E = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1397 | getStruct<MachO::encryption_info_command_64>(*this, Load.Ptr); |
| 1398 | if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize, |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 1399 | &EncryptLoadCmd, "LC_ENCRYPTION_INFO_64"))) |
| 1400 | return; |
Kevin Enderby | 68fffa8 | 2016-10-11 21:04:39 +0000 | [diff] [blame] | 1401 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1402 | if ((Err = checkLinkerOptCommand(*this, Load, I))) |
Kevin Enderby | 68fffa8 | 2016-10-11 21:04:39 +0000 | [diff] [blame] | 1403 | return; |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 1404 | } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) { |
| 1405 | if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) { |
| 1406 | Err = malformedError("load command " + Twine(I) + |
| 1407 | " LC_SUB_FRAMEWORK cmdsize too small"); |
| 1408 | return; |
| 1409 | } |
| 1410 | MachO::sub_framework_command S = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1411 | getStruct<MachO::sub_framework_command>(*this, Load.Ptr); |
| 1412 | if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_FRAMEWORK", |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 1413 | sizeof(MachO::sub_framework_command), |
| 1414 | "sub_framework_command", S.umbrella, |
| 1415 | "umbrella"))) |
| 1416 | return; |
| 1417 | } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) { |
| 1418 | if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) { |
| 1419 | Err = malformedError("load command " + Twine(I) + |
| 1420 | " LC_SUB_UMBRELLA cmdsize too small"); |
| 1421 | return; |
| 1422 | } |
| 1423 | MachO::sub_umbrella_command S = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1424 | getStruct<MachO::sub_umbrella_command>(*this, Load.Ptr); |
| 1425 | if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_UMBRELLA", |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 1426 | sizeof(MachO::sub_umbrella_command), |
| 1427 | "sub_umbrella_command", S.sub_umbrella, |
| 1428 | "sub_umbrella"))) |
| 1429 | return; |
| 1430 | } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) { |
| 1431 | if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) { |
| 1432 | Err = malformedError("load command " + Twine(I) + |
| 1433 | " LC_SUB_LIBRARY cmdsize too small"); |
| 1434 | return; |
| 1435 | } |
| 1436 | MachO::sub_library_command S = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1437 | getStruct<MachO::sub_library_command>(*this, Load.Ptr); |
| 1438 | if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_LIBRARY", |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 1439 | sizeof(MachO::sub_library_command), |
| 1440 | "sub_library_command", S.sub_library, |
| 1441 | "sub_library"))) |
| 1442 | return; |
| 1443 | } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) { |
| 1444 | if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) { |
| 1445 | Err = malformedError("load command " + Twine(I) + |
| 1446 | " LC_SUB_CLIENT cmdsize too small"); |
| 1447 | return; |
| 1448 | } |
| 1449 | MachO::sub_client_command S = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1450 | getStruct<MachO::sub_client_command>(*this, Load.Ptr); |
| 1451 | if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_CLIENT", |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame] | 1452 | sizeof(MachO::sub_client_command), |
| 1453 | "sub_client_command", S.client, "client"))) |
| 1454 | return; |
Kevin Enderby | 6f69582 | 2016-10-18 17:54:17 +0000 | [diff] [blame] | 1455 | } else if (Load.C.cmd == MachO::LC_ROUTINES) { |
| 1456 | if (Load.C.cmdsize != sizeof(MachO::routines_command)) { |
| 1457 | Err = malformedError("LC_ROUTINES command " + Twine(I) + |
| 1458 | " has incorrect cmdsize"); |
| 1459 | return; |
| 1460 | } |
| 1461 | if (RoutinesLoadCmd) { |
| 1462 | Err = malformedError("more than one LC_ROUTINES and or LC_ROUTINES_64 " |
| 1463 | "command"); |
| 1464 | return; |
| 1465 | } |
| 1466 | RoutinesLoadCmd = Load.Ptr; |
| 1467 | } else if (Load.C.cmd == MachO::LC_ROUTINES_64) { |
| 1468 | if (Load.C.cmdsize != sizeof(MachO::routines_command_64)) { |
| 1469 | Err = malformedError("LC_ROUTINES_64 command " + Twine(I) + |
| 1470 | " has incorrect cmdsize"); |
| 1471 | return; |
| 1472 | } |
| 1473 | if (RoutinesLoadCmd) { |
| 1474 | Err = malformedError("more than one LC_ROUTINES_64 and or LC_ROUTINES " |
| 1475 | "command"); |
| 1476 | return; |
| 1477 | } |
| 1478 | RoutinesLoadCmd = Load.Ptr; |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1479 | } else if (Load.C.cmd == MachO::LC_UNIXTHREAD) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1480 | if ((Err = checkThreadCommand(*this, Load, I, "LC_UNIXTHREAD"))) |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1481 | return; |
| 1482 | if (UnixThreadLoadCmd) { |
| 1483 | Err = malformedError("more than one LC_UNIXTHREAD command"); |
| 1484 | return; |
| 1485 | } |
| 1486 | UnixThreadLoadCmd = Load.Ptr; |
| 1487 | } else if (Load.C.cmd == MachO::LC_THREAD) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1488 | if ((Err = checkThreadCommand(*this, Load, I, "LC_THREAD"))) |
Kevin Enderby | 210030b | 2016-10-19 23:44:34 +0000 | [diff] [blame] | 1489 | return; |
Kevin Enderby | bc5c29a | 2016-10-27 20:59:10 +0000 | [diff] [blame] | 1490 | // Note: LC_TWOLEVEL_HINTS is really obsolete and is not supported. |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1491 | } else if (Load.C.cmd == MachO::LC_TWOLEVEL_HINTS) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1492 | if ((Err = checkTwoLevelHintsCommand(*this, Load, I, |
Kevin Enderby | fbebe16 | 2016-11-02 21:08:39 +0000 | [diff] [blame] | 1493 | &TwoLevelHintsLoadCmd, Elements))) |
Kevin Enderby | c8bb422 | 2016-10-20 20:10:30 +0000 | [diff] [blame] | 1494 | return; |
Kevin Enderby | bc5c29a | 2016-10-27 20:59:10 +0000 | [diff] [blame] | 1495 | } else if (isLoadCommandObsolete(Load.C.cmd)) { |
| 1496 | Err = malformedError("load command " + Twine(I) + " for cmd value of: " + |
| 1497 | Twine(Load.C.cmd) + " is obsolete and not " |
| 1498 | "supported"); |
| 1499 | return; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1500 | } |
Kevin Enderby | bc5c29a | 2016-10-27 20:59:10 +0000 | [diff] [blame] | 1501 | // TODO: generate a error for unknown load commands by default. But still |
| 1502 | // need work out an approach to allow or not allow unknown values like this |
| 1503 | // as an option for some uses like lldb. |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1504 | if (I < LoadCommandCount - 1) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1505 | if (auto LoadOrErr = getNextLoadCommandInfo(*this, I, Load)) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1506 | Load = *LoadOrErr; |
| 1507 | else { |
| 1508 | Err = LoadOrErr.takeError(); |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1509 | return; |
| 1510 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1511 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1512 | } |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1513 | if (!SymtabLoadCmd) { |
| 1514 | if (DysymtabLoadCmd) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1515 | Err = malformedError("contains LC_DYSYMTAB load command without a " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1516 | "LC_SYMTAB load command"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | } |
| 1519 | } else if (DysymtabLoadCmd) { |
| 1520 | MachO::symtab_command Symtab = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1521 | getStruct<MachO::symtab_command>(*this, SymtabLoadCmd); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1522 | MachO::dysymtab_command Dysymtab = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1523 | getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1524 | if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1525 | Err = malformedError("ilocalsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1526 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1527 | return; |
| 1528 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1529 | uint64_t BigSize = Dysymtab.ilocalsym; |
| 1530 | BigSize += Dysymtab.nlocalsym; |
| 1531 | if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1532 | Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1533 | "command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1534 | return; |
| 1535 | } |
| 1536 | if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1537 | Err = malformedError("nextdefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1538 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1539 | return; |
| 1540 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1541 | BigSize = Dysymtab.iextdefsym; |
| 1542 | BigSize += Dysymtab.nextdefsym; |
| 1543 | if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1544 | Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1545 | "load command extends past the end of the symbol " |
| 1546 | "table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1550 | Err = malformedError("nundefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1551 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1554 | BigSize = Dysymtab.iundefsym; |
| 1555 | BigSize += Dysymtab.nundefsym; |
| 1556 | if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1557 | Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1558 | " command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | } |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1562 | if ((getHeader().filetype == MachO::MH_DYLIB || |
| 1563 | getHeader().filetype == MachO::MH_DYLIB_STUB) && |
| 1564 | DyldIdLoadCmd == nullptr) { |
| 1565 | Err = malformedError("no LC_ID_DYLIB load command in dynamic library " |
| 1566 | "filetype"); |
| 1567 | return; |
| 1568 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 1569 | assert(LoadCommands.size() == LoadCommandCount); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1570 | |
| 1571 | Err = Error::success(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Kevin Enderby | 22fc007 | 2016-11-14 20:57:04 +0000 | [diff] [blame] | 1574 | Error MachOObjectFile::checkSymbolTable() const { |
| 1575 | uint32_t Flags = 0; |
| 1576 | if (is64Bit()) { |
| 1577 | MachO::mach_header_64 H_64 = MachOObjectFile::getHeader64(); |
| 1578 | Flags = H_64.flags; |
| 1579 | } else { |
| 1580 | MachO::mach_header H = MachOObjectFile::getHeader(); |
| 1581 | Flags = H.flags; |
| 1582 | } |
| 1583 | uint8_t NType = 0; |
| 1584 | uint8_t NSect = 0; |
| 1585 | uint16_t NDesc = 0; |
| 1586 | uint32_t NStrx = 0; |
| 1587 | uint64_t NValue = 0; |
| 1588 | uint32_t SymbolIndex = 0; |
| 1589 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 1590 | for (const SymbolRef &Symbol : symbols()) { |
| 1591 | DataRefImpl SymDRI = Symbol.getRawDataRefImpl(); |
| 1592 | if (is64Bit()) { |
| 1593 | MachO::nlist_64 STE_64 = getSymbol64TableEntry(SymDRI); |
| 1594 | NType = STE_64.n_type; |
| 1595 | NSect = STE_64.n_sect; |
| 1596 | NDesc = STE_64.n_desc; |
| 1597 | NStrx = STE_64.n_strx; |
| 1598 | NValue = STE_64.n_value; |
| 1599 | } else { |
| 1600 | MachO::nlist STE = getSymbolTableEntry(SymDRI); |
| 1601 | NType = STE.n_type; |
| 1602 | NType = STE.n_type; |
| 1603 | NSect = STE.n_sect; |
| 1604 | NDesc = STE.n_desc; |
| 1605 | NStrx = STE.n_strx; |
| 1606 | NValue = STE.n_value; |
| 1607 | } |
| 1608 | if ((NType & MachO::N_STAB) == 0 && |
| 1609 | (NType & MachO::N_TYPE) == MachO::N_SECT) { |
| 1610 | if (NSect == 0 || NSect > Sections.size()) |
| 1611 | return malformedError("bad section index: " + Twine((int)NSect) + |
| 1612 | " for symbol at index " + Twine(SymbolIndex)); |
| 1613 | } |
| 1614 | if ((NType & MachO::N_STAB) == 0 && |
| 1615 | (NType & MachO::N_TYPE) == MachO::N_INDR) { |
| 1616 | if (NValue >= S.strsize) |
| 1617 | return malformedError("bad n_value: " + Twine((int)NValue) + " past " |
| 1618 | "the end of string table, for N_INDR symbol at " |
| 1619 | "index " + Twine(SymbolIndex)); |
| 1620 | } |
| 1621 | if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL && |
| 1622 | (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) || |
| 1623 | (NType & MachO::N_TYPE) == MachO::N_PBUD)) { |
| 1624 | uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc); |
| 1625 | if (LibraryOrdinal != 0 && |
| 1626 | LibraryOrdinal != MachO::EXECUTABLE_ORDINAL && |
| 1627 | LibraryOrdinal != MachO::DYNAMIC_LOOKUP_ORDINAL && |
| 1628 | LibraryOrdinal - 1 >= Libraries.size() ) { |
| 1629 | return malformedError("bad library ordinal: " + Twine(LibraryOrdinal) + |
| 1630 | " for symbol at index " + Twine(SymbolIndex)); |
| 1631 | } |
| 1632 | } |
| 1633 | if (NStrx >= S.strsize) |
| 1634 | return malformedError("bad string table index: " + Twine((int)NStrx) + |
| 1635 | " past the end of string table, for symbol at " |
| 1636 | "index " + Twine(SymbolIndex)); |
| 1637 | SymbolIndex++; |
| 1638 | } |
| 1639 | return Error::success(); |
| 1640 | } |
| 1641 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1642 | void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1643 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1644 | sizeof(MachO::nlist_64) : |
| 1645 | sizeof(MachO::nlist); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1646 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1649 | Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1650 | StringRef StringTable = getStringTableData(); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1651 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1652 | const char *Start = &StringTable.data()[Entry.n_strx]; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1653 | if (Start < getData().begin() || Start >= getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1654 | return malformedError("bad string index: " + Twine(Entry.n_strx) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1655 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1656 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1657 | return StringRef(Start); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 1660 | unsigned MachOObjectFile::getSectionType(SectionRef Sec) const { |
| 1661 | DataRefImpl DRI = Sec.getRawDataRefImpl(); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1662 | uint32_t Flags = getSectionFlags(*this, DRI); |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 1663 | return Flags & MachO::SECTION_TYPE; |
| 1664 | } |
| 1665 | |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 1666 | uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const { |
| 1667 | if (is64Bit()) { |
| 1668 | MachO::nlist_64 Entry = getSymbol64TableEntry(Sym); |
| 1669 | return Entry.n_value; |
| 1670 | } |
| 1671 | MachO::nlist Entry = getSymbolTableEntry(Sym); |
| 1672 | return Entry.n_value; |
| 1673 | } |
| 1674 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1675 | // getIndirectName() returns the name of the alias'ed symbol who's string table |
| 1676 | // index is in the n_value field. |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1677 | std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, |
| 1678 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1679 | StringRef StringTable = getStringTableData(); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1680 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb); |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 1681 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 1682 | return object_error::parse_failed; |
| 1683 | uint64_t NValue = getNValue(Symb); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1684 | if (NValue >= StringTable.size()) |
| 1685 | return object_error::parse_failed; |
| 1686 | const char *Start = &StringTable.data()[NValue]; |
| 1687 | Res = StringRef(Start); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1688 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 1691 | uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const { |
Rafael Espindola | 7e7be92 | 2015-07-07 15:05:09 +0000 | [diff] [blame] | 1692 | return getNValue(Sym); |
Rafael Espindola | 991af66 | 2015-06-24 19:11:10 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 1695 | Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const { |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 1696 | return getSymbolValue(Sym); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1699 | uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const { |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1700 | uint32_t flags = getSymbolFlags(DRI); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1701 | if (flags & SymbolRef::SF_Common) { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1702 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI); |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1703 | return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1704 | } |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1705 | return 0; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 1708 | uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 1709 | return getNValue(DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1712 | Expected<SymbolRef::Type> |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1713 | MachOObjectFile::getSymbolType(DataRefImpl Symb) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1714 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1715 | uint8_t n_type = Entry.n_type; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1716 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1717 | // If this is a STAB debugging symbol, we can do nothing more. |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1718 | if (n_type & MachO::N_STAB) |
| 1719 | return SymbolRef::ST_Debug; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1720 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1721 | switch (n_type & MachO::N_TYPE) { |
| 1722 | case MachO::N_UNDF : |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1723 | return SymbolRef::ST_Unknown; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1724 | case MachO::N_SECT : |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1725 | Expected<section_iterator> SecOrError = getSymbolSection(Symb); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1726 | if (!SecOrError) |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1727 | return SecOrError.takeError(); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1728 | section_iterator Sec = *SecOrError; |
Kuba Brecka | de83322 | 2015-11-12 09:40:29 +0000 | [diff] [blame] | 1729 | if (Sec->isData() || Sec->isBSS()) |
| 1730 | return SymbolRef::ST_Data; |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1731 | return SymbolRef::ST_Function; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1732 | } |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1733 | return SymbolRef::ST_Other; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1736 | uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1737 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1738 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1739 | uint8_t MachOType = Entry.n_type; |
| 1740 | uint16_t MachOFlags = Entry.n_desc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1741 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1742 | uint32_t Result = SymbolRef::SF_None; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1743 | |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 1744 | if ((MachOType & MachO::N_TYPE) == MachO::N_INDR) |
| 1745 | Result |= SymbolRef::SF_Indirect; |
| 1746 | |
Rafael Espindola | a135632 | 2013-11-02 05:03:24 +0000 | [diff] [blame] | 1747 | if (MachOType & MachO::N_STAB) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1748 | Result |= SymbolRef::SF_FormatSpecific; |
| 1749 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1750 | if (MachOType & MachO::N_EXT) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1751 | Result |= SymbolRef::SF_Global; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1752 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 1753 | if (getNValue(DRI)) |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1754 | Result |= SymbolRef::SF_Common; |
Rafael Espindola | d824772 | 2015-07-07 14:26:39 +0000 | [diff] [blame] | 1755 | else |
| 1756 | Result |= SymbolRef::SF_Undefined; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1757 | } |
Lang Hames | 7e0692b | 2015-01-15 22:33:30 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (!(MachOType & MachO::N_PEXT)) |
| 1760 | Result |= SymbolRef::SF_Exported; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1763 | if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1764 | Result |= SymbolRef::SF_Weak; |
| 1765 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1766 | if (MachOFlags & (MachO::N_ARM_THUMB_DEF)) |
| 1767 | Result |= SymbolRef::SF_Thumb; |
| 1768 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1769 | if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1770 | Result |= SymbolRef::SF_Absolute; |
| 1771 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1772 | return Result; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1775 | Expected<section_iterator> |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1776 | MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1777 | MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1778 | uint8_t index = Entry.n_sect; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1779 | |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1780 | if (index == 0) |
| 1781 | return section_end(); |
| 1782 | DataRefImpl DRI; |
| 1783 | DRI.d.a = index - 1; |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1784 | if (DRI.d.a >= Sections.size()){ |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1785 | return malformedError("bad section index: " + Twine((int)index) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1786 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1787 | } |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1788 | return section_iterator(SectionRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1791 | unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const { |
| 1792 | MachO::nlist_base Entry = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1793 | getSymbolTableEntryBase(*this, Sym.getRawDataRefImpl()); |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1794 | return Entry.n_sect - 1; |
| 1795 | } |
| 1796 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1797 | void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1798 | Sec.d.a++; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1801 | std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec, |
| 1802 | StringRef &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1803 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 1804 | Result = parseSegmentOrSectionName(Raw.data()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1805 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1808 | uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const { |
| 1809 | if (is64Bit()) |
| 1810 | return getSection64(Sec).addr; |
| 1811 | return getSection(Sec).addr; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1814 | uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const { |
Kevin Enderby | 46e642f | 2015-10-08 22:50:55 +0000 | [diff] [blame] | 1815 | // In the case if a malformed Mach-O file where the section offset is past |
| 1816 | // the end of the file or some part of the section size is past the end of |
| 1817 | // the file return a size of zero or a size that covers the rest of the file |
| 1818 | // but does not extend past the end of the file. |
| 1819 | uint32_t SectOffset, SectType; |
| 1820 | uint64_t SectSize; |
| 1821 | |
| 1822 | if (is64Bit()) { |
| 1823 | MachO::section_64 Sect = getSection64(Sec); |
| 1824 | SectOffset = Sect.offset; |
| 1825 | SectSize = Sect.size; |
| 1826 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1827 | } else { |
| 1828 | MachO::section Sect = getSection(Sec); |
| 1829 | SectOffset = Sect.offset; |
| 1830 | SectSize = Sect.size; |
| 1831 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1832 | } |
| 1833 | if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL) |
| 1834 | return SectSize; |
| 1835 | uint64_t FileSize = getData().size(); |
| 1836 | if (SectOffset > FileSize) |
| 1837 | return 0; |
| 1838 | if (FileSize - SectOffset < SectSize) |
| 1839 | return FileSize - SectOffset; |
| 1840 | return SectSize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1843 | std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec, |
| 1844 | StringRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1845 | uint32_t Offset; |
| 1846 | uint64_t Size; |
| 1847 | |
| 1848 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1849 | MachO::section_64 Sect = getSection64(Sec); |
| 1850 | Offset = Sect.offset; |
| 1851 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1852 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1853 | MachO::section Sect = getSection(Sec); |
| 1854 | Offset = Sect.offset; |
| 1855 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | Res = this->getData().substr(Offset, Size); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1859 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1862 | uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1863 | uint32_t Align; |
| 1864 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1865 | MachO::section_64 Sect = getSection64(Sec); |
| 1866 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1867 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1868 | MachO::section Sect = getSection(Sec); |
| 1869 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1872 | return uint64_t(1) << Align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
George Rimar | 401e4e5 | 2016-05-24 12:48:46 +0000 | [diff] [blame] | 1875 | bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1879 | bool MachOObjectFile::isSectionText(DataRefImpl Sec) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1880 | uint32_t Flags = getSectionFlags(*this, Sec); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1881 | return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1884 | bool MachOObjectFile::isSectionData(DataRefImpl Sec) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1885 | uint32_t Flags = getSectionFlags(*this, Sec); |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1886 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1887 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1888 | !(SectionType == MachO::S_ZEROFILL || |
| 1889 | SectionType == MachO::S_GB_ZEROFILL); |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1892 | bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1893 | uint32_t Flags = getSectionFlags(*this, Sec); |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1894 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1895 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1896 | (SectionType == MachO::S_ZEROFILL || |
| 1897 | SectionType == MachO::S_GB_ZEROFILL); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1900 | unsigned MachOObjectFile::getSectionID(SectionRef Sec) const { |
| 1901 | return Sec.getRawDataRefImpl().d.a; |
| 1902 | } |
| 1903 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1904 | bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const { |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1905 | // FIXME: Unimplemented. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1906 | return false; |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Steven Wu | f2fe014 | 2016-02-29 19:40:10 +0000 | [diff] [blame] | 1909 | bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const { |
| 1910 | StringRef SegmentName = getSectionFinalSegmentName(Sec); |
| 1911 | StringRef SectName; |
| 1912 | if (!getSectionName(Sec, SectName)) |
| 1913 | return (SegmentName == "__LLVM" && SectName == "__bitcode"); |
| 1914 | return false; |
| 1915 | } |
| 1916 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1917 | relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1918 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1919 | Ret.d.a = Sec.d.a; |
| 1920 | Ret.d.b = 0; |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1921 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1922 | } |
Rafael Espindola | c0406e1 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 1923 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1924 | relocation_iterator |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1925 | MachOObjectFile::section_rel_end(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1926 | uint32_t Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1927 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1928 | MachO::section_64 Sect = getSection64(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1929 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1930 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1931 | MachO::section Sect = getSection(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1932 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1933 | } |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1934 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1935 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1936 | Ret.d.a = Sec.d.a; |
| 1937 | Ret.d.b = Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1938 | return relocation_iterator(RelocationRef(Ret, this)); |
| 1939 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1940 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1941 | void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1942 | ++Rel.d.b; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1943 | } |
Owen Anderson | 171f485 | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1944 | |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1945 | uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const { |
Rafael Espindola | 7247546 | 2014-04-04 00:31:12 +0000 | [diff] [blame] | 1946 | assert(getHeader().filetype == MachO::MH_OBJECT && |
| 1947 | "Only implemented for MH_OBJECT"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1948 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1949 | return getAnyRelocationAddress(RE); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1952 | symbol_iterator |
| 1953 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1954 | MachO::any_relocation_info RE = getRelocation(Rel); |
Tim Northover | 07f99fb | 2014-07-04 10:57:56 +0000 | [diff] [blame] | 1955 | if (isRelocationScattered(RE)) |
| 1956 | return symbol_end(); |
| 1957 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1958 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 1959 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1960 | if (!isExtern) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1961 | return symbol_end(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1962 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1963 | MachO::symtab_command S = getSymtabLoadCommand(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1964 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1965 | sizeof(MachO::nlist_64) : |
| 1966 | sizeof(MachO::nlist); |
| 1967 | uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1968 | DataRefImpl Sym; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 1969 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset)); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1970 | return symbol_iterator(SymbolRef(Sym, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1973 | section_iterator |
| 1974 | MachOObjectFile::getRelocationSection(DataRefImpl Rel) const { |
| 1975 | return section_iterator(getAnyRelocationSection(getRelocation(Rel))); |
| 1976 | } |
| 1977 | |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1978 | uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1979 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1980 | return getAnyRelocationType(RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1983 | void MachOObjectFile::getRelocationTypeName( |
| 1984 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1985 | StringRef res; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1986 | uint64_t RType = getRelocationType(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1987 | |
| 1988 | unsigned Arch = this->getArch(); |
| 1989 | |
| 1990 | switch (Arch) { |
| 1991 | case Triple::x86: { |
| 1992 | static const char *const Table[] = { |
| 1993 | "GENERIC_RELOC_VANILLA", |
| 1994 | "GENERIC_RELOC_PAIR", |
| 1995 | "GENERIC_RELOC_SECTDIFF", |
| 1996 | "GENERIC_RELOC_PB_LA_PTR", |
| 1997 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 1998 | "GENERIC_RELOC_TLV" }; |
| 1999 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 2000 | if (RType > 5) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2001 | res = "Unknown"; |
| 2002 | else |
| 2003 | res = Table[RType]; |
| 2004 | break; |
| 2005 | } |
| 2006 | case Triple::x86_64: { |
| 2007 | static const char *const Table[] = { |
| 2008 | "X86_64_RELOC_UNSIGNED", |
| 2009 | "X86_64_RELOC_SIGNED", |
| 2010 | "X86_64_RELOC_BRANCH", |
| 2011 | "X86_64_RELOC_GOT_LOAD", |
| 2012 | "X86_64_RELOC_GOT", |
| 2013 | "X86_64_RELOC_SUBTRACTOR", |
| 2014 | "X86_64_RELOC_SIGNED_1", |
| 2015 | "X86_64_RELOC_SIGNED_2", |
| 2016 | "X86_64_RELOC_SIGNED_4", |
| 2017 | "X86_64_RELOC_TLV" }; |
| 2018 | |
| 2019 | if (RType > 9) |
| 2020 | res = "Unknown"; |
| 2021 | else |
| 2022 | res = Table[RType]; |
| 2023 | break; |
| 2024 | } |
| 2025 | case Triple::arm: { |
| 2026 | static const char *const Table[] = { |
| 2027 | "ARM_RELOC_VANILLA", |
| 2028 | "ARM_RELOC_PAIR", |
| 2029 | "ARM_RELOC_SECTDIFF", |
| 2030 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 2031 | "ARM_RELOC_PB_LA_PTR", |
| 2032 | "ARM_RELOC_BR24", |
| 2033 | "ARM_THUMB_RELOC_BR22", |
| 2034 | "ARM_THUMB_32BIT_BRANCH", |
| 2035 | "ARM_RELOC_HALF", |
| 2036 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 2037 | |
| 2038 | if (RType > 9) |
| 2039 | res = "Unknown"; |
| 2040 | else |
| 2041 | res = Table[RType]; |
| 2042 | break; |
| 2043 | } |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2044 | case Triple::aarch64: { |
| 2045 | static const char *const Table[] = { |
| 2046 | "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR", |
| 2047 | "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", |
| 2048 | "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21", |
| 2049 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT", |
| 2050 | "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", |
| 2051 | "ARM64_RELOC_ADDEND" |
| 2052 | }; |
| 2053 | |
| 2054 | if (RType >= array_lengthof(Table)) |
| 2055 | res = "Unknown"; |
| 2056 | else |
| 2057 | res = Table[RType]; |
| 2058 | break; |
| 2059 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2060 | case Triple::ppc: { |
| 2061 | static const char *const Table[] = { |
| 2062 | "PPC_RELOC_VANILLA", |
| 2063 | "PPC_RELOC_PAIR", |
| 2064 | "PPC_RELOC_BR14", |
| 2065 | "PPC_RELOC_BR24", |
| 2066 | "PPC_RELOC_HI16", |
| 2067 | "PPC_RELOC_LO16", |
| 2068 | "PPC_RELOC_HA16", |
| 2069 | "PPC_RELOC_LO14", |
| 2070 | "PPC_RELOC_SECTDIFF", |
| 2071 | "PPC_RELOC_PB_LA_PTR", |
| 2072 | "PPC_RELOC_HI16_SECTDIFF", |
| 2073 | "PPC_RELOC_LO16_SECTDIFF", |
| 2074 | "PPC_RELOC_HA16_SECTDIFF", |
| 2075 | "PPC_RELOC_JBSR", |
| 2076 | "PPC_RELOC_LO14_SECTDIFF", |
| 2077 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 2078 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 2079 | if (RType > 15) |
| 2080 | res = "Unknown"; |
| 2081 | else |
| 2082 | res = Table[RType]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2083 | break; |
| 2084 | } |
| 2085 | case Triple::UnknownArch: |
| 2086 | res = "Unknown"; |
| 2087 | break; |
| 2088 | } |
| 2089 | Result.append(res.begin(), res.end()); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 2092 | uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const { |
| 2093 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 2094 | return getAnyRelocationLength(RE); |
| 2095 | } |
| 2096 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2097 | // |
| 2098 | // guessLibraryShortName() is passed a name of a dynamic library and returns a |
| 2099 | // guess on what the short name is. Then name is returned as a substring of the |
| 2100 | // StringRef Name passed in. The name of the dynamic library is recognized as |
| 2101 | // a framework if it has one of the two following forms: |
| 2102 | // Foo.framework/Versions/A/Foo |
| 2103 | // Foo.framework/Foo |
| 2104 | // Where A and Foo can be any string. And may contain a trailing suffix |
| 2105 | // starting with an underbar. If the Name is recognized as a framework then |
| 2106 | // isFramework is set to true else it is set to false. If the Name has a |
| 2107 | // suffix then Suffix is set to the substring in Name that contains the suffix |
| 2108 | // else it is set to a NULL StringRef. |
| 2109 | // |
| 2110 | // The Name of the dynamic library is recognized as a library name if it has |
| 2111 | // one of the two following forms: |
| 2112 | // libFoo.A.dylib |
| 2113 | // libFoo.dylib |
| 2114 | // The library may have a suffix trailing the name Foo of the form: |
| 2115 | // libFoo_profile.A.dylib |
| 2116 | // libFoo_profile.dylib |
| 2117 | // |
| 2118 | // The Name of the dynamic library is also recognized as a library name if it |
| 2119 | // has the following form: |
| 2120 | // Foo.qtx |
| 2121 | // |
| 2122 | // If the Name of the dynamic library is none of the forms above then a NULL |
| 2123 | // StringRef is returned. |
| 2124 | // |
| 2125 | StringRef MachOObjectFile::guessLibraryShortName(StringRef Name, |
| 2126 | bool &isFramework, |
| 2127 | StringRef &Suffix) { |
| 2128 | StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx; |
| 2129 | size_t a, b, c, d, Idx; |
| 2130 | |
| 2131 | isFramework = false; |
| 2132 | Suffix = StringRef(); |
| 2133 | |
| 2134 | // Pull off the last component and make Foo point to it |
| 2135 | a = Name.rfind('/'); |
| 2136 | if (a == Name.npos || a == 0) |
| 2137 | goto guess_library; |
| 2138 | Foo = Name.slice(a+1, Name.npos); |
| 2139 | |
| 2140 | // Look for a suffix starting with a '_' |
| 2141 | Idx = Foo.rfind('_'); |
| 2142 | if (Idx != Foo.npos && Foo.size() >= 2) { |
| 2143 | Suffix = Foo.slice(Idx, Foo.npos); |
| 2144 | Foo = Foo.slice(0, Idx); |
| 2145 | } |
| 2146 | |
| 2147 | // First look for the form Foo.framework/Foo |
| 2148 | b = Name.rfind('/', a); |
| 2149 | if (b == Name.npos) |
| 2150 | Idx = 0; |
| 2151 | else |
| 2152 | Idx = b+1; |
| 2153 | F = Name.slice(Idx, Idx + Foo.size()); |
| 2154 | DotFramework = Name.slice(Idx + Foo.size(), |
| 2155 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 2156 | if (F == Foo && DotFramework == ".framework/") { |
| 2157 | isFramework = true; |
| 2158 | return Foo; |
| 2159 | } |
| 2160 | |
| 2161 | // Next look for the form Foo.framework/Versions/A/Foo |
| 2162 | if (b == Name.npos) |
| 2163 | goto guess_library; |
| 2164 | c = Name.rfind('/', b); |
| 2165 | if (c == Name.npos || c == 0) |
| 2166 | goto guess_library; |
| 2167 | V = Name.slice(c+1, Name.npos); |
| 2168 | if (!V.startswith("Versions/")) |
| 2169 | goto guess_library; |
| 2170 | d = Name.rfind('/', c); |
| 2171 | if (d == Name.npos) |
| 2172 | Idx = 0; |
| 2173 | else |
| 2174 | Idx = d+1; |
| 2175 | F = Name.slice(Idx, Idx + Foo.size()); |
| 2176 | DotFramework = Name.slice(Idx + Foo.size(), |
| 2177 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 2178 | if (F == Foo && DotFramework == ".framework/") { |
| 2179 | isFramework = true; |
| 2180 | return Foo; |
| 2181 | } |
| 2182 | |
| 2183 | guess_library: |
| 2184 | // pull off the suffix after the "." and make a point to it |
| 2185 | a = Name.rfind('.'); |
| 2186 | if (a == Name.npos || a == 0) |
| 2187 | return StringRef(); |
| 2188 | Dylib = Name.slice(a, Name.npos); |
| 2189 | if (Dylib != ".dylib") |
| 2190 | goto guess_qtx; |
| 2191 | |
| 2192 | // First pull off the version letter for the form Foo.A.dylib if any. |
| 2193 | if (a >= 3) { |
| 2194 | Dot = Name.slice(a-2, a-1); |
| 2195 | if (Dot == ".") |
| 2196 | a = a - 2; |
| 2197 | } |
| 2198 | |
| 2199 | b = Name.rfind('/', a); |
| 2200 | if (b == Name.npos) |
| 2201 | b = 0; |
| 2202 | else |
| 2203 | b = b+1; |
| 2204 | // ignore any suffix after an underbar like Foo_profile.A.dylib |
| 2205 | Idx = Name.find('_', b); |
| 2206 | if (Idx != Name.npos && Idx != b) { |
| 2207 | Lib = Name.slice(b, Idx); |
| 2208 | Suffix = Name.slice(Idx, a); |
| 2209 | } |
| 2210 | else |
| 2211 | Lib = Name.slice(b, a); |
| 2212 | // There are incorrect library names of the form: |
| 2213 | // libATS.A_profile.dylib so check for these. |
| 2214 | if (Lib.size() >= 3) { |
| 2215 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 2216 | if (Dot == ".") |
| 2217 | Lib = Lib.slice(0, Lib.size()-2); |
| 2218 | } |
| 2219 | return Lib; |
| 2220 | |
| 2221 | guess_qtx: |
| 2222 | Qtx = Name.slice(a, Name.npos); |
| 2223 | if (Qtx != ".qtx") |
| 2224 | return StringRef(); |
| 2225 | b = Name.rfind('/', a); |
| 2226 | if (b == Name.npos) |
| 2227 | Lib = Name.slice(0, a); |
| 2228 | else |
| 2229 | Lib = Name.slice(b+1, a); |
| 2230 | // There are library names of the form: QT.A.qtx so check for these. |
| 2231 | if (Lib.size() >= 3) { |
| 2232 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 2233 | if (Dot == ".") |
| 2234 | Lib = Lib.slice(0, Lib.size()-2); |
| 2235 | } |
| 2236 | return Lib; |
| 2237 | } |
| 2238 | |
| 2239 | // getLibraryShortNameByIndex() is used to get the short name of the library |
| 2240 | // for an undefined symbol in a linked Mach-O binary that was linked with the |
| 2241 | // normal two-level namespace default (that is MH_TWOLEVEL in the header). |
| 2242 | // It is passed the index (0 - based) of the library as translated from |
| 2243 | // GET_LIBRARY_ORDINAL (1 - based). |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 2244 | std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index, |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2245 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2246 | if (Index >= Libraries.size()) |
| 2247 | return object_error::parse_failed; |
| 2248 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2249 | // If the cache of LibrariesShortNames is not built up do that first for |
| 2250 | // all the Libraries. |
| 2251 | if (LibrariesShortNames.size() == 0) { |
| 2252 | for (unsigned i = 0; i < Libraries.size(); i++) { |
| 2253 | MachO::dylib_command D = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2254 | getStruct<MachO::dylib_command>(*this, Libraries[i]); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 2255 | if (D.dylib.name >= D.cmdsize) |
| 2256 | return object_error::parse_failed; |
Kevin Enderby | 4eff6cd | 2014-06-20 18:07:34 +0000 | [diff] [blame] | 2257 | const char *P = (const char *)(Libraries[i]) + D.dylib.name; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2258 | StringRef Name = StringRef(P); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 2259 | if (D.dylib.name+Name.size() >= D.cmdsize) |
| 2260 | return object_error::parse_failed; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2261 | StringRef Suffix; |
| 2262 | bool isFramework; |
| 2263 | StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 2264 | if (shortName.empty()) |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2265 | LibrariesShortNames.push_back(Name); |
| 2266 | else |
| 2267 | LibrariesShortNames.push_back(shortName); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | Res = LibrariesShortNames[Index]; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 2272 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 2275 | uint32_t MachOObjectFile::getLibraryCount() const { |
| 2276 | return Libraries.size(); |
| 2277 | } |
| 2278 | |
Rafael Espindola | 76ad232 | 2015-07-06 14:55:37 +0000 | [diff] [blame] | 2279 | section_iterator |
| 2280 | MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const { |
| 2281 | DataRefImpl Sec; |
| 2282 | Sec.d.a = Rel->getRawDataRefImpl().d.a; |
| 2283 | return section_iterator(SectionRef(Sec, this)); |
| 2284 | } |
| 2285 | |
Peter Collingbourne | 435890a | 2016-11-22 03:38:40 +0000 | [diff] [blame] | 2286 | basic_symbol_iterator MachOObjectFile::symbol_begin() const { |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 2287 | DataRefImpl DRI; |
| 2288 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2289 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
| 2290 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2291 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 2292 | return getSymbolByIndex(0); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Peter Collingbourne | 435890a | 2016-11-22 03:38:40 +0000 | [diff] [blame] | 2295 | basic_symbol_iterator MachOObjectFile::symbol_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2296 | DataRefImpl DRI; |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 2297 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2298 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 2299 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2300 | |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2301 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2302 | sizeof(MachO::nlist_64) : |
| 2303 | sizeof(MachO::nlist); |
| 2304 | unsigned Offset = Symtab.symoff + |
| 2305 | Symtab.nsyms * SymbolTableEntrySize; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2306 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset)); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 2307 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 2310 | basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const { |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 2311 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 2312 | if (!SymtabLoadCmd || Index >= Symtab.nsyms) |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 2313 | report_fatal_error("Requested symbol index is out of range."); |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 2314 | unsigned SymbolTableEntrySize = |
| 2315 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 2316 | DataRefImpl DRI; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2317 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff)); |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 2318 | DRI.p += Index * SymbolTableEntrySize; |
| 2319 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 2320 | } |
| 2321 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 2322 | uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const { |
| 2323 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 2324 | if (!SymtabLoadCmd) |
| 2325 | report_fatal_error("getSymbolIndex() called with no symbol table symbol"); |
| 2326 | unsigned SymbolTableEntrySize = |
| 2327 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 2328 | DataRefImpl DRIstart; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2329 | DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff)); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 2330 | uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize; |
| 2331 | return Index; |
| 2332 | } |
| 2333 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 2334 | section_iterator MachOObjectFile::section_begin() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2335 | DataRefImpl DRI; |
| 2336 | return section_iterator(SectionRef(DRI, this)); |
| 2337 | } |
| 2338 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 2339 | section_iterator MachOObjectFile::section_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2340 | DataRefImpl DRI; |
| 2341 | DRI.d.a = Sections.size(); |
| 2342 | return section_iterator(SectionRef(DRI, this)); |
| 2343 | } |
| 2344 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2345 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 2346 | return is64Bit() ? 8 : 4; |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2349 | StringRef MachOObjectFile::getFileFormatName() const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2350 | unsigned CPUType = getCPUType(*this); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2351 | if (!is64Bit()) { |
| 2352 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2353 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2354 | return "Mach-O 32-bit i386"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2355 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2356 | return "Mach-O arm"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2357 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2358 | return "Mach-O 32-bit ppc"; |
| 2359 | default: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2360 | return "Mach-O 32-bit unknown"; |
| 2361 | } |
| 2362 | } |
| 2363 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2364 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2365 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2366 | return "Mach-O 64-bit x86-64"; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2367 | case llvm::MachO::CPU_TYPE_ARM64: |
| 2368 | return "Mach-O arm64"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2369 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2370 | return "Mach-O 64-bit ppc64"; |
| 2371 | default: |
| 2372 | return "Mach-O 64-bit unknown"; |
| 2373 | } |
| 2374 | } |
| 2375 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 2376 | Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) { |
| 2377 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2378 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2379 | return Triple::x86; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2380 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2381 | return Triple::x86_64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2382 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2383 | return Triple::arm; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 2384 | case llvm::MachO::CPU_TYPE_ARM64: |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 2385 | return Triple::aarch64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2386 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2387 | return Triple::ppc; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 2388 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2389 | return Triple::ppc64; |
| 2390 | default: |
| 2391 | return Triple::UnknownArch; |
| 2392 | } |
| 2393 | } |
| 2394 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 2395 | Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType, |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2396 | const char **McpuDefault, |
| 2397 | const char **ArchFlag) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2398 | if (McpuDefault) |
| 2399 | *McpuDefault = nullptr; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2400 | if (ArchFlag) |
| 2401 | *ArchFlag = nullptr; |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2402 | |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2403 | switch (CPUType) { |
| 2404 | case MachO::CPU_TYPE_I386: |
| 2405 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2406 | case MachO::CPU_SUBTYPE_I386_ALL: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2407 | if (ArchFlag) |
| 2408 | *ArchFlag = "i386"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2409 | return Triple("i386-apple-darwin"); |
| 2410 | default: |
| 2411 | return Triple(); |
| 2412 | } |
| 2413 | case MachO::CPU_TYPE_X86_64: |
| 2414 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2415 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2416 | if (ArchFlag) |
| 2417 | *ArchFlag = "x86_64"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2418 | return Triple("x86_64-apple-darwin"); |
| 2419 | case MachO::CPU_SUBTYPE_X86_64_H: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2420 | if (ArchFlag) |
| 2421 | *ArchFlag = "x86_64h"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2422 | return Triple("x86_64h-apple-darwin"); |
| 2423 | default: |
| 2424 | return Triple(); |
| 2425 | } |
| 2426 | case MachO::CPU_TYPE_ARM: |
| 2427 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2428 | case MachO::CPU_SUBTYPE_ARM_V4T: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2429 | if (ArchFlag) |
| 2430 | *ArchFlag = "armv4t"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2431 | return Triple("armv4t-apple-darwin"); |
| 2432 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2433 | if (ArchFlag) |
| 2434 | *ArchFlag = "armv5e"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2435 | return Triple("armv5e-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 2436 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2437 | if (ArchFlag) |
| 2438 | *ArchFlag = "xscale"; |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 2439 | return Triple("xscale-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2440 | case MachO::CPU_SUBTYPE_ARM_V6: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2441 | if (ArchFlag) |
| 2442 | *ArchFlag = "armv6"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2443 | return Triple("armv6-apple-darwin"); |
| 2444 | case MachO::CPU_SUBTYPE_ARM_V6M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2445 | if (McpuDefault) |
| 2446 | *McpuDefault = "cortex-m0"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2447 | if (ArchFlag) |
| 2448 | *ArchFlag = "armv6m"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2449 | return Triple("armv6m-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 2450 | case MachO::CPU_SUBTYPE_ARM_V7: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2451 | if (ArchFlag) |
| 2452 | *ArchFlag = "armv7"; |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 2453 | return Triple("armv7-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2454 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2455 | if (McpuDefault) |
| 2456 | *McpuDefault = "cortex-m4"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2457 | if (ArchFlag) |
| 2458 | *ArchFlag = "armv7em"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 2459 | return Triple("thumbv7em-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2460 | case MachO::CPU_SUBTYPE_ARM_V7K: |
Kevin Enderby | 7a16575 | 2017-01-24 23:41:04 +0000 | [diff] [blame] | 2461 | if (McpuDefault) |
| 2462 | *McpuDefault = "cortex-a7"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2463 | if (ArchFlag) |
| 2464 | *ArchFlag = "armv7k"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2465 | return Triple("armv7k-apple-darwin"); |
| 2466 | case MachO::CPU_SUBTYPE_ARM_V7M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2467 | if (McpuDefault) |
| 2468 | *McpuDefault = "cortex-m3"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2469 | if (ArchFlag) |
| 2470 | *ArchFlag = "armv7m"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 2471 | return Triple("thumbv7m-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2472 | case MachO::CPU_SUBTYPE_ARM_V7S: |
Kevin Enderby | 7a16575 | 2017-01-24 23:41:04 +0000 | [diff] [blame] | 2473 | if (McpuDefault) |
| 2474 | *McpuDefault = "cortex-a7"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2475 | if (ArchFlag) |
| 2476 | *ArchFlag = "armv7s"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2477 | return Triple("armv7s-apple-darwin"); |
| 2478 | default: |
| 2479 | return Triple(); |
| 2480 | } |
| 2481 | case MachO::CPU_TYPE_ARM64: |
| 2482 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2483 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
Kevin Enderby | dc412cc | 2017-02-10 19:27:10 +0000 | [diff] [blame] | 2484 | if (McpuDefault) |
| 2485 | *McpuDefault = "cyclone"; |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2486 | if (ArchFlag) |
| 2487 | *ArchFlag = "arm64"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2488 | return Triple("arm64-apple-darwin"); |
| 2489 | default: |
| 2490 | return Triple(); |
| 2491 | } |
| 2492 | case MachO::CPU_TYPE_POWERPC: |
| 2493 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 2494 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2495 | if (ArchFlag) |
| 2496 | *ArchFlag = "ppc"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2497 | return Triple("ppc-apple-darwin"); |
| 2498 | default: |
| 2499 | return Triple(); |
| 2500 | } |
| 2501 | case MachO::CPU_TYPE_POWERPC64: |
Reid Kleckner | 4da3d57 | 2014-06-30 20:12:59 +0000 | [diff] [blame] | 2502 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2503 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 2504 | if (ArchFlag) |
| 2505 | *ArchFlag = "ppc64"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2506 | return Triple("ppc64-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2507 | default: |
| 2508 | return Triple(); |
| 2509 | } |
| 2510 | default: |
| 2511 | return Triple(); |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | Triple MachOObjectFile::getHostArch() { |
| 2516 | return Triple(sys::getDefaultTargetTriple()); |
| 2517 | } |
| 2518 | |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 2519 | bool MachOObjectFile::isValidArch(StringRef ArchFlag) { |
| 2520 | return StringSwitch<bool>(ArchFlag) |
| 2521 | .Case("i386", true) |
| 2522 | .Case("x86_64", true) |
| 2523 | .Case("x86_64h", true) |
| 2524 | .Case("armv4t", true) |
| 2525 | .Case("arm", true) |
| 2526 | .Case("armv5e", true) |
| 2527 | .Case("armv6", true) |
| 2528 | .Case("armv6m", true) |
Frederic Riss | 40baa0a | 2015-06-16 17:37:03 +0000 | [diff] [blame] | 2529 | .Case("armv7", true) |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 2530 | .Case("armv7em", true) |
| 2531 | .Case("armv7k", true) |
| 2532 | .Case("armv7m", true) |
| 2533 | .Case("armv7s", true) |
| 2534 | .Case("arm64", true) |
| 2535 | .Case("ppc", true) |
| 2536 | .Case("ppc64", true) |
| 2537 | .Default(false); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 2540 | unsigned MachOObjectFile::getArch() const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2541 | return getArch(getCPUType(*this)); |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 2544 | Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const { |
| 2545 | return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2548 | relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2549 | DataRefImpl DRI; |
| 2550 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2551 | return section_rel_begin(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2554 | relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2555 | DataRefImpl DRI; |
| 2556 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2557 | return section_rel_end(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2560 | dice_iterator MachOObjectFile::begin_dices() const { |
| 2561 | DataRefImpl DRI; |
| 2562 | if (!DataInCodeLoadCmd) |
| 2563 | return dice_iterator(DiceRef(DRI, this)); |
| 2564 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2565 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2566 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, DicLC.dataoff)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2567 | return dice_iterator(DiceRef(DRI, this)); |
| 2568 | } |
| 2569 | |
| 2570 | dice_iterator MachOObjectFile::end_dices() const { |
| 2571 | DataRefImpl DRI; |
| 2572 | if (!DataInCodeLoadCmd) |
| 2573 | return dice_iterator(DiceRef(DRI, this)); |
| 2574 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2575 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 2576 | unsigned Offset = DicLC.dataoff + DicLC.datasize; |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 2577 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2578 | return dice_iterator(DiceRef(DRI, this)); |
| 2579 | } |
| 2580 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2581 | ExportEntry::ExportEntry(ArrayRef<uint8_t> T) |
| 2582 | : Trie(T), Malformed(false), Done(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2583 | |
| 2584 | void ExportEntry::moveToFirst() { |
| 2585 | pushNode(0); |
| 2586 | pushDownUntilBottom(); |
| 2587 | } |
| 2588 | |
| 2589 | void ExportEntry::moveToEnd() { |
| 2590 | Stack.clear(); |
| 2591 | Done = true; |
| 2592 | } |
| 2593 | |
| 2594 | bool ExportEntry::operator==(const ExportEntry &Other) const { |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 2595 | // Common case, one at end, other iterating from begin. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2596 | if (Done || Other.Done) |
| 2597 | return (Done == Other.Done); |
| 2598 | // Not equal if different stack sizes. |
| 2599 | if (Stack.size() != Other.Stack.size()) |
| 2600 | return false; |
| 2601 | // Not equal if different cumulative strings. |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 2602 | if (!CumulativeString.equals(Other.CumulativeString)) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2603 | return false; |
| 2604 | // Equal if all nodes in both stacks match. |
| 2605 | for (unsigned i=0; i < Stack.size(); ++i) { |
| 2606 | if (Stack[i].Start != Other.Stack[i].Start) |
| 2607 | return false; |
| 2608 | } |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2609 | return true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2612 | uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) { |
| 2613 | unsigned Count; |
| 2614 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2615 | Ptr += Count; |
| 2616 | if (Ptr > Trie.end()) { |
| 2617 | Ptr = Trie.end(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2618 | Malformed = true; |
| 2619 | } |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2620 | return Result; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | StringRef ExportEntry::name() const { |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 2624 | return CumulativeString; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | uint64_t ExportEntry::flags() const { |
| 2628 | return Stack.back().Flags; |
| 2629 | } |
| 2630 | |
| 2631 | uint64_t ExportEntry::address() const { |
| 2632 | return Stack.back().Address; |
| 2633 | } |
| 2634 | |
| 2635 | uint64_t ExportEntry::other() const { |
| 2636 | return Stack.back().Other; |
| 2637 | } |
| 2638 | |
| 2639 | StringRef ExportEntry::otherName() const { |
| 2640 | const char* ImportName = Stack.back().ImportName; |
| 2641 | if (ImportName) |
| 2642 | return StringRef(ImportName); |
| 2643 | return StringRef(); |
| 2644 | } |
| 2645 | |
| 2646 | uint32_t ExportEntry::nodeOffset() const { |
| 2647 | return Stack.back().Start - Trie.begin(); |
| 2648 | } |
| 2649 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2650 | ExportEntry::NodeState::NodeState(const uint8_t *Ptr) |
| 2651 | : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0), |
| 2652 | ImportName(nullptr), ChildCount(0), NextChildIndex(0), |
| 2653 | ParentStringLength(0), IsExportNode(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2654 | |
| 2655 | void ExportEntry::pushNode(uint64_t offset) { |
| 2656 | const uint8_t *Ptr = Trie.begin() + offset; |
| 2657 | NodeState State(Ptr); |
| 2658 | uint64_t ExportInfoSize = readULEB128(State.Current); |
| 2659 | State.IsExportNode = (ExportInfoSize != 0); |
| 2660 | const uint8_t* Children = State.Current + ExportInfoSize; |
| 2661 | if (State.IsExportNode) { |
| 2662 | State.Flags = readULEB128(State.Current); |
| 2663 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { |
| 2664 | State.Address = 0; |
| 2665 | State.Other = readULEB128(State.Current); // dylib ordinal |
| 2666 | State.ImportName = reinterpret_cast<const char*>(State.Current); |
| 2667 | } else { |
| 2668 | State.Address = readULEB128(State.Current); |
Nick Kledzik | 1b591bd | 2014-08-30 01:57:34 +0000 | [diff] [blame] | 2669 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2670 | State.Other = readULEB128(State.Current); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | State.ChildCount = *Children; |
| 2674 | State.Current = Children + 1; |
| 2675 | State.NextChildIndex = 0; |
| 2676 | State.ParentStringLength = CumulativeString.size(); |
| 2677 | Stack.push_back(State); |
| 2678 | } |
| 2679 | |
| 2680 | void ExportEntry::pushDownUntilBottom() { |
| 2681 | while (Stack.back().NextChildIndex < Stack.back().ChildCount) { |
| 2682 | NodeState &Top = Stack.back(); |
| 2683 | CumulativeString.resize(Top.ParentStringLength); |
| 2684 | for (;*Top.Current != 0; Top.Current++) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2685 | char C = *Top.Current; |
| 2686 | CumulativeString.push_back(C); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2687 | } |
| 2688 | Top.Current += 1; |
| 2689 | uint64_t childNodeIndex = readULEB128(Top.Current); |
| 2690 | Top.NextChildIndex += 1; |
| 2691 | pushNode(childNodeIndex); |
| 2692 | } |
| 2693 | if (!Stack.back().IsExportNode) { |
| 2694 | Malformed = true; |
| 2695 | moveToEnd(); |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | // We have a trie data structure and need a way to walk it that is compatible |
| 2700 | // with the C++ iterator model. The solution is a non-recursive depth first |
| 2701 | // traversal where the iterator contains a stack of parent nodes along with a |
| 2702 | // string that is the accumulation of all edge strings along the parent chain |
| 2703 | // to this point. |
| 2704 | // |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2705 | // There is one "export" node for each exported symbol. But because some |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2706 | // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 2707 | // node may have child nodes too. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2708 | // |
| 2709 | // The algorithm for moveNext() is to keep moving down the leftmost unvisited |
| 2710 | // child until hitting a node with no children (which is an export node or |
| 2711 | // else the trie is malformed). On the way down, each node is pushed on the |
| 2712 | // stack ivar. If there is no more ways down, it pops up one and tries to go |
| 2713 | // down a sibling path until a childless node is reached. |
| 2714 | void ExportEntry::moveNext() { |
| 2715 | if (Stack.empty() || !Stack.back().IsExportNode) { |
| 2716 | Malformed = true; |
| 2717 | moveToEnd(); |
| 2718 | return; |
| 2719 | } |
| 2720 | |
| 2721 | Stack.pop_back(); |
| 2722 | while (!Stack.empty()) { |
| 2723 | NodeState &Top = Stack.back(); |
| 2724 | if (Top.NextChildIndex < Top.ChildCount) { |
| 2725 | pushDownUntilBottom(); |
| 2726 | // Now at the next export node. |
| 2727 | return; |
| 2728 | } else { |
| 2729 | if (Top.IsExportNode) { |
| 2730 | // This node has no children but is itself an export node. |
| 2731 | CumulativeString.resize(Top.ParentStringLength); |
| 2732 | return; |
| 2733 | } |
| 2734 | Stack.pop_back(); |
| 2735 | } |
| 2736 | } |
| 2737 | Done = true; |
| 2738 | } |
| 2739 | |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2740 | iterator_range<export_iterator> |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2741 | MachOObjectFile::exports(ArrayRef<uint8_t> Trie) { |
| 2742 | ExportEntry Start(Trie); |
Juergen Ributzka | 4d7f70d | 2014-12-19 02:31:01 +0000 | [diff] [blame] | 2743 | if (Trie.size() == 0) |
| 2744 | Start.moveToEnd(); |
| 2745 | else |
| 2746 | Start.moveToFirst(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2747 | |
| 2748 | ExportEntry Finish(Trie); |
| 2749 | Finish.moveToEnd(); |
| 2750 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2751 | return make_range(export_iterator(Start), export_iterator(Finish)); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | iterator_range<export_iterator> MachOObjectFile::exports() const { |
| 2755 | return exports(getDyldInfoExportsTrie()); |
| 2756 | } |
| 2757 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2758 | MachORebaseEntry::MachORebaseEntry(Error *E, const MachOObjectFile *O, |
| 2759 | ArrayRef<uint8_t> Bytes, bool is64Bit) |
| 2760 | : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2761 | SegmentIndex(-1), RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0), |
| 2762 | PointerSize(is64Bit ? 8 : 4), Done(false) {} |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2763 | |
| 2764 | void MachORebaseEntry::moveToFirst() { |
| 2765 | Ptr = Opcodes.begin(); |
| 2766 | moveNext(); |
| 2767 | } |
| 2768 | |
| 2769 | void MachORebaseEntry::moveToEnd() { |
| 2770 | Ptr = Opcodes.end(); |
| 2771 | RemainingLoopCount = 0; |
| 2772 | Done = true; |
| 2773 | } |
| 2774 | |
| 2775 | void MachORebaseEntry::moveNext() { |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2776 | ErrorAsOutParameter ErrAsOutParam(E); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2777 | // If in the middle of some loop, move to next rebasing in loop. |
| 2778 | SegmentOffset += AdvanceAmount; |
| 2779 | if (RemainingLoopCount) { |
| 2780 | --RemainingLoopCount; |
| 2781 | return; |
| 2782 | } |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2783 | if (Ptr >= Opcodes.end()) { |
| 2784 | if (Opcodes.begin() != Opcodes.end() && Done != true) { |
| 2785 | *E = malformedError("missing REBASE_OPCODE_DONE at end of opcodes"); |
| 2786 | moveToEnd(); |
| 2787 | return; |
| 2788 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2789 | Done = true; |
| 2790 | return; |
| 2791 | } |
| 2792 | bool More = true; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2793 | while (More) { |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2794 | // Parse next opcode and set up next loop. |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2795 | const uint8_t *OpcodeStart = Ptr; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2796 | uint8_t Byte = *Ptr++; |
| 2797 | uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK; |
| 2798 | uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2799 | uint32_t Count, Skip; |
| 2800 | const char *error = nullptr; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2801 | switch (Opcode) { |
| 2802 | case MachO::REBASE_OPCODE_DONE: |
| 2803 | More = false; |
| 2804 | Done = true; |
| 2805 | moveToEnd(); |
| 2806 | DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n"); |
| 2807 | break; |
| 2808 | case MachO::REBASE_OPCODE_SET_TYPE_IMM: |
| 2809 | RebaseType = ImmValue; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2810 | if (RebaseType > MachO::REBASE_TYPE_TEXT_PCREL32) { |
| 2811 | *E = malformedError("for REBASE_OPCODE_SET_TYPE_IMM bad bind type: " + |
| 2812 | Twine((int)RebaseType) + " for opcode at: 0x" + |
| 2813 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2814 | moveToEnd(); |
| 2815 | return; |
| 2816 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2817 | DEBUG_WITH_TYPE( |
| 2818 | "mach-o-rebase", |
| 2819 | llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: " |
| 2820 | << "RebaseType=" << (int) RebaseType << "\n"); |
| 2821 | break; |
| 2822 | case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 2823 | SegmentIndex = ImmValue; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2824 | SegmentOffset = readULEB128(&error); |
| 2825 | if (error) { |
| 2826 | *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 2827 | Twine(error) + " for opcode at: 0x" + |
| 2828 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2829 | moveToEnd(); |
| 2830 | return; |
| 2831 | } |
| 2832 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2833 | true); |
| 2834 | if (error) { |
| 2835 | *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 2836 | Twine(error) + " for opcode at: 0x" + |
| 2837 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2838 | moveToEnd(); |
| 2839 | return; |
| 2840 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2841 | DEBUG_WITH_TYPE( |
| 2842 | "mach-o-rebase", |
| 2843 | llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 2844 | << "SegmentIndex=" << SegmentIndex << ", " |
| 2845 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2846 | << "\n"); |
| 2847 | break; |
| 2848 | case MachO::REBASE_OPCODE_ADD_ADDR_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2849 | SegmentOffset += readULEB128(&error); |
| 2850 | if (error) { |
| 2851 | *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + |
| 2852 | Twine(error) + " for opcode at: 0x" + |
| 2853 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2854 | moveToEnd(); |
| 2855 | return; |
| 2856 | } |
| 2857 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2858 | true); |
| 2859 | if (error) { |
| 2860 | *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + |
| 2861 | Twine(error) + " for opcode at: 0x" + |
| 2862 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2863 | moveToEnd(); |
| 2864 | return; |
| 2865 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2866 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2867 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: " |
| 2868 | << format("SegmentOffset=0x%06X", |
| 2869 | SegmentOffset) << "\n"); |
| 2870 | break; |
| 2871 | case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2872 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2873 | true); |
| 2874 | if (error) { |
| 2875 | *E = malformedError("for REBASE_OPCODE_ADD_ADDR_IMM_SCALED " + |
| 2876 | Twine(error) + " for opcode at: 0x" + |
| 2877 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2878 | moveToEnd(); |
| 2879 | return; |
| 2880 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2881 | SegmentOffset += ImmValue * PointerSize; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2882 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2883 | false); |
| 2884 | if (error) { |
| 2885 | *E = malformedError("for REBASE_OPCODE_ADD_ADDR_IMM_SCALED " |
| 2886 | " (after adding immediate times the pointer size) " + |
| 2887 | Twine(error) + " for opcode at: 0x" + |
| 2888 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2889 | moveToEnd(); |
| 2890 | return; |
| 2891 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2892 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2893 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: " |
| 2894 | << format("SegmentOffset=0x%06X", |
| 2895 | SegmentOffset) << "\n"); |
| 2896 | break; |
| 2897 | case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2898 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2899 | true); |
| 2900 | if (error) { |
| 2901 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_IMM_TIMES " + |
| 2902 | Twine(error) + " for opcode at: 0x" + |
| 2903 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2904 | moveToEnd(); |
| 2905 | return; |
| 2906 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2907 | AdvanceAmount = PointerSize; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2908 | Skip = 0; |
| 2909 | Count = ImmValue; |
| 2910 | if (ImmValue != 0) |
| 2911 | RemainingLoopCount = ImmValue - 1; |
| 2912 | else |
| 2913 | RemainingLoopCount = 0; |
| 2914 | error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize, |
| 2915 | SegmentIndex, SegmentOffset); |
| 2916 | if (error) { |
| 2917 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_IMM_TIMES " |
| 2918 | + Twine(error) + " for opcode at: 0x" + |
| 2919 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2920 | moveToEnd(); |
| 2921 | return; |
| 2922 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2923 | DEBUG_WITH_TYPE( |
| 2924 | "mach-o-rebase", |
| 2925 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: " |
| 2926 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2927 | << ", AdvanceAmount=" << AdvanceAmount |
| 2928 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2929 | << "\n"); |
| 2930 | return; |
| 2931 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2932 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2933 | true); |
| 2934 | if (error) { |
| 2935 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " + |
| 2936 | Twine(error) + " for opcode at: 0x" + |
| 2937 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2938 | moveToEnd(); |
| 2939 | return; |
| 2940 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2941 | AdvanceAmount = PointerSize; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2942 | Skip = 0; |
| 2943 | Count = readULEB128(&error); |
| 2944 | if (error) { |
| 2945 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " + |
| 2946 | Twine(error) + " for opcode at: 0x" + |
| 2947 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2948 | moveToEnd(); |
| 2949 | return; |
| 2950 | } |
| 2951 | if (Count != 0) |
| 2952 | RemainingLoopCount = Count - 1; |
| 2953 | else |
| 2954 | RemainingLoopCount = 0; |
| 2955 | error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize, |
| 2956 | SegmentIndex, SegmentOffset); |
| 2957 | if (error) { |
| 2958 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " |
| 2959 | + Twine(error) + " for opcode at: 0x" + |
| 2960 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2961 | moveToEnd(); |
| 2962 | return; |
| 2963 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2964 | DEBUG_WITH_TYPE( |
| 2965 | "mach-o-rebase", |
| 2966 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: " |
| 2967 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2968 | << ", AdvanceAmount=" << AdvanceAmount |
| 2969 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2970 | << "\n"); |
| 2971 | return; |
| 2972 | case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2973 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 2974 | true); |
| 2975 | if (error) { |
| 2976 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " + |
| 2977 | Twine(error) + " for opcode at: 0x" + |
| 2978 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2979 | moveToEnd(); |
| 2980 | return; |
| 2981 | } |
| 2982 | Skip = readULEB128(&error); |
| 2983 | if (error) { |
| 2984 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " + |
| 2985 | Twine(error) + " for opcode at: 0x" + |
| 2986 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2987 | moveToEnd(); |
| 2988 | return; |
| 2989 | } |
| 2990 | AdvanceAmount = Skip + PointerSize; |
| 2991 | Count = 1; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2992 | RemainingLoopCount = 0; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 2993 | error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize, |
| 2994 | SegmentIndex, SegmentOffset); |
| 2995 | if (error) { |
| 2996 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " |
| 2997 | + Twine(error) + " for opcode at: 0x" + |
| 2998 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 2999 | moveToEnd(); |
| 3000 | return; |
| 3001 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3002 | DEBUG_WITH_TYPE( |
| 3003 | "mach-o-rebase", |
| 3004 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: " |
| 3005 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 3006 | << ", AdvanceAmount=" << AdvanceAmount |
| 3007 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3008 | << "\n"); |
| 3009 | return; |
| 3010 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3011 | error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, |
| 3012 | true); |
| 3013 | if (error) { |
| 3014 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3015 | "ULEB " + Twine(error) + " for opcode at: 0x" + |
| 3016 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3017 | moveToEnd(); |
| 3018 | return; |
| 3019 | } |
| 3020 | Count = readULEB128(&error); |
| 3021 | if (error) { |
| 3022 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3023 | "ULEB " + Twine(error) + " for opcode at: 0x" + |
| 3024 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3025 | moveToEnd(); |
| 3026 | return; |
| 3027 | } |
| 3028 | if (Count != 0) |
| 3029 | RemainingLoopCount = Count - 1; |
| 3030 | else |
| 3031 | RemainingLoopCount = 0; |
| 3032 | Skip = readULEB128(&error); |
| 3033 | if (error) { |
| 3034 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3035 | "ULEB " + Twine(error) + " for opcode at: 0x" + |
| 3036 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3037 | moveToEnd(); |
| 3038 | return; |
| 3039 | } |
| 3040 | AdvanceAmount = Skip + PointerSize; |
| 3041 | |
| 3042 | error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize, |
| 3043 | SegmentIndex, SegmentOffset); |
| 3044 | if (error) { |
| 3045 | *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_" |
| 3046 | "ULEB " + Twine(error) + " for opcode at: 0x" + |
| 3047 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3048 | moveToEnd(); |
| 3049 | return; |
| 3050 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3051 | DEBUG_WITH_TYPE( |
| 3052 | "mach-o-rebase", |
| 3053 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: " |
| 3054 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 3055 | << ", AdvanceAmount=" << AdvanceAmount |
| 3056 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3057 | << "\n"); |
| 3058 | return; |
| 3059 | default: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3060 | *E = malformedError("bad rebase info (bad opcode value 0x" + |
| 3061 | utohexstr(Opcode) + " for opcode at: 0x" + |
| 3062 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3063 | moveToEnd(); |
| 3064 | return; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3069 | uint64_t MachORebaseEntry::readULEB128(const char **error) { |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3070 | unsigned Count; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3071 | uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3072 | Ptr += Count; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3073 | if (Ptr > Opcodes.end()) |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3074 | Ptr = Opcodes.end(); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3075 | return Result; |
| 3076 | } |
| 3077 | |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3078 | int32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3079 | |
| 3080 | uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; } |
| 3081 | |
| 3082 | StringRef MachORebaseEntry::typeName() const { |
| 3083 | switch (RebaseType) { |
| 3084 | case MachO::REBASE_TYPE_POINTER: |
| 3085 | return "pointer"; |
| 3086 | case MachO::REBASE_TYPE_TEXT_ABSOLUTE32: |
| 3087 | return "text abs32"; |
| 3088 | case MachO::REBASE_TYPE_TEXT_PCREL32: |
| 3089 | return "text rel32"; |
| 3090 | } |
| 3091 | return "unknown"; |
| 3092 | } |
| 3093 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3094 | // For use with the SegIndex of a checked Mach-O Rebase entry |
| 3095 | // to get the segment name. |
| 3096 | StringRef MachORebaseEntry::segmentName() const { |
| 3097 | return O->BindRebaseSegmentName(SegmentIndex); |
| 3098 | } |
| 3099 | |
| 3100 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry |
| 3101 | // to get the section name. |
| 3102 | StringRef MachORebaseEntry::sectionName() const { |
| 3103 | return O->BindRebaseSectionName(SegmentIndex, SegmentOffset); |
| 3104 | } |
| 3105 | |
| 3106 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry |
| 3107 | // to get the address. |
| 3108 | uint64_t MachORebaseEntry::address() const { |
| 3109 | return O->BindRebaseAddress(SegmentIndex, SegmentOffset); |
| 3110 | } |
| 3111 | |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3112 | bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const { |
Saleem Abdulrasool | 1d84d9a | 2017-01-08 19:14:15 +0000 | [diff] [blame] | 3113 | #ifdef EXPENSIVE_CHECKS |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3114 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
Saleem Abdulrasool | 1d84d9a | 2017-01-08 19:14:15 +0000 | [diff] [blame] | 3115 | #else |
| 3116 | assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files"); |
| 3117 | #endif |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3118 | return (Ptr == Other.Ptr) && |
| 3119 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 3120 | (Done == Other.Done); |
| 3121 | } |
| 3122 | |
| 3123 | iterator_range<rebase_iterator> |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3124 | MachOObjectFile::rebaseTable(Error &Err, MachOObjectFile *O, |
| 3125 | ArrayRef<uint8_t> Opcodes, bool is64) { |
| 3126 | if (O->BindRebaseSectionTable == nullptr) |
| 3127 | O->BindRebaseSectionTable = llvm::make_unique<BindRebaseSegInfo>(O); |
| 3128 | MachORebaseEntry Start(&Err, O, Opcodes, is64); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3129 | Start.moveToFirst(); |
| 3130 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3131 | MachORebaseEntry Finish(&Err, O, Opcodes, is64); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3132 | Finish.moveToEnd(); |
| 3133 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 3134 | return make_range(rebase_iterator(Start), rebase_iterator(Finish)); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3137 | iterator_range<rebase_iterator> MachOObjectFile::rebaseTable(Error &Err) { |
| 3138 | return rebaseTable(Err, this, getDyldInfoRebaseOpcodes(), is64Bit()); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3141 | MachOBindEntry::MachOBindEntry(Error *E, const MachOObjectFile *O, |
| 3142 | ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK) |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3143 | : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), |
| 3144 | SegmentIndex(-1), LibraryOrdinalSet(false), Ordinal(0), Flags(0), |
| 3145 | Addend(0), RemainingLoopCount(0), AdvanceAmount(0), BindType(0), |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3146 | PointerSize(is64Bit ? 8 : 4), TableKind(BK), Done(false) {} |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3147 | |
| 3148 | void MachOBindEntry::moveToFirst() { |
| 3149 | Ptr = Opcodes.begin(); |
| 3150 | moveNext(); |
| 3151 | } |
| 3152 | |
| 3153 | void MachOBindEntry::moveToEnd() { |
| 3154 | Ptr = Opcodes.end(); |
| 3155 | RemainingLoopCount = 0; |
| 3156 | Done = true; |
| 3157 | } |
| 3158 | |
| 3159 | void MachOBindEntry::moveNext() { |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3160 | ErrorAsOutParameter ErrAsOutParam(E); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3161 | // If in the middle of some loop, move to next binding in loop. |
| 3162 | SegmentOffset += AdvanceAmount; |
| 3163 | if (RemainingLoopCount) { |
| 3164 | --RemainingLoopCount; |
| 3165 | return; |
| 3166 | } |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3167 | if (Ptr >= Opcodes.end()) { |
| 3168 | if (Opcodes.begin() != Opcodes.end() && Done != true) { |
| 3169 | *E = malformedError("missing BIND_OPCODE_DONE at end of opcodes"); |
| 3170 | moveToEnd(); |
| 3171 | return; |
| 3172 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3173 | Done = true; |
| 3174 | return; |
| 3175 | } |
| 3176 | bool More = true; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3177 | while (More) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3178 | // Parse next opcode and set up next loop. |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3179 | const uint8_t *OpcodeStart = Ptr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3180 | uint8_t Byte = *Ptr++; |
| 3181 | uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK; |
| 3182 | uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK; |
| 3183 | int8_t SignExtended; |
| 3184 | const uint8_t *SymStart; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3185 | uint32_t Count, Skip; |
| 3186 | const char *error = nullptr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3187 | switch (Opcode) { |
| 3188 | case MachO::BIND_OPCODE_DONE: |
| 3189 | if (TableKind == Kind::Lazy) { |
| 3190 | // Lazying bindings have a DONE opcode between entries. Need to ignore |
| 3191 | // it to advance to next entry. But need not if this is last entry. |
| 3192 | bool NotLastEntry = false; |
| 3193 | for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) { |
| 3194 | if (*P) { |
| 3195 | NotLastEntry = true; |
| 3196 | } |
| 3197 | } |
| 3198 | if (NotLastEntry) |
| 3199 | break; |
| 3200 | } |
| 3201 | More = false; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3202 | moveToEnd(); |
| 3203 | DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n"); |
| 3204 | break; |
| 3205 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3206 | if (TableKind == Kind::Weak) { |
| 3207 | *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM not allowed in " |
| 3208 | "weak bind table for opcode at: 0x" + |
| 3209 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3210 | moveToEnd(); |
| 3211 | return; |
| 3212 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3213 | Ordinal = ImmValue; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3214 | LibraryOrdinalSet = true; |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3215 | if (ImmValue > O->getLibraryCount()) { |
| 3216 | *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad " |
| 3217 | "library ordinal: " + Twine((int)ImmValue) + " (max " + |
| 3218 | Twine((int)O->getLibraryCount()) + ") for opcode at: 0x" + |
| 3219 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3220 | moveToEnd(); |
| 3221 | return; |
| 3222 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3223 | DEBUG_WITH_TYPE( |
| 3224 | "mach-o-bind", |
| 3225 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: " |
| 3226 | << "Ordinal=" << Ordinal << "\n"); |
| 3227 | break; |
| 3228 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3229 | if (TableKind == Kind::Weak) { |
| 3230 | *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB not allowed in " |
| 3231 | "weak bind table for opcode at: 0x" + |
| 3232 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3233 | moveToEnd(); |
| 3234 | return; |
| 3235 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3236 | Ordinal = readULEB128(&error); |
| 3237 | LibraryOrdinalSet = true; |
| 3238 | if (error) { |
| 3239 | *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB " + |
| 3240 | Twine(error) + " for opcode at: 0x" + |
| 3241 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3242 | moveToEnd(); |
| 3243 | return; |
| 3244 | } |
| 3245 | if (Ordinal > (int)O->getLibraryCount()) { |
| 3246 | *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad " |
| 3247 | "library ordinal: " + Twine((int)Ordinal) + " (max " + |
| 3248 | Twine((int)O->getLibraryCount()) + ") for opcode at: 0x" + |
| 3249 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3250 | moveToEnd(); |
| 3251 | return; |
| 3252 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3253 | DEBUG_WITH_TYPE( |
| 3254 | "mach-o-bind", |
| 3255 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: " |
| 3256 | << "Ordinal=" << Ordinal << "\n"); |
| 3257 | break; |
| 3258 | case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3259 | if (TableKind == Kind::Weak) { |
| 3260 | *E = malformedError("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM not allowed in " |
| 3261 | "weak bind table for opcode at: 0x" + |
| 3262 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3263 | moveToEnd(); |
| 3264 | return; |
| 3265 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3266 | if (ImmValue) { |
| 3267 | SignExtended = MachO::BIND_OPCODE_MASK | ImmValue; |
| 3268 | Ordinal = SignExtended; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3269 | LibraryOrdinalSet = true; |
| 3270 | if (Ordinal < MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP) { |
| 3271 | *E = malformedError("for BIND_OPCODE_SET_DYLIB_SPECIAL_IMM unknown " |
| 3272 | "special ordinal: " + Twine((int)Ordinal) + " for opcode at: " |
| 3273 | "0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3274 | moveToEnd(); |
| 3275 | return; |
| 3276 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3277 | } else |
| 3278 | Ordinal = 0; |
| 3279 | DEBUG_WITH_TYPE( |
| 3280 | "mach-o-bind", |
| 3281 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: " |
| 3282 | << "Ordinal=" << Ordinal << "\n"); |
| 3283 | break; |
| 3284 | case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: |
| 3285 | Flags = ImmValue; |
| 3286 | SymStart = Ptr; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3287 | while (*Ptr && (Ptr < Opcodes.end())) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3288 | ++Ptr; |
| 3289 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3290 | if (Ptr == Opcodes.end()) { |
| 3291 | *E = malformedError("for BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM " |
| 3292 | "symbol name extends past opcodes for opcode at: 0x" + |
| 3293 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3294 | moveToEnd(); |
| 3295 | return; |
| 3296 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3297 | SymbolName = StringRef(reinterpret_cast<const char*>(SymStart), |
| 3298 | Ptr-SymStart); |
Nick Kledzik | a637536 | 2014-09-17 01:51:43 +0000 | [diff] [blame] | 3299 | ++Ptr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3300 | DEBUG_WITH_TYPE( |
| 3301 | "mach-o-bind", |
| 3302 | llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: " |
| 3303 | << "SymbolName=" << SymbolName << "\n"); |
| 3304 | if (TableKind == Kind::Weak) { |
| 3305 | if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) |
| 3306 | return; |
| 3307 | } |
| 3308 | break; |
| 3309 | case MachO::BIND_OPCODE_SET_TYPE_IMM: |
| 3310 | BindType = ImmValue; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3311 | if (ImmValue > MachO::BIND_TYPE_TEXT_PCREL32) { |
| 3312 | *E = malformedError("for BIND_OPCODE_SET_TYPE_IMM bad bind type: " + |
| 3313 | Twine((int)ImmValue) + " for opcode at: 0x" + |
| 3314 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3315 | moveToEnd(); |
| 3316 | return; |
| 3317 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3318 | DEBUG_WITH_TYPE( |
| 3319 | "mach-o-bind", |
| 3320 | llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: " |
| 3321 | << "BindType=" << (int)BindType << "\n"); |
| 3322 | break; |
| 3323 | case MachO::BIND_OPCODE_SET_ADDEND_SLEB: |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3324 | Addend = readSLEB128(&error); |
| 3325 | if (error) { |
| 3326 | *E = malformedError("for BIND_OPCODE_SET_ADDEND_SLEB " + |
| 3327 | Twine(error) + " for opcode at: 0x" + |
| 3328 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3329 | moveToEnd(); |
| 3330 | return; |
| 3331 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3332 | DEBUG_WITH_TYPE( |
| 3333 | "mach-o-bind", |
| 3334 | llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: " |
| 3335 | << "Addend=" << Addend << "\n"); |
| 3336 | break; |
| 3337 | case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 3338 | SegmentIndex = ImmValue; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3339 | SegmentOffset = readULEB128(&error); |
| 3340 | if (error) { |
| 3341 | *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 3342 | Twine(error) + " for opcode at: 0x" + |
| 3343 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3344 | moveToEnd(); |
| 3345 | return; |
| 3346 | } |
| 3347 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3348 | if (error) { |
| 3349 | *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " + |
| 3350 | Twine(error) + " for opcode at: 0x" + |
| 3351 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3352 | moveToEnd(); |
| 3353 | return; |
| 3354 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3355 | DEBUG_WITH_TYPE( |
| 3356 | "mach-o-bind", |
| 3357 | llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 3358 | << "SegmentIndex=" << SegmentIndex << ", " |
| 3359 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 3360 | << "\n"); |
| 3361 | break; |
| 3362 | case MachO::BIND_OPCODE_ADD_ADDR_ULEB: |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3363 | SegmentOffset += readULEB128(&error); |
| 3364 | if (error) { |
| 3365 | *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + |
| 3366 | Twine(error) + " for opcode at: 0x" + |
| 3367 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3368 | moveToEnd(); |
| 3369 | return; |
| 3370 | } |
| 3371 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3372 | if (error) { |
| 3373 | *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + |
| 3374 | Twine(error) + " for opcode at: 0x" + |
| 3375 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3376 | moveToEnd(); |
| 3377 | return; |
| 3378 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3379 | DEBUG_WITH_TYPE("mach-o-bind", |
| 3380 | llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: " |
| 3381 | << format("SegmentOffset=0x%06X", |
| 3382 | SegmentOffset) << "\n"); |
| 3383 | break; |
| 3384 | case MachO::BIND_OPCODE_DO_BIND: |
| 3385 | AdvanceAmount = PointerSize; |
| 3386 | RemainingLoopCount = 0; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3387 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3388 | if (error) { |
| 3389 | *E = malformedError("for BIND_OPCODE_DO_BIND " + Twine(error) + |
| 3390 | " for opcode at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3391 | moveToEnd(); |
| 3392 | return; |
| 3393 | } |
| 3394 | if (SymbolName == StringRef()) { |
| 3395 | *E = malformedError("for BIND_OPCODE_DO_BIND missing preceding " |
| 3396 | "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode at: 0x" + |
| 3397 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3398 | moveToEnd(); |
| 3399 | return; |
| 3400 | } |
| 3401 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 3402 | *E = malformedError("for BIND_OPCODE_DO_BIND missing preceding " |
| 3403 | "BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" + |
| 3404 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3405 | moveToEnd(); |
| 3406 | return; |
| 3407 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3408 | DEBUG_WITH_TYPE("mach-o-bind", |
| 3409 | llvm::dbgs() << "BIND_OPCODE_DO_BIND: " |
| 3410 | << format("SegmentOffset=0x%06X", |
| 3411 | SegmentOffset) << "\n"); |
| 3412 | return; |
| 3413 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3414 | if (TableKind == Kind::Lazy) { |
| 3415 | *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB not allowed in " |
| 3416 | "lazy bind table for opcode at: 0x" + |
| 3417 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3418 | moveToEnd(); |
| 3419 | return; |
| 3420 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3421 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3422 | if (error) { |
| 3423 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " + |
| 3424 | Twine(error) + " for opcode at: 0x" + |
| 3425 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3426 | moveToEnd(); |
| 3427 | return; |
| 3428 | } |
| 3429 | if (SymbolName == StringRef()) { |
| 3430 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing " |
| 3431 | "preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode " |
| 3432 | "at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3433 | moveToEnd(); |
| 3434 | return; |
| 3435 | } |
| 3436 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 3437 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing " |
| 3438 | "preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" + |
| 3439 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3440 | moveToEnd(); |
| 3441 | return; |
| 3442 | } |
| 3443 | AdvanceAmount = readULEB128(&error) + PointerSize; |
| 3444 | if (error) { |
| 3445 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " + |
| 3446 | Twine(error) + " for opcode at: 0x" + |
| 3447 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3448 | moveToEnd(); |
| 3449 | return; |
| 3450 | } |
| 3451 | // Note, this is not really an error until the next bind but make no sense |
| 3452 | // for a BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB to not be followed by another |
| 3453 | // bind operation. |
| 3454 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset + |
| 3455 | AdvanceAmount, false); |
| 3456 | if (error) { |
| 3457 | *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB (after adding " |
| 3458 | "ULEB) " + Twine(error) + " for opcode at: 0x" + |
| 3459 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3460 | moveToEnd(); |
| 3461 | return; |
| 3462 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3463 | RemainingLoopCount = 0; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3464 | DEBUG_WITH_TYPE( |
| 3465 | "mach-o-bind", |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 3466 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: " |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3467 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 3468 | << ", AdvanceAmount=" << AdvanceAmount |
| 3469 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3470 | << "\n"); |
| 3471 | return; |
| 3472 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3473 | if (TableKind == Kind::Lazy) { |
| 3474 | *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED not " |
| 3475 | "allowed in lazy bind table for opcode at: 0x" + |
| 3476 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3477 | moveToEnd(); |
| 3478 | return; |
| 3479 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3480 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3481 | if (error) { |
| 3482 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " + |
| 3483 | Twine(error) + " for opcode at: 0x" + |
| 3484 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3485 | moveToEnd(); |
| 3486 | return; |
| 3487 | } |
| 3488 | if (SymbolName == StringRef()) { |
| 3489 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " |
| 3490 | "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for " |
| 3491 | "opcode at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3492 | moveToEnd(); |
| 3493 | return; |
| 3494 | } |
| 3495 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 3496 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " |
| 3497 | "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode " |
| 3498 | "at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3499 | moveToEnd(); |
| 3500 | return; |
| 3501 | } |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 3502 | AdvanceAmount = ImmValue * PointerSize + PointerSize; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3503 | RemainingLoopCount = 0; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3504 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset + |
| 3505 | AdvanceAmount, false); |
| 3506 | if (error) { |
| 3507 | *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " |
| 3508 | " (after adding immediate times the pointer size) " + |
| 3509 | Twine(error) + " for opcode at: 0x" + |
| 3510 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3511 | moveToEnd(); |
| 3512 | return; |
| 3513 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3514 | DEBUG_WITH_TYPE("mach-o-bind", |
| 3515 | llvm::dbgs() |
| 3516 | << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: " |
| 3517 | << format("SegmentOffset=0x%06X", |
| 3518 | SegmentOffset) << "\n"); |
| 3519 | return; |
| 3520 | case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3521 | if (TableKind == Kind::Lazy) { |
| 3522 | *E = malformedError("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB not " |
| 3523 | "allowed in lazy bind table for opcode at: 0x" + |
| 3524 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3525 | moveToEnd(); |
| 3526 | return; |
| 3527 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3528 | Count = readULEB128(&error); |
| 3529 | if (Count != 0) |
| 3530 | RemainingLoopCount = Count - 1; |
| 3531 | else |
| 3532 | RemainingLoopCount = 0; |
| 3533 | if (error) { |
| 3534 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3535 | " (count value) " + Twine(error) + " for opcode at" |
| 3536 | ": 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3537 | moveToEnd(); |
| 3538 | return; |
| 3539 | } |
| 3540 | Skip = readULEB128(&error); |
| 3541 | AdvanceAmount = Skip + PointerSize; |
| 3542 | if (error) { |
| 3543 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3544 | " (skip value) " + Twine(error) + " for opcode at" |
| 3545 | ": 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3546 | moveToEnd(); |
| 3547 | return; |
| 3548 | } |
| 3549 | error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true); |
| 3550 | if (error) { |
| 3551 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3552 | + Twine(error) + " for opcode at: 0x" + |
| 3553 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3554 | moveToEnd(); |
| 3555 | return; |
| 3556 | } |
| 3557 | if (SymbolName == StringRef()) { |
| 3558 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3559 | "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for " |
| 3560 | "opcode at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3561 | moveToEnd(); |
| 3562 | return; |
| 3563 | } |
| 3564 | if (!LibraryOrdinalSet && TableKind != Kind::Weak) { |
| 3565 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3566 | "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode " |
| 3567 | "at: 0x" + utohexstr(OpcodeStart - Opcodes.begin())); |
| 3568 | moveToEnd(); |
| 3569 | return; |
| 3570 | } |
| 3571 | error = O->BindEntryCheckCountAndSkip(Count, Skip, PointerSize, |
| 3572 | SegmentIndex, SegmentOffset); |
| 3573 | if (error) { |
| 3574 | *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " |
| 3575 | + Twine(error) + " for opcode at: 0x" + |
| 3576 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3577 | moveToEnd(); |
| 3578 | return; |
| 3579 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3580 | DEBUG_WITH_TYPE( |
| 3581 | "mach-o-bind", |
| 3582 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: " |
| 3583 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 3584 | << ", AdvanceAmount=" << AdvanceAmount |
| 3585 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 3586 | << "\n"); |
| 3587 | return; |
| 3588 | default: |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3589 | *E = malformedError("bad bind info (bad opcode value 0x" + |
| 3590 | utohexstr(Opcode) + " for opcode at: 0x" + |
| 3591 | utohexstr(OpcodeStart - Opcodes.begin())); |
| 3592 | moveToEnd(); |
| 3593 | return; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3598 | uint64_t MachOBindEntry::readULEB128(const char **error) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3599 | unsigned Count; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3600 | uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3601 | Ptr += Count; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3602 | if (Ptr > Opcodes.end()) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3603 | Ptr = Opcodes.end(); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3604 | return Result; |
| 3605 | } |
| 3606 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3607 | int64_t MachOBindEntry::readSLEB128(const char **error) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3608 | unsigned Count; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3609 | int64_t Result = decodeSLEB128(Ptr, &Count, Opcodes.end(), error); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3610 | Ptr += Count; |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3611 | if (Ptr > Opcodes.end()) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3612 | Ptr = Opcodes.end(); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3613 | return Result; |
| 3614 | } |
| 3615 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3616 | int32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3617 | |
| 3618 | uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; } |
| 3619 | |
| 3620 | StringRef MachOBindEntry::typeName() const { |
| 3621 | switch (BindType) { |
| 3622 | case MachO::BIND_TYPE_POINTER: |
| 3623 | return "pointer"; |
| 3624 | case MachO::BIND_TYPE_TEXT_ABSOLUTE32: |
| 3625 | return "text abs32"; |
| 3626 | case MachO::BIND_TYPE_TEXT_PCREL32: |
| 3627 | return "text rel32"; |
| 3628 | } |
| 3629 | return "unknown"; |
| 3630 | } |
| 3631 | |
| 3632 | StringRef MachOBindEntry::symbolName() const { return SymbolName; } |
| 3633 | |
| 3634 | int64_t MachOBindEntry::addend() const { return Addend; } |
| 3635 | |
| 3636 | uint32_t MachOBindEntry::flags() const { return Flags; } |
| 3637 | |
| 3638 | int MachOBindEntry::ordinal() const { return Ordinal; } |
| 3639 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3640 | // For use with the SegIndex of a checked Mach-O Bind entry |
| 3641 | // to get the segment name. |
| 3642 | StringRef MachOBindEntry::segmentName() const { |
| 3643 | return O->BindRebaseSegmentName(SegmentIndex); |
| 3644 | } |
| 3645 | |
| 3646 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry |
| 3647 | // to get the section name. |
| 3648 | StringRef MachOBindEntry::sectionName() const { |
| 3649 | return O->BindRebaseSectionName(SegmentIndex, SegmentOffset); |
| 3650 | } |
| 3651 | |
| 3652 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry |
| 3653 | // to get the address. |
| 3654 | uint64_t MachOBindEntry::address() const { |
| 3655 | return O->BindRebaseAddress(SegmentIndex, SegmentOffset); |
| 3656 | } |
| 3657 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3658 | bool MachOBindEntry::operator==(const MachOBindEntry &Other) const { |
Saleem Abdulrasool | 1d84d9a | 2017-01-08 19:14:15 +0000 | [diff] [blame] | 3659 | #ifdef EXPENSIVE_CHECKS |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3660 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
Saleem Abdulrasool | 1d84d9a | 2017-01-08 19:14:15 +0000 | [diff] [blame] | 3661 | #else |
| 3662 | assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files"); |
| 3663 | #endif |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3664 | return (Ptr == Other.Ptr) && |
| 3665 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 3666 | (Done == Other.Done); |
| 3667 | } |
| 3668 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3669 | // Build table of sections so SegIndex/SegOffset pairs can be translated. |
| 3670 | BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) { |
| 3671 | uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0; |
| 3672 | StringRef CurSegName; |
| 3673 | uint64_t CurSegAddress; |
| 3674 | for (const SectionRef &Section : Obj->sections()) { |
| 3675 | SectionInfo Info; |
| 3676 | Section.getName(Info.SectionName); |
| 3677 | Info.Address = Section.getAddress(); |
| 3678 | Info.Size = Section.getSize(); |
| 3679 | Info.SegmentName = |
| 3680 | Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl()); |
| 3681 | if (!Info.SegmentName.equals(CurSegName)) { |
| 3682 | ++CurSegIndex; |
| 3683 | CurSegName = Info.SegmentName; |
| 3684 | CurSegAddress = Info.Address; |
| 3685 | } |
| 3686 | Info.SegmentIndex = CurSegIndex - 1; |
| 3687 | Info.OffsetInSegment = Info.Address - CurSegAddress; |
| 3688 | Info.SegmentStartAddress = CurSegAddress; |
| 3689 | Sections.push_back(Info); |
| 3690 | } |
| 3691 | MaxSegIndex = CurSegIndex; |
| 3692 | } |
| 3693 | |
| 3694 | // For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3695 | // validate a MachOBindEntry or MachORebaseEntry. |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3696 | const char * BindRebaseSegInfo::checkSegAndOffset(int32_t SegIndex, |
| 3697 | uint64_t SegOffset, |
| 3698 | bool endInvalid) { |
| 3699 | if (SegIndex == -1) |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3700 | return "missing preceding *_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB"; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3701 | if (SegIndex >= MaxSegIndex) |
| 3702 | return "bad segIndex (too large)"; |
| 3703 | for (const SectionInfo &SI : Sections) { |
| 3704 | if (SI.SegmentIndex != SegIndex) |
| 3705 | continue; |
| 3706 | if (SI.OffsetInSegment > SegOffset) |
| 3707 | continue; |
| 3708 | if (SegOffset > (SI.OffsetInSegment + SI.Size)) |
| 3709 | continue; |
| 3710 | if (endInvalid && SegOffset >= (SI.OffsetInSegment + SI.Size)) |
| 3711 | continue; |
| 3712 | return nullptr; |
| 3713 | } |
| 3714 | return "bad segOffset, too large"; |
| 3715 | } |
| 3716 | |
| 3717 | // For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3718 | // the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode and for use in |
| 3719 | // MachORebaseEntry::moveNext() to validate a MachORebaseEntry for |
| 3720 | // REBASE_OPCODE_DO_*_TIMES* opcodes. The SegIndex and SegOffset must have |
| 3721 | // been already checked. |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3722 | const char * BindRebaseSegInfo::checkCountAndSkip(uint32_t Count, uint32_t Skip, |
| 3723 | uint8_t PointerSize, |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3724 | int32_t SegIndex, |
| 3725 | uint64_t SegOffset) { |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3726 | const SectionInfo &SI = findSection(SegIndex, SegOffset); |
| 3727 | uint64_t addr = SI.SegmentStartAddress + SegOffset; |
| 3728 | if (addr >= SI.Address + SI.Size) |
| 3729 | return "bad segOffset, too large"; |
| 3730 | uint64_t i = 0; |
| 3731 | if (Count > 1) |
| 3732 | i = (Skip + PointerSize) * (Count - 1); |
Kevin Enderby | 6c1d2b4 | 2017-03-27 20:09:23 +0000 | [diff] [blame] | 3733 | else if (Count == 1) |
| 3734 | i = Skip + PointerSize; |
| 3735 | if (addr + i >= SI.Address + SI.Size) { |
| 3736 | // For rebase opcodes they can step from one section to another. |
| 3737 | uint64_t TrailingSegOffset = (addr + i) - SI.SegmentStartAddress; |
| 3738 | const char *error = checkSegAndOffset(SegIndex, TrailingSegOffset, false); |
| 3739 | if (error) |
| 3740 | return "bad count and skip, too large"; |
| 3741 | } |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3742 | return nullptr; |
| 3743 | } |
| 3744 | |
| 3745 | // For use with the SegIndex of a checked Mach-O Bind or Rebase entry |
| 3746 | // to get the segment name. |
| 3747 | StringRef BindRebaseSegInfo::segmentName(int32_t SegIndex) { |
| 3748 | for (const SectionInfo &SI : Sections) { |
| 3749 | if (SI.SegmentIndex == SegIndex) |
| 3750 | return SI.SegmentName; |
| 3751 | } |
| 3752 | llvm_unreachable("invalid SegIndex"); |
| 3753 | } |
| 3754 | |
| 3755 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 3756 | // to get the SectionInfo. |
| 3757 | const BindRebaseSegInfo::SectionInfo &BindRebaseSegInfo::findSection( |
| 3758 | int32_t SegIndex, uint64_t SegOffset) { |
| 3759 | for (const SectionInfo &SI : Sections) { |
| 3760 | if (SI.SegmentIndex != SegIndex) |
| 3761 | continue; |
| 3762 | if (SI.OffsetInSegment > SegOffset) |
| 3763 | continue; |
| 3764 | if (SegOffset >= (SI.OffsetInSegment + SI.Size)) |
| 3765 | continue; |
| 3766 | return SI; |
| 3767 | } |
| 3768 | llvm_unreachable("SegIndex and SegOffset not in any section"); |
| 3769 | } |
| 3770 | |
| 3771 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 3772 | // entry to get the section name. |
| 3773 | StringRef BindRebaseSegInfo::sectionName(int32_t SegIndex, |
| 3774 | uint64_t SegOffset) { |
| 3775 | return findSection(SegIndex, SegOffset).SectionName; |
| 3776 | } |
| 3777 | |
| 3778 | // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase |
| 3779 | // entry to get the address. |
| 3780 | uint64_t BindRebaseSegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 3781 | const SectionInfo &SI = findSection(SegIndex, OffsetInSeg); |
| 3782 | return SI.SegmentStartAddress + OffsetInSeg; |
| 3783 | } |
| 3784 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3785 | iterator_range<bind_iterator> |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3786 | MachOObjectFile::bindTable(Error &Err, MachOObjectFile *O, |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3787 | ArrayRef<uint8_t> Opcodes, bool is64, |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3788 | MachOBindEntry::Kind BKind) { |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3789 | if (O->BindRebaseSectionTable == nullptr) |
| 3790 | O->BindRebaseSectionTable = llvm::make_unique<BindRebaseSegInfo>(O); |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3791 | MachOBindEntry Start(&Err, O, Opcodes, is64, BKind); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3792 | Start.moveToFirst(); |
| 3793 | |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3794 | MachOBindEntry Finish(&Err, O, Opcodes, is64, BKind); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3795 | Finish.moveToEnd(); |
| 3796 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 3797 | return make_range(bind_iterator(Start), bind_iterator(Finish)); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3800 | iterator_range<bind_iterator> MachOObjectFile::bindTable(Error &Err) { |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3801 | return bindTable(Err, this, getDyldInfoBindOpcodes(), is64Bit(), |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3802 | MachOBindEntry::Kind::Regular); |
| 3803 | } |
| 3804 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3805 | iterator_range<bind_iterator> MachOObjectFile::lazyBindTable(Error &Err) { |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3806 | return bindTable(Err, this, getDyldInfoLazyBindOpcodes(), is64Bit(), |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3807 | MachOBindEntry::Kind::Lazy); |
| 3808 | } |
| 3809 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 3810 | iterator_range<bind_iterator> MachOObjectFile::weakBindTable(Error &Err) { |
Kevin Enderby | feb63b9 | 2017-02-28 21:47:07 +0000 | [diff] [blame] | 3811 | return bindTable(Err, this, getDyldInfoWeakBindOpcodes(), is64Bit(), |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 3812 | MachOBindEntry::Kind::Weak); |
| 3813 | } |
| 3814 | |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 3815 | MachOObjectFile::load_command_iterator |
| 3816 | MachOObjectFile::begin_load_commands() const { |
| 3817 | return LoadCommands.begin(); |
| 3818 | } |
| 3819 | |
| 3820 | MachOObjectFile::load_command_iterator |
| 3821 | MachOObjectFile::end_load_commands() const { |
| 3822 | return LoadCommands.end(); |
| 3823 | } |
| 3824 | |
| 3825 | iterator_range<MachOObjectFile::load_command_iterator> |
| 3826 | MachOObjectFile::load_commands() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 3827 | return make_range(begin_load_commands(), end_load_commands()); |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3830 | StringRef |
| 3831 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 3832 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 3833 | return parseSegmentOrSectionName(Raw.data()); |
| 3834 | } |
| 3835 | |
| 3836 | ArrayRef<char> |
| 3837 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 3838 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3839 | const section_base *Base = |
| 3840 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 3841 | return makeArrayRef(Base->sectname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | ArrayRef<char> |
| 3845 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 3846 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3847 | const section_base *Base = |
| 3848 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 3849 | return makeArrayRef(Base->segname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | bool |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3853 | MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3854 | const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3855 | if (getCPUType(*this) == MachO::CPU_TYPE_X86_64) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3856 | return false; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3857 | return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3860 | unsigned MachOObjectFile::getPlainRelocationSymbolNum( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3861 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3862 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3863 | return RE.r_word1 & 0xffffff; |
| 3864 | return RE.r_word1 >> 8; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3867 | bool MachOObjectFile::getPlainRelocationExternal( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3868 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3869 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3870 | return (RE.r_word1 >> 27) & 1; |
| 3871 | return (RE.r_word1 >> 4) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3874 | bool MachOObjectFile::getScatteredRelocationScattered( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3875 | const MachO::any_relocation_info &RE) const { |
| 3876 | return RE.r_word0 >> 31; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3879 | uint32_t MachOObjectFile::getScatteredRelocationValue( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3880 | const MachO::any_relocation_info &RE) const { |
| 3881 | return RE.r_word1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 3884 | uint32_t MachOObjectFile::getScatteredRelocationType( |
| 3885 | const MachO::any_relocation_info &RE) const { |
| 3886 | return (RE.r_word0 >> 24) & 0xf; |
| 3887 | } |
| 3888 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3889 | unsigned MachOObjectFile::getAnyRelocationAddress( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3890 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3891 | if (isRelocationScattered(RE)) |
| 3892 | return getScatteredRelocationAddress(RE); |
| 3893 | return getPlainRelocationAddress(RE); |
| 3894 | } |
| 3895 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3896 | unsigned MachOObjectFile::getAnyRelocationPCRel( |
| 3897 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3898 | if (isRelocationScattered(RE)) |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3899 | return getScatteredRelocationPCRel(RE); |
| 3900 | return getPlainRelocationPCRel(*this, RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 3903 | unsigned MachOObjectFile::getAnyRelocationLength( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3904 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3905 | if (isRelocationScattered(RE)) |
| 3906 | return getScatteredRelocationLength(RE); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3907 | return getPlainRelocationLength(*this, RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3911 | MachOObjectFile::getAnyRelocationType( |
| 3912 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3913 | if (isRelocationScattered(RE)) |
| 3914 | return getScatteredRelocationType(RE); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3915 | return getPlainRelocationType(*this, RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 3918 | SectionRef |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 3919 | MachOObjectFile::getAnyRelocationSection( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3920 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 3921 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 3922 | return *section_end(); |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 3923 | unsigned SecNum = getPlainRelocationSymbolNum(RE); |
| 3924 | if (SecNum == MachO::R_ABS || SecNum > Sections.size()) |
| 3925 | return *section_end(); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 3926 | DataRefImpl DRI; |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 3927 | DRI.d.a = SecNum - 1; |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 3928 | return SectionRef(DRI, this); |
| 3929 | } |
| 3930 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3931 | MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 3932 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3933 | return getStruct<MachO::section>(*this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3936 | MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 3937 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3938 | return getStruct<MachO::section_64>(*this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3941 | MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3942 | unsigned Index) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3943 | const char *Sec = getSectionPtr(*this, L, Index); |
| 3944 | return getStruct<MachO::section>(*this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3947 | MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 3948 | unsigned Index) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3949 | const char *Sec = getSectionPtr(*this, L, Index); |
| 3950 | return getStruct<MachO::section_64>(*this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3953 | MachO::nlist |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3954 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 3955 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3956 | return getStruct<MachO::nlist>(*this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3959 | MachO::nlist_64 |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3960 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 3961 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3962 | return getStruct<MachO::nlist_64>(*this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3965 | MachO::linkedit_data_command |
| 3966 | MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3967 | return getStruct<MachO::linkedit_data_command>(*this, L.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3970 | MachO::segment_command |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3971 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3972 | return getStruct<MachO::segment_command>(*this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3975 | MachO::segment_command_64 |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3976 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3977 | return getStruct<MachO::segment_command_64>(*this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 3980 | MachO::linker_option_command |
| 3981 | MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3982 | return getStruct<MachO::linker_option_command>(*this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 3985 | MachO::version_min_command |
| 3986 | MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 3987 | return getStruct<MachO::version_min_command>(*this, L.Ptr); |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Kevin Enderby | a4579c4 | 2017-01-19 17:36:31 +0000 | [diff] [blame] | 3990 | MachO::note_command |
| 3991 | MachOObjectFile::getNoteLoadCommand(const LoadCommandInfo &L) const { |
| 3992 | return getStruct<MachO::note_command>(*this, L.Ptr); |
| 3993 | } |
| 3994 | |
Steven Wu | 5b54a42 | 2017-01-23 20:07:55 +0000 | [diff] [blame] | 3995 | MachO::build_version_command |
| 3996 | MachOObjectFile::getBuildVersionLoadCommand(const LoadCommandInfo &L) const { |
| 3997 | return getStruct<MachO::build_version_command>(*this, L.Ptr); |
| 3998 | } |
| 3999 | |
| 4000 | MachO::build_tool_version |
| 4001 | MachOObjectFile::getBuildToolVersion(unsigned index) const { |
| 4002 | return getStruct<MachO::build_tool_version>(*this, BuildTools[index]); |
| 4003 | } |
| 4004 | |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 4005 | MachO::dylib_command |
| 4006 | MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4007 | return getStruct<MachO::dylib_command>(*this, L.Ptr); |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4010 | MachO::dyld_info_command |
| 4011 | MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4012 | return getStruct<MachO::dyld_info_command>(*this, L.Ptr); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | MachO::dylinker_command |
| 4016 | MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4017 | return getStruct<MachO::dylinker_command>(*this, L.Ptr); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | MachO::uuid_command |
| 4021 | MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4022 | return getStruct<MachO::uuid_command>(*this, L.Ptr); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 4025 | MachO::rpath_command |
| 4026 | MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4027 | return getStruct<MachO::rpath_command>(*this, L.Ptr); |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4030 | MachO::source_version_command |
| 4031 | MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4032 | return getStruct<MachO::source_version_command>(*this, L.Ptr); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | MachO::entry_point_command |
| 4036 | MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4037 | return getStruct<MachO::entry_point_command>(*this, L.Ptr); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 4040 | MachO::encryption_info_command |
| 4041 | MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4042 | return getStruct<MachO::encryption_info_command>(*this, L.Ptr); |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 4045 | MachO::encryption_info_command_64 |
| 4046 | MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4047 | return getStruct<MachO::encryption_info_command_64>(*this, L.Ptr); |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 4050 | MachO::sub_framework_command |
| 4051 | MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4052 | return getStruct<MachO::sub_framework_command>(*this, L.Ptr); |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 4053 | } |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 4054 | |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 4055 | MachO::sub_umbrella_command |
| 4056 | MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4057 | return getStruct<MachO::sub_umbrella_command>(*this, L.Ptr); |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 4060 | MachO::sub_library_command |
| 4061 | MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4062 | return getStruct<MachO::sub_library_command>(*this, L.Ptr); |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 4065 | MachO::sub_client_command |
| 4066 | MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4067 | return getStruct<MachO::sub_client_command>(*this, L.Ptr); |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 4070 | MachO::routines_command |
| 4071 | MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4072 | return getStruct<MachO::routines_command>(*this, L.Ptr); |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | MachO::routines_command_64 |
| 4076 | MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4077 | return getStruct<MachO::routines_command_64>(*this, L.Ptr); |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 4080 | MachO::thread_command |
| 4081 | MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const { |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4082 | return getStruct<MachO::thread_command>(*this, L.Ptr); |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4085 | MachO::any_relocation_info |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4086 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 4087 | DataRefImpl Sec; |
| 4088 | Sec.d.a = Rel.d.a; |
| 4089 | uint32_t Offset; |
| 4090 | if (is64Bit()) { |
| 4091 | MachO::section_64 Sect = getSection64(Sec); |
| 4092 | Offset = Sect.reloff; |
| 4093 | } else { |
| 4094 | MachO::section Sect = getSection(Sec); |
| 4095 | Offset = Sect.reloff; |
| 4096 | } |
| 4097 | |
| 4098 | auto P = reinterpret_cast<const MachO::any_relocation_info *>( |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4099 | getPtr(*this, Offset)) + Rel.d.b; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 4100 | return getStruct<MachO::any_relocation_info>( |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4101 | *this, reinterpret_cast<const char *>(P)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4104 | MachO::data_in_code_entry |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 4105 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 4106 | const char *P = reinterpret_cast<const char *>(Rel.p); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4107 | return getStruct<MachO::data_in_code_entry>(*this, P); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 4110 | const MachO::mach_header &MachOObjectFile::getHeader() const { |
Alexey Samsonov | fa5edc5 | 2015-06-04 22:49:55 +0000 | [diff] [blame] | 4111 | return Header; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 4114 | const MachO::mach_header_64 &MachOObjectFile::getHeader64() const { |
| 4115 | assert(is64Bit()); |
| 4116 | return Header64; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4119 | uint32_t MachOObjectFile::getIndirectSymbolTableEntry( |
| 4120 | const MachO::dysymtab_command &DLC, |
| 4121 | unsigned Index) const { |
| 4122 | uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4123 | return getStruct<uint32_t>(*this, getPtr(*this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4126 | MachO::data_in_code_entry |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4127 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 4128 | unsigned Index) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4129 | uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4130 | return getStruct<MachO::data_in_code_entry>(*this, getPtr(*this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4133 | MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4134 | if (SymtabLoadCmd) |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4135 | return getStruct<MachO::symtab_command>(*this, SymtabLoadCmd); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4136 | |
| 4137 | // If there is no SymtabLoadCmd return a load command with zero'ed fields. |
| 4138 | MachO::symtab_command Cmd; |
| 4139 | Cmd.cmd = MachO::LC_SYMTAB; |
| 4140 | Cmd.cmdsize = sizeof(MachO::symtab_command); |
| 4141 | Cmd.symoff = 0; |
| 4142 | Cmd.nsyms = 0; |
| 4143 | Cmd.stroff = 0; |
| 4144 | Cmd.strsize = 0; |
| 4145 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4148 | MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4149 | if (DysymtabLoadCmd) |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4150 | return getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4151 | |
| 4152 | // If there is no DysymtabLoadCmd return a load command with zero'ed fields. |
| 4153 | MachO::dysymtab_command Cmd; |
| 4154 | Cmd.cmd = MachO::LC_DYSYMTAB; |
| 4155 | Cmd.cmdsize = sizeof(MachO::dysymtab_command); |
| 4156 | Cmd.ilocalsym = 0; |
| 4157 | Cmd.nlocalsym = 0; |
| 4158 | Cmd.iextdefsym = 0; |
| 4159 | Cmd.nextdefsym = 0; |
| 4160 | Cmd.iundefsym = 0; |
| 4161 | Cmd.nundefsym = 0; |
| 4162 | Cmd.tocoff = 0; |
| 4163 | Cmd.ntoc = 0; |
| 4164 | Cmd.modtaboff = 0; |
| 4165 | Cmd.nmodtab = 0; |
| 4166 | Cmd.extrefsymoff = 0; |
| 4167 | Cmd.nextrefsyms = 0; |
| 4168 | Cmd.indirectsymoff = 0; |
| 4169 | Cmd.nindirectsyms = 0; |
| 4170 | Cmd.extreloff = 0; |
| 4171 | Cmd.nextrel = 0; |
| 4172 | Cmd.locreloff = 0; |
| 4173 | Cmd.nlocrel = 0; |
| 4174 | return Cmd; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4177 | MachO::linkedit_data_command |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 4178 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 4179 | if (DataInCodeLoadCmd) |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4180 | return getStruct<MachO::linkedit_data_command>(*this, DataInCodeLoadCmd); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 4181 | |
| 4182 | // 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] | 4183 | MachO::linkedit_data_command Cmd; |
| 4184 | Cmd.cmd = MachO::LC_DATA_IN_CODE; |
| 4185 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 4186 | Cmd.dataoff = 0; |
| 4187 | Cmd.datasize = 0; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 4188 | return Cmd; |
| 4189 | } |
| 4190 | |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 4191 | MachO::linkedit_data_command |
| 4192 | MachOObjectFile::getLinkOptHintsLoadCommand() const { |
| 4193 | if (LinkOptHintsLoadCmd) |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4194 | return getStruct<MachO::linkedit_data_command>(*this, LinkOptHintsLoadCmd); |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 4195 | |
| 4196 | // If there is no LinkOptHintsLoadCmd return a load command with zero'ed |
| 4197 | // fields. |
| 4198 | MachO::linkedit_data_command Cmd; |
| 4199 | Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT; |
| 4200 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 4201 | Cmd.dataoff = 0; |
| 4202 | Cmd.datasize = 0; |
| 4203 | return Cmd; |
| 4204 | } |
| 4205 | |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4206 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 4207 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4208 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4209 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4210 | MachO::dyld_info_command DyldInfo = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4211 | getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4212 | const uint8_t *Ptr = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4213 | reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.rebase_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4214 | return makeArrayRef(Ptr, DyldInfo.rebase_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 4218 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4219 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4220 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4221 | MachO::dyld_info_command DyldInfo = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4222 | getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4223 | const uint8_t *Ptr = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4224 | reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4225 | return makeArrayRef(Ptr, DyldInfo.bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 4229 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4230 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4231 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4232 | MachO::dyld_info_command DyldInfo = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4233 | getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4234 | const uint8_t *Ptr = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4235 | reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.weak_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4236 | return makeArrayRef(Ptr, DyldInfo.weak_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 4240 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4241 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4242 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4243 | MachO::dyld_info_command DyldInfo = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4244 | getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4245 | const uint8_t *Ptr = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4246 | reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.lazy_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4247 | return makeArrayRef(Ptr, DyldInfo.lazy_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 4251 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4252 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4253 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4254 | MachO::dyld_info_command DyldInfo = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4255 | getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd); |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 4256 | const uint8_t *Ptr = |
Lang Hames | 697e7cd | 2016-12-04 01:56:10 +0000 | [diff] [blame] | 4257 | reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.export_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4258 | return makeArrayRef(Ptr, DyldInfo.export_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 4261 | ArrayRef<uint8_t> MachOObjectFile::getUuid() const { |
| 4262 | if (!UuidLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4263 | return None; |
Benjamin Kramer | 014601d | 2014-10-24 15:52:05 +0000 | [diff] [blame] | 4264 | // Returning a pointer is fine as uuid doesn't need endian swapping. |
| 4265 | const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 4266 | return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16); |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 4267 | } |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4268 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4269 | StringRef MachOObjectFile::getStringTableData() const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 4270 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 4271 | return getData().substr(S.stroff, S.strsize); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4274 | bool MachOObjectFile::is64Bit() const { |
| 4275 | return getType() == getMachOType(false, true) || |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 4276 | getType() == getMachOType(true, true); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 4280 | SmallVectorImpl<uint64_t> &Out) const { |
| 4281 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 4282 | |
| 4283 | uint32_t offset = Index; |
| 4284 | uint64_t data = 0; |
| 4285 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 4286 | data += delta; |
| 4287 | Out.push_back(data); |
| 4288 | } |
| 4289 | } |
| 4290 | |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 4291 | bool MachOObjectFile::isRelocatableObject() const { |
| 4292 | return getHeader().filetype == MachO::MH_OBJECT; |
| 4293 | } |
| 4294 | |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 4295 | Expected<std::unique_ptr<MachOObjectFile>> |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 4296 | ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer, |
| 4297 | uint32_t UniversalCputype, |
| 4298 | uint32_t UniversalIndex) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 4299 | StringRef Magic = Buffer.getBuffer().slice(0, 4); |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 4300 | if (Magic == "\xFE\xED\xFA\xCE") |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 4301 | return MachOObjectFile::create(Buffer, false, false, |
| 4302 | UniversalCputype, UniversalIndex); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 4303 | if (Magic == "\xCE\xFA\xED\xFE") |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 4304 | return MachOObjectFile::create(Buffer, true, false, |
| 4305 | UniversalCputype, UniversalIndex); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 4306 | if (Magic == "\xFE\xED\xFA\xCF") |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 4307 | return MachOObjectFile::create(Buffer, false, true, |
| 4308 | UniversalCputype, UniversalIndex); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 4309 | if (Magic == "\xCF\xFA\xED\xFE") |
Kevin Enderby | 79d6c63 | 2016-10-24 21:15:11 +0000 | [diff] [blame] | 4310 | return MachOObjectFile::create(Buffer, true, true, |
| 4311 | UniversalCputype, UniversalIndex); |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 4312 | return make_error<GenericBinaryError>("Unrecognized MachO magic number", |
Justin Bogner | 2a42da9 | 2016-05-05 23:59:57 +0000 | [diff] [blame] | 4313 | object_error::invalid_file_type); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 4314 | } |