1 /* This file is part of centaur.
3 * centaur is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License 2 as
5 * published by the Free Software Foundation.
7 * centaur is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with centaur. If not, see <http://www.gnu.org/licenses/>.
16 #include <libelfu/libelfu.h>
19 static void* subFindByAddr(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
21 GElf_Addr a = *(GElf_Addr*)aux1;
23 if (OVERLAPPING(ms->shdr.sh_addr, ms->shdr.sh_size, a, 1)) {
32 void elfu_mDetour(ElfuElf *me, GElf_Addr from, GElf_Addr to)
36 unsigned char detourcode[] = {0xe9, 0xfc, 0xff, 0xff, 0xff};
38 ms = elfu_mScnForall(me, subFindByAddr, &from, NULL);
41 ELFU_WARN("mDetour: Cannot find address %x in any section.\n",
46 if (ms->shdr.sh_type != SHT_PROGBITS) {
47 ELFU_WARN("mDetour: Cannot detour in non-PROGBITS section %s.\n",
48 elfu_mScnName(me, ms));
52 scnoffset = from - ms->shdr.sh_addr;
54 if (ms->shdr.sh_size - scnoffset < 5) {
55 ELFU_WARN("mDetour: Not enough space to insert a detour.\n");
59 ELFU_DEBUG("mDetour: Detouring at address %x in section %s to %x.\n",
61 elfu_mScnName(me, ms),
64 *(Elf32_Word*)(detourcode + 1) = to - from - 5;
65 memcpy(ms->databuf + scnoffset, detourcode, 5);