qobj.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * qobj.c - identify and parse a PowerPC object file
  11. * forsyth@terzarima.net
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <mach.h>
  17. #include "qc/q.out.h"
  18. #include "obj.h"
  19. typedef struct Addr Addr;
  20. struct Addr
  21. {
  22. char type;
  23. char sym;
  24. char name;
  25. };
  26. static Addr addr(Biobuf*);
  27. static char type2char(int);
  28. static void skip(Biobuf*, int);
  29. int
  30. _isq(char *s)
  31. {
  32. return (s[0]&0377) == ANAME /* ANAME */
  33. && (s[1]&0377) == ANAME>>8
  34. && s[2] == D_FILE /* type */
  35. && s[3] == 1 /* sym */
  36. && s[4] == '<'; /* name of file */
  37. }
  38. int
  39. _readq(Biobuf *bp, Prog *p)
  40. {
  41. int as, n, c;
  42. Addr a;
  43. as = Bgetc(bp); /* as(low) */
  44. if(as < 0)
  45. return 0;
  46. c = Bgetc(bp); /* as(high) */
  47. if(c < 0)
  48. return 0;
  49. as |= ((c & 0xff) << 8);
  50. p->kind = aNone;
  51. p->sig = 0;
  52. if(as == ANAME || as == ASIGNAME){
  53. if(as == ASIGNAME){
  54. Bread(bp, &p->sig, 4);
  55. p->sig = beswal(p->sig);
  56. }
  57. p->kind = aName;
  58. p->type = type2char(Bgetc(bp)); /* type */
  59. p->sym = Bgetc(bp); /* sym */
  60. n = 0;
  61. for(;;) {
  62. as = Bgetc(bp);
  63. if(as < 0)
  64. return 0;
  65. n++;
  66. if(as == 0)
  67. break;
  68. }
  69. p->id = malloc(n);
  70. if(p->id == 0)
  71. return 0;
  72. Bseek(bp, -n, 1);
  73. if(Bread(bp, p->id, n) != n)
  74. return 0;
  75. return 1;
  76. }
  77. if(as == ATEXT)
  78. p->kind = aText;
  79. else if(as == AGLOBL)
  80. p->kind = aData;
  81. n = Bgetc(bp); /* reg and flag */
  82. skip(bp, 4); /* lineno(4) */
  83. a = addr(bp);
  84. if(n & 0x40)
  85. addr(bp);
  86. addr(bp);
  87. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  88. p->kind = aNone;
  89. p->sym = a.sym;
  90. return 1;
  91. }
  92. static Addr
  93. addr(Biobuf *bp)
  94. {
  95. Addr a;
  96. int32_t off;
  97. a.type = Bgetc(bp); /* a.type */
  98. skip(bp,1); /* reg */
  99. a.sym = Bgetc(bp); /* sym index */
  100. a.name = Bgetc(bp); /* sym type */
  101. switch(a.type){
  102. default:
  103. case D_NONE: case D_REG: case D_FREG: case D_CREG:
  104. case D_FPSCR: case D_MSR: case D_SREG:
  105. break;
  106. case D_SPR:
  107. case D_OREG:
  108. case D_DCR:
  109. case D_CONST:
  110. case D_BRANCH:
  111. off = Bgetc(bp);
  112. off |= Bgetc(bp) << 8;
  113. off |= Bgetc(bp) << 16;
  114. off |= Bgetc(bp) << 24;
  115. if(off < 0)
  116. off = -off;
  117. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  118. _offset(a.sym, off);
  119. break;
  120. case D_SCONST:
  121. skip(bp, NSNAME);
  122. break;
  123. case D_FCONST:
  124. skip(bp, 8);
  125. break;
  126. }
  127. return a;
  128. }
  129. static char
  130. type2char(int t)
  131. {
  132. switch(t){
  133. case D_EXTERN: return 'U';
  134. case D_STATIC: return 'b';
  135. case D_AUTO: return 'a';
  136. case D_PARAM: return 'p';
  137. default: return UNKNOWN;
  138. }
  139. }
  140. static void
  141. skip(Biobuf *bp, int n)
  142. {
  143. while (n-- > 0)
  144. Bgetc(bp);
  145. }