Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "disasm.h" /* wsprintf() */
  5. #include "drwatson.h"
  6. #define _lread(h, adr, cnt) _lread(h, (LPSTR)(adr), cnt)
  7. /* Last entry in .SYM file */
  8. typedef struct tagMAPEND {
  9. unsigned chnend; /* end of map chain (0) */
  10. char rel; /* release */
  11. char ver; /* version */
  12. } MAPEND;
  13. /* Structure of .SYM file symbol entry */
  14. typedef struct tagSYMDEF {
  15. unsigned sym_val; /* 16 bit symbol addr or const */
  16. char nam_len; /* 8 bit symbol name length */
  17. } SYMDEF;
  18. /* Structure of a .SYM file segment entry */
  19. typedef struct tagSEGDEF {
  20. unsigned nxt_seg; /* 16 bit ptr to next segment(0 if end) */
  21. int sym_cnt; /* 16 bit count of symbols in sym list */
  22. unsigned sym_ptr; /* 16 bit ptr to symbol list */
  23. unsigned seg_lsa; /* 16 bit Load Segment address */
  24. unsigned seg_in0; /* 16 bit instance 0 physical address */
  25. unsigned seg_in1; /* 16 bit instance 1 physical address */
  26. unsigned seg_in2; /* 16 bit instance 2 physical address */
  27. unsigned seg_in3; /* 16 bit instance 3 physical address */
  28. unsigned seg_lin; /* 16 bit ptr to line number record */
  29. char seg_ldd; /* 8 bit boolean 0 if seg not loaded */
  30. char seg_cin; /* 8 bit current instance */
  31. char nam_len; /* 8 bit Segment name length */
  32. } SEGDEF;
  33. /* Structure of a .SYM file MAP entry */
  34. typedef struct tagMAPDEF {
  35. unsigned map_ptr; /* 16 bit ptr to next map (0 if end) */
  36. unsigned lsa ; /* 16 bit Load Segment address */
  37. unsigned pgm_ent; /* 16 bit entry point segment value */
  38. int abs_cnt; /* 16 bit count of constants in map */
  39. unsigned abs_ptr; /* 16 bit ptr to constant chain */
  40. int seg_cnt; /* 16 bit count of segments in map */
  41. unsigned seg_ptr; /* 16 bit ptr to segment chain */
  42. char nam_max; /* 8 bit Maximum Symbol name length */
  43. char nam_len; /* 8 bit Symbol table name length */
  44. } MAPDEF;
  45. /* should cache last 4 files, last 4 segments, last 4 symbol blocks */
  46. void cdecl Show(char *foo, ...);
  47. #define MAXSYM 64
  48. char *FindSym(unsigned segIndex, unsigned offset, int h) {
  49. static char sym_name[MAXSYM+5];
  50. char name[MAXSYM+3];
  51. int i;
  52. MAPDEF mod;
  53. SEGDEF seg;
  54. SYMDEF sym, *sp;
  55. if (sizeof(mod) != _lread(h, &mod, sizeof(mod))) return 0;
  56. if (segIndex > (unsigned)mod.seg_cnt) return 0;
  57. seg.nxt_seg = mod.seg_ptr;
  58. for (i=0; i<mod.seg_cnt; i++) {
  59. _llseek(h, (long)seg.nxt_seg << 4, SEEK_SET);
  60. _lread(h, &seg, sizeof(seg));
  61. if (seg.seg_lsa == segIndex) break;
  62. }
  63. if (seg.seg_lsa != segIndex) return 0;
  64. _llseek(h, seg.nam_len, SEEK_CUR);
  65. sym_name[0] = 0;
  66. sym.sym_val = 0xffff;
  67. sym.nam_len = 0;
  68. for (i=0; i<seg.sym_cnt; i++) {
  69. unsigned len = sizeof(sym) + sym.nam_len;
  70. if (len >= sizeof(name)) return 0;
  71. if (len != _lread(h, name, len)) return 0;
  72. sp = (SYMDEF *)(name + sym.nam_len);
  73. if (sp->sym_val > offset)
  74. break;
  75. sym = *sp;
  76. }
  77. name[sym.nam_len] = 0;
  78. if (name[0] == 0) return 0;
  79. if (sym.sym_val == offset) strcpy(sym_name, name);
  80. else sprintf(sym_name, "%s+%04x", (FP)name, offset-sym.sym_val);
  81. return sym_name;
  82. } /* FindSym */
  83. char *NearestSym(int segIndex, unsigned offset, char *exeName) {
  84. char fName[80];
  85. /* OFSTRUCT reOpen; */
  86. char *s;
  87. int h;
  88. strcpy(fName, exeName);
  89. strcpy(fName+strlen(fName)-4, ".sym");
  90. h = _lopen(fName, OF_READ | OF_SHARE_DENY_WRITE);
  91. if (h == -1) return 0;
  92. s = FindSym(segIndex, offset, h);
  93. _lclose(h);
  94. return s;
  95. } /* NearestSym */