1 //===-- ProfiledBinary.cpp - Binary decoder ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ProfiledBinary.h"
10 #include "ErrorHandling.h"
11 #include "ProfileGenerator.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Demangle/Demangle.h"
14 #include "llvm/IR/DebugInfoMetadata.h"
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/TargetSelect.h"
19 
20 #define DEBUG_TYPE "load-binary"
21 
22 using namespace llvm;
23 using namespace sampleprof;
24 
25 cl::opt<bool> ShowDisassemblyOnly("show-disassembly-only", cl::init(false),
26                                   cl::ZeroOrMore,
27                                   cl::desc("Print disassembled code."));
28 
29 cl::opt<bool> ShowSourceLocations("show-source-locations", cl::init(false),
30                                   cl::ZeroOrMore,
31                                   cl::desc("Print source locations."));
32 
33 static cl::opt<bool>
34     ShowCanonicalFnName("show-canonical-fname", cl::init(false), cl::ZeroOrMore,
35                         cl::desc("Print canonical function name."));
36 
37 static cl::opt<bool> ShowPseudoProbe(
38     "show-pseudo-probe", cl::init(false), cl::ZeroOrMore,
39     cl::desc("Print pseudo probe section and disassembled info."));
40 
41 static cl::opt<bool> UseDwarfCorrelation(
42     "use-dwarf-correlation", cl::init(false), cl::ZeroOrMore,
43     cl::desc("Use dwarf for profile correlation even when binary contains "
44              "pseudo probe."));
45 
46 static cl::list<std::string> DisassembleFunctions(
47     "disassemble-functions", cl::CommaSeparated,
48     cl::desc("List of functions to print disassembly for. Accept demangled "
49              "names only. Only work with show-disassembly-only"));
50 
51 extern cl::opt<bool> ShowDetailedWarning;
52 
53 namespace llvm {
54 namespace sampleprof {
55 
56 static const Target *getTarget(const ObjectFile *Obj) {
57   Triple TheTriple = Obj->makeTriple();
58   std::string Error;
59   std::string ArchName;
60   const Target *TheTarget =
61       TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
62   if (!TheTarget)
63     exitWithError(Error, Obj->getFileName());
64   return TheTarget;
65 }
66 
67 void BinarySizeContextTracker::addInstructionForContext(
68     const SampleContextFrameVector &Context, uint32_t InstrSize) {
69   ContextTrieNode *CurNode = &RootContext;
70   bool IsLeaf = true;
71   for (const auto &Callsite : reverse(Context)) {
72     StringRef CallerName = Callsite.FuncName;
73     LineLocation CallsiteLoc = IsLeaf ? LineLocation(0, 0) : Callsite.Location;
74     CurNode = CurNode->getOrCreateChildContext(CallsiteLoc, CallerName);
75     IsLeaf = false;
76   }
77 
78   CurNode->addFunctionSize(InstrSize);
79 }
80 
81 uint32_t
82 BinarySizeContextTracker::getFuncSizeForContext(const SampleContext &Context) {
83   ContextTrieNode *CurrNode = &RootContext;
84   ContextTrieNode *PrevNode = nullptr;
85   SampleContextFrames Frames = Context.getContextFrames();
86   int32_t I = Frames.size() - 1;
87   Optional<uint32_t> Size;
88 
89   // Start from top-level context-less function, traverse down the reverse
90   // context trie to find the best/longest match for given context, then
91   // retrieve the size.
92 
93   while (CurrNode && I >= 0) {
94     // Process from leaf function to callers (added to context).
95     const auto &ChildFrame = Frames[I--];
96     PrevNode = CurrNode;
97     CurrNode =
98         CurrNode->getChildContext(ChildFrame.Location, ChildFrame.FuncName);
99     if (CurrNode && CurrNode->getFunctionSize().hasValue())
100       Size = CurrNode->getFunctionSize().getValue();
101   }
102 
103   // If we traversed all nodes along the path of the context and haven't
104   // found a size yet, pivot to look for size from sibling nodes, i.e size
105   // of inlinee under different context.
106   if (!Size.hasValue()) {
107     if (!CurrNode)
108       CurrNode = PrevNode;
109     while (!Size.hasValue() && CurrNode &&
110            !CurrNode->getAllChildContext().empty()) {
111       CurrNode = &CurrNode->getAllChildContext().begin()->second;
112       if (CurrNode->getFunctionSize().hasValue())
113         Size = CurrNode->getFunctionSize().getValue();
114     }
115   }
116 
117   assert(Size.hasValue() && "We should at least find one context size.");
118   return Size.getValue();
119 }
120 
121 void BinarySizeContextTracker::trackInlineesOptimizedAway(
122     MCPseudoProbeDecoder &ProbeDecoder) {
123   ProbeFrameStack ProbeContext;
124   for (const auto &Child : ProbeDecoder.getDummyInlineRoot().getChildren())
125     trackInlineesOptimizedAway(ProbeDecoder, *Child.second.get(), ProbeContext);
126 }
127 
128 void BinarySizeContextTracker::trackInlineesOptimizedAway(
129     MCPseudoProbeDecoder &ProbeDecoder,
130     MCDecodedPseudoProbeInlineTree &ProbeNode, ProbeFrameStack &ProbeContext) {
131   StringRef FuncName =
132       ProbeDecoder.getFuncDescForGUID(ProbeNode.Guid)->FuncName;
133   ProbeContext.emplace_back(FuncName, 0);
134 
135   // This ProbeContext has a probe, so it has code before inlining and
136   // optimization. Make sure we mark its size as known.
137   if (!ProbeNode.getProbes().empty()) {
138     ContextTrieNode *SizeContext = &RootContext;
139     for (auto &ProbeFrame : reverse(ProbeContext)) {
140       StringRef CallerName = ProbeFrame.first;
141       LineLocation CallsiteLoc(ProbeFrame.second, 0);
142       SizeContext =
143           SizeContext->getOrCreateChildContext(CallsiteLoc, CallerName);
144     }
145     // Add 0 size to make known.
146     SizeContext->addFunctionSize(0);
147   }
148 
149   // DFS down the probe inline tree
150   for (const auto &ChildNode : ProbeNode.getChildren()) {
151     InlineSite Location = ChildNode.first;
152     ProbeContext.back().second = std::get<1>(Location);
153     trackInlineesOptimizedAway(ProbeDecoder, *ChildNode.second.get(), ProbeContext);
154   }
155 
156   ProbeContext.pop_back();
157 }
158 
159 void ProfiledBinary::warnNoFuncEntry() {
160   uint64_t NoFuncEntryNum = 0;
161   for (auto &F : BinaryFunctions) {
162     if (F.second.Ranges.empty())
163       continue;
164     bool hasFuncEntry = false;
165     for (auto &R : F.second.Ranges) {
166       if (FuncRange *FR = findFuncRangeForStartOffset(R.first)) {
167         if (FR->IsFuncEntry) {
168           hasFuncEntry = true;
169           break;
170         }
171       }
172     }
173 
174     if (!hasFuncEntry) {
175       NoFuncEntryNum++;
176       if (ShowDetailedWarning)
177         WithColor::warning()
178             << "Failed to determine function entry for " << F.first
179             << " due to inconsistent name from symbol table and dwarf info.\n";
180     }
181   }
182   emitWarningSummary(NoFuncEntryNum, BinaryFunctions.size(),
183                      "of functions failed to determine function entry due to "
184                      "inconsistent name from symbol table and dwarf info.");
185 }
186 
187 void ProfiledBinary::load() {
188   // Attempt to open the binary.
189   OwningBinary<Binary> OBinary = unwrapOrError(createBinary(Path), Path);
190   Binary &Binary = *OBinary.getBinary();
191 
192   auto *Obj = dyn_cast<ELFObjectFileBase>(&Binary);
193   if (!Obj)
194     exitWithError("not a valid Elf image", Path);
195 
196   TheTriple = Obj->makeTriple();
197   // Current only support X86
198   if (!TheTriple.isX86())
199     exitWithError("unsupported target", TheTriple.getTriple());
200   LLVM_DEBUG(dbgs() << "Loading " << Path << "\n");
201 
202   // Find the preferred load address for text sections.
203   setPreferredTextSegmentAddresses(Obj);
204 
205   // Decode pseudo probe related section
206   decodePseudoProbe(Obj);
207 
208   // Load debug info of subprograms from DWARF section.
209   loadSymbolsFromDWARF(*dyn_cast<ObjectFile>(&Binary));
210 
211   // Disassemble the text sections.
212   disassemble(Obj);
213 
214   // Track size for optimized inlinees when probe is available
215   if (UsePseudoProbes && TrackFuncContextSize)
216     FuncSizeTracker.trackInlineesOptimizedAway(ProbeDecoder);
217 
218   // Use function start and return address to infer prolog and epilog
219   ProEpilogTracker.inferPrologOffsets(StartOffset2FuncRangeMap);
220   ProEpilogTracker.inferEpilogOffsets(RetOffsets);
221 
222   warnNoFuncEntry();
223 
224   // TODO: decode other sections.
225 }
226 
227 bool ProfiledBinary::inlineContextEqual(uint64_t Address1, uint64_t Address2) {
228   uint64_t Offset1 = virtualAddrToOffset(Address1);
229   uint64_t Offset2 = virtualAddrToOffset(Address2);
230   const SampleContextFrameVector &Context1 = getFrameLocationStack(Offset1);
231   const SampleContextFrameVector &Context2 = getFrameLocationStack(Offset2);
232   if (Context1.size() != Context2.size())
233     return false;
234   if (Context1.empty())
235     return false;
236   // The leaf frame contains location within the leaf, and it
237   // needs to be remove that as it's not part of the calling context
238   return std::equal(Context1.begin(), Context1.begin() + Context1.size() - 1,
239                     Context2.begin(), Context2.begin() + Context2.size() - 1);
240 }
241 
242 SampleContextFrameVector
243 ProfiledBinary::getExpandedContext(const SmallVectorImpl<uint64_t> &Stack,
244                                    bool &WasLeafInlined) {
245   SampleContextFrameVector ContextVec;
246   // Process from frame root to leaf
247   for (auto Address : Stack) {
248     uint64_t Offset = virtualAddrToOffset(Address);
249     const SampleContextFrameVector &ExpandedContext =
250         getFrameLocationStack(Offset);
251     // An instruction without a valid debug line will be ignored by sample
252     // processing
253     if (ExpandedContext.empty())
254       return SampleContextFrameVector();
255     // Set WasLeafInlined to the size of inlined frame count for the last
256     // address which is leaf
257     WasLeafInlined = (ExpandedContext.size() > 1);
258     ContextVec.append(ExpandedContext);
259   }
260 
261   // Replace with decoded base discriminator
262   for (auto &Frame : ContextVec) {
263     Frame.Location.Discriminator = ProfileGeneratorBase::getBaseDiscriminator(
264         Frame.Location.Discriminator);
265   }
266 
267   assert(ContextVec.size() && "Context length should be at least 1");
268 
269   // Compress the context string except for the leaf frame
270   auto LeafFrame = ContextVec.back();
271   LeafFrame.Location = LineLocation(0, 0);
272   ContextVec.pop_back();
273   CSProfileGenerator::compressRecursionContext(ContextVec);
274   CSProfileGenerator::trimContext(ContextVec);
275   ContextVec.push_back(LeafFrame);
276   return ContextVec;
277 }
278 
279 template <class ELFT>
280 void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj, StringRef FileName) {
281   const auto &PhdrRange = unwrapOrError(Obj.program_headers(), FileName);
282   // FIXME: This should be the page size of the system running profiling.
283   // However such info isn't available at post-processing time, assuming
284   // 4K page now. Note that we don't use EXEC_PAGESIZE from <linux/param.h>
285   // because we may build the tools on non-linux.
286   uint32_t PageSize = 0x1000;
287   for (const typename ELFT::Phdr &Phdr : PhdrRange) {
288     if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_flags & ELF::PF_X)) {
289         // Segments will always be loaded at a page boundary.
290         PreferredTextSegmentAddresses.push_back(Phdr.p_vaddr &
291                                                 ~(PageSize - 1U));
292         TextSegmentOffsets.push_back(Phdr.p_offset & ~(PageSize - 1U));
293       }
294   }
295 
296   if (PreferredTextSegmentAddresses.empty())
297     exitWithError("no executable segment found", FileName);
298 }
299 
300 void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFObjectFileBase *Obj) {
301   if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
302     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
303   else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
304     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
305   else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
306     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
307   else if (const auto *ELFObj = cast<ELF64BEObjectFile>(Obj))
308     setPreferredTextSegmentAddresses(ELFObj->getELFFile(), Obj->getFileName());
309   else
310     llvm_unreachable("invalid ELF object format");
311 }
312 
313 void ProfiledBinary::decodePseudoProbe(const ELFObjectFileBase *Obj) {
314   if (UseDwarfCorrelation)
315     return;
316 
317   StringRef FileName = Obj->getFileName();
318   for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
319        SI != SE; ++SI) {
320     const SectionRef &Section = *SI;
321     StringRef SectionName = unwrapOrError(Section.getName(), FileName);
322 
323     if (SectionName == ".pseudo_probe_desc") {
324       StringRef Contents = unwrapOrError(Section.getContents(), FileName);
325       if (!ProbeDecoder.buildGUID2FuncDescMap(
326               reinterpret_cast<const uint8_t *>(Contents.data()),
327               Contents.size()))
328         exitWithError("Pseudo Probe decoder fail in .pseudo_probe_desc section");
329     } else if (SectionName == ".pseudo_probe") {
330       StringRef Contents = unwrapOrError(Section.getContents(), FileName);
331       if (!ProbeDecoder.buildAddress2ProbeMap(
332               reinterpret_cast<const uint8_t *>(Contents.data()),
333               Contents.size()))
334         exitWithError("Pseudo Probe decoder fail in .pseudo_probe section");
335       // set UsePseudoProbes flag, used for PerfReader
336       UsePseudoProbes = true;
337     }
338   }
339 
340   if (ShowPseudoProbe)
341     ProbeDecoder.printGUID2FuncDescMap(outs());
342 }
343 
344 void ProfiledBinary::setIsFuncEntry(uint64_t Offset, StringRef RangeSymName) {
345   // Note that the start offset of each ELF section can be a non-function
346   // symbol, we need to binary search for the start of a real function range.
347   auto *FuncRange = findFuncRangeForOffset(Offset);
348   // Skip external function symbol.
349   if (!FuncRange)
350     return;
351 
352   // Set IsFuncEntry to ture if there is only one range in the function or the
353   // RangeSymName from ELF is equal to its DWARF-based function name.
354   if (FuncRange->Func->Ranges.size() == 1 ||
355       (!FuncRange->IsFuncEntry && FuncRange->getFuncName() == RangeSymName))
356     FuncRange->IsFuncEntry = true;
357 }
358 
359 bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
360                                         SectionSymbolsTy &Symbols,
361                                         const SectionRef &Section) {
362   std::size_t SE = Symbols.size();
363   uint64_t SectionOffset = Section.getAddress() - getPreferredBaseAddress();
364   uint64_t SectSize = Section.getSize();
365   uint64_t StartOffset = Symbols[SI].Addr - getPreferredBaseAddress();
366   uint64_t NextStartOffset =
367       (SI + 1 < SE) ? Symbols[SI + 1].Addr - getPreferredBaseAddress()
368                     : SectionOffset + SectSize;
369   setIsFuncEntry(StartOffset,
370                  FunctionSamples::getCanonicalFnName(Symbols[SI].Name));
371 
372   StringRef SymbolName =
373       ShowCanonicalFnName
374           ? FunctionSamples::getCanonicalFnName(Symbols[SI].Name)
375           : Symbols[SI].Name;
376   bool ShowDisassembly =
377       ShowDisassemblyOnly && (DisassembleFunctionSet.empty() ||
378                               DisassembleFunctionSet.count(SymbolName));
379   if (ShowDisassembly)
380     outs() << '<' << SymbolName << ">:\n";
381 
382   auto WarnInvalidInsts = [](uint64_t Start, uint64_t End) {
383     WithColor::warning() << "Invalid instructions at "
384                          << format("%8" PRIx64, Start) << " - "
385                          << format("%8" PRIx64, End) << "\n";
386   };
387 
388   uint64_t Offset = StartOffset;
389   // Size of a consecutive invalid instruction range starting from Offset -1
390   // backwards.
391   uint64_t InvalidInstLength = 0;
392   while (Offset < NextStartOffset) {
393     MCInst Inst;
394     uint64_t Size;
395     // Disassemble an instruction.
396     bool Disassembled =
397         DisAsm->getInstruction(Inst, Size, Bytes.slice(Offset - SectionOffset),
398                                Offset + getPreferredBaseAddress(), nulls());
399     if (Size == 0)
400       Size = 1;
401 
402     if (ShowDisassembly) {
403       if (ShowPseudoProbe) {
404         ProbeDecoder.printProbeForAddress(outs(),
405                                           Offset + getPreferredBaseAddress());
406       }
407       outs() << format("%8" PRIx64 ":", Offset + getPreferredBaseAddress());
408       size_t Start = outs().tell();
409       if (Disassembled)
410         IPrinter->printInst(&Inst, Offset + Size, "", *STI.get(), outs());
411       else
412         outs() << "\t<unknown>";
413       if (ShowSourceLocations) {
414         unsigned Cur = outs().tell() - Start;
415         if (Cur < 40)
416           outs().indent(40 - Cur);
417         InstructionPointer IP(this, Offset);
418         outs() << getReversedLocWithContext(
419             symbolize(IP, ShowCanonicalFnName, ShowPseudoProbe));
420       }
421       outs() << "\n";
422     }
423 
424     if (Disassembled) {
425       const MCInstrDesc &MCDesc = MII->get(Inst.getOpcode());
426 
427       // Record instruction size.
428       Offset2InstSizeMap[Offset] = Size;
429 
430       // Populate address maps.
431       CodeAddrOffsets.push_back(Offset);
432       if (MCDesc.isCall())
433         CallOffsets.insert(Offset);
434       else if (MCDesc.isReturn())
435         RetOffsets.insert(Offset);
436       else if (MCDesc.isBranch())
437         BranchOffsets.insert(Offset);
438 
439       if (InvalidInstLength) {
440         WarnInvalidInsts(Offset - InvalidInstLength, Offset - 1);
441         InvalidInstLength = 0;
442       }
443     } else {
444       InvalidInstLength += Size;
445     }
446 
447     Offset += Size;
448   }
449 
450   if (InvalidInstLength)
451     WarnInvalidInsts(Offset - InvalidInstLength, Offset - 1);
452 
453   if (ShowDisassembly)
454     outs() << "\n";
455 
456   return true;
457 }
458 
459 void ProfiledBinary::setUpDisassembler(const ELFObjectFileBase *Obj) {
460   const Target *TheTarget = getTarget(Obj);
461   std::string TripleName = TheTriple.getTriple();
462   StringRef FileName = Obj->getFileName();
463 
464   MRI.reset(TheTarget->createMCRegInfo(TripleName));
465   if (!MRI)
466     exitWithError("no register info for target " + TripleName, FileName);
467 
468   MCTargetOptions MCOptions;
469   AsmInfo.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
470   if (!AsmInfo)
471     exitWithError("no assembly info for target " + TripleName, FileName);
472 
473   SubtargetFeatures Features = Obj->getFeatures();
474   STI.reset(
475       TheTarget->createMCSubtargetInfo(TripleName, "", Features.getString()));
476   if (!STI)
477     exitWithError("no subtarget info for target " + TripleName, FileName);
478 
479   MII.reset(TheTarget->createMCInstrInfo());
480   if (!MII)
481     exitWithError("no instruction info for target " + TripleName, FileName);
482 
483   MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
484   std::unique_ptr<MCObjectFileInfo> MOFI(
485       TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
486   Ctx.setObjectFileInfo(MOFI.get());
487   DisAsm.reset(TheTarget->createMCDisassembler(*STI, Ctx));
488   if (!DisAsm)
489     exitWithError("no disassembler for target " + TripleName, FileName);
490 
491   MIA.reset(TheTarget->createMCInstrAnalysis(MII.get()));
492 
493   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
494   IPrinter.reset(TheTarget->createMCInstPrinter(
495       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
496   IPrinter->setPrintBranchImmAsAddress(true);
497 }
498 
499 void ProfiledBinary::disassemble(const ELFObjectFileBase *Obj) {
500   // Set up disassembler and related components.
501   setUpDisassembler(Obj);
502 
503   // Create a mapping from virtual address to symbol name. The symbols in text
504   // sections are the candidates to dissassemble.
505   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
506   StringRef FileName = Obj->getFileName();
507   for (const SymbolRef &Symbol : Obj->symbols()) {
508     const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
509     const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
510     section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
511     if (SecI != Obj->section_end())
512       AllSymbols[*SecI].push_back(SymbolInfoTy(Addr, Name, ELF::STT_NOTYPE));
513   }
514 
515   // Sort all the symbols. Use a stable sort to stabilize the output.
516   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
517     stable_sort(SecSyms.second);
518 
519   DisassembleFunctionSet.insert(DisassembleFunctions.begin(),
520                                 DisassembleFunctions.end());
521   assert((DisassembleFunctionSet.empty() || ShowDisassemblyOnly) &&
522          "Functions to disassemble should be only specified together with "
523          "--show-disassembly-only");
524 
525   if (ShowDisassemblyOnly)
526     outs() << "\nDisassembly of " << FileName << ":\n";
527 
528   // Dissassemble a text section.
529   for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
530        SI != SE; ++SI) {
531     const SectionRef &Section = *SI;
532     if (!Section.isText())
533       continue;
534 
535     uint64_t ImageLoadAddr = getPreferredBaseAddress();
536     uint64_t SectionOffset = Section.getAddress() - ImageLoadAddr;
537     uint64_t SectSize = Section.getSize();
538     if (!SectSize)
539       continue;
540 
541     // Register the text section.
542     TextSections.insert({SectionOffset, SectSize});
543 
544     if (ShowDisassemblyOnly) {
545       StringRef SectionName = unwrapOrError(Section.getName(), FileName);
546       outs() << "\nDisassembly of section " << SectionName;
547       outs() << " [" << format("0x%" PRIx64, Section.getAddress()) << ", "
548              << format("0x%" PRIx64, Section.getAddress() + SectSize)
549              << "]:\n\n";
550     }
551 
552     // Get the section data.
553     ArrayRef<uint8_t> Bytes =
554         arrayRefFromStringRef(unwrapOrError(Section.getContents(), FileName));
555 
556     // Get the list of all the symbols in this section.
557     SectionSymbolsTy &Symbols = AllSymbols[Section];
558 
559     // Disassemble symbol by symbol.
560     for (std::size_t SI = 0, SE = Symbols.size(); SI != SE; ++SI) {
561       if (!dissassembleSymbol(SI, Bytes, Symbols, Section))
562         exitWithError("disassembling error", FileName);
563     }
564   }
565 }
566 
567 void ProfiledBinary::loadSymbolsFromDWARF(ObjectFile &Obj) {
568   auto DebugContext = llvm::DWARFContext::create(Obj);
569   if (!DebugContext)
570     exitWithError("Misssing debug info.", Path);
571 
572   for (const auto &CompilationUnit : DebugContext->compile_units()) {
573     for (const auto &DieInfo : CompilationUnit->dies()) {
574       llvm::DWARFDie Die(CompilationUnit.get(), &DieInfo);
575 
576       if (!Die.isSubprogramDIE())
577         continue;
578       auto Name = Die.getName(llvm::DINameKind::LinkageName);
579       if (!Name)
580         Name = Die.getName(llvm::DINameKind::ShortName);
581       if (!Name)
582         continue;
583 
584       auto RangesOrError = Die.getAddressRanges();
585       if (!RangesOrError)
586         continue;
587       const DWARFAddressRangesVector &Ranges = RangesOrError.get();
588 
589       if (Ranges.empty())
590         continue;
591 
592       // Different DWARF symbols can have same function name, search or create
593       // BinaryFunction indexed by the name.
594       auto Ret = BinaryFunctions.emplace(Name, BinaryFunction());
595       auto &Func = Ret.first->second;
596       if (Ret.second)
597         Func.FuncName = Ret.first->first;
598 
599       for (const auto &Range : Ranges) {
600         uint64_t FuncStart = Range.LowPC;
601         uint64_t FuncSize = Range.HighPC - FuncStart;
602 
603         if (FuncSize == 0 || FuncStart < getPreferredBaseAddress())
604           continue;
605 
606         uint64_t StartOffset = FuncStart - getPreferredBaseAddress();
607         uint64_t EndOffset = Range.HighPC - getPreferredBaseAddress();
608 
609         // We may want to know all ranges for one function. Here group the
610         // ranges and store them into BinaryFunction.
611         Func.Ranges.emplace_back(StartOffset, EndOffset);
612 
613         auto R = StartOffset2FuncRangeMap.emplace(StartOffset, FuncRange());
614         if (R.second) {
615           FuncRange &FRange = R.first->second;
616           FRange.Func = &Func;
617           FRange.StartOffset = StartOffset;
618           FRange.EndOffset = EndOffset;
619         } else {
620           WithColor::warning()
621               << "Duplicated symbol start address at "
622               << format("%8" PRIx64, StartOffset + getPreferredBaseAddress())
623               << " " << R.first->second.getFuncName() << " and " << Name
624               << "\n";
625         }
626       }
627     }
628   }
629   assert(!StartOffset2FuncRangeMap.empty() && "Misssing debug info.");
630 }
631 
632 void ProfiledBinary::populateSymbolListFromDWARF(
633     ProfileSymbolList &SymbolList) {
634   for (auto &I : StartOffset2FuncRangeMap)
635     SymbolList.add(I.second.getFuncName());
636 }
637 
638 void ProfiledBinary::setupSymbolizer() {
639   symbolize::LLVMSymbolizer::Options SymbolizerOpts;
640   SymbolizerOpts.PrintFunctions =
641       DILineInfoSpecifier::FunctionNameKind::LinkageName;
642   SymbolizerOpts.Demangle = false;
643   SymbolizerOpts.DefaultArch = TheTriple.getArchName().str();
644   SymbolizerOpts.UseSymbolTable = false;
645   SymbolizerOpts.RelativeAddresses = false;
646   Symbolizer = std::make_unique<symbolize::LLVMSymbolizer>(SymbolizerOpts);
647 }
648 
649 SampleContextFrameVector ProfiledBinary::symbolize(const InstructionPointer &IP,
650                                                    bool UseCanonicalFnName,
651                                                    bool UseProbeDiscriminator) {
652   assert(this == IP.Binary &&
653          "Binary should only symbolize its own instruction");
654   auto Addr = object::SectionedAddress{IP.Offset + getPreferredBaseAddress(),
655                                        object::SectionedAddress::UndefSection};
656   DIInliningInfo InlineStack =
657       unwrapOrError(Symbolizer->symbolizeInlinedCode(Path, Addr), getName());
658 
659   SampleContextFrameVector CallStack;
660   for (int32_t I = InlineStack.getNumberOfFrames() - 1; I >= 0; I--) {
661     const auto &CallerFrame = InlineStack.getFrame(I);
662     if (CallerFrame.FunctionName == "<invalid>")
663       break;
664 
665     StringRef FunctionName(CallerFrame.FunctionName);
666     if (UseCanonicalFnName)
667       FunctionName = FunctionSamples::getCanonicalFnName(FunctionName);
668 
669     uint32_t Discriminator = CallerFrame.Discriminator;
670     uint32_t LineOffset = CallerFrame.Line - CallerFrame.StartLine;
671     if (UseProbeDiscriminator) {
672       LineOffset =
673           PseudoProbeDwarfDiscriminator::extractProbeIndex(Discriminator);
674       Discriminator = 0;
675     } else {
676       // Filter out invalid negative(int type) lineOffset
677       if (LineOffset & 0xffff0000)
678         return SampleContextFrameVector();
679     }
680 
681     LineLocation Line(LineOffset, Discriminator);
682     auto It = NameStrings.insert(FunctionName.str());
683     CallStack.emplace_back(*It.first, Line);
684   }
685 
686   return CallStack;
687 }
688 
689 void ProfiledBinary::computeInlinedContextSizeForRange(uint64_t StartOffset,
690                                                        uint64_t EndOffset) {
691   uint64_t RangeBegin = offsetToVirtualAddr(StartOffset);
692   uint64_t RangeEnd = offsetToVirtualAddr(EndOffset);
693   InstructionPointer IP(this, RangeBegin, true);
694 
695   if (IP.Address != RangeBegin)
696     WithColor::warning() << "Invalid start instruction at "
697                          << format("%8" PRIx64, RangeBegin) << "\n";
698 
699   if (IP.Address >= RangeEnd)
700     return;
701 
702   do {
703     uint64_t Offset = virtualAddrToOffset(IP.Address);
704     const SampleContextFrameVector &SymbolizedCallStack =
705         getFrameLocationStack(Offset, UsePseudoProbes);
706     uint64_t Size = Offset2InstSizeMap[Offset];
707 
708     // Record instruction size for the corresponding context
709     FuncSizeTracker.addInstructionForContext(SymbolizedCallStack, Size);
710 
711   } while (IP.advance() && IP.Address < RangeEnd);
712 }
713 
714 InstructionPointer::InstructionPointer(const ProfiledBinary *Binary,
715                                        uint64_t Address, bool RoundToNext)
716     : Binary(Binary), Address(Address) {
717   Index = Binary->getIndexForAddr(Address);
718   if (RoundToNext) {
719     // we might get address which is not the code
720     // it should round to the next valid address
721     if (Index >= Binary->getCodeOffsetsSize())
722       this->Address = UINT64_MAX;
723     else
724       this->Address = Binary->getAddressforIndex(Index);
725   }
726 }
727 
728 bool InstructionPointer::advance() {
729   Index++;
730   if (Index >= Binary->getCodeOffsetsSize()) {
731     Address = UINT64_MAX;
732     return false;
733   }
734   Address = Binary->getAddressforIndex(Index);
735   return true;
736 }
737 
738 bool InstructionPointer::backward() {
739   if (Index == 0) {
740     Address = 0;
741     return false;
742   }
743   Index--;
744   Address = Binary->getAddressforIndex(Index);
745   return true;
746 }
747 
748 void InstructionPointer::update(uint64_t Addr) {
749   Address = Addr;
750   Index = Binary->getIndexForAddr(Address);
751 }
752 
753 } // end namespace sampleprof
754 } // end namespace llvm
755