qobj.c 2.4 KB

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