Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- Symbols.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 | |
| 10 | #include "lldb/Host/Symbols.h" |
| 11 | |
| 12 | // C Includes |
| 13 | #include <dirent.h> |
| 14 | #include <mach-o/loader.h> |
| 15 | #include <mach-o/fat.h> |
| 16 | |
| 17 | // C++ Includes |
| 18 | // Other libraries and framework includes |
| 19 | #include <CoreFoundation/CoreFoundation.h> |
| 20 | |
| 21 | // Project includes |
| 22 | #include "CFCReleaser.h" |
| 23 | #include "lldb/Core/ArchSpec.h" |
| 24 | #include "lldb/Core/DataBuffer.h" |
| 25 | #include "lldb/Core/DataExtractor.h" |
| 26 | #include "lldb/Core/Timer.h" |
| 27 | #include "lldb/Core/UUID.h" |
| 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | extern "C" { |
| 33 | CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url); |
| 34 | CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url); |
| 35 | }; |
| 36 | |
| 37 | static bool |
| 38 | SkinnyMachOFileContainsArchAndUUID |
| 39 | ( |
| 40 | const FileSpec &file_spec, |
| 41 | const ArchSpec *arch, |
| 42 | const UUID *uuid, // the UUID we are looking for |
| 43 | off_t file_offset, |
| 44 | DataExtractor& data, |
| 45 | uint32_t data_offset, |
| 46 | const uint32_t magic |
| 47 | ) |
| 48 | { |
| 49 | assert(magic == MH_CIGAM || magic == MH_MAGIC || magic == MH_CIGAM_64 || magic == MH_MAGIC_64); |
| 50 | if (magic == MH_MAGIC || magic == MH_MAGIC_64) |
| 51 | data.SetByteOrder (eByteOrderHost); |
| 52 | else if (eByteOrderHost == eByteOrderBig) |
| 53 | data.SetByteOrder (eByteOrderLittle); |
| 54 | else |
| 55 | data.SetByteOrder (eByteOrderBig); |
| 56 | |
| 57 | uint32_t i; |
| 58 | const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier |
| 59 | const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier |
| 60 | data_offset+=4; // Skip mach file type |
| 61 | const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands |
| 62 | const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands |
| 63 | data_offset+=4; // Skip flags |
| 64 | |
| 65 | // Check the architecture if we have a valid arch pointer |
| 66 | if (arch) |
| 67 | { |
| 68 | ArchSpec file_arch(cputype, cpusubtype); |
| 69 | |
| 70 | if (file_arch != *arch) |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // The file exists, and if a valid arch pointer was passed in we know |
| 75 | // if already matches, so we can return if we aren't looking for a specific |
| 76 | // UUID |
| 77 | if (uuid == NULL) |
| 78 | return true; |
| 79 | |
| 80 | if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) |
| 81 | data_offset += 4; // Skip reserved field for in mach_header_64 |
| 82 | |
| 83 | // Make sure we have enough data for all the load commands |
| 84 | if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) |
| 85 | { |
| 86 | if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds) |
| 87 | { |
| 88 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds)); |
| 89 | data.SetData (data_buffer_sp); |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds) |
| 95 | { |
| 96 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds)); |
| 97 | data.SetData (data_buffer_sp); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | for (i=0; i<ncmds; i++) |
| 102 | { |
| 103 | const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry! |
| 104 | uint32_t cmd = data.GetU32(&data_offset); |
| 105 | uint32_t cmd_size = data.GetU32(&data_offset); |
| 106 | if (cmd == LC_UUID) |
| 107 | { |
| 108 | UUID file_uuid (data.GetData(&data_offset, 16), 16); |
| 109 | return file_uuid == *uuid; |
| 110 | } |
| 111 | data_offset = cmd_offset + cmd_size; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | UniversalMachOFileContainsArchAndUUID |
| 118 | ( |
| 119 | const FileSpec &file_spec, |
| 120 | const ArchSpec *arch, |
| 121 | const UUID *uuid, |
| 122 | off_t file_offset, |
| 123 | DataExtractor& data, |
| 124 | uint32_t data_offset, |
| 125 | const uint32_t magic |
| 126 | ) |
| 127 | { |
| 128 | assert(magic == FAT_CIGAM || magic == FAT_MAGIC); |
| 129 | |
| 130 | // Universal mach-o files always have their headers encoded as BIG endian |
| 131 | data.SetByteOrder(eByteOrderBig); |
| 132 | |
| 133 | uint32_t i; |
| 134 | const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow |
| 135 | const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch); |
| 136 | if (data.GetByteSize() < fat_header_and_arch_size) |
| 137 | { |
| 138 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size)); |
| 139 | data.SetData (data_buffer_sp); |
| 140 | } |
| 141 | |
| 142 | for (i=0; i<nfat_arch; i++) |
| 143 | { |
| 144 | cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int) |
| 145 | cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int) |
| 146 | uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file |
| 147 | // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file |
| 148 | // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2 |
| 149 | data_offset += 8; // Skip size and align as we don't need those |
| 150 | // Only process this slice if the cpu type/subtype matches |
| 151 | if (arch) |
| 152 | { |
| 153 | ArchSpec fat_arch(arch_cputype, arch_cpusubtype); |
| 154 | if (fat_arch != *arch) |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | // Create a buffer with only the arch slice date in it |
| 159 | DataExtractor arch_data; |
| 160 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000)); |
| 161 | arch_data.SetData(data_buffer_sp); |
| 162 | uint32_t arch_data_offset = 0; |
| 163 | uint32_t arch_magic = arch_data.GetU32(&arch_data_offset); |
| 164 | |
| 165 | switch (arch_magic) |
| 166 | { |
| 167 | case MH_CIGAM: // 32 bit mach-o file |
| 168 | case MH_MAGIC: // 32 bit mach-o file |
| 169 | case MH_CIGAM_64: // 64 bit mach-o file |
| 170 | case MH_MAGIC_64: // 64 bit mach-o file |
| 171 | if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic)) |
| 172 | return true; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | static bool |
| 180 | FileAtPathContainsArchAndUUID |
| 181 | ( |
| 182 | const FileSpec &file_spec, |
| 183 | const ArchSpec *arch, |
| 184 | const UUID *uuid |
| 185 | ) |
| 186 | { |
| 187 | DataExtractor data; |
| 188 | off_t file_offset = 0; |
| 189 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000)); |
| 190 | |
| 191 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) |
| 192 | { |
| 193 | data.SetData(data_buffer_sp); |
| 194 | |
| 195 | uint32_t data_offset = 0; |
| 196 | uint32_t magic = data.GetU32(&data_offset); |
| 197 | |
| 198 | switch (magic) |
| 199 | { |
| 200 | // 32 bit mach-o file |
| 201 | case MH_CIGAM: |
| 202 | case MH_MAGIC: |
| 203 | case MH_CIGAM_64: |
| 204 | case MH_MAGIC_64: |
| 205 | return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 206 | |
| 207 | // fat mach-o file |
| 208 | case FAT_CIGAM: |
| 209 | case FAT_MAGIC: |
| 210 | return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 211 | |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | static FileSpec |
| 220 | LocateDSYMMachFileInDSYMBundle |
| 221 | ( |
| 222 | const FileSpec& dsym_bundle_fspec, |
| 223 | const UUID *uuid, |
| 224 | const ArchSpec *arch) |
| 225 | { |
| 226 | char path[PATH_MAX]; |
| 227 | |
| 228 | FileSpec dsym_fspec; |
| 229 | |
| 230 | if (dsym_bundle_fspec.GetPath(path, sizeof(path))) |
| 231 | { |
| 232 | ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1); |
| 233 | |
| 234 | DIR* dirp = ::opendir(path); |
| 235 | if (dirp != NULL) |
| 236 | { |
| 237 | const size_t path_len = strlen(path); |
| 238 | const int bytes_left = sizeof(path) - path_len - 1; |
| 239 | struct dirent* dp; |
| 240 | while ((dp = readdir(dirp)) != NULL) |
| 241 | { |
| 242 | // Only search directories |
| 243 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) |
| 244 | { |
| 245 | if (dp->d_namlen == 1 && dp->d_name[0] == '.') |
| 246 | continue; |
| 247 | |
| 248 | if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN) |
| 253 | { |
| 254 | ::strncpy (&path[path_len], dp->d_name, bytes_left); |
| 255 | |
| 256 | dsym_fspec.SetFile(path); |
| 257 | if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
| 258 | return dsym_fspec; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | dsym_fspec.Clear(); |
| 264 | return dsym_fspec; |
| 265 | } |
| 266 | |
| 267 | static int |
| 268 | LocateMacOSXFilesUsingDebugSymbols |
| 269 | ( |
| 270 | const FileSpec *exec_fspec, // An executable path that may or may not be correct if UUID is specified |
| 271 | const ArchSpec* arch, // Limit the search to files with this architecture if non-NULL |
| 272 | const UUID *uuid, // Match the UUID value if non-NULL, |
| 273 | FileSpec *out_exec_fspec, // If non-NULL, try and find the executable |
| 274 | FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file |
| 275 | ) |
| 276 | { |
| 277 | int items_found = 0; |
| 278 | |
| 279 | if (out_exec_fspec) |
| 280 | out_exec_fspec->Clear(); |
| 281 | |
| 282 | if (out_dsym_fspec) |
| 283 | out_dsym_fspec->Clear(); |
| 284 | |
| 285 | if (uuid && uuid->IsValid()) |
| 286 | { |
| 287 | // Try and locate the dSYM file using DebugSymbols first |
| 288 | const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes(); |
| 289 | if (module_uuid != NULL) |
| 290 | { |
| 291 | CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes ( NULL, |
| 292 | module_uuid[0], |
| 293 | module_uuid[1], |
| 294 | module_uuid[2], |
| 295 | module_uuid[3], |
| 296 | module_uuid[4], |
| 297 | module_uuid[5], |
| 298 | module_uuid[6], |
| 299 | module_uuid[7], |
| 300 | module_uuid[8], |
| 301 | module_uuid[9], |
| 302 | module_uuid[10], |
| 303 | module_uuid[11], |
| 304 | module_uuid[12], |
| 305 | module_uuid[13], |
| 306 | module_uuid[14], |
| 307 | module_uuid[15])); |
| 308 | |
| 309 | if (module_uuid_ref.get()) |
| 310 | { |
| 311 | CFCReleaser<CFURLRef> exec_url; |
| 312 | |
| 313 | if (exec_fspec) |
| 314 | { |
| 315 | char exec_cf_path[PATH_MAX]; |
| 316 | if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path))) |
| 317 | exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL, |
| 318 | (const UInt8 *)exec_cf_path, |
| 319 | strlen(exec_cf_path), |
| 320 | FALSE)); |
| 321 | } |
| 322 | |
| 323 | CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get())); |
| 324 | char path[PATH_MAX]; |
| 325 | |
| 326 | if (dsym_url.get()) |
| 327 | { |
| 328 | if (out_dsym_fspec) |
| 329 | { |
| 330 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 331 | { |
| 332 | out_dsym_fspec->SetFile(path); |
| 333 | |
| 334 | if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory) |
| 335 | { |
| 336 | *out_dsym_fspec = LocateDSYMMachFileInDSYMBundle (*out_dsym_fspec, uuid, arch); |
| 337 | if (*out_dsym_fspec) |
| 338 | ++items_found; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | ++items_found; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (out_exec_fspec) |
| 348 | { |
| 349 | CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));; |
| 350 | if (dict.get()) |
| 351 | { |
| 352 | CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (dict.get(), CFSTR("DBGSymbolRichExecutable"))); |
| 353 | if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path))) |
| 354 | { |
| 355 | ++items_found; |
| 356 | out_dsym_fspec->SetFile(path); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | return items_found; |
| 365 | } |
| 366 | |
| 367 | static bool |
| 368 | LocateDSYMInVincinityOfExecutable (const FileSpec *exec_fspec, const ArchSpec* arch, const UUID *uuid, FileSpec &dsym_fspec) |
| 369 | { |
| 370 | if (exec_fspec) |
| 371 | { |
| 372 | char path[PATH_MAX]; |
| 373 | if (exec_fspec->GetPath(path, sizeof(path))) |
| 374 | { |
| 375 | // Make sure the module isn't already just a dSYM file... |
| 376 | if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) |
| 377 | { |
| 378 | size_t obj_file_path_length = strlen(path); |
| 379 | strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 380 | strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
| 381 | |
| 382 | dsym_fspec.SetFile(path); |
| 383 | |
| 384 | if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
| 385 | { |
| 386 | return true; |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | path[obj_file_path_length] = '\0'; |
| 391 | |
| 392 | char *last_dot = strrchr(path, '.'); |
| 393 | while (last_dot != NULL && last_dot[0]) |
| 394 | { |
| 395 | char *next_slash = strchr(last_dot, '/'); |
| 396 | if (next_slash != NULL) |
| 397 | { |
| 398 | *next_slash = '\0'; |
| 399 | strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 400 | strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
| 401 | dsym_fspec.SetFile(path); |
| 402 | if (dsym_fspec.Exists()) |
| 403 | return true; |
| 404 | else |
| 405 | { |
| 406 | *last_dot = '\0'; |
| 407 | char *prev_slash = strrchr(path, '/'); |
| 408 | if (prev_slash != NULL) |
| 409 | *prev_slash = '\0'; |
| 410 | else |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | dsym_fspec.Clear(); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | FileSpec |
| 428 | Symbols::LocateExecutableObjectFile (const FileSpec *exec_fspec, const ArchSpec* arch, const UUID *uuid) |
| 429 | { |
| 430 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 431 | "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
| 432 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
| 433 | arch ? arch->AsCString() : "<NULL>", |
| 434 | uuid); |
| 435 | |
| 436 | FileSpec objfile_fspec; |
| 437 | if (exec_fspec && FileAtPathContainsArchAndUUID (*exec_fspec, arch, uuid)) |
| 438 | objfile_fspec = *exec_fspec; |
| 439 | else |
| 440 | LocateMacOSXFilesUsingDebugSymbols (exec_fspec, arch, uuid, &objfile_fspec, NULL); |
| 441 | return objfile_fspec; |
| 442 | } |
| 443 | |
| 444 | FileSpec |
| 445 | Symbols::LocateExecutableSymbolFile (const FileSpec *exec_fspec, const ArchSpec* arch, const UUID *uuid) |
| 446 | { |
| 447 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 448 | "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)", |
| 449 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
| 450 | arch ? arch->AsCString() : "<NULL>", |
| 451 | uuid); |
| 452 | |
| 453 | FileSpec symbol_fspec; |
| 454 | // First try and find the dSYM in the same directory as the executable or in |
| 455 | // an appropriate parent directory |
| 456 | if (LocateDSYMInVincinityOfExecutable (exec_fspec, arch, uuid, symbol_fspec) == false) |
| 457 | { |
| 458 | // We failed to easily find the dSYM above, so use DebugSymbols |
| 459 | LocateMacOSXFilesUsingDebugSymbols (exec_fspec, arch, uuid, NULL, &symbol_fspec); |
| 460 | } |
| 461 | return symbol_fspec; |
| 462 | } |