LIB_PatternCollector.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "LIB_PatternCollector.h"
  2. #include "msvc_fixes.h"
  3. #include <cstring>
  4. #include <cstring>
  5. /** \note there is an untested assumption that the *first* segment definition
  6. with class CODE will be the one containing all useful functions in the
  7. LEDATA records. Functions such as _exit() have more than one segment
  8. declared with class CODE (MSC8 libraries) */
  9. extern void fixWildCards(uint8_t pat[]);
  10. void readNN(int n, FILE *fl)
  11. {
  12. if (fseek(fl, (long)n, SEEK_CUR) != 0)
  13. {
  14. printf("Could not seek file\n");
  15. exit(2);
  16. }
  17. }
  18. void LIB_PatternCollector::readString(FILE *fl)
  19. {
  20. uint8_t len;
  21. len = readByte(fl);
  22. if (fread(buf, 1, len, fl) != len)
  23. {
  24. printf("Could not read string len %d\n", len);
  25. exit(2);
  26. }
  27. buf[len] = '\0';
  28. offset += len;
  29. }
  30. int LIB_PatternCollector::readSyms(FILE *fl)
  31. {
  32. int i;
  33. int count = 0;
  34. int firstSym = 0; /* First symbol this module */
  35. uint8_t b, c, type;
  36. uint16_t w, len;
  37. codeLNAMES = NONE; /* Invalidate indexes for code segment */
  38. codeSEGDEF = NONE; /* Else won't be assigned */
  39. offset = 0; /* For diagnostics, really */
  40. if ((leData = (uint8_t *)malloc(0xFF80)) == 0)
  41. {
  42. printf("Could not malloc 64k bytes for LEDATA\n");
  43. exit(10);
  44. }
  45. while (!feof(fl))
  46. {
  47. type = readByte(fl);
  48. len = readWord(fl);
  49. /* Note: uncommenting the following generates a *lot* of output */
  50. /*printf("Offset %05lX: type %02X len %d\n", offset-3, type, len);//*/
  51. switch (type)
  52. {
  53. case 0x96: /* LNAMES */
  54. while (len > 1)
  55. {
  56. readString(fl);
  57. ++lnum;
  58. if (strcmp((char *)buf, "CODE") == 0)
  59. {
  60. /* This is the class name we're looking for */
  61. codeLNAMES= lnum;
  62. }
  63. len -= strlen((char *)buf)+1;
  64. }
  65. b = readByte(fl); /* Checksum */
  66. break;
  67. case 0x98: /* Segment definition */
  68. b = readByte(fl); /* Segment attributes */
  69. if ((b & 0xE0) == 0)
  70. {
  71. /* Alignment field is zero. Frame and offset follow */
  72. readWord(fl);
  73. readByte(fl);
  74. }
  75. w = readWord(fl); /* Segment length */
  76. b = readByte(fl); /* Segment name index */
  77. ++segnum;
  78. b = readByte(fl); /* Class name index */
  79. if ((b == codeLNAMES) and (codeSEGDEF == NONE))
  80. {
  81. /* This is the segment defining the code class */
  82. codeSEGDEF = segnum;
  83. }
  84. b = readByte(fl); /* Overlay index */
  85. b = readByte(fl); /* Checksum */
  86. break;
  87. case 0x90: /* PUBDEF: public symbols */
  88. b = readByte(fl); /* Base group */
  89. c = readByte(fl); /* Base segment */
  90. len -= 2;
  91. if (c == 0)
  92. {
  93. w = readWord(fl);
  94. len -= 2;
  95. }
  96. while (len > 1)
  97. {
  98. readString(fl);
  99. w = readWord(fl); /* Offset */
  100. b = readByte(fl); /* Type index */
  101. if (c == codeSEGDEF)
  102. {
  103. char *p;
  104. HASHENTRY entry;
  105. p = (char *)buf;
  106. if (buf[0] == '_') /* Leading underscore? */
  107. {
  108. p++; /* Yes, remove it*/
  109. }
  110. i = std::min(size_t(SYMLEN-1), strlen(p));
  111. memcpy(entry.name, p, i);
  112. entry.name[i] = '\0';
  113. entry.offset = w;
  114. /*printf("%04X: %s is sym #%d\n", w, keys[count].name, count);//*/
  115. keys.push_back(entry);
  116. count++;
  117. }
  118. len -= strlen((char *)buf) + 1 + 2 + 1;
  119. }
  120. b = readByte(fl); /* Checksum */
  121. break;
  122. case 0xA0: /* LEDATA */
  123. {
  124. b = readByte(fl); /* Segment index */
  125. w = readWord(fl); /* Offset */
  126. len -= 3;
  127. /*printf("LEDATA seg %d off %02X len %Xh, looking for %d\n", b, w, len-1, codeSEGDEF);//*/
  128. if (b != codeSEGDEF)
  129. {
  130. readNN(len,fl); /* Skip the data */
  131. break; /* Next record */
  132. }
  133. if (fread(&leData[w], 1, len-1, fl) != len-1)
  134. {
  135. printf("Could not read LEDATA length %d\n", len-1);
  136. exit(2);
  137. }
  138. offset += len-1;
  139. maxLeData = std::max<uint16_t>(maxLeData, w+len-1);
  140. readByte(fl); /* Checksum */
  141. break;
  142. }
  143. default:
  144. readNN(len,fl); /* Just skip the lot */
  145. if (type == 0x8A) /* Mod end */
  146. {
  147. /* Now find all the patterns for public code symbols that
  148. we have found */
  149. for (i=firstSym; i < count; i++)
  150. {
  151. uint16_t off = keys[i].offset;
  152. if (off == (uint16_t)-1)
  153. {
  154. continue; /* Ignore if already done */
  155. }
  156. if (keys[i].offset > maxLeData)
  157. {
  158. printf(
  159. "Warning: no LEDATA for symbol #%d %s "
  160. "(offset %04X, max %04X)\n",
  161. i, keys[i].name, off, maxLeData);
  162. /* To make things consistant, we set the pattern for
  163. this symbol to nulls */
  164. memset(&keys[i].pat, 0, PATLEN);
  165. continue;
  166. }
  167. /* Copy to temp buffer so don't overrun later patterns.
  168. (e.g. when chopping a short pattern).
  169. Beware of short patterns! */
  170. if (off+PATLEN <= maxLeData)
  171. {
  172. /* Available pattern is >= PATLEN */
  173. memcpy(buf, &leData[off], PATLEN);
  174. }
  175. else
  176. {
  177. /* Short! Only copy what is available (and malloced!) */
  178. memcpy(buf, &leData[off], maxLeData-off);
  179. /* Set rest to zeroes */
  180. memset(&buf[maxLeData-off], 0, PATLEN-(maxLeData-off));
  181. }
  182. fixWildCards((uint8_t *)buf);
  183. /* Save into the hash entry. */
  184. memcpy(keys[i].pat, buf, PATLEN);
  185. keys[i].offset = (uint16_t)-1; // Flag it as done
  186. //printf("Saved pattern for %s\n", keys[i].name);
  187. }
  188. while (readByte(fl) == 0);
  189. readNN(-1,fl); /* Unget the last byte (= type) */
  190. lnum = 0; /* Reset index into lnames */
  191. segnum = 0; /* Reset index into snames */
  192. firstSym = count; /* Remember index of first sym this mod */
  193. codeLNAMES = NONE; /* Invalidate indexes for code segment */
  194. codeSEGDEF = NONE;
  195. memset(leData, 0, maxLeData); /* Clear out old junk */
  196. maxLeData = 0; /* No data read this module */
  197. }
  198. else if (type == 0xF1)
  199. {
  200. /* Library end record */
  201. return count;
  202. }
  203. }
  204. }
  205. free(leData);
  206. keys.clear();
  207. return count;
  208. }