Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "leaky.h" |
michael@0 | 6 | |
michael@0 | 7 | #ifdef USE_COFF |
michael@0 | 8 | |
michael@0 | 9 | #define LANGUAGE_C |
michael@0 | 10 | #include <sym.h> |
michael@0 | 11 | #include <cmplrs/stsupport.h> |
michael@0 | 12 | #include <symconst.h> |
michael@0 | 13 | #include <filehdr.h> |
michael@0 | 14 | #include <ldfcn.h> |
michael@0 | 15 | #include <string.h> |
michael@0 | 16 | #include <stdlib.h> |
michael@0 | 17 | |
michael@0 | 18 | #ifdef IRIX4 |
michael@0 | 19 | extern "C" { |
michael@0 | 20 | extern char *demangle(char const* in); |
michael@0 | 21 | }; |
michael@0 | 22 | #else |
michael@0 | 23 | #include <dem.h> |
michael@0 | 24 | #endif |
michael@0 | 25 | |
michael@0 | 26 | static char *Demangle(char *rawName) |
michael@0 | 27 | { |
michael@0 | 28 | #ifdef IRIX4 |
michael@0 | 29 | return strdup(demangle(rawName)); |
michael@0 | 30 | #else |
michael@0 | 31 | char namebuf[4000]; |
michael@0 | 32 | demangle(rawName, namebuf); |
michael@0 | 33 | return strdup(namebuf); |
michael@0 | 34 | #endif |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | void leaky::readSymbols(const char *fileName) |
michael@0 | 38 | { |
michael@0 | 39 | LDFILE *ldptr; |
michael@0 | 40 | |
michael@0 | 41 | ldptr = ldopen(fileName, nullptr); |
michael@0 | 42 | if (!ldptr) { |
michael@0 | 43 | fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName, |
michael@0 | 44 | fileName); |
michael@0 | 45 | exit(-1); |
michael@0 | 46 | } |
michael@0 | 47 | if (PSYMTAB(ldptr) == 0) { |
michael@0 | 48 | fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName, |
michael@0 | 49 | fileName); |
michael@0 | 50 | exit(-1); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | long isymMax = SYMHEADER(ldptr).isymMax; |
michael@0 | 54 | long iextMax = SYMHEADER(ldptr).iextMax; |
michael@0 | 55 | long iMax = isymMax + iextMax; |
michael@0 | 56 | |
michael@0 | 57 | long alloced = 10000; |
michael@0 | 58 | Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000); |
michael@0 | 59 | Symbol* sp = syms; |
michael@0 | 60 | Symbol* last = syms + alloced; |
michael@0 | 61 | SYMR symr; |
michael@0 | 62 | |
michael@0 | 63 | for (long isym = 0; isym < iMax; isym++) { |
michael@0 | 64 | if (ldtbread(ldptr, isym, &symr) != SUCCESS) { |
michael@0 | 65 | fprintf(stderr, "%s: can't read symbol #%d\n", applicationName, |
michael@0 | 66 | isym); |
michael@0 | 67 | exit(-1); |
michael@0 | 68 | } |
michael@0 | 69 | if (isym < isymMax) { |
michael@0 | 70 | if ((symr.st == stStaticProc) |
michael@0 | 71 | || ((symr.st == stProc) && |
michael@0 | 72 | ((symr.sc == scText) || (symr.sc == scAbs))) |
michael@0 | 73 | || ((symr.st == stBlock) && |
michael@0 | 74 | (symr.sc == scText))) { |
michael@0 | 75 | // Text symbol. Set name field to point to the symbol name |
michael@0 | 76 | sp->name = Demangle(ldgetname(ldptr, &symr)); |
michael@0 | 77 | sp->address = symr.value; |
michael@0 | 78 | sp++; |
michael@0 | 79 | if (sp >= last) { |
michael@0 | 80 | long n = alloced + 10000; |
michael@0 | 81 | syms = (Symbol*) |
michael@0 | 82 | realloc(syms, (size_t) (sizeof(Symbol) * n)); |
michael@0 | 83 | last = syms + n; |
michael@0 | 84 | sp = syms + alloced; |
michael@0 | 85 | alloced = n; |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | int interesting = sp - syms; |
michael@0 | 92 | if (!quiet) { |
michael@0 | 93 | printf("Total of %d symbols\n", interesting); |
michael@0 | 94 | } |
michael@0 | 95 | usefulSymbols = interesting; |
michael@0 | 96 | externalSymbols = syms; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | #endif /* USE_COFF */ |