this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

CoreServices fixes, code cleanups

+241 -195
+1 -1
src/CoreServices/CMakeLists.txt
··· 35 35 ) 36 36 37 37 add_library(CoreServices SHARED ${CoreServices_SRCS}) 38 - target_link_libraries(CoreServices -licuuc) 38 + target_link_libraries(CoreServices -licuuc System.B.dylib) 39 39 40 40 install(TARGETS CoreServices DESTINATION "lib${SUFFIX}/darling") 41 41
+42 -16
src/CoreServices/MacLocales.cpp
··· 31 31 return 0; 32 32 } 33 33 34 - // LocaleRef is a pointer to a struct 35 - // Instead of storing a pointer, we simply use 4 bytes of the pointer to save the lang code 36 34 char lc[3], rc[3]; 37 35 lc[0] = char(langCode & 255); 38 36 lc[1] = char(langCode >> 8); ··· 157 155 158 156 // variant and nameMask currently ignored 159 157 158 + size_t r; 160 159 UnicodeString str, str2; 161 160 loc.getDisplayLanguage(locDisplay, str); 162 161 str += " ("; 163 162 loc.getDisplayCountry(locDisplay, str2); 164 163 str += str2; 165 164 str += ")"; 166 - 167 - const UChar* buf = str.getTerminatedBuffer(); 168 - size_t inLen = (str.length()+1) * sizeof(UChar); 169 - size_t outLen = maxLen * sizeof(Utf16Char); 170 - const char* inbuf = reinterpret_cast<const char*>(buf); 171 - char* outbuf = reinterpret_cast<char*>(displayName); 172 - size_t r = iconv(g_icUtf32ToUtf16, const_cast<char**>(&inbuf), &inLen, &outbuf, &outLen); 165 + 166 + static_assert(sizeof(UChar) == 4 || sizeof(UChar) == 2, "Unsupported UChar size"); 167 + 168 + if (sizeof(UChar) == 4) 169 + { 170 + const UChar* buf = str.getTerminatedBuffer(); 171 + size_t inLen = (str.length()+1) * sizeof(UChar); 172 + size_t outLen = maxLen * sizeof(Utf16Char); 173 + const char* inbuf = reinterpret_cast<const char*>(buf); 174 + char* outbuf = reinterpret_cast<char*>(displayName); 175 + 176 + r = iconv(g_icUtf32ToUtf16, const_cast<char**>(&inbuf), &inLen, &outbuf, &outLen); 173 177 174 - if (r == size_t(-1)) 178 + if (r == size_t(-1)) 179 + { 180 + if (errno == E2BIG) 181 + { 182 + displayName[maxLen-1] = 0; 183 + r = maxLen; 184 + return -30001; 185 + } 186 + else 187 + { 188 + r = 0; 189 + *displayName = 0; 190 + return makeOSStatus(errnoLinuxToDarwin(errno)); 191 + } 192 + } 193 + } 194 + else if (sizeof(UChar) == sizeof(Utf16Char)) 175 195 { 176 - if (errno == E2BIG) 196 + if (str.length()+1 > maxLen) 177 197 { 178 - displayName[maxLen-1] = 0; 179 - r = maxLen; 198 + *lenOut = str.length()+1; 199 + *displayName = 0; 200 + return -30001; 180 201 } 181 202 else 182 203 { 183 - r = 0; 184 - *displayName = 0; 185 - return makeOSStatus(errnoLinuxToDarwin(errno)); 204 + memcpy(displayName, str.getTerminatedBuffer(), (str.length()+1) * sizeof(UChar)); 205 + r = str.length()+1; 186 206 } 187 207 } 188 208 ··· 207 227 208 228 OSStatus LocaleOperationGetName(LocaleOperationClass cls, LocaleRef ref, unsigned long maxLen, unsigned long* lenOut, Utf16Char* displayName) 209 229 { 230 + *lenOut = 0; 231 + *displayName = 0; 210 232 return unimpErr; 211 233 } 212 234 213 235 OSStatus LocaleOperationCountNames(LocaleOperationClass cls, unsigned long* count) 214 236 { 237 + *count = 0; 215 238 return unimpErr; 216 239 } 217 240 218 241 OSStatus LocaleOperationGetIndName(LocaleOperationClass cls, unsigned long index, unsigned long maxLen, unsigned long* lenOut, Utf16Char* displayName, LocaleRef* displayLocale) 219 242 { 243 + *displayName = 0; 244 + *displayLocale = 0; 245 + *lenOut = 0; 220 246 return unimpErr; 221 247 } 222 248
+30 -12
src/dyld/MachOLoader.cpp
··· 315 315 void MachOLoader::doBind(const MachO& mach, intptr slide) 316 316 { 317 317 std::string last_weak_name; 318 - char* last_weak_sym = 0; 318 + uintptr_t last_weak_sym = 0; 319 319 size_t seen_weak_bind_index = 0; 320 320 size_t seen_weak_binds_orig_size = m_seen_weak_binds.size(); 321 321 ··· 323 323 324 324 for (MachO::Bind* bind : mach.binds()) 325 325 { 326 - if (bind->type == BIND_TYPE_POINTER) 326 + if (bind->type == BIND_TYPE_POINTER || bind->type == BIND_TYPE_STUB) 327 327 { 328 328 std::string name = bind->name.substr(1); 329 - void** ptr = (void**)(bind->vmaddr + slide); 330 - char* sym = 0; 329 + uintptr_t* ptr = (uintptr_t*)(bind->vmaddr + slide); 330 + uintptr_t sym = 0; 331 331 332 332 if (bind->is_weak) 333 333 { ··· 348 348 { 349 349 if (bind->is_classic) 350 350 { 351 - *ptr = last_weak_sym = (char*)bind->value; 351 + *ptr = last_weak_sym = (uintptr_t)bind->value; 352 352 } 353 353 else 354 354 { 355 355 const Exports::const_iterator export_found = m_exports.find(bind->name); 356 356 if (export_found != m_exports.end()) 357 - *ptr = last_weak_sym = (char*)export_found->second.addr; 357 + *ptr = last_weak_sym = (uintptr_t)export_found->second.addr; 358 358 else 359 - last_weak_sym = (char*)*ptr; 359 + last_weak_sym = *ptr; 360 360 } 361 361 } 362 362 ··· 384 384 { 385 385 // assume local (e.g. dyld_stub_binder) 386 386 name = bind->name; // for correct error reporting 387 - sym = reinterpret_cast<char*>(dlsym(dlopen(0, 0), bind->name.c_str())); 387 + sym = reinterpret_cast<uintptr_t>(dlsym(dlopen(0, 0), bind->name.c_str())); 388 388 } 389 389 else 390 - sym = reinterpret_cast<char*>(__darwin_dlsym(DARWIN_RTLD_DEFAULT, name.c_str())); 390 + sym = reinterpret_cast<uintptr_t>(__darwin_dlsym(DARWIN_RTLD_DEFAULT, name.c_str())); 391 391 392 392 if (!sym) 393 393 { ··· 402 402 char* dname = new char[name.size()+1]; 403 403 strcpy(dname, name.c_str()); 404 404 405 - sym = reinterpret_cast<char*>(m_pUndefMgr->generateNew(dname)); 405 + sym = reinterpret_cast<uintptr_t>(m_pUndefMgr->generateNew(dname)); 406 406 } 407 407 else 408 408 #endif ··· 423 423 << *ptr << " => " << (void*)sym << " @" << ptr << std::endl; 424 424 425 425 if (g_trampoline) 426 - *ptr = m_pTrampolineMgr->generate(sym, name.c_str()); 427 - else 426 + sym = (uintptr_t) m_pTrampolineMgr->generate((void*)sym, name.c_str()); 427 + if (bind->type == BIND_TYPE_POINTER) 428 + { 428 429 *ptr = sym; 430 + } 431 + #ifdef __i386__ 432 + else if (bind->type == BIND_TYPE_STUB) 433 + { 434 + struct jmp_instr 435 + { 436 + uint8_t relJmp; 437 + uint32_t addr; 438 + } __attribute__((packed)); 439 + static_assert(sizeof(jmp_instr) == 5, "Incorrect jmp instruction size"); 440 + 441 + jmp_instr* instr = reinterpret_cast<jmp_instr*>(ptr); 442 + 443 + instr->relJmp = 0xE9; // x86 jmp rel32 444 + instr->addr = sym - uint32_t(ptr) + sizeof(jmp_instr); 445 + } 446 + #endif 429 447 } 430 448 else 431 449 {
+1 -1
src/dyld/MachOLoader.h
··· 93 93 intptr m_last_addr; 94 94 std::vector<uint64_t> m_init_funcs; 95 95 Exports m_exports; 96 - std::vector<std::pair<std::string, char*> > m_seen_weak_binds; 96 + std::vector<std::pair<std::string, uintptr_t> > m_seen_weak_binds; 97 97 UndefMgr* m_pUndefMgr; 98 98 TrampolineMgr* m_pTrampolineMgr; 99 99
+18 -26
src/libmach-o/MachO.cpp
··· 1 - // Copyright 2011 Shinichiro Hamaji. All rights reserved. 2 - // 3 - // Redistribution and use in source and binary forms, with or without 4 - // modification, are permitted provided that the following conditions 5 - // are met: 6 - // 7 - // 1. Redistributions of source code must retain the above copyright 8 - // notice, this list of conditions and the following disclaimer. 9 - // 10 - // 2. Redistributions in binary form must reproduce the above 11 - // copyright notice, this list of conditions and the following 12 - // disclaimer in the documentation and/or other materials 13 - // provided with the distribution. 14 - // 15 - // THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY 16 - // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 - // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 - // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR 19 - // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 - // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 - // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 22 - // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 - // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 - // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 - // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 - // SUCH DAMAGE. 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2012 Lubos Dolezel 5 + Copyright (C) 2011 Shinichiro Hamaji 27 6 7 + Darling is free software: you can redistribute it and/or modify 8 + it under the terms of the GNU General Public License as published by 9 + the Free Software Foundation, either version 3 of the License, or 10 + (at your option) any later version. 11 + 12 + Darling is distributed in the hope that it will be useful, 13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + GNU General Public License for more details. 16 + 17 + You should have received a copy of the GNU General Public License 18 + along with Foobar. If not, see <http://www.gnu.org/licenses/>. 19 + */ 28 20 29 21 #include "MachO.h" 30 22 #include "FatMachO.h"
+23 -28
src/libmach-o/MachO.h
··· 1 - // Copyright 2011 Shinichiro Hamaji. All rights reserved. 2 - // 3 - // Redistribution and use in source and binary forms, with or without 4 - // modification, are permitted provided that the following conditions 5 - // are met: 6 - // 7 - // 1. Redistributions of source code must retain the above copyright 8 - // notice, this list of conditions and the following disclaimer. 9 - // 10 - // 2. Redistributions in binary form must reproduce the above 11 - // copyright notice, this list of conditions and the following 12 - // disclaimer in the documentation and/or other materials 13 - // provided with the distribution. 14 - // 15 - // THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY 16 - // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 - // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 - // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR 19 - // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 - // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 - // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 22 - // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 - // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 - // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 - // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 - // SUCH DAMAGE. 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2012 Lubos Dolezel 5 + Copyright (C) 2011 Shinichiro Hamaji 6 + 7 + Darling is free software: you can redistribute it and/or modify 8 + it under the terms of the GNU General Public License as published by 9 + the Free Software Foundation, either version 3 of the License, or 10 + (at your option) any later version. 11 + 12 + Darling is distributed in the hope that it will be useful, 13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + GNU General Public License for more details. 27 16 17 + You should have received a copy of the GNU General Public License 18 + along with Foobar. If not, see <http://www.gnu.org/licenses/>. 19 + */ 28 20 29 21 #ifndef MACH_O_H_ 30 22 #define MACH_O_H_ ··· 105 97 106 98 const std::vector<Symbol>& symbols() const { return m_symbols; } 107 99 108 - const char* base() const { return m_base; } 100 + uintptr_t base() const { return m_base; } 109 101 110 102 uint64_t entry() const { return m_entry; } 111 103 uint64_t main() const { return m_main; } ··· 138 130 std::vector<Bind*> m_binds; 139 131 std::vector<Export*> m_exports; 140 132 std::vector<Symbol> m_symbols; 141 - const char* m_base; 133 + uintptr_t m_base; 142 134 uint64_t m_entry, m_main; 143 135 std::vector<uint64_t> m_init_funcs; 144 136 std::vector<uint64_t> m_exit_funcs; ··· 162 154 private: 163 155 std::vector<std::string> m_archs; 164 156 }; 157 + 158 + // we express stub binds as another type of ordinary bind 159 + #define BIND_TYPE_STUB 100 165 160 166 161 #endif // MACH_O_H_
+105 -85
src/libmach-o/MachOImpl.cpp
··· 1 - // Copyright 2011 Shinichiro Hamaji. All rights reserved. 2 - // 3 - // Redistribution and use in source and binary forms, with or without 4 - // modification, are permitted provided that the following conditions 5 - // are met: 6 - // 7 - // 1. Redistributions of source code must retain the above copyright 8 - // notice, this list of conditions and the following disclaimer. 9 - // 10 - // 2. Redistributions in binary form must reproduce the above 11 - // copyright notice, this list of conditions and the following 12 - // disclaimer in the documentation and/or other materials 13 - // provided with the distribution. 14 - // 15 - // THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY 16 - // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 - // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 - // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR 19 - // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 - // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 - // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 22 - // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 - // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 - // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 - // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 - // SUCH DAMAGE. 1 + /* 2 + This file is part of Darling. 27 3 4 + Copyright (C) 2012 Lubos Dolezel 5 + Copyright (C) 2011 Shinichiro Hamaji 6 + 7 + Darling is free software: you can redistribute it and/or modify 8 + it under the terms of the GNU General Public License as published by 9 + the Free Software Foundation, either version 3 of the License, or 10 + (at your option) any later version. 11 + 12 + Darling is distributed in the hope that it will be useful, 13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + GNU General Public License for more details. 16 + 17 + You should have received a copy of the GNU General Public License 18 + along with Foobar. If not, see <http://www.gnu.org/licenses/>. 19 + */ 28 20 29 21 #include "MachOImpl.h" 30 22 #include "log.h" ··· 46 38 template <class section> 47 39 void MachOImpl::readClassicBind(const section& sec, uint32_t* dysyms, uint32_t* symtab, const char* symstrtab) 48 40 { 49 - uint32_t indirect_offset = sec.reserved1; 50 - int count = sec.size / m_ptrsize; 41 + const uint32_t indirect_offset = sec.reserved1; 42 + const int count = sec.size / m_ptrsize; 51 43 52 44 for (int i = 0; i < count; i++) 53 45 { ··· 63 55 bind->ordinal = 1; 64 56 bind->is_weak = ((sym->n_desc & N_WEAK_DEF) != 0); 65 57 bind->is_classic = true; 66 - LOGF("add classic bind! %s type=%d sect=%d desc=%d value=%lld " 67 - "vmaddr=%p is_weak=%d\n", 68 - bind->name.c_str(), sym->n_type, sym->n_sect, sym->n_desc, (ll)sym->n_value, 69 - (void*)(bind->vmaddr), bind->is_weak); 58 + LOG << "add classic bind: " << bind->name << " type=" << sym->n_type << " sect=" << sym->n_sect 59 + << " desc=" << sym->n_desc << " value=" << sym->n_value << " vmaddr=" << (void*)(bind->vmaddr) 60 + << " is_weak=" << bind->is_weak << std::endl; 70 61 m_binds.push_back(bind); 71 62 } 72 63 } 73 64 65 + void MachOImpl::readStubBind(const section& sec, uint32_t* dysyms, uint32_t* symtab, const char* symstrtab) 66 + { 67 + const uint32_t element_size = sec.reserved2; 68 + const uint32_t indirect_offset = sec.reserved1; 69 + const int count = sec.size / element_size; 70 + 71 + if (element_size != 5) 72 + { 73 + LOG << "MachOImpl::readStubBind(): cannot handle stubs of elem size != 5, size=" << element_size << std::endl; 74 + return; 75 + } 76 + 77 + for (int i = 0; i < count; i++) 78 + { 79 + uint32_t dysym = dysyms[indirect_offset + i]; 80 + uint32_t index = dysym & 0x3fffffff; 81 + nlist* sym = (nlist*)(symtab + index * 3); 82 + 83 + MachO::Bind* bind = new MachO::Bind(); 84 + bind->name = symstrtab + sym->n_strx; 85 + bind->vmaddr = sec.addr + i * element_size; 86 + bind->value = sym->n_value; 87 + bind->type = BIND_TYPE_STUB; 88 + bind->ordinal = 1; 89 + bind->is_weak = ((sym->n_desc & N_WEAK_DEF) != 0); 90 + bind->is_classic = true; 91 + 92 + m_binds.push_back(bind); 93 + 94 + LOG << "add stub bind: " << bind->name.c_str() << " vmaddr=" << (void*) bind->vmaddr << std::endl; 95 + } 96 + } 74 97 75 98 template <class segment_command, class section> 76 99 void MachOImpl::readSegment(char* cmds_ptr, std::vector<segment_command*>* segments, std::vector<section*>* bind_sections) ··· 78 101 segment_command* segment = reinterpret_cast<segment_command*>(cmds_ptr); 79 102 segments->push_back(segment); 80 103 81 - LOGF("segment %s: vmaddr=%p vmsize=%llu " 82 - "fileoff=%llu filesize=%llu " 83 - "maxprot=%d initprot=%d nsects=%u flags=%u\n", 84 - segment->segname, 85 - (void*)(intptr_t)segment->vmaddr, (ull)segment->vmsize, 86 - (ull)segment->fileoff, (ull)segment->filesize, 87 - segment->maxprot, segment->initprot, 88 - segment->nsects, segment->flags); 104 + LOG << "segment " << segment->segname << ": vmaddr=" << (void*)segment->vmaddr 105 + << " vmsize=" << std::hex << segment->vmsize << " file_offset=" << segment->fileoff 106 + << " file_size=" << segment->filesize << " maxprot=" << segment->maxprot 107 + << " init_prot=" << segment->initprot << " nsects=" << std::dec << segment->nsects 108 + << " flags=" << std::hex << segment->flags << std::dec << std::endl; 89 109 90 110 section* sections = reinterpret_cast<section*>(cmds_ptr + sizeof(segment_command)); 91 111 ··· 96 116 { 97 117 const section& sec = sections[j]; 98 118 Section savedSection{sec.segname, sec.sectname, uintptr_t(sec.addr), uintptr_t(sec.size)}; 99 - 100 - LOGF("section %s in %s: " 101 - "addr=%p size=%llu offset=%u align=%u " 102 - "reloff=%u nreloc=%u flags=%u " 103 - "reserved1=%u reserved2=%u\n", 104 - sec.sectname, sec.segname, 105 - (void*)(intptr_t)sec.addr, (ull)sec.size, 106 - sec.offset, sec.align, 107 - sec.reloff, sec.nreloc, sec.flags, 108 - sec.reserved1, sec.reserved2); 119 + 120 + LOG << "section " << sec.sectname << " in " << sec.segname << ": addr=" 121 + << ((void*)(uintptr_t)sec.addr) << " size=" << std::hex << sec.size << " offset=" 122 + << sec.offset << " align=" << sec.align << " reloff=" << sec.reloff 123 + << " nreloc=" << std::dec << sec.nreloc << " flags=" << std::hex 124 + << sec.flags << " reserved1=" << sec.reserved1 << " reserved2=" 125 + << sec.reserved2 << std::dec << std::endl; 109 126 110 127 if (savedSection.section.size() > sizeof(sec.sectname)) 111 128 savedSection.section.resize(sizeof(sec.sectname)); ··· 153 170 bind_sections->push_back(sections + j); 154 171 break; 155 172 173 + case S_SYMBOL_STUBS: // 0x8, byte size of element in reserved2, indir offset in reserved1 174 + //assert(!m_is64); 175 + if (!m_is64) 176 + bind_sections->push_back(sections + j); 177 + break; 156 178 case S_ZEROFILL: 157 179 case S_CSTRING_LITERALS: // 0x2 158 180 case S_4BYTE_LITERALS: // 0x3 159 181 case S_8BYTE_LITERALS: // 0x4 160 182 case S_LITERAL_POINTERS: // 0x5 161 - case S_SYMBOL_STUBS: // 0x8, byte size of stub in reserved2 162 183 case S_COALESCED: 163 184 case S_GB_ZEROFILL: 164 185 case S_INTERPOSING: // 0xD 165 186 case S_16BYTE_LITERALS: 166 187 case S_DTRACE_DOF: 167 188 case S_LAZY_DYLIB_SYMBOL_POINTERS: 168 - LOGF("FIXME: section type %d will not be handled for %s in %s (%p)\n", 169 - section_type, sec.sectname, sec.segname, sec.addr); 189 + LOG << "Section " << sec.sectname << " in " << sec.segname << " not handled with type " << std::hex << section_type << std::dec << std::endl; 170 190 break; 171 191 172 192 default: 173 - fprintf(stderr, "Unknown section type: %d\n", section_type); 193 + std::cerr << "ERROR: Section " << sec.sectname << " in " << sec.segname << " has unknown type " << std::hex << section_type << std::dec << std::endl; 174 194 abort(); 175 195 break; 176 196 } ··· 209 229 exp->name = *name_buf; 210 230 exp->flag = uleb128(p); 211 231 exp->addr = uleb128(p); 212 - LOGF("export: %s %lu %p\n", 213 - name_buf->c_str(), (long)exp->flag, (void*)exp->addr); 232 + LOG << "export: " << name_buf << " flags=" << std::hex << exp->flag << std::dec << " addr=" << (void*)exp->addr << std::endl; 214 233 215 234 m_exports.push_back(exp); 216 235 ··· 252 271 253 272 ::lseek(fd, 0, SEEK_SET); 254 273 255 - char* bin = m_mapped = reinterpret_cast<char*>( 274 + void* bin = m_mapped = reinterpret_cast<char*>( 256 275 ::mmap(NULL, m_mapped_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, m_fd, offset) 257 276 ); 258 277 259 278 if (bin == MAP_FAILED) 260 279 throw std::runtime_error("Cannot mmap Mach-O file"); 261 280 262 - m_base = bin; 281 + m_base = uintptr_t(bin); 263 282 264 - mach_header* header = reinterpret_cast<mach_header*>(bin); 283 + const mach_header* header = reinterpret_cast<mach_header*>(bin); 265 284 memcpy(&m_header, header, sizeof(*header)); 266 285 267 286 m_is64 = false; ··· 286 305 { 287 306 throw std::runtime_error("Unsupported CPU type in Mach-O"); 288 307 } 289 - 290 - // TODO: split into a method 291 308 309 + processLoaderCommands(header); 310 + } 311 + 312 + void MachOImpl::processLoaderCommands(const mach_header* header) 313 + { 292 314 struct load_command* cmds_ptr = reinterpret_cast<struct load_command*>( 293 - bin + (m_is64 ? sizeof(mach_header_64) : sizeof(mach_header)) 315 + m_base + (m_is64 ? sizeof(mach_header_64) : sizeof(mach_header)) 294 316 ); 295 317 296 318 uint32_t* symtab = 0; ··· 302 324 303 325 for (uint32_t ii = 0; ii < header->ncmds; ii++) 304 326 { 305 - LOGF("cmd type:%x\n", cmds_ptr->cmd); 327 + LOG << "loader command type=" << std::hex << cmds_ptr->cmd << std::dec << std::endl; 306 328 307 329 switch (cmds_ptr->cmd) 308 330 { ··· 332 354 333 355 { 334 356 const uint8_t* p = reinterpret_cast<uint8_t*>( 335 - bin + dyinfo->rebase_off); 357 + m_base + dyinfo->rebase_off); 336 358 const uint8_t* end = p + dyinfo->rebase_size; 337 359 if (dyinfo->rebase_off && dyinfo->rebase_size) 338 360 readRebase(p, end); ··· 340 362 341 363 { 342 364 const uint8_t* p = reinterpret_cast<uint8_t*>( 343 - bin + dyinfo->bind_off); 365 + m_base + dyinfo->bind_off); 344 366 const uint8_t* end = p + dyinfo->bind_size; 345 367 readBind(p, end, false); 346 368 } 347 369 348 370 { 349 371 const uint8_t* p = reinterpret_cast<uint8_t*>( 350 - bin + dyinfo->lazy_bind_off); 372 + m_base + dyinfo->lazy_bind_off); 351 373 const uint8_t* end = p + dyinfo->lazy_bind_size; 352 374 readBind(p, end, false); 353 375 } 354 376 355 377 { 356 378 const uint8_t* p = reinterpret_cast<uint8_t*>( 357 - bin + dyinfo->weak_bind_off); 379 + m_base + dyinfo->weak_bind_off); 358 380 const uint8_t* end = p + dyinfo->weak_bind_size; 359 381 readBind(p, end, true); 360 382 } ··· 362 384 if (m_need_exports) 363 385 { 364 386 const uint8_t* p = reinterpret_cast<uint8_t*>( 365 - bin + dyinfo->export_off); 387 + m_base + dyinfo->export_off); 366 388 const uint8_t* end = p + dyinfo->export_size; 367 389 if (dyinfo->export_off && dyinfo->export_size) 368 390 { ··· 382 404 symtab_cmd->symoff, symtab_cmd->nsyms, 383 405 symtab_cmd->stroff, symtab_cmd->strsize); 384 406 385 - uint32_t* symtab_top = symtab = reinterpret_cast<uint32_t*>(bin + symtab_cmd->symoff); 386 - symstrtab = bin + symtab_cmd->stroff; 407 + uint32_t* symtab_top = symtab = reinterpret_cast<uint32_t*>(m_base + symtab_cmd->symoff); 408 + symstrtab = (const char*) m_base + symtab_cmd->stroff; 387 409 388 410 if (FLAGS_READ_SYMTAB) 389 411 { ··· 443 465 if (dysymtab_cmd->nindirectsyms) 444 466 { 445 467 dysyms = reinterpret_cast<uint32_t*>( 446 - bin + dysymtab_cmd->indirectsymoff); 468 + m_base + dysymtab_cmd->indirectsymoff); 447 469 } 448 470 if (FLAGS_READ_DYSYMTAB) 449 471 { ··· 462 484 LOGF("dysym %d %s(%u)%s%s\n", j, symstrtab + sym[0], index, local, abs); 463 485 } 464 486 465 - uint32_t* dymods = reinterpret_cast<uint32_t*>( bin + dysymtab_cmd->modtaboff); 487 + uint32_t* dymods = reinterpret_cast<uint32_t*>( m_base + dysymtab_cmd->modtaboff); 466 488 for (uint32_t j = 0; j < dysymtab_cmd->nmodtab; j++) 467 489 LOGF("dymods: %u\n", dymods[j]); 468 490 } ··· 473 495 case LC_LOAD_DYLINKER: 474 496 { 475 497 lc_str name = reinterpret_cast<struct dylinker_command*>(cmds_ptr)->name; 476 - LOGF("dylinker: %s\n", (char*)cmds_ptr + name.offset); 498 + LOG << "dynamic linker: " << ((char*)cmds_ptr + name.offset) << std::endl; 477 499 break; 478 500 } 479 501 ··· 483 505 case LC_UNIXTHREAD: 484 506 { 485 507 uint32_t* p = reinterpret_cast<uint32_t*>(cmds_ptr); 486 - LOGF("UNIXTHREAD"); 487 - 488 - for (uint32_t i = 2; i < p[1]; i++) 489 - LOGF(" %d:%x", i, p[i]); 490 - 491 - LOGF("\n"); 492 508 493 509 if (m_is64) 494 510 m_entry = reinterpret_cast<uint64_t*>(cmds_ptr)[18]; 495 511 else 496 512 m_entry = reinterpret_cast<uint32_t*>(cmds_ptr)[14]; 497 513 498 - LOGF("entry=%llx\n", (ull)m_entry); 514 + LOG << "UNIXTHREAD entry=" << (void*)m_entry << std::endl; 499 515 break; 500 516 } 501 517 502 518 case LC_MAIN: 503 519 { 504 520 entry_point_command* cmd = reinterpret_cast<entry_point_command*>(cmds_ptr); 505 - LOGF("MAIN: entry offset: %x\n", cmd->entryoff); 521 + LOG << "Main: entry offset: " << std::hex << cmd->entryoff << std::dec << std::endl; 506 522 m_main = reinterpret_cast<uint64_t>(m_text_offset + cmd->entryoff); 507 523 break; 508 524 } ··· 510 526 case LC_LOAD_DYLIB: 511 527 { 512 528 dylib* lib = &reinterpret_cast<dylib_command*>(cmds_ptr)->dylib; 513 - LOGF("dylib: '%s'\n", (char*)cmds_ptr + lib->name.offset); 514 - m_dylibs.push_back((char*)cmds_ptr + lib->name.offset); 529 + const char* name = (char*)cmds_ptr + lib->name.offset; 530 + LOG << "dylib: '" << name << "'\n"; 531 + m_dylibs.push_back(name); 515 532 break; 516 533 } 517 534 ··· 521 538 reinterpret_cast<char*>(cmds_ptr) + cmds_ptr->cmdsize); 522 539 } 523 540 524 - LOGF("%p vs %p\n", cmds_ptr, bin + m_mapped_size); 541 + //LOGF("%p vs %p\n", cmds_ptr, bin + m_mapped_size); 525 542 526 543 LOG << "dyinfo: " << dyinfo << ", dysyms: " << dysyms << ", symtab: " << symtab << ", symstrtab: " << symstrtab << ", symbol count: " << m_symbols.size() << std::endl; 527 544 // No LC_DYLD_INFO_ONLY, we will read classic binding info. ··· 534 551 } 535 552 for (size_t i = 0; i < bind_sections_32.size(); i++) 536 553 { 537 - readClassicBind<section>(*bind_sections_32[i], dysyms, symtab, symstrtab); 554 + if ((bind_sections_32[i]->flags & SECTION_TYPE) == S_SYMBOL_STUBS) 555 + readStubBind(*bind_sections_32[i], dysyms, symtab, symstrtab); 556 + else 557 + readClassicBind<section>(*bind_sections_32[i], dysyms, symtab, symstrtab); 538 558 } 539 559 } 540 560 }
+21 -26
src/libmach-o/MachOImpl.h
··· 1 - // Copyright 2011 Shinichiro Hamaji. All rights reserved. 2 - // 3 - // Redistribution and use in source and binary forms, with or without 4 - // modification, are permitted provided that the following conditions 5 - // are met: 6 - // 7 - // 1. Redistributions of source code must retain the above copyright 8 - // notice, this list of conditions and the following disclaimer. 9 - // 10 - // 2. Redistributions in binary form must reproduce the above 11 - // copyright notice, this list of conditions and the following 12 - // disclaimer in the documentation and/or other materials 13 - // provided with the distribution. 14 - // 15 - // THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY 16 - // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 - // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 - // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR 19 - // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 - // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 - // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 22 - // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 - // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 - // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 - // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 - // SUCH DAMAGE. 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2012 Lubos Dolezel 5 + Copyright (C) 2011 Shinichiro Hamaji 6 + 7 + Darling is free software: you can redistribute it and/or modify 8 + it under the terms of the GNU General Public License as published by 9 + the Free Software Foundation, either version 3 of the License, or 10 + (at your option) any later version. 11 + 12 + Darling is distributed in the hope that it will be useful, 13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + GNU General Public License for more details. 27 16 17 + You should have received a copy of the GNU General Public License 18 + along with Foobar. If not, see <http://www.gnu.org/licenses/>. 19 + */ 28 20 29 21 #ifndef MACHOIMPL_H 30 22 #define MACHOIMPL_H ··· 51 43 friend class RebaseState; 52 44 friend class BindState; 53 45 46 + void processLoaderCommands(const mach_header* header); 47 + 54 48 template <class segment_command, class section> 55 49 void readSegment(char* cmds_ptr, std::vector<segment_command*>* segments, std::vector<section*>* bind_sections); 56 50 ··· 60 54 61 55 template <class section> 62 56 void readClassicBind(const section& sec, uint32_t* dysyms, uint32_t* symtab, const char* symstrtab); 57 + void readStubBind(const section& sec, uint32_t* dysyms, uint32_t* symtab, const char* symstrtab); 63 58 64 59 char* m_mapped; 65 60 size_t m_mapped_size;