qobj.c 2.3 KB

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