| 157 | } |
| 158 | |
| 159 | export function resolve_import(import_addr) { |
| 160 | if (import_addr.read16(0) !== 0x25ff) { |
| 161 | throw Error( |
| 162 | `instruction at ${import_addr} is not of the form: jmp qword` |
| 163 | + ' [rip + X]'); |
| 164 | } |
| 165 | // module_function_import: |
| 166 | // jmp qword [rip + X] |
| 167 | // ff 25 xx xx xx xx // signed 32-bit displacement |
| 168 | const disp = import_addr.read32(2); |
| 169 | // assume disp and offset are 32-bit integers |
| 170 | // x | 0 will always be a signed integer |
| 171 | const offset = (disp | 0) + 6; |
| 172 | // The rIP value used by "jmp [rip + X]" instructions is actually the rIP |
| 173 | // of the next instruction. This means that the actual address used is |
| 174 | // [rip + X + sizeof(jmp_insn)], where sizeof(jmp_insn) is the size of the |
| 175 | // jump instruction, which is 6 in this case. |
| 176 | const function_addr = import_addr.readp(offset); |
| 177 | |
| 178 | return function_addr; |
| 179 | } |
| 180 | |
| 181 | export function init_syscall_array( |
| 182 | syscall_array, |