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 ); 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, int 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); 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) 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 return; 189 } 190 191 h = *name; 192 for(cp = name+1; *cp; h += *cp++) 193 h *= HASHMUL; 194 if(h < 0) 195 h = ~h; 196 h &= (NHASH-1); 197 if (type == 'U' || type == 'b' || islocal(type)) { 198 for(sp = hash[h]; sp; sp = sp->next) 199 if(strcmp(sp->s.name, name) == 0) { 200 switch(sp->s.type) { 201 case 'T': 202 case 'D': 203 case 'U': 204 if (type == 'U') { 205 names[id] = &sp->s; 206 return; 207 } 208 break; 209 case 't': 210 case 'd': 211 case 'b': 212 if (type == 'b') { 213 names[id] = &sp->s; 214 return; 215 } 216 break; 217 case 'a': 218 case 'p': 219 if (islocal(type)) { 220 names[id] = &sp->s; 221 return; 222 } 223 break; 224 default: 225 break; 226 } 227 } 228 } 229 sp = malloc(sizeof(Symtab)); 230 sp->s.name = name; 231 sp->s.type = type; 232 sp->s.value = islocal(type) ? MAXOFF : 0; 233 names[id] = &sp->s; 234 sp->next = hash[h]; 235 hash[h] = sp; 236 return; 237 } 238 /* 239 * traverse the symbol lists 240 */ 241 void 242 objtraverse(void (*fn)(Sym*, void*), void *pointer) 243 { 244 int i; 245 Symtab *s; 246 247 for(i = 0; i < NHASH; i++) 248 for(s = hash[i]; s; s = s->next) 249 (*fn)(&s->s, pointer); 250 } 251 252 /* 253 * update the offset information for a 'a' or 'p' symbol in an intermediate file 254 */ 255 void 256 _offset(int id, long off) 257 { 258 Sym *s; 259 260 s = names[id]; 261 if (s && s->name[0] && islocal(s->type) && s->value > off) 262 s->value = off; 263 } 264 265 /* 266 * update the type of a global text or data symbol 267 */ 268 static void 269 objupdate(int id, int type) 270 { 271 Sym *s; 272 273 s = names[id]; 274 if (s && s->name[0]) 275 if (s->type == 'U') 276 s->type = type; 277 else if (s->type == 'b') 278 s->type = tolower(type); 279 } 280 281 /* 282 * look for the next file in an archive 283 */ 284 int 285 nextar(Biobuf *bp, int offset, char *buf) 286 { 287 struct ar_hdr a; 288 int i, r; 289 long arsize; 290 291 if (offset&01) 292 offset++; 293 Bseek(bp, offset, 0); 294 r = Bread(bp, &a, SAR_HDR); 295 if(r != SAR_HDR) 296 return 0; 297 if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag))) 298 return -1; 299 for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++) 300 buf[i] = a.name[i]; 301 buf[i] = 0; 302 arsize = atol(a.size); 303 if (arsize&1) 304 arsize++; 305 return arsize + SAR_HDR; 306 } 307 308 static void 309 objreset(void) 310 { 311 int i; 312 Symtab *s, *n; 313 314 for(i = 0; i < NHASH; i++) { 315 for(s = hash[i]; s; s = n) { 316 n = s->next; 317 free(s->s.name); 318 free(s); 319 } 320 hash[i] = 0; 321 } 322 memset(names, 0, sizeof names); 323 } 324