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