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