From 8079ae192d6465c2fa0f1241f9a0436932509ed6 Mon Sep 17 00:00:00 2001 From: Ted Mielczarek Date: Wed, 16 Sep 2015 06:46:55 -0400 Subject: Fix Mac Breakpad host tools to build in Linux cross-compile We're working on building our Firefox Mac builds as a Linux cross-compile (https://bugzilla.mozilla.org/show_bug.cgi?id=921040) and we need symbol dumping to work. This change ports the Mac dump_syms tool to build and work on Linux. I've tested it and it produces identical output to running the tool on Mac. The bulk of the work here was converting src/common/mac/dump_syms.mm and src/tools/mac/dump_syms/dump_syms_tool.mm from ObjC++ to C++ and removing their use of Foundation classes in favor of standard C/C++. This won't compile out-of-the-box on Linux, it requires some Mac system headers that are not included in this patch. I have those tentatively in a separate patch to land in Gecko (http://hg.mozilla.org/users/tmielczarek_mozilla.com/mc/rev/5fb8da23c83c), but I wasn't sure if you'd be interested in having them in the Breakpad tree. We could almost certainly pare down the set of headers included there, I didn't spend too much time trying to minimize them (we primarily just need the Mach-O structs and a few associated bits). I just realized that this patch is missing updating the XCode project files (ugh). I'll fix that up in a bit. R=mark@chromium.org BUG=https://bugzilla.mozilla.org/show_bug.cgi?id=543111 Review URL: https://codereview.chromium.org/1340543002 . --- src/common/mac/arch_utilities.cc | 118 +++++- src/common/mac/byteswap.h | 25 ++ src/common/mac/dump_syms.cc | 626 ++++++++++++++++++++++++++++++++ src/common/mac/dump_syms.h | 17 +- src/common/mac/dump_syms.mm | 603 ------------------------------ src/common/mac/file_id.cc | 3 +- src/common/mac/macho_id.cc | 36 +- src/common/mac/macho_reader_unittest.cc | 26 +- src/common/mac/macho_utilities.cc | 89 ++++- src/common/mac/macho_utilities.h | 27 +- src/common/mac/macho_walker.cc | 28 +- 11 files changed, 907 insertions(+), 691 deletions(-) create mode 100644 src/common/mac/dump_syms.cc delete mode 100644 src/common/mac/dump_syms.mm (limited to 'src/common/mac') diff --git a/src/common/mac/arch_utilities.cc b/src/common/mac/arch_utilities.cc index 94e3be3b..bfafc0d9 100644 --- a/src/common/mac/arch_utilities.cc +++ b/src/common/mac/arch_utilities.cc @@ -30,27 +30,20 @@ #include "common/mac/arch_utilities.h" #include +#include #include #include -#ifndef CPU_TYPE_ARM -#define CPU_TYPE_ARM (static_cast(12)) -#endif // CPU_TYPE_ARM - -#ifndef CPU_SUBTYPE_ARM_V7 -#define CPU_SUBTYPE_ARM_V7 (static_cast(9)) -#endif // CPU_SUBTYPE_ARM_V7 - #ifndef CPU_SUBTYPE_ARM_V7S #define CPU_SUBTYPE_ARM_V7S (static_cast(11)) #endif // CPU_SUBTYPE_ARM_V7S #ifndef CPU_TYPE_ARM64 -#define CPU_TYPE_ARM64 (static_cast(16777228)) +#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64) #endif // CPU_TYPE_ARM64 #ifndef CPU_SUBTYPE_ARM64_ALL -#define CPU_SUBTYPE_ARM64_ALL (static_cast(0)) +#define CPU_SUBTYPE_ARM64_ALL (static_cast(0)) #endif // CPU_SUBTYPE_ARM64_ALL namespace { @@ -111,3 +104,108 @@ const NXArchInfo* BreakpadGetArchInfoFromCpuType(cpu_type_t cpu_type, } } // namespace google_breakpad + +#ifndef __APPLE__ +namespace { + +enum Architecture { + kArch_i386 = 0, + kArch_x86_64, + kArch_arm, + kArch_arm64, + kArch_ppc, + // This must be last. + kNumArchitectures +}; + +// enum Architecture above and kKnownArchitectures below +// must be kept in sync. +const NXArchInfo kKnownArchitectures[] = { + { + "i386", + CPU_TYPE_I386, + CPU_SUBTYPE_I386_ALL, + NX_LittleEndian, + "Intel 80x86" + }, + { + "x86_64", + CPU_TYPE_X86_64, + CPU_SUBTYPE_X86_64_ALL, + NX_LittleEndian, + "Intel x86-64" + }, + { + "arm", + CPU_TYPE_ARM, + CPU_SUBTYPE_ARM_ALL, + NX_LittleEndian, + "ARM" + }, + { + "arm64", + CPU_TYPE_ARM64, + CPU_SUBTYPE_ARM64_ALL, + NX_LittleEndian, + "ARM64" + }, + { + "ppc", + CPU_TYPE_POWERPC, + CPU_SUBTYPE_POWERPC_ALL, + NX_BigEndian, + "PowerPC" + } +}; + +} // namespace + +const NXArchInfo *NXGetLocalArchInfo(void) { + Architecture arch; +#if defined(__i386__) + arch = kArch_i386; +#elif defined(__x86_64__) + arch = kArch_x86_64; +#elif defined(__arm64) + arch = kArch_arm64; +#elif defined(__arm__) + arch = kArch_arm; +#elif defined(__powerpc__) + arch = kArch_ppc; +#else + #error "Unsupported CPU architecture" +#endif + return &kKnownArchitectures[arch]; +} + +const NXArchInfo *NXGetArchInfoFromName(const char *name) { + for (int arch = 0; arch < kNumArchitectures; ++arch) { + if (strcmp(name, kKnownArchitectures[arch].name)) { + return &kKnownArchitectures[arch]; + } + } + return NULL; +} + +const NXArchInfo *NXGetArchInfoFromCpuType(cpu_type_t cputype, + cpu_subtype_t cpusubtype) { + for (int arch = 0; arch < kNumArchitectures; ++arch) { + if (kKnownArchitectures[arch].cputype == cputype) { + return &kKnownArchitectures[arch]; + } + } + return NULL; +} + +struct fat_arch *NXFindBestFatArch(cpu_type_t cputype, + cpu_subtype_t cpusubtype, + struct fat_arch *fat_archs, + uint32_t nfat_archs) { + for (uint32_t f = 0; f < nfat_archs; ++f) { + if (fat_archs[f].cputype == cputype) { + return &fat_archs[f]; + } + } + return NULL; +} +#endif // !__APPLE__ diff --git a/src/common/mac/byteswap.h b/src/common/mac/byteswap.h index a5d745b3..b7bbc0b9 100644 --- a/src/common/mac/byteswap.h +++ b/src/common/mac/byteswap.h @@ -36,6 +36,7 @@ #ifndef COMMON_MAC_BYTESWAP_H_ #define COMMON_MAC_BYTESWAP_H_ +#ifdef __APPLE__ #include static inline uint16_t ByteSwap(uint16_t v) { return OSSwapInt16(v); } @@ -45,4 +46,28 @@ static inline int16_t ByteSwap(int16_t v) { return OSSwapInt16(v); } static inline int32_t ByteSwap(int32_t v) { return OSSwapInt32(v); } static inline int64_t ByteSwap(int64_t v) { return OSSwapInt64(v); } +#elif defined(__linux__) +// For NXByteOrder +#include +#include +#include +#include_next + +static inline uint16_t ByteSwap(uint16_t v) { return bswap_16(v); } +static inline uint32_t ByteSwap(uint32_t v) { return bswap_32(v); } +static inline uint64_t ByteSwap(uint64_t v) { return bswap_64(v); } +static inline int16_t ByteSwap(int16_t v) { return bswap_16(v); } +static inline int32_t ByteSwap(int32_t v) { return bswap_32(v); } +static inline int64_t ByteSwap(int64_t v) { return bswap_64(v); } + +static inline NXByteOrder NXHostByteOrder() { +#ifdef __LITTLE_ENDIAN + return NX_LittleEndian; +#else + return NX_BigEndian; +#endif +} + +#endif // __APPLE__ + #endif // COMMON_MAC_BYTESWAP_H_ diff --git a/src/common/mac/dump_syms.cc b/src/common/mac/dump_syms.cc new file mode 100644 index 00000000..4dcdb73e --- /dev/null +++ b/src/common/mac/dump_syms.cc @@ -0,0 +1,626 @@ +// -*- mode: c++ -*- + +// Copyright (c) 2011, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Author: Jim Blandy + +// dump_syms.mm: Create a symbol file for use with minidumps + +#include "common/mac/dump_syms.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "common/dwarf/bytereader-inl.h" +#include "common/dwarf/dwarf2reader.h" +#include "common/dwarf_cfi_to_module.h" +#include "common/dwarf_cu_to_module.h" +#include "common/dwarf_line_to_module.h" +#include "common/mac/file_id.h" +#include "common/mac/arch_utilities.h" +#include "common/mac/macho_reader.h" +#include "common/module.h" +#include "common/scoped_ptr.h" +#include "common/stabs_reader.h" +#include "common/stabs_to_module.h" +#include "common/symbol_data.h" + +#ifndef CPU_TYPE_ARM +#define CPU_TYPE_ARM (static_cast(12)) +#endif // CPU_TYPE_ARM + +#ifndef CPU_TYPE_ARM64 +#define CPU_TYPE_ARM64 (static_cast(16777228)) +#endif // CPU_TYPE_ARM64 + +using dwarf2reader::ByteReader; +using google_breakpad::DwarfCUToModule; +using google_breakpad::DwarfLineToModule; +using google_breakpad::FileID; +using google_breakpad::mach_o::FatReader; +using google_breakpad::mach_o::Section; +using google_breakpad::mach_o::Segment; +using google_breakpad::Module; +using google_breakpad::StabsReader; +using google_breakpad::StabsToModule; +using google_breakpad::scoped_ptr; +using std::make_pair; +using std::pair; +using std::string; +using std::vector; + +namespace { +// Return a vector with absolute paths to all the entries +// in directory (excluding . and ..). +vector list_directory(const string& directory) { + vector entries; + DIR* dir = opendir(directory.c_str()); + if (!dir) { + return entries; + } + + string path = directory; + if (path[path.length() - 1] != '/') { + path += '/'; + } + + struct dirent* entry = NULL; + while ((entry = readdir(dir))) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + entries.push_back(path + entry->d_name); + } + } + + closedir(dir); + return entries; +} +} + +namespace google_breakpad { + +bool DumpSymbols::Read(const string &filename) { + struct stat st; + if (stat(filename.c_str(), &st) == -1) { + fprintf(stderr, "Could not access object file %s: %s\n", + filename.c_str(), strerror(errno)); + return false; + } + + input_pathname_ = filename; + + // Does this filename refer to a dSYM bundle? + string contents_path = input_pathname_ + "/Contents/Resources/DWARF"; + if (S_ISDIR(st.st_mode) && + access(contents_path.c_str(), F_OK) == 0) { + // If there's one file under Contents/Resources/DWARF then use that, + // otherwise bail out. + const vector entries = list_directory(contents_path); + if (entries.size() == 0) { + fprintf(stderr, "Unable to find DWARF-bearing file in bundle: %s\n", + input_pathname_.c_str()); + return false; + } + if (entries.size() > 1) { + fprintf(stderr, "Too many DWARF files in bundle: %s\n", + input_pathname_.c_str()); + return false; + } + + object_filename_ = entries[0]; + } else { + object_filename_ = input_pathname_; + } + + // Read the file's contents into memory. + bool read_ok = true; + string error; + if (stat(object_filename_.c_str(), &st) != -1) { + FILE* f = fopen(object_filename_.c_str(), "rb"); + if (f) { + contents_.reset(new uint8_t[st.st_size]); + off_t total = 0; + while (total < st.st_size && !feof(f)) { + size_t read = fread(&contents_[0] + total, 1, st.st_size - total, f); + if (read == 0) { + if (ferror(f)) { + read_ok = false; + error = strerror(errno); + } + break; + } + total += read; + } + fclose(f); + } else { + error = strerror(errno); + } + } + + if (!read_ok) { + fprintf(stderr, "Error reading object file: %s: %s\n", + object_filename_.c_str(), + error.c_str()); + return false; + } + + // Get the list of object files present in the file. + FatReader::Reporter fat_reporter(object_filename_); + FatReader fat_reader(&fat_reporter); + if (!fat_reader.Read(&contents_[0], + st.st_size)) { + return false; + } + + // Get our own copy of fat_reader's object file list. + size_t object_files_count; + const SuperFatArch *object_files = + fat_reader.object_files(&object_files_count); + if (object_files_count == 0) { + fprintf(stderr, "Fat binary file contains *no* architectures: %s\n", + object_filename_.c_str()); + return false; + } + object_files_.resize(object_files_count); + memcpy(&object_files_[0], object_files, + sizeof(SuperFatArch) * object_files_count); + + return true; +} + +bool DumpSymbols::SetArchitecture(cpu_type_t cpu_type, + cpu_subtype_t cpu_subtype) { + // Find the best match for the architecture the user requested. + const SuperFatArch *best_match = FindBestMatchForArchitecture( + cpu_type, cpu_subtype); + if (!best_match) return false; + + // Record the selected object file. + selected_object_file_ = best_match; + return true; +} + +bool DumpSymbols::SetArchitecture(const std::string &arch_name) { + bool arch_set = false; + const NXArchInfo *arch_info = + google_breakpad::BreakpadGetArchInfoFromName(arch_name.c_str()); + if (arch_info) { + arch_set = SetArchitecture(arch_info->cputype, arch_info->cpusubtype); + } + return arch_set; +} + +SuperFatArch* DumpSymbols::FindBestMatchForArchitecture( + cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) { + // Check if all the object files can be converted to struct fat_arch. + bool can_convert_to_fat_arch = true; + vector fat_arch_vector; + for (vector::const_iterator it = object_files_.begin(); + it != object_files_.end(); + ++it) { + struct fat_arch arch; + bool success = it->ConvertToFatArch(&arch); + if (!success) { + can_convert_to_fat_arch = false; + break; + } + fat_arch_vector.push_back(arch); + } + + // If all the object files can be converted to struct fat_arch, use + // NXFindBestFatArch. + if (can_convert_to_fat_arch) { + const struct fat_arch *best_match + = NXFindBestFatArch(cpu_type, cpu_subtype, &fat_arch_vector[0], + static_cast(fat_arch_vector.size())); + + for (size_t i = 0; i < fat_arch_vector.size(); ++i) { + if (best_match == &fat_arch_vector[i]) + return &object_files_[i]; + } + assert(best_match == NULL); + return NULL; + } + + // Check for an exact match with cpu_type and cpu_subtype. + for (vector::iterator it = object_files_.begin(); + it != object_files_.end(); + ++it) { + if (static_cast(it->cputype) == cpu_type && + static_cast(it->cpusubtype) == cpu_subtype) + return &*it; + } + + // No exact match found. + // TODO(erikchen): If it becomes necessary, we can copy the implementation of + // NXFindBestFatArch, located at + // http://web.mit.edu/darwin/src/modules/cctools/libmacho/arch.c. + fprintf(stderr, "Failed to find an exact match for an object file with cpu " + "type: %d and cpu subtype: %d. Furthermore, at least one object file is " + "larger than 2**32.\n", cpu_type, cpu_subtype); + return NULL; +} + +string DumpSymbols::Identifier() { + FileID file_id(object_filename_.c_str()); + unsigned char identifier_bytes[16]; + cpu_type_t cpu_type = selected_object_file_->cputype; + cpu_subtype_t cpu_subtype = selected_object_file_->cpusubtype; + if (!file_id.MachoIdentifier(cpu_type, cpu_subtype, identifier_bytes)) { + fprintf(stderr, "Unable to calculate UUID of mach-o binary %s!\n", + object_filename_.c_str()); + return ""; + } + + char identifier_string[40]; + FileID::ConvertIdentifierToString(identifier_bytes, identifier_string, + sizeof(identifier_string)); + + string compacted(identifier_string); + for(size_t i = compacted.find('-'); i != string::npos; + i = compacted.find('-', i)) + compacted.erase(i, 1); + + return compacted; +} + +// A line-to-module loader that accepts line number info parsed by +// dwarf2reader::LineInfo and populates a Module and a line vector +// with the results. +class DumpSymbols::DumperLineToModule: + public DwarfCUToModule::LineToModuleHandler { + public: + // Create a line-to-module converter using BYTE_READER. + DumperLineToModule(dwarf2reader::ByteReader *byte_reader) + : byte_reader_(byte_reader) { } + + void StartCompilationUnit(const string& compilation_dir) { + compilation_dir_ = compilation_dir; + } + + void ReadProgram(const char *program, uint64 length, + Module *module, vector *lines) { + DwarfLineToModule handler(module, compilation_dir_, lines); + dwarf2reader::LineInfo parser(program, length, byte_reader_, &handler); + parser.Start(); + } + private: + string compilation_dir_; + dwarf2reader::ByteReader *byte_reader_; // WEAK +}; + +bool DumpSymbols::ReadDwarf(google_breakpad::Module *module, + const mach_o::Reader &macho_reader, + const mach_o::SectionMap &dwarf_sections, + bool handle_inter_cu_refs) const { + // Build a byte reader of the appropriate endianness. + ByteReader byte_reader(macho_reader.big_endian() + ? dwarf2reader::ENDIANNESS_BIG + : dwarf2reader::ENDIANNESS_LITTLE); + + // Construct a context for this file. + DwarfCUToModule::FileContext file_context(selected_object_name_, + module, + handle_inter_cu_refs); + + // Build a dwarf2reader::SectionMap from our mach_o::SectionMap. + for (mach_o::SectionMap::const_iterator it = dwarf_sections.begin(); + it != dwarf_sections.end(); ++it) { + file_context.AddSectionToSectionMap( + it->first, + reinterpret_cast(it->second.contents.start), + it->second.contents.Size()); + } + + // Find the __debug_info section. + dwarf2reader::SectionMap::const_iterator debug_info_entry = + file_context.section_map().find("__debug_info"); + assert(debug_info_entry != file_context.section_map().end()); + const std::pair& debug_info_section = + debug_info_entry->second; + // There had better be a __debug_info section! + if (!debug_info_section.first) { + fprintf(stderr, "%s: __DWARF segment of file has no __debug_info section\n", + selected_object_name_.c_str()); + return false; + } + + // Build a line-to-module loader for the root handler to use. + DumperLineToModule line_to_module(&byte_reader); + + // Walk the __debug_info section, one compilation unit at a time. + uint64 debug_info_length = debug_info_section.second; + for (uint64 offset = 0; offset < debug_info_length;) { + // Make a handler for the root DIE that populates MODULE with the + // debug info. + DwarfCUToModule::WarningReporter reporter(selected_object_name_, + offset); + DwarfCUToModule root_handler(&file_context, &line_to_module, &reporter); + // Make a Dwarf2Handler that drives our DIEHandler. + dwarf2reader::DIEDispatcher die_dispatcher(&root_handler); + // Make a DWARF parser for the compilation unit at OFFSET. + dwarf2reader::CompilationUnit dwarf_reader(file_context.section_map(), + offset, + &byte_reader, + &die_dispatcher); + // Process the entire compilation unit; get the offset of the next. + offset += dwarf_reader.Start(); + } + + return true; +} + +bool DumpSymbols::ReadCFI(google_breakpad::Module *module, + const mach_o::Reader &macho_reader, + const mach_o::Section §ion, + bool eh_frame) const { + // Find the appropriate set of register names for this file's + // architecture. + vector register_names; + switch (macho_reader.cpu_type()) { + case CPU_TYPE_X86: + register_names = DwarfCFIToModule::RegisterNames::I386(); + break; + case CPU_TYPE_X86_64: + register_names = DwarfCFIToModule::RegisterNames::X86_64(); + break; + case CPU_TYPE_ARM: + register_names = DwarfCFIToModule::RegisterNames::ARM(); + break; + case CPU_TYPE_ARM64: + register_names = DwarfCFIToModule::RegisterNames::ARM64(); + break; + default: { + const NXArchInfo *arch = google_breakpad::BreakpadGetArchInfoFromCpuType( + macho_reader.cpu_type(), macho_reader.cpu_subtype()); + fprintf(stderr, "%s: cannot convert DWARF call frame information for ", + selected_object_name_.c_str()); + if (arch) + fprintf(stderr, "architecture '%s'", arch->name); + else + fprintf(stderr, "architecture %d,%d", + macho_reader.cpu_type(), macho_reader.cpu_subtype()); + fprintf(stderr, " to Breakpad symbol file: no register name table\n"); + return false; + } + } + + // Find the call frame information and its size. + const char *cfi = reinterpret_cast(section.contents.start); + size_t cfi_size = section.contents.Size(); + + // Plug together the parser, handler, and their entourages. + DwarfCFIToModule::Reporter module_reporter(selected_object_name_, + section.section_name); + DwarfCFIToModule handler(module, register_names, &module_reporter); + dwarf2reader::ByteReader byte_reader(macho_reader.big_endian() ? + dwarf2reader::ENDIANNESS_BIG : + dwarf2reader::ENDIANNESS_LITTLE); + byte_reader.SetAddressSize(macho_reader.bits_64() ? 8 : 4); + // At the moment, according to folks at Apple and some cursory + // investigation, Mac OS X only uses DW_EH_PE_pcrel-based pointers, so + // this is the only base address the CFI parser will need. + byte_reader.SetCFIDataBase(section.address, cfi); + + dwarf2reader::CallFrameInfo::Reporter dwarf_reporter(selected_object_name_, + section.section_name); + dwarf2reader::CallFrameInfo parser(cfi, cfi_size, + &byte_reader, &handler, &dwarf_reporter, + eh_frame); + parser.Start(); + return true; +} + +// A LoadCommandHandler that loads whatever debugging data it finds into a +// Module. +class DumpSymbols::LoadCommandDumper: + public mach_o::Reader::LoadCommandHandler { + public: + // Create a load command dumper handling load commands from READER's + // file, and adding data to MODULE. + LoadCommandDumper(const DumpSymbols &dumper, + google_breakpad::Module *module, + const mach_o::Reader &reader, + SymbolData symbol_data, + bool handle_inter_cu_refs) + : dumper_(dumper), + module_(module), + reader_(reader), + symbol_data_(symbol_data), + handle_inter_cu_refs_(handle_inter_cu_refs) { } + + bool SegmentCommand(const mach_o::Segment &segment); + bool SymtabCommand(const ByteBuffer &entries, const ByteBuffer &strings); + + private: + const DumpSymbols &dumper_; + google_breakpad::Module *module_; // WEAK + const mach_o::Reader &reader_; + const SymbolData symbol_data_; + const bool handle_inter_cu_refs_; +}; + +bool DumpSymbols::LoadCommandDumper::SegmentCommand(const Segment &segment) { + mach_o::SectionMap section_map; + if (!reader_.MapSegmentSections(segment, §ion_map)) + return false; + + if (segment.name == "__TEXT") { + module_->SetLoadAddress(segment.vmaddr); + if (symbol_data_ != NO_CFI) { + mach_o::SectionMap::const_iterator eh_frame = + section_map.find("__eh_frame"); + if (eh_frame != section_map.end()) { + // If there is a problem reading this, don't treat it as a fatal error. + dumper_.ReadCFI(module_, reader_, eh_frame->second, true); + } + } + return true; + } + + if (segment.name == "__DWARF") { + if (symbol_data_ != ONLY_CFI) { + if (!dumper_.ReadDwarf(module_, reader_, section_map, + handle_inter_cu_refs_)) { + return false; + } + } + if (symbol_data_ != NO_CFI) { + mach_o::SectionMap::const_iterator debug_frame + = section_map.find("__debug_frame"); + if (debug_frame != section_map.end()) { + // If there is a problem reading this, don't treat it as a fatal error. + dumper_.ReadCFI(module_, reader_, debug_frame->second, false); + } + } + } + + return true; +} + +bool DumpSymbols::LoadCommandDumper::SymtabCommand(const ByteBuffer &entries, + const ByteBuffer &strings) { + StabsToModule stabs_to_module(module_); + // Mac OS X STABS are never "unitized", and the size of the 'value' field + // matches the address size of the executable. + StabsReader stabs_reader(entries.start, entries.Size(), + strings.start, strings.Size(), + reader_.big_endian(), + reader_.bits_64() ? 8 : 4, + true, + &stabs_to_module); + if (!stabs_reader.Process()) + return false; + stabs_to_module.Finalize(); + return true; +} + +bool DumpSymbols::ReadSymbolData(Module** out_module) { + // Select an object file, if SetArchitecture hasn't been called to set one + // explicitly. + if (!selected_object_file_) { + // If there's only one architecture, that's the one. + if (object_files_.size() == 1) + selected_object_file_ = &object_files_[0]; + else { + // Look for an object file whose architecture matches our own. + const NXArchInfo *local_arch = NXGetLocalArchInfo(); + if (!SetArchitecture(local_arch->cputype, local_arch->cpusubtype)) { + fprintf(stderr, "%s: object file contains more than one" + " architecture, none of which match the current" + " architecture; specify an architecture explicitly" + " with '-a ARCH' to resolve the ambiguity\n", + object_filename_.c_str()); + return false; + } + } + } + + assert(selected_object_file_); + + // Find the name of the selected file's architecture, to appear in + // the MODULE record and in error messages. + const NXArchInfo *selected_arch_info = + google_breakpad::BreakpadGetArchInfoFromCpuType( + selected_object_file_->cputype, selected_object_file_->cpusubtype); + + const char *selected_arch_name = selected_arch_info->name; + if (strcmp(selected_arch_name, "i386") == 0) + selected_arch_name = "x86"; + + // Produce a name to use in error messages that includes the + // filename, and the architecture, if there is more than one. + selected_object_name_ = object_filename_; + if (object_files_.size() > 1) { + selected_object_name_ += ", architecture "; + selected_object_name_ + selected_arch_name; + } + + // Compute a module name, to appear in the MODULE record. + string module_name = object_filename_; + module_name = basename(&module_name[0]); + + // Choose an identifier string, to appear in the MODULE record. + string identifier = Identifier(); + if (identifier.empty()) + return false; + identifier += "0"; + + // Create a module to hold the debugging information. + scoped_ptr module(new Module(module_name, + "mac", + selected_arch_name, + identifier)); + + // Parse the selected object file. + mach_o::Reader::Reporter reporter(selected_object_name_); + mach_o::Reader reader(&reporter); + if (!reader.Read(&contents_[0] + + selected_object_file_->offset, + selected_object_file_->size, + selected_object_file_->cputype, + selected_object_file_->cpusubtype)) + return false; + + // Walk its load commands, and deal with whatever is there. + LoadCommandDumper load_command_dumper(*this, module.get(), reader, + symbol_data_, handle_inter_cu_refs_); + if (!reader.WalkLoadCommands(&load_command_dumper)) + return false; + + *out_module = module.release(); + + return true; +} + +bool DumpSymbols::WriteSymbolFile(std::ostream &stream) { + Module* module = NULL; + + if (ReadSymbolData(&module) && module) { + bool res = module->Write(stream, symbol_data_); + delete module; + return res; + } + + return false; +} + +} // namespace google_breakpad diff --git a/src/common/mac/dump_syms.h b/src/common/mac/dump_syms.h index babed701..b09928c9 100644 --- a/src/common/mac/dump_syms.h +++ b/src/common/mac/dump_syms.h @@ -35,7 +35,6 @@ // reading debugging information from Mach-O files and writing it out as a // Breakpad symbol file. -#include #include #include #include @@ -48,6 +47,7 @@ #include "common/mac/macho_reader.h" #include "common/mac/super_fat_arch.h" #include "common/module.h" +#include "common/scoped_ptr.h" #include "common/symbol_data.h" namespace google_breakpad { @@ -64,20 +64,13 @@ class DumpSymbols { selected_object_file_(), selected_object_name_() { } ~DumpSymbols() { - [input_pathname_ release]; - [object_filename_ release]; - [contents_ release]; } // Prepare to read debugging information from |filename|. |filename| may be // the name of a universal binary, a Mach-O file, or a dSYM bundle // containing either of the above. On success, return true; if there is a // problem reading |filename|, report it and return false. - // - // (This class uses NSString for filenames and related values, - // because the Mac Foundation framework seems to support - // filename-related operations more fully on NSString values.) - bool Read(NSString *filename); + bool Read(const std::string &filename); // If this dumper's file includes an object file for |cpu_type| and // |cpu_subtype|, then select that object file for dumping, and return @@ -163,16 +156,16 @@ class DumpSymbols { // The name of the file or bundle whose symbols this will dump. // This is the path given to Read, for use in error messages. - NSString *input_pathname_; + std::string input_pathname_; // The name of the file this DumpSymbols will actually read debugging // information from. Normally, this is the same as input_pathname_, but if // filename refers to a dSYM bundle, then this is the resource file // within that bundle. - NSString *object_filename_; + std::string object_filename_; // The complete contents of object_filename_, mapped into memory. - NSData *contents_; + scoped_array contents_; // A vector of SuperFatArch structures describing the object files // object_filename_ contains. If object_filename_ refers to a fat binary, diff --git a/src/common/mac/dump_syms.mm b/src/common/mac/dump_syms.mm deleted file mode 100644 index 58cf4c20..00000000 --- a/src/common/mac/dump_syms.mm +++ /dev/null @@ -1,603 +0,0 @@ -// -*- mode: c++ -*- - -// Copyright (c) 2011, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Author: Jim Blandy - -// dump_syms.mm: Create a symbol file for use with minidumps - -#include "common/mac/dump_syms.h" - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "common/dwarf/bytereader-inl.h" -#include "common/dwarf/dwarf2reader.h" -#include "common/dwarf_cfi_to_module.h" -#include "common/dwarf_cu_to_module.h" -#include "common/dwarf_line_to_module.h" -#include "common/mac/file_id.h" -#include "common/mac/arch_utilities.h" -#include "common/mac/macho_reader.h" -#include "common/module.h" -#include "common/scoped_ptr.h" -#include "common/stabs_reader.h" -#include "common/stabs_to_module.h" -#include "common/symbol_data.h" - -#ifndef CPU_TYPE_ARM -#define CPU_TYPE_ARM (static_cast(12)) -#endif // CPU_TYPE_ARM - -#ifndef CPU_TYPE_ARM64 -#define CPU_TYPE_ARM64 (static_cast(16777228)) -#endif // CPU_TYPE_ARM64 - -using dwarf2reader::ByteReader; -using google_breakpad::DwarfCUToModule; -using google_breakpad::DwarfLineToModule; -using google_breakpad::FileID; -using google_breakpad::mach_o::FatReader; -using google_breakpad::mach_o::Section; -using google_breakpad::mach_o::Segment; -using google_breakpad::Module; -using google_breakpad::StabsReader; -using google_breakpad::StabsToModule; -using google_breakpad::scoped_ptr; -using std::make_pair; -using std::pair; -using std::string; -using std::vector; - -namespace google_breakpad { - -bool DumpSymbols::Read(NSString *filename) { - if (![[NSFileManager defaultManager] fileExistsAtPath:filename]) { - fprintf(stderr, "Object file does not exist: %s\n", - [filename fileSystemRepresentation]); - return false; - } - - input_pathname_ = [filename retain]; - - // Does this filename refer to a dSYM bundle? - NSBundle *bundle = [NSBundle bundleWithPath:input_pathname_]; - - if (bundle) { - // Filenames referring to bundles usually have names of the form - // ".dSYM"; however, if the user has specified a wrapper - // suffix (the WRAPPER_SUFFIX and WRAPPER_EXTENSION build settings), - // then the name may have the form "..dSYM". In - // either case, the resource name for the file containing the DWARF - // info within the bundle is . - // - // Since there's no way to tell how much to strip off, remove one - // extension at a time, and use the first one that - // pathForResource:ofType:inDirectory likes. - NSString *base_name = [input_pathname_ lastPathComponent]; - NSString *dwarf_resource; - - do { - NSString *new_base_name = [base_name stringByDeletingPathExtension]; - - // If stringByDeletingPathExtension returned the name unchanged, then - // there's nothing more for us to strip off --- lose. - if ([new_base_name isEqualToString:base_name]) { - fprintf(stderr, "Unable to find DWARF-bearing file in bundle: %s\n", - [input_pathname_ fileSystemRepresentation]); - return false; - } - - // Take the shortened result as our new base_name. - base_name = new_base_name; - - // Try to find a DWARF resource in the bundle under the new base_name. - dwarf_resource = [bundle pathForResource:base_name - ofType:nil inDirectory:@"DWARF"]; - } while (!dwarf_resource); - - object_filename_ = [dwarf_resource retain]; - } else { - object_filename_ = [input_pathname_ retain]; - } - - // Read the file's contents into memory. - // - // The documentation for dataWithContentsOfMappedFile says: - // - // Because of file mapping restrictions, this method should only be - // used if the file is guaranteed to exist for the duration of the - // data object’s existence. It is generally safer to use the - // dataWithContentsOfFile: method. - // - // I gather this means that OS X doesn't have (or at least, that method - // doesn't use) a form of mapping like Linux's MAP_PRIVATE, where the - // process appears to get its own copy of the data, and changes to the - // file don't affect memory and vice versa). - NSError *error; - contents_ = [NSData dataWithContentsOfFile:object_filename_ - options:0 - error:&error]; - if (!contents_) { - fprintf(stderr, "Error reading object file: %s: %s\n", - [object_filename_ fileSystemRepresentation], - [[error localizedDescription] UTF8String]); - return false; - } - [contents_ retain]; - - // Get the list of object files present in the file. - FatReader::Reporter fat_reporter([object_filename_ - fileSystemRepresentation]); - FatReader fat_reader(&fat_reporter); - if (!fat_reader.Read(reinterpret_cast([contents_ bytes]), - [contents_ length])) { - return false; - } - - // Get our own copy of fat_reader's object file list. - size_t object_files_count; - const SuperFatArch *object_files = - fat_reader.object_files(&object_files_count); - if (object_files_count == 0) { - fprintf(stderr, "Fat binary file contains *no* architectures: %s\n", - [object_filename_ fileSystemRepresentation]); - return false; - } - object_files_.resize(object_files_count); - memcpy(&object_files_[0], object_files, - sizeof(SuperFatArch) * object_files_count); - - return true; -} - -bool DumpSymbols::SetArchitecture(cpu_type_t cpu_type, - cpu_subtype_t cpu_subtype) { - // Find the best match for the architecture the user requested. - const SuperFatArch *best_match = FindBestMatchForArchitecture( - cpu_type, cpu_subtype); - if (!best_match) return false; - - // Record the selected object file. - selected_object_file_ = best_match; - return true; -} - -bool DumpSymbols::SetArchitecture(const std::string &arch_name) { - bool arch_set = false; - const NXArchInfo *arch_info = - google_breakpad::BreakpadGetArchInfoFromName(arch_name.c_str()); - if (arch_info) { - arch_set = SetArchitecture(arch_info->cputype, arch_info->cpusubtype); - } - return arch_set; -} - -SuperFatArch* DumpSymbols::FindBestMatchForArchitecture( - cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) { - // Check if all the object files can be converted to struct fat_arch. - bool can_convert_to_fat_arch = true; - vector fat_arch_vector; - for (vector::const_iterator it = object_files_.begin(); - it != object_files_.end(); - ++it) { - struct fat_arch arch; - bool success = it->ConvertToFatArch(&arch); - if (!success) { - can_convert_to_fat_arch = false; - break; - } - fat_arch_vector.push_back(arch); - } - - // If all the object files can be converted to struct fat_arch, use - // NXFindBestFatArch. - if (can_convert_to_fat_arch) { - const struct fat_arch *best_match - = NXFindBestFatArch(cpu_type, cpu_subtype, &fat_arch_vector[0], - static_cast(fat_arch_vector.size())); - - for (size_t i = 0; i < fat_arch_vector.size(); ++i) { - if (best_match == &fat_arch_vector[i]) - return &object_files_[i]; - } - assert(best_match == NULL); - return NULL; - } - - // Check for an exact match with cpu_type and cpu_subtype. - for (vector::iterator it = object_files_.begin(); - it != object_files_.end(); - ++it) { - if (it->cputype == cpu_type && it->cpusubtype == cpu_subtype) - return &*it; - } - - // No exact match found. - // TODO(erikchen): If it becomes necessary, we can copy the implementation of - // NXFindBestFatArch, located at - // http://web.mit.edu/darwin/src/modules/cctools/libmacho/arch.c. - fprintf(stderr, "Failed to find an exact match for an object file with cpu " - "type: %d and cpu subtype: %d. Furthermore, at least one object file is " - "larger than 2**32.\n", cpu_type, cpu_subtype); - return NULL; -} - -string DumpSymbols::Identifier() { - FileID file_id([object_filename_ fileSystemRepresentation]); - unsigned char identifier_bytes[16]; - cpu_type_t cpu_type = selected_object_file_->cputype; - cpu_subtype_t cpu_subtype = selected_object_file_->cpusubtype; - if (!file_id.MachoIdentifier(cpu_type, cpu_subtype, identifier_bytes)) { - fprintf(stderr, "Unable to calculate UUID of mach-o binary %s!\n", - [object_filename_ fileSystemRepresentation]); - return ""; - } - - char identifier_string[40]; - FileID::ConvertIdentifierToString(identifier_bytes, identifier_string, - sizeof(identifier_string)); - - string compacted(identifier_string); - for(size_t i = compacted.find('-'); i != string::npos; - i = compacted.find('-', i)) - compacted.erase(i, 1); - - return compacted; -} - -// A line-to-module loader that accepts line number info parsed by -// dwarf2reader::LineInfo and populates a Module and a line vector -// with the results. -class DumpSymbols::DumperLineToModule: - public DwarfCUToModule::LineToModuleHandler { - public: - // Create a line-to-module converter using BYTE_READER. - DumperLineToModule(dwarf2reader::ByteReader *byte_reader) - : byte_reader_(byte_reader) { } - - void StartCompilationUnit(const string& compilation_dir) { - compilation_dir_ = compilation_dir; - } - - void ReadProgram(const char *program, uint64 length, - Module *module, vector *lines) { - DwarfLineToModule handler(module, compilation_dir_, lines); - dwarf2reader::LineInfo parser(program, length, byte_reader_, &handler); - parser.Start(); - } - private: - string compilation_dir_; - dwarf2reader::ByteReader *byte_reader_; // WEAK -}; - -bool DumpSymbols::ReadDwarf(google_breakpad::Module *module, - const mach_o::Reader &macho_reader, - const mach_o::SectionMap &dwarf_sections, - bool handle_inter_cu_refs) const { - // Build a byte reader of the appropriate endianness. - ByteReader byte_reader(macho_reader.big_endian() - ? dwarf2reader::ENDIANNESS_BIG - : dwarf2reader::ENDIANNESS_LITTLE); - - // Construct a context for this file. - DwarfCUToModule::FileContext file_context(selected_object_name_, - module, - handle_inter_cu_refs); - - // Build a dwarf2reader::SectionMap from our mach_o::SectionMap. - for (mach_o::SectionMap::const_iterator it = dwarf_sections.begin(); - it != dwarf_sections.end(); ++it) { - file_context.AddSectionToSectionMap( - it->first, - reinterpret_cast(it->second.contents.start), - it->second.contents.Size()); - } - - // Find the __debug_info section. - dwarf2reader::SectionMap::const_iterator debug_info_entry = - file_context.section_map().find("__debug_info"); - assert(debug_info_entry != file_context.section_map().end()); - const std::pair& debug_info_section = - debug_info_entry->second; - // There had better be a __debug_info section! - if (!debug_info_section.first) { - fprintf(stderr, "%s: __DWARF segment of file has no __debug_info section\n", - selected_object_name_.c_str()); - return false; - } - - // Build a line-to-module loader for the root handler to use. - DumperLineToModule line_to_module(&byte_reader); - - // Walk the __debug_info section, one compilation unit at a time. - uint64 debug_info_length = debug_info_section.second; - for (uint64 offset = 0; offset < debug_info_length;) { - // Make a handler for the root DIE that populates MODULE with the - // debug info. - DwarfCUToModule::WarningReporter reporter(selected_object_name_, - offset); - DwarfCUToModule root_handler(&file_context, &line_to_module, &reporter); - // Make a Dwarf2Handler that drives our DIEHandler. - dwarf2reader::DIEDispatcher die_dispatcher(&root_handler); - // Make a DWARF parser for the compilation unit at OFFSET. - dwarf2reader::CompilationUnit dwarf_reader(file_context.section_map(), - offset, - &byte_reader, - &die_dispatcher); - // Process the entire compilation unit; get the offset of the next. - offset += dwarf_reader.Start(); - } - - return true; -} - -bool DumpSymbols::ReadCFI(google_breakpad::Module *module, - const mach_o::Reader &macho_reader, - const mach_o::Section §ion, - bool eh_frame) const { - // Find the appropriate set of register names for this file's - // architecture. - vector register_names; - switch (macho_reader.cpu_type()) { - case CPU_TYPE_X86: - register_names = DwarfCFIToModule::RegisterNames::I386(); - break; - case CPU_TYPE_X86_64: - register_names = DwarfCFIToModule::RegisterNames::X86_64(); - break; - case CPU_TYPE_ARM: - register_names = DwarfCFIToModule::RegisterNames::ARM(); - break; - case CPU_TYPE_ARM64: - register_names = DwarfCFIToModule::RegisterNames::ARM64(); - break; - default: { - const NXArchInfo *arch = google_breakpad::BreakpadGetArchInfoFromCpuType( - macho_reader.cpu_type(), macho_reader.cpu_subtype()); - fprintf(stderr, "%s: cannot convert DWARF call frame information for ", - selected_object_name_.c_str()); - if (arch) - fprintf(stderr, "architecture '%s'", arch->name); - else - fprintf(stderr, "architecture %d,%d", - macho_reader.cpu_type(), macho_reader.cpu_subtype()); - fprintf(stderr, " to Breakpad symbol file: no register name table\n"); - return false; - } - } - - // Find the call frame information and its size. - const char *cfi = reinterpret_cast(section.contents.start); - size_t cfi_size = section.contents.Size(); - - // Plug together the parser, handler, and their entourages. - DwarfCFIToModule::Reporter module_reporter(selected_object_name_, - section.section_name); - DwarfCFIToModule handler(module, register_names, &module_reporter); - dwarf2reader::ByteReader byte_reader(macho_reader.big_endian() ? - dwarf2reader::ENDIANNESS_BIG : - dwarf2reader::ENDIANNESS_LITTLE); - byte_reader.SetAddressSize(macho_reader.bits_64() ? 8 : 4); - // At the moment, according to folks at Apple and some cursory - // investigation, Mac OS X only uses DW_EH_PE_pcrel-based pointers, so - // this is the only base address the CFI parser will need. - byte_reader.SetCFIDataBase(section.address, cfi); - - dwarf2reader::CallFrameInfo::Reporter dwarf_reporter(selected_object_name_, - section.section_name); - dwarf2reader::CallFrameInfo parser(cfi, cfi_size, - &byte_reader, &handler, &dwarf_reporter, - eh_frame); - parser.Start(); - return true; -} - -// A LoadCommandHandler that loads whatever debugging data it finds into a -// Module. -class DumpSymbols::LoadCommandDumper: - public mach_o::Reader::LoadCommandHandler { - public: - // Create a load command dumper handling load commands from READER's - // file, and adding data to MODULE. - LoadCommandDumper(const DumpSymbols &dumper, - google_breakpad::Module *module, - const mach_o::Reader &reader, - SymbolData symbol_data, - bool handle_inter_cu_refs) - : dumper_(dumper), - module_(module), - reader_(reader), - symbol_data_(symbol_data), - handle_inter_cu_refs_(handle_inter_cu_refs) { } - - bool SegmentCommand(const mach_o::Segment &segment); - bool SymtabCommand(const ByteBuffer &entries, const ByteBuffer &strings); - - private: - const DumpSymbols &dumper_; - google_breakpad::Module *module_; // WEAK - const mach_o::Reader &reader_; - const SymbolData symbol_data_; - const bool handle_inter_cu_refs_; -}; - -bool DumpSymbols::LoadCommandDumper::SegmentCommand(const Segment &segment) { - mach_o::SectionMap section_map; - if (!reader_.MapSegmentSections(segment, §ion_map)) - return false; - - if (segment.name == "__TEXT") { - module_->SetLoadAddress(segment.vmaddr); - if (symbol_data_ != NO_CFI) { - mach_o::SectionMap::const_iterator eh_frame = - section_map.find("__eh_frame"); - if (eh_frame != section_map.end()) { - // If there is a problem reading this, don't treat it as a fatal error. - dumper_.ReadCFI(module_, reader_, eh_frame->second, true); - } - } - return true; - } - - if (segment.name == "__DWARF") { - if (symbol_data_ != ONLY_CFI) { - if (!dumper_.ReadDwarf(module_, reader_, section_map, - handle_inter_cu_refs_)) { - return false; - } - } - if (symbol_data_ != NO_CFI) { - mach_o::SectionMap::const_iterator debug_frame - = section_map.find("__debug_frame"); - if (debug_frame != section_map.end()) { - // If there is a problem reading this, don't treat it as a fatal error. - dumper_.ReadCFI(module_, reader_, debug_frame->second, false); - } - } - } - - return true; -} - -bool DumpSymbols::LoadCommandDumper::SymtabCommand(const ByteBuffer &entries, - const ByteBuffer &strings) { - StabsToModule stabs_to_module(module_); - // Mac OS X STABS are never "unitized", and the size of the 'value' field - // matches the address size of the executable. - StabsReader stabs_reader(entries.start, entries.Size(), - strings.start, strings.Size(), - reader_.big_endian(), - reader_.bits_64() ? 8 : 4, - true, - &stabs_to_module); - if (!stabs_reader.Process()) - return false; - stabs_to_module.Finalize(); - return true; -} - -bool DumpSymbols::ReadSymbolData(Module** out_module) { - // Select an object file, if SetArchitecture hasn't been called to set one - // explicitly. - if (!selected_object_file_) { - // If there's only one architecture, that's the one. - if (object_files_.size() == 1) - selected_object_file_ = &object_files_[0]; - else { - // Look for an object file whose architecture matches our own. - const NXArchInfo *local_arch = NXGetLocalArchInfo(); - if (!SetArchitecture(local_arch->cputype, local_arch->cpusubtype)) { - fprintf(stderr, "%s: object file contains more than one" - " architecture, none of which match the current" - " architecture; specify an architecture explicitly" - " with '-a ARCH' to resolve the ambiguity\n", - [object_filename_ fileSystemRepresentation]); - return false; - } - } - } - - assert(selected_object_file_); - - // Find the name of the selected file's architecture, to appear in - // the MODULE record and in error messages. - const NXArchInfo *selected_arch_info = - google_breakpad::BreakpadGetArchInfoFromCpuType( - selected_object_file_->cputype, selected_object_file_->cpusubtype); - - const char *selected_arch_name = selected_arch_info->name; - if (strcmp(selected_arch_name, "i386") == 0) - selected_arch_name = "x86"; - - // Produce a name to use in error messages that includes the - // filename, and the architecture, if there is more than one. - selected_object_name_ = [object_filename_ UTF8String]; - if (object_files_.size() > 1) { - selected_object_name_ += ", architecture "; - selected_object_name_ + selected_arch_name; - } - - // Compute a module name, to appear in the MODULE record. - NSString *module_name = [object_filename_ lastPathComponent]; - - // Choose an identifier string, to appear in the MODULE record. - string identifier = Identifier(); - if (identifier.empty()) - return false; - identifier += "0"; - - // Create a module to hold the debugging information. - scoped_ptr module(new Module([module_name UTF8String], - "mac", - selected_arch_name, - identifier)); - - // Parse the selected object file. - mach_o::Reader::Reporter reporter(selected_object_name_); - mach_o::Reader reader(&reporter); - if (!reader.Read(reinterpret_cast([contents_ bytes]) - + selected_object_file_->offset, - selected_object_file_->size, - selected_object_file_->cputype, - selected_object_file_->cpusubtype)) - return false; - - // Walk its load commands, and deal with whatever is there. - LoadCommandDumper load_command_dumper(*this, module.get(), reader, - symbol_data_, handle_inter_cu_refs_); - if (!reader.WalkLoadCommands(&load_command_dumper)) - return false; - - *out_module = module.release(); - - return true; -} - -bool DumpSymbols::WriteSymbolFile(std::ostream &stream) { - Module* module = NULL; - - if (ReadSymbolData(&module) && module) { - bool res = module->Write(stream, symbol_data_); - delete module; - return res; - } - - return false; -} - -} // namespace google_breakpad diff --git a/src/common/mac/file_id.cc b/src/common/mac/file_id.cc index c5d7ed56..4661d5d6 100644 --- a/src/common/mac/file_id.cc +++ b/src/common/mac/file_id.cc @@ -34,6 +34,7 @@ // Author: Dan Waylonis #include +#include #include #include @@ -45,7 +46,7 @@ using MacFileUtilities::MachoID; namespace google_breakpad { FileID::FileID(const char *path) { - strlcpy(path_, path, sizeof(path_)); + snprintf(path_, sizeof(path_), "%s", path); } bool FileID::FileIdentifier(unsigned char identifier[16]) { diff --git a/src/common/mac/macho_id.cc b/src/common/mac/macho_id.cc index 09a2a82e..c396ad88 100644 --- a/src/common/mac/macho_id.cc +++ b/src/common/mac/macho_id.cc @@ -33,17 +33,15 @@ // // Author: Dan Waylonis -extern "C" { // necessary for Leopard - #include - #include - #include - #include - #include - #include - #include - #include - #include -} + +#include +#include +#include +#include +#include +#include +#include +#include #include "common/mac/macho_id.h" #include "common/mac/macho_walker.h" @@ -61,7 +59,7 @@ MachoID::MachoID(const char *path) crc_(0), md5_context_(), update_function_(NULL) { - strlcpy(path_, path, sizeof(path_)); + snprintf(path_, sizeof(path_), "%s", path); } MachoID::MachoID(const char *path, void *memory, size_t size) @@ -70,7 +68,7 @@ MachoID::MachoID(const char *path, void *memory, size_t size) crc_(0), md5_context_(), update_function_(NULL) { - strlcpy(path_, path, sizeof(path_)); + snprintf(path_, sizeof(path_), "%s", path); } MachoID::~MachoID() { @@ -261,7 +259,7 @@ bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - swap_segment_command(&seg, NXHostByteOrder()); + breakpad_swap_segment_command(&seg); struct mach_header_64 header; off_t header_offset; @@ -278,7 +276,7 @@ bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - swap_section(&sec, 1, NXHostByteOrder()); + breakpad_swap_section(&sec, 1); // sections of type S_ZEROFILL are "virtual" and contain no data // in the file itself @@ -294,7 +292,7 @@ bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - breakpad_swap_segment_command_64(&seg64, NXHostByteOrder()); + breakpad_swap_segment_command_64(&seg64); struct mach_header_64 header; off_t header_offset; @@ -311,7 +309,7 @@ bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - breakpad_swap_section_64(&sec64, 1, NXHostByteOrder()); + breakpad_swap_section_64(&sec64, 1); // sections of type S_ZEROFILL are "virtual" and contain no data // in the file itself @@ -340,7 +338,7 @@ bool MachoID::UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - breakpad_swap_uuid_command(uuid_cmd, NXHostByteOrder()); + breakpad_swap_uuid_command(uuid_cmd); return false; } @@ -359,7 +357,7 @@ bool MachoID::IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, return false; if (swap) - swap_dylib_command(dylib_cmd, NXHostByteOrder()); + breakpad_swap_dylib_command(dylib_cmd); return false; } diff --git a/src/common/mac/macho_reader_unittest.cc b/src/common/mac/macho_reader_unittest.cc index 9bc6d25a..8ceab14b 100644 --- a/src/common/mac/macho_reader_unittest.cc +++ b/src/common/mac/macho_reader_unittest.cc @@ -196,7 +196,7 @@ struct FatReaderFixture { FatReaderFixture() : fat(kBigEndian), reporter("reporter filename"), - reader(&reporter), object_files(), object_files_size() { + reader(&reporter), object_files() { EXPECT_CALL(reporter, BadHeader()).Times(0); EXPECT_CALL(reporter, TooShort()).Times(0); @@ -224,7 +224,12 @@ struct FatReaderFixture { fat_bytes = reinterpret_cast(contents.data()); if (expect_parse_success) { EXPECT_TRUE(reader.Read(fat_bytes, contents.size())); - object_files = reader.object_files(&object_files_size); + size_t fat_files_count; + const SuperFatArch* fat_files = reader.object_files(&fat_files_count); + object_files.resize(fat_files_count); + for (size_t i = 0; i < fat_files_count; ++i) { + EXPECT_TRUE(fat_files[i].ConvertToFatArch(&object_files[i])); + } } else EXPECT_FALSE(reader.Read(fat_bytes, contents.size())); @@ -234,8 +239,7 @@ struct FatReaderFixture { FatReader reader; string contents; const uint8_t *fat_bytes; - const struct fat_arch *object_files; - size_t object_files_size; + vector object_files; }; class FatReaderTest: public FatReaderFixture, public Test { }; @@ -289,7 +293,7 @@ TEST_F(FatReaderTest, NoObjectFiles) { .B32(0xcafebabe) // magic number .B32(0); // number of architectures ReadFat(); - EXPECT_EQ(0U, object_files_size); + EXPECT_EQ(0U, object_files.size()); } TEST_F(FatReaderTest, OneObjectFile) { @@ -304,7 +308,7 @@ TEST_F(FatReaderTest, OneObjectFile) { .Mark(&obj1_offset) .Append(0x42, '*'); // dummy contents ReadFat(); - ASSERT_EQ(1U, object_files_size); + ASSERT_EQ(1U, object_files.size()); EXPECT_EQ(0x5e3a6e91, object_files[0].cputype); EXPECT_EQ(0x52ccd852, object_files[0].cpusubtype); EXPECT_EQ(obj1_offset.Value(), object_files[0].offset); @@ -334,7 +338,7 @@ TEST_F(FatReaderTest, ThreeObjectFiles) { ReadFat(); - ASSERT_EQ(3U, object_files_size); + ASSERT_EQ(3U, object_files.size()); // First object file. EXPECT_EQ(0x0cb92c30, object_files[0].cputype); @@ -373,7 +377,7 @@ TEST_F(FatReaderTest, BigEndianMachO32) { // FatReader should treat a Mach-O file as if it were a fat binary file // containing one object file --- the whole thing. - ASSERT_EQ(1U, object_files_size); + ASSERT_EQ(1U, object_files.size()); EXPECT_EQ(0x1a9d0518, object_files[0].cputype); EXPECT_EQ(0x1b779357, object_files[0].cpusubtype); EXPECT_EQ(0U, object_files[0].offset); @@ -395,7 +399,7 @@ TEST_F(FatReaderTest, BigEndianMachO64) { // FatReader should treat a Mach-O file as if it were a fat binary file // containing one object file --- the whole thing. - ASSERT_EQ(1U, object_files_size); + ASSERT_EQ(1U, object_files.size()); EXPECT_EQ(0x5aff8487, object_files[0].cputype); EXPECT_EQ(0x4c6a57f7, object_files[0].cpusubtype); EXPECT_EQ(0U, object_files[0].offset); @@ -417,7 +421,7 @@ TEST_F(FatReaderTest, LittleEndianMachO32) { // FatReader should treat a Mach-O file as if it were a fat binary file // containing one object file --- the whole thing. - ASSERT_EQ(1U, object_files_size); + ASSERT_EQ(1U, object_files.size()); EXPECT_EQ(0x1a9d0518, object_files[0].cputype); EXPECT_EQ(0x1b779357, object_files[0].cpusubtype); EXPECT_EQ(0U, object_files[0].offset); @@ -439,7 +443,7 @@ TEST_F(FatReaderTest, LittleEndianMachO64) { // FatReader should treat a Mach-O file as if it were a fat binary file // containing one object file --- the whole thing. - ASSERT_EQ(1U, object_files_size); + ASSERT_EQ(1U, object_files.size()); EXPECT_EQ(0x5aff8487, object_files[0].cputype); EXPECT_EQ(0x4c6a57f7, object_files[0].cpusubtype); EXPECT_EQ(0U, object_files[0].offset); diff --git a/src/common/mac/macho_utilities.cc b/src/common/mac/macho_utilities.cc index 89f9e775..f56fe768 100644 --- a/src/common/mac/macho_utilities.cc +++ b/src/common/mac/macho_utilities.cc @@ -34,16 +34,44 @@ #include "common/mac/byteswap.h" #include "common/mac/macho_utilities.h" -void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc, - enum NXByteOrder target_byte_order) -{ +#include +#include + +void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc) { uc->cmd = ByteSwap(uc->cmd); uc->cmdsize = ByteSwap(uc->cmdsize); } -void breakpad_swap_segment_command_64(struct segment_command_64 *sg, - enum NXByteOrder target_byte_order) -{ +void breakpad_swap_load_command(struct load_command *lc) { + lc->cmd = ByteSwap(lc->cmd); + lc->cmdsize = ByteSwap(lc->cmdsize); +} + +void breakpad_swap_dylib_command(struct dylib_command *dc) { + dc->cmd = ByteSwap(dc->cmd); + dc->cmdsize = ByteSwap(dc->cmdsize); + + dc->dylib.name.offset = ByteSwap(dc->dylib.name.offset); + dc->dylib.timestamp = ByteSwap(dc->dylib.timestamp); + dc->dylib.current_version = ByteSwap(dc->dylib.current_version); + dc->dylib.compatibility_version = ByteSwap(dc->dylib.compatibility_version); +} + +void breakpad_swap_segment_command(struct segment_command *sc) { + sc->cmd = ByteSwap(sc->cmd); + sc->cmdsize = ByteSwap(sc->cmdsize); + + sc->vmaddr = ByteSwap(sc->vmaddr); + sc->vmsize = ByteSwap(sc->vmsize); + sc->fileoff = ByteSwap(sc->fileoff); + sc->filesize = ByteSwap(sc->filesize); + sc->maxprot = ByteSwap(sc->maxprot); + sc->initprot = ByteSwap(sc->initprot); + sc->nsects = ByteSwap(sc->nsects); + sc->flags = ByteSwap(sc->flags); +} + +void breakpad_swap_segment_command_64(struct segment_command_64 *sg) { sg->cmd = ByteSwap(sg->cmd); sg->cmdsize = ByteSwap(sg->cmdsize); @@ -58,9 +86,32 @@ void breakpad_swap_segment_command_64(struct segment_command_64 *sg, sg->flags = ByteSwap(sg->flags); } -void breakpad_swap_mach_header_64(struct mach_header_64 *mh, - enum NXByteOrder target_byte_order) -{ +void breakpad_swap_fat_header(struct fat_header *fh) { + fh->magic = ByteSwap(fh->magic); + fh->nfat_arch = ByteSwap(fh->nfat_arch); +} + +void breakpad_swap_fat_arch(struct fat_arch *fa, uint32_t narchs) { + for (uint32_t i = 0; i < narchs; ++i) { + fa[i].cputype = ByteSwap(fa[i].cputype); + fa[i].cpusubtype = ByteSwap(fa[i].cpusubtype); + fa[i].offset = ByteSwap(fa[i].offset); + fa[i].size = ByteSwap(fa[i].size); + fa[i].align = ByteSwap(fa[i].align); + } +} + +void breakpad_swap_mach_header(struct mach_header *mh) { + mh->magic = ByteSwap(mh->magic); + mh->cputype = ByteSwap(mh->cputype); + mh->cpusubtype = ByteSwap(mh->cpusubtype); + mh->filetype = ByteSwap(mh->filetype); + mh->ncmds = ByteSwap(mh->ncmds); + mh->sizeofcmds = ByteSwap(mh->sizeofcmds); + mh->flags = ByteSwap(mh->flags); +} + +void breakpad_swap_mach_header_64(struct mach_header_64 *mh) { mh->magic = ByteSwap(mh->magic); mh->cputype = ByteSwap(mh->cputype); mh->cpusubtype = ByteSwap(mh->cpusubtype); @@ -71,10 +122,24 @@ void breakpad_swap_mach_header_64(struct mach_header_64 *mh, mh->reserved = ByteSwap(mh->reserved); } +void breakpad_swap_section(struct section *s, + uint32_t nsects) { + for (uint32_t i = 0; i < nsects; i++) { + s[i].addr = ByteSwap(s[i].addr); + s[i].size = ByteSwap(s[i].size); + + s[i].offset = ByteSwap(s[i].offset); + s[i].align = ByteSwap(s[i].align); + s[i].reloff = ByteSwap(s[i].reloff); + s[i].nreloc = ByteSwap(s[i].nreloc); + s[i].flags = ByteSwap(s[i].flags); + s[i].reserved1 = ByteSwap(s[i].reserved1); + s[i].reserved2 = ByteSwap(s[i].reserved2); + } +} + void breakpad_swap_section_64(struct section_64 *s, - uint32_t nsects, - enum NXByteOrder target_byte_order) -{ + uint32_t nsects) { for (uint32_t i = 0; i < nsects; i++) { s[i].addr = ByteSwap(s[i].addr); s[i].size = ByteSwap(s[i].size); diff --git a/src/common/mac/macho_utilities.h b/src/common/mac/macho_utilities.h index a200c0f7..00563a77 100644 --- a/src/common/mac/macho_utilities.h +++ b/src/common/mac/macho_utilities.h @@ -62,23 +62,34 @@ struct breakpad_uuid_command { uint8_t uuid[16]; /* the 128-bit uuid */ }; -void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc, - enum NXByteOrder target_byte_order); +void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc); + +void breakpad_swap_load_command(struct load_command *lc); + +void breakpad_swap_dylib_command(struct dylib_command *dc); // Older SDKs defines thread_state_data_t as an int[] instead // of the natural_t[] it should be. typedef natural_t breakpad_thread_state_data_t[THREAD_STATE_MAX]; +void breakpad_swap_segment_command(struct segment_command *sc); + // The 64-bit swap routines were added during the 10.4 series, their // presence isn't guaranteed. -void breakpad_swap_segment_command_64(struct segment_command_64 *sg, - enum NXByteOrder target_byte_order); +void breakpad_swap_segment_command_64(struct segment_command_64 *sg); + +void breakpad_swap_fat_header(struct fat_header *fh); + +void breakpad_swap_fat_arch(struct fat_arch *fa, uint32_t narchs); + +void breakpad_swap_mach_header(struct mach_header *mh); + +void breakpad_swap_mach_header_64(struct mach_header_64 *mh); -void breakpad_swap_mach_header_64(struct mach_header_64 *mh, - enum NXByteOrder target_byte_order); +void breakpad_swap_section(struct section *s, + uint32_t nsects); void breakpad_swap_section_64(struct section_64 *s, - uint32_t nsects, - enum NXByteOrder target_byte_order); + uint32_t nsects); #endif diff --git a/src/common/mac/macho_walker.cc b/src/common/mac/macho_walker.cc index eee27d64..1acd8665 100644 --- a/src/common/mac/macho_walker.cc +++ b/src/common/mac/macho_walker.cc @@ -33,15 +33,13 @@ // // Author: Dan Waylonis -extern "C" { // necessary for Leopard - #include - #include - #include - #include - #include - #include - #include -} +#include +#include +#include +#include +#include +#include +#include #include "common/mac/byteswap.h" #include "common/mac/macho_walker.h" @@ -156,7 +154,7 @@ bool MachoWalker::FindHeader(cpu_type_t cpu_type, return false; if (magic == MH_CIGAM || magic == MH_CIGAM_64) - swap_mach_header(&header, NXHostByteOrder()); + breakpad_swap_mach_header(&header); if (cpu_type != header.cputype || (cpu_subtype != CPU_SUBTYPE_MULTIPLE && @@ -174,7 +172,7 @@ bool MachoWalker::FindHeader(cpu_type_t cpu_type, return false; if (NXHostByteOrder() != NX_BigEndian) - swap_fat_header(&fat, NXHostByteOrder()); + breakpad_swap_fat_header(&fat); offset += sizeof(fat); @@ -185,7 +183,7 @@ bool MachoWalker::FindHeader(cpu_type_t cpu_type, return false; if (NXHostByteOrder() != NX_BigEndian) - swap_fat_arch(&arch, 1, NXHostByteOrder()); + breakpad_swap_fat_arch(&arch, 1); if (arch.cputype == cpu_type && (cpu_subtype == CPU_SUBTYPE_MULTIPLE || @@ -208,7 +206,7 @@ bool MachoWalker::WalkHeaderAtOffset(off_t offset) { bool swap = (header.magic == MH_CIGAM); if (swap) - swap_mach_header(&header, NXHostByteOrder()); + breakpad_swap_mach_header(&header); // Copy the data into the mach_header_64 structure. Since the 32-bit and // 64-bit only differ in the last field (reserved), this is safe to do. @@ -234,7 +232,7 @@ bool MachoWalker::WalkHeader64AtOffset(off_t offset) { bool swap = (header.magic == MH_CIGAM_64); if (swap) - breakpad_swap_mach_header_64(&header, NXHostByteOrder()); + breakpad_swap_mach_header_64(&header); current_header_ = &header; current_header_size_ = sizeof(header); @@ -255,7 +253,7 @@ bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, return false; if (swap) - swap_load_command(&cmd, NXHostByteOrder()); + breakpad_swap_load_command(&cmd); // Call the user callback if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) -- cgit v1.2.1