shinobininja

semtex 2.6.37-3.x.x x86_64

Feb 2nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. #define KSYM_NAME_LEN 127
  9.  
  10.  
  11. struct sym_entry {
  12. unsigned long long addr;
  13. unsigned int len;
  14. unsigned char *sym;
  15. };
  16.  
  17.  
  18. static struct sym_entry *table;
  19. static unsigned int table_size, table_cnt;
  20. static unsigned long long _text, _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;
  21. static int all_symbols = 0;
  22. static char symbol_prefix_char = '\0';
  23.  
  24. int token_profit[0x10000];
  25.  
  26. /* the table that holds the result of the compression */
  27. unsigned char best_table[256][2];
  28. unsigned char best_table_len[256];
  29.  
  30.  
  31. static void usage(void)
  32. {
  33. fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
  34. exit(1);
  35. }
  36.  
  37. /*
  38. * This ignores the intensely annoying "mapping symbols" found
  39. * in ARM ELF files: $a, $t and $d.
  40. */
  41. static inline int is_arm_mapping_symbol(const char *str)
  42. {
  43. return str[0] == '$' && strchr("atd", str[1])
  44. && (str[2] == '\0' || str[2] == '.');
  45. }
  46.  
  47. static int read_symbol(FILE *in, struct sym_entry *s)
  48. {
  49. char str[500];
  50. char *sym, stype;
  51. int rc;
  52.  
  53. rc = fscanf(in, "%llx %c I9s\n", &s->addr, &stype, str);
  54. if (rc != 3) {
  55. if (rc != EOF) {
  56. /* skip line */
  57. fgets(str, 500, in);
  58. }
  59. return -1;
  60. }
  61.  
  62. sym = str;
  63. /* skip prefix char */
  64. if (symbol_prefix_char && str[0] == symbol_prefix_char)
  65. sym++;
  66.  
  67. /* Ignore most absolute/undefined (?) symbols. */
  68. if (strcmp(sym, "_text") == 0)
  69. _text = s->addr;
  70. else if (strcmp(sym, "_stext") == 0)
  71. _stext = s->addr;
  72. else if (strcmp(sym, "_etext") == 0)
  73. _etext = s->addr;
  74. else if (strcmp(sym, "_sinittext") == 0)
  75. _sinittext = s->addr;
  76. else if (strcmp(sym, "_einittext") == 0)
  77. _einittext = s->addr;
  78. else if (strcmp(sym, "_sextratext") == 0)
  79. _sextratext = s->addr;
  80. else if (strcmp(sym, "_eextratext") == 0)
  81. _eextratext = s->addr;
  82. else if (toupper(stype) == 'A')
  83. {
  84. /* Keep these useful absolute symbols */
  85. if (strcmp(sym, "__kernel_syscall_via_break") &&
  86. strcmp(sym, "__kernel_syscall_via_epc") &&
  87. strcmp(sym, "__kernel_sigtramp") &&
  88. strcmp(sym, "__gp"))
  89. return -1;
  90.  
  91. }
  92. else if (toupper(stype) == 'U' ||
  93. is_arm_mapping_symbol(sym))
  94. return -1;
  95. /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
  96. else if (str[0] == '$')
  97. return -1;
  98.  
  99. /* include the type field in the symbol name, so that it gets
  100. * compressed together */
  101. s->len = strlen(str) + 1;
  102. s->sym = malloc(s->len + 1);
  103. if (!s->sym) {
  104. fprintf(stderr, "kallsyms failure: "
  105. "unable to allocate required amount of memory\n");
  106. exit(EXIT_FAILURE);
  107. }
  108. strcpy((char *)s->sym + 1, str);
  109. s->sym[0] = stype;
  110.  
  111. return 0;
  112. }
  113.  
  114. static int symbol_valid(struct sym_entry *s)
  115. {
  116. /* Symbols which vary between passes. Passes 1 and 2 must have
  117. * identical symbol lists. The kallsyms_* symbols below are only added
  118. * after pass 1, they would be included in pass 2 when --all-symbols is
  119. * specified so exclude them to get a stable symbol list.
  120. */
  121. static char *special_symbols[] = {
  122. "kallsyms_addresses",
  123. "kallsyms_num_syms",
  124. "kallsyms_names",
  125. "kallsyms_markers",
  126. "kallsyms_token_table",
  127. "kallsyms_token_index",
  128.  
  129. /* Exclude linker generated symbols which vary between passes */
  130. "_SDA_BASE_", /* ppc */
  131. "_SDA2_BASE_", /* ppc */
  132. NULL };
  133. int i;
  134. int offset = 1;
  135.  
  136. /* skip prefix char */
  137. if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
  138. offset++;
  139.  
  140. /* if --all-symbols is not specified, then symbols outside the text
  141. * and inittext sections are discarded */
  142. if (!all_symbols) {
  143. if ((s->addr < _stext || s->addr > _etext)
  144. && (s->addr < _sinittext || s->addr > _einittext)
  145. && (s->addr < _sextratext || s->addr > _eextratext))
  146. return 0;
  147. /* Corner case. Discard any symbols with the same value as
  148. * _etext _einittext or _eextratext; they can move between pass
  149. * 1 and 2 when the kallsyms data are added. If these symbols
  150. * move then they may get dropped in pass 2, which breaks the
  151. * kallsyms rules.
  152. */
  153. if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
  154. (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
  155. (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
  156. return 0;
  157. }
  158.  
  159. /* Exclude symbols which vary between passes. */
  160. if (strstr((char *)s->sym + offset, "_compiled."))
  161. return 0;
  162.  
  163. for (i = 0; special_symbols[i]; i++)
  164. if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
  165. return 0;
  166.  
  167. return 1;
  168. }
  169.  
  170. static void read_map(FILE *in)
  171. {
  172. while (!feof(in)) {
  173. if (table_cnt >= table_size) {
  174. table_size += 10000;
  175. table = realloc(table, sizeof(*table) * table_size);
  176. if (!table) {
  177. fprintf(stderr, "out of memory\n");
  178. exit (1);
  179. }
  180. }
  181. if (read_symbol(in, &table[table_cnt]) == 0)
  182. table_cnt++;
  183. }
  184. }
  185.  
  186. static void output_label(char *label)
  187. {
  188. if (symbol_prefix_char)
  189. printf(".globl %c%s\n", symbol_prefix_char, label);
  190. else
  191. printf(".globl %s\n", label);
  192. printf("\tALGN\n");
  193. if (symbol_prefix_char)
  194. printf("%c%s:\n", symbol_prefix_char, label);
  195. else
  196. printf("%s:\n", label);
  197. }
  198.  
  199. /* uncompress a compressed symbol. When this function is called, the best table
  200. * might still be compressed itself, so the function needs to be recursive */
  201. static int expand_symbol(unsigned char *data, int len, char *result)
  202. {
  203. int c, rlen, total=0;
  204.  
  205. while (len) {
  206. c = *data;
  207. /* if the table holds a single char that is the same as the one
  208. * we are looking for, then end the search */
  209. if (best_table[c][0]==c && best_table_len[c]==1) {
  210. *result++ = c;
  211. total++;
  212. } else {
  213. /* if not, recurse and expand */
  214. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  215. total += rlen;
  216. result += rlen;
  217. }
  218. data++;
  219. len--;
  220. }
  221. *result=0;
  222.  
  223. return total;
  224. }
  225.  
  226. static void write_src(void)
  227. {
  228. unsigned int i, k, off;
  229. unsigned int best_idx[256];
  230. unsigned int *markers;
  231. char buf[KSYM_NAME_LEN+1];
  232.  
  233. printf("#include <asm/types.h>\n");
  234. printf("#if BITS_PER_LONG == 64\n");
  235. printf("#define PTR .quad\n");
  236. printf("#define ALGN .align 8\n");
  237. printf("#else\n");
  238. printf("#define PTR .long\n");
  239. printf("#define ALGN .align 4\n");
  240. printf("#endif\n");
  241.  
  242. printf(".data\n");
  243.  
  244. /* Provide proper symbols relocatability by their '_text'
  245. * relativeness. The symbol names cannot be used to construct
  246. * normal symbol references as the list of symbols contains
  247. * symbols that are declared static and are private to their
  248. * .o files. This prevents .tmp_kallsyms.o or any other
  249. * object from referencing them.
  250. */
  251. output_label("kallsyms_addresses");
  252. for (i = 0; i < table_cnt; i++) {
  253. if (toupper(table[i].sym[0]) != 'A') {
  254. printf("\tPTR\t_text + %#llx\n",
  255. table[i].addr - _text);
  256. } else {
  257. printf("\tPTR\t%#llx\n", table[i].addr);
  258. }
  259. }
  260. printf("\n");
  261.  
  262. output_label("kallsyms_num_syms");
  263. printf("\tPTR\t%d\n", table_cnt);
  264. printf("\n");
  265.  
  266. /* table of offset markers, that give the offset in the compressed stream
  267. * every 256 symbols */
  268. markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  269. if (!markers) {
  270. fprintf(stderr, "kallsyms failure: "
  271. "unable to allocate required memory\n");
  272. exit(EXIT_FAILURE);
  273. }
  274.  
  275. output_label("kallsyms_names");
  276. off = 0;
  277. for (i = 0; i < table_cnt; i++) {
  278. if ((i & 0xFF) == 0)
  279. markers[i >> 8] = off;
  280.  
  281. printf("\t.byte 0xx", table[i].len);
  282. for (k = 0; k < table[i].len; k++)
  283. printf(", 0xx", table[i].sym[k]);
  284. printf("\n");
  285.  
  286. off += table[i].len + 1;
  287. }
  288. printf("\n");
  289.  
  290. output_label("kallsyms_markers");
  291. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  292. printf("\tPTR\t%d\n", markers[i]);
  293. printf("\n");
  294.  
  295. free(markers);
  296.  
  297. output_label("kallsyms_token_table");
  298. off = 0;
  299. for (i = 0; i < 256; i++) {
  300. best_idx[i] = off;
  301. expand_symbol(best_table[i], best_table_len[i], buf);
  302. printf("\t.asciz\t\"%s\"\n", buf);
  303. off += strlen(buf) + 1;
  304. }
  305. printf("\n");
  306.  
  307. output_label("kallsyms_token_index");
  308. for (i = 0; i < 256; i++)
  309. printf("\t.short\t%d\n", best_idx[i]);
  310. printf("\n");
  311. }
  312.  
  313.  
  314. /* table lookup compression functions */
  315.  
  316. /* count all the possible tokens in a symbol */
  317. static void learn_symbol(unsigned char *symbol, int len)
  318. {
  319. int i;
  320.  
  321. for (i = 0; i < len - 1; i++)
  322. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  323. }
  324.  
  325. /* decrease the count for all the possible tokens in a symbol */
  326. static void forget_symbol(unsigned char *symbol, int len)
  327. {
  328. int i;
  329.  
  330. for (i = 0; i < len - 1; i++)
  331. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
  332. }
  333.  
  334. /* remove all the invalid symbols from the table and do the initial token count */
  335. static void build_initial_tok_table(void)
  336. {
  337. unsigned int i, pos;
  338.  
  339. pos = 0;
  340. for (i = 0; i < table_cnt; i++) {
  341. if ( symbol_valid(&table[i]) ) {
  342. if (pos != i)
  343. table[pos] = table[i];
  344. learn_symbol(table[pos].sym, table[pos].len);
  345. pos++;
  346. }
  347. }
  348. table_cnt = pos;
  349. }
  350.  
  351. /* replace a given token in all the valid symbols. Use the sampled symbols
  352. * to update the counts */
  353. static void compress_symbols(unsigned char *str, int idx)
  354. {
  355. unsigned int i, len, size;
  356. unsigned char *p1, *p2;
  357.  
  358. for (i = 0; i < table_cnt; i++) {
  359.  
  360. len = table[i].len;
  361. p1 = table[i].sym;
  362.  
  363. /* find the token on the symbol */
  364. p2 = memmem(p1, len, str, 2);
  365. if (!p2) continue;
  366.  
  367. /* decrease the counts for this symbol's tokens */
  368. forget_symbol(table[i].sym, len);
  369.  
  370. size = len;
  371.  
  372. do {
  373. *p2 = idx;
  374. p2++;
  375. size -= (p2 - p1);
  376. memmove(p2, p2 + 1, size);
  377. p1 = p2;
  378. len--;
  379.  
  380. if (size < 2) break;
  381.  
  382. /* find the token on the symbol */
  383. p2 = memmem(p1, size, str, 2);
  384.  
  385. } while (p2);
  386.  
  387. table[i].len = len;
  388.  
  389. /* increase the counts for this symbol's new tokens */
  390. learn_symbol(table[i].sym, len);
  391. }
  392. }
  393.  
  394. /* search the token with the maximum profit */
  395. static int find_best_token(void)
  396. {
  397. int i, best, bestprofit;
  398.  
  399. bestprofit=-10000;
  400. best = 0;
  401.  
  402. for (i = 0; i < 0x10000; i++) {
  403. if (token_profit[i] > bestprofit) {
  404. best = i;
  405. bestprofit = token_profit[i];
  406. }
  407. }
  408. return best;
  409. }
  410.  
  411. /* this is the core of the algorithm: calculate the "best" table */
  412. static void optimize_result(void)
  413. {
  414. int i, best;
  415.  
  416. /* using the '\0' symbol last allows compress_symbols to use standard
  417. * fast string functions */
  418. for (i = 255; i >= 0; i--) {
  419.  
  420. /* if this table slot is empty (it is not used by an actual
  421. * original char code */
  422. if (!best_table_len[i]) {
  423.  
  424. /* find the token with the breates profit value */
  425. best = find_best_token();
  426.  
  427. /* place it in the "best" table */
  428. best_table_len[i] = 2;
  429. best_table[i][0] = best & 0xFF;
  430. best_table[i][1] = (best >> 8) & 0xFF;
  431.  
  432. /* replace this token in all the valid symbols */
  433. compress_symbols(best_table[i], i);
  434. }
  435. }
  436. }
  437.  
  438. /* start by placing the symbols that are actually used on the table */
  439. static void insert_real_symbols_in_table(void)
  440. {
  441. unsigned int i, j, c;
  442.  
  443. memset(best_table, 0, sizeof(best_table));
  444. memset(best_table_len, 0, sizeof(best_table_len));
  445.  
  446. for (i = 0; i < table_cnt; i++) {
  447. for (j = 0; j < table[i].len; j++) {
  448. c = table[i].sym[j];
  449. best_table[c][0]=c;
  450. best_table_len[c]=1;
  451. }
  452. }
  453. }
  454.  
  455. static void optimize_token_table(void)
  456. {
  457. build_initial_tok_table();
  458.  
  459. insert_real_symbols_in_table();
  460.  
  461. /* When valid symbol is not registered, exit to error */
  462. if (!table_cnt) {
  463. fprintf(stderr, "No valid symbol.\n");
  464. exit(1);
  465. }
  466.  
  467. optimize_result();
  468. }
  469.  
  470.  
  471. int main(int argc, char **argv)
  472. {
  473. if (argc >= 2) {
  474. int i;
  475. for (i = 1; i < argc; i++) {
  476. if(strcmp(argv[i], "--all-symbols") == 0)
  477. all_symbols = 1;
  478. else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
  479. char *p = &argv[i][16];
  480. /* skip quote */
  481. if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
  482. p++;
  483. symbol_prefix_char = *p;
  484. } else
  485. usage();
  486. }
  487. } else if (argc != 1)
  488. usage();
  489.  
  490. read_map(stdin);
  491. optimize_token_table();
  492. write_src();
  493.  
  494. return 0;
  495. }
Add Comment
Please, Sign In to add comment