xref: /AOO41X/main/soltools/ldump/ldump.cxx (revision 7a4715d38e2b2b0b3018baee48d4fff8c3b378ce)
1*7a4715d3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*7a4715d3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*7a4715d3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*7a4715d3SAndrew Rist  * distributed with this work for additional information
6*7a4715d3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*7a4715d3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*7a4715d3SAndrew Rist  * "License"); you may not use this file except in compliance
9*7a4715d3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*7a4715d3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*7a4715d3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*7a4715d3SAndrew Rist  * software distributed under the License is distributed on an
15*7a4715d3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7a4715d3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*7a4715d3SAndrew Rist  * specific language governing permissions and limitations
18*7a4715d3SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*7a4715d3SAndrew Rist  *************************************************************/
21*7a4715d3SAndrew Rist 
22*7a4715d3SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_soltools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <direct.h>
29cdf0e10cSrcweir #include <stdio.h>
30cdf0e10cSrcweir #include <stdlib.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "ldump.hxx"
33cdf0e10cSrcweir #include "hashtbl.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #define MAXSYM     65536
36cdf0e10cSrcweir #define MAXBASE    98304
37cdf0e10cSrcweir #define MAX_MAN		4096
38cdf0e10cSrcweir 
39cdf0e10cSrcweir int	bFilter = 0;
40cdf0e10cSrcweir int bLdump3 = 0;
41cdf0e10cSrcweir int bUseDirectives = 0;
42cdf0e10cSrcweir int bVerbose = 0;
43cdf0e10cSrcweir int bExportByName = 0;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class ExportSet : public HashTable
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
ExportSet(unsigned long lSize,double dMaxLoadFactor=HashTable::m_defMaxLoadFactor,double dGrowFactor=HashTable::m_defDefGrowFactor)48cdf0e10cSrcweir     ExportSet
49cdf0e10cSrcweir     (
50cdf0e10cSrcweir         unsigned long lSize,
51cdf0e10cSrcweir         double	dMaxLoadFactor = HashTable::m_defMaxLoadFactor,
52cdf0e10cSrcweir         double	dGrowFactor = HashTable::m_defDefGrowFactor
53cdf0e10cSrcweir     )
54cdf0e10cSrcweir         : HashTable(lSize,false,dMaxLoadFactor,dGrowFactor) {}
55cdf0e10cSrcweir 
~ExportSet()56cdf0e10cSrcweir     virtual ~ExportSet() {}
57cdf0e10cSrcweir 
Find(char * const & Key) const58cdf0e10cSrcweir     LibExport *  Find (char * const& Key) const
59cdf0e10cSrcweir     { return (LibExport *) HashTable::Find((char *) Key); }
60cdf0e10cSrcweir 
Insert(char * const & Key,LibExport * Object)61cdf0e10cSrcweir     bool Insert (char * const& Key, LibExport * Object)
62cdf0e10cSrcweir     { return HashTable::Insert((char *) Key, (void*) Object); }
63cdf0e10cSrcweir 
Delete(char * const & Key)64cdf0e10cSrcweir     LibExport *  Delete (char * const&Key)
65cdf0e10cSrcweir     { return (LibExport *) HashTable::Delete ((char *) Key); }
66cdf0e10cSrcweir };
67cdf0e10cSrcweir 
LibDump(char * cFileName,int bExportByName)68cdf0e10cSrcweir LibDump::LibDump( char *cFileName, int bExportByName )
69cdf0e10cSrcweir 				: cBName( NULL ),
70cdf0e10cSrcweir 				cAPrefix( NULL ),
71cdf0e10cSrcweir 				cLibName( NULL ),
72cdf0e10cSrcweir 				cFilterName( NULL ),
73cdf0e10cSrcweir 				cModName( NULL )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	fprintf( stderr, "LIB-NT File Dumper v4.00 (C) 2000 Sun Microsystems, Inc.\n\n" );
76cdf0e10cSrcweir 	fprintf( stderr, "%s ", cFileName );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     bExportName = bExportByName;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	unsigned long nSlots =  0xfffff;
81cdf0e10cSrcweir 	pBaseTab = new ExportSet( nSlots );
82cdf0e10cSrcweir 	pIndexTab = new ExportSet( nSlots );
83cdf0e10cSrcweir 	pFilterLines = new char * [MAXFILT];
84cdf0e10cSrcweir 	CheckLibrary(cFileName);
85cdf0e10cSrcweir 	bBase = 0;
86cdf0e10cSrcweir 	bAll = false;
87cdf0e10cSrcweir 	nDefStart = 0;
88cdf0e10cSrcweir 	nBaseLines = 0;
89cdf0e10cSrcweir 	nFilterLines = 0;
90cdf0e10cSrcweir 	bDef = true;
91cdf0e10cSrcweir 	cAPrefix = new char[ 1 ];
92cdf0e10cSrcweir 	cAPrefix[ 0 ] = 0;
93cdf0e10cSrcweir     if (!bExportName)
94cdf0e10cSrcweir 	    CheckDataBase();
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
Dump()97cdf0e10cSrcweir bool LibDump::Dump()
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	FILE *pList;
100cdf0e10cSrcweir     char  aBuf[MAX_MAN];
101cdf0e10cSrcweir     int   nLen;
102cdf0e10cSrcweir     char  aName[MAX_MAN];
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     pList = fopen( cLibName, "rb");
105cdf0e10cSrcweir 	if (!pList)
106cdf0e10cSrcweir 		DumpError(10);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // forget about offset when working on linker directives
109cdf0e10cSrcweir     if ( !bUseDirectives )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         // calculating offset for name section
112cdf0e10cSrcweir     	unsigned char TmpBuffer[4];
113cdf0e10cSrcweir 	    fread( TmpBuffer, 1, 4, pList);
114cdf0e10cSrcweir     	// anzahl bigendian mal laenge + ueberspringen der naechsten laengenangabe
115cdf0e10cSrcweir     	unsigned long nOffSet = (unsigned long) ( TmpBuffer[2] * 256 + TmpBuffer[3] ) * 4 + 4;
116cdf0e10cSrcweir     	fseek( pList, (long) nOffSet, 0);
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	char aTmpBuf[4096];
120cdf0e10cSrcweir 	// reading file containing symbols
121cdf0e10cSrcweir     while( !feof( pList ) )
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir    		int i = 0;
124cdf0e10cSrcweir         if ( !bUseDirectives )
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir 	    	// symbol komplett einlesen
127cdf0e10cSrcweir             for (;;)
128cdf0e10cSrcweir             {
129cdf0e10cSrcweir                 int c = fgetc( pList );
130cdf0e10cSrcweir                 if ( c == '\0' )
131cdf0e10cSrcweir                 {
132cdf0e10cSrcweir                     break;
133cdf0e10cSrcweir                 }
134cdf0e10cSrcweir                 if ( ((c >= 33) && (c <= 126)) && ( c!=40 && c!=41) )
135cdf0e10cSrcweir                 	aBuf[i] = static_cast< char >(c);
136cdf0e10cSrcweir                 else
137cdf0e10cSrcweir                 {
138cdf0e10cSrcweir                     aBuf[0] = '\0';
139cdf0e10cSrcweir                     break;
140cdf0e10cSrcweir                 }
141cdf0e10cSrcweir             	i++;
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir     		// Namen found
144cdf0e10cSrcweir             aBuf[i] = '\0';
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         else
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             fgets( aTmpBuf, 4096, pList );
149cdf0e10cSrcweir             char * pEnd = 0;
150cdf0e10cSrcweir             char *pFound = 0;
151cdf0e10cSrcweir             aBuf[0] = '\0';
152cdf0e10cSrcweir             pFound = strchr( aTmpBuf, 'E' );
153cdf0e10cSrcweir             while ( pFound )
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 if ( strncmp( "EXPORT:", pFound, 7) == 0 )
156cdf0e10cSrcweir                 {
157cdf0e10cSrcweir                     pFound += 7;
158cdf0e10cSrcweir                     pEnd = strchr( pFound, ',');
159cdf0e10cSrcweir                     if ( pEnd )
160cdf0e10cSrcweir                         *pEnd = '\0';
161cdf0e10cSrcweir                     strncpy( aBuf, pFound, strlen( pFound));
162cdf0e10cSrcweir                     aBuf[ strlen( pFound) ] = '\0';
163cdf0e10cSrcweir //                    fprintf( stderr, "\n--- %s\n", aBuf);
164cdf0e10cSrcweir                     break;
165cdf0e10cSrcweir                 }
166cdf0e10cSrcweir                 else
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     pFound++;
169cdf0e10cSrcweir                     pFound = strchr( pFound, 'E' );
170cdf0e10cSrcweir                 }
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	    if ((aBuf[0] =='?') || !strncmp(aBuf, "__CT",4))
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			nLen = (int) strlen(aBuf);
177cdf0e10cSrcweir             memset( aName, 0, sizeof( aName ) );
178cdf0e10cSrcweir             int nName = 0;
179cdf0e10cSrcweir             for( i = 0; i < nLen; i++ )
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir                 if ( (aBuf[i] != '\n') && (aBuf[i] != '\r') )
182cdf0e10cSrcweir                 {
183cdf0e10cSrcweir                     aName[nName] = aBuf[i];
184cdf0e10cSrcweir                     nName++;
185cdf0e10cSrcweir                 }
186cdf0e10cSrcweir             }
187cdf0e10cSrcweir 			// und raus damit
188cdf0e10cSrcweir             PrintSym( aName, bExportByName );
189cdf0e10cSrcweir 		}
190cdf0e10cSrcweir 		else if ( bAll == true )
191cdf0e10cSrcweir 		{
192cdf0e10cSrcweir 			int nPreLen = (int) strlen( cAPrefix );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir             nLen = (int) strlen(aBuf);
195cdf0e10cSrcweir             memset( aName, 0, sizeof( aName ) );
196cdf0e10cSrcweir             int nName = 0;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             for( i = 0; i < nLen; i++ )
199cdf0e10cSrcweir             {
200cdf0e10cSrcweir               if ( (aBuf[i] != '\n') && (aBuf[i] != '\r') )
201cdf0e10cSrcweir               {
202cdf0e10cSrcweir                   aName[nName] = aBuf[i];
203cdf0e10cSrcweir                   nName++;
204cdf0e10cSrcweir 			  }
205cdf0e10cSrcweir 			}
206cdf0e10cSrcweir 			//fprintf( stderr, "Gefundenen Prefix : %s %d \n", aTmpBuf, nPreLen );
207cdf0e10cSrcweir 			// den ersten _ raus
208cdf0e10cSrcweir             nLen = (int) strlen(aName);
209cdf0e10cSrcweir             if (aName[0] == '_')
210cdf0e10cSrcweir 				strcpy( aBuf , &aName[1] );
211cdf0e10cSrcweir 			strncpy ( aTmpBuf, aBuf, (size_t) nPreLen );
212cdf0e10cSrcweir 			aTmpBuf[nPreLen] = '\0';
213cdf0e10cSrcweir 			if ( !strcmp( aTmpBuf, cAPrefix ))
214cdf0e10cSrcweir 			{
215cdf0e10cSrcweir 				if ( bLdump3 ) {
216cdf0e10cSrcweir 					int nChar = '@';
217cdf0e10cSrcweir 					char *pNeu = strchr( aBuf, nChar );
218cdf0e10cSrcweir 					int nPos = pNeu - aBuf + 1;
219cdf0e10cSrcweir 					if ( nPos > 0 )
220cdf0e10cSrcweir 					{
221cdf0e10cSrcweir 						char aOldBuf[MAX_MAN];
222cdf0e10cSrcweir 						strcpy( aOldBuf, aBuf );
223cdf0e10cSrcweir 						char pChar[MAX_MAN];
224cdf0e10cSrcweir 						strncpy( pChar, aBuf, (size_t) (nPos -1) );
225cdf0e10cSrcweir 						pChar[nPos-1] = '\0';
226cdf0e10cSrcweir 						strcpy( aBuf, pChar );
227cdf0e10cSrcweir 						strcat( aBuf, "=" );
228cdf0e10cSrcweir 						strcat( aBuf, aOldBuf );
229cdf0e10cSrcweir 						strcpy( pChar, "" );
230cdf0e10cSrcweir 					}
231cdf0e10cSrcweir 				}
232cdf0e10cSrcweir 				// und raus damit
233cdf0e10cSrcweir             	PrintSym( aBuf, true );
234cdf0e10cSrcweir 			}
235cdf0e10cSrcweir 		}
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 	fclose(pList);
238cdf0e10cSrcweir 	return true;
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
ReadFilter(char * cFilterName)241cdf0e10cSrcweir bool LibDump::ReadFilter( char * cFilterName )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir 	FILE* pfFilter = 0;
244cdf0e10cSrcweir     char  aBuf[MAX_MAN];
245cdf0e10cSrcweir     char* pStr;
246cdf0e10cSrcweir     int   nLen;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     pfFilter = fopen( cFilterName, "r" );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     if ( !pfFilter )
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir         ::bFilter = 0;
253cdf0e10cSrcweir         DumpError( 500 );
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     while( fgets( aBuf, MAX_MAN, pfFilter ) != 0 )
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         nLen = (int) strlen(aBuf);
259cdf0e10cSrcweir         pStr = new char[(unsigned int) nLen];
260cdf0e10cSrcweir         if ( !pStr )
261cdf0e10cSrcweir             DumpError( 98 );
262cdf0e10cSrcweir         memcpy( pStr, aBuf, (unsigned int) nLen );
263cdf0e10cSrcweir         if ( *(pStr+nLen-1) == '\n' )
264cdf0e10cSrcweir             *(pStr+nLen-1) = '\0';
265cdf0e10cSrcweir 		pFilterLines[nFilterLines] = pStr;
266cdf0e10cSrcweir 		nFilterLines++;
267cdf0e10cSrcweir 		if ( nFilterLines >= MAXFILT )
268cdf0e10cSrcweir 				DumpError( 510 );
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 	fclose( pfFilter );
272cdf0e10cSrcweir 	return true;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
PrintSym(char * pName,bool bName)275cdf0e10cSrcweir bool LibDump::PrintSym(char *pName, bool bName )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	LibExport *pData;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     // Filter auswerten
281cdf0e10cSrcweir     if ( Filter( pName ) )
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         if ( strlen( pName ) > 3 )
284cdf0e10cSrcweir         {
285cdf0e10cSrcweir             if ( bDef )
286cdf0e10cSrcweir 		    {
287cdf0e10cSrcweir 		        if (!bBase)
288cdf0e10cSrcweir                     if (bExportName) {
289cdf0e10cSrcweir 						fprintf( stdout, "\t%s\n", pName );
290cdf0e10cSrcweir                     } else {
291cdf0e10cSrcweir 						fprintf( stdout, "\t%s\t\t@%lu\n", pName, nDefStart );
292cdf0e10cSrcweir                     }
293cdf0e10cSrcweir 				else
294cdf0e10cSrcweir 				{
295cdf0e10cSrcweir 					 pData = pBaseTab->Find( pName );
296cdf0e10cSrcweir 					 if ( pData )
297cdf0e10cSrcweir 					 {
298cdf0e10cSrcweir 						pData->bExport = true;
299cdf0e10cSrcweir 						if ( bName )
300cdf0e10cSrcweir 							pData->bByName = true;
301cdf0e10cSrcweir 						else
302cdf0e10cSrcweir 							pData->bByName = false;
303cdf0e10cSrcweir 						if ( bVerbose )
304cdf0e10cSrcweir 							fprintf(stderr,".");
305cdf0e10cSrcweir 					 }
306cdf0e10cSrcweir 					 else
307cdf0e10cSrcweir 					 {
308cdf0e10cSrcweir 					 	// neuen Export eintragen
309cdf0e10cSrcweir 					 	pData = new LibExport;
310cdf0e10cSrcweir 						pData->cExportName = new char[ strlen( pName ) + 1 ];
311cdf0e10cSrcweir 						strcpy( pData->cExportName, pName );
312cdf0e10cSrcweir 						pData->nOrdinal = nBaseLines++;
313cdf0e10cSrcweir 						pData->bExport = true;
314cdf0e10cSrcweir 						if ( bName )
315cdf0e10cSrcweir 							pData->bByName = true;
316cdf0e10cSrcweir 						else
317cdf0e10cSrcweir 							pData->bByName = false;
318cdf0e10cSrcweir 						pBaseTab->Insert( pData->cExportName, pData );
319cdf0e10cSrcweir 						char *cBuffer = new char[ 30 ];
320cdf0e10cSrcweir 						sprintf( cBuffer, "%lu", pData->nOrdinal );
321cdf0e10cSrcweir 						pIndexTab->Insert( cBuffer, pData );
322cdf0e10cSrcweir 						delete [] cBuffer;
323cdf0e10cSrcweir 						if ( bVerbose )
324cdf0e10cSrcweir 							fprintf(stderr,"n");
325cdf0e10cSrcweir 					 }
326cdf0e10cSrcweir 				}
327cdf0e10cSrcweir 		    }
328cdf0e10cSrcweir             else
329cdf0e10cSrcweir                 printf( "%s\n", pName );
330cdf0e10cSrcweir             nDefStart++;
331cdf0e10cSrcweir         }
332cdf0e10cSrcweir     }
333cdf0e10cSrcweir 	return true;
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
IsFromAnonymousNamespace(char * pExportName)336cdf0e10cSrcweir bool LibDump::IsFromAnonymousNamespace (char *pExportName) {
337cdf0e10cSrcweir     char* pattern1 = "@?A0x";
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     if (strstr(pExportName, pattern1)) {
340cdf0e10cSrcweir         return true;
341cdf0e10cSrcweir     };
342cdf0e10cSrcweir     return false;
343cdf0e10cSrcweir };
344cdf0e10cSrcweir 
Filter(char * pExportName)345cdf0e10cSrcweir bool LibDump::Filter(char *pExportName)
346cdf0e10cSrcweir {
347cdf0e10cSrcweir 	unsigned long i;
348cdf0e10cSrcweir 	char pTest[256];
349cdf0e10cSrcweir 
350cdf0e10cSrcweir     // filter out symbols from anonymous namespaces
351cdf0e10cSrcweir     if (IsFromAnonymousNamespace (pExportName))
352cdf0e10cSrcweir         return false;
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     // Kein Filter gesetzt
355cdf0e10cSrcweir     if ( ::bFilter == 0 )
356cdf0e10cSrcweir         return true;
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 	for ( i=0; i<nFilterLines; i++ )
359cdf0e10cSrcweir 	{
360cdf0e10cSrcweir 		//Zum vergleichen mu� das Plus abgeschnitteb werden
361cdf0e10cSrcweir 		if(pFilterLines[i][0] != '+')
362cdf0e10cSrcweir 		{
363cdf0e10cSrcweir 			if ( strstr( pExportName, pFilterLines[i]))
364cdf0e10cSrcweir 				return false;
365cdf0e10cSrcweir 		}
366cdf0e10cSrcweir 		else
367cdf0e10cSrcweir 		{
368cdf0e10cSrcweir 			strcpy(pTest,&pFilterLines[i][1]);
369cdf0e10cSrcweir 			if ( strstr( pExportName, pTest))
370cdf0e10cSrcweir 				return true;
371cdf0e10cSrcweir 		}
372cdf0e10cSrcweir 	}
373cdf0e10cSrcweir     return true;
374cdf0e10cSrcweir }
375cdf0e10cSrcweir 
SetFilter(char * cFilterName)376cdf0e10cSrcweir bool LibDump::SetFilter(char * cFilterName)
377cdf0e10cSrcweir {
378cdf0e10cSrcweir 	ReadFilter( cFilterName );
379cdf0e10cSrcweir 	return true;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir 
CheckLibrary(char * cName)382cdf0e10cSrcweir bool LibDump::CheckLibrary(char * cName)
383cdf0e10cSrcweir {
384cdf0e10cSrcweir 	delete [] cLibName;
385cdf0e10cSrcweir 	cLibName = new char[ strlen( cName ) + 1 ];
386cdf0e10cSrcweir 	strcpy( cLibName, cName );
387cdf0e10cSrcweir 	return true;
388cdf0e10cSrcweir }
389cdf0e10cSrcweir 
ReadDataBase()390cdf0e10cSrcweir bool LibDump::ReadDataBase()
391cdf0e10cSrcweir {
392cdf0e10cSrcweir 	FILE* pfBase = 0;
393cdf0e10cSrcweir     char  aBuf[MAX_MAN];
394cdf0e10cSrcweir     char* pStr;
395cdf0e10cSrcweir     char  cBuffer[ 30 ];
396cdf0e10cSrcweir     int   nLen;
397cdf0e10cSrcweir 	LibExport *pData;
398cdf0e10cSrcweir 
399cdf0e10cSrcweir     pfBase = fopen( cBName, "r" );
400cdf0e10cSrcweir 
401cdf0e10cSrcweir     if ( !pfBase )
402cdf0e10cSrcweir     {
403cdf0e10cSrcweir         bBase = 0;
404cdf0e10cSrcweir         DumpError( 600 );
405cdf0e10cSrcweir     }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 	bool bRet = true;
408cdf0e10cSrcweir 	while( fgets( aBuf, MAX_MAN, pfBase ) != 0 )
409cdf0e10cSrcweir     {
410cdf0e10cSrcweir         nLen = (int) strlen(aBuf);
411cdf0e10cSrcweir         pStr = new char[(unsigned int) nLen];
412cdf0e10cSrcweir         if ( !pStr )
413cdf0e10cSrcweir             DumpError( 98 );
414cdf0e10cSrcweir         memcpy( pStr, aBuf, (size_t) nLen );
415cdf0e10cSrcweir         if ( *(pStr+nLen-1) == '\n' )
416cdf0e10cSrcweir             *(pStr+nLen-1) = '\0';
417cdf0e10cSrcweir 		pData = new LibExport;
418cdf0e10cSrcweir 		pData->cExportName = pStr;
419cdf0e10cSrcweir 		pData->nOrdinal = nBaseLines;
420cdf0e10cSrcweir 		pData->bExport=false;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 		if (pBaseTab->Insert(pData->cExportName, pData ) == NULL)
423cdf0e10cSrcweir 			bRet = false;
424cdf0e10cSrcweir         ltoa( (long) pData->nOrdinal, cBuffer, 10 );
425cdf0e10cSrcweir 		if (pIndexTab->Insert( cBuffer, pData ) == NULL)
426cdf0e10cSrcweir 			bRet = false;
427cdf0e10cSrcweir         nBaseLines++;
428cdf0e10cSrcweir         if ( nBaseLines >= MAXBASE )
429cdf0e10cSrcweir             DumpError( 610 );
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir     fclose( pfBase );
432cdf0e10cSrcweir 	return bRet;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir class ExportSetIter : public HashTableIterator
436cdf0e10cSrcweir {
437cdf0e10cSrcweir public:
ExportSetIter(HashTable const & aTable)438cdf0e10cSrcweir     ExportSetIter(HashTable const& aTable)
439cdf0e10cSrcweir 		: HashTableIterator(aTable)	{}
440cdf0e10cSrcweir 
GetFirst()441cdf0e10cSrcweir     LibExport * GetFirst()
442cdf0e10cSrcweir     { return (LibExport *)HashTableIterator::GetFirst(); }
GetNext()443cdf0e10cSrcweir     LibExport * GetNext()
444cdf0e10cSrcweir     { return (LibExport *)HashTableIterator::GetNext();  }
GetLast()445cdf0e10cSrcweir     LibExport * GetLast()
446cdf0e10cSrcweir     { return (LibExport *)HashTableIterator::GetLast();  }
GetPrev()447cdf0e10cSrcweir     LibExport * GetPrev()
448cdf0e10cSrcweir     { return (LibExport *)HashTableIterator::GetPrev();  }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir private:
451cdf0e10cSrcweir     void operator =(ExportSetIter &); // not defined
452cdf0e10cSrcweir };
453cdf0e10cSrcweir 
PrintDataBase()454cdf0e10cSrcweir bool LibDump::PrintDataBase()
455cdf0e10cSrcweir {
456cdf0e10cSrcweir     if (bExportName)
457cdf0e10cSrcweir         return true;
458cdf0e10cSrcweir 	FILE *pFp;
459cdf0e10cSrcweir 	pFp = fopen (cBName,"w+");
460cdf0e10cSrcweir 	if (!pFp)
461cdf0e10cSrcweir 		fprintf( stderr, "Error opening DataBase File\n" );
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 	LibExport *pData;
464cdf0e10cSrcweir 	for ( unsigned long i=0; i < nBaseLines+10; i++ )
465cdf0e10cSrcweir 	{
466cdf0e10cSrcweir 		char * cBuffer = new char[ 30 ];
467cdf0e10cSrcweir 		sprintf( cBuffer, "%lu", i );
468cdf0e10cSrcweir 		pData = pIndexTab->Find( cBuffer );
469cdf0e10cSrcweir 		delete [] cBuffer;
470cdf0e10cSrcweir 		if ( pData )
471cdf0e10cSrcweir 			fprintf(pFp,"%s\n",pData->cExportName);
472cdf0e10cSrcweir 	}
473cdf0e10cSrcweir 	fclose(pFp);
474cdf0e10cSrcweir 	return true;
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
PrintDefFile()477cdf0e10cSrcweir bool LibDump::PrintDefFile()
478cdf0e10cSrcweir {
479cdf0e10cSrcweir #ifdef FAST
480cdf0e10cSrcweir 	ExportSetIter aIterator( *pBaseTab );
481cdf0e10cSrcweir 	for ( LibExport *pData = aIterator.GetFirst(); pData != NULL;
482cdf0e10cSrcweir 										pData = aIterator.GetNext() )
483cdf0e10cSrcweir 	{
484cdf0e10cSrcweir 		if ( pData->bExport )
485cdf0e10cSrcweir 		{
486cdf0e10cSrcweir 			if ( pData->bByName )
487cdf0e10cSrcweir 			{
488cdf0e10cSrcweir 				fprintf(stdout,"\t%s\n",
489cdf0e10cSrcweir 					pData->sExportName.GetBuffer());
490cdf0e10cSrcweir 			}
491cdf0e10cSrcweir 			else
492cdf0e10cSrcweir 			{
493cdf0e10cSrcweir 				fprintf(stdout,"\t%s\t\t@%d NONAME\n",
494cdf0e10cSrcweir 					pData->sExportName.GetBuffer(), pData->nOrdinal+nBegin);
495cdf0e10cSrcweir 			}
496cdf0e10cSrcweir 		}
497cdf0e10cSrcweir 	}
498cdf0e10cSrcweir #else
499cdf0e10cSrcweir 	// sortiert nach Ordinals;
500cdf0e10cSrcweir 	LibExport *pData;
501cdf0e10cSrcweir 	for ( unsigned long i=0; i<nBaseLines+1; i++)
502cdf0e10cSrcweir 	{
503cdf0e10cSrcweir 		char * cBuffer = new char[ 30 ];
504cdf0e10cSrcweir 		sprintf( cBuffer, "%lu", i );
505cdf0e10cSrcweir 		pData = pIndexTab->Find( cBuffer );
506cdf0e10cSrcweir 		delete [] cBuffer;
507cdf0e10cSrcweir 		if ( pData )
508cdf0e10cSrcweir 			if ( pData->bExport )
509cdf0e10cSrcweir 			{
510cdf0e10cSrcweir 				if ( pData->bByName )
511cdf0e10cSrcweir 				{
512cdf0e10cSrcweir 					if ( strlen( pData->cExportName ))
513cdf0e10cSrcweir 						fprintf(stdout,"\t%s\n",
514cdf0e10cSrcweir 							pData->cExportName);
515cdf0e10cSrcweir 				}
516cdf0e10cSrcweir 				else
517cdf0e10cSrcweir 				{
518cdf0e10cSrcweir 					if ( strlen( pData->cExportName ))
519cdf0e10cSrcweir 						fprintf(stdout,"\t%s\t\t@%d NONAME\n",
520cdf0e10cSrcweir 							pData->cExportName, pData->nOrdinal+nBegin);
521cdf0e10cSrcweir 				}
522cdf0e10cSrcweir 			}
523cdf0e10cSrcweir 	}
524cdf0e10cSrcweir #endif
525cdf0e10cSrcweir 	return true;
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
CheckDataBase()528cdf0e10cSrcweir bool LibDump::CheckDataBase()
529cdf0e10cSrcweir {
530cdf0e10cSrcweir     // existiert eine Datenbasis ?
531cdf0e10cSrcweir     if (!bBase)
532cdf0e10cSrcweir     {
533cdf0e10cSrcweir 		cBName = new char[ 2048 ];
534cdf0e10cSrcweir 		char *pTmp = "defs\\";
535cdf0e10cSrcweir 
536cdf0e10cSrcweir     	FILE *fp;
537cdf0e10cSrcweir #ifdef OS2
538cdf0e10cSrcweir 		_mkdir ("defs", 0777);
539cdf0e10cSrcweir #else
540cdf0e10cSrcweir 		_mkdir ("defs");
541cdf0e10cSrcweir #endif
542cdf0e10cSrcweir 		strcpy(cBName,pTmp);
543cdf0e10cSrcweir #ifdef OS2
544cdf0e10cSrcweir 		strcat(cBName,"gcc");
545cdf0e10cSrcweir #else
546cdf0e10cSrcweir 		strcat(cBName,getenv ("COMP_ENV"));
547cdf0e10cSrcweir #endif
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		fp = fopen (cBName,"r");
550cdf0e10cSrcweir 		if (fp)
551cdf0e10cSrcweir 		{
552cdf0e10cSrcweir 			bBase = true;
553cdf0e10cSrcweir 		}
554cdf0e10cSrcweir 		else
555cdf0e10cSrcweir 		{
556cdf0e10cSrcweir 			fp = fopen (cBName,"w+");
557cdf0e10cSrcweir 			bBase = true;
558cdf0e10cSrcweir 		}
559cdf0e10cSrcweir 		fclose (fp);
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir     // lese Datenbasis !
562cdf0e10cSrcweir     if (bBase)
563cdf0e10cSrcweir 	{
564cdf0e10cSrcweir     	ReadDataBase();
565cdf0e10cSrcweir 	}
566cdf0e10cSrcweir 	return true;
567cdf0e10cSrcweir }
568cdf0e10cSrcweir 
~LibDump()569cdf0e10cSrcweir LibDump::~LibDump()
570cdf0e10cSrcweir {
571cdf0e10cSrcweir 	delete [] cBName;
572cdf0e10cSrcweir 	delete [] cAPrefix;
573cdf0e10cSrcweir //	delete [] cLibName;
574cdf0e10cSrcweir 	delete [] cFilterName;
575cdf0e10cSrcweir 	delete [] cModName;
576cdf0e10cSrcweir }
577cdf0e10cSrcweir 
SetCExport(char * pName)578cdf0e10cSrcweir void LibDump::SetCExport( char* pName )
579cdf0e10cSrcweir {
580cdf0e10cSrcweir 	delete [] cAPrefix;
581cdf0e10cSrcweir 	cAPrefix = new char[ strlen( pName ) + 1 ];
582cdf0e10cSrcweir 	strcpy( cAPrefix, pName );bAll = true;
583cdf0e10cSrcweir }
584cdf0e10cSrcweir 
585cdf0e10cSrcweir //******************************************************************
586cdf0e10cSrcweir //* Error() - Gibt Fehlermeldumg aus
587cdf0e10cSrcweir //******************************************************************
588cdf0e10cSrcweir 
DumpError(unsigned long n)589cdf0e10cSrcweir void LibDump::DumpError( unsigned long n )
590cdf0e10cSrcweir {
591cdf0e10cSrcweir     char *p;
592cdf0e10cSrcweir 
593cdf0e10cSrcweir     switch (n)
594cdf0e10cSrcweir     {
595cdf0e10cSrcweir         case 1:  p = "Input error in library file"; break;
596cdf0e10cSrcweir         case 2:  p = "Position error in library file (no THEADR set)"; break;
597cdf0e10cSrcweir         case 3:  p = "Overflow of symbol table"; break;
598cdf0e10cSrcweir #ifdef WNT
599cdf0e10cSrcweir         case 10: p = "EXP file not found"; break;
600cdf0e10cSrcweir         case 11: p = "No valid EXP file"; break;
601cdf0e10cSrcweir #else
602cdf0e10cSrcweir         case 10: p = "Library file not found"; break;
603cdf0e10cSrcweir         case 11: p = "No valid library file"; break;
604cdf0e10cSrcweir #endif
605cdf0e10cSrcweir         case 98: p = "Out of memory"; break;
606cdf0e10cSrcweir         case 99: p = "LDUMP [-LD3] [-D] [-N] [-A] [-E nn] [-F name] Filename[.LIB]\n"
607cdf0e10cSrcweir 					 "-LD3   : Supports feature set of ldump3 (default: ldump/ldump2)\n"
608cdf0e10cSrcweir                      "-A     : all symbols (default: only C++)\n"
609cdf0e10cSrcweir                      "-E nn  : gerenration of export table beginning with number nn\n"
610cdf0e10cSrcweir                      "-F name: Filter file\n"
611cdf0e10cSrcweir                      "-D     : file contains \"dumpbin\" directives\n"
612cdf0e10cSrcweir                      "-N     : export by name\n"
613cdf0e10cSrcweir                      "-V     : be verbose\n"; break;
614cdf0e10cSrcweir         case 500: p = "Unable to open filter file\n"; break;
615cdf0e10cSrcweir         case 510: p = "Overflow of filter table\n"; break;
616cdf0e10cSrcweir         case 600: p = "Unable to open base database file\n"; break;
617cdf0e10cSrcweir         case 610: p = "Overflow in base database table\n"; break;
618cdf0e10cSrcweir         default: p = "Unspecified error";
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir 	fprintf( stdout, "%s\n", p );
621cdf0e10cSrcweir     exit (1);
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir /*********************************************************************
625cdf0e10cSrcweir 		Test Funktionen
626cdf0e10cSrcweir *********************************************************************/
627cdf0e10cSrcweir 
628cdf0e10cSrcweir 
usage()629cdf0e10cSrcweir void usage()
630cdf0e10cSrcweir {
631cdf0e10cSrcweir 	LibDump::DumpError(99);
632cdf0e10cSrcweir }
633cdf0e10cSrcweir 
634cdf0e10cSrcweir #define STATE_NON		0x0000
635cdf0e10cSrcweir #define STATE_BEGIN		0x0001
636cdf0e10cSrcweir #define STATE_FILTER	0x0002
637cdf0e10cSrcweir #define STATE_CEXPORT	0x0003
638cdf0e10cSrcweir 
639cdf0e10cSrcweir int
640cdf0e10cSrcweir #ifdef WNT
641cdf0e10cSrcweir __cdecl
642cdf0e10cSrcweir #endif
main(int argc,char ** argv)643cdf0e10cSrcweir main( int argc, char **argv )
644cdf0e10cSrcweir {
645cdf0e10cSrcweir 	char *pLibName = NULL, *pFilterName = NULL, *pCExport= NULL;
646cdf0e10cSrcweir 	unsigned short nBegin=1;
647cdf0e10cSrcweir 
648cdf0e10cSrcweir 	unsigned short nState = STATE_NON;
649cdf0e10cSrcweir 
650cdf0e10cSrcweir 	if ( argc == 1 ) {
651cdf0e10cSrcweir 		usage();
652cdf0e10cSrcweir 	}
653cdf0e10cSrcweir 
654cdf0e10cSrcweir 	for ( int i = 1; i < argc; i++ ) {
655cdf0e10cSrcweir 		if (( !strcmp( argv[ i ], "-H" )) ||
656cdf0e10cSrcweir 			( !strcmp( argv[ i ], "-h" )) ||
657cdf0e10cSrcweir 			( !strcmp( argv[ i ], "-?" )))
658cdf0e10cSrcweir 		{
659cdf0e10cSrcweir 			usage();
660cdf0e10cSrcweir 		}
661cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-LD3" )) ||
662cdf0e10cSrcweir 			( !strcmp( argv[ i ], "-Ld3" )) ||
663cdf0e10cSrcweir 			( !strcmp( argv[ i ], "-ld3" )) ||
664cdf0e10cSrcweir 			( !strcmp( argv[ i ], "-lD3" )))
665cdf0e10cSrcweir 		{
666cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
667cdf0e10cSrcweir 				usage();
668cdf0e10cSrcweir 			}
669cdf0e10cSrcweir 			bLdump3 = 1;
670cdf0e10cSrcweir 		}
671cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-E" )) || ( !strcmp( argv[ i ], "-e" ))) {
672cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
673cdf0e10cSrcweir 				usage();
674cdf0e10cSrcweir 			}
675cdf0e10cSrcweir 			nState = STATE_BEGIN;
676cdf0e10cSrcweir 		}
677cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-F" )) || ( !strcmp( argv[ i ], "-f" ))) {
678cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
679cdf0e10cSrcweir 				usage();
680cdf0e10cSrcweir 			}
681cdf0e10cSrcweir 			nState = STATE_FILTER;
682cdf0e10cSrcweir 		}
683cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-A" )) || ( !strcmp( argv[ i ], "-a" ))) {
684cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
685cdf0e10cSrcweir 				usage();
686cdf0e10cSrcweir 			}
687cdf0e10cSrcweir 			nState = STATE_CEXPORT;
688cdf0e10cSrcweir 			pCExport = new char[ 1 ];
689cdf0e10cSrcweir 			pCExport[ 0 ] = 0;
690cdf0e10cSrcweir 		}
691cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-D" )) || ( !strcmp( argv[ i ], "-d" ))) {
692cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
693cdf0e10cSrcweir 				usage();
694cdf0e10cSrcweir 			}
695cdf0e10cSrcweir             bUseDirectives = 1;
696cdf0e10cSrcweir 		}
697cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-N" )) || ( !strcmp( argv[ i ], "-n" ))) {
698cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
699cdf0e10cSrcweir 				usage();
700cdf0e10cSrcweir 			}
701cdf0e10cSrcweir             bExportByName = 1;
702cdf0e10cSrcweir 		}
703cdf0e10cSrcweir 		else if (( !strcmp( argv[ i ], "-V" )) || ( !strcmp( argv[ i ], "-v" ))) {
704cdf0e10cSrcweir 			if ( nState != STATE_NON ) {
705cdf0e10cSrcweir 				usage();
706cdf0e10cSrcweir 			}
707cdf0e10cSrcweir             bVerbose = 1;
708cdf0e10cSrcweir 		}
709cdf0e10cSrcweir 		else {
710cdf0e10cSrcweir 			switch ( nState ) {
711cdf0e10cSrcweir 				case STATE_BEGIN:
712cdf0e10cSrcweir 					nBegin = static_cast< unsigned short >(atoi( argv[ i ] ));
713cdf0e10cSrcweir 					nState = STATE_NON;
714cdf0e10cSrcweir 				break;
715cdf0e10cSrcweir 				case STATE_FILTER:
716cdf0e10cSrcweir 					pFilterName = new char[ strlen( argv[ i ] ) + 1 ];
717cdf0e10cSrcweir 					strcpy( pFilterName, argv[ i ] );
718cdf0e10cSrcweir 					bFilter = 1;
719cdf0e10cSrcweir 					nState = STATE_NON;
720cdf0e10cSrcweir 				break;
721cdf0e10cSrcweir 				case STATE_CEXPORT:
722cdf0e10cSrcweir 					delete [] pCExport;
723cdf0e10cSrcweir 					pCExport = new char[ strlen( argv[ i ] ) + 1 ];
724cdf0e10cSrcweir 					strcpy( pCExport, argv[ i ] );
725cdf0e10cSrcweir 					nState = STATE_NON;
726cdf0e10cSrcweir 				break;
727cdf0e10cSrcweir 				default:
728cdf0e10cSrcweir 					pLibName = new char[ strlen( argv[ i ] ) + 1 ];
729cdf0e10cSrcweir 					strcpy( pLibName, argv[ i ] );
730cdf0e10cSrcweir 				break;
731cdf0e10cSrcweir 			}
732cdf0e10cSrcweir 		}
733cdf0e10cSrcweir 	}
734cdf0e10cSrcweir 
735cdf0e10cSrcweir 	if ( !pLibName ) {
736cdf0e10cSrcweir 		usage();
737cdf0e10cSrcweir 	}
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 	LibDump *pDump = new LibDump( pLibName, bExportByName );
740cdf0e10cSrcweir 	pDump->SetBeginExport(nBegin);
741cdf0e10cSrcweir 	if ( bFilter != 0 )
742cdf0e10cSrcweir 		pDump->SetFilter( pFilterName );
743cdf0e10cSrcweir 	if ( pCExport )
744cdf0e10cSrcweir 		pDump->SetCExport( pCExport );
745cdf0e10cSrcweir 	else {
746cdf0e10cSrcweir 		char *pEmpty = "";
747cdf0e10cSrcweir 		pDump->SetCExport( pEmpty );
748cdf0e10cSrcweir 	}
749cdf0e10cSrcweir 	pDump->Dump();
750cdf0e10cSrcweir 	pDump->PrintDefFile();
751cdf0e10cSrcweir 	pDump->PrintDataBase();
752cdf0e10cSrcweir 	delete pDump;
753cdf0e10cSrcweir 	return 0;
754cdf0e10cSrcweir }
755