Commit 429b1d5a authored by pratik ashar's avatar pratik ashar Committed by gbsbuild

Insert Program Header Table in elf. Fix elf header to make it gdb

compliant. Added is_stmt for 1-step elf

Change-Id: I8483478f7d587afbcad28bb2bdc17407e90a5826
parent d5ab3ee8
......@@ -135,6 +135,13 @@ bool DebugInfoPass::runOnModule(llvm::Module& M)
EmitDebugInfo(finalize);
}
// set VISA dbg info to nullptr to indicate 1-step debug is enabled
if (isOneStepElf)
{
currShader->ProgramOutput()->m_debugDataGenISASize = 0;
currShader->ProgramOutput()->m_debugDataGenISA = nullptr;
}
if (finalize)
{
IDebugEmitter::Release(m_pDebugEmitter);
......@@ -142,16 +149,6 @@ bool DebugInfoPass::runOnModule(llvm::Module& M)
// destroy VISA builder
auto encoder = &(m_currShader->GetEncoder());
encoder->DestroyVISABuilder();
// set VISA dbg info to nullptr to indicate 1-step debug is enabled
if (isOneStepElf)
{
for (auto& sp : units)
{
sp->ProgramOutput()->m_debugDataGenISASize = 0;
sp->ProgramOutput()->m_debugDataGenISA = nullptr;
}
}
}
}
......
......@@ -1456,6 +1456,7 @@ DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract)
return VariableDie;
}
bool addDecoration = false;
if (m_pModule->isDirectElfInput)
{
if (Loc.HasSurface())
......@@ -1475,6 +1476,8 @@ DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract)
buildPointer(DV, VariableDie, &Loc);
else
buildGeneral(DV, VariableDie, &Loc);
addDecoration = true;
}
else
{
......@@ -1540,23 +1543,29 @@ DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract)
// Now attach the location information to the DIE.
addBlock(VariableDie, dwarf::DW_AT_location, Block);
if (Loc.IsVectorized())
{
// Add description stating whether variable was vectorized in VISA
addString(VariableDie, dwarf::DW_AT_description, "vectorized");
uint16_t simdSize = m_pModule->GetSIMDSize();
addString(VariableDie, dwarf::DW_AT_description,
simdSize == 8 ? "simd8" : simdSize == 16 ? "simd16" : "???");
}
if (Loc.IsInGlobalAddrSpace())
{
addString(VariableDie, dwarf::DW_AT_description, "global");
}
addDecoration = true;
}
DV.setDIE(VariableDie);
}
if (addDecoration)
{
if (Loc.IsVectorized())
{
// Add description stating whether variable was vectorized in VISA
addString(VariableDie, dwarf::DW_AT_description, "vectorized");
uint16_t simdSize = m_pModule->GetSIMDSize();
addString(VariableDie, dwarf::DW_AT_description,
simdSize == 8 ? "simd8" : simdSize == 16 ? "simd16" : "???");
}
if (Loc.IsInGlobalAddrSpace())
{
addString(VariableDie, dwarf::DW_AT_description, "global");
}
}
return VariableDie;
}
......
......@@ -593,6 +593,31 @@ namespace IGC
return &DISPToFunction;
}
bool isStmtExists(unsigned int line, llvm::DILocation* inlinedAt, bool add)
{
auto it = isStmtSet.find(line);
if (it == isStmtSet.end())
{
if (add)
{
std::vector<llvm::DILocation*> v = { inlinedAt };
isStmtSet.insert(std::make_pair(line, v));
}
return false;
}
for (auto& iat : (*it).second)
{
if (iat == inlinedAt)
return true;
}
if (add)
(*it).second.push_back(inlinedAt);
return false;
}
unsigned int lowPc = 0, highPc = 0;
DbgDecoder* getDecodedDbg() { return decodedDbg; }
......
......@@ -487,7 +487,7 @@ class VISAMCCodeEmitter : public MCCodeEmitter
} // namespace IGC
StreamEmitter::StreamEmitter(raw_pwrite_stream& outStream, const std::string& dataLayout, const std::string& targetTriple) :
StreamEmitter::StreamEmitter(raw_pwrite_stream& outStream, const std::string& dataLayout, const std::string& targetTriple, bool isDirectElf) :
m_targetTriple(targetTriple), m_setCounter(0)
{
#if LLVM_VERSION_MAJOR == 4
......@@ -534,8 +534,15 @@ StreamEmitter::StreamEmitter(raw_pwrite_stream& outStream, const std::string& da
bool is64Bit = GetPointerSize() == 8;
uint8_t osABI = MCELFObjectTargetWriter::getOSABI(triple.getOS());
uint16_t eMachine = is64Bit ? ELF::EM_X86_64 : ELF::EM_386;
bool hasRelocationAddend = eMachine != ELF::EM_386;
// Earlier eMachine was set to ELF::EM_X86_64 or ELF::EM_386
// This creates a problem for gdb so it is now set to 182
// which is an encoding reserved for Intel. It is not part of
// the enum so its value in inlined.
#define EM_INTEL_GEN 182
uint16_t eMachine =
isDirectElf ? EM_INTEL_GEN :
is64Bit ? ELF::EM_X86_64 : ELF::EM_386;
bool hasRelocationAddend = is64Bit;
std::unique_ptr<MCAsmBackend> pAsmBackend
= llvm::make_unique<VISAAsmBackend>(GetTargetTriple(), is64Bit);
std::unique_ptr<MCELFObjectTargetWriter> pTargetObjectWriter
......
......@@ -65,7 +65,8 @@ public:
/// @param raw_ostream instance of raw_ostream to emit all bitcode into.
/// @param dataLayout data layout string.
/// @param targetTriple target triple string.
StreamEmitter(llvm::raw_pwrite_stream&, const std::string& dataLayout, const std::string& targetTriple);
/// @param isDirectElf set to true when using 1-step elf
StreamEmitter(llvm::raw_pwrite_stream&, const std::string& dataLayout, const std::string& targetTriple, bool isDirectElf);
/// @brief Destructor.
~StreamEmitter();
......
......@@ -42,6 +42,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "llvm/IR/Module.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/MC/MCDwarf.h"
#if LLVM_VERSION_MAJOR == 4
#include "llvm/Support/ELF.h"
#elif LLVM_VERSION_MAJOR >= 7
#include "llvm/BinaryFormat/ELF.h"
#endif
#include "common/LLVMWarningsPop.hpp"
#include "Compiler/DebugInfo/DebugInfoUtils.hpp"
......@@ -173,7 +179,7 @@ void DebugEmitter::Initialize(CShader* pShader, bool debugEnabled)
}
std::string dataLayout = m_pVISAModule->GetDataLayout();
m_pStreamEmitter = new StreamEmitter(m_outStream, dataLayout, m_pVISAModule->GetTargetTriple());
m_pStreamEmitter = new StreamEmitter(m_outStream, dataLayout, m_pVISAModule->GetTargetTriple(), m_pVISAModule->isDirectElfInput);
m_pDwarfDebug = new DwarfDebug(m_pStreamEmitter, m_pVISAModule);
}
......@@ -238,7 +244,7 @@ void DebugEmitter::Finalize(void *&pBuffer, unsigned int &size, bool finalize)
}
void* genxISA = m_pVISAModule->m_pShader->ProgramOutput()->m_programBin;
unsigned int prevSrcLine = 0;
DebugLoc prevSrcLoc = DebugLoc();
unsigned int pc = prevLastGenOff;
for (auto item : GenISAToVISAIndex)
{
......@@ -281,12 +287,17 @@ void DebugEmitter::Finalize(void *&pBuffer, unsigned int &size, bool finalize)
auto scope = loc->getScope();
auto src = m_pDwarfDebug->getOrCreateSourceID(scope->getFilename(), scope->getDirectory(), m_pStreamEmitter->GetDwarfCompileUnitID());
if (loc.getLine() != prevSrcLine)
if (loc != prevSrcLoc)
{
m_pStreamEmitter->EmitDwarfLocDirective(src, loc.getLine(), loc.getCol(), 1, 0, 0, scope->getFilename());
unsigned int Flags = 0;
if (!m_pDwarfDebug->isStmtExists(loc.getLine(), loc.getInlinedAt(), true))
{
Flags |= DWARF2_FLAG_IS_STMT;
}
m_pStreamEmitter->EmitDwarfLocDirective(src, loc.getLine(), loc.getCol(), Flags, 0, 0, scope->getFilename());
}
prevSrcLine = loc.getLine();
prevSrcLoc = loc;
}
}
}
......@@ -342,14 +353,84 @@ void DebugEmitter::Finalize(void *&pBuffer, unsigned int &size, bool finalize)
m_pStreamEmitter->Finalize();
size = m_outStream.str().size();
pBuffer = (char*)malloc(size * sizeof(char));
// Add program header table to satisfy latest gdb
unsigned int is64Bit = (GetVISAModule()->GetModule()->getDataLayout().getPointerSize() == 8);
unsigned int phtSize = sizeof(llvm::ELF::Elf32_Phdr);
if (is64Bit)
phtSize = sizeof(llvm::ELF::Elf64_Phdr);
pBuffer = (char*)malloc((size + phtSize) * sizeof(char));
memcpy_s(pBuffer, size * sizeof(char), m_outStream.str().data(), size);
writeProgramHeaderTable(is64Bit, pBuffer, size);
if (m_pVISAModule->isDirectElfInput)
setElfType(is64Bit, pBuffer);
size += phtSize;
// Reset all members and prepare for next beginModule() call.
Reset();
}
}
void DebugEmitter::setElfType(bool is64Bit, void* pBuffer)
{
// Set 1-step elf's e_type to ET_EXEC
if (!pBuffer)
return;
if (is64Bit)
{
void* etypeOff = ((char*)pBuffer) + (offsetof(llvm::ELF::Elf64_Ehdr, e_type));
*((llvm::ELF::Elf64_Half*)etypeOff) = llvm::ELF::ET_EXEC;
}
else
{
void* etypeOff = ((char*)pBuffer) + (offsetof(llvm::ELF::Elf32_Ehdr, e_type));
*((llvm::ELF::Elf32_Half*)etypeOff) = llvm::ELF::ET_EXEC;
}
}
void DebugEmitter::writeProgramHeaderTable(bool is64Bit, void* pBuffer, unsigned int size)
{
// Write program header table at end of elf
if (is64Bit)
{
llvm::ELF::Elf64_Phdr hdr;
hdr.p_type = llvm::ELF::PT_LOAD;
hdr.p_flags = 0;
hdr.p_offset = 0;
hdr.p_vaddr = 0;
hdr.p_paddr = 0;
hdr.p_filesz = size;
hdr.p_memsz = size;
hdr.p_align = 4;
void* phOffAddr = ((char*)pBuffer) + (offsetof(llvm::ELF::Elf64_Ehdr, e_phoff));
*(llvm::ELF::Elf64_Off*)(phOffAddr) = size;
((char*)pBuffer)[offsetof(llvm::ELF::Elf64_Ehdr, e_phentsize)] = sizeof(llvm::ELF::Elf64_Phdr);
((char*)pBuffer)[offsetof(llvm::ELF::Elf64_Ehdr, e_phnum)] = 1;
memcpy_s((char*)pBuffer + size, sizeof(llvm::ELF::Elf64_Phdr), &hdr, sizeof(hdr));
}
else
{
llvm::ELF::Elf32_Phdr hdr;
hdr.p_type = llvm::ELF::PT_LOAD;
hdr.p_offset = 0;
hdr.p_vaddr = 0;
hdr.p_paddr = 0;
hdr.p_filesz = size;
hdr.p_memsz = size;
hdr.p_flags = 0;
hdr.p_align = 4;
void* phOffAddr = ((char*)pBuffer) + (offsetof(llvm::ELF::Elf32_Ehdr, e_phoff));
*(llvm::ELF::Elf32_Off*)(phOffAddr) = size;
((char*)pBuffer)[offsetof(llvm::ELF::Elf32_Ehdr, e_phentsize)] = sizeof(llvm::ELF::Elf32_Phdr);
((char*)pBuffer)[offsetof(llvm::ELF::Elf32_Ehdr, e_phnum)] = 1;
memcpy_s((char*)pBuffer + size, sizeof(llvm::ELF::Elf32_Phdr), &hdr, sizeof(hdr));
}
}
void DebugEmitter::BeginInstruction(Instruction *pInst)
{
BeginEncodingMark();
......
......@@ -116,6 +116,9 @@ private:
std::vector<VISAModule*> toFree;
unsigned int lastGenOff = 0;
void writeProgramHeaderTable(bool is64Bit, void* pBuffer, unsigned int size);
void setElfType(bool is64Bit, void* pBuffer);
};
} // namespace IGC
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment