elflib reimplementation - initial commit

Includes:
-add explicit definition of enum types
-replace NULL for nullptr
-add namespace for constants
-replace c-style casting
-add SH_FLAG_NONE for consistency

Change-Id: I3eb8d26cc6b549e91a940ae0d3c095a9f96785f2
This commit is contained in:
Kamil Diedrich
2018-07-24 16:13:56 +02:00
committed by sys_ocldev
parent 6e03446a22
commit 282f335269
13 changed files with 162 additions and 160 deletions

View File

@@ -31,9 +31,9 @@ namespace CLElfLib {
CElfReader::CElfReader(
const char *pElfBinary,
const size_t elfBinarySize) {
m_pNameTable = NULL;
m_pNameTable = nullptr;
m_nameTableSize = 0;
m_pElfHeader = (SElf64Header *)pElfBinary;
m_pElfHeader = reinterpret_cast<const SElf64Header *>(pElfBinary);
m_pBinary = pElfBinary;
// get a pointer to the string table
@@ -56,7 +56,7 @@ CElfReader::~CElfReader() {
CElfReader *CElfReader::create(
const char *pElfBinary,
const size_t elfBinarySize) {
CElfReader *pNewReader = NULL;
CElfReader *pNewReader = nullptr;
if (isValidElf64(pElfBinary, elfBinarySize)) {
pNewReader = new CElfReader(pElfBinary, elfBinarySize);
@@ -72,7 +72,7 @@ void CElfReader::destroy(
CElfReader *&pElfReader) {
if (pElfReader) {
delete pElfReader;
pElfReader = NULL;
pElfReader = nullptr;
}
}
@@ -85,25 +85,26 @@ bool CElfReader::isValidElf64(
const void *pBinary,
const size_t binarySize) {
bool retVal = false;
SElf64Header *pElf64Header = NULL;
SElf64SectionHeader *pSectionHeader = NULL;
char *pNameTable = NULL;
char *pEnd = NULL;
const SElf64Header *pElf64Header = nullptr;
const SElf64SectionHeader *pSectionHeader = nullptr;
const char *pNameTable = nullptr;
const char *pEnd = nullptr;
size_t ourSize = 0;
size_t entrySize = 0;
size_t indexedSectionHeaderOffset = 0;
auto pBinaryLocal = static_cast<const char *>(pBinary);
// validate header
if (pBinary && (binarySize >= sizeof(SElf64Header))) {
// calculate a pointer to the end
pEnd = (char *)pBinary + binarySize;
pElf64Header = (SElf64Header *)pBinary;
pEnd = pBinaryLocal + binarySize;
pElf64Header = static_cast<const SElf64Header *>(pBinary);
if ((pElf64Header->Identity[ID_IDX_MAGIC0] == ELF_MAG0) &&
(pElf64Header->Identity[ID_IDX_MAGIC1] == ELF_MAG1) &&
(pElf64Header->Identity[ID_IDX_MAGIC2] == ELF_MAG2) &&
(pElf64Header->Identity[ID_IDX_MAGIC3] == ELF_MAG3) &&
(pElf64Header->Identity[ID_IDX_CLASS] == EH_CLASS_64)) {
if ((pElf64Header->Identity[ELFConstants::idIdxMagic0] == ELFConstants::elfMag0) &&
(pElf64Header->Identity[ELFConstants::idIdxMagic1] == ELFConstants::elfMag1) &&
(pElf64Header->Identity[ELFConstants::idIdxMagic2] == ELFConstants::elfMag2) &&
(pElf64Header->Identity[ELFConstants::idIdxMagic3] == ELFConstants::elfMag3) &&
(pElf64Header->Identity[ELFConstants::idIdxClass] == static_cast<uint32_t>(E_EH_CLASS::EH_CLASS_64))) {
ourSize += pElf64Header->ElfHeaderSize;
retVal = true;
}
@@ -117,29 +118,26 @@ bool CElfReader::isValidElf64(
// get an offset to the name table
if (pElf64Header->SectionNameTableIndex <
pElf64Header->NumSectionHeaderEntries) {
indexedSectionHeaderOffset =
(size_t)pElf64Header->SectionHeadersOffset +
(pElf64Header->SectionNameTableIndex * entrySize);
indexedSectionHeaderOffset = static_cast<size_t>(pElf64Header->SectionHeadersOffset) + (pElf64Header->SectionNameTableIndex * entrySize);
if (((char *)pBinary + indexedSectionHeaderOffset) <= pEnd) {
pNameTable = (char *)pBinary + indexedSectionHeaderOffset;
if ((pBinaryLocal + indexedSectionHeaderOffset) <= pEnd) {
pNameTable = pBinaryLocal + indexedSectionHeaderOffset;
}
}
for (unsigned int i = 0; i < pElf64Header->NumSectionHeaderEntries; i++) {
indexedSectionHeaderOffset = (size_t)pElf64Header->SectionHeadersOffset +
(i * entrySize);
indexedSectionHeaderOffset = static_cast<size_t>(pElf64Header->SectionHeadersOffset) + (i * entrySize);
// check section header offset
if (((char *)pBinary + indexedSectionHeaderOffset) > pEnd) {
if ((pBinaryLocal + indexedSectionHeaderOffset) > pEnd) {
retVal = false;
break;
}
pSectionHeader = (SElf64SectionHeader *)((char *)pBinary + indexedSectionHeaderOffset);
pSectionHeader = reinterpret_cast<const SElf64SectionHeader *>(pBinaryLocal + indexedSectionHeaderOffset);
// check section data
if (((char *)pBinary + pSectionHeader->DataOffset + pSectionHeader->DataSize) > pEnd) {
if ((pBinaryLocal + pSectionHeader->DataOffset + pSectionHeader->DataSize) > pEnd) {
retVal = false;
break;
}
@@ -151,8 +149,8 @@ bool CElfReader::isValidElf64(
}
// tally up the sizes
ourSize += (size_t)pSectionHeader->DataSize;
ourSize += (size_t)entrySize;
ourSize += static_cast<size_t>(pSectionHeader->DataSize);
ourSize += static_cast<size_t>(entrySize);
}
if (ourSize != binarySize) {
@@ -177,15 +175,14 @@ const SElf64Header *CElfReader::getElfHeader() {
\******************************************************************************/
const SElf64SectionHeader *CElfReader::getSectionHeader(
unsigned int sectionIndex) {
SElf64SectionHeader *pSectionHeader = NULL;
const SElf64SectionHeader *pSectionHeader = nullptr;
size_t indexedSectionHeaderOffset = 0;
size_t entrySize = m_pElfHeader->SectionHeaderEntrySize;
if (sectionIndex < m_pElfHeader->NumSectionHeaderEntries) {
indexedSectionHeaderOffset = (size_t)m_pElfHeader->SectionHeadersOffset +
(sectionIndex * entrySize);
indexedSectionHeaderOffset = static_cast<size_t>(m_pElfHeader->SectionHeadersOffset) + (sectionIndex * entrySize);
pSectionHeader = (SElf64SectionHeader *)((char *)m_pElfHeader + indexedSectionHeaderOffset);
pSectionHeader = reinterpret_cast<const SElf64SectionHeader *>(reinterpret_cast<const char *>(m_pElfHeader) + indexedSectionHeaderOffset);
}
return pSectionHeader;
@@ -193,7 +190,7 @@ const SElf64SectionHeader *CElfReader::getSectionHeader(
/******************************************************************************\
Member Function: GetSectionData
Description: Returns a pointer to and size of the requested section's
Description: Returns a pointer to and size of the requested section's
data
\******************************************************************************/
bool CElfReader::getSectionData(
@@ -203,8 +200,8 @@ bool CElfReader::getSectionData(
const SElf64SectionHeader *pSectionHeader = getSectionHeader(sectionIndex);
if (pSectionHeader) {
pData = (char *)m_pBinary + pSectionHeader->DataOffset;
dataSize = (size_t)pSectionHeader->DataSize;
pData = const_cast<char *>(m_pBinary) + pSectionHeader->DataOffset;
dataSize = static_cast<size_t>(pSectionHeader->DataSize);
return true;
}
@@ -213,14 +210,14 @@ bool CElfReader::getSectionData(
/******************************************************************************\
Member Function: GetSectionData
Description: Returns a pointer to and size of the requested section's
Description: Returns a pointer to and size of the requested section's
data
\******************************************************************************/
bool CElfReader::getSectionData(
const char *pName,
char *&pData,
size_t &dataSize) {
const char *pSectionName = NULL;
const char *pSectionName = nullptr;
for (unsigned int i = 1; i < m_pElfHeader->NumSectionHeaderEntries; i++) {
pSectionName = getSectionName(i);
@@ -241,7 +238,7 @@ bool CElfReader::getSectionData(
\******************************************************************************/
const char *CElfReader::getSectionName(
unsigned int sectionIndex) {
char *pName = NULL;
char *pName = nullptr;
const SElf64SectionHeader *pSectionHeader = getSectionHeader(sectionIndex);
if (pSectionHeader) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -77,7 +77,7 @@ class CElfReader {
ELF_CALL ~CElfReader();
SElf64Header *m_pElfHeader; // pointer to the ELF header
const SElf64Header *m_pElfHeader; // pointer to the ELF header
const char *m_pBinary; // portable ELF binary
char *m_pNameTable; // pointer to the string table
size_t m_nameTableSize; // size of string table in bytes

View File

@@ -32,7 +32,7 @@ namespace CLElfLib {
\******************************************************************************/
// E_ID_IDX - Defines a file as being ELF
enum E_ID_IDX {
enum class E_ID_IDX : uint32_t {
ID_IDX_MAGIC0 = 0,
ID_IDX_MAGIC1 = 1,
ID_IDX_MAGIC2 = 2,
@@ -46,7 +46,7 @@ enum E_ID_IDX {
};
// E_EHT_CLASS - Describes what data types the ELF structures will use.
enum E_EH_CLASS {
enum class E_EH_CLASS : uint32_t {
EH_CLASS_NONE = 0,
EH_CLASS_32 = 1, // Use Elf32 data types
EH_CLASS_64 = 2, // Use Elf64 data types
@@ -55,7 +55,7 @@ enum E_EH_CLASS {
// E_EHT_TYPE - List of pre-defined types header types.
// OS-specific codes start at 0xfe00 and run to 0xfeff.
// Processor-specific codes start at 0xff00 and end at 0xffff.
enum E_EH_TYPE {
enum class E_EH_TYPE : uint16_t {
EH_TYPE_NONE = 0,
EH_TYPE_RELOCATABLE = 1,
EH_TYPE_EXECUTABLE = 2,
@@ -71,21 +71,21 @@ enum E_EH_TYPE {
// E_EH_MACHINE - List of pre-defined machine types.
// For OpenCL, currently, we do not need this information, so this is not
// fully defined.
enum E_EH_MACHINE {
enum class E_EH_MACHINE : uint16_t {
EH_MACHINE_NONE = 0,
//EHT_MACHINE_LO_RSVD = 1, // Beginning of range of reserved types.
//EHT_MACHINE_HI_RSVD = 200, // End of range of reserved types.
};
// E_EHT_VERSION - ELF header version options.
enum E_EHT_VERSION {
enum class E_EHT_VERSION : uint32_t {
EH_VERSION_INVALID = 0,
EH_VERSION_CURRENT = 1,
};
// E_SH_TYPE - List of pre-defined section header types.
// Processor-specific codes start at 0xff00 and end at 0xffff.
enum E_SH_TYPE {
enum class E_SH_TYPE : uint32_t {
SH_TYPE_NULL = 0,
SH_TYPE_PROG_BITS = 1,
SH_TYPE_SYM_TBL = 2,
@@ -117,7 +117,8 @@ enum E_SH_TYPE {
};
// E_SH_FLAG - List of section header flags.
enum E_SH_FLAG {
enum class E_SH_FLAG : uint64_t {
SH_FLAG_NONE = 0x0,
SH_FLAG_WRITE = 0x1,
SH_FLAG_ALLOC = 0x2,
SH_FLAG_EXEC_INSTR = 0x4,
@@ -135,40 +136,44 @@ enum E_SH_FLAG {
/******************************************************************************\
ELF-64 Data Types
\******************************************************************************/
#if defined(_MSC_VER) // && (_MSC_VER < 1700)
typedef unsigned __int64 Elf64_Addr;
typedef unsigned __int64 Elf64_Off;
typedef unsigned __int16 Elf64_Short; // Renaming Elf64_Half to Elf64_Short to avoid a conflict with Android
typedef unsigned __int32 Elf64_Word;
typedef __int32 Elf64_Sword;
typedef unsigned __int64 Elf64_Xword;
#else
#if !defined(_UAPI_LINUX_ELF_H)
typedef uint64_t Elf64_Addr;
typedef uint64_t Elf64_Off;
typedef uint32_t Elf64_Word;
typedef int32_t Elf64_Sword;
typedef uint64_t Elf64_Xword;
#endif
typedef uint16_t Elf64_Short; // Renaming Elf64_Half to Elf64_Short to avoid a conflict with Android
#endif
/******************************************************************************\
ELF Constants
\******************************************************************************/
static const unsigned char ELF_MAG0 = 0x7f; // ELFHeader.Identity[ELF_ID_MAGIC0]
static const unsigned char ELF_MAG1 = 'E'; // ELFHeader.Identity[ELF_ID_MAGIC1]
static const unsigned char ELF_MAG2 = 'L'; // ELFHeader.Identity[ELF_ID_MAGIC2]
static const unsigned char ELF_MAG3 = 'F'; // ELFHeader.Identity[ELF_ID_MAGIC3]
static const unsigned int ELF_ALIGN_BYTES = 16; // Alignment set to 16-bytes
namespace ELFConstants {
static const unsigned char elfMag0 = 0x7f; // ELFHeader.Identity[ELF_ID_MAGIC0]
static const unsigned char elfMag1 = 'E'; // ELFHeader.Identity[ELF_ID_MAGIC1]
static const unsigned char elfMag2 = 'L'; // ELFHeader.Identity[ELF_ID_MAGIC2]
static const unsigned char elfMag3 = 'F'; // ELFHeader.Identity[ELF_ID_MAGIC3]
static const unsigned int elfAlignBytes = 16; // Alignment set to 16-bytes
static const uint32_t idIdxMagic0 = 0;
static const uint32_t idIdxMagic1 = 1;
static const uint32_t idIdxMagic2 = 2;
static const uint32_t idIdxMagic3 = 3;
static const uint32_t idIdxClass = 4;
static const uint32_t idIdxVersion = 5;
static const uint32_t idIdxOsabi = 6;
static const uint32_t idIdxAbiVersion = 7;
static const uint32_t idIdxPadding = 8;
static const uint32_t idIdxNumBytes = 16;
} // namespace ELFConstants
/******************************************************************************\
ELF-64 Header
\******************************************************************************/
struct SElf64Header {
unsigned char Identity[ID_IDX_NUM_BYTES];
Elf64_Short Type;
Elf64_Short Machine;
unsigned char Identity[ELFConstants::idIdxNumBytes];
E_EH_TYPE Type;
E_EH_MACHINE Machine;
Elf64_Word Version;
Elf64_Addr EntryAddress;
Elf64_Off ProgramHeadersOffset;
@@ -187,8 +192,8 @@ struct SElf64Header {
\******************************************************************************/
struct SElf64SectionHeader {
Elf64_Word Name;
Elf64_Word Type;
Elf64_Xword Flags;
E_SH_TYPE Type;
E_SH_FLAG Flags;
Elf64_Addr Address;
Elf64_Off DataOffset;
Elf64_Xword DataSize;

View File

@@ -40,7 +40,7 @@ CElfWriter::CElfWriter(
Destructor: CElfWriter::~CElfWriter
\******************************************************************************/
CElfWriter::~CElfWriter() {
SSectionNode *pNode = NULL;
SSectionNode *pNode = nullptr;
// Walk through the section nodes
while (m_nodeQueue.empty() == false) {
@@ -51,7 +51,7 @@ CElfWriter::~CElfWriter() {
if (pNode) {
if (pNode->pData) {
delete[] pNode->pData;
pNode->pData = NULL;
pNode->pData = nullptr;
}
delete pNode;
@@ -83,7 +83,7 @@ void CElfWriter::destroy(
CElfWriter *&pWriter) {
if (pWriter) {
delete pWriter;
pWriter = NULL;
pWriter = nullptr;
}
}
@@ -93,7 +93,7 @@ void CElfWriter::destroy(
bool CElfWriter::addSection(
SSectionNode *pSectionNode) {
bool retVal = true;
SSectionNode *pNode = NULL;
SSectionNode *pNode = nullptr;
size_t nameSize = 0;
unsigned int dataSize = 0;
@@ -148,11 +148,11 @@ bool CElfWriter::resolveBinary(
char *const pBinary,
size_t &binarySize) {
bool retVal = true;
SSectionNode *pNode = NULL;
SElf64SectionHeader *pCurSectionHeader = NULL;
char *pData = NULL;
char *pStringTable = NULL;
char *pCurString = NULL;
SSectionNode *pNode = nullptr;
SElf64SectionHeader *pCurSectionHeader = nullptr;
char *pData = nullptr;
char *pStringTable = nullptr;
char *pCurString = nullptr;
m_totalBinarySize =
sizeof(SElf64Header) +
@@ -162,7 +162,7 @@ bool CElfWriter::resolveBinary(
if (pBinary) {
// get a pointer to the first section header
pCurSectionHeader = (SElf64SectionHeader *)(pBinary + sizeof(SElf64Header));
pCurSectionHeader = reinterpret_cast<SElf64SectionHeader *>(pBinary + sizeof(SElf64Header));
// get a pointer to the data
pData = pBinary +
@@ -189,8 +189,8 @@ bool CElfWriter::resolveBinary(
pCurSectionHeader->Flags = pNode->Flags;
pCurSectionHeader->DataSize = pNode->DataSize;
pCurSectionHeader->DataOffset = pData - pBinary;
pCurSectionHeader->Name = (Elf64_Word)(pCurString - pStringTable);
pCurSectionHeader = (SElf64SectionHeader *)((unsigned char *)pCurSectionHeader + sizeof(SElf64SectionHeader));
pCurSectionHeader->Name = static_cast<Elf64_Word>(pCurString - pStringTable);
pCurSectionHeader = reinterpret_cast<SElf64SectionHeader *>(reinterpret_cast<unsigned char *>(pCurSectionHeader) + sizeof(SElf64SectionHeader));
// copy the data, move the data pointer
memcpy_s(pData, pNode->DataSize, pNode->pData, pNode->DataSize);
@@ -206,7 +206,7 @@ bool CElfWriter::resolveBinary(
// delete the node and it's data
if (pNode->pData) {
delete[] pNode->pData;
pNode->pData = NULL;
pNode->pData = nullptr;
}
delete pNode;
@@ -216,8 +216,8 @@ bool CElfWriter::resolveBinary(
// add the string table section header
SElf64SectionHeader stringSectionHeader = {0};
stringSectionHeader.Type = SH_TYPE_STR_TBL;
stringSectionHeader.Flags = 0;
stringSectionHeader.Type = E_SH_TYPE::SH_TYPE_STR_TBL;
stringSectionHeader.Flags = E_SH_FLAG::SH_FLAG_NONE;
stringSectionHeader.DataOffset = pStringTable - pBinary;
stringSectionHeader.DataSize = m_stringTableSize;
stringSectionHeader.Name = 0;
@@ -254,26 +254,26 @@ bool CElfWriter::initialize() {
Member Function: CElfWriter::PatchElfHeader
\******************************************************************************/
bool CElfWriter::patchElfHeader(char *const pBinary) {
SElf64Header *pElfHeader = (SElf64Header *)pBinary;
SElf64Header *pElfHeader = reinterpret_cast<SElf64Header *>(pBinary);
if (pElfHeader) {
// Setup the identity
memset(pElfHeader, 0x00, sizeof(SElf64Header));
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;
pElfHeader->Identity[ID_IDX_CLASS] = EH_CLASS_64;
pElfHeader->Identity[ID_IDX_VERSION] = EH_VERSION_CURRENT;
pElfHeader->Identity[ELFConstants::idIdxMagic0] = ELFConstants::elfMag0;
pElfHeader->Identity[ELFConstants::idIdxMagic1] = ELFConstants::elfMag1;
pElfHeader->Identity[ELFConstants::idIdxMagic2] = ELFConstants::elfMag2;
pElfHeader->Identity[ELFConstants::idIdxMagic3] = ELFConstants::elfMag3;
pElfHeader->Identity[ELFConstants::idIdxClass] = static_cast<uint32_t>(E_EH_CLASS::EH_CLASS_64);
pElfHeader->Identity[ELFConstants::idIdxVersion] = static_cast<uint32_t>(E_EHT_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(SElf64Header);
pElfHeader->SectionHeaderEntrySize = sizeof(SElf64SectionHeader);
pElfHeader->NumSectionHeaderEntries = (Elf64_Short)m_numSections;
pElfHeader->SectionHeadersOffset = (unsigned int)(sizeof(SElf64Header));
pElfHeader->Flags = static_cast<uint32_t>(m_flags);
pElfHeader->ElfHeaderSize = static_cast<uint32_t>(sizeof(SElf64Header));
pElfHeader->SectionHeaderEntrySize = static_cast<uint32_t>(sizeof(SElf64SectionHeader));
pElfHeader->NumSectionHeaderEntries = m_numSections;
pElfHeader->SectionHeadersOffset = static_cast<uint32_t>(sizeof(SElf64Header));
pElfHeader->SectionNameTableIndex = m_numSections - 1; // last index
return true;

View File

@@ -39,15 +39,15 @@ static const unsigned int g_scInitNumSectionHeaders = 8;
struct SSectionNode {
E_SH_TYPE Type;
unsigned int Flags;
E_SH_FLAG Flags;
string Name;
char *pData;
unsigned int DataSize;
SSectionNode() {
Type = SH_TYPE_NULL;
Flags = 0;
pData = NULL;
Type = E_SH_TYPE::SH_TYPE_NULL;
Flags = E_SH_FLAG::SH_FLAG_NONE;
pData = nullptr;
DataSize = 0;
}
@@ -91,14 +91,14 @@ class CElfWriter {
ELF_CALL ~CElfWriter();
E_EH_TYPE m_type = EH_TYPE_NONE;
E_EH_MACHINE m_machine = EH_MACHINE_NONE;
E_EH_TYPE m_type = E_EH_TYPE::EH_TYPE_NONE;
E_EH_MACHINE m_machine = E_EH_MACHINE::EH_MACHINE_NONE;
Elf64_Xword m_flags = 0U;
std::queue<SSectionNode *> m_nodeQueue;
unsigned int m_dataSize = 0U;
unsigned int m_numSections = 0U;
uint32_t m_dataSize = 0U;
uint32_t m_numSections = 0U;
size_t m_stringTableSize = 0U;
size_t m_totalBinarySize = 0U;
};

View File

@@ -679,14 +679,14 @@ bool OfflineCompiler::generateElfBinary() {
}
if (retVal) {
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_EXECUTABLE, CLElfLib::EH_MACHINE_NONE, 0);
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_EXECUTABLE, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
if (pElfWriter) {
CLElfLib::SSectionNode sectionNode;
// Always add the options string
sectionNode.Name = "BuildOptions";
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_OPTIONS;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_OPTIONS;
sectionNode.pData = (char *)options.c_str();
sectionNode.DataSize = (uint32_t)(strlen(options.c_str()) + 1);
@@ -694,7 +694,7 @@ bool OfflineCompiler::generateElfBinary() {
if (retVal) {
sectionNode.Name = "Intel(R) OpenCL LLVM Object";
sectionNode.Type = isSpirV ? CLElfLib::SH_TYPE_SPIRV : CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.Type = isSpirV ? CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV : CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.pData = irBinary;
sectionNode.DataSize = (uint32_t)irBinarySize;
retVal = pElfWriter->addSection(&sectionNode);
@@ -703,7 +703,7 @@ bool OfflineCompiler::generateElfBinary() {
// Add the device binary if it exists
if (retVal && genBinary) {
sectionNode.Name = "Intel(R) OpenCL Device Binary";
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_DEV_BINARY;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_DEV_BINARY;
sectionNode.pData = genBinary;
sectionNode.DataSize = (uint32_t)genBinarySize;

View File

@@ -98,7 +98,7 @@ cl_int Program::compile(
}
// create ELF writer to process all sources to be compiled
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_SOURCE, CLElfLib::EH_MACHINE_NONE, 0);
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_SOURCE, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
UNRECOVERABLE_IF(pElfWriter == nullptr);
CLElfLib::SSectionNode sectionNode;
@@ -107,8 +107,8 @@ cl_int Program::compile(
sectionNode.Name = "CLMain";
sectionNode.pData = (char *)sourceCode.c_str();
sectionNode.DataSize = (unsigned int)(strlen(sourceCode.c_str()) + 1);
sectionNode.Flags = 0;
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_SOURCE;
sectionNode.Flags = CLElfLib::E_SH_FLAG::SH_FLAG_NONE;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_SOURCE;
// add main program's source
pElfWriter->addSection(&sectionNode);
@@ -125,8 +125,8 @@ cl_int Program::compile(
break;
}
sectionNode.Name = headerIncludeNames[i];
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_HEADER;
sectionNode.Flags = 0;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_HEADER;
sectionNode.Flags = CLElfLib::E_SH_FLAG::SH_FLAG_NONE;
// collect required data from the header
retVal = pHeaderProgObj->getSource(sectionNode.pData, sectionNode.DataSize);
if (retVal != CL_SUCCESS) {

View File

@@ -81,7 +81,7 @@ cl_int Program::link(
buildStatus = CL_BUILD_IN_PROGRESS;
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_OBJECTS, CLElfLib::EH_MACHINE_NONE, 0);
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_OBJECTS, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
StackVec<const Program *, 16> inputProgramsInternal;
for (cl_uint i = 0; i < numInputPrograms; i++) {
@@ -102,11 +102,11 @@ cl_int Program::link(
}
sectionNode.Name = "";
if (pInputProgObj->getIsSpirV()) {
sectionNode.Type = CLElfLib::SH_TYPE_SPIRV;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV;
} else {
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_LLVM_BINARY;
}
sectionNode.Flags = 0;
sectionNode.Flags = CLElfLib::E_SH_FLAG::SH_FLAG_NONE;
sectionNode.pData = pInputProgObj->irBinary;
sectionNode.DataSize = static_cast<unsigned int>(pInputProgObj->irBinarySize);

View File

@@ -68,15 +68,15 @@ cl_int Program::processElfBinary(
pElfHeader = pElfReader->getElfHeader();
switch (pElfHeader->Type) {
case CLElfLib::EH_TYPE_OPENCL_EXECUTABLE:
case CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_EXECUTABLE:
programBinaryType = CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
break;
case CLElfLib::EH_TYPE_OPENCL_LIBRARY:
case CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_LIBRARY:
programBinaryType = CL_PROGRAM_BINARY_TYPE_LIBRARY;
break;
case CLElfLib::EH_TYPE_OPENCL_OBJECTS:
case CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_OBJECTS:
programBinaryType = CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;
break;
@@ -94,17 +94,17 @@ cl_int Program::processElfBinary(
sectionDataSize = 0;
switch (pSectionHeader->Type) {
case CLElfLib::SH_TYPE_SPIRV:
case CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV:
isSpirV = true;
CPP_ATTRIBUTE_FALLTHROUGH;
case CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY:
case CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_LLVM_BINARY:
pElfReader->getSectionData(i, pSectionData, sectionDataSize);
if (pSectionData && sectionDataSize) {
storeIrBinary(pSectionData, sectionDataSize, isSpirV);
}
break;
case CLElfLib::SH_TYPE_OPENCL_DEV_BINARY:
case CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_DEV_BINARY:
pElfReader->getSectionData(i, pSectionData, sectionDataSize);
if (pSectionData && sectionDataSize && validateGenBinaryHeader((SProgramBinaryHeader *)pSectionData)) {
storeGenBinary(pSectionData, sectionDataSize);
@@ -115,14 +115,14 @@ cl_int Program::processElfBinary(
}
break;
case CLElfLib::SH_TYPE_OPENCL_OPTIONS:
case CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_OPTIONS:
pElfReader->getSectionData(i, pSectionData, sectionDataSize);
if (pSectionData && sectionDataSize) {
options = pSectionData;
}
break;
case CLElfLib::SH_TYPE_STR_TBL:
case CLElfLib::E_SH_TYPE::SH_TYPE_STR_TBL:
// We can skip the string table
break;
@@ -159,7 +159,7 @@ cl_int Program::resolveProgramBinary() {
switch (programBinaryType) {
case CL_PROGRAM_BINARY_TYPE_EXECUTABLE:
headerType = CLElfLib::EH_TYPE_OPENCL_EXECUTABLE;
headerType = CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_EXECUTABLE;
if (!genBinary || !genBinarySize) {
retVal = CL_INVALID_BINARY;
@@ -167,7 +167,7 @@ cl_int Program::resolveProgramBinary() {
break;
case CL_PROGRAM_BINARY_TYPE_LIBRARY:
headerType = CLElfLib::EH_TYPE_OPENCL_LIBRARY;
headerType = CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_LIBRARY;
if (!irBinary || !irBinarySize) {
retVal = CL_INVALID_BINARY;
@@ -175,7 +175,7 @@ cl_int Program::resolveProgramBinary() {
break;
case CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT:
headerType = CLElfLib::EH_TYPE_OPENCL_OBJECTS;
headerType = CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_OBJECTS;
if (!irBinary || !irBinarySize) {
retVal = CL_INVALID_BINARY;
@@ -187,14 +187,14 @@ cl_int Program::resolveProgramBinary() {
}
if (retVal == CL_SUCCESS) {
pElfWriter = CLElfLib::CElfWriter::create(headerType, CLElfLib::EH_MACHINE_NONE, 0);
pElfWriter = CLElfLib::CElfWriter::create(headerType, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
if (pElfWriter) {
CLElfLib::SSectionNode sectionNode;
// Always add the options string
sectionNode.Name = "BuildOptions";
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_OPTIONS;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_OPTIONS;
sectionNode.pData = (char *)options.c_str();
sectionNode.DataSize = (uint32_t)(strlen(options.c_str()) + 1);
@@ -203,11 +203,11 @@ cl_int Program::resolveProgramBinary() {
if (elfRetVal) {
// Add the LLVM component if available
if (getIsSpirV()) {
sectionNode.Type = CLElfLib::SH_TYPE_SPIRV;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV;
} else {
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_LLVM_BINARY;
}
if (headerType == CLElfLib::EH_TYPE_OPENCL_LIBRARY) {
if (headerType == CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_LIBRARY) {
sectionNode.Name = "Intel(R) OpenCL LLVM Archive";
sectionNode.pData = (char *)irBinary;
sectionNode.DataSize = (uint32_t)irBinarySize;
@@ -223,7 +223,7 @@ cl_int Program::resolveProgramBinary() {
// Add the device binary if it exists
if (elfRetVal && genBinary) {
sectionNode.Name = "Intel(R) OpenCL Device Binary";
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_DEV_BINARY;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_DEV_BINARY;
sectionNode.pData = (char *)genBinary;
sectionNode.DataSize = (uint32_t)genBinarySize;
@@ -233,7 +233,7 @@ cl_int Program::resolveProgramBinary() {
// Add the device debug data if it exists
if (elfRetVal && (debugData != nullptr)) {
sectionNode.Name = "Intel(R) OpenCL Device Debug";
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_DEV_DEBUG;
sectionNode.Type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_DEV_DEBUG;
sectionNode.pData = debugData;
sectionNode.DataSize = (uint32_t)debugDataSize;
elfRetVal = pElfWriter->addSection(&sectionNode);

View File

@@ -182,13 +182,13 @@ cl_int Program::rebuildProgramFromIr() {
isSpirV = true;
}
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_OBJECTS, CLElfLib::EH_MACHINE_NONE, 0);
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_OBJECTS, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
UNRECOVERABLE_IF(pElfWriter == nullptr);
CLElfLib::SSectionNode sectionNode;
sectionNode.Name = "";
sectionNode.Type = isSpirV ? CLElfLib::SH_TYPE_SPIRV : CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.Flags = 0;
sectionNode.Type = isSpirV ? CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV : CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_LLVM_BINARY;
sectionNode.Flags = CLElfLib::E_SH_FLAG::SH_FLAG_NONE;
sectionNode.pData = irBinary;
sectionNode.DataSize = static_cast<unsigned int>(irBinarySize);
pElfWriter->addSection(&sectionNode);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Intel Corporation
* Copyright (c) 2017 - 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -41,8 +41,8 @@ struct ElfTests : public MemoryManagementFixture,
TEST_F(ElfTests, Create_Delete_Writer_Simple) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
EXPECT_NE((CElfWriter *)NULL, pWriter);
@@ -70,8 +70,8 @@ TEST_F(ElfTests, Create_Reader_Garbage_Binary) {
TEST_F(ElfTests, Create_Delete_Reader_Writer_NoCheck) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
EXPECT_NE((CElfWriter *)NULL, pWriter);
@@ -103,8 +103,8 @@ TEST_F(ElfTests, givenNullptrWriterWhenDestroyThenDontCrash) {
TEST_F(ElfTests, givenElfWriterWhenNullptrThenFalseIsReturned) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
ASSERT_NE(nullptr, pWriter);
@@ -117,8 +117,8 @@ TEST_F(ElfTests, givenElfWriterWhenNullptrThenFalseIsReturned) {
TEST_F(ElfTests, givenElfWriterWhenSectionAndFailuresThenFalseIsReturned) {
InjectedFunction method = [](size_t failureIndex) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
if (nonfailingAllocation == failureIndex) {
ASSERT_NE(nullptr, pWriter);
@@ -131,9 +131,9 @@ TEST_F(ElfTests, givenElfWriterWhenSectionAndFailuresThenFalseIsReturned) {
SSectionNode sectionNode;
sectionNode.DataSize = 16;
sectionNode.pData = sectionData;
sectionNode.Flags = SH_FLAG_WRITE;
sectionNode.Flags = E_SH_FLAG::SH_FLAG_WRITE;
sectionNode.Name = "Steve";
sectionNode.Type = SH_TYPE_OPENCL_SOURCE;
sectionNode.Type = E_SH_TYPE::SH_TYPE_OPENCL_SOURCE;
auto ret = pWriter->addSection(&sectionNode);
if (nonfailingAllocation == failureIndex) {
@@ -150,8 +150,8 @@ TEST_F(ElfTests, givenElfWriterWhenSectionAndFailuresThenFalseIsReturned) {
TEST_F(ElfTests, givenElfWriterWhenPatchNullptrThenFalseIsReturned) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
ASSERT_NE(nullptr, pWriter);
@@ -163,8 +163,8 @@ TEST_F(ElfTests, givenElfWriterWhenPatchNullptrThenFalseIsReturned) {
TEST_F(ElfTests, Write_Read_Section_Data_By_Name) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
EXPECT_NE((CElfWriter *)NULL, pWriter);
@@ -174,9 +174,9 @@ TEST_F(ElfTests, Write_Read_Section_Data_By_Name) {
SSectionNode sectionNode;
sectionNode.DataSize = 16;
sectionNode.pData = sectionData;
sectionNode.Flags = SH_FLAG_WRITE;
sectionNode.Flags = E_SH_FLAG::SH_FLAG_WRITE;
sectionNode.Name = "Steve";
sectionNode.Type = SH_TYPE_OPENCL_SOURCE;
sectionNode.Type = E_SH_TYPE::SH_TYPE_OPENCL_SOURCE;
pWriter->addSection(&sectionNode);
@@ -212,8 +212,8 @@ TEST_F(ElfTests, Write_Read_Section_Data_By_Name) {
TEST_F(ElfTests, Write_Read_Section_Data_By_Index) {
CElfWriter *pWriter = CElfWriter::create(
EH_TYPE_EXECUTABLE,
EH_MACHINE_NONE,
E_EH_TYPE::EH_TYPE_EXECUTABLE,
E_EH_MACHINE::EH_MACHINE_NONE,
0);
EXPECT_NE((CElfWriter *)NULL, pWriter);
@@ -223,9 +223,9 @@ TEST_F(ElfTests, Write_Read_Section_Data_By_Index) {
SSectionNode sectionNode;
sectionNode.DataSize = 16;
sectionNode.pData = sectionData;
sectionNode.Flags = SH_FLAG_WRITE;
sectionNode.Flags = E_SH_FLAG::SH_FLAG_WRITE;
sectionNode.Name = "";
sectionNode.Type = SH_TYPE_OPENCL_SOURCE;
sectionNode.Type = E_SH_TYPE::SH_TYPE_OPENCL_SOURCE;
pWriter->addSection(&sectionNode);

View File

@@ -96,7 +96,7 @@ TEST_F(ProcessElfBinaryTests, ValidSpirvBinary) {
ASSERT_NE(nullptr, pElfReader);
const CLElfLib::SElf64Header *pElfHeader = pElfReader->getElfHeader();
ASSERT_NE(nullptr, pElfHeader);
EXPECT_EQ(pElfHeader->Type, CLElfLib::EH_TYPE_OPENCL_LIBRARY);
EXPECT_EQ(pElfHeader->Type, CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_LIBRARY);
EXPECT_TRUE(CLElfLib::CElfReader::isValidElf64(elfBinary, elfBinarySize));
//check if ELF binary contains section SH_TYPE_SPIRV
@@ -104,7 +104,7 @@ TEST_F(ProcessElfBinaryTests, ValidSpirvBinary) {
for (uint32_t i = 1; i < pElfHeader->NumSectionHeaderEntries; i++) {
const CLElfLib::SElf64SectionHeader *pSectionHeader = pElfReader->getSectionHeader(i);
ASSERT_NE(nullptr, pSectionHeader);
if (pSectionHeader->Type == CLElfLib::SH_TYPE_SPIRV) {
if (pSectionHeader->Type == CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV) {
hasSpirvSection = true;
break;
}

View File

@@ -2215,11 +2215,11 @@ TEST_F(ProgramTests, ValidBinaryWithIGCVersionEqual0) {
size_t sectionDataSize = 0;
SProgramBinaryHeader *pBHdr = nullptr;
EXPECT_NE(nullptr, pElfHeader);
EXPECT_EQ(pElfHeader->Type, CLElfLib::EH_TYPE_OPENCL_EXECUTABLE);
EXPECT_EQ(pElfHeader->Type, CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_EXECUTABLE);
for (uint32_t i = 1; i < pElfHeader->NumSectionHeaderEntries; i++) {
const CLElfLib::SElf64SectionHeader *pSectionHeader = pElfReader->getSectionHeader(i);
if (pSectionHeader->Type != CLElfLib::SH_TYPE_OPENCL_DEV_BINARY) {
if (pSectionHeader->Type != CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_DEV_BINARY) {
continue;
}
pElfReader->getSectionData(i, pSectionData, sectionDataSize);
@@ -3066,7 +3066,7 @@ TEST_F(ProgramTests, givenProgramWithSpirvWhenRebuildProgramIsCalledThenSpirvPat
size_t spvSectionDataSize = 0;
for (uint32_t i = 0; i < elfReader->getElfHeader()->NumSectionHeaderEntries; i++) {
const SElf64SectionHeader *section = elfReader->getSectionHeader(i);
if (section->Type == CLElfLib::SH_TYPE_SPIRV) {
if (section->Type == CLElfLib::E_SH_TYPE::SH_TYPE_SPIRV) {
EXPECT_EQ(nullptr, spvSection);
elfReader->getSectionData(i, spvSectionData, spvSectionDataSize);
spvSection = section;