1 // Copyright (c) 2010, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31 
32 // macho_dump.cc: Dump the contents of a Mach-O file. This is mostly
33 // a test program for the Mach_O::FatReader and Mach_O::Reader classes.
34 
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <libgen.h>
38 #include <mach-o/arch.h>
39 #include <sys/mman.h>
40 #include <stdint.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 
45 #include <sstream>
46 #include <string>
47 #include <vector>
48 
49 #include "common/byte_cursor.h"
50 #include "common/mac/macho_reader.h"
51 
52 using google_breakpad::ByteBuffer;
53 using std::ostringstream;
54 using std::string;
55 using std::vector;
56 
57 namespace {
58 namespace mach_o = google_breakpad::mach_o;
59 
60 string program_name;
61 
check_syscall(int result,const char * operation,const char * filename)62 int check_syscall(int result, const char *operation, const char *filename) {
63   if (result < 0) {
64     fprintf(stderr, "%s: %s '%s': %s\n",
65             program_name.c_str(), operation,
66             filename, strerror(errno));
67     exit(1);
68   }
69   return result;
70 }
71 
72 class DumpSection: public mach_o::Reader::SectionHandler {
73  public:
DumpSection()74   DumpSection() : index_(0) { }
HandleSection(const mach_o::Section & section)75   bool HandleSection(const mach_o::Section &section) {
76     printf("        section %d '%s' in segment '%s'\n"
77            "          address: 0x%llx\n"
78            "          alignment: 1 << %d B\n"
79            "          flags: %d\n"
80            "          size: %ld\n",
81            index_++, section.section_name.c_str(), section.segment_name.c_str(),
82            section.address, section.align,
83            mach_o::SectionFlags(section.flags),
84            section.contents.Size());
85     return true;
86   }
87 
88  private:
89   int index_;
90 };
91 
92 class DumpCommand: public mach_o::Reader::LoadCommandHandler {
93  public:
DumpCommand(mach_o::Reader * reader)94   DumpCommand(mach_o::Reader *reader) : reader_(reader), index_(0) { }
UnknownCommand(mach_o::LoadCommandType type,const ByteBuffer & contents)95   bool UnknownCommand(mach_o::LoadCommandType type,
96                       const ByteBuffer &contents) {
97     printf("      load command %d: %d", index_++, type);
98     return true;
99   }
SegmentCommand(const mach_o::Segment & segment)100   bool SegmentCommand(const mach_o::Segment &segment) {
101     printf("      load command %d: %s-bit segment '%s'\n"
102            "        address: 0x%llx\n"
103            "        memory size: 0x%llx\n"
104            "        maximum protection: 0x%x\n"
105            "        initial protection: 0x%x\n"
106            "        flags: %d\n"
107            "        section_list size: %ld B\n",
108            index_++, (segment.bits_64 ? "64" : "32"), segment.name.c_str(),
109            segment.vmaddr, segment.vmsize, segment.maxprot,
110            segment.initprot, mach_o::SegmentFlags(segment.flags),
111            segment.section_list.Size());
112 
113     DumpSection dump_section;
114     return reader_->WalkSegmentSections(segment, &dump_section);
115   }
116  private:
117   mach_o::Reader *reader_;
118   int index_;
119 };
120 
DumpFile(const char * filename)121 void DumpFile(const char *filename) {
122   int fd = check_syscall(open(filename, O_RDONLY), "opening", filename);
123   struct stat attributes;
124   check_syscall(fstat(fd, &attributes),
125                 "getting file attributes for", filename);
126   void *mapping = mmap(NULL, attributes.st_size, PROT_READ,
127                        MAP_PRIVATE, fd, 0);
128   close(fd);
129   check_syscall(mapping == (void *)-1 ? -1 : 0,
130                 "mapping contents of", filename);
131 
132   mach_o::FatReader::Reporter fat_reporter(filename);
133   mach_o::FatReader fat_reader(&fat_reporter);
134   if (!fat_reader.Read(reinterpret_cast<uint8_t *>(mapping),
135                        attributes.st_size)) {
136     exit(1);
137   }
138   printf("filename: %s\n", filename);
139   size_t object_files_size;
140   const struct fat_arch *object_files
141     = fat_reader.object_files(&object_files_size);
142   printf("  object file count: %ld\n", object_files_size);
143   for (size_t i = 0; i < object_files_size; i++) {
144     const struct fat_arch &file = object_files[i];
145     const NXArchInfo *fat_arch_info
146       = NXGetArchInfoFromCpuType(file.cputype, file.cpusubtype);
147     printf("\n  object file %ld:\n"
148            "    fat header:\n:"
149            "      CPU type: %s (%s)\n"
150            "      size: %d B\n"
151            "      alignment: 1<<%d B\n",
152            i, fat_arch_info->name, fat_arch_info->description,
153            file.size, file.align);
154 
155     ostringstream name;
156     name << filename;
157     if (object_files_size > 1)
158       name << ", object file #" << i;
159     ByteBuffer file_contents(reinterpret_cast<uint8_t *>(mapping)
160                              + file.offset, file.size);
161     mach_o::Reader::Reporter reporter(name.str());
162     mach_o::Reader reader(&reporter);
163     if (!reader.Read(file_contents, file.cputype, file.cpusubtype)) {
164       exit(1);
165     }
166 
167     const NXArchInfo *macho_arch_info =
168       NXGetArchInfoFromCpuType(reader.cpu_type(),
169                                reader.cpu_subtype());
170     printf("    Mach-O header:\n"
171            "      word size: %s\n"
172            "      CPU type: %s (%s)\n"
173            "      File type: %d\n"
174            "      flags: %x\n",
175            (reader.bits_64() ? "64 bits" : "32 bits"),
176            macho_arch_info->name, macho_arch_info->description,
177            reader.file_type(), reader.flags());
178 
179     DumpCommand dump_command(&reader);
180     reader.WalkLoadCommands(&dump_command);
181   }
182   munmap(mapping, attributes.st_size);
183 }
184 
185 }  // namespace
186 
main(int argc,char ** argv)187 int main(int argc, char **argv) {
188   program_name = basename(argv[0]);
189   if (argc == 1) {
190     fprintf(stderr, "Usage: %s FILE ...\n"
191             "Dump the contents of the Mach-O or fat binary files "
192             "'FILE ...'.\n", program_name.c_str());
193   }
194   for (int i = 1; i < argc; i++) {
195     DumpFile(argv[i]);
196   }
197 }
198