From 35c41e00ee2cf9280fe0122c75877ba70b41bb46 Mon Sep 17 00:00:00 2001 From: "ted.mielczarek" Date: Fri, 25 Jun 2010 16:56:16 +0000 Subject: Breakpad Mac symbol dumper: Add new Mach-O reader class. This patch adds files defining new classes in the google_breakpad::Mach_O namespace for parsing fat binaries and Mach-O files. These are used in the new dumper to handle STABS debugging information, DWARF call frame information, and .eh_frame exception handling stack walking information. These new classes are independent of endianness and word size, and therefore can be used on binaries of all the relevant architectures: x86, x86_64, ppc, and ARM. The patch adds a complete set of unit tests for the new classes. A=jimb R=mark (http://breakpad.appspot.com/93001/show, http://breakpad.appspot.com/115001/show) git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@610 4c0a9323-5329-0410-9bdc-e9ce6186880e --- .../dump_syms/dump_syms.xcodeproj/project.pbxproj | 209 ++++++++++++++++++++- src/tools/mac/dump_syms/macho_dump.cc | 196 +++++++++++++++++++ 2 files changed, 399 insertions(+), 6 deletions(-) create mode 100644 src/tools/mac/dump_syms/macho_dump.cc (limited to 'src/tools') diff --git a/src/tools/mac/dump_syms/dump_syms.xcodeproj/project.pbxproj b/src/tools/mac/dump_syms/dump_syms.xcodeproj/project.pbxproj index d6b6d7e0..0c345928 100644 --- a/src/tools/mac/dump_syms/dump_syms.xcodeproj/project.pbxproj +++ b/src/tools/mac/dump_syms/dump_syms.xcodeproj/project.pbxproj @@ -7,7 +7,15 @@ objects = { /* Begin PBXBuildFile section */ + B88FAE0B11665B5700407530 /* test_assembler.cc in Sources */ = {isa = PBXBuildFile; fileRef = B88FAE0911665B5700407530 /* test_assembler.cc */; }; B89E0E601166556C00DD08C9 /* libcrypto.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B89E0E5F1166556C00DD08C9 /* libcrypto.dylib */; }; + B89E0E781166576C00DD08C9 /* macho_reader.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0E6E1166571D00DD08C9 /* macho_reader.cc */; }; + B89E0E7A1166576C00DD08C9 /* macho_dump.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0E701166573700DD08C9 /* macho_dump.cc */; }; + B89E0E9911665A7200DD08C9 /* macho_reader_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0E6D1166571D00DD08C9 /* macho_reader_unittest.cc */; }; + B89E0E9A11665A7200DD08C9 /* macho_reader.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0E6E1166571D00DD08C9 /* macho_reader.cc */; }; + B89E0EA111665AC300DD08C9 /* gtest_main.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0E9F11665AC300DD08C9 /* gtest_main.cc */; }; + B89E0EA211665AC300DD08C9 /* gtest-all.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0EA011665AC300DD08C9 /* gtest-all.cc */; }; + B89E0EA411665AEA00DD08C9 /* gmock-all.cc in Sources */ = {isa = PBXBuildFile; fileRef = B89E0EA311665AEA00DD08C9 /* gmock-all.cc */; }; B8C5B5161166534700D34F4E /* functioninfo.cc in Sources */ = {isa = PBXBuildFile; fileRef = F9C7ED420E8AD93000E953AD /* functioninfo.cc */; }; B8C5B5171166534700D34F4E /* dwarf2reader.cc in Sources */ = {isa = PBXBuildFile; fileRef = F95B422F0E0E22D100DBDE83 /* dwarf2reader.cc */; }; B8C5B5181166534700D34F4E /* bytereader.cc in Sources */ = {isa = PBXBuildFile; fileRef = F95B422C0E0E22D100DBDE83 /* bytereader.cc */; }; @@ -33,7 +41,18 @@ 9BE650440B52F6D800611104 /* macho_id.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = macho_id.h; path = ../../../common/mac/macho_id.h; sourceTree = SOURCE_ROOT; }; 9BE650450B52F6D800611104 /* macho_walker.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = macho_walker.cc; path = ../../../common/mac/macho_walker.cc; sourceTree = SOURCE_ROOT; }; 9BE650460B52F6D800611104 /* macho_walker.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = macho_walker.h; path = ../../../common/mac/macho_walker.h; sourceTree = SOURCE_ROOT; }; + B88FAE0911665B5700407530 /* test_assembler.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = test_assembler.cc; path = ../../../common/test_assembler.cc; sourceTree = SOURCE_ROOT; }; + B88FAE0A11665B5700407530 /* test_assembler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = test_assembler.h; path = ../../../common/test_assembler.h; sourceTree = SOURCE_ROOT; }; B89E0E5F1166556C00DD08C9 /* libcrypto.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcrypto.dylib; path = /usr/lib/libcrypto.dylib; sourceTree = ""; }; + B89E0E6D1166571D00DD08C9 /* macho_reader_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = macho_reader_unittest.cc; path = ../../../common/mac/macho_reader_unittest.cc; sourceTree = SOURCE_ROOT; }; + B89E0E6E1166571D00DD08C9 /* macho_reader.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = macho_reader.cc; path = ../../../common/mac/macho_reader.cc; sourceTree = SOURCE_ROOT; }; + B89E0E6F1166571D00DD08C9 /* macho_reader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = macho_reader.h; path = ../../../common/mac/macho_reader.h; sourceTree = SOURCE_ROOT; }; + B89E0E701166573700DD08C9 /* macho_dump.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = macho_dump.cc; sourceTree = ""; }; + B89E0E741166575200DD08C9 /* macho_dump */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = macho_dump; sourceTree = BUILT_PRODUCTS_DIR; }; + B89E0E9511665A6400DD08C9 /* macho_reader_unittest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = macho_reader_unittest; sourceTree = BUILT_PRODUCTS_DIR; }; + B89E0E9F11665AC300DD08C9 /* gtest_main.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = gtest_main.cc; path = ../../../testing/gtest/src/gtest_main.cc; sourceTree = SOURCE_ROOT; }; + B89E0EA011665AC300DD08C9 /* gtest-all.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "gtest-all.cc"; path = "../../../testing/gtest/src/gtest-all.cc"; sourceTree = SOURCE_ROOT; }; + B89E0EA311665AEA00DD08C9 /* gmock-all.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "gmock-all.cc"; path = "../../../testing/src/gmock-all.cc"; sourceTree = SOURCE_ROOT; }; B8C5B5111166531A00D34F4E /* dump_syms */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = dump_syms; sourceTree = BUILT_PRODUCTS_DIR; }; B8E8CA0C1156C854009E61B2 /* byteswap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = byteswap.h; path = ../../../common/mac/byteswap.h; sourceTree = SOURCE_ROOT; }; F95B422B0E0E22D100DBDE83 /* bytereader-inl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "bytereader-inl.h"; path = "../../../common/dwarf/bytereader-inl.h"; sourceTree = SOURCE_ROOT; }; @@ -48,6 +67,20 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + B89E0E721166575200DD08C9 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + B89E0E9311665A6400DD08C9 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; B8C5B50F1166531A00D34F4E /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -63,19 +96,16 @@ 08FB7794FE84155DC02AAC07 /* dump_syms */ = { isa = PBXGroup; children = ( + B89E0E9D11665A9500DD08C9 /* TESTING */, F9F5344B0E7C8FFC0012363F /* DWARF */, + B89E0E6C1166569700DD08C9 /* MACHO */, B8E8CA0C1156C854009E61B2 /* byteswap.h */, - 557800890BE1F3AB00EC23E0 /* macho_utilities.cc */, - 5578008A0BE1F3AB00EC23E0 /* macho_utilities.h */, 9BE650410B52F6D800611104 /* file_id.cc */, 9BE650420B52F6D800611104 /* file_id.h */, - 9BE650430B52F6D800611104 /* macho_id.cc */, - 9BE650440B52F6D800611104 /* macho_id.h */, - 9BE650450B52F6D800611104 /* macho_walker.cc */, - 9BE650460B52F6D800611104 /* macho_walker.h */, 9BDF186D0B1BB43700F8391B /* dump_syms.h */, 08FB7796FE84155DC02AAC07 /* dump_syms.mm */, 9BDF186E0B1BB43700F8391B /* dump_syms_tool.mm */, + B89E0E701166573700DD08C9 /* macho_dump.cc */, 08FB779DFE84155DC02AAC07 /* External Frameworks and Libraries */, 1AB674ADFE9D54B511CA2CBB /* Products */, ); @@ -95,10 +125,40 @@ isa = PBXGroup; children = ( B8C5B5111166531A00D34F4E /* dump_syms */, + B89E0E741166575200DD08C9 /* macho_dump */, + B89E0E9511665A6400DD08C9 /* macho_reader_unittest */, ); name = Products; sourceTree = ""; }; + B89E0E6C1166569700DD08C9 /* MACHO */ = { + isa = PBXGroup; + children = ( + B89E0E6D1166571D00DD08C9 /* macho_reader_unittest.cc */, + B89E0E6E1166571D00DD08C9 /* macho_reader.cc */, + B89E0E6F1166571D00DD08C9 /* macho_reader.h */, + 557800890BE1F3AB00EC23E0 /* macho_utilities.cc */, + 5578008A0BE1F3AB00EC23E0 /* macho_utilities.h */, + 9BE650430B52F6D800611104 /* macho_id.cc */, + 9BE650440B52F6D800611104 /* macho_id.h */, + 9BE650450B52F6D800611104 /* macho_walker.cc */, + 9BE650460B52F6D800611104 /* macho_walker.h */, + ); + name = MACHO; + sourceTree = ""; + }; + B89E0E9D11665A9500DD08C9 /* TESTING */ = { + isa = PBXGroup; + children = ( + B88FAE0911665B5700407530 /* test_assembler.cc */, + B88FAE0A11665B5700407530 /* test_assembler.h */, + B89E0EA311665AEA00DD08C9 /* gmock-all.cc */, + B89E0E9F11665AC300DD08C9 /* gtest_main.cc */, + B89E0EA011665AC300DD08C9 /* gtest-all.cc */, + ); + name = TESTING; + sourceTree = ""; + }; F9F5344B0E7C8FFC0012363F /* DWARF */ = { isa = PBXGroup; children = ( @@ -118,6 +178,38 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + B89E0E731166575200DD08C9 /* macho_dump */ = { + isa = PBXNativeTarget; + buildConfigurationList = B89E0E7F116657A100DD08C9 /* Build configuration list for PBXNativeTarget "macho_dump" */; + buildPhases = ( + B89E0E711166575200DD08C9 /* Sources */, + B89E0E721166575200DD08C9 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = macho_dump; + productName = macho_dump; + productReference = B89E0E741166575200DD08C9 /* macho_dump */; + productType = "com.apple.product-type.tool"; + }; + B89E0E9411665A6400DD08C9 /* macho_reader_unittest */ = { + isa = PBXNativeTarget; + buildConfigurationList = B89E0E9E11665A9600DD08C9 /* Build configuration list for PBXNativeTarget "macho_reader_unittest" */; + buildPhases = ( + B89E0E9211665A6400DD08C9 /* Sources */, + B89E0E9311665A6400DD08C9 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = macho_reader_unittest; + productName = macho_reader_unittest; + productReference = B89E0E9511665A6400DD08C9 /* macho_reader_unittest */; + productType = "com.apple.product-type.tool"; + }; B8C5B5101166531A00D34F4E /* dump_syms */ = { isa = PBXNativeTarget; buildConfigurationList = B8C5B5151166533900D34F4E /* Build configuration list for PBXNativeTarget "dump_syms" */; @@ -139,6 +231,9 @@ /* Begin PBXProject section */ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = NO; + }; buildConfigurationList = 1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "dump_syms" */; compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; @@ -147,11 +242,35 @@ projectRoot = ""; targets = ( B8C5B5101166531A00D34F4E /* dump_syms */, + B89E0E731166575200DD08C9 /* macho_dump */, + B89E0E9411665A6400DD08C9 /* macho_reader_unittest */, ); }; /* End PBXProject section */ /* Begin PBXSourcesBuildPhase section */ + B89E0E711166575200DD08C9 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B89E0E781166576C00DD08C9 /* macho_reader.cc in Sources */, + B89E0E7A1166576C00DD08C9 /* macho_dump.cc in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + B89E0E9211665A6400DD08C9 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B89E0E9911665A7200DD08C9 /* macho_reader_unittest.cc in Sources */, + B89E0E9A11665A7200DD08C9 /* macho_reader.cc in Sources */, + B89E0EA111665AC300DD08C9 /* gtest_main.cc in Sources */, + B89E0EA211665AC300DD08C9 /* gtest-all.cc in Sources */, + B89E0EA411665AEA00DD08C9 /* gmock-all.cc in Sources */, + B88FAE0B11665B5700407530 /* test_assembler.cc in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; B8C5B50E1166531A00D34F4E /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -192,6 +311,66 @@ }; name = Release; }; + B89E0E761166575300DD08C9 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_MODEL_TUNING = G5; + GCC_OPTIMIZATION_LEVEL = 0; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = macho_dump; + }; + name = Debug; + }; + B89E0E771166575300DD08C9 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_ENABLE_FIX_AND_CONTINUE = NO; + GCC_MODEL_TUNING = G5; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = macho_dump; + ZERO_LINK = NO; + }; + name = Release; + }; + B89E0E9711665A6400DD08C9 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_FIX_AND_CONTINUE = YES; + GCC_MODEL_TUNING = G5; + GCC_OPTIMIZATION_LEVEL = 0; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = macho_reader_unittest; + }; + name = Debug; + }; + B89E0E9811665A6400DD08C9 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_ENABLE_FIX_AND_CONTINUE = NO; + GCC_MODEL_TUNING = G5; + INSTALL_PATH = /usr/local/bin; + PREBINDING = NO; + PRODUCT_NAME = macho_reader_unittest; + ZERO_LINK = NO; + }; + name = Release; + }; B8C5B5131166531B00D34F4E /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -235,6 +414,24 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + B89E0E7F116657A100DD08C9 /* Build configuration list for PBXNativeTarget "macho_dump" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B89E0E761166575300DD08C9 /* Debug */, + B89E0E771166575300DD08C9 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + B89E0E9E11665A9600DD08C9 /* Build configuration list for PBXNativeTarget "macho_reader_unittest" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B89E0E9711665A6400DD08C9 /* Debug */, + B89E0E9811665A6400DD08C9 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; B8C5B5151166533900D34F4E /* Build configuration list for PBXNativeTarget "dump_syms" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/src/tools/mac/dump_syms/macho_dump.cc b/src/tools/mac/dump_syms/macho_dump.cc new file mode 100644 index 00000000..fcef37c5 --- /dev/null +++ b/src/tools/mac/dump_syms/macho_dump.cc @@ -0,0 +1,196 @@ +// Copyright (c) 2010, 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. + +// Original author: Jim Blandy + +// macho_dump.cc: Dump the contents of a Mach-O file. This is mostly +// a test program for the Mach_O::FatReader and Mach_O::Reader classes. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "common/byte_cursor.h" +#include "common/mac/macho_reader.h" + +using google_breakpad::ByteBuffer; +using std::ostringstream; +using std::string; +using std::vector; + +namespace { +namespace mach_o = google_breakpad::mach_o; + +string program_name; + +int check_syscall(int result, const char *operation, const char *filename) { + if (result < 0) { + fprintf(stderr, "%s: %s '%s': %s\n", + program_name.c_str(), operation, + filename, strerror(errno)); + exit(1); + } + return result; +} + +class DumpSection: public mach_o::Reader::SectionHandler { + public: + DumpSection() : index_(0) { } + bool HandleSection(const mach_o::Section §ion) { + printf(" section %d '%s' in segment '%s'\n" + " address: 0x%llx\n" + " alignment: 1 << %d B\n" + " flags: %d\n" + " size: %ld\n", + index_++, section.section_name.c_str(), section.segment_name.c_str(), + section.address, section.align, + mach_o::SectionFlags(section.flags), + section.contents.Size()); + return true; + } + + private: + int index_; +}; + +class DumpCommand: public mach_o::Reader::LoadCommandHandler { + public: + DumpCommand(mach_o::Reader *reader) : reader_(reader), index_(0) { } + bool UnknownCommand(mach_o::LoadCommandType type, + const ByteBuffer &contents) { + printf(" load command %d: %d", index_++, type); + return true; + } + bool SegmentCommand(const mach_o::Segment &segment) { + printf(" load command %d: %s-bit segment '%s'\n" + " address: 0x%llx\n" + " memory size: 0x%llx\n" + " maximum protection: 0x%x\n" + " initial protection: 0x%x\n" + " flags: %d\n" + " section_list size: %ld B\n", + index_++, (segment.bits_64 ? "64" : "32"), segment.name.c_str(), + segment.vmaddr, segment.vmsize, segment.maxprot, + segment.initprot, mach_o::SegmentFlags(segment.flags), + segment.section_list.Size()); + + DumpSection dump_section; + return reader_->WalkSegmentSections(segment, &dump_section); + } + private: + mach_o::Reader *reader_; + int index_; +}; + +void DumpFile(const char *filename) { + int fd = check_syscall(open(filename, O_RDONLY), "opening", filename); + struct stat attributes; + check_syscall(fstat(fd, &attributes), + "getting file attributes for", filename); + void *mapping = mmap(NULL, attributes.st_size, PROT_READ, + MAP_PRIVATE, fd, 0); + close(fd); + check_syscall(mapping == (void *)-1 ? -1 : 0, + "mapping contents of", filename); + + mach_o::FatReader::Reporter fat_reporter(filename); + mach_o::FatReader fat_reader(&fat_reporter); + if (!fat_reader.Read(reinterpret_cast(mapping), + attributes.st_size)) { + exit(1); + } + printf("filename: %s\n", filename); + size_t object_files_size; + struct fat_arch *object_files = fat_reader.object_files(&object_files_size); + printf(" object file count: %ld\n", object_files_size); + for (size_t i = 0; i < object_files_size; i++) { + const struct fat_arch &file = object_files[i]; + const NXArchInfo *fat_arch_info + = NXGetArchInfoFromCpuType(file.cputype, file.cpusubtype); + printf("\n object file %ld:\n" + " fat header:\n:" + " CPU type: %s (%s)\n" + " size: %d B\n" + " alignment: 1<<%d B\n", + i, fat_arch_info->name, fat_arch_info->description, + file.size, file.align); + + ostringstream name; + name << filename; + if (object_files_size > 1) + name << ", object file #" << i; + ByteBuffer file_contents(reinterpret_cast(mapping) + + file.offset, file.size); + mach_o::Reader::Reporter reporter(name.str()); + mach_o::Reader reader(&reporter); + if (!reader.Read(file_contents, file.cputype, file.cpusubtype)) { + exit(1); + } + + const NXArchInfo *macho_arch_info = + NXGetArchInfoFromCpuType(reader.cpu_type(), + reader.cpu_subtype()); + printf(" Mach-O header:\n" + " word size: %s\n" + " CPU type: %s (%s)\n" + " File type: %d\n" + " flags: %x\n", + (reader.bits_64() ? "64 bits" : "32 bits"), + macho_arch_info->name, macho_arch_info->description, + reader.file_type(), reader.flags()); + + DumpCommand dump_command(&reader); + reader.WalkLoadCommands(&dump_command); + } + munmap(mapping, attributes.st_size); +} + +} // namespace + +int main(int argc, char **argv) { + program_name = basename(argv[0]); + if (argc == 1) { + fprintf(stderr, "Usage: %s FILE ...\n" + "Dump the contents of the Mach-O or fat binary files " + "'FILE ...'.\n", program_name.c_str()); + } + for (int i = 1; i < argc; i++) { + DumpFile(argv[i]); + } +} -- cgit v1.2.1