3 * routines universal to all object files
12 #define islocal(t) ((t)=='a' || (t)=='p')
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 */
23 int _is2(char*), /* in [$OS].c */
33 _read2(Biobuf*, Prog*),
34 _read5(Biobuf*, Prog*),
35 _read6(Biobuf*, Prog*),
36 _read8(Biobuf*, Prog*),
37 _read9(Biobuf*, Prog*),
38 _readk(Biobuf*, Prog*),
39 _readq(Biobuf*, Prog*),
40 _readv(Biobuf*, Prog*),
41 _readu(Biobuf*, Prog*);
43 typedef struct Obj Obj;
44 typedef struct Symtab Symtab;
46 struct Obj /* functions to handle each intermediate (.$O) file */
48 char *name; /* name of each $O file */
49 int (*is)(char*); /* test for each type of $O file */
50 int (*read)(Biobuf*, Prog*); /* read for each type of $O file*/
54 { /* functions to identify and parse each type of obj */
55 [Obj68020] "68020 .2", _is2, _read2,
56 [ObjAmd64] "amd64 .6", _is6, _read6,
57 [ObjArm] "arm .5", _is5, _read5,
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 [ObjPower64] "power64 .9", _is9, _read9,
73 static Symtab *hash[NHASH];
74 static Sym *names[NNAMES]; /* working set of active names */
76 static int processprog(Prog*,int); /* decode each symbol reference */
77 static void objreset(void);
78 static void objlookup(int, char *, int, uint);
79 static void objupdate(int, int);
82 objtype(Biobuf *bp, char **name)
87 if(Bread(bp, buf, MAXIS) < MAXIS)
90 for (i = 0; i < Maxobjtype; i++) {
91 if (obj[i].is && (*obj[i].is)(buf)) {
106 n = Bread(bp, magbuf, SARMAG);
107 if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
113 * determine what kind of object file this is and process it.
114 * return whether or not this was a recognized intermediate file.
117 readobj(Biobuf *bp, int objtype)
121 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
124 while ((*obj[objtype].read)(bp, &p))
125 if (!processprog(&p, 1))
131 readar(Biobuf *bp, int objtype, vlong end, int doautos)
135 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
138 while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
139 if (!processprog(&p, doautos))
145 * decode a symbol reference or definition
148 processprog(Prog *p, int doautos)
152 if(p->sym < 0 || p->sym >= NNAMES)
158 if(p->type != 'U' && p->type != 'b')
160 objlookup(p->sym, p->id, p->type, p->sig);
163 objupdate(p->sym, 'T');
166 objupdate(p->sym, 'D');
175 * find the entry for s in the symbol array.
176 * make a new entry if it is not already there.
179 objlookup(int id, char *name, int type, uint sig)
187 if(s && strcmp(s->name, name) == 0) {
194 for(cp = name+1; *cp; h += *cp++)
199 if (type == 'U' || type == 'b' || islocal(type)) {
200 for(sp = hash[h]; sp; sp = sp->next)
201 if(strcmp(sp->s.name, name) == 0) {
231 sp = malloc(sizeof(Symtab));
235 sp->s.value = islocal(type) ? MAXOFF : 0;
242 * traverse the symbol lists
245 objtraverse(void (*fn)(Sym*, void*), void *pointer)
250 for(i = 0; i < NHASH; i++)
251 for(s = hash[i]; s; s = s->next)
252 (*fn)(&s->s, pointer);
256 * update the offset information for a 'a' or 'p' symbol in an intermediate file
259 _offset(int id, vlong off)
264 if (s && s->name[0] && islocal(s->type) && s->value > off)
269 * update the type of a global text or data symbol
272 objupdate(int id, int type)
280 else if (s->type == 'b')
281 s->type = tolower(type);
285 * look for the next file in an archive
288 nextar(Biobuf *bp, int offset, char *buf)
296 Bseek(bp, offset, 0);
297 r = Bread(bp, &a, SAR_HDR);
300 if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
302 for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
305 arsize = strtol(a.size, 0, 0);
308 return arsize + SAR_HDR;
317 for(i = 0; i < NHASH; i++) {
318 for(s = hash[i]; s; s = n) {
325 memset(names, 0, sizeof names);