7obj.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * 7obj.c - identify and parse an alpha object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include <mach.h>
  8. #include "7c/7.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. _is7(char *s)
  22. {
  23. return s[0] == ANAME /* ANAME */
  24. && s[1] == D_FILE /* type */
  25. && s[2] == 1 /* sym */
  26. && s[3] == '<'; /* name of file */
  27. }
  28. int
  29. _read7(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. p->sig = 0;
  38. if(as == ANAME || as == ASIGNAME){
  39. if(as == ASIGNAME){
  40. Bread(bp, &p->sig, 4);
  41. p->sig = leswal(p->sig);
  42. }
  43. p->kind = aName;
  44. p->type = type2char(Bgetc(bp)); /* type */
  45. p->sym = Bgetc(bp); /* sym */
  46. n = 0;
  47. for(;;) {
  48. as = Bgetc(bp);
  49. if(as < 0)
  50. return 0;
  51. n++;
  52. if(as == 0)
  53. break;
  54. }
  55. p->id = malloc(n);
  56. if(p->id == 0)
  57. return 0;
  58. Bseek(bp, -n, 1);
  59. if(Bread(bp, p->id, n) != n)
  60. return 0;
  61. return 1;
  62. }
  63. if(as == ATEXT)
  64. p->kind = aText;
  65. else if(as == AGLOBL)
  66. p->kind = aData;
  67. skip(bp, 5); /* reg(1), lineno(4) */
  68. a = addr(bp);
  69. addr(bp);
  70. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  71. p->kind = aNone;
  72. p->sym = a.sym;
  73. return 1;
  74. }
  75. static Addr
  76. addr(Biobuf *bp)
  77. {
  78. Addr a;
  79. vlong off;
  80. a.type = Bgetc(bp); /* a.type */
  81. skip(bp,1); /* reg */
  82. a.sym = Bgetc(bp); /* sym index */
  83. a.name = Bgetc(bp); /* sym type */
  84. switch(a.type){
  85. default:
  86. case D_NONE: case D_REG: case D_FREG: case D_PREG:
  87. case D_FCREG: case D_PCC:
  88. break;
  89. case D_OREG:
  90. case D_CONST:
  91. case D_BRANCH:
  92. off = (uvlong)Bgetc(bp);
  93. off |= (uvlong)Bgetc(bp) << 8;
  94. off |= (uvlong)Bgetc(bp) << 16;
  95. off |= (uvlong)Bgetc(bp) << 24;
  96. off |= (uvlong)Bgetc(bp) << 32;
  97. off |= (uvlong)Bgetc(bp) << 40;
  98. off |= (uvlong)Bgetc(bp) << 48;
  99. off |= (uvlong)Bgetc(bp) << 56;
  100. if(off < 0)
  101. off = -off;
  102. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  103. _offset(a.sym, off);
  104. break;
  105. case D_SCONST:
  106. skip(bp, NSNAME);
  107. break;
  108. case D_FCONST:
  109. skip(bp, 8);
  110. break;
  111. }
  112. return a;
  113. }
  114. static char
  115. type2char(int t)
  116. {
  117. switch(t){
  118. case D_EXTERN: return 'U';
  119. case D_STATIC: return 'b';
  120. case D_AUTO: return 'a';
  121. case D_PARAM: return 'p';
  122. default: return UNKNOWN;
  123. }
  124. }
  125. static void
  126. skip(Biobuf *bp, int n)
  127. {
  128. while (n-- > 0)
  129. Bgetc(bp);
  130. }