Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1 | //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the MachOObjectFile class, which binds the MachOObject |
| 11 | // class to the generic ObjectFile wrapper. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Owen Anderson | 27c579d | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 15 | #include "llvm/Object/MachO.h" |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Rafael Espindola | 421305a | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DataExtractor.h" |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Owen Anderson | bc14bd3 | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Host.h" |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LEB128.h" |
| 24 | #include "llvm/Support/MachO.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Jakub Staszak | 84a0ae7 | 2013-08-21 01:20:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 27 | #include <cctype> |
| 28 | #include <cstring> |
| 29 | #include <limits> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | using namespace object; |
| 33 | |
Artyom Skrobov | 7d602f7 | 2014-07-20 12:08:28 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | struct section_base { |
| 36 | char sectname[16]; |
| 37 | char segname[16]; |
| 38 | }; |
| 39 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 40 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 41 | static Error |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 42 | malformedError(Twine Msg) { |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 43 | std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")"; |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 44 | return make_error<GenericBinaryError>(std::move(StringMsg), |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 45 | object_error::parse_failed); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 48 | // FIXME: Replace all uses of this function with getStructOrErr. |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 49 | template <typename T> |
Artyom Skrobov | 7d602f7 | 2014-07-20 12:08:28 +0000 | [diff] [blame] | 50 | static T getStruct(const MachOObjectFile *O, const char *P) { |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 51 | // Don't read before the beginning or past the end of the file |
| 52 | if (P < O->getData().begin() || P + sizeof(T) > O->getData().end()) |
| 53 | report_fatal_error("Malformed MachO file."); |
| 54 | |
Rafael Espindola | 3cdeb17 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 55 | T Cmd; |
| 56 | memcpy(&Cmd, P, sizeof(T)); |
| 57 | if (O->isLittleEndian() != sys::IsLittleEndianHost) |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 58 | MachO::swapStruct(Cmd); |
Rafael Espindola | 3cdeb17 | 2013-04-19 13:45:05 +0000 | [diff] [blame] | 59 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 62 | template <typename T> |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 63 | static Expected<T> getStructOrErr(const MachOObjectFile *O, const char *P) { |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 64 | // Don't read before the beginning or past the end of the file |
| 65 | if (P < O->getData().begin() || P + sizeof(T) > O->getData().end()) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 66 | return malformedError("Structure read out-of-range"); |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 67 | |
| 68 | T Cmd; |
| 69 | memcpy(&Cmd, P, sizeof(T)); |
| 70 | if (O->isLittleEndian() != sys::IsLittleEndianHost) |
| 71 | MachO::swapStruct(Cmd); |
| 72 | return Cmd; |
| 73 | } |
| 74 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 75 | static const char * |
| 76 | getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L, |
| 77 | unsigned Sec) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 78 | uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr); |
| 79 | |
| 80 | bool Is64 = O->is64Bit(); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 81 | unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) : |
| 82 | sizeof(MachO::segment_command); |
| 83 | unsigned SectionSize = Is64 ? sizeof(MachO::section_64) : |
| 84 | sizeof(MachO::section); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 85 | |
| 86 | uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize; |
Charles Davis | 1827bd8 | 2013-08-27 05:38:30 +0000 | [diff] [blame] | 87 | return reinterpret_cast<const char*>(SectionAddr); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 90 | static const char *getPtr(const MachOObjectFile *O, size_t Offset) { |
| 91 | return O->getData().substr(Offset, 1).data(); |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 94 | static MachO::nlist_base |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 95 | getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 96 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 97 | return getStruct<MachO::nlist_base>(O, P); |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 100 | static StringRef parseSegmentOrSectionName(const char *P) { |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 101 | if (P[15] == 0) |
| 102 | // Null terminated. |
| 103 | return P; |
| 104 | // Not null terminated, so this is a 16 char string. |
| 105 | return StringRef(P, 16); |
| 106 | } |
| 107 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 108 | // Helper to advance a section or symbol iterator multiple increments at a time. |
| 109 | template<class T> |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 110 | static void advance(T &it, size_t Val) { |
| 111 | while (Val--) |
| 112 | ++it; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static unsigned getCPUType(const MachOObjectFile *O) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 116 | return O->getHeader().cputype; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 119 | static uint32_t |
| 120 | getPlainRelocationAddress(const MachO::any_relocation_info &RE) { |
| 121 | return RE.r_word0; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 125 | getScatteredRelocationAddress(const MachO::any_relocation_info &RE) { |
| 126 | return RE.r_word0 & 0xffffff; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static bool getPlainRelocationPCRel(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 130 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 131 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 132 | return (RE.r_word1 >> 24) & 1; |
| 133 | return (RE.r_word1 >> 7) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static bool |
| 137 | getScatteredRelocationPCRel(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 138 | const MachO::any_relocation_info &RE) { |
| 139 | return (RE.r_word0 >> 30) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static unsigned getPlainRelocationLength(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 143 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 144 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 145 | return (RE.r_word1 >> 25) & 3; |
| 146 | return (RE.r_word1 >> 5) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 150 | getScatteredRelocationLength(const MachO::any_relocation_info &RE) { |
| 151 | return (RE.r_word0 >> 28) & 3; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static unsigned getPlainRelocationType(const MachOObjectFile *O, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 155 | const MachO::any_relocation_info &RE) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 156 | if (O->isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 157 | return RE.r_word1 >> 28; |
| 158 | return RE.r_word1 & 0xf; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 161 | static uint32_t getSectionFlags(const MachOObjectFile *O, |
| 162 | DataRefImpl Sec) { |
| 163 | if (O->is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 164 | MachO::section_64 Sect = O->getSection64(Sec); |
| 165 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 166 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 167 | MachO::section Sect = O->getSection(Sec); |
| 168 | return Sect.flags; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 171 | static Expected<MachOObjectFile::LoadCommandInfo> |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 172 | getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr, |
| 173 | uint32_t LoadCommandIndex) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 174 | if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) { |
| 175 | if (CmdOrErr->cmdsize < 8) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 176 | return malformedError("load command " + Twine(LoadCommandIndex) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 177 | " with size less than 8 bytes"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 178 | return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr}); |
| 179 | } else |
| 180 | return CmdOrErr.takeError(); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 183 | static Expected<MachOObjectFile::LoadCommandInfo> |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 184 | getFirstLoadCommandInfo(const MachOObjectFile *Obj) { |
| 185 | unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64) |
| 186 | : sizeof(MachO::mach_header); |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 187 | if (sizeof(MachO::load_command) > Obj->getHeader().sizeofcmds) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 188 | return malformedError("load command 0 extends past the end all load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 189 | "commands in the file"); |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 190 | return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 193 | static Expected<MachOObjectFile::LoadCommandInfo> |
Kevin Enderby | 368e714 | 2016-05-03 17:16:08 +0000 | [diff] [blame] | 194 | getNextLoadCommandInfo(const MachOObjectFile *Obj, uint32_t LoadCommandIndex, |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 195 | const MachOObjectFile::LoadCommandInfo &L) { |
Kevin Enderby | 368e714 | 2016-05-03 17:16:08 +0000 | [diff] [blame] | 196 | unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64) |
| 197 | : sizeof(MachO::mach_header); |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 198 | if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) > |
Kevin Enderby | 368e714 | 2016-05-03 17:16:08 +0000 | [diff] [blame] | 199 | Obj->getData().data() + HeaderSize + Obj->getHeader().sizeofcmds) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 200 | return malformedError("load command " + Twine(LoadCommandIndex + 1) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 201 | " extends past the end all load commands in the file"); |
Kevin Enderby | a8e3ab0 | 2016-05-03 23:13:50 +0000 | [diff] [blame] | 202 | return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1); |
Alexey Samsonov | 4fdbed3 | 2015-06-04 19:34:14 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 205 | template <typename T> |
| 206 | static void parseHeader(const MachOObjectFile *Obj, T &Header, |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 207 | Error &Err) { |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 208 | if (sizeof(T) > Obj->getData().size()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 209 | Err = malformedError("the mach header extends past the end of the " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 210 | "file"); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 213 | if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0))) |
| 214 | Header = *HeaderOrErr; |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 215 | else |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 216 | Err = HeaderOrErr.takeError(); |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 219 | // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all |
| 220 | // sections to \param Sections, and optionally sets |
| 221 | // \param IsPageZeroSegment to true. |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 222 | template <typename Segment, typename Section> |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 223 | static Error parseSegmentLoadCommand( |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 224 | const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load, |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 225 | SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment, |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 226 | uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders) { |
| 227 | const unsigned SegmentLoadSize = sizeof(Segment); |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 228 | if (Load.C.cmdsize < SegmentLoadSize) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 229 | return malformedError("load command " + Twine(LoadCommandIndex) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 230 | " " + CmdName + " cmdsize too small"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 231 | if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) { |
| 232 | Segment S = SegOrErr.get(); |
| 233 | const unsigned SectionSize = sizeof(Section); |
| 234 | uint64_t FileSize = Obj->getData().size(); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 235 | if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || |
| 236 | S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 237 | return malformedError("load command " + Twine(LoadCommandIndex) + |
NAKAMURA Takumi | 9d0b531 | 2016-08-22 00:58:47 +0000 | [diff] [blame] | 238 | " inconsistent cmdsize in " + CmdName + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 239 | " for the number of sections"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 240 | for (unsigned J = 0; J < S.nsects; ++J) { |
| 241 | const char *Sec = getSectionPtr(Obj, Load, J); |
| 242 | Sections.push_back(Sec); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 243 | Section s = getStruct<Section>(Obj, Sec); |
| 244 | if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 245 | Obj->getHeader().filetype != MachO::MH_DSYM && |
| 246 | s.flags != MachO::S_ZEROFILL && |
| 247 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 248 | s.offset > FileSize) |
| 249 | return malformedError("offset field of section " + Twine(J) + " in " + |
| 250 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 251 | " extends past the end of the file"); |
| 252 | if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 253 | Obj->getHeader().filetype != MachO::MH_DSYM && |
| 254 | s.flags != MachO::S_ZEROFILL && |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 255 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 && |
| 256 | s.offset < SizeOfHeaders && s.size != 0) |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 257 | return malformedError("offset field of section " + Twine(J) + " in " + |
| 258 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 259 | " not past the headers of the file"); |
| 260 | uint64_t BigSize = s.offset; |
| 261 | BigSize += s.size; |
| 262 | if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 263 | Obj->getHeader().filetype != MachO::MH_DSYM && |
| 264 | s.flags != MachO::S_ZEROFILL && |
| 265 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 266 | BigSize > FileSize) |
| 267 | return malformedError("offset field plus size field of section " + |
| 268 | Twine(J) + " in " + CmdName + " command " + |
| 269 | Twine(LoadCommandIndex) + |
| 270 | " extends past the end of the file"); |
| 271 | if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB && |
| 272 | Obj->getHeader().filetype != MachO::MH_DSYM && |
| 273 | s.flags != MachO::S_ZEROFILL && |
| 274 | s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && |
| 275 | s.size > S.filesize) |
| 276 | return malformedError("size field of section " + |
| 277 | Twine(J) + " in " + CmdName + " command " + |
| 278 | Twine(LoadCommandIndex) + |
| 279 | " greater than the segment"); |
| 280 | if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB && |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 281 | Obj->getHeader().filetype != MachO::MH_DSYM && s.size != 0 && |
| 282 | s.addr < S.vmaddr) |
| 283 | return malformedError("addr field of section " + Twine(J) + " in " + |
| 284 | CmdName + " command " + Twine(LoadCommandIndex) + |
| 285 | " less than the segment's vmaddr"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 286 | BigSize = s.addr; |
| 287 | BigSize += s.size; |
| 288 | uint64_t BigEnd = S.vmaddr; |
| 289 | BigEnd += S.vmsize; |
| 290 | if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd) |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 291 | return malformedError("addr field plus size of section " + Twine(J) + |
| 292 | " in " + CmdName + " command " + |
| 293 | Twine(LoadCommandIndex) + |
| 294 | " greater than than " |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 295 | "the segment's vmaddr plus vmsize"); |
| 296 | if (s.reloff > FileSize) |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 297 | return malformedError("reloff field of section " + Twine(J) + " in " + |
| 298 | CmdName + " command " + Twine(LoadCommandIndex) + |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 299 | " extends past the end of the file"); |
| 300 | BigSize = s.nreloc; |
| 301 | BigSize *= sizeof(struct MachO::relocation_info); |
| 302 | BigSize += s.reloff; |
| 303 | if (BigSize > FileSize) |
| 304 | return malformedError("reloff field plus nreloc field times sizeof(" |
| 305 | "struct relocation_info) of section " + |
| 306 | Twine(J) + " in " + CmdName + " command " + |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 307 | Twine(LoadCommandIndex) + |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 308 | " extends past the end of the file"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 309 | } |
Kevin Enderby | 600fb3f | 2016-08-05 18:19:40 +0000 | [diff] [blame] | 310 | if (S.fileoff > FileSize) |
| 311 | return malformedError("load command " + Twine(LoadCommandIndex) + |
NAKAMURA Takumi | 9d0b531 | 2016-08-22 00:58:47 +0000 | [diff] [blame] | 312 | " fileoff field in " + CmdName + |
Kevin Enderby | 600fb3f | 2016-08-05 18:19:40 +0000 | [diff] [blame] | 313 | " extends past the end of the file"); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 314 | uint64_t BigSize = S.fileoff; |
| 315 | BigSize += S.filesize; |
| 316 | if (BigSize > FileSize) |
| 317 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 318 | " fileoff field plus filesize field in " + |
| 319 | CmdName + " extends past the end of the file"); |
| 320 | if (S.vmsize != 0 && S.filesize > S.vmsize) |
| 321 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 322 | " fileoff field in " + CmdName + |
| 323 | " greater than vmsize field"); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 324 | IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname); |
| 325 | } else |
| 326 | return SegOrErr.takeError(); |
| 327 | |
| 328 | return Error::success(); |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 331 | static Error checkSymtabCommand(const MachOObjectFile *Obj, |
| 332 | const MachOObjectFile::LoadCommandInfo &Load, |
| 333 | uint32_t LoadCommandIndex, |
| 334 | const char **SymtabLoadCmd) { |
| 335 | if (Load.C.cmdsize < sizeof(MachO::symtab_command)) |
| 336 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 337 | " LC_SYMTAB cmdsize too small"); |
| 338 | if (*SymtabLoadCmd != nullptr) |
| 339 | return malformedError("more than one LC_SYMTAB command"); |
| 340 | MachO::symtab_command Symtab = |
| 341 | getStruct<MachO::symtab_command>(Obj, Load.Ptr); |
| 342 | if (Symtab.cmdsize != sizeof(MachO::symtab_command)) |
| 343 | return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) + |
| 344 | " has incorrect cmdsize"); |
| 345 | uint64_t FileSize = Obj->getData().size(); |
| 346 | if (Symtab.symoff > FileSize) |
| 347 | return malformedError("symoff field of LC_SYMTAB command " + |
| 348 | Twine(LoadCommandIndex) + " extends past the end " |
| 349 | "of the file"); |
| 350 | uint64_t BigSize = Symtab.nsyms; |
| 351 | const char *struct_nlist_name; |
| 352 | if (Obj->is64Bit()) { |
| 353 | BigSize *= sizeof(MachO::nlist_64); |
| 354 | struct_nlist_name = "struct nlist_64"; |
| 355 | } else { |
| 356 | BigSize *= sizeof(MachO::nlist); |
| 357 | struct_nlist_name = "struct nlist"; |
| 358 | } |
| 359 | BigSize += Symtab.symoff; |
| 360 | if (BigSize > FileSize) |
| 361 | return malformedError("symoff field plus nsyms field times sizeof(" + |
| 362 | Twine(struct_nlist_name) + ") of LC_SYMTAB command " + |
| 363 | Twine(LoadCommandIndex) + " extends past the end " |
| 364 | "of the file"); |
| 365 | if (Symtab.stroff > FileSize) |
| 366 | return malformedError("stroff field of LC_SYMTAB command " + |
| 367 | Twine(LoadCommandIndex) + " extends past the end " |
| 368 | "of the file"); |
| 369 | BigSize = Symtab.stroff; |
| 370 | BigSize += Symtab.strsize; |
| 371 | if (BigSize > FileSize) |
| 372 | return malformedError("stroff field plus strsize field of LC_SYMTAB " |
| 373 | "command " + Twine(LoadCommandIndex) + " extends " |
| 374 | "past the end of the file"); |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 375 | *SymtabLoadCmd = Load.Ptr; |
| 376 | return Error::success(); |
| 377 | } |
| 378 | |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 379 | static Error checkDysymtabCommand(const MachOObjectFile *Obj, |
| 380 | const MachOObjectFile::LoadCommandInfo &Load, |
| 381 | uint32_t LoadCommandIndex, |
| 382 | const char **DysymtabLoadCmd) { |
| 383 | if (Load.C.cmdsize < sizeof(MachO::dysymtab_command)) |
| 384 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 385 | " LC_DYSYMTAB cmdsize too small"); |
| 386 | if (*DysymtabLoadCmd != nullptr) |
| 387 | return malformedError("more than one LC_DYSYMTAB command"); |
| 388 | MachO::dysymtab_command Dysymtab = |
| 389 | getStruct<MachO::dysymtab_command>(Obj, Load.Ptr); |
| 390 | if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command)) |
| 391 | return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) + |
| 392 | " has incorrect cmdsize"); |
| 393 | uint64_t FileSize = Obj->getData().size(); |
| 394 | if (Dysymtab.tocoff > FileSize) |
| 395 | return malformedError("tocoff field of LC_DYSYMTAB command " + |
| 396 | Twine(LoadCommandIndex) + " extends past the end of " |
| 397 | "the file"); |
| 398 | uint64_t BigSize = Dysymtab.ntoc; |
| 399 | BigSize *= sizeof(MachO::dylib_table_of_contents); |
| 400 | BigSize += Dysymtab.tocoff; |
| 401 | if (BigSize > FileSize) |
| 402 | return malformedError("tocoff field plus ntoc field times sizeof(struct " |
| 403 | "dylib_table_of_contents) of LC_DYSYMTAB command " + |
| 404 | Twine(LoadCommandIndex) + " extends past the end of " |
| 405 | "the file"); |
| 406 | if (Dysymtab.modtaboff > FileSize) |
| 407 | return malformedError("modtaboff field of LC_DYSYMTAB command " + |
| 408 | Twine(LoadCommandIndex) + " extends past the end of " |
| 409 | "the file"); |
| 410 | BigSize = Dysymtab.nmodtab; |
| 411 | const char *struct_dylib_module_name; |
| 412 | if (Obj->is64Bit()) { |
| 413 | BigSize *= sizeof(MachO::dylib_module_64); |
| 414 | struct_dylib_module_name = "struct dylib_module_64"; |
| 415 | } else { |
| 416 | BigSize *= sizeof(MachO::dylib_module); |
| 417 | struct_dylib_module_name = "struct dylib_module"; |
| 418 | } |
| 419 | BigSize += Dysymtab.modtaboff; |
| 420 | if (BigSize > FileSize) |
| 421 | return malformedError("modtaboff field plus nmodtab field times sizeof(" + |
| 422 | Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB " |
| 423 | "command " + Twine(LoadCommandIndex) + " extends " |
| 424 | "past the end of the file"); |
| 425 | if (Dysymtab.extrefsymoff > FileSize) |
| 426 | return malformedError("extrefsymoff field of LC_DYSYMTAB command " + |
| 427 | Twine(LoadCommandIndex) + " extends past the end of " |
| 428 | "the file"); |
| 429 | BigSize = Dysymtab.nextrefsyms; |
| 430 | BigSize *= sizeof(MachO::dylib_reference); |
| 431 | BigSize += Dysymtab.extrefsymoff; |
| 432 | if (BigSize > FileSize) |
| 433 | return malformedError("extrefsymoff field plus nextrefsyms field times " |
| 434 | "sizeof(struct dylib_reference) of LC_DYSYMTAB " |
| 435 | "command " + Twine(LoadCommandIndex) + " extends " |
| 436 | "past the end of the file"); |
| 437 | if (Dysymtab.indirectsymoff > FileSize) |
| 438 | return malformedError("indirectsymoff field of LC_DYSYMTAB command " + |
| 439 | Twine(LoadCommandIndex) + " extends past the end of " |
| 440 | "the file"); |
| 441 | BigSize = Dysymtab.nindirectsyms; |
| 442 | BigSize *= sizeof(uint32_t); |
| 443 | BigSize += Dysymtab.indirectsymoff; |
| 444 | if (BigSize > FileSize) |
| 445 | return malformedError("indirectsymoff field plus nindirectsyms field times " |
| 446 | "sizeof(uint32_t) of LC_DYSYMTAB command " + |
| 447 | Twine(LoadCommandIndex) + " extends past the end of " |
| 448 | "the file"); |
| 449 | if (Dysymtab.extreloff > FileSize) |
| 450 | return malformedError("extreloff field of LC_DYSYMTAB command " + |
| 451 | Twine(LoadCommandIndex) + " extends past the end of " |
| 452 | "the file"); |
| 453 | BigSize = Dysymtab.nextrel; |
| 454 | BigSize *= sizeof(MachO::relocation_info); |
| 455 | BigSize += Dysymtab.extreloff; |
| 456 | if (BigSize > FileSize) |
| 457 | return malformedError("extreloff field plus nextrel field times sizeof" |
| 458 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 459 | Twine(LoadCommandIndex) + " extends past the end of " |
| 460 | "the file"); |
| 461 | if (Dysymtab.locreloff > FileSize) |
| 462 | return malformedError("locreloff field of LC_DYSYMTAB command " + |
| 463 | Twine(LoadCommandIndex) + " extends past the end of " |
| 464 | "the file"); |
| 465 | BigSize = Dysymtab.nlocrel; |
| 466 | BigSize *= sizeof(MachO::relocation_info); |
| 467 | BigSize += Dysymtab.locreloff; |
| 468 | if (BigSize > FileSize) |
| 469 | return malformedError("locreloff field plus nlocrel field times sizeof" |
| 470 | "(struct relocation_info) of LC_DYSYMTAB command " + |
| 471 | Twine(LoadCommandIndex) + " extends past the end of " |
| 472 | "the file"); |
| 473 | *DysymtabLoadCmd = Load.Ptr; |
| 474 | return Error::success(); |
| 475 | } |
| 476 | |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 477 | static Error checkLinkeditDataCommand(const MachOObjectFile *Obj, |
| 478 | const MachOObjectFile::LoadCommandInfo &Load, |
| 479 | uint32_t LoadCommandIndex, |
| 480 | const char **LoadCmd, const char *CmdName) { |
| 481 | if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command)) |
| 482 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 483 | CmdName + " cmdsize too small"); |
| 484 | if (*LoadCmd != nullptr) |
| 485 | return malformedError("more than one " + Twine(CmdName) + " command"); |
| 486 | MachO::linkedit_data_command LinkData = |
| 487 | getStruct<MachO::linkedit_data_command>(Obj, Load.Ptr); |
| 488 | if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command)) |
| 489 | return malformedError(Twine(CmdName) + " command " + |
| 490 | Twine(LoadCommandIndex) + " has incorrect cmdsize"); |
| 491 | uint64_t FileSize = Obj->getData().size(); |
| 492 | if (LinkData.dataoff > FileSize) |
| 493 | return malformedError("dataoff field of " + Twine(CmdName) + " command " + |
| 494 | Twine(LoadCommandIndex) + " extends past the end of " |
| 495 | "the file"); |
| 496 | uint64_t BigSize = LinkData.dataoff; |
| 497 | BigSize += LinkData.datasize; |
| 498 | if (BigSize > FileSize) |
| 499 | return malformedError("dataoff field plus datasize field of " + |
| 500 | Twine(CmdName) + " command " + |
| 501 | Twine(LoadCommandIndex) + " extends past the end of " |
| 502 | "the file"); |
| 503 | *LoadCmd = Load.Ptr; |
| 504 | return Error::success(); |
| 505 | } |
| 506 | |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 507 | static Error checkDyldInfoCommand(const MachOObjectFile *Obj, |
| 508 | const MachOObjectFile::LoadCommandInfo &Load, |
| 509 | uint32_t LoadCommandIndex, |
| 510 | const char **LoadCmd, const char *CmdName) { |
| 511 | if (Load.C.cmdsize < sizeof(MachO::dyld_info_command)) |
| 512 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 513 | CmdName + " cmdsize too small"); |
| 514 | if (*LoadCmd != nullptr) |
| 515 | return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY " |
| 516 | "command"); |
| 517 | MachO::dyld_info_command DyldInfo = |
| 518 | getStruct<MachO::dyld_info_command>(Obj, Load.Ptr); |
| 519 | if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command)) |
| 520 | return malformedError(Twine(CmdName) + " command " + |
| 521 | Twine(LoadCommandIndex) + " has incorrect cmdsize"); |
| 522 | uint64_t FileSize = Obj->getData().size(); |
| 523 | if (DyldInfo.rebase_off > FileSize) |
| 524 | return malformedError("rebase_off field of " + Twine(CmdName) + |
| 525 | " command " + Twine(LoadCommandIndex) + " extends " |
| 526 | "past the end of the file"); |
| 527 | uint64_t BigSize = DyldInfo.rebase_off; |
| 528 | BigSize += DyldInfo.rebase_size; |
| 529 | if (BigSize > FileSize) |
| 530 | return malformedError("rebase_off field plus rebase_size field of " + |
| 531 | Twine(CmdName) + " command " + |
| 532 | Twine(LoadCommandIndex) + " extends past the end of " |
| 533 | "the file"); |
| 534 | if (DyldInfo.bind_off > FileSize) |
| 535 | return malformedError("bind_off field of " + Twine(CmdName) + |
| 536 | " command " + Twine(LoadCommandIndex) + " extends " |
| 537 | "past the end of the file"); |
| 538 | BigSize = DyldInfo.bind_off; |
| 539 | BigSize += DyldInfo.bind_size; |
| 540 | if (BigSize > FileSize) |
| 541 | return malformedError("bind_off field plus bind_size field of " + |
| 542 | Twine(CmdName) + " command " + |
| 543 | Twine(LoadCommandIndex) + " extends past the end of " |
| 544 | "the file"); |
| 545 | if (DyldInfo.weak_bind_off > FileSize) |
| 546 | return malformedError("weak_bind_off field of " + Twine(CmdName) + |
| 547 | " command " + Twine(LoadCommandIndex) + " extends " |
| 548 | "past the end of the file"); |
| 549 | BigSize = DyldInfo.weak_bind_off; |
| 550 | BigSize += DyldInfo.weak_bind_size; |
| 551 | if (BigSize > FileSize) |
| 552 | return malformedError("weak_bind_off field plus weak_bind_size field of " + |
| 553 | Twine(CmdName) + " command " + |
| 554 | Twine(LoadCommandIndex) + " extends past the end of " |
| 555 | "the file"); |
| 556 | if (DyldInfo.lazy_bind_off > FileSize) |
| 557 | return malformedError("lazy_bind_off field of " + Twine(CmdName) + |
| 558 | " command " + Twine(LoadCommandIndex) + " extends " |
| 559 | "past the end of the file"); |
| 560 | BigSize = DyldInfo.lazy_bind_off; |
| 561 | BigSize += DyldInfo.lazy_bind_size; |
| 562 | if (BigSize > FileSize) |
| 563 | return malformedError("lazy_bind_off field plus lazy_bind_size field of " + |
| 564 | Twine(CmdName) + " command " + |
| 565 | Twine(LoadCommandIndex) + " extends past the end of " |
| 566 | "the file"); |
| 567 | if (DyldInfo.export_off > FileSize) |
| 568 | return malformedError("export_off field of " + Twine(CmdName) + |
| 569 | " command " + Twine(LoadCommandIndex) + " extends " |
| 570 | "past the end of the file"); |
| 571 | BigSize = DyldInfo.export_off; |
| 572 | BigSize += DyldInfo.export_size; |
| 573 | if (BigSize > FileSize) |
| 574 | return malformedError("export_off field plus export_size field of " + |
| 575 | Twine(CmdName) + " command " + |
| 576 | Twine(LoadCommandIndex) + " extends past the end of " |
| 577 | "the file"); |
| 578 | *LoadCmd = Load.Ptr; |
| 579 | return Error::success(); |
| 580 | } |
| 581 | |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 582 | static Error checkDylibCommand(const MachOObjectFile *Obj, |
| 583 | const MachOObjectFile::LoadCommandInfo &Load, |
| 584 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 585 | if (Load.C.cmdsize < sizeof(MachO::dylib_command)) |
| 586 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 587 | CmdName + " cmdsize too small"); |
| 588 | MachO::dylib_command D = getStruct<MachO::dylib_command>(Obj, Load.Ptr); |
| 589 | if (D.dylib.name < sizeof(MachO::dylib_command)) |
| 590 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 591 | CmdName + " name.offset field too small, not past " |
| 592 | "the end of the dylib_command struct"); |
| 593 | if (D.dylib.name >= D.cmdsize) |
| 594 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 595 | CmdName + " name.offset field extends past the end " |
| 596 | "of the load command"); |
| 597 | // Make sure there is a null between the starting offset of the name and |
| 598 | // the end of the load command. |
| 599 | uint32_t i; |
| 600 | const char *P = (const char *)Load.Ptr; |
| 601 | for (i = D.dylib.name; i < D.cmdsize; i++) |
| 602 | if (P[i] == '\0') |
| 603 | break; |
| 604 | if (i >= D.cmdsize) |
| 605 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 606 | CmdName + " library name extends past the end of the " |
| 607 | "load command"); |
| 608 | return Error::success(); |
| 609 | } |
| 610 | |
| 611 | static Error checkDylibIdCommand(const MachOObjectFile *Obj, |
| 612 | const MachOObjectFile::LoadCommandInfo &Load, |
| 613 | uint32_t LoadCommandIndex, |
| 614 | const char **LoadCmd) { |
| 615 | if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex, |
| 616 | "LC_ID_DYLIB")) |
| 617 | return Err; |
| 618 | if (*LoadCmd != nullptr) |
| 619 | return malformedError("more than one LC_ID_DYLIB command"); |
| 620 | if (Obj->getHeader().filetype != MachO::MH_DYLIB && |
| 621 | Obj->getHeader().filetype != MachO::MH_DYLIB_STUB) |
| 622 | return malformedError("LC_ID_DYLIB load command in non-dynamic library " |
| 623 | "file type"); |
| 624 | *LoadCmd = Load.Ptr; |
| 625 | return Error::success(); |
| 626 | } |
| 627 | |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 628 | static Error checkDyldCommand(const MachOObjectFile *Obj, |
| 629 | const MachOObjectFile::LoadCommandInfo &Load, |
| 630 | uint32_t LoadCommandIndex, const char *CmdName) { |
| 631 | if (Load.C.cmdsize < sizeof(MachO::dylinker_command)) |
| 632 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 633 | CmdName + " cmdsize too small"); |
| 634 | MachO::dylinker_command D = getStruct<MachO::dylinker_command>(Obj, Load.Ptr); |
| 635 | if (D.name < sizeof(MachO::dylinker_command)) |
| 636 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 637 | CmdName + " name.offset field too small, not past " |
| 638 | "the end of the dylinker_command struct"); |
| 639 | if (D.name >= D.cmdsize) |
| 640 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 641 | CmdName + " name.offset field extends past the end " |
| 642 | "of the load command"); |
| 643 | // Make sure there is a null between the starting offset of the name and |
| 644 | // the end of the load command. |
| 645 | uint32_t i; |
| 646 | const char *P = (const char *)Load.Ptr; |
| 647 | for (i = D.name; i < D.cmdsize; i++) |
| 648 | if (P[i] == '\0') |
| 649 | break; |
| 650 | if (i >= D.cmdsize) |
| 651 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 652 | CmdName + " dyld name extends past the end of the " |
| 653 | "load command"); |
| 654 | return Error::success(); |
| 655 | } |
| 656 | |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 657 | static Error checkVersCommand(const MachOObjectFile *Obj, |
| 658 | const MachOObjectFile::LoadCommandInfo &Load, |
| 659 | uint32_t LoadCommandIndex, |
| 660 | const char **LoadCmd, const char *CmdName) { |
| 661 | if (Load.C.cmdsize != sizeof(MachO::version_min_command)) |
| 662 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 663 | CmdName + " has incorrect cmdsize"); |
| 664 | if (*LoadCmd != nullptr) |
| 665 | return malformedError("more than one LC_VERSION_MIN_MACOSX, " |
| 666 | "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or " |
| 667 | "LC_VERSION_MIN_WATCHOS command"); |
| 668 | *LoadCmd = Load.Ptr; |
| 669 | return Error::success(); |
| 670 | } |
| 671 | |
Kevin Enderby | 76966bf | 2016-09-28 23:16:01 +0000 | [diff] [blame] | 672 | static Error checkRpathCommand(const MachOObjectFile *Obj, |
| 673 | const MachOObjectFile::LoadCommandInfo &Load, |
| 674 | uint32_t LoadCommandIndex) { |
| 675 | if (Load.C.cmdsize < sizeof(MachO::rpath_command)) |
| 676 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 677 | " LC_RPATH cmdsize too small"); |
| 678 | MachO::rpath_command R = getStruct<MachO::rpath_command>(Obj, Load.Ptr); |
| 679 | if (R.path < sizeof(MachO::rpath_command)) |
| 680 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 681 | " LC_RPATH path.offset field too small, not past " |
| 682 | "the end of the rpath_command struct"); |
| 683 | if (R.path >= R.cmdsize) |
| 684 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 685 | " LC_RPATH path.offset field extends past the end " |
| 686 | "of the load command"); |
| 687 | // Make sure there is a null between the starting offset of the path and |
| 688 | // the end of the load command. |
| 689 | uint32_t i; |
| 690 | const char *P = (const char *)Load.Ptr; |
| 691 | for (i = R.path; i < R.cmdsize; i++) |
| 692 | if (P[i] == '\0') |
| 693 | break; |
| 694 | if (i >= R.cmdsize) |
| 695 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 696 | " LC_RPATH library name extends past the end of the " |
| 697 | "load command"); |
| 698 | return Error::success(); |
| 699 | } |
| 700 | |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 701 | static Error checkEncryptCommand(const MachOObjectFile *Obj, |
| 702 | const MachOObjectFile::LoadCommandInfo &Load, |
| 703 | uint32_t LoadCommandIndex, |
| 704 | uint64_t cryptoff, uint64_t cryptsize, |
| 705 | const char **LoadCmd, const char *CmdName) { |
| 706 | if (*LoadCmd != nullptr) |
| 707 | return malformedError("more than one LC_ENCRYPTION_INFO and or " |
| 708 | "LC_ENCRYPTION_INFO_64 command"); |
| 709 | uint64_t FileSize = Obj->getData().size(); |
| 710 | if (cryptoff > FileSize) |
| 711 | return malformedError("cryptoff field of " + Twine(CmdName) + |
| 712 | " command " + Twine(LoadCommandIndex) + " extends " |
| 713 | "past the end of the file"); |
| 714 | uint64_t BigSize = cryptoff; |
| 715 | BigSize += cryptsize; |
| 716 | if (BigSize > FileSize) |
| 717 | return malformedError("cryptoff field plus cryptsize field of " + |
| 718 | Twine(CmdName) + " command " + |
| 719 | Twine(LoadCommandIndex) + " extends past the end of " |
| 720 | "the file"); |
| 721 | *LoadCmd = Load.Ptr; |
| 722 | return Error::success(); |
| 723 | } |
| 724 | |
Kevin Enderby | 68fffa8 | 2016-10-11 21:04:39 +0000 | [diff] [blame] | 725 | static Error checkLinkerOptCommand(const MachOObjectFile *Obj, |
| 726 | const MachOObjectFile::LoadCommandInfo &Load, |
| 727 | uint32_t LoadCommandIndex) { |
| 728 | if (Load.C.cmdsize < sizeof(MachO::linker_option_command)) |
| 729 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 730 | " LC_LINKER_OPTION cmdsize too small"); |
| 731 | MachO::linker_option_command L = |
| 732 | getStruct<MachO::linker_option_command>(Obj, Load.Ptr); |
| 733 | // Make sure the count of strings is correct. |
| 734 | const char *string = (const char *)Load.Ptr + |
| 735 | sizeof(struct MachO::linker_option_command); |
| 736 | uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command); |
| 737 | uint32_t i = 0; |
| 738 | while (left > 0) { |
| 739 | while (*string == '\0' && left > 0) { |
| 740 | string++; |
| 741 | left--; |
| 742 | } |
| 743 | if (left > 0) { |
| 744 | i++; |
| 745 | uint32_t NullPos = StringRef(string, left).find('\0'); |
| 746 | uint32_t len = std::min(NullPos, left) + 1; |
| 747 | string += len; |
| 748 | left -= len; |
| 749 | } |
| 750 | } |
| 751 | if (L.count != i) |
| 752 | return malformedError("load command " + Twine(LoadCommandIndex) + |
| 753 | " LC_LINKER_OPTION string count " + Twine(L.count) + |
| 754 | " does not match number of strings"); |
| 755 | return Error::success(); |
| 756 | } |
| 757 | |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame^] | 758 | static Error checkSubCommand(const MachOObjectFile *Obj, |
| 759 | const MachOObjectFile::LoadCommandInfo &Load, |
| 760 | uint32_t LoadCommandIndex, const char *CmdName, |
| 761 | size_t SizeOfCmd, const char *CmdStructName, |
| 762 | uint32_t PathOffset, const char *PathFieldName) { |
| 763 | if (PathOffset < SizeOfCmd) |
| 764 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 765 | CmdName + " " + PathFieldName + ".offset field too " |
| 766 | "small, not past the end of the " + CmdStructName); |
| 767 | if (PathOffset >= Load.C.cmdsize) |
| 768 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 769 | CmdName + " " + PathFieldName + ".offset field " |
| 770 | "extends past the end of the load command"); |
| 771 | // Make sure there is a null between the starting offset of the path and |
| 772 | // the end of the load command. |
| 773 | uint32_t i; |
| 774 | const char *P = (const char *)Load.Ptr; |
| 775 | for (i = PathOffset; i < Load.C.cmdsize; i++) |
| 776 | if (P[i] == '\0') |
| 777 | break; |
| 778 | if (i >= Load.C.cmdsize) |
| 779 | return malformedError("load command " + Twine(LoadCommandIndex) + " " + |
| 780 | CmdName + " " + PathFieldName + " name extends past " |
| 781 | "the end of the load command"); |
| 782 | return Error::success(); |
| 783 | } |
| 784 | |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 785 | Expected<std::unique_ptr<MachOObjectFile>> |
| 786 | MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian, |
| 787 | bool Is64Bits) { |
Lang Hames | d1af8fc | 2016-03-25 23:54:32 +0000 | [diff] [blame] | 788 | Error Err; |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 789 | std::unique_ptr<MachOObjectFile> Obj( |
| 790 | new MachOObjectFile(std::move(Object), IsLittleEndian, |
| 791 | Is64Bits, Err)); |
| 792 | if (Err) |
| 793 | return std::move(Err); |
| 794 | return std::move(Obj); |
| 795 | } |
| 796 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 797 | MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 798 | bool Is64bits, Error &Err) |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 799 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 800 | SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr), |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 801 | DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr), |
| 802 | DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr), |
| 803 | HasPageZeroSegment(false) { |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 804 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 805 | uint64_t SizeOfHeaders; |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 806 | if (is64Bit()) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 807 | parseHeader(this, Header64, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 808 | SizeOfHeaders = sizeof(MachO::mach_header_64); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 809 | } else { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 810 | parseHeader(this, Header, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 811 | SizeOfHeaders = sizeof(MachO::mach_header); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 812 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 813 | if (Err) |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 814 | return; |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 815 | SizeOfHeaders += getHeader().sizeofcmds; |
| 816 | if (getData().data() + SizeOfHeaders > getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 817 | Err = malformedError("load commands extend past the end of the file"); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 818 | return; |
| 819 | } |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 820 | |
| 821 | uint32_t LoadCommandCount = getHeader().ncmds; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 822 | LoadCommandInfo Load; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 823 | if (LoadCommandCount != 0) { |
| 824 | if (auto LoadOrErr = getFirstLoadCommandInfo(this)) |
| 825 | Load = *LoadOrErr; |
| 826 | else { |
| 827 | Err = LoadOrErr.takeError(); |
| 828 | return; |
| 829 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 830 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 831 | |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 832 | const char *DyldIdLoadCmd = nullptr; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 833 | const char *FuncStartsLoadCmd = nullptr; |
| 834 | const char *SplitInfoLoadCmd = nullptr; |
| 835 | const char *CodeSignDrsLoadCmd = nullptr; |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 836 | const char *VersLoadCmd = nullptr; |
Kevin Enderby | 245be3e | 2016-09-29 17:45:23 +0000 | [diff] [blame] | 837 | const char *SourceLoadCmd = nullptr; |
Kevin Enderby | 4f229d8 | 2016-09-29 21:07:29 +0000 | [diff] [blame] | 838 | const char *EntryPointLoadCmd = nullptr; |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 839 | const char *EncryptLoadCmd = nullptr; |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 840 | for (unsigned I = 0; I < LoadCommandCount; ++I) { |
Kevin Enderby | 1851a82 | 2016-07-07 22:11:42 +0000 | [diff] [blame] | 841 | if (is64Bit()) { |
| 842 | if (Load.C.cmdsize % 8 != 0) { |
| 843 | // We have a hack here to allow 64-bit Mach-O core files to have |
| 844 | // LC_THREAD commands that are only a multiple of 4 and not 8 to be |
| 845 | // allowed since the macOS kernel produces them. |
| 846 | if (getHeader().filetype != MachO::MH_CORE || |
| 847 | Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) { |
| 848 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 849 | "multiple of 8"); |
| 850 | return; |
| 851 | } |
| 852 | } |
| 853 | } else { |
| 854 | if (Load.C.cmdsize % 4 != 0) { |
| 855 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 856 | "multiple of 4"); |
| 857 | return; |
| 858 | } |
| 859 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 860 | LoadCommands.push_back(Load); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 861 | if (Load.C.cmd == MachO::LC_SYMTAB) { |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 862 | if ((Err = checkSymtabCommand(this, Load, I, &SymtabLoadCmd))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 863 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 864 | } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 865 | if ((Err = checkDysymtabCommand(this, Load, I, &DysymtabLoadCmd))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 866 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 867 | } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 868 | if ((Err = checkLinkeditDataCommand(this, Load, I, &DataInCodeLoadCmd, |
| 869 | "LC_DATA_IN_CODE"))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 870 | return; |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 871 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 872 | if ((Err = checkLinkeditDataCommand(this, Load, I, &LinkOptHintsLoadCmd, |
| 873 | "LC_LINKER_OPTIMIZATION_HINT"))) |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 874 | return; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 875 | } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) { |
| 876 | if ((Err = checkLinkeditDataCommand(this, Load, I, &FuncStartsLoadCmd, |
| 877 | "LC_FUNCTION_STARTS"))) |
| 878 | return; |
| 879 | } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) { |
| 880 | if ((Err = checkLinkeditDataCommand(this, Load, I, &SplitInfoLoadCmd, |
| 881 | "LC_SEGMENT_SPLIT_INFO"))) |
| 882 | return; |
| 883 | } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) { |
| 884 | if ((Err = checkLinkeditDataCommand(this, Load, I, &CodeSignDrsLoadCmd, |
| 885 | "LC_DYLIB_CODE_SIGN_DRS"))) |
| 886 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 887 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO) { |
| 888 | if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd, |
| 889 | "LC_DYLD_INFO"))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 890 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 891 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
| 892 | if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd, |
| 893 | "LC_DYLD_INFO_ONLY"))) |
| 894 | return; |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 895 | } else if (Load.C.cmd == MachO::LC_UUID) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 896 | if (Load.C.cmdsize != sizeof(MachO::uuid_command)) { |
| 897 | Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect " |
| 898 | "cmdsize"); |
| 899 | return; |
| 900 | } |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 901 | if (UuidLoadCmd) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 902 | Err = malformedError("more than one LC_UUID command"); |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 903 | return; |
| 904 | } |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 905 | UuidLoadCmd = Load.Ptr; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 906 | } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 907 | if ((Err = parseSegmentLoadCommand<MachO::segment_command_64, |
| 908 | MachO::section_64>( |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 909 | this, Load, Sections, HasPageZeroSegment, I, |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 910 | "LC_SEGMENT_64", SizeOfHeaders))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 911 | return; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 912 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 913 | if ((Err = parseSegmentLoadCommand<MachO::segment_command, |
| 914 | MachO::section>( |
| 915 | this, Load, Sections, HasPageZeroSegment, I, |
| 916 | "LC_SEGMENT", SizeOfHeaders))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 917 | return; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 918 | } else if (Load.C.cmd == MachO::LC_ID_DYLIB) { |
| 919 | if ((Err = checkDylibIdCommand(this, Load, I, &DyldIdLoadCmd))) |
| 920 | return; |
| 921 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) { |
| 922 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_DYLIB"))) |
| 923 | return; |
| 924 | Libraries.push_back(Load.Ptr); |
| 925 | } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) { |
| 926 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_WEAK_DYLIB"))) |
| 927 | return; |
| 928 | Libraries.push_back(Load.Ptr); |
| 929 | } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) { |
| 930 | if ((Err = checkDylibCommand(this, Load, I, "LC_LAZY_LOAD_DYLIB"))) |
| 931 | return; |
| 932 | Libraries.push_back(Load.Ptr); |
| 933 | } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) { |
| 934 | if ((Err = checkDylibCommand(this, Load, I, "LC_REEXPORT_DYLIB"))) |
| 935 | return; |
| 936 | Libraries.push_back(Load.Ptr); |
| 937 | } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
| 938 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_UPWARD_DYLIB"))) |
| 939 | return; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 940 | Libraries.push_back(Load.Ptr); |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 941 | } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) { |
| 942 | if ((Err = checkDyldCommand(this, Load, I, "LC_ID_DYLINKER"))) |
| 943 | return; |
| 944 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) { |
| 945 | if ((Err = checkDyldCommand(this, Load, I, "LC_LOAD_DYLINKER"))) |
| 946 | return; |
| 947 | } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
| 948 | if ((Err = checkDyldCommand(this, Load, I, "LC_DYLD_ENVIRONMENT"))) |
| 949 | return; |
Kevin Enderby | 32359db | 2016-09-28 21:20:45 +0000 | [diff] [blame] | 950 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) { |
| 951 | if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd, |
| 952 | "LC_VERSION_MIN_MACOSX"))) |
| 953 | return; |
| 954 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) { |
| 955 | if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd, |
| 956 | "LC_VERSION_MIN_IPHONEOS"))) |
| 957 | return; |
| 958 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) { |
| 959 | if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd, |
| 960 | "LC_VERSION_MIN_TVOS"))) |
| 961 | return; |
| 962 | } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { |
| 963 | if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd, |
| 964 | "LC_VERSION_MIN_WATCHOS"))) |
| 965 | return; |
Kevin Enderby | 76966bf | 2016-09-28 23:16:01 +0000 | [diff] [blame] | 966 | } else if (Load.C.cmd == MachO::LC_RPATH) { |
| 967 | if ((Err = checkRpathCommand(this, Load, I))) |
| 968 | return; |
Kevin Enderby | 245be3e | 2016-09-29 17:45:23 +0000 | [diff] [blame] | 969 | } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) { |
| 970 | if (Load.C.cmdsize != sizeof(MachO::source_version_command)) { |
| 971 | Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) + |
| 972 | " has incorrect cmdsize"); |
| 973 | return; |
| 974 | } |
| 975 | if (SourceLoadCmd) { |
| 976 | Err = malformedError("more than one LC_SOURCE_VERSION command"); |
| 977 | return; |
| 978 | } |
| 979 | SourceLoadCmd = Load.Ptr; |
Kevin Enderby | 4f229d8 | 2016-09-29 21:07:29 +0000 | [diff] [blame] | 980 | } else if (Load.C.cmd == MachO::LC_MAIN) { |
| 981 | if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) { |
| 982 | Err = malformedError("LC_MAIN command " + Twine(I) + |
| 983 | " has incorrect cmdsize"); |
| 984 | return; |
| 985 | } |
| 986 | if (EntryPointLoadCmd) { |
| 987 | Err = malformedError("more than one LC_MAIN command"); |
| 988 | return; |
| 989 | } |
| 990 | EntryPointLoadCmd = Load.Ptr; |
Kevin Enderby | f993d6e | 2016-10-04 20:37:43 +0000 | [diff] [blame] | 991 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) { |
| 992 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) { |
| 993 | Err = malformedError("LC_ENCRYPTION_INFO command " + Twine(I) + |
| 994 | " has incorrect cmdsize"); |
| 995 | return; |
| 996 | } |
| 997 | MachO::encryption_info_command E = |
| 998 | getStruct<MachO::encryption_info_command>(this, Load.Ptr); |
| 999 | if ((Err = checkEncryptCommand(this, Load, I, E.cryptoff, E.cryptsize, |
| 1000 | &EncryptLoadCmd, "LC_ENCRYPTION_INFO"))) |
| 1001 | return; |
| 1002 | } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { |
| 1003 | if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) { |
| 1004 | Err = malformedError("LC_ENCRYPTION_INFO_64 command " + Twine(I) + |
| 1005 | " has incorrect cmdsize"); |
| 1006 | return; |
| 1007 | } |
| 1008 | MachO::encryption_info_command_64 E = |
| 1009 | getStruct<MachO::encryption_info_command_64>(this, Load.Ptr); |
| 1010 | if ((Err = checkEncryptCommand(this, Load, I, E.cryptoff, E.cryptsize, |
| 1011 | &EncryptLoadCmd, "LC_ENCRYPTION_INFO_64"))) |
| 1012 | return; |
Kevin Enderby | 68fffa8 | 2016-10-11 21:04:39 +0000 | [diff] [blame] | 1013 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) { |
| 1014 | if ((Err = checkLinkerOptCommand(this, Load, I))) |
| 1015 | return; |
Kevin Enderby | 2490de0 | 2016-10-17 22:09:25 +0000 | [diff] [blame^] | 1016 | } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) { |
| 1017 | if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) { |
| 1018 | Err = malformedError("load command " + Twine(I) + |
| 1019 | " LC_SUB_FRAMEWORK cmdsize too small"); |
| 1020 | return; |
| 1021 | } |
| 1022 | MachO::sub_framework_command S = |
| 1023 | getStruct<MachO::sub_framework_command>(this, Load.Ptr); |
| 1024 | if ((Err = checkSubCommand(this, Load, I, "LC_SUB_FRAMEWORK", |
| 1025 | sizeof(MachO::sub_framework_command), |
| 1026 | "sub_framework_command", S.umbrella, |
| 1027 | "umbrella"))) |
| 1028 | return; |
| 1029 | } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) { |
| 1030 | if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) { |
| 1031 | Err = malformedError("load command " + Twine(I) + |
| 1032 | " LC_SUB_UMBRELLA cmdsize too small"); |
| 1033 | return; |
| 1034 | } |
| 1035 | MachO::sub_umbrella_command S = |
| 1036 | getStruct<MachO::sub_umbrella_command>(this, Load.Ptr); |
| 1037 | if ((Err = checkSubCommand(this, Load, I, "LC_SUB_UMBRELLA", |
| 1038 | sizeof(MachO::sub_umbrella_command), |
| 1039 | "sub_umbrella_command", S.sub_umbrella, |
| 1040 | "sub_umbrella"))) |
| 1041 | return; |
| 1042 | } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) { |
| 1043 | if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) { |
| 1044 | Err = malformedError("load command " + Twine(I) + |
| 1045 | " LC_SUB_LIBRARY cmdsize too small"); |
| 1046 | return; |
| 1047 | } |
| 1048 | MachO::sub_library_command S = |
| 1049 | getStruct<MachO::sub_library_command>(this, Load.Ptr); |
| 1050 | if ((Err = checkSubCommand(this, Load, I, "LC_SUB_LIBRARY", |
| 1051 | sizeof(MachO::sub_library_command), |
| 1052 | "sub_library_command", S.sub_library, |
| 1053 | "sub_library"))) |
| 1054 | return; |
| 1055 | } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) { |
| 1056 | if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) { |
| 1057 | Err = malformedError("load command " + Twine(I) + |
| 1058 | " LC_SUB_CLIENT cmdsize too small"); |
| 1059 | return; |
| 1060 | } |
| 1061 | MachO::sub_client_command S = |
| 1062 | getStruct<MachO::sub_client_command>(this, Load.Ptr); |
| 1063 | if ((Err = checkSubCommand(this, Load, I, "LC_SUB_CLIENT", |
| 1064 | sizeof(MachO::sub_client_command), |
| 1065 | "sub_client_command", S.client, "client"))) |
| 1066 | return; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1067 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1068 | if (I < LoadCommandCount - 1) { |
Kevin Enderby | 368e714 | 2016-05-03 17:16:08 +0000 | [diff] [blame] | 1069 | if (auto LoadOrErr = getNextLoadCommandInfo(this, I, Load)) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1070 | Load = *LoadOrErr; |
| 1071 | else { |
| 1072 | Err = LoadOrErr.takeError(); |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 1075 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1076 | } |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1077 | if (!SymtabLoadCmd) { |
| 1078 | if (DysymtabLoadCmd) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1079 | Err = malformedError("contains LC_DYSYMTAB load command without a " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1080 | "LC_SYMTAB load command"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1081 | return; |
| 1082 | } |
| 1083 | } else if (DysymtabLoadCmd) { |
| 1084 | MachO::symtab_command Symtab = |
| 1085 | getStruct<MachO::symtab_command>(this, SymtabLoadCmd); |
| 1086 | MachO::dysymtab_command Dysymtab = |
| 1087 | getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); |
| 1088 | if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1089 | Err = malformedError("ilocalsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1090 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1091 | return; |
| 1092 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1093 | uint64_t BigSize = Dysymtab.ilocalsym; |
| 1094 | BigSize += Dysymtab.nlocalsym; |
| 1095 | if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1096 | Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1097 | "command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1098 | return; |
| 1099 | } |
| 1100 | if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1101 | Err = malformedError("nextdefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1102 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1105 | BigSize = Dysymtab.iextdefsym; |
| 1106 | BigSize += Dysymtab.nextdefsym; |
| 1107 | if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1108 | Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1109 | "load command extends past the end of the symbol " |
| 1110 | "table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1111 | return; |
| 1112 | } |
| 1113 | if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1114 | Err = malformedError("nundefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1115 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1116 | return; |
| 1117 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 1118 | BigSize = Dysymtab.iundefsym; |
| 1119 | BigSize += Dysymtab.nundefsym; |
| 1120 | if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1121 | Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1122 | " command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1123 | return; |
| 1124 | } |
| 1125 | } |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 1126 | if ((getHeader().filetype == MachO::MH_DYLIB || |
| 1127 | getHeader().filetype == MachO::MH_DYLIB_STUB) && |
| 1128 | DyldIdLoadCmd == nullptr) { |
| 1129 | Err = malformedError("no LC_ID_DYLIB load command in dynamic library " |
| 1130 | "filetype"); |
| 1131 | return; |
| 1132 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 1133 | assert(LoadCommands.size() == LoadCommandCount); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 1134 | |
| 1135 | Err = Error::success(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1138 | void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1139 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1140 | sizeof(MachO::nlist_64) : |
| 1141 | sizeof(MachO::nlist); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1142 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1145 | Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1146 | StringRef StringTable = getStringTableData(); |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1147 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1148 | const char *Start = &StringTable.data()[Entry.n_strx]; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1149 | if (Start < getData().begin() || Start >= getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1150 | return malformedError("bad string index: " + Twine(Entry.n_strx) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1151 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1152 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1153 | return StringRef(Start); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 1156 | unsigned MachOObjectFile::getSectionType(SectionRef Sec) const { |
| 1157 | DataRefImpl DRI = Sec.getRawDataRefImpl(); |
| 1158 | uint32_t Flags = getSectionFlags(this, DRI); |
| 1159 | return Flags & MachO::SECTION_TYPE; |
| 1160 | } |
| 1161 | |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 1162 | uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const { |
| 1163 | if (is64Bit()) { |
| 1164 | MachO::nlist_64 Entry = getSymbol64TableEntry(Sym); |
| 1165 | return Entry.n_value; |
| 1166 | } |
| 1167 | MachO::nlist Entry = getSymbolTableEntry(Sym); |
| 1168 | return Entry.n_value; |
| 1169 | } |
| 1170 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1171 | // getIndirectName() returns the name of the alias'ed symbol who's string table |
| 1172 | // index is in the n_value field. |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1173 | std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, |
| 1174 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1175 | StringRef StringTable = getStringTableData(); |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 1176 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
| 1177 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 1178 | return object_error::parse_failed; |
| 1179 | uint64_t NValue = getNValue(Symb); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1180 | if (NValue >= StringTable.size()) |
| 1181 | return object_error::parse_failed; |
| 1182 | const char *Start = &StringTable.data()[NValue]; |
| 1183 | Res = StringRef(Start); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1184 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 1187 | uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const { |
Rafael Espindola | 7e7be92 | 2015-07-07 15:05:09 +0000 | [diff] [blame] | 1188 | return getNValue(Sym); |
Rafael Espindola | 991af66 | 2015-06-24 19:11:10 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 1191 | Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const { |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 1192 | return getSymbolValue(Sym); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1195 | uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const { |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1196 | uint32_t flags = getSymbolFlags(DRI); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1197 | if (flags & SymbolRef::SF_Common) { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1198 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1199 | return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1200 | } |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 1201 | return 0; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 1204 | uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 1205 | return getNValue(DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1208 | Expected<SymbolRef::Type> |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1209 | MachOObjectFile::getSymbolType(DataRefImpl Symb) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1210 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1211 | uint8_t n_type = Entry.n_type; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1212 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1213 | // If this is a STAB debugging symbol, we can do nothing more. |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1214 | if (n_type & MachO::N_STAB) |
| 1215 | return SymbolRef::ST_Debug; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1216 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1217 | switch (n_type & MachO::N_TYPE) { |
| 1218 | case MachO::N_UNDF : |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1219 | return SymbolRef::ST_Unknown; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1220 | case MachO::N_SECT : |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1221 | Expected<section_iterator> SecOrError = getSymbolSection(Symb); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1222 | if (!SecOrError) |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1223 | return SecOrError.takeError(); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1224 | section_iterator Sec = *SecOrError; |
Kuba Brecka | de83322 | 2015-11-12 09:40:29 +0000 | [diff] [blame] | 1225 | if (Sec->isData() || Sec->isBSS()) |
| 1226 | return SymbolRef::ST_Data; |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1227 | return SymbolRef::ST_Function; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1228 | } |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 1229 | return SymbolRef::ST_Other; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1232 | uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1233 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1234 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1235 | uint8_t MachOType = Entry.n_type; |
| 1236 | uint16_t MachOFlags = Entry.n_desc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1237 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1238 | uint32_t Result = SymbolRef::SF_None; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1239 | |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 1240 | if ((MachOType & MachO::N_TYPE) == MachO::N_INDR) |
| 1241 | Result |= SymbolRef::SF_Indirect; |
| 1242 | |
Rafael Espindola | a135632 | 2013-11-02 05:03:24 +0000 | [diff] [blame] | 1243 | if (MachOType & MachO::N_STAB) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1244 | Result |= SymbolRef::SF_FormatSpecific; |
| 1245 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1246 | if (MachOType & MachO::N_EXT) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1247 | Result |= SymbolRef::SF_Global; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1248 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 1249 | if (getNValue(DRI)) |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1250 | Result |= SymbolRef::SF_Common; |
Rafael Espindola | d824772 | 2015-07-07 14:26:39 +0000 | [diff] [blame] | 1251 | else |
| 1252 | Result |= SymbolRef::SF_Undefined; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1253 | } |
Lang Hames | 7e0692b | 2015-01-15 22:33:30 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (!(MachOType & MachO::N_PEXT)) |
| 1256 | Result |= SymbolRef::SF_Exported; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1259 | if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1260 | Result |= SymbolRef::SF_Weak; |
| 1261 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1262 | if (MachOFlags & (MachO::N_ARM_THUMB_DEF)) |
| 1263 | Result |= SymbolRef::SF_Thumb; |
| 1264 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1265 | if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1266 | Result |= SymbolRef::SF_Absolute; |
| 1267 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1268 | return Result; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1271 | Expected<section_iterator> |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1272 | MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1273 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1274 | uint8_t index = Entry.n_sect; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1275 | |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1276 | if (index == 0) |
| 1277 | return section_end(); |
| 1278 | DataRefImpl DRI; |
| 1279 | DRI.d.a = index - 1; |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1280 | if (DRI.d.a >= Sections.size()){ |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1281 | return malformedError("bad section index: " + Twine((int)index) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1282 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1283 | } |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1284 | return section_iterator(SectionRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1287 | unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const { |
| 1288 | MachO::nlist_base Entry = |
| 1289 | getSymbolTableEntryBase(this, Sym.getRawDataRefImpl()); |
| 1290 | return Entry.n_sect - 1; |
| 1291 | } |
| 1292 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1293 | void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1294 | Sec.d.a++; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1297 | std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec, |
| 1298 | StringRef &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1299 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 1300 | Result = parseSegmentOrSectionName(Raw.data()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1301 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1304 | uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const { |
| 1305 | if (is64Bit()) |
| 1306 | return getSection64(Sec).addr; |
| 1307 | return getSection(Sec).addr; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1310 | uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const { |
Kevin Enderby | 46e642f | 2015-10-08 22:50:55 +0000 | [diff] [blame] | 1311 | // In the case if a malformed Mach-O file where the section offset is past |
| 1312 | // the end of the file or some part of the section size is past the end of |
| 1313 | // the file return a size of zero or a size that covers the rest of the file |
| 1314 | // but does not extend past the end of the file. |
| 1315 | uint32_t SectOffset, SectType; |
| 1316 | uint64_t SectSize; |
| 1317 | |
| 1318 | if (is64Bit()) { |
| 1319 | MachO::section_64 Sect = getSection64(Sec); |
| 1320 | SectOffset = Sect.offset; |
| 1321 | SectSize = Sect.size; |
| 1322 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1323 | } else { |
| 1324 | MachO::section Sect = getSection(Sec); |
| 1325 | SectOffset = Sect.offset; |
| 1326 | SectSize = Sect.size; |
| 1327 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1328 | } |
| 1329 | if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL) |
| 1330 | return SectSize; |
| 1331 | uint64_t FileSize = getData().size(); |
| 1332 | if (SectOffset > FileSize) |
| 1333 | return 0; |
| 1334 | if (FileSize - SectOffset < SectSize) |
| 1335 | return FileSize - SectOffset; |
| 1336 | return SectSize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1339 | std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec, |
| 1340 | StringRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1341 | uint32_t Offset; |
| 1342 | uint64_t Size; |
| 1343 | |
| 1344 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1345 | MachO::section_64 Sect = getSection64(Sec); |
| 1346 | Offset = Sect.offset; |
| 1347 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1348 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1349 | MachO::section Sect = getSection(Sec); |
| 1350 | Offset = Sect.offset; |
| 1351 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | Res = this->getData().substr(Offset, Size); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1355 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1358 | uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1359 | uint32_t Align; |
| 1360 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1361 | MachO::section_64 Sect = getSection64(Sec); |
| 1362 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1363 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1364 | MachO::section Sect = getSection(Sec); |
| 1365 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1368 | return uint64_t(1) << Align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
George Rimar | 401e4e5 | 2016-05-24 12:48:46 +0000 | [diff] [blame] | 1371 | bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1375 | bool MachOObjectFile::isSectionText(DataRefImpl Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1376 | uint32_t Flags = getSectionFlags(this, Sec); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1377 | return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1380 | bool MachOObjectFile::isSectionData(DataRefImpl Sec) const { |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1381 | uint32_t Flags = getSectionFlags(this, Sec); |
| 1382 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1383 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1384 | !(SectionType == MachO::S_ZEROFILL || |
| 1385 | SectionType == MachO::S_GB_ZEROFILL); |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1388 | bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const { |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1389 | uint32_t Flags = getSectionFlags(this, Sec); |
| 1390 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1391 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1392 | (SectionType == MachO::S_ZEROFILL || |
| 1393 | SectionType == MachO::S_GB_ZEROFILL); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1396 | unsigned MachOObjectFile::getSectionID(SectionRef Sec) const { |
| 1397 | return Sec.getRawDataRefImpl().d.a; |
| 1398 | } |
| 1399 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1400 | bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const { |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1401 | // FIXME: Unimplemented. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1402 | return false; |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Steven Wu | f2fe014 | 2016-02-29 19:40:10 +0000 | [diff] [blame] | 1405 | bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const { |
| 1406 | StringRef SegmentName = getSectionFinalSegmentName(Sec); |
| 1407 | StringRef SectName; |
| 1408 | if (!getSectionName(Sec, SectName)) |
| 1409 | return (SegmentName == "__LLVM" && SectName == "__bitcode"); |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1413 | relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1414 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1415 | Ret.d.a = Sec.d.a; |
| 1416 | Ret.d.b = 0; |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1417 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1418 | } |
Rafael Espindola | c0406e1 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 1419 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1420 | relocation_iterator |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1421 | MachOObjectFile::section_rel_end(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1422 | uint32_t Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1423 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1424 | MachO::section_64 Sect = getSection64(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1425 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1426 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1427 | MachO::section Sect = getSection(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1428 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1429 | } |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1430 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1431 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1432 | Ret.d.a = Sec.d.a; |
| 1433 | Ret.d.b = Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1434 | return relocation_iterator(RelocationRef(Ret, this)); |
| 1435 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1436 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1437 | void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1438 | ++Rel.d.b; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1439 | } |
Owen Anderson | 171f485 | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1440 | |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1441 | uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const { |
Rafael Espindola | 7247546 | 2014-04-04 00:31:12 +0000 | [diff] [blame] | 1442 | assert(getHeader().filetype == MachO::MH_OBJECT && |
| 1443 | "Only implemented for MH_OBJECT"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1444 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1445 | return getAnyRelocationAddress(RE); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1448 | symbol_iterator |
| 1449 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1450 | MachO::any_relocation_info RE = getRelocation(Rel); |
Tim Northover | 07f99fb | 2014-07-04 10:57:56 +0000 | [diff] [blame] | 1451 | if (isRelocationScattered(RE)) |
| 1452 | return symbol_end(); |
| 1453 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1454 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 1455 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1456 | if (!isExtern) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1457 | return symbol_end(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1458 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1459 | MachO::symtab_command S = getSymtabLoadCommand(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1460 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1461 | sizeof(MachO::nlist_64) : |
| 1462 | sizeof(MachO::nlist); |
| 1463 | uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1464 | DataRefImpl Sym; |
| 1465 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1466 | return symbol_iterator(SymbolRef(Sym, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1469 | section_iterator |
| 1470 | MachOObjectFile::getRelocationSection(DataRefImpl Rel) const { |
| 1471 | return section_iterator(getAnyRelocationSection(getRelocation(Rel))); |
| 1472 | } |
| 1473 | |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1474 | uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1475 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1476 | return getAnyRelocationType(RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1479 | void MachOObjectFile::getRelocationTypeName( |
| 1480 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1481 | StringRef res; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1482 | uint64_t RType = getRelocationType(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1483 | |
| 1484 | unsigned Arch = this->getArch(); |
| 1485 | |
| 1486 | switch (Arch) { |
| 1487 | case Triple::x86: { |
| 1488 | static const char *const Table[] = { |
| 1489 | "GENERIC_RELOC_VANILLA", |
| 1490 | "GENERIC_RELOC_PAIR", |
| 1491 | "GENERIC_RELOC_SECTDIFF", |
| 1492 | "GENERIC_RELOC_PB_LA_PTR", |
| 1493 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 1494 | "GENERIC_RELOC_TLV" }; |
| 1495 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 1496 | if (RType > 5) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1497 | res = "Unknown"; |
| 1498 | else |
| 1499 | res = Table[RType]; |
| 1500 | break; |
| 1501 | } |
| 1502 | case Triple::x86_64: { |
| 1503 | static const char *const Table[] = { |
| 1504 | "X86_64_RELOC_UNSIGNED", |
| 1505 | "X86_64_RELOC_SIGNED", |
| 1506 | "X86_64_RELOC_BRANCH", |
| 1507 | "X86_64_RELOC_GOT_LOAD", |
| 1508 | "X86_64_RELOC_GOT", |
| 1509 | "X86_64_RELOC_SUBTRACTOR", |
| 1510 | "X86_64_RELOC_SIGNED_1", |
| 1511 | "X86_64_RELOC_SIGNED_2", |
| 1512 | "X86_64_RELOC_SIGNED_4", |
| 1513 | "X86_64_RELOC_TLV" }; |
| 1514 | |
| 1515 | if (RType > 9) |
| 1516 | res = "Unknown"; |
| 1517 | else |
| 1518 | res = Table[RType]; |
| 1519 | break; |
| 1520 | } |
| 1521 | case Triple::arm: { |
| 1522 | static const char *const Table[] = { |
| 1523 | "ARM_RELOC_VANILLA", |
| 1524 | "ARM_RELOC_PAIR", |
| 1525 | "ARM_RELOC_SECTDIFF", |
| 1526 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 1527 | "ARM_RELOC_PB_LA_PTR", |
| 1528 | "ARM_RELOC_BR24", |
| 1529 | "ARM_THUMB_RELOC_BR22", |
| 1530 | "ARM_THUMB_32BIT_BRANCH", |
| 1531 | "ARM_RELOC_HALF", |
| 1532 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 1533 | |
| 1534 | if (RType > 9) |
| 1535 | res = "Unknown"; |
| 1536 | else |
| 1537 | res = Table[RType]; |
| 1538 | break; |
| 1539 | } |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1540 | case Triple::aarch64: { |
| 1541 | static const char *const Table[] = { |
| 1542 | "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR", |
| 1543 | "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", |
| 1544 | "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21", |
| 1545 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT", |
| 1546 | "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", |
| 1547 | "ARM64_RELOC_ADDEND" |
| 1548 | }; |
| 1549 | |
| 1550 | if (RType >= array_lengthof(Table)) |
| 1551 | res = "Unknown"; |
| 1552 | else |
| 1553 | res = Table[RType]; |
| 1554 | break; |
| 1555 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1556 | case Triple::ppc: { |
| 1557 | static const char *const Table[] = { |
| 1558 | "PPC_RELOC_VANILLA", |
| 1559 | "PPC_RELOC_PAIR", |
| 1560 | "PPC_RELOC_BR14", |
| 1561 | "PPC_RELOC_BR24", |
| 1562 | "PPC_RELOC_HI16", |
| 1563 | "PPC_RELOC_LO16", |
| 1564 | "PPC_RELOC_HA16", |
| 1565 | "PPC_RELOC_LO14", |
| 1566 | "PPC_RELOC_SECTDIFF", |
| 1567 | "PPC_RELOC_PB_LA_PTR", |
| 1568 | "PPC_RELOC_HI16_SECTDIFF", |
| 1569 | "PPC_RELOC_LO16_SECTDIFF", |
| 1570 | "PPC_RELOC_HA16_SECTDIFF", |
| 1571 | "PPC_RELOC_JBSR", |
| 1572 | "PPC_RELOC_LO14_SECTDIFF", |
| 1573 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 1574 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 1575 | if (RType > 15) |
| 1576 | res = "Unknown"; |
| 1577 | else |
| 1578 | res = Table[RType]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | } |
| 1581 | case Triple::UnknownArch: |
| 1582 | res = "Unknown"; |
| 1583 | break; |
| 1584 | } |
| 1585 | Result.append(res.begin(), res.end()); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 1588 | uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const { |
| 1589 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 1590 | return getAnyRelocationLength(RE); |
| 1591 | } |
| 1592 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1593 | // |
| 1594 | // guessLibraryShortName() is passed a name of a dynamic library and returns a |
| 1595 | // guess on what the short name is. Then name is returned as a substring of the |
| 1596 | // StringRef Name passed in. The name of the dynamic library is recognized as |
| 1597 | // a framework if it has one of the two following forms: |
| 1598 | // Foo.framework/Versions/A/Foo |
| 1599 | // Foo.framework/Foo |
| 1600 | // Where A and Foo can be any string. And may contain a trailing suffix |
| 1601 | // starting with an underbar. If the Name is recognized as a framework then |
| 1602 | // isFramework is set to true else it is set to false. If the Name has a |
| 1603 | // suffix then Suffix is set to the substring in Name that contains the suffix |
| 1604 | // else it is set to a NULL StringRef. |
| 1605 | // |
| 1606 | // The Name of the dynamic library is recognized as a library name if it has |
| 1607 | // one of the two following forms: |
| 1608 | // libFoo.A.dylib |
| 1609 | // libFoo.dylib |
| 1610 | // The library may have a suffix trailing the name Foo of the form: |
| 1611 | // libFoo_profile.A.dylib |
| 1612 | // libFoo_profile.dylib |
| 1613 | // |
| 1614 | // The Name of the dynamic library is also recognized as a library name if it |
| 1615 | // has the following form: |
| 1616 | // Foo.qtx |
| 1617 | // |
| 1618 | // If the Name of the dynamic library is none of the forms above then a NULL |
| 1619 | // StringRef is returned. |
| 1620 | // |
| 1621 | StringRef MachOObjectFile::guessLibraryShortName(StringRef Name, |
| 1622 | bool &isFramework, |
| 1623 | StringRef &Suffix) { |
| 1624 | StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx; |
| 1625 | size_t a, b, c, d, Idx; |
| 1626 | |
| 1627 | isFramework = false; |
| 1628 | Suffix = StringRef(); |
| 1629 | |
| 1630 | // Pull off the last component and make Foo point to it |
| 1631 | a = Name.rfind('/'); |
| 1632 | if (a == Name.npos || a == 0) |
| 1633 | goto guess_library; |
| 1634 | Foo = Name.slice(a+1, Name.npos); |
| 1635 | |
| 1636 | // Look for a suffix starting with a '_' |
| 1637 | Idx = Foo.rfind('_'); |
| 1638 | if (Idx != Foo.npos && Foo.size() >= 2) { |
| 1639 | Suffix = Foo.slice(Idx, Foo.npos); |
| 1640 | Foo = Foo.slice(0, Idx); |
| 1641 | } |
| 1642 | |
| 1643 | // First look for the form Foo.framework/Foo |
| 1644 | b = Name.rfind('/', a); |
| 1645 | if (b == Name.npos) |
| 1646 | Idx = 0; |
| 1647 | else |
| 1648 | Idx = b+1; |
| 1649 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1650 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1651 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1652 | if (F == Foo && DotFramework == ".framework/") { |
| 1653 | isFramework = true; |
| 1654 | return Foo; |
| 1655 | } |
| 1656 | |
| 1657 | // Next look for the form Foo.framework/Versions/A/Foo |
| 1658 | if (b == Name.npos) |
| 1659 | goto guess_library; |
| 1660 | c = Name.rfind('/', b); |
| 1661 | if (c == Name.npos || c == 0) |
| 1662 | goto guess_library; |
| 1663 | V = Name.slice(c+1, Name.npos); |
| 1664 | if (!V.startswith("Versions/")) |
| 1665 | goto guess_library; |
| 1666 | d = Name.rfind('/', c); |
| 1667 | if (d == Name.npos) |
| 1668 | Idx = 0; |
| 1669 | else |
| 1670 | Idx = d+1; |
| 1671 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1672 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1673 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1674 | if (F == Foo && DotFramework == ".framework/") { |
| 1675 | isFramework = true; |
| 1676 | return Foo; |
| 1677 | } |
| 1678 | |
| 1679 | guess_library: |
| 1680 | // pull off the suffix after the "." and make a point to it |
| 1681 | a = Name.rfind('.'); |
| 1682 | if (a == Name.npos || a == 0) |
| 1683 | return StringRef(); |
| 1684 | Dylib = Name.slice(a, Name.npos); |
| 1685 | if (Dylib != ".dylib") |
| 1686 | goto guess_qtx; |
| 1687 | |
| 1688 | // First pull off the version letter for the form Foo.A.dylib if any. |
| 1689 | if (a >= 3) { |
| 1690 | Dot = Name.slice(a-2, a-1); |
| 1691 | if (Dot == ".") |
| 1692 | a = a - 2; |
| 1693 | } |
| 1694 | |
| 1695 | b = Name.rfind('/', a); |
| 1696 | if (b == Name.npos) |
| 1697 | b = 0; |
| 1698 | else |
| 1699 | b = b+1; |
| 1700 | // ignore any suffix after an underbar like Foo_profile.A.dylib |
| 1701 | Idx = Name.find('_', b); |
| 1702 | if (Idx != Name.npos && Idx != b) { |
| 1703 | Lib = Name.slice(b, Idx); |
| 1704 | Suffix = Name.slice(Idx, a); |
| 1705 | } |
| 1706 | else |
| 1707 | Lib = Name.slice(b, a); |
| 1708 | // There are incorrect library names of the form: |
| 1709 | // libATS.A_profile.dylib so check for these. |
| 1710 | if (Lib.size() >= 3) { |
| 1711 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1712 | if (Dot == ".") |
| 1713 | Lib = Lib.slice(0, Lib.size()-2); |
| 1714 | } |
| 1715 | return Lib; |
| 1716 | |
| 1717 | guess_qtx: |
| 1718 | Qtx = Name.slice(a, Name.npos); |
| 1719 | if (Qtx != ".qtx") |
| 1720 | return StringRef(); |
| 1721 | b = Name.rfind('/', a); |
| 1722 | if (b == Name.npos) |
| 1723 | Lib = Name.slice(0, a); |
| 1724 | else |
| 1725 | Lib = Name.slice(b+1, a); |
| 1726 | // There are library names of the form: QT.A.qtx so check for these. |
| 1727 | if (Lib.size() >= 3) { |
| 1728 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1729 | if (Dot == ".") |
| 1730 | Lib = Lib.slice(0, Lib.size()-2); |
| 1731 | } |
| 1732 | return Lib; |
| 1733 | } |
| 1734 | |
| 1735 | // getLibraryShortNameByIndex() is used to get the short name of the library |
| 1736 | // for an undefined symbol in a linked Mach-O binary that was linked with the |
| 1737 | // normal two-level namespace default (that is MH_TWOLEVEL in the header). |
| 1738 | // It is passed the index (0 - based) of the library as translated from |
| 1739 | // GET_LIBRARY_ORDINAL (1 - based). |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1740 | std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index, |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1741 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1742 | if (Index >= Libraries.size()) |
| 1743 | return object_error::parse_failed; |
| 1744 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1745 | // If the cache of LibrariesShortNames is not built up do that first for |
| 1746 | // all the Libraries. |
| 1747 | if (LibrariesShortNames.size() == 0) { |
| 1748 | for (unsigned i = 0; i < Libraries.size(); i++) { |
| 1749 | MachO::dylib_command D = |
| 1750 | getStruct<MachO::dylib_command>(this, Libraries[i]); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1751 | if (D.dylib.name >= D.cmdsize) |
| 1752 | return object_error::parse_failed; |
Kevin Enderby | 4eff6cd | 2014-06-20 18:07:34 +0000 | [diff] [blame] | 1753 | const char *P = (const char *)(Libraries[i]) + D.dylib.name; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1754 | StringRef Name = StringRef(P); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1755 | if (D.dylib.name+Name.size() >= D.cmdsize) |
| 1756 | return object_error::parse_failed; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1757 | StringRef Suffix; |
| 1758 | bool isFramework; |
| 1759 | StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1760 | if (shortName.empty()) |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1761 | LibrariesShortNames.push_back(Name); |
| 1762 | else |
| 1763 | LibrariesShortNames.push_back(shortName); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | Res = LibrariesShortNames[Index]; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1768 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Rafael Espindola | 76ad232 | 2015-07-06 14:55:37 +0000 | [diff] [blame] | 1771 | section_iterator |
| 1772 | MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const { |
| 1773 | DataRefImpl Sec; |
| 1774 | Sec.d.a = Rel->getRawDataRefImpl().d.a; |
| 1775 | return section_iterator(SectionRef(Sec, this)); |
| 1776 | } |
| 1777 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1778 | basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const { |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1779 | DataRefImpl DRI; |
| 1780 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1781 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
| 1782 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1783 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1784 | return getSymbolByIndex(0); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1787 | basic_symbol_iterator MachOObjectFile::symbol_end_impl() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1788 | DataRefImpl DRI; |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1789 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1790 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1791 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1792 | |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1793 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1794 | sizeof(MachO::nlist_64) : |
| 1795 | sizeof(MachO::nlist); |
| 1796 | unsigned Offset = Symtab.symoff + |
| 1797 | Symtab.nsyms * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1798 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1799 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1802 | basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const { |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1803 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1804 | if (!SymtabLoadCmd || Index >= Symtab.nsyms) |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 1805 | report_fatal_error("Requested symbol index is out of range."); |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1806 | unsigned SymbolTableEntrySize = |
| 1807 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1808 | DataRefImpl DRI; |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1809 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); |
| 1810 | DRI.p += Index * SymbolTableEntrySize; |
| 1811 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1812 | } |
| 1813 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1814 | uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const { |
| 1815 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1816 | if (!SymtabLoadCmd) |
| 1817 | report_fatal_error("getSymbolIndex() called with no symbol table symbol"); |
| 1818 | unsigned SymbolTableEntrySize = |
| 1819 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 1820 | DataRefImpl DRIstart; |
| 1821 | DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); |
| 1822 | uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize; |
| 1823 | return Index; |
| 1824 | } |
| 1825 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1826 | section_iterator MachOObjectFile::section_begin() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1827 | DataRefImpl DRI; |
| 1828 | return section_iterator(SectionRef(DRI, this)); |
| 1829 | } |
| 1830 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1831 | section_iterator MachOObjectFile::section_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1832 | DataRefImpl DRI; |
| 1833 | DRI.d.a = Sections.size(); |
| 1834 | return section_iterator(SectionRef(DRI, this)); |
| 1835 | } |
| 1836 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1837 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 1838 | return is64Bit() ? 8 : 4; |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1841 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1842 | unsigned CPUType = getCPUType(this); |
| 1843 | if (!is64Bit()) { |
| 1844 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1845 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1846 | return "Mach-O 32-bit i386"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1847 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1848 | return "Mach-O arm"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1849 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1850 | return "Mach-O 32-bit ppc"; |
| 1851 | default: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1852 | return "Mach-O 32-bit unknown"; |
| 1853 | } |
| 1854 | } |
| 1855 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1856 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1857 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1858 | return "Mach-O 64-bit x86-64"; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1859 | case llvm::MachO::CPU_TYPE_ARM64: |
| 1860 | return "Mach-O arm64"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1861 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1862 | return "Mach-O 64-bit ppc64"; |
| 1863 | default: |
| 1864 | return "Mach-O 64-bit unknown"; |
| 1865 | } |
| 1866 | } |
| 1867 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1868 | Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) { |
| 1869 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1870 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1871 | return Triple::x86; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1872 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1873 | return Triple::x86_64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1874 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1875 | return Triple::arm; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1876 | case llvm::MachO::CPU_TYPE_ARM64: |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 1877 | return Triple::aarch64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1878 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1879 | return Triple::ppc; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1880 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1881 | return Triple::ppc64; |
| 1882 | default: |
| 1883 | return Triple::UnknownArch; |
| 1884 | } |
| 1885 | } |
| 1886 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1887 | Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType, |
| 1888 | const char **McpuDefault) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1889 | if (McpuDefault) |
| 1890 | *McpuDefault = nullptr; |
| 1891 | |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1892 | switch (CPUType) { |
| 1893 | case MachO::CPU_TYPE_I386: |
| 1894 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1895 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 1896 | return Triple("i386-apple-darwin"); |
| 1897 | default: |
| 1898 | return Triple(); |
| 1899 | } |
| 1900 | case MachO::CPU_TYPE_X86_64: |
| 1901 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1902 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 1903 | return Triple("x86_64-apple-darwin"); |
| 1904 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 1905 | return Triple("x86_64h-apple-darwin"); |
| 1906 | default: |
| 1907 | return Triple(); |
| 1908 | } |
| 1909 | case MachO::CPU_TYPE_ARM: |
| 1910 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1911 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 1912 | return Triple("armv4t-apple-darwin"); |
| 1913 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 1914 | return Triple("armv5e-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 1915 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 1916 | return Triple("xscale-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1917 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 1918 | return Triple("armv6-apple-darwin"); |
| 1919 | case MachO::CPU_SUBTYPE_ARM_V6M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1920 | if (McpuDefault) |
| 1921 | *McpuDefault = "cortex-m0"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1922 | return Triple("armv6m-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 1923 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 1924 | return Triple("armv7-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1925 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1926 | if (McpuDefault) |
| 1927 | *McpuDefault = "cortex-m4"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1928 | return Triple("thumbv7em-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1929 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 1930 | return Triple("armv7k-apple-darwin"); |
| 1931 | case MachO::CPU_SUBTYPE_ARM_V7M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1932 | if (McpuDefault) |
| 1933 | *McpuDefault = "cortex-m3"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1934 | return Triple("thumbv7m-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1935 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 1936 | return Triple("armv7s-apple-darwin"); |
| 1937 | default: |
| 1938 | return Triple(); |
| 1939 | } |
| 1940 | case MachO::CPU_TYPE_ARM64: |
| 1941 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1942 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 1943 | return Triple("arm64-apple-darwin"); |
| 1944 | default: |
| 1945 | return Triple(); |
| 1946 | } |
| 1947 | case MachO::CPU_TYPE_POWERPC: |
| 1948 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1949 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1950 | return Triple("ppc-apple-darwin"); |
| 1951 | default: |
| 1952 | return Triple(); |
| 1953 | } |
| 1954 | case MachO::CPU_TYPE_POWERPC64: |
Reid Kleckner | 4da3d57 | 2014-06-30 20:12:59 +0000 | [diff] [blame] | 1955 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1956 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1957 | return Triple("ppc64-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1958 | default: |
| 1959 | return Triple(); |
| 1960 | } |
| 1961 | default: |
| 1962 | return Triple(); |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | Triple MachOObjectFile::getHostArch() { |
| 1967 | return Triple(sys::getDefaultTargetTriple()); |
| 1968 | } |
| 1969 | |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 1970 | bool MachOObjectFile::isValidArch(StringRef ArchFlag) { |
| 1971 | return StringSwitch<bool>(ArchFlag) |
| 1972 | .Case("i386", true) |
| 1973 | .Case("x86_64", true) |
| 1974 | .Case("x86_64h", true) |
| 1975 | .Case("armv4t", true) |
| 1976 | .Case("arm", true) |
| 1977 | .Case("armv5e", true) |
| 1978 | .Case("armv6", true) |
| 1979 | .Case("armv6m", true) |
Frederic Riss | 40baa0a | 2015-06-16 17:37:03 +0000 | [diff] [blame] | 1980 | .Case("armv7", true) |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 1981 | .Case("armv7em", true) |
| 1982 | .Case("armv7k", true) |
| 1983 | .Case("armv7m", true) |
| 1984 | .Case("armv7s", true) |
| 1985 | .Case("arm64", true) |
| 1986 | .Case("ppc", true) |
| 1987 | .Case("ppc64", true) |
| 1988 | .Default(false); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1991 | unsigned MachOObjectFile::getArch() const { |
| 1992 | return getArch(getCPUType(this)); |
| 1993 | } |
| 1994 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1995 | Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const { |
| 1996 | return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1999 | relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2000 | DataRefImpl DRI; |
| 2001 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2002 | return section_rel_begin(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2005 | relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2006 | DataRefImpl DRI; |
| 2007 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 2008 | return section_rel_end(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2011 | dice_iterator MachOObjectFile::begin_dices() const { |
| 2012 | DataRefImpl DRI; |
| 2013 | if (!DataInCodeLoadCmd) |
| 2014 | return dice_iterator(DiceRef(DRI, this)); |
| 2015 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2016 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 2017 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2018 | return dice_iterator(DiceRef(DRI, this)); |
| 2019 | } |
| 2020 | |
| 2021 | dice_iterator MachOObjectFile::end_dices() const { |
| 2022 | DataRefImpl DRI; |
| 2023 | if (!DataInCodeLoadCmd) |
| 2024 | return dice_iterator(DiceRef(DRI, this)); |
| 2025 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2026 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 2027 | unsigned Offset = DicLC.dataoff + DicLC.datasize; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2028 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 2029 | return dice_iterator(DiceRef(DRI, this)); |
| 2030 | } |
| 2031 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2032 | ExportEntry::ExportEntry(ArrayRef<uint8_t> T) |
| 2033 | : Trie(T), Malformed(false), Done(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2034 | |
| 2035 | void ExportEntry::moveToFirst() { |
| 2036 | pushNode(0); |
| 2037 | pushDownUntilBottom(); |
| 2038 | } |
| 2039 | |
| 2040 | void ExportEntry::moveToEnd() { |
| 2041 | Stack.clear(); |
| 2042 | Done = true; |
| 2043 | } |
| 2044 | |
| 2045 | bool ExportEntry::operator==(const ExportEntry &Other) const { |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 2046 | // Common case, one at end, other iterating from begin. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2047 | if (Done || Other.Done) |
| 2048 | return (Done == Other.Done); |
| 2049 | // Not equal if different stack sizes. |
| 2050 | if (Stack.size() != Other.Stack.size()) |
| 2051 | return false; |
| 2052 | // Not equal if different cumulative strings. |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 2053 | if (!CumulativeString.equals(Other.CumulativeString)) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | // Equal if all nodes in both stacks match. |
| 2056 | for (unsigned i=0; i < Stack.size(); ++i) { |
| 2057 | if (Stack[i].Start != Other.Stack[i].Start) |
| 2058 | return false; |
| 2059 | } |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2060 | return true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2063 | uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) { |
| 2064 | unsigned Count; |
| 2065 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2066 | Ptr += Count; |
| 2067 | if (Ptr > Trie.end()) { |
| 2068 | Ptr = Trie.end(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2069 | Malformed = true; |
| 2070 | } |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2071 | return Result; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | StringRef ExportEntry::name() const { |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 2075 | return CumulativeString; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | uint64_t ExportEntry::flags() const { |
| 2079 | return Stack.back().Flags; |
| 2080 | } |
| 2081 | |
| 2082 | uint64_t ExportEntry::address() const { |
| 2083 | return Stack.back().Address; |
| 2084 | } |
| 2085 | |
| 2086 | uint64_t ExportEntry::other() const { |
| 2087 | return Stack.back().Other; |
| 2088 | } |
| 2089 | |
| 2090 | StringRef ExportEntry::otherName() const { |
| 2091 | const char* ImportName = Stack.back().ImportName; |
| 2092 | if (ImportName) |
| 2093 | return StringRef(ImportName); |
| 2094 | return StringRef(); |
| 2095 | } |
| 2096 | |
| 2097 | uint32_t ExportEntry::nodeOffset() const { |
| 2098 | return Stack.back().Start - Trie.begin(); |
| 2099 | } |
| 2100 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2101 | ExportEntry::NodeState::NodeState(const uint8_t *Ptr) |
| 2102 | : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0), |
| 2103 | ImportName(nullptr), ChildCount(0), NextChildIndex(0), |
| 2104 | ParentStringLength(0), IsExportNode(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2105 | |
| 2106 | void ExportEntry::pushNode(uint64_t offset) { |
| 2107 | const uint8_t *Ptr = Trie.begin() + offset; |
| 2108 | NodeState State(Ptr); |
| 2109 | uint64_t ExportInfoSize = readULEB128(State.Current); |
| 2110 | State.IsExportNode = (ExportInfoSize != 0); |
| 2111 | const uint8_t* Children = State.Current + ExportInfoSize; |
| 2112 | if (State.IsExportNode) { |
| 2113 | State.Flags = readULEB128(State.Current); |
| 2114 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { |
| 2115 | State.Address = 0; |
| 2116 | State.Other = readULEB128(State.Current); // dylib ordinal |
| 2117 | State.ImportName = reinterpret_cast<const char*>(State.Current); |
| 2118 | } else { |
| 2119 | State.Address = readULEB128(State.Current); |
Nick Kledzik | 1b591bd | 2014-08-30 01:57:34 +0000 | [diff] [blame] | 2120 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2121 | State.Other = readULEB128(State.Current); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | State.ChildCount = *Children; |
| 2125 | State.Current = Children + 1; |
| 2126 | State.NextChildIndex = 0; |
| 2127 | State.ParentStringLength = CumulativeString.size(); |
| 2128 | Stack.push_back(State); |
| 2129 | } |
| 2130 | |
| 2131 | void ExportEntry::pushDownUntilBottom() { |
| 2132 | while (Stack.back().NextChildIndex < Stack.back().ChildCount) { |
| 2133 | NodeState &Top = Stack.back(); |
| 2134 | CumulativeString.resize(Top.ParentStringLength); |
| 2135 | for (;*Top.Current != 0; Top.Current++) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 2136 | char C = *Top.Current; |
| 2137 | CumulativeString.push_back(C); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2138 | } |
| 2139 | Top.Current += 1; |
| 2140 | uint64_t childNodeIndex = readULEB128(Top.Current); |
| 2141 | Top.NextChildIndex += 1; |
| 2142 | pushNode(childNodeIndex); |
| 2143 | } |
| 2144 | if (!Stack.back().IsExportNode) { |
| 2145 | Malformed = true; |
| 2146 | moveToEnd(); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | // We have a trie data structure and need a way to walk it that is compatible |
| 2151 | // with the C++ iterator model. The solution is a non-recursive depth first |
| 2152 | // traversal where the iterator contains a stack of parent nodes along with a |
| 2153 | // string that is the accumulation of all edge strings along the parent chain |
| 2154 | // to this point. |
| 2155 | // |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2156 | // There is one "export" node for each exported symbol. But because some |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2157 | // 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] | 2158 | // node may have child nodes too. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2159 | // |
| 2160 | // The algorithm for moveNext() is to keep moving down the leftmost unvisited |
| 2161 | // child until hitting a node with no children (which is an export node or |
| 2162 | // else the trie is malformed). On the way down, each node is pushed on the |
| 2163 | // stack ivar. If there is no more ways down, it pops up one and tries to go |
| 2164 | // down a sibling path until a childless node is reached. |
| 2165 | void ExportEntry::moveNext() { |
| 2166 | if (Stack.empty() || !Stack.back().IsExportNode) { |
| 2167 | Malformed = true; |
| 2168 | moveToEnd(); |
| 2169 | return; |
| 2170 | } |
| 2171 | |
| 2172 | Stack.pop_back(); |
| 2173 | while (!Stack.empty()) { |
| 2174 | NodeState &Top = Stack.back(); |
| 2175 | if (Top.NextChildIndex < Top.ChildCount) { |
| 2176 | pushDownUntilBottom(); |
| 2177 | // Now at the next export node. |
| 2178 | return; |
| 2179 | } else { |
| 2180 | if (Top.IsExportNode) { |
| 2181 | // This node has no children but is itself an export node. |
| 2182 | CumulativeString.resize(Top.ParentStringLength); |
| 2183 | return; |
| 2184 | } |
| 2185 | Stack.pop_back(); |
| 2186 | } |
| 2187 | } |
| 2188 | Done = true; |
| 2189 | } |
| 2190 | |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2191 | iterator_range<export_iterator> |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2192 | MachOObjectFile::exports(ArrayRef<uint8_t> Trie) { |
| 2193 | ExportEntry Start(Trie); |
Juergen Ributzka | 4d7f70d | 2014-12-19 02:31:01 +0000 | [diff] [blame] | 2194 | if (Trie.size() == 0) |
| 2195 | Start.moveToEnd(); |
| 2196 | else |
| 2197 | Start.moveToFirst(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2198 | |
| 2199 | ExportEntry Finish(Trie); |
| 2200 | Finish.moveToEnd(); |
| 2201 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2202 | return make_range(export_iterator(Start), export_iterator(Finish)); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | iterator_range<export_iterator> MachOObjectFile::exports() const { |
| 2206 | return exports(getDyldInfoExportsTrie()); |
| 2207 | } |
| 2208 | |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2209 | MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit) |
| 2210 | : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0), |
| 2211 | RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0), |
| 2212 | PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {} |
| 2213 | |
| 2214 | void MachORebaseEntry::moveToFirst() { |
| 2215 | Ptr = Opcodes.begin(); |
| 2216 | moveNext(); |
| 2217 | } |
| 2218 | |
| 2219 | void MachORebaseEntry::moveToEnd() { |
| 2220 | Ptr = Opcodes.end(); |
| 2221 | RemainingLoopCount = 0; |
| 2222 | Done = true; |
| 2223 | } |
| 2224 | |
| 2225 | void MachORebaseEntry::moveNext() { |
| 2226 | // If in the middle of some loop, move to next rebasing in loop. |
| 2227 | SegmentOffset += AdvanceAmount; |
| 2228 | if (RemainingLoopCount) { |
| 2229 | --RemainingLoopCount; |
| 2230 | return; |
| 2231 | } |
| 2232 | if (Ptr == Opcodes.end()) { |
| 2233 | Done = true; |
| 2234 | return; |
| 2235 | } |
| 2236 | bool More = true; |
| 2237 | while (More && !Malformed) { |
| 2238 | // Parse next opcode and set up next loop. |
| 2239 | uint8_t Byte = *Ptr++; |
| 2240 | uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK; |
| 2241 | uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK; |
| 2242 | switch (Opcode) { |
| 2243 | case MachO::REBASE_OPCODE_DONE: |
| 2244 | More = false; |
| 2245 | Done = true; |
| 2246 | moveToEnd(); |
| 2247 | DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n"); |
| 2248 | break; |
| 2249 | case MachO::REBASE_OPCODE_SET_TYPE_IMM: |
| 2250 | RebaseType = ImmValue; |
| 2251 | DEBUG_WITH_TYPE( |
| 2252 | "mach-o-rebase", |
| 2253 | llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: " |
| 2254 | << "RebaseType=" << (int) RebaseType << "\n"); |
| 2255 | break; |
| 2256 | case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 2257 | SegmentIndex = ImmValue; |
| 2258 | SegmentOffset = readULEB128(); |
| 2259 | DEBUG_WITH_TYPE( |
| 2260 | "mach-o-rebase", |
| 2261 | llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 2262 | << "SegmentIndex=" << SegmentIndex << ", " |
| 2263 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2264 | << "\n"); |
| 2265 | break; |
| 2266 | case MachO::REBASE_OPCODE_ADD_ADDR_ULEB: |
| 2267 | SegmentOffset += readULEB128(); |
| 2268 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2269 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: " |
| 2270 | << format("SegmentOffset=0x%06X", |
| 2271 | SegmentOffset) << "\n"); |
| 2272 | break; |
| 2273 | case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED: |
| 2274 | SegmentOffset += ImmValue * PointerSize; |
| 2275 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2276 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: " |
| 2277 | << format("SegmentOffset=0x%06X", |
| 2278 | SegmentOffset) << "\n"); |
| 2279 | break; |
| 2280 | case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES: |
| 2281 | AdvanceAmount = PointerSize; |
| 2282 | RemainingLoopCount = ImmValue - 1; |
| 2283 | DEBUG_WITH_TYPE( |
| 2284 | "mach-o-rebase", |
| 2285 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: " |
| 2286 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2287 | << ", AdvanceAmount=" << AdvanceAmount |
| 2288 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2289 | << "\n"); |
| 2290 | return; |
| 2291 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES: |
| 2292 | AdvanceAmount = PointerSize; |
| 2293 | RemainingLoopCount = readULEB128() - 1; |
| 2294 | DEBUG_WITH_TYPE( |
| 2295 | "mach-o-rebase", |
| 2296 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: " |
| 2297 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2298 | << ", AdvanceAmount=" << AdvanceAmount |
| 2299 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2300 | << "\n"); |
| 2301 | return; |
| 2302 | case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: |
| 2303 | AdvanceAmount = readULEB128() + PointerSize; |
| 2304 | RemainingLoopCount = 0; |
| 2305 | DEBUG_WITH_TYPE( |
| 2306 | "mach-o-rebase", |
| 2307 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: " |
| 2308 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2309 | << ", AdvanceAmount=" << AdvanceAmount |
| 2310 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2311 | << "\n"); |
| 2312 | return; |
| 2313 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: |
| 2314 | RemainingLoopCount = readULEB128() - 1; |
| 2315 | AdvanceAmount = readULEB128() + PointerSize; |
| 2316 | DEBUG_WITH_TYPE( |
| 2317 | "mach-o-rebase", |
| 2318 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: " |
| 2319 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2320 | << ", AdvanceAmount=" << AdvanceAmount |
| 2321 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2322 | << "\n"); |
| 2323 | return; |
| 2324 | default: |
| 2325 | Malformed = true; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | uint64_t MachORebaseEntry::readULEB128() { |
| 2331 | unsigned Count; |
| 2332 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2333 | Ptr += Count; |
| 2334 | if (Ptr > Opcodes.end()) { |
| 2335 | Ptr = Opcodes.end(); |
| 2336 | Malformed = true; |
| 2337 | } |
| 2338 | return Result; |
| 2339 | } |
| 2340 | |
| 2341 | uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; } |
| 2342 | |
| 2343 | uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; } |
| 2344 | |
| 2345 | StringRef MachORebaseEntry::typeName() const { |
| 2346 | switch (RebaseType) { |
| 2347 | case MachO::REBASE_TYPE_POINTER: |
| 2348 | return "pointer"; |
| 2349 | case MachO::REBASE_TYPE_TEXT_ABSOLUTE32: |
| 2350 | return "text abs32"; |
| 2351 | case MachO::REBASE_TYPE_TEXT_PCREL32: |
| 2352 | return "text rel32"; |
| 2353 | } |
| 2354 | return "unknown"; |
| 2355 | } |
| 2356 | |
| 2357 | bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const { |
| 2358 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
| 2359 | return (Ptr == Other.Ptr) && |
| 2360 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 2361 | (Done == Other.Done); |
| 2362 | } |
| 2363 | |
| 2364 | iterator_range<rebase_iterator> |
| 2365 | MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) { |
| 2366 | MachORebaseEntry Start(Opcodes, is64); |
| 2367 | Start.moveToFirst(); |
| 2368 | |
| 2369 | MachORebaseEntry Finish(Opcodes, is64); |
| 2370 | Finish.moveToEnd(); |
| 2371 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2372 | return make_range(rebase_iterator(Start), rebase_iterator(Finish)); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const { |
| 2376 | return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit()); |
| 2377 | } |
| 2378 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2379 | MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2380 | : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0), |
| 2381 | Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0), |
| 2382 | BindType(0), PointerSize(is64Bit ? 8 : 4), |
| 2383 | TableKind(BK), Malformed(false), Done(false) {} |
| 2384 | |
| 2385 | void MachOBindEntry::moveToFirst() { |
| 2386 | Ptr = Opcodes.begin(); |
| 2387 | moveNext(); |
| 2388 | } |
| 2389 | |
| 2390 | void MachOBindEntry::moveToEnd() { |
| 2391 | Ptr = Opcodes.end(); |
| 2392 | RemainingLoopCount = 0; |
| 2393 | Done = true; |
| 2394 | } |
| 2395 | |
| 2396 | void MachOBindEntry::moveNext() { |
| 2397 | // If in the middle of some loop, move to next binding in loop. |
| 2398 | SegmentOffset += AdvanceAmount; |
| 2399 | if (RemainingLoopCount) { |
| 2400 | --RemainingLoopCount; |
| 2401 | return; |
| 2402 | } |
| 2403 | if (Ptr == Opcodes.end()) { |
| 2404 | Done = true; |
| 2405 | return; |
| 2406 | } |
| 2407 | bool More = true; |
| 2408 | while (More && !Malformed) { |
| 2409 | // Parse next opcode and set up next loop. |
| 2410 | uint8_t Byte = *Ptr++; |
| 2411 | uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK; |
| 2412 | uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK; |
| 2413 | int8_t SignExtended; |
| 2414 | const uint8_t *SymStart; |
| 2415 | switch (Opcode) { |
| 2416 | case MachO::BIND_OPCODE_DONE: |
| 2417 | if (TableKind == Kind::Lazy) { |
| 2418 | // Lazying bindings have a DONE opcode between entries. Need to ignore |
| 2419 | // it to advance to next entry. But need not if this is last entry. |
| 2420 | bool NotLastEntry = false; |
| 2421 | for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) { |
| 2422 | if (*P) { |
| 2423 | NotLastEntry = true; |
| 2424 | } |
| 2425 | } |
| 2426 | if (NotLastEntry) |
| 2427 | break; |
| 2428 | } |
| 2429 | More = false; |
| 2430 | Done = true; |
| 2431 | moveToEnd(); |
| 2432 | DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n"); |
| 2433 | break; |
| 2434 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: |
| 2435 | Ordinal = ImmValue; |
| 2436 | DEBUG_WITH_TYPE( |
| 2437 | "mach-o-bind", |
| 2438 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: " |
| 2439 | << "Ordinal=" << Ordinal << "\n"); |
| 2440 | break; |
| 2441 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: |
| 2442 | Ordinal = readULEB128(); |
| 2443 | DEBUG_WITH_TYPE( |
| 2444 | "mach-o-bind", |
| 2445 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: " |
| 2446 | << "Ordinal=" << Ordinal << "\n"); |
| 2447 | break; |
| 2448 | case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: |
| 2449 | if (ImmValue) { |
| 2450 | SignExtended = MachO::BIND_OPCODE_MASK | ImmValue; |
| 2451 | Ordinal = SignExtended; |
| 2452 | } else |
| 2453 | Ordinal = 0; |
| 2454 | DEBUG_WITH_TYPE( |
| 2455 | "mach-o-bind", |
| 2456 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: " |
| 2457 | << "Ordinal=" << Ordinal << "\n"); |
| 2458 | break; |
| 2459 | case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: |
| 2460 | Flags = ImmValue; |
| 2461 | SymStart = Ptr; |
| 2462 | while (*Ptr) { |
| 2463 | ++Ptr; |
| 2464 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2465 | SymbolName = StringRef(reinterpret_cast<const char*>(SymStart), |
| 2466 | Ptr-SymStart); |
Nick Kledzik | a637536 | 2014-09-17 01:51:43 +0000 | [diff] [blame] | 2467 | ++Ptr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2468 | DEBUG_WITH_TYPE( |
| 2469 | "mach-o-bind", |
| 2470 | llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: " |
| 2471 | << "SymbolName=" << SymbolName << "\n"); |
| 2472 | if (TableKind == Kind::Weak) { |
| 2473 | if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) |
| 2474 | return; |
| 2475 | } |
| 2476 | break; |
| 2477 | case MachO::BIND_OPCODE_SET_TYPE_IMM: |
| 2478 | BindType = ImmValue; |
| 2479 | DEBUG_WITH_TYPE( |
| 2480 | "mach-o-bind", |
| 2481 | llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: " |
| 2482 | << "BindType=" << (int)BindType << "\n"); |
| 2483 | break; |
| 2484 | case MachO::BIND_OPCODE_SET_ADDEND_SLEB: |
| 2485 | Addend = readSLEB128(); |
| 2486 | if (TableKind == Kind::Lazy) |
| 2487 | Malformed = true; |
| 2488 | DEBUG_WITH_TYPE( |
| 2489 | "mach-o-bind", |
| 2490 | llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: " |
| 2491 | << "Addend=" << Addend << "\n"); |
| 2492 | break; |
| 2493 | case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 2494 | SegmentIndex = ImmValue; |
| 2495 | SegmentOffset = readULEB128(); |
| 2496 | DEBUG_WITH_TYPE( |
| 2497 | "mach-o-bind", |
| 2498 | llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 2499 | << "SegmentIndex=" << SegmentIndex << ", " |
| 2500 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2501 | << "\n"); |
| 2502 | break; |
| 2503 | case MachO::BIND_OPCODE_ADD_ADDR_ULEB: |
| 2504 | SegmentOffset += readULEB128(); |
| 2505 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2506 | llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: " |
| 2507 | << format("SegmentOffset=0x%06X", |
| 2508 | SegmentOffset) << "\n"); |
| 2509 | break; |
| 2510 | case MachO::BIND_OPCODE_DO_BIND: |
| 2511 | AdvanceAmount = PointerSize; |
| 2512 | RemainingLoopCount = 0; |
| 2513 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2514 | llvm::dbgs() << "BIND_OPCODE_DO_BIND: " |
| 2515 | << format("SegmentOffset=0x%06X", |
| 2516 | SegmentOffset) << "\n"); |
| 2517 | return; |
| 2518 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2519 | AdvanceAmount = readULEB128() + PointerSize; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2520 | RemainingLoopCount = 0; |
| 2521 | if (TableKind == Kind::Lazy) |
| 2522 | Malformed = true; |
| 2523 | DEBUG_WITH_TYPE( |
| 2524 | "mach-o-bind", |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2525 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: " |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2526 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2527 | << ", AdvanceAmount=" << AdvanceAmount |
| 2528 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2529 | << "\n"); |
| 2530 | return; |
| 2531 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2532 | AdvanceAmount = ImmValue * PointerSize + PointerSize; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2533 | RemainingLoopCount = 0; |
| 2534 | if (TableKind == Kind::Lazy) |
| 2535 | Malformed = true; |
| 2536 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2537 | llvm::dbgs() |
| 2538 | << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: " |
| 2539 | << format("SegmentOffset=0x%06X", |
| 2540 | SegmentOffset) << "\n"); |
| 2541 | return; |
| 2542 | case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: |
| 2543 | RemainingLoopCount = readULEB128() - 1; |
| 2544 | AdvanceAmount = readULEB128() + PointerSize; |
| 2545 | if (TableKind == Kind::Lazy) |
| 2546 | Malformed = true; |
| 2547 | DEBUG_WITH_TYPE( |
| 2548 | "mach-o-bind", |
| 2549 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: " |
| 2550 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2551 | << ", AdvanceAmount=" << AdvanceAmount |
| 2552 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2553 | << "\n"); |
| 2554 | return; |
| 2555 | default: |
| 2556 | Malformed = true; |
| 2557 | } |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | uint64_t MachOBindEntry::readULEB128() { |
| 2562 | unsigned Count; |
| 2563 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2564 | Ptr += Count; |
| 2565 | if (Ptr > Opcodes.end()) { |
| 2566 | Ptr = Opcodes.end(); |
| 2567 | Malformed = true; |
| 2568 | } |
| 2569 | return Result; |
| 2570 | } |
| 2571 | |
| 2572 | int64_t MachOBindEntry::readSLEB128() { |
| 2573 | unsigned Count; |
| 2574 | int64_t Result = decodeSLEB128(Ptr, &Count); |
| 2575 | Ptr += Count; |
| 2576 | if (Ptr > Opcodes.end()) { |
| 2577 | Ptr = Opcodes.end(); |
| 2578 | Malformed = true; |
| 2579 | } |
| 2580 | return Result; |
| 2581 | } |
| 2582 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2583 | uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; } |
| 2584 | |
| 2585 | uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; } |
| 2586 | |
| 2587 | StringRef MachOBindEntry::typeName() const { |
| 2588 | switch (BindType) { |
| 2589 | case MachO::BIND_TYPE_POINTER: |
| 2590 | return "pointer"; |
| 2591 | case MachO::BIND_TYPE_TEXT_ABSOLUTE32: |
| 2592 | return "text abs32"; |
| 2593 | case MachO::BIND_TYPE_TEXT_PCREL32: |
| 2594 | return "text rel32"; |
| 2595 | } |
| 2596 | return "unknown"; |
| 2597 | } |
| 2598 | |
| 2599 | StringRef MachOBindEntry::symbolName() const { return SymbolName; } |
| 2600 | |
| 2601 | int64_t MachOBindEntry::addend() const { return Addend; } |
| 2602 | |
| 2603 | uint32_t MachOBindEntry::flags() const { return Flags; } |
| 2604 | |
| 2605 | int MachOBindEntry::ordinal() const { return Ordinal; } |
| 2606 | |
| 2607 | bool MachOBindEntry::operator==(const MachOBindEntry &Other) const { |
| 2608 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
| 2609 | return (Ptr == Other.Ptr) && |
| 2610 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 2611 | (Done == Other.Done); |
| 2612 | } |
| 2613 | |
| 2614 | iterator_range<bind_iterator> |
| 2615 | MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64, |
| 2616 | MachOBindEntry::Kind BKind) { |
| 2617 | MachOBindEntry Start(Opcodes, is64, BKind); |
| 2618 | Start.moveToFirst(); |
| 2619 | |
| 2620 | MachOBindEntry Finish(Opcodes, is64, BKind); |
| 2621 | Finish.moveToEnd(); |
| 2622 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2623 | return make_range(bind_iterator(Start), bind_iterator(Finish)); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | iterator_range<bind_iterator> MachOObjectFile::bindTable() const { |
| 2627 | return bindTable(getDyldInfoBindOpcodes(), is64Bit(), |
| 2628 | MachOBindEntry::Kind::Regular); |
| 2629 | } |
| 2630 | |
| 2631 | iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const { |
| 2632 | return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(), |
| 2633 | MachOBindEntry::Kind::Lazy); |
| 2634 | } |
| 2635 | |
| 2636 | iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const { |
| 2637 | return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(), |
| 2638 | MachOBindEntry::Kind::Weak); |
| 2639 | } |
| 2640 | |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 2641 | MachOObjectFile::load_command_iterator |
| 2642 | MachOObjectFile::begin_load_commands() const { |
| 2643 | return LoadCommands.begin(); |
| 2644 | } |
| 2645 | |
| 2646 | MachOObjectFile::load_command_iterator |
| 2647 | MachOObjectFile::end_load_commands() const { |
| 2648 | return LoadCommands.end(); |
| 2649 | } |
| 2650 | |
| 2651 | iterator_range<MachOObjectFile::load_command_iterator> |
| 2652 | MachOObjectFile::load_commands() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2653 | return make_range(begin_load_commands(), end_load_commands()); |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2656 | StringRef |
| 2657 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 2658 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 2659 | return parseSegmentOrSectionName(Raw.data()); |
| 2660 | } |
| 2661 | |
| 2662 | ArrayRef<char> |
| 2663 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 2664 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2665 | const section_base *Base = |
| 2666 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 2667 | return makeArrayRef(Base->sectname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | ArrayRef<char> |
| 2671 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 2672 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2673 | const section_base *Base = |
| 2674 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 2675 | return makeArrayRef(Base->segname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | bool |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2679 | MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2680 | const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2681 | if (getCPUType(this) == MachO::CPU_TYPE_X86_64) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2682 | return false; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2683 | return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2686 | unsigned MachOObjectFile::getPlainRelocationSymbolNum( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2687 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2688 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2689 | return RE.r_word1 & 0xffffff; |
| 2690 | return RE.r_word1 >> 8; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2693 | bool MachOObjectFile::getPlainRelocationExternal( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2694 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2695 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2696 | return (RE.r_word1 >> 27) & 1; |
| 2697 | return (RE.r_word1 >> 4) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2700 | bool MachOObjectFile::getScatteredRelocationScattered( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2701 | const MachO::any_relocation_info &RE) const { |
| 2702 | return RE.r_word0 >> 31; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2705 | uint32_t MachOObjectFile::getScatteredRelocationValue( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2706 | const MachO::any_relocation_info &RE) const { |
| 2707 | return RE.r_word1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 2710 | uint32_t MachOObjectFile::getScatteredRelocationType( |
| 2711 | const MachO::any_relocation_info &RE) const { |
| 2712 | return (RE.r_word0 >> 24) & 0xf; |
| 2713 | } |
| 2714 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2715 | unsigned MachOObjectFile::getAnyRelocationAddress( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2716 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2717 | if (isRelocationScattered(RE)) |
| 2718 | return getScatteredRelocationAddress(RE); |
| 2719 | return getPlainRelocationAddress(RE); |
| 2720 | } |
| 2721 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2722 | unsigned MachOObjectFile::getAnyRelocationPCRel( |
| 2723 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2724 | if (isRelocationScattered(RE)) |
| 2725 | return getScatteredRelocationPCRel(this, RE); |
| 2726 | return getPlainRelocationPCRel(this, RE); |
| 2727 | } |
| 2728 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2729 | unsigned MachOObjectFile::getAnyRelocationLength( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2730 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2731 | if (isRelocationScattered(RE)) |
| 2732 | return getScatteredRelocationLength(RE); |
| 2733 | return getPlainRelocationLength(this, RE); |
| 2734 | } |
| 2735 | |
| 2736 | unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2737 | MachOObjectFile::getAnyRelocationType( |
| 2738 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2739 | if (isRelocationScattered(RE)) |
| 2740 | return getScatteredRelocationType(RE); |
| 2741 | return getPlainRelocationType(this, RE); |
| 2742 | } |
| 2743 | |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2744 | SectionRef |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 2745 | MachOObjectFile::getAnyRelocationSection( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2746 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2747 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 2748 | return *section_end(); |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 2749 | unsigned SecNum = getPlainRelocationSymbolNum(RE); |
| 2750 | if (SecNum == MachO::R_ABS || SecNum > Sections.size()) |
| 2751 | return *section_end(); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2752 | DataRefImpl DRI; |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 2753 | DRI.d.a = SecNum - 1; |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2754 | return SectionRef(DRI, this); |
| 2755 | } |
| 2756 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2757 | MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 2758 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2759 | return getStruct<MachO::section>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2762 | MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 2763 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2764 | return getStruct<MachO::section_64>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2767 | MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2768 | unsigned Index) const { |
| 2769 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2770 | return getStruct<MachO::section>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2773 | MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 2774 | unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2775 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2776 | return getStruct<MachO::section_64>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2779 | MachO::nlist |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2780 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2781 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2782 | return getStruct<MachO::nlist>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2785 | MachO::nlist_64 |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2786 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2787 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2788 | return getStruct<MachO::nlist_64>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2791 | MachO::linkedit_data_command |
| 2792 | MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { |
| 2793 | return getStruct<MachO::linkedit_data_command>(this, L.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2796 | MachO::segment_command |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2797 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2798 | return getStruct<MachO::segment_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2801 | MachO::segment_command_64 |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2802 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2803 | return getStruct<MachO::segment_command_64>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 2806 | MachO::linker_option_command |
| 2807 | MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const { |
| 2808 | return getStruct<MachO::linker_option_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 2811 | MachO::version_min_command |
| 2812 | MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { |
| 2813 | return getStruct<MachO::version_min_command>(this, L.Ptr); |
| 2814 | } |
| 2815 | |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 2816 | MachO::dylib_command |
| 2817 | MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const { |
| 2818 | return getStruct<MachO::dylib_command>(this, L.Ptr); |
| 2819 | } |
| 2820 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2821 | MachO::dyld_info_command |
| 2822 | MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const { |
| 2823 | return getStruct<MachO::dyld_info_command>(this, L.Ptr); |
| 2824 | } |
| 2825 | |
| 2826 | MachO::dylinker_command |
| 2827 | MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const { |
| 2828 | return getStruct<MachO::dylinker_command>(this, L.Ptr); |
| 2829 | } |
| 2830 | |
| 2831 | MachO::uuid_command |
| 2832 | MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const { |
| 2833 | return getStruct<MachO::uuid_command>(this, L.Ptr); |
| 2834 | } |
| 2835 | |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 2836 | MachO::rpath_command |
| 2837 | MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const { |
| 2838 | return getStruct<MachO::rpath_command>(this, L.Ptr); |
| 2839 | } |
| 2840 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2841 | MachO::source_version_command |
| 2842 | MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const { |
| 2843 | return getStruct<MachO::source_version_command>(this, L.Ptr); |
| 2844 | } |
| 2845 | |
| 2846 | MachO::entry_point_command |
| 2847 | MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const { |
| 2848 | return getStruct<MachO::entry_point_command>(this, L.Ptr); |
| 2849 | } |
| 2850 | |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 2851 | MachO::encryption_info_command |
| 2852 | MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const { |
| 2853 | return getStruct<MachO::encryption_info_command>(this, L.Ptr); |
| 2854 | } |
| 2855 | |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 2856 | MachO::encryption_info_command_64 |
| 2857 | MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const { |
| 2858 | return getStruct<MachO::encryption_info_command_64>(this, L.Ptr); |
| 2859 | } |
| 2860 | |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 2861 | MachO::sub_framework_command |
| 2862 | MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const { |
| 2863 | return getStruct<MachO::sub_framework_command>(this, L.Ptr); |
| 2864 | } |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 2865 | |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 2866 | MachO::sub_umbrella_command |
| 2867 | MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const { |
| 2868 | return getStruct<MachO::sub_umbrella_command>(this, L.Ptr); |
| 2869 | } |
| 2870 | |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 2871 | MachO::sub_library_command |
| 2872 | MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const { |
| 2873 | return getStruct<MachO::sub_library_command>(this, L.Ptr); |
| 2874 | } |
| 2875 | |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 2876 | MachO::sub_client_command |
| 2877 | MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const { |
| 2878 | return getStruct<MachO::sub_client_command>(this, L.Ptr); |
| 2879 | } |
| 2880 | |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 2881 | MachO::routines_command |
| 2882 | MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const { |
| 2883 | return getStruct<MachO::routines_command>(this, L.Ptr); |
| 2884 | } |
| 2885 | |
| 2886 | MachO::routines_command_64 |
| 2887 | MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const { |
| 2888 | return getStruct<MachO::routines_command_64>(this, L.Ptr); |
| 2889 | } |
| 2890 | |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 2891 | MachO::thread_command |
| 2892 | MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const { |
| 2893 | return getStruct<MachO::thread_command>(this, L.Ptr); |
| 2894 | } |
| 2895 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2896 | MachO::any_relocation_info |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2897 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 2898 | DataRefImpl Sec; |
| 2899 | Sec.d.a = Rel.d.a; |
| 2900 | uint32_t Offset; |
| 2901 | if (is64Bit()) { |
| 2902 | MachO::section_64 Sect = getSection64(Sec); |
| 2903 | Offset = Sect.reloff; |
| 2904 | } else { |
| 2905 | MachO::section Sect = getSection(Sec); |
| 2906 | Offset = Sect.reloff; |
| 2907 | } |
| 2908 | |
| 2909 | auto P = reinterpret_cast<const MachO::any_relocation_info *>( |
| 2910 | getPtr(this, Offset)) + Rel.d.b; |
| 2911 | return getStruct<MachO::any_relocation_info>( |
| 2912 | this, reinterpret_cast<const char *>(P)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2915 | MachO::data_in_code_entry |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2916 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 2917 | const char *P = reinterpret_cast<const char *>(Rel.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2918 | return getStruct<MachO::data_in_code_entry>(this, P); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 2921 | const MachO::mach_header &MachOObjectFile::getHeader() const { |
Alexey Samsonov | fa5edc5 | 2015-06-04 22:49:55 +0000 | [diff] [blame] | 2922 | return Header; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 2925 | const MachO::mach_header_64 &MachOObjectFile::getHeader64() const { |
| 2926 | assert(is64Bit()); |
| 2927 | return Header64; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2930 | uint32_t MachOObjectFile::getIndirectSymbolTableEntry( |
| 2931 | const MachO::dysymtab_command &DLC, |
| 2932 | unsigned Index) const { |
| 2933 | uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); |
| 2934 | return getStruct<uint32_t>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2937 | MachO::data_in_code_entry |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2938 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 2939 | unsigned Index) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2940 | uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); |
| 2941 | return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2944 | MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2945 | if (SymtabLoadCmd) |
| 2946 | return getStruct<MachO::symtab_command>(this, SymtabLoadCmd); |
| 2947 | |
| 2948 | // If there is no SymtabLoadCmd return a load command with zero'ed fields. |
| 2949 | MachO::symtab_command Cmd; |
| 2950 | Cmd.cmd = MachO::LC_SYMTAB; |
| 2951 | Cmd.cmdsize = sizeof(MachO::symtab_command); |
| 2952 | Cmd.symoff = 0; |
| 2953 | Cmd.nsyms = 0; |
| 2954 | Cmd.stroff = 0; |
| 2955 | Cmd.strsize = 0; |
| 2956 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2959 | MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2960 | if (DysymtabLoadCmd) |
| 2961 | return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); |
| 2962 | |
| 2963 | // If there is no DysymtabLoadCmd return a load command with zero'ed fields. |
| 2964 | MachO::dysymtab_command Cmd; |
| 2965 | Cmd.cmd = MachO::LC_DYSYMTAB; |
| 2966 | Cmd.cmdsize = sizeof(MachO::dysymtab_command); |
| 2967 | Cmd.ilocalsym = 0; |
| 2968 | Cmd.nlocalsym = 0; |
| 2969 | Cmd.iextdefsym = 0; |
| 2970 | Cmd.nextdefsym = 0; |
| 2971 | Cmd.iundefsym = 0; |
| 2972 | Cmd.nundefsym = 0; |
| 2973 | Cmd.tocoff = 0; |
| 2974 | Cmd.ntoc = 0; |
| 2975 | Cmd.modtaboff = 0; |
| 2976 | Cmd.nmodtab = 0; |
| 2977 | Cmd.extrefsymoff = 0; |
| 2978 | Cmd.nextrefsyms = 0; |
| 2979 | Cmd.indirectsymoff = 0; |
| 2980 | Cmd.nindirectsyms = 0; |
| 2981 | Cmd.extreloff = 0; |
| 2982 | Cmd.nextrel = 0; |
| 2983 | Cmd.locreloff = 0; |
| 2984 | Cmd.nlocrel = 0; |
| 2985 | return Cmd; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2988 | MachO::linkedit_data_command |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2989 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 2990 | if (DataInCodeLoadCmd) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2991 | return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2992 | |
| 2993 | // 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] | 2994 | MachO::linkedit_data_command Cmd; |
| 2995 | Cmd.cmd = MachO::LC_DATA_IN_CODE; |
| 2996 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 2997 | Cmd.dataoff = 0; |
| 2998 | Cmd.datasize = 0; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2999 | return Cmd; |
| 3000 | } |
| 3001 | |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 3002 | MachO::linkedit_data_command |
| 3003 | MachOObjectFile::getLinkOptHintsLoadCommand() const { |
| 3004 | if (LinkOptHintsLoadCmd) |
| 3005 | return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd); |
| 3006 | |
| 3007 | // If there is no LinkOptHintsLoadCmd return a load command with zero'ed |
| 3008 | // fields. |
| 3009 | MachO::linkedit_data_command Cmd; |
| 3010 | Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT; |
| 3011 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 3012 | Cmd.dataoff = 0; |
| 3013 | Cmd.datasize = 0; |
| 3014 | return Cmd; |
| 3015 | } |
| 3016 | |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3017 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 3018 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3019 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3020 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 3021 | MachO::dyld_info_command DyldInfo = |
| 3022 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 3023 | const uint8_t *Ptr = |
| 3024 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.rebase_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3025 | return makeArrayRef(Ptr, DyldInfo.rebase_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 3029 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3030 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3031 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 3032 | MachO::dyld_info_command DyldInfo = |
| 3033 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 3034 | const uint8_t *Ptr = |
| 3035 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3036 | return makeArrayRef(Ptr, DyldInfo.bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 3040 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3041 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3042 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 3043 | MachO::dyld_info_command DyldInfo = |
| 3044 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 3045 | const uint8_t *Ptr = |
| 3046 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.weak_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3047 | return makeArrayRef(Ptr, DyldInfo.weak_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 3051 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3052 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3053 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 3054 | MachO::dyld_info_command DyldInfo = |
| 3055 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 3056 | const uint8_t *Ptr = |
| 3057 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.lazy_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3058 | return makeArrayRef(Ptr, DyldInfo.lazy_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 3062 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3063 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3064 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 3065 | MachO::dyld_info_command DyldInfo = |
| 3066 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 3067 | const uint8_t *Ptr = |
| 3068 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.export_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3069 | return makeArrayRef(Ptr, DyldInfo.export_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 3072 | ArrayRef<uint8_t> MachOObjectFile::getUuid() const { |
| 3073 | if (!UuidLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3074 | return None; |
Benjamin Kramer | 014601d | 2014-10-24 15:52:05 +0000 | [diff] [blame] | 3075 | // Returning a pointer is fine as uuid doesn't need endian swapping. |
| 3076 | const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 3077 | return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16); |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 3078 | } |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 3079 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3080 | StringRef MachOObjectFile::getStringTableData() const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 3081 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 3082 | return getData().substr(S.stroff, S.strsize); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3085 | bool MachOObjectFile::is64Bit() const { |
| 3086 | return getType() == getMachOType(false, true) || |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 3087 | getType() == getMachOType(true, true); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 3091 | SmallVectorImpl<uint64_t> &Out) const { |
| 3092 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 3093 | |
| 3094 | uint32_t offset = Index; |
| 3095 | uint64_t data = 0; |
| 3096 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 3097 | data += delta; |
| 3098 | Out.push_back(data); |
| 3099 | } |
| 3100 | } |
| 3101 | |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 3102 | bool MachOObjectFile::isRelocatableObject() const { |
| 3103 | return getHeader().filetype == MachO::MH_OBJECT; |
| 3104 | } |
| 3105 | |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 3106 | Expected<std::unique_ptr<MachOObjectFile>> |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 3107 | ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) { |
| 3108 | StringRef Magic = Buffer.getBuffer().slice(0, 4); |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 3109 | if (Magic == "\xFE\xED\xFA\xCE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 3110 | return MachOObjectFile::create(Buffer, false, false); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 3111 | if (Magic == "\xCE\xFA\xED\xFE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 3112 | return MachOObjectFile::create(Buffer, true, false); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 3113 | if (Magic == "\xFE\xED\xFA\xCF") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 3114 | return MachOObjectFile::create(Buffer, false, true); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 3115 | if (Magic == "\xCF\xFA\xED\xFE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 3116 | return MachOObjectFile::create(Buffer, true, true); |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 3117 | return make_error<GenericBinaryError>("Unrecognized MachO magic number", |
Justin Bogner | 2a42da9 | 2016-05-05 23:59:57 +0000 | [diff] [blame] | 3118 | object_error::invalid_file_type); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 3119 | } |