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