|
|
1.1 ! root 1: /* ! 2: scan nm output and detect constructors and destructors for static objects. ! 3: the name on an nm output line is expected to be in the right hand margin. ! 4: the name is expected to be on the form __STD*_ or __STI*_ and less than ! 5: 100 characters long. ! 6: nm output lines are assumed to be less than 256 characters long. ! 7: constructors found are called by _main() called from main(). ! 8: destructors found is called by exit(). ! 9: return 0 if no constructor or destructor is found otherwise. ! 10: The output is ? ! 11: ! 12: */ ! 13: ! 14: #include <stdio.h> ! 15: //extern int strcpy(char*, char*); ! 16: //extern char * strtok(char*, char*); ! 17: ! 18: struct sbuf { ! 19: sbuf* next; ! 20: char str[100]; ! 21: sbuf(sbuf* l, char* p); ! 22: }; ! 23: ! 24: sbuf::sbuf(sbuf* l, char* p) ! 25: { ! 26: next=l; ! 27: // strcpy(str,strtok(p," |")); ! 28: // ``unrolled'' since strtok() is not on bsd systems ! 29: register char* s = str; ! 30: for (register char c = *p++; c && c!=' ' && c!='|'; c = *p++) *s++ = c; ! 31: *s = 0; ! 32: } ! 33: ! 34: sbuf* dtor; // list of constructors ! 35: sbuf* ctor; // list of destructors ! 36: ! 37: void _main() { }; // make munch.c independent of libC.a ! 38: char* malloc(unsigned); ! 39: void* operator new(long s) { return malloc(s); } ! 40: ! 41: main (int, char* argv[]) ! 42: { ! 43: char buf[256]; ! 44: register char* p; ! 45: ! 46: newline: ! 47: p = buf; ! 48: for(;;) { ! 49: int c; ! 50: switch (c=getchar()) { ! 51: case EOF: ! 52: goto done; ! 53: case '\n': ! 54: { if (p == buf) goto newline; // ignore empty lines ! 55: *p = 0; // terminate string ! 56: p = buf; ! 57: while (*p++!='_') if (*p == 0) goto newline; ! 58: if (p[-2] == '.') goto newline; // RT: ignore ._STI ! 59: for (p--; *p == '_'; p++) ; // accept _STI and __STI ! 60: register char* st = p-1; ! 61: if (st[0]!='_' || ! 62: (st[1]!='S' && st[1]!='s') || ! 63: (st[2]!='T' && st[2]!='t') || ! 64: (st[1]=='s' && st[2]=='t' && ! 65: (st[4]!='_' || st[5]!='_' || st[-1]!='_'))) goto newline; ! 66: switch (st[3]) { ! 67: case 'd': ! 68: st--; ! 69: case 'D': ! 70: dtor = new sbuf(dtor,st); ! 71: goto newline; ! 72: case 'i': ! 73: st--; ! 74: case 'I': ! 75: ctor = new sbuf(ctor,st); ! 76: default: ! 77: goto newline; ! 78: } ! 79: } ! 80: default: ! 81: *p++ = c; ! 82: } ! 83: } ! 84: ! 85: done: ! 86: int cond = dtor||ctor; ! 87: ! 88: if (cond == 0) exit(0); ! 89: ! 90: printf("typedef int (*PFV)();\n"); // "int" to dodge bsd4.2 bug ! 91: if (ctor) { ! 92: for (sbuf* p = ctor; p; p=p->next) printf("int %s();\n",p->str); ! 93: printf("extern PFV _ctors[];\nPFV _ctors[] = {\n"); ! 94: for (sbuf* q = ctor; q; q=q->next) printf("\t%s,\n",q->str); ! 95: printf("\t0\n};\n"); ! 96: } ! 97: ! 98: if (dtor) { ! 99: for (sbuf* p = dtor; p; p=p->next) printf("int %s();\n",p->str); ! 100: printf("extern PFV _dtors[];\nPFV _dtors[] = {\n"); ! 101: for (sbuf* q = dtor; q; q=q->next) printf("\t%s,\n",q->str); ! 102: printf("\t0\n};\n"); ! 103: } ! 104: ! 105: exit(1); ! 106: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.