tools/elf4rom/src/elfsectionmanager.cpp
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 #include <string.h>
       
    20 
       
    21 #include "elfsectionmanager.h"
       
    22 
       
    23 void ElfSectionManager::GetFileFragmentData(FileFragmentData & aFileFragmentData ){
       
    24 	int nSections = iSections.size();
       
    25 	if (nSections > 0){
       
    26 		iData = new Elf32_Shdr[nSections];
       
    27 		Elf32_Shdr * p = iData;
       
    28 		SectionList::iterator aSection = iSections.begin();
       
    29 		SectionList::iterator end = iSections.end();
       
    30 		while (aSection != end) {
       
    31 			*p = aSection->GetSectionHeader().iElf32Shdr;
       
    32 			p++;
       
    33 			aSection++;
       
    34 		}
       
    35 		SetFileFragmentData(aFileFragmentData, Size(), reinterpret_cast<char *>(iData));
       
    36 	}
       
    37 }
       
    38 
       
    39 size_t ElfSectionManager::Size(){
       
    40 	return SectionHeaderSize();
       
    41 }
       
    42 
       
    43 void ElfSectionManager::DeleteFileFragmentData(){
       
    44 	if (iData) {
       
    45 		Elf32_Shdr * d = iData;
       
    46 		iData = NULL;
       
    47 		delete[] d;
       
    48 	}
       
    49 }
       
    50 
       
    51 void ElfSectionManager::AddData(){
       
    52 	AddData(iOutputFile);
       
    53 }
       
    54 
       
    55 void ElfSectionManager::AddData(OutputFile & aOutputFile){
       
    56 	if (iSections.size() > 0) {
       
    57 		SectionList::iterator aSection = iSections.begin();
       
    58 		SectionList::iterator end = iSections.end();
       
    59 		while (aSection != end) {
       
    60 			aSection->AddData(aOutputFile);
       
    61 			aSection++;
       
    62 		}
       
    63 		AddSectionTable();
       
    64 	}
       
    65 }
       
    66 
       
    67 void ElfSectionManager::EnsureSectionStringTableSectionAdded(){
       
    68 	if (iSectionStringTableSectionAdded) return;
       
    69 	
       
    70 	// set iSectionStringTableSectionAdded true now so we don't do the next bit twice;
       
    71 	iSectionStringTableSectionAdded = true;
       
    72 
       
    73 	// first create the UNDEF section
       
    74 	ElfSectionNoData * aUndefData = new ElfSectionNoData();
       
    75 	
       
    76 	Elf32_Shdr undef;
       
    77 	undef.sh_name = 0; 
       
    78 	undef.sh_type = SHT_NULL;
       
    79 	undef.sh_flags = 0;
       
    80 	undef.sh_addr = 0;
       
    81 	undef.sh_offset = 0; 
       
    82 	undef.sh_size = 0; 
       
    83 	undef.sh_link = 0;
       
    84 	undef.sh_info = 0;
       
    85 	undef.sh_addralign = 0;
       
    86 	undef.sh_entsize = 0;
       
    87 	
       
    88 	ElfSection aUndefSection(aUndefData, "", undef);
       
    89 	AddSection(aUndefSection);
       
    90 	
       
    91 	ElfSectionElfData * aSectionStringTableSectionData = new ElfSectionElfData(iStringTable);
       
    92 	Elf32_Shdr shdr;
       
    93 	shdr.sh_name = 0; // for now.
       
    94 	shdr.sh_type = SHT_STRTAB;
       
    95 	shdr.sh_flags = 0;
       
    96 	shdr.sh_addr = 0;
       
    97 	shdr.sh_offset = 0; // for now
       
    98 	shdr.sh_size = 0; // for now.
       
    99 	shdr.sh_link = SHN_UNDEF;
       
   100 	shdr.sh_info = 0;
       
   101 	shdr.sh_addralign = 0;
       
   102 	shdr.sh_entsize = 0;
       
   103 
       
   104 	ElfSection aSectionStringTableSection(aSectionStringTableSectionData, ".shstrtab", shdr);
       
   105 	AddSection(aSectionStringTableSection);
       
   106 	iElf32Header.SetSectionStringNdx(iSections.size() - 1);
       
   107 }
       
   108 void ElfSectionManager::AddSection(ElfSection & aSection){
       
   109 	EnsureSectionStringTableSectionAdded();
       
   110 	if (aSection.GetName().size() > 0){
       
   111 		// rename sections for GDB (yuk!)
       
   112 		String sectionName(aSection.GetName());
       
   113 		String ro("ER_RO");
       
   114 		String text(".text");
       
   115 		if (sectionName == ro){
       
   116 			sectionName = text;
       
   117 		} else {
       
   118 			String rw("ER_RW");
       
   119 			String data(".data");
       
   120 			if (sectionName == rw){
       
   121 				sectionName = data;
       
   122 			} else {
       
   123 				String zi("ER_ZI");
       
   124 				String bss(".bss");
       
   125 				if (sectionName == zi)
       
   126 					sectionName = bss;
       
   127 			}
       
   128 		}
       
   129 		size_t nameOffset = iStringTable.AddName(sectionName);
       
   130 		aSection.SetNameOffset(nameOffset);
       
   131 	} else {
       
   132 		// use the initial Null String.
       
   133 		size_t nameOffset = iStringTable.AllocateInitialNullString();
       
   134 		aSection.SetNameOffset(nameOffset);
       
   135 	}
       
   136 	aSection.SetIndex(iSections.size());
       
   137 	iSections.push_back(aSection);
       
   138 	iElf32Header.AddSectionHdr();
       
   139 }
       
   140 	
       
   141 size_t ElfSectionManager::SectionHeaderSize(){
       
   142 	return iSections.size() * sizeof(Elf32_Shdr);
       
   143 }
       
   144 
       
   145 void ElfSectionManager::AddSectionStringTable(){
       
   146 	// Assume the section header already setup and we've got hold of it. We need to say where the strings ended up.
       
   147 	const FileFragment & aSectionTableFrag = iOutputFile.GetFileFragment(this);	
       
   148 	SetOffset(aSectionTableFrag.GetOffset());
       
   149 }
       
   150 
       
   151 void ElfSectionManager::AddSectionTable(){
       
   152 	const FileFragment & aSectionTableFrag = iOutputFile.GetFileFragment(this);	
       
   153 	SetOffset(aSectionTableFrag.GetOffset());
       
   154 	iElf32Header.SetSectionHdrOffset(GetOffset());
       
   155 }