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 | |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 657 | Expected<std::unique_ptr<MachOObjectFile>> |
| 658 | MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian, |
| 659 | bool Is64Bits) { |
Lang Hames | d1af8fc | 2016-03-25 23:54:32 +0000 | [diff] [blame] | 660 | Error Err; |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 661 | std::unique_ptr<MachOObjectFile> Obj( |
| 662 | new MachOObjectFile(std::move(Object), IsLittleEndian, |
| 663 | Is64Bits, Err)); |
| 664 | if (Err) |
| 665 | return std::move(Err); |
| 666 | return std::move(Obj); |
| 667 | } |
| 668 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 669 | MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 670 | bool Is64bits, Error &Err) |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 671 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 672 | SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr), |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 673 | DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr), |
| 674 | DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr), |
| 675 | HasPageZeroSegment(false) { |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 676 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 677 | uint64_t SizeOfHeaders; |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 678 | if (is64Bit()) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 679 | parseHeader(this, Header64, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 680 | SizeOfHeaders = sizeof(MachO::mach_header_64); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 681 | } else { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 682 | parseHeader(this, Header, Err); |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 683 | SizeOfHeaders = sizeof(MachO::mach_header); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 684 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 685 | if (Err) |
Alexey Samsonov | 9f33663 | 2015-06-04 19:45:22 +0000 | [diff] [blame] | 686 | return; |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 687 | SizeOfHeaders += getHeader().sizeofcmds; |
| 688 | if (getData().data() + SizeOfHeaders > getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 689 | Err = malformedError("load commands extend past the end of the file"); |
Kevin Enderby | 8702574 | 2016-04-13 21:17:58 +0000 | [diff] [blame] | 690 | return; |
| 691 | } |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 692 | |
| 693 | uint32_t LoadCommandCount = getHeader().ncmds; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 694 | LoadCommandInfo Load; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 695 | if (LoadCommandCount != 0) { |
| 696 | if (auto LoadOrErr = getFirstLoadCommandInfo(this)) |
| 697 | Load = *LoadOrErr; |
| 698 | else { |
| 699 | Err = LoadOrErr.takeError(); |
| 700 | return; |
| 701 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 702 | } |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 703 | |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 704 | const char *DyldIdLoadCmd = nullptr; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 705 | const char *FuncStartsLoadCmd = nullptr; |
| 706 | const char *SplitInfoLoadCmd = nullptr; |
| 707 | const char *CodeSignDrsLoadCmd = nullptr; |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 708 | for (unsigned I = 0; I < LoadCommandCount; ++I) { |
Kevin Enderby | 1851a82 | 2016-07-07 22:11:42 +0000 | [diff] [blame] | 709 | if (is64Bit()) { |
| 710 | if (Load.C.cmdsize % 8 != 0) { |
| 711 | // We have a hack here to allow 64-bit Mach-O core files to have |
| 712 | // LC_THREAD commands that are only a multiple of 4 and not 8 to be |
| 713 | // allowed since the macOS kernel produces them. |
| 714 | if (getHeader().filetype != MachO::MH_CORE || |
| 715 | Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) { |
| 716 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 717 | "multiple of 8"); |
| 718 | return; |
| 719 | } |
| 720 | } |
| 721 | } else { |
| 722 | if (Load.C.cmdsize % 4 != 0) { |
| 723 | Err = malformedError("load command " + Twine(I) + " cmdsize not a " |
| 724 | "multiple of 4"); |
| 725 | return; |
| 726 | } |
| 727 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 728 | LoadCommands.push_back(Load); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 729 | if (Load.C.cmd == MachO::LC_SYMTAB) { |
Kevin Enderby | 0e52c92 | 2016-08-26 19:34:07 +0000 | [diff] [blame] | 730 | if ((Err = checkSymtabCommand(this, Load, I, &SymtabLoadCmd))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 731 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 732 | } else if (Load.C.cmd == MachO::LC_DYSYMTAB) { |
Kevin Enderby | dcbc504 | 2016-08-30 21:28:30 +0000 | [diff] [blame] | 733 | if ((Err = checkDysymtabCommand(this, Load, I, &DysymtabLoadCmd))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 734 | return; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 735 | } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) { |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 736 | if ((Err = checkLinkeditDataCommand(this, Load, I, &DataInCodeLoadCmd, |
| 737 | "LC_DATA_IN_CODE"))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 738 | return; |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 739 | } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
Kevin Enderby | 9d0c945 | 2016-08-31 17:57:46 +0000 | [diff] [blame] | 740 | if ((Err = checkLinkeditDataCommand(this, Load, I, &LinkOptHintsLoadCmd, |
| 741 | "LC_LINKER_OPTIMIZATION_HINT"))) |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 742 | return; |
Kevin Enderby | 90986e6 | 2016-09-26 21:11:03 +0000 | [diff] [blame] | 743 | } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) { |
| 744 | if ((Err = checkLinkeditDataCommand(this, Load, I, &FuncStartsLoadCmd, |
| 745 | "LC_FUNCTION_STARTS"))) |
| 746 | return; |
| 747 | } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) { |
| 748 | if ((Err = checkLinkeditDataCommand(this, Load, I, &SplitInfoLoadCmd, |
| 749 | "LC_SEGMENT_SPLIT_INFO"))) |
| 750 | return; |
| 751 | } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) { |
| 752 | if ((Err = checkLinkeditDataCommand(this, Load, I, &CodeSignDrsLoadCmd, |
| 753 | "LC_DYLIB_CODE_SIGN_DRS"))) |
| 754 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 755 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO) { |
| 756 | if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd, |
| 757 | "LC_DYLD_INFO"))) |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 758 | return; |
Kevin Enderby | f76b56c | 2016-09-13 21:42:28 +0000 | [diff] [blame] | 759 | } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
| 760 | if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd, |
| 761 | "LC_DYLD_INFO_ONLY"))) |
| 762 | return; |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 763 | } else if (Load.C.cmd == MachO::LC_UUID) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 764 | if (Load.C.cmdsize != sizeof(MachO::uuid_command)) { |
| 765 | Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect " |
| 766 | "cmdsize"); |
| 767 | return; |
| 768 | } |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 769 | if (UuidLoadCmd) { |
Kevin Enderby | e71e13c | 2016-09-21 20:03:09 +0000 | [diff] [blame] | 770 | Err = malformedError("more than one LC_UUID command"); |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 771 | return; |
| 772 | } |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 773 | UuidLoadCmd = Load.Ptr; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 774 | } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 775 | if ((Err = parseSegmentLoadCommand<MachO::segment_command_64, |
| 776 | MachO::section_64>( |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 777 | this, Load, Sections, HasPageZeroSegment, I, |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 778 | "LC_SEGMENT_64", SizeOfHeaders))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 779 | return; |
Alexey Samsonov | e1a76ab | 2015-06-04 22:08:37 +0000 | [diff] [blame] | 780 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
Kevin Enderby | c614d28 | 2016-08-12 20:10:25 +0000 | [diff] [blame] | 781 | if ((Err = parseSegmentLoadCommand<MachO::segment_command, |
| 782 | MachO::section>( |
| 783 | this, Load, Sections, HasPageZeroSegment, I, |
| 784 | "LC_SEGMENT", SizeOfHeaders))) |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 785 | return; |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 786 | } else if (Load.C.cmd == MachO::LC_ID_DYLIB) { |
| 787 | if ((Err = checkDylibIdCommand(this, Load, I, &DyldIdLoadCmd))) |
| 788 | return; |
| 789 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) { |
| 790 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_DYLIB"))) |
| 791 | return; |
| 792 | Libraries.push_back(Load.Ptr); |
| 793 | } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) { |
| 794 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_WEAK_DYLIB"))) |
| 795 | return; |
| 796 | Libraries.push_back(Load.Ptr); |
| 797 | } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) { |
| 798 | if ((Err = checkDylibCommand(this, Load, I, "LC_LAZY_LOAD_DYLIB"))) |
| 799 | return; |
| 800 | Libraries.push_back(Load.Ptr); |
| 801 | } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) { |
| 802 | if ((Err = checkDylibCommand(this, Load, I, "LC_REEXPORT_DYLIB"))) |
| 803 | return; |
| 804 | Libraries.push_back(Load.Ptr); |
| 805 | } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
| 806 | if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_UPWARD_DYLIB"))) |
| 807 | return; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 808 | Libraries.push_back(Load.Ptr); |
Kevin Enderby | 3e490ef | 2016-09-27 23:24:13 +0000 | [diff] [blame] | 809 | } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) { |
| 810 | if ((Err = checkDyldCommand(this, Load, I, "LC_ID_DYLINKER"))) |
| 811 | return; |
| 812 | } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) { |
| 813 | if ((Err = checkDyldCommand(this, Load, I, "LC_LOAD_DYLINKER"))) |
| 814 | return; |
| 815 | } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
| 816 | if ((Err = checkDyldCommand(this, Load, I, "LC_DYLD_ENVIRONMENT"))) |
| 817 | return; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 818 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 819 | if (I < LoadCommandCount - 1) { |
Kevin Enderby | 368e714 | 2016-05-03 17:16:08 +0000 | [diff] [blame] | 820 | if (auto LoadOrErr = getNextLoadCommandInfo(this, I, Load)) |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 821 | Load = *LoadOrErr; |
| 822 | else { |
| 823 | Err = LoadOrErr.takeError(); |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 824 | return; |
| 825 | } |
Alexey Samsonov | de5a94a | 2015-06-04 19:57:46 +0000 | [diff] [blame] | 826 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 827 | } |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 828 | if (!SymtabLoadCmd) { |
| 829 | if (DysymtabLoadCmd) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 830 | Err = malformedError("contains LC_DYSYMTAB load command without a " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 831 | "LC_SYMTAB load command"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | } else if (DysymtabLoadCmd) { |
| 835 | MachO::symtab_command Symtab = |
| 836 | getStruct<MachO::symtab_command>(this, SymtabLoadCmd); |
| 837 | MachO::dysymtab_command Dysymtab = |
| 838 | getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); |
| 839 | if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 840 | Err = malformedError("ilocalsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 841 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 844 | uint64_t BigSize = Dysymtab.ilocalsym; |
| 845 | BigSize += Dysymtab.nlocalsym; |
| 846 | if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 847 | Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 848 | "command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 849 | return; |
| 850 | } |
| 851 | if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 852 | Err = malformedError("nextdefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 853 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 854 | return; |
| 855 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 856 | BigSize = Dysymtab.iextdefsym; |
| 857 | BigSize += Dysymtab.nextdefsym; |
| 858 | if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 859 | Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 860 | "load command extends past the end of the symbol " |
| 861 | "table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 865 | Err = malformedError("nundefsym in LC_DYSYMTAB load command " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 866 | "extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 867 | return; |
| 868 | } |
Kevin Enderby | 5e55d17 | 2016-04-21 20:29:49 +0000 | [diff] [blame] | 869 | BigSize = Dysymtab.iundefsym; |
| 870 | BigSize += Dysymtab.nundefsym; |
| 871 | if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 872 | Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load " |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 873 | " command extends past the end of the symbol table"); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | } |
Kevin Enderby | fc0929a | 2016-09-20 20:14:14 +0000 | [diff] [blame] | 877 | if ((getHeader().filetype == MachO::MH_DYLIB || |
| 878 | getHeader().filetype == MachO::MH_DYLIB_STUB) && |
| 879 | DyldIdLoadCmd == nullptr) { |
| 880 | Err = malformedError("no LC_ID_DYLIB load command in dynamic library " |
| 881 | "filetype"); |
| 882 | return; |
| 883 | } |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 884 | assert(LoadCommands.size() == LoadCommandCount); |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 885 | |
| 886 | Err = Error::success(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 889 | void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 890 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 891 | sizeof(MachO::nlist_64) : |
| 892 | sizeof(MachO::nlist); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 893 | Symb.p += SymbolTableEntrySize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 896 | Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 897 | StringRef StringTable = getStringTableData(); |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 898 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 899 | const char *Start = &StringTable.data()[Entry.n_strx]; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 900 | if (Start < getData().begin() || Start >= getData().end()) { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 901 | return malformedError("bad string index: " + Twine(Entry.n_strx) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 902 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 903 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 904 | return StringRef(Start); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 907 | unsigned MachOObjectFile::getSectionType(SectionRef Sec) const { |
| 908 | DataRefImpl DRI = Sec.getRawDataRefImpl(); |
| 909 | uint32_t Flags = getSectionFlags(this, DRI); |
| 910 | return Flags & MachO::SECTION_TYPE; |
| 911 | } |
| 912 | |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 913 | uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const { |
| 914 | if (is64Bit()) { |
| 915 | MachO::nlist_64 Entry = getSymbol64TableEntry(Sym); |
| 916 | return Entry.n_value; |
| 917 | } |
| 918 | MachO::nlist Entry = getSymbolTableEntry(Sym); |
| 919 | return Entry.n_value; |
| 920 | } |
| 921 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 922 | // getIndirectName() returns the name of the alias'ed symbol who's string table |
| 923 | // index is in the n_value field. |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 924 | std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, |
| 925 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 926 | StringRef StringTable = getStringTableData(); |
Rafael Espindola | 5912892 | 2015-06-24 18:14:41 +0000 | [diff] [blame] | 927 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
| 928 | if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR) |
| 929 | return object_error::parse_failed; |
| 930 | uint64_t NValue = getNValue(Symb); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 931 | if (NValue >= StringTable.size()) |
| 932 | return object_error::parse_failed; |
| 933 | const char *Start = &StringTable.data()[NValue]; |
| 934 | Res = StringRef(Start); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 935 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 938 | uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const { |
Rafael Espindola | 7e7be92 | 2015-07-07 15:05:09 +0000 | [diff] [blame] | 939 | return getNValue(Sym); |
Rafael Espindola | 991af66 | 2015-06-24 19:11:10 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 942 | Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const { |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 943 | return getSymbolValue(Sym); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 946 | uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const { |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 947 | uint32_t flags = getSymbolFlags(DRI); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 948 | if (flags & SymbolRef::SF_Common) { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 949 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 950 | return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc); |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 951 | } |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 952 | return 0; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 955 | uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 956 | return getNValue(DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 959 | Expected<SymbolRef::Type> |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 960 | MachOObjectFile::getSymbolType(DataRefImpl Symb) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 961 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 962 | uint8_t n_type = Entry.n_type; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 963 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 964 | // If this is a STAB debugging symbol, we can do nothing more. |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 965 | if (n_type & MachO::N_STAB) |
| 966 | return SymbolRef::ST_Debug; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 967 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 968 | switch (n_type & MachO::N_TYPE) { |
| 969 | case MachO::N_UNDF : |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 970 | return SymbolRef::ST_Unknown; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 971 | case MachO::N_SECT : |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 972 | Expected<section_iterator> SecOrError = getSymbolSection(Symb); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 973 | if (!SecOrError) |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 974 | return SecOrError.takeError(); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 975 | section_iterator Sec = *SecOrError; |
Kuba Brecka | de83322 | 2015-11-12 09:40:29 +0000 | [diff] [blame] | 976 | if (Sec->isData() || Sec->isBSS()) |
| 977 | return SymbolRef::ST_Data; |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 978 | return SymbolRef::ST_Function; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 979 | } |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 980 | return SymbolRef::ST_Other; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 983 | uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 984 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 985 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 986 | uint8_t MachOType = Entry.n_type; |
| 987 | uint16_t MachOFlags = Entry.n_desc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 988 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 989 | uint32_t Result = SymbolRef::SF_None; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 990 | |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 991 | if ((MachOType & MachO::N_TYPE) == MachO::N_INDR) |
| 992 | Result |= SymbolRef::SF_Indirect; |
| 993 | |
Rafael Espindola | a135632 | 2013-11-02 05:03:24 +0000 | [diff] [blame] | 994 | if (MachOType & MachO::N_STAB) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 995 | Result |= SymbolRef::SF_FormatSpecific; |
| 996 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 997 | if (MachOType & MachO::N_EXT) { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 998 | Result |= SymbolRef::SF_Global; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 999 | if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) { |
Rafael Espindola | 05cbccc | 2015-07-07 13:58:32 +0000 | [diff] [blame] | 1000 | if (getNValue(DRI)) |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1001 | Result |= SymbolRef::SF_Common; |
Rafael Espindola | d824772 | 2015-07-07 14:26:39 +0000 | [diff] [blame] | 1002 | else |
| 1003 | Result |= SymbolRef::SF_Undefined; |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 1004 | } |
Lang Hames | 7e0692b | 2015-01-15 22:33:30 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (!(MachOType & MachO::N_PEXT)) |
| 1007 | Result |= SymbolRef::SF_Exported; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1010 | if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1011 | Result |= SymbolRef::SF_Weak; |
| 1012 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1013 | if (MachOFlags & (MachO::N_ARM_THUMB_DEF)) |
| 1014 | Result |= SymbolRef::SF_Thumb; |
| 1015 | |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1016 | if ((MachOType & MachO::N_TYPE) == MachO::N_ABS) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1017 | Result |= SymbolRef::SF_Absolute; |
| 1018 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 1019 | return Result; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1022 | Expected<section_iterator> |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1023 | MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { |
Artyom Skrobov | 78d5daf | 2014-07-18 09:26:16 +0000 | [diff] [blame] | 1024 | MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1025 | uint8_t index = Entry.n_sect; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1026 | |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1027 | if (index == 0) |
| 1028 | return section_end(); |
| 1029 | DataRefImpl DRI; |
| 1030 | DRI.d.a = index - 1; |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1031 | if (DRI.d.a >= Sections.size()){ |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 1032 | return malformedError("bad section index: " + Twine((int)index) + |
Kevin Enderby | 8913496 | 2016-05-05 23:41:05 +0000 | [diff] [blame] | 1033 | " for symbol at index " + Twine(getSymbolIndex(Symb))); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1034 | } |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1035 | return section_iterator(SectionRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1038 | unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const { |
| 1039 | MachO::nlist_base Entry = |
| 1040 | getSymbolTableEntryBase(this, Sym.getRawDataRefImpl()); |
| 1041 | return Entry.n_sect - 1; |
| 1042 | } |
| 1043 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1044 | void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1045 | Sec.d.a++; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1048 | std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec, |
| 1049 | StringRef &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1050 | ArrayRef<char> Raw = getSectionRawName(Sec); |
| 1051 | Result = parseSegmentOrSectionName(Raw.data()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1052 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1055 | uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const { |
| 1056 | if (is64Bit()) |
| 1057 | return getSection64(Sec).addr; |
| 1058 | return getSection(Sec).addr; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1061 | uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const { |
Kevin Enderby | 46e642f | 2015-10-08 22:50:55 +0000 | [diff] [blame] | 1062 | // In the case if a malformed Mach-O file where the section offset is past |
| 1063 | // the end of the file or some part of the section size is past the end of |
| 1064 | // the file return a size of zero or a size that covers the rest of the file |
| 1065 | // but does not extend past the end of the file. |
| 1066 | uint32_t SectOffset, SectType; |
| 1067 | uint64_t SectSize; |
| 1068 | |
| 1069 | if (is64Bit()) { |
| 1070 | MachO::section_64 Sect = getSection64(Sec); |
| 1071 | SectOffset = Sect.offset; |
| 1072 | SectSize = Sect.size; |
| 1073 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1074 | } else { |
| 1075 | MachO::section Sect = getSection(Sec); |
| 1076 | SectOffset = Sect.offset; |
| 1077 | SectSize = Sect.size; |
| 1078 | SectType = Sect.flags & MachO::SECTION_TYPE; |
| 1079 | } |
| 1080 | if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL) |
| 1081 | return SectSize; |
| 1082 | uint64_t FileSize = getData().size(); |
| 1083 | if (SectOffset > FileSize) |
| 1084 | return 0; |
| 1085 | if (FileSize - SectOffset < SectSize) |
| 1086 | return FileSize - SectOffset; |
| 1087 | return SectSize; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1090 | std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec, |
| 1091 | StringRef &Res) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1092 | uint32_t Offset; |
| 1093 | uint64_t Size; |
| 1094 | |
| 1095 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1096 | MachO::section_64 Sect = getSection64(Sec); |
| 1097 | Offset = Sect.offset; |
| 1098 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1099 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1100 | MachO::section Sect = getSection(Sec); |
| 1101 | Offset = Sect.offset; |
| 1102 | Size = Sect.size; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | Res = this->getData().substr(Offset, Size); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1106 | return std::error_code(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1109 | uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1110 | uint32_t Align; |
| 1111 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1112 | MachO::section_64 Sect = getSection64(Sec); |
| 1113 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1114 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1115 | MachO::section Sect = getSection(Sec); |
| 1116 | Align = Sect.align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1119 | return uint64_t(1) << Align; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
George Rimar | 401e4e5 | 2016-05-24 12:48:46 +0000 | [diff] [blame] | 1122 | bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1126 | bool MachOObjectFile::isSectionText(DataRefImpl Sec) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1127 | uint32_t Flags = getSectionFlags(this, Sec); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1128 | return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1131 | bool MachOObjectFile::isSectionData(DataRefImpl Sec) const { |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1132 | uint32_t Flags = getSectionFlags(this, Sec); |
| 1133 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1134 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1135 | !(SectionType == MachO::S_ZEROFILL || |
| 1136 | SectionType == MachO::S_GB_ZEROFILL); |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1139 | bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const { |
Kevin Enderby | 403258f | 2014-05-19 20:36:02 +0000 | [diff] [blame] | 1140 | uint32_t Flags = getSectionFlags(this, Sec); |
| 1141 | unsigned SectionType = Flags & MachO::SECTION_TYPE; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1142 | return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) && |
| 1143 | (SectionType == MachO::S_ZEROFILL || |
| 1144 | SectionType == MachO::S_GB_ZEROFILL); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 1147 | unsigned MachOObjectFile::getSectionID(SectionRef Sec) const { |
| 1148 | return Sec.getRawDataRefImpl().d.a; |
| 1149 | } |
| 1150 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1151 | bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const { |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1152 | // FIXME: Unimplemented. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1153 | return false; |
Rafael Espindola | c2413f5 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Steven Wu | f2fe014 | 2016-02-29 19:40:10 +0000 | [diff] [blame] | 1156 | bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const { |
| 1157 | StringRef SegmentName = getSectionFinalSegmentName(Sec); |
| 1158 | StringRef SectName; |
| 1159 | if (!getSectionName(Sec, SectName)) |
| 1160 | return (SegmentName == "__LLVM" && SectName == "__bitcode"); |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1164 | relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1165 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1166 | Ret.d.a = Sec.d.a; |
| 1167 | Ret.d.b = 0; |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1168 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1169 | } |
Rafael Espindola | c0406e1 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 1170 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1171 | relocation_iterator |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1172 | MachOObjectFile::section_rel_end(DataRefImpl Sec) const { |
Rafael Espindola | 04d3f49 | 2013-04-25 12:45:46 +0000 | [diff] [blame] | 1173 | uint32_t Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1174 | if (is64Bit()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1175 | MachO::section_64 Sect = getSection64(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1176 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1177 | } else { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1178 | MachO::section Sect = getSection(Sec); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1179 | Num = Sect.nreloc; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1180 | } |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1181 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1182 | DataRefImpl Ret; |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1183 | Ret.d.a = Sec.d.a; |
| 1184 | Ret.d.b = Num; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1185 | return relocation_iterator(RelocationRef(Ret, this)); |
| 1186 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1187 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1188 | void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 1189 | ++Rel.d.b; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1190 | } |
Owen Anderson | 171f485 | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1191 | |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1192 | uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const { |
Rafael Espindola | 7247546 | 2014-04-04 00:31:12 +0000 | [diff] [blame] | 1193 | assert(getHeader().filetype == MachO::MH_OBJECT && |
| 1194 | "Only implemented for MH_OBJECT"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1195 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1196 | return getAnyRelocationAddress(RE); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1199 | symbol_iterator |
| 1200 | MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1201 | MachO::any_relocation_info RE = getRelocation(Rel); |
Tim Northover | 07f99fb | 2014-07-04 10:57:56 +0000 | [diff] [blame] | 1202 | if (isRelocationScattered(RE)) |
| 1203 | return symbol_end(); |
| 1204 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1205 | uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE); |
| 1206 | bool isExtern = getPlainRelocationExternal(RE); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1207 | if (!isExtern) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1208 | return symbol_end(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1209 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1210 | MachO::symtab_command S = getSymtabLoadCommand(); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1211 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1212 | sizeof(MachO::nlist_64) : |
| 1213 | sizeof(MachO::nlist); |
| 1214 | uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1215 | DataRefImpl Sym; |
| 1216 | Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 1217 | return symbol_iterator(SymbolRef(Sym, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1220 | section_iterator |
| 1221 | MachOObjectFile::getRelocationSection(DataRefImpl Rel) const { |
| 1222 | return section_iterator(getAnyRelocationSection(getRelocation(Rel))); |
| 1223 | } |
| 1224 | |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1225 | uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1226 | MachO::any_relocation_info RE = getRelocation(Rel); |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1227 | return getAnyRelocationType(RE); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1230 | void MachOObjectFile::getRelocationTypeName( |
| 1231 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1232 | StringRef res; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1233 | uint64_t RType = getRelocationType(Rel); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1234 | |
| 1235 | unsigned Arch = this->getArch(); |
| 1236 | |
| 1237 | switch (Arch) { |
| 1238 | case Triple::x86: { |
| 1239 | static const char *const Table[] = { |
| 1240 | "GENERIC_RELOC_VANILLA", |
| 1241 | "GENERIC_RELOC_PAIR", |
| 1242 | "GENERIC_RELOC_SECTDIFF", |
| 1243 | "GENERIC_RELOC_PB_LA_PTR", |
| 1244 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
| 1245 | "GENERIC_RELOC_TLV" }; |
| 1246 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 1247 | if (RType > 5) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1248 | res = "Unknown"; |
| 1249 | else |
| 1250 | res = Table[RType]; |
| 1251 | break; |
| 1252 | } |
| 1253 | case Triple::x86_64: { |
| 1254 | static const char *const Table[] = { |
| 1255 | "X86_64_RELOC_UNSIGNED", |
| 1256 | "X86_64_RELOC_SIGNED", |
| 1257 | "X86_64_RELOC_BRANCH", |
| 1258 | "X86_64_RELOC_GOT_LOAD", |
| 1259 | "X86_64_RELOC_GOT", |
| 1260 | "X86_64_RELOC_SUBTRACTOR", |
| 1261 | "X86_64_RELOC_SIGNED_1", |
| 1262 | "X86_64_RELOC_SIGNED_2", |
| 1263 | "X86_64_RELOC_SIGNED_4", |
| 1264 | "X86_64_RELOC_TLV" }; |
| 1265 | |
| 1266 | if (RType > 9) |
| 1267 | res = "Unknown"; |
| 1268 | else |
| 1269 | res = Table[RType]; |
| 1270 | break; |
| 1271 | } |
| 1272 | case Triple::arm: { |
| 1273 | static const char *const Table[] = { |
| 1274 | "ARM_RELOC_VANILLA", |
| 1275 | "ARM_RELOC_PAIR", |
| 1276 | "ARM_RELOC_SECTDIFF", |
| 1277 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 1278 | "ARM_RELOC_PB_LA_PTR", |
| 1279 | "ARM_RELOC_BR24", |
| 1280 | "ARM_THUMB_RELOC_BR22", |
| 1281 | "ARM_THUMB_32BIT_BRANCH", |
| 1282 | "ARM_RELOC_HALF", |
| 1283 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 1284 | |
| 1285 | if (RType > 9) |
| 1286 | res = "Unknown"; |
| 1287 | else |
| 1288 | res = Table[RType]; |
| 1289 | break; |
| 1290 | } |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1291 | case Triple::aarch64: { |
| 1292 | static const char *const Table[] = { |
| 1293 | "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR", |
| 1294 | "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21", |
| 1295 | "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21", |
| 1296 | "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT", |
| 1297 | "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", |
| 1298 | "ARM64_RELOC_ADDEND" |
| 1299 | }; |
| 1300 | |
| 1301 | if (RType >= array_lengthof(Table)) |
| 1302 | res = "Unknown"; |
| 1303 | else |
| 1304 | res = Table[RType]; |
| 1305 | break; |
| 1306 | } |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1307 | case Triple::ppc: { |
| 1308 | static const char *const Table[] = { |
| 1309 | "PPC_RELOC_VANILLA", |
| 1310 | "PPC_RELOC_PAIR", |
| 1311 | "PPC_RELOC_BR14", |
| 1312 | "PPC_RELOC_BR24", |
| 1313 | "PPC_RELOC_HI16", |
| 1314 | "PPC_RELOC_LO16", |
| 1315 | "PPC_RELOC_HA16", |
| 1316 | "PPC_RELOC_LO14", |
| 1317 | "PPC_RELOC_SECTDIFF", |
| 1318 | "PPC_RELOC_PB_LA_PTR", |
| 1319 | "PPC_RELOC_HI16_SECTDIFF", |
| 1320 | "PPC_RELOC_LO16_SECTDIFF", |
| 1321 | "PPC_RELOC_HA16_SECTDIFF", |
| 1322 | "PPC_RELOC_JBSR", |
| 1323 | "PPC_RELOC_LO14_SECTDIFF", |
| 1324 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 1325 | |
Eric Christopher | 13250cb | 2013-12-06 02:33:38 +0000 | [diff] [blame] | 1326 | if (RType > 15) |
| 1327 | res = "Unknown"; |
| 1328 | else |
| 1329 | res = Table[RType]; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | case Triple::UnknownArch: |
| 1333 | res = "Unknown"; |
| 1334 | break; |
| 1335 | } |
| 1336 | Result.append(res.begin(), res.end()); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Keno Fischer | 281b694 | 2015-05-30 19:44:53 +0000 | [diff] [blame] | 1339 | uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const { |
| 1340 | MachO::any_relocation_info RE = getRelocation(Rel); |
| 1341 | return getAnyRelocationLength(RE); |
| 1342 | } |
| 1343 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1344 | // |
| 1345 | // guessLibraryShortName() is passed a name of a dynamic library and returns a |
| 1346 | // guess on what the short name is. Then name is returned as a substring of the |
| 1347 | // StringRef Name passed in. The name of the dynamic library is recognized as |
| 1348 | // a framework if it has one of the two following forms: |
| 1349 | // Foo.framework/Versions/A/Foo |
| 1350 | // Foo.framework/Foo |
| 1351 | // Where A and Foo can be any string. And may contain a trailing suffix |
| 1352 | // starting with an underbar. If the Name is recognized as a framework then |
| 1353 | // isFramework is set to true else it is set to false. If the Name has a |
| 1354 | // suffix then Suffix is set to the substring in Name that contains the suffix |
| 1355 | // else it is set to a NULL StringRef. |
| 1356 | // |
| 1357 | // The Name of the dynamic library is recognized as a library name if it has |
| 1358 | // one of the two following forms: |
| 1359 | // libFoo.A.dylib |
| 1360 | // libFoo.dylib |
| 1361 | // The library may have a suffix trailing the name Foo of the form: |
| 1362 | // libFoo_profile.A.dylib |
| 1363 | // libFoo_profile.dylib |
| 1364 | // |
| 1365 | // The Name of the dynamic library is also recognized as a library name if it |
| 1366 | // has the following form: |
| 1367 | // Foo.qtx |
| 1368 | // |
| 1369 | // If the Name of the dynamic library is none of the forms above then a NULL |
| 1370 | // StringRef is returned. |
| 1371 | // |
| 1372 | StringRef MachOObjectFile::guessLibraryShortName(StringRef Name, |
| 1373 | bool &isFramework, |
| 1374 | StringRef &Suffix) { |
| 1375 | StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx; |
| 1376 | size_t a, b, c, d, Idx; |
| 1377 | |
| 1378 | isFramework = false; |
| 1379 | Suffix = StringRef(); |
| 1380 | |
| 1381 | // Pull off the last component and make Foo point to it |
| 1382 | a = Name.rfind('/'); |
| 1383 | if (a == Name.npos || a == 0) |
| 1384 | goto guess_library; |
| 1385 | Foo = Name.slice(a+1, Name.npos); |
| 1386 | |
| 1387 | // Look for a suffix starting with a '_' |
| 1388 | Idx = Foo.rfind('_'); |
| 1389 | if (Idx != Foo.npos && Foo.size() >= 2) { |
| 1390 | Suffix = Foo.slice(Idx, Foo.npos); |
| 1391 | Foo = Foo.slice(0, Idx); |
| 1392 | } |
| 1393 | |
| 1394 | // First look for the form Foo.framework/Foo |
| 1395 | b = Name.rfind('/', a); |
| 1396 | if (b == Name.npos) |
| 1397 | Idx = 0; |
| 1398 | else |
| 1399 | Idx = b+1; |
| 1400 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1401 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1402 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1403 | if (F == Foo && DotFramework == ".framework/") { |
| 1404 | isFramework = true; |
| 1405 | return Foo; |
| 1406 | } |
| 1407 | |
| 1408 | // Next look for the form Foo.framework/Versions/A/Foo |
| 1409 | if (b == Name.npos) |
| 1410 | goto guess_library; |
| 1411 | c = Name.rfind('/', b); |
| 1412 | if (c == Name.npos || c == 0) |
| 1413 | goto guess_library; |
| 1414 | V = Name.slice(c+1, Name.npos); |
| 1415 | if (!V.startswith("Versions/")) |
| 1416 | goto guess_library; |
| 1417 | d = Name.rfind('/', c); |
| 1418 | if (d == Name.npos) |
| 1419 | Idx = 0; |
| 1420 | else |
| 1421 | Idx = d+1; |
| 1422 | F = Name.slice(Idx, Idx + Foo.size()); |
| 1423 | DotFramework = Name.slice(Idx + Foo.size(), |
| 1424 | Idx + Foo.size() + sizeof(".framework/")-1); |
| 1425 | if (F == Foo && DotFramework == ".framework/") { |
| 1426 | isFramework = true; |
| 1427 | return Foo; |
| 1428 | } |
| 1429 | |
| 1430 | guess_library: |
| 1431 | // pull off the suffix after the "." and make a point to it |
| 1432 | a = Name.rfind('.'); |
| 1433 | if (a == Name.npos || a == 0) |
| 1434 | return StringRef(); |
| 1435 | Dylib = Name.slice(a, Name.npos); |
| 1436 | if (Dylib != ".dylib") |
| 1437 | goto guess_qtx; |
| 1438 | |
| 1439 | // First pull off the version letter for the form Foo.A.dylib if any. |
| 1440 | if (a >= 3) { |
| 1441 | Dot = Name.slice(a-2, a-1); |
| 1442 | if (Dot == ".") |
| 1443 | a = a - 2; |
| 1444 | } |
| 1445 | |
| 1446 | b = Name.rfind('/', a); |
| 1447 | if (b == Name.npos) |
| 1448 | b = 0; |
| 1449 | else |
| 1450 | b = b+1; |
| 1451 | // ignore any suffix after an underbar like Foo_profile.A.dylib |
| 1452 | Idx = Name.find('_', b); |
| 1453 | if (Idx != Name.npos && Idx != b) { |
| 1454 | Lib = Name.slice(b, Idx); |
| 1455 | Suffix = Name.slice(Idx, a); |
| 1456 | } |
| 1457 | else |
| 1458 | Lib = Name.slice(b, a); |
| 1459 | // There are incorrect library names of the form: |
| 1460 | // libATS.A_profile.dylib so check for these. |
| 1461 | if (Lib.size() >= 3) { |
| 1462 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1463 | if (Dot == ".") |
| 1464 | Lib = Lib.slice(0, Lib.size()-2); |
| 1465 | } |
| 1466 | return Lib; |
| 1467 | |
| 1468 | guess_qtx: |
| 1469 | Qtx = Name.slice(a, Name.npos); |
| 1470 | if (Qtx != ".qtx") |
| 1471 | return StringRef(); |
| 1472 | b = Name.rfind('/', a); |
| 1473 | if (b == Name.npos) |
| 1474 | Lib = Name.slice(0, a); |
| 1475 | else |
| 1476 | Lib = Name.slice(b+1, a); |
| 1477 | // There are library names of the form: QT.A.qtx so check for these. |
| 1478 | if (Lib.size() >= 3) { |
| 1479 | Dot = Lib.slice(Lib.size()-2, Lib.size()-1); |
| 1480 | if (Dot == ".") |
| 1481 | Lib = Lib.slice(0, Lib.size()-2); |
| 1482 | } |
| 1483 | return Lib; |
| 1484 | } |
| 1485 | |
| 1486 | // getLibraryShortNameByIndex() is used to get the short name of the library |
| 1487 | // for an undefined symbol in a linked Mach-O binary that was linked with the |
| 1488 | // normal two-level namespace default (that is MH_TWOLEVEL in the header). |
| 1489 | // It is passed the index (0 - based) of the library as translated from |
| 1490 | // GET_LIBRARY_ORDINAL (1 - based). |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 1491 | std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index, |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1492 | StringRef &Res) const { |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1493 | if (Index >= Libraries.size()) |
| 1494 | return object_error::parse_failed; |
| 1495 | |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1496 | // If the cache of LibrariesShortNames is not built up do that first for |
| 1497 | // all the Libraries. |
| 1498 | if (LibrariesShortNames.size() == 0) { |
| 1499 | for (unsigned i = 0; i < Libraries.size(); i++) { |
| 1500 | MachO::dylib_command D = |
| 1501 | getStruct<MachO::dylib_command>(this, Libraries[i]); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1502 | if (D.dylib.name >= D.cmdsize) |
| 1503 | return object_error::parse_failed; |
Kevin Enderby | 4eff6cd | 2014-06-20 18:07:34 +0000 | [diff] [blame] | 1504 | const char *P = (const char *)(Libraries[i]) + D.dylib.name; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1505 | StringRef Name = StringRef(P); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1506 | if (D.dylib.name+Name.size() >= D.cmdsize) |
| 1507 | return object_error::parse_failed; |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1508 | StringRef Suffix; |
| 1509 | bool isFramework; |
| 1510 | StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix); |
Nick Kledzik | 3006130 | 2014-09-17 00:25:22 +0000 | [diff] [blame] | 1511 | if (shortName.empty()) |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1512 | LibrariesShortNames.push_back(Name); |
| 1513 | else |
| 1514 | LibrariesShortNames.push_back(shortName); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | Res = LibrariesShortNames[Index]; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 1519 | return std::error_code(); |
Kevin Enderby | 980b258 | 2014-06-05 21:21:57 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Rafael Espindola | 76ad232 | 2015-07-06 14:55:37 +0000 | [diff] [blame] | 1522 | section_iterator |
| 1523 | MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const { |
| 1524 | DataRefImpl Sec; |
| 1525 | Sec.d.a = Rel->getRawDataRefImpl().d.a; |
| 1526 | return section_iterator(SectionRef(Sec, this)); |
| 1527 | } |
| 1528 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1529 | basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const { |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1530 | DataRefImpl DRI; |
| 1531 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1532 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
| 1533 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1534 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1535 | return getSymbolByIndex(0); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1538 | basic_symbol_iterator MachOObjectFile::symbol_end_impl() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1539 | DataRefImpl DRI; |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1540 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1541 | if (!SymtabLoadCmd || Symtab.nsyms == 0) |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1542 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1543 | |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1544 | unsigned SymbolTableEntrySize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1545 | sizeof(MachO::nlist_64) : |
| 1546 | sizeof(MachO::nlist); |
| 1547 | unsigned Offset = Symtab.symoff + |
| 1548 | Symtab.nsyms * SymbolTableEntrySize; |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 1549 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1550 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1553 | basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const { |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1554 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1555 | if (!SymtabLoadCmd || Index >= Symtab.nsyms) |
Filipe Cabecinhas | 4013950 | 2015-01-15 22:52:38 +0000 | [diff] [blame] | 1556 | report_fatal_error("Requested symbol index is out of range."); |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1557 | unsigned SymbolTableEntrySize = |
| 1558 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
Kevin Enderby | 1829c68 | 2016-01-22 22:49:55 +0000 | [diff] [blame] | 1559 | DataRefImpl DRI; |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 1560 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); |
| 1561 | DRI.p += Index * SymbolTableEntrySize; |
| 1562 | return basic_symbol_iterator(SymbolRef(DRI, this)); |
| 1563 | } |
| 1564 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1565 | uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const { |
| 1566 | MachO::symtab_command Symtab = getSymtabLoadCommand(); |
| 1567 | if (!SymtabLoadCmd) |
| 1568 | report_fatal_error("getSymbolIndex() called with no symbol table symbol"); |
| 1569 | unsigned SymbolTableEntrySize = |
| 1570 | is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist); |
| 1571 | DataRefImpl DRIstart; |
| 1572 | DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff)); |
| 1573 | uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize; |
| 1574 | return Index; |
| 1575 | } |
| 1576 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1577 | section_iterator MachOObjectFile::section_begin() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1578 | DataRefImpl DRI; |
| 1579 | return section_iterator(SectionRef(DRI, this)); |
| 1580 | } |
| 1581 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1582 | section_iterator MachOObjectFile::section_end() const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1583 | DataRefImpl DRI; |
| 1584 | DRI.d.a = Sections.size(); |
| 1585 | return section_iterator(SectionRef(DRI, this)); |
| 1586 | } |
| 1587 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1588 | uint8_t MachOObjectFile::getBytesInAddress() const { |
Rafael Espindola | 6068998 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 1589 | return is64Bit() ? 8 : 4; |
Eric Christopher | 7b015c7 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1592 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1593 | unsigned CPUType = getCPUType(this); |
| 1594 | if (!is64Bit()) { |
| 1595 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1596 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1597 | return "Mach-O 32-bit i386"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1598 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1599 | return "Mach-O arm"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1600 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1601 | return "Mach-O 32-bit ppc"; |
| 1602 | default: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1603 | return "Mach-O 32-bit unknown"; |
| 1604 | } |
| 1605 | } |
| 1606 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1607 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1608 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1609 | return "Mach-O 64-bit x86-64"; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1610 | case llvm::MachO::CPU_TYPE_ARM64: |
| 1611 | return "Mach-O arm64"; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1612 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1613 | return "Mach-O 64-bit ppc64"; |
| 1614 | default: |
| 1615 | return "Mach-O 64-bit unknown"; |
| 1616 | } |
| 1617 | } |
| 1618 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1619 | Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) { |
| 1620 | switch (CPUType) { |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1621 | case llvm::MachO::CPU_TYPE_I386: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1622 | return Triple::x86; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1623 | case llvm::MachO::CPU_TYPE_X86_64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1624 | return Triple::x86_64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1625 | case llvm::MachO::CPU_TYPE_ARM: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1626 | return Triple::arm; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 1627 | case llvm::MachO::CPU_TYPE_ARM64: |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 1628 | return Triple::aarch64; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1629 | case llvm::MachO::CPU_TYPE_POWERPC: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1630 | return Triple::ppc; |
Charles Davis | 74ec8b0 | 2013-08-27 05:00:13 +0000 | [diff] [blame] | 1631 | case llvm::MachO::CPU_TYPE_POWERPC64: |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1632 | return Triple::ppc64; |
| 1633 | default: |
| 1634 | return Triple::UnknownArch; |
| 1635 | } |
| 1636 | } |
| 1637 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1638 | Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType, |
| 1639 | const char **McpuDefault) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1640 | if (McpuDefault) |
| 1641 | *McpuDefault = nullptr; |
| 1642 | |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1643 | switch (CPUType) { |
| 1644 | case MachO::CPU_TYPE_I386: |
| 1645 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1646 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 1647 | return Triple("i386-apple-darwin"); |
| 1648 | default: |
| 1649 | return Triple(); |
| 1650 | } |
| 1651 | case MachO::CPU_TYPE_X86_64: |
| 1652 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1653 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 1654 | return Triple("x86_64-apple-darwin"); |
| 1655 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 1656 | return Triple("x86_64h-apple-darwin"); |
| 1657 | default: |
| 1658 | return Triple(); |
| 1659 | } |
| 1660 | case MachO::CPU_TYPE_ARM: |
| 1661 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1662 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 1663 | return Triple("armv4t-apple-darwin"); |
| 1664 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 1665 | return Triple("armv5e-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 1666 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 1667 | return Triple("xscale-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1668 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 1669 | return Triple("armv6-apple-darwin"); |
| 1670 | case MachO::CPU_SUBTYPE_ARM_V6M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1671 | if (McpuDefault) |
| 1672 | *McpuDefault = "cortex-m0"; |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1673 | return Triple("armv6m-apple-darwin"); |
Kevin Enderby | ae2a9a2 | 2014-08-07 21:30:25 +0000 | [diff] [blame] | 1674 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 1675 | return Triple("armv7-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1676 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1677 | if (McpuDefault) |
| 1678 | *McpuDefault = "cortex-m4"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1679 | return Triple("thumbv7em-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1680 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 1681 | return Triple("armv7k-apple-darwin"); |
| 1682 | case MachO::CPU_SUBTYPE_ARM_V7M: |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1683 | if (McpuDefault) |
| 1684 | *McpuDefault = "cortex-m3"; |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1685 | return Triple("thumbv7m-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1686 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 1687 | return Triple("armv7s-apple-darwin"); |
| 1688 | default: |
| 1689 | return Triple(); |
| 1690 | } |
| 1691 | case MachO::CPU_TYPE_ARM64: |
| 1692 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1693 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 1694 | return Triple("arm64-apple-darwin"); |
| 1695 | default: |
| 1696 | return Triple(); |
| 1697 | } |
| 1698 | case MachO::CPU_TYPE_POWERPC: |
| 1699 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
| 1700 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1701 | return Triple("ppc-apple-darwin"); |
| 1702 | default: |
| 1703 | return Triple(); |
| 1704 | } |
| 1705 | case MachO::CPU_TYPE_POWERPC64: |
Reid Kleckner | 4da3d57 | 2014-06-30 20:12:59 +0000 | [diff] [blame] | 1706 | switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) { |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1707 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 1708 | return Triple("ppc64-apple-darwin"); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1709 | default: |
| 1710 | return Triple(); |
| 1711 | } |
| 1712 | default: |
| 1713 | return Triple(); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | Triple MachOObjectFile::getHostArch() { |
| 1718 | return Triple(sys::getDefaultTargetTriple()); |
| 1719 | } |
| 1720 | |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 1721 | bool MachOObjectFile::isValidArch(StringRef ArchFlag) { |
| 1722 | return StringSwitch<bool>(ArchFlag) |
| 1723 | .Case("i386", true) |
| 1724 | .Case("x86_64", true) |
| 1725 | .Case("x86_64h", true) |
| 1726 | .Case("armv4t", true) |
| 1727 | .Case("arm", true) |
| 1728 | .Case("armv5e", true) |
| 1729 | .Case("armv6", true) |
| 1730 | .Case("armv6m", true) |
Frederic Riss | 40baa0a | 2015-06-16 17:37:03 +0000 | [diff] [blame] | 1731 | .Case("armv7", true) |
Rafael Espindola | 72318b4 | 2014-08-08 16:30:17 +0000 | [diff] [blame] | 1732 | .Case("armv7em", true) |
| 1733 | .Case("armv7k", true) |
| 1734 | .Case("armv7m", true) |
| 1735 | .Case("armv7s", true) |
| 1736 | .Case("arm64", true) |
| 1737 | .Case("ppc", true) |
| 1738 | .Case("ppc64", true) |
| 1739 | .Default(false); |
Kevin Enderby | 4c8dfe4 | 2014-06-30 18:45:23 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 1742 | unsigned MachOObjectFile::getArch() const { |
| 1743 | return getArch(getCPUType(this)); |
| 1744 | } |
| 1745 | |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 1746 | Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const { |
| 1747 | return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1750 | relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1751 | DataRefImpl DRI; |
| 1752 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1753 | return section_rel_begin(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1756 | relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1757 | DataRefImpl DRI; |
| 1758 | DRI.d.a = Index; |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 1759 | return section_rel_end(DRI); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1762 | dice_iterator MachOObjectFile::begin_dices() const { |
| 1763 | DataRefImpl DRI; |
| 1764 | if (!DataInCodeLoadCmd) |
| 1765 | return dice_iterator(DiceRef(DRI, this)); |
| 1766 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1767 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 1768 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1769 | return dice_iterator(DiceRef(DRI, this)); |
| 1770 | } |
| 1771 | |
| 1772 | dice_iterator MachOObjectFile::end_dices() const { |
| 1773 | DataRefImpl DRI; |
| 1774 | if (!DataInCodeLoadCmd) |
| 1775 | return dice_iterator(DiceRef(DRI, this)); |
| 1776 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1777 | MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand(); |
| 1778 | unsigned Offset = DicLC.dataoff + DicLC.datasize; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1779 | DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset)); |
| 1780 | return dice_iterator(DiceRef(DRI, this)); |
| 1781 | } |
| 1782 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 1783 | ExportEntry::ExportEntry(ArrayRef<uint8_t> T) |
| 1784 | : Trie(T), Malformed(false), Done(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1785 | |
| 1786 | void ExportEntry::moveToFirst() { |
| 1787 | pushNode(0); |
| 1788 | pushDownUntilBottom(); |
| 1789 | } |
| 1790 | |
| 1791 | void ExportEntry::moveToEnd() { |
| 1792 | Stack.clear(); |
| 1793 | Done = true; |
| 1794 | } |
| 1795 | |
| 1796 | bool ExportEntry::operator==(const ExportEntry &Other) const { |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 1797 | // Common case, one at end, other iterating from begin. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1798 | if (Done || Other.Done) |
| 1799 | return (Done == Other.Done); |
| 1800 | // Not equal if different stack sizes. |
| 1801 | if (Stack.size() != Other.Stack.size()) |
| 1802 | return false; |
| 1803 | // Not equal if different cumulative strings. |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1804 | if (!CumulativeString.equals(Other.CumulativeString)) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1805 | return false; |
| 1806 | // Equal if all nodes in both stacks match. |
| 1807 | for (unsigned i=0; i < Stack.size(); ++i) { |
| 1808 | if (Stack[i].Start != Other.Stack[i].Start) |
| 1809 | return false; |
| 1810 | } |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 1811 | return true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 1814 | uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) { |
| 1815 | unsigned Count; |
| 1816 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 1817 | Ptr += Count; |
| 1818 | if (Ptr > Trie.end()) { |
| 1819 | Ptr = Trie.end(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1820 | Malformed = true; |
| 1821 | } |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 1822 | return Result; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | StringRef ExportEntry::name() const { |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1826 | return CumulativeString; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | uint64_t ExportEntry::flags() const { |
| 1830 | return Stack.back().Flags; |
| 1831 | } |
| 1832 | |
| 1833 | uint64_t ExportEntry::address() const { |
| 1834 | return Stack.back().Address; |
| 1835 | } |
| 1836 | |
| 1837 | uint64_t ExportEntry::other() const { |
| 1838 | return Stack.back().Other; |
| 1839 | } |
| 1840 | |
| 1841 | StringRef ExportEntry::otherName() const { |
| 1842 | const char* ImportName = Stack.back().ImportName; |
| 1843 | if (ImportName) |
| 1844 | return StringRef(ImportName); |
| 1845 | return StringRef(); |
| 1846 | } |
| 1847 | |
| 1848 | uint32_t ExportEntry::nodeOffset() const { |
| 1849 | return Stack.back().Start - Trie.begin(); |
| 1850 | } |
| 1851 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 1852 | ExportEntry::NodeState::NodeState(const uint8_t *Ptr) |
| 1853 | : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0), |
| 1854 | ImportName(nullptr), ChildCount(0), NextChildIndex(0), |
| 1855 | ParentStringLength(0), IsExportNode(false) {} |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1856 | |
| 1857 | void ExportEntry::pushNode(uint64_t offset) { |
| 1858 | const uint8_t *Ptr = Trie.begin() + offset; |
| 1859 | NodeState State(Ptr); |
| 1860 | uint64_t ExportInfoSize = readULEB128(State.Current); |
| 1861 | State.IsExportNode = (ExportInfoSize != 0); |
| 1862 | const uint8_t* Children = State.Current + ExportInfoSize; |
| 1863 | if (State.IsExportNode) { |
| 1864 | State.Flags = readULEB128(State.Current); |
| 1865 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { |
| 1866 | State.Address = 0; |
| 1867 | State.Other = readULEB128(State.Current); // dylib ordinal |
| 1868 | State.ImportName = reinterpret_cast<const char*>(State.Current); |
| 1869 | } else { |
| 1870 | State.Address = readULEB128(State.Current); |
Nick Kledzik | 1b591bd | 2014-08-30 01:57:34 +0000 | [diff] [blame] | 1871 | if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 1872 | State.Other = readULEB128(State.Current); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | State.ChildCount = *Children; |
| 1876 | State.Current = Children + 1; |
| 1877 | State.NextChildIndex = 0; |
| 1878 | State.ParentStringLength = CumulativeString.size(); |
| 1879 | Stack.push_back(State); |
| 1880 | } |
| 1881 | |
| 1882 | void ExportEntry::pushDownUntilBottom() { |
| 1883 | while (Stack.back().NextChildIndex < Stack.back().ChildCount) { |
| 1884 | NodeState &Top = Stack.back(); |
| 1885 | CumulativeString.resize(Top.ParentStringLength); |
| 1886 | for (;*Top.Current != 0; Top.Current++) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 1887 | char C = *Top.Current; |
| 1888 | CumulativeString.push_back(C); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1889 | } |
| 1890 | Top.Current += 1; |
| 1891 | uint64_t childNodeIndex = readULEB128(Top.Current); |
| 1892 | Top.NextChildIndex += 1; |
| 1893 | pushNode(childNodeIndex); |
| 1894 | } |
| 1895 | if (!Stack.back().IsExportNode) { |
| 1896 | Malformed = true; |
| 1897 | moveToEnd(); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // We have a trie data structure and need a way to walk it that is compatible |
| 1902 | // with the C++ iterator model. The solution is a non-recursive depth first |
| 1903 | // traversal where the iterator contains a stack of parent nodes along with a |
| 1904 | // string that is the accumulation of all edge strings along the parent chain |
| 1905 | // to this point. |
| 1906 | // |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 1907 | // There is one "export" node for each exported symbol. But because some |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1908 | // 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] | 1909 | // node may have child nodes too. |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1910 | // |
| 1911 | // The algorithm for moveNext() is to keep moving down the leftmost unvisited |
| 1912 | // child until hitting a node with no children (which is an export node or |
| 1913 | // else the trie is malformed). On the way down, each node is pushed on the |
| 1914 | // stack ivar. If there is no more ways down, it pops up one and tries to go |
| 1915 | // down a sibling path until a childless node is reached. |
| 1916 | void ExportEntry::moveNext() { |
| 1917 | if (Stack.empty() || !Stack.back().IsExportNode) { |
| 1918 | Malformed = true; |
| 1919 | moveToEnd(); |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | Stack.pop_back(); |
| 1924 | while (!Stack.empty()) { |
| 1925 | NodeState &Top = Stack.back(); |
| 1926 | if (Top.NextChildIndex < Top.ChildCount) { |
| 1927 | pushDownUntilBottom(); |
| 1928 | // Now at the next export node. |
| 1929 | return; |
| 1930 | } else { |
| 1931 | if (Top.IsExportNode) { |
| 1932 | // This node has no children but is itself an export node. |
| 1933 | CumulativeString.resize(Top.ParentStringLength); |
| 1934 | return; |
| 1935 | } |
| 1936 | Stack.pop_back(); |
| 1937 | } |
| 1938 | } |
| 1939 | Done = true; |
| 1940 | } |
| 1941 | |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 1942 | iterator_range<export_iterator> |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1943 | MachOObjectFile::exports(ArrayRef<uint8_t> Trie) { |
| 1944 | ExportEntry Start(Trie); |
Juergen Ributzka | 4d7f70d | 2014-12-19 02:31:01 +0000 | [diff] [blame] | 1945 | if (Trie.size() == 0) |
| 1946 | Start.moveToEnd(); |
| 1947 | else |
| 1948 | Start.moveToFirst(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1949 | |
| 1950 | ExportEntry Finish(Trie); |
| 1951 | Finish.moveToEnd(); |
| 1952 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 1953 | return make_range(export_iterator(Start), export_iterator(Finish)); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | iterator_range<export_iterator> MachOObjectFile::exports() const { |
| 1957 | return exports(getDyldInfoExportsTrie()); |
| 1958 | } |
| 1959 | |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 1960 | MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit) |
| 1961 | : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0), |
| 1962 | RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0), |
| 1963 | PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {} |
| 1964 | |
| 1965 | void MachORebaseEntry::moveToFirst() { |
| 1966 | Ptr = Opcodes.begin(); |
| 1967 | moveNext(); |
| 1968 | } |
| 1969 | |
| 1970 | void MachORebaseEntry::moveToEnd() { |
| 1971 | Ptr = Opcodes.end(); |
| 1972 | RemainingLoopCount = 0; |
| 1973 | Done = true; |
| 1974 | } |
| 1975 | |
| 1976 | void MachORebaseEntry::moveNext() { |
| 1977 | // If in the middle of some loop, move to next rebasing in loop. |
| 1978 | SegmentOffset += AdvanceAmount; |
| 1979 | if (RemainingLoopCount) { |
| 1980 | --RemainingLoopCount; |
| 1981 | return; |
| 1982 | } |
| 1983 | if (Ptr == Opcodes.end()) { |
| 1984 | Done = true; |
| 1985 | return; |
| 1986 | } |
| 1987 | bool More = true; |
| 1988 | while (More && !Malformed) { |
| 1989 | // Parse next opcode and set up next loop. |
| 1990 | uint8_t Byte = *Ptr++; |
| 1991 | uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK; |
| 1992 | uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK; |
| 1993 | switch (Opcode) { |
| 1994 | case MachO::REBASE_OPCODE_DONE: |
| 1995 | More = false; |
| 1996 | Done = true; |
| 1997 | moveToEnd(); |
| 1998 | DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n"); |
| 1999 | break; |
| 2000 | case MachO::REBASE_OPCODE_SET_TYPE_IMM: |
| 2001 | RebaseType = ImmValue; |
| 2002 | DEBUG_WITH_TYPE( |
| 2003 | "mach-o-rebase", |
| 2004 | llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: " |
| 2005 | << "RebaseType=" << (int) RebaseType << "\n"); |
| 2006 | break; |
| 2007 | case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 2008 | SegmentIndex = ImmValue; |
| 2009 | SegmentOffset = readULEB128(); |
| 2010 | DEBUG_WITH_TYPE( |
| 2011 | "mach-o-rebase", |
| 2012 | llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 2013 | << "SegmentIndex=" << SegmentIndex << ", " |
| 2014 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2015 | << "\n"); |
| 2016 | break; |
| 2017 | case MachO::REBASE_OPCODE_ADD_ADDR_ULEB: |
| 2018 | SegmentOffset += readULEB128(); |
| 2019 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2020 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: " |
| 2021 | << format("SegmentOffset=0x%06X", |
| 2022 | SegmentOffset) << "\n"); |
| 2023 | break; |
| 2024 | case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED: |
| 2025 | SegmentOffset += ImmValue * PointerSize; |
| 2026 | DEBUG_WITH_TYPE("mach-o-rebase", |
| 2027 | llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: " |
| 2028 | << format("SegmentOffset=0x%06X", |
| 2029 | SegmentOffset) << "\n"); |
| 2030 | break; |
| 2031 | case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES: |
| 2032 | AdvanceAmount = PointerSize; |
| 2033 | RemainingLoopCount = ImmValue - 1; |
| 2034 | DEBUG_WITH_TYPE( |
| 2035 | "mach-o-rebase", |
| 2036 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: " |
| 2037 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2038 | << ", AdvanceAmount=" << AdvanceAmount |
| 2039 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2040 | << "\n"); |
| 2041 | return; |
| 2042 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES: |
| 2043 | AdvanceAmount = PointerSize; |
| 2044 | RemainingLoopCount = readULEB128() - 1; |
| 2045 | DEBUG_WITH_TYPE( |
| 2046 | "mach-o-rebase", |
| 2047 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: " |
| 2048 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2049 | << ", AdvanceAmount=" << AdvanceAmount |
| 2050 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2051 | << "\n"); |
| 2052 | return; |
| 2053 | case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: |
| 2054 | AdvanceAmount = readULEB128() + PointerSize; |
| 2055 | RemainingLoopCount = 0; |
| 2056 | DEBUG_WITH_TYPE( |
| 2057 | "mach-o-rebase", |
| 2058 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: " |
| 2059 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2060 | << ", AdvanceAmount=" << AdvanceAmount |
| 2061 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2062 | << "\n"); |
| 2063 | return; |
| 2064 | case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: |
| 2065 | RemainingLoopCount = readULEB128() - 1; |
| 2066 | AdvanceAmount = readULEB128() + PointerSize; |
| 2067 | DEBUG_WITH_TYPE( |
| 2068 | "mach-o-rebase", |
| 2069 | llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: " |
| 2070 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2071 | << ", AdvanceAmount=" << AdvanceAmount |
| 2072 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2073 | << "\n"); |
| 2074 | return; |
| 2075 | default: |
| 2076 | Malformed = true; |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | uint64_t MachORebaseEntry::readULEB128() { |
| 2082 | unsigned Count; |
| 2083 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2084 | Ptr += Count; |
| 2085 | if (Ptr > Opcodes.end()) { |
| 2086 | Ptr = Opcodes.end(); |
| 2087 | Malformed = true; |
| 2088 | } |
| 2089 | return Result; |
| 2090 | } |
| 2091 | |
| 2092 | uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; } |
| 2093 | |
| 2094 | uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; } |
| 2095 | |
| 2096 | StringRef MachORebaseEntry::typeName() const { |
| 2097 | switch (RebaseType) { |
| 2098 | case MachO::REBASE_TYPE_POINTER: |
| 2099 | return "pointer"; |
| 2100 | case MachO::REBASE_TYPE_TEXT_ABSOLUTE32: |
| 2101 | return "text abs32"; |
| 2102 | case MachO::REBASE_TYPE_TEXT_PCREL32: |
| 2103 | return "text rel32"; |
| 2104 | } |
| 2105 | return "unknown"; |
| 2106 | } |
| 2107 | |
| 2108 | bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const { |
| 2109 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
| 2110 | return (Ptr == Other.Ptr) && |
| 2111 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 2112 | (Done == Other.Done); |
| 2113 | } |
| 2114 | |
| 2115 | iterator_range<rebase_iterator> |
| 2116 | MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) { |
| 2117 | MachORebaseEntry Start(Opcodes, is64); |
| 2118 | Start.moveToFirst(); |
| 2119 | |
| 2120 | MachORebaseEntry Finish(Opcodes, is64); |
| 2121 | Finish.moveToEnd(); |
| 2122 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2123 | return make_range(rebase_iterator(Start), rebase_iterator(Finish)); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const { |
| 2127 | return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit()); |
| 2128 | } |
| 2129 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2130 | MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2131 | : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0), |
| 2132 | Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0), |
| 2133 | BindType(0), PointerSize(is64Bit ? 8 : 4), |
| 2134 | TableKind(BK), Malformed(false), Done(false) {} |
| 2135 | |
| 2136 | void MachOBindEntry::moveToFirst() { |
| 2137 | Ptr = Opcodes.begin(); |
| 2138 | moveNext(); |
| 2139 | } |
| 2140 | |
| 2141 | void MachOBindEntry::moveToEnd() { |
| 2142 | Ptr = Opcodes.end(); |
| 2143 | RemainingLoopCount = 0; |
| 2144 | Done = true; |
| 2145 | } |
| 2146 | |
| 2147 | void MachOBindEntry::moveNext() { |
| 2148 | // If in the middle of some loop, move to next binding in loop. |
| 2149 | SegmentOffset += AdvanceAmount; |
| 2150 | if (RemainingLoopCount) { |
| 2151 | --RemainingLoopCount; |
| 2152 | return; |
| 2153 | } |
| 2154 | if (Ptr == Opcodes.end()) { |
| 2155 | Done = true; |
| 2156 | return; |
| 2157 | } |
| 2158 | bool More = true; |
| 2159 | while (More && !Malformed) { |
| 2160 | // Parse next opcode and set up next loop. |
| 2161 | uint8_t Byte = *Ptr++; |
| 2162 | uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK; |
| 2163 | uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK; |
| 2164 | int8_t SignExtended; |
| 2165 | const uint8_t *SymStart; |
| 2166 | switch (Opcode) { |
| 2167 | case MachO::BIND_OPCODE_DONE: |
| 2168 | if (TableKind == Kind::Lazy) { |
| 2169 | // Lazying bindings have a DONE opcode between entries. Need to ignore |
| 2170 | // it to advance to next entry. But need not if this is last entry. |
| 2171 | bool NotLastEntry = false; |
| 2172 | for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) { |
| 2173 | if (*P) { |
| 2174 | NotLastEntry = true; |
| 2175 | } |
| 2176 | } |
| 2177 | if (NotLastEntry) |
| 2178 | break; |
| 2179 | } |
| 2180 | More = false; |
| 2181 | Done = true; |
| 2182 | moveToEnd(); |
| 2183 | DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n"); |
| 2184 | break; |
| 2185 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: |
| 2186 | Ordinal = ImmValue; |
| 2187 | DEBUG_WITH_TYPE( |
| 2188 | "mach-o-bind", |
| 2189 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: " |
| 2190 | << "Ordinal=" << Ordinal << "\n"); |
| 2191 | break; |
| 2192 | case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: |
| 2193 | Ordinal = readULEB128(); |
| 2194 | DEBUG_WITH_TYPE( |
| 2195 | "mach-o-bind", |
| 2196 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: " |
| 2197 | << "Ordinal=" << Ordinal << "\n"); |
| 2198 | break; |
| 2199 | case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: |
| 2200 | if (ImmValue) { |
| 2201 | SignExtended = MachO::BIND_OPCODE_MASK | ImmValue; |
| 2202 | Ordinal = SignExtended; |
| 2203 | } else |
| 2204 | Ordinal = 0; |
| 2205 | DEBUG_WITH_TYPE( |
| 2206 | "mach-o-bind", |
| 2207 | llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: " |
| 2208 | << "Ordinal=" << Ordinal << "\n"); |
| 2209 | break; |
| 2210 | case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: |
| 2211 | Flags = ImmValue; |
| 2212 | SymStart = Ptr; |
| 2213 | while (*Ptr) { |
| 2214 | ++Ptr; |
| 2215 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2216 | SymbolName = StringRef(reinterpret_cast<const char*>(SymStart), |
| 2217 | Ptr-SymStart); |
Nick Kledzik | a637536 | 2014-09-17 01:51:43 +0000 | [diff] [blame] | 2218 | ++Ptr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2219 | DEBUG_WITH_TYPE( |
| 2220 | "mach-o-bind", |
| 2221 | llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: " |
| 2222 | << "SymbolName=" << SymbolName << "\n"); |
| 2223 | if (TableKind == Kind::Weak) { |
| 2224 | if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) |
| 2225 | return; |
| 2226 | } |
| 2227 | break; |
| 2228 | case MachO::BIND_OPCODE_SET_TYPE_IMM: |
| 2229 | BindType = ImmValue; |
| 2230 | DEBUG_WITH_TYPE( |
| 2231 | "mach-o-bind", |
| 2232 | llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: " |
| 2233 | << "BindType=" << (int)BindType << "\n"); |
| 2234 | break; |
| 2235 | case MachO::BIND_OPCODE_SET_ADDEND_SLEB: |
| 2236 | Addend = readSLEB128(); |
| 2237 | if (TableKind == Kind::Lazy) |
| 2238 | Malformed = true; |
| 2239 | DEBUG_WITH_TYPE( |
| 2240 | "mach-o-bind", |
| 2241 | llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: " |
| 2242 | << "Addend=" << Addend << "\n"); |
| 2243 | break; |
| 2244 | case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: |
| 2245 | SegmentIndex = ImmValue; |
| 2246 | SegmentOffset = readULEB128(); |
| 2247 | DEBUG_WITH_TYPE( |
| 2248 | "mach-o-bind", |
| 2249 | llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: " |
| 2250 | << "SegmentIndex=" << SegmentIndex << ", " |
| 2251 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2252 | << "\n"); |
| 2253 | break; |
| 2254 | case MachO::BIND_OPCODE_ADD_ADDR_ULEB: |
| 2255 | SegmentOffset += readULEB128(); |
| 2256 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2257 | llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: " |
| 2258 | << format("SegmentOffset=0x%06X", |
| 2259 | SegmentOffset) << "\n"); |
| 2260 | break; |
| 2261 | case MachO::BIND_OPCODE_DO_BIND: |
| 2262 | AdvanceAmount = PointerSize; |
| 2263 | RemainingLoopCount = 0; |
| 2264 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2265 | llvm::dbgs() << "BIND_OPCODE_DO_BIND: " |
| 2266 | << format("SegmentOffset=0x%06X", |
| 2267 | SegmentOffset) << "\n"); |
| 2268 | return; |
| 2269 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2270 | AdvanceAmount = readULEB128() + PointerSize; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2271 | RemainingLoopCount = 0; |
| 2272 | if (TableKind == Kind::Lazy) |
| 2273 | Malformed = true; |
| 2274 | DEBUG_WITH_TYPE( |
| 2275 | "mach-o-bind", |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2276 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: " |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2277 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2278 | << ", AdvanceAmount=" << AdvanceAmount |
| 2279 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2280 | << "\n"); |
| 2281 | return; |
| 2282 | case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: |
Nick Kledzik | 3b2aa05 | 2014-10-18 01:21:02 +0000 | [diff] [blame] | 2283 | AdvanceAmount = ImmValue * PointerSize + PointerSize; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2284 | RemainingLoopCount = 0; |
| 2285 | if (TableKind == Kind::Lazy) |
| 2286 | Malformed = true; |
| 2287 | DEBUG_WITH_TYPE("mach-o-bind", |
| 2288 | llvm::dbgs() |
| 2289 | << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: " |
| 2290 | << format("SegmentOffset=0x%06X", |
| 2291 | SegmentOffset) << "\n"); |
| 2292 | return; |
| 2293 | case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: |
| 2294 | RemainingLoopCount = readULEB128() - 1; |
| 2295 | AdvanceAmount = readULEB128() + PointerSize; |
| 2296 | if (TableKind == Kind::Lazy) |
| 2297 | Malformed = true; |
| 2298 | DEBUG_WITH_TYPE( |
| 2299 | "mach-o-bind", |
| 2300 | llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: " |
| 2301 | << format("SegmentOffset=0x%06X", SegmentOffset) |
| 2302 | << ", AdvanceAmount=" << AdvanceAmount |
| 2303 | << ", RemainingLoopCount=" << RemainingLoopCount |
| 2304 | << "\n"); |
| 2305 | return; |
| 2306 | default: |
| 2307 | Malformed = true; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | uint64_t MachOBindEntry::readULEB128() { |
| 2313 | unsigned Count; |
| 2314 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 2315 | Ptr += Count; |
| 2316 | if (Ptr > Opcodes.end()) { |
| 2317 | Ptr = Opcodes.end(); |
| 2318 | Malformed = true; |
| 2319 | } |
| 2320 | return Result; |
| 2321 | } |
| 2322 | |
| 2323 | int64_t MachOBindEntry::readSLEB128() { |
| 2324 | unsigned Count; |
| 2325 | int64_t Result = decodeSLEB128(Ptr, &Count); |
| 2326 | Ptr += Count; |
| 2327 | if (Ptr > Opcodes.end()) { |
| 2328 | Ptr = Opcodes.end(); |
| 2329 | Malformed = true; |
| 2330 | } |
| 2331 | return Result; |
| 2332 | } |
| 2333 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2334 | uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; } |
| 2335 | |
| 2336 | uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; } |
| 2337 | |
| 2338 | StringRef MachOBindEntry::typeName() const { |
| 2339 | switch (BindType) { |
| 2340 | case MachO::BIND_TYPE_POINTER: |
| 2341 | return "pointer"; |
| 2342 | case MachO::BIND_TYPE_TEXT_ABSOLUTE32: |
| 2343 | return "text abs32"; |
| 2344 | case MachO::BIND_TYPE_TEXT_PCREL32: |
| 2345 | return "text rel32"; |
| 2346 | } |
| 2347 | return "unknown"; |
| 2348 | } |
| 2349 | |
| 2350 | StringRef MachOBindEntry::symbolName() const { return SymbolName; } |
| 2351 | |
| 2352 | int64_t MachOBindEntry::addend() const { return Addend; } |
| 2353 | |
| 2354 | uint32_t MachOBindEntry::flags() const { return Flags; } |
| 2355 | |
| 2356 | int MachOBindEntry::ordinal() const { return Ordinal; } |
| 2357 | |
| 2358 | bool MachOBindEntry::operator==(const MachOBindEntry &Other) const { |
| 2359 | assert(Opcodes == Other.Opcodes && "compare iterators of different files"); |
| 2360 | return (Ptr == Other.Ptr) && |
| 2361 | (RemainingLoopCount == Other.RemainingLoopCount) && |
| 2362 | (Done == Other.Done); |
| 2363 | } |
| 2364 | |
| 2365 | iterator_range<bind_iterator> |
| 2366 | MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64, |
| 2367 | MachOBindEntry::Kind BKind) { |
| 2368 | MachOBindEntry Start(Opcodes, is64, BKind); |
| 2369 | Start.moveToFirst(); |
| 2370 | |
| 2371 | MachOBindEntry Finish(Opcodes, is64, BKind); |
| 2372 | Finish.moveToEnd(); |
| 2373 | |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2374 | return make_range(bind_iterator(Start), bind_iterator(Finish)); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | iterator_range<bind_iterator> MachOObjectFile::bindTable() const { |
| 2378 | return bindTable(getDyldInfoBindOpcodes(), is64Bit(), |
| 2379 | MachOBindEntry::Kind::Regular); |
| 2380 | } |
| 2381 | |
| 2382 | iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const { |
| 2383 | return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(), |
| 2384 | MachOBindEntry::Kind::Lazy); |
| 2385 | } |
| 2386 | |
| 2387 | iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const { |
| 2388 | return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(), |
| 2389 | MachOBindEntry::Kind::Weak); |
| 2390 | } |
| 2391 | |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 2392 | MachOObjectFile::load_command_iterator |
| 2393 | MachOObjectFile::begin_load_commands() const { |
| 2394 | return LoadCommands.begin(); |
| 2395 | } |
| 2396 | |
| 2397 | MachOObjectFile::load_command_iterator |
| 2398 | MachOObjectFile::end_load_commands() const { |
| 2399 | return LoadCommands.end(); |
| 2400 | } |
| 2401 | |
| 2402 | iterator_range<MachOObjectFile::load_command_iterator> |
| 2403 | MachOObjectFile::load_commands() const { |
Craig Topper | 15576e1 | 2015-12-06 05:08:07 +0000 | [diff] [blame] | 2404 | return make_range(begin_load_commands(), end_load_commands()); |
Alexey Samsonov | d319c4f | 2015-06-03 22:19:36 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2407 | StringRef |
| 2408 | MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const { |
| 2409 | ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec); |
| 2410 | return parseSegmentOrSectionName(Raw.data()); |
| 2411 | } |
| 2412 | |
| 2413 | ArrayRef<char> |
| 2414 | MachOObjectFile::getSectionRawName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 2415 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2416 | const section_base *Base = |
| 2417 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 2418 | return makeArrayRef(Base->sectname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | ArrayRef<char> |
| 2422 | MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const { |
Rafael Espindola | 0d85d10 | 2015-05-22 14:59:27 +0000 | [diff] [blame] | 2423 | assert(Sec.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2424 | const section_base *Base = |
| 2425 | reinterpret_cast<const section_base *>(Sections[Sec.d.a]); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 2426 | return makeArrayRef(Base->segname); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | bool |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2430 | MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2431 | const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2432 | if (getCPUType(this) == MachO::CPU_TYPE_X86_64) |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2433 | return false; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2434 | return getPlainRelocationAddress(RE) & MachO::R_SCATTERED; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2437 | unsigned MachOObjectFile::getPlainRelocationSymbolNum( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2438 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2439 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2440 | return RE.r_word1 & 0xffffff; |
| 2441 | return RE.r_word1 >> 8; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2444 | bool MachOObjectFile::getPlainRelocationExternal( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2445 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2446 | if (isLittleEndian()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2447 | return (RE.r_word1 >> 27) & 1; |
| 2448 | return (RE.r_word1 >> 4) & 1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2451 | bool MachOObjectFile::getScatteredRelocationScattered( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2452 | const MachO::any_relocation_info &RE) const { |
| 2453 | return RE.r_word0 >> 31; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2456 | uint32_t MachOObjectFile::getScatteredRelocationValue( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2457 | const MachO::any_relocation_info &RE) const { |
| 2458 | return RE.r_word1; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 2461 | uint32_t MachOObjectFile::getScatteredRelocationType( |
| 2462 | const MachO::any_relocation_info &RE) const { |
| 2463 | return (RE.r_word0 >> 24) & 0xf; |
| 2464 | } |
| 2465 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2466 | unsigned MachOObjectFile::getAnyRelocationAddress( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2467 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2468 | if (isRelocationScattered(RE)) |
| 2469 | return getScatteredRelocationAddress(RE); |
| 2470 | return getPlainRelocationAddress(RE); |
| 2471 | } |
| 2472 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2473 | unsigned MachOObjectFile::getAnyRelocationPCRel( |
| 2474 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2475 | if (isRelocationScattered(RE)) |
| 2476 | return getScatteredRelocationPCRel(this, RE); |
| 2477 | return getPlainRelocationPCRel(this, RE); |
| 2478 | } |
| 2479 | |
Eric Christopher | 1d62c25 | 2013-07-22 22:25:07 +0000 | [diff] [blame] | 2480 | unsigned MachOObjectFile::getAnyRelocationLength( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2481 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2482 | if (isRelocationScattered(RE)) |
| 2483 | return getScatteredRelocationLength(RE); |
| 2484 | return getPlainRelocationLength(this, RE); |
| 2485 | } |
| 2486 | |
| 2487 | unsigned |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2488 | MachOObjectFile::getAnyRelocationType( |
| 2489 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2490 | if (isRelocationScattered(RE)) |
| 2491 | return getScatteredRelocationType(RE); |
| 2492 | return getPlainRelocationType(this, RE); |
| 2493 | } |
| 2494 | |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2495 | SectionRef |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 2496 | MachOObjectFile::getAnyRelocationSection( |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2497 | const MachO::any_relocation_info &RE) const { |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2498 | if (isRelocationScattered(RE) || getPlainRelocationExternal(RE)) |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 2499 | return *section_end(); |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 2500 | unsigned SecNum = getPlainRelocationSymbolNum(RE); |
| 2501 | if (SecNum == MachO::R_ABS || SecNum > Sections.size()) |
| 2502 | return *section_end(); |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2503 | DataRefImpl DRI; |
Rafael Espindola | 9ac06a0 | 2015-06-18 22:38:20 +0000 | [diff] [blame] | 2504 | DRI.d.a = SecNum - 1; |
Rafael Espindola | 5250103 | 2013-04-30 15:40:54 +0000 | [diff] [blame] | 2505 | return SectionRef(DRI, this); |
| 2506 | } |
| 2507 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2508 | MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 2509 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2510 | return getStruct<MachO::section>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2513 | MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const { |
Rafael Espindola | 62a07cb | 2015-05-22 15:43:00 +0000 | [diff] [blame] | 2514 | assert(DRI.d.a < Sections.size() && "Should have detected this earlier"); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2515 | return getStruct<MachO::section_64>(this, Sections[DRI.d.a]); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2518 | MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L, |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2519 | unsigned Index) const { |
| 2520 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2521 | return getStruct<MachO::section>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2524 | MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L, |
| 2525 | unsigned Index) const { |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2526 | const char *Sec = getSectionPtr(this, L, Index); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2527 | return getStruct<MachO::section_64>(this, Sec); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2530 | MachO::nlist |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2531 | MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2532 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2533 | return getStruct<MachO::nlist>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2536 | MachO::nlist_64 |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2537 | MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { |
Rafael Espindola | 75c3036 | 2013-04-24 19:47:55 +0000 | [diff] [blame] | 2538 | const char *P = reinterpret_cast<const char *>(DRI.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2539 | return getStruct<MachO::nlist_64>(this, P); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2542 | MachO::linkedit_data_command |
| 2543 | MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const { |
| 2544 | return getStruct<MachO::linkedit_data_command>(this, L.Ptr); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2547 | MachO::segment_command |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2548 | MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2549 | return getStruct<MachO::segment_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2552 | MachO::segment_command_64 |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2553 | MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2554 | return getStruct<MachO::segment_command_64>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 2557 | MachO::linker_option_command |
| 2558 | MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const { |
| 2559 | return getStruct<MachO::linker_option_command>(this, L.Ptr); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 2562 | MachO::version_min_command |
| 2563 | MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const { |
| 2564 | return getStruct<MachO::version_min_command>(this, L.Ptr); |
| 2565 | } |
| 2566 | |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 2567 | MachO::dylib_command |
| 2568 | MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const { |
| 2569 | return getStruct<MachO::dylib_command>(this, L.Ptr); |
| 2570 | } |
| 2571 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2572 | MachO::dyld_info_command |
| 2573 | MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const { |
| 2574 | return getStruct<MachO::dyld_info_command>(this, L.Ptr); |
| 2575 | } |
| 2576 | |
| 2577 | MachO::dylinker_command |
| 2578 | MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const { |
| 2579 | return getStruct<MachO::dylinker_command>(this, L.Ptr); |
| 2580 | } |
| 2581 | |
| 2582 | MachO::uuid_command |
| 2583 | MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const { |
| 2584 | return getStruct<MachO::uuid_command>(this, L.Ptr); |
| 2585 | } |
| 2586 | |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 2587 | MachO::rpath_command |
| 2588 | MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const { |
| 2589 | return getStruct<MachO::rpath_command>(this, L.Ptr); |
| 2590 | } |
| 2591 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2592 | MachO::source_version_command |
| 2593 | MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const { |
| 2594 | return getStruct<MachO::source_version_command>(this, L.Ptr); |
| 2595 | } |
| 2596 | |
| 2597 | MachO::entry_point_command |
| 2598 | MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const { |
| 2599 | return getStruct<MachO::entry_point_command>(this, L.Ptr); |
| 2600 | } |
| 2601 | |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 2602 | MachO::encryption_info_command |
| 2603 | MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const { |
| 2604 | return getStruct<MachO::encryption_info_command>(this, L.Ptr); |
| 2605 | } |
| 2606 | |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 2607 | MachO::encryption_info_command_64 |
| 2608 | MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const { |
| 2609 | return getStruct<MachO::encryption_info_command_64>(this, L.Ptr); |
| 2610 | } |
| 2611 | |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 2612 | MachO::sub_framework_command |
| 2613 | MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const { |
| 2614 | return getStruct<MachO::sub_framework_command>(this, L.Ptr); |
| 2615 | } |
Tim Northover | 8f9590b | 2014-06-30 14:40:57 +0000 | [diff] [blame] | 2616 | |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 2617 | MachO::sub_umbrella_command |
| 2618 | MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const { |
| 2619 | return getStruct<MachO::sub_umbrella_command>(this, L.Ptr); |
| 2620 | } |
| 2621 | |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 2622 | MachO::sub_library_command |
| 2623 | MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const { |
| 2624 | return getStruct<MachO::sub_library_command>(this, L.Ptr); |
| 2625 | } |
| 2626 | |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 2627 | MachO::sub_client_command |
| 2628 | MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const { |
| 2629 | return getStruct<MachO::sub_client_command>(this, L.Ptr); |
| 2630 | } |
| 2631 | |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 2632 | MachO::routines_command |
| 2633 | MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const { |
| 2634 | return getStruct<MachO::routines_command>(this, L.Ptr); |
| 2635 | } |
| 2636 | |
| 2637 | MachO::routines_command_64 |
| 2638 | MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const { |
| 2639 | return getStruct<MachO::routines_command_64>(this, L.Ptr); |
| 2640 | } |
| 2641 | |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 2642 | MachO::thread_command |
| 2643 | MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const { |
| 2644 | return getStruct<MachO::thread_command>(this, L.Ptr); |
| 2645 | } |
| 2646 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2647 | MachO::any_relocation_info |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2648 | MachOObjectFile::getRelocation(DataRefImpl Rel) const { |
Rafael Espindola | 128b811 | 2014-04-03 23:51:28 +0000 | [diff] [blame] | 2649 | DataRefImpl Sec; |
| 2650 | Sec.d.a = Rel.d.a; |
| 2651 | uint32_t Offset; |
| 2652 | if (is64Bit()) { |
| 2653 | MachO::section_64 Sect = getSection64(Sec); |
| 2654 | Offset = Sect.reloff; |
| 2655 | } else { |
| 2656 | MachO::section Sect = getSection(Sec); |
| 2657 | Offset = Sect.reloff; |
| 2658 | } |
| 2659 | |
| 2660 | auto P = reinterpret_cast<const MachO::any_relocation_info *>( |
| 2661 | getPtr(this, Offset)) + Rel.d.b; |
| 2662 | return getStruct<MachO::any_relocation_info>( |
| 2663 | this, reinterpret_cast<const char *>(P)); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2666 | MachO::data_in_code_entry |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2667 | MachOObjectFile::getDice(DataRefImpl Rel) const { |
| 2668 | const char *P = reinterpret_cast<const char *>(Rel.p); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2669 | return getStruct<MachO::data_in_code_entry>(this, P); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 2672 | const MachO::mach_header &MachOObjectFile::getHeader() const { |
Alexey Samsonov | fa5edc5 | 2015-06-04 22:49:55 +0000 | [diff] [blame] | 2673 | return Header; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Alexey Samsonov | 13415ed | 2015-06-04 19:22:03 +0000 | [diff] [blame] | 2676 | const MachO::mach_header_64 &MachOObjectFile::getHeader64() const { |
| 2677 | assert(is64Bit()); |
| 2678 | return Header64; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2681 | uint32_t MachOObjectFile::getIndirectSymbolTableEntry( |
| 2682 | const MachO::dysymtab_command &DLC, |
| 2683 | unsigned Index) const { |
| 2684 | uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t); |
| 2685 | return getStruct<uint32_t>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2688 | MachO::data_in_code_entry |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2689 | MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset, |
| 2690 | unsigned Index) const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2691 | uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry); |
| 2692 | return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset)); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2695 | MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2696 | if (SymtabLoadCmd) |
| 2697 | return getStruct<MachO::symtab_command>(this, SymtabLoadCmd); |
| 2698 | |
| 2699 | // If there is no SymtabLoadCmd return a load command with zero'ed fields. |
| 2700 | MachO::symtab_command Cmd; |
| 2701 | Cmd.cmd = MachO::LC_SYMTAB; |
| 2702 | Cmd.cmdsize = sizeof(MachO::symtab_command); |
| 2703 | Cmd.symoff = 0; |
| 2704 | Cmd.nsyms = 0; |
| 2705 | Cmd.stroff = 0; |
| 2706 | Cmd.strsize = 0; |
| 2707 | return Cmd; |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2710 | MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2711 | if (DysymtabLoadCmd) |
| 2712 | return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd); |
| 2713 | |
| 2714 | // If there is no DysymtabLoadCmd return a load command with zero'ed fields. |
| 2715 | MachO::dysymtab_command Cmd; |
| 2716 | Cmd.cmd = MachO::LC_DYSYMTAB; |
| 2717 | Cmd.cmdsize = sizeof(MachO::dysymtab_command); |
| 2718 | Cmd.ilocalsym = 0; |
| 2719 | Cmd.nlocalsym = 0; |
| 2720 | Cmd.iextdefsym = 0; |
| 2721 | Cmd.nextdefsym = 0; |
| 2722 | Cmd.iundefsym = 0; |
| 2723 | Cmd.nundefsym = 0; |
| 2724 | Cmd.tocoff = 0; |
| 2725 | Cmd.ntoc = 0; |
| 2726 | Cmd.modtaboff = 0; |
| 2727 | Cmd.nmodtab = 0; |
| 2728 | Cmd.extrefsymoff = 0; |
| 2729 | Cmd.nextrefsyms = 0; |
| 2730 | Cmd.indirectsymoff = 0; |
| 2731 | Cmd.nindirectsyms = 0; |
| 2732 | Cmd.extreloff = 0; |
| 2733 | Cmd.nextrel = 0; |
| 2734 | Cmd.locreloff = 0; |
| 2735 | Cmd.nlocrel = 0; |
| 2736 | return Cmd; |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2739 | MachO::linkedit_data_command |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2740 | MachOObjectFile::getDataInCodeLoadCommand() const { |
| 2741 | if (DataInCodeLoadCmd) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2742 | return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2743 | |
| 2744 | // 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] | 2745 | MachO::linkedit_data_command Cmd; |
| 2746 | Cmd.cmd = MachO::LC_DATA_IN_CODE; |
| 2747 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 2748 | Cmd.dataoff = 0; |
| 2749 | Cmd.datasize = 0; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2750 | return Cmd; |
| 2751 | } |
| 2752 | |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 2753 | MachO::linkedit_data_command |
| 2754 | MachOObjectFile::getLinkOptHintsLoadCommand() const { |
| 2755 | if (LinkOptHintsLoadCmd) |
| 2756 | return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd); |
| 2757 | |
| 2758 | // If there is no LinkOptHintsLoadCmd return a load command with zero'ed |
| 2759 | // fields. |
| 2760 | MachO::linkedit_data_command Cmd; |
| 2761 | Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT; |
| 2762 | Cmd.cmdsize = sizeof(MachO::linkedit_data_command); |
| 2763 | Cmd.dataoff = 0; |
| 2764 | Cmd.datasize = 0; |
| 2765 | return Cmd; |
| 2766 | } |
| 2767 | |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2768 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2769 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2770 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2771 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2772 | MachO::dyld_info_command DyldInfo = |
| 2773 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 2774 | const uint8_t *Ptr = |
| 2775 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.rebase_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2776 | return makeArrayRef(Ptr, DyldInfo.rebase_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2780 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2781 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2782 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2783 | MachO::dyld_info_command DyldInfo = |
| 2784 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 2785 | const uint8_t *Ptr = |
| 2786 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2787 | return makeArrayRef(Ptr, DyldInfo.bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2791 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2792 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2793 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2794 | MachO::dyld_info_command DyldInfo = |
| 2795 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 2796 | const uint8_t *Ptr = |
| 2797 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.weak_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2798 | return makeArrayRef(Ptr, DyldInfo.weak_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2802 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2803 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2804 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2805 | MachO::dyld_info_command DyldInfo = |
| 2806 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 2807 | const uint8_t *Ptr = |
| 2808 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.lazy_bind_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2809 | return makeArrayRef(Ptr, DyldInfo.lazy_bind_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const { |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 2813 | if (!DyldInfoLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2814 | return None; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2815 | |
NAKAMURA Takumi | 70ad98a | 2015-09-22 11:13:55 +0000 | [diff] [blame] | 2816 | MachO::dyld_info_command DyldInfo = |
| 2817 | getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd); |
| 2818 | const uint8_t *Ptr = |
| 2819 | reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.export_off)); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2820 | return makeArrayRef(Ptr, DyldInfo.export_size); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 2823 | ArrayRef<uint8_t> MachOObjectFile::getUuid() const { |
| 2824 | if (!UuidLoadCmd) |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2825 | return None; |
Benjamin Kramer | 014601d | 2014-10-24 15:52:05 +0000 | [diff] [blame] | 2826 | // Returning a pointer is fine as uuid doesn't need endian swapping. |
| 2827 | const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid); |
Craig Topper | 0013be1 | 2015-09-21 05:32:41 +0000 | [diff] [blame] | 2828 | return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16); |
Alexander Potapenko | 6909b5b | 2014-10-15 23:35:45 +0000 | [diff] [blame] | 2829 | } |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2830 | |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2831 | StringRef MachOObjectFile::getStringTableData() const { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 2832 | MachO::symtab_command S = getSymtabLoadCommand(); |
| 2833 | return getData().substr(S.stroff, S.strsize); |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2836 | bool MachOObjectFile::is64Bit() const { |
| 2837 | return getType() == getMachOType(false, true) || |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 2838 | getType() == getMachOType(true, true); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | void MachOObjectFile::ReadULEB128s(uint64_t Index, |
| 2842 | SmallVectorImpl<uint64_t> &Out) const { |
| 2843 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 2844 | |
| 2845 | uint32_t offset = Index; |
| 2846 | uint64_t data = 0; |
| 2847 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 2848 | data += delta; |
| 2849 | Out.push_back(data); |
| 2850 | } |
| 2851 | } |
| 2852 | |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 2853 | bool MachOObjectFile::isRelocatableObject() const { |
| 2854 | return getHeader().filetype == MachO::MH_OBJECT; |
| 2855 | } |
| 2856 | |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 2857 | Expected<std::unique_ptr<MachOObjectFile>> |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 2858 | ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) { |
| 2859 | StringRef Magic = Buffer.getBuffer().slice(0, 4); |
Lang Hames | 8262764 | 2016-03-25 21:59:14 +0000 | [diff] [blame] | 2860 | if (Magic == "\xFE\xED\xFA\xCE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 2861 | return MachOObjectFile::create(Buffer, false, false); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 2862 | if (Magic == "\xCE\xFA\xED\xFE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 2863 | return MachOObjectFile::create(Buffer, true, false); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 2864 | if (Magic == "\xFE\xED\xFA\xCF") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 2865 | return MachOObjectFile::create(Buffer, false, true); |
David Blaikie | b805f73 | 2016-03-28 17:45:48 +0000 | [diff] [blame] | 2866 | if (Magic == "\xCF\xFA\xED\xFE") |
Lang Hames | ff044b1 | 2016-03-25 23:11:52 +0000 | [diff] [blame] | 2867 | return MachOObjectFile::create(Buffer, true, true); |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 2868 | return make_error<GenericBinaryError>("Unrecognized MachO magic number", |
Justin Bogner | 2a42da9 | 2016-05-05 23:59:57 +0000 | [diff] [blame] | 2869 | object_error::invalid_file_type); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 2870 | } |