| 1 | /* $Id: innidmdll.c 1517 2004-09-19 15:53:31Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * | 
|---|
| 4 | * Innotek IDM DLL - demangler DLL for the linker. | 
|---|
| 5 | * | 
|---|
| 6 | * This DLL is loaded by the linker when a symbol need demangling. This is | 
|---|
| 7 | * usually only done for error messages. For some unfortunate reason link386 | 
|---|
| 8 | * doesn't load the DLL before trying to get the exported functions. It works | 
|---|
| 9 | * nice for ilink (which uses 32bit entry points and reloads the dll for | 
|---|
| 10 | * every call it seems). | 
|---|
| 11 | * | 
|---|
| 12 | * | 
|---|
| 13 | * Copyright (c) 2003 InnoTek Systemberatung GmbH | 
|---|
| 14 | * Author: knut st. osmundsen <bird-srcspam@anduin.net> | 
|---|
| 15 | * | 
|---|
| 16 | * | 
|---|
| 17 | * This program is free software; you can redistribute it and/or modify | 
|---|
| 18 | * it under the terms of the GNU General Public License as published by | 
|---|
| 19 | * the Free Software Foundation; either version 2 of the License, or | 
|---|
| 20 | * (at your option) any later version. | 
|---|
| 21 | * | 
|---|
| 22 | * This program is distributed in the hope that it will be useful, | 
|---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 25 | * GNU General Public License for more details. | 
|---|
| 26 | * | 
|---|
| 27 | * You should have received a copy of the GNU General Public License | 
|---|
| 28 | * along with This program; if not, write to the Free Software | 
|---|
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
| 30 | * | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | /******************************************************************************* | 
|---|
| 34 | *   Header Files                                                               * | 
|---|
| 35 | *******************************************************************************/ | 
|---|
| 36 | #include <string.h> | 
|---|
| 37 | #include <stdlib.h> | 
|---|
| 38 | #include <demangle.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "demangle.h" | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | /******************************************************************************* | 
|---|
| 44 | *   Defined Constants And Macros                                               * | 
|---|
| 45 | *******************************************************************************/ | 
|---|
| 46 | #ifndef TRUE | 
|---|
| 47 | #define TRUE 1 | 
|---|
| 48 | #define FALSE 0 | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | unsigned long _System InitDemangleID32(const char * psInitParms); | 
|---|
| 53 | unsigned long _System DemangleID32(const char * psMangledName, char * pszPrototype, unsigned long cchPrototype); | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | /** | 
|---|
| 57 | * Initiate the demangler. | 
|---|
| 58 | * @returns TRUE (success) | 
|---|
| 59 | * @param   psInitParms     Initiations parameter string. | 
|---|
| 60 | */ | 
|---|
| 61 | unsigned long _System InitDemangleID32(const char * psInitParms) | 
|---|
| 62 | { | 
|---|
| 63 | return TRUE; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | /** | 
|---|
| 68 | * Demangle name pointed to by pszMangledName. | 
|---|
| 69 | * | 
|---|
| 70 | * @returns TRUE/FALSE - Success indicator. | 
|---|
| 71 | * @param   psMangledName   Symbol to demangle. Pascal string. | 
|---|
| 72 | * @param   pszPrototype    Return buffer. (zero string) | 
|---|
| 73 | * @param   cchPrototype    Size of the pszPrototype buffer. | 
|---|
| 74 | */ | 
|---|
| 75 | unsigned long _System DemangleID32(const char * psMangledName, char * pszPrototype, unsigned long cchPrototype) | 
|---|
| 76 | { | 
|---|
| 77 | char *  pszWeak; | 
|---|
| 78 | char *  pszProto; | 
|---|
| 79 | char *  pszMang; | 
|---|
| 80 | char *  pszMangFree; | 
|---|
| 81 | char *  psz; | 
|---|
| 82 | int     cch; | 
|---|
| 83 |  | 
|---|
| 84 | /* | 
|---|
| 85 | * Make zero terminated string output of the pascal string. | 
|---|
| 86 | */ | 
|---|
| 87 | pszMang = malloc((unsigned char)*psMangledName + 1); | 
|---|
| 88 | if (!pszMang) | 
|---|
| 89 | return FALSE; | 
|---|
| 90 | memcpy(pszMang, psMangledName + 1, (unsigned char)*psMangledName); | 
|---|
| 91 | pszMang[(unsigned char)*psMangledName] = '\0'; | 
|---|
| 92 |  | 
|---|
| 93 | /* | 
|---|
| 94 | * Skip any leading underscore. | 
|---|
| 95 | */ | 
|---|
| 96 | pszMangFree = pszMang; | 
|---|
| 97 | if (*pszMang == '_') | 
|---|
| 98 | pszMang++; | 
|---|
| 99 |  | 
|---|
| 100 | /* | 
|---|
| 101 | * Check if weak, and if so we must | 
|---|
| 102 | */ | 
|---|
| 103 | pszWeak = strstr(pszMang, "$w$"); | 
|---|
| 104 | if (pszWeak) | 
|---|
| 105 | *pszWeak = '\0'; | 
|---|
| 106 |  | 
|---|
| 107 | /* | 
|---|
| 108 | * Call the demangler. | 
|---|
| 109 | * On failure return mangled name. | 
|---|
| 110 | */ | 
|---|
| 111 | psz = pszProto = cplus_demangle(pszMang, DMGL_PARAMS | DMGL_ANSI); | 
|---|
| 112 | if (!psz) | 
|---|
| 113 | psz = pszMangFree; | 
|---|
| 114 |  | 
|---|
| 115 | /* | 
|---|
| 116 | * Copy to result buffer. | 
|---|
| 117 | */ | 
|---|
| 118 | cch = strlen(psz); | 
|---|
| 119 | if (cch >= cchPrototype) | 
|---|
| 120 | { | 
|---|
| 121 | memcpy(pszPrototype, psz, cchPrototype - 1); | 
|---|
| 122 | pszPrototype[cchPrototype - 1] = '\0'; | 
|---|
| 123 | } | 
|---|
| 124 | else | 
|---|
| 125 | { | 
|---|
| 126 | memcpy(pszPrototype, psz, cch + 1); | 
|---|
| 127 | if (pszWeak && cch + 6 < cchPrototype) | 
|---|
| 128 | strcpy(pszPrototype + cch, " weak"); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /* | 
|---|
| 132 | * Cleanup. | 
|---|
| 133 | */ | 
|---|
| 134 | free(pszProto); | 
|---|
| 135 | free(pszMangFree); | 
|---|
| 136 |  | 
|---|
| 137 | return 1; /* we never fail */ | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|