Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectFileMachO.cpp -------------------------------------*- 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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" |
| 13 | #include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h" |
| 14 | #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" |
| 15 | #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" |
Jason Molenda | f6ce26f | 2013-04-10 05:58:57 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/FileSpecList.h" |
| 18 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 1eac0c7 | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 21 | #include "lldb/Core/RangeMap.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Section.h" |
| 23 | #include "lldb/Core/StreamFile.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Host.h" |
Jason Molenda | 5635f77 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/ObjectFile.h" |
Jason Molenda | 13becd4 | 2016-07-29 00:18:39 +0000 | [diff] [blame] | 27 | #include "lldb/Target/DynamicLoader.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 28 | #include "lldb/Target/MemoryRegionInfo.h" |
Greg Clayton | 26b47e2 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Platform.h" |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Process.h" |
Greg Clayton | 7524e09 | 2014-02-06 20:10:16 +0000 | [diff] [blame] | 31 | #include "lldb/Target/SectionLoadList.h" |
Greg Clayton | 26b47e2 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Target.h" |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Thread.h" |
| 34 | #include "lldb/Target/ThreadList.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 35 | #include "lldb/Utility/ArchSpec.h" |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 36 | #include "lldb/Utility/DataBuffer.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 37 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 38 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 39 | #include "lldb/Utility/RegisterValue.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 40 | #include "lldb/Utility/Status.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 41 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/Timer.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/UUID.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
Pavel Labath | 7704473 | 2018-09-12 12:26:05 +0000 | [diff] [blame] | 45 | #include "lldb/Host/SafeMachO.h" |
Jim Ingham | 46d005d | 2014-04-02 22:53:21 +0000 | [diff] [blame] | 46 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 47 | #include "llvm/Support/MemoryBuffer.h" |
| 48 | |
Jim Ingham | 46d005d | 2014-04-02 22:53:21 +0000 | [diff] [blame] | 49 | #include "ObjectFileMachO.h" |
| 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | #if defined(__APPLE__) && \ |
| 52 | (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
Jason Molenda | 0e0954c | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 53 | // GetLLDBSharedCacheUUID() needs to call dlsym() |
| 54 | #include <dlfcn.h> |
| 55 | #endif |
| 56 | |
Daniel Malea | ffeb4b6 | 2013-04-17 19:24:22 +0000 | [diff] [blame] | 57 | #ifndef __APPLE__ |
| 58 | #include "Utility/UuidCompatibility.h" |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 59 | #else |
| 60 | #include <uuid/uuid.h> |
Daniel Malea | ffeb4b6 | 2013-04-17 19:24:22 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Greg Clayton | b887da1 | 2015-07-16 19:50:57 +0000 | [diff] [blame] | 63 | #define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | using namespace lldb; |
| 65 | using namespace lldb_private; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 66 | using namespace llvm::MachO; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 68 | // Some structure definitions needed for parsing the dyld shared cache files |
| 69 | // found on iOS devices. |
| 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | struct lldb_copy_dyld_cache_header_v1 { |
| 72 | char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. |
| 73 | uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info |
| 74 | uint32_t mappingCount; // number of dyld_cache_mapping_info entries |
| 75 | uint32_t imagesOffset; |
| 76 | uint32_t imagesCount; |
| 77 | uint64_t dyldBaseAddress; |
| 78 | uint64_t codeSignatureOffset; |
| 79 | uint64_t codeSignatureSize; |
| 80 | uint64_t slideInfoOffset; |
| 81 | uint64_t slideInfoSize; |
| 82 | uint64_t localSymbolsOffset; |
| 83 | uint64_t localSymbolsSize; |
| 84 | uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 |
| 85 | // and later |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | struct lldb_copy_dyld_cache_mapping_info { |
| 89 | uint64_t address; |
| 90 | uint64_t size; |
| 91 | uint64_t fileOffset; |
| 92 | uint32_t maxProt; |
| 93 | uint32_t initProt; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | struct lldb_copy_dyld_cache_local_symbols_info { |
| 97 | uint32_t nlistOffset; |
| 98 | uint32_t nlistCount; |
| 99 | uint32_t stringsOffset; |
| 100 | uint32_t stringsSize; |
| 101 | uint32_t entriesOffset; |
| 102 | uint32_t entriesCount; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 103 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | struct lldb_copy_dyld_cache_local_symbols_entry { |
| 105 | uint32_t dylibOffset; |
| 106 | uint32_t nlistStartIndex; |
| 107 | uint32_t nlistCount; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 111 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | RegisterContextDarwin_x86_64_Mach(lldb_private::Thread &thread, |
| 113 | const DataExtractor &data) |
| 114 | : RegisterContextDarwin_x86_64(thread, 0) { |
| 115 | SetRegisterDataFrom_LC_THREAD(data); |
| 116 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | void InvalidateAllRegisters() override { |
| 119 | // Do nothing... registers are always valid... |
| 120 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { |
| 123 | lldb::offset_t offset = 0; |
| 124 | SetError(GPRRegSet, Read, -1); |
| 125 | SetError(FPURegSet, Read, -1); |
| 126 | SetError(EXCRegSet, Read, -1); |
| 127 | bool done = false; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | while (!done) { |
| 130 | int flavor = data.GetU32(&offset); |
| 131 | if (flavor == 0) |
| 132 | done = true; |
| 133 | else { |
| 134 | uint32_t i; |
| 135 | uint32_t count = data.GetU32(&offset); |
| 136 | switch (flavor) { |
| 137 | case GPRRegSet: |
| 138 | for (i = 0; i < count; ++i) |
| 139 | (&gpr.rax)[i] = data.GetU64(&offset); |
| 140 | SetError(GPRRegSet, Read, 0); |
| 141 | done = true; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | break; |
| 144 | case FPURegSet: |
| 145 | // TODO: fill in FPU regs.... |
| 146 | // SetError (FPURegSet, Read, -1); |
| 147 | done = true; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | break; |
| 150 | case EXCRegSet: |
| 151 | exc.trapno = data.GetU32(&offset); |
| 152 | exc.err = data.GetU32(&offset); |
| 153 | exc.faultvaddr = data.GetU64(&offset); |
| 154 | SetError(EXCRegSet, Read, 0); |
| 155 | done = true; |
| 156 | break; |
| 157 | case 7: |
| 158 | case 8: |
| 159 | case 9: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 160 | // fancy flavors that encapsulate of the above flavors... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 162 | |
Greg Clayton | 9aae0a1 | 2013-05-15 19:52:08 +0000 | [diff] [blame] | 163 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | done = true; |
| 165 | break; |
| 166 | } |
| 167 | } |
Greg Clayton | 9aae0a1 | 2013-05-15 19:52:08 +0000 | [diff] [blame] | 168 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static size_t WriteRegister(RegisterContext *reg_ctx, const char *name, |
| 172 | const char *alt_name, size_t reg_byte_size, |
| 173 | Stream &data) { |
| 174 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name); |
| 175 | if (reg_info == NULL) |
| 176 | reg_info = reg_ctx->GetRegisterInfoByName(alt_name); |
| 177 | if (reg_info) { |
| 178 | lldb_private::RegisterValue reg_value; |
| 179 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
| 180 | if (reg_info->byte_size >= reg_byte_size) |
| 181 | data.Write(reg_value.GetBytes(), reg_byte_size); |
| 182 | else { |
| 183 | data.Write(reg_value.GetBytes(), reg_info->byte_size); |
| 184 | for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; |
| 185 | ++i) |
| 186 | data.PutChar(0); |
| 187 | } |
| 188 | return reg_byte_size; |
| 189 | } |
| 190 | } |
| 191 | // Just write zeros if all else fails |
| 192 | for (size_t i = 0; i < reg_byte_size; ++i) |
| 193 | data.PutChar(0); |
| 194 | return reg_byte_size; |
| 195 | } |
| 196 | |
| 197 | static bool Create_LC_THREAD(Thread *thread, Stream &data) { |
| 198 | RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); |
| 199 | if (reg_ctx_sp) { |
| 200 | RegisterContext *reg_ctx = reg_ctx_sp.get(); |
| 201 | |
| 202 | data.PutHex32(GPRRegSet); // Flavor |
| 203 | data.PutHex32(GPRWordCount); |
| 204 | WriteRegister(reg_ctx, "rax", NULL, 8, data); |
| 205 | WriteRegister(reg_ctx, "rbx", NULL, 8, data); |
| 206 | WriteRegister(reg_ctx, "rcx", NULL, 8, data); |
| 207 | WriteRegister(reg_ctx, "rdx", NULL, 8, data); |
| 208 | WriteRegister(reg_ctx, "rdi", NULL, 8, data); |
| 209 | WriteRegister(reg_ctx, "rsi", NULL, 8, data); |
| 210 | WriteRegister(reg_ctx, "rbp", NULL, 8, data); |
| 211 | WriteRegister(reg_ctx, "rsp", NULL, 8, data); |
| 212 | WriteRegister(reg_ctx, "r8", NULL, 8, data); |
| 213 | WriteRegister(reg_ctx, "r9", NULL, 8, data); |
| 214 | WriteRegister(reg_ctx, "r10", NULL, 8, data); |
| 215 | WriteRegister(reg_ctx, "r11", NULL, 8, data); |
| 216 | WriteRegister(reg_ctx, "r12", NULL, 8, data); |
| 217 | WriteRegister(reg_ctx, "r13", NULL, 8, data); |
| 218 | WriteRegister(reg_ctx, "r14", NULL, 8, data); |
| 219 | WriteRegister(reg_ctx, "r15", NULL, 8, data); |
| 220 | WriteRegister(reg_ctx, "rip", NULL, 8, data); |
| 221 | WriteRegister(reg_ctx, "rflags", NULL, 8, data); |
| 222 | WriteRegister(reg_ctx, "cs", NULL, 8, data); |
| 223 | WriteRegister(reg_ctx, "fs", NULL, 8, data); |
| 224 | WriteRegister(reg_ctx, "gs", NULL, 8, data); |
| 225 | |
| 226 | // // Write out the FPU registers |
| 227 | // const size_t fpu_byte_size = sizeof(FPU); |
| 228 | // size_t bytes_written = 0; |
| 229 | // data.PutHex32 (FPURegSet); |
| 230 | // data.PutHex32 (fpu_byte_size/sizeof(uint64_t)); |
| 231 | // bytes_written += data.PutHex32(0); // uint32_t pad[0] |
| 232 | // bytes_written += data.PutHex32(0); // uint32_t pad[1] |
| 233 | // bytes_written += WriteRegister (reg_ctx, "fcw", "fctrl", 2, |
| 234 | // data); // uint16_t fcw; // "fctrl" |
| 235 | // bytes_written += WriteRegister (reg_ctx, "fsw" , "fstat", 2, |
| 236 | // data); // uint16_t fsw; // "fstat" |
| 237 | // bytes_written += WriteRegister (reg_ctx, "ftw" , "ftag", 1, |
| 238 | // data); // uint8_t ftw; // "ftag" |
| 239 | // bytes_written += data.PutHex8 (0); // uint8_t pad1; |
| 240 | // bytes_written += WriteRegister (reg_ctx, "fop" , NULL, 2, |
| 241 | // data); // uint16_t fop; // "fop" |
| 242 | // bytes_written += WriteRegister (reg_ctx, "fioff", "ip", 4, |
| 243 | // data); // uint32_t ip; // "fioff" |
| 244 | // bytes_written += WriteRegister (reg_ctx, "fiseg", NULL, 2, |
| 245 | // data); // uint16_t cs; // "fiseg" |
| 246 | // bytes_written += data.PutHex16 (0); // uint16_t pad2; |
| 247 | // bytes_written += WriteRegister (reg_ctx, "dp", "fooff" , 4, |
| 248 | // data); // uint32_t dp; // "fooff" |
| 249 | // bytes_written += WriteRegister (reg_ctx, "foseg", NULL, 2, |
| 250 | // data); // uint16_t ds; // "foseg" |
| 251 | // bytes_written += data.PutHex16 (0); // uint16_t pad3; |
| 252 | // bytes_written += WriteRegister (reg_ctx, "mxcsr", NULL, 4, |
| 253 | // data); // uint32_t mxcsr; |
| 254 | // bytes_written += WriteRegister (reg_ctx, "mxcsrmask", NULL, |
| 255 | // 4, data);// uint32_t mxcsrmask; |
| 256 | // bytes_written += WriteRegister (reg_ctx, "stmm0", NULL, |
| 257 | // sizeof(MMSReg), data); |
| 258 | // bytes_written += WriteRegister (reg_ctx, "stmm1", NULL, |
| 259 | // sizeof(MMSReg), data); |
| 260 | // bytes_written += WriteRegister (reg_ctx, "stmm2", NULL, |
| 261 | // sizeof(MMSReg), data); |
| 262 | // bytes_written += WriteRegister (reg_ctx, "stmm3", NULL, |
| 263 | // sizeof(MMSReg), data); |
| 264 | // bytes_written += WriteRegister (reg_ctx, "stmm4", NULL, |
| 265 | // sizeof(MMSReg), data); |
| 266 | // bytes_written += WriteRegister (reg_ctx, "stmm5", NULL, |
| 267 | // sizeof(MMSReg), data); |
| 268 | // bytes_written += WriteRegister (reg_ctx, "stmm6", NULL, |
| 269 | // sizeof(MMSReg), data); |
| 270 | // bytes_written += WriteRegister (reg_ctx, "stmm7", NULL, |
| 271 | // sizeof(MMSReg), data); |
| 272 | // bytes_written += WriteRegister (reg_ctx, "xmm0" , NULL, |
| 273 | // sizeof(XMMReg), data); |
| 274 | // bytes_written += WriteRegister (reg_ctx, "xmm1" , NULL, |
| 275 | // sizeof(XMMReg), data); |
| 276 | // bytes_written += WriteRegister (reg_ctx, "xmm2" , NULL, |
| 277 | // sizeof(XMMReg), data); |
| 278 | // bytes_written += WriteRegister (reg_ctx, "xmm3" , NULL, |
| 279 | // sizeof(XMMReg), data); |
| 280 | // bytes_written += WriteRegister (reg_ctx, "xmm4" , NULL, |
| 281 | // sizeof(XMMReg), data); |
| 282 | // bytes_written += WriteRegister (reg_ctx, "xmm5" , NULL, |
| 283 | // sizeof(XMMReg), data); |
| 284 | // bytes_written += WriteRegister (reg_ctx, "xmm6" , NULL, |
| 285 | // sizeof(XMMReg), data); |
| 286 | // bytes_written += WriteRegister (reg_ctx, "xmm7" , NULL, |
| 287 | // sizeof(XMMReg), data); |
| 288 | // bytes_written += WriteRegister (reg_ctx, "xmm8" , NULL, |
| 289 | // sizeof(XMMReg), data); |
| 290 | // bytes_written += WriteRegister (reg_ctx, "xmm9" , NULL, |
| 291 | // sizeof(XMMReg), data); |
| 292 | // bytes_written += WriteRegister (reg_ctx, "xmm10", NULL, |
| 293 | // sizeof(XMMReg), data); |
| 294 | // bytes_written += WriteRegister (reg_ctx, "xmm11", NULL, |
| 295 | // sizeof(XMMReg), data); |
| 296 | // bytes_written += WriteRegister (reg_ctx, "xmm12", NULL, |
| 297 | // sizeof(XMMReg), data); |
| 298 | // bytes_written += WriteRegister (reg_ctx, "xmm13", NULL, |
| 299 | // sizeof(XMMReg), data); |
| 300 | // bytes_written += WriteRegister (reg_ctx, "xmm14", NULL, |
| 301 | // sizeof(XMMReg), data); |
| 302 | // bytes_written += WriteRegister (reg_ctx, "xmm15", NULL, |
| 303 | // sizeof(XMMReg), data); |
| 304 | // |
| 305 | // // Fill rest with zeros |
| 306 | // for (size_t i=0, n = fpu_byte_size - bytes_written; i<n; ++ |
| 307 | // i) |
| 308 | // data.PutChar(0); |
| 309 | |
| 310 | // Write out the EXC registers |
| 311 | data.PutHex32(EXCRegSet); |
| 312 | data.PutHex32(EXCWordCount); |
| 313 | WriteRegister(reg_ctx, "trapno", NULL, 4, data); |
| 314 | WriteRegister(reg_ctx, "err", NULL, 4, data); |
| 315 | WriteRegister(reg_ctx, "faultvaddr", NULL, 8, data); |
| 316 | return true; |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | protected: |
| 322 | int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; } |
| 323 | |
| 324 | int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; } |
| 325 | |
| 326 | int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; } |
| 327 | |
| 328 | int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { |
Greg Clayton | 9aae0a1 | 2013-05-15 19:52:08 +0000 | [diff] [blame] | 329 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { |
| 337 | return 0; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 { |
| 342 | public: |
| 343 | RegisterContextDarwin_i386_Mach(lldb_private::Thread &thread, |
| 344 | const DataExtractor &data) |
| 345 | : RegisterContextDarwin_i386(thread, 0) { |
| 346 | SetRegisterDataFrom_LC_THREAD(data); |
| 347 | } |
| 348 | |
| 349 | void InvalidateAllRegisters() override { |
| 350 | // Do nothing... registers are always valid... |
| 351 | } |
| 352 | |
| 353 | void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { |
| 354 | lldb::offset_t offset = 0; |
| 355 | SetError(GPRRegSet, Read, -1); |
| 356 | SetError(FPURegSet, Read, -1); |
| 357 | SetError(EXCRegSet, Read, -1); |
| 358 | bool done = false; |
| 359 | |
| 360 | while (!done) { |
| 361 | int flavor = data.GetU32(&offset); |
| 362 | if (flavor == 0) |
| 363 | done = true; |
| 364 | else { |
| 365 | uint32_t i; |
| 366 | uint32_t count = data.GetU32(&offset); |
| 367 | switch (flavor) { |
| 368 | case GPRRegSet: |
| 369 | for (i = 0; i < count; ++i) |
| 370 | (&gpr.eax)[i] = data.GetU32(&offset); |
| 371 | SetError(GPRRegSet, Read, 0); |
| 372 | done = true; |
| 373 | |
| 374 | break; |
| 375 | case FPURegSet: |
| 376 | // TODO: fill in FPU regs.... |
| 377 | // SetError (FPURegSet, Read, -1); |
| 378 | done = true; |
| 379 | |
| 380 | break; |
| 381 | case EXCRegSet: |
| 382 | exc.trapno = data.GetU32(&offset); |
| 383 | exc.err = data.GetU32(&offset); |
| 384 | exc.faultvaddr = data.GetU32(&offset); |
| 385 | SetError(EXCRegSet, Read, 0); |
| 386 | done = true; |
| 387 | break; |
| 388 | case 7: |
| 389 | case 8: |
| 390 | case 9: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 391 | // fancy flavors that encapsulate of the above flavors... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | default: |
| 395 | done = true; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | static size_t WriteRegister(RegisterContext *reg_ctx, const char *name, |
| 403 | const char *alt_name, size_t reg_byte_size, |
| 404 | Stream &data) { |
| 405 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name); |
| 406 | if (reg_info == NULL) |
| 407 | reg_info = reg_ctx->GetRegisterInfoByName(alt_name); |
| 408 | if (reg_info) { |
| 409 | lldb_private::RegisterValue reg_value; |
| 410 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
| 411 | if (reg_info->byte_size >= reg_byte_size) |
| 412 | data.Write(reg_value.GetBytes(), reg_byte_size); |
| 413 | else { |
| 414 | data.Write(reg_value.GetBytes(), reg_info->byte_size); |
| 415 | for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; |
| 416 | ++i) |
| 417 | data.PutChar(0); |
| 418 | } |
| 419 | return reg_byte_size; |
| 420 | } |
| 421 | } |
| 422 | // Just write zeros if all else fails |
| 423 | for (size_t i = 0; i < reg_byte_size; ++i) |
| 424 | data.PutChar(0); |
| 425 | return reg_byte_size; |
| 426 | } |
| 427 | |
| 428 | static bool Create_LC_THREAD(Thread *thread, Stream &data) { |
| 429 | RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); |
| 430 | if (reg_ctx_sp) { |
| 431 | RegisterContext *reg_ctx = reg_ctx_sp.get(); |
| 432 | |
| 433 | data.PutHex32(GPRRegSet); // Flavor |
| 434 | data.PutHex32(GPRWordCount); |
| 435 | WriteRegister(reg_ctx, "eax", NULL, 4, data); |
| 436 | WriteRegister(reg_ctx, "ebx", NULL, 4, data); |
| 437 | WriteRegister(reg_ctx, "ecx", NULL, 4, data); |
| 438 | WriteRegister(reg_ctx, "edx", NULL, 4, data); |
| 439 | WriteRegister(reg_ctx, "edi", NULL, 4, data); |
| 440 | WriteRegister(reg_ctx, "esi", NULL, 4, data); |
| 441 | WriteRegister(reg_ctx, "ebp", NULL, 4, data); |
| 442 | WriteRegister(reg_ctx, "esp", NULL, 4, data); |
| 443 | WriteRegister(reg_ctx, "ss", NULL, 4, data); |
| 444 | WriteRegister(reg_ctx, "eflags", NULL, 4, data); |
| 445 | WriteRegister(reg_ctx, "eip", NULL, 4, data); |
| 446 | WriteRegister(reg_ctx, "cs", NULL, 4, data); |
| 447 | WriteRegister(reg_ctx, "ds", NULL, 4, data); |
| 448 | WriteRegister(reg_ctx, "es", NULL, 4, data); |
| 449 | WriteRegister(reg_ctx, "fs", NULL, 4, data); |
| 450 | WriteRegister(reg_ctx, "gs", NULL, 4, data); |
| 451 | |
| 452 | // Write out the EXC registers |
| 453 | data.PutHex32(EXCRegSet); |
| 454 | data.PutHex32(EXCWordCount); |
| 455 | WriteRegister(reg_ctx, "trapno", NULL, 4, data); |
| 456 | WriteRegister(reg_ctx, "err", NULL, 4, data); |
| 457 | WriteRegister(reg_ctx, "faultvaddr", NULL, 4, data); |
| 458 | return true; |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | protected: |
| 464 | int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; } |
| 465 | |
| 466 | int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; } |
| 467 | |
| 468 | int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; } |
| 469 | |
| 470 | int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { |
| 479 | return 0; |
| 480 | } |
| 481 | }; |
| 482 | |
| 483 | class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm { |
| 484 | public: |
| 485 | RegisterContextDarwin_arm_Mach(lldb_private::Thread &thread, |
| 486 | const DataExtractor &data) |
| 487 | : RegisterContextDarwin_arm(thread, 0) { |
| 488 | SetRegisterDataFrom_LC_THREAD(data); |
| 489 | } |
| 490 | |
| 491 | void InvalidateAllRegisters() override { |
| 492 | // Do nothing... registers are always valid... |
| 493 | } |
| 494 | |
| 495 | void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { |
| 496 | lldb::offset_t offset = 0; |
| 497 | SetError(GPRRegSet, Read, -1); |
| 498 | SetError(FPURegSet, Read, -1); |
| 499 | SetError(EXCRegSet, Read, -1); |
| 500 | bool done = false; |
| 501 | |
| 502 | while (!done) { |
| 503 | int flavor = data.GetU32(&offset); |
| 504 | uint32_t count = data.GetU32(&offset); |
| 505 | lldb::offset_t next_thread_state = offset + (count * 4); |
| 506 | switch (flavor) { |
| 507 | case GPRAltRegSet: |
| 508 | case GPRRegSet: |
| 509 | for (uint32_t i = 0; i < count; ++i) { |
| 510 | gpr.r[i] = data.GetU32(&offset); |
| 511 | } |
| 512 | |
| 513 | // Note that gpr.cpsr is also copied by the above loop; this loop |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 514 | // technically extends one element past the end of the gpr.r[] array. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 515 | |
| 516 | SetError(GPRRegSet, Read, 0); |
| 517 | offset = next_thread_state; |
| 518 | break; |
| 519 | |
| 520 | case FPURegSet: { |
| 521 | uint8_t *fpu_reg_buf = (uint8_t *)&fpu.floats.s[0]; |
| 522 | const int fpu_reg_buf_size = sizeof(fpu.floats); |
| 523 | if (data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle, |
| 524 | fpu_reg_buf) == fpu_reg_buf_size) { |
| 525 | offset += fpu_reg_buf_size; |
| 526 | fpu.fpscr = data.GetU32(&offset); |
| 527 | SetError(FPURegSet, Read, 0); |
| 528 | } else { |
| 529 | done = true; |
| 530 | } |
| 531 | } |
| 532 | offset = next_thread_state; |
| 533 | break; |
| 534 | |
| 535 | case EXCRegSet: |
| 536 | if (count == 3) { |
| 537 | exc.exception = data.GetU32(&offset); |
| 538 | exc.fsr = data.GetU32(&offset); |
| 539 | exc.far = data.GetU32(&offset); |
| 540 | SetError(EXCRegSet, Read, 0); |
| 541 | } |
| 542 | done = true; |
| 543 | offset = next_thread_state; |
| 544 | break; |
| 545 | |
| 546 | // Unknown register set flavor, stop trying to parse. |
| 547 | default: |
| 548 | done = true; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static size_t WriteRegister(RegisterContext *reg_ctx, const char *name, |
| 554 | const char *alt_name, size_t reg_byte_size, |
| 555 | Stream &data) { |
| 556 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name); |
| 557 | if (reg_info == NULL) |
| 558 | reg_info = reg_ctx->GetRegisterInfoByName(alt_name); |
| 559 | if (reg_info) { |
| 560 | lldb_private::RegisterValue reg_value; |
| 561 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
| 562 | if (reg_info->byte_size >= reg_byte_size) |
| 563 | data.Write(reg_value.GetBytes(), reg_byte_size); |
| 564 | else { |
| 565 | data.Write(reg_value.GetBytes(), reg_info->byte_size); |
| 566 | for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; |
| 567 | ++i) |
| 568 | data.PutChar(0); |
| 569 | } |
| 570 | return reg_byte_size; |
| 571 | } |
| 572 | } |
| 573 | // Just write zeros if all else fails |
| 574 | for (size_t i = 0; i < reg_byte_size; ++i) |
| 575 | data.PutChar(0); |
| 576 | return reg_byte_size; |
| 577 | } |
| 578 | |
| 579 | static bool Create_LC_THREAD(Thread *thread, Stream &data) { |
| 580 | RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); |
| 581 | if (reg_ctx_sp) { |
| 582 | RegisterContext *reg_ctx = reg_ctx_sp.get(); |
| 583 | |
| 584 | data.PutHex32(GPRRegSet); // Flavor |
| 585 | data.PutHex32(GPRWordCount); |
| 586 | WriteRegister(reg_ctx, "r0", NULL, 4, data); |
| 587 | WriteRegister(reg_ctx, "r1", NULL, 4, data); |
| 588 | WriteRegister(reg_ctx, "r2", NULL, 4, data); |
| 589 | WriteRegister(reg_ctx, "r3", NULL, 4, data); |
| 590 | WriteRegister(reg_ctx, "r4", NULL, 4, data); |
| 591 | WriteRegister(reg_ctx, "r5", NULL, 4, data); |
| 592 | WriteRegister(reg_ctx, "r6", NULL, 4, data); |
| 593 | WriteRegister(reg_ctx, "r7", NULL, 4, data); |
| 594 | WriteRegister(reg_ctx, "r8", NULL, 4, data); |
| 595 | WriteRegister(reg_ctx, "r9", NULL, 4, data); |
| 596 | WriteRegister(reg_ctx, "r10", NULL, 4, data); |
| 597 | WriteRegister(reg_ctx, "r11", NULL, 4, data); |
| 598 | WriteRegister(reg_ctx, "r12", NULL, 4, data); |
| 599 | WriteRegister(reg_ctx, "sp", NULL, 4, data); |
| 600 | WriteRegister(reg_ctx, "lr", NULL, 4, data); |
| 601 | WriteRegister(reg_ctx, "pc", NULL, 4, data); |
| 602 | WriteRegister(reg_ctx, "cpsr", NULL, 4, data); |
| 603 | |
| 604 | // Write out the EXC registers |
| 605 | // data.PutHex32 (EXCRegSet); |
| 606 | // data.PutHex32 (EXCWordCount); |
| 607 | // WriteRegister (reg_ctx, "exception", NULL, 4, data); |
| 608 | // WriteRegister (reg_ctx, "fsr", NULL, 4, data); |
| 609 | // WriteRegister (reg_ctx, "far", NULL, 4, data); |
| 610 | return true; |
| 611 | } |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | protected: |
| 616 | int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; } |
| 617 | |
| 618 | int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; } |
| 619 | |
| 620 | int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; } |
| 621 | |
| 622 | int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; } |
| 623 | |
| 624 | int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override { |
| 637 | return -1; |
| 638 | } |
| 639 | }; |
| 640 | |
| 641 | class RegisterContextDarwin_arm64_Mach : public RegisterContextDarwin_arm64 { |
| 642 | public: |
| 643 | RegisterContextDarwin_arm64_Mach(lldb_private::Thread &thread, |
| 644 | const DataExtractor &data) |
| 645 | : RegisterContextDarwin_arm64(thread, 0) { |
| 646 | SetRegisterDataFrom_LC_THREAD(data); |
| 647 | } |
| 648 | |
| 649 | void InvalidateAllRegisters() override { |
| 650 | // Do nothing... registers are always valid... |
| 651 | } |
| 652 | |
| 653 | void SetRegisterDataFrom_LC_THREAD(const DataExtractor &data) { |
| 654 | lldb::offset_t offset = 0; |
| 655 | SetError(GPRRegSet, Read, -1); |
| 656 | SetError(FPURegSet, Read, -1); |
| 657 | SetError(EXCRegSet, Read, -1); |
| 658 | bool done = false; |
| 659 | while (!done) { |
| 660 | int flavor = data.GetU32(&offset); |
| 661 | uint32_t count = data.GetU32(&offset); |
| 662 | lldb::offset_t next_thread_state = offset + (count * 4); |
| 663 | switch (flavor) { |
| 664 | case GPRRegSet: |
| 665 | // x0-x29 + fp + lr + sp + pc (== 33 64-bit registers) plus cpsr (1 |
| 666 | // 32-bit register) |
| 667 | if (count >= (33 * 2) + 1) { |
Vedant Kumar | 6ba1db9 | 2016-11-07 02:39:37 +0000 | [diff] [blame] | 668 | for (uint32_t i = 0; i < 29; ++i) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | gpr.x[i] = data.GetU64(&offset); |
Vedant Kumar | 6ba1db9 | 2016-11-07 02:39:37 +0000 | [diff] [blame] | 670 | gpr.fp = data.GetU64(&offset); |
| 671 | gpr.lr = data.GetU64(&offset); |
| 672 | gpr.sp = data.GetU64(&offset); |
| 673 | gpr.pc = data.GetU64(&offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 674 | gpr.cpsr = data.GetU32(&offset); |
| 675 | SetError(GPRRegSet, Read, 0); |
| 676 | } |
| 677 | offset = next_thread_state; |
| 678 | break; |
| 679 | case FPURegSet: { |
| 680 | uint8_t *fpu_reg_buf = (uint8_t *)&fpu.v[0]; |
| 681 | const int fpu_reg_buf_size = sizeof(fpu); |
Jason Molenda | 4f34614 | 2017-11-16 00:50:29 +0000 | [diff] [blame] | 682 | if (fpu_reg_buf_size == count * sizeof(uint32_t) && |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 683 | data.ExtractBytes(offset, fpu_reg_buf_size, eByteOrderLittle, |
| 684 | fpu_reg_buf) == fpu_reg_buf_size) { |
| 685 | SetError(FPURegSet, Read, 0); |
| 686 | } else { |
| 687 | done = true; |
| 688 | } |
| 689 | } |
| 690 | offset = next_thread_state; |
| 691 | break; |
| 692 | case EXCRegSet: |
| 693 | if (count == 4) { |
| 694 | exc.far = data.GetU64(&offset); |
| 695 | exc.esr = data.GetU32(&offset); |
| 696 | exc.exception = data.GetU32(&offset); |
| 697 | SetError(EXCRegSet, Read, 0); |
| 698 | } |
| 699 | offset = next_thread_state; |
| 700 | break; |
| 701 | default: |
| 702 | done = true; |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static size_t WriteRegister(RegisterContext *reg_ctx, const char *name, |
| 709 | const char *alt_name, size_t reg_byte_size, |
| 710 | Stream &data) { |
| 711 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name); |
| 712 | if (reg_info == NULL) |
| 713 | reg_info = reg_ctx->GetRegisterInfoByName(alt_name); |
| 714 | if (reg_info) { |
| 715 | lldb_private::RegisterValue reg_value; |
| 716 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
| 717 | if (reg_info->byte_size >= reg_byte_size) |
| 718 | data.Write(reg_value.GetBytes(), reg_byte_size); |
| 719 | else { |
| 720 | data.Write(reg_value.GetBytes(), reg_info->byte_size); |
| 721 | for (size_t i = 0, n = reg_byte_size - reg_info->byte_size; i < n; |
| 722 | ++i) |
| 723 | data.PutChar(0); |
| 724 | } |
| 725 | return reg_byte_size; |
| 726 | } |
| 727 | } |
| 728 | // Just write zeros if all else fails |
| 729 | for (size_t i = 0; i < reg_byte_size; ++i) |
| 730 | data.PutChar(0); |
| 731 | return reg_byte_size; |
| 732 | } |
| 733 | |
| 734 | static bool Create_LC_THREAD(Thread *thread, Stream &data) { |
| 735 | RegisterContextSP reg_ctx_sp(thread->GetRegisterContext()); |
| 736 | if (reg_ctx_sp) { |
| 737 | RegisterContext *reg_ctx = reg_ctx_sp.get(); |
| 738 | |
| 739 | data.PutHex32(GPRRegSet); // Flavor |
| 740 | data.PutHex32(GPRWordCount); |
| 741 | WriteRegister(reg_ctx, "x0", NULL, 8, data); |
| 742 | WriteRegister(reg_ctx, "x1", NULL, 8, data); |
| 743 | WriteRegister(reg_ctx, "x2", NULL, 8, data); |
| 744 | WriteRegister(reg_ctx, "x3", NULL, 8, data); |
| 745 | WriteRegister(reg_ctx, "x4", NULL, 8, data); |
| 746 | WriteRegister(reg_ctx, "x5", NULL, 8, data); |
| 747 | WriteRegister(reg_ctx, "x6", NULL, 8, data); |
| 748 | WriteRegister(reg_ctx, "x7", NULL, 8, data); |
| 749 | WriteRegister(reg_ctx, "x8", NULL, 8, data); |
| 750 | WriteRegister(reg_ctx, "x9", NULL, 8, data); |
| 751 | WriteRegister(reg_ctx, "x10", NULL, 8, data); |
| 752 | WriteRegister(reg_ctx, "x11", NULL, 8, data); |
| 753 | WriteRegister(reg_ctx, "x12", NULL, 8, data); |
| 754 | WriteRegister(reg_ctx, "x13", NULL, 8, data); |
| 755 | WriteRegister(reg_ctx, "x14", NULL, 8, data); |
| 756 | WriteRegister(reg_ctx, "x15", NULL, 8, data); |
| 757 | WriteRegister(reg_ctx, "x16", NULL, 8, data); |
| 758 | WriteRegister(reg_ctx, "x17", NULL, 8, data); |
| 759 | WriteRegister(reg_ctx, "x18", NULL, 8, data); |
| 760 | WriteRegister(reg_ctx, "x19", NULL, 8, data); |
| 761 | WriteRegister(reg_ctx, "x20", NULL, 8, data); |
| 762 | WriteRegister(reg_ctx, "x21", NULL, 8, data); |
| 763 | WriteRegister(reg_ctx, "x22", NULL, 8, data); |
| 764 | WriteRegister(reg_ctx, "x23", NULL, 8, data); |
| 765 | WriteRegister(reg_ctx, "x24", NULL, 8, data); |
| 766 | WriteRegister(reg_ctx, "x25", NULL, 8, data); |
| 767 | WriteRegister(reg_ctx, "x26", NULL, 8, data); |
| 768 | WriteRegister(reg_ctx, "x27", NULL, 8, data); |
| 769 | WriteRegister(reg_ctx, "x28", NULL, 8, data); |
| 770 | WriteRegister(reg_ctx, "fp", NULL, 8, data); |
| 771 | WriteRegister(reg_ctx, "lr", NULL, 8, data); |
| 772 | WriteRegister(reg_ctx, "sp", NULL, 8, data); |
| 773 | WriteRegister(reg_ctx, "pc", NULL, 8, data); |
| 774 | WriteRegister(reg_ctx, "cpsr", NULL, 4, data); |
| 775 | |
| 776 | // Write out the EXC registers |
| 777 | // data.PutHex32 (EXCRegSet); |
| 778 | // data.PutHex32 (EXCWordCount); |
| 779 | // WriteRegister (reg_ctx, "far", NULL, 8, data); |
| 780 | // WriteRegister (reg_ctx, "esr", NULL, 4, data); |
| 781 | // WriteRegister (reg_ctx, "exception", NULL, 4, data); |
| 782 | return true; |
| 783 | } |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | protected: |
| 788 | int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; } |
| 789 | |
| 790 | int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; } |
| 791 | |
| 792 | int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; } |
| 793 | |
| 794 | int DoReadDBG(lldb::tid_t tid, int flavor, DBG &dbg) override { return -1; } |
| 795 | |
| 796 | int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override { |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | int DoWriteFPU(lldb::tid_t tid, int flavor, const FPU &fpu) override { |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | int DoWriteEXC(lldb::tid_t tid, int flavor, const EXC &exc) override { |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | int DoWriteDBG(lldb::tid_t tid, int flavor, const DBG &dbg) override { |
| 809 | return -1; |
| 810 | } |
| 811 | }; |
| 812 | |
| 813 | static uint32_t MachHeaderSizeFromMagic(uint32_t magic) { |
| 814 | switch (magic) { |
| 815 | case MH_MAGIC: |
| 816 | case MH_CIGAM: |
| 817 | return sizeof(struct mach_header); |
| 818 | |
| 819 | case MH_MAGIC_64: |
| 820 | case MH_CIGAM_64: |
| 821 | return sizeof(struct mach_header_64); |
| 822 | break; |
| 823 | |
| 824 | default: |
| 825 | break; |
| 826 | } |
| 827 | return 0; |
Greg Clayton | 9aae0a1 | 2013-05-15 19:52:08 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 830 | #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 831 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 832 | void ObjectFileMachO::Initialize() { |
| 833 | PluginManager::RegisterPlugin( |
| 834 | GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, |
| 835 | CreateMemoryInstance, GetModuleSpecifications, SaveCore); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 838 | void ObjectFileMachO::Terminate() { |
| 839 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | lldb_private::ConstString ObjectFileMachO::GetPluginNameStatic() { |
| 843 | static ConstString g_name("mach-o"); |
| 844 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 847 | const char *ObjectFileMachO::GetPluginDescriptionStatic() { |
| 848 | return "Mach-o object file reader (32 and 64 bit)"; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp, |
| 852 | DataBufferSP &data_sp, |
| 853 | lldb::offset_t data_offset, |
| 854 | const FileSpec *file, |
| 855 | lldb::offset_t file_offset, |
| 856 | lldb::offset_t length) { |
| 857 | if (!data_sp) { |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 858 | data_sp = MapFileData(*file, length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 859 | if (!data_sp) |
| 860 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 861 | data_offset = 0; |
| 862 | } |
| 863 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 864 | if (!ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length)) |
| 865 | return nullptr; |
| 866 | |
| 867 | // Update the data to contain the entire file if it doesn't already |
| 868 | if (data_sp->GetByteSize() < length) { |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 869 | data_sp = MapFileData(*file, length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 870 | if (!data_sp) |
| 871 | return nullptr; |
| 872 | data_offset = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 873 | } |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 874 | auto objfile_ap = llvm::make_unique<ObjectFileMachO>( |
| 875 | module_sp, data_sp, data_offset, file, file_offset, length); |
| 876 | if (!objfile_ap || !objfile_ap->ParseHeader()) |
| 877 | return nullptr; |
| 878 | |
| 879 | return objfile_ap.release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 882 | ObjectFile *ObjectFileMachO::CreateMemoryInstance( |
| 883 | const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, |
| 884 | const ProcessSP &process_sp, lldb::addr_t header_addr) { |
| 885 | if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { |
| 886 | std::unique_ptr<ObjectFile> objfile_ap( |
| 887 | new ObjectFileMachO(module_sp, data_sp, process_sp, header_addr)); |
| 888 | if (objfile_ap.get() && objfile_ap->ParseHeader()) |
| 889 | return objfile_ap.release(); |
| 890 | } |
| 891 | return NULL; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 894 | size_t ObjectFileMachO::GetModuleSpecifications( |
| 895 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 896 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 897 | lldb::offset_t length, lldb_private::ModuleSpecList &specs) { |
| 898 | const size_t initial_count = specs.GetSize(); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 899 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 900 | if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 901 | DataExtractor data; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 902 | data.SetData(data_sp); |
| 903 | llvm::MachO::mach_header header; |
| 904 | if (ParseHeader(data, &data_offset, header)) { |
| 905 | size_t header_and_load_cmds = |
| 906 | header.sizeofcmds + MachHeaderSizeFromMagic(header.magic); |
| 907 | if (header_and_load_cmds >= data_sp->GetByteSize()) { |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 908 | data_sp = MapFileData(file, header_and_load_cmds, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 909 | data.SetData(data_sp); |
| 910 | data_offset = MachHeaderSizeFromMagic(header.magic); |
| 911 | } |
| 912 | if (data_sp) { |
| 913 | ModuleSpec spec; |
| 914 | spec.GetFileSpec() = file; |
| 915 | spec.SetObjectOffset(file_offset); |
| 916 | spec.SetObjectSize(length); |
| 917 | |
| 918 | if (GetArchitecture(header, data, data_offset, |
| 919 | spec.GetArchitecture())) { |
| 920 | if (spec.GetArchitecture().IsValid()) { |
| 921 | GetUUID(header, data, data_offset, spec.GetUUID()); |
| 922 | specs.Append(spec); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | return specs.GetSize() - initial_count; |
| 929 | } |
| 930 | |
| 931 | const ConstString &ObjectFileMachO::GetSegmentNameTEXT() { |
| 932 | static ConstString g_segment_name_TEXT("__TEXT"); |
| 933 | return g_segment_name_TEXT; |
| 934 | } |
| 935 | |
| 936 | const ConstString &ObjectFileMachO::GetSegmentNameDATA() { |
| 937 | static ConstString g_segment_name_DATA("__DATA"); |
| 938 | return g_segment_name_DATA; |
| 939 | } |
| 940 | |
| 941 | const ConstString &ObjectFileMachO::GetSegmentNameDATA_DIRTY() { |
| 942 | static ConstString g_segment_name("__DATA_DIRTY"); |
| 943 | return g_segment_name; |
| 944 | } |
| 945 | |
| 946 | const ConstString &ObjectFileMachO::GetSegmentNameDATA_CONST() { |
| 947 | static ConstString g_segment_name("__DATA_CONST"); |
| 948 | return g_segment_name; |
| 949 | } |
| 950 | |
| 951 | const ConstString &ObjectFileMachO::GetSegmentNameOBJC() { |
| 952 | static ConstString g_segment_name_OBJC("__OBJC"); |
| 953 | return g_segment_name_OBJC; |
| 954 | } |
| 955 | |
| 956 | const ConstString &ObjectFileMachO::GetSegmentNameLINKEDIT() { |
| 957 | static ConstString g_section_name_LINKEDIT("__LINKEDIT"); |
| 958 | return g_section_name_LINKEDIT; |
| 959 | } |
| 960 | |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 961 | const ConstString &ObjectFileMachO::GetSegmentNameDWARF() { |
| 962 | static ConstString g_section_name("__DWARF"); |
| 963 | return g_section_name; |
| 964 | } |
| 965 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 966 | const ConstString &ObjectFileMachO::GetSectionNameEHFrame() { |
| 967 | static ConstString g_section_name_eh_frame("__eh_frame"); |
| 968 | return g_section_name_eh_frame; |
| 969 | } |
| 970 | |
| 971 | bool ObjectFileMachO::MagicBytesMatch(DataBufferSP &data_sp, |
| 972 | lldb::addr_t data_offset, |
| 973 | lldb::addr_t data_length) { |
| 974 | DataExtractor data; |
| 975 | data.SetData(data_sp, data_offset, data_length); |
| 976 | lldb::offset_t offset = 0; |
| 977 | uint32_t magic = data.GetU32(&offset); |
| 978 | return MachHeaderSizeFromMagic(magic) != 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 981 | ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 982 | DataBufferSP &data_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 983 | lldb::offset_t data_offset, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 984 | const FileSpec *file, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 985 | lldb::offset_t file_offset, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 986 | lldb::offset_t length) |
| 987 | : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
| 988 | m_mach_segments(), m_mach_sections(), m_entry_point_address(), |
| 989 | m_thread_context_offsets(), m_thread_context_offsets_valid(false), |
| 990 | m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) { |
| 991 | ::memset(&m_header, 0, sizeof(m_header)); |
| 992 | ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 995 | ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, |
| 996 | lldb::DataBufferSP &header_data_sp, |
| 997 | const lldb::ProcessSP &process_sp, |
| 998 | lldb::addr_t header_addr) |
| 999 | : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), |
| 1000 | m_mach_segments(), m_mach_sections(), m_entry_point_address(), |
| 1001 | m_thread_context_offsets(), m_thread_context_offsets_valid(false), |
| 1002 | m_reexported_dylibs(), m_allow_assembly_emulation_unwind_plans(true) { |
| 1003 | ::memset(&m_header, 0, sizeof(m_header)); |
| 1004 | ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1005 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1006 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | bool ObjectFileMachO::ParseHeader(DataExtractor &data, |
| 1008 | lldb::offset_t *data_offset_ptr, |
| 1009 | llvm::MachO::mach_header &header) { |
| 1010 | data.SetByteOrder(endian::InlHostByteOrder()); |
| 1011 | // Leave magic in the original byte order |
| 1012 | header.magic = data.GetU32(data_offset_ptr); |
| 1013 | bool can_parse = false; |
| 1014 | bool is_64_bit = false; |
| 1015 | switch (header.magic) { |
| 1016 | case MH_MAGIC: |
| 1017 | data.SetByteOrder(endian::InlHostByteOrder()); |
| 1018 | data.SetAddressByteSize(4); |
| 1019 | can_parse = true; |
| 1020 | break; |
| 1021 | |
| 1022 | case MH_MAGIC_64: |
| 1023 | data.SetByteOrder(endian::InlHostByteOrder()); |
| 1024 | data.SetAddressByteSize(8); |
| 1025 | can_parse = true; |
| 1026 | is_64_bit = true; |
| 1027 | break; |
| 1028 | |
| 1029 | case MH_CIGAM: |
| 1030 | data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig |
| 1031 | ? eByteOrderLittle |
| 1032 | : eByteOrderBig); |
| 1033 | data.SetAddressByteSize(4); |
| 1034 | can_parse = true; |
| 1035 | break; |
| 1036 | |
| 1037 | case MH_CIGAM_64: |
| 1038 | data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig |
| 1039 | ? eByteOrderLittle |
| 1040 | : eByteOrderBig); |
| 1041 | data.SetAddressByteSize(8); |
| 1042 | is_64_bit = true; |
| 1043 | can_parse = true; |
| 1044 | break; |
| 1045 | |
| 1046 | default: |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | if (can_parse) { |
| 1051 | data.GetU32(data_offset_ptr, &header.cputype, 6); |
| 1052 | if (is_64_bit) |
| 1053 | *data_offset_ptr += 4; |
| 1054 | return true; |
| 1055 | } else { |
| 1056 | memset(&header, 0, sizeof(header)); |
| 1057 | } |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | bool ObjectFileMachO::ParseHeader() { |
| 1062 | ModuleSP module_sp(GetModule()); |
| 1063 | if (module_sp) { |
| 1064 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 1065 | bool can_parse = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1066 | lldb::offset_t offset = 0; |
| 1067 | m_data.SetByteOrder(endian::InlHostByteOrder()); |
| 1068 | // Leave magic in the original byte order |
| 1069 | m_header.magic = m_data.GetU32(&offset); |
| 1070 | switch (m_header.magic) { |
| 1071 | case MH_MAGIC: |
| 1072 | m_data.SetByteOrder(endian::InlHostByteOrder()); |
| 1073 | m_data.SetAddressByteSize(4); |
| 1074 | can_parse = true; |
| 1075 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1077 | case MH_MAGIC_64: |
| 1078 | m_data.SetByteOrder(endian::InlHostByteOrder()); |
| 1079 | m_data.SetAddressByteSize(8); |
| 1080 | can_parse = true; |
| 1081 | break; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 1082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1083 | case MH_CIGAM: |
| 1084 | m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig |
| 1085 | ? eByteOrderLittle |
| 1086 | : eByteOrderBig); |
| 1087 | m_data.SetAddressByteSize(4); |
| 1088 | can_parse = true; |
| 1089 | break; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 1090 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1091 | case MH_CIGAM_64: |
| 1092 | m_data.SetByteOrder(endian::InlHostByteOrder() == eByteOrderBig |
| 1093 | ? eByteOrderLittle |
| 1094 | : eByteOrderBig); |
| 1095 | m_data.SetAddressByteSize(8); |
| 1096 | can_parse = true; |
| 1097 | break; |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 1098 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1099 | default: |
| 1100 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1103 | if (can_parse) { |
| 1104 | m_data.GetU32(&offset, &m_header.cputype, 6); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1106 | ArchSpec mach_arch; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | if (GetArchitecture(mach_arch)) { |
| 1109 | // Check if the module has a required architecture |
| 1110 | const ArchSpec &module_arch = module_sp->GetArchitecture(); |
| 1111 | if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch)) |
| 1112 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1114 | if (SetModulesArchitecture(mach_arch)) { |
| 1115 | const size_t header_and_lc_size = |
| 1116 | m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic); |
| 1117 | if (m_data.GetByteSize() < header_and_lc_size) { |
| 1118 | DataBufferSP data_sp; |
| 1119 | ProcessSP process_sp(m_process_wp.lock()); |
| 1120 | if (process_sp) { |
| 1121 | data_sp = |
| 1122 | ReadMemory(process_sp, m_memory_addr, header_and_lc_size); |
| 1123 | } else { |
| 1124 | // Read in all only the load command data from the file on disk |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 1125 | data_sp = MapFileData(m_file, header_and_lc_size, m_file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1126 | if (data_sp->GetByteSize() != header_and_lc_size) |
Greg Clayton | 8f265f7 | 2015-10-28 20:49:34 +0000 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1129 | if (data_sp) |
| 1130 | m_data.SetData(data_sp); |
| 1131 | } |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 1132 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1133 | return true; |
| 1134 | } |
| 1135 | } else { |
| 1136 | memset(&m_header, 0, sizeof(struct mach_header)); |
| 1137 | } |
| 1138 | } |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | ByteOrder ObjectFileMachO::GetByteOrder() const { |
| 1143 | return m_data.GetByteOrder(); |
| 1144 | } |
| 1145 | |
| 1146 | bool ObjectFileMachO::IsExecutable() const { |
| 1147 | return m_header.filetype == MH_EXECUTE; |
| 1148 | } |
| 1149 | |
| 1150 | uint32_t ObjectFileMachO::GetAddressByteSize() const { |
| 1151 | return m_data.GetAddressByteSize(); |
| 1152 | } |
| 1153 | |
| 1154 | AddressClass ObjectFileMachO::GetAddressClass(lldb::addr_t file_addr) { |
| 1155 | Symtab *symtab = GetSymtab(); |
| 1156 | if (symtab) { |
| 1157 | Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); |
| 1158 | if (symbol) { |
| 1159 | if (symbol->ValueIsAddress()) { |
| 1160 | SectionSP section_sp(symbol->GetAddressRef().GetSection()); |
| 1161 | if (section_sp) { |
| 1162 | const lldb::SectionType section_type = section_sp->GetType(); |
| 1163 | switch (section_type) { |
| 1164 | case eSectionTypeInvalid: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1165 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1166 | |
| 1167 | case eSectionTypeCode: |
| 1168 | if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1169 | // For ARM we have a bit in the n_desc field of the symbol that |
| 1170 | // tells us ARM/Thumb which is bit 0x0008. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1171 | if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1172 | return AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1173 | } |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1174 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1175 | |
| 1176 | case eSectionTypeContainer: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1177 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1178 | |
| 1179 | case eSectionTypeData: |
| 1180 | case eSectionTypeDataCString: |
| 1181 | case eSectionTypeDataCStringPointers: |
| 1182 | case eSectionTypeDataSymbolAddress: |
| 1183 | case eSectionTypeData4: |
| 1184 | case eSectionTypeData8: |
| 1185 | case eSectionTypeData16: |
| 1186 | case eSectionTypeDataPointers: |
| 1187 | case eSectionTypeZeroFill: |
| 1188 | case eSectionTypeDataObjCMessageRefs: |
| 1189 | case eSectionTypeDataObjCCFStrings: |
| 1190 | case eSectionTypeGoSymtab: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1191 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1192 | |
| 1193 | case eSectionTypeDebug: |
| 1194 | case eSectionTypeDWARFDebugAbbrev: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 1195 | case eSectionTypeDWARFDebugAbbrevDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1196 | case eSectionTypeDWARFDebugAddr: |
| 1197 | case eSectionTypeDWARFDebugAranges: |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 1198 | case eSectionTypeDWARFDebugCuIndex: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1199 | case eSectionTypeDWARFDebugFrame: |
| 1200 | case eSectionTypeDWARFDebugInfo: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 1201 | case eSectionTypeDWARFDebugInfoDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1202 | case eSectionTypeDWARFDebugLine: |
George Rimar | c6c7bfc | 2018-09-13 17:06:47 +0000 | [diff] [blame] | 1203 | case eSectionTypeDWARFDebugLineStr: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1204 | case eSectionTypeDWARFDebugLoc: |
George Rimar | e4dee26 | 2018-10-23 09:46:15 +0000 | [diff] [blame] | 1205 | case eSectionTypeDWARFDebugLocLists: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1206 | case eSectionTypeDWARFDebugMacInfo: |
| 1207 | case eSectionTypeDWARFDebugMacro: |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 1208 | case eSectionTypeDWARFDebugNames: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1209 | case eSectionTypeDWARFDebugPubNames: |
| 1210 | case eSectionTypeDWARFDebugPubTypes: |
| 1211 | case eSectionTypeDWARFDebugRanges: |
George Rimar | 6e35712 | 2018-10-10 08:11:15 +0000 | [diff] [blame] | 1212 | case eSectionTypeDWARFDebugRngLists: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1213 | case eSectionTypeDWARFDebugStr: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 1214 | case eSectionTypeDWARFDebugStrDwo: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1215 | case eSectionTypeDWARFDebugStrOffsets: |
George Rimar | 004bcb7 | 2018-11-14 13:01:15 +0000 | [diff] [blame] | 1216 | case eSectionTypeDWARFDebugStrOffsetsDwo: |
Greg Clayton | 2550ca1 | 2018-05-08 17:19:24 +0000 | [diff] [blame] | 1217 | case eSectionTypeDWARFDebugTypes: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1218 | case eSectionTypeDWARFAppleNames: |
| 1219 | case eSectionTypeDWARFAppleTypes: |
| 1220 | case eSectionTypeDWARFAppleNamespaces: |
| 1221 | case eSectionTypeDWARFAppleObjC: |
Jan Kratochvil | e4777a9 | 2018-04-29 19:47:48 +0000 | [diff] [blame] | 1222 | case eSectionTypeDWARFGNUDebugAltLink: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1223 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1224 | |
| 1225 | case eSectionTypeEHFrame: |
| 1226 | case eSectionTypeARMexidx: |
| 1227 | case eSectionTypeARMextab: |
| 1228 | case eSectionTypeCompactUnwind: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1229 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1230 | |
| 1231 | case eSectionTypeAbsoluteAddress: |
| 1232 | case eSectionTypeELFSymbolTable: |
| 1233 | case eSectionTypeELFDynamicSymbols: |
| 1234 | case eSectionTypeELFRelocationEntries: |
| 1235 | case eSectionTypeELFDynamicLinkInfo: |
| 1236 | case eSectionTypeOther: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1237 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | const SymbolType symbol_type = symbol->GetType(); |
| 1243 | switch (symbol_type) { |
| 1244 | case eSymbolTypeAny: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1245 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1246 | case eSymbolTypeAbsolute: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1247 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1248 | |
| 1249 | case eSymbolTypeCode: |
| 1250 | case eSymbolTypeTrampoline: |
| 1251 | case eSymbolTypeResolver: |
| 1252 | if (m_header.cputype == llvm::MachO::CPU_TYPE_ARM) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1253 | // For ARM we have a bit in the n_desc field of the symbol that tells |
| 1254 | // us ARM/Thumb which is bit 0x0008. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1255 | if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1256 | return AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1257 | } |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1258 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1259 | |
| 1260 | case eSymbolTypeData: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1261 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1262 | case eSymbolTypeRuntime: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1263 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1264 | case eSymbolTypeException: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1265 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | case eSymbolTypeSourceFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1267 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1268 | case eSymbolTypeHeaderFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1269 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1270 | case eSymbolTypeObjectFile: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1271 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1272 | case eSymbolTypeCommonBlock: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1273 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | case eSymbolTypeBlock: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1275 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1276 | case eSymbolTypeLocal: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1277 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1278 | case eSymbolTypeParam: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1279 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1280 | case eSymbolTypeVariable: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1281 | return AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1282 | case eSymbolTypeVariableType: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1283 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1284 | case eSymbolTypeLineEntry: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1285 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1286 | case eSymbolTypeLineHeader: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1287 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1288 | case eSymbolTypeScopeBegin: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1289 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1290 | case eSymbolTypeScopeEnd: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1291 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1292 | case eSymbolTypeAdditional: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1293 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1294 | case eSymbolTypeCompiler: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1295 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1296 | case eSymbolTypeInstrumentation: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1297 | return AddressClass::eDebug; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1298 | case eSymbolTypeUndefined: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1299 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1300 | case eSymbolTypeObjCClass: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1301 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1302 | case eSymbolTypeObjCMetaClass: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1303 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1304 | case eSymbolTypeObjCIVar: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1305 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1306 | case eSymbolTypeReExported: |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1307 | return AddressClass::eRuntime; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | } |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1311 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | Symtab *ObjectFileMachO::GetSymtab() { |
| 1315 | ModuleSP module_sp(GetModule()); |
| 1316 | if (module_sp) { |
| 1317 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 1318 | if (m_symtab_ap.get() == NULL) { |
| 1319 | m_symtab_ap.reset(new Symtab(this)); |
| 1320 | std::lock_guard<std::recursive_mutex> symtab_guard( |
| 1321 | m_symtab_ap->GetMutex()); |
| 1322 | ParseSymtab(); |
Davide Italiano | 407c691 | 2018-11-02 21:59:14 +0000 | [diff] [blame] | 1323 | m_symtab_ap->Finalize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | return m_symtab_ap.get(); |
| 1327 | } |
| 1328 | |
| 1329 | bool ObjectFileMachO::IsStripped() { |
| 1330 | if (m_dysymtab.cmd == 0) { |
| 1331 | ModuleSP module_sp(GetModule()); |
| 1332 | if (module_sp) { |
| 1333 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 1334 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
| 1335 | const lldb::offset_t load_cmd_offset = offset; |
| 1336 | |
| 1337 | load_command lc; |
| 1338 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 1339 | break; |
| 1340 | if (lc.cmd == LC_DYSYMTAB) { |
| 1341 | m_dysymtab.cmd = lc.cmd; |
| 1342 | m_dysymtab.cmdsize = lc.cmdsize; |
| 1343 | if (m_data.GetU32(&offset, &m_dysymtab.ilocalsym, |
| 1344 | (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2) == |
| 1345 | NULL) { |
| 1346 | // Clear m_dysymtab if we were unable to read all items from the |
| 1347 | // load command |
| 1348 | ::memset(&m_dysymtab, 0, sizeof(m_dysymtab)); |
| 1349 | } |
| 1350 | } |
| 1351 | offset = load_cmd_offset + lc.cmdsize; |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | if (m_dysymtab.cmd) |
| 1356 | return m_dysymtab.nlocalsym <= 1; |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1360 | ObjectFileMachO::EncryptedFileRanges ObjectFileMachO::GetEncryptedFileRanges() { |
| 1361 | EncryptedFileRanges result; |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1362 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1363 | |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1364 | encryption_info_command encryption_cmd; |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1365 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1366 | const lldb::offset_t load_cmd_offset = offset; |
| 1367 | if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL) |
| 1368 | break; |
| 1369 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1370 | // LC_ENCRYPTION_INFO and LC_ENCRYPTION_INFO_64 have the same sizes for the |
| 1371 | // 3 fields we care about, so treat them the same. |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1372 | if (encryption_cmd.cmd == LC_ENCRYPTION_INFO || |
| 1373 | encryption_cmd.cmd == LC_ENCRYPTION_INFO_64) { |
| 1374 | if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3)) { |
| 1375 | if (encryption_cmd.cryptid != 0) { |
| 1376 | EncryptedFileRanges::Entry entry; |
| 1377 | entry.SetRangeBase(encryption_cmd.cryptoff); |
| 1378 | entry.SetByteSize(encryption_cmd.cryptsize); |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1379 | result.Append(entry); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1382 | } |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1383 | offset = load_cmd_offset + encryption_cmd.cmdsize; |
| 1384 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1385 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1386 | return result; |
| 1387 | } |
| 1388 | |
| 1389 | void ObjectFileMachO::SanitizeSegmentCommand(segment_command_64 &seg_cmd, |
| 1390 | uint32_t cmd_idx) { |
| 1391 | if (m_length == 0 || seg_cmd.filesize == 0) |
| 1392 | return; |
| 1393 | |
| 1394 | if (seg_cmd.fileoff > m_length) { |
| 1395 | // We have a load command that says it extends past the end of the file. |
| 1396 | // This is likely a corrupt file. We don't have any way to return an error |
| 1397 | // condition here (this method was likely invoked from something like |
| 1398 | // ObjectFile::GetSectionList()), so we just null out the section contents, |
| 1399 | // and dump a message to stdout. The most common case here is core file |
| 1400 | // debugging with a truncated file. |
| 1401 | const char *lc_segment_name = |
| 1402 | seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; |
| 1403 | GetModule()->ReportWarning( |
| 1404 | "load command %u %s has a fileoff (0x%" PRIx64 |
| 1405 | ") that extends beyond the end of the file (0x%" PRIx64 |
| 1406 | "), ignoring this section", |
| 1407 | cmd_idx, lc_segment_name, seg_cmd.fileoff, m_length); |
| 1408 | |
| 1409 | seg_cmd.fileoff = 0; |
| 1410 | seg_cmd.filesize = 0; |
| 1411 | } |
| 1412 | |
| 1413 | if (seg_cmd.fileoff + seg_cmd.filesize > m_length) { |
| 1414 | // We have a load command that says it extends past the end of the file. |
| 1415 | // This is likely a corrupt file. We don't have any way to return an error |
| 1416 | // condition here (this method was likely invoked from something like |
| 1417 | // ObjectFile::GetSectionList()), so we just null out the section contents, |
| 1418 | // and dump a message to stdout. The most common case here is core file |
| 1419 | // debugging with a truncated file. |
| 1420 | const char *lc_segment_name = |
| 1421 | seg_cmd.cmd == LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; |
| 1422 | GetModule()->ReportWarning( |
| 1423 | "load command %u %s has a fileoff + filesize (0x%" PRIx64 |
| 1424 | ") that extends beyond the end of the file (0x%" PRIx64 |
| 1425 | "), the segment will be truncated to match", |
| 1426 | cmd_idx, lc_segment_name, seg_cmd.fileoff + seg_cmd.filesize, m_length); |
| 1427 | |
| 1428 | // Truncate the length |
| 1429 | seg_cmd.filesize = m_length - seg_cmd.fileoff; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | static uint32_t GetSegmentPermissions(const segment_command_64 &seg_cmd) { |
| 1434 | uint32_t result = 0; |
| 1435 | if (seg_cmd.initprot & VM_PROT_READ) |
| 1436 | result |= ePermissionsReadable; |
| 1437 | if (seg_cmd.initprot & VM_PROT_WRITE) |
| 1438 | result |= ePermissionsWritable; |
| 1439 | if (seg_cmd.initprot & VM_PROT_EXECUTE) |
| 1440 | result |= ePermissionsExecutable; |
| 1441 | return result; |
| 1442 | } |
| 1443 | |
| 1444 | static lldb::SectionType GetSectionType(uint32_t flags, |
| 1445 | ConstString section_name) { |
| 1446 | |
| 1447 | if (flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS)) |
| 1448 | return eSectionTypeCode; |
| 1449 | |
| 1450 | uint32_t mach_sect_type = flags & SECTION_TYPE; |
| 1451 | static ConstString g_sect_name_objc_data("__objc_data"); |
| 1452 | static ConstString g_sect_name_objc_msgrefs("__objc_msgrefs"); |
| 1453 | static ConstString g_sect_name_objc_selrefs("__objc_selrefs"); |
| 1454 | static ConstString g_sect_name_objc_classrefs("__objc_classrefs"); |
| 1455 | static ConstString g_sect_name_objc_superrefs("__objc_superrefs"); |
| 1456 | static ConstString g_sect_name_objc_const("__objc_const"); |
| 1457 | static ConstString g_sect_name_objc_classlist("__objc_classlist"); |
| 1458 | static ConstString g_sect_name_cfstring("__cfstring"); |
| 1459 | |
| 1460 | static ConstString g_sect_name_dwarf_debug_abbrev("__debug_abbrev"); |
| 1461 | static ConstString g_sect_name_dwarf_debug_aranges("__debug_aranges"); |
| 1462 | static ConstString g_sect_name_dwarf_debug_frame("__debug_frame"); |
| 1463 | static ConstString g_sect_name_dwarf_debug_info("__debug_info"); |
| 1464 | static ConstString g_sect_name_dwarf_debug_line("__debug_line"); |
| 1465 | static ConstString g_sect_name_dwarf_debug_loc("__debug_loc"); |
George Rimar | e4dee26 | 2018-10-23 09:46:15 +0000 | [diff] [blame] | 1466 | static ConstString g_sect_name_dwarf_debug_loclists("__debug_loclists"); |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1467 | static ConstString g_sect_name_dwarf_debug_macinfo("__debug_macinfo"); |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 1468 | static ConstString g_sect_name_dwarf_debug_names("__debug_names"); |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1469 | static ConstString g_sect_name_dwarf_debug_pubnames("__debug_pubnames"); |
| 1470 | static ConstString g_sect_name_dwarf_debug_pubtypes("__debug_pubtypes"); |
| 1471 | static ConstString g_sect_name_dwarf_debug_ranges("__debug_ranges"); |
| 1472 | static ConstString g_sect_name_dwarf_debug_str("__debug_str"); |
Greg Clayton | 2550ca1 | 2018-05-08 17:19:24 +0000 | [diff] [blame] | 1473 | static ConstString g_sect_name_dwarf_debug_types("__debug_types"); |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1474 | static ConstString g_sect_name_dwarf_apple_names("__apple_names"); |
| 1475 | static ConstString g_sect_name_dwarf_apple_types("__apple_types"); |
| 1476 | static ConstString g_sect_name_dwarf_apple_namespaces("__apple_namespac"); |
| 1477 | static ConstString g_sect_name_dwarf_apple_objc("__apple_objc"); |
| 1478 | static ConstString g_sect_name_eh_frame("__eh_frame"); |
| 1479 | static ConstString g_sect_name_compact_unwind("__unwind_info"); |
| 1480 | static ConstString g_sect_name_text("__text"); |
| 1481 | static ConstString g_sect_name_data("__data"); |
| 1482 | static ConstString g_sect_name_go_symtab("__gosymtab"); |
| 1483 | |
| 1484 | if (section_name == g_sect_name_dwarf_debug_abbrev) |
| 1485 | return eSectionTypeDWARFDebugAbbrev; |
| 1486 | if (section_name == g_sect_name_dwarf_debug_aranges) |
| 1487 | return eSectionTypeDWARFDebugAranges; |
| 1488 | if (section_name == g_sect_name_dwarf_debug_frame) |
| 1489 | return eSectionTypeDWARFDebugFrame; |
| 1490 | if (section_name == g_sect_name_dwarf_debug_info) |
| 1491 | return eSectionTypeDWARFDebugInfo; |
| 1492 | if (section_name == g_sect_name_dwarf_debug_line) |
| 1493 | return eSectionTypeDWARFDebugLine; |
| 1494 | if (section_name == g_sect_name_dwarf_debug_loc) |
| 1495 | return eSectionTypeDWARFDebugLoc; |
George Rimar | e4dee26 | 2018-10-23 09:46:15 +0000 | [diff] [blame] | 1496 | if (section_name == g_sect_name_dwarf_debug_loclists) |
| 1497 | return eSectionTypeDWARFDebugLocLists; |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1498 | if (section_name == g_sect_name_dwarf_debug_macinfo) |
| 1499 | return eSectionTypeDWARFDebugMacInfo; |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 1500 | if (section_name == g_sect_name_dwarf_debug_names) |
| 1501 | return eSectionTypeDWARFDebugNames; |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1502 | if (section_name == g_sect_name_dwarf_debug_pubnames) |
| 1503 | return eSectionTypeDWARFDebugPubNames; |
| 1504 | if (section_name == g_sect_name_dwarf_debug_pubtypes) |
| 1505 | return eSectionTypeDWARFDebugPubTypes; |
| 1506 | if (section_name == g_sect_name_dwarf_debug_ranges) |
George Rimar | 6e35712 | 2018-10-10 08:11:15 +0000 | [diff] [blame] | 1507 | return eSectionTypeDWARFDebugRanges; |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1508 | if (section_name == g_sect_name_dwarf_debug_str) |
| 1509 | return eSectionTypeDWARFDebugStr; |
Greg Clayton | 2550ca1 | 2018-05-08 17:19:24 +0000 | [diff] [blame] | 1510 | if (section_name == g_sect_name_dwarf_debug_types) |
| 1511 | return eSectionTypeDWARFDebugTypes; |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1512 | if (section_name == g_sect_name_dwarf_apple_names) |
| 1513 | return eSectionTypeDWARFAppleNames; |
| 1514 | if (section_name == g_sect_name_dwarf_apple_types) |
| 1515 | return eSectionTypeDWARFAppleTypes; |
| 1516 | if (section_name == g_sect_name_dwarf_apple_namespaces) |
| 1517 | return eSectionTypeDWARFAppleNamespaces; |
| 1518 | if (section_name == g_sect_name_dwarf_apple_objc) |
| 1519 | return eSectionTypeDWARFAppleObjC; |
| 1520 | if (section_name == g_sect_name_objc_selrefs) |
| 1521 | return eSectionTypeDataCStringPointers; |
| 1522 | if (section_name == g_sect_name_objc_msgrefs) |
| 1523 | return eSectionTypeDataObjCMessageRefs; |
| 1524 | if (section_name == g_sect_name_eh_frame) |
| 1525 | return eSectionTypeEHFrame; |
| 1526 | if (section_name == g_sect_name_compact_unwind) |
| 1527 | return eSectionTypeCompactUnwind; |
| 1528 | if (section_name == g_sect_name_cfstring) |
| 1529 | return eSectionTypeDataObjCCFStrings; |
| 1530 | if (section_name == g_sect_name_go_symtab) |
| 1531 | return eSectionTypeGoSymtab; |
| 1532 | if (section_name == g_sect_name_objc_data || |
| 1533 | section_name == g_sect_name_objc_classrefs || |
| 1534 | section_name == g_sect_name_objc_superrefs || |
| 1535 | section_name == g_sect_name_objc_const || |
| 1536 | section_name == g_sect_name_objc_classlist) { |
| 1537 | return eSectionTypeDataPointers; |
| 1538 | } |
| 1539 | |
| 1540 | switch (mach_sect_type) { |
| 1541 | // TODO: categorize sections by other flags for regular sections |
| 1542 | case S_REGULAR: |
| 1543 | if (section_name == g_sect_name_text) |
| 1544 | return eSectionTypeCode; |
| 1545 | if (section_name == g_sect_name_data) |
| 1546 | return eSectionTypeData; |
| 1547 | return eSectionTypeOther; |
| 1548 | case S_ZEROFILL: |
| 1549 | return eSectionTypeZeroFill; |
| 1550 | case S_CSTRING_LITERALS: // section with only literal C strings |
| 1551 | return eSectionTypeDataCString; |
| 1552 | case S_4BYTE_LITERALS: // section with only 4 byte literals |
| 1553 | return eSectionTypeData4; |
| 1554 | case S_8BYTE_LITERALS: // section with only 8 byte literals |
| 1555 | return eSectionTypeData8; |
| 1556 | case S_LITERAL_POINTERS: // section with only pointers to literals |
| 1557 | return eSectionTypeDataPointers; |
| 1558 | case S_NON_LAZY_SYMBOL_POINTERS: // section with only non-lazy symbol pointers |
| 1559 | return eSectionTypeDataPointers; |
| 1560 | case S_LAZY_SYMBOL_POINTERS: // section with only lazy symbol pointers |
| 1561 | return eSectionTypeDataPointers; |
| 1562 | case S_SYMBOL_STUBS: // section with only symbol stubs, byte size of stub in |
| 1563 | // the reserved2 field |
| 1564 | return eSectionTypeCode; |
| 1565 | case S_MOD_INIT_FUNC_POINTERS: // section with only function pointers for |
| 1566 | // initialization |
| 1567 | return eSectionTypeDataPointers; |
| 1568 | case S_MOD_TERM_FUNC_POINTERS: // section with only function pointers for |
| 1569 | // termination |
| 1570 | return eSectionTypeDataPointers; |
| 1571 | case S_COALESCED: |
| 1572 | return eSectionTypeOther; |
| 1573 | case S_GB_ZEROFILL: |
| 1574 | return eSectionTypeZeroFill; |
| 1575 | case S_INTERPOSING: // section with only pairs of function pointers for |
| 1576 | // interposing |
| 1577 | return eSectionTypeCode; |
| 1578 | case S_16BYTE_LITERALS: // section with only 16 byte literals |
| 1579 | return eSectionTypeData16; |
| 1580 | case S_DTRACE_DOF: |
| 1581 | return eSectionTypeDebug; |
| 1582 | case S_LAZY_DYLIB_SYMBOL_POINTERS: |
| 1583 | return eSectionTypeDataPointers; |
| 1584 | default: |
| 1585 | return eSectionTypeOther; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | struct ObjectFileMachO::SegmentParsingContext { |
| 1590 | const EncryptedFileRanges EncryptedRanges; |
| 1591 | lldb_private::SectionList &UnifiedList; |
| 1592 | uint32_t NextSegmentIdx = 0; |
| 1593 | uint32_t NextSectionIdx = 0; |
| 1594 | bool FileAddressesChanged = false; |
| 1595 | |
| 1596 | SegmentParsingContext(EncryptedFileRanges EncryptedRanges, |
| 1597 | lldb_private::SectionList &UnifiedList) |
| 1598 | : EncryptedRanges(std::move(EncryptedRanges)), UnifiedList(UnifiedList) {} |
| 1599 | }; |
| 1600 | |
| 1601 | void ObjectFileMachO::ProcessSegmentCommand(const load_command &load_cmd_, |
| 1602 | lldb::offset_t offset, |
| 1603 | uint32_t cmd_idx, |
| 1604 | SegmentParsingContext &context) { |
| 1605 | segment_command_64 load_cmd; |
| 1606 | memcpy(&load_cmd, &load_cmd_, sizeof(load_cmd_)); |
| 1607 | |
| 1608 | if (!m_data.GetU8(&offset, (uint8_t *)load_cmd.segname, 16)) |
| 1609 | return; |
| 1610 | |
| 1611 | ModuleSP module_sp = GetModule(); |
| 1612 | const bool is_core = GetType() == eTypeCoreFile; |
| 1613 | const bool is_dsym = (m_header.filetype == MH_DSYM); |
| 1614 | bool add_section = true; |
| 1615 | bool add_to_unified = true; |
| 1616 | ConstString const_segname( |
| 1617 | load_cmd.segname, |
| 1618 | std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); |
| 1619 | |
| 1620 | SectionSP unified_section_sp( |
| 1621 | context.UnifiedList.FindSectionByName(const_segname)); |
| 1622 | if (is_dsym && unified_section_sp) { |
| 1623 | if (const_segname == GetSegmentNameLINKEDIT()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1624 | // We need to keep the __LINKEDIT segment private to this object file |
| 1625 | // only |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1626 | add_to_unified = false; |
| 1627 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1628 | // This is the dSYM file and this section has already been created by the |
| 1629 | // object file, no need to create it. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1630 | add_section = false; |
| 1631 | } |
| 1632 | } |
| 1633 | load_cmd.vmaddr = m_data.GetAddress(&offset); |
| 1634 | load_cmd.vmsize = m_data.GetAddress(&offset); |
| 1635 | load_cmd.fileoff = m_data.GetAddress(&offset); |
| 1636 | load_cmd.filesize = m_data.GetAddress(&offset); |
| 1637 | if (!m_data.GetU32(&offset, &load_cmd.maxprot, 4)) |
| 1638 | return; |
| 1639 | |
| 1640 | SanitizeSegmentCommand(load_cmd, cmd_idx); |
| 1641 | |
| 1642 | const uint32_t segment_permissions = GetSegmentPermissions(load_cmd); |
| 1643 | const bool segment_is_encrypted = |
| 1644 | (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0; |
| 1645 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1646 | // Keep a list of mach segments around in case we need to get at data that |
| 1647 | // isn't stored in the abstracted Sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1648 | m_mach_segments.push_back(load_cmd); |
| 1649 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1650 | // Use a segment ID of the segment index shifted left by 8 so they never |
| 1651 | // conflict with any of the sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1652 | SectionSP segment_sp; |
| 1653 | if (add_section && (const_segname || is_core)) { |
| 1654 | segment_sp.reset(new Section( |
| 1655 | module_sp, // Module to which this section belongs |
| 1656 | this, // Object file to which this sections belongs |
| 1657 | ++context.NextSegmentIdx |
| 1658 | << 8, // Section ID is the 1 based segment index |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1659 | // shifted right by 8 bits as not to collide with any of the 256 |
| 1660 | // section IDs that are possible |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1661 | const_segname, // Name of this section |
| 1662 | eSectionTypeContainer, // This section is a container of other |
| 1663 | // sections. |
| 1664 | load_cmd.vmaddr, // File VM address == addresses as they are |
| 1665 | // found in the object file |
| 1666 | load_cmd.vmsize, // VM size in bytes of this section |
| 1667 | load_cmd.fileoff, // Offset to the data for this section in |
| 1668 | // the file |
| 1669 | load_cmd.filesize, // Size in bytes of this section as found |
| 1670 | // in the file |
| 1671 | 0, // Segments have no alignment information |
| 1672 | load_cmd.flags)); // Flags for this section |
| 1673 | |
| 1674 | segment_sp->SetIsEncrypted(segment_is_encrypted); |
| 1675 | m_sections_ap->AddSection(segment_sp); |
| 1676 | segment_sp->SetPermissions(segment_permissions); |
| 1677 | if (add_to_unified) |
| 1678 | context.UnifiedList.AddSection(segment_sp); |
| 1679 | } else if (unified_section_sp) { |
| 1680 | if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) { |
| 1681 | // Check to see if the module was read from memory? |
Pavel Labath | d1e3fe2 | 2018-12-11 15:21:15 +0000 | [diff] [blame] | 1682 | if (module_sp->GetObjectFile()->GetBaseAddress().IsValid()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1683 | // We have a module that is in memory and needs to have its file |
| 1684 | // address adjusted. We need to do this because when we load a file |
| 1685 | // from memory, its addresses will be slid already, yet the addresses |
| 1686 | // in the new symbol file will still be unslid. Since everything is |
| 1687 | // stored as section offset, this shouldn't cause any problems. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1688 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1689 | // Make sure we've parsed the symbol table from the ObjectFile before |
| 1690 | // we go around changing its Sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1691 | module_sp->GetObjectFile()->GetSymtab(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1692 | // eh_frame would present the same problems but we parse that on a per- |
| 1693 | // function basis as-needed so it's more difficult to remove its use of |
| 1694 | // the Sections. Realistically, the environments where this code path |
| 1695 | // will be taken will not have eh_frame sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1696 | |
| 1697 | unified_section_sp->SetFileAddress(load_cmd.vmaddr); |
| 1698 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1699 | // Notify the module that the section addresses have been changed once |
| 1700 | // we're done so any file-address caches can be updated. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1701 | context.FileAddressesChanged = true; |
| 1702 | } |
| 1703 | } |
| 1704 | m_sections_ap->AddSection(unified_section_sp); |
| 1705 | } |
| 1706 | |
| 1707 | struct section_64 sect64; |
| 1708 | ::memset(§64, 0, sizeof(sect64)); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1709 | // Push a section into our mach sections for the section at index zero |
| 1710 | // (NO_SECT) if we don't have any mach sections yet... |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1711 | if (m_mach_sections.empty()) |
| 1712 | m_mach_sections.push_back(sect64); |
| 1713 | uint32_t segment_sect_idx; |
| 1714 | const lldb::user_id_t first_segment_sectID = context.NextSectionIdx + 1; |
| 1715 | |
| 1716 | const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8; |
| 1717 | for (segment_sect_idx = 0; segment_sect_idx < load_cmd.nsects; |
| 1718 | ++segment_sect_idx) { |
| 1719 | if (m_data.GetU8(&offset, (uint8_t *)sect64.sectname, |
| 1720 | sizeof(sect64.sectname)) == NULL) |
| 1721 | break; |
| 1722 | if (m_data.GetU8(&offset, (uint8_t *)sect64.segname, |
| 1723 | sizeof(sect64.segname)) == NULL) |
| 1724 | break; |
| 1725 | sect64.addr = m_data.GetAddress(&offset); |
| 1726 | sect64.size = m_data.GetAddress(&offset); |
| 1727 | |
| 1728 | if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) |
| 1729 | break; |
| 1730 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1731 | // Keep a list of mach sections around in case we need to get at data that |
| 1732 | // isn't stored in the abstracted Sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1733 | m_mach_sections.push_back(sect64); |
| 1734 | |
| 1735 | if (add_section) { |
| 1736 | ConstString section_name( |
| 1737 | sect64.sectname, |
| 1738 | std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); |
| 1739 | if (!const_segname) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1740 | // We have a segment with no name so we need to conjure up segments |
| 1741 | // that correspond to the section's segname if there isn't already such |
| 1742 | // a section. If there is such a section, we resize the section so that |
| 1743 | // it spans all sections. We also mark these sections as fake so |
| 1744 | // address matches don't hit if they land in the gaps between the child |
| 1745 | // sections. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1746 | const_segname.SetTrimmedCStringWithLength(sect64.segname, |
| 1747 | sizeof(sect64.segname)); |
| 1748 | segment_sp = context.UnifiedList.FindSectionByName(const_segname); |
| 1749 | if (segment_sp.get()) { |
| 1750 | Section *segment = segment_sp.get(); |
| 1751 | // Grow the section size as needed. |
| 1752 | const lldb::addr_t sect64_min_addr = sect64.addr; |
| 1753 | const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; |
| 1754 | const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); |
| 1755 | const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); |
| 1756 | const lldb::addr_t curr_seg_max_addr = |
| 1757 | curr_seg_min_addr + curr_seg_byte_size; |
| 1758 | if (sect64_min_addr >= curr_seg_min_addr) { |
| 1759 | const lldb::addr_t new_seg_byte_size = |
| 1760 | sect64_max_addr - curr_seg_min_addr; |
| 1761 | // Only grow the section size if needed |
| 1762 | if (new_seg_byte_size > curr_seg_byte_size) |
| 1763 | segment->SetByteSize(new_seg_byte_size); |
| 1764 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1765 | // We need to change the base address of the segment and adjust the |
| 1766 | // child section offsets for all existing children. |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1767 | const lldb::addr_t slide_amount = |
| 1768 | sect64_min_addr - curr_seg_min_addr; |
| 1769 | segment->Slide(slide_amount, false); |
| 1770 | segment->GetChildren().Slide(-slide_amount, false); |
| 1771 | segment->SetByteSize(curr_seg_max_addr - sect64_min_addr); |
| 1772 | } |
| 1773 | |
| 1774 | // Grow the section size as needed. |
| 1775 | if (sect64.offset) { |
| 1776 | const lldb::addr_t segment_min_file_offset = |
| 1777 | segment->GetFileOffset(); |
| 1778 | const lldb::addr_t segment_max_file_offset = |
| 1779 | segment_min_file_offset + segment->GetFileSize(); |
| 1780 | |
| 1781 | const lldb::addr_t section_min_file_offset = sect64.offset; |
| 1782 | const lldb::addr_t section_max_file_offset = |
| 1783 | section_min_file_offset + sect64.size; |
| 1784 | const lldb::addr_t new_file_offset = |
| 1785 | std::min(section_min_file_offset, segment_min_file_offset); |
| 1786 | const lldb::addr_t new_file_size = |
| 1787 | std::max(section_max_file_offset, segment_max_file_offset) - |
| 1788 | new_file_offset; |
| 1789 | segment->SetFileOffset(new_file_offset); |
| 1790 | segment->SetFileSize(new_file_size); |
| 1791 | } |
| 1792 | } else { |
| 1793 | // Create a fake section for the section's named segment |
| 1794 | segment_sp.reset(new Section( |
| 1795 | segment_sp, // Parent section |
| 1796 | module_sp, // Module to which this section belongs |
| 1797 | this, // Object file to which this section belongs |
| 1798 | ++context.NextSegmentIdx |
| 1799 | << 8, // Section ID is the 1 based segment index |
| 1800 | // shifted right by 8 bits as not to |
| 1801 | // collide with any of the 256 section IDs |
| 1802 | // that are possible |
| 1803 | const_segname, // Name of this section |
| 1804 | eSectionTypeContainer, // This section is a container of |
| 1805 | // other sections. |
| 1806 | sect64.addr, // File VM address == addresses as they are |
| 1807 | // found in the object file |
| 1808 | sect64.size, // VM size in bytes of this section |
| 1809 | sect64.offset, // Offset to the data for this section in |
| 1810 | // the file |
| 1811 | sect64.offset ? sect64.size : 0, // Size in bytes of |
| 1812 | // this section as |
| 1813 | // found in the file |
| 1814 | sect64.align, |
| 1815 | load_cmd.flags)); // Flags for this section |
| 1816 | segment_sp->SetIsFake(true); |
| 1817 | segment_sp->SetPermissions(segment_permissions); |
| 1818 | m_sections_ap->AddSection(segment_sp); |
| 1819 | if (add_to_unified) |
| 1820 | context.UnifiedList.AddSection(segment_sp); |
| 1821 | segment_sp->SetIsEncrypted(segment_is_encrypted); |
| 1822 | } |
| 1823 | } |
| 1824 | assert(segment_sp.get()); |
| 1825 | |
| 1826 | lldb::SectionType sect_type = GetSectionType(sect64.flags, section_name); |
| 1827 | |
| 1828 | SectionSP section_sp(new Section( |
| 1829 | segment_sp, module_sp, this, ++context.NextSectionIdx, section_name, |
| 1830 | sect_type, sect64.addr - segment_sp->GetFileAddress(), sect64.size, |
| 1831 | sect64.offset, sect64.offset == 0 ? 0 : sect64.size, sect64.align, |
| 1832 | sect64.flags)); |
| 1833 | // Set the section to be encrypted to match the segment |
| 1834 | |
| 1835 | bool section_is_encrypted = false; |
| 1836 | if (!segment_is_encrypted && load_cmd.filesize != 0) |
| 1837 | section_is_encrypted = context.EncryptedRanges.FindEntryThatContains( |
| 1838 | sect64.offset) != NULL; |
| 1839 | |
| 1840 | section_sp->SetIsEncrypted(segment_is_encrypted || section_is_encrypted); |
| 1841 | section_sp->SetPermissions(segment_permissions); |
| 1842 | segment_sp->GetChildren().AddSection(section_sp); |
| 1843 | |
| 1844 | if (segment_sp->IsFake()) { |
| 1845 | segment_sp.reset(); |
| 1846 | const_segname.Clear(); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | if (segment_sp && is_dsym) { |
| 1851 | if (first_segment_sectID <= context.NextSectionIdx) { |
| 1852 | lldb::user_id_t sect_uid; |
| 1853 | for (sect_uid = first_segment_sectID; sect_uid <= context.NextSectionIdx; |
| 1854 | ++sect_uid) { |
| 1855 | SectionSP curr_section_sp( |
| 1856 | segment_sp->GetChildren().FindSectionByID(sect_uid)); |
| 1857 | SectionSP next_section_sp; |
| 1858 | if (sect_uid + 1 <= context.NextSectionIdx) |
| 1859 | next_section_sp = |
| 1860 | segment_sp->GetChildren().FindSectionByID(sect_uid + 1); |
| 1861 | |
| 1862 | if (curr_section_sp.get()) { |
| 1863 | if (curr_section_sp->GetByteSize() == 0) { |
| 1864 | if (next_section_sp.get() != NULL) |
| 1865 | curr_section_sp->SetByteSize(next_section_sp->GetFileAddress() - |
| 1866 | curr_section_sp->GetFileAddress()); |
| 1867 | else |
| 1868 | curr_section_sp->SetByteSize(load_cmd.vmsize); |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | void ObjectFileMachO::ProcessDysymtabCommand(const load_command &load_cmd, |
| 1877 | lldb::offset_t offset) { |
| 1878 | m_dysymtab.cmd = load_cmd.cmd; |
| 1879 | m_dysymtab.cmdsize = load_cmd.cmdsize; |
| 1880 | m_data.GetU32(&offset, &m_dysymtab.ilocalsym, |
| 1881 | (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); |
| 1882 | } |
| 1883 | |
| 1884 | void ObjectFileMachO::CreateSections(SectionList &unified_section_list) { |
| 1885 | if (m_sections_ap) |
| 1886 | return; |
| 1887 | |
| 1888 | m_sections_ap.reset(new SectionList()); |
| 1889 | |
| 1890 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 1891 | // bool dump_sections = false; |
| 1892 | ModuleSP module_sp(GetModule()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1893 | |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1894 | offset = MachHeaderSizeFromMagic(m_header.magic); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1895 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1896 | SegmentParsingContext context(GetEncryptedFileRanges(), unified_section_list); |
| 1897 | struct load_command load_cmd; |
| 1898 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1899 | const lldb::offset_t load_cmd_offset = offset; |
| 1900 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 1901 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1902 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1903 | if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64) |
| 1904 | ProcessSegmentCommand(load_cmd, offset, i, context); |
| 1905 | else if (load_cmd.cmd == LC_DYSYMTAB) |
| 1906 | ProcessDysymtabCommand(load_cmd, offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1907 | |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1908 | offset = load_cmd_offset + load_cmd.cmdsize; |
| 1909 | } |
| 1910 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 1911 | if (context.FileAddressesChanged && module_sp) |
Pavel Labath | f90054d | 2018-03-03 22:07:47 +0000 | [diff] [blame] | 1912 | module_sp->SectionFileAddressesChanged(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | class MachSymtabSectionInfo { |
| 1916 | public: |
| 1917 | MachSymtabSectionInfo(SectionList *section_list) |
| 1918 | : m_section_list(section_list), m_section_infos() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1919 | // Get the number of sections down to a depth of 1 to include all segments |
| 1920 | // and their sections, but no other sections that may be added for debug |
| 1921 | // map or |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1922 | m_section_infos.resize(section_list->GetNumSections(1)); |
| 1923 | } |
| 1924 | |
| 1925 | SectionSP GetSection(uint8_t n_sect, addr_t file_addr) { |
| 1926 | if (n_sect == 0) |
| 1927 | return SectionSP(); |
| 1928 | if (n_sect < m_section_infos.size()) { |
| 1929 | if (!m_section_infos[n_sect].section_sp) { |
| 1930 | SectionSP section_sp(m_section_list->FindSectionByID(n_sect)); |
| 1931 | m_section_infos[n_sect].section_sp = section_sp; |
| 1932 | if (section_sp) { |
| 1933 | m_section_infos[n_sect].vm_range.SetBaseAddress( |
| 1934 | section_sp->GetFileAddress()); |
| 1935 | m_section_infos[n_sect].vm_range.SetByteSize( |
| 1936 | section_sp->GetByteSize()); |
| 1937 | } else { |
| 1938 | Host::SystemLog(Host::eSystemLogError, |
| 1939 | "error: unable to find section for section %u\n", |
| 1940 | n_sect); |
| 1941 | } |
| 1942 | } |
| 1943 | if (m_section_infos[n_sect].vm_range.Contains(file_addr)) { |
| 1944 | // Symbol is in section. |
| 1945 | return m_section_infos[n_sect].section_sp; |
| 1946 | } else if (m_section_infos[n_sect].vm_range.GetByteSize() == 0 && |
| 1947 | m_section_infos[n_sect].vm_range.GetBaseAddress() == |
| 1948 | file_addr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1949 | // Symbol is in section with zero size, but has the same start address |
| 1950 | // as the section. This can happen with linker symbols (symbols that |
| 1951 | // start with the letter 'l' or 'L'. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1952 | return m_section_infos[n_sect].section_sp; |
| 1953 | } |
| 1954 | } |
| 1955 | return m_section_list->FindSectionContainingFileAddress(file_addr); |
| 1956 | } |
| 1957 | |
| 1958 | protected: |
| 1959 | struct SectionInfo { |
| 1960 | SectionInfo() : vm_range(), section_sp() {} |
| 1961 | |
| 1962 | VMRange vm_range; |
| 1963 | SectionSP section_sp; |
| 1964 | }; |
| 1965 | SectionList *m_section_list; |
| 1966 | std::vector<SectionInfo> m_section_infos; |
| 1967 | }; |
| 1968 | |
| 1969 | struct TrieEntry { |
| 1970 | TrieEntry() |
| 1971 | : name(), address(LLDB_INVALID_ADDRESS), flags(0), other(0), |
| 1972 | import_name() {} |
| 1973 | |
| 1974 | void Clear() { |
| 1975 | name.Clear(); |
| 1976 | address = LLDB_INVALID_ADDRESS; |
| 1977 | flags = 0; |
| 1978 | other = 0; |
| 1979 | import_name.Clear(); |
| 1980 | } |
| 1981 | |
| 1982 | void Dump() const { |
| 1983 | printf("0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"", |
| 1984 | static_cast<unsigned long long>(address), |
| 1985 | static_cast<unsigned long long>(flags), |
| 1986 | static_cast<unsigned long long>(other), name.GetCString()); |
| 1987 | if (import_name) |
| 1988 | printf(" -> \"%s\"\n", import_name.GetCString()); |
| 1989 | else |
| 1990 | printf("\n"); |
| 1991 | } |
| 1992 | ConstString name; |
| 1993 | uint64_t address; |
| 1994 | uint64_t flags; |
| 1995 | uint64_t other; |
| 1996 | ConstString import_name; |
| 1997 | }; |
| 1998 | |
| 1999 | struct TrieEntryWithOffset { |
| 2000 | lldb::offset_t nodeOffset; |
| 2001 | TrieEntry entry; |
| 2002 | |
| 2003 | TrieEntryWithOffset(lldb::offset_t offset) : nodeOffset(offset), entry() {} |
| 2004 | |
| 2005 | void Dump(uint32_t idx) const { |
| 2006 | printf("[%3u] 0x%16.16llx: ", idx, |
| 2007 | static_cast<unsigned long long>(nodeOffset)); |
| 2008 | entry.Dump(); |
| 2009 | } |
| 2010 | |
| 2011 | bool operator<(const TrieEntryWithOffset &other) const { |
| 2012 | return (nodeOffset < other.nodeOffset); |
| 2013 | } |
| 2014 | }; |
| 2015 | |
| 2016 | static bool ParseTrieEntries(DataExtractor &data, lldb::offset_t offset, |
| 2017 | const bool is_arm, |
| 2018 | std::vector<llvm::StringRef> &nameSlices, |
| 2019 | std::set<lldb::addr_t> &resolver_addresses, |
| 2020 | std::vector<TrieEntryWithOffset> &output) { |
| 2021 | if (!data.ValidOffset(offset)) |
Greg Clayton | 8f265f7 | 2015-10-28 20:49:34 +0000 | [diff] [blame] | 2022 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2023 | |
| 2024 | const uint64_t terminalSize = data.GetULEB128(&offset); |
| 2025 | lldb::offset_t children_offset = offset + terminalSize; |
| 2026 | if (terminalSize != 0) { |
| 2027 | TrieEntryWithOffset e(offset); |
| 2028 | e.entry.flags = data.GetULEB128(&offset); |
| 2029 | const char *import_name = NULL; |
| 2030 | if (e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) { |
| 2031 | e.entry.address = 0; |
| 2032 | e.entry.other = data.GetULEB128(&offset); // dylib ordinal |
| 2033 | import_name = data.GetCStr(&offset); |
| 2034 | } else { |
| 2035 | e.entry.address = data.GetULEB128(&offset); |
| 2036 | if (e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) { |
| 2037 | e.entry.other = data.GetULEB128(&offset); |
| 2038 | uint64_t resolver_addr = e.entry.other; |
| 2039 | if (is_arm) |
| 2040 | resolver_addr &= THUMB_ADDRESS_BIT_MASK; |
| 2041 | resolver_addresses.insert(resolver_addr); |
| 2042 | } else |
| 2043 | e.entry.other = 0; |
| 2044 | } |
| 2045 | // Only add symbols that are reexport symbols with a valid import name |
| 2046 | if (EXPORT_SYMBOL_FLAGS_REEXPORT & e.entry.flags && import_name && |
| 2047 | import_name[0]) { |
| 2048 | std::string name; |
| 2049 | if (!nameSlices.empty()) { |
| 2050 | for (auto name_slice : nameSlices) |
| 2051 | name.append(name_slice.data(), name_slice.size()); |
| 2052 | } |
| 2053 | if (name.size() > 1) { |
| 2054 | // Skip the leading '_' |
| 2055 | e.entry.name.SetCStringWithLength(name.c_str() + 1, name.size() - 1); |
| 2056 | } |
| 2057 | if (import_name) { |
| 2058 | // Skip the leading '_' |
| 2059 | e.entry.import_name.SetCString(import_name + 1); |
| 2060 | } |
| 2061 | output.push_back(e); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | const uint8_t childrenCount = data.GetU8(&children_offset); |
| 2066 | for (uint8_t i = 0; i < childrenCount; ++i) { |
| 2067 | const char *cstr = data.GetCStr(&children_offset); |
| 2068 | if (cstr) |
| 2069 | nameSlices.push_back(llvm::StringRef(cstr)); |
| 2070 | else |
| 2071 | return false; // Corrupt data |
| 2072 | lldb::offset_t childNodeOffset = data.GetULEB128(&children_offset); |
| 2073 | if (childNodeOffset) { |
| 2074 | if (!ParseTrieEntries(data, childNodeOffset, is_arm, nameSlices, |
| 2075 | resolver_addresses, output)) { |
| 2076 | return false; |
| 2077 | } |
| 2078 | } |
| 2079 | nameSlices.pop_back(); |
| 2080 | } |
| 2081 | return true; |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 2084 | // Read the UUID out of a dyld_shared_cache file on-disk. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2085 | UUID ObjectFileMachO::GetSharedCacheUUID(FileSpec dyld_shared_cache, |
| 2086 | const ByteOrder byte_order, |
| 2087 | const uint32_t addr_byte_size) { |
| 2088 | UUID dsc_uuid; |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 2089 | DataBufferSP DscData = MapFileData( |
| 2090 | dyld_shared_cache, sizeof(struct lldb_copy_dyld_cache_header_v1), 0); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 2091 | if (!DscData) |
| 2092 | return dsc_uuid; |
| 2093 | DataExtractor dsc_header_data(DscData, byte_order, addr_byte_size); |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 2094 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 2095 | char version_str[7]; |
| 2096 | lldb::offset_t offset = 0; |
| 2097 | memcpy(version_str, dsc_header_data.GetData(&offset, 6), 6); |
| 2098 | version_str[6] = '\0'; |
| 2099 | if (strcmp(version_str, "dyld_v") == 0) { |
| 2100 | offset = offsetof(struct lldb_copy_dyld_cache_header_v1, uuid); |
Pavel Labath | 2f93fd1 | 2018-06-26 15:12:20 +0000 | [diff] [blame] | 2101 | dsc_uuid = UUID::fromOptionalData( |
| 2102 | dsc_header_data.GetData(&offset, sizeof(uuid_t)), sizeof(uuid_t)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2103 | } |
Jason Molenda | fd443e9 | 2018-02-07 01:28:29 +0000 | [diff] [blame] | 2104 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); |
| 2105 | if (log && dsc_uuid.IsValid()) { |
| 2106 | log->Printf("Shared cache %s has UUID %s", dyld_shared_cache.GetPath().c_str(), |
| 2107 | dsc_uuid.GetAsString().c_str()); |
| 2108 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2109 | return dsc_uuid; |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2112 | size_t ObjectFileMachO::ParseSymtab() { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 2113 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
| 2114 | Timer scoped_timer(func_cat, "ObjectFileMachO::ParseSymtab () module = %s", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2115 | m_file.GetFilename().AsCString("")); |
| 2116 | ModuleSP module_sp(GetModule()); |
| 2117 | if (!module_sp) |
| 2118 | return 0; |
| 2119 | |
| 2120 | struct symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0}; |
| 2121 | struct linkedit_data_command function_starts_load_command = {0, 0, 0, 0}; |
| 2122 | struct dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 2123 | typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts; |
| 2124 | FunctionStarts function_starts; |
| 2125 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 2126 | uint32_t i; |
| 2127 | FileSpecList dylib_files; |
| 2128 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); |
| 2129 | static const llvm::StringRef g_objc_v2_prefix_class("_OBJC_CLASS_$_"); |
| 2130 | static const llvm::StringRef g_objc_v2_prefix_metaclass("_OBJC_METACLASS_$_"); |
| 2131 | static const llvm::StringRef g_objc_v2_prefix_ivar("_OBJC_IVAR_$_"); |
| 2132 | |
| 2133 | for (i = 0; i < m_header.ncmds; ++i) { |
| 2134 | const lldb::offset_t cmd_offset = offset; |
| 2135 | // Read in the load command and load command size |
| 2136 | struct load_command lc; |
| 2137 | if (m_data.GetU32(&offset, &lc, 2) == NULL) |
| 2138 | break; |
| 2139 | // Watch for the symbol table load command |
| 2140 | switch (lc.cmd) { |
| 2141 | case LC_SYMTAB: |
| 2142 | symtab_load_command.cmd = lc.cmd; |
| 2143 | symtab_load_command.cmdsize = lc.cmdsize; |
| 2144 | // Read in the rest of the symtab load command |
| 2145 | if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == |
| 2146 | 0) // fill in symoff, nsyms, stroff, strsize fields |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2147 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2148 | if (symtab_load_command.symoff == 0) { |
| 2149 | if (log) |
| 2150 | module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0"); |
| 2151 | return 0; |
| 2152 | } |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2154 | if (symtab_load_command.stroff == 0) { |
| 2155 | if (log) |
| 2156 | module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0"); |
| 2157 | return 0; |
| 2158 | } |
Greg Clayton | 77ccca7 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 2159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2160 | if (symtab_load_command.nsyms == 0) { |
| 2161 | if (log) |
| 2162 | module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0"); |
| 2163 | return 0; |
| 2164 | } |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2166 | if (symtab_load_command.strsize == 0) { |
| 2167 | if (log) |
| 2168 | module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0"); |
| 2169 | return 0; |
| 2170 | } |
| 2171 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2173 | case LC_DYLD_INFO: |
| 2174 | case LC_DYLD_INFO_ONLY: |
| 2175 | if (m_data.GetU32(&offset, &dyld_info.rebase_off, 10)) { |
| 2176 | dyld_info.cmd = lc.cmd; |
| 2177 | dyld_info.cmdsize = lc.cmdsize; |
| 2178 | } else { |
| 2179 | memset(&dyld_info, 0, sizeof(dyld_info)); |
| 2180 | } |
| 2181 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2183 | case LC_LOAD_DYLIB: |
| 2184 | case LC_LOAD_WEAK_DYLIB: |
| 2185 | case LC_REEXPORT_DYLIB: |
| 2186 | case LC_LOADFVMLIB: |
| 2187 | case LC_LOAD_UPWARD_DYLIB: { |
| 2188 | uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); |
| 2189 | const char *path = m_data.PeekCStr(name_offset); |
| 2190 | if (path) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 2191 | FileSpec file_spec(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2192 | // Strip the path if there is @rpath, @executable, etc so we just use |
| 2193 | // the basename |
| 2194 | if (path[0] == '@') |
| 2195 | file_spec.GetDirectory().Clear(); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2197 | if (lc.cmd == LC_REEXPORT_DYLIB) { |
| 2198 | m_reexported_dylibs.AppendIfUnique(file_spec); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2199 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2200 | |
| 2201 | dylib_files.Append(file_spec); |
| 2202 | } |
| 2203 | } break; |
| 2204 | |
| 2205 | case LC_FUNCTION_STARTS: |
| 2206 | function_starts_load_command.cmd = lc.cmd; |
| 2207 | function_starts_load_command.cmdsize = lc.cmdsize; |
| 2208 | if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == |
| 2209 | NULL) // fill in symoff, nsyms, stroff, strsize fields |
| 2210 | memset(&function_starts_load_command, 0, |
| 2211 | sizeof(function_starts_load_command)); |
| 2212 | break; |
| 2213 | |
| 2214 | default: |
| 2215 | break; |
| 2216 | } |
| 2217 | offset = cmd_offset + lc.cmdsize; |
| 2218 | } |
| 2219 | |
| 2220 | if (symtab_load_command.cmd) { |
| 2221 | Symtab *symtab = m_symtab_ap.get(); |
| 2222 | SectionList *section_list = GetSectionList(); |
| 2223 | if (section_list == NULL) |
| 2224 | return 0; |
| 2225 | |
| 2226 | const uint32_t addr_byte_size = m_data.GetAddressByteSize(); |
| 2227 | const ByteOrder byte_order = m_data.GetByteOrder(); |
| 2228 | bool bit_width_32 = addr_byte_size == 4; |
| 2229 | const size_t nlist_byte_size = |
| 2230 | bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); |
| 2231 | |
| 2232 | DataExtractor nlist_data(NULL, 0, byte_order, addr_byte_size); |
| 2233 | DataExtractor strtab_data(NULL, 0, byte_order, addr_byte_size); |
| 2234 | DataExtractor function_starts_data(NULL, 0, byte_order, addr_byte_size); |
| 2235 | DataExtractor indirect_symbol_index_data(NULL, 0, byte_order, |
| 2236 | addr_byte_size); |
| 2237 | DataExtractor dyld_trie_data(NULL, 0, byte_order, addr_byte_size); |
| 2238 | |
| 2239 | const addr_t nlist_data_byte_size = |
| 2240 | symtab_load_command.nsyms * nlist_byte_size; |
| 2241 | const addr_t strtab_data_byte_size = symtab_load_command.strsize; |
| 2242 | addr_t strtab_addr = LLDB_INVALID_ADDRESS; |
| 2243 | |
| 2244 | ProcessSP process_sp(m_process_wp.lock()); |
| 2245 | Process *process = process_sp.get(); |
| 2246 | |
| 2247 | uint32_t memory_module_load_level = eMemoryModuleLoadLevelComplete; |
| 2248 | |
| 2249 | if (process && m_header.filetype != llvm::MachO::MH_OBJECT) { |
| 2250 | Target &target = process->GetTarget(); |
| 2251 | |
| 2252 | memory_module_load_level = target.GetMemoryModuleLoadLevel(); |
| 2253 | |
| 2254 | SectionSP linkedit_section_sp( |
| 2255 | section_list->FindSectionByName(GetSegmentNameLINKEDIT())); |
| 2256 | // Reading mach file from memory in a process or core file... |
| 2257 | |
| 2258 | if (linkedit_section_sp) { |
| 2259 | addr_t linkedit_load_addr = |
| 2260 | linkedit_section_sp->GetLoadBaseAddress(&target); |
| 2261 | if (linkedit_load_addr == LLDB_INVALID_ADDRESS) { |
| 2262 | // We might be trying to access the symbol table before the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2263 | // __LINKEDIT's load address has been set in the target. We can't |
| 2264 | // fail to read the symbol table, so calculate the right address |
| 2265 | // manually |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2266 | linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage( |
| 2267 | m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get()); |
| 2268 | } |
| 2269 | |
| 2270 | const addr_t linkedit_file_offset = |
| 2271 | linkedit_section_sp->GetFileOffset(); |
| 2272 | const addr_t symoff_addr = linkedit_load_addr + |
| 2273 | symtab_load_command.symoff - |
| 2274 | linkedit_file_offset; |
| 2275 | strtab_addr = linkedit_load_addr + symtab_load_command.stroff - |
| 2276 | linkedit_file_offset; |
| 2277 | |
| 2278 | bool data_was_read = false; |
| 2279 | |
| 2280 | #if defined(__APPLE__) && \ |
| 2281 | (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
| 2282 | if (m_header.flags & 0x80000000u && |
| 2283 | process->GetAddressByteSize() == sizeof(void *)) { |
| 2284 | // This mach-o memory file is in the dyld shared cache. If this |
| 2285 | // program is not remote and this is iOS, then this process will |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2286 | // share the same shared cache as the process we are debugging and we |
| 2287 | // can read the entire __LINKEDIT from the address space in this |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2288 | // process. This is a needed optimization that is used for local iOS |
| 2289 | // debugging only since all shared libraries in the shared cache do |
| 2290 | // not have corresponding files that exist in the file system of the |
| 2291 | // device. They have been combined into a single file. This means we |
| 2292 | // always have to load these files from memory. All of the symbol and |
| 2293 | // string tables from all of the __LINKEDIT sections from the shared |
| 2294 | // libraries in the shared cache have been merged into a single large |
| 2295 | // symbol and string table. Reading all of this symbol and string |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2296 | // table data across can slow down debug launch times, so we optimize |
| 2297 | // this by reading the memory for the __LINKEDIT section from this |
| 2298 | // process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2299 | |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 2300 | UUID lldb_shared_cache; |
| 2301 | addr_t lldb_shared_cache_addr; |
| 2302 | GetLLDBSharedCacheUUID (lldb_shared_cache_addr, lldb_shared_cache); |
| 2303 | UUID process_shared_cache; |
| 2304 | addr_t process_shared_cache_addr; |
| 2305 | GetProcessSharedCacheUUID(process, process_shared_cache_addr, process_shared_cache); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2306 | bool use_lldb_cache = true; |
| 2307 | if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 2308 | (lldb_shared_cache != process_shared_cache |
| 2309 | || process_shared_cache_addr != lldb_shared_cache_addr)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2310 | use_lldb_cache = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | PlatformSP platform_sp(target.GetPlatform()); |
| 2314 | if (platform_sp && platform_sp->IsHost() && use_lldb_cache) { |
| 2315 | data_was_read = true; |
| 2316 | nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, |
| 2317 | eByteOrderLittle); |
| 2318 | strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, |
| 2319 | eByteOrderLittle); |
| 2320 | if (function_starts_load_command.cmd) { |
| 2321 | const addr_t func_start_addr = |
| 2322 | linkedit_load_addr + function_starts_load_command.dataoff - |
| 2323 | linkedit_file_offset; |
| 2324 | function_starts_data.SetData( |
| 2325 | (void *)func_start_addr, |
| 2326 | function_starts_load_command.datasize, eByteOrderLittle); |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | #endif |
| 2331 | |
| 2332 | if (!data_was_read) { |
| 2333 | // Always load dyld - the dynamic linker - from memory if we didn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2334 | // find a binary anywhere else. lldb will not register |
| 2335 | // dylib/framework/bundle loads/unloads if we don't have the dyld |
| 2336 | // symbols, we force dyld to load from memory despite the user's |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2337 | // target.memory-module-load-level setting. |
| 2338 | if (memory_module_load_level == eMemoryModuleLoadLevelComplete || |
| 2339 | m_header.filetype == llvm::MachO::MH_DYLINKER) { |
| 2340 | DataBufferSP nlist_data_sp( |
| 2341 | ReadMemory(process_sp, symoff_addr, nlist_data_byte_size)); |
| 2342 | if (nlist_data_sp) |
| 2343 | nlist_data.SetData(nlist_data_sp, 0, |
| 2344 | nlist_data_sp->GetByteSize()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2345 | if (m_dysymtab.nindirectsyms != 0) { |
| 2346 | const addr_t indirect_syms_addr = linkedit_load_addr + |
| 2347 | m_dysymtab.indirectsymoff - |
| 2348 | linkedit_file_offset; |
| 2349 | DataBufferSP indirect_syms_data_sp( |
| 2350 | ReadMemory(process_sp, indirect_syms_addr, |
| 2351 | m_dysymtab.nindirectsyms * 4)); |
| 2352 | if (indirect_syms_data_sp) |
| 2353 | indirect_symbol_index_data.SetData( |
| 2354 | indirect_syms_data_sp, 0, |
| 2355 | indirect_syms_data_sp->GetByteSize()); |
Jason Molenda | b0d33e9 | 2018-09-06 00:55:27 +0000 | [diff] [blame] | 2356 | // If this binary is outside the shared cache, |
| 2357 | // cache the string table. |
| 2358 | // Binaries in the shared cache all share a giant string table, and |
| 2359 | // we can't share the string tables across multiple ObjectFileMachO's, |
| 2360 | // so we'd end up re-reading this mega-strtab for every binary |
| 2361 | // in the shared cache - it would be a big perf problem. |
| 2362 | // For binaries outside the shared cache, it's faster to read the |
| 2363 | // entire strtab at once instead of piece-by-piece as we process |
| 2364 | // the nlist records. |
| 2365 | if ((m_header.flags & 0x80000000u) == 0) { |
| 2366 | DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, |
| 2367 | strtab_data_byte_size)); |
| 2368 | if (strtab_data_sp) { |
| 2369 | strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize()); |
| 2370 | } |
| 2371 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2372 | } |
Jim Ingham | 8a5d7a2 | 2018-08-15 23:10:32 +0000 | [diff] [blame] | 2373 | } |
| 2374 | if (memory_module_load_level >= |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2375 | eMemoryModuleLoadLevelPartial) { |
| 2376 | if (function_starts_load_command.cmd) { |
| 2377 | const addr_t func_start_addr = |
| 2378 | linkedit_load_addr + function_starts_load_command.dataoff - |
| 2379 | linkedit_file_offset; |
| 2380 | DataBufferSP func_start_data_sp( |
| 2381 | ReadMemory(process_sp, func_start_addr, |
| 2382 | function_starts_load_command.datasize)); |
| 2383 | if (func_start_data_sp) |
| 2384 | function_starts_data.SetData(func_start_data_sp, 0, |
| 2385 | func_start_data_sp->GetByteSize()); |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | } else { |
| 2391 | nlist_data.SetData(m_data, symtab_load_command.symoff, |
| 2392 | nlist_data_byte_size); |
| 2393 | strtab_data.SetData(m_data, symtab_load_command.stroff, |
| 2394 | strtab_data_byte_size); |
| 2395 | |
| 2396 | if (dyld_info.export_size > 0) { |
| 2397 | dyld_trie_data.SetData(m_data, dyld_info.export_off, |
| 2398 | dyld_info.export_size); |
| 2399 | } |
| 2400 | |
| 2401 | if (m_dysymtab.nindirectsyms != 0) { |
| 2402 | indirect_symbol_index_data.SetData(m_data, m_dysymtab.indirectsymoff, |
| 2403 | m_dysymtab.nindirectsyms * 4); |
| 2404 | } |
| 2405 | if (function_starts_load_command.cmd) { |
| 2406 | function_starts_data.SetData(m_data, |
| 2407 | function_starts_load_command.dataoff, |
| 2408 | function_starts_load_command.datasize); |
| 2409 | } |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2412 | if (nlist_data.GetByteSize() == 0 && |
| 2413 | memory_module_load_level == eMemoryModuleLoadLevelComplete) { |
| 2414 | if (log) |
| 2415 | module_sp->LogMessage(log, "failed to read nlist data"); |
| 2416 | return 0; |
| 2417 | } |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2419 | const bool have_strtab_data = strtab_data.GetByteSize() > 0; |
| 2420 | if (!have_strtab_data) { |
| 2421 | if (process) { |
| 2422 | if (strtab_addr == LLDB_INVALID_ADDRESS) { |
| 2423 | if (log) |
| 2424 | module_sp->LogMessage(log, "failed to locate the strtab in memory"); |
| 2425 | return 0; |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2426 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2427 | } else { |
| 2428 | if (log) |
| 2429 | module_sp->LogMessage(log, "failed to read strtab data"); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | } |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 2433 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2434 | const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT(); |
| 2435 | const ConstString &g_segment_name_DATA = GetSegmentNameDATA(); |
| 2436 | const ConstString &g_segment_name_DATA_DIRTY = GetSegmentNameDATA_DIRTY(); |
| 2437 | const ConstString &g_segment_name_DATA_CONST = GetSegmentNameDATA_CONST(); |
| 2438 | const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC(); |
| 2439 | const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame(); |
| 2440 | SectionSP text_section_sp( |
| 2441 | section_list->FindSectionByName(g_segment_name_TEXT)); |
| 2442 | SectionSP data_section_sp( |
| 2443 | section_list->FindSectionByName(g_segment_name_DATA)); |
| 2444 | SectionSP data_dirty_section_sp( |
| 2445 | section_list->FindSectionByName(g_segment_name_DATA_DIRTY)); |
| 2446 | SectionSP data_const_section_sp( |
| 2447 | section_list->FindSectionByName(g_segment_name_DATA_CONST)); |
| 2448 | SectionSP objc_section_sp( |
| 2449 | section_list->FindSectionByName(g_segment_name_OBJC)); |
| 2450 | SectionSP eh_frame_section_sp; |
| 2451 | if (text_section_sp.get()) |
| 2452 | eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName( |
| 2453 | g_section_name_eh_frame); |
| 2454 | else |
| 2455 | eh_frame_section_sp = |
| 2456 | section_list->FindSectionByName(g_section_name_eh_frame); |
Greg Clayton | 77ccca7 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 2457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2458 | const bool is_arm = (m_header.cputype == llvm::MachO::CPU_TYPE_ARM); |
Greg Clayton | 86eac94 | 2013-08-13 21:32:34 +0000 | [diff] [blame] | 2459 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2460 | // lldb works best if it knows the start address of all functions in a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2461 | // module. Linker symbols or debug info are normally the best source of |
| 2462 | // information for start addr / size but they may be stripped in a released |
| 2463 | // binary. Two additional sources of information exist in Mach-O binaries: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2464 | // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each |
| 2465 | // function's start address in the |
| 2466 | // binary, relative to the text section. |
| 2467 | // eh_frame - the eh_frame FDEs have the start addr & size of |
| 2468 | // each function |
| 2469 | // LC_FUNCTION_STARTS is the fastest source to read in, and is present on |
| 2470 | // all modern binaries. |
| 2471 | // Binaries built to run on older releases may need to use eh_frame |
| 2472 | // information. |
Greg Clayton | 4c82d42 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2474 | if (text_section_sp && function_starts_data.GetByteSize()) { |
| 2475 | FunctionStarts::Entry function_start_entry; |
| 2476 | function_start_entry.data = false; |
| 2477 | lldb::offset_t function_start_offset = 0; |
| 2478 | function_start_entry.addr = text_section_sp->GetFileAddress(); |
| 2479 | uint64_t delta; |
| 2480 | while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > |
| 2481 | 0) { |
| 2482 | // Now append the current entry |
| 2483 | function_start_entry.addr += delta; |
| 2484 | function_starts.Append(function_start_entry); |
| 2485 | } |
| 2486 | } else { |
| 2487 | // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2488 | // load command claiming an eh_frame but it doesn't actually have the |
| 2489 | // eh_frame content. And if we have a dSYM, we don't need to do any of |
| 2490 | // this fill-in-the-missing-symbols works anyway - the debug info should |
| 2491 | // give us all the functions in the module. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2492 | if (text_section_sp.get() && eh_frame_section_sp.get() && |
| 2493 | m_type != eTypeDebugInfo) { |
| 2494 | DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, |
Pavel Labath | 3f2a081 | 2017-06-28 09:09:19 +0000 | [diff] [blame] | 2495 | DWARFCallFrameInfo::EH); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2496 | DWARFCallFrameInfo::FunctionAddressAndSizeVector functions; |
| 2497 | eh_frame.GetFunctionAddressAndSizeVector(functions); |
| 2498 | addr_t text_base_addr = text_section_sp->GetFileAddress(); |
| 2499 | size_t count = functions.GetSize(); |
| 2500 | for (size_t i = 0; i < count; ++i) { |
| 2501 | const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = |
| 2502 | functions.GetEntryAtIndex(i); |
| 2503 | if (func) { |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2504 | FunctionStarts::Entry function_start_entry; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2505 | function_start_entry.addr = func->base - text_base_addr; |
| 2506 | function_starts.Append(function_start_entry); |
| 2507 | } |
Jason Molenda | d63d3c7 | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 2508 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | const size_t function_starts_count = function_starts.GetSize(); |
| 2513 | |
| 2514 | // For user process binaries (executables, dylibs, frameworks, bundles), if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2515 | // we don't have LC_FUNCTION_STARTS/eh_frame section in this binary, we're |
| 2516 | // going to assume the binary has been stripped. Don't allow assembly |
| 2517 | // language instruction emulation because we don't know proper function |
| 2518 | // start boundaries. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2519 | // |
| 2520 | // For all other types of binaries (kernels, stand-alone bare board |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2521 | // binaries, kexts), they may not have LC_FUNCTION_STARTS / eh_frame |
| 2522 | // sections - we should not make any assumptions about them based on that. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2523 | if (function_starts_count == 0 && CalculateStrata() == eStrataUser) { |
| 2524 | m_allow_assembly_emulation_unwind_plans = false; |
| 2525 | Log *unwind_or_symbol_log(lldb_private::GetLogIfAnyCategoriesSet( |
| 2526 | LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_UNWIND)); |
| 2527 | |
| 2528 | if (unwind_or_symbol_log) |
| 2529 | module_sp->LogMessage( |
| 2530 | unwind_or_symbol_log, |
| 2531 | "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds"); |
| 2532 | } |
| 2533 | |
| 2534 | const user_id_t TEXT_eh_frame_sectID = |
| 2535 | eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() |
| 2536 | : static_cast<user_id_t>(NO_SECT); |
| 2537 | |
| 2538 | lldb::offset_t nlist_data_offset = 0; |
| 2539 | |
| 2540 | uint32_t N_SO_index = UINT32_MAX; |
| 2541 | |
| 2542 | MachSymtabSectionInfo section_info(section_list); |
| 2543 | std::vector<uint32_t> N_FUN_indexes; |
| 2544 | std::vector<uint32_t> N_NSYM_indexes; |
| 2545 | std::vector<uint32_t> N_INCL_indexes; |
| 2546 | std::vector<uint32_t> N_BRAC_indexes; |
| 2547 | std::vector<uint32_t> N_COMM_indexes; |
| 2548 | typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap; |
| 2549 | typedef std::map<uint32_t, uint32_t> NListIndexToSymbolIndexMap; |
| 2550 | typedef std::map<const char *, uint32_t> ConstNameToSymbolIndexMap; |
| 2551 | ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; |
| 2552 | ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; |
| 2553 | ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2554 | // Any symbols that get merged into another will get an entry in this map |
| 2555 | // so we know |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2556 | NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; |
| 2557 | uint32_t nlist_idx = 0; |
| 2558 | Symbol *symbol_ptr = NULL; |
| 2559 | |
| 2560 | uint32_t sym_idx = 0; |
| 2561 | Symbol *sym = NULL; |
| 2562 | size_t num_syms = 0; |
| 2563 | std::string memory_symbol_name; |
| 2564 | uint32_t unmapped_local_symbols_found = 0; |
| 2565 | |
| 2566 | std::vector<TrieEntryWithOffset> trie_entries; |
| 2567 | std::set<lldb::addr_t> resolver_addresses; |
| 2568 | |
| 2569 | if (dyld_trie_data.GetByteSize() > 0) { |
| 2570 | std::vector<llvm::StringRef> nameSlices; |
| 2571 | ParseTrieEntries(dyld_trie_data, 0, is_arm, nameSlices, |
| 2572 | resolver_addresses, trie_entries); |
| 2573 | |
| 2574 | ConstString text_segment_name("__TEXT"); |
| 2575 | SectionSP text_segment_sp = |
| 2576 | GetSectionList()->FindSectionByName(text_segment_name); |
| 2577 | if (text_segment_sp) { |
| 2578 | const lldb::addr_t text_segment_file_addr = |
| 2579 | text_segment_sp->GetFileAddress(); |
| 2580 | if (text_segment_file_addr != LLDB_INVALID_ADDRESS) { |
| 2581 | for (auto &e : trie_entries) |
| 2582 | e.entry.address += text_segment_file_addr; |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | typedef std::set<ConstString> IndirectSymbols; |
| 2588 | IndirectSymbols indirect_symbol_names; |
| 2589 | |
| 2590 | #if defined(__APPLE__) && \ |
| 2591 | (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
| 2592 | |
| 2593 | // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2594 | // optimized by moving LOCAL symbols out of the memory mapped portion of |
| 2595 | // the DSC. The symbol information has all been retained, but it isn't |
| 2596 | // available in the normal nlist data. However, there *are* duplicate |
| 2597 | // entries of *some* |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2598 | // LOCAL symbols in the normal nlist data. To handle this situation |
| 2599 | // correctly, we must first attempt |
| 2600 | // to parse any DSC unmapped symbol information. If we find any, we set a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2601 | // flag that tells the normal nlist parser to ignore all LOCAL symbols. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2602 | |
| 2603 | if (m_header.flags & 0x80000000u) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2604 | // Before we can start mapping the DSC, we need to make certain the |
| 2605 | // target process is actually using the cache we can find. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Next we need to determine the correct path for the dyld shared cache. |
| 2608 | |
| 2609 | ArchSpec header_arch; |
| 2610 | GetArchitecture(header_arch); |
| 2611 | char dsc_path[PATH_MAX]; |
| 2612 | char dsc_path_development[PATH_MAX]; |
| 2613 | |
| 2614 | snprintf( |
| 2615 | dsc_path, sizeof(dsc_path), "%s%s%s", |
| 2616 | "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR |
| 2617 | */ |
| 2618 | "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ |
| 2619 | header_arch.GetArchitectureName()); |
| 2620 | |
| 2621 | snprintf( |
| 2622 | dsc_path_development, sizeof(dsc_path), "%s%s%s%s", |
| 2623 | "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR |
| 2624 | */ |
| 2625 | "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ |
Ilia K | 4f730dc | 2016-09-12 05:25:33 +0000 | [diff] [blame] | 2626 | header_arch.GetArchitectureName(), ".development"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2627 | |
| 2628 | FileSpec dsc_nondevelopment_filespec(dsc_path, false); |
| 2629 | FileSpec dsc_development_filespec(dsc_path_development, false); |
| 2630 | FileSpec dsc_filespec; |
| 2631 | |
| 2632 | UUID dsc_uuid; |
| 2633 | UUID process_shared_cache_uuid; |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 2634 | addr_t process_shared_cache_base_addr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2635 | |
| 2636 | if (process) { |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 2637 | GetProcessSharedCacheUUID(process, process_shared_cache_base_addr, process_shared_cache_uuid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2640 | // First see if we can find an exact match for the inferior process |
| 2641 | // shared cache UUID in the development or non-development shared caches |
| 2642 | // on disk. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2643 | if (process_shared_cache_uuid.IsValid()) { |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 2644 | if (FileSystem::Instance().Exists(dsc_development_filespec)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2645 | UUID dsc_development_uuid = GetSharedCacheUUID( |
| 2646 | dsc_development_filespec, byte_order, addr_byte_size); |
| 2647 | if (dsc_development_uuid.IsValid() && |
| 2648 | dsc_development_uuid == process_shared_cache_uuid) { |
| 2649 | dsc_filespec = dsc_development_filespec; |
| 2650 | dsc_uuid = dsc_development_uuid; |
| 2651 | } |
| 2652 | } |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 2653 | if (!dsc_uuid.IsValid() && |
| 2654 | FileSystem::Instance().Exists(dsc_nondevelopment_filespec)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2655 | UUID dsc_nondevelopment_uuid = GetSharedCacheUUID( |
| 2656 | dsc_nondevelopment_filespec, byte_order, addr_byte_size); |
| 2657 | if (dsc_nondevelopment_uuid.IsValid() && |
| 2658 | dsc_nondevelopment_uuid == process_shared_cache_uuid) { |
| 2659 | dsc_filespec = dsc_nondevelopment_filespec; |
| 2660 | dsc_uuid = dsc_nondevelopment_uuid; |
| 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | // Failing a UUID match, prefer the development dyld_shared cache if both |
| 2666 | // are present. |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 2667 | if (!FileSystem::Instance().Exists(dsc_filespec)) { |
| 2668 | if (FileSystem::Instance().Exists(dsc_development_filespec)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2669 | dsc_filespec = dsc_development_filespec; |
| 2670 | } else { |
| 2671 | dsc_filespec = dsc_nondevelopment_filespec; |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | /* The dyld_cache_header has a pointer to the |
| 2676 | dyld_cache_local_symbols_info structure (localSymbolsOffset). |
| 2677 | The dyld_cache_local_symbols_info structure gives us three things: |
| 2678 | 1. The start and count of the nlist records in the dyld_shared_cache |
| 2679 | file |
| 2680 | 2. The start and size of the strings for these nlist records |
| 2681 | 3. The start and count of dyld_cache_local_symbols_entry entries |
| 2682 | |
| 2683 | There is one dyld_cache_local_symbols_entry per dylib/framework in the |
| 2684 | dyld shared cache. |
| 2685 | The "dylibOffset" field is the Mach-O header of this dylib/framework in |
| 2686 | the dyld shared cache. |
| 2687 | The dyld_cache_local_symbols_entry also lists the start of this |
| 2688 | dylib/framework's nlist records |
| 2689 | and the count of how many nlist records there are for this |
| 2690 | dylib/framework. |
| 2691 | */ |
| 2692 | |
| 2693 | // Process the dyld shared cache header to find the unmapped symbols |
| 2694 | |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 2695 | DataBufferSP dsc_data_sp = MapFileData( |
| 2696 | dsc_filespec, sizeof(struct lldb_copy_dyld_cache_header_v1), 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2697 | if (!dsc_uuid.IsValid()) { |
| 2698 | dsc_uuid = GetSharedCacheUUID(dsc_filespec, byte_order, addr_byte_size); |
| 2699 | } |
| 2700 | if (dsc_data_sp) { |
| 2701 | DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size); |
| 2702 | |
| 2703 | bool uuid_match = true; |
| 2704 | if (dsc_uuid.IsValid() && process) { |
| 2705 | if (process_shared_cache_uuid.IsValid() && |
| 2706 | dsc_uuid != process_shared_cache_uuid) { |
| 2707 | // The on-disk dyld_shared_cache file is not the same as the one in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2708 | // this process' memory, don't use it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2709 | uuid_match = false; |
| 2710 | ModuleSP module_sp(GetModule()); |
| 2711 | if (module_sp) |
| 2712 | module_sp->ReportWarning("process shared cache does not match " |
| 2713 | "on-disk dyld_shared_cache file, some " |
| 2714 | "symbol names will be missing."); |
| 2715 | } |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2716 | } |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2717 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2718 | offset = offsetof(struct lldb_copy_dyld_cache_header_v1, mappingOffset); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2719 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2720 | uint32_t mappingOffset = dsc_header_data.GetU32(&offset); |
Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 2721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2722 | // If the mappingOffset points to a location inside the header, we've |
| 2723 | // opened an old dyld shared cache, and should not proceed further. |
| 2724 | if (uuid_match && |
| 2725 | mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)) { |
Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 2726 | |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 2727 | DataBufferSP dsc_mapping_info_data_sp = MapFileData( |
| 2728 | dsc_filespec, sizeof(struct lldb_copy_dyld_cache_mapping_info), |
| 2729 | mappingOffset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 2730 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2731 | DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, |
| 2732 | byte_order, addr_byte_size); |
| 2733 | offset = 0; |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2734 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2735 | // The File addresses (from the in-memory Mach-O load commands) for |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2736 | // the shared libraries in the shared library cache need to be |
| 2737 | // adjusted by an offset to match up with the dylibOffset identifying |
| 2738 | // field in the dyld_cache_local_symbol_entry's. This offset is |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2739 | // recorded in mapping_offset_value. |
| 2740 | const uint64_t mapping_offset_value = |
| 2741 | dsc_mapping_info_data.GetU64(&offset); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2742 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2743 | offset = offsetof(struct lldb_copy_dyld_cache_header_v1, |
| 2744 | localSymbolsOffset); |
| 2745 | uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset); |
| 2746 | uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2747 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2748 | if (localSymbolsOffset && localSymbolsSize) { |
| 2749 | // Map the local symbols |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 2750 | DataBufferSP dsc_local_symbols_data_sp = |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 2751 | MapFileData(dsc_filespec, localSymbolsSize, localSymbolsOffset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 2752 | |
| 2753 | if (dsc_local_symbols_data_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2754 | DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, |
| 2755 | byte_order, addr_byte_size); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2756 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2757 | offset = 0; |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2758 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2759 | typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap; |
| 2760 | typedef std::map<uint32_t, ConstString> SymbolIndexToName; |
| 2761 | UndefinedNameToDescMap undefined_name_to_desc; |
| 2762 | SymbolIndexToName reexport_shlib_needs_fixup; |
Jim Ingham | ea3ac27 | 2014-01-10 22:55:37 +0000 | [diff] [blame] | 2763 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2764 | // Read the local_symbols_infos struct in one shot |
| 2765 | struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info; |
| 2766 | dsc_local_symbols_data.GetU32(&offset, |
| 2767 | &local_symbols_info.nlistOffset, 6); |
Jim Ingham | ea3ac27 | 2014-01-10 22:55:37 +0000 | [diff] [blame] | 2768 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2769 | SectionSP text_section_sp( |
| 2770 | section_list->FindSectionByName(GetSegmentNameTEXT())); |
Greg Clayton | b65c629 | 2015-02-20 22:20:05 +0000 | [diff] [blame] | 2771 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2772 | uint32_t header_file_offset = |
| 2773 | (text_section_sp->GetFileAddress() - mapping_offset_value); |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2774 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2775 | offset = local_symbols_info.entriesOffset; |
| 2776 | for (uint32_t entry_index = 0; |
| 2777 | entry_index < local_symbols_info.entriesCount; |
| 2778 | entry_index++) { |
| 2779 | struct lldb_copy_dyld_cache_local_symbols_entry |
| 2780 | local_symbols_entry; |
| 2781 | local_symbols_entry.dylibOffset = |
| 2782 | dsc_local_symbols_data.GetU32(&offset); |
| 2783 | local_symbols_entry.nlistStartIndex = |
| 2784 | dsc_local_symbols_data.GetU32(&offset); |
| 2785 | local_symbols_entry.nlistCount = |
| 2786 | dsc_local_symbols_data.GetU32(&offset); |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2787 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2788 | if (header_file_offset == local_symbols_entry.dylibOffset) { |
| 2789 | unmapped_local_symbols_found = local_symbols_entry.nlistCount; |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2791 | // The normal nlist code cannot correctly size the Symbols |
| 2792 | // array, we need to allocate it here. |
| 2793 | sym = symtab->Resize( |
| 2794 | symtab_load_command.nsyms + m_dysymtab.nindirectsyms + |
| 2795 | unmapped_local_symbols_found - m_dysymtab.nlocalsym); |
| 2796 | num_syms = symtab->GetNumSymbols(); |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2797 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2798 | nlist_data_offset = |
| 2799 | local_symbols_info.nlistOffset + |
| 2800 | (nlist_byte_size * local_symbols_entry.nlistStartIndex); |
| 2801 | uint32_t string_table_offset = |
| 2802 | local_symbols_info.stringsOffset; |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2803 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2804 | for (uint32_t nlist_index = 0; |
| 2805 | nlist_index < local_symbols_entry.nlistCount; |
| 2806 | nlist_index++) { |
| 2807 | ///////////////////////////// |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 2808 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2809 | struct nlist_64 nlist; |
| 2810 | if (!dsc_local_symbols_data.ValidOffsetForDataOfSize( |
| 2811 | nlist_data_offset, nlist_byte_size)) |
| 2812 | break; |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 2813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2814 | nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked( |
| 2815 | &nlist_data_offset); |
| 2816 | nlist.n_type = dsc_local_symbols_data.GetU8_unchecked( |
| 2817 | &nlist_data_offset); |
| 2818 | nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked( |
| 2819 | &nlist_data_offset); |
| 2820 | nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked( |
| 2821 | &nlist_data_offset); |
| 2822 | nlist.n_value = |
| 2823 | dsc_local_symbols_data.GetAddress_unchecked( |
| 2824 | &nlist_data_offset); |
Jason Molenda | a5609c8 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2826 | SymbolType type = eSymbolTypeInvalid; |
| 2827 | const char *symbol_name = dsc_local_symbols_data.PeekCStr( |
| 2828 | string_table_offset + nlist.n_strx); |
Jason Molenda | f813086 | 2012-06-22 03:28:35 +0000 | [diff] [blame] | 2829 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2830 | if (symbol_name == NULL) { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2831 | // No symbol should be NULL, even the symbols with no |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2832 | // string values should have an offset zero which |
| 2833 | // points to an empty C-string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2834 | Host::SystemLog( |
| 2835 | Host::eSystemLogError, |
| 2836 | "error: DSC unmapped local symbol[%u] has invalid " |
| 2837 | "string table offset 0x%x in %s, ignoring symbol\n", |
| 2838 | entry_index, nlist.n_strx, |
| 2839 | module_sp->GetFileSpec().GetPath().c_str()); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2840 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2841 | } |
| 2842 | if (symbol_name[0] == '\0') |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2843 | symbol_name = NULL; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2844 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2845 | const char *symbol_name_non_abi_mangled = NULL; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2846 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2847 | SectionSP symbol_section; |
| 2848 | uint32_t symbol_byte_size = 0; |
| 2849 | bool add_nlist = true; |
| 2850 | bool is_debug = ((nlist.n_type & N_STAB) != 0); |
| 2851 | bool demangled_is_synthesized = false; |
| 2852 | bool is_gsym = false; |
| 2853 | bool set_value = true; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2855 | assert(sym_idx < num_syms); |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2856 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2857 | sym[sym_idx].SetDebug(is_debug); |
Greg Clayton | 29e08cb | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 2858 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2859 | if (is_debug) { |
| 2860 | switch (nlist.n_type) { |
| 2861 | case N_GSYM: |
| 2862 | // global symbol: name,,NO_SECT,type,0 |
| 2863 | // Sometimes the N_GSYM value contains the address. |
| 2864 | |
| 2865 | // FIXME: In the .o files, we have a GSYM and a debug |
| 2866 | // symbol for all the ObjC data. They |
| 2867 | // have the same address, but we want to ensure that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2868 | // we always find only the real symbol, 'cause we |
| 2869 | // don't currently correctly attribute the |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2870 | // GSYM one to the ObjCClass/Ivar/MetaClass |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2871 | // symbol type. This is a temporary hack to make |
| 2872 | // sure the ObjectiveC symbols get treated correctly. |
| 2873 | // To do this right, we should coalesce all the GSYM |
| 2874 | // & global symbols that have the same address. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2875 | |
| 2876 | is_gsym = true; |
| 2877 | sym[sym_idx].SetExternal(true); |
| 2878 | |
| 2879 | if (symbol_name && symbol_name[0] == '_' && |
| 2880 | symbol_name[1] == 'O') { |
Greg Clayton | 1e28adf | 2015-02-25 17:25:02 +0000 | [diff] [blame] | 2881 | llvm::StringRef symbol_name_ref(symbol_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2882 | if (symbol_name_ref.startswith( |
| 2883 | g_objc_v2_prefix_class)) { |
| 2884 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2885 | symbol_name = |
| 2886 | symbol_name + g_objc_v2_prefix_class.size(); |
| 2887 | type = eSymbolTypeObjCClass; |
| 2888 | demangled_is_synthesized = true; |
| 2889 | |
| 2890 | } else if (symbol_name_ref.startswith( |
| 2891 | g_objc_v2_prefix_metaclass)) { |
| 2892 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2893 | symbol_name = symbol_name + |
| 2894 | g_objc_v2_prefix_metaclass.size(); |
| 2895 | type = eSymbolTypeObjCMetaClass; |
| 2896 | demangled_is_synthesized = true; |
| 2897 | } else if (symbol_name_ref.startswith( |
| 2898 | g_objc_v2_prefix_ivar)) { |
| 2899 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2900 | symbol_name = |
| 2901 | symbol_name + g_objc_v2_prefix_ivar.size(); |
| 2902 | type = eSymbolTypeObjCIVar; |
| 2903 | demangled_is_synthesized = true; |
Greg Clayton | 1e28adf | 2015-02-25 17:25:02 +0000 | [diff] [blame] | 2904 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2905 | } else { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2906 | if (nlist.n_value != 0) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2907 | symbol_section = section_info.GetSection( |
| 2908 | nlist.n_sect, nlist.n_value); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2909 | type = eSymbolTypeData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2910 | } |
| 2911 | break; |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2912 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2913 | case N_FNAME: |
| 2914 | // procedure name (f77 kludge): name,,NO_SECT,0,0 |
| 2915 | type = eSymbolTypeCompiler; |
| 2916 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2917 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2918 | case N_FUN: |
| 2919 | // procedure: name,,n_sect,linenumber,address |
| 2920 | if (symbol_name) { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2921 | type = eSymbolTypeCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2922 | symbol_section = section_info.GetSection( |
| 2923 | nlist.n_sect, nlist.n_value); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2924 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2925 | N_FUN_addr_to_sym_idx.insert( |
| 2926 | std::make_pair(nlist.n_value, sym_idx)); |
| 2927 | // We use the current number of symbols in the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2928 | // symbol table in lieu of using nlist_idx in case |
| 2929 | // we ever start trimming entries out |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2930 | N_FUN_indexes.push_back(sym_idx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2931 | } else { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2932 | type = eSymbolTypeCompiler; |
| 2933 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2934 | if (!N_FUN_indexes.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2935 | // Copy the size of the function into the |
| 2936 | // original |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2937 | // STAB entry so we don't have |
| 2938 | // to hunt for it later |
| 2939 | symtab->SymbolAtIndex(N_FUN_indexes.back()) |
| 2940 | ->SetByteSize(nlist.n_value); |
| 2941 | N_FUN_indexes.pop_back(); |
| 2942 | // We don't really need the end function STAB as |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2943 | // it contains the size which we already placed |
| 2944 | // with the original symbol, so don't add it if |
| 2945 | // we want a minimal symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2946 | add_nlist = false; |
Greg Clayton | dacc4a9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2947 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2948 | } |
| 2949 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2950 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2951 | case N_STSYM: |
| 2952 | // static symbol: name,,n_sect,type,address |
| 2953 | N_STSYM_addr_to_sym_idx.insert( |
| 2954 | std::make_pair(nlist.n_value, sym_idx)); |
| 2955 | symbol_section = section_info.GetSection( |
| 2956 | nlist.n_sect, nlist.n_value); |
| 2957 | if (symbol_name && symbol_name[0]) { |
| 2958 | type = ObjectFile::GetSymbolTypeFromName( |
| 2959 | symbol_name + 1, eSymbolTypeData); |
| 2960 | } |
| 2961 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2962 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2963 | case N_LCSYM: |
| 2964 | // .lcomm symbol: name,,n_sect,type,address |
| 2965 | symbol_section = section_info.GetSection( |
| 2966 | nlist.n_sect, nlist.n_value); |
| 2967 | type = eSymbolTypeCommonBlock; |
| 2968 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2969 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2970 | case N_BNSYM: |
| 2971 | // We use the current number of symbols in the symbol |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2972 | // table in lieu of using nlist_idx in case we ever |
| 2973 | // start trimming entries out Skip these if we want |
| 2974 | // minimal symbol tables |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2975 | add_nlist = false; |
| 2976 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2977 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2978 | case N_ENSYM: |
| 2979 | // Set the size of the N_BNSYM to the terminating |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2980 | // index of this N_ENSYM so that we can always skip |
| 2981 | // the entire symbol if we need to navigate more |
| 2982 | // quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2983 | // Skip these if we want minimal symbol tables |
| 2984 | add_nlist = false; |
| 2985 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2986 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2987 | case N_OPT: |
| 2988 | // emitted with gcc2_compiled and in gcc source |
| 2989 | type = eSymbolTypeCompiler; |
| 2990 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2991 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2992 | case N_RSYM: |
| 2993 | // register sym: name,,NO_SECT,type,register |
| 2994 | type = eSymbolTypeVariable; |
| 2995 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 2996 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2997 | case N_SLINE: |
| 2998 | // src line: 0,,n_sect,linenumber,address |
| 2999 | symbol_section = section_info.GetSection( |
| 3000 | nlist.n_sect, nlist.n_value); |
| 3001 | type = eSymbolTypeLineEntry; |
| 3002 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3003 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3004 | case N_SSYM: |
| 3005 | // structure elt: name,,NO_SECT,type,struct_offset |
| 3006 | type = eSymbolTypeVariableType; |
| 3007 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3008 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3009 | case N_SO: |
| 3010 | // source file name |
| 3011 | type = eSymbolTypeSourceFile; |
| 3012 | if (symbol_name == NULL) { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3013 | add_nlist = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3014 | if (N_SO_index != UINT32_MAX) { |
| 3015 | // Set the size of the N_SO to the terminating |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3016 | // index of this N_SO so that we can always skip |
| 3017 | // the entire N_SO if we need to navigate more |
| 3018 | // quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3019 | symbol_ptr = symtab->SymbolAtIndex(N_SO_index); |
| 3020 | symbol_ptr->SetByteSize(sym_idx); |
| 3021 | symbol_ptr->SetSizeIsSibling(true); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3022 | } |
| 3023 | N_NSYM_indexes.clear(); |
| 3024 | N_INCL_indexes.clear(); |
| 3025 | N_BRAC_indexes.clear(); |
| 3026 | N_COMM_indexes.clear(); |
| 3027 | N_FUN_indexes.clear(); |
| 3028 | N_SO_index = UINT32_MAX; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3029 | } else { |
| 3030 | // We use the current number of symbols in the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3031 | // symbol table in lieu of using nlist_idx in case |
| 3032 | // we ever start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3033 | const bool N_SO_has_full_path = |
| 3034 | symbol_name[0] == '/'; |
| 3035 | if (N_SO_has_full_path) { |
| 3036 | if ((N_SO_index == sym_idx - 1) && |
| 3037 | ((sym_idx - 1) < num_syms)) { |
| 3038 | // We have two consecutive N_SO entries where |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3039 | // the first contains a directory and the |
| 3040 | // second contains a full path. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3041 | sym[sym_idx - 1].GetMangled().SetValue( |
| 3042 | ConstString(symbol_name), false); |
| 3043 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 3044 | add_nlist = false; |
| 3045 | } else { |
| 3046 | // This is the first entry in a N_SO that |
| 3047 | // contains a directory or |
| 3048 | // a full path to the source file |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3049 | N_SO_index = sym_idx; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3050 | } |
| 3051 | } else if ((N_SO_index == sym_idx - 1) && |
| 3052 | ((sym_idx - 1) < num_syms)) { |
| 3053 | // This is usually the second N_SO entry that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3054 | // contains just the filename, so here we combine |
| 3055 | // it with the first one if we are minimizing the |
| 3056 | // symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3057 | const char *so_path = |
| 3058 | sym[sym_idx - 1] |
| 3059 | .GetMangled() |
| 3060 | .GetDemangledName( |
| 3061 | lldb::eLanguageTypeUnknown) |
| 3062 | .AsCString(); |
| 3063 | if (so_path && so_path[0]) { |
| 3064 | std::string full_so_path(so_path); |
| 3065 | const size_t double_slash_pos = |
| 3066 | full_so_path.find("//"); |
| 3067 | if (double_slash_pos != std::string::npos) { |
| 3068 | // The linker has been generating bad N_SO |
| 3069 | // entries with doubled up paths |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3070 | // in the format "%s%s" where the first |
| 3071 | // string in the DW_AT_comp_dir, and the |
| 3072 | // second is the directory for the source |
| 3073 | // file so you end up with a path that looks |
| 3074 | // like "/tmp/src//tmp/src/" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3075 | FileSpec so_dir(so_path, false); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 3076 | if (!FileSystem::Instance().Exists(so_dir)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3077 | so_dir.SetFile( |
| 3078 | &full_so_path[double_slash_pos + 1], |
| 3079 | false); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 3080 | if (FileSystem::Instance().Exists(so_dir)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3081 | // Trim off the incorrect path |
| 3082 | full_so_path.erase(0, |
| 3083 | double_slash_pos + 1); |
| 3084 | } |
| 3085 | } |
| 3086 | } |
| 3087 | if (*full_so_path.rbegin() != '/') |
| 3088 | full_so_path += '/'; |
| 3089 | full_so_path += symbol_name; |
| 3090 | sym[sym_idx - 1].GetMangled().SetValue( |
| 3091 | ConstString(full_so_path.c_str()), false); |
| 3092 | add_nlist = false; |
| 3093 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 3094 | } |
| 3095 | } else { |
| 3096 | // This could be a relative path to a N_SO |
| 3097 | N_SO_index = sym_idx; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3098 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3099 | } |
| 3100 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3102 | case N_OSO: |
| 3103 | // object file name: name,,0,0,st_mtime |
| 3104 | type = eSymbolTypeObjectFile; |
| 3105 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3107 | case N_LSYM: |
| 3108 | // local sym: name,,NO_SECT,type,offset |
| 3109 | type = eSymbolTypeLocal; |
| 3110 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3112 | //---------------------------------------------------------------------- |
| 3113 | // INCL scopes |
| 3114 | //---------------------------------------------------------------------- |
| 3115 | case N_BINCL: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3116 | // include file beginning: name,,NO_SECT,0,sum We use |
| 3117 | // the current number of symbols in the symbol table |
| 3118 | // in lieu of using nlist_idx in case we ever start |
| 3119 | // trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3120 | N_INCL_indexes.push_back(sym_idx); |
| 3121 | type = eSymbolTypeScopeBegin; |
| 3122 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3124 | case N_EINCL: |
| 3125 | // include file end: name,,NO_SECT,0,0 |
| 3126 | // Set the size of the N_BINCL to the terminating |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3127 | // index of this N_EINCL so that we can always skip |
| 3128 | // the entire symbol if we need to navigate more |
| 3129 | // quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3130 | if (!N_INCL_indexes.empty()) { |
| 3131 | symbol_ptr = |
| 3132 | symtab->SymbolAtIndex(N_INCL_indexes.back()); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3133 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3134 | symbol_ptr->SetSizeIsSibling(true); |
| 3135 | N_INCL_indexes.pop_back(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3136 | } |
| 3137 | type = eSymbolTypeScopeEnd; |
| 3138 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3140 | case N_SOL: |
| 3141 | // #included file name: name,,n_sect,0,address |
| 3142 | type = eSymbolTypeHeaderFile; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3144 | // We currently don't use the header files on darwin |
| 3145 | add_nlist = false; |
| 3146 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3148 | case N_PARAMS: |
| 3149 | // compiler parameters: name,,NO_SECT,0,0 |
| 3150 | type = eSymbolTypeCompiler; |
| 3151 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3153 | case N_VERSION: |
| 3154 | // compiler version: name,,NO_SECT,0,0 |
| 3155 | type = eSymbolTypeCompiler; |
| 3156 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3158 | case N_OLEVEL: |
| 3159 | // compiler -O level: name,,NO_SECT,0,0 |
| 3160 | type = eSymbolTypeCompiler; |
| 3161 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3163 | case N_PSYM: |
| 3164 | // parameter: name,,NO_SECT,type,offset |
| 3165 | type = eSymbolTypeVariable; |
| 3166 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3168 | case N_ENTRY: |
| 3169 | // alternate entry: name,,n_sect,linenumber,address |
| 3170 | symbol_section = section_info.GetSection( |
| 3171 | nlist.n_sect, nlist.n_value); |
| 3172 | type = eSymbolTypeLineEntry; |
| 3173 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3175 | //---------------------------------------------------------------------- |
| 3176 | // Left and Right Braces |
| 3177 | //---------------------------------------------------------------------- |
| 3178 | case N_LBRAC: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3179 | // left bracket: 0,,NO_SECT,nesting level,address We |
| 3180 | // use the current number of symbols in the symbol |
| 3181 | // table in lieu of using nlist_idx in case we ever |
| 3182 | // start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3183 | symbol_section = section_info.GetSection( |
| 3184 | nlist.n_sect, nlist.n_value); |
| 3185 | N_BRAC_indexes.push_back(sym_idx); |
| 3186 | type = eSymbolTypeScopeBegin; |
| 3187 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3189 | case N_RBRAC: |
| 3190 | // right bracket: 0,,NO_SECT,nesting level,address |
| 3191 | // Set the size of the N_LBRAC to the terminating |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3192 | // index of this N_RBRAC so that we can always skip |
| 3193 | // the entire symbol if we need to navigate more |
| 3194 | // quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3195 | symbol_section = section_info.GetSection( |
| 3196 | nlist.n_sect, nlist.n_value); |
| 3197 | if (!N_BRAC_indexes.empty()) { |
| 3198 | symbol_ptr = |
| 3199 | symtab->SymbolAtIndex(N_BRAC_indexes.back()); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3200 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3201 | symbol_ptr->SetSizeIsSibling(true); |
| 3202 | N_BRAC_indexes.pop_back(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3203 | } |
| 3204 | type = eSymbolTypeScopeEnd; |
| 3205 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3207 | case N_EXCL: |
| 3208 | // deleted include file: name,,NO_SECT,0,sum |
| 3209 | type = eSymbolTypeHeaderFile; |
| 3210 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3212 | //---------------------------------------------------------------------- |
| 3213 | // COMM scopes |
| 3214 | //---------------------------------------------------------------------- |
| 3215 | case N_BCOMM: |
| 3216 | // begin common: name,,NO_SECT,0,0 |
| 3217 | // We use the current number of symbols in the symbol |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3218 | // table in lieu of using nlist_idx in case we ever |
| 3219 | // start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3220 | type = eSymbolTypeScopeBegin; |
| 3221 | N_COMM_indexes.push_back(sym_idx); |
| 3222 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3224 | case N_ECOML: |
| 3225 | // end common (local name): 0,,n_sect,0,address |
| 3226 | symbol_section = section_info.GetSection( |
| 3227 | nlist.n_sect, nlist.n_value); |
| 3228 | // Fall through |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3230 | case N_ECOMM: |
| 3231 | // end common: name,,n_sect,0,0 |
| 3232 | // Set the size of the N_BCOMM to the terminating |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3233 | // index of this N_ECOMM/N_ECOML so that we can |
| 3234 | // always skip the entire symbol if we need to |
| 3235 | // navigate more quickly at the source level when |
| 3236 | // parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3237 | if (!N_COMM_indexes.empty()) { |
| 3238 | symbol_ptr = |
| 3239 | symtab->SymbolAtIndex(N_COMM_indexes.back()); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3240 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3241 | symbol_ptr->SetSizeIsSibling(true); |
| 3242 | N_COMM_indexes.pop_back(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3243 | } |
| 3244 | type = eSymbolTypeScopeEnd; |
| 3245 | break; |
| 3246 | |
| 3247 | case N_LENG: |
| 3248 | // second stab entry with length information |
| 3249 | type = eSymbolTypeAdditional; |
| 3250 | break; |
| 3251 | |
| 3252 | default: |
| 3253 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3254 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3255 | } else { |
| 3256 | // uint8_t n_pext = N_PEXT & nlist.n_type; |
| 3257 | uint8_t n_type = N_TYPE & nlist.n_type; |
| 3258 | sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0); |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3260 | switch (n_type) { |
| 3261 | case N_INDR: { |
| 3262 | const char *reexport_name_cstr = |
| 3263 | strtab_data.PeekCStr(nlist.n_value); |
| 3264 | if (reexport_name_cstr && reexport_name_cstr[0]) { |
| 3265 | type = eSymbolTypeReExported; |
| 3266 | ConstString reexport_name( |
| 3267 | reexport_name_cstr + |
| 3268 | ((reexport_name_cstr[0] == '_') ? 1 : 0)); |
| 3269 | sym[sym_idx].SetReExportedSymbolName(reexport_name); |
| 3270 | set_value = false; |
| 3271 | reexport_shlib_needs_fixup[sym_idx] = reexport_name; |
| 3272 | indirect_symbol_names.insert( |
| 3273 | ConstString(symbol_name + |
| 3274 | ((symbol_name[0] == '_') ? 1 : 0))); |
| 3275 | } else |
| 3276 | type = eSymbolTypeUndefined; |
| 3277 | } break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3279 | case N_UNDF: |
| 3280 | if (symbol_name && symbol_name[0]) { |
| 3281 | ConstString undefined_name( |
| 3282 | symbol_name + |
| 3283 | ((symbol_name[0] == '_') ? 1 : 0)); |
| 3284 | undefined_name_to_desc[undefined_name] = |
| 3285 | nlist.n_desc; |
| 3286 | } |
| 3287 | // Fall through |
| 3288 | case N_PBUD: |
| 3289 | type = eSymbolTypeUndefined; |
| 3290 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3292 | case N_ABS: |
| 3293 | type = eSymbolTypeAbsolute; |
| 3294 | break; |
Greg Clayton | 60038be | 2015-02-14 00:51:13 +0000 | [diff] [blame] | 3295 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3296 | case N_SECT: { |
| 3297 | symbol_section = section_info.GetSection( |
| 3298 | nlist.n_sect, nlist.n_value); |
Jason Molenda | 62e0681 | 2016-02-16 04:14:33 +0000 | [diff] [blame] | 3299 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3300 | if (symbol_section == NULL) { |
| 3301 | // TODO: warn about this? |
| 3302 | add_nlist = false; |
| 3303 | break; |
| 3304 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3306 | if (TEXT_eh_frame_sectID == nlist.n_sect) { |
| 3307 | type = eSymbolTypeException; |
| 3308 | } else { |
| 3309 | uint32_t section_type = |
| 3310 | symbol_section->Get() & SECTION_TYPE; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3312 | switch (section_type) { |
| 3313 | case S_CSTRING_LITERALS: |
| 3314 | type = eSymbolTypeData; |
| 3315 | break; // section with only literal C strings |
| 3316 | case S_4BYTE_LITERALS: |
| 3317 | type = eSymbolTypeData; |
| 3318 | break; // section with only 4 byte literals |
| 3319 | case S_8BYTE_LITERALS: |
| 3320 | type = eSymbolTypeData; |
| 3321 | break; // section with only 8 byte literals |
| 3322 | case S_LITERAL_POINTERS: |
| 3323 | type = eSymbolTypeTrampoline; |
| 3324 | break; // section with only pointers to literals |
| 3325 | case S_NON_LAZY_SYMBOL_POINTERS: |
| 3326 | type = eSymbolTypeTrampoline; |
| 3327 | break; // section with only non-lazy symbol |
| 3328 | // pointers |
| 3329 | case S_LAZY_SYMBOL_POINTERS: |
| 3330 | type = eSymbolTypeTrampoline; |
| 3331 | break; // section with only lazy symbol pointers |
| 3332 | case S_SYMBOL_STUBS: |
| 3333 | type = eSymbolTypeTrampoline; |
| 3334 | break; // section with only symbol stubs, byte |
| 3335 | // size of stub in the reserved2 field |
| 3336 | case S_MOD_INIT_FUNC_POINTERS: |
| 3337 | type = eSymbolTypeCode; |
| 3338 | break; // section with only function pointers for |
| 3339 | // initialization |
| 3340 | case S_MOD_TERM_FUNC_POINTERS: |
| 3341 | type = eSymbolTypeCode; |
| 3342 | break; // section with only function pointers for |
| 3343 | // termination |
| 3344 | case S_INTERPOSING: |
| 3345 | type = eSymbolTypeTrampoline; |
| 3346 | break; // section with only pairs of function |
| 3347 | // pointers for interposing |
| 3348 | case S_16BYTE_LITERALS: |
| 3349 | type = eSymbolTypeData; |
| 3350 | break; // section with only 16 byte literals |
| 3351 | case S_DTRACE_DOF: |
| 3352 | type = eSymbolTypeInstrumentation; |
| 3353 | break; |
| 3354 | case S_LAZY_DYLIB_SYMBOL_POINTERS: |
| 3355 | type = eSymbolTypeTrampoline; |
| 3356 | break; |
| 3357 | default: |
| 3358 | switch (symbol_section->GetType()) { |
| 3359 | case lldb::eSectionTypeCode: |
| 3360 | type = eSymbolTypeCode; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3361 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3362 | case eSectionTypeData: |
| 3363 | case eSectionTypeDataCString: // Inlined C string |
| 3364 | // data |
| 3365 | case eSectionTypeDataCStringPointers: // Pointers |
| 3366 | // to C |
| 3367 | // string |
| 3368 | // data |
| 3369 | case eSectionTypeDataSymbolAddress: // Address of |
| 3370 | // a symbol in |
| 3371 | // the symbol |
| 3372 | // table |
| 3373 | case eSectionTypeData4: |
| 3374 | case eSectionTypeData8: |
| 3375 | case eSectionTypeData16: |
| 3376 | type = eSymbolTypeData; |
| 3377 | break; |
| 3378 | default: |
| 3379 | break; |
| 3380 | } |
| 3381 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3384 | if (type == eSymbolTypeInvalid) { |
| 3385 | const char *symbol_sect_name = |
| 3386 | symbol_section->GetName().AsCString(); |
| 3387 | if (symbol_section->IsDescendant( |
| 3388 | text_section_sp.get())) { |
| 3389 | if (symbol_section->IsClear( |
| 3390 | S_ATTR_PURE_INSTRUCTIONS | |
| 3391 | S_ATTR_SELF_MODIFYING_CODE | |
| 3392 | S_ATTR_SOME_INSTRUCTIONS)) |
| 3393 | type = eSymbolTypeData; |
| 3394 | else |
| 3395 | type = eSymbolTypeCode; |
| 3396 | } else if (symbol_section->IsDescendant( |
| 3397 | data_section_sp.get()) || |
| 3398 | symbol_section->IsDescendant( |
| 3399 | data_dirty_section_sp.get()) || |
| 3400 | symbol_section->IsDescendant( |
| 3401 | data_const_section_sp.get())) { |
| 3402 | if (symbol_sect_name && |
| 3403 | ::strstr(symbol_sect_name, "__objc") == |
| 3404 | symbol_sect_name) { |
| 3405 | type = eSymbolTypeRuntime; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3406 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3407 | if (symbol_name) { |
| 3408 | llvm::StringRef symbol_name_ref( |
| 3409 | symbol_name); |
| 3410 | if (symbol_name_ref.startswith("_OBJC_")) { |
| 3411 | static const llvm::StringRef |
| 3412 | g_objc_v2_prefix_class( |
| 3413 | "_OBJC_CLASS_$_"); |
| 3414 | static const llvm::StringRef |
| 3415 | g_objc_v2_prefix_metaclass( |
| 3416 | "_OBJC_METACLASS_$_"); |
| 3417 | static const llvm::StringRef |
| 3418 | g_objc_v2_prefix_ivar( |
| 3419 | "_OBJC_IVAR_$_"); |
| 3420 | if (symbol_name_ref.startswith( |
| 3421 | g_objc_v2_prefix_class)) { |
| 3422 | symbol_name_non_abi_mangled = |
| 3423 | symbol_name + 1; |
| 3424 | symbol_name = |
| 3425 | symbol_name + |
| 3426 | g_objc_v2_prefix_class.size(); |
| 3427 | type = eSymbolTypeObjCClass; |
| 3428 | demangled_is_synthesized = true; |
| 3429 | } else if ( |
| 3430 | symbol_name_ref.startswith( |
| 3431 | g_objc_v2_prefix_metaclass)) { |
| 3432 | symbol_name_non_abi_mangled = |
| 3433 | symbol_name + 1; |
| 3434 | symbol_name = |
| 3435 | symbol_name + |
| 3436 | g_objc_v2_prefix_metaclass.size(); |
| 3437 | type = eSymbolTypeObjCMetaClass; |
| 3438 | demangled_is_synthesized = true; |
| 3439 | } else if (symbol_name_ref.startswith( |
| 3440 | g_objc_v2_prefix_ivar)) { |
| 3441 | symbol_name_non_abi_mangled = |
| 3442 | symbol_name + 1; |
| 3443 | symbol_name = |
| 3444 | symbol_name + |
| 3445 | g_objc_v2_prefix_ivar.size(); |
| 3446 | type = eSymbolTypeObjCIVar; |
| 3447 | demangled_is_synthesized = true; |
| 3448 | } |
Greg Clayton | 38f9cc4 | 2014-06-16 22:53:16 +0000 | [diff] [blame] | 3449 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3450 | } |
| 3451 | } else if (symbol_sect_name && |
| 3452 | ::strstr(symbol_sect_name, |
| 3453 | "__gcc_except_tab") == |
| 3454 | symbol_sect_name) { |
| 3455 | type = eSymbolTypeException; |
| 3456 | } else { |
| 3457 | type = eSymbolTypeData; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3458 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3459 | } else if (symbol_sect_name && |
| 3460 | ::strstr(symbol_sect_name, |
| 3461 | "__IMPORT") == |
| 3462 | symbol_sect_name) { |
| 3463 | type = eSymbolTypeTrampoline; |
| 3464 | } else if (symbol_section->IsDescendant( |
| 3465 | objc_section_sp.get())) { |
| 3466 | type = eSymbolTypeRuntime; |
| 3467 | if (symbol_name && symbol_name[0] == '.') { |
| 3468 | llvm::StringRef symbol_name_ref(symbol_name); |
| 3469 | static const llvm::StringRef |
| 3470 | g_objc_v1_prefix_class( |
| 3471 | ".objc_class_name_"); |
| 3472 | if (symbol_name_ref.startswith( |
| 3473 | g_objc_v1_prefix_class)) { |
| 3474 | symbol_name_non_abi_mangled = symbol_name; |
| 3475 | symbol_name = symbol_name + |
| 3476 | g_objc_v1_prefix_class.size(); |
| 3477 | type = eSymbolTypeObjCClass; |
| 3478 | demangled_is_synthesized = true; |
| 3479 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3480 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3481 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3482 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3483 | } |
| 3484 | } break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3485 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3486 | } |
Greg Clayton | dacc4a9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 3487 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3488 | if (add_nlist) { |
| 3489 | uint64_t symbol_value = nlist.n_value; |
| 3490 | if (symbol_name_non_abi_mangled) { |
| 3491 | sym[sym_idx].GetMangled().SetMangledName( |
| 3492 | ConstString(symbol_name_non_abi_mangled)); |
| 3493 | sym[sym_idx].GetMangled().SetDemangledName( |
| 3494 | ConstString(symbol_name)); |
| 3495 | } else { |
| 3496 | bool symbol_name_is_mangled = false; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3497 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3498 | if (symbol_name && symbol_name[0] == '_') { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3499 | symbol_name_is_mangled = symbol_name[1] == '_'; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3500 | symbol_name++; // Skip the leading underscore |
| 3501 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3503 | if (symbol_name) { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3504 | ConstString const_symbol_name(symbol_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3505 | sym[sym_idx].GetMangled().SetValue( |
| 3506 | const_symbol_name, symbol_name_is_mangled); |
| 3507 | if (is_gsym && is_debug) { |
| 3508 | const char *gsym_name = |
| 3509 | sym[sym_idx] |
| 3510 | .GetMangled() |
| 3511 | .GetName(lldb::eLanguageTypeUnknown, |
| 3512 | Mangled::ePreferMangled) |
| 3513 | .GetCString(); |
| 3514 | if (gsym_name) |
| 3515 | N_GSYM_name_to_sym_idx[gsym_name] = sym_idx; |
| 3516 | } |
| 3517 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3518 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3519 | if (symbol_section) { |
| 3520 | const addr_t section_file_addr = |
| 3521 | symbol_section->GetFileAddress(); |
| 3522 | if (symbol_byte_size == 0 && |
| 3523 | function_starts_count > 0) { |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3524 | addr_t symbol_lookup_file_addr = nlist.n_value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3525 | // Do an exact address match for non-ARM addresses, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3526 | // else get the closest since the symbol might be a |
| 3527 | // thumb symbol which has an address with bit zero |
| 3528 | // set |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3529 | FunctionStarts::Entry *func_start_entry = |
| 3530 | function_starts.FindEntry( |
| 3531 | symbol_lookup_file_addr, !is_arm); |
| 3532 | if (is_arm && func_start_entry) { |
| 3533 | // Verify that the function start address is the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3534 | // symbol address (ARM) or the symbol address + 1 |
| 3535 | // (thumb) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3536 | if (func_start_entry->addr != |
| 3537 | symbol_lookup_file_addr && |
| 3538 | func_start_entry->addr != |
| 3539 | (symbol_lookup_file_addr + 1)) { |
| 3540 | // Not the right entry, NULL it out... |
| 3541 | func_start_entry = NULL; |
| 3542 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3543 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3544 | if (func_start_entry) { |
| 3545 | func_start_entry->data = true; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3546 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3547 | addr_t symbol_file_addr = func_start_entry->addr; |
| 3548 | uint32_t symbol_flags = 0; |
| 3549 | if (is_arm) { |
| 3550 | if (symbol_file_addr & 1) |
| 3551 | symbol_flags = |
| 3552 | MACHO_NLIST_ARM_SYMBOL_IS_THUMB; |
| 3553 | symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; |
| 3554 | } |
| 3555 | |
| 3556 | const FunctionStarts::Entry |
| 3557 | *next_func_start_entry = |
| 3558 | function_starts.FindNextEntry( |
| 3559 | func_start_entry); |
| 3560 | const addr_t section_end_file_addr = |
| 3561 | section_file_addr + |
| 3562 | symbol_section->GetByteSize(); |
| 3563 | if (next_func_start_entry) { |
| 3564 | addr_t next_symbol_file_addr = |
| 3565 | next_func_start_entry->addr; |
| 3566 | // Be sure the clear the Thumb address bit when |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3567 | // we calculate the size from the current and |
| 3568 | // next address |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3569 | if (is_arm) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3570 | next_symbol_file_addr &= |
| 3571 | THUMB_ADDRESS_BIT_MASK; |
| 3572 | symbol_byte_size = std::min<lldb::addr_t>( |
| 3573 | next_symbol_file_addr - symbol_file_addr, |
| 3574 | section_end_file_addr - symbol_file_addr); |
| 3575 | } else { |
| 3576 | symbol_byte_size = |
| 3577 | section_end_file_addr - symbol_file_addr; |
| 3578 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3579 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3580 | } |
| 3581 | symbol_value -= section_file_addr; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3582 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3583 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3584 | if (is_debug == false) { |
| 3585 | if (type == eSymbolTypeCode) { |
| 3586 | // See if we can find a N_FUN entry for any code |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3587 | // symbols. If we do find a match, and the name |
| 3588 | // matches, then we can merge the two into just the |
| 3589 | // function symbol to avoid duplicate entries in |
| 3590 | // the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3591 | std::pair<ValueToSymbolIndexMap::const_iterator, |
| 3592 | ValueToSymbolIndexMap::const_iterator> |
| 3593 | range; |
| 3594 | range = N_FUN_addr_to_sym_idx.equal_range( |
| 3595 | nlist.n_value); |
| 3596 | if (range.first != range.second) { |
| 3597 | bool found_it = false; |
| 3598 | for (ValueToSymbolIndexMap::const_iterator pos = |
| 3599 | range.first; |
| 3600 | pos != range.second; ++pos) { |
| 3601 | if (sym[sym_idx].GetMangled().GetName( |
| 3602 | lldb::eLanguageTypeUnknown, |
| 3603 | Mangled::ePreferMangled) == |
| 3604 | sym[pos->second].GetMangled().GetName( |
| 3605 | lldb::eLanguageTypeUnknown, |
| 3606 | Mangled::ePreferMangled)) { |
| 3607 | m_nlist_idx_to_sym_idx[nlist_idx] = |
| 3608 | pos->second; |
| 3609 | // We just need the flags from the linker |
| 3610 | // symbol, so put these flags |
| 3611 | // into the N_FUN flags to avoid duplicate |
| 3612 | // symbols in the symbol table |
| 3613 | sym[pos->second].SetExternal( |
| 3614 | sym[sym_idx].IsExternal()); |
| 3615 | sym[pos->second].SetFlags(nlist.n_type << 16 | |
| 3616 | nlist.n_desc); |
| 3617 | if (resolver_addresses.find(nlist.n_value) != |
| 3618 | resolver_addresses.end()) |
| 3619 | sym[pos->second].SetType( |
| 3620 | eSymbolTypeResolver); |
| 3621 | sym[sym_idx].Clear(); |
| 3622 | found_it = true; |
| 3623 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3624 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3625 | } |
| 3626 | if (found_it) |
| 3627 | continue; |
| 3628 | } else { |
| 3629 | if (resolver_addresses.find(nlist.n_value) != |
| 3630 | resolver_addresses.end()) |
| 3631 | type = eSymbolTypeResolver; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3632 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3633 | } else if (type == eSymbolTypeData || |
| 3634 | type == eSymbolTypeObjCClass || |
| 3635 | type == eSymbolTypeObjCMetaClass || |
| 3636 | type == eSymbolTypeObjCIVar) { |
| 3637 | // See if we can find a N_STSYM entry for any data |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3638 | // symbols. If we do find a match, and the name |
| 3639 | // matches, then we can merge the two into just the |
| 3640 | // Static symbol to avoid duplicate entries in the |
| 3641 | // symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3642 | std::pair<ValueToSymbolIndexMap::const_iterator, |
| 3643 | ValueToSymbolIndexMap::const_iterator> |
| 3644 | range; |
| 3645 | range = N_STSYM_addr_to_sym_idx.equal_range( |
| 3646 | nlist.n_value); |
| 3647 | if (range.first != range.second) { |
| 3648 | bool found_it = false; |
| 3649 | for (ValueToSymbolIndexMap::const_iterator pos = |
| 3650 | range.first; |
| 3651 | pos != range.second; ++pos) { |
| 3652 | if (sym[sym_idx].GetMangled().GetName( |
| 3653 | lldb::eLanguageTypeUnknown, |
| 3654 | Mangled::ePreferMangled) == |
| 3655 | sym[pos->second].GetMangled().GetName( |
| 3656 | lldb::eLanguageTypeUnknown, |
| 3657 | Mangled::ePreferMangled)) { |
| 3658 | m_nlist_idx_to_sym_idx[nlist_idx] = |
| 3659 | pos->second; |
| 3660 | // We just need the flags from the linker |
| 3661 | // symbol, so put these flags |
| 3662 | // into the N_STSYM flags to avoid duplicate |
| 3663 | // symbols in the symbol table |
| 3664 | sym[pos->second].SetExternal( |
| 3665 | sym[sym_idx].IsExternal()); |
| 3666 | sym[pos->second].SetFlags(nlist.n_type << 16 | |
| 3667 | nlist.n_desc); |
| 3668 | sym[sym_idx].Clear(); |
| 3669 | found_it = true; |
| 3670 | break; |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3671 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3672 | } |
| 3673 | if (found_it) |
| 3674 | continue; |
| 3675 | } else { |
| 3676 | const char *gsym_name = |
| 3677 | sym[sym_idx] |
| 3678 | .GetMangled() |
| 3679 | .GetName(lldb::eLanguageTypeUnknown, |
| 3680 | Mangled::ePreferMangled) |
| 3681 | .GetCString(); |
| 3682 | if (gsym_name) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3683 | // Combine N_GSYM stab entries with the non |
| 3684 | // stab symbol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3685 | ConstNameToSymbolIndexMap::const_iterator pos = |
| 3686 | N_GSYM_name_to_sym_idx.find(gsym_name); |
| 3687 | if (pos != N_GSYM_name_to_sym_idx.end()) { |
| 3688 | const uint32_t GSYM_sym_idx = pos->second; |
| 3689 | m_nlist_idx_to_sym_idx[nlist_idx] = |
| 3690 | GSYM_sym_idx; |
| 3691 | // Copy the address, because often the N_GSYM |
| 3692 | // address has an invalid address of zero |
| 3693 | // when the global is a common symbol |
| 3694 | sym[GSYM_sym_idx].GetAddressRef().SetSection( |
| 3695 | symbol_section); |
| 3696 | sym[GSYM_sym_idx].GetAddressRef().SetOffset( |
| 3697 | symbol_value); |
| 3698 | // We just need the flags from the linker |
| 3699 | // symbol, so put these flags |
| 3700 | // into the N_GSYM flags to avoid duplicate |
| 3701 | // symbols in the symbol table |
| 3702 | sym[GSYM_sym_idx].SetFlags( |
| 3703 | nlist.n_type << 16 | nlist.n_desc); |
| 3704 | sym[sym_idx].Clear(); |
| 3705 | continue; |
| 3706 | } |
| 3707 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3708 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3709 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3710 | } |
Greg Clayton | fd814c5 | 2013-08-13 01:42:25 +0000 | [diff] [blame] | 3711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3712 | sym[sym_idx].SetID(nlist_idx); |
| 3713 | sym[sym_idx].SetType(type); |
| 3714 | if (set_value) { |
| 3715 | sym[sym_idx].GetAddressRef().SetSection( |
| 3716 | symbol_section); |
| 3717 | sym[sym_idx].GetAddressRef().SetOffset(symbol_value); |
Greg Clayton | 29e08cb | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3718 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3719 | sym[sym_idx].SetFlags(nlist.n_type << 16 | |
| 3720 | nlist.n_desc); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3722 | if (symbol_byte_size > 0) |
| 3723 | sym[sym_idx].SetByteSize(symbol_byte_size); |
Greg Clayton | f3bb3e4 | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3725 | if (demangled_is_synthesized) |
| 3726 | sym[sym_idx].SetDemangledNameIsSynthesized(true); |
Greg Clayton | 60038be | 2015-02-14 00:51:13 +0000 | [diff] [blame] | 3727 | ++sym_idx; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3728 | } else { |
| 3729 | sym[sym_idx].Clear(); |
| 3730 | } |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 3731 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3732 | ///////////////////////////// |
| 3733 | } |
| 3734 | break; // No more entries to consider |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 3735 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | for (const auto &pos : reexport_shlib_needs_fixup) { |
| 3739 | const auto undef_pos = undefined_name_to_desc.find(pos.second); |
| 3740 | if (undef_pos != undefined_name_to_desc.end()) { |
| 3741 | const uint8_t dylib_ordinal = |
| 3742 | llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second); |
| 3743 | if (dylib_ordinal > 0 && |
| 3744 | dylib_ordinal < dylib_files.GetSize()) |
| 3745 | sym[pos.first].SetReExportedSymbolSharedLibrary( |
| 3746 | dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1)); |
| 3747 | } |
| 3748 | } |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 3749 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3750 | } |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 3751 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3752 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3753 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3754 | |
| 3755 | // Must reset this in case it was mutated above! |
| 3756 | nlist_data_offset = 0; |
| 3757 | #endif |
| 3758 | |
| 3759 | if (nlist_data.GetByteSize() > 0) { |
| 3760 | |
| 3761 | // If the sym array was not created while parsing the DSC unmapped |
| 3762 | // symbols, create it now. |
| 3763 | if (sym == NULL) { |
| 3764 | sym = symtab->Resize(symtab_load_command.nsyms + |
| 3765 | m_dysymtab.nindirectsyms); |
| 3766 | num_syms = symtab->GetNumSymbols(); |
| 3767 | } |
| 3768 | |
| 3769 | if (unmapped_local_symbols_found) { |
| 3770 | assert(m_dysymtab.ilocalsym == 0); |
| 3771 | nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size); |
| 3772 | nlist_idx = m_dysymtab.nlocalsym; |
| 3773 | } else { |
| 3774 | nlist_idx = 0; |
| 3775 | } |
| 3776 | |
| 3777 | typedef std::map<ConstString, uint16_t> UndefinedNameToDescMap; |
| 3778 | typedef std::map<uint32_t, ConstString> SymbolIndexToName; |
| 3779 | UndefinedNameToDescMap undefined_name_to_desc; |
| 3780 | SymbolIndexToName reexport_shlib_needs_fixup; |
| 3781 | for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) { |
| 3782 | struct nlist_64 nlist; |
| 3783 | if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, |
| 3784 | nlist_byte_size)) |
| 3785 | break; |
| 3786 | |
| 3787 | nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset); |
| 3788 | nlist.n_type = nlist_data.GetU8_unchecked(&nlist_data_offset); |
| 3789 | nlist.n_sect = nlist_data.GetU8_unchecked(&nlist_data_offset); |
| 3790 | nlist.n_desc = nlist_data.GetU16_unchecked(&nlist_data_offset); |
| 3791 | nlist.n_value = nlist_data.GetAddress_unchecked(&nlist_data_offset); |
| 3792 | |
| 3793 | SymbolType type = eSymbolTypeInvalid; |
| 3794 | const char *symbol_name = NULL; |
| 3795 | |
| 3796 | if (have_strtab_data) { |
| 3797 | symbol_name = strtab_data.PeekCStr(nlist.n_strx); |
| 3798 | |
| 3799 | if (symbol_name == NULL) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3800 | // No symbol should be NULL, even the symbols with no string values |
| 3801 | // should have an offset zero which points to an empty C-string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3802 | Host::SystemLog(Host::eSystemLogError, |
| 3803 | "error: symbol[%u] has invalid string table offset " |
| 3804 | "0x%x in %s, ignoring symbol\n", |
| 3805 | nlist_idx, nlist.n_strx, |
| 3806 | module_sp->GetFileSpec().GetPath().c_str()); |
| 3807 | continue; |
| 3808 | } |
| 3809 | if (symbol_name[0] == '\0') |
| 3810 | symbol_name = NULL; |
| 3811 | } else { |
| 3812 | const addr_t str_addr = strtab_addr + nlist.n_strx; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 3813 | Status str_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3814 | if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, |
| 3815 | str_error)) |
| 3816 | symbol_name = memory_symbol_name.c_str(); |
| 3817 | } |
| 3818 | const char *symbol_name_non_abi_mangled = NULL; |
| 3819 | |
| 3820 | SectionSP symbol_section; |
| 3821 | lldb::addr_t symbol_byte_size = 0; |
| 3822 | bool add_nlist = true; |
| 3823 | bool is_gsym = false; |
| 3824 | bool is_debug = ((nlist.n_type & N_STAB) != 0); |
| 3825 | bool demangled_is_synthesized = false; |
| 3826 | bool set_value = true; |
| 3827 | assert(sym_idx < num_syms); |
| 3828 | |
| 3829 | sym[sym_idx].SetDebug(is_debug); |
| 3830 | |
| 3831 | if (is_debug) { |
| 3832 | switch (nlist.n_type) { |
| 3833 | case N_GSYM: |
| 3834 | // global symbol: name,,NO_SECT,type,0 |
| 3835 | // Sometimes the N_GSYM value contains the address. |
| 3836 | |
| 3837 | // FIXME: In the .o files, we have a GSYM and a debug symbol for all |
| 3838 | // the ObjC data. They |
| 3839 | // have the same address, but we want to ensure that we always find |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3840 | // only the real symbol, 'cause we don't currently correctly |
| 3841 | // attribute the GSYM one to the ObjCClass/Ivar/MetaClass symbol |
| 3842 | // type. This is a temporary hack to make sure the ObjectiveC |
| 3843 | // symbols get treated correctly. To do this right, we should |
| 3844 | // coalesce all the GSYM & global symbols that have the same |
| 3845 | // address. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3846 | is_gsym = true; |
| 3847 | sym[sym_idx].SetExternal(true); |
| 3848 | |
| 3849 | if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O') { |
| 3850 | llvm::StringRef symbol_name_ref(symbol_name); |
| 3851 | if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) { |
| 3852 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3853 | symbol_name = symbol_name + g_objc_v2_prefix_class.size(); |
| 3854 | type = eSymbolTypeObjCClass; |
| 3855 | demangled_is_synthesized = true; |
| 3856 | |
| 3857 | } else if (symbol_name_ref.startswith( |
| 3858 | g_objc_v2_prefix_metaclass)) { |
| 3859 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3860 | symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); |
| 3861 | type = eSymbolTypeObjCMetaClass; |
| 3862 | demangled_is_synthesized = true; |
| 3863 | } else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) { |
| 3864 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3865 | symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); |
| 3866 | type = eSymbolTypeObjCIVar; |
| 3867 | demangled_is_synthesized = true; |
| 3868 | } |
| 3869 | } else { |
| 3870 | if (nlist.n_value != 0) |
| 3871 | symbol_section = |
| 3872 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 3873 | type = eSymbolTypeData; |
| 3874 | } |
| 3875 | break; |
| 3876 | |
| 3877 | case N_FNAME: |
| 3878 | // procedure name (f77 kludge): name,,NO_SECT,0,0 |
| 3879 | type = eSymbolTypeCompiler; |
| 3880 | break; |
| 3881 | |
| 3882 | case N_FUN: |
| 3883 | // procedure: name,,n_sect,linenumber,address |
| 3884 | if (symbol_name) { |
| 3885 | type = eSymbolTypeCode; |
| 3886 | symbol_section = |
| 3887 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 3888 | |
| 3889 | N_FUN_addr_to_sym_idx.insert( |
| 3890 | std::make_pair(nlist.n_value, sym_idx)); |
| 3891 | // We use the current number of symbols in the symbol table in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3892 | // lieu of using nlist_idx in case we ever start trimming entries |
| 3893 | // out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3894 | N_FUN_indexes.push_back(sym_idx); |
| 3895 | } else { |
| 3896 | type = eSymbolTypeCompiler; |
| 3897 | |
| 3898 | if (!N_FUN_indexes.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3899 | // Copy the size of the function into the original STAB entry |
| 3900 | // so we don't have to hunt for it later |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3901 | symtab->SymbolAtIndex(N_FUN_indexes.back()) |
| 3902 | ->SetByteSize(nlist.n_value); |
| 3903 | N_FUN_indexes.pop_back(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3904 | // We don't really need the end function STAB as it contains |
| 3905 | // the size which we already placed with the original symbol, |
| 3906 | // so don't add it if we want a minimal symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3907 | add_nlist = false; |
| 3908 | } |
| 3909 | } |
| 3910 | break; |
| 3911 | |
| 3912 | case N_STSYM: |
| 3913 | // static symbol: name,,n_sect,type,address |
| 3914 | N_STSYM_addr_to_sym_idx.insert( |
| 3915 | std::make_pair(nlist.n_value, sym_idx)); |
| 3916 | symbol_section = |
| 3917 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 3918 | if (symbol_name && symbol_name[0]) { |
| 3919 | type = ObjectFile::GetSymbolTypeFromName(symbol_name + 1, |
| 3920 | eSymbolTypeData); |
| 3921 | } |
| 3922 | break; |
| 3923 | |
| 3924 | case N_LCSYM: |
| 3925 | // .lcomm symbol: name,,n_sect,type,address |
| 3926 | symbol_section = |
| 3927 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 3928 | type = eSymbolTypeCommonBlock; |
| 3929 | break; |
| 3930 | |
| 3931 | case N_BNSYM: |
| 3932 | // We use the current number of symbols in the symbol table in lieu |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3933 | // of using nlist_idx in case we ever start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3934 | // Skip these if we want minimal symbol tables |
| 3935 | add_nlist = false; |
| 3936 | break; |
| 3937 | |
| 3938 | case N_ENSYM: |
| 3939 | // Set the size of the N_BNSYM to the terminating index of this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3940 | // N_ENSYM so that we can always skip the entire symbol if we need |
| 3941 | // to navigate more quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3942 | // Skip these if we want minimal symbol tables |
| 3943 | add_nlist = false; |
| 3944 | break; |
| 3945 | |
| 3946 | case N_OPT: |
| 3947 | // emitted with gcc2_compiled and in gcc source |
| 3948 | type = eSymbolTypeCompiler; |
| 3949 | break; |
| 3950 | |
| 3951 | case N_RSYM: |
| 3952 | // register sym: name,,NO_SECT,type,register |
| 3953 | type = eSymbolTypeVariable; |
| 3954 | break; |
| 3955 | |
| 3956 | case N_SLINE: |
| 3957 | // src line: 0,,n_sect,linenumber,address |
| 3958 | symbol_section = |
| 3959 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 3960 | type = eSymbolTypeLineEntry; |
| 3961 | break; |
| 3962 | |
| 3963 | case N_SSYM: |
| 3964 | // structure elt: name,,NO_SECT,type,struct_offset |
| 3965 | type = eSymbolTypeVariableType; |
| 3966 | break; |
| 3967 | |
| 3968 | case N_SO: |
| 3969 | // source file name |
| 3970 | type = eSymbolTypeSourceFile; |
| 3971 | if (symbol_name == NULL) { |
| 3972 | add_nlist = false; |
| 3973 | if (N_SO_index != UINT32_MAX) { |
| 3974 | // Set the size of the N_SO to the terminating index of this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3975 | // N_SO so that we can always skip the entire N_SO if we need |
| 3976 | // to navigate more quickly at the source level when parsing |
| 3977 | // STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3978 | symbol_ptr = symtab->SymbolAtIndex(N_SO_index); |
| 3979 | symbol_ptr->SetByteSize(sym_idx); |
| 3980 | symbol_ptr->SetSizeIsSibling(true); |
| 3981 | } |
| 3982 | N_NSYM_indexes.clear(); |
| 3983 | N_INCL_indexes.clear(); |
| 3984 | N_BRAC_indexes.clear(); |
| 3985 | N_COMM_indexes.clear(); |
| 3986 | N_FUN_indexes.clear(); |
| 3987 | N_SO_index = UINT32_MAX; |
| 3988 | } else { |
| 3989 | // We use the current number of symbols in the symbol table in |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3990 | // lieu of using nlist_idx in case we ever start trimming entries |
| 3991 | // out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3992 | const bool N_SO_has_full_path = symbol_name[0] == '/'; |
| 3993 | if (N_SO_has_full_path) { |
| 3994 | if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) { |
| 3995 | // We have two consecutive N_SO entries where the first |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3996 | // contains a directory and the second contains a full path. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3997 | sym[sym_idx - 1].GetMangled().SetValue( |
| 3998 | ConstString(symbol_name), false); |
| 3999 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 4000 | add_nlist = false; |
| 4001 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4002 | // This is the first entry in a N_SO that contains a |
| 4003 | // directory or a full path to the source file |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4004 | N_SO_index = sym_idx; |
| 4005 | } |
| 4006 | } else if ((N_SO_index == sym_idx - 1) && |
| 4007 | ((sym_idx - 1) < num_syms)) { |
| 4008 | // This is usually the second N_SO entry that contains just the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4009 | // filename, so here we combine it with the first one if we are |
| 4010 | // minimizing the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4011 | const char *so_path = |
| 4012 | sym[sym_idx - 1] |
| 4013 | .GetMangled() |
| 4014 | .GetDemangledName(lldb::eLanguageTypeUnknown) |
| 4015 | .AsCString(); |
| 4016 | if (so_path && so_path[0]) { |
| 4017 | std::string full_so_path(so_path); |
| 4018 | const size_t double_slash_pos = full_so_path.find("//"); |
| 4019 | if (double_slash_pos != std::string::npos) { |
| 4020 | // The linker has been generating bad N_SO entries with |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4021 | // doubled up paths in the format "%s%s" where the first |
| 4022 | // string in the DW_AT_comp_dir, and the second is the |
| 4023 | // directory for the source file so you end up with a path |
| 4024 | // that looks like "/tmp/src//tmp/src/" |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 4025 | FileSpec so_dir(so_path); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 4026 | if (!FileSystem::Instance().Exists(so_dir)) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 4027 | so_dir.SetFile(&full_so_path[double_slash_pos + 1], |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 4028 | FileSpec::Style::native); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 4029 | if (FileSystem::Instance().Exists(so_dir)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4030 | // Trim off the incorrect path |
| 4031 | full_so_path.erase(0, double_slash_pos + 1); |
| 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | if (*full_so_path.rbegin() != '/') |
| 4036 | full_so_path += '/'; |
| 4037 | full_so_path += symbol_name; |
| 4038 | sym[sym_idx - 1].GetMangled().SetValue( |
| 4039 | ConstString(full_so_path.c_str()), false); |
| 4040 | add_nlist = false; |
| 4041 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 4042 | } |
| 4043 | } else { |
| 4044 | // This could be a relative path to a N_SO |
| 4045 | N_SO_index = sym_idx; |
| 4046 | } |
| 4047 | } |
| 4048 | break; |
| 4049 | |
| 4050 | case N_OSO: |
| 4051 | // object file name: name,,0,0,st_mtime |
| 4052 | type = eSymbolTypeObjectFile; |
| 4053 | break; |
| 4054 | |
| 4055 | case N_LSYM: |
| 4056 | // local sym: name,,NO_SECT,type,offset |
| 4057 | type = eSymbolTypeLocal; |
| 4058 | break; |
| 4059 | |
| 4060 | //---------------------------------------------------------------------- |
| 4061 | // INCL scopes |
| 4062 | //---------------------------------------------------------------------- |
| 4063 | case N_BINCL: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4064 | // include file beginning: name,,NO_SECT,0,sum We use the current |
| 4065 | // number of symbols in the symbol table in lieu of using nlist_idx |
| 4066 | // in case we ever start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4067 | N_INCL_indexes.push_back(sym_idx); |
| 4068 | type = eSymbolTypeScopeBegin; |
| 4069 | break; |
| 4070 | |
| 4071 | case N_EINCL: |
| 4072 | // include file end: name,,NO_SECT,0,0 |
| 4073 | // Set the size of the N_BINCL to the terminating index of this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4074 | // N_EINCL so that we can always skip the entire symbol if we need |
| 4075 | // to navigate more quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4076 | if (!N_INCL_indexes.empty()) { |
| 4077 | symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); |
| 4078 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 4079 | symbol_ptr->SetSizeIsSibling(true); |
| 4080 | N_INCL_indexes.pop_back(); |
| 4081 | } |
| 4082 | type = eSymbolTypeScopeEnd; |
| 4083 | break; |
| 4084 | |
| 4085 | case N_SOL: |
| 4086 | // #included file name: name,,n_sect,0,address |
| 4087 | type = eSymbolTypeHeaderFile; |
| 4088 | |
| 4089 | // We currently don't use the header files on darwin |
| 4090 | add_nlist = false; |
| 4091 | break; |
| 4092 | |
| 4093 | case N_PARAMS: |
| 4094 | // compiler parameters: name,,NO_SECT,0,0 |
| 4095 | type = eSymbolTypeCompiler; |
| 4096 | break; |
| 4097 | |
| 4098 | case N_VERSION: |
| 4099 | // compiler version: name,,NO_SECT,0,0 |
| 4100 | type = eSymbolTypeCompiler; |
| 4101 | break; |
| 4102 | |
| 4103 | case N_OLEVEL: |
| 4104 | // compiler -O level: name,,NO_SECT,0,0 |
| 4105 | type = eSymbolTypeCompiler; |
| 4106 | break; |
| 4107 | |
| 4108 | case N_PSYM: |
| 4109 | // parameter: name,,NO_SECT,type,offset |
| 4110 | type = eSymbolTypeVariable; |
| 4111 | break; |
| 4112 | |
| 4113 | case N_ENTRY: |
| 4114 | // alternate entry: name,,n_sect,linenumber,address |
| 4115 | symbol_section = |
| 4116 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 4117 | type = eSymbolTypeLineEntry; |
| 4118 | break; |
| 4119 | |
| 4120 | //---------------------------------------------------------------------- |
| 4121 | // Left and Right Braces |
| 4122 | //---------------------------------------------------------------------- |
| 4123 | case N_LBRAC: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4124 | // left bracket: 0,,NO_SECT,nesting level,address We use the |
| 4125 | // current number of symbols in the symbol table in lieu of using |
| 4126 | // nlist_idx in case we ever start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4127 | symbol_section = |
| 4128 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 4129 | N_BRAC_indexes.push_back(sym_idx); |
| 4130 | type = eSymbolTypeScopeBegin; |
| 4131 | break; |
| 4132 | |
| 4133 | case N_RBRAC: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4134 | // right bracket: 0,,NO_SECT,nesting level,address Set the size of |
| 4135 | // the N_LBRAC to the terminating index of this N_RBRAC so that we |
| 4136 | // can always skip the entire symbol if we need to navigate more |
| 4137 | // quickly at the source level when parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4138 | symbol_section = |
| 4139 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 4140 | if (!N_BRAC_indexes.empty()) { |
| 4141 | symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); |
| 4142 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 4143 | symbol_ptr->SetSizeIsSibling(true); |
| 4144 | N_BRAC_indexes.pop_back(); |
| 4145 | } |
| 4146 | type = eSymbolTypeScopeEnd; |
| 4147 | break; |
| 4148 | |
| 4149 | case N_EXCL: |
| 4150 | // deleted include file: name,,NO_SECT,0,sum |
| 4151 | type = eSymbolTypeHeaderFile; |
| 4152 | break; |
| 4153 | |
| 4154 | //---------------------------------------------------------------------- |
| 4155 | // COMM scopes |
| 4156 | //---------------------------------------------------------------------- |
| 4157 | case N_BCOMM: |
| 4158 | // begin common: name,,NO_SECT,0,0 |
| 4159 | // We use the current number of symbols in the symbol table in lieu |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4160 | // of using nlist_idx in case we ever start trimming entries out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4161 | type = eSymbolTypeScopeBegin; |
| 4162 | N_COMM_indexes.push_back(sym_idx); |
| 4163 | break; |
| 4164 | |
| 4165 | case N_ECOML: |
| 4166 | // end common (local name): 0,,n_sect,0,address |
| 4167 | symbol_section = |
| 4168 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 4169 | LLVM_FALLTHROUGH; |
| 4170 | |
| 4171 | case N_ECOMM: |
| 4172 | // end common: name,,n_sect,0,0 |
| 4173 | // Set the size of the N_BCOMM to the terminating index of this |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4174 | // N_ECOMM/N_ECOML so that we can always skip the entire symbol if |
| 4175 | // we need to navigate more quickly at the source level when |
| 4176 | // parsing STABS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4177 | if (!N_COMM_indexes.empty()) { |
| 4178 | symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); |
| 4179 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 4180 | symbol_ptr->SetSizeIsSibling(true); |
| 4181 | N_COMM_indexes.pop_back(); |
| 4182 | } |
| 4183 | type = eSymbolTypeScopeEnd; |
| 4184 | break; |
| 4185 | |
| 4186 | case N_LENG: |
| 4187 | // second stab entry with length information |
| 4188 | type = eSymbolTypeAdditional; |
| 4189 | break; |
| 4190 | |
| 4191 | default: |
| 4192 | break; |
| 4193 | } |
| 4194 | } else { |
| 4195 | // uint8_t n_pext = N_PEXT & nlist.n_type; |
| 4196 | uint8_t n_type = N_TYPE & nlist.n_type; |
| 4197 | sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0); |
| 4198 | |
| 4199 | switch (n_type) { |
| 4200 | case N_INDR: { |
| 4201 | const char *reexport_name_cstr = |
| 4202 | strtab_data.PeekCStr(nlist.n_value); |
| 4203 | if (reexport_name_cstr && reexport_name_cstr[0]) { |
| 4204 | type = eSymbolTypeReExported; |
| 4205 | ConstString reexport_name( |
| 4206 | reexport_name_cstr + |
| 4207 | ((reexport_name_cstr[0] == '_') ? 1 : 0)); |
| 4208 | sym[sym_idx].SetReExportedSymbolName(reexport_name); |
| 4209 | set_value = false; |
| 4210 | reexport_shlib_needs_fixup[sym_idx] = reexport_name; |
| 4211 | indirect_symbol_names.insert( |
| 4212 | ConstString(symbol_name + ((symbol_name[0] == '_') ? 1 : 0))); |
| 4213 | } else |
| 4214 | type = eSymbolTypeUndefined; |
| 4215 | } break; |
| 4216 | |
| 4217 | case N_UNDF: |
| 4218 | if (symbol_name && symbol_name[0]) { |
| 4219 | ConstString undefined_name(symbol_name + |
| 4220 | ((symbol_name[0] == '_') ? 1 : 0)); |
| 4221 | undefined_name_to_desc[undefined_name] = nlist.n_desc; |
| 4222 | } |
| 4223 | LLVM_FALLTHROUGH; |
| 4224 | |
| 4225 | case N_PBUD: |
| 4226 | type = eSymbolTypeUndefined; |
| 4227 | break; |
| 4228 | |
| 4229 | case N_ABS: |
| 4230 | type = eSymbolTypeAbsolute; |
| 4231 | break; |
| 4232 | |
| 4233 | case N_SECT: { |
| 4234 | symbol_section = |
| 4235 | section_info.GetSection(nlist.n_sect, nlist.n_value); |
| 4236 | |
| 4237 | if (!symbol_section) { |
| 4238 | // TODO: warn about this? |
| 4239 | add_nlist = false; |
| 4240 | break; |
| 4241 | } |
| 4242 | |
| 4243 | if (TEXT_eh_frame_sectID == nlist.n_sect) { |
| 4244 | type = eSymbolTypeException; |
| 4245 | } else { |
| 4246 | uint32_t section_type = symbol_section->Get() & SECTION_TYPE; |
| 4247 | |
| 4248 | switch (section_type) { |
| 4249 | case S_CSTRING_LITERALS: |
| 4250 | type = eSymbolTypeData; |
| 4251 | break; // section with only literal C strings |
| 4252 | case S_4BYTE_LITERALS: |
| 4253 | type = eSymbolTypeData; |
| 4254 | break; // section with only 4 byte literals |
| 4255 | case S_8BYTE_LITERALS: |
| 4256 | type = eSymbolTypeData; |
| 4257 | break; // section with only 8 byte literals |
| 4258 | case S_LITERAL_POINTERS: |
| 4259 | type = eSymbolTypeTrampoline; |
| 4260 | break; // section with only pointers to literals |
| 4261 | case S_NON_LAZY_SYMBOL_POINTERS: |
| 4262 | type = eSymbolTypeTrampoline; |
| 4263 | break; // section with only non-lazy symbol pointers |
| 4264 | case S_LAZY_SYMBOL_POINTERS: |
| 4265 | type = eSymbolTypeTrampoline; |
| 4266 | break; // section with only lazy symbol pointers |
| 4267 | case S_SYMBOL_STUBS: |
| 4268 | type = eSymbolTypeTrampoline; |
| 4269 | break; // section with only symbol stubs, byte size of stub in |
| 4270 | // the reserved2 field |
| 4271 | case S_MOD_INIT_FUNC_POINTERS: |
| 4272 | type = eSymbolTypeCode; |
| 4273 | break; // section with only function pointers for initialization |
| 4274 | case S_MOD_TERM_FUNC_POINTERS: |
| 4275 | type = eSymbolTypeCode; |
| 4276 | break; // section with only function pointers for termination |
| 4277 | case S_INTERPOSING: |
| 4278 | type = eSymbolTypeTrampoline; |
| 4279 | break; // section with only pairs of function pointers for |
| 4280 | // interposing |
| 4281 | case S_16BYTE_LITERALS: |
| 4282 | type = eSymbolTypeData; |
| 4283 | break; // section with only 16 byte literals |
| 4284 | case S_DTRACE_DOF: |
| 4285 | type = eSymbolTypeInstrumentation; |
| 4286 | break; |
| 4287 | case S_LAZY_DYLIB_SYMBOL_POINTERS: |
| 4288 | type = eSymbolTypeTrampoline; |
| 4289 | break; |
| 4290 | default: |
| 4291 | switch (symbol_section->GetType()) { |
| 4292 | case lldb::eSectionTypeCode: |
| 4293 | type = eSymbolTypeCode; |
| 4294 | break; |
| 4295 | case eSectionTypeData: |
| 4296 | case eSectionTypeDataCString: // Inlined C string data |
| 4297 | case eSectionTypeDataCStringPointers: // Pointers to C string |
| 4298 | // data |
| 4299 | case eSectionTypeDataSymbolAddress: // Address of a symbol in |
| 4300 | // the symbol table |
| 4301 | case eSectionTypeData4: |
| 4302 | case eSectionTypeData8: |
| 4303 | case eSectionTypeData16: |
| 4304 | type = eSymbolTypeData; |
| 4305 | break; |
| 4306 | default: |
| 4307 | break; |
| 4308 | } |
| 4309 | break; |
| 4310 | } |
| 4311 | |
| 4312 | if (type == eSymbolTypeInvalid) { |
| 4313 | const char *symbol_sect_name = |
| 4314 | symbol_section->GetName().AsCString(); |
| 4315 | if (symbol_section->IsDescendant(text_section_sp.get())) { |
| 4316 | if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS | |
| 4317 | S_ATTR_SELF_MODIFYING_CODE | |
| 4318 | S_ATTR_SOME_INSTRUCTIONS)) |
| 4319 | type = eSymbolTypeData; |
| 4320 | else |
| 4321 | type = eSymbolTypeCode; |
| 4322 | } else if (symbol_section->IsDescendant( |
| 4323 | data_section_sp.get()) || |
| 4324 | symbol_section->IsDescendant( |
| 4325 | data_dirty_section_sp.get()) || |
| 4326 | symbol_section->IsDescendant( |
| 4327 | data_const_section_sp.get())) { |
| 4328 | if (symbol_sect_name && |
| 4329 | ::strstr(symbol_sect_name, "__objc") == |
| 4330 | symbol_sect_name) { |
| 4331 | type = eSymbolTypeRuntime; |
| 4332 | |
| 4333 | if (symbol_name) { |
| 4334 | llvm::StringRef symbol_name_ref(symbol_name); |
| 4335 | if (symbol_name_ref.startswith("_OBJC_")) { |
| 4336 | static const llvm::StringRef g_objc_v2_prefix_class( |
| 4337 | "_OBJC_CLASS_$_"); |
| 4338 | static const llvm::StringRef g_objc_v2_prefix_metaclass( |
| 4339 | "_OBJC_METACLASS_$_"); |
| 4340 | static const llvm::StringRef g_objc_v2_prefix_ivar( |
| 4341 | "_OBJC_IVAR_$_"); |
| 4342 | if (symbol_name_ref.startswith( |
| 4343 | g_objc_v2_prefix_class)) { |
| 4344 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 4345 | symbol_name = |
| 4346 | symbol_name + g_objc_v2_prefix_class.size(); |
| 4347 | type = eSymbolTypeObjCClass; |
| 4348 | demangled_is_synthesized = true; |
| 4349 | } else if (symbol_name_ref.startswith( |
| 4350 | g_objc_v2_prefix_metaclass)) { |
| 4351 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 4352 | symbol_name = |
| 4353 | symbol_name + g_objc_v2_prefix_metaclass.size(); |
| 4354 | type = eSymbolTypeObjCMetaClass; |
| 4355 | demangled_is_synthesized = true; |
| 4356 | } else if (symbol_name_ref.startswith( |
| 4357 | g_objc_v2_prefix_ivar)) { |
| 4358 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 4359 | symbol_name = |
| 4360 | symbol_name + g_objc_v2_prefix_ivar.size(); |
| 4361 | type = eSymbolTypeObjCIVar; |
| 4362 | demangled_is_synthesized = true; |
| 4363 | } |
| 4364 | } |
| 4365 | } |
| 4366 | } else if (symbol_sect_name && |
| 4367 | ::strstr(symbol_sect_name, "__gcc_except_tab") == |
| 4368 | symbol_sect_name) { |
| 4369 | type = eSymbolTypeException; |
| 4370 | } else { |
| 4371 | type = eSymbolTypeData; |
| 4372 | } |
| 4373 | } else if (symbol_sect_name && |
| 4374 | ::strstr(symbol_sect_name, "__IMPORT") == |
| 4375 | symbol_sect_name) { |
| 4376 | type = eSymbolTypeTrampoline; |
| 4377 | } else if (symbol_section->IsDescendant( |
| 4378 | objc_section_sp.get())) { |
| 4379 | type = eSymbolTypeRuntime; |
| 4380 | if (symbol_name && symbol_name[0] == '.') { |
| 4381 | llvm::StringRef symbol_name_ref(symbol_name); |
| 4382 | static const llvm::StringRef g_objc_v1_prefix_class( |
| 4383 | ".objc_class_name_"); |
| 4384 | if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) { |
| 4385 | symbol_name_non_abi_mangled = symbol_name; |
| 4386 | symbol_name = symbol_name + g_objc_v1_prefix_class.size(); |
| 4387 | type = eSymbolTypeObjCClass; |
| 4388 | demangled_is_synthesized = true; |
| 4389 | } |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | } |
| 4394 | } break; |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | if (add_nlist) { |
| 4399 | uint64_t symbol_value = nlist.n_value; |
| 4400 | |
| 4401 | if (symbol_name_non_abi_mangled) { |
| 4402 | sym[sym_idx].GetMangled().SetMangledName( |
| 4403 | ConstString(symbol_name_non_abi_mangled)); |
| 4404 | sym[sym_idx].GetMangled().SetDemangledName( |
| 4405 | ConstString(symbol_name)); |
| 4406 | } else { |
| 4407 | bool symbol_name_is_mangled = false; |
| 4408 | |
| 4409 | if (symbol_name && symbol_name[0] == '_') { |
| 4410 | symbol_name_is_mangled = symbol_name[1] == '_'; |
| 4411 | symbol_name++; // Skip the leading underscore |
| 4412 | } |
| 4413 | |
| 4414 | if (symbol_name) { |
| 4415 | ConstString const_symbol_name(symbol_name); |
| 4416 | sym[sym_idx].GetMangled().SetValue(const_symbol_name, |
| 4417 | symbol_name_is_mangled); |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | if (is_gsym) { |
| 4422 | const char *gsym_name = sym[sym_idx] |
| 4423 | .GetMangled() |
| 4424 | .GetName(lldb::eLanguageTypeUnknown, |
| 4425 | Mangled::ePreferMangled) |
| 4426 | .GetCString(); |
| 4427 | if (gsym_name) |
| 4428 | N_GSYM_name_to_sym_idx[gsym_name] = sym_idx; |
| 4429 | } |
| 4430 | |
| 4431 | if (symbol_section) { |
| 4432 | const addr_t section_file_addr = symbol_section->GetFileAddress(); |
| 4433 | if (symbol_byte_size == 0 && function_starts_count > 0) { |
| 4434 | addr_t symbol_lookup_file_addr = nlist.n_value; |
| 4435 | // Do an exact address match for non-ARM addresses, else get the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4436 | // closest since the symbol might be a thumb symbol which has an |
| 4437 | // address with bit zero set |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4438 | FunctionStarts::Entry *func_start_entry = |
| 4439 | function_starts.FindEntry(symbol_lookup_file_addr, !is_arm); |
| 4440 | if (is_arm && func_start_entry) { |
| 4441 | // Verify that the function start address is the symbol address |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4442 | // (ARM) or the symbol address + 1 (thumb) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4443 | if (func_start_entry->addr != symbol_lookup_file_addr && |
| 4444 | func_start_entry->addr != (symbol_lookup_file_addr + 1)) { |
| 4445 | // Not the right entry, NULL it out... |
| 4446 | func_start_entry = NULL; |
| 4447 | } |
| 4448 | } |
| 4449 | if (func_start_entry) { |
| 4450 | func_start_entry->data = true; |
| 4451 | |
| 4452 | addr_t symbol_file_addr = func_start_entry->addr; |
| 4453 | if (is_arm) |
| 4454 | symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; |
| 4455 | |
| 4456 | const FunctionStarts::Entry *next_func_start_entry = |
| 4457 | function_starts.FindNextEntry(func_start_entry); |
| 4458 | const addr_t section_end_file_addr = |
| 4459 | section_file_addr + symbol_section->GetByteSize(); |
| 4460 | if (next_func_start_entry) { |
| 4461 | addr_t next_symbol_file_addr = next_func_start_entry->addr; |
| 4462 | // Be sure the clear the Thumb address bit when we calculate |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4463 | // the size from the current and next address |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4464 | if (is_arm) |
| 4465 | next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; |
| 4466 | symbol_byte_size = std::min<lldb::addr_t>( |
| 4467 | next_symbol_file_addr - symbol_file_addr, |
| 4468 | section_end_file_addr - symbol_file_addr); |
| 4469 | } else { |
| 4470 | symbol_byte_size = section_end_file_addr - symbol_file_addr; |
| 4471 | } |
| 4472 | } |
| 4473 | } |
| 4474 | symbol_value -= section_file_addr; |
| 4475 | } |
| 4476 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 4477 | if (!is_debug) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4478 | if (type == eSymbolTypeCode) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4479 | // See if we can find a N_FUN entry for any code symbols. If we |
| 4480 | // do find a match, and the name matches, then we can merge the |
| 4481 | // two into just the function symbol to avoid duplicate entries |
| 4482 | // in the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4483 | std::pair<ValueToSymbolIndexMap::const_iterator, |
| 4484 | ValueToSymbolIndexMap::const_iterator> |
| 4485 | range; |
| 4486 | range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value); |
| 4487 | if (range.first != range.second) { |
| 4488 | bool found_it = false; |
| 4489 | for (ValueToSymbolIndexMap::const_iterator pos = range.first; |
| 4490 | pos != range.second; ++pos) { |
| 4491 | if (sym[sym_idx].GetMangled().GetName( |
| 4492 | lldb::eLanguageTypeUnknown, |
| 4493 | Mangled::ePreferMangled) == |
| 4494 | sym[pos->second].GetMangled().GetName( |
| 4495 | lldb::eLanguageTypeUnknown, |
| 4496 | Mangled::ePreferMangled)) { |
| 4497 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 4498 | // We just need the flags from the linker symbol, so put |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4499 | // these flags into the N_FUN flags to avoid duplicate |
| 4500 | // symbols in the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4501 | sym[pos->second].SetExternal(sym[sym_idx].IsExternal()); |
| 4502 | sym[pos->second].SetFlags(nlist.n_type << 16 | |
| 4503 | nlist.n_desc); |
| 4504 | if (resolver_addresses.find(nlist.n_value) != |
| 4505 | resolver_addresses.end()) |
| 4506 | sym[pos->second].SetType(eSymbolTypeResolver); |
| 4507 | sym[sym_idx].Clear(); |
| 4508 | found_it = true; |
| 4509 | break; |
| 4510 | } |
| 4511 | } |
| 4512 | if (found_it) |
| 4513 | continue; |
| 4514 | } else { |
| 4515 | if (resolver_addresses.find(nlist.n_value) != |
| 4516 | resolver_addresses.end()) |
| 4517 | type = eSymbolTypeResolver; |
| 4518 | } |
| 4519 | } else if (type == eSymbolTypeData || |
| 4520 | type == eSymbolTypeObjCClass || |
| 4521 | type == eSymbolTypeObjCMetaClass || |
| 4522 | type == eSymbolTypeObjCIVar) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4523 | // See if we can find a N_STSYM entry for any data symbols. If we |
| 4524 | // do find a match, and the name matches, then we can merge the |
| 4525 | // two into just the Static symbol to avoid duplicate entries in |
| 4526 | // the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4527 | std::pair<ValueToSymbolIndexMap::const_iterator, |
| 4528 | ValueToSymbolIndexMap::const_iterator> |
| 4529 | range; |
| 4530 | range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value); |
| 4531 | if (range.first != range.second) { |
| 4532 | bool found_it = false; |
| 4533 | for (ValueToSymbolIndexMap::const_iterator pos = range.first; |
| 4534 | pos != range.second; ++pos) { |
| 4535 | if (sym[sym_idx].GetMangled().GetName( |
| 4536 | lldb::eLanguageTypeUnknown, |
| 4537 | Mangled::ePreferMangled) == |
| 4538 | sym[pos->second].GetMangled().GetName( |
| 4539 | lldb::eLanguageTypeUnknown, |
| 4540 | Mangled::ePreferMangled)) { |
| 4541 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 4542 | // We just need the flags from the linker symbol, so put |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4543 | // these flags into the N_STSYM flags to avoid duplicate |
| 4544 | // symbols in the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4545 | sym[pos->second].SetExternal(sym[sym_idx].IsExternal()); |
| 4546 | sym[pos->second].SetFlags(nlist.n_type << 16 | |
| 4547 | nlist.n_desc); |
| 4548 | sym[sym_idx].Clear(); |
| 4549 | found_it = true; |
| 4550 | break; |
| 4551 | } |
| 4552 | } |
| 4553 | if (found_it) |
| 4554 | continue; |
| 4555 | } else { |
| 4556 | // Combine N_GSYM stab entries with the non stab symbol |
| 4557 | const char *gsym_name = sym[sym_idx] |
| 4558 | .GetMangled() |
| 4559 | .GetName(lldb::eLanguageTypeUnknown, |
| 4560 | Mangled::ePreferMangled) |
| 4561 | .GetCString(); |
| 4562 | if (gsym_name) { |
| 4563 | ConstNameToSymbolIndexMap::const_iterator pos = |
| 4564 | N_GSYM_name_to_sym_idx.find(gsym_name); |
| 4565 | if (pos != N_GSYM_name_to_sym_idx.end()) { |
| 4566 | const uint32_t GSYM_sym_idx = pos->second; |
| 4567 | m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4568 | // Copy the address, because often the N_GSYM address has |
| 4569 | // an invalid address of zero when the global is a common |
| 4570 | // symbol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4571 | sym[GSYM_sym_idx].GetAddressRef().SetSection( |
| 4572 | symbol_section); |
| 4573 | sym[GSYM_sym_idx].GetAddressRef().SetOffset(symbol_value); |
| 4574 | // We just need the flags from the linker symbol, so put |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4575 | // these flags into the N_GSYM flags to avoid duplicate |
| 4576 | // symbols in the symbol table |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4577 | sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 | |
| 4578 | nlist.n_desc); |
| 4579 | sym[sym_idx].Clear(); |
| 4580 | continue; |
| 4581 | } |
| 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | sym[sym_idx].SetID(nlist_idx); |
| 4588 | sym[sym_idx].SetType(type); |
| 4589 | if (set_value) { |
| 4590 | sym[sym_idx].GetAddressRef().SetSection(symbol_section); |
| 4591 | sym[sym_idx].GetAddressRef().SetOffset(symbol_value); |
| 4592 | } |
| 4593 | sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc); |
| 4594 | |
| 4595 | if (symbol_byte_size > 0) |
| 4596 | sym[sym_idx].SetByteSize(symbol_byte_size); |
| 4597 | |
| 4598 | if (demangled_is_synthesized) |
| 4599 | sym[sym_idx].SetDemangledNameIsSynthesized(true); |
| 4600 | |
| 4601 | ++sym_idx; |
| 4602 | } else { |
| 4603 | sym[sym_idx].Clear(); |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | for (const auto &pos : reexport_shlib_needs_fixup) { |
| 4608 | const auto undef_pos = undefined_name_to_desc.find(pos.second); |
| 4609 | if (undef_pos != undefined_name_to_desc.end()) { |
| 4610 | const uint8_t dylib_ordinal = |
| 4611 | llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second); |
| 4612 | if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize()) |
| 4613 | sym[pos.first].SetReExportedSymbolSharedLibrary( |
| 4614 | dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1)); |
| 4615 | } |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | uint32_t synthetic_sym_id = symtab_load_command.nsyms; |
| 4620 | |
| 4621 | if (function_starts_count > 0) { |
| 4622 | uint32_t num_synthetic_function_symbols = 0; |
| 4623 | for (i = 0; i < function_starts_count; ++i) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 4624 | if (!function_starts.GetEntryRef(i).data) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4625 | ++num_synthetic_function_symbols; |
| 4626 | } |
| 4627 | |
| 4628 | if (num_synthetic_function_symbols > 0) { |
| 4629 | if (num_syms < sym_idx + num_synthetic_function_symbols) { |
| 4630 | num_syms = sym_idx + num_synthetic_function_symbols; |
| 4631 | sym = symtab->Resize(num_syms); |
| 4632 | } |
| 4633 | for (i = 0; i < function_starts_count; ++i) { |
| 4634 | const FunctionStarts::Entry *func_start_entry = |
| 4635 | function_starts.GetEntryAtIndex(i); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 4636 | if (!func_start_entry->data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4637 | addr_t symbol_file_addr = func_start_entry->addr; |
| 4638 | uint32_t symbol_flags = 0; |
| 4639 | if (is_arm) { |
| 4640 | if (symbol_file_addr & 1) |
| 4641 | symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; |
| 4642 | symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; |
| 4643 | } |
| 4644 | Address symbol_addr; |
| 4645 | if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) { |
| 4646 | SectionSP symbol_section(symbol_addr.GetSection()); |
| 4647 | uint32_t symbol_byte_size = 0; |
| 4648 | if (symbol_section) { |
| 4649 | const addr_t section_file_addr = |
| 4650 | symbol_section->GetFileAddress(); |
| 4651 | const FunctionStarts::Entry *next_func_start_entry = |
| 4652 | function_starts.FindNextEntry(func_start_entry); |
| 4653 | const addr_t section_end_file_addr = |
| 4654 | section_file_addr + symbol_section->GetByteSize(); |
| 4655 | if (next_func_start_entry) { |
| 4656 | addr_t next_symbol_file_addr = next_func_start_entry->addr; |
| 4657 | if (is_arm) |
| 4658 | next_symbol_file_addr &= THUMB_ADDRESS_BIT_MASK; |
| 4659 | symbol_byte_size = std::min<lldb::addr_t>( |
| 4660 | next_symbol_file_addr - symbol_file_addr, |
| 4661 | section_end_file_addr - symbol_file_addr); |
| 4662 | } else { |
| 4663 | symbol_byte_size = section_end_file_addr - symbol_file_addr; |
| 4664 | } |
| 4665 | sym[sym_idx].SetID(synthetic_sym_id++); |
| 4666 | sym[sym_idx].GetMangled().SetDemangledName( |
| 4667 | GetNextSyntheticSymbolName()); |
| 4668 | sym[sym_idx].SetType(eSymbolTypeCode); |
| 4669 | sym[sym_idx].SetIsSynthetic(true); |
| 4670 | sym[sym_idx].GetAddressRef() = symbol_addr; |
| 4671 | if (symbol_flags) |
| 4672 | sym[sym_idx].SetFlags(symbol_flags); |
| 4673 | if (symbol_byte_size) |
| 4674 | sym[sym_idx].SetByteSize(symbol_byte_size); |
| 4675 | ++sym_idx; |
| 4676 | } |
| 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4683 | // Trim our symbols down to just what we ended up with after removing any |
| 4684 | // symbols. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4685 | if (sym_idx < num_syms) { |
| 4686 | num_syms = sym_idx; |
| 4687 | sym = symtab->Resize(num_syms); |
| 4688 | } |
| 4689 | |
| 4690 | // Now synthesize indirect symbols |
| 4691 | if (m_dysymtab.nindirectsyms != 0) { |
| 4692 | if (indirect_symbol_index_data.GetByteSize()) { |
| 4693 | NListIndexToSymbolIndexMap::const_iterator end_index_pos = |
| 4694 | m_nlist_idx_to_sym_idx.end(); |
| 4695 | |
| 4696 | for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); |
| 4697 | ++sect_idx) { |
| 4698 | if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) == |
| 4699 | S_SYMBOL_STUBS) { |
| 4700 | uint32_t symbol_stub_byte_size = |
| 4701 | m_mach_sections[sect_idx].reserved2; |
| 4702 | if (symbol_stub_byte_size == 0) |
| 4703 | continue; |
| 4704 | |
| 4705 | const uint32_t num_symbol_stubs = |
| 4706 | m_mach_sections[sect_idx].size / symbol_stub_byte_size; |
| 4707 | |
| 4708 | if (num_symbol_stubs == 0) |
| 4709 | continue; |
| 4710 | |
| 4711 | const uint32_t symbol_stub_index_offset = |
| 4712 | m_mach_sections[sect_idx].reserved1; |
| 4713 | for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; |
| 4714 | ++stub_idx) { |
| 4715 | const uint32_t symbol_stub_index = |
| 4716 | symbol_stub_index_offset + stub_idx; |
| 4717 | const lldb::addr_t symbol_stub_addr = |
| 4718 | m_mach_sections[sect_idx].addr + |
| 4719 | (stub_idx * symbol_stub_byte_size); |
| 4720 | lldb::offset_t symbol_stub_offset = symbol_stub_index * 4; |
| 4721 | if (indirect_symbol_index_data.ValidOffsetForDataOfSize( |
| 4722 | symbol_stub_offset, 4)) { |
| 4723 | const uint32_t stub_sym_id = |
| 4724 | indirect_symbol_index_data.GetU32(&symbol_stub_offset); |
| 4725 | if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL)) |
| 4726 | continue; |
| 4727 | |
| 4728 | NListIndexToSymbolIndexMap::const_iterator index_pos = |
| 4729 | m_nlist_idx_to_sym_idx.find(stub_sym_id); |
| 4730 | Symbol *stub_symbol = NULL; |
| 4731 | if (index_pos != end_index_pos) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4732 | // We have a remapping from the original nlist index to a |
| 4733 | // current symbol index, so just look this up by index |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4734 | stub_symbol = symtab->SymbolAtIndex(index_pos->second); |
| 4735 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4736 | // We need to lookup a symbol using the original nlist symbol |
| 4737 | // index since this index is coming from the S_SYMBOL_STUBS |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4738 | stub_symbol = symtab->FindSymbolByID(stub_sym_id); |
| 4739 | } |
| 4740 | |
| 4741 | if (stub_symbol) { |
| 4742 | Address so_addr(symbol_stub_addr, section_list); |
| 4743 | |
| 4744 | if (stub_symbol->GetType() == eSymbolTypeUndefined) { |
| 4745 | // Change the external symbol into a trampoline that makes |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4746 | // sense These symbols were N_UNDF N_EXT, and are useless |
| 4747 | // to us, so we can re-use them so we don't have to make up |
| 4748 | // a synthetic symbol for no good reason. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4749 | if (resolver_addresses.find(symbol_stub_addr) == |
| 4750 | resolver_addresses.end()) |
| 4751 | stub_symbol->SetType(eSymbolTypeTrampoline); |
| 4752 | else |
| 4753 | stub_symbol->SetType(eSymbolTypeResolver); |
| 4754 | stub_symbol->SetExternal(false); |
| 4755 | stub_symbol->GetAddressRef() = so_addr; |
| 4756 | stub_symbol->SetByteSize(symbol_stub_byte_size); |
| 4757 | } else { |
| 4758 | // Make a synthetic symbol to describe the trampoline stub |
| 4759 | Mangled stub_symbol_mangled_name(stub_symbol->GetMangled()); |
| 4760 | if (sym_idx >= num_syms) { |
| 4761 | sym = symtab->Resize(++num_syms); |
| 4762 | stub_symbol = NULL; // this pointer no longer valid |
| 4763 | } |
| 4764 | sym[sym_idx].SetID(synthetic_sym_id++); |
| 4765 | sym[sym_idx].GetMangled() = stub_symbol_mangled_name; |
| 4766 | if (resolver_addresses.find(symbol_stub_addr) == |
| 4767 | resolver_addresses.end()) |
| 4768 | sym[sym_idx].SetType(eSymbolTypeTrampoline); |
| 4769 | else |
| 4770 | sym[sym_idx].SetType(eSymbolTypeResolver); |
| 4771 | sym[sym_idx].SetIsSynthetic(true); |
| 4772 | sym[sym_idx].GetAddressRef() = so_addr; |
| 4773 | sym[sym_idx].SetByteSize(symbol_stub_byte_size); |
| 4774 | ++sym_idx; |
| 4775 | } |
| 4776 | } else { |
| 4777 | if (log) |
| 4778 | log->Warning("symbol stub referencing symbol table symbol " |
| 4779 | "%u that isn't in our minimal symbol table, " |
| 4780 | "fix this!!!", |
| 4781 | stub_sym_id); |
| 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | } |
| 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | if (!trie_entries.empty()) { |
| 4791 | for (const auto &e : trie_entries) { |
| 4792 | if (e.entry.import_name) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4793 | // Only add indirect symbols from the Trie entries if we didn't have |
| 4794 | // a N_INDR nlist entry for this already |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4795 | if (indirect_symbol_names.find(e.entry.name) == |
| 4796 | indirect_symbol_names.end()) { |
| 4797 | // Make a synthetic symbol to describe re-exported symbol. |
| 4798 | if (sym_idx >= num_syms) |
| 4799 | sym = symtab->Resize(++num_syms); |
| 4800 | sym[sym_idx].SetID(synthetic_sym_id++); |
| 4801 | sym[sym_idx].GetMangled() = Mangled(e.entry.name); |
| 4802 | sym[sym_idx].SetType(eSymbolTypeReExported); |
| 4803 | sym[sym_idx].SetIsSynthetic(true); |
| 4804 | sym[sym_idx].SetReExportedSymbolName(e.entry.import_name); |
| 4805 | if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize()) { |
| 4806 | sym[sym_idx].SetReExportedSymbolSharedLibrary( |
| 4807 | dylib_files.GetFileSpecAtIndex(e.entry.other - 1)); |
| 4808 | } |
| 4809 | ++sym_idx; |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | |
Davide Italiano | 407c691 | 2018-11-02 21:59:14 +0000 | [diff] [blame] | 4815 | // StreamFile s(stdout, false); |
| 4816 | // s.Printf ("Symbol table before CalculateSymbolSizes():\n"); |
| 4817 | // symtab->Dump(&s, NULL, eSortOrderNone); |
| 4818 | // Set symbol byte sizes correctly since mach-o nlist entries don't have |
| 4819 | // sizes |
| 4820 | symtab->CalculateSymbolSizes(); |
| 4821 | |
| 4822 | // s.Printf ("Symbol table after CalculateSymbolSizes():\n"); |
| 4823 | // symtab->Dump(&s, NULL, eSortOrderNone); |
| 4824 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4825 | return symtab->GetNumSymbols(); |
| 4826 | } |
| 4827 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4830 | void ObjectFileMachO::Dump(Stream *s) { |
| 4831 | ModuleSP module_sp(GetModule()); |
| 4832 | if (module_sp) { |
| 4833 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 4834 | s->Printf("%p: ", static_cast<void *>(this)); |
| 4835 | s->Indent(); |
| 4836 | if (m_header.magic == MH_MAGIC_64 || m_header.magic == MH_CIGAM_64) |
| 4837 | s->PutCString("ObjectFileMachO64"); |
| 4838 | else |
| 4839 | s->PutCString("ObjectFileMachO32"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4841 | ArchSpec header_arch; |
| 4842 | GetArchitecture(header_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4843 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4844 | *s << ", file = '" << m_file |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4845 | << "', triple = " << header_arch.GetTriple().getTriple() << "\n"; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4846 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4847 | SectionList *sections = GetSectionList(); |
| 4848 | if (sections) |
| 4849 | sections->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4850 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4851 | if (m_symtab_ap.get()) |
| 4852 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
| 4853 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4856 | bool ObjectFileMachO::GetUUID(const llvm::MachO::mach_header &header, |
| 4857 | const lldb_private::DataExtractor &data, |
| 4858 | lldb::offset_t lc_offset, |
| 4859 | lldb_private::UUID &uuid) { |
| 4860 | uint32_t i; |
| 4861 | struct uuid_command load_cmd; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 4862 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4863 | lldb::offset_t offset = lc_offset; |
| 4864 | for (i = 0; i < header.ncmds; ++i) { |
| 4865 | const lldb::offset_t cmd_offset = offset; |
| 4866 | if (data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 4867 | break; |
| 4868 | |
| 4869 | if (load_cmd.cmd == LC_UUID) { |
| 4870 | const uint8_t *uuid_bytes = data.PeekData(offset, 16); |
| 4871 | |
| 4872 | if (uuid_bytes) { |
| 4873 | // OpenCL on Mac OS X uses the same UUID for each of its object files. |
| 4874 | // We pretend these object files have no UUID to prevent crashing. |
| 4875 | |
| 4876 | const uint8_t opencl_uuid[] = {0x8c, 0x8e, 0xb3, 0x9b, 0x3b, 0xa8, |
| 4877 | 0x4b, 0x16, 0xb6, 0xa4, 0x27, 0x63, |
| 4878 | 0xbb, 0x14, 0xf0, 0x0d}; |
| 4879 | |
| 4880 | if (!memcmp(uuid_bytes, opencl_uuid, 16)) |
| 4881 | return false; |
| 4882 | |
Pavel Labath | 2f93fd1 | 2018-06-26 15:12:20 +0000 | [diff] [blame] | 4883 | uuid = UUID::fromOptionalData(uuid_bytes, 16); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4884 | return true; |
| 4885 | } |
| 4886 | return false; |
| 4887 | } |
| 4888 | offset = cmd_offset + load_cmd.cmdsize; |
| 4889 | } |
| 4890 | return false; |
| 4891 | } |
| 4892 | |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4893 | static llvm::StringRef GetOSName(uint32_t cmd) { |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4894 | switch (cmd) { |
| 4895 | case llvm::MachO::LC_VERSION_MIN_IPHONEOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4896 | return llvm::Triple::getOSTypeName(llvm::Triple::IOS); |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4897 | case llvm::MachO::LC_VERSION_MIN_MACOSX: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4898 | return llvm::Triple::getOSTypeName(llvm::Triple::MacOSX); |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4899 | case llvm::MachO::LC_VERSION_MIN_TVOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4900 | return llvm::Triple::getOSTypeName(llvm::Triple::TvOS); |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4901 | case llvm::MachO::LC_VERSION_MIN_WATCHOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4902 | return llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 4903 | default: |
| 4904 | llvm_unreachable("unexpected LC_VERSION load command"); |
| 4905 | } |
| 4906 | } |
| 4907 | |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4908 | namespace { |
| 4909 | struct OSEnv { |
| 4910 | llvm::StringRef os_type; |
| 4911 | llvm::StringRef environment; |
| 4912 | OSEnv(uint32_t cmd) { |
| 4913 | switch (cmd) { |
| 4914 | case PLATFORM_MACOS: |
| 4915 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::MacOSX); |
| 4916 | return; |
| 4917 | case PLATFORM_IOS: |
| 4918 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS); |
| 4919 | return; |
| 4920 | case PLATFORM_TVOS: |
| 4921 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS); |
| 4922 | return; |
| 4923 | case PLATFORM_WATCHOS: |
| 4924 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); |
| 4925 | return; |
| 4926 | // NEED_BRIDGEOS_TRIPLE case PLATFORM_BRIDGEOS: |
| 4927 | // NEED_BRIDGEOS_TRIPLE os_type = llvm::Triple::getOSTypeName(llvm::Triple::BridgeOS); |
| 4928 | // NEED_BRIDGEOS_TRIPLE return; |
| 4929 | #if defined (PLATFORM_IOSSIMULATOR) && defined (PLATFORM_TVOSSIMULATOR) && defined (PLATFORM_WATCHOSSIMULATOR) |
| 4930 | case PLATFORM_IOSSIMULATOR: |
| 4931 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS); |
| 4932 | environment = |
| 4933 | llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); |
| 4934 | return; |
| 4935 | case PLATFORM_TVOSSIMULATOR: |
| 4936 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS); |
| 4937 | environment = |
| 4938 | llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); |
| 4939 | return; |
| 4940 | case PLATFORM_WATCHOSSIMULATOR: |
| 4941 | os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS); |
| 4942 | environment = |
| 4943 | llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator); |
| 4944 | return; |
| 4945 | #endif |
| 4946 | default: { |
| 4947 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | |
| 4948 | LIBLLDB_LOG_PROCESS)); |
| 4949 | if (log) |
| 4950 | log->Printf("unsupported platform in LC_BUILD_VERSION"); |
| 4951 | } |
| 4952 | } |
| 4953 | } |
| 4954 | }; |
| 4955 | |
| 4956 | struct MinOS { |
Eric Liu | 7d2f783 | 2018-10-11 14:44:12 +0000 | [diff] [blame] | 4957 | uint32_t major_version, minor_version, patch_version; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4958 | MinOS(uint32_t version) |
Eric Liu | 7d2f783 | 2018-10-11 14:44:12 +0000 | [diff] [blame] | 4959 | : major_version(version >> 16), |
| 4960 | minor_version((version >> 8) & 0xffu), |
| 4961 | patch_version(version & 0xffu) {} |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4962 | }; |
| 4963 | } // namespace |
| 4964 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4965 | bool ObjectFileMachO::GetArchitecture(const llvm::MachO::mach_header &header, |
| 4966 | const lldb_private::DataExtractor &data, |
| 4967 | lldb::offset_t lc_offset, |
| 4968 | ArchSpec &arch) { |
| 4969 | arch.SetArchitecture(eArchTypeMachO, header.cputype, header.cpusubtype); |
| 4970 | |
| 4971 | if (arch.IsValid()) { |
| 4972 | llvm::Triple &triple = arch.GetTriple(); |
| 4973 | |
| 4974 | // Set OS to an unspecified unknown or a "*" so it can match any OS |
| 4975 | triple.setOS(llvm::Triple::UnknownOS); |
| 4976 | triple.setOSName(llvm::StringRef()); |
| 4977 | |
| 4978 | if (header.filetype == MH_PRELOAD) { |
| 4979 | if (header.cputype == CPU_TYPE_ARM) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4980 | // If this is a 32-bit arm binary, and it's a standalone binary, force |
| 4981 | // the Vendor to Apple so we don't accidentally pick up the generic |
| 4982 | // armv7 ABI at runtime. Apple's armv7 ABI always uses r7 for the |
| 4983 | // frame pointer register; most other armv7 ABIs use a combination of |
| 4984 | // r7 and r11. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4985 | triple.setVendor(llvm::Triple::Apple); |
| 4986 | } else { |
| 4987 | // Set vendor to an unspecified unknown or a "*" so it can match any |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4988 | // vendor This is required for correct behavior of EFI debugging on |
| 4989 | // x86_64 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4990 | triple.setVendor(llvm::Triple::UnknownVendor); |
| 4991 | triple.setVendorName(llvm::StringRef()); |
| 4992 | } |
| 4993 | return true; |
| 4994 | } else { |
| 4995 | struct load_command load_cmd; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4996 | llvm::SmallString<16> os_name; |
| 4997 | llvm::raw_svector_ostream os(os_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4998 | |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 4999 | // See if there is an LC_VERSION_MIN_* load command that can give |
| 5000 | // us the OS type. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5001 | lldb::offset_t offset = lc_offset; |
| 5002 | for (uint32_t i = 0; i < header.ncmds; ++i) { |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 5003 | const lldb::offset_t cmd_offset = offset; |
| 5004 | if (data.GetU32(&offset, &load_cmd, 2) == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5005 | break; |
| 5006 | |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 5007 | struct version_min_command version_min; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5008 | switch (load_cmd.cmd) { |
| 5009 | case llvm::MachO::LC_VERSION_MIN_IPHONEOS: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5010 | case llvm::MachO::LC_VERSION_MIN_MACOSX: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5011 | case llvm::MachO::LC_VERSION_MIN_TVOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5012 | case llvm::MachO::LC_VERSION_MIN_WATCHOS: { |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 5013 | if (load_cmd.cmdsize != sizeof(version_min)) |
| 5014 | break; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5015 | if (data.ExtractBytes(cmd_offset, sizeof(version_min), |
| 5016 | data.GetByteOrder(), &version_min) == 0) |
| 5017 | break; |
| 5018 | MinOS min_os(version_min.version); |
Eric Liu | 7d2f783 | 2018-10-11 14:44:12 +0000 | [diff] [blame] | 5019 | os << GetOSName(load_cmd.cmd) << min_os.major_version << '.' |
| 5020 | << min_os.minor_version << '.' << min_os.patch_version; |
Adrian Prantl | 9702c96 | 2018-05-11 00:41:20 +0000 | [diff] [blame] | 5021 | triple.setOSName(os.str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5022 | return true; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5023 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5024 | default: |
| 5025 | break; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 5026 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5027 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 5028 | offset = cmd_offset + load_cmd.cmdsize; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5031 | // See if there is an LC_BUILD_VERSION load command that can give |
| 5032 | // us the OS type. |
| 5033 | |
| 5034 | offset = lc_offset; |
| 5035 | for (uint32_t i = 0; i < header.ncmds; ++i) { |
| 5036 | const lldb::offset_t cmd_offset = offset; |
| 5037 | if (data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 5038 | break; |
Adrian Prantl | 874e32d | 2018-11-13 23:14:37 +0000 | [diff] [blame] | 5039 | do { |
| 5040 | if (load_cmd.cmd == llvm::MachO::LC_BUILD_VERSION) { |
| 5041 | struct build_version_command build_version; |
| 5042 | if (load_cmd.cmdsize < sizeof(build_version)) { |
| 5043 | // Malformed load command. |
| 5044 | break; |
| 5045 | } |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5046 | if (data.ExtractBytes(cmd_offset, sizeof(build_version), |
| 5047 | data.GetByteOrder(), &build_version) == 0) |
Adrian Prantl | 874e32d | 2018-11-13 23:14:37 +0000 | [diff] [blame] | 5048 | break; |
| 5049 | MinOS min_os(build_version.minos); |
| 5050 | OSEnv os_env(build_version.platform); |
| 5051 | if (os_env.os_type.empty()) |
| 5052 | break; |
| 5053 | os << os_env.os_type << min_os.major_version << '.' |
| 5054 | << min_os.minor_version << '.' << min_os.patch_version; |
| 5055 | triple.setOSName(os.str()); |
| 5056 | if (!os_env.environment.empty()) |
| 5057 | triple.setEnvironmentName(os_env.environment); |
| 5058 | return true; |
| 5059 | } |
| 5060 | } while (0); |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5061 | offset = cmd_offset + load_cmd.cmdsize; |
| 5062 | } |
| 5063 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5064 | if (header.filetype != MH_KEXT_BUNDLE) { |
| 5065 | // We didn't find a LC_VERSION_MIN load command and this isn't a KEXT |
| 5066 | // so lets not say our Vendor is Apple, leave it as an unspecified |
| 5067 | // unknown |
| 5068 | triple.setVendor(llvm::Triple::UnknownVendor); |
| 5069 | triple.setVendorName(llvm::StringRef()); |
| 5070 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 5071 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5072 | } |
| 5073 | return arch.IsValid(); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 5074 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5075 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5076 | bool ObjectFileMachO::GetUUID(lldb_private::UUID *uuid) { |
| 5077 | ModuleSP module_sp(GetModule()); |
| 5078 | if (module_sp) { |
| 5079 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5080 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5081 | return GetUUID(m_header, m_data, offset, *uuid); |
| 5082 | } |
| 5083 | return false; |
Greg Clayton | 7ab7f89 | 2014-05-29 21:33:45 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5086 | uint32_t ObjectFileMachO::GetDependentModules(FileSpecList &files) { |
| 5087 | uint32_t count = 0; |
| 5088 | ModuleSP module_sp(GetModule()); |
| 5089 | if (module_sp) { |
| 5090 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5091 | struct load_command load_cmd; |
| 5092 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5093 | std::vector<std::string> rpath_paths; |
| 5094 | std::vector<std::string> rpath_relative_paths; |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5095 | std::vector<std::string> at_exec_relative_paths; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5096 | uint32_t i; |
| 5097 | for (i = 0; i < m_header.ncmds; ++i) { |
| 5098 | const uint32_t cmd_offset = offset; |
| 5099 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 5100 | break; |
| 5101 | |
| 5102 | switch (load_cmd.cmd) { |
| 5103 | case LC_RPATH: |
| 5104 | case LC_LOAD_DYLIB: |
| 5105 | case LC_LOAD_WEAK_DYLIB: |
| 5106 | case LC_REEXPORT_DYLIB: |
| 5107 | case LC_LOAD_DYLINKER: |
| 5108 | case LC_LOADFVMLIB: |
| 5109 | case LC_LOAD_UPWARD_DYLIB: { |
| 5110 | uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); |
| 5111 | const char *path = m_data.PeekCStr(name_offset); |
| 5112 | if (path) { |
| 5113 | if (load_cmd.cmd == LC_RPATH) |
| 5114 | rpath_paths.push_back(path); |
| 5115 | else { |
| 5116 | if (path[0] == '@') { |
| 5117 | if (strncmp(path, "@rpath", strlen("@rpath")) == 0) |
| 5118 | rpath_relative_paths.push_back(path + strlen("@rpath")); |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5119 | else if (strncmp(path, "@executable_path", |
| 5120 | strlen("@executable_path")) == 0) |
| 5121 | at_exec_relative_paths.push_back(path |
| 5122 | + strlen("@executable_path")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5123 | } else { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 5124 | FileSpec file_spec(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5125 | if (files.AppendIfUnique(file_spec)) |
| 5126 | count++; |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | } break; |
| 5131 | |
| 5132 | default: |
| 5133 | break; |
| 5134 | } |
| 5135 | offset = cmd_offset + load_cmd.cmdsize; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5136 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5137 | |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5138 | FileSpec this_file_spec(m_file); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 5139 | FileSystem::Instance().Resolve(this_file_spec); |
| 5140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5141 | if (!rpath_paths.empty()) { |
| 5142 | // Fixup all LC_RPATH values to be absolute paths |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5143 | std::string loader_path("@loader_path"); |
| 5144 | std::string executable_path("@executable_path"); |
| 5145 | for (auto &rpath : rpath_paths) { |
| 5146 | if (rpath.find(loader_path) == 0) { |
| 5147 | rpath.erase(0, loader_path.size()); |
| 5148 | rpath.insert(0, this_file_spec.GetDirectory().GetCString()); |
| 5149 | } else if (rpath.find(executable_path) == 0) { |
| 5150 | rpath.erase(0, executable_path.size()); |
| 5151 | rpath.insert(0, this_file_spec.GetDirectory().GetCString()); |
| 5152 | } |
| 5153 | } |
| 5154 | |
| 5155 | for (const auto &rpath_relative_path : rpath_relative_paths) { |
| 5156 | for (const auto &rpath : rpath_paths) { |
| 5157 | std::string path = rpath; |
| 5158 | path += rpath_relative_path; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5159 | // It is OK to resolve this path because we must find a file on disk |
| 5160 | // for us to accept it anyway if it is rpath relative. |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 5161 | FileSpec file_spec(path); |
| 5162 | FileSystem::Instance().Resolve(file_spec); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 5163 | if (FileSystem::Instance().Exists(file_spec) && |
| 5164 | files.AppendIfUnique(file_spec)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5165 | count++; |
| 5166 | break; |
| 5167 | } |
| 5168 | } |
| 5169 | } |
| 5170 | } |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5171 | |
| 5172 | // We may have @executable_paths but no RPATHS. Figure those out here. |
| 5173 | // Only do this if this object file is the executable. We have no way to |
| 5174 | // get back to the actual executable otherwise, so we won't get the right |
| 5175 | // path. |
| 5176 | if (!at_exec_relative_paths.empty() && CalculateType() == eTypeExecutable) { |
| 5177 | FileSpec exec_dir = this_file_spec.CopyByRemovingLastPathComponent(); |
| 5178 | for (const auto &at_exec_relative_path : at_exec_relative_paths) { |
| 5179 | FileSpec file_spec = |
| 5180 | exec_dir.CopyByAppendingPathComponent(at_exec_relative_path); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 5181 | if (FileSystem::Instance().Exists(file_spec) && |
| 5182 | files.AppendIfUnique(file_spec)) |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5183 | count++; |
Jim Ingham | 1aa0ed4 | 2017-03-20 19:21:31 +0000 | [diff] [blame] | 5184 | } |
| 5185 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5186 | } |
| 5187 | return count; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5190 | lldb_private::Address ObjectFileMachO::GetEntryPointAddress() { |
| 5191 | // If the object file is not an executable it can't hold the entry point. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5192 | // m_entry_point_address is initialized to an invalid address, so we can just |
| 5193 | // return that. If m_entry_point_address is valid it means we've found it |
| 5194 | // already, so return the cached value. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5196 | if (!IsExecutable() || m_entry_point_address.IsValid()) |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 5197 | return m_entry_point_address; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 5198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5199 | // Otherwise, look for the UnixThread or Thread command. The data for the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5200 | // Thread command is given in /usr/include/mach-o.h, but it is basically: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5201 | // |
| 5202 | // uint32_t flavor - this is the flavor argument you would pass to |
| 5203 | // thread_get_state |
| 5204 | // uint32_t count - this is the count of longs in the thread state data |
| 5205 | // struct XXX_thread_state state - this is the structure from |
| 5206 | // <machine/thread_status.h> corresponding to the flavor. |
| 5207 | // <repeat this trio> |
| 5208 | // |
| 5209 | // So we just keep reading the various register flavors till we find the GPR |
| 5210 | // one, then read the PC out of there. |
| 5211 | // FIXME: We will need to have a "RegisterContext data provider" class at some |
| 5212 | // point that can get all the registers |
| 5213 | // out of data in this form & attach them to a given thread. That should |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5214 | // underlie the MacOS X User process plugin, and we'll also need it for the |
| 5215 | // MacOS X Core File process plugin. When we have that we can also use it |
| 5216 | // here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5217 | // |
| 5218 | // For now we hard-code the offsets and flavors we need: |
| 5219 | // |
| 5220 | // |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 5221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5222 | ModuleSP module_sp(GetModule()); |
| 5223 | if (module_sp) { |
| 5224 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5225 | struct load_command load_cmd; |
| 5226 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5227 | uint32_t i; |
| 5228 | lldb::addr_t start_address = LLDB_INVALID_ADDRESS; |
| 5229 | bool done = false; |
| 5230 | |
| 5231 | for (i = 0; i < m_header.ncmds; ++i) { |
| 5232 | const lldb::offset_t cmd_offset = offset; |
| 5233 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 5234 | break; |
| 5235 | |
| 5236 | switch (load_cmd.cmd) { |
| 5237 | case LC_UNIXTHREAD: |
| 5238 | case LC_THREAD: { |
| 5239 | while (offset < cmd_offset + load_cmd.cmdsize) { |
| 5240 | uint32_t flavor = m_data.GetU32(&offset); |
| 5241 | uint32_t count = m_data.GetU32(&offset); |
| 5242 | if (count == 0) { |
| 5243 | // We've gotten off somehow, log and exit; |
| 5244 | return m_entry_point_address; |
| 5245 | } |
| 5246 | |
| 5247 | switch (m_header.cputype) { |
| 5248 | case llvm::MachO::CPU_TYPE_ARM: |
| 5249 | if (flavor == 1 || |
| 5250 | flavor == 9) // ARM_THREAD_STATE/ARM_THREAD_STATE32 from |
| 5251 | // mach/arm/thread_status.h |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5252 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5253 | offset += 60; // This is the offset of pc in the GPR thread state |
| 5254 | // data structure. |
| 5255 | start_address = m_data.GetU32(&offset); |
| 5256 | done = true; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5257 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5258 | break; |
| 5259 | case llvm::MachO::CPU_TYPE_ARM64: |
| 5260 | if (flavor == 6) // ARM_THREAD_STATE64 from mach/arm/thread_status.h |
| 5261 | { |
| 5262 | offset += 256; // This is the offset of pc in the GPR thread state |
| 5263 | // data structure. |
| 5264 | start_address = m_data.GetU64(&offset); |
| 5265 | done = true; |
| 5266 | } |
| 5267 | break; |
| 5268 | case llvm::MachO::CPU_TYPE_I386: |
| 5269 | if (flavor == |
| 5270 | 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h |
| 5271 | { |
| 5272 | offset += 40; // This is the offset of eip in the GPR thread state |
| 5273 | // data structure. |
| 5274 | start_address = m_data.GetU32(&offset); |
| 5275 | done = true; |
| 5276 | } |
| 5277 | break; |
| 5278 | case llvm::MachO::CPU_TYPE_X86_64: |
| 5279 | if (flavor == |
| 5280 | 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h |
| 5281 | { |
| 5282 | offset += 16 * 8; // This is the offset of rip in the GPR thread |
| 5283 | // state data structure. |
| 5284 | start_address = m_data.GetU64(&offset); |
| 5285 | done = true; |
| 5286 | } |
| 5287 | break; |
| 5288 | default: |
| 5289 | return m_entry_point_address; |
| 5290 | } |
| 5291 | // Haven't found the GPR flavor yet, skip over the data for this |
| 5292 | // flavor: |
| 5293 | if (done) |
| 5294 | break; |
| 5295 | offset += count * 4; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5296 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5297 | } break; |
| 5298 | case LC_MAIN: { |
| 5299 | ConstString text_segment_name("__TEXT"); |
| 5300 | uint64_t entryoffset = m_data.GetU64(&offset); |
| 5301 | SectionSP text_segment_sp = |
| 5302 | GetSectionList()->FindSectionByName(text_segment_name); |
| 5303 | if (text_segment_sp) { |
| 5304 | done = true; |
| 5305 | start_address = text_segment_sp->GetFileAddress() + entryoffset; |
| 5306 | } |
| 5307 | } break; |
| 5308 | |
| 5309 | default: |
| 5310 | break; |
| 5311 | } |
| 5312 | if (done) |
| 5313 | break; |
| 5314 | |
| 5315 | // Go to the next load command: |
| 5316 | offset = cmd_offset + load_cmd.cmdsize; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5317 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5318 | |
| 5319 | if (start_address != LLDB_INVALID_ADDRESS) { |
| 5320 | // We got the start address from the load commands, so now resolve that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5321 | // address in the sections of this ObjectFile: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5322 | if (!m_entry_point_address.ResolveAddressUsingFileSections( |
| 5323 | start_address, GetSectionList())) { |
| 5324 | m_entry_point_address.Clear(); |
| 5325 | } |
| 5326 | } else { |
| 5327 | // We couldn't read the UnixThread load command - maybe it wasn't there. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5328 | // As a fallback look for the "start" symbol in the main executable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5329 | |
| 5330 | ModuleSP module_sp(GetModule()); |
| 5331 | |
| 5332 | if (module_sp) { |
| 5333 | SymbolContextList contexts; |
| 5334 | SymbolContext context; |
| 5335 | if (module_sp->FindSymbolsWithNameAndType(ConstString("start"), |
| 5336 | eSymbolTypeCode, contexts)) { |
| 5337 | if (contexts.GetContextAtIndex(0, context)) |
| 5338 | m_entry_point_address = context.symbol->GetAddress(); |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | return m_entry_point_address; |
| 5345 | } |
| 5346 | |
Pavel Labath | d1e3fe2 | 2018-12-11 15:21:15 +0000 | [diff] [blame] | 5347 | lldb_private::Address ObjectFileMachO::GetBaseAddress() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5348 | lldb_private::Address header_addr; |
| 5349 | SectionList *section_list = GetSectionList(); |
| 5350 | if (section_list) { |
| 5351 | SectionSP text_segment_sp( |
| 5352 | section_list->FindSectionByName(GetSegmentNameTEXT())); |
| 5353 | if (text_segment_sp) { |
| 5354 | header_addr.SetSection(text_segment_sp); |
| 5355 | header_addr.SetOffset(0); |
| 5356 | } |
| 5357 | } |
| 5358 | return header_addr; |
| 5359 | } |
| 5360 | |
| 5361 | uint32_t ObjectFileMachO::GetNumThreadContexts() { |
| 5362 | ModuleSP module_sp(GetModule()); |
| 5363 | if (module_sp) { |
| 5364 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5365 | if (!m_thread_context_offsets_valid) { |
| 5366 | m_thread_context_offsets_valid = true; |
| 5367 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5368 | FileRangeArray::Entry file_range; |
| 5369 | thread_command thread_cmd; |
| 5370 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
| 5371 | const uint32_t cmd_offset = offset; |
| 5372 | if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL) |
| 5373 | break; |
| 5374 | |
| 5375 | if (thread_cmd.cmd == LC_THREAD) { |
| 5376 | file_range.SetRangeBase(offset); |
| 5377 | file_range.SetByteSize(thread_cmd.cmdsize - 8); |
| 5378 | m_thread_context_offsets.Append(file_range); |
| 5379 | } |
| 5380 | offset = cmd_offset + thread_cmd.cmdsize; |
| 5381 | } |
| 5382 | } |
| 5383 | } |
| 5384 | return m_thread_context_offsets.GetSize(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 5387 | std::string ObjectFileMachO::GetIdentifierString() { |
| 5388 | std::string result; |
| 5389 | ModuleSP module_sp(GetModule()); |
| 5390 | if (module_sp) { |
| 5391 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5392 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5393 | // First, look over the load commands for an LC_NOTE load command with |
| 5394 | // data_owner string "kern ver str" & use that if found. |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 5395 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5396 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
| 5397 | const uint32_t cmd_offset = offset; |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5398 | load_command lc; |
| 5399 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 5400 | break; |
| 5401 | if (lc.cmd == LC_NOTE) |
| 5402 | { |
| 5403 | char data_owner[17]; |
| 5404 | m_data.CopyData (offset, 16, data_owner); |
| 5405 | data_owner[16] = '\0'; |
| 5406 | offset += 16; |
| 5407 | uint64_t fileoff = m_data.GetU64_unchecked (&offset); |
| 5408 | uint64_t size = m_data.GetU64_unchecked (&offset); |
| 5409 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5410 | // "kern ver str" has a uint32_t version and then a nul terminated |
| 5411 | // c-string. |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5412 | if (strcmp ("kern ver str", data_owner) == 0) |
| 5413 | { |
| 5414 | offset = fileoff; |
| 5415 | uint32_t version; |
| 5416 | if (m_data.GetU32 (&offset, &version, 1) != nullptr) |
| 5417 | { |
| 5418 | if (version == 1) |
| 5419 | { |
| 5420 | uint32_t strsize = size - sizeof (uint32_t); |
| 5421 | char *buf = (char*) malloc (strsize); |
| 5422 | if (buf) |
| 5423 | { |
| 5424 | m_data.CopyData (offset, strsize, buf); |
| 5425 | buf[strsize - 1] = '\0'; |
| 5426 | result = buf; |
| 5427 | if (buf) |
| 5428 | free (buf); |
| 5429 | return result; |
| 5430 | } |
| 5431 | } |
| 5432 | } |
| 5433 | } |
| 5434 | } |
| 5435 | offset = cmd_offset + lc.cmdsize; |
| 5436 | } |
| 5437 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5438 | // Second, make a pass over the load commands looking for an obsolete |
| 5439 | // LC_IDENT load command. |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5440 | offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5441 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
| 5442 | const uint32_t cmd_offset = offset; |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 5443 | struct ident_command ident_command; |
| 5444 | if (m_data.GetU32(&offset, &ident_command, 2) == NULL) |
| 5445 | break; |
| 5446 | if (ident_command.cmd == LC_IDENT && ident_command.cmdsize != 0) { |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5447 | char *buf = (char *) malloc (ident_command.cmdsize); |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 5448 | if (buf != nullptr |
| 5449 | && m_data.CopyData (offset, ident_command.cmdsize, buf) == ident_command.cmdsize) { |
| 5450 | buf[ident_command.cmdsize - 1] = '\0'; |
| 5451 | result = buf; |
| 5452 | } |
| 5453 | if (buf) |
| 5454 | free (buf); |
| 5455 | } |
| 5456 | offset = cmd_offset + ident_command.cmdsize; |
| 5457 | } |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5458 | |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 5459 | } |
| 5460 | return result; |
| 5461 | } |
| 5462 | |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5463 | bool ObjectFileMachO::GetCorefileMainBinaryInfo (addr_t &address, UUID &uuid) { |
| 5464 | address = LLDB_INVALID_ADDRESS; |
| 5465 | uuid.Clear(); |
| 5466 | ModuleSP module_sp(GetModule()); |
| 5467 | if (module_sp) { |
| 5468 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5469 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5470 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
| 5471 | const uint32_t cmd_offset = offset; |
| 5472 | load_command lc; |
| 5473 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 5474 | break; |
| 5475 | if (lc.cmd == LC_NOTE) |
| 5476 | { |
| 5477 | char data_owner[17]; |
| 5478 | memset (data_owner, 0, sizeof (data_owner)); |
| 5479 | m_data.CopyData (offset, 16, data_owner); |
| 5480 | offset += 16; |
| 5481 | uint64_t fileoff = m_data.GetU64_unchecked (&offset); |
| 5482 | uint64_t size = m_data.GetU64_unchecked (&offset); |
| 5483 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5484 | // "main bin spec" (main binary specification) data payload is |
| 5485 | // formatted: |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5486 | // uint32_t version [currently 1] |
| 5487 | // uint32_t type [0 == unspecified, 1 == kernel, 2 == user process] |
| 5488 | // uint64_t address [ UINT64_MAX if address not specified ] |
| 5489 | // uuid_t uuid [ all zero's if uuid not specified ] |
| 5490 | // uint32_t log2_pagesize [ process page size in log base 2, e.g. 4k pages are 12. 0 for unspecified ] |
| 5491 | |
| 5492 | if (strcmp ("main bin spec", data_owner) == 0 && size >= 32) |
| 5493 | { |
| 5494 | offset = fileoff; |
| 5495 | uint32_t version; |
| 5496 | if (m_data.GetU32 (&offset, &version, 1) != nullptr && version == 1) |
| 5497 | { |
| 5498 | uint32_t type = 0; |
| 5499 | uuid_t raw_uuid; |
Jason Molenda | 3f60829 | 2017-04-13 02:12:32 +0000 | [diff] [blame] | 5500 | memset (raw_uuid, 0, sizeof (uuid_t)); |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5501 | |
Pavel Labath | 2f93fd1 | 2018-06-26 15:12:20 +0000 | [diff] [blame] | 5502 | if (m_data.GetU32(&offset, &type, 1) && |
| 5503 | m_data.GetU64(&offset, &address, 1) && |
| 5504 | m_data.CopyData(offset, sizeof(uuid_t), raw_uuid) != 0) { |
| 5505 | uuid = UUID::fromOptionalData(raw_uuid, sizeof(uuid_t)); |
| 5506 | return true; |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 5507 | } |
| 5508 | } |
| 5509 | } |
| 5510 | } |
| 5511 | offset = cmd_offset + lc.cmdsize; |
| 5512 | } |
| 5513 | } |
| 5514 | return false; |
| 5515 | } |
| 5516 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5517 | lldb::RegisterContextSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5518 | ObjectFileMachO::GetThreadContextAtIndex(uint32_t idx, |
| 5519 | lldb_private::Thread &thread) { |
| 5520 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 5521 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5522 | ModuleSP module_sp(GetModule()); |
| 5523 | if (module_sp) { |
| 5524 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5525 | if (!m_thread_context_offsets_valid) |
| 5526 | GetNumThreadContexts(); |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 5527 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5528 | const FileRangeArray::Entry *thread_context_file_range = |
| 5529 | m_thread_context_offsets.GetEntryAtIndex(idx); |
| 5530 | if (thread_context_file_range) { |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 5531 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5532 | DataExtractor data(m_data, thread_context_file_range->GetRangeBase(), |
| 5533 | thread_context_file_range->GetByteSize()); |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 5534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5535 | switch (m_header.cputype) { |
| 5536 | case llvm::MachO::CPU_TYPE_ARM64: |
| 5537 | reg_ctx_sp.reset(new RegisterContextDarwin_arm64_Mach(thread, data)); |
| 5538 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 5539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5540 | case llvm::MachO::CPU_TYPE_ARM: |
| 5541 | reg_ctx_sp.reset(new RegisterContextDarwin_arm_Mach(thread, data)); |
| 5542 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 5543 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5544 | case llvm::MachO::CPU_TYPE_I386: |
| 5545 | reg_ctx_sp.reset(new RegisterContextDarwin_i386_Mach(thread, data)); |
| 5546 | break; |
| 5547 | |
| 5548 | case llvm::MachO::CPU_TYPE_X86_64: |
| 5549 | reg_ctx_sp.reset(new RegisterContextDarwin_x86_64_Mach(thread, data)); |
| 5550 | break; |
| 5551 | } |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5552 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5553 | } |
| 5554 | return reg_ctx_sp; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5557 | ObjectFile::Type ObjectFileMachO::CalculateType() { |
| 5558 | switch (m_header.filetype) { |
| 5559 | case MH_OBJECT: // 0x1u |
| 5560 | if (GetAddressByteSize() == 4) { |
| 5561 | // 32 bit kexts are just object files, but they do have a valid |
| 5562 | // UUID load command. |
| 5563 | UUID uuid; |
| 5564 | if (GetUUID(&uuid)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5565 | // this checking for the UUID load command is not enough we could |
| 5566 | // eventually look for the symbol named "OSKextGetCurrentIdentifier" as |
| 5567 | // this is required of kexts |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5568 | if (m_strata == eStrataInvalid) |
| 5569 | m_strata = eStrataKernel; |
| 5570 | return eTypeSharedLibrary; |
| 5571 | } |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5572 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5573 | return eTypeObjectFile; |
| 5574 | |
| 5575 | case MH_EXECUTE: |
| 5576 | return eTypeExecutable; // 0x2u |
| 5577 | case MH_FVMLIB: |
| 5578 | return eTypeSharedLibrary; // 0x3u |
| 5579 | case MH_CORE: |
| 5580 | return eTypeCoreFile; // 0x4u |
| 5581 | case MH_PRELOAD: |
| 5582 | return eTypeSharedLibrary; // 0x5u |
| 5583 | case MH_DYLIB: |
| 5584 | return eTypeSharedLibrary; // 0x6u |
| 5585 | case MH_DYLINKER: |
| 5586 | return eTypeDynamicLinker; // 0x7u |
| 5587 | case MH_BUNDLE: |
| 5588 | return eTypeSharedLibrary; // 0x8u |
| 5589 | case MH_DYLIB_STUB: |
| 5590 | return eTypeStubLibrary; // 0x9u |
| 5591 | case MH_DSYM: |
| 5592 | return eTypeDebugInfo; // 0xAu |
| 5593 | case MH_KEXT_BUNDLE: |
| 5594 | return eTypeSharedLibrary; // 0xBu |
| 5595 | default: |
| 5596 | break; |
| 5597 | } |
| 5598 | return eTypeUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5601 | ObjectFile::Strata ObjectFileMachO::CalculateStrata() { |
| 5602 | switch (m_header.filetype) { |
| 5603 | case MH_OBJECT: // 0x1u |
| 5604 | { |
| 5605 | // 32 bit kexts are just object files, but they do have a valid |
| 5606 | // UUID load command. |
| 5607 | UUID uuid; |
| 5608 | if (GetUUID(&uuid)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5609 | // this checking for the UUID load command is not enough we could |
| 5610 | // eventually look for the symbol named "OSKextGetCurrentIdentifier" as |
| 5611 | // this is required of kexts |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5612 | if (m_type == eTypeInvalid) |
| 5613 | m_type = eTypeSharedLibrary; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5614 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5615 | return eStrataKernel; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5616 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5617 | } |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5618 | return eStrataUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5619 | |
| 5620 | case MH_EXECUTE: // 0x2u |
| 5621 | // Check for the MH_DYLDLINK bit in the flags |
| 5622 | if (m_header.flags & MH_DYLDLINK) { |
| 5623 | return eStrataUser; |
| 5624 | } else { |
| 5625 | SectionList *section_list = GetSectionList(); |
| 5626 | if (section_list) { |
| 5627 | static ConstString g_kld_section_name("__KLD"); |
| 5628 | if (section_list->FindSectionByName(g_kld_section_name)) |
| 5629 | return eStrataKernel; |
| 5630 | } |
| 5631 | } |
| 5632 | return eStrataRawImage; |
| 5633 | |
| 5634 | case MH_FVMLIB: |
| 5635 | return eStrataUser; // 0x3u |
| 5636 | case MH_CORE: |
| 5637 | return eStrataUnknown; // 0x4u |
| 5638 | case MH_PRELOAD: |
| 5639 | return eStrataRawImage; // 0x5u |
| 5640 | case MH_DYLIB: |
| 5641 | return eStrataUser; // 0x6u |
| 5642 | case MH_DYLINKER: |
| 5643 | return eStrataUser; // 0x7u |
| 5644 | case MH_BUNDLE: |
| 5645 | return eStrataUser; // 0x8u |
| 5646 | case MH_DYLIB_STUB: |
| 5647 | return eStrataUser; // 0x9u |
| 5648 | case MH_DSYM: |
| 5649 | return eStrataUnknown; // 0xAu |
| 5650 | case MH_KEXT_BUNDLE: |
| 5651 | return eStrataKernel; // 0xBu |
| 5652 | default: |
| 5653 | break; |
| 5654 | } |
| 5655 | return eStrataUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5658 | llvm::VersionTuple ObjectFileMachO::GetVersion() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5659 | ModuleSP module_sp(GetModule()); |
| 5660 | if (module_sp) { |
| 5661 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5662 | struct dylib_command load_cmd; |
| 5663 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5664 | uint32_t version_cmd = 0; |
| 5665 | uint64_t version = 0; |
| 5666 | uint32_t i; |
| 5667 | for (i = 0; i < m_header.ncmds; ++i) { |
| 5668 | const lldb::offset_t cmd_offset = offset; |
| 5669 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 5670 | break; |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 5671 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5672 | if (load_cmd.cmd == LC_ID_DYLIB) { |
| 5673 | if (version_cmd == 0) { |
| 5674 | version_cmd = load_cmd.cmd; |
| 5675 | if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL) |
| 5676 | break; |
| 5677 | version = load_cmd.dylib.current_version; |
Greg Clayton | c2ff931 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 5678 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5679 | break; // Break for now unless there is another more complete version |
| 5680 | // number load command in the future. |
| 5681 | } |
| 5682 | offset = cmd_offset + load_cmd.cmdsize; |
| 5683 | } |
Jason Molenda | 4e7511e | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 5684 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5685 | if (version_cmd == LC_ID_DYLIB) { |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5686 | unsigned major = (version & 0xFFFF0000ull) >> 16; |
| 5687 | unsigned minor = (version & 0x0000FF00ull) >> 8; |
| 5688 | unsigned subminor = (version & 0x000000FFull); |
| 5689 | return llvm::VersionTuple(major, minor, subminor); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5690 | } |
| 5691 | } |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5692 | return llvm::VersionTuple(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | bool ObjectFileMachO::GetArchitecture(ArchSpec &arch) { |
| 5696 | ModuleSP module_sp(GetModule()); |
| 5697 | if (module_sp) { |
| 5698 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 5699 | return GetArchitecture(m_header, m_data, |
| 5700 | MachHeaderSizeFromMagic(m_header.magic), arch); |
| 5701 | } |
| 5702 | return false; |
| 5703 | } |
| 5704 | |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5705 | void ObjectFileMachO::GetProcessSharedCacheUUID(Process *process, addr_t &base_addr, UUID &uuid) { |
| 5706 | uuid.Clear(); |
| 5707 | base_addr = LLDB_INVALID_ADDRESS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5708 | if (process && process->GetDynamicLoader()) { |
| 5709 | DynamicLoader *dl = process->GetDynamicLoader(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5710 | LazyBool using_shared_cache; |
| 5711 | LazyBool private_shared_cache; |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5712 | dl->GetSharedCacheInformation(base_addr, uuid, using_shared_cache, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5713 | private_shared_cache); |
| 5714 | } |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5715 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_PROCESS)); |
Jason Molenda | fd443e9 | 2018-02-07 01:28:29 +0000 | [diff] [blame] | 5716 | if (log) |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5717 | log->Printf("inferior process shared cache has a UUID of %s, base address 0x%" PRIx64 , uuid.GetAsString().c_str(), base_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5720 | // From dyld SPI header dyld_process_info.h |
| 5721 | typedef void *dyld_process_info; |
| 5722 | struct lldb_copy__dyld_process_cache_info { |
| 5723 | uuid_t cacheUUID; // UUID of cache used by process |
| 5724 | uint64_t cacheBaseAddress; // load address of dyld shared cache |
| 5725 | bool noCache; // process is running without a dyld cache |
| 5726 | bool privateCache; // process is using a private copy of its dyld cache |
| 5727 | }; |
| 5728 | |
| 5729 | // #including mach/mach.h pulls in machine.h & CPU_TYPE_ARM etc conflicts with llvm |
| 5730 | // enum definitions llvm::MachO::CPU_TYPE_ARM turning them into compile errors. |
| 5731 | // So we need to use the actual underlying types of task_t and kern_return_t |
| 5732 | // below. |
| 5733 | extern "C" unsigned int /*task_t*/ mach_task_self(); |
| 5734 | |
| 5735 | void ObjectFileMachO::GetLLDBSharedCacheUUID(addr_t &base_addr, UUID &uuid) { |
| 5736 | uuid.Clear(); |
| 5737 | base_addr = LLDB_INVALID_ADDRESS; |
| 5738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5739 | #if defined(__APPLE__) && \ |
| 5740 | (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
| 5741 | uint8_t *(*dyld_get_all_image_infos)(void); |
| 5742 | dyld_get_all_image_infos = |
| 5743 | (uint8_t * (*)())dlsym(RTLD_DEFAULT, "_dyld_get_all_image_infos"); |
| 5744 | if (dyld_get_all_image_infos) { |
| 5745 | uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos(); |
| 5746 | if (dyld_all_image_infos_address) { |
| 5747 | uint32_t *version = (uint32_t *) |
| 5748 | dyld_all_image_infos_address; // version <mach-o/dyld_images.h> |
| 5749 | if (*version >= 13) { |
| 5750 | uuid_t *sharedCacheUUID_address = 0; |
| 5751 | int wordsize = sizeof(uint8_t *); |
| 5752 | if (wordsize == 8) { |
| 5753 | sharedCacheUUID_address = |
| 5754 | (uuid_t *)((uint8_t *)dyld_all_image_infos_address + |
| 5755 | 160); // sharedCacheUUID <mach-o/dyld_images.h> |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5756 | if (*version >= 15) |
| 5757 | base_addr = *(uint64_t *) ((uint8_t *) dyld_all_image_infos_address |
| 5758 | + 176); // sharedCacheBaseAddress <mach-o/dyld_images.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5759 | } else { |
| 5760 | sharedCacheUUID_address = |
| 5761 | (uuid_t *)((uint8_t *)dyld_all_image_infos_address + |
| 5762 | 84); // sharedCacheUUID <mach-o/dyld_images.h> |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5763 | if (*version >= 15) { |
| 5764 | base_addr = 0; |
| 5765 | base_addr = *(uint32_t *) ((uint8_t *) dyld_all_image_infos_address |
| 5766 | + 100); // sharedCacheBaseAddress <mach-o/dyld_images.h> |
| 5767 | } |
Greg Clayton | c2ff931 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 5768 | } |
Pavel Labath | 2f93fd1 | 2018-06-26 15:12:20 +0000 | [diff] [blame] | 5769 | uuid = UUID::fromOptionalData(sharedCacheUUID_address, sizeof(uuid_t)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5770 | } |
Greg Clayton | c2ff931 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 5771 | } |
Jason Molenda | fd443e9 | 2018-02-07 01:28:29 +0000 | [diff] [blame] | 5772 | } else { |
| 5773 | // Exists in macOS 10.12 and later, iOS 10.0 and later - dyld SPI |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5774 | dyld_process_info (*dyld_process_info_create)(unsigned int /* task_t */ task, uint64_t timestamp, unsigned int /*kern_return_t*/ *kernelError); |
| 5775 | void (*dyld_process_info_get_cache)(void *info, void *cacheInfo); |
| 5776 | void (*dyld_process_info_release)(dyld_process_info info); |
| 5777 | |
| 5778 | dyld_process_info_create = (void *(*)(unsigned int /* task_t */, uint64_t, unsigned int /*kern_return_t*/ *)) |
| 5779 | dlsym (RTLD_DEFAULT, "_dyld_process_info_create"); |
| 5780 | dyld_process_info_get_cache = (void (*)(void *, void *)) |
| 5781 | dlsym (RTLD_DEFAULT, "_dyld_process_info_get_cache"); |
| 5782 | dyld_process_info_release = (void (*)(void *)) |
| 5783 | dlsym (RTLD_DEFAULT, "_dyld_process_info_release"); |
| 5784 | |
| 5785 | if (dyld_process_info_create && dyld_process_info_get_cache) { |
| 5786 | unsigned int /*kern_return_t */ kern_ret; |
| 5787 | dyld_process_info process_info = dyld_process_info_create(::mach_task_self(), 0, &kern_ret); |
| 5788 | if (process_info) { |
| 5789 | struct lldb_copy__dyld_process_cache_info sc_info; |
| 5790 | memset (&sc_info, 0, sizeof (struct lldb_copy__dyld_process_cache_info)); |
| 5791 | dyld_process_info_get_cache (process_info, &sc_info); |
| 5792 | if (sc_info.cacheBaseAddress != 0) { |
| 5793 | base_addr = sc_info.cacheBaseAddress; |
Pavel Labath | 2f93fd1 | 2018-06-26 15:12:20 +0000 | [diff] [blame] | 5794 | uuid = UUID::fromOptionalData(sc_info.cacheUUID, sizeof(uuid_t)); |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5795 | } |
| 5796 | dyld_process_info_release (process_info); |
| 5797 | } |
Jason Molenda | fd443e9 | 2018-02-07 01:28:29 +0000 | [diff] [blame] | 5798 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5799 | } |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5800 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS | LIBLLDB_LOG_PROCESS)); |
Jason Molenda | fd443e9 | 2018-02-07 01:28:29 +0000 | [diff] [blame] | 5801 | if (log && uuid.IsValid()) |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 5802 | log->Printf("lldb's in-memory shared cache has a UUID of %s base address of 0x%" PRIx64, uuid.GetAsString().c_str(), base_addr); |
Jason Molenda | 0e0954c | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 5803 | #endif |
Jason Molenda | 0e0954c | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5806 | llvm::VersionTuple ObjectFileMachO::GetMinimumOSVersion() { |
| 5807 | if (!m_min_os_version) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5808 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5809 | for (uint32_t i = 0; i < m_header.ncmds; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5810 | const lldb::offset_t load_cmd_offset = offset; |
| 5811 | |
| 5812 | version_min_command lc; |
| 5813 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 5814 | break; |
| 5815 | if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX || |
| 5816 | lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS || |
| 5817 | lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS || |
| 5818 | lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS) { |
| 5819 | if (m_data.GetU32(&offset, &lc.version, |
| 5820 | (sizeof(lc) / sizeof(uint32_t)) - 2)) { |
| 5821 | const uint32_t xxxx = lc.version >> 16; |
| 5822 | const uint32_t yy = (lc.version >> 8) & 0xffu; |
| 5823 | const uint32_t zz = lc.version & 0xffu; |
| 5824 | if (xxxx) { |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5825 | m_min_os_version = llvm::VersionTuple(xxxx, yy, zz); |
| 5826 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5827 | } |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5828 | } |
| 5829 | } else if (lc.cmd == llvm::MachO::LC_BUILD_VERSION) { |
| 5830 | // struct build_version_command { |
| 5831 | // uint32_t cmd; /* LC_BUILD_VERSION */ |
| 5832 | // uint32_t cmdsize; /* sizeof(struct build_version_command) plus */ |
| 5833 | // /* ntools * sizeof(struct build_tool_version) */ |
| 5834 | // uint32_t platform; /* platform */ |
| 5835 | // uint32_t minos; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */ |
| 5836 | // uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */ |
| 5837 | // uint32_t ntools; /* number of tool entries following this */ |
| 5838 | // }; |
| 5839 | |
| 5840 | offset += 4; // skip platform |
| 5841 | uint32_t minos = m_data.GetU32(&offset); |
| 5842 | |
| 5843 | const uint32_t xxxx = minos >> 16; |
| 5844 | const uint32_t yy = (minos >> 8) & 0xffu; |
| 5845 | const uint32_t zz = minos & 0xffu; |
| 5846 | if (xxxx) { |
| 5847 | m_min_os_version = llvm::VersionTuple(xxxx, yy, zz); |
| 5848 | break; |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5849 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5850 | } |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5852 | offset = load_cmd_offset + lc.cmdsize; |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5853 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5854 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5855 | if (!m_min_os_version) { |
| 5856 | // Set version to an empty value so we don't keep trying to |
| 5857 | m_min_os_version = llvm::VersionTuple(); |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5858 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 5861 | return *m_min_os_version; |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5862 | } |
| 5863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5864 | uint32_t ObjectFileMachO::GetSDKVersion(uint32_t *versions, |
| 5865 | uint32_t num_versions) { |
| 5866 | if (m_sdk_versions.empty()) { |
| 5867 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5868 | bool success = false; |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5869 | for (uint32_t i = 0; !success && i < m_header.ncmds; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5870 | const lldb::offset_t load_cmd_offset = offset; |
| 5871 | |
| 5872 | version_min_command lc; |
| 5873 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 5874 | break; |
| 5875 | if (lc.cmd == llvm::MachO::LC_VERSION_MIN_MACOSX || |
| 5876 | lc.cmd == llvm::MachO::LC_VERSION_MIN_IPHONEOS || |
| 5877 | lc.cmd == llvm::MachO::LC_VERSION_MIN_TVOS || |
| 5878 | lc.cmd == llvm::MachO::LC_VERSION_MIN_WATCHOS) { |
| 5879 | if (m_data.GetU32(&offset, &lc.version, |
| 5880 | (sizeof(lc) / sizeof(uint32_t)) - 2)) { |
| 5881 | const uint32_t xxxx = lc.sdk >> 16; |
| 5882 | const uint32_t yy = (lc.sdk >> 8) & 0xffu; |
| 5883 | const uint32_t zz = lc.sdk & 0xffu; |
| 5884 | if (xxxx) { |
| 5885 | m_sdk_versions.push_back(xxxx); |
| 5886 | m_sdk_versions.push_back(yy); |
| 5887 | m_sdk_versions.push_back(zz); |
Jason Molenda | 32a9142 | 2018-06-25 23:45:39 +0000 | [diff] [blame] | 5888 | success = true; |
| 5889 | } else { |
| 5890 | GetModule()->ReportWarning( |
| 5891 | "minimum OS version load command with invalid (0) version found."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5892 | } |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5893 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5894 | } |
| 5895 | offset = load_cmd_offset + lc.cmdsize; |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5896 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5897 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5898 | if (!success) { |
| 5899 | offset = MachHeaderSizeFromMagic(m_header.magic); |
| 5900 | for (uint32_t i = 0; !success && i < m_header.ncmds; ++i) { |
| 5901 | const lldb::offset_t load_cmd_offset = offset; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5902 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5903 | version_min_command lc; |
| 5904 | if (m_data.GetU32(&offset, &lc.cmd, 2) == NULL) |
| 5905 | break; |
| 5906 | if (lc.cmd == llvm::MachO::LC_BUILD_VERSION) { |
| 5907 | // struct build_version_command { |
| 5908 | // uint32_t cmd; /* LC_BUILD_VERSION */ |
| 5909 | // uint32_t cmdsize; /* sizeof(struct |
| 5910 | // build_version_command) plus */ |
| 5911 | // /* ntools * sizeof(struct |
| 5912 | // build_tool_version) */ |
| 5913 | // uint32_t platform; /* platform */ |
| 5914 | // uint32_t minos; /* X.Y.Z is encoded in nibbles |
| 5915 | // xxxx.yy.zz */ uint32_t sdk; /* X.Y.Z is encoded |
| 5916 | // in nibbles xxxx.yy.zz */ uint32_t ntools; /* number |
| 5917 | // of tool entries following this */ |
| 5918 | // }; |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5919 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5920 | offset += 4; // skip platform |
| 5921 | uint32_t minos = m_data.GetU32(&offset); |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5922 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5923 | const uint32_t xxxx = minos >> 16; |
| 5924 | const uint32_t yy = (minos >> 8) & 0xffu; |
| 5925 | const uint32_t zz = minos & 0xffu; |
| 5926 | if (xxxx) { |
| 5927 | m_sdk_versions.push_back(xxxx); |
| 5928 | m_sdk_versions.push_back(yy); |
| 5929 | m_sdk_versions.push_back(zz); |
| 5930 | success = true; |
| 5931 | } |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5932 | } |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5933 | offset = load_cmd_offset + lc.cmdsize; |
| 5934 | } |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5937 | if (!success) { |
Jason Molenda | 32a9142 | 2018-06-25 23:45:39 +0000 | [diff] [blame] | 5938 | // Push an invalid value so we don't try to find |
| 5939 | // the version # again on the next call to this |
| 5940 | // method. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5941 | m_sdk_versions.push_back(UINT32_MAX); |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5942 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5943 | } |
| 5944 | |
Jason Molenda | 32a9142 | 2018-06-25 23:45:39 +0000 | [diff] [blame] | 5945 | // Legitimate version numbers will have 3 entries pushed |
| 5946 | // on to m_sdk_versions. If we only have one value, it's |
| 5947 | // the sentinel value indicating that this object file |
| 5948 | // does not have a valid minimum os version #. |
| 5949 | if (m_sdk_versions.size() > 1) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5950 | if (versions != NULL && num_versions > 0) { |
| 5951 | for (size_t i = 0; i < num_versions; ++i) { |
| 5952 | if (i < m_sdk_versions.size()) |
| 5953 | versions[i] = m_sdk_versions[i]; |
| 5954 | else |
| 5955 | versions[i] = 0; |
| 5956 | } |
| 5957 | } |
| 5958 | return m_sdk_versions.size(); |
| 5959 | } |
| 5960 | // Call the superclasses version that will empty out the data |
| 5961 | return ObjectFile::GetSDKVersion(versions, num_versions); |
Greg Clayton | 9b23498 | 2013-10-24 22:54:08 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5964 | bool ObjectFileMachO::GetIsDynamicLinkEditor() { |
| 5965 | return m_header.filetype == llvm::MachO::MH_DYLINKER; |
Greg Clayton | 08928f3 | 2015-02-05 02:01:34 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5968 | bool ObjectFileMachO::AllowAssemblyEmulationUnwindPlans() { |
| 5969 | return m_allow_assembly_emulation_unwind_plans; |
Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5972 | //------------------------------------------------------------------ |
| 5973 | // PluginInterface protocol |
| 5974 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5975 | lldb_private::ConstString ObjectFileMachO::GetPluginName() { |
| 5976 | return GetPluginNameStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5979 | uint32_t ObjectFileMachO::GetPluginVersion() { return 1; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 5980 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5981 | Section *ObjectFileMachO::GetMachHeaderSection() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5982 | // Find the first address of the mach header which is the first non-zero file |
| 5983 | // sized section whose file offset is zero. This is the base file address of |
| 5984 | // the mach-o file which can be subtracted from the vmaddr of the other |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5985 | // segments found in memory and added to the load address |
| 5986 | ModuleSP module_sp = GetModule(); |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 5987 | if (!module_sp) |
| 5988 | return nullptr; |
| 5989 | SectionList *section_list = GetSectionList(); |
| 5990 | if (!section_list) |
| 5991 | return nullptr; |
| 5992 | const size_t num_sections = section_list->GetSize(); |
| 5993 | for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
| 5994 | Section *section = section_list->GetSectionAtIndex(sect_idx).get(); |
| 5995 | if (section->GetFileOffset() == 0 && SectionIsLoadable(section)) |
| 5996 | return section; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5997 | } |
| 5998 | return nullptr; |
Greg Clayton | 0734737 | 2015-06-08 21:53:11 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 6001 | bool ObjectFileMachO::SectionIsLoadable(const Section *section) { |
| 6002 | if (!section) |
| 6003 | return false; |
| 6004 | const bool is_dsym = (m_header.filetype == MH_DSYM); |
| 6005 | if (section->GetFileSize() == 0 && !is_dsym) |
| 6006 | return false; |
| 6007 | if (section->IsThreadSpecific()) |
| 6008 | return false; |
| 6009 | if (GetModule().get() != section->GetModule().get()) |
| 6010 | return false; |
| 6011 | // Be careful with __LINKEDIT and __DWARF segments |
| 6012 | if (section->GetName() == GetSegmentNameLINKEDIT() || |
| 6013 | section->GetName() == GetSegmentNameDWARF()) { |
| 6014 | // Only map __LINKEDIT and __DWARF if we have an in memory image and |
| 6015 | // this isn't a kernel binary like a kext or mach_kernel. |
| 6016 | const bool is_memory_image = (bool)m_process_wp.lock(); |
| 6017 | const Strata strata = GetStrata(); |
| 6018 | if (is_memory_image == false || strata == eStrataKernel) |
| 6019 | return false; |
| 6020 | } |
| 6021 | return true; |
| 6022 | } |
| 6023 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6024 | lldb::addr_t ObjectFileMachO::CalculateSectionLoadAddressForMemoryImage( |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 6025 | lldb::addr_t header_load_address, const Section *header_section, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6026 | const Section *section) { |
| 6027 | ModuleSP module_sp = GetModule(); |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 6028 | if (module_sp && header_section && section && |
| 6029 | header_load_address != LLDB_INVALID_ADDRESS) { |
| 6030 | lldb::addr_t file_addr = header_section->GetFileAddress(); |
| 6031 | if (file_addr != LLDB_INVALID_ADDRESS && SectionIsLoadable(section)) |
| 6032 | return section->GetFileAddress() - file_addr + header_load_address; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6033 | } |
| 6034 | return LLDB_INVALID_ADDRESS; |
Greg Clayton | 0734737 | 2015-06-08 21:53:11 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6037 | bool ObjectFileMachO::SetLoadAddress(Target &target, lldb::addr_t value, |
| 6038 | bool value_is_offset) { |
| 6039 | ModuleSP module_sp = GetModule(); |
| 6040 | if (module_sp) { |
| 6041 | size_t num_loaded_sections = 0; |
| 6042 | SectionList *section_list = GetSectionList(); |
| 6043 | if (section_list) { |
| 6044 | const size_t num_sections = section_list->GetSize(); |
Greg Clayton | 7524e09 | 2014-02-06 20:10:16 +0000 | [diff] [blame] | 6045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6046 | if (value_is_offset) { |
| 6047 | // "value" is an offset to apply to each top level segment |
| 6048 | for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6049 | // Iterate through the object file sections to find all of the |
| 6050 | // sections that size on disk (to avoid __PAGEZERO) and load them |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6051 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame^] | 6052 | if (SectionIsLoadable(section_sp.get())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6053 | if (target.GetSectionLoadList().SetSectionLoadAddress( |
| 6054 | section_sp, section_sp->GetFileAddress() + value)) |
| 6055 | ++num_loaded_sections; |
Greg Clayton | 7524e09 | 2014-02-06 20:10:16 +0000 | [diff] [blame] | 6056 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6057 | } else { |
| 6058 | // "value" is the new base address of the mach_header, adjust each |
| 6059 | // section accordingly |
| 6060 | |
| 6061 | Section *mach_header_section = GetMachHeaderSection(); |
| 6062 | if (mach_header_section) { |
| 6063 | for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
| 6064 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 6065 | |
| 6066 | lldb::addr_t section_load_addr = |
| 6067 | CalculateSectionLoadAddressForMemoryImage( |
| 6068 | value, mach_header_section, section_sp.get()); |
| 6069 | if (section_load_addr != LLDB_INVALID_ADDRESS) { |
| 6070 | if (target.GetSectionLoadList().SetSectionLoadAddress( |
| 6071 | section_sp, section_load_addr)) |
| 6072 | ++num_loaded_sections; |
| 6073 | } |
| 6074 | } |
| 6075 | } |
| 6076 | } |
Greg Clayton | 7524e09 | 2014-02-06 20:10:16 +0000 | [diff] [blame] | 6077 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6078 | return num_loaded_sections > 0; |
| 6079 | } |
| 6080 | return false; |
Greg Clayton | 7524e09 | 2014-02-06 20:10:16 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6083 | bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6084 | const FileSpec &outfile, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6085 | if (process_sp) { |
| 6086 | Target &target = process_sp->GetTarget(); |
| 6087 | const ArchSpec target_arch = target.GetArchitecture(); |
| 6088 | const llvm::Triple &target_triple = target_arch.GetTriple(); |
| 6089 | if (target_triple.getVendor() == llvm::Triple::Apple && |
| 6090 | (target_triple.getOS() == llvm::Triple::MacOSX || |
| 6091 | target_triple.getOS() == llvm::Triple::IOS || |
| 6092 | target_triple.getOS() == llvm::Triple::WatchOS || |
| 6093 | target_triple.getOS() == llvm::Triple::TvOS)) { |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 6094 | // NEED_BRIDGEOS_TRIPLE target_triple.getOS() == llvm::Triple::BridgeOS)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6095 | bool make_core = false; |
| 6096 | switch (target_arch.GetMachine()) { |
| 6097 | case llvm::Triple::aarch64: |
| 6098 | case llvm::Triple::arm: |
| 6099 | case llvm::Triple::thumb: |
| 6100 | case llvm::Triple::x86: |
| 6101 | case llvm::Triple::x86_64: |
| 6102 | make_core = true; |
| 6103 | break; |
| 6104 | default: |
| 6105 | error.SetErrorStringWithFormat("unsupported core architecture: %s", |
| 6106 | target_triple.str().c_str()); |
| 6107 | break; |
| 6108 | } |
| 6109 | |
| 6110 | if (make_core) { |
| 6111 | std::vector<segment_command_64> segment_load_commands; |
| 6112 | // uint32_t range_info_idx = 0; |
| 6113 | MemoryRegionInfo range_info; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6114 | Status range_error = process_sp->GetMemoryRegionInfo(0, range_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6115 | const uint32_t addr_byte_size = target_arch.GetAddressByteSize(); |
| 6116 | const ByteOrder byte_order = target_arch.GetByteOrder(); |
| 6117 | if (range_error.Success()) { |
| 6118 | while (range_info.GetRange().GetRangeBase() != LLDB_INVALID_ADDRESS) { |
| 6119 | const addr_t addr = range_info.GetRange().GetRangeBase(); |
| 6120 | const addr_t size = range_info.GetRange().GetByteSize(); |
| 6121 | |
| 6122 | if (size == 0) |
| 6123 | break; |
| 6124 | |
| 6125 | // Calculate correct protections |
| 6126 | uint32_t prot = 0; |
| 6127 | if (range_info.GetReadable() == MemoryRegionInfo::eYes) |
| 6128 | prot |= VM_PROT_READ; |
| 6129 | if (range_info.GetWritable() == MemoryRegionInfo::eYes) |
| 6130 | prot |= VM_PROT_WRITE; |
| 6131 | if (range_info.GetExecutable() == MemoryRegionInfo::eYes) |
| 6132 | prot |= VM_PROT_EXECUTE; |
| 6133 | |
| 6134 | // printf ("[%3u] [0x%16.16" PRIx64 " - |
| 6135 | // 0x%16.16" PRIx64 ") %c%c%c\n", |
| 6136 | // range_info_idx, |
| 6137 | // addr, |
| 6138 | // size, |
| 6139 | // (prot & VM_PROT_READ ) ? 'r' : |
| 6140 | // '-', |
| 6141 | // (prot & VM_PROT_WRITE ) ? 'w' : |
| 6142 | // '-', |
| 6143 | // (prot & VM_PROT_EXECUTE) ? 'x' : |
| 6144 | // '-'); |
| 6145 | |
| 6146 | if (prot != 0) { |
| 6147 | uint32_t cmd_type = LC_SEGMENT_64; |
| 6148 | uint32_t segment_size = sizeof(segment_command_64); |
| 6149 | if (addr_byte_size == 4) { |
| 6150 | cmd_type = LC_SEGMENT; |
| 6151 | segment_size = sizeof(segment_command); |
| 6152 | } |
| 6153 | segment_command_64 segment = { |
| 6154 | cmd_type, // uint32_t cmd; |
| 6155 | segment_size, // uint32_t cmdsize; |
| 6156 | {0}, // char segname[16]; |
| 6157 | addr, // uint64_t vmaddr; // uint32_t for 32-bit Mach-O |
| 6158 | size, // uint64_t vmsize; // uint32_t for 32-bit Mach-O |
| 6159 | 0, // uint64_t fileoff; // uint32_t for 32-bit Mach-O |
| 6160 | size, // uint64_t filesize; // uint32_t for 32-bit Mach-O |
| 6161 | prot, // uint32_t maxprot; |
| 6162 | prot, // uint32_t initprot; |
| 6163 | 0, // uint32_t nsects; |
| 6164 | 0}; // uint32_t flags; |
| 6165 | segment_load_commands.push_back(segment); |
| 6166 | } else { |
| 6167 | // No protections and a size of 1 used to be returned from old |
| 6168 | // debugservers when we asked about a region that was past the |
| 6169 | // last memory region and it indicates the end... |
| 6170 | if (size == 1) |
| 6171 | break; |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6172 | } |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6174 | range_error = process_sp->GetMemoryRegionInfo( |
| 6175 | range_info.GetRange().GetRangeEnd(), range_info); |
| 6176 | if (range_error.Fail()) |
| 6177 | break; |
| 6178 | } |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6180 | StreamString buffer(Stream::eBinary, addr_byte_size, byte_order); |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6182 | mach_header_64 mach_header; |
| 6183 | if (addr_byte_size == 8) { |
| 6184 | mach_header.magic = MH_MAGIC_64; |
| 6185 | } else { |
| 6186 | mach_header.magic = MH_MAGIC; |
| 6187 | } |
| 6188 | mach_header.cputype = target_arch.GetMachOCPUType(); |
| 6189 | mach_header.cpusubtype = target_arch.GetMachOCPUSubType(); |
| 6190 | mach_header.filetype = MH_CORE; |
| 6191 | mach_header.ncmds = segment_load_commands.size(); |
| 6192 | mach_header.flags = 0; |
| 6193 | mach_header.reserved = 0; |
| 6194 | ThreadList &thread_list = process_sp->GetThreadList(); |
| 6195 | const uint32_t num_threads = thread_list.GetSize(); |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6196 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6197 | // Make an array of LC_THREAD data items. Each one contains the |
| 6198 | // contents of the LC_THREAD load command. The data doesn't contain |
| 6199 | // the load command + load command size, we will add the load command |
| 6200 | // and load command size as we emit the data. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6201 | std::vector<StreamString> LC_THREAD_datas(num_threads); |
| 6202 | for (auto &LC_THREAD_data : LC_THREAD_datas) { |
| 6203 | LC_THREAD_data.GetFlags().Set(Stream::eBinary); |
| 6204 | LC_THREAD_data.SetAddressByteSize(addr_byte_size); |
| 6205 | LC_THREAD_data.SetByteOrder(byte_order); |
| 6206 | } |
| 6207 | for (uint32_t thread_idx = 0; thread_idx < num_threads; |
| 6208 | ++thread_idx) { |
| 6209 | ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx)); |
| 6210 | if (thread_sp) { |
| 6211 | switch (mach_header.cputype) { |
| 6212 | case llvm::MachO::CPU_TYPE_ARM64: |
| 6213 | RegisterContextDarwin_arm64_Mach::Create_LC_THREAD( |
| 6214 | thread_sp.get(), LC_THREAD_datas[thread_idx]); |
| 6215 | break; |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6217 | case llvm::MachO::CPU_TYPE_ARM: |
| 6218 | RegisterContextDarwin_arm_Mach::Create_LC_THREAD( |
| 6219 | thread_sp.get(), LC_THREAD_datas[thread_idx]); |
| 6220 | break; |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6222 | case llvm::MachO::CPU_TYPE_I386: |
| 6223 | RegisterContextDarwin_i386_Mach::Create_LC_THREAD( |
| 6224 | thread_sp.get(), LC_THREAD_datas[thread_idx]); |
| 6225 | break; |
Jason Molenda | 2295258 | 2014-11-12 01:11:36 +0000 | [diff] [blame] | 6226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6227 | case llvm::MachO::CPU_TYPE_X86_64: |
| 6228 | RegisterContextDarwin_x86_64_Mach::Create_LC_THREAD( |
| 6229 | thread_sp.get(), LC_THREAD_datas[thread_idx]); |
| 6230 | break; |
| 6231 | } |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6232 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6233 | } |
| 6234 | |
| 6235 | // The size of the load command is the size of the segments... |
| 6236 | if (addr_byte_size == 8) { |
| 6237 | mach_header.sizeofcmds = segment_load_commands.size() * |
| 6238 | sizeof(struct segment_command_64); |
| 6239 | } else { |
| 6240 | mach_header.sizeofcmds = |
| 6241 | segment_load_commands.size() * sizeof(struct segment_command); |
| 6242 | } |
| 6243 | |
| 6244 | // and the size of all LC_THREAD load command |
| 6245 | for (const auto &LC_THREAD_data : LC_THREAD_datas) { |
| 6246 | ++mach_header.ncmds; |
| 6247 | mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize(); |
| 6248 | } |
| 6249 | |
| 6250 | printf("mach_header: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x " |
| 6251 | "0x%8.8x 0x%8.8x\n", |
| 6252 | mach_header.magic, mach_header.cputype, mach_header.cpusubtype, |
| 6253 | mach_header.filetype, mach_header.ncmds, |
| 6254 | mach_header.sizeofcmds, mach_header.flags, |
| 6255 | mach_header.reserved); |
| 6256 | |
| 6257 | // Write the mach header |
| 6258 | buffer.PutHex32(mach_header.magic); |
| 6259 | buffer.PutHex32(mach_header.cputype); |
| 6260 | buffer.PutHex32(mach_header.cpusubtype); |
| 6261 | buffer.PutHex32(mach_header.filetype); |
| 6262 | buffer.PutHex32(mach_header.ncmds); |
| 6263 | buffer.PutHex32(mach_header.sizeofcmds); |
| 6264 | buffer.PutHex32(mach_header.flags); |
| 6265 | if (addr_byte_size == 8) { |
| 6266 | buffer.PutHex32(mach_header.reserved); |
| 6267 | } |
| 6268 | |
| 6269 | // Skip the mach header and all load commands and align to the next |
| 6270 | // 0x1000 byte boundary |
| 6271 | addr_t file_offset = buffer.GetSize() + mach_header.sizeofcmds; |
| 6272 | if (file_offset & 0x00000fff) { |
| 6273 | file_offset += 0x00001000ull; |
| 6274 | file_offset &= (~0x00001000ull + 1); |
| 6275 | } |
| 6276 | |
| 6277 | for (auto &segment : segment_load_commands) { |
| 6278 | segment.fileoff = file_offset; |
| 6279 | file_offset += segment.filesize; |
| 6280 | } |
| 6281 | |
| 6282 | // Write out all of the LC_THREAD load commands |
| 6283 | for (const auto &LC_THREAD_data : LC_THREAD_datas) { |
| 6284 | const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize(); |
| 6285 | buffer.PutHex32(LC_THREAD); |
| 6286 | buffer.PutHex32(8 + LC_THREAD_data_size); // cmd + cmdsize + data |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 6287 | buffer.Write(LC_THREAD_data.GetString().data(), |
| 6288 | LC_THREAD_data_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | // Write out all of the segment load commands |
| 6292 | for (const auto &segment : segment_load_commands) { |
| 6293 | printf("0x%8.8x 0x%8.8x [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 |
| 6294 | ") [0x%16.16" PRIx64 " 0x%16.16" PRIx64 |
| 6295 | ") 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x]\n", |
| 6296 | segment.cmd, segment.cmdsize, segment.vmaddr, |
| 6297 | segment.vmaddr + segment.vmsize, segment.fileoff, |
| 6298 | segment.filesize, segment.maxprot, segment.initprot, |
| 6299 | segment.nsects, segment.flags); |
| 6300 | |
| 6301 | buffer.PutHex32(segment.cmd); |
| 6302 | buffer.PutHex32(segment.cmdsize); |
| 6303 | buffer.PutRawBytes(segment.segname, sizeof(segment.segname)); |
| 6304 | if (addr_byte_size == 8) { |
| 6305 | buffer.PutHex64(segment.vmaddr); |
| 6306 | buffer.PutHex64(segment.vmsize); |
| 6307 | buffer.PutHex64(segment.fileoff); |
| 6308 | buffer.PutHex64(segment.filesize); |
| 6309 | } else { |
| 6310 | buffer.PutHex32(static_cast<uint32_t>(segment.vmaddr)); |
| 6311 | buffer.PutHex32(static_cast<uint32_t>(segment.vmsize)); |
| 6312 | buffer.PutHex32(static_cast<uint32_t>(segment.fileoff)); |
| 6313 | buffer.PutHex32(static_cast<uint32_t>(segment.filesize)); |
| 6314 | } |
| 6315 | buffer.PutHex32(segment.maxprot); |
| 6316 | buffer.PutHex32(segment.initprot); |
| 6317 | buffer.PutHex32(segment.nsects); |
| 6318 | buffer.PutHex32(segment.flags); |
| 6319 | } |
| 6320 | |
| 6321 | File core_file; |
| 6322 | std::string core_file_path(outfile.GetPath()); |
Jonas Devlieghere | 50bc1ed | 2018-11-02 22:34:51 +0000 | [diff] [blame] | 6323 | error = FileSystem::Instance().Open(core_file, outfile, |
| 6324 | File::eOpenOptionWrite | |
| 6325 | File::eOpenOptionTruncate | |
| 6326 | File::eOpenOptionCanCreate); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6327 | if (error.Success()) { |
| 6328 | // Read 1 page at a time |
| 6329 | uint8_t bytes[0x1000]; |
| 6330 | // Write the mach header and load commands out to the core file |
| 6331 | size_t bytes_written = buffer.GetString().size(); |
| 6332 | error = core_file.Write(buffer.GetString().data(), bytes_written); |
| 6333 | if (error.Success()) { |
| 6334 | // Now write the file data for all memory segments in the process |
| 6335 | for (const auto &segment : segment_load_commands) { |
| 6336 | if (core_file.SeekFromStart(segment.fileoff) == -1) { |
| 6337 | error.SetErrorStringWithFormat( |
| 6338 | "unable to seek to offset 0x%" PRIx64 " in '%s'", |
| 6339 | segment.fileoff, core_file_path.c_str()); |
| 6340 | break; |
| 6341 | } |
| 6342 | |
| 6343 | printf("Saving %" PRId64 |
| 6344 | " bytes of data for memory region at 0x%" PRIx64 "\n", |
| 6345 | segment.vmsize, segment.vmaddr); |
| 6346 | addr_t bytes_left = segment.vmsize; |
| 6347 | addr_t addr = segment.vmaddr; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6348 | Status memory_read_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6349 | while (bytes_left > 0 && error.Success()) { |
| 6350 | const size_t bytes_to_read = |
| 6351 | bytes_left > sizeof(bytes) ? sizeof(bytes) : bytes_left; |
| 6352 | const size_t bytes_read = process_sp->ReadMemory( |
| 6353 | addr, bytes, bytes_to_read, memory_read_error); |
| 6354 | if (bytes_read == bytes_to_read) { |
| 6355 | size_t bytes_written = bytes_read; |
| 6356 | error = core_file.Write(bytes, bytes_written); |
| 6357 | bytes_left -= bytes_read; |
| 6358 | addr += bytes_read; |
| 6359 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6360 | // Some pages within regions are not readable, those should |
| 6361 | // be zero filled |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6362 | memset(bytes, 0, bytes_to_read); |
| 6363 | size_t bytes_written = bytes_to_read; |
| 6364 | error = core_file.Write(bytes, bytes_written); |
| 6365 | bytes_left -= bytes_to_read; |
| 6366 | addr += bytes_to_read; |
| 6367 | } |
| 6368 | } |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | } else { |
| 6373 | error.SetErrorString( |
| 6374 | "process doesn't support getting memory region info"); |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6375 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6376 | } |
| 6377 | return true; // This is the right plug to handle saving core files for |
| 6378 | // this process |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6379 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6380 | } |
| 6381 | return false; |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 6382 | } |