qobj.c 2.2 KB

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