2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
#include "leaky.h"
|
|
|
|
|
|
|
|
#ifdef USE_COFF
|
|
|
|
|
|
|
|
# define LANGUAGE_C
|
|
|
|
# include <sym.h>
|
|
|
|
# include <cmplrs/stsupport.h>
|
|
|
|
# include <symconst.h>
|
|
|
|
# include <filehdr.h>
|
|
|
|
# include <ldfcn.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdlib.h>
|
2019-01-18 12:16:18 +03:00
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
# ifdef IRIX4
|
|
|
|
extern "C" {
|
2019-05-01 11:47:10 +03:00
|
|
|
extern char* demangle(char const* in);
|
2000-01-18 06:43:26 +03:00
|
|
|
};
|
|
|
|
# else
|
|
|
|
# include <dem.h>
|
|
|
|
# endif
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static char* Demangle(char* rawName) {
|
2000-01-18 06:43:26 +03:00
|
|
|
# ifdef IRIX4
|
|
|
|
return strdup(demangle(rawName));
|
|
|
|
# else
|
|
|
|
char namebuf[4000];
|
|
|
|
demangle(rawName, namebuf);
|
|
|
|
return strdup(namebuf);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void leaky::readSymbols(const char* fileName) {
|
|
|
|
LDFILE* ldptr;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2014-01-06 19:07:38 +04:00
|
|
|
ldptr = ldopen(fileName, nullptr);
|
2000-01-18 06:43:26 +03:00
|
|
|
if (!ldptr) {
|
|
|
|
fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName, fileName);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (PSYMTAB(ldptr) == 0) {
|
|
|
|
fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName,
|
|
|
|
fileName);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
long isymMax = SYMHEADER(ldptr).isymMax;
|
|
|
|
long iextMax = SYMHEADER(ldptr).iextMax;
|
|
|
|
long iMax = isymMax + iextMax;
|
|
|
|
|
|
|
|
long alloced = 10000;
|
2019-05-01 11:47:10 +03:00
|
|
|
Symbol* syms = (Symbol*)malloc(sizeof(Symbol) * 10000);
|
|
|
|
Symbol* sp = syms;
|
|
|
|
Symbol* last = syms + alloced;
|
2000-01-18 06:43:26 +03:00
|
|
|
SYMR symr;
|
|
|
|
|
|
|
|
for (long isym = 0; isym < iMax; isym++) {
|
|
|
|
if (ldtbread(ldptr, isym, &symr) != SUCCESS) {
|
|
|
|
fprintf(stderr, "%s: can't read symbol #%d\n", applicationName, isym);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (isym < isymMax) {
|
|
|
|
if ((symr.st == stStaticProc) ||
|
|
|
|
((symr.st == stProc) &&
|
|
|
|
((symr.sc == scText) || (symr.sc == scAbs))) ||
|
|
|
|
((symr.st == stBlock) && (symr.sc == scText))) {
|
|
|
|
// Text symbol. Set name field to point to the symbol name
|
|
|
|
sp->name = Demangle(ldgetname(ldptr, &symr));
|
|
|
|
sp->address = symr.value;
|
2018-11-30 13:46:48 +03:00
|
|
|
sp++;
|
2000-01-18 06:43:26 +03:00
|
|
|
if (sp >= last) {
|
|
|
|
long n = alloced + 10000;
|
2019-05-01 11:47:10 +03:00
|
|
|
syms = (Symbol*)realloc(syms, (size_t)(sizeof(Symbol) * n));
|
2000-01-18 06:43:26 +03:00
|
|
|
last = syms + n;
|
|
|
|
sp = syms + alloced;
|
|
|
|
alloced = n;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
int interesting = sp - syms;
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!quiet) {
|
2000-01-18 06:43:26 +03:00
|
|
|
printf("Total of %d symbols\n", interesting);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
usefulSymbols = interesting;
|
|
|
|
externalSymbols = syms;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_COFF */
|