9obj.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * 9obj.c - identify and parse a PowerPC-64 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 "9c/9.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. _is9(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. _read9(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. vlong off;
  89. long l;
  90. a.type = Bgetc(bp); /* a.type */
  91. skip(bp,1); /* reg */
  92. a.sym = Bgetc(bp); /* sym index */
  93. a.name = Bgetc(bp); /* sym type */
  94. switch(a.type){
  95. default:
  96. case D_NONE: case D_REG: case D_FREG: case D_CREG:
  97. case D_FPSCR: case D_MSR:
  98. break;
  99. case D_SPR:
  100. case D_OREG:
  101. case D_CONST:
  102. case D_BRANCH:
  103. case D_DCONST:
  104. case D_DCR:
  105. l = Bgetc(bp);
  106. l |= Bgetc(bp) << 8;
  107. l |= Bgetc(bp) << 16;
  108. l |= Bgetc(bp) << 24;
  109. off = l;
  110. if(a.type == D_DCONST){
  111. l = Bgetc(bp);
  112. l |= Bgetc(bp) << 8;
  113. l |= Bgetc(bp) << 16;
  114. l |= Bgetc(bp) << 24;
  115. off = ((vlong)l << 32) | (off & 0xFFFFFFFF);
  116. a.type = D_CONST; /* perhaps */
  117. }
  118. if(off < 0)
  119. off = -off;
  120. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  121. _offset(a.sym, off);
  122. break;
  123. case D_SCONST:
  124. skip(bp, NSNAME);
  125. break;
  126. case D_FCONST:
  127. skip(bp, 8);
  128. break;
  129. }
  130. return a;
  131. }
  132. static char
  133. type2char(int t)
  134. {
  135. switch(t){
  136. case D_EXTERN: return 'U';
  137. case D_STATIC: return 'b';
  138. case D_AUTO: return 'a';
  139. case D_PARAM: return 'p';
  140. default: return UNKNOWN;
  141. }
  142. }
  143. static void
  144. skip(Biobuf *bp, int n)
  145. {
  146. while (n-- > 0)
  147. Bgetc(bp);
  148. }