comparison dynload/dynload_syms_mach-o.c @ 275:1e3617d8a951

- mach-o loader fix (discovered on High Sierra, not sure what other versions were affected)
author Tassilo Philipp
date Thu, 01 Feb 2018 12:59:00 +0100
parents 85b61e8facfe
children 7ed642a9c3e9
comparison
equal deleted inserted replaced
274:5c8eb8f34ae6 275:1e3617d8a951
5 File: dynload/dynload_syms_mach-o.c 5 File: dynload/dynload_syms_mach-o.c
6 Description: 6 Description:
7 License: 7 License:
8 8
9 Copyright (c) 2007-2015 Olivier Chafik <olivier.chafik@gmail.com>, 9 Copyright (c) 2007-2015 Olivier Chafik <olivier.chafik@gmail.com>,
10 2017 refactored completely for stability, API 10 2017-2018 refactored completely for stability, API
11 consistency and portability by Tassilo Philipp. 11 consistency and portability by Tassilo Philipp.
12 12
13 Permission to use, copy, modify, and distribute this software for any 13 Permission to use, copy, modify, and distribute this software for any
14 purpose with or without fee is hereby granted, provided that the above 14 purpose with or without fee is hereby granted, provided that the above
15 copyright notice and this permission notice appear in all copies. 15 copyright notice and this permission notice appear in all copies.
112 for(i = 0, n = pHeader->ncmds; i < n; ++i, cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize)) 112 for(i = 0, n = pHeader->ncmds; i < n; ++i, cmd = (const struct load_command*)((const char*)cmd + cmd->cmdsize))
113 { 113 {
114 if(cmd->cmd == SEGMEND_COMMAND_ID) 114 if(cmd->cmd == SEGMEND_COMMAND_ID)
115 { 115 {
116 const struct SEGMENT_COMMAND* seg = (struct SEGMENT_COMMAND*)cmd; 116 const struct SEGMENT_COMMAND* seg = (struct SEGMENT_COMMAND*)cmd;
117 if((seg->fileoff == 0) && (seg->filesize != 0)) 117 /*@@@ unsure why I used this instead of checking __TEXT: if((seg->fileoff == 0) && (seg->filesize != 0))*/
118 if(strcmp(seg->segname, "__TEXT") == 0)
118 slide = (uintptr_t)pHeader - seg->vmaddr; /* effective offset of segment from header */ 119 slide = (uintptr_t)pHeader - seg->vmaddr; /* effective offset of segment from header */
119 120
121 /* If we have __LINKEDIT segment (= raw data for dynamic linkers), use that one to find symbal table address. */
120 if(strcmp(seg->segname, "__LINKEDIT") == 0) { 122 if(strcmp(seg->segname, "__LINKEDIT") == 0) {
121 /* Adjust pBase depending on where __LINKEDIT segment is */ 123 /* Recompute pBase relative to where __LINKEDIT segment is in memory. */
122 pBase = (const char*)(seg->vmaddr - seg->fileoff) + slide; 124 pBase = (const char*)(seg->vmaddr - seg->fileoff) + slide;
125
126 /*@@@ we might want to also check maxprot and initprot here:
127 VM_PROT_READ ((vm_prot_t) 0x01)
128 VM_PROT_WRITE ((vm_prot_t) 0x02)
129 VM_PROT_EXECUTE ((vm_prot_t) 0x04)*/
130
123 symOffset = slide; /* this is also offset of symbols */ 131 symOffset = slide; /* this is also offset of symbols */
124 } 132 }
125 } 133 }
126 else if(cmd->cmd == LC_SYMTAB && !pSyms/* only init once - just safety check */) 134 else if(cmd->cmd == LC_SYMTAB && !pSyms/* only init once - just safety check */)
127 { 135 {
136 pSyms->pStringTable = pBase + scmd->stroff; 144 pSyms->pStringTable = pBase + scmd->stroff;
137 pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff); 145 pSyms->pSymbolTable = (struct NLIST_TYPE*)(pBase + scmd->symoff);
138 pSyms->symOffset = symOffset; 146 pSyms->symOffset = symOffset;
139 pSyms->pLib = pLib; 147 pSyms->pLib = pLib;
140 } 148 }
141 else if(cmd->cmd == LC_DYSYMTAB) 149 else if(cmd->cmd == LC_DYSYMTAB && !dysymtab_cmd/* only init once - just safety check */)
142 { 150 {
143 dysymtab_cmd = (const struct dysymtab_command*)cmd; 151 /* @@@ unused, we'll always run over all symbols, and not check locals, globals, etc.
144 /*@@@ check if(cmd->cmdsize != sizeof(struct dysymtab_command)) { 152 if(cmd->cmdsize != sizeof(struct symtab_command)) {
145 dlFreeMem.... 153 dlSymsCleanup(pSyms);
146 break; 154 break;
147 }*/ 155 }
156
157 dysymtab_cmd = (const struct dysymtab_command*)cmd;*/
148 } 158 }
149 } 159 }
150 } 160 }
151 161
152 /* Got symbol table? */ 162 /* Got symbol table? */