1 // Copyright (c) 2006, 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 // macho_utilties.cc: Utilities for dealing with mach-o files
31 //
32 // Author: Dave Camp
33 
34 #include "common/mac/byteswap.h"
35 #include "common/mac/macho_utilities.h"
36 
37 #include <mach-o/fat.h>
38 #include <mach-o/loader.h>
39 
breakpad_swap_uuid_command(struct breakpad_uuid_command * uc)40 void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc) {
41   uc->cmd = ByteSwap(uc->cmd);
42   uc->cmdsize = ByteSwap(uc->cmdsize);
43 }
44 
breakpad_swap_load_command(struct load_command * lc)45 void breakpad_swap_load_command(struct load_command *lc) {
46   lc->cmd = ByteSwap(lc->cmd);
47   lc->cmdsize = ByteSwap(lc->cmdsize);
48 }
49 
breakpad_swap_dylib_command(struct dylib_command * dc)50 void breakpad_swap_dylib_command(struct dylib_command *dc) {
51   dc->cmd = ByteSwap(dc->cmd);
52   dc->cmdsize = ByteSwap(dc->cmdsize);
53 
54   dc->dylib.name.offset = ByteSwap(dc->dylib.name.offset);
55   dc->dylib.timestamp = ByteSwap(dc->dylib.timestamp);
56   dc->dylib.current_version = ByteSwap(dc->dylib.current_version);
57   dc->dylib.compatibility_version = ByteSwap(dc->dylib.compatibility_version);
58 }
59 
breakpad_swap_segment_command(struct segment_command * sc)60 void breakpad_swap_segment_command(struct segment_command *sc) {
61   sc->cmd = ByteSwap(sc->cmd);
62   sc->cmdsize = ByteSwap(sc->cmdsize);
63 
64   sc->vmaddr = ByteSwap(sc->vmaddr);
65   sc->vmsize = ByteSwap(sc->vmsize);
66   sc->fileoff = ByteSwap(sc->fileoff);
67   sc->filesize = ByteSwap(sc->filesize);
68   sc->maxprot = ByteSwap(sc->maxprot);
69   sc->initprot = ByteSwap(sc->initprot);
70   sc->nsects = ByteSwap(sc->nsects);
71   sc->flags = ByteSwap(sc->flags);
72 }
73 
breakpad_swap_segment_command_64(struct segment_command_64 * sg)74 void breakpad_swap_segment_command_64(struct segment_command_64 *sg) {
75   sg->cmd = ByteSwap(sg->cmd);
76   sg->cmdsize = ByteSwap(sg->cmdsize);
77 
78   sg->vmaddr = ByteSwap(sg->vmaddr);
79   sg->vmsize = ByteSwap(sg->vmsize);
80   sg->fileoff = ByteSwap(sg->fileoff);
81   sg->filesize = ByteSwap(sg->filesize);
82 
83   sg->maxprot = ByteSwap(sg->maxprot);
84   sg->initprot = ByteSwap(sg->initprot);
85   sg->nsects = ByteSwap(sg->nsects);
86   sg->flags = ByteSwap(sg->flags);
87 }
88 
breakpad_swap_fat_header(struct fat_header * fh)89 void breakpad_swap_fat_header(struct fat_header *fh) {
90   fh->magic = ByteSwap(fh->magic);
91   fh->nfat_arch = ByteSwap(fh->nfat_arch);
92 }
93 
breakpad_swap_fat_arch(struct fat_arch * fa,uint32_t narchs)94 void breakpad_swap_fat_arch(struct fat_arch *fa, uint32_t narchs) {
95   for (uint32_t i = 0; i < narchs; ++i) {
96     fa[i].cputype = ByteSwap(fa[i].cputype);
97     fa[i].cpusubtype = ByteSwap(fa[i].cpusubtype);
98     fa[i].offset = ByteSwap(fa[i].offset);
99     fa[i].size = ByteSwap(fa[i].size);
100     fa[i].align = ByteSwap(fa[i].align);
101   }
102 }
103 
breakpad_swap_mach_header(struct mach_header * mh)104 void breakpad_swap_mach_header(struct mach_header *mh) {
105   mh->magic = ByteSwap(mh->magic);
106   mh->cputype = ByteSwap(mh->cputype);
107   mh->cpusubtype = ByteSwap(mh->cpusubtype);
108   mh->filetype = ByteSwap(mh->filetype);
109   mh->ncmds = ByteSwap(mh->ncmds);
110   mh->sizeofcmds = ByteSwap(mh->sizeofcmds);
111   mh->flags = ByteSwap(mh->flags);
112 }
113 
breakpad_swap_mach_header_64(struct mach_header_64 * mh)114 void breakpad_swap_mach_header_64(struct mach_header_64 *mh) {
115   mh->magic = ByteSwap(mh->magic);
116   mh->cputype = ByteSwap(mh->cputype);
117   mh->cpusubtype = ByteSwap(mh->cpusubtype);
118   mh->filetype = ByteSwap(mh->filetype);
119   mh->ncmds = ByteSwap(mh->ncmds);
120   mh->sizeofcmds = ByteSwap(mh->sizeofcmds);
121   mh->flags = ByteSwap(mh->flags);
122   mh->reserved = ByteSwap(mh->reserved);
123 }
124 
breakpad_swap_section(struct section * s,uint32_t nsects)125 void breakpad_swap_section(struct section *s,
126                            uint32_t nsects) {
127   for (uint32_t i = 0; i < nsects; i++) {
128     s[i].addr = ByteSwap(s[i].addr);
129     s[i].size = ByteSwap(s[i].size);
130 
131     s[i].offset = ByteSwap(s[i].offset);
132     s[i].align = ByteSwap(s[i].align);
133     s[i].reloff = ByteSwap(s[i].reloff);
134     s[i].nreloc = ByteSwap(s[i].nreloc);
135     s[i].flags = ByteSwap(s[i].flags);
136     s[i].reserved1 = ByteSwap(s[i].reserved1);
137     s[i].reserved2 = ByteSwap(s[i].reserved2);
138   }
139 }
140 
breakpad_swap_section_64(struct section_64 * s,uint32_t nsects)141 void breakpad_swap_section_64(struct section_64 *s,
142                               uint32_t nsects) {
143   for (uint32_t i = 0; i < nsects; i++) {
144     s[i].addr = ByteSwap(s[i].addr);
145     s[i].size = ByteSwap(s[i].size);
146 
147     s[i].offset = ByteSwap(s[i].offset);
148     s[i].align = ByteSwap(s[i].align);
149     s[i].reloff = ByteSwap(s[i].reloff);
150     s[i].nreloc = ByteSwap(s[i].nreloc);
151     s[i].flags = ByteSwap(s[i].flags);
152     s[i].reserved1 = ByteSwap(s[i].reserved1);
153     s[i].reserved2 = ByteSwap(s[i].reserved2);
154   }
155 }
156