macho_utilities.cc
1 // Copyright 2006 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // macho_utilties.cc: Utilities for dealing with mach-o files 30 // 31 // Author: Dave Camp 32 33 #ifdef HAVE_CONFIG_H 34 #include <config.h> // Must come first 35 #endif 36 37 #include "common/mac/byteswap.h" 38 #include "common/mac/macho_utilities.h" 39 40 #include <mach-o/fat.h> 41 #include <mach-o/loader.h> 42 43 void breakpad_swap_uuid_command(struct breakpad_uuid_command *uc) { 44 uc->cmd = ByteSwap(uc->cmd); 45 uc->cmdsize = ByteSwap(uc->cmdsize); 46 } 47 48 void breakpad_swap_load_command(struct load_command *lc) { 49 lc->cmd = ByteSwap(lc->cmd); 50 lc->cmdsize = ByteSwap(lc->cmdsize); 51 } 52 53 void breakpad_swap_dylib_command(struct dylib_command *dc) { 54 dc->cmd = ByteSwap(dc->cmd); 55 dc->cmdsize = ByteSwap(dc->cmdsize); 56 57 dc->dylib.name.offset = ByteSwap(dc->dylib.name.offset); 58 dc->dylib.timestamp = ByteSwap(dc->dylib.timestamp); 59 dc->dylib.current_version = ByteSwap(dc->dylib.current_version); 60 dc->dylib.compatibility_version = ByteSwap(dc->dylib.compatibility_version); 61 } 62 63 void breakpad_swap_segment_command(struct segment_command *sc) { 64 sc->cmd = ByteSwap(sc->cmd); 65 sc->cmdsize = ByteSwap(sc->cmdsize); 66 67 sc->vmaddr = ByteSwap(sc->vmaddr); 68 sc->vmsize = ByteSwap(sc->vmsize); 69 sc->fileoff = ByteSwap(sc->fileoff); 70 sc->filesize = ByteSwap(sc->filesize); 71 sc->maxprot = ByteSwap(sc->maxprot); 72 sc->initprot = ByteSwap(sc->initprot); 73 sc->nsects = ByteSwap(sc->nsects); 74 sc->flags = ByteSwap(sc->flags); 75 } 76 77 void breakpad_swap_segment_command_64(struct segment_command_64 *sg) { 78 sg->cmd = ByteSwap(sg->cmd); 79 sg->cmdsize = ByteSwap(sg->cmdsize); 80 81 sg->vmaddr = ByteSwap(sg->vmaddr); 82 sg->vmsize = ByteSwap(sg->vmsize); 83 sg->fileoff = ByteSwap(sg->fileoff); 84 sg->filesize = ByteSwap(sg->filesize); 85 86 sg->maxprot = ByteSwap(sg->maxprot); 87 sg->initprot = ByteSwap(sg->initprot); 88 sg->nsects = ByteSwap(sg->nsects); 89 sg->flags = ByteSwap(sg->flags); 90 } 91 92 void breakpad_swap_fat_header(struct fat_header *fh) { 93 fh->magic = ByteSwap(fh->magic); 94 fh->nfat_arch = ByteSwap(fh->nfat_arch); 95 } 96 97 void breakpad_swap_fat_arch(struct fat_arch *fa, uint32_t narchs) { 98 for (uint32_t i = 0; i < narchs; ++i) { 99 fa[i].cputype = ByteSwap(fa[i].cputype); 100 fa[i].cpusubtype = ByteSwap(fa[i].cpusubtype); 101 fa[i].offset = ByteSwap(fa[i].offset); 102 fa[i].size = ByteSwap(fa[i].size); 103 fa[i].align = ByteSwap(fa[i].align); 104 } 105 } 106 107 void breakpad_swap_mach_header(struct mach_header *mh) { 108 mh->magic = ByteSwap(mh->magic); 109 mh->cputype = ByteSwap(mh->cputype); 110 mh->cpusubtype = ByteSwap(mh->cpusubtype); 111 mh->filetype = ByteSwap(mh->filetype); 112 mh->ncmds = ByteSwap(mh->ncmds); 113 mh->sizeofcmds = ByteSwap(mh->sizeofcmds); 114 mh->flags = ByteSwap(mh->flags); 115 } 116 117 void breakpad_swap_mach_header_64(struct mach_header_64 *mh) { 118 mh->magic = ByteSwap(mh->magic); 119 mh->cputype = ByteSwap(mh->cputype); 120 mh->cpusubtype = ByteSwap(mh->cpusubtype); 121 mh->filetype = ByteSwap(mh->filetype); 122 mh->ncmds = ByteSwap(mh->ncmds); 123 mh->sizeofcmds = ByteSwap(mh->sizeofcmds); 124 mh->flags = ByteSwap(mh->flags); 125 mh->reserved = ByteSwap(mh->reserved); 126 } 127 128 void breakpad_swap_section(struct section *s, 129 uint32_t nsects) { 130 for (uint32_t i = 0; i < nsects; i++) { 131 s[i].addr = ByteSwap(s[i].addr); 132 s[i].size = ByteSwap(s[i].size); 133 134 s[i].offset = ByteSwap(s[i].offset); 135 s[i].align = ByteSwap(s[i].align); 136 s[i].reloff = ByteSwap(s[i].reloff); 137 s[i].nreloc = ByteSwap(s[i].nreloc); 138 s[i].flags = ByteSwap(s[i].flags); 139 s[i].reserved1 = ByteSwap(s[i].reserved1); 140 s[i].reserved2 = ByteSwap(s[i].reserved2); 141 } 142 } 143 144 void breakpad_swap_section_64(struct section_64 *s, 145 uint32_t nsects) { 146 for (uint32_t i = 0; i < nsects; i++) { 147 s[i].addr = ByteSwap(s[i].addr); 148 s[i].size = ByteSwap(s[i].size); 149 150 s[i].offset = ByteSwap(s[i].offset); 151 s[i].align = ByteSwap(s[i].align); 152 s[i].reloff = ByteSwap(s[i].reloff); 153 s[i].nreloc = ByteSwap(s[i].nreloc); 154 s[i].flags = ByteSwap(s[i].flags); 155 s[i].reserved1 = ByteSwap(s[i].reserved1); 156 s[i].reserved2 = ByteSwap(s[i].reserved2); 157 } 158 }