michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "leaky.h" michael@0: michael@0: #ifdef USE_COFF michael@0: michael@0: #define LANGUAGE_C michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef IRIX4 michael@0: extern "C" { michael@0: extern char *demangle(char const* in); michael@0: }; michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: static char *Demangle(char *rawName) michael@0: { michael@0: #ifdef IRIX4 michael@0: return strdup(demangle(rawName)); michael@0: #else michael@0: char namebuf[4000]; michael@0: demangle(rawName, namebuf); michael@0: return strdup(namebuf); michael@0: #endif michael@0: } michael@0: michael@0: void leaky::readSymbols(const char *fileName) michael@0: { michael@0: LDFILE *ldptr; michael@0: michael@0: ldptr = ldopen(fileName, nullptr); michael@0: if (!ldptr) { michael@0: fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName, michael@0: fileName); michael@0: exit(-1); michael@0: } michael@0: if (PSYMTAB(ldptr) == 0) { michael@0: fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName, michael@0: fileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: long isymMax = SYMHEADER(ldptr).isymMax; michael@0: long iextMax = SYMHEADER(ldptr).iextMax; michael@0: long iMax = isymMax + iextMax; michael@0: michael@0: long alloced = 10000; michael@0: Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000); michael@0: Symbol* sp = syms; michael@0: Symbol* last = syms + alloced; michael@0: SYMR symr; michael@0: michael@0: for (long isym = 0; isym < iMax; isym++) { michael@0: if (ldtbread(ldptr, isym, &symr) != SUCCESS) { michael@0: fprintf(stderr, "%s: can't read symbol #%d\n", applicationName, michael@0: isym); michael@0: exit(-1); michael@0: } michael@0: if (isym < isymMax) { michael@0: if ((symr.st == stStaticProc) michael@0: || ((symr.st == stProc) && michael@0: ((symr.sc == scText) || (symr.sc == scAbs))) michael@0: || ((symr.st == stBlock) && michael@0: (symr.sc == scText))) { michael@0: // Text symbol. Set name field to point to the symbol name michael@0: sp->name = Demangle(ldgetname(ldptr, &symr)); michael@0: sp->address = symr.value; michael@0: sp++; michael@0: if (sp >= last) { michael@0: long n = alloced + 10000; michael@0: syms = (Symbol*) michael@0: realloc(syms, (size_t) (sizeof(Symbol) * n)); michael@0: last = syms + n; michael@0: sp = syms + alloced; michael@0: alloced = n; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: int interesting = sp - syms; michael@0: if (!quiet) { michael@0: printf("Total of %d symbols\n", interesting); michael@0: } michael@0: usefulSymbols = interesting; michael@0: externalSymbols = syms; michael@0: } michael@0: michael@0: #endif /* USE_COFF */