Back to index

lightning-sunbird  0.9+nobinonly
coff.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is Kipp E.B. Hickman.
00017  * Portions created by the Initial Developer are Copyright (C) 1999
00018  * the Initial Developer. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *
00022  * Alternatively, the contents of this file may be used under the terms of
00023  * either the GNU General Public License Version 2 or later (the "GPL"), or
00024  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00025  * in which case the provisions of the GPL or the LGPL are applicable instead
00026  * of those above. If you wish to allow use of your version of this file only
00027  * under the terms of either the GPL or the LGPL, and not to allow others to
00028  * use your version of this file under the terms of the MPL, indicate your
00029  * decision by deleting the provisions above and replace them with the notice
00030  * and other provisions required by the GPL or the LGPL. If you do not delete
00031  * the provisions above, a recipient may use your version of this file under
00032  * the terms of any one of the MPL, the GPL or the LGPL.
00033  *
00034  * ***** END LICENSE BLOCK ***** */
00035 
00036 #include "leaky.h"
00037 
00038 #ifdef USE_COFF
00039 
00040 #define       LANGUAGE_C
00041 #include <sym.h>
00042 #include <cmplrs/stsupport.h>
00043 #include <symconst.h>
00044 #include <filehdr.h>
00045 #include <ldfcn.h>
00046 #include <string.h>
00047 #include <stdlib.h>
00048 
00049 #ifdef IRIX4
00050 extern "C" {
00051     extern char *demangle(char const* in);
00052 };
00053 #else
00054 #include <dem.h>
00055 #endif
00056 
00057 static char *Demangle(char *rawName)
00058 {
00059 #ifdef IRIX4
00060     return strdup(demangle(rawName));
00061 #else
00062     char namebuf[4000];
00063     demangle(rawName, namebuf);
00064     return strdup(namebuf);
00065 #endif
00066 }
00067 
00068 void leaky::readSymbols(const char *fileName)
00069 {
00070     LDFILE *ldptr;
00071 
00072     ldptr = ldopen(fileName, NULL);
00073     if (!ldptr) {
00074        fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName,
00075               fileName);
00076        exit(-1);
00077     }
00078     if (PSYMTAB(ldptr) == 0) {
00079        fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName,
00080               fileName);
00081        exit(-1);
00082     }
00083 
00084     long isymMax = SYMHEADER(ldptr).isymMax;
00085     long iextMax = SYMHEADER(ldptr).iextMax;
00086     long iMax = isymMax + iextMax;
00087 
00088     long alloced = 10000;
00089     Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000);
00090     Symbol* sp = syms;
00091     Symbol* last = syms + alloced;
00092     SYMR symr;
00093 
00094     for (long isym = 0; isym < iMax; isym++) {
00095        if (ldtbread(ldptr, isym, &symr) != SUCCESS) {
00096            fprintf(stderr, "%s: can't read symbol #%d\n", applicationName,
00097                   isym);
00098            exit(-1);
00099        }
00100        if (isym < isymMax) {
00101            if ((symr.st == stStaticProc)
00102               || ((symr.st == stProc) &&
00103                   ((symr.sc == scText) || (symr.sc == scAbs)))
00104               || ((symr.st == stBlock) &&
00105                   (symr.sc == scText))) {
00106               // Text symbol. Set name field to point to the symbol name
00107               sp->name = Demangle(ldgetname(ldptr, &symr));
00108               sp->address = symr.value;
00109               sp++;
00110               if (sp >= last) {
00111                   long n = alloced + 10000;
00112                   syms = (Symbol*)
00113                      realloc(syms, (size_t) (sizeof(Symbol) * n));
00114                   last = syms + n;
00115                   sp = syms + alloced;
00116                   alloced = n;
00117               }
00118            }
00119        }
00120     }
00121 
00122     int interesting = sp - syms;
00123     if (!quiet) {
00124        printf("Total of %d symbols\n", interesting);
00125     }
00126     usefulSymbols = interesting;
00127     externalSymbols = syms;
00128 }
00129 
00130 #endif /* USE_COFF */