summaryrefslogtreecommitdiff
path: root/other/Kermit/lib/SymbolTable.cpp
blob: 1d412e0bb07c2607f1a65c4cef9ec0ea1126d9b6 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * SymbolTable.cpp:
 * written by palmers / teso
 */
#include <SymbolTable.hpp>


/*
 * helper classes
 */
class DumpTable
  {
private:
    string		file;

public:
    DumpTable (string a)
    {
      file = string (a);
    }

    ~DumpTable ()
    {
    }

    void operator() (zzSym *foo)
    {
      ofstream dump (file.c_str ());
      if (!dump.is_open ())
	{
	  cerr << "Pfff..." << endl;
	  abort ();
	}
      dump << foo->Name << "\tE\t" << foo->Address << endl;
      dump.close ();
    }
  };


class FindFind
  {
  private:
    string		search;

  public:
    FindFind (string a)
    {
      search = string (a);
    }

    bool operator() (struct zzSym *foo)
    {
      if (search.compare (foo->Name, search.length ()))
	{
	  return true;
	}
     return false;
    }
  };


/*
 * member functions
 */
  bool SymbolTable::loadFiles (string a, string b)
  {
/* check for System.map */
    mapp = SystemMap (a);

/* load a dumped cache */
    rest = SystemMap (b);
    return true;
  }


  bool SymbolTable::createObjects (rwKernel *a)
  {
    if (a == NULL)
      patt = new DevMemPatt ();
    else
      patt = new DevMemPatt (a);
    fing = new SymbolFingp ();
    return true;
  }


  SymbolTable::SymbolTable ()
  {
    loadFiles (DEFAULTSYSTEMMAP, DEFAULTDUMP);
    createObjects (NULL);

/* XXX, init set of exported symbols */
    if (false)
      {
        exported = SystemMap ();
      }
  }


  SymbolTable::SymbolTable (string res, string sys)
  {
    loadFiles (sys, res);
    createObjects (NULL);

/* XXX, init set of exported symbols */
    if (false)
      {
        exported = SystemMap ();
      }
  }


  SymbolTable::SymbolTable (rwKernel *f)
  {
    loadFiles (DEFAULTSYSTEMMAP, DEFAULTDUMP);
    createObjects (f);
  }


  SymbolTable::~SymbolTable ()
  {
    delete fing;
    delete patt;
    clearCache ();
  }


  void SymbolTable::setSaveFile (string file)
  {
    dump_file = string (file);
  }


  unsigned int SymbolTable::getSymbol (string name)
  {
    zzSymList::iterator	x;
    zzSym		*y = NULL;

    x = find_if (symList.begin (), symList.end (), FindFind (name));
    if (x == symList.end ())
      return 0;
    y = *x;
    return y->Address;
  }


  bool SymbolTable::findSymbol (string name)
  {
    unsigned int	x = 0;
/*
 * first  check if the symbol can be found in restore date
 * second check if the symbol can be found using SymbolFingp
 * third  is list of exported symbols
 * fourth System.map (if supplied)
 */
    if (rest.contains (name))
      {
	addSymbolToCache (name, rest[name]);
	return true;
      }

    if ((x = patt->find_patt (fing->getFinger (name))) != 0)
      {
	addSymbolToCache (name, x);
	return true;
      }

    if (exported.contains (name))
      {
	addSymbolToCache (name, exported[name]);
	return true;
      }

    if (mapp.contains (name))
      {
	addSymbolToCache (name, mapp[name]);
	return true;
      }

    return false;
  }


  void SymbolTable::addSymbolToCache (string name, unsigned int add)
  {
    zzSym		*x = new zzSym;

    x->Name = string (name);
    x->Address = add;
    symList.push_back (x);
  }


  void SymbolTable::clearCache ()
  {
    symList.clear ();
  }


  bool SymbolTable::saveCache ()
  {
    for_each (symList.begin (), symList.end (), DumpTable (dump_file));
    return true;
  }