Back to index

lightning-sunbird  0.9+nobinonly
bfd.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
00017  * Kipp E.B. Hickman.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "leaky.h"
00038 
00039 #ifdef USE_BFD
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include <bfd.h>
00043 
00044 extern "C" {
00045   char *cplus_demangle (const char *mangled, int options);
00046 }
00047 
00048 void leaky::ReadSymbols(const char *aFileName, u_long aBaseAddress)
00049 {
00050   static bfd_boolean kDynamic = (bfd_boolean) false;
00051 
00052   static int firstTime = 1;
00053   if (firstTime) {
00054     firstTime = 0;
00055     bfd_init ();
00056   }
00057 
00058   bfd* lib = bfd_openr(aFileName, NULL);
00059   if (NULL == lib) {
00060     return;
00061   }
00062   char **matching;
00063   if (!bfd_check_format_matches(lib, bfd_object, &matching)) {
00064     bfd_close(lib);
00065     return;
00066   }
00067 
00068   asymbol* store;
00069   store = bfd_make_empty_symbol(lib);
00070 
00071   // read mini symbols
00072   PTR minisyms;
00073   unsigned int size;
00074   long symcount = bfd_read_minisymbols(lib, kDynamic, &minisyms, &size);
00075 
00076   int initialSymbols = usefulSymbols;
00077   if (NULL == externalSymbols) {
00078     externalSymbols = (Symbol*) malloc(sizeof(Symbol) * 10000);
00079     numExternalSymbols = 10000;
00080   }
00081   Symbol* sp = externalSymbols + usefulSymbols;
00082   Symbol* lastSymbol = externalSymbols + numExternalSymbols;
00083 
00084   // Scan symbols
00085   bfd_byte* from = (bfd_byte *) minisyms;
00086   bfd_byte* fromend = from + symcount * size;
00087   for (; from < fromend; from += size) {
00088     asymbol *sym;
00089     sym = bfd_minisymbol_to_symbol(lib, kDynamic, (const PTR) from, store);
00090 
00091     symbol_info syminfo;
00092     bfd_get_symbol_info (lib, sym, &syminfo);
00093 
00094 //    if ((syminfo.type == 'T') || (syminfo.type == 't')) {
00095       const char* nm = bfd_asymbol_name(sym);
00096       if (nm && nm[0]) {
00097        char* dnm = NULL;
00098        if (strncmp("__thunk", nm, 7)) {
00099          dnm = cplus_demangle(nm, 1);
00100        }
00101        sp->Init(dnm ? dnm : nm, syminfo.value + aBaseAddress);
00102        sp++;
00103        if (sp >= lastSymbol) {
00104          long n = numExternalSymbols + 10000;
00105          externalSymbols = (Symbol*)
00106            realloc(externalSymbols, (size_t) (sizeof(Symbol) * n));
00107          lastSymbol = externalSymbols + n;
00108          sp = externalSymbols + numExternalSymbols;
00109          numExternalSymbols = n;
00110        }
00111       }
00112 //    }
00113   }
00114 
00115 
00116   bfd_close(lib);
00117 
00118   int interesting = sp - externalSymbols;
00119   if (!quiet) {
00120     printf("%s provided %d symbols\n", aFileName,
00121           interesting - initialSymbols);
00122   }
00123   usefulSymbols = interesting;
00124 }
00125 
00126 #endif /* USE_BFD */