Chris Lattner | 24943d2 | 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 | |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringRef.h" |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 11 | #include "llvm/Support/MachO.h" |
| 12 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "ObjectFileMachO.h" |
| 14 | |
Greg Clayton | d4330e6 | 2012-09-05 01:38:55 +0000 | [diff] [blame] | 15 | #include "lldb/lldb-private-log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ArchSpec.h" |
| 17 | #include "lldb/Core/DataBuffer.h" |
Jason Molenda | adf9e3d | 2013-04-10 05:58:57 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/FileSpecList.h" |
Greg Clayton | d4330e6 | 2012-09-05 01:38:55 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Module.h" |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 22 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 24 | #include "lldb/Core/RangeMap.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Section.h" |
| 26 | #include "lldb/Core/StreamFile.h" |
| 27 | #include "lldb/Core/StreamString.h" |
| 28 | #include "lldb/Core/Timer.h" |
| 29 | #include "lldb/Core/UUID.h" |
Greg Clayton | df6dc88 | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Host.h" |
| 31 | #include "lldb/Host/FileSpec.h" |
Sean Callanan | 3e80cd9 | 2011-10-12 02:08:07 +0000 | [diff] [blame] | 32 | #include "lldb/Symbol/ClangNamespaceDecl.h" |
Jason Molenda | d793839 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 33 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Platform.h" |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 36 | #include "lldb/Target/Process.h" |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 37 | #include "lldb/Target/Target.h" |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 38 | #include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" |
| 39 | #include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 40 | #include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 42 | #if defined (__APPLE__) && defined (__arm__) |
| 43 | // GetLLDBSharedCacheUUID() needs to call dlsym() |
| 44 | #include <dlfcn.h> |
| 45 | #endif |
| 46 | |
Daniel Malea | 3e64905 | 2013-04-17 19:24:22 +0000 | [diff] [blame] | 47 | #ifndef __APPLE__ |
| 48 | #include "Utility/UuidCompatibility.h" |
| 49 | #endif |
| 50 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | using namespace lldb; |
| 52 | using namespace lldb_private; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 53 | using namespace llvm::MachO; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 55 | class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 56 | { |
| 57 | public: |
| 58 | RegisterContextDarwin_x86_64_Mach (lldb_private::Thread &thread, const DataExtractor &data) : |
| 59 | RegisterContextDarwin_x86_64 (thread, 0) |
| 60 | { |
| 61 | SetRegisterDataFrom_LC_THREAD (data); |
| 62 | } |
| 63 | |
| 64 | virtual void |
| 65 | InvalidateAllRegisters () |
| 66 | { |
| 67 | // Do nothing... registers are always valid... |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) |
| 72 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 73 | lldb::offset_t offset = 0; |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 74 | SetError (GPRRegSet, Read, -1); |
| 75 | SetError (FPURegSet, Read, -1); |
| 76 | SetError (EXCRegSet, Read, -1); |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 77 | bool done = false; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 78 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 79 | while (!done) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 80 | { |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 81 | int flavor = data.GetU32 (&offset); |
| 82 | if (flavor == 0) |
| 83 | done = true; |
| 84 | else |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 85 | { |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 86 | uint32_t i; |
| 87 | uint32_t count = data.GetU32 (&offset); |
| 88 | switch (flavor) |
| 89 | { |
| 90 | case GPRRegSet: |
| 91 | for (i=0; i<count; ++i) |
| 92 | (&gpr.rax)[i] = data.GetU64(&offset); |
| 93 | SetError (GPRRegSet, Read, 0); |
| 94 | done = true; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 95 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 96 | break; |
| 97 | case FPURegSet: |
| 98 | // TODO: fill in FPU regs.... |
| 99 | //SetError (FPURegSet, Read, -1); |
| 100 | done = true; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 101 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 102 | break; |
| 103 | case EXCRegSet: |
| 104 | exc.trapno = data.GetU32(&offset); |
| 105 | exc.err = data.GetU32(&offset); |
| 106 | exc.faultvaddr = data.GetU64(&offset); |
| 107 | SetError (EXCRegSet, Read, 0); |
| 108 | done = true; |
| 109 | break; |
| 110 | case 7: |
| 111 | case 8: |
| 112 | case 9: |
| 113 | // fancy flavors that encapsulate of the the above |
| 114 | // falvors... |
| 115 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 116 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 117 | default: |
| 118 | done = true; |
| 119 | break; |
| 120 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 121 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | protected: |
| 125 | virtual int |
| 126 | DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 130 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 131 | virtual int |
| 132 | DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) |
| 133 | { |
| 134 | return 0; |
| 135 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 136 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 137 | virtual int |
| 138 | DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) |
| 139 | { |
| 140 | return 0; |
| 141 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 142 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 143 | virtual int |
| 144 | DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) |
| 145 | { |
| 146 | return 0; |
| 147 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 148 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 149 | virtual int |
| 150 | DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) |
| 151 | { |
| 152 | return 0; |
| 153 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 154 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 155 | virtual int |
| 156 | DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) |
| 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | }; |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 161 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 162 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 163 | class RegisterContextDarwin_i386_Mach : public RegisterContextDarwin_i386 |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 164 | { |
| 165 | public: |
| 166 | RegisterContextDarwin_i386_Mach (lldb_private::Thread &thread, const DataExtractor &data) : |
| 167 | RegisterContextDarwin_i386 (thread, 0) |
| 168 | { |
| 169 | SetRegisterDataFrom_LC_THREAD (data); |
| 170 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 171 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 172 | virtual void |
| 173 | InvalidateAllRegisters () |
| 174 | { |
| 175 | // Do nothing... registers are always valid... |
| 176 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 177 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 178 | void |
| 179 | SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) |
| 180 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 181 | lldb::offset_t offset = 0; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 182 | SetError (GPRRegSet, Read, -1); |
| 183 | SetError (FPURegSet, Read, -1); |
| 184 | SetError (EXCRegSet, Read, -1); |
| 185 | bool done = false; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 186 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 187 | while (!done) |
| 188 | { |
| 189 | int flavor = data.GetU32 (&offset); |
| 190 | if (flavor == 0) |
| 191 | done = true; |
| 192 | else |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 193 | { |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 194 | uint32_t i; |
| 195 | uint32_t count = data.GetU32 (&offset); |
| 196 | switch (flavor) |
| 197 | { |
| 198 | case GPRRegSet: |
| 199 | for (i=0; i<count; ++i) |
| 200 | (&gpr.eax)[i] = data.GetU32(&offset); |
| 201 | SetError (GPRRegSet, Read, 0); |
| 202 | done = true; |
| 203 | |
| 204 | break; |
| 205 | case FPURegSet: |
| 206 | // TODO: fill in FPU regs.... |
| 207 | //SetError (FPURegSet, Read, -1); |
| 208 | done = true; |
| 209 | |
| 210 | break; |
| 211 | case EXCRegSet: |
| 212 | exc.trapno = data.GetU32(&offset); |
| 213 | exc.err = data.GetU32(&offset); |
| 214 | exc.faultvaddr = data.GetU32(&offset); |
| 215 | SetError (EXCRegSet, Read, 0); |
| 216 | done = true; |
| 217 | break; |
| 218 | case 7: |
| 219 | case 8: |
| 220 | case 9: |
| 221 | // fancy flavors that encapsulate of the the above |
| 222 | // falvors... |
| 223 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 224 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 225 | default: |
| 226 | done = true; |
| 227 | break; |
| 228 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | protected: |
| 233 | virtual int |
| 234 | DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) |
| 235 | { |
| 236 | return 0; |
| 237 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 238 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 239 | virtual int |
| 240 | DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) |
| 241 | { |
| 242 | return 0; |
| 243 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 244 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 245 | virtual int |
| 246 | DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) |
| 247 | { |
| 248 | return 0; |
| 249 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 250 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 251 | virtual int |
| 252 | DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) |
| 253 | { |
| 254 | return 0; |
| 255 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 256 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 257 | virtual int |
| 258 | DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) |
| 259 | { |
| 260 | return 0; |
| 261 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 262 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 263 | virtual int |
| 264 | DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) |
| 265 | { |
| 266 | return 0; |
| 267 | } |
| 268 | }; |
| 269 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 270 | class RegisterContextDarwin_arm_Mach : public RegisterContextDarwin_arm |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 271 | { |
| 272 | public: |
| 273 | RegisterContextDarwin_arm_Mach (lldb_private::Thread &thread, const DataExtractor &data) : |
Greg Clayton | b5431d0 | 2012-10-30 23:57:32 +0000 | [diff] [blame] | 274 | RegisterContextDarwin_arm (thread, 0) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 275 | { |
| 276 | SetRegisterDataFrom_LC_THREAD (data); |
| 277 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 278 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 279 | virtual void |
| 280 | InvalidateAllRegisters () |
| 281 | { |
| 282 | // Do nothing... registers are always valid... |
| 283 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 284 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 285 | void |
| 286 | SetRegisterDataFrom_LC_THREAD (const DataExtractor &data) |
| 287 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 288 | lldb::offset_t offset = 0; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 289 | SetError (GPRRegSet, Read, -1); |
| 290 | SetError (FPURegSet, Read, -1); |
| 291 | SetError (EXCRegSet, Read, -1); |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 292 | bool done = false; |
| 293 | |
| 294 | while (!done) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 295 | { |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 296 | int flavor = data.GetU32 (&offset); |
| 297 | uint32_t count = data.GetU32 (&offset); |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 298 | lldb::offset_t next_thread_state = offset + (count * 4); |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 299 | switch (flavor) |
| 300 | { |
| 301 | case GPRRegSet: |
| 302 | for (uint32_t i=0; i<count; ++i) |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 303 | { |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 304 | gpr.r[i] = data.GetU32(&offset); |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Note that gpr.cpsr is also copied by the above loop; this loop technically extends |
| 308 | // one element past the end of the gpr.r[] array. |
| 309 | |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 310 | SetError (GPRRegSet, Read, 0); |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 311 | offset = next_thread_state; |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 312 | break; |
| 313 | |
| 314 | case FPURegSet: |
| 315 | { |
Jason Molenda | 2f03876 | 2013-05-14 03:52:22 +0000 | [diff] [blame] | 316 | uint8_t *fpu_reg_buf = (uint8_t*) &fpu.floats.s[0]; |
| 317 | const int fpu_reg_buf_size = sizeof (fpu.floats); |
| 318 | if (data.ExtractBytes (offset, fpu_reg_buf_size, eByteOrderLittle, fpu_reg_buf) == fpu_reg_buf_size) |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 319 | { |
Jason Molenda | 2f03876 | 2013-05-14 03:52:22 +0000 | [diff] [blame] | 320 | offset += fpu_reg_buf_size; |
| 321 | fpu.fpscr = data.GetU32(&offset); |
| 322 | SetError (FPURegSet, Read, 0); |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 323 | } |
Jason Molenda | 2f03876 | 2013-05-14 03:52:22 +0000 | [diff] [blame] | 324 | else |
| 325 | { |
| 326 | done = true; |
| 327 | } |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 328 | } |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 329 | offset = next_thread_state; |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case EXCRegSet: |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 333 | if (count == 3) |
| 334 | { |
| 335 | exc.exception = data.GetU32(&offset); |
| 336 | exc.fsr = data.GetU32(&offset); |
| 337 | exc.far = data.GetU32(&offset); |
| 338 | SetError (EXCRegSet, Read, 0); |
| 339 | } |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 340 | done = true; |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 341 | offset = next_thread_state; |
Jason Molenda | 3b244b7 | 2013-05-14 03:25:58 +0000 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | // Unknown register set flavor, stop trying to parse. |
| 345 | default: |
| 346 | done = true; |
| 347 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | protected: |
| 351 | virtual int |
| 352 | DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) |
| 353 | { |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 354 | return -1; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 355 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 356 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 357 | virtual int |
| 358 | DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) |
| 359 | { |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 360 | return -1; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 361 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 362 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 363 | virtual int |
| 364 | DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) |
| 365 | { |
Jason Molenda | 0c7c90a | 2013-05-14 04:50:47 +0000 | [diff] [blame] | 366 | return -1; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 367 | } |
Greg Clayton | b5431d0 | 2012-10-30 23:57:32 +0000 | [diff] [blame] | 368 | |
| 369 | virtual int |
| 370 | DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg) |
| 371 | { |
| 372 | return -1; |
| 373 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 374 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 375 | virtual int |
| 376 | DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) |
| 377 | { |
| 378 | return 0; |
| 379 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 380 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 381 | virtual int |
| 382 | DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) |
| 383 | { |
| 384 | return 0; |
| 385 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 386 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 387 | virtual int |
| 388 | DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) |
| 389 | { |
| 390 | return 0; |
| 391 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 392 | |
Greg Clayton | b5431d0 | 2012-10-30 23:57:32 +0000 | [diff] [blame] | 393 | virtual int |
| 394 | DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg) |
| 395 | { |
| 396 | return -1; |
| 397 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Greg Clayton | 95fd285 | 2013-05-15 19:52:08 +0000 | [diff] [blame^] | 400 | static uint32_t |
| 401 | MachHeaderSizeFromMagic(uint32_t magic) |
| 402 | { |
| 403 | switch (magic) |
| 404 | { |
| 405 | case HeaderMagic32: |
| 406 | case HeaderMagic32Swapped: |
| 407 | return sizeof(struct mach_header); |
| 408 | |
| 409 | case HeaderMagic64: |
| 410 | case HeaderMagic64Swapped: |
| 411 | return sizeof(struct mach_header_64); |
| 412 | break; |
| 413 | |
| 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 420 | #define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | |
| 422 | void |
| 423 | ObjectFileMachO::Initialize() |
| 424 | { |
| 425 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 426 | GetPluginDescriptionStatic(), |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 427 | CreateInstance, |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 428 | CreateMemoryInstance, |
| 429 | GetModuleSpecifications); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void |
| 433 | ObjectFileMachO::Terminate() |
| 434 | { |
| 435 | PluginManager::UnregisterPlugin (CreateInstance); |
| 436 | } |
| 437 | |
| 438 | |
Greg Clayton | 0e19160 | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 439 | lldb_private::ConstString |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 440 | ObjectFileMachO::GetPluginNameStatic() |
| 441 | { |
Greg Clayton | 0e19160 | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 442 | static ConstString g_name("mach-o"); |
| 443 | return g_name; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | const char * |
| 447 | ObjectFileMachO::GetPluginDescriptionStatic() |
| 448 | { |
| 449 | return "Mach-o object file reader (32 and 64 bit)"; |
| 450 | } |
| 451 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | ObjectFile * |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 453 | ObjectFileMachO::CreateInstance (const lldb::ModuleSP &module_sp, |
| 454 | DataBufferSP& data_sp, |
| 455 | lldb::offset_t data_offset, |
| 456 | const FileSpec* file, |
| 457 | lldb::offset_t file_offset, |
| 458 | lldb::offset_t length) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | { |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 460 | if (!data_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 461 | { |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 462 | data_sp = file->MemoryMapFileContents(file_offset, length); |
| 463 | data_offset = 0; |
| 464 | } |
| 465 | |
| 466 | if (ObjectFileMachO::MagicBytesMatch(data_sp, data_offset, length)) |
| 467 | { |
| 468 | // Update the data to contain the entire file if it doesn't already |
| 469 | if (data_sp->GetByteSize() < length) |
| 470 | { |
| 471 | data_sp = file->MemoryMapFileContents(file_offset, length); |
| 472 | data_offset = 0; |
| 473 | } |
Greg Clayton | 102b2c2 | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 474 | std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, data_offset, file, file_offset, length)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | if (objfile_ap.get() && objfile_ap->ParseHeader()) |
| 476 | return objfile_ap.release(); |
| 477 | } |
| 478 | return NULL; |
| 479 | } |
| 480 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 481 | ObjectFile * |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 482 | ObjectFileMachO::CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
| 483 | DataBufferSP& data_sp, |
| 484 | const ProcessSP &process_sp, |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 485 | lldb::addr_t header_addr) |
| 486 | { |
| 487 | if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) |
| 488 | { |
Greg Clayton | 102b2c2 | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 489 | std::unique_ptr<ObjectFile> objfile_ap(new ObjectFileMachO (module_sp, data_sp, process_sp, header_addr)); |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 490 | if (objfile_ap.get() && objfile_ap->ParseHeader()) |
| 491 | return objfile_ap.release(); |
| 492 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 493 | return NULL; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 496 | size_t |
| 497 | ObjectFileMachO::GetModuleSpecifications (const lldb_private::FileSpec& file, |
| 498 | lldb::DataBufferSP& data_sp, |
| 499 | lldb::offset_t data_offset, |
| 500 | lldb::offset_t file_offset, |
| 501 | lldb::offset_t length, |
| 502 | lldb_private::ModuleSpecList &specs) |
| 503 | { |
| 504 | const size_t initial_count = specs.GetSize(); |
| 505 | |
| 506 | if (ObjectFileMachO::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) |
| 507 | { |
| 508 | DataExtractor data; |
| 509 | data.SetData(data_sp); |
| 510 | llvm::MachO::mach_header header; |
| 511 | if (ParseHeader (data, &data_offset, header)) |
| 512 | { |
| 513 | if (header.sizeofcmds >= data_sp->GetByteSize()) |
| 514 | { |
| 515 | data_sp = file.ReadFileContents(file_offset, header.sizeofcmds); |
Greg Clayton | 95fd285 | 2013-05-15 19:52:08 +0000 | [diff] [blame^] | 516 | data_offset = MachHeaderSizeFromMagic(header.magic) + file_offset; |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 517 | } |
| 518 | if (data_sp) |
| 519 | { |
| 520 | ModuleSpec spec; |
| 521 | spec.GetFileSpec() = file; |
| 522 | spec.GetArchitecture().SetArchitecture(eArchTypeMachO, |
| 523 | header.cputype, |
| 524 | header.cpusubtype); |
| 525 | if (spec.GetArchitecture().IsValid()) |
| 526 | { |
| 527 | GetUUID (header, data, data_offset, spec.GetUUID()); |
| 528 | specs.Append(spec); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | return specs.GetSize() - initial_count; |
| 534 | } |
| 535 | |
| 536 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 537 | |
| 538 | const ConstString & |
| 539 | ObjectFileMachO::GetSegmentNameTEXT() |
| 540 | { |
| 541 | static ConstString g_segment_name_TEXT ("__TEXT"); |
| 542 | return g_segment_name_TEXT; |
| 543 | } |
| 544 | |
| 545 | const ConstString & |
| 546 | ObjectFileMachO::GetSegmentNameDATA() |
| 547 | { |
| 548 | static ConstString g_segment_name_DATA ("__DATA"); |
| 549 | return g_segment_name_DATA; |
| 550 | } |
| 551 | |
| 552 | const ConstString & |
| 553 | ObjectFileMachO::GetSegmentNameOBJC() |
| 554 | { |
| 555 | static ConstString g_segment_name_OBJC ("__OBJC"); |
| 556 | return g_segment_name_OBJC; |
| 557 | } |
| 558 | |
| 559 | const ConstString & |
| 560 | ObjectFileMachO::GetSegmentNameLINKEDIT() |
| 561 | { |
| 562 | static ConstString g_section_name_LINKEDIT ("__LINKEDIT"); |
| 563 | return g_section_name_LINKEDIT; |
| 564 | } |
| 565 | |
| 566 | const ConstString & |
| 567 | ObjectFileMachO::GetSectionNameEHFrame() |
| 568 | { |
| 569 | static ConstString g_section_name_eh_frame ("__eh_frame"); |
| 570 | return g_section_name_eh_frame; |
| 571 | } |
| 572 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 573 | bool |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 574 | ObjectFileMachO::MagicBytesMatch (DataBufferSP& data_sp, |
| 575 | lldb::addr_t data_offset, |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 576 | lldb::addr_t data_length) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 577 | { |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 578 | DataExtractor data; |
| 579 | data.SetData (data_sp, data_offset, data_length); |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 580 | lldb::offset_t offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | uint32_t magic = data.GetU32(&offset); |
| 582 | return MachHeaderSizeFromMagic(magic) != 0; |
| 583 | } |
| 584 | |
| 585 | |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 586 | ObjectFileMachO::ObjectFileMachO(const lldb::ModuleSP &module_sp, |
| 587 | DataBufferSP& data_sp, |
| 588 | lldb::offset_t data_offset, |
| 589 | const FileSpec* file, |
| 590 | lldb::offset_t file_offset, |
| 591 | lldb::offset_t length) : |
| 592 | ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 593 | m_mach_segments(), |
| 594 | m_mach_sections(), |
| 595 | m_entry_point_address(), |
| 596 | m_thread_context_offsets(), |
| 597 | m_thread_context_offsets_valid(false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 598 | { |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 599 | ::memset (&m_header, 0, sizeof(m_header)); |
| 600 | ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 603 | ObjectFileMachO::ObjectFileMachO (const lldb::ModuleSP &module_sp, |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 604 | lldb::DataBufferSP& header_data_sp, |
| 605 | const lldb::ProcessSP &process_sp, |
| 606 | lldb::addr_t header_addr) : |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 607 | ObjectFile(module_sp, process_sp, header_addr, header_data_sp), |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 608 | m_mach_segments(), |
| 609 | m_mach_sections(), |
| 610 | m_entry_point_address(), |
| 611 | m_thread_context_offsets(), |
| 612 | m_thread_context_offsets_valid(false) |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 613 | { |
| 614 | ::memset (&m_header, 0, sizeof(m_header)); |
| 615 | ::memset (&m_dysymtab, 0, sizeof(m_dysymtab)); |
| 616 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | |
| 618 | ObjectFileMachO::~ObjectFileMachO() |
| 619 | { |
| 620 | } |
| 621 | |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 622 | bool |
| 623 | ObjectFileMachO::ParseHeader (DataExtractor &data, |
| 624 | lldb::offset_t *data_offset_ptr, |
| 625 | llvm::MachO::mach_header &header) |
| 626 | { |
| 627 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 628 | // Leave magic in the original byte order |
| 629 | header.magic = data.GetU32(data_offset_ptr); |
| 630 | bool can_parse = false; |
| 631 | bool is_64_bit = false; |
| 632 | switch (header.magic) |
| 633 | { |
| 634 | case HeaderMagic32: |
| 635 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 636 | data.SetAddressByteSize(4); |
| 637 | can_parse = true; |
| 638 | break; |
| 639 | |
| 640 | case HeaderMagic64: |
| 641 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 642 | data.SetAddressByteSize(8); |
| 643 | can_parse = true; |
| 644 | is_64_bit = true; |
| 645 | break; |
| 646 | |
| 647 | case HeaderMagic32Swapped: |
| 648 | data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
| 649 | data.SetAddressByteSize(4); |
| 650 | can_parse = true; |
| 651 | break; |
| 652 | |
| 653 | case HeaderMagic64Swapped: |
| 654 | data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
| 655 | data.SetAddressByteSize(8); |
| 656 | is_64_bit = true; |
| 657 | can_parse = true; |
| 658 | break; |
| 659 | |
| 660 | default: |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | if (can_parse) |
| 665 | { |
| 666 | data.GetU32(data_offset_ptr, &header.cputype, 6); |
| 667 | if (is_64_bit) |
| 668 | *data_offset_ptr += 4; |
| 669 | return true; |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | memset(&header, 0, sizeof(header)); |
| 674 | } |
| 675 | return false; |
| 676 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 677 | |
| 678 | bool |
| 679 | ObjectFileMachO::ParseHeader () |
| 680 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 681 | ModuleSP module_sp(GetModule()); |
| 682 | if (module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 684 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 685 | bool can_parse = false; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 686 | lldb::offset_t offset = 0; |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 687 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 688 | // Leave magic in the original byte order |
| 689 | m_header.magic = m_data.GetU32(&offset); |
| 690 | switch (m_header.magic) |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 691 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 692 | case HeaderMagic32: |
| 693 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 694 | m_data.SetAddressByteSize(4); |
| 695 | can_parse = true; |
| 696 | break; |
| 697 | |
| 698 | case HeaderMagic64: |
| 699 | m_data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 700 | m_data.SetAddressByteSize(8); |
| 701 | can_parse = true; |
| 702 | break; |
| 703 | |
| 704 | case HeaderMagic32Swapped: |
| 705 | m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
| 706 | m_data.SetAddressByteSize(4); |
| 707 | can_parse = true; |
| 708 | break; |
| 709 | |
| 710 | case HeaderMagic64Swapped: |
| 711 | m_data.SetByteOrder(lldb::endian::InlHostByteOrder() == eByteOrderBig ? eByteOrderLittle : eByteOrderBig); |
| 712 | m_data.SetAddressByteSize(8); |
| 713 | can_parse = true; |
| 714 | break; |
| 715 | |
| 716 | default: |
| 717 | break; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 718 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 719 | |
| 720 | if (can_parse) |
| 721 | { |
| 722 | m_data.GetU32(&offset, &m_header.cputype, 6); |
| 723 | |
| 724 | ArchSpec mach_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 725 | |
Greg Clayton | 21a2543 | 2012-11-16 21:36:10 +0000 | [diff] [blame] | 726 | // Check if the module has a required architecture |
| 727 | const ArchSpec &module_arch = module_sp->GetArchitecture(); |
Sean Callanan | 40e278c | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 728 | if (module_arch.IsValid() && !module_arch.IsCompatibleMatch(mach_arch)) |
Greg Clayton | 21a2543 | 2012-11-16 21:36:10 +0000 | [diff] [blame] | 729 | return false; |
| 730 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 731 | if (SetModulesArchitecture (mach_arch)) |
| 732 | { |
| 733 | const size_t header_and_lc_size = m_header.sizeofcmds + MachHeaderSizeFromMagic(m_header.magic); |
| 734 | if (m_data.GetByteSize() < header_and_lc_size) |
| 735 | { |
| 736 | DataBufferSP data_sp; |
| 737 | ProcessSP process_sp (m_process_wp.lock()); |
| 738 | if (process_sp) |
| 739 | { |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 740 | data_sp = ReadMemory (process_sp, m_memory_addr, header_and_lc_size); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 741 | } |
| 742 | else |
| 743 | { |
| 744 | // Read in all only the load command data from the file on disk |
Greg Clayton | cbe61bd | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 745 | data_sp = m_file.ReadFileContents(m_file_offset, header_and_lc_size); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 746 | if (data_sp->GetByteSize() != header_and_lc_size) |
| 747 | return false; |
| 748 | } |
| 749 | if (data_sp) |
| 750 | m_data.SetData (data_sp); |
| 751 | } |
| 752 | } |
| 753 | return true; |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | memset(&m_header, 0, sizeof(struct mach_header)); |
| 758 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | } |
| 760 | return false; |
| 761 | } |
| 762 | |
| 763 | |
| 764 | ByteOrder |
| 765 | ObjectFileMachO::GetByteOrder () const |
| 766 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | return m_data.GetByteOrder (); |
| 768 | } |
| 769 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 770 | bool |
| 771 | ObjectFileMachO::IsExecutable() const |
| 772 | { |
| 773 | return m_header.filetype == HeaderFileTypeExecutable; |
| 774 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 775 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 776 | uint32_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 777 | ObjectFileMachO::GetAddressByteSize () const |
| 778 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 779 | return m_data.GetAddressByteSize (); |
| 780 | } |
| 781 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 782 | AddressClass |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 783 | ObjectFileMachO::GetAddressClass (lldb::addr_t file_addr) |
| 784 | { |
| 785 | Symtab *symtab = GetSymtab(); |
| 786 | if (symtab) |
| 787 | { |
| 788 | Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); |
| 789 | if (symbol) |
| 790 | { |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 791 | if (symbol->ValueIsAddress()) |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 792 | { |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 793 | SectionSP section_sp (symbol->GetAddress().GetSection()); |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 794 | if (section_sp) |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 795 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 796 | const SectionType section_type = section_sp->GetType(); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 797 | switch (section_type) |
| 798 | { |
| 799 | case eSectionTypeInvalid: return eAddressClassUnknown; |
| 800 | case eSectionTypeCode: |
| 801 | if (m_header.cputype == llvm::MachO::CPUTypeARM) |
| 802 | { |
| 803 | // For ARM we have a bit in the n_desc field of the symbol |
| 804 | // that tells us ARM/Thumb which is bit 0x0008. |
| 805 | if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) |
| 806 | return eAddressClassCodeAlternateISA; |
| 807 | } |
| 808 | return eAddressClassCode; |
| 809 | |
| 810 | case eSectionTypeContainer: return eAddressClassUnknown; |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 811 | case eSectionTypeData: |
| 812 | case eSectionTypeDataCString: |
| 813 | case eSectionTypeDataCStringPointers: |
| 814 | case eSectionTypeDataSymbolAddress: |
| 815 | case eSectionTypeData4: |
| 816 | case eSectionTypeData8: |
| 817 | case eSectionTypeData16: |
| 818 | case eSectionTypeDataPointers: |
| 819 | case eSectionTypeZeroFill: |
| 820 | case eSectionTypeDataObjCMessageRefs: |
| 821 | case eSectionTypeDataObjCCFStrings: |
| 822 | return eAddressClassData; |
| 823 | case eSectionTypeDebug: |
| 824 | case eSectionTypeDWARFDebugAbbrev: |
| 825 | case eSectionTypeDWARFDebugAranges: |
| 826 | case eSectionTypeDWARFDebugFrame: |
| 827 | case eSectionTypeDWARFDebugInfo: |
| 828 | case eSectionTypeDWARFDebugLine: |
| 829 | case eSectionTypeDWARFDebugLoc: |
| 830 | case eSectionTypeDWARFDebugMacInfo: |
| 831 | case eSectionTypeDWARFDebugPubNames: |
| 832 | case eSectionTypeDWARFDebugPubTypes: |
| 833 | case eSectionTypeDWARFDebugRanges: |
| 834 | case eSectionTypeDWARFDebugStr: |
| 835 | case eSectionTypeDWARFAppleNames: |
| 836 | case eSectionTypeDWARFAppleTypes: |
| 837 | case eSectionTypeDWARFAppleNamespaces: |
| 838 | case eSectionTypeDWARFAppleObjC: |
| 839 | return eAddressClassDebug; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 840 | case eSectionTypeEHFrame: return eAddressClassRuntime; |
| 841 | case eSectionTypeOther: return eAddressClassUnknown; |
| 842 | } |
| 843 | } |
| 844 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 845 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 846 | const SymbolType symbol_type = symbol->GetType(); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 847 | switch (symbol_type) |
| 848 | { |
| 849 | case eSymbolTypeAny: return eAddressClassUnknown; |
| 850 | case eSymbolTypeAbsolute: return eAddressClassUnknown; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 851 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 852 | case eSymbolTypeCode: |
| 853 | case eSymbolTypeTrampoline: |
Greg Clayton | 0688435 | 2013-02-27 21:16:04 +0000 | [diff] [blame] | 854 | case eSymbolTypeResolver: |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 855 | if (m_header.cputype == llvm::MachO::CPUTypeARM) |
| 856 | { |
| 857 | // For ARM we have a bit in the n_desc field of the symbol |
| 858 | // that tells us ARM/Thumb which is bit 0x0008. |
| 859 | if (symbol->GetFlags() & MACHO_NLIST_ARM_SYMBOL_IS_THUMB) |
| 860 | return eAddressClassCodeAlternateISA; |
| 861 | } |
| 862 | return eAddressClassCode; |
| 863 | |
| 864 | case eSymbolTypeData: return eAddressClassData; |
| 865 | case eSymbolTypeRuntime: return eAddressClassRuntime; |
| 866 | case eSymbolTypeException: return eAddressClassRuntime; |
| 867 | case eSymbolTypeSourceFile: return eAddressClassDebug; |
| 868 | case eSymbolTypeHeaderFile: return eAddressClassDebug; |
| 869 | case eSymbolTypeObjectFile: return eAddressClassDebug; |
| 870 | case eSymbolTypeCommonBlock: return eAddressClassDebug; |
| 871 | case eSymbolTypeBlock: return eAddressClassDebug; |
| 872 | case eSymbolTypeLocal: return eAddressClassData; |
| 873 | case eSymbolTypeParam: return eAddressClassData; |
| 874 | case eSymbolTypeVariable: return eAddressClassData; |
| 875 | case eSymbolTypeVariableType: return eAddressClassDebug; |
| 876 | case eSymbolTypeLineEntry: return eAddressClassDebug; |
| 877 | case eSymbolTypeLineHeader: return eAddressClassDebug; |
| 878 | case eSymbolTypeScopeBegin: return eAddressClassDebug; |
| 879 | case eSymbolTypeScopeEnd: return eAddressClassDebug; |
| 880 | case eSymbolTypeAdditional: return eAddressClassUnknown; |
| 881 | case eSymbolTypeCompiler: return eAddressClassDebug; |
| 882 | case eSymbolTypeInstrumentation:return eAddressClassDebug; |
| 883 | case eSymbolTypeUndefined: return eAddressClassUnknown; |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 884 | case eSymbolTypeObjCClass: return eAddressClassRuntime; |
| 885 | case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; |
| 886 | case eSymbolTypeObjCIVar: return eAddressClassRuntime; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | } |
| 890 | return eAddressClassUnknown; |
| 891 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | |
| 893 | Symtab * |
| 894 | ObjectFileMachO::GetSymtab() |
| 895 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 896 | ModuleSP module_sp(GetModule()); |
| 897 | if (module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 898 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 899 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 900 | if (m_symtab_ap.get() == NULL) |
| 901 | { |
| 902 | m_symtab_ap.reset(new Symtab(this)); |
| 903 | Mutex::Locker symtab_locker (m_symtab_ap->GetMutex()); |
| 904 | ParseSymtab (true); |
| 905 | m_symtab_ap->Finalize (); |
| 906 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 907 | } |
| 908 | return m_symtab_ap.get(); |
| 909 | } |
| 910 | |
| 911 | |
| 912 | SectionList * |
| 913 | ObjectFileMachO::GetSectionList() |
| 914 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 915 | ModuleSP module_sp(GetModule()); |
| 916 | if (module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 917 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 918 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 919 | if (m_sections_ap.get() == NULL) |
| 920 | { |
| 921 | m_sections_ap.reset(new SectionList()); |
| 922 | ParseSections(); |
| 923 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 924 | } |
| 925 | return m_sections_ap.get(); |
| 926 | } |
| 927 | |
| 928 | |
| 929 | size_t |
| 930 | ObjectFileMachO::ParseSections () |
| 931 | { |
| 932 | lldb::user_id_t segID = 0; |
| 933 | lldb::user_id_t sectID = 0; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 934 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 935 | uint32_t i; |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 936 | const bool is_core = GetType() == eTypeCoreFile; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 937 | //bool dump_sections = false; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 938 | ModuleSP module_sp (GetModule()); |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 939 | // First look up any LC_ENCRYPTION_INFO load commands |
| 940 | typedef RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges; |
| 941 | EncryptedFileRanges encrypted_file_ranges; |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 942 | encryption_info_command encryption_cmd; |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 943 | for (i=0; i<m_header.ncmds; ++i) |
| 944 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 945 | const lldb::offset_t load_cmd_offset = offset; |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 946 | if (m_data.GetU32(&offset, &encryption_cmd, 2) == NULL) |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 947 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 948 | |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 949 | if (encryption_cmd.cmd == LoadCommandEncryptionInfo) |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 950 | { |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 951 | if (m_data.GetU32(&offset, &encryption_cmd.cryptoff, 3)) |
| 952 | { |
| 953 | if (encryption_cmd.cryptid != 0) |
| 954 | { |
| 955 | EncryptedFileRanges::Entry entry; |
| 956 | entry.SetRangeBase(encryption_cmd.cryptoff); |
| 957 | entry.SetByteSize(encryption_cmd.cryptsize); |
| 958 | encrypted_file_ranges.Append(entry); |
| 959 | } |
| 960 | } |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 961 | } |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 962 | offset = load_cmd_offset + encryption_cmd.cmdsize; |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | offset = MachHeaderSizeFromMagic(m_header.magic); |
| 966 | |
Greg Clayton | 54e3371 | 2012-05-25 18:09:55 +0000 | [diff] [blame] | 967 | struct segment_command_64 load_cmd; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 968 | for (i=0; i<m_header.ncmds; ++i) |
| 969 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 970 | const lldb::offset_t load_cmd_offset = offset; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 972 | break; |
| 973 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 974 | if (load_cmd.cmd == LoadCommandSegment32 || load_cmd.cmd == LoadCommandSegment64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 975 | { |
| 976 | if (m_data.GetU8(&offset, (uint8_t*)load_cmd.segname, 16)) |
| 977 | { |
| 978 | load_cmd.vmaddr = m_data.GetAddress(&offset); |
| 979 | load_cmd.vmsize = m_data.GetAddress(&offset); |
| 980 | load_cmd.fileoff = m_data.GetAddress(&offset); |
| 981 | load_cmd.filesize = m_data.GetAddress(&offset); |
Jason Molenda | adf9e3d | 2013-04-10 05:58:57 +0000 | [diff] [blame] | 982 | if (m_length != 0 && load_cmd.filesize != 0) |
| 983 | { |
Greg Clayton | bb75986 | 2013-04-16 16:51:19 +0000 | [diff] [blame] | 984 | if (load_cmd.fileoff > m_length) |
| 985 | { |
| 986 | // We have a load command that says it extends past the end of hte file. This is likely |
| 987 | // a corrupt file. We don't have any way to return an error condition here (this method |
| 988 | // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do |
| 989 | // is null out the SectionList vector and if a process has been set up, dump a message |
| 990 | // to stdout. The most common case here is core file debugging with a truncated file. |
| 991 | const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; |
| 992 | GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")", |
| 993 | i, |
| 994 | lc_segment_name, |
| 995 | load_cmd.fileoff, |
| 996 | m_length); |
| 997 | m_sections_ap->Clear(); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Jason Molenda | adf9e3d | 2013-04-10 05:58:57 +0000 | [diff] [blame] | 1001 | if (load_cmd.fileoff + load_cmd.filesize > m_length) |
| 1002 | { |
| 1003 | // We have a load command that says it extends past the end of hte file. This is likely |
| 1004 | // a corrupt file. We don't have any way to return an error condition here (this method |
| 1005 | // was likely invokved from something like ObjectFile::GetSectionList()) -- all we can do |
| 1006 | // is null out the SectionList vector and if a process has been set up, dump a message |
Greg Clayton | bb75986 | 2013-04-16 16:51:19 +0000 | [diff] [blame] | 1007 | // to stdout. The most common case here is core file debugging with a truncated file. |
| 1008 | const char *lc_segment_name = load_cmd.cmd == LoadCommandSegment64 ? "LC_SEGMENT_64" : "LC_SEGMENT"; |
| 1009 | GetModule()->ReportError("is a corrupt mach-o file: load command %u %s has a fileoff + filesize (0x%" PRIx64 ") that extends beyond the end of the file (0x%" PRIx64 ")", |
| 1010 | i, |
| 1011 | lc_segment_name, |
| 1012 | load_cmd.fileoff + load_cmd.filesize, |
| 1013 | m_length); |
Jason Molenda | adf9e3d | 2013-04-10 05:58:57 +0000 | [diff] [blame] | 1014 | m_sections_ap->Clear(); |
| 1015 | return 0; |
| 1016 | } |
| 1017 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1018 | if (m_data.GetU32(&offset, &load_cmd.maxprot, 4)) |
| 1019 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1020 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1021 | const bool segment_is_encrypted = (load_cmd.flags & SegmentCommandFlagBitProtectedVersion1) != 0; |
| 1022 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1023 | // Keep a list of mach segments around in case we need to |
| 1024 | // get at data that isn't stored in the abstracted Sections. |
| 1025 | m_mach_segments.push_back (load_cmd); |
| 1026 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1027 | ConstString segment_name (load_cmd.segname, std::min<size_t>(strlen(load_cmd.segname), sizeof(load_cmd.segname))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1028 | // Use a segment ID of the segment index shifted left by 8 so they |
| 1029 | // never conflict with any of the sections. |
| 1030 | SectionSP segment_sp; |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 1031 | if (segment_name || is_core) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1032 | { |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 1033 | segment_sp.reset(new Section (module_sp, // Module to which this section belongs |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | ++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 |
| 1035 | segment_name, // Name of this section |
| 1036 | eSectionTypeContainer, // This section is a container of other sections. |
| 1037 | load_cmd.vmaddr, // File VM address == addresses as they are found in the object file |
| 1038 | load_cmd.vmsize, // VM size in bytes of this section |
| 1039 | load_cmd.fileoff, // Offset to the data for this section in the file |
| 1040 | load_cmd.filesize, // Size in bytes of this section as found in the the file |
| 1041 | load_cmd.flags)); // Flags for this section |
| 1042 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1043 | segment_sp->SetIsEncrypted (segment_is_encrypted); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1044 | m_sections_ap->AddSection(segment_sp); |
| 1045 | } |
| 1046 | |
| 1047 | struct section_64 sect64; |
Greg Clayton | ddff7cc | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 1048 | ::memset (§64, 0, sizeof(sect64)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1049 | // Push a section into our mach sections for the section at |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1050 | // index zero (NListSectionNoSection) if we don't have any |
Greg Clayton | 6af4fad | 2010-10-06 01:26:32 +0000 | [diff] [blame] | 1051 | // mach sections yet... |
| 1052 | if (m_mach_sections.empty()) |
| 1053 | m_mach_sections.push_back(sect64); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1054 | uint32_t segment_sect_idx; |
| 1055 | const lldb::user_id_t first_segment_sectID = sectID + 1; |
| 1056 | |
| 1057 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1058 | const uint32_t num_u32s = load_cmd.cmd == LoadCommandSegment32 ? 7 : 8; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1059 | for (segment_sect_idx=0; segment_sect_idx<load_cmd.nsects; ++segment_sect_idx) |
| 1060 | { |
| 1061 | if (m_data.GetU8(&offset, (uint8_t*)sect64.sectname, sizeof(sect64.sectname)) == NULL) |
| 1062 | break; |
| 1063 | if (m_data.GetU8(&offset, (uint8_t*)sect64.segname, sizeof(sect64.segname)) == NULL) |
| 1064 | break; |
| 1065 | sect64.addr = m_data.GetAddress(&offset); |
| 1066 | sect64.size = m_data.GetAddress(&offset); |
| 1067 | |
| 1068 | if (m_data.GetU32(&offset, §64.offset, num_u32s) == NULL) |
| 1069 | break; |
| 1070 | |
| 1071 | // Keep a list of mach sections around in case we need to |
| 1072 | // get at data that isn't stored in the abstracted Sections. |
| 1073 | m_mach_sections.push_back (sect64); |
| 1074 | |
| 1075 | ConstString section_name (sect64.sectname, std::min<size_t>(strlen(sect64.sectname), sizeof(sect64.sectname))); |
| 1076 | if (!segment_name) |
| 1077 | { |
| 1078 | // We have a segment with no name so we need to conjure up |
| 1079 | // segments that correspond to the section's segname if there |
| 1080 | // isn't already such a section. If there is such a section, |
| 1081 | // we resize the section so that it spans all sections. |
| 1082 | // We also mark these sections as fake so address matches don't |
| 1083 | // hit if they land in the gaps between the child sections. |
| 1084 | segment_name.SetTrimmedCStringWithLength(sect64.segname, sizeof(sect64.segname)); |
| 1085 | segment_sp = m_sections_ap->FindSectionByName (segment_name); |
| 1086 | if (segment_sp.get()) |
| 1087 | { |
| 1088 | Section *segment = segment_sp.get(); |
| 1089 | // Grow the section size as needed. |
| 1090 | const lldb::addr_t sect64_min_addr = sect64.addr; |
| 1091 | const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size; |
| 1092 | const lldb::addr_t curr_seg_byte_size = segment->GetByteSize(); |
| 1093 | const lldb::addr_t curr_seg_min_addr = segment->GetFileAddress(); |
| 1094 | const lldb::addr_t curr_seg_max_addr = curr_seg_min_addr + curr_seg_byte_size; |
| 1095 | if (sect64_min_addr >= curr_seg_min_addr) |
| 1096 | { |
| 1097 | const lldb::addr_t new_seg_byte_size = sect64_max_addr - curr_seg_min_addr; |
| 1098 | // Only grow the section size if needed |
| 1099 | if (new_seg_byte_size > curr_seg_byte_size) |
| 1100 | segment->SetByteSize (new_seg_byte_size); |
| 1101 | } |
| 1102 | else |
| 1103 | { |
| 1104 | // We need to change the base address of the segment and |
| 1105 | // adjust the child section offsets for all existing children. |
| 1106 | const lldb::addr_t slide_amount = sect64_min_addr - curr_seg_min_addr; |
| 1107 | segment->Slide(slide_amount, false); |
Sean Callanan | 716a664 | 2012-06-08 02:16:08 +0000 | [diff] [blame] | 1108 | segment->GetChildren().Slide(-slide_amount, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1109 | segment->SetByteSize (curr_seg_max_addr - sect64_min_addr); |
| 1110 | } |
Greg Clayton | 661825b | 2010-06-28 23:51:11 +0000 | [diff] [blame] | 1111 | |
| 1112 | // Grow the section size as needed. |
| 1113 | if (sect64.offset) |
| 1114 | { |
| 1115 | const lldb::addr_t segment_min_file_offset = segment->GetFileOffset(); |
| 1116 | const lldb::addr_t segment_max_file_offset = segment_min_file_offset + segment->GetFileSize(); |
| 1117 | |
| 1118 | const lldb::addr_t section_min_file_offset = sect64.offset; |
| 1119 | const lldb::addr_t section_max_file_offset = section_min_file_offset + sect64.size; |
| 1120 | const lldb::addr_t new_file_offset = std::min (section_min_file_offset, segment_min_file_offset); |
| 1121 | const lldb::addr_t new_file_size = std::max (section_max_file_offset, segment_max_file_offset) - new_file_offset; |
| 1122 | segment->SetFileOffset (new_file_offset); |
| 1123 | segment->SetFileSize (new_file_size); |
| 1124 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1125 | } |
| 1126 | else |
| 1127 | { |
| 1128 | // Create a fake section for the section's named segment |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1129 | segment_sp.reset(new Section (segment_sp, // Parent section |
| 1130 | module_sp, // Module to which this section belongs |
| 1131 | ++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 |
| 1132 | segment_name, // Name of this section |
| 1133 | eSectionTypeContainer, // This section is a container of other sections. |
| 1134 | sect64.addr, // File VM address == addresses as they are found in the object file |
| 1135 | sect64.size, // VM size in bytes of this section |
| 1136 | sect64.offset, // Offset to the data for this section in the file |
| 1137 | sect64.offset ? sect64.size : 0, // Size in bytes of this section as found in the the file |
| 1138 | load_cmd.flags)); // Flags for this section |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | segment_sp->SetIsFake(true); |
| 1140 | m_sections_ap->AddSection(segment_sp); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1141 | segment_sp->SetIsEncrypted (segment_is_encrypted); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | assert (segment_sp.get()); |
| 1145 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1146 | uint32_t mach_sect_type = sect64.flags & SectionFlagMaskSectionType; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1147 | static ConstString g_sect_name_objc_data ("__objc_data"); |
| 1148 | static ConstString g_sect_name_objc_msgrefs ("__objc_msgrefs"); |
| 1149 | static ConstString g_sect_name_objc_selrefs ("__objc_selrefs"); |
| 1150 | static ConstString g_sect_name_objc_classrefs ("__objc_classrefs"); |
| 1151 | static ConstString g_sect_name_objc_superrefs ("__objc_superrefs"); |
| 1152 | static ConstString g_sect_name_objc_const ("__objc_const"); |
| 1153 | static ConstString g_sect_name_objc_classlist ("__objc_classlist"); |
| 1154 | static ConstString g_sect_name_cfstring ("__cfstring"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1155 | |
| 1156 | static ConstString g_sect_name_dwarf_debug_abbrev ("__debug_abbrev"); |
| 1157 | static ConstString g_sect_name_dwarf_debug_aranges ("__debug_aranges"); |
| 1158 | static ConstString g_sect_name_dwarf_debug_frame ("__debug_frame"); |
| 1159 | static ConstString g_sect_name_dwarf_debug_info ("__debug_info"); |
| 1160 | static ConstString g_sect_name_dwarf_debug_line ("__debug_line"); |
| 1161 | static ConstString g_sect_name_dwarf_debug_loc ("__debug_loc"); |
| 1162 | static ConstString g_sect_name_dwarf_debug_macinfo ("__debug_macinfo"); |
| 1163 | static ConstString g_sect_name_dwarf_debug_pubnames ("__debug_pubnames"); |
| 1164 | static ConstString g_sect_name_dwarf_debug_pubtypes ("__debug_pubtypes"); |
| 1165 | static ConstString g_sect_name_dwarf_debug_ranges ("__debug_ranges"); |
| 1166 | static ConstString g_sect_name_dwarf_debug_str ("__debug_str"); |
Greg Clayton | f6e3de2 | 2011-09-28 17:06:40 +0000 | [diff] [blame] | 1167 | static ConstString g_sect_name_dwarf_apple_names ("__apple_names"); |
| 1168 | static ConstString g_sect_name_dwarf_apple_types ("__apple_types"); |
Greg Clayton | 00db215 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 1169 | static ConstString g_sect_name_dwarf_apple_namespaces ("__apple_namespac"); |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1170 | static ConstString g_sect_name_dwarf_apple_objc ("__apple_objc"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1171 | static ConstString g_sect_name_eh_frame ("__eh_frame"); |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1172 | static ConstString g_sect_name_DATA ("__DATA"); |
| 1173 | static ConstString g_sect_name_TEXT ("__TEXT"); |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1175 | SectionType sect_type = eSectionTypeOther; |
| 1176 | |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1177 | if (section_name == g_sect_name_dwarf_debug_abbrev) |
| 1178 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1179 | else if (section_name == g_sect_name_dwarf_debug_aranges) |
| 1180 | sect_type = eSectionTypeDWARFDebugAranges; |
| 1181 | else if (section_name == g_sect_name_dwarf_debug_frame) |
| 1182 | sect_type = eSectionTypeDWARFDebugFrame; |
| 1183 | else if (section_name == g_sect_name_dwarf_debug_info) |
| 1184 | sect_type = eSectionTypeDWARFDebugInfo; |
| 1185 | else if (section_name == g_sect_name_dwarf_debug_line) |
| 1186 | sect_type = eSectionTypeDWARFDebugLine; |
| 1187 | else if (section_name == g_sect_name_dwarf_debug_loc) |
| 1188 | sect_type = eSectionTypeDWARFDebugLoc; |
| 1189 | else if (section_name == g_sect_name_dwarf_debug_macinfo) |
| 1190 | sect_type = eSectionTypeDWARFDebugMacInfo; |
| 1191 | else if (section_name == g_sect_name_dwarf_debug_pubnames) |
| 1192 | sect_type = eSectionTypeDWARFDebugPubNames; |
| 1193 | else if (section_name == g_sect_name_dwarf_debug_pubtypes) |
| 1194 | sect_type = eSectionTypeDWARFDebugPubTypes; |
| 1195 | else if (section_name == g_sect_name_dwarf_debug_ranges) |
| 1196 | sect_type = eSectionTypeDWARFDebugRanges; |
| 1197 | else if (section_name == g_sect_name_dwarf_debug_str) |
| 1198 | sect_type = eSectionTypeDWARFDebugStr; |
Greg Clayton | f6e3de2 | 2011-09-28 17:06:40 +0000 | [diff] [blame] | 1199 | else if (section_name == g_sect_name_dwarf_apple_names) |
| 1200 | sect_type = eSectionTypeDWARFAppleNames; |
| 1201 | else if (section_name == g_sect_name_dwarf_apple_types) |
| 1202 | sect_type = eSectionTypeDWARFAppleTypes; |
Greg Clayton | 00db215 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 1203 | else if (section_name == g_sect_name_dwarf_apple_namespaces) |
| 1204 | sect_type = eSectionTypeDWARFAppleNamespaces; |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1205 | else if (section_name == g_sect_name_dwarf_apple_objc) |
| 1206 | sect_type = eSectionTypeDWARFAppleObjC; |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1207 | else if (section_name == g_sect_name_objc_selrefs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1208 | sect_type = eSectionTypeDataCStringPointers; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1209 | else if (section_name == g_sect_name_objc_msgrefs) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1210 | sect_type = eSectionTypeDataObjCMessageRefs; |
Greg Clayton | 32a8c7e | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1211 | else if (section_name == g_sect_name_eh_frame) |
| 1212 | sect_type = eSectionTypeEHFrame; |
| 1213 | else if (section_name == g_sect_name_cfstring) |
| 1214 | sect_type = eSectionTypeDataObjCCFStrings; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1215 | else if (section_name == g_sect_name_objc_data || |
| 1216 | section_name == g_sect_name_objc_classrefs || |
| 1217 | section_name == g_sect_name_objc_superrefs || |
| 1218 | section_name == g_sect_name_objc_const || |
| 1219 | section_name == g_sect_name_objc_classlist) |
| 1220 | { |
| 1221 | sect_type = eSectionTypeDataPointers; |
| 1222 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (sect_type == eSectionTypeOther) |
| 1225 | { |
| 1226 | switch (mach_sect_type) |
| 1227 | { |
| 1228 | // TODO: categorize sections by other flags for regular sections |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1229 | case SectionTypeRegular: |
| 1230 | if (segment_sp->GetName() == g_sect_name_TEXT) |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1231 | sect_type = eSectionTypeCode; |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1232 | else if (segment_sp->GetName() == g_sect_name_DATA) |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1233 | sect_type = eSectionTypeData; |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1234 | else |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1235 | sect_type = eSectionTypeOther; |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1236 | break; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1237 | case SectionTypeZeroFill: sect_type = eSectionTypeZeroFill; break; |
| 1238 | case SectionTypeCStringLiterals: sect_type = eSectionTypeDataCString; break; // section with only literal C strings |
| 1239 | case SectionType4ByteLiterals: sect_type = eSectionTypeData4; break; // section with only 4 byte literals |
| 1240 | case SectionType8ByteLiterals: sect_type = eSectionTypeData8; break; // section with only 8 byte literals |
| 1241 | case SectionTypeLiteralPointers: sect_type = eSectionTypeDataPointers; break; // section with only pointers to literals |
| 1242 | case SectionTypeNonLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only non-lazy symbol pointers |
| 1243 | case SectionTypeLazySymbolPointers: sect_type = eSectionTypeDataPointers; break; // section with only lazy symbol pointers |
| 1244 | case SectionTypeSymbolStubs: sect_type = eSectionTypeCode; break; // section with only symbol stubs, byte size of stub in the reserved2 field |
| 1245 | case SectionTypeModuleInitFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for initialization |
| 1246 | case SectionTypeModuleTermFunctionPointers: sect_type = eSectionTypeDataPointers; break; // section with only function pointers for termination |
| 1247 | case SectionTypeCoalesced: sect_type = eSectionTypeOther; break; |
| 1248 | case SectionTypeZeroFillLarge: sect_type = eSectionTypeZeroFill; break; |
| 1249 | case SectionTypeInterposing: sect_type = eSectionTypeCode; break; // section with only pairs of function pointers for interposing |
| 1250 | case SectionType16ByteLiterals: sect_type = eSectionTypeData16; break; // section with only 16 byte literals |
| 1251 | case SectionTypeDTraceObjectFormat: sect_type = eSectionTypeDebug; break; |
| 1252 | case SectionTypeLazyDylibSymbolPointers: sect_type = eSectionTypeDataPointers; break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1253 | default: break; |
| 1254 | } |
| 1255 | } |
| 1256 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1257 | SectionSP section_sp(new Section (segment_sp, |
| 1258 | module_sp, |
| 1259 | ++sectID, |
| 1260 | section_name, |
| 1261 | sect_type, |
| 1262 | sect64.addr - segment_sp->GetFileAddress(), |
| 1263 | sect64.size, |
| 1264 | sect64.offset, |
| 1265 | sect64.offset == 0 ? 0 : sect64.size, |
| 1266 | sect64.flags)); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1267 | // Set the section to be encrypted to match the segment |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1268 | |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 1269 | bool section_is_encrypted = false; |
| 1270 | if (!segment_is_encrypted && load_cmd.filesize != 0) |
| 1271 | section_is_encrypted = encrypted_file_ranges.FindEntryThatContains(sect64.offset) != NULL; |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1272 | |
Greg Clayton | 6f7f8da | 2012-04-24 03:06:13 +0000 | [diff] [blame] | 1273 | section_sp->SetIsEncrypted (segment_is_encrypted || section_is_encrypted); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1274 | segment_sp->GetChildren().AddSection(section_sp); |
| 1275 | |
| 1276 | if (segment_sp->IsFake()) |
| 1277 | { |
| 1278 | segment_sp.reset(); |
| 1279 | segment_name.Clear(); |
| 1280 | } |
| 1281 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1282 | if (segment_sp && m_header.filetype == HeaderFileTypeDSYM) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1283 | { |
| 1284 | if (first_segment_sectID <= sectID) |
| 1285 | { |
| 1286 | lldb::user_id_t sect_uid; |
| 1287 | for (sect_uid = first_segment_sectID; sect_uid <= sectID; ++sect_uid) |
| 1288 | { |
| 1289 | SectionSP curr_section_sp(segment_sp->GetChildren().FindSectionByID (sect_uid)); |
| 1290 | SectionSP next_section_sp; |
| 1291 | if (sect_uid + 1 <= sectID) |
| 1292 | next_section_sp = segment_sp->GetChildren().FindSectionByID (sect_uid+1); |
| 1293 | |
| 1294 | if (curr_section_sp.get()) |
| 1295 | { |
| 1296 | if (curr_section_sp->GetByteSize() == 0) |
| 1297 | { |
| 1298 | if (next_section_sp.get() != NULL) |
| 1299 | curr_section_sp->SetByteSize ( next_section_sp->GetFileAddress() - curr_section_sp->GetFileAddress() ); |
| 1300 | else |
| 1301 | curr_section_sp->SetByteSize ( load_cmd.vmsize ); |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | } |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1310 | else if (load_cmd.cmd == LoadCommandDynamicSymtabInfo) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1311 | { |
| 1312 | m_dysymtab.cmd = load_cmd.cmd; |
| 1313 | m_dysymtab.cmdsize = load_cmd.cmdsize; |
| 1314 | m_data.GetU32 (&offset, &m_dysymtab.ilocalsym, (sizeof(m_dysymtab) / sizeof(uint32_t)) - 2); |
| 1315 | } |
| 1316 | |
| 1317 | offset = load_cmd_offset + load_cmd.cmdsize; |
| 1318 | } |
| 1319 | // if (dump_sections) |
| 1320 | // { |
| 1321 | // StreamFile s(stdout); |
| 1322 | // m_sections_ap->Dump(&s, true); |
| 1323 | // } |
| 1324 | return sectID; // Return the number of sections we registered with the module |
| 1325 | } |
| 1326 | |
| 1327 | class MachSymtabSectionInfo |
| 1328 | { |
| 1329 | public: |
| 1330 | |
| 1331 | MachSymtabSectionInfo (SectionList *section_list) : |
| 1332 | m_section_list (section_list), |
| 1333 | m_section_infos() |
| 1334 | { |
| 1335 | // Get the number of sections down to a depth of 1 to include |
| 1336 | // all segments and their sections, but no other sections that |
| 1337 | // may be added for debug map or |
| 1338 | m_section_infos.resize(section_list->GetNumSections(1)); |
| 1339 | } |
| 1340 | |
| 1341 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1342 | SectionSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1343 | GetSection (uint8_t n_sect, addr_t file_addr) |
| 1344 | { |
| 1345 | if (n_sect == 0) |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1346 | return SectionSP(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1347 | if (n_sect < m_section_infos.size()) |
| 1348 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1349 | if (!m_section_infos[n_sect].section_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1350 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1351 | SectionSP section_sp (m_section_list->FindSectionByID (n_sect)); |
| 1352 | m_section_infos[n_sect].section_sp = section_sp; |
Sean Callanan | b386d82 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 1353 | if (section_sp) |
Greg Clayton | 5638d2c | 2011-07-10 17:32:33 +0000 | [diff] [blame] | 1354 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1355 | m_section_infos[n_sect].vm_range.SetBaseAddress (section_sp->GetFileAddress()); |
| 1356 | m_section_infos[n_sect].vm_range.SetByteSize (section_sp->GetByteSize()); |
Greg Clayton | 5638d2c | 2011-07-10 17:32:33 +0000 | [diff] [blame] | 1357 | } |
| 1358 | else |
| 1359 | { |
Greg Clayton | df6dc88 | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 1360 | Host::SystemLog (Host::eSystemLogError, "error: unable to find section for section %u\n", n_sect); |
Greg Clayton | 5638d2c | 2011-07-10 17:32:33 +0000 | [diff] [blame] | 1361 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1362 | } |
| 1363 | if (m_section_infos[n_sect].vm_range.Contains(file_addr)) |
Greg Clayton | 811b9c5 | 2011-08-26 20:01:35 +0000 | [diff] [blame] | 1364 | { |
| 1365 | // Symbol is in section. |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1366 | return m_section_infos[n_sect].section_sp; |
Greg Clayton | 811b9c5 | 2011-08-26 20:01:35 +0000 | [diff] [blame] | 1367 | } |
| 1368 | else if (m_section_infos[n_sect].vm_range.GetByteSize () == 0 && |
| 1369 | m_section_infos[n_sect].vm_range.GetBaseAddress() == file_addr) |
| 1370 | { |
| 1371 | // Symbol is in section with zero size, but has the same start |
| 1372 | // address as the section. This can happen with linker symbols |
| 1373 | // (symbols that start with the letter 'l' or 'L'. |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1374 | return m_section_infos[n_sect].section_sp; |
Greg Clayton | 811b9c5 | 2011-08-26 20:01:35 +0000 | [diff] [blame] | 1375 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1376 | } |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1377 | return m_section_list->FindSectionContainingFileAddress(file_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | protected: |
| 1381 | struct SectionInfo |
| 1382 | { |
| 1383 | SectionInfo () : |
| 1384 | vm_range(), |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1385 | section_sp () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1386 | { |
| 1387 | } |
| 1388 | |
| 1389 | VMRange vm_range; |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1390 | SectionSP section_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1391 | }; |
| 1392 | SectionList *m_section_list; |
| 1393 | std::vector<SectionInfo> m_section_infos; |
| 1394 | }; |
| 1395 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1396 | size_t |
| 1397 | ObjectFileMachO::ParseSymtab (bool minimize) |
| 1398 | { |
| 1399 | Timer scoped_timer(__PRETTY_FUNCTION__, |
| 1400 | "ObjectFileMachO::ParseSymtab () module = %s", |
| 1401 | m_file.GetFilename().AsCString("")); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1402 | ModuleSP module_sp (GetModule()); |
| 1403 | if (!module_sp) |
| 1404 | return 0; |
| 1405 | |
| 1406 | struct symtab_command symtab_load_command = { 0, 0, 0, 0, 0, 0 }; |
| 1407 | struct linkedit_data_command function_starts_load_command = { 0, 0, 0, 0 }; |
| 1408 | typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts; |
| 1409 | FunctionStarts function_starts; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1410 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1411 | uint32_t i; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1412 | |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1413 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS)); |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1415 | for (i=0; i<m_header.ncmds; ++i) |
| 1416 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1417 | const lldb::offset_t cmd_offset = offset; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1418 | // Read in the load command and load command size |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1419 | struct load_command lc; |
| 1420 | if (m_data.GetU32(&offset, &lc, 2) == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1421 | break; |
| 1422 | // Watch for the symbol table load command |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1423 | switch (lc.cmd) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1424 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1425 | case LoadCommandSymtab: |
| 1426 | symtab_load_command.cmd = lc.cmd; |
| 1427 | symtab_load_command.cmdsize = lc.cmdsize; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1428 | // Read in the rest of the symtab load command |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1429 | if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == 0) // fill in symoff, nsyms, stroff, strsize fields |
| 1430 | return 0; |
| 1431 | if (symtab_load_command.symoff == 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1432 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1433 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1434 | module_sp->LogMessage(log, "LC_SYMTAB.symoff == 0"); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | if (symtab_load_command.stroff == 0) |
| 1439 | { |
| 1440 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1441 | module_sp->LogMessage(log, "LC_SYMTAB.stroff == 0"); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1442 | return 0; |
| 1443 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1444 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1445 | if (symtab_load_command.nsyms == 0) |
| 1446 | { |
| 1447 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1448 | module_sp->LogMessage(log, "LC_SYMTAB.nsyms == 0"); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1451 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1452 | if (symtab_load_command.strsize == 0) |
| 1453 | { |
| 1454 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1455 | module_sp->LogMessage(log, "LC_SYMTAB.strsize == 0"); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1456 | return 0; |
| 1457 | } |
| 1458 | break; |
| 1459 | |
| 1460 | case LoadCommandFunctionStarts: |
| 1461 | function_starts_load_command.cmd = lc.cmd; |
| 1462 | function_starts_load_command.cmdsize = lc.cmdsize; |
| 1463 | if (m_data.GetU32(&offset, &function_starts_load_command.dataoff, 2) == NULL) // fill in symoff, nsyms, stroff, strsize fields |
| 1464 | bzero (&function_starts_load_command, sizeof(function_starts_load_command)); |
| 1465 | break; |
| 1466 | |
| 1467 | default: |
| 1468 | break; |
| 1469 | } |
| 1470 | offset = cmd_offset + lc.cmdsize; |
| 1471 | } |
| 1472 | |
| 1473 | if (symtab_load_command.cmd) |
| 1474 | { |
| 1475 | Symtab *symtab = m_symtab_ap.get(); |
| 1476 | SectionList *section_list = GetSectionList(); |
| 1477 | if (section_list == NULL) |
| 1478 | return 0; |
| 1479 | |
| 1480 | ProcessSP process_sp (m_process_wp.lock()); |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1481 | Process *process = process_sp.get(); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1482 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1483 | const uint32_t addr_byte_size = m_data.GetAddressByteSize(); |
| 1484 | const ByteOrder byte_order = m_data.GetByteOrder(); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1485 | bool bit_width_32 = addr_byte_size == 4; |
| 1486 | const size_t nlist_byte_size = bit_width_32 ? sizeof(struct nlist) : sizeof(struct nlist_64); |
| 1487 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1488 | DataExtractor nlist_data (NULL, 0, byte_order, addr_byte_size); |
| 1489 | DataExtractor strtab_data (NULL, 0, byte_order, addr_byte_size); |
| 1490 | DataExtractor function_starts_data (NULL, 0, byte_order, addr_byte_size); |
Jason Molenda | 0bf2238 | 2013-02-05 22:31:24 +0000 | [diff] [blame] | 1491 | DataExtractor indirect_symbol_index_data (NULL, 0, byte_order, addr_byte_size); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1492 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1493 | const addr_t nlist_data_byte_size = symtab_load_command.nsyms * nlist_byte_size; |
| 1494 | const addr_t strtab_data_byte_size = symtab_load_command.strsize; |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1495 | addr_t strtab_addr = LLDB_INVALID_ADDRESS; |
| 1496 | if (process) |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1497 | { |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1498 | Target &target = process->GetTarget(); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1499 | SectionSP linkedit_section_sp(section_list->FindSectionByName(GetSegmentNameLINKEDIT())); |
| 1500 | // Reading mach file from memory in a process or core file... |
| 1501 | |
| 1502 | if (linkedit_section_sp) |
| 1503 | { |
| 1504 | const addr_t linkedit_load_addr = linkedit_section_sp->GetLoadBaseAddress(&target); |
| 1505 | const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset(); |
| 1506 | const addr_t symoff_addr = linkedit_load_addr + symtab_load_command.symoff - linkedit_file_offset; |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1507 | strtab_addr = linkedit_load_addr + symtab_load_command.stroff - linkedit_file_offset; |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1508 | |
| 1509 | bool data_was_read = false; |
| 1510 | |
| 1511 | #if defined (__APPLE__) && defined (__arm__) |
| 1512 | if (m_header.flags & 0x80000000u) |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 1513 | { |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1514 | // This mach-o memory file is in the dyld shared cache. If this |
| 1515 | // program is not remote and this is iOS, then this process will |
| 1516 | // share the same shared cache as the process we are debugging and |
| 1517 | // we can read the entire __LINKEDIT from the address space in this |
| 1518 | // process. This is a needed optimization that is used for local iOS |
| 1519 | // debugging only since all shared libraries in the shared cache do |
| 1520 | // not have corresponding files that exist in the file system of the |
| 1521 | // device. They have been combined into a single file. This means we |
| 1522 | // always have to load these files from memory. All of the symbol and |
| 1523 | // string tables from all of the __LINKEDIT sections from the shared |
| 1524 | // libraries in the shared cache have been merged into a single large |
| 1525 | // symbol and string table. Reading all of this symbol and string table |
| 1526 | // data across can slow down debug launch times, so we optimize this by |
| 1527 | // reading the memory for the __LINKEDIT section from this process. |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1528 | |
| 1529 | UUID lldb_shared_cache(GetLLDBSharedCacheUUID()); |
| 1530 | UUID process_shared_cache(GetProcessSharedCacheUUID(process)); |
| 1531 | bool use_lldb_cache = true; |
| 1532 | if (lldb_shared_cache.IsValid() && process_shared_cache.IsValid() && lldb_shared_cache != process_shared_cache) |
| 1533 | { |
| 1534 | use_lldb_cache = false; |
Jason Molenda | 6ff916f | 2013-04-16 21:42:58 +0000 | [diff] [blame] | 1535 | ModuleSP module_sp (GetModule()); |
| 1536 | if (module_sp) |
| 1537 | module_sp->ReportWarning ("shared cache in process does not match lldb's own shared cache, startup will be slow."); |
| 1538 | |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1541 | PlatformSP platform_sp (target.GetPlatform()); |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1542 | if (platform_sp && platform_sp->IsHost() && use_lldb_cache) |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1543 | { |
| 1544 | data_was_read = true; |
| 1545 | nlist_data.SetData((void *)symoff_addr, nlist_data_byte_size, eByteOrderLittle); |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1546 | strtab_data.SetData((void *)strtab_addr, strtab_data_byte_size, eByteOrderLittle); |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1547 | if (function_starts_load_command.cmd) |
| 1548 | { |
| 1549 | const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset; |
| 1550 | function_starts_data.SetData ((void *)func_start_addr, function_starts_load_command.datasize, eByteOrderLittle); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | #endif |
| 1555 | |
| 1556 | if (!data_was_read) |
| 1557 | { |
| 1558 | DataBufferSP nlist_data_sp (ReadMemory (process_sp, symoff_addr, nlist_data_byte_size)); |
| 1559 | if (nlist_data_sp) |
| 1560 | nlist_data.SetData (nlist_data_sp, 0, nlist_data_sp->GetByteSize()); |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1561 | //DataBufferSP strtab_data_sp (ReadMemory (process_sp, strtab_addr, strtab_data_byte_size)); |
| 1562 | //if (strtab_data_sp) |
| 1563 | // strtab_data.SetData (strtab_data_sp, 0, strtab_data_sp->GetByteSize()); |
Jason Molenda | 0bf2238 | 2013-02-05 22:31:24 +0000 | [diff] [blame] | 1564 | if (m_dysymtab.nindirectsyms != 0) |
| 1565 | { |
| 1566 | const addr_t indirect_syms_addr = linkedit_load_addr + m_dysymtab.indirectsymoff - linkedit_file_offset; |
| 1567 | DataBufferSP indirect_syms_data_sp (ReadMemory (process_sp, indirect_syms_addr, m_dysymtab.nindirectsyms * 4)); |
| 1568 | if (indirect_syms_data_sp) |
| 1569 | indirect_symbol_index_data.SetData (indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize()); |
| 1570 | } |
Greg Clayton | 29021d3 | 2012-04-18 05:19:20 +0000 | [diff] [blame] | 1571 | if (function_starts_load_command.cmd) |
| 1572 | { |
| 1573 | const addr_t func_start_addr = linkedit_load_addr + function_starts_load_command.dataoff - linkedit_file_offset; |
| 1574 | DataBufferSP func_start_data_sp (ReadMemory (process_sp, func_start_addr, function_starts_load_command.datasize)); |
| 1575 | if (func_start_data_sp) |
| 1576 | function_starts_data.SetData (func_start_data_sp, 0, func_start_data_sp->GetByteSize()); |
| 1577 | } |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 1578 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | else |
| 1582 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1583 | nlist_data.SetData (m_data, |
| 1584 | symtab_load_command.symoff, |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1585 | nlist_data_byte_size); |
| 1586 | strtab_data.SetData (m_data, |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1587 | symtab_load_command.stroff, |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1588 | strtab_data_byte_size); |
Jason Molenda | 0bf2238 | 2013-02-05 22:31:24 +0000 | [diff] [blame] | 1589 | if (m_dysymtab.nindirectsyms != 0) |
| 1590 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1591 | indirect_symbol_index_data.SetData (m_data, |
| 1592 | m_dysymtab.indirectsymoff, |
Jason Molenda | 0bf2238 | 2013-02-05 22:31:24 +0000 | [diff] [blame] | 1593 | m_dysymtab.nindirectsyms * 4); |
| 1594 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1595 | if (function_starts_load_command.cmd) |
| 1596 | { |
| 1597 | function_starts_data.SetData (m_data, |
| 1598 | function_starts_load_command.dataoff, |
| 1599 | function_starts_load_command.datasize); |
| 1600 | } |
| 1601 | } |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 1602 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1603 | if (nlist_data.GetByteSize() == 0) |
| 1604 | { |
| 1605 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1606 | module_sp->LogMessage(log, "failed to read nlist data"); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | |
Greg Clayton | 3a5dc01 | 2012-05-25 17:04:00 +0000 | [diff] [blame] | 1611 | const bool have_strtab_data = strtab_data.GetByteSize() > 0; |
| 1612 | if (!have_strtab_data) |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1613 | { |
Greg Clayton | 3a5dc01 | 2012-05-25 17:04:00 +0000 | [diff] [blame] | 1614 | if (process) |
| 1615 | { |
| 1616 | if (strtab_addr == LLDB_INVALID_ADDRESS) |
| 1617 | { |
| 1618 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1619 | module_sp->LogMessage(log, "failed to locate the strtab in memory"); |
Greg Clayton | 3a5dc01 | 2012-05-25 17:04:00 +0000 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
| 1622 | } |
| 1623 | else |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1624 | { |
| 1625 | if (log) |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1626 | module_sp->LogMessage(log, "failed to read strtab data"); |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1627 | return 0; |
| 1628 | } |
| 1629 | } |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1630 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1631 | const ConstString &g_segment_name_TEXT = GetSegmentNameTEXT(); |
| 1632 | const ConstString &g_segment_name_DATA = GetSegmentNameDATA(); |
| 1633 | const ConstString &g_segment_name_OBJC = GetSegmentNameOBJC(); |
| 1634 | const ConstString &g_section_name_eh_frame = GetSectionNameEHFrame(); |
| 1635 | SectionSP text_section_sp(section_list->FindSectionByName(g_segment_name_TEXT)); |
| 1636 | SectionSP data_section_sp(section_list->FindSectionByName(g_segment_name_DATA)); |
| 1637 | SectionSP objc_section_sp(section_list->FindSectionByName(g_segment_name_OBJC)); |
| 1638 | SectionSP eh_frame_section_sp; |
| 1639 | if (text_section_sp.get()) |
| 1640 | eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName (g_section_name_eh_frame); |
| 1641 | else |
| 1642 | eh_frame_section_sp = section_list->FindSectionByName (g_section_name_eh_frame); |
| 1643 | |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 1644 | const bool is_arm = (m_header.cputype == llvm::MachO::CPUTypeARM); |
Jason Molenda | d793839 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 1645 | |
| 1646 | // lldb works best if it knows the start addresss of all functions in a module. |
| 1647 | // Linker symbols or debug info are normally the best source of information for start addr / size but |
| 1648 | // they may be stripped in a released binary. |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1649 | // Two additional sources of information exist in Mach-O binaries: |
Jason Molenda | d793839 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 1650 | // LC_FUNCTION_STARTS - a list of ULEB128 encoded offsets of each function's start address in the |
| 1651 | // binary, relative to the text section. |
| 1652 | // eh_frame - the eh_frame FDEs have the start addr & size of each function |
| 1653 | // LC_FUNCTION_STARTS is the fastest source to read in, and is present on all modern binaries. |
| 1654 | // Binaries built to run on older releases may need to use eh_frame information. |
| 1655 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1656 | if (text_section_sp && function_starts_data.GetByteSize()) |
| 1657 | { |
| 1658 | FunctionStarts::Entry function_start_entry; |
| 1659 | function_start_entry.data = false; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1660 | lldb::offset_t function_start_offset = 0; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1661 | function_start_entry.addr = text_section_sp->GetFileAddress(); |
| 1662 | uint64_t delta; |
| 1663 | while ((delta = function_starts_data.GetULEB128(&function_start_offset)) > 0) |
| 1664 | { |
| 1665 | // Now append the current entry |
| 1666 | function_start_entry.addr += delta; |
| 1667 | function_starts.Append(function_start_entry); |
| 1668 | } |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1669 | } |
Jason Molenda | d793839 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 1670 | else |
| 1671 | { |
Jason Molenda | 825a96a | 2013-03-22 00:38:45 +0000 | [diff] [blame] | 1672 | // If m_type is eTypeDebugInfo, then this is a dSYM - it will have the load command claiming an eh_frame |
| 1673 | // but it doesn't actually have the eh_frame content. And if we have a dSYM, we don't need to do any |
| 1674 | // of this fill-in-the-missing-symbols works anyway - the debug info should give us all the functions in |
| 1675 | // the module. |
| 1676 | if (text_section_sp.get() && eh_frame_section_sp.get() && m_type != eTypeDebugInfo) |
Jason Molenda | d793839 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 1677 | { |
| 1678 | DWARFCallFrameInfo eh_frame(*this, eh_frame_section_sp, eRegisterKindGCC, true); |
| 1679 | DWARFCallFrameInfo::FunctionAddressAndSizeVector functions; |
| 1680 | eh_frame.GetFunctionAddressAndSizeVector (functions); |
| 1681 | addr_t text_base_addr = text_section_sp->GetFileAddress(); |
| 1682 | size_t count = functions.GetSize(); |
| 1683 | for (size_t i = 0; i < count; ++i) |
| 1684 | { |
| 1685 | const DWARFCallFrameInfo::FunctionAddressAndSizeVector::Entry *func = functions.GetEntryAtIndex (i); |
| 1686 | if (func) |
| 1687 | { |
| 1688 | FunctionStarts::Entry function_start_entry; |
| 1689 | function_start_entry.addr = func->base - text_base_addr; |
| 1690 | function_starts.Append(function_start_entry); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1694 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1695 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1696 | const size_t function_starts_count = function_starts.GetSize(); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1697 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1698 | const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get() ? eh_frame_section_sp->GetID() : NListSectionNoSection; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1699 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1700 | lldb::offset_t nlist_data_offset = 0; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1701 | |
| 1702 | uint32_t N_SO_index = UINT32_MAX; |
| 1703 | |
| 1704 | MachSymtabSectionInfo section_info (section_list); |
| 1705 | std::vector<uint32_t> N_FUN_indexes; |
| 1706 | std::vector<uint32_t> N_NSYM_indexes; |
| 1707 | std::vector<uint32_t> N_INCL_indexes; |
| 1708 | std::vector<uint32_t> N_BRAC_indexes; |
| 1709 | std::vector<uint32_t> N_COMM_indexes; |
| 1710 | typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap; |
| 1711 | typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap; |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 1712 | typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1713 | ValueToSymbolIndexMap N_FUN_addr_to_sym_idx; |
| 1714 | ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx; |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 1715 | ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1716 | // Any symbols that get merged into another will get an entry |
| 1717 | // in this map so we know |
| 1718 | NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx; |
| 1719 | uint32_t nlist_idx = 0; |
| 1720 | Symbol *symbol_ptr = NULL; |
| 1721 | |
| 1722 | uint32_t sym_idx = 0; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1723 | Symbol *sym = NULL; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1724 | size_t num_syms = 0; |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 1725 | std::string memory_symbol_name; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1726 | uint32_t unmapped_local_symbols_found = 0; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 1727 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1728 | #if defined (__APPLE__) && defined (__arm__) |
| 1729 | |
| 1730 | // Some recent builds of the dyld_shared_cache (hereafter: DSC) have been optimized by moving LOCAL |
| 1731 | // symbols out of the memory mapped portion of the DSC. The symbol information has all been retained, |
| 1732 | // but it isn't available in the normal nlist data. However, there *are* duplicate entries of *some* |
| 1733 | // LOCAL symbols in the normal nlist data. To handle this situation correctly, we must first attempt |
| 1734 | // to parse any DSC unmapped symbol information. If we find any, we set a flag that tells the normal |
| 1735 | // nlist parser to ignore all LOCAL symbols. |
| 1736 | |
| 1737 | if (m_header.flags & 0x80000000u) |
| 1738 | { |
| 1739 | // Before we can start mapping the DSC, we need to make certain the target process is actually |
| 1740 | // using the cache we can find. |
| 1741 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1742 | // Next we need to determine the correct path for the dyld shared cache. |
| 1743 | |
| 1744 | ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
| 1745 | char dsc_path[PATH_MAX]; |
| 1746 | |
| 1747 | snprintf(dsc_path, sizeof(dsc_path), "%s%s%s", |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1748 | "/System/Library/Caches/com.apple.dyld/", /* IPHONE_DYLD_SHARED_CACHE_DIR */ |
| 1749 | "dyld_shared_cache_", /* DYLD_SHARED_CACHE_BASE_NAME */ |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1750 | header_arch.GetArchitectureName()); |
| 1751 | |
| 1752 | FileSpec dsc_filespec(dsc_path, false); |
| 1753 | |
| 1754 | // We need definitions of two structures in the on-disk DSC, copy them here manually |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1755 | struct lldb_copy_dyld_cache_header_v0 |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 1756 | { |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1757 | char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. |
| 1758 | uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info |
| 1759 | uint32_t mappingCount; // number of dyld_cache_mapping_info entries |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1760 | uint32_t imagesOffset; |
| 1761 | uint32_t imagesCount; |
| 1762 | uint64_t dyldBaseAddress; |
| 1763 | uint64_t codeSignatureOffset; |
| 1764 | uint64_t codeSignatureSize; |
| 1765 | uint64_t slideInfoOffset; |
| 1766 | uint64_t slideInfoSize; |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1767 | uint64_t localSymbolsOffset; // file offset of where local symbols are stored |
| 1768 | uint64_t localSymbolsSize; // size of local symbols information |
| 1769 | }; |
| 1770 | struct lldb_copy_dyld_cache_header_v1 |
| 1771 | { |
| 1772 | char magic[16]; // e.g. "dyld_v0 i386", "dyld_v1 armv7", etc. |
| 1773 | uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info |
| 1774 | uint32_t mappingCount; // number of dyld_cache_mapping_info entries |
| 1775 | uint32_t imagesOffset; |
| 1776 | uint32_t imagesCount; |
| 1777 | uint64_t dyldBaseAddress; |
| 1778 | uint64_t codeSignatureOffset; |
| 1779 | uint64_t codeSignatureSize; |
| 1780 | uint64_t slideInfoOffset; |
| 1781 | uint64_t slideInfoSize; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1782 | uint64_t localSymbolsOffset; |
| 1783 | uint64_t localSymbolsSize; |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1784 | uint8_t uuid[16]; // v1 and above, also recorded in dyld_all_image_infos v13 and later |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 1785 | }; |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1786 | |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1787 | struct lldb_copy_dyld_cache_mapping_info |
| 1788 | { |
| 1789 | uint64_t address; |
| 1790 | uint64_t size; |
| 1791 | uint64_t fileOffset; |
| 1792 | uint32_t maxProt; |
| 1793 | uint32_t initProt; |
| 1794 | }; |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1795 | |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 1796 | struct lldb_copy_dyld_cache_local_symbols_info |
| 1797 | { |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1798 | uint32_t nlistOffset; |
| 1799 | uint32_t nlistCount; |
| 1800 | uint32_t stringsOffset; |
| 1801 | uint32_t stringsSize; |
| 1802 | uint32_t entriesOffset; |
| 1803 | uint32_t entriesCount; |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 1804 | }; |
| 1805 | struct lldb_copy_dyld_cache_local_symbols_entry |
| 1806 | { |
Jason Molenda | 2120aef | 2013-04-16 00:18:44 +0000 | [diff] [blame] | 1807 | uint32_t dylibOffset; |
| 1808 | uint32_t nlistStartIndex; |
| 1809 | uint32_t nlistCount; |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 1810 | }; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1811 | |
Jason Molenda | fd3b35d | 2012-06-22 03:28:35 +0000 | [diff] [blame] | 1812 | /* The dyld_cache_header has a pointer to the dyld_cache_local_symbols_info structure (localSymbolsOffset). |
| 1813 | The dyld_cache_local_symbols_info structure gives us three things: |
| 1814 | 1. The start and count of the nlist records in the dyld_shared_cache file |
| 1815 | 2. The start and size of the strings for these nlist records |
| 1816 | 3. The start and count of dyld_cache_local_symbols_entry entries |
| 1817 | |
| 1818 | There is one dyld_cache_local_symbols_entry per dylib/framework in the dyld shared cache. |
| 1819 | The "dylibOffset" field is the Mach-O header of this dylib/framework in the dyld shared cache. |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1820 | The dyld_cache_local_symbols_entry also lists the start of this dylib/framework's nlist records |
Jason Molenda | fd3b35d | 2012-06-22 03:28:35 +0000 | [diff] [blame] | 1821 | and the count of how many nlist records there are for this dylib/framework. |
| 1822 | */ |
| 1823 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1824 | // Process the dsc header to find the unmapped symbols |
| 1825 | // |
| 1826 | // Save some VM space, do not map the entire cache in one shot. |
| 1827 | |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1828 | DataBufferSP dsc_data_sp; |
| 1829 | dsc_data_sp = dsc_filespec.MemoryMapFileContents(0, sizeof(struct lldb_copy_dyld_cache_header_v1)); |
| 1830 | |
| 1831 | if (dsc_data_sp) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1832 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1833 | DataExtractor dsc_header_data(dsc_data_sp, byte_order, addr_byte_size); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1834 | |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1835 | char version_str[17]; |
| 1836 | int version = -1; |
| 1837 | lldb::offset_t offset = 0; |
| 1838 | memcpy (version_str, dsc_header_data.GetData (&offset, 16), 16); |
| 1839 | version_str[16] = '\0'; |
| 1840 | if (strncmp (version_str, "dyld_v", 6) == 0 && isdigit (version_str[6])) |
| 1841 | { |
| 1842 | int v; |
| 1843 | if (::sscanf (version_str + 6, "%d", &v) == 1) |
| 1844 | { |
| 1845 | version = v; |
| 1846 | } |
| 1847 | } |
| 1848 | |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1849 | UUID dsc_uuid; |
| 1850 | if (version >= 1) |
| 1851 | { |
| 1852 | offset = offsetof (struct lldb_copy_dyld_cache_header_v1, uuid); |
| 1853 | uint8_t uuid_bytes[sizeof (uuid_t)]; |
| 1854 | memcpy (uuid_bytes, dsc_header_data.GetData (&offset, sizeof (uuid_t)), sizeof (uuid_t)); |
| 1855 | dsc_uuid.SetBytes (uuid_bytes); |
| 1856 | } |
| 1857 | |
| 1858 | bool uuid_match = true; |
| 1859 | if (dsc_uuid.IsValid() && process) |
| 1860 | { |
| 1861 | UUID shared_cache_uuid(GetProcessSharedCacheUUID(process)); |
| 1862 | |
| 1863 | if (shared_cache_uuid.IsValid() && dsc_uuid != shared_cache_uuid) |
| 1864 | { |
| 1865 | // The on-disk dyld_shared_cache file is not the same as the one in this |
| 1866 | // process' memory, don't use it. |
| 1867 | uuid_match = false; |
Jason Molenda | 6ff916f | 2013-04-16 21:42:58 +0000 | [diff] [blame] | 1868 | ModuleSP module_sp (GetModule()); |
| 1869 | if (module_sp) |
| 1870 | module_sp->ReportWarning ("process shared cache does not match on-disk dyld_shared_cache file, some symbol names will be missing."); |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1874 | offset = offsetof (struct lldb_copy_dyld_cache_header_v1, mappingOffset); |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1875 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1876 | uint32_t mappingOffset = dsc_header_data.GetU32(&offset); |
| 1877 | |
| 1878 | // If the mappingOffset points to a location inside the header, we've |
| 1879 | // opened an old dyld shared cache, and should not proceed further. |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 1880 | if (uuid_match && mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v0)) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1881 | { |
| 1882 | |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1883 | DataBufferSP dsc_mapping_info_data_sp = dsc_filespec.MemoryMapFileContents(mappingOffset, sizeof (struct lldb_copy_dyld_cache_mapping_info)); |
| 1884 | DataExtractor dsc_mapping_info_data(dsc_mapping_info_data_sp, byte_order, addr_byte_size); |
| 1885 | offset = 0; |
| 1886 | |
| 1887 | // The File addresses (from the in-memory Mach-O load commands) for the shared libraries |
| 1888 | // in the shared library cache need to be adjusted by an offset to match up with the |
| 1889 | // dylibOffset identifying field in the dyld_cache_local_symbol_entry's. This offset is |
| 1890 | // recorded in mapping_offset_value. |
| 1891 | const uint64_t mapping_offset_value = dsc_mapping_info_data.GetU64(&offset); |
| 1892 | |
| 1893 | offset = offsetof (struct lldb_copy_dyld_cache_header_v1, localSymbolsOffset); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1894 | uint64_t localSymbolsOffset = dsc_header_data.GetU64(&offset); |
| 1895 | uint64_t localSymbolsSize = dsc_header_data.GetU64(&offset); |
| 1896 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1897 | if (localSymbolsOffset && localSymbolsSize) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1898 | { |
| 1899 | // Map the local symbols |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1900 | if (DataBufferSP dsc_local_symbols_data_sp = dsc_filespec.MemoryMapFileContents(localSymbolsOffset, localSymbolsSize)) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1901 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1902 | DataExtractor dsc_local_symbols_data(dsc_local_symbols_data_sp, byte_order, addr_byte_size); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1903 | |
| 1904 | offset = 0; |
| 1905 | |
| 1906 | // Read the local_symbols_infos struct in one shot |
| 1907 | struct lldb_copy_dyld_cache_local_symbols_info local_symbols_info; |
| 1908 | dsc_local_symbols_data.GetU32(&offset, &local_symbols_info.nlistOffset, 6); |
| 1909 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1910 | SectionSP text_section_sp(section_list->FindSectionByName(GetSegmentNameTEXT())); |
| 1911 | |
Jason Molenda | 6bcabae | 2013-03-06 23:17:36 +0000 | [diff] [blame] | 1912 | uint32_t header_file_offset = (text_section_sp->GetFileAddress() - mapping_offset_value); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1913 | |
| 1914 | offset = local_symbols_info.entriesOffset; |
| 1915 | for (uint32_t entry_index = 0; entry_index < local_symbols_info.entriesCount; entry_index++) |
| 1916 | { |
| 1917 | struct lldb_copy_dyld_cache_local_symbols_entry local_symbols_entry; |
| 1918 | local_symbols_entry.dylibOffset = dsc_local_symbols_data.GetU32(&offset); |
| 1919 | local_symbols_entry.nlistStartIndex = dsc_local_symbols_data.GetU32(&offset); |
| 1920 | local_symbols_entry.nlistCount = dsc_local_symbols_data.GetU32(&offset); |
| 1921 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1922 | if (header_file_offset == local_symbols_entry.dylibOffset) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1923 | { |
| 1924 | unmapped_local_symbols_found = local_symbols_entry.nlistCount; |
| 1925 | |
| 1926 | // The normal nlist code cannot correctly size the Symbols array, we need to allocate it here. |
| 1927 | sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms + unmapped_local_symbols_found - m_dysymtab.nlocalsym); |
| 1928 | num_syms = symtab->GetNumSymbols(); |
| 1929 | |
| 1930 | nlist_data_offset = local_symbols_info.nlistOffset + (nlist_byte_size * local_symbols_entry.nlistStartIndex); |
| 1931 | uint32_t string_table_offset = local_symbols_info.stringsOffset; |
| 1932 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 1933 | for (uint32_t nlist_index = 0; nlist_index < local_symbols_entry.nlistCount; nlist_index++) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1934 | { |
| 1935 | ///////////////////////////// |
| 1936 | { |
| 1937 | struct nlist_64 nlist; |
| 1938 | if (!dsc_local_symbols_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size)) |
| 1939 | break; |
| 1940 | |
| 1941 | nlist.n_strx = dsc_local_symbols_data.GetU32_unchecked(&nlist_data_offset); |
| 1942 | nlist.n_type = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset); |
| 1943 | nlist.n_sect = dsc_local_symbols_data.GetU8_unchecked (&nlist_data_offset); |
| 1944 | nlist.n_desc = dsc_local_symbols_data.GetU16_unchecked (&nlist_data_offset); |
| 1945 | nlist.n_value = dsc_local_symbols_data.GetAddress_unchecked (&nlist_data_offset); |
| 1946 | |
| 1947 | SymbolType type = eSymbolTypeInvalid; |
| 1948 | const char *symbol_name = dsc_local_symbols_data.PeekCStr(string_table_offset + nlist.n_strx); |
| 1949 | |
| 1950 | if (symbol_name == NULL) |
| 1951 | { |
| 1952 | // No symbol should be NULL, even the symbols with no |
| 1953 | // string values should have an offset zero which points |
| 1954 | // to an empty C-string |
| 1955 | Host::SystemLog (Host::eSystemLogError, |
Greg Clayton | 97a19b2 | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 1956 | "error: DSC unmapped local symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n", |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1957 | entry_index, |
| 1958 | nlist.n_strx, |
Greg Clayton | 97a19b2 | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 1959 | module_sp->GetFileSpec().GetPath().c_str()); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1960 | continue; |
| 1961 | } |
| 1962 | if (symbol_name[0] == '\0') |
| 1963 | symbol_name = NULL; |
| 1964 | |
| 1965 | const char *symbol_name_non_abi_mangled = NULL; |
| 1966 | |
| 1967 | SectionSP symbol_section; |
| 1968 | uint32_t symbol_byte_size = 0; |
| 1969 | bool add_nlist = true; |
| 1970 | bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 1971 | bool demangled_is_synthesized = false; |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 1972 | bool is_gsym = false; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 1973 | |
| 1974 | assert (sym_idx < num_syms); |
| 1975 | |
| 1976 | sym[sym_idx].SetDebug (is_debug); |
| 1977 | |
| 1978 | if (is_debug) |
| 1979 | { |
| 1980 | switch (nlist.n_type) |
| 1981 | { |
| 1982 | case StabGlobalSymbol: |
| 1983 | // N_GSYM -- global symbol: name,,NO_SECT,type,0 |
| 1984 | // Sometimes the N_GSYM value contains the address. |
| 1985 | |
| 1986 | // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They |
| 1987 | // have the same address, but we want to ensure that we always find only the real symbol, |
| 1988 | // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass |
| 1989 | // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated |
| 1990 | // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the |
| 1991 | // same address. |
| 1992 | |
| 1993 | if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O' |
| 1994 | && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0 |
| 1995 | || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0 |
| 1996 | || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0)) |
| 1997 | add_nlist = false; |
| 1998 | else |
| 1999 | { |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2000 | is_gsym = true; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2001 | sym[sym_idx].SetExternal(true); |
| 2002 | if (nlist.n_value != 0) |
| 2003 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2004 | type = eSymbolTypeData; |
| 2005 | } |
| 2006 | break; |
| 2007 | |
| 2008 | case StabFunctionName: |
| 2009 | // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 |
| 2010 | type = eSymbolTypeCompiler; |
| 2011 | break; |
| 2012 | |
| 2013 | case StabFunction: |
| 2014 | // N_FUN -- procedure: name,,n_sect,linenumber,address |
| 2015 | if (symbol_name) |
| 2016 | { |
| 2017 | type = eSymbolTypeCode; |
| 2018 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2019 | |
| 2020 | N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; |
| 2021 | // We use the current number of symbols in the symbol table in lieu of |
| 2022 | // using nlist_idx in case we ever start trimming entries out |
| 2023 | N_FUN_indexes.push_back(sym_idx); |
| 2024 | } |
| 2025 | else |
| 2026 | { |
| 2027 | type = eSymbolTypeCompiler; |
| 2028 | |
| 2029 | if ( !N_FUN_indexes.empty() ) |
| 2030 | { |
| 2031 | // Copy the size of the function into the original STAB entry so we don't have |
| 2032 | // to hunt for it later |
| 2033 | symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); |
| 2034 | N_FUN_indexes.pop_back(); |
| 2035 | // We don't really need the end function STAB as it contains the size which |
| 2036 | // we already placed with the original symbol, so don't add it if we want a |
| 2037 | // minimal symbol table |
| 2038 | if (minimize) |
| 2039 | add_nlist = false; |
| 2040 | } |
| 2041 | } |
| 2042 | break; |
| 2043 | |
| 2044 | case StabStaticSymbol: |
| 2045 | // N_STSYM -- static symbol: name,,n_sect,type,address |
| 2046 | N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; |
| 2047 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2048 | type = eSymbolTypeData; |
| 2049 | break; |
| 2050 | |
| 2051 | case StabLocalCommon: |
| 2052 | // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address |
| 2053 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2054 | type = eSymbolTypeCommonBlock; |
| 2055 | break; |
| 2056 | |
| 2057 | case StabBeginSymbol: |
| 2058 | // N_BNSYM |
| 2059 | // We use the current number of symbols in the symbol table in lieu of |
| 2060 | // using nlist_idx in case we ever start trimming entries out |
| 2061 | if (minimize) |
| 2062 | { |
| 2063 | // Skip these if we want minimal symbol tables |
| 2064 | add_nlist = false; |
| 2065 | } |
| 2066 | else |
| 2067 | { |
| 2068 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2069 | N_NSYM_indexes.push_back(sym_idx); |
| 2070 | type = eSymbolTypeScopeBegin; |
| 2071 | } |
| 2072 | break; |
| 2073 | |
| 2074 | case StabEndSymbol: |
| 2075 | // N_ENSYM |
| 2076 | // Set the size of the N_BNSYM to the terminating index of this N_ENSYM |
| 2077 | // so that we can always skip the entire symbol if we need to navigate |
| 2078 | // more quickly at the source level when parsing STABS |
| 2079 | if (minimize) |
| 2080 | { |
| 2081 | // Skip these if we want minimal symbol tables |
| 2082 | add_nlist = false; |
| 2083 | } |
| 2084 | else |
| 2085 | { |
| 2086 | if ( !N_NSYM_indexes.empty() ) |
| 2087 | { |
| 2088 | symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); |
| 2089 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 2090 | symbol_ptr->SetSizeIsSibling(true); |
| 2091 | N_NSYM_indexes.pop_back(); |
| 2092 | } |
| 2093 | type = eSymbolTypeScopeEnd; |
| 2094 | } |
| 2095 | break; |
| 2096 | |
| 2097 | |
| 2098 | case StabSourceFileOptions: |
| 2099 | // N_OPT - emitted with gcc2_compiled and in gcc source |
| 2100 | type = eSymbolTypeCompiler; |
| 2101 | break; |
| 2102 | |
| 2103 | case StabRegisterSymbol: |
| 2104 | // N_RSYM - register sym: name,,NO_SECT,type,register |
| 2105 | type = eSymbolTypeVariable; |
| 2106 | break; |
| 2107 | |
| 2108 | case StabSourceLine: |
| 2109 | // N_SLINE - src line: 0,,n_sect,linenumber,address |
| 2110 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2111 | type = eSymbolTypeLineEntry; |
| 2112 | break; |
| 2113 | |
| 2114 | case StabStructureType: |
| 2115 | // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset |
| 2116 | type = eSymbolTypeVariableType; |
| 2117 | break; |
| 2118 | |
| 2119 | case StabSourceFileName: |
| 2120 | // N_SO - source file name |
| 2121 | type = eSymbolTypeSourceFile; |
| 2122 | if (symbol_name == NULL) |
| 2123 | { |
| 2124 | if (minimize) |
| 2125 | add_nlist = false; |
| 2126 | if (N_SO_index != UINT32_MAX) |
| 2127 | { |
| 2128 | // Set the size of the N_SO to the terminating index of this N_SO |
| 2129 | // so that we can always skip the entire N_SO if we need to navigate |
| 2130 | // more quickly at the source level when parsing STABS |
| 2131 | symbol_ptr = symtab->SymbolAtIndex(N_SO_index); |
| 2132 | symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1)); |
| 2133 | symbol_ptr->SetSizeIsSibling(true); |
| 2134 | } |
| 2135 | N_NSYM_indexes.clear(); |
| 2136 | N_INCL_indexes.clear(); |
| 2137 | N_BRAC_indexes.clear(); |
| 2138 | N_COMM_indexes.clear(); |
| 2139 | N_FUN_indexes.clear(); |
| 2140 | N_SO_index = UINT32_MAX; |
| 2141 | } |
| 2142 | else |
| 2143 | { |
| 2144 | // We use the current number of symbols in the symbol table in lieu of |
| 2145 | // using nlist_idx in case we ever start trimming entries out |
| 2146 | const bool N_SO_has_full_path = symbol_name[0] == '/'; |
| 2147 | if (N_SO_has_full_path) |
| 2148 | { |
| 2149 | if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) |
| 2150 | { |
| 2151 | // We have two consecutive N_SO entries where the first contains a directory |
| 2152 | // and the second contains a full path. |
Jason Molenda | 292cca8 | 2012-07-20 03:35:44 +0000 | [diff] [blame] | 2153 | sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2154 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 2155 | add_nlist = false; |
| 2156 | } |
| 2157 | else |
| 2158 | { |
| 2159 | // This is the first entry in a N_SO that contains a directory or |
| 2160 | // a full path to the source file |
| 2161 | N_SO_index = sym_idx; |
| 2162 | } |
| 2163 | } |
| 2164 | else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) |
| 2165 | { |
| 2166 | // This is usually the second N_SO entry that contains just the filename, |
| 2167 | // so here we combine it with the first one if we are minimizing the symbol table |
| 2168 | const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); |
| 2169 | if (so_path && so_path[0]) |
| 2170 | { |
| 2171 | std::string full_so_path (so_path); |
Greg Clayton | 4df2b7f | 2012-09-07 20:29:13 +0000 | [diff] [blame] | 2172 | const size_t double_slash_pos = full_so_path.find("//"); |
| 2173 | if (double_slash_pos != std::string::npos) |
| 2174 | { |
| 2175 | // The linker has been generating bad N_SO entries with doubled up paths |
| 2176 | // in the format "%s%s" where the first stirng in the DW_AT_comp_dir, |
| 2177 | // and the second is the directory for the source file so you end up with |
| 2178 | // a path that looks like "/tmp/src//tmp/src/" |
| 2179 | FileSpec so_dir(so_path, false); |
| 2180 | if (!so_dir.Exists()) |
| 2181 | { |
| 2182 | so_dir.SetFile(&full_so_path[double_slash_pos + 1], false); |
| 2183 | if (so_dir.Exists()) |
| 2184 | { |
| 2185 | // Trim off the incorrect path |
| 2186 | full_so_path.erase(0, double_slash_pos + 1); |
| 2187 | } |
| 2188 | } |
| 2189 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2190 | if (*full_so_path.rbegin() != '/') |
| 2191 | full_so_path += '/'; |
| 2192 | full_so_path += symbol_name; |
Jason Molenda | 292cca8 | 2012-07-20 03:35:44 +0000 | [diff] [blame] | 2193 | sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2194 | add_nlist = false; |
| 2195 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 2196 | } |
| 2197 | } |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 2198 | else |
| 2199 | { |
| 2200 | // This could be a relative path to a N_SO |
| 2201 | N_SO_index = sym_idx; |
| 2202 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2203 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2204 | break; |
| 2205 | |
| 2206 | case StabObjectFileName: |
| 2207 | // N_OSO - object file name: name,,0,0,st_mtime |
| 2208 | type = eSymbolTypeObjectFile; |
| 2209 | break; |
| 2210 | |
| 2211 | case StabLocalSymbol: |
| 2212 | // N_LSYM - local sym: name,,NO_SECT,type,offset |
| 2213 | type = eSymbolTypeLocal; |
| 2214 | break; |
| 2215 | |
| 2216 | //---------------------------------------------------------------------- |
| 2217 | // INCL scopes |
| 2218 | //---------------------------------------------------------------------- |
| 2219 | case StabBeginIncludeFileName: |
| 2220 | // N_BINCL - include file beginning: name,,NO_SECT,0,sum |
| 2221 | // We use the current number of symbols in the symbol table in lieu of |
| 2222 | // using nlist_idx in case we ever start trimming entries out |
| 2223 | N_INCL_indexes.push_back(sym_idx); |
| 2224 | type = eSymbolTypeScopeBegin; |
| 2225 | break; |
| 2226 | |
| 2227 | case StabEndIncludeFile: |
| 2228 | // N_EINCL - include file end: name,,NO_SECT,0,0 |
| 2229 | // Set the size of the N_BINCL to the terminating index of this N_EINCL |
| 2230 | // so that we can always skip the entire symbol if we need to navigate |
| 2231 | // more quickly at the source level when parsing STABS |
| 2232 | if ( !N_INCL_indexes.empty() ) |
| 2233 | { |
| 2234 | symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); |
| 2235 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 2236 | symbol_ptr->SetSizeIsSibling(true); |
| 2237 | N_INCL_indexes.pop_back(); |
| 2238 | } |
| 2239 | type = eSymbolTypeScopeEnd; |
| 2240 | break; |
| 2241 | |
| 2242 | case StabIncludeFileName: |
| 2243 | // N_SOL - #included file name: name,,n_sect,0,address |
| 2244 | type = eSymbolTypeHeaderFile; |
| 2245 | |
| 2246 | // We currently don't use the header files on darwin |
| 2247 | if (minimize) |
| 2248 | add_nlist = false; |
| 2249 | break; |
| 2250 | |
| 2251 | case StabCompilerParameters: |
| 2252 | // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 |
| 2253 | type = eSymbolTypeCompiler; |
| 2254 | break; |
| 2255 | |
| 2256 | case StabCompilerVersion: |
| 2257 | // N_VERSION - compiler version: name,,NO_SECT,0,0 |
| 2258 | type = eSymbolTypeCompiler; |
| 2259 | break; |
| 2260 | |
| 2261 | case StabCompilerOptLevel: |
| 2262 | // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 |
| 2263 | type = eSymbolTypeCompiler; |
| 2264 | break; |
| 2265 | |
| 2266 | case StabParameter: |
| 2267 | // N_PSYM - parameter: name,,NO_SECT,type,offset |
| 2268 | type = eSymbolTypeVariable; |
| 2269 | break; |
| 2270 | |
| 2271 | case StabAlternateEntry: |
| 2272 | // N_ENTRY - alternate entry: name,,n_sect,linenumber,address |
| 2273 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2274 | type = eSymbolTypeLineEntry; |
| 2275 | break; |
| 2276 | |
| 2277 | //---------------------------------------------------------------------- |
| 2278 | // Left and Right Braces |
| 2279 | //---------------------------------------------------------------------- |
| 2280 | case StabLeftBracket: |
| 2281 | // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address |
| 2282 | // We use the current number of symbols in the symbol table in lieu of |
| 2283 | // using nlist_idx in case we ever start trimming entries out |
| 2284 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2285 | N_BRAC_indexes.push_back(sym_idx); |
| 2286 | type = eSymbolTypeScopeBegin; |
| 2287 | break; |
| 2288 | |
| 2289 | case StabRightBracket: |
| 2290 | // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address |
| 2291 | // Set the size of the N_LBRAC to the terminating index of this N_RBRAC |
| 2292 | // so that we can always skip the entire symbol if we need to navigate |
| 2293 | // more quickly at the source level when parsing STABS |
| 2294 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2295 | if ( !N_BRAC_indexes.empty() ) |
| 2296 | { |
| 2297 | symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); |
| 2298 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 2299 | symbol_ptr->SetSizeIsSibling(true); |
| 2300 | N_BRAC_indexes.pop_back(); |
| 2301 | } |
| 2302 | type = eSymbolTypeScopeEnd; |
| 2303 | break; |
| 2304 | |
| 2305 | case StabDeletedIncludeFile: |
| 2306 | // N_EXCL - deleted include file: name,,NO_SECT,0,sum |
| 2307 | type = eSymbolTypeHeaderFile; |
| 2308 | break; |
| 2309 | |
| 2310 | //---------------------------------------------------------------------- |
| 2311 | // COMM scopes |
| 2312 | //---------------------------------------------------------------------- |
| 2313 | case StabBeginCommon: |
| 2314 | // N_BCOMM - begin common: name,,NO_SECT,0,0 |
| 2315 | // We use the current number of symbols in the symbol table in lieu of |
| 2316 | // using nlist_idx in case we ever start trimming entries out |
| 2317 | type = eSymbolTypeScopeBegin; |
| 2318 | N_COMM_indexes.push_back(sym_idx); |
| 2319 | break; |
| 2320 | |
| 2321 | case StabEndCommonLocal: |
| 2322 | // N_ECOML - end common (local name): 0,,n_sect,0,address |
| 2323 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2324 | // Fall through |
| 2325 | |
| 2326 | case StabEndCommon: |
| 2327 | // N_ECOMM - end common: name,,n_sect,0,0 |
| 2328 | // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML |
| 2329 | // so that we can always skip the entire symbol if we need to navigate |
| 2330 | // more quickly at the source level when parsing STABS |
| 2331 | if ( !N_COMM_indexes.empty() ) |
| 2332 | { |
| 2333 | symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); |
| 2334 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 2335 | symbol_ptr->SetSizeIsSibling(true); |
| 2336 | N_COMM_indexes.pop_back(); |
| 2337 | } |
| 2338 | type = eSymbolTypeScopeEnd; |
| 2339 | break; |
| 2340 | |
| 2341 | case StabLength: |
| 2342 | // N_LENG - second stab entry with length information |
| 2343 | type = eSymbolTypeAdditional; |
| 2344 | break; |
| 2345 | |
| 2346 | default: break; |
| 2347 | } |
| 2348 | } |
| 2349 | else |
| 2350 | { |
| 2351 | //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; |
| 2352 | uint8_t n_type = NlistMaskType & nlist.n_type; |
| 2353 | sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); |
| 2354 | |
| 2355 | switch (n_type) |
| 2356 | { |
| 2357 | case NListTypeIndirect: // N_INDR - Fall through |
| 2358 | case NListTypePreboundUndefined:// N_PBUD - Fall through |
| 2359 | case NListTypeUndefined: // N_UNDF |
| 2360 | type = eSymbolTypeUndefined; |
| 2361 | break; |
| 2362 | |
| 2363 | case NListTypeAbsolute: // N_ABS |
| 2364 | type = eSymbolTypeAbsolute; |
| 2365 | break; |
| 2366 | |
| 2367 | case NListTypeSection: // N_SECT |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2368 | { |
| 2369 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2370 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2371 | if (symbol_section == NULL) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2372 | { |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2373 | // TODO: warn about this? |
| 2374 | add_nlist = false; |
| 2375 | break; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2376 | } |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2377 | |
| 2378 | if (TEXT_eh_frame_sectID == nlist.n_sect) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2379 | { |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2380 | type = eSymbolTypeException; |
| 2381 | } |
| 2382 | else |
| 2383 | { |
| 2384 | uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2385 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2386 | switch (section_type) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2387 | { |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2388 | case SectionTypeRegular: break; // regular section |
| 2389 | //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section |
| 2390 | case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings |
| 2391 | case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals |
| 2392 | case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals |
| 2393 | case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals |
| 2394 | case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers |
| 2395 | case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers |
| 2396 | case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field |
| 2397 | case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization |
| 2398 | case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination |
| 2399 | //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced |
| 2400 | //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) |
| 2401 | case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing |
| 2402 | case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals |
| 2403 | case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; |
| 2404 | case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; |
| 2405 | default: break; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2406 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2407 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2408 | if (type == eSymbolTypeInvalid) |
| 2409 | { |
| 2410 | const char *symbol_sect_name = symbol_section->GetName().AsCString(); |
| 2411 | if (symbol_section->IsDescendant (text_section_sp.get())) |
| 2412 | { |
| 2413 | if (symbol_section->IsClear(SectionAttrUserPureInstructions | |
| 2414 | SectionAttrUserSelfModifyingCode | |
| 2415 | SectionAttrSytemSomeInstructions)) |
| 2416 | type = eSymbolTypeData; |
| 2417 | else |
| 2418 | type = eSymbolTypeCode; |
| 2419 | } |
| 2420 | else if (symbol_section->IsDescendant(data_section_sp.get())) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2421 | { |
| 2422 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) |
| 2423 | { |
| 2424 | type = eSymbolTypeRuntime; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2425 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2426 | if (symbol_name && |
| 2427 | symbol_name[0] == '_' && |
| 2428 | symbol_name[1] == 'O' && |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2429 | symbol_name[2] == 'B') |
| 2430 | { |
| 2431 | llvm::StringRef symbol_name_ref(symbol_name); |
| 2432 | static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_"); |
| 2433 | static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_"); |
| 2434 | static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_"); |
| 2435 | if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) |
| 2436 | { |
| 2437 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2438 | symbol_name = symbol_name + g_objc_v2_prefix_class.size(); |
| 2439 | type = eSymbolTypeObjCClass; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2440 | demangled_is_synthesized = true; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2441 | } |
| 2442 | else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) |
| 2443 | { |
| 2444 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2445 | symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); |
| 2446 | type = eSymbolTypeObjCMetaClass; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2447 | demangled_is_synthesized = true; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2448 | } |
| 2449 | else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) |
| 2450 | { |
| 2451 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 2452 | symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); |
| 2453 | type = eSymbolTypeObjCIVar; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2454 | demangled_is_synthesized = true; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | } |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2458 | else if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2459 | { |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2460 | type = eSymbolTypeException; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2461 | } |
| 2462 | else |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2463 | { |
| 2464 | type = eSymbolTypeData; |
| 2465 | } |
| 2466 | } |
| 2467 | else if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) |
| 2468 | { |
| 2469 | type = eSymbolTypeTrampoline; |
| 2470 | } |
| 2471 | else if (symbol_section->IsDescendant(objc_section_sp.get())) |
| 2472 | { |
| 2473 | type = eSymbolTypeRuntime; |
| 2474 | if (symbol_name && symbol_name[0] == '.') |
| 2475 | { |
| 2476 | llvm::StringRef symbol_name_ref(symbol_name); |
| 2477 | static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_"); |
| 2478 | if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2479 | { |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2480 | symbol_name_non_abi_mangled = symbol_name; |
| 2481 | symbol_name = symbol_name + g_objc_v1_prefix_class.size(); |
| 2482 | type = eSymbolTypeObjCClass; |
| 2483 | demangled_is_synthesized = true; |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2484 | } |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2488 | } |
| 2489 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2490 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2491 | } |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | if (add_nlist) |
| 2495 | { |
| 2496 | uint64_t symbol_value = nlist.n_value; |
| 2497 | bool symbol_name_is_mangled = false; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2498 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2499 | if (symbol_name_non_abi_mangled) |
| 2500 | { |
Jason Molenda | 292cca8 | 2012-07-20 03:35:44 +0000 | [diff] [blame] | 2501 | sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled)); |
| 2502 | sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name)); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2503 | } |
| 2504 | else |
| 2505 | { |
| 2506 | if (symbol_name && symbol_name[0] == '_') |
| 2507 | { |
| 2508 | symbol_name_is_mangled = symbol_name[1] == '_'; |
| 2509 | symbol_name++; // Skip the leading underscore |
| 2510 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2511 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2512 | if (symbol_name) |
| 2513 | { |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2514 | ConstString const_symbol_name(symbol_name); |
| 2515 | if (is_gsym) |
| 2516 | N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx; |
| 2517 | sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | if (symbol_section) |
| 2521 | { |
| 2522 | const addr_t section_file_addr = symbol_section->GetFileAddress(); |
| 2523 | if (symbol_byte_size == 0 && function_starts_count > 0) |
| 2524 | { |
| 2525 | addr_t symbol_lookup_file_addr = nlist.n_value; |
| 2526 | // Do an exact address match for non-ARM addresses, else get the closest since |
| 2527 | // the symbol might be a thumb symbol which has an address with bit zero set |
| 2528 | FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm); |
| 2529 | if (is_arm && func_start_entry) |
| 2530 | { |
| 2531 | // Verify that the function start address is the symbol address (ARM) |
| 2532 | // or the symbol address + 1 (thumb) |
| 2533 | if (func_start_entry->addr != symbol_lookup_file_addr && |
| 2534 | func_start_entry->addr != (symbol_lookup_file_addr + 1)) |
| 2535 | { |
| 2536 | // Not the right entry, NULL it out... |
| 2537 | func_start_entry = NULL; |
| 2538 | } |
| 2539 | } |
| 2540 | if (func_start_entry) |
| 2541 | { |
| 2542 | func_start_entry->data = true; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2543 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2544 | addr_t symbol_file_addr = func_start_entry->addr; |
| 2545 | uint32_t symbol_flags = 0; |
| 2546 | if (is_arm) |
| 2547 | { |
| 2548 | if (symbol_file_addr & 1) |
| 2549 | symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; |
| 2550 | symbol_file_addr &= 0xfffffffffffffffeull; |
| 2551 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2552 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2553 | const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); |
| 2554 | const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); |
| 2555 | if (next_func_start_entry) |
| 2556 | { |
| 2557 | addr_t next_symbol_file_addr = next_func_start_entry->addr; |
| 2558 | // Be sure the clear the Thumb address bit when we calculate the size |
| 2559 | // from the current and next address |
| 2560 | if (is_arm) |
| 2561 | next_symbol_file_addr &= 0xfffffffffffffffeull; |
| 2562 | symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); |
| 2563 | } |
| 2564 | else |
| 2565 | { |
| 2566 | symbol_byte_size = section_end_file_addr - symbol_file_addr; |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | symbol_value -= section_file_addr; |
| 2571 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2572 | |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2573 | if (is_debug == false) |
| 2574 | { |
| 2575 | if (type == eSymbolTypeCode) |
| 2576 | { |
| 2577 | // See if we can find a N_FUN entry for any code symbols. |
| 2578 | // If we do find a match, and the name matches, then we |
| 2579 | // can merge the two into just the function symbol to avoid |
| 2580 | // duplicate entries in the symbol table |
| 2581 | ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); |
| 2582 | if (pos != N_FUN_addr_to_sym_idx.end()) |
| 2583 | { |
| 2584 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 2585 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 2586 | { |
| 2587 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 2588 | // We just need the flags from the linker symbol, so put these flags |
| 2589 | // into the N_FUN flags to avoid duplicate symbols in the symbol table |
| 2590 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 2591 | sym[sym_idx].Clear(); |
| 2592 | continue; |
| 2593 | } |
| 2594 | } |
| 2595 | } |
| 2596 | else if (type == eSymbolTypeData) |
| 2597 | { |
| 2598 | // See if we can find a N_STSYM entry for any data symbols. |
| 2599 | // If we do find a match, and the name matches, then we |
| 2600 | // can merge the two into just the Static symbol to avoid |
| 2601 | // duplicate entries in the symbol table |
| 2602 | ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); |
| 2603 | if (pos != N_STSYM_addr_to_sym_idx.end()) |
| 2604 | { |
| 2605 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 2606 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 2607 | { |
| 2608 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 2609 | // We just need the flags from the linker symbol, so put these flags |
| 2610 | // into the N_STSYM flags to avoid duplicate symbols in the symbol table |
| 2611 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 2612 | sym[sym_idx].Clear(); |
| 2613 | continue; |
| 2614 | } |
| 2615 | } |
| 2616 | else |
| 2617 | { |
| 2618 | // Combine N_GSYM stab entries with the non stab symbol |
| 2619 | ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString()); |
| 2620 | if (pos != N_GSYM_name_to_sym_idx.end()) |
| 2621 | { |
| 2622 | const uint32_t GSYM_sym_idx = pos->second; |
| 2623 | m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; |
| 2624 | // Copy the address, because often the N_GSYM address has an invalid address of zero |
| 2625 | // when the global is a common symbol |
| 2626 | sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section); |
| 2627 | sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value); |
| 2628 | // We just need the flags from the linker symbol, so put these flags |
| 2629 | // into the N_STSYM flags to avoid duplicate symbols in the symbol table |
| 2630 | sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 2631 | sym[sym_idx].Clear(); |
| 2632 | continue; |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2638 | sym[sym_idx].SetID (nlist_idx); |
| 2639 | sym[sym_idx].SetType (type); |
| 2640 | sym[sym_idx].GetAddress().SetSection (symbol_section); |
| 2641 | sym[sym_idx].GetAddress().SetOffset (symbol_value); |
| 2642 | sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2643 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2644 | if (symbol_byte_size > 0) |
| 2645 | sym[sym_idx].SetByteSize(symbol_byte_size); |
| 2646 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2647 | if (demangled_is_synthesized) |
| 2648 | sym[sym_idx].SetDemangledNameIsSynthesized(true); |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2649 | ++sym_idx; |
| 2650 | } |
| 2651 | else |
| 2652 | { |
| 2653 | sym[sym_idx].Clear(); |
| 2654 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2655 | |
Jason Molenda | b62abd5 | 2012-06-21 01:51:02 +0000 | [diff] [blame] | 2656 | } |
| 2657 | ///////////////////////////// |
| 2658 | } |
| 2659 | break; // No more entries to consider |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | // Must reset this in case it was mutated above! |
| 2669 | nlist_data_offset = 0; |
| 2670 | #endif |
| 2671 | |
| 2672 | // If the sym array was not created while parsing the DSC unmapped |
| 2673 | // symbols, create it now. |
| 2674 | if (sym == NULL) |
| 2675 | { |
| 2676 | sym = symtab->Resize (symtab_load_command.nsyms + m_dysymtab.nindirectsyms); |
| 2677 | num_syms = symtab->GetNumSymbols(); |
| 2678 | } |
| 2679 | |
| 2680 | if (unmapped_local_symbols_found) |
| 2681 | { |
| 2682 | assert(m_dysymtab.ilocalsym == 0); |
| 2683 | nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size); |
| 2684 | nlist_idx = m_dysymtab.nlocalsym; |
| 2685 | } |
| 2686 | else |
| 2687 | { |
| 2688 | nlist_idx = 0; |
| 2689 | } |
| 2690 | |
| 2691 | for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2692 | { |
| 2693 | struct nlist_64 nlist; |
| 2694 | if (!nlist_data.ValidOffsetForDataOfSize(nlist_data_offset, nlist_byte_size)) |
| 2695 | break; |
| 2696 | |
| 2697 | nlist.n_strx = nlist_data.GetU32_unchecked(&nlist_data_offset); |
| 2698 | nlist.n_type = nlist_data.GetU8_unchecked (&nlist_data_offset); |
| 2699 | nlist.n_sect = nlist_data.GetU8_unchecked (&nlist_data_offset); |
| 2700 | nlist.n_desc = nlist_data.GetU16_unchecked (&nlist_data_offset); |
| 2701 | nlist.n_value = nlist_data.GetAddress_unchecked (&nlist_data_offset); |
| 2702 | |
| 2703 | SymbolType type = eSymbolTypeInvalid; |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2704 | const char *symbol_name = NULL; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2705 | |
Greg Clayton | 3a5dc01 | 2012-05-25 17:04:00 +0000 | [diff] [blame] | 2706 | if (have_strtab_data) |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2707 | { |
| 2708 | symbol_name = strtab_data.PeekCStr(nlist.n_strx); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2709 | |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2710 | if (symbol_name == NULL) |
| 2711 | { |
| 2712 | // No symbol should be NULL, even the symbols with no |
| 2713 | // string values should have an offset zero which points |
| 2714 | // to an empty C-string |
| 2715 | Host::SystemLog (Host::eSystemLogError, |
Greg Clayton | 97a19b2 | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 2716 | "error: symbol[%u] has invalid string table offset 0x%x in %s, ignoring symbol\n", |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2717 | nlist_idx, |
| 2718 | nlist.n_strx, |
Greg Clayton | 97a19b2 | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 2719 | module_sp->GetFileSpec().GetPath().c_str()); |
Greg Clayton | dd29b97 | 2012-05-18 23:20:01 +0000 | [diff] [blame] | 2720 | continue; |
| 2721 | } |
| 2722 | if (symbol_name[0] == '\0') |
| 2723 | symbol_name = NULL; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2724 | } |
Greg Clayton | 3a5dc01 | 2012-05-25 17:04:00 +0000 | [diff] [blame] | 2725 | else |
| 2726 | { |
| 2727 | const addr_t str_addr = strtab_addr + nlist.n_strx; |
| 2728 | Error str_error; |
| 2729 | if (process->ReadCStringFromMemory(str_addr, memory_symbol_name, str_error)) |
| 2730 | symbol_name = memory_symbol_name.c_str(); |
| 2731 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2732 | const char *symbol_name_non_abi_mangled = NULL; |
| 2733 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2734 | SectionSP symbol_section; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2735 | lldb::addr_t symbol_byte_size = 0; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2736 | bool add_nlist = true; |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2737 | bool is_gsym = false; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2738 | bool is_debug = ((nlist.n_type & NlistMaskStab) != 0); |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 2739 | bool demangled_is_synthesized = false; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2740 | |
| 2741 | assert (sym_idx < num_syms); |
| 2742 | |
| 2743 | sym[sym_idx].SetDebug (is_debug); |
| 2744 | |
| 2745 | if (is_debug) |
| 2746 | { |
| 2747 | switch (nlist.n_type) |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 2748 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2749 | case StabGlobalSymbol: |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2750 | // N_GSYM -- global symbol: name,,NO_SECT,type,0 |
| 2751 | // Sometimes the N_GSYM value contains the address. |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2752 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2753 | // FIXME: In the .o files, we have a GSYM and a debug symbol for all the ObjC data. They |
| 2754 | // have the same address, but we want to ensure that we always find only the real symbol, |
| 2755 | // 'cause we don't currently correctly attribute the GSYM one to the ObjCClass/Ivar/MetaClass |
| 2756 | // symbol type. This is a temporary hack to make sure the ObjectiveC symbols get treated |
| 2757 | // correctly. To do this right, we should coalesce all the GSYM & global symbols that have the |
| 2758 | // same address. |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2759 | |
| 2760 | if (symbol_name && symbol_name[0] == '_' && symbol_name[1] == 'O' |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2761 | && (strncmp (symbol_name, "_OBJC_IVAR_$_", strlen ("_OBJC_IVAR_$_")) == 0 |
| 2762 | || strncmp (symbol_name, "_OBJC_CLASS_$_", strlen ("_OBJC_CLASS_$_")) == 0 |
| 2763 | || strncmp (symbol_name, "_OBJC_METACLASS_$_", strlen ("_OBJC_METACLASS_$_")) == 0)) |
| 2764 | add_nlist = false; |
| 2765 | else |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2766 | { |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 2767 | is_gsym = true; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2768 | sym[sym_idx].SetExternal(true); |
| 2769 | if (nlist.n_value != 0) |
| 2770 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2771 | type = eSymbolTypeData; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2772 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2773 | break; |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2774 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2775 | case StabFunctionName: |
| 2776 | // N_FNAME -- procedure name (f77 kludge): name,,NO_SECT,0,0 |
| 2777 | type = eSymbolTypeCompiler; |
| 2778 | break; |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 2779 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2780 | case StabFunction: |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2781 | // N_FUN -- procedure: name,,n_sect,linenumber,address |
| 2782 | if (symbol_name) |
Greg Clayton | a9c4f31 | 2011-10-31 20:50:40 +0000 | [diff] [blame] | 2783 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2784 | type = eSymbolTypeCode; |
| 2785 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2786 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2787 | N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx; |
| 2788 | // We use the current number of symbols in the symbol table in lieu of |
| 2789 | // using nlist_idx in case we ever start trimming entries out |
| 2790 | N_FUN_indexes.push_back(sym_idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2791 | } |
| 2792 | else |
| 2793 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2794 | type = eSymbolTypeCompiler; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2795 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2796 | if ( !N_FUN_indexes.empty() ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2797 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2798 | // Copy the size of the function into the original STAB entry so we don't have |
| 2799 | // to hunt for it later |
| 2800 | symtab->SymbolAtIndex(N_FUN_indexes.back())->SetByteSize(nlist.n_value); |
| 2801 | N_FUN_indexes.pop_back(); |
| 2802 | // We don't really need the end function STAB as it contains the size which |
| 2803 | // we already placed with the original symbol, so don't add it if we want a |
| 2804 | // minimal symbol table |
| 2805 | if (minimize) |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2806 | add_nlist = false; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2807 | } |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2808 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2809 | break; |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2810 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2811 | case StabStaticSymbol: |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2812 | // N_STSYM -- static symbol: name,,n_sect,type,address |
| 2813 | N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx; |
| 2814 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2815 | type = eSymbolTypeData; |
| 2816 | break; |
| 2817 | |
| 2818 | case StabLocalCommon: |
| 2819 | // N_LCSYM -- .lcomm symbol: name,,n_sect,type,address |
| 2820 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2821 | type = eSymbolTypeCommonBlock; |
| 2822 | break; |
| 2823 | |
| 2824 | case StabBeginSymbol: |
| 2825 | // N_BNSYM |
| 2826 | // We use the current number of symbols in the symbol table in lieu of |
| 2827 | // using nlist_idx in case we ever start trimming entries out |
| 2828 | if (minimize) |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2829 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2830 | // Skip these if we want minimal symbol tables |
| 2831 | add_nlist = false; |
| 2832 | } |
| 2833 | else |
| 2834 | { |
| 2835 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2836 | N_NSYM_indexes.push_back(sym_idx); |
| 2837 | type = eSymbolTypeScopeBegin; |
| 2838 | } |
| 2839 | break; |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2840 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2841 | case StabEndSymbol: |
| 2842 | // N_ENSYM |
| 2843 | // Set the size of the N_BNSYM to the terminating index of this N_ENSYM |
| 2844 | // so that we can always skip the entire symbol if we need to navigate |
| 2845 | // more quickly at the source level when parsing STABS |
| 2846 | if (minimize) |
| 2847 | { |
| 2848 | // Skip these if we want minimal symbol tables |
| 2849 | add_nlist = false; |
| 2850 | } |
| 2851 | else |
| 2852 | { |
| 2853 | if ( !N_NSYM_indexes.empty() ) |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 2854 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2855 | symbol_ptr = symtab->SymbolAtIndex(N_NSYM_indexes.back()); |
| 2856 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 2857 | symbol_ptr->SetSizeIsSibling(true); |
| 2858 | N_NSYM_indexes.pop_back(); |
| 2859 | } |
| 2860 | type = eSymbolTypeScopeEnd; |
| 2861 | } |
| 2862 | break; |
| 2863 | |
| 2864 | |
| 2865 | case StabSourceFileOptions: |
| 2866 | // N_OPT - emitted with gcc2_compiled and in gcc source |
| 2867 | type = eSymbolTypeCompiler; |
| 2868 | break; |
| 2869 | |
| 2870 | case StabRegisterSymbol: |
| 2871 | // N_RSYM - register sym: name,,NO_SECT,type,register |
| 2872 | type = eSymbolTypeVariable; |
| 2873 | break; |
| 2874 | |
| 2875 | case StabSourceLine: |
| 2876 | // N_SLINE - src line: 0,,n_sect,linenumber,address |
| 2877 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 2878 | type = eSymbolTypeLineEntry; |
| 2879 | break; |
| 2880 | |
| 2881 | case StabStructureType: |
| 2882 | // N_SSYM - structure elt: name,,NO_SECT,type,struct_offset |
| 2883 | type = eSymbolTypeVariableType; |
| 2884 | break; |
| 2885 | |
| 2886 | case StabSourceFileName: |
| 2887 | // N_SO - source file name |
| 2888 | type = eSymbolTypeSourceFile; |
| 2889 | if (symbol_name == NULL) |
| 2890 | { |
| 2891 | if (minimize) |
| 2892 | add_nlist = false; |
| 2893 | if (N_SO_index != UINT32_MAX) |
| 2894 | { |
| 2895 | // Set the size of the N_SO to the terminating index of this N_SO |
| 2896 | // so that we can always skip the entire N_SO if we need to navigate |
| 2897 | // more quickly at the source level when parsing STABS |
| 2898 | symbol_ptr = symtab->SymbolAtIndex(N_SO_index); |
| 2899 | symbol_ptr->SetByteSize(sym_idx + (minimize ? 0 : 1)); |
| 2900 | symbol_ptr->SetSizeIsSibling(true); |
| 2901 | } |
| 2902 | N_NSYM_indexes.clear(); |
| 2903 | N_INCL_indexes.clear(); |
| 2904 | N_BRAC_indexes.clear(); |
| 2905 | N_COMM_indexes.clear(); |
| 2906 | N_FUN_indexes.clear(); |
| 2907 | N_SO_index = UINT32_MAX; |
| 2908 | } |
| 2909 | else |
| 2910 | { |
| 2911 | // We use the current number of symbols in the symbol table in lieu of |
| 2912 | // using nlist_idx in case we ever start trimming entries out |
Greg Clayton | 5fa6cd3 | 2012-05-30 20:20:34 +0000 | [diff] [blame] | 2913 | const bool N_SO_has_full_path = symbol_name[0] == '/'; |
| 2914 | if (N_SO_has_full_path) |
| 2915 | { |
| 2916 | if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) |
| 2917 | { |
| 2918 | // We have two consecutive N_SO entries where the first contains a directory |
| 2919 | // and the second contains a full path. |
Greg Clayton | c024004 | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 2920 | sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name), false); |
Greg Clayton | 5fa6cd3 | 2012-05-30 20:20:34 +0000 | [diff] [blame] | 2921 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 2922 | add_nlist = false; |
| 2923 | } |
| 2924 | else |
| 2925 | { |
| 2926 | // This is the first entry in a N_SO that contains a directory or |
| 2927 | // a full path to the source file |
| 2928 | N_SO_index = sym_idx; |
| 2929 | } |
| 2930 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2931 | else if (minimize && (N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) |
| 2932 | { |
Greg Clayton | 5fa6cd3 | 2012-05-30 20:20:34 +0000 | [diff] [blame] | 2933 | // This is usually the second N_SO entry that contains just the filename, |
| 2934 | // so here we combine it with the first one if we are minimizing the symbol table |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2935 | const char *so_path = sym[sym_idx - 1].GetMangled().GetDemangledName().AsCString(); |
| 2936 | if (so_path && so_path[0]) |
| 2937 | { |
| 2938 | std::string full_so_path (so_path); |
Greg Clayton | 4df2b7f | 2012-09-07 20:29:13 +0000 | [diff] [blame] | 2939 | const size_t double_slash_pos = full_so_path.find("//"); |
| 2940 | if (double_slash_pos != std::string::npos) |
| 2941 | { |
| 2942 | // The linker has been generating bad N_SO entries with doubled up paths |
| 2943 | // in the format "%s%s" where the first stirng in the DW_AT_comp_dir, |
| 2944 | // and the second is the directory for the source file so you end up with |
| 2945 | // a path that looks like "/tmp/src//tmp/src/" |
| 2946 | FileSpec so_dir(so_path, false); |
| 2947 | if (!so_dir.Exists()) |
| 2948 | { |
| 2949 | so_dir.SetFile(&full_so_path[double_slash_pos + 1], false); |
| 2950 | if (so_dir.Exists()) |
| 2951 | { |
| 2952 | // Trim off the incorrect path |
| 2953 | full_so_path.erase(0, double_slash_pos + 1); |
| 2954 | } |
| 2955 | } |
| 2956 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2957 | if (*full_so_path.rbegin() != '/') |
| 2958 | full_so_path += '/'; |
| 2959 | full_so_path += symbol_name; |
Greg Clayton | c024004 | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 2960 | sym[sym_idx - 1].GetMangled().SetValue(ConstString(full_so_path.c_str()), false); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2961 | add_nlist = false; |
| 2962 | m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1; |
| 2963 | } |
| 2964 | } |
Greg Clayton | ab77dcb | 2012-09-05 22:30:51 +0000 | [diff] [blame] | 2965 | else |
| 2966 | { |
| 2967 | // This could be a relative path to a N_SO |
| 2968 | N_SO_index = sym_idx; |
| 2969 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2970 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 2971 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 2972 | break; |
| 2973 | |
| 2974 | case StabObjectFileName: |
| 2975 | // N_OSO - object file name: name,,0,0,st_mtime |
| 2976 | type = eSymbolTypeObjectFile; |
| 2977 | break; |
| 2978 | |
| 2979 | case StabLocalSymbol: |
| 2980 | // N_LSYM - local sym: name,,NO_SECT,type,offset |
| 2981 | type = eSymbolTypeLocal; |
| 2982 | break; |
| 2983 | |
| 2984 | //---------------------------------------------------------------------- |
| 2985 | // INCL scopes |
| 2986 | //---------------------------------------------------------------------- |
| 2987 | case StabBeginIncludeFileName: |
| 2988 | // N_BINCL - include file beginning: name,,NO_SECT,0,sum |
| 2989 | // We use the current number of symbols in the symbol table in lieu of |
| 2990 | // using nlist_idx in case we ever start trimming entries out |
| 2991 | N_INCL_indexes.push_back(sym_idx); |
| 2992 | type = eSymbolTypeScopeBegin; |
| 2993 | break; |
| 2994 | |
| 2995 | case StabEndIncludeFile: |
| 2996 | // N_EINCL - include file end: name,,NO_SECT,0,0 |
| 2997 | // Set the size of the N_BINCL to the terminating index of this N_EINCL |
| 2998 | // so that we can always skip the entire symbol if we need to navigate |
| 2999 | // more quickly at the source level when parsing STABS |
| 3000 | if ( !N_INCL_indexes.empty() ) |
| 3001 | { |
| 3002 | symbol_ptr = symtab->SymbolAtIndex(N_INCL_indexes.back()); |
| 3003 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3004 | symbol_ptr->SetSizeIsSibling(true); |
| 3005 | N_INCL_indexes.pop_back(); |
| 3006 | } |
| 3007 | type = eSymbolTypeScopeEnd; |
| 3008 | break; |
| 3009 | |
| 3010 | case StabIncludeFileName: |
| 3011 | // N_SOL - #included file name: name,,n_sect,0,address |
| 3012 | type = eSymbolTypeHeaderFile; |
| 3013 | |
| 3014 | // We currently don't use the header files on darwin |
| 3015 | if (minimize) |
| 3016 | add_nlist = false; |
| 3017 | break; |
| 3018 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3019 | case StabCompilerParameters: |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3020 | // N_PARAMS - compiler parameters: name,,NO_SECT,0,0 |
| 3021 | type = eSymbolTypeCompiler; |
| 3022 | break; |
| 3023 | |
| 3024 | case StabCompilerVersion: |
| 3025 | // N_VERSION - compiler version: name,,NO_SECT,0,0 |
| 3026 | type = eSymbolTypeCompiler; |
| 3027 | break; |
| 3028 | |
| 3029 | case StabCompilerOptLevel: |
| 3030 | // N_OLEVEL - compiler -O level: name,,NO_SECT,0,0 |
| 3031 | type = eSymbolTypeCompiler; |
| 3032 | break; |
| 3033 | |
| 3034 | case StabParameter: |
| 3035 | // N_PSYM - parameter: name,,NO_SECT,type,offset |
| 3036 | type = eSymbolTypeVariable; |
| 3037 | break; |
| 3038 | |
| 3039 | case StabAlternateEntry: |
| 3040 | // N_ENTRY - alternate entry: name,,n_sect,linenumber,address |
| 3041 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 3042 | type = eSymbolTypeLineEntry; |
| 3043 | break; |
| 3044 | |
| 3045 | //---------------------------------------------------------------------- |
| 3046 | // Left and Right Braces |
| 3047 | //---------------------------------------------------------------------- |
| 3048 | case StabLeftBracket: |
| 3049 | // N_LBRAC - left bracket: 0,,NO_SECT,nesting level,address |
| 3050 | // We use the current number of symbols in the symbol table in lieu of |
| 3051 | // using nlist_idx in case we ever start trimming entries out |
| 3052 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 3053 | N_BRAC_indexes.push_back(sym_idx); |
| 3054 | type = eSymbolTypeScopeBegin; |
| 3055 | break; |
| 3056 | |
| 3057 | case StabRightBracket: |
| 3058 | // N_RBRAC - right bracket: 0,,NO_SECT,nesting level,address |
| 3059 | // Set the size of the N_LBRAC to the terminating index of this N_RBRAC |
| 3060 | // so that we can always skip the entire symbol if we need to navigate |
| 3061 | // more quickly at the source level when parsing STABS |
| 3062 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 3063 | if ( !N_BRAC_indexes.empty() ) |
| 3064 | { |
| 3065 | symbol_ptr = symtab->SymbolAtIndex(N_BRAC_indexes.back()); |
| 3066 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3067 | symbol_ptr->SetSizeIsSibling(true); |
| 3068 | N_BRAC_indexes.pop_back(); |
| 3069 | } |
| 3070 | type = eSymbolTypeScopeEnd; |
| 3071 | break; |
| 3072 | |
| 3073 | case StabDeletedIncludeFile: |
| 3074 | // N_EXCL - deleted include file: name,,NO_SECT,0,sum |
| 3075 | type = eSymbolTypeHeaderFile; |
| 3076 | break; |
| 3077 | |
| 3078 | //---------------------------------------------------------------------- |
| 3079 | // COMM scopes |
| 3080 | //---------------------------------------------------------------------- |
| 3081 | case StabBeginCommon: |
| 3082 | // N_BCOMM - begin common: name,,NO_SECT,0,0 |
| 3083 | // We use the current number of symbols in the symbol table in lieu of |
| 3084 | // using nlist_idx in case we ever start trimming entries out |
| 3085 | type = eSymbolTypeScopeBegin; |
| 3086 | N_COMM_indexes.push_back(sym_idx); |
| 3087 | break; |
| 3088 | |
| 3089 | case StabEndCommonLocal: |
| 3090 | // N_ECOML - end common (local name): 0,,n_sect,0,address |
| 3091 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 3092 | // Fall through |
| 3093 | |
| 3094 | case StabEndCommon: |
| 3095 | // N_ECOMM - end common: name,,n_sect,0,0 |
| 3096 | // Set the size of the N_BCOMM to the terminating index of this N_ECOMM/N_ECOML |
| 3097 | // so that we can always skip the entire symbol if we need to navigate |
| 3098 | // more quickly at the source level when parsing STABS |
| 3099 | if ( !N_COMM_indexes.empty() ) |
| 3100 | { |
| 3101 | symbol_ptr = symtab->SymbolAtIndex(N_COMM_indexes.back()); |
| 3102 | symbol_ptr->SetByteSize(sym_idx + 1); |
| 3103 | symbol_ptr->SetSizeIsSibling(true); |
| 3104 | N_COMM_indexes.pop_back(); |
| 3105 | } |
| 3106 | type = eSymbolTypeScopeEnd; |
| 3107 | break; |
| 3108 | |
| 3109 | case StabLength: |
| 3110 | // N_LENG - second stab entry with length information |
| 3111 | type = eSymbolTypeAdditional; |
| 3112 | break; |
| 3113 | |
| 3114 | default: break; |
| 3115 | } |
| 3116 | } |
| 3117 | else |
| 3118 | { |
| 3119 | //uint8_t n_pext = NlistMaskPrivateExternal & nlist.n_type; |
| 3120 | uint8_t n_type = NlistMaskType & nlist.n_type; |
| 3121 | sym[sym_idx].SetExternal((NlistMaskExternal & nlist.n_type) != 0); |
| 3122 | |
| 3123 | switch (n_type) |
| 3124 | { |
| 3125 | case NListTypeIndirect: // N_INDR - Fall through |
| 3126 | case NListTypePreboundUndefined:// N_PBUD - Fall through |
| 3127 | case NListTypeUndefined: // N_UNDF |
| 3128 | type = eSymbolTypeUndefined; |
| 3129 | break; |
| 3130 | |
| 3131 | case NListTypeAbsolute: // N_ABS |
| 3132 | type = eSymbolTypeAbsolute; |
| 3133 | break; |
| 3134 | |
| 3135 | case NListTypeSection: // N_SECT |
| 3136 | { |
| 3137 | symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value); |
| 3138 | |
Sean Callanan | b386d82 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 3139 | if (!symbol_section) |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3140 | { |
| 3141 | // TODO: warn about this? |
| 3142 | add_nlist = false; |
| 3143 | break; |
| 3144 | } |
| 3145 | |
| 3146 | if (TEXT_eh_frame_sectID == nlist.n_sect) |
| 3147 | { |
| 3148 | type = eSymbolTypeException; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3149 | } |
| 3150 | else |
| 3151 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3152 | uint32_t section_type = symbol_section->Get() & SectionFlagMaskSectionType; |
| 3153 | |
| 3154 | switch (section_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3155 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3156 | case SectionTypeRegular: break; // regular section |
| 3157 | //case SectionTypeZeroFill: type = eSymbolTypeData; break; // zero fill on demand section |
| 3158 | case SectionTypeCStringLiterals: type = eSymbolTypeData; break; // section with only literal C strings |
| 3159 | case SectionType4ByteLiterals: type = eSymbolTypeData; break; // section with only 4 byte literals |
| 3160 | case SectionType8ByteLiterals: type = eSymbolTypeData; break; // section with only 8 byte literals |
| 3161 | case SectionTypeLiteralPointers: type = eSymbolTypeTrampoline; break; // section with only pointers to literals |
| 3162 | case SectionTypeNonLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only non-lazy symbol pointers |
| 3163 | case SectionTypeLazySymbolPointers: type = eSymbolTypeTrampoline; break; // section with only lazy symbol pointers |
| 3164 | case SectionTypeSymbolStubs: type = eSymbolTypeTrampoline; break; // section with only symbol stubs, byte size of stub in the reserved2 field |
| 3165 | case SectionTypeModuleInitFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for initialization |
| 3166 | case SectionTypeModuleTermFunctionPointers: type = eSymbolTypeCode; break; // section with only function pointers for termination |
| 3167 | //case SectionTypeCoalesced: type = eSymbolType; break; // section contains symbols that are to be coalesced |
| 3168 | //case SectionTypeZeroFillLarge: type = eSymbolTypeData; break; // zero fill on demand section (that can be larger than 4 gigabytes) |
| 3169 | case SectionTypeInterposing: type = eSymbolTypeTrampoline; break; // section with only pairs of function pointers for interposing |
| 3170 | case SectionType16ByteLiterals: type = eSymbolTypeData; break; // section with only 16 byte literals |
| 3171 | case SectionTypeDTraceObjectFormat: type = eSymbolTypeInstrumentation; break; |
| 3172 | case SectionTypeLazyDylibSymbolPointers: type = eSymbolTypeTrampoline; break; |
| 3173 | default: break; |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 3174 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3175 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3176 | if (type == eSymbolTypeInvalid) |
Greg Clayton | 3f69eac | 2011-12-03 02:30:59 +0000 | [diff] [blame] | 3177 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3178 | const char *symbol_sect_name = symbol_section->GetName().AsCString(); |
| 3179 | if (symbol_section->IsDescendant (text_section_sp.get())) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 3180 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3181 | if (symbol_section->IsClear(SectionAttrUserPureInstructions | |
| 3182 | SectionAttrUserSelfModifyingCode | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3183 | SectionAttrSytemSomeInstructions)) |
| 3184 | type = eSymbolTypeData; |
| 3185 | else |
| 3186 | type = eSymbolTypeCode; |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 3187 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3188 | else |
| 3189 | if (symbol_section->IsDescendant(data_section_sp.get())) |
Greg Clayton | 576a68b | 2010-09-08 16:38:06 +0000 | [diff] [blame] | 3190 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3191 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__objc") == symbol_sect_name) |
Greg Clayton | 7c36fa0 | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 3192 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3193 | type = eSymbolTypeRuntime; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3194 | |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3195 | if (symbol_name && |
| 3196 | symbol_name[0] == '_' && |
| 3197 | symbol_name[1] == 'O' && |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3198 | symbol_name[2] == 'B') |
Greg Clayton | 637029b | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 3199 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3200 | llvm::StringRef symbol_name_ref(symbol_name); |
| 3201 | static const llvm::StringRef g_objc_v2_prefix_class ("_OBJC_CLASS_$_"); |
| 3202 | static const llvm::StringRef g_objc_v2_prefix_metaclass ("_OBJC_METACLASS_$_"); |
| 3203 | static const llvm::StringRef g_objc_v2_prefix_ivar ("_OBJC_IVAR_$_"); |
| 3204 | if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3205 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3206 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3207 | symbol_name = symbol_name + g_objc_v2_prefix_class.size(); |
| 3208 | type = eSymbolTypeObjCClass; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 3209 | demangled_is_synthesized = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3210 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3211 | else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3212 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3213 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3214 | symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size(); |
| 3215 | type = eSymbolTypeObjCMetaClass; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 3216 | demangled_is_synthesized = true; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3217 | } |
| 3218 | else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) |
| 3219 | { |
| 3220 | symbol_name_non_abi_mangled = symbol_name + 1; |
| 3221 | symbol_name = symbol_name + g_objc_v2_prefix_ivar.size(); |
| 3222 | type = eSymbolTypeObjCIVar; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 3223 | demangled_is_synthesized = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3227 | else |
| 3228 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__gcc_except_tab") == symbol_sect_name) |
| 3229 | { |
| 3230 | type = eSymbolTypeException; |
| 3231 | } |
| 3232 | else |
| 3233 | { |
| 3234 | type = eSymbolTypeData; |
| 3235 | } |
| 3236 | } |
| 3237 | else |
| 3238 | if (symbol_sect_name && ::strstr (symbol_sect_name, "__IMPORT") == symbol_sect_name) |
| 3239 | { |
| 3240 | type = eSymbolTypeTrampoline; |
| 3241 | } |
| 3242 | else |
| 3243 | if (symbol_section->IsDescendant(objc_section_sp.get())) |
| 3244 | { |
| 3245 | type = eSymbolTypeRuntime; |
| 3246 | if (symbol_name && symbol_name[0] == '.') |
| 3247 | { |
| 3248 | llvm::StringRef symbol_name_ref(symbol_name); |
| 3249 | static const llvm::StringRef g_objc_v1_prefix_class (".objc_class_name_"); |
| 3250 | if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) |
| 3251 | { |
| 3252 | symbol_name_non_abi_mangled = symbol_name; |
| 3253 | symbol_name = symbol_name + g_objc_v1_prefix_class.size(); |
| 3254 | type = eSymbolTypeObjCClass; |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 3255 | demangled_is_synthesized = true; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3256 | } |
| 3257 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | } |
| 3261 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3262 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3263 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | if (add_nlist) |
| 3267 | { |
| 3268 | uint64_t symbol_value = nlist.n_value; |
| 3269 | bool symbol_name_is_mangled = false; |
| 3270 | |
| 3271 | if (symbol_name_non_abi_mangled) |
| 3272 | { |
Greg Clayton | c024004 | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 3273 | sym[sym_idx].GetMangled().SetMangledName (ConstString(symbol_name_non_abi_mangled)); |
| 3274 | sym[sym_idx].GetMangled().SetDemangledName (ConstString(symbol_name)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3275 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3276 | else |
| 3277 | { |
| 3278 | if (symbol_name && symbol_name[0] == '_') |
| 3279 | { |
| 3280 | symbol_name_is_mangled = symbol_name[1] == '_'; |
| 3281 | symbol_name++; // Skip the leading underscore |
| 3282 | } |
| 3283 | |
| 3284 | if (symbol_name) |
| 3285 | { |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 3286 | ConstString const_symbol_name(symbol_name); |
| 3287 | if (is_gsym) |
| 3288 | N_GSYM_name_to_sym_idx[const_symbol_name.GetCString()] = sym_idx; |
| 3289 | sym[sym_idx].GetMangled().SetValue(const_symbol_name, symbol_name_is_mangled); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | if (symbol_section) |
| 3293 | { |
| 3294 | const addr_t section_file_addr = symbol_section->GetFileAddress(); |
| 3295 | if (symbol_byte_size == 0 && function_starts_count > 0) |
| 3296 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3297 | addr_t symbol_lookup_file_addr = nlist.n_value; |
| 3298 | // Do an exact address match for non-ARM addresses, else get the closest since |
| 3299 | // the symbol might be a thumb symbol which has an address with bit zero set |
| 3300 | FunctionStarts::Entry *func_start_entry = function_starts.FindEntry (symbol_lookup_file_addr, !is_arm); |
| 3301 | if (is_arm && func_start_entry) |
| 3302 | { |
| 3303 | // Verify that the function start address is the symbol address (ARM) |
| 3304 | // or the symbol address + 1 (thumb) |
| 3305 | if (func_start_entry->addr != symbol_lookup_file_addr && |
| 3306 | func_start_entry->addr != (symbol_lookup_file_addr + 1)) |
| 3307 | { |
| 3308 | // Not the right entry, NULL it out... |
| 3309 | func_start_entry = NULL; |
| 3310 | } |
| 3311 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3312 | if (func_start_entry) |
| 3313 | { |
| 3314 | func_start_entry->data = true; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3315 | |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3316 | addr_t symbol_file_addr = func_start_entry->addr; |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3317 | if (is_arm) |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3318 | symbol_file_addr &= 0xfffffffffffffffeull; |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3319 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3320 | const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); |
| 3321 | const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); |
| 3322 | if (next_func_start_entry) |
| 3323 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3324 | addr_t next_symbol_file_addr = next_func_start_entry->addr; |
| 3325 | // Be sure the clear the Thumb address bit when we calculate the size |
| 3326 | // from the current and next address |
| 3327 | if (is_arm) |
| 3328 | next_symbol_file_addr &= 0xfffffffffffffffeull; |
| 3329 | symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3330 | } |
| 3331 | else |
| 3332 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3333 | symbol_byte_size = section_end_file_addr - symbol_file_addr; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | symbol_value -= section_file_addr; |
| 3338 | } |
| 3339 | |
Greg Clayton | a8364e9 | 2013-05-14 22:19:37 +0000 | [diff] [blame] | 3340 | if (is_debug == false) |
| 3341 | { |
| 3342 | if (type == eSymbolTypeCode) |
| 3343 | { |
| 3344 | // See if we can find a N_FUN entry for any code symbols. |
| 3345 | // If we do find a match, and the name matches, then we |
| 3346 | // can merge the two into just the function symbol to avoid |
| 3347 | // duplicate entries in the symbol table |
| 3348 | ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value); |
| 3349 | if (pos != N_FUN_addr_to_sym_idx.end()) |
| 3350 | { |
| 3351 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 3352 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 3353 | { |
| 3354 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 3355 | // We just need the flags from the linker symbol, so put these flags |
| 3356 | // into the N_FUN flags to avoid duplicate symbols in the symbol table |
| 3357 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 3358 | sym[sym_idx].Clear(); |
| 3359 | continue; |
| 3360 | } |
| 3361 | } |
| 3362 | } |
| 3363 | else if (type == eSymbolTypeData) |
| 3364 | { |
| 3365 | // See if we can find a N_STSYM entry for any data symbols. |
| 3366 | // If we do find a match, and the name matches, then we |
| 3367 | // can merge the two into just the Static symbol to avoid |
| 3368 | // duplicate entries in the symbol table |
| 3369 | ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value); |
| 3370 | if (pos != N_STSYM_addr_to_sym_idx.end()) |
| 3371 | { |
| 3372 | if ((symbol_name_is_mangled == true && sym[sym_idx].GetMangled().GetMangledName() == sym[pos->second].GetMangled().GetMangledName()) || |
| 3373 | (symbol_name_is_mangled == false && sym[sym_idx].GetMangled().GetDemangledName() == sym[pos->second].GetMangled().GetDemangledName())) |
| 3374 | { |
| 3375 | m_nlist_idx_to_sym_idx[nlist_idx] = pos->second; |
| 3376 | // We just need the flags from the linker symbol, so put these flags |
| 3377 | // into the N_STSYM flags to avoid duplicate symbols in the symbol table |
| 3378 | sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 3379 | sym[sym_idx].Clear(); |
| 3380 | continue; |
| 3381 | } |
| 3382 | } |
| 3383 | else |
| 3384 | { |
| 3385 | // Combine N_GSYM stab entries with the non stab symbol |
| 3386 | ConstNameToSymbolIndexMap::const_iterator pos = N_GSYM_name_to_sym_idx.find(sym[sym_idx].GetMangled().GetMangledName().GetCString()); |
| 3387 | if (pos != N_GSYM_name_to_sym_idx.end()) |
| 3388 | { |
| 3389 | const uint32_t GSYM_sym_idx = pos->second; |
| 3390 | m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx; |
| 3391 | // Copy the address, because often the N_GSYM address has an invalid address of zero |
| 3392 | // when the global is a common symbol |
| 3393 | sym[GSYM_sym_idx].GetAddress().SetSection (symbol_section); |
| 3394 | sym[GSYM_sym_idx].GetAddress().SetOffset (symbol_value); |
| 3395 | // We just need the flags from the linker symbol, so put these flags |
| 3396 | // into the N_STSYM flags to avoid duplicate symbols in the symbol table |
| 3397 | sym[GSYM_sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 3398 | sym[sym_idx].Clear(); |
| 3399 | continue; |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3405 | sym[sym_idx].SetID (nlist_idx); |
| 3406 | sym[sym_idx].SetType (type); |
| 3407 | sym[sym_idx].GetAddress().SetSection (symbol_section); |
| 3408 | sym[sym_idx].GetAddress().SetOffset (symbol_value); |
| 3409 | sym[sym_idx].SetFlags (nlist.n_type << 16 | nlist.n_desc); |
| 3410 | |
| 3411 | if (symbol_byte_size > 0) |
| 3412 | sym[sym_idx].SetByteSize(symbol_byte_size); |
| 3413 | |
Greg Clayton | 01e6a58 | 2012-11-27 01:52:16 +0000 | [diff] [blame] | 3414 | if (demangled_is_synthesized) |
| 3415 | sym[sym_idx].SetDemangledNameIsSynthesized(true); |
| 3416 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3417 | ++sym_idx; |
| 3418 | } |
| 3419 | else |
| 3420 | { |
| 3421 | sym[sym_idx].Clear(); |
| 3422 | } |
| 3423 | |
| 3424 | } |
| 3425 | |
| 3426 | // STAB N_GSYM entries end up having a symbol type eSymbolTypeGlobal and when the symbol value |
| 3427 | // is zero, the address of the global ends up being in a non-STAB entry. Try and fix up all |
| 3428 | // such entries by figuring out what the address for the global is by looking up this non-STAB |
| 3429 | // entry and copying the value into the debug symbol's value to save us the hassle in the |
| 3430 | // debug symbol parser. |
| 3431 | |
| 3432 | Symbol *global_symbol = NULL; |
| 3433 | for (nlist_idx = 0; |
| 3434 | nlist_idx < symtab_load_command.nsyms && (global_symbol = symtab->FindSymbolWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, nlist_idx)) != NULL; |
| 3435 | nlist_idx++) |
| 3436 | { |
| 3437 | if (global_symbol->GetAddress().GetFileAddress() == 0) |
| 3438 | { |
| 3439 | std::vector<uint32_t> indexes; |
| 3440 | if (symtab->AppendSymbolIndexesWithName (global_symbol->GetMangled().GetName(), indexes) > 0) |
| 3441 | { |
| 3442 | std::vector<uint32_t>::const_iterator pos; |
| 3443 | std::vector<uint32_t>::const_iterator end = indexes.end(); |
| 3444 | for (pos = indexes.begin(); pos != end; ++pos) |
| 3445 | { |
| 3446 | symbol_ptr = symtab->SymbolAtIndex(*pos); |
| 3447 | if (symbol_ptr != global_symbol && symbol_ptr->IsDebug() == false) |
| 3448 | { |
| 3449 | global_symbol->GetAddress() = symbol_ptr->GetAddress(); |
| 3450 | break; |
| 3451 | } |
| 3452 | } |
| 3453 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3456 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3457 | uint32_t synthetic_sym_id = symtab_load_command.nsyms; |
| 3458 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3459 | if (function_starts_count > 0) |
| 3460 | { |
| 3461 | char synthetic_function_symbol[PATH_MAX]; |
| 3462 | uint32_t num_synthetic_function_symbols = 0; |
| 3463 | for (i=0; i<function_starts_count; ++i) |
| 3464 | { |
| 3465 | if (function_starts.GetEntryRef (i).data == false) |
| 3466 | ++num_synthetic_function_symbols; |
| 3467 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3468 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3469 | if (num_synthetic_function_symbols > 0) |
| 3470 | { |
| 3471 | if (num_syms < sym_idx + num_synthetic_function_symbols) |
| 3472 | { |
| 3473 | num_syms = sym_idx + num_synthetic_function_symbols; |
| 3474 | sym = symtab->Resize (num_syms); |
| 3475 | } |
| 3476 | uint32_t synthetic_function_symbol_idx = 0; |
| 3477 | for (i=0; i<function_starts_count; ++i) |
| 3478 | { |
| 3479 | const FunctionStarts::Entry *func_start_entry = function_starts.GetEntryAtIndex (i); |
| 3480 | if (func_start_entry->data == false) |
| 3481 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3482 | addr_t symbol_file_addr = func_start_entry->addr; |
| 3483 | uint32_t symbol_flags = 0; |
| 3484 | if (is_arm) |
| 3485 | { |
| 3486 | if (symbol_file_addr & 1) |
| 3487 | symbol_flags = MACHO_NLIST_ARM_SYMBOL_IS_THUMB; |
| 3488 | symbol_file_addr &= 0xfffffffffffffffeull; |
| 3489 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3490 | Address symbol_addr; |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3491 | if (module_sp->ResolveFileAddress (symbol_file_addr, symbol_addr)) |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3492 | { |
| 3493 | SectionSP symbol_section (symbol_addr.GetSection()); |
| 3494 | uint32_t symbol_byte_size = 0; |
| 3495 | if (symbol_section) |
| 3496 | { |
| 3497 | const addr_t section_file_addr = symbol_section->GetFileAddress(); |
| 3498 | const FunctionStarts::Entry *next_func_start_entry = function_starts.FindNextEntry (func_start_entry); |
| 3499 | const addr_t section_end_file_addr = section_file_addr + symbol_section->GetByteSize(); |
| 3500 | if (next_func_start_entry) |
| 3501 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3502 | addr_t next_symbol_file_addr = next_func_start_entry->addr; |
| 3503 | if (is_arm) |
| 3504 | next_symbol_file_addr &= 0xfffffffffffffffeull; |
| 3505 | symbol_byte_size = std::min<lldb::addr_t>(next_symbol_file_addr - symbol_file_addr, section_end_file_addr - symbol_file_addr); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3506 | } |
| 3507 | else |
| 3508 | { |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3509 | symbol_byte_size = section_end_file_addr - symbol_file_addr; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3510 | } |
| 3511 | snprintf (synthetic_function_symbol, |
| 3512 | sizeof(synthetic_function_symbol), |
| 3513 | "___lldb_unnamed_function%u$$%s", |
| 3514 | ++synthetic_function_symbol_idx, |
| 3515 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 3516 | sym[sym_idx].SetID (synthetic_sym_id++); |
Greg Clayton | c024004 | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 3517 | sym[sym_idx].GetMangled().SetDemangledName(ConstString(synthetic_function_symbol)); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3518 | sym[sym_idx].SetType (eSymbolTypeCode); |
| 3519 | sym[sym_idx].SetIsSynthetic (true); |
| 3520 | sym[sym_idx].GetAddress() = symbol_addr; |
Greg Clayton | d2653c2 | 2012-03-14 01:53:24 +0000 | [diff] [blame] | 3521 | if (symbol_flags) |
| 3522 | sym[sym_idx].SetFlags (symbol_flags); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3523 | if (symbol_byte_size) |
| 3524 | sym[sym_idx].SetByteSize (symbol_byte_size); |
| 3525 | ++sym_idx; |
| 3526 | } |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | // Trim our symbols down to just what we ended up with after |
| 3534 | // removing any symbols. |
| 3535 | if (sym_idx < num_syms) |
| 3536 | { |
| 3537 | num_syms = sym_idx; |
| 3538 | sym = symtab->Resize (num_syms); |
| 3539 | } |
| 3540 | |
| 3541 | // Now synthesize indirect symbols |
| 3542 | if (m_dysymtab.nindirectsyms != 0) |
| 3543 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3544 | if (indirect_symbol_index_data.GetByteSize()) |
| 3545 | { |
| 3546 | NListIndexToSymbolIndexMap::const_iterator end_index_pos = m_nlist_idx_to_sym_idx.end(); |
| 3547 | |
| 3548 | for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size(); ++sect_idx) |
| 3549 | { |
| 3550 | if ((m_mach_sections[sect_idx].flags & SectionFlagMaskSectionType) == SectionTypeSymbolStubs) |
| 3551 | { |
| 3552 | uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2; |
| 3553 | if (symbol_stub_byte_size == 0) |
| 3554 | continue; |
| 3555 | |
| 3556 | const uint32_t num_symbol_stubs = m_mach_sections[sect_idx].size / symbol_stub_byte_size; |
| 3557 | |
| 3558 | if (num_symbol_stubs == 0) |
| 3559 | continue; |
| 3560 | |
| 3561 | const uint32_t symbol_stub_index_offset = m_mach_sections[sect_idx].reserved1; |
| 3562 | for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) |
| 3563 | { |
| 3564 | const uint32_t symbol_stub_index = symbol_stub_index_offset + stub_idx; |
| 3565 | const lldb::addr_t symbol_stub_addr = m_mach_sections[sect_idx].addr + (stub_idx * symbol_stub_byte_size); |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3566 | lldb::offset_t symbol_stub_offset = symbol_stub_index * 4; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3567 | if (indirect_symbol_index_data.ValidOffsetForDataOfSize(symbol_stub_offset, 4)) |
| 3568 | { |
| 3569 | const uint32_t stub_sym_id = indirect_symbol_index_data.GetU32 (&symbol_stub_offset); |
| 3570 | if (stub_sym_id & (IndirectSymbolAbsolute | IndirectSymbolLocal)) |
| 3571 | continue; |
| 3572 | |
| 3573 | NListIndexToSymbolIndexMap::const_iterator index_pos = m_nlist_idx_to_sym_idx.find (stub_sym_id); |
| 3574 | Symbol *stub_symbol = NULL; |
| 3575 | if (index_pos != end_index_pos) |
| 3576 | { |
| 3577 | // We have a remapping from the original nlist index to |
| 3578 | // a current symbol index, so just look this up by index |
| 3579 | stub_symbol = symtab->SymbolAtIndex (index_pos->second); |
| 3580 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3581 | else |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3582 | { |
| 3583 | // We need to lookup a symbol using the original nlist |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3584 | // symbol index since this index is coming from the |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3585 | // S_SYMBOL_STUBS |
| 3586 | stub_symbol = symtab->FindSymbolByID (stub_sym_id); |
| 3587 | } |
| 3588 | |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3589 | if (stub_symbol) |
| 3590 | { |
| 3591 | Address so_addr(symbol_stub_addr, section_list); |
| 3592 | |
| 3593 | if (stub_symbol->GetType() == eSymbolTypeUndefined) |
| 3594 | { |
| 3595 | // Change the external symbol into a trampoline that makes sense |
| 3596 | // These symbols were N_UNDF N_EXT, and are useless to us, so we |
| 3597 | // can re-use them so we don't have to make up a synthetic symbol |
| 3598 | // for no good reason. |
| 3599 | stub_symbol->SetType (eSymbolTypeTrampoline); |
| 3600 | stub_symbol->SetExternal (false); |
| 3601 | stub_symbol->GetAddress() = so_addr; |
| 3602 | stub_symbol->SetByteSize (symbol_stub_byte_size); |
| 3603 | } |
| 3604 | else |
| 3605 | { |
| 3606 | // Make a synthetic symbol to describe the trampoline stub |
Jason Molenda | 2a76fbf | 2012-04-24 02:09:58 +0000 | [diff] [blame] | 3607 | Mangled stub_symbol_mangled_name(stub_symbol->GetMangled()); |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3608 | if (sym_idx >= num_syms) |
Jason Molenda | 2a76fbf | 2012-04-24 02:09:58 +0000 | [diff] [blame] | 3609 | { |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3610 | sym = symtab->Resize (++num_syms); |
Jason Molenda | 2a76fbf | 2012-04-24 02:09:58 +0000 | [diff] [blame] | 3611 | stub_symbol = NULL; // this pointer no longer valid |
| 3612 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3613 | sym[sym_idx].SetID (synthetic_sym_id++); |
Jason Molenda | 2a76fbf | 2012-04-24 02:09:58 +0000 | [diff] [blame] | 3614 | sym[sym_idx].GetMangled() = stub_symbol_mangled_name; |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3615 | sym[sym_idx].SetType (eSymbolTypeTrampoline); |
| 3616 | sym[sym_idx].SetIsSynthetic (true); |
| 3617 | sym[sym_idx].GetAddress() = so_addr; |
| 3618 | sym[sym_idx].SetByteSize (symbol_stub_byte_size); |
| 3619 | ++sym_idx; |
| 3620 | } |
| 3621 | } |
Greg Clayton | d4330e6 | 2012-09-05 01:38:55 +0000 | [diff] [blame] | 3622 | else |
| 3623 | { |
| 3624 | if (log) |
| 3625 | log->Warning ("symbol stub referencing symbol table symbol %u that isn't in our minimal symbol table, fix this!!!", stub_sym_id); |
| 3626 | } |
Greg Clayton | 4aa2edf | 2012-03-09 04:26:05 +0000 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | } |
| 3632 | } |
| 3633 | return symtab->GetNumSymbols(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3634 | } |
| 3635 | return 0; |
| 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | void |
| 3640 | ObjectFileMachO::Dump (Stream *s) |
| 3641 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3642 | ModuleSP module_sp(GetModule()); |
| 3643 | if (module_sp) |
| 3644 | { |
| 3645 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 3646 | s->Printf("%p: ", this); |
| 3647 | s->Indent(); |
| 3648 | if (m_header.magic == HeaderMagic64 || m_header.magic == HeaderMagic64Swapped) |
| 3649 | s->PutCString("ObjectFileMachO64"); |
| 3650 | else |
| 3651 | s->PutCString("ObjectFileMachO32"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3652 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3653 | ArchSpec header_arch(eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3654 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3655 | *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3656 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3657 | if (m_sections_ap.get()) |
| 3658 | m_sections_ap->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3659 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3660 | if (m_symtab_ap.get()) |
| 3661 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
| 3662 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 3665 | bool |
| 3666 | ObjectFileMachO::GetUUID (const llvm::MachO::mach_header &header, |
| 3667 | const lldb_private::DataExtractor &data, |
| 3668 | lldb::offset_t lc_offset, |
| 3669 | lldb_private::UUID& uuid) |
| 3670 | { |
| 3671 | uint32_t i; |
| 3672 | struct uuid_command load_cmd; |
| 3673 | |
| 3674 | lldb::offset_t offset = lc_offset; |
| 3675 | for (i=0; i<header.ncmds; ++i) |
| 3676 | { |
| 3677 | const lldb::offset_t cmd_offset = offset; |
| 3678 | if (data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 3679 | break; |
| 3680 | |
| 3681 | if (load_cmd.cmd == LoadCommandUUID) |
| 3682 | { |
| 3683 | const uint8_t *uuid_bytes = data.PeekData(offset, 16); |
| 3684 | |
| 3685 | if (uuid_bytes) |
| 3686 | { |
| 3687 | // OpenCL on Mac OS X uses the same UUID for each of its object files. |
| 3688 | // We pretend these object files have no UUID to prevent crashing. |
| 3689 | |
| 3690 | const uint8_t opencl_uuid[] = { 0x8c, 0x8e, 0xb3, 0x9b, |
| 3691 | 0x3b, 0xa8, |
| 3692 | 0x4b, 0x16, |
| 3693 | 0xb6, 0xa4, |
| 3694 | 0x27, 0x63, 0xbb, 0x14, 0xf0, 0x0d }; |
| 3695 | |
| 3696 | if (!memcmp(uuid_bytes, opencl_uuid, 16)) |
| 3697 | return false; |
| 3698 | |
| 3699 | uuid.SetBytes (uuid_bytes); |
| 3700 | return true; |
| 3701 | } |
| 3702 | return false; |
| 3703 | } |
| 3704 | offset = cmd_offset + load_cmd.cmdsize; |
| 3705 | } |
| 3706 | return false; |
| 3707 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3708 | |
| 3709 | bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 3710 | ObjectFileMachO::GetUUID (lldb_private::UUID* uuid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3711 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3712 | ModuleSP module_sp(GetModule()); |
| 3713 | if (module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3714 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3715 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3716 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Greg Clayton | 36b877d | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 3717 | return GetUUID (m_header, m_data, offset, *uuid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3718 | } |
| 3719 | return false; |
| 3720 | } |
| 3721 | |
| 3722 | |
| 3723 | uint32_t |
| 3724 | ObjectFileMachO::GetDependentModules (FileSpecList& files) |
| 3725 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3726 | uint32_t count = 0; |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3727 | ModuleSP module_sp(GetModule()); |
| 3728 | if (module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3729 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3730 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 3731 | struct load_command load_cmd; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3732 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3733 | const bool resolve_path = false; // Don't resolve the dependend file paths since they may not reside on this system |
| 3734 | uint32_t i; |
| 3735 | for (i=0; i<m_header.ncmds; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3736 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3737 | const uint32_t cmd_offset = offset; |
| 3738 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 3739 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3740 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3741 | switch (load_cmd.cmd) |
| 3742 | { |
| 3743 | case LoadCommandDylibLoad: |
| 3744 | case LoadCommandDylibLoadWeak: |
| 3745 | case LoadCommandDylibReexport: |
| 3746 | case LoadCommandDynamicLinkerLoad: |
| 3747 | case LoadCommandFixedVMShlibLoad: |
| 3748 | case LoadCommandDylibLoadUpward: |
| 3749 | { |
| 3750 | uint32_t name_offset = cmd_offset + m_data.GetU32(&offset); |
| 3751 | const char *path = m_data.PeekCStr(name_offset); |
| 3752 | // Skip any path that starts with '@' since these are usually: |
| 3753 | // @executable_path/.../file |
| 3754 | // @rpath/.../file |
| 3755 | if (path && path[0] != '@') |
| 3756 | { |
| 3757 | FileSpec file_spec(path, resolve_path); |
| 3758 | if (files.AppendIfUnique(file_spec)) |
| 3759 | count++; |
| 3760 | } |
| 3761 | } |
| 3762 | break; |
| 3763 | |
| 3764 | default: |
| 3765 | break; |
| 3766 | } |
| 3767 | offset = cmd_offset + load_cmd.cmdsize; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3768 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3769 | } |
| 3770 | return count; |
| 3771 | } |
| 3772 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3773 | lldb_private::Address |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3774 | ObjectFileMachO::GetEntryPointAddress () |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3775 | { |
| 3776 | // If the object file is not an executable it can't hold the entry point. m_entry_point_address |
| 3777 | // is initialized to an invalid address, so we can just return that. |
| 3778 | // If m_entry_point_address is valid it means we've found it already, so return the cached value. |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3779 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3780 | if (!IsExecutable() || m_entry_point_address.IsValid()) |
| 3781 | return m_entry_point_address; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3782 | |
| 3783 | // Otherwise, look for the UnixThread or Thread command. The data for the Thread command is given in |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3784 | // /usr/include/mach-o.h, but it is basically: |
| 3785 | // |
| 3786 | // uint32_t flavor - this is the flavor argument you would pass to thread_get_state |
| 3787 | // uint32_t count - this is the count of longs in the thread state data |
| 3788 | // struct XXX_thread_state state - this is the structure from <machine/thread_status.h> corresponding to the flavor. |
| 3789 | // <repeat this trio> |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3790 | // |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3791 | // So we just keep reading the various register flavors till we find the GPR one, then read the PC out of there. |
| 3792 | // FIXME: We will need to have a "RegisterContext data provider" class at some point that can get all the registers |
| 3793 | // out of data in this form & attach them to a given thread. That should underlie the MacOS X User process plugin, |
| 3794 | // and we'll also need it for the MacOS X Core File process plugin. When we have that we can also use it here. |
| 3795 | // |
| 3796 | // For now we hard-code the offsets and flavors we need: |
| 3797 | // |
| 3798 | // |
| 3799 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3800 | ModuleSP module_sp(GetModule()); |
| 3801 | if (module_sp) |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3802 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3803 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 3804 | struct load_command load_cmd; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3805 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3806 | uint32_t i; |
| 3807 | lldb::addr_t start_address = LLDB_INVALID_ADDRESS; |
| 3808 | bool done = false; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3809 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3810 | for (i=0; i<m_header.ncmds; ++i) |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3811 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3812 | const lldb::offset_t cmd_offset = offset; |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3813 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 3814 | break; |
| 3815 | |
| 3816 | switch (load_cmd.cmd) |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3817 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3818 | case LoadCommandUnixThread: |
| 3819 | case LoadCommandThread: |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3820 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3821 | while (offset < cmd_offset + load_cmd.cmdsize) |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3822 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3823 | uint32_t flavor = m_data.GetU32(&offset); |
| 3824 | uint32_t count = m_data.GetU32(&offset); |
| 3825 | if (count == 0) |
| 3826 | { |
| 3827 | // We've gotten off somehow, log and exit; |
| 3828 | return m_entry_point_address; |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3829 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3830 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3831 | switch (m_header.cputype) |
| 3832 | { |
| 3833 | case llvm::MachO::CPUTypeARM: |
| 3834 | if (flavor == 1) // ARM_THREAD_STATE from mach/arm/thread_status.h |
| 3835 | { |
| 3836 | offset += 60; // This is the offset of pc in the GPR thread state data structure. |
| 3837 | start_address = m_data.GetU32(&offset); |
| 3838 | done = true; |
| 3839 | } |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3840 | break; |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3841 | case llvm::MachO::CPUTypeI386: |
| 3842 | if (flavor == 1) // x86_THREAD_STATE32 from mach/i386/thread_status.h |
| 3843 | { |
| 3844 | offset += 40; // This is the offset of eip in the GPR thread state data structure. |
| 3845 | start_address = m_data.GetU32(&offset); |
| 3846 | done = true; |
| 3847 | } |
| 3848 | break; |
| 3849 | case llvm::MachO::CPUTypeX86_64: |
| 3850 | if (flavor == 4) // x86_THREAD_STATE64 from mach/i386/thread_status.h |
| 3851 | { |
| 3852 | offset += 16 * 8; // This is the offset of rip in the GPR thread state data structure. |
| 3853 | start_address = m_data.GetU64(&offset); |
| 3854 | done = true; |
| 3855 | } |
| 3856 | break; |
| 3857 | default: |
| 3858 | return m_entry_point_address; |
| 3859 | } |
| 3860 | // Haven't found the GPR flavor yet, skip over the data for this flavor: |
| 3861 | if (done) |
| 3862 | break; |
| 3863 | offset += count * 4; |
| 3864 | } |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3865 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3866 | break; |
| 3867 | case LoadCommandMain: |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 3868 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3869 | ConstString text_segment_name ("__TEXT"); |
| 3870 | uint64_t entryoffset = m_data.GetU64(&offset); |
| 3871 | SectionSP text_segment_sp = GetSectionList()->FindSectionByName(text_segment_name); |
| 3872 | if (text_segment_sp) |
| 3873 | { |
| 3874 | done = true; |
| 3875 | start_address = text_segment_sp->GetFileAddress() + entryoffset; |
| 3876 | } |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 3877 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3878 | |
| 3879 | default: |
| 3880 | break; |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 3881 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3882 | if (done) |
| 3883 | break; |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3884 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3885 | // Go to the next load command: |
| 3886 | offset = cmd_offset + load_cmd.cmdsize; |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3887 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3888 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3889 | if (start_address != LLDB_INVALID_ADDRESS) |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 3890 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3891 | // We got the start address from the load commands, so now resolve that address in the sections |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3892 | // of this ObjectFile: |
| 3893 | if (!m_entry_point_address.ResolveAddressUsingFileSections (start_address, GetSectionList())) |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 3894 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3895 | m_entry_point_address.Clear(); |
| 3896 | } |
| 3897 | } |
| 3898 | else |
| 3899 | { |
| 3900 | // We couldn't read the UnixThread load command - maybe it wasn't there. As a fallback look for the |
| 3901 | // "start" symbol in the main executable. |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3902 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3903 | ModuleSP module_sp (GetModule()); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3904 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3905 | if (module_sp) |
| 3906 | { |
| 3907 | SymbolContextList contexts; |
| 3908 | SymbolContext context; |
| 3909 | if (module_sp->FindSymbolsWithNameAndType(ConstString ("start"), eSymbolTypeCode, contexts)) |
| 3910 | { |
| 3911 | if (contexts.GetContextAtIndex(0, context)) |
| 3912 | m_entry_point_address = context.symbol->GetAddress(); |
| 3913 | } |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 3914 | } |
| 3915 | } |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3916 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3917 | |
Jim Ingham | 2877594 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 3918 | return m_entry_point_address; |
| 3919 | |
| 3920 | } |
| 3921 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 3922 | lldb_private::Address |
| 3923 | ObjectFileMachO::GetHeaderAddress () |
| 3924 | { |
| 3925 | lldb_private::Address header_addr; |
| 3926 | SectionList *section_list = GetSectionList(); |
| 3927 | if (section_list) |
| 3928 | { |
| 3929 | SectionSP text_segment_sp (section_list->FindSectionByName (GetSegmentNameTEXT())); |
| 3930 | if (text_segment_sp) |
| 3931 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 3932 | header_addr.SetSection (text_segment_sp); |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 3933 | header_addr.SetOffset (0); |
| 3934 | } |
| 3935 | } |
| 3936 | return header_addr; |
| 3937 | } |
| 3938 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3939 | uint32_t |
| 3940 | ObjectFileMachO::GetNumThreadContexts () |
| 3941 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3942 | ModuleSP module_sp(GetModule()); |
| 3943 | if (module_sp) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3944 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3945 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 3946 | if (!m_thread_context_offsets_valid) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3947 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3948 | m_thread_context_offsets_valid = true; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 3949 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3950 | FileRangeArray::Entry file_range; |
| 3951 | thread_command thread_cmd; |
| 3952 | for (uint32_t i=0; i<m_header.ncmds; ++i) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3953 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3954 | const uint32_t cmd_offset = offset; |
| 3955 | if (m_data.GetU32(&offset, &thread_cmd, 2) == NULL) |
| 3956 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3957 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3958 | if (thread_cmd.cmd == LoadCommandThread) |
| 3959 | { |
| 3960 | file_range.SetRangeBase (offset); |
| 3961 | file_range.SetByteSize (thread_cmd.cmdsize - 8); |
| 3962 | m_thread_context_offsets.Append (file_range); |
| 3963 | } |
| 3964 | offset = cmd_offset + thread_cmd.cmdsize; |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3965 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | return m_thread_context_offsets.GetSize(); |
| 3969 | } |
| 3970 | |
| 3971 | lldb::RegisterContextSP |
| 3972 | ObjectFileMachO::GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread) |
| 3973 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3974 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 3975 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3976 | ModuleSP module_sp(GetModule()); |
| 3977 | if (module_sp) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 3978 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3979 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 3980 | if (!m_thread_context_offsets_valid) |
| 3981 | GetNumThreadContexts (); |
| 3982 | |
| 3983 | const FileRangeArray::Entry *thread_context_file_range = m_thread_context_offsets.GetEntryAtIndex (idx); |
Jim Ingham | 6f01c93 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 3984 | if (thread_context_file_range) |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 3985 | { |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3986 | |
| 3987 | DataExtractor data (m_data, |
| 3988 | thread_context_file_range->GetRangeBase(), |
Jim Ingham | 6f01c93 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 3989 | thread_context_file_range->GetByteSize()); |
| 3990 | |
| 3991 | switch (m_header.cputype) |
| 3992 | { |
| 3993 | case llvm::MachO::CPUTypeARM: |
| 3994 | reg_ctx_sp.reset (new RegisterContextDarwin_arm_Mach (thread, data)); |
| 3995 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 3996 | |
Jim Ingham | 6f01c93 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 3997 | case llvm::MachO::CPUTypeI386: |
| 3998 | reg_ctx_sp.reset (new RegisterContextDarwin_i386_Mach (thread, data)); |
| 3999 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4000 | |
Jim Ingham | 6f01c93 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 4001 | case llvm::MachO::CPUTypeX86_64: |
| 4002 | reg_ctx_sp.reset (new RegisterContextDarwin_x86_64_Mach (thread, data)); |
| 4003 | break; |
| 4004 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4005 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 4006 | } |
| 4007 | return reg_ctx_sp; |
| 4008 | } |
| 4009 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 4010 | |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4011 | ObjectFile::Type |
| 4012 | ObjectFileMachO::CalculateType() |
| 4013 | { |
| 4014 | switch (m_header.filetype) |
| 4015 | { |
| 4016 | case HeaderFileTypeObject: // 0x1u MH_OBJECT |
| 4017 | if (GetAddressByteSize () == 4) |
| 4018 | { |
| 4019 | // 32 bit kexts are just object files, but they do have a valid |
| 4020 | // UUID load command. |
| 4021 | UUID uuid; |
| 4022 | if (GetUUID(&uuid)) |
| 4023 | { |
| 4024 | // this checking for the UUID load command is not enough |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4025 | // we could eventually look for the symbol named |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4026 | // "OSKextGetCurrentIdentifier" as this is required of kexts |
| 4027 | if (m_strata == eStrataInvalid) |
| 4028 | m_strata = eStrataKernel; |
| 4029 | return eTypeSharedLibrary; |
| 4030 | } |
| 4031 | } |
| 4032 | return eTypeObjectFile; |
| 4033 | |
| 4034 | case HeaderFileTypeExecutable: return eTypeExecutable; // 0x2u MH_EXECUTE |
| 4035 | case HeaderFileTypeFixedVMShlib: return eTypeSharedLibrary; // 0x3u MH_FVMLIB |
| 4036 | case HeaderFileTypeCore: return eTypeCoreFile; // 0x4u MH_CORE |
| 4037 | case HeaderFileTypePreloadedExecutable: return eTypeSharedLibrary; // 0x5u MH_PRELOAD |
| 4038 | case HeaderFileTypeDynamicShlib: return eTypeSharedLibrary; // 0x6u MH_DYLIB |
| 4039 | case HeaderFileTypeDynamicLinkEditor: return eTypeDynamicLinker; // 0x7u MH_DYLINKER |
| 4040 | case HeaderFileTypeBundle: return eTypeSharedLibrary; // 0x8u MH_BUNDLE |
| 4041 | case HeaderFileTypeDynamicShlibStub: return eTypeStubLibrary; // 0x9u MH_DYLIB_STUB |
| 4042 | case HeaderFileTypeDSYM: return eTypeDebugInfo; // 0xAu MH_DSYM |
| 4043 | case HeaderFileTypeKextBundle: return eTypeSharedLibrary; // 0xBu MH_KEXT_BUNDLE |
| 4044 | default: |
| 4045 | break; |
| 4046 | } |
| 4047 | return eTypeUnknown; |
| 4048 | } |
| 4049 | |
| 4050 | ObjectFile::Strata |
| 4051 | ObjectFileMachO::CalculateStrata() |
| 4052 | { |
| 4053 | switch (m_header.filetype) |
| 4054 | { |
| 4055 | case HeaderFileTypeObject: // 0x1u MH_OBJECT |
| 4056 | { |
| 4057 | // 32 bit kexts are just object files, but they do have a valid |
| 4058 | // UUID load command. |
| 4059 | UUID uuid; |
| 4060 | if (GetUUID(&uuid)) |
| 4061 | { |
| 4062 | // this checking for the UUID load command is not enough |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4063 | // we could eventually look for the symbol named |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4064 | // "OSKextGetCurrentIdentifier" as this is required of kexts |
| 4065 | if (m_type == eTypeInvalid) |
| 4066 | m_type = eTypeSharedLibrary; |
| 4067 | |
| 4068 | return eStrataKernel; |
| 4069 | } |
| 4070 | } |
| 4071 | return eStrataUnknown; |
| 4072 | |
| 4073 | case HeaderFileTypeExecutable: // 0x2u MH_EXECUTE |
| 4074 | // Check for the MH_DYLDLINK bit in the flags |
| 4075 | if (m_header.flags & HeaderFlagBitIsDynamicLinkObject) |
Sean Callanan | ac725af | 2012-02-10 20:22:35 +0000 | [diff] [blame] | 4076 | { |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4077 | return eStrataUser; |
Sean Callanan | ac725af | 2012-02-10 20:22:35 +0000 | [diff] [blame] | 4078 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4079 | else |
Sean Callanan | ac725af | 2012-02-10 20:22:35 +0000 | [diff] [blame] | 4080 | { |
| 4081 | SectionList *section_list = GetSectionList(); |
| 4082 | if (section_list) |
| 4083 | { |
| 4084 | static ConstString g_kld_section_name ("__KLD"); |
| 4085 | if (section_list->FindSectionByName(g_kld_section_name)) |
| 4086 | return eStrataKernel; |
| 4087 | } |
| 4088 | } |
| 4089 | return eStrataRawImage; |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4090 | |
| 4091 | case HeaderFileTypeFixedVMShlib: return eStrataUser; // 0x3u MH_FVMLIB |
| 4092 | case HeaderFileTypeCore: return eStrataUnknown; // 0x4u MH_CORE |
Sean Callanan | ac725af | 2012-02-10 20:22:35 +0000 | [diff] [blame] | 4093 | case HeaderFileTypePreloadedExecutable: return eStrataRawImage; // 0x5u MH_PRELOAD |
Greg Clayton | ca31997 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 4094 | case HeaderFileTypeDynamicShlib: return eStrataUser; // 0x6u MH_DYLIB |
| 4095 | case HeaderFileTypeDynamicLinkEditor: return eStrataUser; // 0x7u MH_DYLINKER |
| 4096 | case HeaderFileTypeBundle: return eStrataUser; // 0x8u MH_BUNDLE |
| 4097 | case HeaderFileTypeDynamicShlibStub: return eStrataUser; // 0x9u MH_DYLIB_STUB |
| 4098 | case HeaderFileTypeDSYM: return eStrataUnknown; // 0xAu MH_DSYM |
| 4099 | case HeaderFileTypeKextBundle: return eStrataKernel; // 0xBu MH_KEXT_BUNDLE |
| 4100 | default: |
| 4101 | break; |
| 4102 | } |
| 4103 | return eStrataUnknown; |
| 4104 | } |
| 4105 | |
| 4106 | |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4107 | uint32_t |
| 4108 | ObjectFileMachO::GetVersion (uint32_t *versions, uint32_t num_versions) |
| 4109 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4110 | ModuleSP module_sp(GetModule()); |
| 4111 | if (module_sp) |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4112 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4113 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 4114 | struct dylib_command load_cmd; |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 4115 | lldb::offset_t offset = MachHeaderSizeFromMagic(m_header.magic); |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4116 | uint32_t version_cmd = 0; |
| 4117 | uint64_t version = 0; |
| 4118 | uint32_t i; |
| 4119 | for (i=0; i<m_header.ncmds; ++i) |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4120 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 4121 | const lldb::offset_t cmd_offset = offset; |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4122 | if (m_data.GetU32(&offset, &load_cmd, 2) == NULL) |
| 4123 | break; |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4124 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4125 | if (load_cmd.cmd == LoadCommandDylibIdent) |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4126 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4127 | if (version_cmd == 0) |
| 4128 | { |
| 4129 | version_cmd = load_cmd.cmd; |
| 4130 | if (m_data.GetU32(&offset, &load_cmd.dylib, 4) == NULL) |
| 4131 | break; |
| 4132 | version = load_cmd.dylib.current_version; |
| 4133 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4134 | break; // Break for now unless there is another more complete version |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4135 | // number load command in the future. |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4136 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4137 | offset = cmd_offset + load_cmd.cmdsize; |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4138 | } |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4139 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4140 | if (version_cmd == LoadCommandDylibIdent) |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4141 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4142 | if (versions != NULL && num_versions > 0) |
| 4143 | { |
| 4144 | if (num_versions > 0) |
| 4145 | versions[0] = (version & 0xFFFF0000ull) >> 16; |
| 4146 | if (num_versions > 1) |
| 4147 | versions[1] = (version & 0x0000FF00ull) >> 8; |
| 4148 | if (num_versions > 2) |
| 4149 | versions[2] = (version & 0x000000FFull); |
| 4150 | // Fill in an remaining version numbers with invalid values |
| 4151 | for (i=3; i<num_versions; ++i) |
| 4152 | versions[i] = UINT32_MAX; |
| 4153 | } |
| 4154 | // The LC_ID_DYLIB load command has a version with 3 version numbers |
| 4155 | // in it, so always return 3 |
| 4156 | return 3; |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4157 | } |
Greg Clayton | 49f4bf2 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 4158 | } |
| 4159 | return false; |
| 4160 | } |
| 4161 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4162 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 4163 | ObjectFileMachO::GetArchitecture (ArchSpec &arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4164 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4165 | ModuleSP module_sp(GetModule()); |
| 4166 | if (module_sp) |
Greg Clayton | 6a64bbf | 2011-09-21 03:57:31 +0000 | [diff] [blame] | 4167 | { |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4168 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
| 4169 | arch.SetArchitecture (eArchTypeMachO, m_header.cputype, m_header.cpusubtype); |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4170 | |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4171 | // Files with type MH_PRELOAD are currently used in cases where the image |
Jason Molenda | 9badb6c | 2013-03-06 23:19:17 +0000 | [diff] [blame] | 4172 | // debugs at the addresses in the file itself. Below we set the OS to |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4173 | // unknown to make sure we use the DynamicLoaderStatic()... |
| 4174 | if (m_header.filetype == HeaderFileTypePreloadedExecutable) |
| 4175 | { |
| 4176 | arch.GetTriple().setOS (llvm::Triple::UnknownOS); |
| 4177 | } |
| 4178 | return true; |
Greg Clayton | 6a64bbf | 2011-09-21 03:57:31 +0000 | [diff] [blame] | 4179 | } |
Greg Clayton | 9482f05 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 4180 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 4184 | UUID |
| 4185 | ObjectFileMachO::GetProcessSharedCacheUUID (Process *process) |
| 4186 | { |
| 4187 | UUID uuid; |
| 4188 | if (process) |
| 4189 | { |
| 4190 | addr_t all_image_infos = process->GetImageInfoAddress(); |
| 4191 | |
| 4192 | // The address returned by GetImageInfoAddress may be the address of dyld (don't want) |
| 4193 | // or it may be the address of the dyld_all_image_infos structure (want). The first four |
| 4194 | // bytes will be either the version field (all_image_infos) or a Mach-O file magic constant. |
| 4195 | // Version 13 and higher of dyld_all_image_infos is required to get the sharedCacheUUID field. |
| 4196 | |
| 4197 | Error err; |
| 4198 | uint32_t version_or_magic = process->ReadUnsignedIntegerFromMemory (all_image_infos, 4, -1, err); |
| 4199 | if (version_or_magic != -1 |
| 4200 | && version_or_magic != HeaderMagic32 |
| 4201 | && version_or_magic != HeaderMagic32Swapped |
| 4202 | && version_or_magic != HeaderMagic64 |
| 4203 | && version_or_magic != HeaderMagic64Swapped |
| 4204 | && version_or_magic >= 13) |
| 4205 | { |
| 4206 | addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS; |
| 4207 | int wordsize = process->GetAddressByteSize(); |
| 4208 | if (wordsize == 8) |
| 4209 | { |
| 4210 | sharedCacheUUID_address = all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h> |
| 4211 | } |
| 4212 | if (wordsize == 4) |
| 4213 | { |
| 4214 | sharedCacheUUID_address = all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h> |
| 4215 | } |
| 4216 | if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) |
| 4217 | { |
| 4218 | uuid_t shared_cache_uuid; |
| 4219 | if (process->ReadMemory (sharedCacheUUID_address, shared_cache_uuid, sizeof (uuid_t), err) == sizeof (uuid_t)) |
| 4220 | { |
| 4221 | uuid.SetBytes (shared_cache_uuid); |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | } |
| 4226 | return uuid; |
| 4227 | } |
| 4228 | |
| 4229 | UUID |
| 4230 | ObjectFileMachO::GetLLDBSharedCacheUUID () |
| 4231 | { |
| 4232 | UUID uuid; |
| 4233 | #if defined (__APPLE__) && defined (__arm__) |
| 4234 | uint8_t *(*dyld_get_all_image_infos)(void); |
| 4235 | dyld_get_all_image_infos = (uint8_t*(*)()) dlsym (RTLD_DEFAULT, "_dyld_get_all_image_infos"); |
| 4236 | if (dyld_get_all_image_infos) |
| 4237 | { |
| 4238 | uint8_t *dyld_all_image_infos_address = dyld_get_all_image_infos(); |
| 4239 | if (dyld_all_image_infos_address) |
| 4240 | { |
Jason Molenda | 6ff916f | 2013-04-16 21:42:58 +0000 | [diff] [blame] | 4241 | uint32_t *version = (uint32_t*) dyld_all_image_infos_address; // version <mach-o/dyld_images.h> |
| 4242 | if (*version >= 13) |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 4243 | { |
Jason Molenda | 2ceae99 | 2013-04-16 22:56:17 +0000 | [diff] [blame] | 4244 | uuid_t *sharedCacheUUID_address = (uuid_t*) ((uint8_t*) dyld_all_image_infos_address + 84); // sharedCacheUUID <mach-o/dyld_images.h> |
Jason Molenda | 45c7550 | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 4245 | uuid.SetBytes (sharedCacheUUID_address); |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | #endif |
| 4250 | return uuid; |
| 4251 | } |
| 4252 | |
| 4253 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4254 | //------------------------------------------------------------------ |
| 4255 | // PluginInterface protocol |
| 4256 | //------------------------------------------------------------------ |
Greg Clayton | 0e19160 | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 4257 | lldb_private::ConstString |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4258 | ObjectFileMachO::GetPluginName() |
| 4259 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 4260 | return GetPluginNameStatic(); |
| 4261 | } |
| 4262 | |
| 4263 | uint32_t |
| 4264 | ObjectFileMachO::GetPluginVersion() |
| 4265 | { |
| 4266 | return 1; |
| 4267 | } |
| 4268 | |