9obj.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * 9obj.c - identify and parse a PowerPC-64 object file
  11. * forsyth@terzarima.net
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <mach.h>
  17. #include "9c/9.out.h"
  18. #include "obj.h"
  19. typedef struct Addr Addr;
  20. struct Addr
  21. {
  22. char type;
  23. char sym;
  24. char name;
  25. };
  26. static Addr addr(Biobuf*);
  27. static char type2char(int);
  28. static void skip(Biobuf*, int);
  29. int
  30. _is9(char *s)
  31. {
  32. return (s[0]&0377) == ANAME /* ANAME */
  33. && (s[1]&0377) == ANAME>>8
  34. && s[2] == D_FILE /* type */
  35. && s[3] == 1 /* sym */
  36. && s[4] == '<'; /* name of file */
  37. }
  38. int
  39. _read9(Biobuf *bp, Prog *p)
  40. {
  41. int as, n, c;
  42. Addr a;
  43. as = Bgetc(bp); /* as(low) */
  44. if(as < 0)
  45. return 0;
  46. c = Bgetc(bp); /* as(high) */
  47. if(c < 0)
  48. return 0;
  49. as |= ((c & 0xff) << 8);
  50. p->kind = aNone;
  51. p->sig = 0;
  52. if(as == ANAME || as == ASIGNAME){
  53. if(as == ASIGNAME){
  54. Bread(bp, &p->sig, 4);
  55. p->sig = beswal(p->sig);
  56. }
  57. p->kind = aName;
  58. p->type = type2char(Bgetc(bp)); /* type */
  59. p->sym = Bgetc(bp); /* sym */
  60. n = 0;
  61. for(;;) {
  62. as = Bgetc(bp);
  63. if(as < 0)
  64. return 0;
  65. n++;
  66. if(as == 0)
  67. break;
  68. }
  69. p->id = malloc(n);
  70. if(p->id == 0)
  71. return 0;
  72. Bseek(bp, -n, 1);
  73. if(Bread(bp, p->id, n) != n)
  74. return 0;
  75. return 1;
  76. }
  77. if(as == ATEXT)
  78. p->kind = aText;
  79. else if(as == AGLOBL)
  80. p->kind = aData;
  81. n = Bgetc(bp); /* reg and flag */
  82. skip(bp, 4); /* lineno(4) */
  83. a = addr(bp);
  84. if(n & 0x40)
  85. addr(bp);
  86. addr(bp);
  87. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  88. p->kind = aNone;
  89. p->sym = a.sym;
  90. return 1;
  91. }
  92. static Addr
  93. addr(Biobuf *bp)
  94. {
  95. Addr a;
  96. int64_t off;
  97. int32_t l;
  98. a.type = Bgetc(bp); /* a.type */
  99. skip(bp,1); /* reg */
  100. a.sym = Bgetc(bp); /* sym index */
  101. a.name = Bgetc(bp); /* sym type */
  102. switch(a.type){
  103. default:
  104. case D_NONE: case D_REG: case D_FREG: case D_CREG:
  105. case D_FPSCR: case D_MSR:
  106. break;
  107. case D_SPR:
  108. case D_OREG:
  109. case D_CONST:
  110. case D_BRANCH:
  111. case D_DCONST:
  112. case D_DCR:
  113. l = Bgetc(bp);
  114. l |= Bgetc(bp) << 8;
  115. l |= Bgetc(bp) << 16;
  116. l |= Bgetc(bp) << 24;
  117. off = l;
  118. if(a.type == D_DCONST){
  119. l = Bgetc(bp);
  120. l |= Bgetc(bp) << 8;
  121. l |= Bgetc(bp) << 16;
  122. l |= Bgetc(bp) << 24;
  123. off = ((int64_t)l << 32) | (off & 0xFFFFFFFF);
  124. a.type = D_CONST; /* perhaps */
  125. }
  126. if(off < 0)
  127. off = -off;
  128. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  129. _offset(a.sym, off);
  130. break;
  131. case D_SCONST:
  132. skip(bp, NSNAME);
  133. break;
  134. case D_FCONST:
  135. skip(bp, 8);
  136. break;
  137. }
  138. return a;
  139. }
  140. static char
  141. type2char(int t)
  142. {
  143. switch(t){
  144. case D_EXTERN: return 'U';
  145. case D_STATIC: return 'b';
  146. case D_AUTO: return 'a';
  147. case D_PARAM: return 'p';
  148. default: return UNKNOWN;
  149. }
  150. }
  151. static void
  152. skip(Biobuf *bp, int n)
  153. {
  154. while (n-- > 0)
  155. Bgetc(bp);
  156. }