1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "ElfWriter.h"
#include "secure_mem.h" // needed for memcpy_s on linux/android
#include <cstring>
namespace CLElfLib {
/******************************************************************************\
Constructor: CElfWriter::CElfWriter
\******************************************************************************/
CElfWriter::CElfWriter(E_EH_TYPE type, E_EH_MACHINE machine,
#if defined(_X86_)
Elf32_Xword flags
#else
Elf64_Xword flags
#endif
) {
m_type = type;
m_machine = machine;
m_flags = flags;
m_dataSize = 0;
m_numSections = 0;
m_stringTableSize = 0;
m_totalBinarySize = 0;
}
/******************************************************************************\
Destructor: CElfWriter::~CElfWriter
\******************************************************************************/
CElfWriter::~CElfWriter() {
SSectionNode *pNode = NULL;
// Walk through the section nodes
while (m_nodeQueue.empty() == false) {
pNode = m_nodeQueue.front();
m_nodeQueue.pop();
// delete the node and it's data
if (pNode) {
if (pNode->pData) {
delete[] pNode->pData;
pNode->pData = NULL;
}
delete pNode;
}
}
}
/******************************************************************************\
Member Function: CElfWriter::Create
\******************************************************************************/
CElfWriter *CElfWriter::Create(E_EH_TYPE type, E_EH_MACHINE machine,
#if defined(_X86_)
Elf32_Xword flags
#else
Elf64_Xword flags
#endif
) {
CElfWriter *pWriter = new CElfWriter(type, machine, flags);
if ((pWriter) && (pWriter->Initialize() != SUCCESS)) {
Delete(pWriter);
}
return pWriter;
}
/******************************************************************************\
Member Function: CElfWriter::Delete
\******************************************************************************/
void CElfWriter::Delete(CElfWriter *&pWriter) {
if (pWriter) {
delete pWriter;
pWriter = NULL;
}
}
/******************************************************************************\
Member Function: CElfWriter::AddSection
\******************************************************************************/
E_RETVAL CElfWriter::AddSection(SSectionNode *pSectionNode) {
E_RETVAL retVal = SUCCESS;
SSectionNode *pNode = NULL;
size_t nameSize = 0;
unsigned int dataSize = 0;
// The section header must be non-NULL
if (pSectionNode) {
pNode = new SSectionNode();
if (!pNode) {
retVal = OUT_OF_MEMORY;
}
} else {
retVal = FAILURE;
}
if (retVal == SUCCESS) {
pNode->Flags = pSectionNode->Flags;
pNode->Type = pSectionNode->Type;
nameSize = pSectionNode->Name.size() + 1;
dataSize = pSectionNode->DataSize;
pNode->Name = pSectionNode->Name;
// ok to have NULL data
if (dataSize > 0) {
pNode->pData = new char[dataSize];
if (pNode->pData) {
memcpy_s(pNode->pData, dataSize, pSectionNode->pData, dataSize);
pNode->DataSize = dataSize;
} else {
retVal = OUT_OF_MEMORY;
}
}
if (retVal == SUCCESS) {
// push the node onto the queue
m_nodeQueue.push(pNode);
// increment the sizes for each section
m_dataSize += dataSize;
m_stringTableSize += nameSize;
m_numSections++;
} else {
// cleanup allocations
if (pNode->pData) {
delete[] pNode->pData;
pNode->pData = NULL;
}
delete pNode;
}
}
return retVal;
}
/******************************************************************************\
Member Function: CElfWriter::ResolveBinary
\******************************************************************************/
E_RETVAL CElfWriter::ResolveBinary(char *const pBinary, size_t &binarySize) {
E_RETVAL retVal = SUCCESS;
SSectionNode *pNode = NULL;
SElfSectionHeader *pCurSectionHeader = NULL;
char *pData = NULL;
char *pStringTable = NULL;
char *pCurString = NULL;
m_totalBinarySize = sizeof(SElfHeader) +
((m_numSections + 1) * sizeof(SElfSectionHeader)) + // +1 to account for string table entry
m_dataSize + m_stringTableSize;
if (pBinary) {
// get a pointer to the first section header
pCurSectionHeader = (SElfSectionHeader *)(pBinary + sizeof(SElfHeader));
// get a pointer to the data
pData = pBinary + sizeof(SElfHeader) +
((m_numSections + 1) * sizeof(SElfSectionHeader)); // +1 to account for string table entry
// get a pointer to the string table
pStringTable = pBinary + sizeof(SElfHeader) +
((m_numSections + 1) * sizeof(SElfSectionHeader)) + // +1 to account for string table entry
m_dataSize;
pCurString = pStringTable;
// Walk through the section nodes
while (m_nodeQueue.empty() == false) {
pNode = m_nodeQueue.front();
if (pNode) {
m_nodeQueue.pop();
// Copy data into the section header
memset(pCurSectionHeader, 0, sizeof(SElfSectionHeader));
pCurSectionHeader->Type = pNode->Type;
pCurSectionHeader->Flags = pNode->Flags;
pCurSectionHeader->DataSize = pNode->DataSize;
pCurSectionHeader->DataOffset = pData - pBinary;
#if defined(_X86_)
pCurSectionHeader->Name = (Elf32_Word)(pCurString - pStringTable);
#else
pCurSectionHeader->Name = (Elf64_Word)(pCurString - pStringTable);
#endif
pCurSectionHeader = (SElfSectionHeader *)((unsigned char *)pCurSectionHeader + sizeof(SElfSectionHeader));
// copy the data, move the data pointer
memcpy_s(pData, pNode->DataSize, pNode->pData, pNode->DataSize);
pData += pNode->DataSize;
// copy the name into the string table, move the string pointer
if (pNode->Name.size() > 0) {
memcpy_s(pCurString, pNode->Name.size(), pNode->Name.c_str(), pNode->Name.size());
pCurString += pNode->Name.size();
}
*(pCurString++) = '\0';
// delete the node and it's data
if (pNode->pData) {
delete[] pNode->pData;
pNode->pData = NULL;
}
delete pNode;
}
}
// add the string table section header
SElfSectionHeader stringSectionHeader = {0};
stringSectionHeader.Type = SH_TYPE_STR_TBL;
stringSectionHeader.Flags = 0;
stringSectionHeader.DataOffset = pStringTable - pBinary;
stringSectionHeader.DataSize = m_stringTableSize;
stringSectionHeader.Name = 0;
// Copy into the last section header
memcpy_s(pCurSectionHeader, sizeof(SElfSectionHeader), &stringSectionHeader, sizeof(SElfSectionHeader));
// Add to our section number
m_numSections++;
// patch up the ELF header
retVal = PatchElfHeader(pBinary);
}
if (retVal == SUCCESS) {
binarySize = m_totalBinarySize;
}
return retVal;
}
/******************************************************************************\
Member Function: CElfWriter::Initialize
\******************************************************************************/
E_RETVAL CElfWriter::Initialize() {
E_RETVAL retVal = SUCCESS;
SSectionNode emptySection;
// Add an empty section 0 (points to "no-bits")
AddSection(&emptySection);
return retVal;
}
/******************************************************************************\
Member Function: CElfWriter::PatchElfHeader
\******************************************************************************/
E_RETVAL CElfWriter::PatchElfHeader(char *const pBinary) {
E_RETVAL retVal = SUCCESS;
SElfHeader *pElfHeader = (SElfHeader *)pBinary;
if (pElfHeader) {
// Setup the identity
memset(pElfHeader, 0x00, sizeof(SElfHeader));
pElfHeader->Identity[ID_IDX_MAGIC0] = ELF_MAG0;
pElfHeader->Identity[ID_IDX_MAGIC1] = ELF_MAG1;
pElfHeader->Identity[ID_IDX_MAGIC2] = ELF_MAG2;
pElfHeader->Identity[ID_IDX_MAGIC3] = ELF_MAG3;
#if defined(_X86_)
pElfHeader->Identity[ID_IDX_CLASS] = EH_CLASS_32;
#else
pElfHeader->Identity[ID_IDX_CLASS] = EH_CLASS_64;
#endif
pElfHeader->Identity[ID_IDX_VERSION] = EH_VERSION_CURRENT;
// Add other non-zero info
pElfHeader->Type = m_type;
pElfHeader->Machine = m_machine;
pElfHeader->Flags = (unsigned int)m_flags;
pElfHeader->ElfHeaderSize = sizeof(SElfHeader);
pElfHeader->SectionHeaderEntrySize = sizeof(SElfSectionHeader);
#if defined(_X86_)
pElfHeader->NumSectionHeaderEntries = (Elf32_Short)m_numSections;
#else
pElfHeader->NumSectionHeaderEntries = (Elf64_Short)m_numSections;
#endif
pElfHeader->SectionHeadersOffset = (unsigned int)(sizeof(SElfHeader));
pElfHeader->SectionNameTableIndex = m_numSections - 1; // last index
}
return retVal;
}
} // namespace CLElfLib
|