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