btfixupprep.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. Simple utility to prepare vmlinux image for sparc.
  3. Resolves all BTFIXUP uses and settings and creates
  4. a special .s object to link to the image.
  5. Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <malloc.h>
  24. #define MAXSYMS 1024
  25. static char *symtab = "SYMBOL TABLE:";
  26. static char *relrec = "RELOCATION RECORDS FOR [";
  27. static int rellen;
  28. static int symlen;
  29. int mode;
  30. struct _btfixup;
  31. typedef struct _btfixuprel {
  32. char *sect;
  33. unsigned long offset;
  34. struct _btfixup *f;
  35. int frel;
  36. struct _btfixuprel *next;
  37. } btfixuprel;
  38. typedef struct _btfixup {
  39. int type;
  40. int setinitval;
  41. unsigned int initval;
  42. char *initvalstr;
  43. char *name;
  44. btfixuprel *rel;
  45. } btfixup;
  46. btfixup array[MAXSYMS];
  47. int last = 0;
  48. char buffer[1024];
  49. unsigned long lastfoffset = -1;
  50. unsigned long lastfrelno;
  51. btfixup *lastf;
  52. static void fatal(void) __attribute__((noreturn));
  53. static void fatal(void)
  54. {
  55. fprintf(stderr, "Malformed output from objdump\n%s\n", buffer);
  56. exit(1);
  57. }
  58. static btfixup *find(int type, char *name)
  59. {
  60. int i;
  61. for (i = 0; i < last; i++) {
  62. if (array[i].type == type && !strcmp(array[i].name, name))
  63. return array + i;
  64. }
  65. array[last].type = type;
  66. array[last].name = strdup(name);
  67. array[last].setinitval = 0;
  68. if (!array[last].name) fatal();
  69. array[last].rel = NULL;
  70. last++;
  71. if (last >= MAXSYMS) {
  72. fprintf(stderr, "Ugh. Something strange. More than %d different BTFIXUP symbols\n", MAXSYMS);
  73. exit(1);
  74. }
  75. return array + last - 1;
  76. }
  77. static void set_mode (char *buffer)
  78. {
  79. for (mode = 0;; mode++)
  80. if (buffer[mode] < '0' || buffer[mode] > '9')
  81. break;
  82. if (mode != 8 && mode != 16)
  83. fatal();
  84. }
  85. int main(int argc,char **argv)
  86. {
  87. char *p, *q;
  88. char *sect;
  89. int i, j, k;
  90. unsigned int initval;
  91. int shift;
  92. btfixup *f;
  93. btfixuprel *r, **rr;
  94. unsigned long offset;
  95. char *initvalstr;
  96. symlen = strlen(symtab);
  97. while (fgets (buffer, 1024, stdin) != NULL)
  98. if (!strncmp (buffer, symtab, symlen))
  99. goto main0;
  100. fatal();
  101. main0:
  102. rellen = strlen(relrec);
  103. while (fgets (buffer, 1024, stdin) != NULL)
  104. if (!strncmp (buffer, relrec, rellen))
  105. goto main1;
  106. fatal();
  107. main1:
  108. sect = malloc(strlen (buffer + rellen) + 1);
  109. if (!sect) fatal();
  110. strcpy (sect, buffer + rellen);
  111. p = strchr (sect, ']');
  112. if (!p) fatal();
  113. *p = 0;
  114. if (fgets (buffer, 1024, stdin) == NULL)
  115. fatal();
  116. while (fgets (buffer, 1024, stdin) != NULL) {
  117. int nbase;
  118. if (!strncmp (buffer, relrec, rellen))
  119. goto main1;
  120. if (mode == 0)
  121. set_mode (buffer);
  122. p = strchr (buffer, '\n');
  123. if (p) *p = 0;
  124. if (strlen (buffer) < 22+mode)
  125. continue;
  126. if (strncmp (buffer + mode, " R_SPARC_", 9))
  127. continue;
  128. nbase = 27 - 8 + mode;
  129. if (buffer[nbase] != '_' || buffer[nbase+1] != '_' || buffer[nbase+2] != '_')
  130. continue;
  131. switch (buffer[nbase+3]) {
  132. case 'f': /* CALL */
  133. case 'b': /* BLACKBOX */
  134. case 's': /* SIMM13 */
  135. case 'a': /* HALF */
  136. case 'h': /* SETHI */
  137. case 'i': /* INT */
  138. break;
  139. default:
  140. continue;
  141. }
  142. p = strchr (buffer + nbase+5, '+');
  143. if (p) *p = 0;
  144. shift = nbase + 5;
  145. if (buffer[nbase+4] == 's' && buffer[nbase+5] == '_') {
  146. shift = nbase + 6;
  147. if (strcmp (sect, ".init.text")) {
  148. fprintf(stderr,
  149. "Wrong use of '%s' BTFIXUPSET in '%s' section.\n"
  150. "BTFIXUPSET_CALL can be used only in"
  151. " __init sections\n",
  152. buffer + shift, sect);
  153. exit(1);
  154. }
  155. } else if (buffer[nbase+4] != '_')
  156. continue;
  157. if (!strcmp (sect, ".text.exit"))
  158. continue;
  159. if (strcmp (sect, ".text") &&
  160. strcmp (sect, ".init.text") &&
  161. strcmp (sect, ".fixup") &&
  162. (strcmp (sect, "__ksymtab") || buffer[nbase+3] != 'f')) {
  163. if (buffer[nbase+3] == 'f')
  164. fprintf(stderr,
  165. "Wrong use of '%s' in '%s' section.\n"
  166. " It can be used only in .text, .init.text,"
  167. " .fixup and __ksymtab\n",
  168. buffer + shift, sect);
  169. else
  170. fprintf(stderr,
  171. "Wrong use of '%s' in '%s' section.\n"
  172. " It can be only used in .text, .init.text,"
  173. " and .fixup\n", buffer + shift, sect);
  174. exit(1);
  175. }
  176. p = strstr (buffer + shift, "__btset_");
  177. if (p && buffer[nbase+4] == 's') {
  178. fprintf(stderr, "__btset_ in BTFIXUP name can only be used when defining the variable, not for setting\n%s\n", buffer);
  179. exit(1);
  180. }
  181. initval = 0;
  182. initvalstr = NULL;
  183. if (p) {
  184. if (p[8] != '0' || p[9] != 'x') {
  185. fprintf(stderr, "Pre-initialized values can be only initialized with hexadecimal constants starting 0x\n%s\n", buffer);
  186. exit(1);
  187. }
  188. initval = strtoul(p + 10, &q, 16);
  189. if (*q || !initval) {
  190. fprintf(stderr, "Pre-initialized values can be only in the form name__btset_0xXXXXXXXX where X are hex digits.\nThey cannot be name__btset_0x00000000 though. Use BTFIXUPDEF_XX instead of BTFIXUPDEF_XX_INIT then.\n%s\n", buffer);
  191. exit(1);
  192. }
  193. initvalstr = p + 10;
  194. *p = 0;
  195. }
  196. f = find(buffer[nbase+3], buffer + shift);
  197. if (buffer[nbase+4] == 's')
  198. continue;
  199. switch (buffer[nbase+3]) {
  200. case 'f':
  201. if (initval) {
  202. fprintf(stderr, "Cannot use pre-initialized fixups for calls\n%s\n", buffer);
  203. exit(1);
  204. }
  205. if (!strcmp (sect, "__ksymtab")) {
  206. if (strncmp (buffer + mode+9, "32 ", 10)) {
  207. fprintf(stderr, "BTFIXUP_CALL in EXPORT_SYMBOL results in relocation other than R_SPARC_32\n\%s\n", buffer);
  208. exit(1);
  209. }
  210. } else if (strncmp (buffer + mode+9, "WDISP30 ", 10) &&
  211. strncmp (buffer + mode+9, "HI22 ", 10) &&
  212. strncmp (buffer + mode+9, "LO10 ", 10)) {
  213. fprintf(stderr, "BTFIXUP_CALL results in relocation other than R_SPARC_WDISP30, R_SPARC_HI22 or R_SPARC_LO10\n%s\n", buffer);
  214. exit(1);
  215. }
  216. break;
  217. case 'b':
  218. if (initval) {
  219. fprintf(stderr, "Cannot use pre-initialized fixups for blackboxes\n%s\n", buffer);
  220. exit(1);
  221. }
  222. if (strncmp (buffer + mode+9, "HI22 ", 10)) {
  223. fprintf(stderr, "BTFIXUP_BLACKBOX results in relocation other than R_SPARC_HI22\n%s\n", buffer);
  224. exit(1);
  225. }
  226. break;
  227. case 's':
  228. if (initval + 0x1000 >= 0x2000) {
  229. fprintf(stderr, "Wrong initializer for SIMM13. Has to be from $fffff000 to $00000fff\n%s\n", buffer);
  230. exit(1);
  231. }
  232. if (strncmp (buffer + mode+9, "13 ", 10)) {
  233. fprintf(stderr, "BTFIXUP_SIMM13 results in relocation other than R_SPARC_13\n%s\n", buffer);
  234. exit(1);
  235. }
  236. break;
  237. case 'a':
  238. if (initval + 0x1000 >= 0x2000 && (initval & 0x3ff)) {
  239. fprintf(stderr, "Wrong initializer for HALF.\n%s\n", buffer);
  240. exit(1);
  241. }
  242. if (strncmp (buffer + mode+9, "13 ", 10)) {
  243. fprintf(stderr, "BTFIXUP_HALF results in relocation other than R_SPARC_13\n%s\n", buffer);
  244. exit(1);
  245. }
  246. break;
  247. case 'h':
  248. if (initval & 0x3ff) {
  249. fprintf(stderr, "Wrong initializer for SETHI. Cannot have set low 10 bits\n%s\n", buffer);
  250. exit(1);
  251. }
  252. if (strncmp (buffer + mode+9, "HI22 ", 10)) {
  253. fprintf(stderr, "BTFIXUP_SETHI results in relocation other than R_SPARC_HI22\n%s\n", buffer);
  254. exit(1);
  255. }
  256. break;
  257. case 'i':
  258. if (initval) {
  259. fprintf(stderr, "Cannot use pre-initialized fixups for INT\n%s\n", buffer);
  260. exit(1);
  261. }
  262. if (strncmp (buffer + mode+9, "HI22 ", 10) && strncmp (buffer + mode+9, "LO10 ", 10)) {
  263. fprintf(stderr, "BTFIXUP_INT results in relocation other than R_SPARC_HI22 and R_SPARC_LO10\n%s\n", buffer);
  264. exit(1);
  265. }
  266. break;
  267. }
  268. if (!f->setinitval) {
  269. f->initval = initval;
  270. if (initvalstr) {
  271. f->initvalstr = strdup(initvalstr);
  272. if (!f->initvalstr) fatal();
  273. }
  274. f->setinitval = 1;
  275. } else if (f->initval != initval) {
  276. fprintf(stderr, "Btfixup %s previously used with initializer %s which doesn't match with current initializer\n%s\n",
  277. f->name, f->initvalstr ? : "0x00000000", buffer);
  278. exit(1);
  279. } else if (initval && strcmp(f->initvalstr, initvalstr)) {
  280. fprintf(stderr, "Btfixup %s previously used with initializer %s which doesn't match with current initializer.\n"
  281. "Initializers have to match literally as well.\n%s\n",
  282. f->name, f->initvalstr, buffer);
  283. exit(1);
  284. }
  285. offset = strtoul(buffer, &q, 16);
  286. if (q != buffer + mode || (!offset && (mode == 8 ? strncmp (buffer, "00000000 ", 9) : strncmp (buffer, "0000000000000000 ", 17)))) {
  287. fprintf(stderr, "Malformed relocation address in\n%s\n", buffer);
  288. exit(1);
  289. }
  290. for (k = 0, r = f->rel, rr = &f->rel; r; rr = &r->next, r = r->next, k++)
  291. if (r->offset == offset && !strcmp(r->sect, sect)) {
  292. fprintf(stderr, "Ugh. One address has two relocation records\n");
  293. exit(1);
  294. }
  295. *rr = malloc(sizeof(btfixuprel));
  296. if (!*rr) fatal();
  297. (*rr)->offset = offset;
  298. (*rr)->f = NULL;
  299. if (buffer[nbase+3] == 'f') {
  300. lastf = f;
  301. lastfoffset = offset;
  302. lastfrelno = k;
  303. } else if (lastfoffset + 4 == offset) {
  304. (*rr)->f = lastf;
  305. (*rr)->frel = lastfrelno;
  306. }
  307. (*rr)->sect = sect;
  308. (*rr)->next = NULL;
  309. }
  310. printf("! Generated by btfixupprep. Do not edit.\n\n");
  311. printf("\t.section\t\".data..init\",#alloc,#write\n\t.align\t4\n\n");
  312. printf("\t.global\t___btfixup_start\n___btfixup_start:\n\n");
  313. for (i = 0; i < last; i++) {
  314. f = array + i;
  315. printf("\t.global\t___%cs_%s\n", f->type, f->name);
  316. if (f->type == 'f')
  317. printf("___%cs_%s:\n\t.word 0x%08x,0,0,", f->type, f->name, f->type << 24);
  318. else
  319. printf("___%cs_%s:\n\t.word 0x%08x,0,", f->type, f->name, f->type << 24);
  320. for (j = 0, r = f->rel; r != NULL; j++, r = r->next);
  321. if (j)
  322. printf("%d\n\t.word\t", j * 2);
  323. else
  324. printf("0\n");
  325. for (r = f->rel, j--; r != NULL; j--, r = r->next) {
  326. if (!strcmp (r->sect, ".text"))
  327. printf ("_stext+0x%08lx", r->offset);
  328. else if (!strcmp (r->sect, ".init.text"))
  329. printf ("__init_begin+0x%08lx", r->offset);
  330. else if (!strcmp (r->sect, "__ksymtab"))
  331. printf ("__start___ksymtab+0x%08lx", r->offset);
  332. else if (!strcmp (r->sect, ".fixup"))
  333. printf ("__start___fixup+0x%08lx", r->offset);
  334. else
  335. fatal();
  336. if (f->type == 'f' || !r->f)
  337. printf (",0");
  338. else
  339. printf (",___fs_%s+0x%08x", r->f->name, (4 + r->frel*2)*4 + 4);
  340. if (j) printf (",");
  341. else printf ("\n");
  342. }
  343. printf("\n");
  344. }
  345. printf("\n\t.global\t___btfixup_end\n___btfixup_end:\n");
  346. printf("\n\n! Define undefined references\n\n");
  347. for (i = 0; i < last; i++) {
  348. f = array + i;
  349. if (f->type == 'f') {
  350. printf("\t.global\t___f_%s\n", f->name);
  351. printf("___f_%s:\n", f->name);
  352. }
  353. }
  354. printf("\tretl\n\t nop\n\n");
  355. for (i = 0; i < last; i++) {
  356. f = array + i;
  357. if (f->type != 'f') {
  358. if (!f->initval) {
  359. printf("\t.global\t___%c_%s\n", f->type, f->name);
  360. printf("___%c_%s = 0\n", f->type, f->name);
  361. } else {
  362. printf("\t.global\t___%c_%s__btset_0x%s\n", f->type, f->name, f->initvalstr);
  363. printf("___%c_%s__btset_0x%s = 0x%08x\n", f->type, f->name, f->initvalstr, f->initval);
  364. }
  365. }
  366. }
  367. printf("\n\n");
  368. exit(0);
  369. }