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