Chameleon

Chameleon Commit Details

Date:2011-02-09 17:26:27 (13 years 2 months ago)
Author:Evan Lojewski
Commit:741
Parents: 740
Message:Module system cleanup. KextPatcher compiled with -O3
Changes:
M/branches/meklort/i386/modules/KextPatcher/Makefile
M/branches/meklort/i386/boot2/modules.c
M/branches/meklort/i386/boot2/modules.h

File differences

branches/meklort/i386/boot2/modules.c
2323
2424
2525
26
26
2727
2828
2929
......
3939
4040
4141
42
4243
4344
4445
4546
46
47
4748
48
49
50
51
52
53
54
55
56
57
58
49
50
5951
60
52
6153
62
54
55
56
57
6358
6459
65
60
61
62
63
64
65
66
6667
6768
68
69
70
71
72
73
69
7470
7571
7672
7773
7874
7975
80
76
8177
8278
8379
......
113109
114110
115111
116
117112
118113
119114
115
120116
117
118
121119
122
123120
124121
125122
126123
127124
128125
129
130
131
126
132127
133128
134129
135
136
130
137131
138132
139133
......
141135
142136
143137
144
145
146
147138
148139
149140
......
151142
152143
153144
154
155145
156146
157
158
159
160147
161
162
163
148
149
150
151
164152
165
153
154
155
156
166157
167158
168159
169
170
160
161
171162
172
173163
174
164
165
175166
176167
177168
......
189180
190181
191182
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
183
184
185
186
210187
211188
189
212190
213191
214192
......
225203
226204
227205
228
229206
230
231207
208
232209
233210
234211
235
236
237
238
212
213
239214
240215
241216
......
255230
256231
257232
233
258234
259
260235
261236
262237
263
264
238
239
240
241
265242
266243
267
268244
269245
270246
......
277253
278254
279255
280
281256
282257
283
284
285
286
287
288
289
290
291
292
293
294258
295259
296260
unsigned long long textAddress = 0;
unsigned long long textSection = 0;
void* symbols_module_start = (void*)0xFFFFFFFF;// This will be modified post compile
void* symbols_module_start = (void*)0xFFFFFFFF;// TGlobal, value is populated by the makefile with actual address
/** Internal symbols, however there are accessor methods **/
moduleHook_t* moduleCallbacks = NULL;
*/
int init_module_system()
{
int retVal = 0;
void (*module_start)(void) = NULL;
char* module_data = symbols_module_start + BOOT2_ADDR;
// Intialize module system
if(symbols_module_start == (void*)0xFFFFFFFF)
if(symbols_module_start != (void*)0xFFFFFFFF)
{
return 0;// Module system (Symbols.dylib) was not compiled in
}
module_start = parse_mach(module_data, &load_module, &add_symbol);
if(module_start && module_start != (void*)0xFFFFFFFF)
{
// Notify the system that it was laoded
module_loaded(SYMBOLS_MODULE /*moduleName, moduleVersion, moduleCompat*/);
lookup_symbol = (void*)lookup_all_symbols(SYMBOL_LOOKUP_SYMBOL);
// Module system was compiled in (Symbols.dylib addr known)
module_start = parse_mach(module_data, &load_module, &add_symbol);
if((UInt32)lookup_symbol != 0xFFFFFFFF)
if(module_start && module_start != (void*)0xFFFFFFFF)
{
(*module_start)();// Start the module
// Notify the system that it was laoded
module_loaded(SYMBOLS_MODULE /*moduleName, moduleVersion, moduleCompat*/);
(*module_start)();// Start the module. This will point to load_all_modules due to the way the dylib was constructed.
execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);
DBG("Module %s Loaded.\n", SYMBOLS_MODULE);
return 1;
retVal = 1;
}
else
{
// The module does not have a valid start function
printf("Unable to start %s\n", SYMBOLS_MODULE); getc();
}
}
else
{
// The module does not have a valid start function
printf("Unable to start %s\n", SYMBOLS_MODULE); getc();
}
return 0;
return retVal;
}
/*
* Load all modules in the /Extra/modules/ directory
* Module depencdies will be loaded first
* MOdules will only be loaded once. When loaded a module must
* Modules will only be loaded once. When loaded a module must
* setup apropriete function calls and hooks as required.
* NOTE: To ensure a module loads after another you may
* link one module with the other. For dyld to allow this, you must
/*
* Load a module file in /Extra/modules
* TODO: verify version number of module
*/
int load_module(char* module)
{
int retVal = 1;
void (*module_start)(void) = NULL;
char modString[128];
int fh = -1;
// Check to see if the module has already been loaded
if(is_module_loaded(module))
{
return 1;
}
char modString[128];
int fh = -1;
sprintf(modString, "/Extra/modules/%s", module);
sprintf(modString, MODULE_PATH "%s", module);
fh = open(modString, 0);
if(fh < 0)
{
printf("Unable to locate module %s\n", modString); DBGPAUSE();
getc();
printf("WARNING: Unable to locate module %s\n", modString); DBGPAUSE();
return 0;
}
char* module_base = (char*) malloc(moduleSize);
if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)
{
//DBG("Module %s read in.\n", modString);
// Module loaded into memory, parse it
module_start = parse_mach(module_base, &load_module, &add_symbol);
{
// Notify the system that it was laoded
module_loaded(module/*moduleName, moduleVersion, moduleCompat*/);
(*module_start)();// Start the module
DBG("Module %s Loaded.\n", module); DBGPAUSE();
//module_entry = malloc(sizeof(moduleList_t); TODO: mode to module_loaded
}
else {
// The module does not have a valid start function
printf("Unable to start %s\n", module);
#if DEBUG_MODULES
else // The module does not have a valid start function. This may be a library.
{
printf("WARNING: Unable to start %s\n", module);
getc();
}
}
#else
else msglog("WARNING: Unable to start %s\n", module);
#endif
}
else
{
DBG("Unable to read in module %s\n.", module);
getc();
DBG("Unable to read in module %s\n.", module); DBGPAUSE();
retVal = 0;
}
close(fh);
return 1;
close(fh);
return retVal;
}
/*
symbolList_t* entry;
//DBG("Adding symbol %s at 0x%X\n", symbol, addr);
if(!moduleSymbols)
{
moduleSymbols = entry = malloc(sizeof(symbolList_t));
}
else
{
entry = moduleSymbols;
while(entry->next)
{
entry = entry->next;
}
entry->next = malloc(sizeof(symbolList_t));
entry = entry->next;
}
entry->next = NULL;
entry = malloc(sizeof(symbolList_t));
entry->next = moduleSymbols;
moduleSymbols = entry;
entry->addr = (UInt32)addr;
entry->symbol = symbol;
if(strcmp(symbol, "start") == 0)
{
return addr;
*/
void module_loaded(const char* name/*, UInt32 version, UInt32 compat*/)
{
// TODO: insert sorted
moduleList_t* new_entry = malloc(sizeof(moduleList_t));
new_entry->next = loadedModules;
loadedModules = new_entry;
new_entry->name = (char*)name;
new_entry->base_addr = NULL;// TODO
// todo; symbols
new_entry->version = 0; //version;
new_entry->compat = 0; //compat;
//new_entry->version = version;
//new_entry->compat = compat;
}
int is_module_loaded(const char* name)
}
}
DBG("Module %s not found\n", name); DBGPAUSE();
return 0;
}
// Look for symbols using the Smbols moduel function.
// If non are found, look through the list of module symbols
/*
*lookup symbols in all loaded modules. Thins inludes boot syms due to Symbols.dylib construction
*
*/
unsigned int lookup_all_symbols(const char* name)
{
unsigned int addr = 0xFFFFFFFF;
symbolList_t* entry = moduleSymbols;
while(entry)
{
{
entry = entry->next;
}
}
if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)
{
addr = lookup_symbol(name);
if(addr != 0xFFFFFFFF)
{
//DBG("Internal symbol %s located at 0x%X\n", name, addr);
return addr;
}
}
#if DEBUG_MODULES
verbose("Unable to locate symbol %s\n", name);
getc();
branches/meklort/i386/boot2/modules.h
88
99
1010
11
12
13
14
15
1611
1712
1813
14
1915
2016
21
22
23
24
25
26
27
28
29
3017
3118
3219
......
5643
5744
5845
59
60
61
62
63
64
65
66
46
47
6748
6849
6950
#include <mach-o/nlist.h>
// There is a bug with the module system / rebasing / binding
// that causes static variables to be incorrectly rebased or bound
// Disable static variables for the moment
// #define static
#ifndef __BOOT_MODULES_H
#define __BOOT_MODULES_H
#define MODULE_PATH"/Extra/modules/"
#define SYMBOLS_MODULE "Symbols.dylib"
#define SYMBOL_LOOKUP_SYMBOL"_lookup_symbol"
#define STUB_ENTRY_SIZE6
#define SECT_NON_LAZY_SYMBOL_PTR"__nl_symbol_ptr"
#define SECT_SYMBOL_STUBS"__symbol_stub"
#define VALID_FUNCTION(__x__)(__x__ && (void*)__x__ != (void*)0xFFFFFFFF)
extern unsigned long long textAddress;
extern unsigned long long textSection;
typedef struct modulesList_t
{
char*name;
UInt32version;
UInt32compat;
void*base_addr;
symbolList_t*exported_symbols;
symbolList_t*udefined_symbols;
//moduleHook_t*defined_hooks;
//UInt32version;
//UInt32compat;
struct modulesList_t* next;
} moduleList_t;
branches/meklort/i386/modules/KextPatcher/Makefile
88
99
1010
11
11
1212
1313
1414
MODULE_OBJS = kext_patcher.o inflate.o deflate.o zutil.o inftrees.o inffast.o adler32.o hex_editor.o trees.o
OPTIM = -Os -Oz
OPTIM = -O3
DEBUG = -DNOTHING
#DEBUG = -DDEBUG_HELLO_WORLD=1
CFLAGS= $(RC_CFLAGS) $(OPTIM) $(MORECPP) -arch i386 -g -Wmost \

Archive Download the corresponding diff file

Revision: 741