|
|
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: 32 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: ! 17: struct sbuf { ! 18: sbuf* next; ! 19: char str[100]; ! 20: sbuf(sbuf* l, char* p) { next=l; strcpy(str,p); } ! 21: }; ! 22: ! 23: sbuf* dtor; // list of constructors ! 24: sbuf* ctor; // list of destructors ! 25: void _main() { }; ! 26: main (int argc, char* argv[]) ! 27: { ! 28: char buf[256]; ! 29: register char* p; ! 30: int monitor = argc-1; // -p or no argument ! 31: ! 32: newline: ! 33: p = buf; ! 34: for(;;) { ! 35: register char* st; ! 36: int c; ! 37: switch (c=getchar()) { ! 38: case EOF: ! 39: goto done; ! 40: case '\n': ! 41: if (p[-1] != '_') goto newline; ! 42: *p = 0; // terminate string ! 43: p = buf; ! 44: while (*p++!='_'); ! 45: for (p--; *p == '_'; p++) ; ! 46: p--; ! 47: st = p; ! 48: if (st[0]!='_') goto newline; ! 49: if (st[1]!='S') goto newline; ! 50: if (st[2]!='T') goto newline; ! 51: switch (st[3]) { ! 52: case 'D': ! 53: dtor = new sbuf(dtor,st); ! 54: goto newline; ! 55: case 'I': ! 56: ctor = new sbuf(ctor,st); ! 57: goto newline; ! 58: default: ! 59: goto newline; ! 60: } ! 61: default: ! 62: *p++ = c; ! 63: } ! 64: } ! 65: ! 66: done: ! 67: int cond = dtor||ctor; ! 68: ! 69: if (cond == 0) return 0; ! 70: ! 71: printf("typedef int (*PFV)();\n"); // "int" to dodge bsd4.2 bug ! 72: if (monitor) printf("int monitor();\n"); ! 73: if (ctor||monitor) { ! 74: for (sbuf* p = ctor; p; p=p->next) printf("int %s();\n",p->str); ! 75: printf("extern PFV _ctors[];\nPFV _ctors[] = {\n"); ! 76: if (monitor) printf("\tmonitor,\n"); ! 77: for (sbuf* q = ctor; q; q=q->next) printf("\t%s,\n",q->str); ! 78: printf("\t0\n};\n"); ! 79: } ! 80: ! 81: if (dtor||monitor) { ! 82: for (sbuf* p = dtor; p; p=p->next) printf("int %s();\n",p->str); ! 83: printf("extern PFV _dtors[];\nPFV _dtors[] = {\n"); ! 84: for (sbuf* q = dtor; q; q=q->next) printf("\t%s,\n",q->str); ! 85: if (monitor) ! 86: printf("\tmonitor,\n\t0\n};\n"); ! 87: else ! 88: printf("\t0\n};\n"); ! 89: } ! 90: ! 91: return 1; ! 92: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.