forked from taviso/loadlibrary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextra.c
71 lines (61 loc) · 1.92 KB
/
extra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Copyright (C) 2017 Tavis Ormandy
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>
#include <err.h>
#include "winnt_types.h"
#include "pe_linker.h"
#include "ntoskernel.h"
#include "util.h"
#define MAX_EXTRA_EXPORTS 65535
struct wrap_export extra_exports[MAX_EXTRA_EXPORTS];
extern struct hsearch_data extraexports;
static void __destructor cleanup_extra_exports(void)
{
hdestroy_r(&extraexports);
}
// This code is designed to parse a .MAP file produced by IDA.
bool process_extra_exports(void *imagebase, size_t base, const char *filename)
{
char *name;
uintptr_t address;
size_t num = 0;
FILE *exports;
if ((exports = fopen(filename, "r")) == NULL) {
return false;
}
hcreate_r(MAX_EXTRA_EXPORTS, &extraexports);
while (!feof(exports)) {
if (fscanf(exports, "%*X:%X %m[^\n]", &address, &name) == 2) {
ENTRY e, *ep;
e.key = name;
e.data = (void *)((uintptr_t)(imagebase) + address + base);
hsearch_r(e, ENTER, &ep, &extraexports);
if (++num >= MAX_EXTRA_EXPORTS) {
warn("large number of extra symbols in %s, increase MAX_EXTRA_EXPORTS and rebuild", filename);
break;
}
} else {
fgetc(exports);
}
}
fclose(exports);
return true;
}