summaryrefslogtreecommitdiffstats
path: root/src/dwarf2pdb.cpp
diff options
context:
space:
mode:
authorNeeraj Singh <neerajsi@microsoft.com>2021-11-29 20:44:16 (GMT)
committerNeeraj Singh <neerajsi@microsoft.com>2021-12-02 05:49:07 (GMT)
commit28d3fad41817310b8fc2fd6d9e46137249d65fad (patch)
treea6180d64820de4a9eea1f9b69a0c3ab3630814ea /src/dwarf2pdb.cpp
parent9e0795af0123ea890c43805d956ada5eae8b30d8 (diff)
downloadcv2pdb-28d3fad41817310b8fc2fd6d9e46137249d65fad.zip
cv2pdb-28d3fad41817310b8fc2fd6d9e46137249d65fad.tar.gz
cv2pdb-28d3fad41817310b8fc2fd6d9e46137249d65fad.tar.bz2
Refactor PE image section handling. Add debug logging.
Encapsulate dwarf-related PE sections into the PESection class. Remove some unused sections. Add helpers for common section operations. Move the context set via DIECursor::setContext to be static members of the class and add a debug context there. Add a standard method of enabling debug logging across the dwarf and PDB code.
Diffstat (limited to 'src/dwarf2pdb.cpp')
-rw-r--r--src/dwarf2pdb.cpp78
1 files changed, 46 insertions, 32 deletions
diff --git a/src/dwarf2pdb.cpp b/src/dwarf2pdb.cpp
index bc030e6..f58067d 100644
--- a/src/dwarf2pdb.cpp
+++ b/src/dwarf2pdb.cpp
@@ -230,8 +230,8 @@ class CFICursor
{
public:
CFICursor(const PEImage& img)
- : beg((byte*)img.debug_frame)
- , end((byte*)img.debug_frame + img.debug_frame_length)
+ : beg(img.debug_frame.startByte())
+ , end(img.debug_frame.endByte())
, ptr(beg)
{
default_address_size = img.isX64() ? 8 : 4;
@@ -489,7 +489,7 @@ Location findBestCFA(const PEImage& img, const CFIIndex* index, unsigned int pcl
{
bool x64 = img.isX64();
Location ebp = { Location::RegRel, x64 ? 6 : 5, x64 ? 16 : 8 };
- if (!img.debug_frame)
+ if (!img.debug_frame.isPresent())
return ebp;
byte *fde_ptr = index->lookup(pclo, pchi);
@@ -515,7 +515,6 @@ Location findBestCFA(const PEImage& img, const CFIIndex* index, unsigned int pcl
class LOCEntry
{
public:
- byte* ptr;
unsigned long beg_offset;
unsigned long end_offset;
Location loc;
@@ -529,8 +528,8 @@ class LOCCursor
public:
LOCCursor(const PEImage& image, unsigned long off)
: img (image)
- , end((byte*)img.debug_loc + img.debug_loc_length)
- , ptr((byte*)img.debug_loc + off)
+ , end(img.debug_loc.endByte())
+ , ptr(img.debug_loc.byteAt(off))
{
default_address_size = img.isX64() ? 8 : 4;
}
@@ -682,7 +681,7 @@ void CV2PDB::appendLexicalBlock(DWARF_InfoData& id, unsigned int proclo)
dsym->block_v3.end = 0; // destSize + sizeof(dsym->block_v3) + 12;
dsym->block_v3.length = id.pchi - id.pclo;
dsym->block_v3.offset = id.pclo - codeSegOff;
- dsym->block_v3.segment = img.codeSegment + 1;
+ dsym->block_v3.segment = img.text.secNo + 1;
dsym->block_v3.name[0] = 0;
int len = sizeof(dsym->block_v3);
for (; len & 3; len++)
@@ -701,6 +700,9 @@ bool CV2PDB::addDWARFProc(DWARF_InfoData& procid, DWARF_CompilationUnit* cu, DIE
checkUdtSymbolAlloc(100 + kMaxNameLen);
+ if (debug & DbgPdbSyms)
+ fprintf(stderr, "%s:%d: Adding a proc: %s at %x\n", __FUNCTION__, __LINE__, procid.name, pclo);
+
// GLOBALPROC
codeview_symbol*cvs = (codeview_symbol*) (udtSymbols + cbUdtSymbols);
cvs->proc_v2.id = v3 ? S_GPROC_V3 : S_GPROC_V2;
@@ -711,7 +713,7 @@ bool CV2PDB::addDWARFProc(DWARF_InfoData& procid, DWARF_CompilationUnit* cu, DIE
cvs->proc_v2.debug_start = pclo - pclo;
cvs->proc_v2.debug_end = pchi - pclo;
cvs->proc_v2.offset = pclo;
- cvs->proc_v2.segment = img.codeSegment + 1;
+ cvs->proc_v2.segment = img.text.secNo + 1;
cvs->proc_v2.proctype = 0; // translateType(sym->proc_v1.proctype);
cvs->proc_v2.flags = 0;
@@ -793,8 +795,8 @@ bool CV2PDB::addDWARFProc(DWARF_InfoData& procid, DWARF_CompilationUnit* cu, DIE
id.pchi = 0;
// TODO: handle base address selection
- byte *r = (byte *)img.debug_ranges + id.ranges;
- byte *rend = (byte *)img.debug_ranges + img.debug_ranges_length;
+ byte *r = img.debug_ranges.byteAt(id.ranges);
+ byte *rend = img.debug_ranges.endByte();
while (r < rend)
{
uint64_t pclo, pchi;
@@ -1122,7 +1124,7 @@ bool CV2PDB::addDWARFTypes()
checkUdtSymbolAlloc(100);
int prefix = 4;
- DWORD* ddata = new DWORD [img.debug_info_length/4]; // large enough
+ DWORD* ddata = new DWORD [img.debug_info.length/4]; // large enough
unsigned char *data = (unsigned char*) (ddata + prefix);
unsigned int off = 0;
unsigned int len;
@@ -1131,7 +1133,7 @@ bool CV2PDB::addDWARFTypes()
// SSEARCH
codeview_symbol* cvs = (codeview_symbol*) (data + off);
cvs->ssearch_v1.id = S_SSEARCH_V1;
- cvs->ssearch_v1.segment = img.codeSegment + 1;
+ cvs->ssearch_v1.segment = img.text.secNo + 1;
cvs->ssearch_v1.offset = 0;
len = sizeof(cvs->ssearch_v1);
for (; len & (align-1); len++)
@@ -1350,9 +1352,9 @@ bool CV2PDB::mapTypes()
{
int typeID = nextUserType;
unsigned long off = 0;
- while (off < img.debug_info_length)
+ while (off < img.debug_info.length)
{
- DWARF_CompilationUnit* cu = (DWARF_CompilationUnit*)(img.debug_info + off);
+ DWARF_CompilationUnit* cu = (DWARF_CompilationUnit*)img.debug_info.byteAt(off);
DIECursor cursor(cu, (byte*)cu + sizeof(DWARF_CompilationUnit));
DWARF_InfoData id;
@@ -1396,6 +1398,9 @@ bool CV2PDB::mapTypes()
off += sizeof(cu->unit_length) + cu->unit_length;
}
+ if (debug & DbgBasic)
+ fprintf(stderr, "%s:%d: mapped %zd types\n", __FUNCTION__, __LINE__, mapOffsetToType.size());
+
nextDwarfType = typeID;
return true;
}
@@ -1407,17 +1412,21 @@ bool CV2PDB::createTypes()
int typeID = nextUserType;
int pointerAttr = img.isX64() ? 0x1000C : 0x800A;
+ if (debug & DbgBasic)
+ fprintf(stderr, "%s:%d: createTypes()\n", __FUNCTION__, __LINE__);
+
unsigned long off = 0;
- while (off < img.debug_info_length)
+ while (off < img.debug_info.length)
{
- DWARF_CompilationUnit* cu = (DWARF_CompilationUnit*)(img.debug_info + off);
+ DWARF_CompilationUnit* cu = (DWARF_CompilationUnit*)img.debug_info.byteAt(off);
DIECursor cursor(cu, (byte*)cu + sizeof(DWARF_CompilationUnit));
DWARF_InfoData id;
while (cursor.readNext(id))
{
- //printf("0x%08x, level = %d, id.code = %d, id.tag = %d\n",
- // (unsigned char*)cu + id.entryOff - (unsigned char*)img.debug_info, cursor.level, id.code, id.tag);
+ if (debug & DbgDwarfTagRead)
+ fprintf(stderr, "%s:%d: 0x%08x, level = %d, id.code = %d, id.tag = %d\n", __FUNCTION__, __LINE__,
+ cursor.entryOff, cursor.level, id.code, id.tag);
if (id.abstract_origin)
mergeAbstractOrigin(id, cu);
@@ -1497,8 +1506,8 @@ bool CV2PDB::createTypes()
else if (id.ranges != ~0)
{
entry_point = ~0;
- byte* r = (byte*)img.debug_ranges + id.ranges;
- byte* rend = (byte*)img.debug_ranges + img.debug_ranges_length;
+ byte* r = (byte*)img.debug_ranges.byteAt(id.ranges);
+ byte* rend = (byte*)img.debug_ranges.endByte();
while (r < rend)
{
uint64_t pclo, pchi;
@@ -1524,7 +1533,12 @@ bool CV2PDB::createTypes()
}
if (entry_point)
- mod->AddPublic2(id.name, img.codeSegment + 1, entry_point - codeSegOff, 0);
+ {
+ if (debug & DbgPdbSyms)
+ fprintf(stderr, "%s:%d: Adding a public: %s at %x\n", __FUNCTION__, __LINE__, id.name, entry_point);
+
+ mod->AddPublic2(id.name, img.text.secNo + 1, entry_point - codeSegOff, 0);
+ }
}
if (id.pclo && id.pchi)
@@ -1555,10 +1569,10 @@ bool CV2PDB::createTypes()
#if !FULL_CONTRIB
if (id.dir && id.name)
{
- if (id.ranges > 0 && id.ranges < img.debug_ranges_length)
+ if (id.ranges > 0 && id.ranges < img.debug_ranges.length)
{
- unsigned char* r = (unsigned char*)img.debug_ranges + id.ranges;
- unsigned char* rend = (unsigned char*)img.debug_ranges + img.debug_ranges_length;
+ unsigned char* r = img.debug_ranges.byteAt(id.ranges);
+ unsigned char* rend = img.debug_ranges.endByte();
while (r < rend)
{
unsigned long pclo = RD4(r);
@@ -1644,10 +1658,10 @@ bool CV2PDB::createTypes()
bool CV2PDB::createDWARFModules()
{
- if(!img.debug_info)
+ if(!img.debug_info.isPresent())
return setError("no .debug_info section found");
- codeSegOff = img.getImageBase() + img.getSection(img.codeSegment).VirtualAddress;
+ codeSegOff = img.getImageBase() + img.getSection(img.text.secNo).VirtualAddress;
mspdb::Mod* mod = globalMod();
for (int s = 0; s < img.countSections(); s++)
@@ -1662,7 +1676,7 @@ bool CV2PDB::createDWARFModules()
#if FULL_CONTRIB
// we use a single global module, so we can simply add the whole text segment
int segFlags = 0x60101020; // 0x40401040, 0x60500020; // TODO
- int s = img.codeSegment;
+ int s = img.text.secNo;
int pclo = 0; // img.getImageBase() + img.getSection(s).VirtualAddress;
int pchi = pclo + img.getSection(s).Misc.VirtualSize;
int rc = mod->AddSecContrib(s + 1, pclo, pchi - pclo, segFlags);
@@ -1682,7 +1696,7 @@ bool CV2PDB::createDWARFModules()
appendComplex(0x52, 0x42, 12, "creal");
}
- DIECursor::setContext(&img);
+ DIECursor::setContext(&img, debug);
countEntries = 0;
if (!mapTypes())
@@ -1725,10 +1739,10 @@ bool CV2PDB::createDWARFModules()
bool CV2PDB::addDWARFLines()
{
- if(!img.debug_line)
+ if(!img.debug_line.isPresent())
return setError("no .debug_line section found");
- if (!interpretDWARFLines(img, globalMod()))
+ if (!interpretDWARFLines(img, globalMod(), debug))
return setError("cannot add line number info to module");
return true;
@@ -1739,7 +1753,7 @@ bool CV2PDB::addDWARFPublics()
mspdb::Mod* mod = globalMod();
int type = 0;
- int rc = mod->AddPublic2("public_all", img.codeSegment + 1, 0, 0x1000);
+ int rc = mod->AddPublic2("public_all", img.text.secNo + 1, 0, 0x1000);
if (rc <= 0)
return setError("cannot add public");
return true;
@@ -1759,7 +1773,7 @@ bool CV2PDB::writeDWARFImage(const TCHAR* opath)
void CV2PDB::build_cfi_index()
{
- if (img.debug_frame == NULL)
+ if (!img.debug_frame.isPresent())
return;
cfi_index = new CFIIndex(img);
}