toolsandutils/e32tools/elf2e32/source/pl_elfexports.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Implementation of the Class ElfExports for the elf2e32 tool
       
    15 // @internalComponent
       
    16 // @released
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "pl_elfexports.h"
       
    21 #include "pl_elfexecutable.h"
       
    22 #include "pl_dllsymbol.h"
       
    23 
       
    24 using std::set_difference;
       
    25 
       
    26 /**
       
    27 Constructor for class ElfExports
       
    28 @internalComponent
       
    29 @released
       
    30 */
       
    31 ElfExports::ElfExports() : iDllName(NULL), iSorted(false), iExportsFilteredP(false){
       
    32 }
       
    33 
       
    34 /**
       
    35 Destructor for class ElfExports
       
    36 @internalComponent
       
    37 @released
       
    38 */
       
    39 ElfExports::~ElfExports()
       
    40 {
       
    41 	if(iExportList.size())
       
    42 	{
       
    43 		ExportList::iterator aItr = iExportList.begin();
       
    44 		ExportList::iterator last = iExportList.end();
       
    45 		DllSymbol *temp;
       
    46 
       
    47 		while( aItr != last)
       
    48 		{
       
    49 			temp = *aItr;
       
    50 			aItr++;
       
    51 			delete temp;
       
    52 		}
       
    53 	}
       
    54 	iExportList.clear();
       
    55 
       
    56 }
       
    57 
       
    58 /**
       
    59 This function validates exported symbols. The typeinfo name strings
       
    60 are not valid export symbols and are discarded.
       
    61 @param aExecutable - Instance of class ElfExecutable
       
    62 @param aSym	- DllSymbol
       
    63 @return True if symbol is valid, otherwise false
       
    64 @internalComponent
       
    65 @released
       
    66 */
       
    67 bool ElfExports::ValidExportP(ElfExecutable * aExecutable, DllSymbol * aSym)
       
    68 {
       
    69 	char * aSymName = aExecutable->GetSymbolName(aSym->iSymbolIndex);
       
    70 	int result = strncmp(aSymName, "_ZTS", strlen("_ZTS"));
       
    71 	return ( result != 0);
       
    72 }
       
    73 
       
    74 /**
       
    75 This function adds export symbols into exports list.
       
    76 @param aDll - Dll name
       
    77 @param aExecutable - Instance of class ElfExecutable
       
    78 @param aSym - Dll symbol
       
    79 @return Dll Symbol if its valid, otherwise NULL
       
    80 @internalComponent
       
    81 @released
       
    82 */
       
    83 DllSymbol* ElfExports::Add(char *aDll, ElfExecutable * aExecutable, DllSymbol *aSym)
       
    84 {
       
    85 	if (ValidExportP(aExecutable, aSym))
       
    86 	{
       
    87 		if( !iDllName )
       
    88 			iDllName = aDll;
       
    89 
       
    90 		iExportList.push_back(aSym);
       
    91 		iSorted = false;
       
    92 		return aSym;
       
    93 	}
       
    94 	return NULL;
       
    95 }
       
    96 
       
    97 /**
       
    98 Function to add elf exports
       
    99 @param aDll - Dll name
       
   100 @param aSym - Dll symbol
       
   101 @internalComponent
       
   102 @released
       
   103 */
       
   104 void ElfExports::Add(char *aDll, DllSymbol *aSym)
       
   105 {
       
   106 	if( !iDllName )
       
   107 		iDllName = aDll;
       
   108 	iExportList.push_back(aSym);
       
   109 	iSorted = false;
       
   110 }
       
   111 
       
   112 /**
       
   113 Function to sort elf exports. Sorting may be done based on the symbol
       
   114 name or on the ordinal number depending on the usecase.
       
   115 @internalComponent
       
   116 @released
       
   117 */
       
   118 void ElfExports::Sort() 
       
   119 {
       
   120 	if (!iSorted) {
       
   121 		if(iExportsFilteredP) {
       
   122 			std::sort(iFilteredExports.begin(), iFilteredExports.end(), PtrELFExportNameCompare());
       
   123 		}
       
   124 		else {
       
   125 			std::sort(iExportList.begin(), iExportList.end(), PtrELFExportNameCompare());
       
   126 		}
       
   127 		iSorted = true;
       
   128 	}
       
   129 }
       
   130 
       
   131 /**
       
   132 Function to get exports list.
       
   133 @param aSorted - sort before returning the exports.
       
   134 @return export list
       
   135 @internalComponent
       
   136 @released
       
   137 */
       
   138 ElfExports::ExportList & ElfExports::GetExports(bool aSorted)
       
   139 {
       
   140 	if (aSorted) Sort();
       
   141 	
       
   142 	if(iExportsFilteredP) 
       
   143 		return iFilteredExports;
       
   144 	else
       
   145 		return iExportList;
       
   146 }
       
   147 
       
   148 /**
       
   149 Function to get exports in ordinal order
       
   150 @return export list
       
   151 @internalComponent
       
   152 @released
       
   153 */
       
   154 ElfExports::ExportList & ElfExports::GetExportsInOrdinalOrder()
       
   155 {
       
   156 	if (iExportsFilteredP)
       
   157 	{
       
   158 		std::sort(iFilteredExports.begin(), iFilteredExports.end(), PtrELFExportOrdinalCompare());
       
   159 		return iFilteredExports;
       
   160 	}
       
   161 	else
       
   162 	{
       
   163 		std::sort(iExportList.begin(), iExportList.end(), PtrELFExportOrdinalCompare());
       
   164 		return iExportList;
       
   165 	}
       
   166 }
       
   167 
       
   168 /**
       
   169 Function to process the filtered exports
       
   170 @internalComponent
       
   171 @released
       
   172 */
       
   173 void ElfExports::FilterExports()
       
   174 {
       
   175 	std::sort(iExportList.begin(), iExportList.end(), PtrELFExportNameCompare());
       
   176 	std::sort(iFilteredExports.begin(), iFilteredExports.end(), PtrELFExportNameCompare());
       
   177 
       
   178 	ExportList aNewList(iExportList.size());
       
   179 	ExportList::iterator aNewListBegin = aNewList.begin(); 
       
   180 
       
   181 	ExportList::iterator aNewListEnd = set_difference(iExportList.begin(), iExportList.end(), \
       
   182 		iFilteredExports.begin(), iFilteredExports.end(), aNewListBegin, PtrELFExportNameCompare());
       
   183 
       
   184 	iFilteredExports.clear();
       
   185 	while (aNewListBegin != aNewListEnd)
       
   186 	{
       
   187 		iFilteredExports.push_back(*aNewListBegin);
       
   188 		aNewListBegin++;
       
   189 	}
       
   190 }
       
   191 
       
   192 /**
       
   193 Function to get number of exports
       
   194 @return size of export list
       
   195 @internalComponent
       
   196 @released
       
   197 */
       
   198 size_t ElfExports::GetNumExports()
       
   199 {
       
   200 	return iExportList.size();
       
   201 }
       
   202 
       
   203 /**
       
   204 Function to get Dll name
       
   205 @return Dll name
       
   206 @internalComponent
       
   207 @released
       
   208 */
       
   209 char* ElfExports::DllName()
       
   210 { 
       
   211 	return iDllName;
       
   212 }
       
   213 
       
   214 /**
       
   215 Overloaded operator to compare ELF export names.
       
   216 @return True if lhs symbol name < rhs symbol name, otherwise false
       
   217 @internalComponent
       
   218 @released
       
   219 */
       
   220 bool ElfExports::PtrELFExportNameCompare::operator()(const Symbol * lhs, const Symbol * rhs) const
       
   221 {
       
   222 	return strcmp( lhs->SymbolName(), rhs->SymbolName()) < 0;
       
   223 }
       
   224 
       
   225 /**
       
   226 Overloaded operator to compare ordinal numbers of symbols.
       
   227 @return True if lhs symbol name < rhs symbol name, otherwise false
       
   228 @internalComponent
       
   229 @released
       
   230 */
       
   231 bool ElfExports::PtrELFExportOrdinalCompare::operator()(const Symbol * lhs, const Symbol * rhs) const
       
   232 {
       
   233 	return lhs->OrdNum() < rhs->OrdNum();
       
   234 }
       
   235 
       
   236 /**
       
   237 Overloaded operator to compare update symbol attributes that are 
       
   238 being compared. The comparision is done on the symbol names.
       
   239 @return True if lhs symbol name < rhs symbol name, otherwise false
       
   240 @internalComponent
       
   241 @released
       
   242 */
       
   243 bool ElfExports::PtrELFExportNameCompareUpdateAttributes::operator()(const Symbol * lhs, const Symbol * rhs) const
       
   244 {
       
   245 	int result = strcmp(lhs->SymbolName(), rhs->SymbolName());
       
   246 	if (!result) 
       
   247 	{
       
   248 		if (lhs->OrdNum() > 0)
       
   249 			((Symbol*)rhs)->SetOrdinal( lhs->OrdNum());
       
   250 		else if (rhs->OrdNum() > 0)
       
   251 			((Symbol*)lhs)->SetOrdinal( rhs->OrdNum());
       
   252 				
       
   253 		if( ((Symbol*)lhs)->Absent() )
       
   254 			((Symbol*)rhs)->SetAbsent(true);
       
   255 		else if ( ((Symbol*)rhs)->Absent() )
       
   256 			((Symbol*)lhs)->SetAbsent(true);
       
   257 
       
   258 		if( ((Symbol*)lhs)->SymbolSize() )
       
   259 			((Symbol*)rhs)->SetSymbolSize(((Symbol*)lhs)->SymbolSize());
       
   260 		else if( ((Symbol*)rhs)->SymbolSize() )
       
   261 			((Symbol*)lhs)->SetSymbolSize(((Symbol*)rhs)->SymbolSize());
       
   262 	}
       
   263 	return result < 0;
       
   264 }
       
   265