tools/elf4rom/src/elfsymboltablemanager.h
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, either version 3 of the License, or
       
     8 * (at your option) any later version.
       
     9 *
       
    10 * This program is distributed in the hope that it will be useful,
       
    11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 * GNU Lesser General Public License for more details.
       
    14 * 
       
    15 * You should have received a copy of the GNU Lesser General Public License
       
    16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 */
       
    18 
       
    19 #ifndef ELFSYMBOLTABLEMANAGER_H_
       
    20 #define ELFSYMBOLTABLEMANAGER_H_
       
    21 
       
    22 #include <vector>
       
    23 #include <cassert>
       
    24 
       
    25 #include "filefragment.h"
       
    26 #include "elfstringtable.h"
       
    27 #include "outputfile.h"
       
    28 #include "elfheader.h"
       
    29 #include "e32romimage.h"
       
    30 #include "elfsection.h"
       
    31 #include "elfsectionmanager.h"
       
    32 #include "defs.h" // String
       
    33 
       
    34 class ElfSymbolTableManager;
       
    35 
       
    36 class SectionNumberMapping {
       
    37 public:
       
    38 	SectionNumberMapping(size_t aOld, size_t aNew):
       
    39 		iOld(aOld), iNew(aNew)
       
    40 		{}
       
    41 	
       
    42 	SectionNumberMapping & operator=(const SectionNumberMapping & aSectionNumberMapping){
       
    43 		iOld = aSectionNumberMapping.iOld;
       
    44 		iNew = aSectionNumberMapping.iNew;
       
    45 		return *this;
       
    46 	}
       
    47 	
       
    48 	SectionNumberMapping(const SectionNumberMapping & aSectionNumberMapping){
       
    49 		*this = aSectionNumberMapping;
       
    50 	}
       
    51 
       
    52 	size_t iOld;
       
    53 	size_t iNew;
       
    54 };
       
    55 
       
    56 typedef std::vector< SectionNumberMapping > SectionNumberMap;
       
    57 
       
    58 class SectionVaddrAddendMapping {
       
    59 public:
       
    60 	SectionVaddrAddendMapping(size_t aSectionNumber, signed int aAddend):
       
    61 		iSectionNumber(aSectionNumber), iAddend(aAddend)
       
    62 		{}
       
    63 	
       
    64 	SectionVaddrAddendMapping & operator=(const SectionVaddrAddendMapping & aSectionVaddrAddendMapping){
       
    65 		iSectionNumber = aSectionVaddrAddendMapping.iSectionNumber;
       
    66 		iAddend = aSectionVaddrAddendMapping.iAddend;
       
    67 		return *this;
       
    68 	}
       
    69 	
       
    70 	SectionVaddrAddendMapping(const SectionVaddrAddendMapping & SectionVaddrAddendMapping){
       
    71 		*this = SectionVaddrAddendMapping;
       
    72 	}
       
    73 
       
    74 	size_t iSectionNumber;
       
    75 	signed int iAddend;
       
    76 };
       
    77 
       
    78 typedef std::vector< SectionVaddrAddendMapping > SectionVaddrAddendMap;
       
    79 
       
    80 class ElfFileSymbolFragments {
       
    81 public:
       
    82 	ElfFileSymbolFragments(ElfSymbolTableManager * aElfSymbolTableManager):
       
    83 		iPath(""), 
       
    84 		iSymbolTableOffset(0), iSymbolTableSize(0),
       
    85 		iFirstGlobal(0),
       
    86 		iStringTableOffset(0), iStringTableSize(0),
       
    87 		iElfSymbolTableManager(aElfSymbolTableManager)
       
    88 		{
       
    89 		iSectionNumberMap.clear();
       
    90 		iSectionVaddrAddendMap.clear();
       
    91 		}
       
    92 	
       
    93 	ElfFileSymbolFragments & operator=(const ElfFileSymbolFragments & aElfFileSymbolFragments){
       
    94 		iPath = aElfFileSymbolFragments.iPath;
       
    95 		iSymbolTableOffset = aElfFileSymbolFragments.iSymbolTableOffset;
       
    96 		iSymbolTableSize = aElfFileSymbolFragments.iSymbolTableSize;
       
    97 		iFirstGlobal = aElfFileSymbolFragments.iFirstGlobal;
       
    98 		iStringTableOffset = aElfFileSymbolFragments.iStringTableOffset;
       
    99 		iStringTableSize = aElfFileSymbolFragments.iStringTableSize;
       
   100 		iSectionNumberMap = aElfFileSymbolFragments.iSectionNumberMap;
       
   101 		iSectionVaddrAddendMap = aElfFileSymbolFragments.iSectionVaddrAddendMap;
       
   102 		iElfSymbolTableManager = aElfFileSymbolFragments.iElfSymbolTableManager;
       
   103 		return *this;
       
   104 	}
       
   105 	
       
   106 	ElfFileSymbolFragments(const ElfFileSymbolFragments & aElfFileSymbolFragments){
       
   107 		*this = aElfFileSymbolFragments;
       
   108 	}
       
   109 	
       
   110 	
       
   111 
       
   112 	void AddSymbolTable(String & aPath, size_t aOffset, size_t aSize, size_t aFirstGlobal);
       
   113 	
       
   114 	void AddStringTable(String & aPath, size_t aOffset, size_t aSize);
       
   115 	String & GetPath() { return iPath; }
       
   116 	size_t GetSymbolTableOffset() { return iSymbolTableOffset; }
       
   117 	size_t GetSymbolTableSize() { return iSymbolTableSize; }
       
   118 	size_t GetFirstGlobal() { return iFirstGlobal; }
       
   119 	size_t GetStringTableOffset() { return iStringTableOffset; }
       
   120 	size_t GetStringTableSize() { return iStringTableSize; }
       
   121 	
       
   122 	void SetSectionNumberMap(SectionNumberMap & aSectionNumberMap){
       
   123 		iSectionNumberMap = aSectionNumberMap;
       
   124 	}
       
   125 	
       
   126 	size_t LookupSection(size_t ndx);
       
   127 	
       
   128 	void SetSectionVaddrAddendMap(SectionVaddrAddendMap & aSectionVaddrAddendMap){
       
   129 		iSectionVaddrAddendMap = aSectionVaddrAddendMap;
       
   130 	}
       
   131 	
       
   132 	int LookupVaddrAddend(size_t ndx);
       
   133 	
       
   134 	void Validate() {
       
   135 		assert(iPath.size() != 0);
       
   136 		assert(iSymbolTableOffset != 0);
       
   137 		assert(iSymbolTableSize != 0);
       
   138 		assert(iStringTableOffset != 0);
       
   139 		assert(iStringTableSize != 0);
       
   140 		assert(!iSectionNumberMap.empty());
       
   141 		assert(!iSectionVaddrAddendMap.empty());
       
   142 	}
       
   143 	
       
   144 	void Reset(){
       
   145 		new (this) ElfFileSymbolFragments(iElfSymbolTableManager);
       
   146 	}
       
   147 	
       
   148 private:
       
   149 	String iPath;
       
   150 	size_t iSymbolTableOffset;
       
   151 	size_t iSymbolTableSize;
       
   152 	size_t iFirstGlobal;
       
   153 	size_t iStringTableOffset;
       
   154 	size_t iStringTableSize;
       
   155 	SectionNumberMap iSectionNumberMap;
       
   156 	SectionVaddrAddendMap iSectionVaddrAddendMap;
       
   157 	ElfSymbolTableManager * iElfSymbolTableManager;
       
   158 	
       
   159 };
       
   160 
       
   161 class ElfSymTabStringTable : public ElfStringTable {
       
   162 public:
       
   163 	ElfSymTabStringTable(ElfSymbolTableManager & aElfSymbolTableManager):
       
   164 		iElfSymbolTableManager(aElfSymbolTableManager)
       
   165 		{}
       
   166 	
       
   167 	virtual size_t Size();
       
   168 	
       
   169 private:
       
   170 	ElfSymbolTableManager & iElfSymbolTableManager;
       
   171 };
       
   172 
       
   173 class ElfSymbolTableManager : public FileFragmentOwner {
       
   174 public:
       
   175 	ElfSymbolTableManager(Elf32Header & aElf32Header, E32RomImage & aRomImage, 
       
   176 					      OutputFile & aOutputFile, ElfSectionManager & aElfSectionManager) :
       
   177 		iElf32Header(aElf32Header), iRomImage(aRomImage), 
       
   178 		iOutputFile(aOutputFile), iElfSectionManager(aElfSectionManager),
       
   179 		iData(NULL),
       
   180 		iSymbolStringTableSizeValid(false),
       
   181 		iSymbolStringTableSize(0),
       
   182 		iCurrentFragment(this),
       
   183 		iStringTable(*this)
       
   184 		{}
       
   185 	
       
   186 	// The FileFragmentOwner protocol
       
   187 	virtual void GetFileFragmentData(FileFragmentData & aFileFragmentData );
       
   188 	virtual size_t Size();
       
   189 	virtual void DeleteFileFragmentData();
       
   190 	virtual void AddData(OutputFile & aOutputFile);
       
   191 	
       
   192 	void AddSymbolTable(String & aPath, size_t aOffset, size_t aSize, size_t aFirstGlobal){
       
   193 		iCurrentFragment.AddSymbolTable(aPath, aOffset, aSize, aFirstGlobal);
       
   194 	}
       
   195 	void AddStringTable(String & aPath, size_t aOffset, size_t aSize){
       
   196 		iCurrentFragment.AddStringTable(aPath, aOffset, aSize);
       
   197 
       
   198 	}
       
   199 	virtual void AddSymbolFragment(){
       
   200 		iSymbolFragments.push_back(iCurrentFragment);
       
   201 	}
       
   202 	virtual void AddSymbolTable();
       
   203 	
       
   204 	size_t GetSymTabStringsSectionSize();
       
   205 
       
   206 	virtual void Finalize(SectionNumberMap & aSectionNumberMap, SectionVaddrAddendMap & aSectionVaddrAddendMap);
       
   207 	
       
   208 private:
       
   209 	// Don't want one of these to be copied
       
   210 	ElfSymbolTableManager(const ElfSymbolTableManager & aElfSymbolTableManager);
       
   211 	
       
   212 	ElfSymbolTableManager & operator=(const ElfSymbolTableManager & aElfSymbolTableManager);
       
   213 	
       
   214 	size_t GetSymTabSectionSize();
       
   215 	size_t GetFirstNonLocalIndex();
       
   216 
       
   217 private:
       
   218 	typedef std::vector<ElfFileSymbolFragments> SymbolFragmentList;
       
   219 private:
       
   220 	Elf32Header & iElf32Header;
       
   221 	E32RomImage & iRomImage;
       
   222 	OutputFile & iOutputFile;
       
   223 	ElfSectionManager & iElfSectionManager;
       
   224 	
       
   225 	char * iData;
       
   226 	bool iSymbolStringTableSizeValid;
       
   227 	size_t iSymbolStringTableSize;
       
   228 	ElfFileSymbolFragments iCurrentFragment;
       
   229 	SymbolFragmentList iSymbolFragments;
       
   230 	ElfSymTabStringTable iStringTable;
       
   231 };
       
   232 
       
   233 #endif /*ELFSYMBOLTABLEMANAGER_H_*/