qobj.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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] == D_FILE /* type */
  26. && s[2] == 1 /* sym */
  27. && s[3] == '<'; /* name of file */
  28. }
  29. int
  30. _readq(Biobuf *bp, Prog *p)
  31. {
  32. int as, n;
  33. Addr a;
  34. as = Bgetc(bp); /* as */
  35. if(as < 0)
  36. return 0;
  37. p->kind = aNone;
  38. p->sig = 0;
  39. if(as == ANAME || as == ASIGNAME){
  40. if(as == ASIGNAME){
  41. Bread(bp, &p->sig, 4);
  42. p->sig = beswal(p->sig);
  43. }
  44. p->kind = aName;
  45. p->type = type2char(Bgetc(bp)); /* type */
  46. p->sym = Bgetc(bp); /* sym */
  47. n = 0;
  48. for(;;) {
  49. as = Bgetc(bp);
  50. if(as < 0)
  51. return 0;
  52. n++;
  53. if(as == 0)
  54. break;
  55. }
  56. p->id = malloc(n);
  57. if(p->id == 0)
  58. return 0;
  59. Bseek(bp, -n, 1);
  60. if(Bread(bp, p->id, n) != n)
  61. return 0;
  62. return 1;
  63. }
  64. if(as == ATEXT)
  65. p->kind = aText;
  66. else if(as == AGLOBL)
  67. p->kind = aData;
  68. n = Bgetc(bp); /* reg and flag */
  69. skip(bp, 4); /* lineno(4) */
  70. a = addr(bp);
  71. if(n & 0x40)
  72. addr(bp);
  73. addr(bp);
  74. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  75. p->kind = aNone;
  76. p->sym = a.sym;
  77. return 1;
  78. }
  79. static Addr
  80. addr(Biobuf *bp)
  81. {
  82. Addr a;
  83. long off;
  84. a.type = Bgetc(bp); /* a.type */
  85. skip(bp,1); /* reg */
  86. a.sym = Bgetc(bp); /* sym index */
  87. a.name = Bgetc(bp); /* sym type */
  88. switch(a.type){
  89. default:
  90. case D_NONE: case D_REG: case D_FREG: case D_CREG:
  91. case D_FPSCR: case D_MSR: case D_SREG:
  92. break;
  93. case D_SPR:
  94. case D_OREG:
  95. case D_CONST:
  96. case D_BRANCH:
  97. off = Bgetc(bp);
  98. off |= Bgetc(bp) << 8;
  99. off |= Bgetc(bp) << 16;
  100. off |= Bgetc(bp) << 24;
  101. if(off < 0)
  102. off = -off;
  103. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  104. _offset(a.sym, off);
  105. break;
  106. case D_SCONST:
  107. skip(bp, NSNAME);
  108. break;
  109. case D_FCONST:
  110. skip(bp, 8);
  111. break;
  112. }
  113. return a;
  114. }
  115. static char
  116. type2char(int t)
  117. {
  118. switch(t){
  119. case D_EXTERN: return 'U';
  120. case D_STATIC: return 'b';
  121. case D_AUTO: return 'a';
  122. case D_PARAM: return 'p';
  123. default: return UNKNOWN;
  124. }
  125. }
  126. static void
  127. skip(Biobuf *bp, int n)
  128. {
  129. while (n-- > 0)
  130. Bgetc(bp);
  131. }