6obj.c 2.4 KB

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