<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 22, 2021 at 10:50 AM Alexander Yermolovich <<a href="mailto:ayermolo@fb.com">ayermolo@fb.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">




<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
Hello David.<br>
<br>
My apologies, let me provide some context. I am helping with BOLT binary optimizer (soon to be upstreamed). As part of its functionality it updates debug information to reflect the changes it had made to the binary. Moving functions around, extracting cold
 blocks, ICF, etc.<br>
Right now, it works with monolithic Debug information, but not with Fission one.<br>
<br>
It completely re-writes debug line, ranges/aranges, and patches relevant DIEs entries to point to new offsets within those sections. Which means finding what current addresses are in DIE, mapping them to new addresses and from that new offsets within sections.
 For debug fission it also will need to re-write .debug_addr and update indices that point to it.<br>
<br>
I looked at llvm-symbolizer and this seems a bit high level. </div></div></blockquote><div><br>It is, but somewhere down there it has to follow from executable to dwo/dwp files - that part of its implementation might be able to be reused (may benefit/require some refactoring to make it more reusable) for the purposes you have. I'd suggest looking there first, if you have a chance.<br><br>Perhaps that looks like refactoring llvm-symbolizer to use a codepath that looks like the ones you're already using, and making that work with dwos in a way that it doesn't already - or changing your code to more like some aspects of llvm-symbolizer's implementation and follow that codepath.<br><br>So llvm-symbolizer goes down through LLVMSymbolizer::symbolizeInlinedCodeCommon -> SymbolizableObjectFile::symbolizeInlinedCode -> DWARFContext::getInliningInfoForAddress<br><br>It looks like this code does correctly stitch together the addr and ranges tables in "parseDWO" (where it calls setAddrOffsetSection/setRangesSection).<br><br>But it sounds like you're trying to go from loading DWARFContext for dwo/dwp files directly, back to the skeleton/executable - it may be better to go forward instead of backwards? Load up the DWARFContext for the linked executable, then walk the (possibly skeleton) units there, and parseDWO/getDWO to walk into the split units - and those split units, loaded that way, should have their addr table working correctly due to the parseDWO code?<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">So usage model is closer to 1) I think.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
Right now there is no link, but one solution would be to add it, when <i style="font-size:12pt;font-variant-ligatures:inherit;font-variant-caps:inherit;font-weight:inherit">getNonSkeletonUnitDIE/parseDWO </i>is called. This reflects the code in getAddrOffsetSection
 that tries to parse normal CUs current DWARFUnit is DWO. I don't know what original intent of that code was, but as it stands, I don't think it works because it parses none relocated skeleton CU in A.o.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
Rough idea: *<br>
<a href="https://reviews.llvm.org/D96826" id="gmail-m_-5881933187940789829LPlnk980851" target="_blank">https://reviews.llvm.org/D96826</a><br>
<br>
Alternative, that whole code can be skipped entirely. *<br>
<a href="https://reviews.llvm.org/D96827" id="gmail-m_-5881933187940789829LPlnk573221" target="_blank">https://reviews.llvm.org/D96827</a><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
This works because in parseDWO we set AddrOffsetSectionBase, and AddrOffsetSection from .debug_addr in binary. Then in getAddrOffsetSectionItem we have all the information to get addresses from indices. One weird part is that DWARFDataExtractor is created with
 A.o file, while AddrOffsetSection is from A binary.<br>
<br>
The getAddrOffsetSectionItem is an important low level API. For example, it is also used by DWARFUnit::getLowandHighPC, along with DWARFDie::getLocations, DWARFUnit::findLocationLIstFromOffset. So, making a fix at that level, would make other more high-level
 APIs work for DWO contents.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
*Diffs are same ones as previously mentioned.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<br>
Alex</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
"</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0);background-color:rgb(255,255,255)">
<span style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt">Sorry I'm not really following all these pieces.</span>
<pre style="color:rgb(0,0,0)">There's two basic ways these APIs are predominantly used:

1) llvm-dwarfdump: This opens one file/context at a time, and generally
doesn't open other files - such as dwos or o/exe for skeleton. (indeed,
there's no reliable way to find a skeleton, given a dwo - only to find dwos
given skeletons)
2) llvm-symbolizer: this opens executable files (or .o files) and from
there can load dwo/dwp/dsym related files as needed

What sort of use case do you have? I guess it can/should look something
like (2) so can you use the LLVM debug info APIs in a similar manner to
llvm-symbolizer to achieve your goals?</pre>
"</div>
<div id="gmail-m_-5881933187940789829appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-5881933187940789829divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>><br>
<b>Sent:</b> Monday, February 15, 2021 10:09 PM<br>
<b>To:</b> Alexander Yermolovich <<a href="mailto:ayermolo@fb.com" target="_blank">ayermolo@fb.com</a>>; Pavel Labath <<a href="mailto:pavel@labath.sk" target="_blank">pavel@labath.sk</a>><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a> <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm-dev] Extracting LocList address ranges from DWO .debug_info</font>
<div> </div>
</div>
<div><font size="2"><span style="font-size:11pt">
<div>This stuff is a bit ad-hoc at best.<br>
<br>
I believe some of these APIs have been generalized enough to be usable<br>
for your use-case, but it might be at a lower level - specifically I<br>
think the loclist infrastructure is used by lldb when parsing DWARFv5.<br>
But it might be used without some of the LLVM DWARF Unit abstractions<br>
you're using. (those abstractions are used in llvm-dwarfdump - which<br>
often isn't dealing with both .o and .dwo, but only dumping one of the<br>
files & doing what it can (or sometimes dumping one file containing<br>
both sets of sections, in which case it can do some address lookup,<br>
etc, more conveniently))<br>
<br>
On Fri, Feb 12, 2021 at 6:07 PM Alexander Yermolovich via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Hello<br>
><br>
> I am wondering if this is a bug, or more likely something I am doing wrong/using wrong APIs.<br>
> I have binary A, and object file A.o, compiled with Clang debug fission single mode. So .dwo sections are in the object file. Although with split mode it would bre the same behavior.<br>
> Relevant parts of the code:<br>
> for (const auto &CU : DwCtx->compile_units()) {<br>
>       auto *const DwarfUnit = CU.get();<br>
>       if (llvm::Optional<uint64_t> DWOId = DwarfUnit->getDWOId()) {<br>
>         auto *CUDWO = static_cast<DWARFCompileUnit*>(DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit());<br>
>         ...<br>
>       }<br>
>   }<br>
><br>
> Later in the code I iterate over DIEs for .debug_info.dwo and call<br>
> DIE.getLocations(dwarf::DW_AT_location);<br>
><br>
> Alternatively can manually extract offset and call<br>
> CUnit->findLoclistFromOffset(Offset);<br>
><br>
> It fails because it tries to look up address using DWARFUnit in NormalUnits that it extracts from A.o.<br>
> Under the hood vistAsoluteLocationList is called with getAddrOffsetSectionItem passed in.<br>
> Since this DWARFUnit is DWO, it invokes Context.info_section_units(). Which uses A.o to create DW_SECT_INFO and DW_SECT_EXT_TYPES.<br>
> Then calls itself, but from the newly constructed Debug DWARFUnit. The skeleton CU that is in A.o.<br>
><br>
> Since the way it's constructed the AddrOffsetSectionBase is never set, so getAddrOffsetSectionItem returns None. Eventually error is returned from high level API call.<br>
><br>
> I ended up doing this to get address ranges:<br>
>           DWARFLocationExpressionsVector LocEVector;<br>
>           auto CallBack = [&](const DWARFLocationEntry &Entry) -> bool {<br>
>             auto StartAddress =<br>
>                 BaseUnit->getAddrOffsetSectionItem(Entry.Value0);<br>
>             if (!StartAddress) {<br>
>               //TODO: Handle Error<br>
>               return false;<br>
>             }<br>
>             LocEVector.emplace_back(DWARFLocationExpression{DWARFAddressRange{<br>
>                 (*StartAddress).Address, (*StartAddress).Address + Entry.Value1,<br>
>                 Entry.SectionIndex}, Entry.Loc});<br>
>             return true;<br>
>           };<br>
><br>
>           if(Unit->getLocationTable().visitLocationList(&Offset, CallBack))<br>
>             ...<br>
><br>
><br>
> But back to original API calls. Are they just not designed to work with DWO CUs, or am I missing something?<br>
><br>
> Even if AddrOffsetSectionBase was set to 0, the address section it is accessing is in A.o and is not relocated. One would still need to get base address from the address from Skeleton CU to get fully resolved address ranges, or what I did to use index to
 access binary .debug_addr section directly (with appropriate AddrOffsetSectionBase).<br>
><br>
> Thank You<br>
> Alex<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
<br>
</div>
</span></font></div>
</div>

</blockquote></div></div>