1 /*
2 ** tables.c for elfsh
3 **
4 ** A C++ code for this may have been easier to implement
5 **
6 ** Started on  Sat Jan 25 07:48:41 2003 mayhem
7 ** Last update Fri Jul  4 03:21:25 2003 mayhem
8 */
9 #include "elfsh.h"
10 
11 
12 
13 
14 /* The command hash table : hash the command name and returns a elfshcmd_t */
15 hash_t		cmd_hash;
16 
17 /* The constants hash tables : ascii defines from elf.h and elfsh.h */
18 hash_t		const_hash;
19 
20 /* The ELF shell module hash tables: returns a elfshmod_t */
21 hash_t		mod_hash;
22 
23 /* The file hash table : hash the filename and returns a elfshobj_t pointer */
24 hash_t		file_hash;
25 
26 /* The Level 1 object hash table : hash the object name and returns a elfshL1_t pointer */
27 hash_t		L1_hash;
28 
29 /* The Level 2 object hash table list : hash the object name and returns a elfshL2_t pointer */
30 hash_t		elf_L2_hash;	/* For the ELF header fields */
31 hash_t		sht_L2_hash;	/* For the Sections/SHT objects */
32 hash_t		pht_L2_hash;	/* For the Program header table objects */
33 hash_t		sym_L2_hash;	/* For symbol object field */
34 hash_t		rel_L2_hash;	/* For relocation entry fields */
35 hash_t		dynsym_L2_hash;	/* For .dynsym objects */
36 hash_t		dyn_L2_hash;	/* For .dynamic objects */
37 hash_t		sct_L2_hash;	/* Section data (byte/word/dword/instr arrays) */
38 
39 /* hash_t	got_L2_hash;	*/ /* GOT objects */
40 
41 
42 
43 
44 /* Fill all the Level 1 Objects hash tables */
setup_L1hash()45 static void	setup_L1hash()
46 {
47   hash_init(&L1_hash, 29);
48   hash_add(&L1_hash, "hdr"     , (void *) vm_create_L1ENT(elfsh_get_hdr,
49 							  NULL, NULL,
50 							  &elf_L2_hash,
51 							  NULL, NULL, NULL, 0));
52 
53   hash_add(&L1_hash, "pht"     , (void *) vm_create_L1ENT(elfsh_get_pht,
54 							  NULL, NULL,
55 							  &pht_L2_hash,
56 							  elfsh_get_pht_entry_by_index,
57 							  NULL, NULL,
58 							  sizeof (Elf32_Phdr)));
59 
60   hash_add(&L1_hash, "symtab"  , (void *) vm_create_L1ENT(elfsh_get_symtab,
61 							  NULL,
62 							  elfsh_get_symbol_by_name,
63 							  &sym_L2_hash,
64 							  elfsh_get_symbol_by_index,
65 							  NULL, NULL,
66 							  sizeof (Elf32_Sym)));
67 
68   hash_add(&L1_hash, "dynsym"  , (void *) vm_create_L1ENT(elfsh_get_dynsymtab,
69 							  NULL,
70 							  elfsh_get_dynsymbol_by_name,
71 							  &dynsym_L2_hash,
72 							  elfsh_get_symbol_by_index,
73 							  NULL, NULL,
74 							  sizeof (Elf32_Sym)));
75 
76 
77   hash_add(&L1_hash, "got"     , (void *) vm_create_L1ENT(elfsh_get_got,
78 							  NULL,
79 							  elfsh_get_got_entry_by_name,
80 							  NULL,
81 							  elfsh_get_got_entry_by_index,
82 							  elfsh_get_got_entry,
83 							  elfsh_set_got_entry,
84 							  sizeof (long)));
85 
86 
87   hash_add(&L1_hash, "dynamic" , (void *) vm_create_L1ENT(elfsh_get_dynamic,
88 							  NULL, NULL,
89 							  &dyn_L2_hash,
90 							  elfsh_get_dynamic_entry_by_index,
91 							  NULL, NULL,
92 							  sizeof (Elf32_Dyn)));
93 
94   hash_add(&L1_hash, "ctors"   , (void *) vm_create_L1ENT(elfsh_get_ctors,
95 							  NULL,
96 							  elfsh_get_ctors_entry_by_name,
97 							  NULL,
98 							  elfsh_get_ctors_entry_by_index,
99 							  elfsh_get_ctors_entry,
100 							  elfsh_set_ctors_entry,
101 							  sizeof (long)));
102 
103   hash_add(&L1_hash, "dtors"   , (void *) vm_create_L1ENT(elfsh_get_dtors,
104 							  NULL,
105 							  elfsh_get_dtors_entry_by_name,
106 							  NULL,
107 							  elfsh_get_dtors_entry_by_index,
108 							  elfsh_get_dtors_entry,
109 							  elfsh_set_dtors_entry,
110 							  sizeof (long)));
111 
112   hash_add(&L1_hash, "rel"     , (void *) vm_create_L1ENT(NULL,
113 							  elfsh_get_reloc,
114 							  elfsh_get_relent_by_name,
115 							  &rel_L2_hash,
116 							  elfsh_get_relent_by_index,
117 							  NULL, NULL,
118 							  sizeof (Elf32_Rel)));
119 
120   hash_add(&L1_hash, "sht"     , (void *) vm_create_L1ENT(elfsh_get_sht,
121 							  NULL,
122 							  elfsh_get_sht_entry_by_name,
123 							  &sht_L2_hash,
124 							  elfsh_get_sht_entry_by_index,
125 							  NULL, NULL,
126 							  sizeof (Elf32_Shdr)));
127 
128   hash_add(&L1_hash, "section", (void *) vm_create_L1ENT(elfsh_get_section_list,
129 							 NULL,
130 							 elfsh_get_section_by_nam,
131 							 &sct_L2_hash,
132 							 elfsh_get_section_by_idx,
133 							 NULL, NULL,
134 							 sizeof (elfshsect_t)));
135 }
136 
137 
138 /* Hash table for ELF header fields */
setup_elfhash()139 static void	setup_elfhash()
140 {
141 
142   hash_init(&elf_L2_hash, 29);
143   hash_add(&elf_L2_hash, "magic"        , vm_create_L2ENT(elfsh_get_magic,
144 							  elfsh_set_magic, 0,
145 							  NULL, NULL,
146 							  NULL, NULL));
147 
148   hash_add(&elf_L2_hash, "class"        , vm_create_L2ENT(elfsh_get_class,
149 							  elfsh_set_class, 0,
150 							  NULL, NULL,
151 							  NULL, NULL));
152   hash_add(&elf_L2_hash, "type"         , vm_create_L2ENT(elfsh_get_objtype,
153 							  elfsh_set_objtype, 0,
154 							  NULL, NULL,
155 							  NULL, NULL));
156   hash_add(&elf_L2_hash, "machine"      , vm_create_L2ENT(elfsh_get_arch,
157 							  elfsh_set_arch, 0,
158 							  NULL, NULL,
159 							  NULL, NULL));
160   hash_add(&elf_L2_hash, "version"      , vm_create_L2ENT(elfsh_get_version,
161 							  elfsh_set_version, 0,
162 							  NULL, NULL,
163 							  NULL, NULL));
164   hash_add(&elf_L2_hash, "entry"        , vm_create_L2ENT(elfsh_get_entrypoint,
165 							  elfsh_set_entrypoint, 0,
166 							  NULL, NULL,
167 							  NULL, NULL));
168   hash_add(&elf_L2_hash, "phoff"        , vm_create_L2ENT(elfsh_get_phtoff,
169 							  elfsh_set_phtoff, 0,
170 							  NULL, NULL,
171 							  NULL, NULL));
172   hash_add(&elf_L2_hash, "shoff"        , vm_create_L2ENT(elfsh_get_shtoff,
173 							  elfsh_set_shtoff, 0,
174 							  NULL, NULL, NULL, NULL));
175   hash_add(&elf_L2_hash, "flags"        , vm_create_L2ENT(elfsh_get_flags,
176 							  elfsh_set_flags, 0,
177 							  NULL, NULL,
178 							  NULL, NULL));
179   hash_add(&elf_L2_hash, "ehsize"       , vm_create_L2ENT(elfsh_get_ehsize,
180 							  elfsh_set_ehsize, 0,
181 							  NULL, NULL,
182 							  NULL, NULL));
183   hash_add(&elf_L2_hash, "phentsize"    , vm_create_L2ENT(elfsh_get_phentsize,
184 							  elfsh_set_phentsize, 0,
185 							  NULL, NULL,
186 							  NULL, NULL));
187   hash_add(&elf_L2_hash, "shentsize"    , vm_create_L2ENT(elfsh_get_shentsize,
188 							  elfsh_set_shentsize, 0,
189 							  NULL, NULL,
190 							  NULL, NULL));
191   hash_add(&elf_L2_hash, "phnum"        , vm_create_L2ENT(elfsh_get_phtnbr,
192 							  elfsh_set_phtnbr, 0,
193 							  NULL, NULL,
194 							  NULL, NULL));
195   hash_add(&elf_L2_hash, "shnum"        , vm_create_L2ENT(elfsh_get_shtnbr,
196 							  elfsh_set_shtnbr, 0,
197 							  NULL, NULL,
198 							  NULL, NULL));
199   hash_add(&elf_L2_hash, "shstrndx"     , vm_create_L2ENT(elfsh_get_shstrtab_index,
200 							  elfsh_set_shstrtab_index, 0,
201 							  NULL, NULL,
202 							  NULL, NULL));
203   hash_add(&elf_L2_hash, "pax_pageexec" , vm_create_L2ENT(elfsh_get_pax_pageexec,
204 							  elfsh_set_pax_pageexec, 0,
205 							  NULL, NULL,
206 							  NULL, NULL));
207   hash_add(&elf_L2_hash, "pax_emultramp", vm_create_L2ENT(elfsh_get_pax_emultramp,
208 							  elfsh_set_pax_emultramp, 0,
209 							  NULL, NULL,
210 							  NULL, NULL));
211   hash_add(&elf_L2_hash, "pax_mprotect" , vm_create_L2ENT(elfsh_get_pax_mprotect,
212 							  elfsh_set_pax_mprotect, 0,
213 							  NULL, NULL,
214 							  NULL, NULL));
215   hash_add(&elf_L2_hash, "pax_randmmap" , vm_create_L2ENT(elfsh_get_pax_randmmap,
216 							  elfsh_set_pax_randmmap, 0,
217 							  NULL, NULL,
218 							  NULL, NULL));
219   hash_add(&elf_L2_hash, "pax_randexec" , vm_create_L2ENT(elfsh_get_pax_randexec,
220 							  elfsh_set_pax_randexec, 0,
221 							  NULL, NULL,
222 							  NULL, NULL));
223   hash_add(&elf_L2_hash, "pax_segmexec" , vm_create_L2ENT(elfsh_get_pax_segmexec,
224 							  elfsh_set_pax_segmexec, 0,
225 							  NULL, NULL,
226 							  NULL, NULL));
227 }
228 
229 
230 
231 /* Hash table for SHT fields */
setup_shthash()232 static void	setup_shthash()
233 {
234   hash_init(&sht_L2_hash, 29);
235   hash_add(&sht_L2_hash, "type"     , vm_create_L2ENT(elfsh_get_section_type,
236 						      elfsh_set_section_type,
237 						      0, NULL, NULL, NULL, NULL));
238   hash_add(&sht_L2_hash, "addr"     , vm_create_L2ENT(elfsh_get_section_addr,
239 						      elfsh_set_section_addr,
240 						      0, NULL, NULL, NULL, NULL));
241   hash_add(&sht_L2_hash, "offset"   , vm_create_L2ENT(elfsh_get_section_foffset,
242 						      elfsh_set_section_foffset,
243 						      0, NULL, NULL, NULL, NULL));
244   hash_add(&sht_L2_hash, "size"     , vm_create_L2ENT(elfsh_get_section_size,
245 						      elfsh_set_section_size,
246 						      0, NULL, NULL, NULL, NULL));
247   hash_add(&sht_L2_hash, "link"     , vm_create_L2ENT(elfsh_get_section_link,
248 						      elfsh_set_section_link,
249 						      0, NULL, NULL, NULL, NULL));
250   hash_add(&sht_L2_hash, "info"     , vm_create_L2ENT(elfsh_get_section_info,
251 						      elfsh_set_section_info,
252 						      0, NULL, NULL, NULL, NULL));
253   hash_add(&sht_L2_hash, "align"    , vm_create_L2ENT(elfsh_get_section_align,
254 						      elfsh_set_section_align,
255 						      0, NULL, NULL, NULL, NULL));
256   hash_add(&sht_L2_hash, "entsize"  , vm_create_L2ENT(elfsh_get_section_entsize,
257 						      elfsh_set_section_entsize,
258 						      0, NULL, NULL, NULL, NULL));
259   hash_add(&sht_L2_hash, "a"  , vm_create_L2ENT(elfsh_get_section_allocflag,
260 						elfsh_set_section_allocflag,
261 						0, NULL, NULL, NULL, NULL));
262   hash_add(&sht_L2_hash, "w"  , vm_create_L2ENT(elfsh_get_section_writableflag,
263 						elfsh_set_section_writableflag,
264 						0, NULL, NULL, NULL, NULL));
265   hash_add(&sht_L2_hash, "x"  , vm_create_L2ENT(elfsh_get_section_execflag,
266 						elfsh_set_section_execflag,
267 						0, NULL, NULL, NULL, NULL));
268   hash_add(&sht_L2_hash, "s"  , vm_create_L2ENT(elfsh_get_section_strflag,
269 						elfsh_set_section_strflag,
270 						0, NULL, NULL, NULL, NULL));
271   hash_add(&sht_L2_hash, "m"  , vm_create_L2ENT(elfsh_get_section_mergeableflag,
272 						elfsh_set_section_mergeableflag,
273 						0, NULL, NULL, NULL, NULL));
274   hash_add(&sht_L2_hash, "l"  , vm_create_L2ENT(elfsh_get_section_linkflag,
275 						elfsh_set_section_linkflag,
276 						0, NULL, NULL, NULL, NULL));
277   hash_add(&sht_L2_hash, "o"  , vm_create_L2ENT(elfsh_get_section_orderflag,
278 						elfsh_set_section_orderflag,
279 						0, NULL, NULL, NULL, NULL));
280 
281 
282 }
283 
284 /* Hash table for PHT fields */
setup_phthash()285 static void	setup_phthash()
286 {
287   hash_init(&pht_L2_hash, 29);
288   hash_add(&pht_L2_hash, "type"  , vm_create_L2ENT(elfsh_get_segment_type,
289 						   elfsh_set_segment_type,
290 						   0, NULL, NULL, NULL, NULL));
291   hash_add(&pht_L2_hash, "offset", vm_create_L2ENT(elfsh_get_segment_offset,
292 						   elfsh_set_segment_offset,
293 						   0, NULL, NULL, NULL, NULL));
294   hash_add(&pht_L2_hash, "vaddr" , vm_create_L2ENT(elfsh_get_segment_vaddr,
295 						   elfsh_set_segment_vaddr,
296 						   0, NULL, NULL, NULL, NULL));
297   hash_add(&pht_L2_hash, "paddr" , vm_create_L2ENT(elfsh_get_segment_paddr,
298 						   elfsh_set_segment_paddr,
299 						   0, NULL, NULL, NULL, NULL));
300   hash_add(&pht_L2_hash, "filesz", vm_create_L2ENT(elfsh_get_segment_filesz,
301 						   elfsh_set_segment_filesz,
302 						   0, NULL, NULL, NULL, NULL));
303   hash_add(&pht_L2_hash, "memsz" , vm_create_L2ENT(elfsh_get_segment_memsz,
304 						   elfsh_set_segment_memsz,
305 						   0, NULL, NULL, NULL, NULL));
306   hash_add(&pht_L2_hash, "flags" , vm_create_L2ENT(elfsh_get_segment_flags,
307 						   elfsh_set_segment_flags,
308 						   0, NULL, NULL, NULL, NULL));
309   hash_add(&pht_L2_hash, "align" , vm_create_L2ENT(elfsh_get_segment_align,
310 						   elfsh_set_segment_align,
311 						   0, NULL, NULL, NULL, NULL));
312 }
313 
314 
315 /* Hash table for symbol table */
setup_symhash()316 static void	setup_symhash()
317 {
318   hash_init(&sym_L2_hash, 23);
319 
320   hash_add(&sym_L2_hash, "name" , vm_create_L2ENT(NULL, NULL, ELFSH_OBJSTR,
321 						  elfsh_get_symbol_name,
322 						  elfsh_set_symbol_name,
323 						  NULL, NULL));
324 
325   hash_add(&sym_L2_hash, "value", vm_create_L2ENT(elfsh_get_symbol_value,
326 						  elfsh_set_symbol_value,
327 						  0, NULL, NULL, NULL, NULL));
328   hash_add(&sym_L2_hash, "size" , vm_create_L2ENT(elfsh_get_symbol_size,
329 						  elfsh_set_symbol_size,
330 						  0, NULL, NULL, NULL, NULL));
331   hash_add(&sym_L2_hash, "bind" , vm_create_L2ENT(elfsh_get_symbol_bind,
332 						  elfsh_set_symbol_bind,
333 						  0, NULL, NULL, NULL, NULL));
334   hash_add(&sym_L2_hash, "type" , vm_create_L2ENT(elfsh_get_symbol_type,
335 						  elfsh_set_symbol_type,
336 						  0, NULL, NULL, NULL, NULL));
337   hash_add(&sym_L2_hash, "other", vm_create_L2ENT(elfsh_get_symbol_visibility,
338 						  elfsh_set_symbol_visibility,
339 						  0, NULL, NULL, NULL, NULL));
340 
341   hash_add(&sym_L2_hash, "link", vm_create_L2ENT(elfsh_get_symbol_link,
342 						 elfsh_set_symbol_link,
343 						 0, NULL, NULL, NULL, NULL));
344 
345 
346 }
347 
348 /* Hash table for dynamic symbol table */
setup_dynsymhash()349 static void	setup_dynsymhash()
350 {
351   hash_init(&dynsym_L2_hash, 23);
352 
353   hash_add(&dynsym_L2_hash, "name" , vm_create_L2ENT(NULL, NULL, ELFSH_OBJSTR,
354 						     elfsh_get_dynsymbol_name,
355 						     elfsh_set_dynsymbol_name,
356 						     NULL, NULL));
357 
358   hash_add(&dynsym_L2_hash, "value", vm_create_L2ENT(elfsh_get_symbol_value,
359 						     elfsh_set_symbol_value,
360 						     0, NULL, NULL, NULL, NULL));
361   hash_add(&dynsym_L2_hash, "size" , vm_create_L2ENT(elfsh_get_symbol_size,
362 						     elfsh_set_symbol_size,
363 						     0, NULL, NULL, NULL, NULL));
364   hash_add(&dynsym_L2_hash, "bind" , vm_create_L2ENT(elfsh_get_symbol_bind,
365 						     elfsh_set_symbol_bind,
366 						     0, NULL, NULL, NULL, NULL));
367   hash_add(&dynsym_L2_hash, "type" , vm_create_L2ENT(elfsh_get_symbol_type,
368 						     elfsh_set_symbol_type,
369 						     0, NULL, NULL, NULL, NULL));
370   hash_add(&dynsym_L2_hash, "other", vm_create_L2ENT(elfsh_get_symbol_visibility,
371 						     elfsh_set_symbol_visibility,
372 						     0, NULL, NULL, NULL, NULL));
373 
374   hash_add(&dynsym_L2_hash, "link", vm_create_L2ENT(elfsh_get_symbol_link,
375 						    elfsh_set_symbol_link,
376 						    0, NULL, NULL, NULL, NULL));
377 
378 
379 }
380 
381 /* Hash table for .rel sections */
setup_relhash()382 static void	setup_relhash()
383 {
384   hash_init(&rel_L2_hash, 23);
385   hash_add(&rel_L2_hash, "type"  , vm_create_L2ENT(elfsh_get_reltype,
386 						   elfsh_set_reltype,
387 						   0, NULL, NULL, NULL, NULL));
388   hash_add(&rel_L2_hash, "sym"   , vm_create_L2ENT(elfsh_get_relsym,
389 						   elfsh_set_relsym,
390 						   0, NULL, NULL, NULL, NULL));
391   hash_add(&rel_L2_hash, "offset", vm_create_L2ENT(elfsh_get_reloffset,
392 						   elfsh_set_reloffset,
393 						   0, NULL, NULL, NULL, NULL));
394   hash_add(&rel_L2_hash, "addend", vm_create_L2ENT(elfsh_get_reladdend,
395 						   elfsh_set_reladdend,
396 						   0, NULL, NULL, NULL, NULL));
397 }
398 
399 /* Hash table for .dynamic section */
setup_dynhash()400 static void	setup_dynhash()
401 {
402   hash_init(&dyn_L2_hash, 11);
403   hash_add(&dyn_L2_hash, "val", vm_create_L2ENT(elfsh_get_dynentry_val,
404 						elfsh_set_dynentry_val,
405 						0, NULL, NULL, NULL, NULL));
406   hash_add(&dyn_L2_hash, "tag", vm_create_L2ENT(elfsh_get_dynentry_type,
407 						elfsh_set_dynentry_type,
408 						0, NULL, NULL, NULL, NULL));
409 }
410 
411 
412 
413 /* Hash tables for GOT L2 objects */
414 /*
415 static void	setup_gothash()
416 {
417   hash_init(&got_L2_hash, 11);
418   hash_add(&got_L2_hash, "val" , vm_create_L2ENT(elfsh_get_got));
419   hash_add(&got_L2_hash, "idx" , vm_create_L2ENT());
420   hash_add(&got_L2_hash, "addr", vm_create_L2ENT());
421 }
422 */
423 
424 
425 /* Hash tables for sections data */
setup_scthash()426 static void	setup_scthash()
427 {
428   hash_init(&sct_L2_hash, 11);
429   hash_add(&sct_L2_hash, "name", vm_create_L2ENT(NULL, NULL, ELFSH_OBJSTR,
430 						 elfsh_get_section_name,
431 						 elfsh_set_section_name,
432 						 NULL, NULL));
433 
434   hash_add(&sct_L2_hash, "raw", vm_create_L2ENT(NULL, NULL,
435 						ELFSH_OBJRAW,
436 						NULL, NULL,
437 						elfsh_get_section_data,
438 						elfsh_write_section_data));
439 }
440 
441 
442 
443 /* Now comes Level 2 objects hash functions */
setup_L2hash()444 static void	setup_L2hash()
445 {
446   setup_elfhash();
447   setup_shthash();
448   setup_phthash();
449   setup_symhash();
450   setup_relhash();
451   setup_dynsymhash();
452   setup_dynhash();
453   setup_scthash();
454   /* setup_gothash(); */
455 }
456 
457 
458 
459 /* Setup the command hash table */
setup_cmdhash()460 static void	setup_cmdhash()
461 {
462   hash_init(&cmd_hash, 101);
463 
464   /* Interactive mode only commands */
465   if (world.state.vm_mode != ELFSH_VMSTATE_CMDLINE)
466     {
467       vm_addcmd(CMD_LOAD    , (void *) cmd_load     , (void *) vm_getoption, 0);
468       vm_addcmd(CMD_UNLOAD  , (void *) cmd_unload   , (void *) vm_getoption, 0);
469       vm_addcmd(CMD_SAVE    , (void *) cmd_save     , (void *) vm_getoption, 1);
470       vm_addcmd(CMD_SWITCH  , (void *) cmd_doswitch , (void *) vm_getoption, 1);
471       vm_addcmd(CMD_METACMD , (void *) cmd_meta     , (void *) NULL, 0);
472       vm_addcmd(CMD_QUIT    , (void *) cmd_quit     , (void *) NULL, 0);
473       vm_addcmd(CMD_QUIT2   , (void *) cmd_quit     , (void *) NULL, 0);
474       vm_addcmd(CMD_LIST    , (void *) cmd_dolist   , (void *) NULL, 0);
475       vm_addcmd(CMD_LIST2   , (void *) cmd_dolist   , (void *) NULL, 0);
476       vm_addcmd(CMD_STOP    , (void *) cmd_stop     , (void *) NULL, 0);
477     }
478 
479   /* Command line only commands */
480   else
481     {
482       vm_addcmd(CMD_BINFILE_W, (void *) NULL, (void *) vm_getoutput, 0);
483       vm_addcmd(CMD_BINFILE_R, (void *) NULL, (void *) vm_getinput , 0);
484     }
485 
486   /* General purpose command */
487   vm_addcmd(CMD_MODLOAD , (void *) cmd_modload  , (void *) vm_getoption, 0);
488   vm_addcmd(CMD_MODULOAD, (void *) cmd_modunload, (void *) vm_getoption, 0);
489   vm_addcmd(CMD_DISASM  , (void *) cmd_disasm  , (void *) vm_getdisasm    , 1);
490   vm_addcmd(CMD_HEXA    , (void *) cmd_disasm  , (void *) vm_gethexa      , 1);
491   vm_addcmd(CMD_DISASM2 , (void *) cmd_disasm  , (void *) vm_getdisasm    , 1);
492   vm_addcmd(CMD_HEXA2   , (void *) cmd_disasm  , (void *) vm_gethexa      , 1);
493   vm_addcmd(CMD_GET     , (void *) cmd_get     , (void *) vm_getoption    , 1);
494   vm_addcmd(CMD_PRINT   , (void *) cmd_print   , (void *) vm_getvarparams , 1);
495   vm_addcmd(CMD_EXEC    , (void *) cmd_exec    , (void *) vm_getvarparams , 0);
496   vm_addcmd(CMD_PHT     , (void *) cmd_pht     , (void *) vm_getregxoption, 1);
497   vm_addcmd(CMD_GOT     , (void *) cmd_got     , (void *) vm_getregxoption, 1);
498   vm_addcmd(CMD_CTORS   , (void *) cmd_ctors   , (void *) vm_getregxoption, 1);
499   vm_addcmd(CMD_DTORS   , (void *) cmd_dtors   , (void *) vm_getregxoption, 1);
500   vm_addcmd(CMD_NOTE    , (void *) cmd_notes   , (void *) vm_getregxoption, 1);
501   vm_addcmd(CMD_SYM     , (void *) cmd_sym     , (void *) vm_getregxoption, 1);
502   vm_addcmd(CMD_DYNAMIC , (void *) cmd_dyn     , (void *) vm_getregxoption, 1);
503   vm_addcmd(CMD_DYNSYM  , (void *) cmd_dynsym  , (void *) vm_getregxoption, 1);
504   vm_addcmd(CMD_SHT     , (void *) cmd_sht     , (void *) vm_getregxoption, 1);
505   vm_addcmd(CMD_STAB    , (void *) cmd_stab    , (void *) vm_getregxoption, 1);
506   vm_addcmd(CMD_REL     , (void *) cmd_rel     , (void *) vm_getregxoption, 1);
507   vm_addcmd(CMD_COMMENT , (void *) cmd_comments, (void *) vm_getregxoption, 1);
508   vm_addcmd(CMD_PHT2    , (void *) cmd_pht     , (void *) vm_getregxoption, 1);
509   vm_addcmd(CMD_GOT2    , (void *) cmd_got     , (void *) vm_getregxoption, 1);
510   vm_addcmd(CMD_CTORS2  , (void *) cmd_ctors   , (void *) vm_getregxoption, 1);
511   vm_addcmd(CMD_DTORS2  , (void *) cmd_dtors   , (void *) vm_getregxoption, 1);
512   vm_addcmd(CMD_NOTE2   , (void *) cmd_notes   , (void *) vm_getregxoption, 1);
513   vm_addcmd(CMD_SYM2    , (void *) cmd_sym     , (void *) vm_getregxoption, 1);
514   vm_addcmd(CMD_DYNAMIC2, (void *) cmd_dyn     , (void *) vm_getregxoption, 1);
515   vm_addcmd(CMD_DYNSYM2 , (void *) cmd_dynsym  , (void *) vm_getregxoption, 1);
516   vm_addcmd(CMD_SHT2    , (void *) cmd_sht     , (void *) vm_getregxoption, 1);
517   vm_addcmd(CMD_STAB2   , (void *) cmd_stab    , (void *) vm_getregxoption, 1);
518   vm_addcmd(CMD_REL2    , (void *) cmd_rel     , (void *) vm_getregxoption, 1);
519   vm_addcmd(CMD_COMMENT2, (void *) cmd_comments, (void *) vm_getregxoption, 1);
520   vm_addcmd(CMD_SET     , (void *) cmd_set     , (void *) vm_getoption2   , 1);
521   vm_addcmd(CMD_ADD     , (void *) cmd_add     , (void *) vm_getoption2   , 1);
522   vm_addcmd(CMD_SUB     , (void *) cmd_sub     , (void *) vm_getoption2   , 1);
523   vm_addcmd(CMD_MUL     , (void *) cmd_mul     , (void *) vm_getoption2   , 1);
524   vm_addcmd(CMD_DIV     , (void *) cmd_div     , (void *) vm_getoption2   , 1);
525   vm_addcmd(CMD_MOD     , (void *) cmd_set     , (void *) vm_getoption2   , 1);
526   vm_addcmd(CMD_INFO    , (void *) cmd_info    , (void *) NULL            , 0);
527   vm_addcmd(CMD_FIXUP   , (void *) cmd_fixup   , (void *) NULL            , 1);
528   vm_addcmd(CMD_ELF2    , (void *) cmd_elf     , (void *) NULL            , 1);
529   vm_addcmd(CMD_INTERP2 , (void *) cmd_interp  , (void *) NULL            , 1);
530   vm_addcmd(CMD_ELF     , (void *) cmd_elf     , (void *) NULL            , 1);
531   vm_addcmd(CMD_INTERP  , (void *) cmd_interp  , (void *) NULL            , 1);
532   vm_addcmd(CMD_HELP    , (void *) cmd_help    , (void *) NULL            , 0);
533   vm_addcmd(CMD_SHTRM   , (void *) cmd_shtrm   , (void *) NULL            , 1);
534   vm_addcmd(CMD_QUIET   , (void *) cmd_quiet   , (void *) NULL            , 0);
535   vm_addcmd(CMD_QUIET2  , (void *) cmd_quiet   , (void *) NULL            , 0);
536   vm_addcmd(CMD_VERB    , (void *) cmd_verb    , (void *) NULL            , 0);
537   vm_addcmd(CMD_VERB2   , (void *) cmd_verb    , (void *) NULL            , 0);
538   vm_addcmd(CMD_SORT    , (void *) NULL        , (void *) vm_getsort      , 0);
539   vm_addcmd(CMD_SORT2   , (void *) NULL        , (void *) vm_getsort      , 0);
540   vm_addcmd(CMD_ALL	, (void *) NULL        , (void *) vm_getglregx    , 0);
541   vm_addcmd(CMD_ALL2	, (void *) NULL        , (void *) vm_getglregx    , 0);
542   vm_addcmd(CMD_FINDREL , (void *) cmd_findrel , (void *) NULL            , 1);
543   vm_addcmd(CMD_STRIP   , (void *) cmd_strip   , (void *) NULL            , 1);
544   vm_addcmd(CMD_SSTRIP  , (void *) cmd_sstrip  , (void *) NULL            , 1);
545   vm_addcmd(CMD_WRITE	, (void *) cmd_write   , (void *) vm_getoption2   , 1);
546   vm_addcmd(CMD_APPEND  , (void *) cmd_append  , (void *) vm_getoption2   , 1);
547   vm_addcmd(CMD_EXTEND  , (void *) cmd_extend  , (void *) vm_getoption2   , 1);
548   vm_addcmd(CMD_RELINJCT, (void *) cmd_relinject,(void *) vm_getoption2   , 0);
549   vm_addcmd(CMD_HIJACK  , (void *) cmd_hijack  , (void *) vm_getoption2   , 1);
550 }
551 
552 
553 
554 /* Setup ELF constants hash tables */
setup_consthash()555 static void	setup_consthash()
556 {
557   u_int		index;
558 
559   hash_init(&const_hash, 51);
560   for (index = 0; index < ELFSH_SEGTYPE_MAX; index++)
561     hash_add(&const_hash,
562 	     (char *) elfsh_seg_type[index].name,
563 	     elfsh_seg_type + index);
564   for (index = 0; index < ELFSH_ENCODING_MAX; index++)
565     hash_add(&const_hash,
566 	     (char *) elfsh_encoding[index].name,
567 	     elfsh_encoding + index);
568   for (index = 0; index < ELFSH_SHTYPE_MAX; index++)
569     hash_add(&const_hash,
570 	     (char *) elfsh_sh_type[index].name,
571 	     elfsh_sh_type + index);
572   for (index = 0; index < ELFSH_OBJTYPE_MAX; index++)
573     hash_add(&const_hash,
574 	     (char *) elfsh_obj_type[index].name,
575 	     elfsh_obj_type + index);
576   for (index = 0; index < ELFSH_SYMBIND_MAX; index++)
577     hash_add(&const_hash,
578 	     (char *) elfsh_sym_bind[index].name,
579 	     elfsh_sym_bind + index);
580   for (index = 0; index < ELFSH_SYMTYPE_MAX; index++)
581     hash_add(&const_hash,
582 	     (char *) elfsh_sym_type[index].name,
583 	     elfsh_sym_type + index);
584   for (index = 0; index < ELFSH_RELOC_i386_MAX; index++)
585     hash_add(&const_hash,
586 	     (char *) elfsh_rel_type_i386[index].name,
587 	     elfsh_rel_type_i386 + index);
588      for (index = 0; index < ELFSH_RELOC_SPARC_MAX; index++)
589     hash_add(&const_hash,
590 	     (char *) elfsh_rel_type_sparc[index].name,
591 	     elfsh_rel_type_sparc + index);
592 
593   for (index = 0; index < ELFSH_DYNAMIC_MAX; index++)
594     hash_add(&const_hash,
595 	     (char *) elfsh_dynentry_type[index].name,
596 	     elfsh_dynentry_type + index);
597   for (index = 0; index < ELFSH_EXTDYN_MAX; index++)
598     hash_add(&const_hash,
599 	     (char *) elfsh_extdyn_type[index].name,
600 	     elfsh_extdyn_type + index);
601   for (index = 0; index < ELFSH_FEATURE_MAX; index++)
602     hash_add(&const_hash,
603 	     (char *) elfsh_feature1[index].name,
604 	     elfsh_feature1 + index);
605   for (index = 0; index < ELFSH_POSFLAG_MAX; index++)
606     hash_add(&const_hash,
607 	     (char *) elfsh_posflag1[index].name,
608 	     elfsh_posflag1 + index);
609   for (index = 0; index < ELFSH_FLAGS_MAX; index++)
610     hash_add(&const_hash,
611 	     (char *) elfsh_flags[index].name,
612 	     elfsh_flags + index);
613   for (index = 0; index < ELFSH_FLAGS1_MAX; index++)
614     hash_add(&const_hash,
615 	     (char *) elfsh_flags1[index].name,
616 	     elfsh_flags1 + index);
617 }
618 
619 
620 /* Setup all hash tables */
vm_setup_hashtables()621 void		vm_setup_hashtables()
622 {
623   hash_init(&file_hash, 251);
624   hash_init(&mod_hash, 251);
625   setup_cmdhash();
626   setup_consthash();
627   setup_L1hash();
628   setup_L2hash();
629 }
630