* Get the offset for an address. */
| 369 | * Get the offset for an address. |
| 370 | */ |
| 371 | static off_t addrToOffset(const Binary *B, intptr_t lo, intptr_t hi) |
| 372 | { |
| 373 | const Elf64_Phdr *phdrs = |
| 374 | (const Elf64_Phdr *)(B->patched.bytes + B->elf.ehdr->e_phoff); |
| 375 | size_t phnum = (size_t)B->elf.ehdr->e_phnum; |
| 376 | for (unsigned i = 0; i < phnum; i++) |
| 377 | { |
| 378 | const Elf64_Phdr *phdr = phdrs + i; |
| 379 | switch (phdr->p_type) |
| 380 | { |
| 381 | case PT_LOAD: case PT_GNU_RELRO: |
| 382 | break; |
| 383 | default: |
| 384 | continue; |
| 385 | } |
| 386 | intptr_t base = (intptr_t)phdr->p_vaddr; |
| 387 | size_t size = (size_t)phdr->p_filesz; |
| 388 | off_t offset = (off_t)phdr->p_offset; |
| 389 | if (lo >= base && hi < base + (ssize_t)size) |
| 390 | return offset + (lo - base); |
| 391 | } |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Replace an init/fini function. |