6obj.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * 6obj.c - identify and parse an amd64 object file
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <mach.h>
  16. #include "6c/6.out.h"
  17. #include "obj.h"
  18. typedef struct Addr Addr;
  19. struct Addr
  20. {
  21. char sym;
  22. char flags;
  23. };
  24. static Addr addr(Biobuf*);
  25. static char type2char(int);
  26. static void skip(Biobuf*, int);
  27. int
  28. _is6(char *t)
  29. {
  30. uint8_t *s = (uint8_t*)t;
  31. return s[0] == (ANAME&0xff) /* aslo = ANAME */
  32. && s[1] == ((ANAME>>8)&0xff)
  33. && s[2] == D_FILE /* type */
  34. && s[3] == 1 /* sym */
  35. && s[4] == '<'; /* name of file */
  36. }
  37. int
  38. _read6(Biobuf *bp, Prog* p)
  39. {
  40. int as, n, c;
  41. Addr a;
  42. as = Bgetc(bp); /* as(low) */
  43. if(as < 0)
  44. return 0;
  45. c = Bgetc(bp); /* as(high) */
  46. if(c < 0)
  47. return 0;
  48. as |= ((c & 0xff) << 8);
  49. p->kind = aNone;
  50. p->sig = 0;
  51. if(as == ANAME || as == ASIGNAME){
  52. if(as == ASIGNAME){
  53. Bread(bp, &p->sig, 4);
  54. p->sig = leswal(p->sig);
  55. }
  56. p->kind = aName;
  57. p->type = type2char(Bgetc(bp)); /* type */
  58. p->sym = Bgetc(bp); /* sym */
  59. n = 0;
  60. for(;;) {
  61. as = Bgetc(bp);
  62. if(as < 0)
  63. return 0;
  64. n++;
  65. if(as == 0)
  66. break;
  67. }
  68. p->id = malloc(n);
  69. if(p->id == 0)
  70. return 0;
  71. Bseek(bp, -n, 1);
  72. if(Bread(bp, p->id, n) != n)
  73. return 0;
  74. return 1;
  75. }
  76. if(as == ATEXT)
  77. p->kind = aText;
  78. if(as == AGLOBL)
  79. p->kind = aData;
  80. skip(bp, 4); /* lineno(4) */
  81. a = addr(bp);
  82. addr(bp);
  83. if(!(a.flags & T_SYM))
  84. p->kind = aNone;
  85. p->sym = a.sym;
  86. return 1;
  87. }
  88. static Addr
  89. addr(Biobuf *bp)
  90. {
  91. Addr a;
  92. int t;
  93. int32_t l;
  94. int64_t off;
  95. off = 0;
  96. a.sym = -1;
  97. a.flags = Bgetc(bp); /* flags */
  98. if(a.flags & T_INDEX)
  99. skip(bp, 2);
  100. if(a.flags & T_OFFSET){
  101. l = Bgetc(bp);
  102. l |= Bgetc(bp) << 8;
  103. l |= Bgetc(bp) << 16;
  104. l |= Bgetc(bp) << 24;
  105. off = l;
  106. if(a.flags & T_64){
  107. l = Bgetc(bp);
  108. l |= Bgetc(bp) << 8;
  109. l |= Bgetc(bp) << 16;
  110. l |= Bgetc(bp) << 24;
  111. off = ((int64_t)l << 32) | (off & 0xFFFFFFFF);
  112. }
  113. if(off < 0)
  114. off = -off;
  115. }
  116. if(a.flags & T_SYM)
  117. a.sym = Bgetc(bp);
  118. if(a.flags & T_FCONST)
  119. skip(bp, 8);
  120. else
  121. if(a.flags & T_SCONST)
  122. skip(bp, NSNAME);
  123. if(a.flags & T_TYPE) {
  124. t = Bgetc(bp);
  125. if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
  126. _offset(a.sym, off);
  127. }
  128. return a;
  129. }
  130. static char
  131. type2char(int t)
  132. {
  133. switch(t){
  134. case D_EXTERN: return 'U';
  135. case D_STATIC: return 'b';
  136. case D_AUTO: return 'a';
  137. case D_PARAM: return 'p';
  138. default: return UNKNOWN;
  139. }
  140. }
  141. static void
  142. skip(Biobuf *bp, int n)
  143. {
  144. while (n-- > 0)
  145. Bgetc(bp);
  146. }