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