5obj.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * 5obj.c - identify and parse a arm object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "5c/5.out.h"
  8. #include "obj.h"
  9. typedef struct Addr Addr;
  10. struct Addr
  11. {
  12. char type;
  13. char sym;
  14. char name;
  15. };
  16. static Addr addr(Biobuf*);
  17. static char type2char(int);
  18. static void skip(Biobuf*, int);
  19. int
  20. _is5(char *s)
  21. {
  22. return s[0] == ANAME /* ANAME */
  23. && s[1] == D_FILE /* type */
  24. && s[2] == 1 /* sym */
  25. && s[3] == '<'; /* name of file */
  26. }
  27. int
  28. _read5(Biobuf *bp, Prog *p)
  29. {
  30. int as, n;
  31. Addr a;
  32. as = Bgetc(bp); /* as */
  33. if(as < 0)
  34. return 0;
  35. p->kind = aNone;
  36. if(as == ANAME){
  37. p->kind = aName;
  38. p->type = type2char(Bgetc(bp)); /* type */
  39. p->sym = Bgetc(bp); /* sym */
  40. n = 0;
  41. for(;;) {
  42. as = Bgetc(bp);
  43. if(as < 0)
  44. return 0;
  45. n++;
  46. if(as == 0)
  47. break;
  48. }
  49. p->id = malloc(n);
  50. if(p->id == 0)
  51. return 0;
  52. Bseek(bp, -n, 1);
  53. if(Bread(bp, p->id, n) != n)
  54. return 0;
  55. return 1;
  56. }
  57. if(as == ATEXT)
  58. p->kind = aText;
  59. else if(as == AGLOBL)
  60. p->kind = aData;
  61. skip(bp, 6); /* scond(1), reg(1), lineno(4) */
  62. a = addr(bp);
  63. addr(bp);
  64. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  65. p->kind = aNone;
  66. p->sym = a.sym;
  67. return 1;
  68. }
  69. static Addr
  70. addr(Biobuf *bp)
  71. {
  72. Addr a;
  73. long off;
  74. a.type = Bgetc(bp); /* a.type */
  75. skip(bp,1); /* reg */
  76. a.sym = Bgetc(bp); /* sym index */
  77. a.name = Bgetc(bp); /* sym type */
  78. switch(a.type){
  79. default:
  80. case D_NONE:
  81. case D_REG:
  82. case D_FREG:
  83. case D_PSR:
  84. case D_FPCR:
  85. break;
  86. case D_OREG:
  87. case D_CONST:
  88. case D_BRANCH:
  89. case D_SHIFT:
  90. off = Bgetc(bp);
  91. off |= Bgetc(bp) << 8;
  92. off |= Bgetc(bp) << 16;
  93. off |= Bgetc(bp) << 24;
  94. if(off < 0)
  95. off = -off;
  96. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  97. _offset(a.sym, off);
  98. break;
  99. case D_SCONST:
  100. skip(bp, NSNAME);
  101. break;
  102. case D_FCONST:
  103. skip(bp, 8);
  104. break;
  105. }
  106. return a;
  107. }
  108. static char
  109. type2char(int t)
  110. {
  111. switch(t){
  112. case D_EXTERN: return 'U';
  113. case D_STATIC: return 'b';
  114. case D_AUTO: return 'a';
  115. case D_PARAM: return 'p';
  116. default: return UNKNOWN;
  117. }
  118. }
  119. static void
  120. skip(Biobuf *bp, int n)
  121. {
  122. while (n-- > 0)
  123. Bgetc(bp);
  124. }