Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/osx/x86/include/_inject_bundle.s
Views: 11784
;;;1;;; Skip straight to inject_bundle when we assemble this as bin file2;;;3jmp _inject_bundle45;;; --------------------------------------------------------------------6;;; Constants7;;; --------------------------------------------------------------------8%define MAP_ANON 0x10009%define MAP_PRIVATE 0x000210%define PROT_READ 0x0111%define PROT_WRITE 0x021213%define NSLINKMODULE_OPTION_BINDNOW 0x114%define NSLINKMODULE_OPTION_PRIVATE 0x215%define NSLINKMODULE_OPTION_RETURN_ON_ERROR 0x41617;;; --------------------------------------------------------------------18;;; ror13_hash(string symbol_name)19;;;20;;; Compute the 32-bit "ror13" hash for a given symbol name. The hash21;;; value is left in the variable hash22;;; --------------------------------------------------------------------23%macro ror13_hash 124%assign hash 025%assign c 026%strlen len %12728%assign i 129%rep len30%substr c %1 i31%assign hash ((((hash >> 13) | (hash << 19)) + c) & 0xFFFFFFFF)32%assign i i + 133%endrep34%endmacro3536;;; --------------------------------------------------------------------37;;; dyld_resolve(uint32_t hash)38;;;39;;; Lookup the address of an exported symbol within dyld by "ror13" hash.40;;;41;;; Arguments:42;;; hash - 32-bit "ror13" hash of symbol name43;;; --------------------------------------------------------------------44_dyld_resolve:45mov eax, [esp+4]46push eax47push 0x8fe0000048call _macho_resolve49ret 45051;;; --------------------------------------------------------------------52;;; macho_resolve(void* base, uint32_t hash)53;;;54;;; Lookup the address of an exported symbol within the given Mach-O55;;; image by "ror13" hash value.56;;;57;;; Arguments:58;;; base - base address of Mach-O image59;;; hash - 32-bit "ror13" hash of symbol name60;;; --------------------------------------------------------------------61_macho_resolve:62push ebp63mov ebp, esp64sub esp, byte 1265push ebx66push esi67push edi6869mov ebx, [ebp+8] ; mach-o image base address70mov eax, [ebx+16] ; mach_header->ncmds71mov [ebp-4], eax ; ncmds7273add bl, 28 ; Advance ebx to first load command74.loadcmd:75;; Load command loop76xor eax, eax77cmp dword [ebp-4], eax78je .return7980inc eax81cmp [ebx], eax82je .segment83inc eax84cmp [ebx], eax85je .symtab86.next_loadcmd:87;; Advance to the next load command88dec dword [ebp-4]89add ebx, [ebx+4]90jmp .loadcmd9192.segment:93;; Look for "__TEXT" segment94cmp [ebx+10], dword 'TEXT'95je .text96;; Look for "__LINKEDIT" segment97cmp [ebx+10], dword 'LINK'98je .linkedit99100jmp .next_loadcmd101.text:102mov eax, [ebx+24]103mov [ebp-8], eax ; save image preferred load address104jmp .next_loadcmd105.linkedit:106;; We have found the __LINKEDIT segment107mov eax, [ebx+24] ; segcmd->vmaddr108sub eax, [ebp-8] ; image preferred load address109add eax, [ebp+8] ; actual image load address110sub eax, [ebx+32] ; segcmd->fileoff111mov [ebp-12], eax ; save linkedit segment base112113jmp .next_loadcmd114115.symtab:116;; Examine LC_SYMTAB load command117mov ecx, [ebx+12] ; ecx = symtab->nsyms118.symbol:119xor eax, eax120cmp ecx, eax121je .return122dec ecx123124imul edx, ecx, byte 12 ; edx = index into symbol table125add edx, [ebx+8] ; edx += symtab->symoff126add edx, [ebp-12] ; adjust symoff relative to linkedit127128mov esi, [edx] ; esi = index into string table129add esi, [ebx+16] ; esi += symtab->stroff130add esi, [ebp-12] ; adjust stroff relative to linkedit131132;; hash = (hash >> 13) | ((hash & 0x1fff) << 19) + c133xor edi, edi134cld135.hash:136xor eax, eax137lodsb138cmp al, ah139je .compare140ror edi, 13141add edi, eax142jmp .hash143144.compare:145cmp edi, [ebp+12]146jne .symbol147148mov eax, [edx+8] ; return symbols[ecx].n_value149sub eax, [ebp-8] ; adjust to actual load address150add eax, [ebp+8]151.return:152pop edi153pop esi154pop ebx155leave156ret 8157158;;; --------------------------------------------------------------------159;;; inject_bundle(int filedes)160;;;161;;; Read a Mach-O bundle from the given file descriptor, load and link162;;; it into the currently running process.163;;;164;;; Arguments:165;;; filedes (edi) - file descriptor to read() bundle from166;;; --------------------------------------------------------------------167_inject_bundle:168push ebp169mov ebp, esp170sub esp, byte 12171172mov esi, edi ; arg0: filedes173174.read_size:175;; Read a 4-byte size of bundle to read176xor eax, eax177mov al, 4178push eax ; nbyte179lea edi, [ebp-4]180push edi ; buf181push esi ; s182push eax183dec eax184int 0x80185jb .read_error186cmp eax, ecx ; A zero-read signals termination187je .read_error188mov ecx, [ebp-4]189xor eax, eax190cmp ecx, eax191je .read_error ; A zero value signals termination192193jmp .mmap194.read_error:195jmp .error196197.mmap:198;; mmap memory199xor eax, eax200push eax201push -1202push (MAP_ANON | MAP_PRIVATE)203push (PROT_READ | PROT_WRITE)204push ecx ; size205push eax206push eax ; spacer207mov al, 197208int 0x80209jb .error210mov edi, eax ; memory buffer211mov [ebp-8], edi212213;; read bundle from file descriptor into mmap'd buffer214.read_bundle:215xor eax, eax216push ecx ; nbyte217push edi ; buf218push esi ; filedes219push eax ; spacer220mov al, 3221int 0x80222jb .error223add edi, eax224sub ecx, eax225jnz .read_bundle226227mov edi, [ebp-8] ; load original memory buffer228229;; Now that we are calling library methods, we need to make sure230;; that esp is 16-byte aligned at the the point of the call231;; instruction. So we align the stack here and then just be232;; careful to keep it aligned as we call library functions.233234sub esp, byte 16235and esp, 0xfffffff0236237;; load bundle from mmap'd buffer238push byte 0 ; maintain alignment239lea eax, [ebp-8]240push eax ; &objectFileImage241push dword [ebp+12] ; size242push edi ; addr243ror13_hash "_NSCreateObjectFileImageFromMemory"244push dword hash245call _dyld_resolve246call eax247cmp al, 1248jne .error249250;; link bundle from object file image251xor eax, eax252push eax253mov al, (NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_BINDNOW)254push eax255push esp ; ""256push dword [ebp-8]257ror13_hash "_NSLinkModule"258push dword hash259call _dyld_resolve260call eax261262;; run_symbol = NSLookupSymbolInModule(module, "_run")263mov ebx, eax264xor eax, eax265push eax ; "\0\0\0\0"266push 0x6e75725f ; "_run"267mov eax, esp268push eax ; sym269push ebx ; module270271ror13_hash "_NSLookupSymbolInModule"272push dword hash273call _dyld_resolve274call eax275276;; NSAddressOfSymbol(run_symbol)277sub esp, 12 ; maintain alignment278push eax279ror13_hash "_NSAddressOfSymbol"280push dword hash281call _dyld_resolve282call eax283284;; _run(socket)285sub esp, 12 ; maintain alignment286push esi287call eax288289.error:290;; Exit cleanly291xor eax, eax292push eax ; EXIT_SUCCESS293push eax ; spacer294mov al, 1295int 0x80296297298