2 * Copyright (C) 2010 gonzoj
4 * Please check the CREDITS file for further information.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "d2pointers.h"
32 const char *modules
[] =
33 { "D2Client.dll", "D2Common.dll", "D2Gfx.dll", "D2Lang.dll", "D2Win.dll",
34 "D2Net.dll", "D2Game.dll", "D2Launch.dll", "Fog.dll", "BNClient.dll",
35 "Storm.dll", "D2Cmp.dll", "D2Multi.dll" };
38 read_line(FILE *fd
, char *line
)
51 line
[n_bytes
- 1] = '\0';
55 line
[n_bytes
- 1] = chr
;
57 return n_bytes
== 0 ? 0 : 1;
61 get_module_path(const char *module
, char *path
)
65 sprintf(maps
, "/proc/%i/maps", pid
);
66 FILE *fd
= fopen(maps
, "r");
68 while (read_line(fd
, line
))
70 if (strstr(line
, module
) != NULL
)
72 strcpy(path
, strchr(line
, '/'));
83 populate_kernel32_funcs()
85 char module_kernel32
[512];
86 if (!get_module_path("kernel32.dll", module_kernel32
))
88 printf("err: could not find location of kernel32.dll\n");
91 void *h
= dlopen(module_kernel32
, RTLD_LAZY
);
94 printf("err: could not open %s\n", module_kernel32
);
99 for (func
= (void **) &_KERNEL32_FUNC_START
, str
100 = (char **) &_KERNEL32_STR_START
; func
<= (void **) &_KERNEL32_FUNC_END
; func
++, str
++)
102 *func
= dlsym(h
, *str
);
105 printf("err: could not resolve symbol %s\n", *str
);
108 DEBUG_DO(printf("%s: 0x%08X\n", *str
, (vaddr
) *func
);)
114 populate_user32_funcs()
116 char module_user32
[512];
117 if (!get_module_path("user32.dll", module_user32
))
119 printf("err: could not find location of user32.dll\n");
122 void *h
= dlopen(module_user32
, RTLD_LAZY
);
125 printf("err: could not open %s\n", module_user32
);
130 for (func
= (void **) &_USER32_FUNC_START
, str
= (char **) &_USER32_STR_START
; func
131 <= (void **) &_USER32_FUNC_END
; func
++, str
++)
133 *func
= dlsym(h
, *str
);
136 printf("err: could not resolve symbol %s\n", *str
);
139 DEBUG_DO(printf("%s: 0x%08X\n", *str
, (vaddr
) *func
);)
147 if (GetModuleHandleA
== NULL
|| GetProcAddress
== NULL
|| LoadLibraryA
150 printf("err: necessary kernel32 functions missing\n");
154 for (func
= (void **) &_D2FUNCS_START
; func
<= (void **) &_D2FUNCS_END
; func
++)
156 int index
= (vaddr
) *func
& 0xff;
157 int offset
= (int) *func
>> 8;
159 if (((int) *func
>> 8) > 0)
161 module
= LoadLibraryA(modules
[index
]);
164 printf("err: could not get a handle for %s\n", modules
[index
]);
167 *func
= module
+ offset
;
168 DEBUG_DO(printf("populated function pointer (0x%08X) to %s (0x%08X) + 0x%08X\n", (vaddr
) *func
, modules
[index
], (vaddr
) module
, offset
);)
172 module
= GetModuleHandleA(modules
[index
]);
175 printf("err: could not get a handle for %s\n", modules
[index
]);
178 *func
= GetProcAddress(module
, -offset
);
182 "err: could not resolve exported function with ordinal %i\n",
186 DEBUG_DO(printf("populated function pointer (0x%08X) to %s (0x%08X) -> %i\n", (vaddr
) *func
, modules
[index
], (vaddr
) module
, -offset
);)
196 if (GetModuleHandleA
== NULL
|| GetProcAddress
== NULL
|| LoadLibraryA
199 printf("err: necessary kernel32 functions missing\n");
203 for (var
= (void **) &_D2VARS_START
; var
<= (void **) &_D2VARS_END
; var
++)
205 int index
= (vaddr
) *var
& 0xff;
206 int offset
= (vaddr
) *var
>> 8;
207 void *module
= LoadLibraryA(modules
[index
]);
210 printf("err: could not get a handle for %s\n", modules
[index
]);
213 *var
= module
+ offset
;
214 DEBUG_DO(printf("populated variable pointer (0x%08X) to %s (0x%08X) + 0x%08X\n", (vaddr
) *var
, modules
[index
], (vaddr
) module
, offset
);)