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 
breakpad_swap_uuid_command(struct breakpad_uuid_command * uc,enum NXByteOrder target_byte_order)37 void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc,
38                                 enum NXByteOrder target_byte_order)
39 {
40   uc->cmd = ByteSwap(uc->cmd);
41   uc->cmdsize = ByteSwap(uc->cmdsize);
42 }
43 
breakpad_swap_segment_command_64(struct segment_command_64 * sg,enum NXByteOrder target_byte_order)44 void breakpad_swap_segment_command_64(struct segment_command_64 *sg,
45                                       enum NXByteOrder target_byte_order)
46 {
47   sg->cmd = ByteSwap(sg->cmd);
48   sg->cmdsize = ByteSwap(sg->cmdsize);
49 
50   sg->vmaddr = ByteSwap(sg->vmaddr);
51   sg->vmsize = ByteSwap(sg->vmsize);
52   sg->fileoff = ByteSwap(sg->fileoff);
53   sg->filesize = ByteSwap(sg->filesize);
54 
55   sg->maxprot = ByteSwap(sg->maxprot);
56   sg->initprot = ByteSwap(sg->initprot);
57   sg->nsects = ByteSwap(sg->nsects);
58   sg->flags = ByteSwap(sg->flags);
59 }
60 
breakpad_swap_mach_header_64(struct mach_header_64 * mh,enum NXByteOrder target_byte_order)61 void breakpad_swap_mach_header_64(struct mach_header_64 *mh,
62                                   enum NXByteOrder target_byte_order)
63 {
64   mh->magic = ByteSwap(mh->magic);
65   mh->cputype = ByteSwap(mh->cputype);
66   mh->cpusubtype = ByteSwap(mh->cpusubtype);
67   mh->filetype = ByteSwap(mh->filetype);
68   mh->ncmds = ByteSwap(mh->ncmds);
69   mh->sizeofcmds = ByteSwap(mh->sizeofcmds);
70   mh->flags = ByteSwap(mh->flags);
71   mh->reserved = ByteSwap(mh->reserved);
72 }
73 
breakpad_swap_section_64(struct section_64 * s,uint32_t nsects,enum NXByteOrder target_byte_order)74 void breakpad_swap_section_64(struct section_64 *s,
75                               uint32_t nsects,
76                               enum NXByteOrder target_byte_order)
77 {
78   for (uint32_t i = 0; i < nsects; i++) {
79     s[i].addr = ByteSwap(s[i].addr);
80     s[i].size = ByteSwap(s[i].size);
81 
82     s[i].offset = ByteSwap(s[i].offset);
83     s[i].align = ByteSwap(s[i].align);
84     s[i].reloff = ByteSwap(s[i].reloff);
85     s[i].nreloc = ByteSwap(s[i].nreloc);
86     s[i].flags = ByteSwap(s[i].flags);
87     s[i].reserved1 = ByteSwap(s[i].reserved1);
88     s[i].reserved2 = ByteSwap(s[i].reserved2);
89   }
90 }
91