1 | /*␊ |
2 | * Symbols.c␊ |
3 | * ␊ |
4 | * Module loader support module. This module is the first module to ever be loaded.␊ |
5 | * It contains a copy of each symbol inside ov the current version of chameleon as well ␊ |
6 | * as a strcmp function. Chameleon calls lookup_symbol to resolve internal symbols␊ |
7 | * when they are requested by a module. This module does *not* depend on any intenrla␊ |
8 | * symbols, as such it can be loaded without a symbol table initialized.␊ |
9 | *␊ |
10 | * Copyright (c) 2009 Evan Lojewski. All rights reserved.␊ |
11 | */␊ |
12 | ␊ |
13 | #include "Symbols.h"␊ |
14 | ␊ |
15 | static int strcmp(const char * s1, const char * s2);␊ |
16 | ␊ |
17 | void Symbols_start()␊ |
18 | {␊ |
19 | }␊ |
20 | ␊ |
21 | unsigned int lookup_symbol(const char* symbol)␊ |
22 | {␊ |
23 | ␉int upperLimit = sizeof(symbolList) / sizeof(symbolList[0]) - 1;␉␉␊ |
24 | ␉int lowerLimit = 0;␊ |
25 | ␉int compareIndex = (upperLimit - lowerLimit) >> 1; // Midpoint␊ |
26 | ␉int result;␊ |
27 | ␉␊ |
28 | ␉while((result = strcmp(symbol, symbolList[compareIndex].symbol)) != 0)␊ |
29 | ␉{␊ |
30 | ␉␉if(result > 0)␉// We need to search a HIGHER index␊ |
31 | ␉␉{␊ |
32 | ␉␉␉if(compareIndex != lowerLimit)␊ |
33 | ␉␉␉{␊ |
34 | ␉␉␉␉lowerLimit = compareIndex;␊ |
35 | ␉␉␉}␊ |
36 | ␉␉␉else␊ |
37 | ␉␉␉{␊ |
38 | ␉␉␉␉return 0xFFFFFFFF;␉// Symbol not found␊ |
39 | ␉␉␉}␊ |
40 | ␉␉␉compareIndex = (upperLimit + lowerLimit + 1) >> 1;␉// Midpoint, round up␊ |
41 | ␉␉}␊ |
42 | ␉␉else // We Need to search a LOWER index␊ |
43 | ␉␉{␊ |
44 | ␉␉␉if(compareIndex != upperLimit)␊ |
45 | ␉␉␉{␊ |
46 | ␉␉␉␉upperLimit = compareIndex;␊ |
47 | ␉␉␉}␊ |
48 | ␉␉␉else␊ |
49 | ␉␉␉{␊ |
50 | ␉␉␉␉return 0xFFFFFFFF;␉// Symbol not found␊ |
51 | ␉␉␉}␊ |
52 | ␉␉␉compareIndex = (upperLimit + lowerLimit) >> 1;␉// Midpoint, round down␊ |
53 | ␉␉}␊ |
54 | ␉}␊ |
55 | ␉return symbolList[compareIndex].addr;␊ |
56 | }␊ |
57 | ␊ |
58 | /*␊ |
59 | * strcmp - Copied from libsa/string.c due to symbols not able to be resolved at this point␊ |
60 | */␊ |
61 | static int strcmp(const char * s1, const char * s2)␊ |
62 | {␊ |
63 | ␉while (*s1 && (*s1 == *s2)) {␊ |
64 | ␉␉s1++;␊ |
65 | ␉␉s2++;␊ |
66 | ␉}␊ |
67 | ␉return (*s1 - *s2);␊ |
68 | }␊ |
69 | ␊ |
70 | ␊ |
71 | ␊ |
72 | |