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