1 /* 2 * obj.c 3 * routines universal to all object files 4 */ 5 #include <u.h> 6 #include <libc.h> 7 #include <bio.h> 8 #include <ar.h> 9 #include <mach.h> 10 #include "obj.h" 11 12 #define islocal(t) ((t)=='a' || (t)=='p') 13 14 enum 15 { 16 NNAMES = 50, 17 MAXIS = 8, /* max length to determine if a file is a .? file */ 18 MAXOFF = 0x7fffffff, /* larger than any possible local offset */ 19 NHASH = 1024, /* must be power of two */ 20 HASHMUL = 79L, 21 }; 22 23 int _is2(char*), /* in [$OS].c */ 24 _is5(char*), 25 _is6(char*), 26 _is7(char*), 27 _is8(char*), 28 _isk(char*), 29 _isq(char*), 30 _isv(char*), 31 _isu(char*), 32 _read2(Biobuf*, Prog*), 33 _read5(Biobuf*, Prog*), 34 _read6(Biobuf*, Prog*), 35 _read7(Biobuf*, Prog*), 36 _read8(Biobuf*, Prog*), 37 _readk(Biobuf*, Prog*), 38 _readq(Biobuf*, Prog*), 39 _readv(Biobuf*, Prog*), 40 _readu(Biobuf*, Prog*); 41 42 typedef struct Obj Obj; 43 typedef struct Symtab Symtab; 44 45 struct Obj /* functions to handle each intermediate (.$O) file */ 46 { 47 char *name; /* name of each $O file */ 48 int (*is)(char*); /* test for each type of $O file */ 49 int (*read)(Biobuf*, Prog*); /* read for each type of $O file*/ 50 }; 51 52 static Obj obj[] = 53 { /* functions to identify and parse each type of obj */ 54 [Obj68020] "68020 .2", _is2, _read2, 55 [ObjAmd64] "amd64 .6", _is6, _read6, 56 [ObjArm] "arm .5", _is5, _read5, 57 [ObjAlpha] "alpha .7", _is7, _read7, 58 [Obj386] "386 .8", _is8, _read8, 59 [ObjSparc] "sparc .k", _isk, _readk, 60 [ObjPower] "power .q", _isq, _readq, 61 [ObjMips] "mips .v", _isv, _readv, 62 [ObjSparc64] "sparc64 .u", _isu, _readu, 63 [Maxobjtype] 0, 0 64 }; 65 66 struct Symtab 67 { 68 struct Sym s; 69 struct Symtab *next; 70 }; 71 72 static Symtab *hash[NHASH]; 73 static Sym *names[NNAMES]; /* working set of active names */ 74 75 static int processprog(Prog*,int); /* decode each symbol reference */ 76 static void objreset(void); 77 static void objlookup(int, char *, int, uint); 78 static void objupdate(int, int); 79 80 int 81 objtype(Biobuf *bp, char **name) 82 { 83 int i; 84 char buf[MAXIS]; 85 86 if(Bread(bp, buf, MAXIS) < MAXIS) 87 return -1; 88 Bseek(bp, -MAXIS, 1); 89 for (i = 0; i < Maxobjtype; i++) { 90 if (obj[i].is && (*obj[i].is)(buf)) { 91 if (name) 92 *name = obj[i].name; 93 return i; 94 } 95 } 96 return -1; 97 } 98 99 int 100 isar(Biobuf *bp) 101 { 102 int n; 103 char magbuf[SARMAG]; 104 105 n = Bread(bp, magbuf, SARMAG); 106 if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0) 107 return 1; 108 return 0; 109 } 110 111 /* 112 * determine what kind of object file this is and process it. 113 * return whether or not this was a recognized intermediate file. 114 */ 115 int 116 readobj(Biobuf *bp, int objtype) 117 { 118 Prog p; 119 120 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0) 121 return 1; 122 objreset(); 123 while ((*obj[objtype].read)(bp, &p)) 124 if (!processprog(&p, 1)) 125 return 0; 126 return 1; 127 } 128 129 int 130 readar(Biobuf *bp, int objtype, vlong end, int doautos) 131 { 132 Prog p; 133 134 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0) 135 return 1; 136 objreset(); 137 while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end) 138 if (!processprog(&p, doautos)) 139 return 0; 140 return 1; 141 } 142 143 /* 144 * decode a symbol reference or definition 145 */ 146 static int 147 processprog(Prog *p, int doautos) 148 { 149 if(p->kind == aNone) 150 return 1; 151 if(p->sym < 0 || p->sym >= NNAMES) 152 return 0; 153 switch(p->kind) 154 { 155 case aName: 156 if (!doautos) 157 if(p->type != 'U' && p->type != 'b') 158 break; 159 objlookup(p->sym, p->id, p->type, p->sig); 160 break; 161 case aText: 162 objupdate(p->sym, 'T'); 163 break; 164 case aData: 165 objupdate(p->sym, 'D'); 166 break; 167 default: 168 break; 169 } 170 return 1; 171 } 172 173 /* 174 * find the entry for s in the symbol array. 175 * make a new entry if it is not already there. 176 */ 177 static void 178 objlookup(int id, char *name, int type, uint sig) 179 { 180 long h; 181 char *cp; 182 Sym *s; 183 Symtab *sp; 184 185 s = names[id]; 186 if(s && strcmp(s->name, name) == 0) { 187 s->type = type; 188 s->sig = sig; 189 return; 190 } 191 192 h = *name; 193 for(cp = name+1; *cp; h += *cp++) 194 h *= HASHMUL; 195 if(h < 0) 196 h = ~h; 197 h &= (NHASH-1); 198 if (type == 'U' || type == 'b' || islocal(type)) { 199 for(sp = hash[h]; sp; sp = sp->next) 200 if(strcmp(sp->s.name, name) == 0) { 201 switch(sp->s.type) { 202 case 'T': 203 case 'D': 204 case 'U': 205 if (type == 'U') { 206 names[id] = &sp->s; 207 return; 208 } 209 break; 210 case 't': 211 case 'd': 212 case 'b': 213 if (type == 'b') { 214 names[id] = &sp->s; 215 return; 216 } 217 break; 218 case 'a': 219 case 'p': 220 if (islocal(type)) { 221 names[id] = &sp->s; 222 return; 223 } 224 break; 225 default: 226 break; 227 } 228 } 229 } 230 sp = malloc(sizeof(Symtab)); 231 sp->s.name = name; 232 sp->s.type = type; 233 sp->s.sig = sig; 234 sp->s.value = islocal(type) ? MAXOFF : 0; 235 names[id] = &sp->s; 236 sp->next = hash[h]; 237 hash[h] = sp; 238 return; 239 } 240 /* 241 * traverse the symbol lists 242 */ 243 void 244 objtraverse(void (*fn)(Sym*, void*), void *pointer) 245 { 246 int i; 247 Symtab *s; 248 249 for(i = 0; i < NHASH; i++) 250 for(s = hash[i]; s; s = s->next) 251 (*fn)(&s->s, pointer); 252 } 253 254 /* 255 * update the offset information for a 'a' or 'p' symbol in an intermediate file 256 */ 257 void 258 _offset(int id, vlong off) 259 { 260 Sym *s; 261 262 s = names[id]; 263 if (s && s->name[0] && islocal(s->type) && s->value > off) 264 s->value = off; 265 } 266 267 /* 268 * update the type of a global text or data symbol 269 */ 270 static void 271 objupdate(int id, int type) 272 { 273 Sym *s; 274 275 s = names[id]; 276 if (s && s->name[0]) 277 if (s->type == 'U') 278 s->type = type; 279 else if (s->type == 'b') 280 s->type = tolower(type); 281 } 282 283 /* 284 * look for the next file in an archive 285 */ 286 int 287 nextar(Biobuf *bp, int offset, char *buf) 288 { 289 struct ar_hdr a; 290 int i, r; 291 long arsize; 292 293 if (offset&01) 294 offset++; 295 Bseek(bp, offset, 0); 296 r = Bread(bp, &a, SAR_HDR); 297 if(r != SAR_HDR) 298 return 0; 299 if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag))) 300 return -1; 301 for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++) 302 buf[i] = a.name[i]; 303 buf[i] = 0; 304 arsize = strtol(a.size, 0, 0); 305 if (arsize&1) 306 arsize++; 307 return arsize + SAR_HDR; 308 } 309 310 static void 311 objreset(void) 312 { 313 int i; 314 Symtab *s, *n; 315 316 for(i = 0; i < NHASH; i++) { 317 for(s = hash[i]; s; s = n) { 318 n = s->next; 319 free(s->s.name); 320 free(s); 321 } 322 hash[i] = 0; 323 } 324 memset(names, 0, sizeof names); 325 } 326